From 176d9661595c3f4fdba1247a6814cbcf67a4ad0b Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sat, 27 Apr 2019 01:36:58 +0800 Subject: [PATCH] =?UTF-8?q?Shader=E5=A2=9E=E5=8A=A0UBO=20binding=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E5=92=8C=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Vulkan/CMakeLists.txt | 2 +- example/Vulkan/FlatColor.vert | 8 ++++---- example/Vulkan/VKMaterial.cpp | 10 +++++++--- example/Vulkan/VKMaterial.h | 17 +++++++---------- example/Vulkan/VKShader.cpp | 8 ++++++++ example/Vulkan/VKShader.h | 3 +++ example/Vulkan/main.cpp | 16 ++++++++-------- 7 files changed, 38 insertions(+), 26 deletions(-) diff --git a/example/Vulkan/CMakeLists.txt b/example/Vulkan/CMakeLists.txt index 515921cc..bb2b1d2b 100644 --- a/example/Vulkan/CMakeLists.txt +++ b/example/Vulkan/CMakeLists.txt @@ -26,7 +26,7 @@ SET(VULKAN_TEST_SOURCE_FILES main.cpp VKSemaphore.cpp VKFramebuffer.cpp VKFence.cpp - VKMaterial.cpp + #VKMaterial.cpp ) SET(VULKAN_TEST_HEADER_FILES VK.h diff --git a/example/Vulkan/FlatColor.vert b/example/Vulkan/FlatColor.vert index f1325560..91e33c10 100644 --- a/example/Vulkan/FlatColor.vert +++ b/example/Vulkan/FlatColor.vert @@ -3,10 +3,10 @@ layout(location = 0) in vec2 Vertex; layout(location = 1) in vec3 Color; -layout(binding = 0) uniform MVPMatrix +layout(binding = 0) uniform WorldConfig { - mat4 MVP; -} ubo; + mat4 mvp; +} world; layout(location = 0) out vec4 FragmentColor; @@ -14,5 +14,5 @@ void main() { FragmentColor=vec4(Color,1.0); - gl_Position=vec4(Vertex,0.0,1.0)*ubo.MVP; + gl_Position=vec4(Vertex,0.0,1.0)*world.mvp; } diff --git a/example/Vulkan/VKMaterial.cpp b/example/Vulkan/VKMaterial.cpp index 968238b5..15c516ae 100644 --- a/example/Vulkan/VKMaterial.cpp +++ b/example/Vulkan/VKMaterial.cpp @@ -5,19 +5,23 @@ VK_NAMESPACE_BEGIN Material::~Material() { - delete dsl_creater; delete shader; } MaterialInstance *Material::CreateInstance() { - VertexAttributeBinding *vab=shader->CreateVertexAttributeBinding(); + return(new MaterialInstance(this)); +} - return(new MaterialInstance(this,vab)); +MaterialInstance::MaterialInstance(Material *m) +{ + mat=m; + vab=->CreateVertexAttributeBinding(); } MaterialInstance::~MaterialInstance() { delete vab; + delete dsl_creater; } VK_NAMESPACE_END diff --git a/example/Vulkan/VKMaterial.h b/example/Vulkan/VKMaterial.h index 875e9e9c..b8644937 100644 --- a/example/Vulkan/VKMaterial.h +++ b/example/Vulkan/VKMaterial.h @@ -3,6 +3,7 @@ #include"VK.h" VK_NAMESPACE_BEGIN +class Device; class Shader; class DescriptorSetLayoutCreater; class MaterialInstance; @@ -14,15 +15,15 @@ class VertexAttributeBinding; */ class Material { + Device *device; Shader *shader; - DescriptorSetLayoutCreater *dsl_creater; public: - Material(Shader *s,DescriptorSetLayoutCreater *dslc) + Material(Device *dev,Shader *s) { + device=dev; shader=s; - dsl_creater=dslc; } ~Material(); @@ -36,16 +37,12 @@ public: class MaterialInstance { const Material *mat; ///<这里的是对material的完全引用,不做任何修改 - VertexAttributeBinding *vab; ///<这里的vis是Material中vis的复制体 + VertexAttributeBinding *vab; + DescriptorSetLayoutCreater *dsl_creater; public: - MaterialInstance(Material *m,VertexAttributeBinding *vi) - { - mat=m; - vab=vi; - } - + MaterialInstance(Material *m); ~MaterialInstance(); };//class MaterialInstance VK_NAMESPACE_END diff --git a/example/Vulkan/VKShader.cpp b/example/Vulkan/VKShader.cpp index 8d70ea1e..99a8aea7 100644 --- a/example/Vulkan/VKShader.cpp +++ b/example/Vulkan/VKShader.cpp @@ -81,6 +81,14 @@ bool Shader::ParseVertexShader(const void *spv_data,const uint32_t spv_size) ++binding_index; } + for(auto &ub:res.uniform_buffers) + { + const UTF8String & name =comp.get_name(ub.id).c_str(); + const uint binding =comp.get_decoration(ub.id,spv::DecorationBinding); + + ubo_list.Add(name,binding); + } + return(true); } diff --git a/example/Vulkan/VKShader.h b/example/Vulkan/VKShader.h index 2cab9e52..abe37d88 100644 --- a/example/Vulkan/VKShader.h +++ b/example/Vulkan/VKShader.h @@ -22,6 +22,7 @@ private: VkVertexInputAttributeDescription *attribute_list; Map stage_input_locations; + Map ubo_list; Set vab_sets; @@ -61,6 +62,8 @@ public: //shader部分 const uint32_t GetStageCount ()const{return shader_stage_list.GetCount();} const VkPipelineShaderStageCreateInfo * GetShaderStages ()const{return shader_stage_list.GetData();} + const int GetUBO(const UTF8String &name)const{int binding;if(ubo_list.Get(name,binding))return binding;else return -1;} + public: //Vertex Input部分 VertexAttributeBinding * CreateVertexAttributeBinding(); diff --git a/example/Vulkan/main.cpp b/example/Vulkan/main.cpp index a13b2454..f72b0a0d 100644 --- a/example/Vulkan/main.cpp +++ b/example/Vulkan/main.cpp @@ -29,10 +29,10 @@ constexpr uint32_t SCREEN_HEIGHT=720; VkShaderModule vs=nullptr; VkShaderModule fs=nullptr; -struct +struct WorldConfig { Matrix4f mvp; -}ubo_vs; +}world; char *LoadFile(const char *filename,uint32_t &file_length) { @@ -86,16 +86,16 @@ vulkan::Buffer *CreateUBO(vulkan::Device *dev) { const VkExtent2D extent=dev->GetExtent(); - ubo_vs.mvp=ortho(extent.width,extent.height); + world.mvp=ortho(extent.width,extent.height); } - vulkan::Buffer *ubo=dev->CreateUBO(sizeof(ubo_vs)); + vulkan::Buffer *ubo=dev->CreateUBO(sizeof(WorldConfig)); uint8_t *p=ubo->Map(); if(p) { - memcpy(p,&ubo_vs,sizeof(ubo_vs)); + memcpy(p,&world,sizeof(WorldConfig)); ubo->Unmap(); } @@ -196,13 +196,13 @@ int main(int,char **) vulkan::DescriptorSetLayoutCreater dslc(device); - const int mvp_binding=0; //shader->GetBinding("MVPMatrix"); 实现从材质shader文件中根据名称获取binding的功能 + const int ubo_world_config=shader->GetUBO("world"); - dslc.BindUBO(mvp_binding,VK_SHADER_STAGE_VERTEX_BIT); + dslc.BindUBO(ubo_world_config,VK_SHADER_STAGE_VERTEX_BIT); vulkan::DescriptorSetLayout *dsl=dslc.Create(); - dsl->UpdateUBO(mvp_binding,*ubo); + dsl->UpdateUBO(ubo_world_config,*ubo); vulkan::PipelineLayout *pl=CreatePipelineLayout(*device,dsl);