From 339344fb30e20ca59ec3828b4836aa6a8ff247f4 Mon Sep 17 00:00:00 2001 From: HuYingzhuo Date: Fri, 19 Apr 2019 21:49:59 +0800 Subject: [PATCH] =?UTF-8?q?VertexInput::Add=E5=A2=9E=E5=8A=A0location?= =?UTF-8?q?=E4=BC=A0=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Vulkan/VKVertexInput.cpp | 4 +-- example/Vulkan/VKVertexInput.h | 2 +- example/Vulkan/main.cpp | 50 ++++++++++++++++++++++++++++---- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/example/Vulkan/VKVertexInput.cpp b/example/Vulkan/VKVertexInput.cpp index b79338f5..a756c878 100644 --- a/example/Vulkan/VKVertexInput.cpp +++ b/example/Vulkan/VKVertexInput.cpp @@ -2,7 +2,7 @@ #include"VKBuffer.h" VK_NAMESPACE_BEGIN -bool VertexInput::Add(VertexBuffer *buf,bool instance) +bool VertexInput::Add(uint32_t location,VertexBuffer *buf,bool instance) { if(!buf) return(false); @@ -17,7 +17,7 @@ bool VertexInput::Add(VertexBuffer *buf,bool instance) binding.inputRate=instance?VK_VERTEX_INPUT_RATE_INSTANCE:VK_VERTEX_INPUT_RATE_VERTEX; attrib.binding=binding_index; - attrib.location=0; + attrib.location=location; attrib.format=buf->GetFormat(); attrib.offset=0; diff --git a/example/Vulkan/VKVertexInput.h b/example/Vulkan/VKVertexInput.h index be368ca9..75f19e90 100644 --- a/example/Vulkan/VKVertexInput.h +++ b/example/Vulkan/VKVertexInput.h @@ -40,7 +40,7 @@ public: VertexInput()=default; virtual ~VertexInput()=default; - bool Add(VertexBuffer *,bool instance=false); + bool Add(uint32_t location,VertexBuffer *,bool instance=false); public: diff --git a/example/Vulkan/main.cpp b/example/Vulkan/main.cpp index 6a647f98..da2356a4 100644 --- a/example/Vulkan/main.cpp +++ b/example/Vulkan/main.cpp @@ -12,6 +12,7 @@ #include"VKCommandBuffer.h" #include"VKFence.h" #include"VKSemaphore.h" +#include"VKFormat.h" #include #include @@ -71,6 +72,41 @@ vulkan::Shader *LoadShader(VkDevice device) return(nullptr); } +constexpr float vertex_data[]={0.0f,0.5f, -0.5f,-0.5f, 0.5f,-0.5f }; +constexpr float color_data[]={1,0,0, 0,1,0, 0,0,1 }; + +vulkan::VertexInput *CreateVertexBuffer(vulkan::Device *dev) +{ + vulkan::VertexBuffer *vb=dev->CreateVBO(FMT_RG32F,6*sizeof(float)); + vulkan::VertexBuffer *cb=dev->CreateVBO(FMT_RGB32F,9*sizeof(float)); + + { + float *p=(float *)vb->Map(); + + memcpy(p,vertex_data,6*sizeof(float)); + + vb->Unmap(); + } + + { + float *p=(float *)cb->Map(); + + memcpy(p,color_data,9*sizeof(float)); + + cb->Unmap(); + } + + vulkan::VertexInput *vi=new vulkan::VertexInput(); + + constexpr uint32_t position_shader_location=0; //对应shader中的layout(locaiton=0,暂时这样写 + constexpr uint32_t color_shader_location=1; + + vi->Add(position_shader_location, vb); + vi->Add(color_shader_location, cb); + + return vi; +} + int main(int,char **) { #ifdef _DEBUG @@ -125,7 +161,8 @@ int main(int,char **) vulkan::Fence *fence=device->CreateFence(); vulkan::Semaphore *sem=device->CreateSem(); - vulkan::VertexInput vi; + vulkan::VertexInput *vi=CreateVertexBuffer(device); + vulkan::PipelineCreater pc(device); vulkan::RenderPass *rp=device->CreateRenderPass(); @@ -134,22 +171,23 @@ int main(int,char **) vulkan::PipelineLayout *pl=CreatePipelineLayout(device->GetDevice(),dsl); pc.Set(shader); - pc.Set(&vi); + pc.Set(vi); pc.Set(PRIM_TRIANGLES); pc.Set(*pl); pc.Set(*rp); vulkan::Pipeline *pipeline=pc.Create(); - if(pipeline) - { - delete pipeline; - } + if(!pipeline) + return(-4); + + delete pipeline; delete sem; delete fence; delete rp; + delete vi; delete ubo; delete cmd_buf;