From 3bed2806bd13c8b998f687a1016a955a39ba55ea Mon Sep 17 00:00:00 2001 From: HuYingzhuo Date: Fri, 26 Apr 2019 22:39:27 +0800 Subject: [PATCH] =?UTF-8?q?VAB=E6=8B=86=E5=88=86=E5=88=B0=E7=8B=AC?= =?UTF-8?q?=E7=AB=8B=E7=9A=84=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Vulkan/CMakeLists.txt | 2 + example/Vulkan/VKVertexAttributeBinding.cpp | 53 +++++++++++++++++++++ example/Vulkan/VKVertexAttributeBinding.h | 36 ++++++++++++++ example/Vulkan/VKVertexInput.cpp | 49 ------------------- example/Vulkan/main.cpp | 2 +- inc/hgl/TypeFunc.h | 18 +++++-- 6 files changed, 106 insertions(+), 54 deletions(-) create mode 100644 example/Vulkan/VKVertexAttributeBinding.cpp create mode 100644 example/Vulkan/VKVertexAttributeBinding.h diff --git a/example/Vulkan/CMakeLists.txt b/example/Vulkan/CMakeLists.txt index 2c8aa09f..515921cc 100644 --- a/example/Vulkan/CMakeLists.txt +++ b/example/Vulkan/CMakeLists.txt @@ -20,6 +20,7 @@ SET(VULKAN_TEST_SOURCE_FILES main.cpp VKPipelineLayout.cpp VKRenderPass.cpp VKShader.cpp + VKVertexAttributeBinding.cpp VKVertexInput.cpp VKPipeline.cpp VKSemaphore.cpp @@ -44,6 +45,7 @@ SET(VULKAN_TEST_HEADER_FILES VK.h VKRenderPass.h VKShader.h VKVertexInput.h + VKVertexAttributeBinding.h VKSemaphore.h VKPipeline.h VKFramebuffer.h diff --git a/example/Vulkan/VKVertexAttributeBinding.cpp b/example/Vulkan/VKVertexAttributeBinding.cpp new file mode 100644 index 00000000..94abeab8 --- /dev/null +++ b/example/Vulkan/VKVertexAttributeBinding.cpp @@ -0,0 +1,53 @@ +#include"VKVertexAttributeBinding.h" +#include"VKShader.h" + +VK_NAMESPACE_BEGIN +VertexAttributeBinding::VertexAttributeBinding(Shader *s) +{ + shader=s; + + const int count=shader->GetAttrCount(); + + if(count<=0) + { + binding_list=nullptr; + return; + } + + binding_list=hgl_copy_new(count,shader->GetDescList()); +} + +VertexAttributeBinding::~VertexAttributeBinding() +{ + delete[] binding_list; + + shader->Release(this); +} + +bool VertexAttributeBinding::SetInstance(const uint index,bool instance) +{ + if(index>=shader->GetAttrCount())return(false); + + binding_list[index].inputRate=instance?VK_VERTEX_INPUT_RATE_INSTANCE:VK_VERTEX_INPUT_RATE_VERTEX; + + return(true); +} + +bool VertexAttributeBinding::SetInstance(const UTF8String &name,bool instance) +{ + return SetInstance(shader->GetBinding(name),instance); +} + +void VertexAttributeBinding::Write(VkPipelineVertexInputStateCreateInfo &vis_create_info) const +{ + vis_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; + + const uint32_t count=shader->GetAttrCount(); + + vis_create_info.vertexBindingDescriptionCount = count; + vis_create_info.pVertexBindingDescriptions = binding_list; + + vis_create_info.vertexAttributeDescriptionCount = count; + vis_create_info.pVertexAttributeDescriptions = shader->GetAttrList(); +} +VK_NAMESPACE_END diff --git a/example/Vulkan/VKVertexAttributeBinding.h b/example/Vulkan/VKVertexAttributeBinding.h new file mode 100644 index 00000000..fd6b3a53 --- /dev/null +++ b/example/Vulkan/VKVertexAttributeBinding.h @@ -0,0 +1,36 @@ +#ifndef HGL_GRAPH_VULKAN_VERTEX_ATTRIBUTE_BINDING_INCLUDE +#define HGL_GRAPH_VULKAN_VERTEX_ATTRIBUTE_BINDING_INCLUDE + +#include"VK.h" +#include +VK_NAMESPACE_BEGIN +class VertexBuffer; +class IndexBuffer; +class Shader; + +/** +* 顶点输入状态实例
+* 本对象用于传递给MaterialInstance,用于已经确定好顶点格式的情况下,依然可修改部分设定(如instance)。 +*/ +class VertexAttributeBinding +{ + Shader *shader; + VkVertexInputBindingDescription *binding_list; + +private: + + friend class Shader; + + VertexAttributeBinding(Shader *); + +public: + + ~VertexAttributeBinding(); + + bool SetInstance(const uint index,bool instance); + bool SetInstance(const UTF8String &name,bool instance); + + void Write(VkPipelineVertexInputStateCreateInfo &vis)const; +};//class VertexAttributeBinding +VK_NAMESPACE_END +#endif//HGL_GRAPH_VULKAN_VERTEX_ATTRIBUTE_BINDING_INCLUDE diff --git a/example/Vulkan/VKVertexInput.cpp b/example/Vulkan/VKVertexInput.cpp index 91a631ee..54d4d4b0 100644 --- a/example/Vulkan/VKVertexInput.cpp +++ b/example/Vulkan/VKVertexInput.cpp @@ -3,55 +3,6 @@ #include"VKShader.h" VK_NAMESPACE_BEGIN -VertexAttributeBinding::VertexAttributeBinding(Shader *s) -{ - shader=s; - - const int count=shader->GetAttrCount(); - - if(count<=0) - { - binding_list=nullptr; - return; - } - - binding_list=hgl_copy_new(count,shader->GetDescList()); -} - -VertexAttributeBinding::~VertexAttributeBinding() -{ - delete[] binding_list; - - shader->Release(this); -} - -bool VertexAttributeBinding::SetInstance(const uint index,bool instance) -{ - if(index>=shader->GetAttrCount())return(false); - - binding_list[index].inputRate=instance?VK_VERTEX_INPUT_RATE_INSTANCE:VK_VERTEX_INPUT_RATE_VERTEX; - - return(true); -} - -bool VertexAttributeBinding::SetInstance(const UTF8String &name,bool instance) -{ - return SetInstance(shader->GetBinding(name),instance); -} - -void VertexAttributeBinding::Write(VkPipelineVertexInputStateCreateInfo &vis_create_info) const -{ - vis_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; - - const uint32_t count=shader->GetAttrCount(); - - vis_create_info.vertexBindingDescriptionCount = count; - vis_create_info.pVertexBindingDescriptions = binding_list; - - vis_create_info.vertexAttributeDescriptionCount = count; - vis_create_info.pVertexAttributeDescriptions = shader->GetAttrList(); -} - VertexInput::VertexInput(const Shader *s) { shader=s; diff --git a/example/Vulkan/main.cpp b/example/Vulkan/main.cpp index 6d16934a..a13b2454 100644 --- a/example/Vulkan/main.cpp +++ b/example/Vulkan/main.cpp @@ -235,7 +235,7 @@ int main(int,char **) device->Wait(); device->QueuePresent(); - wait_seconds(3); + wait_seconds(1); delete vertex_buffer; delete color_buffer; diff --git a/inc/hgl/TypeFunc.h b/inc/hgl/TypeFunc.h index f8252ac6..27f61aa9 100644 --- a/inc/hgl/TypeFunc.h +++ b/inc/hgl/TypeFunc.h @@ -534,15 +534,25 @@ namespace hgl /** * 鍒嗛厤鎸囧畾绫诲瀷鏁版嵁鍧楀苟娓0 */ - template - inline T *hgl_zero_new(const size_t count) - { + template + inline T *hgl_zero_new(const size_t count) + { if(count<=0)return(nullptr); T *data=new T[count]; memset(data,0,count*sizeof(T)); return data; - } + } + + template + inline T *hgl_copy_new(const size_t count,const T *src) + { + if(count<=0)return(nullptr); + + T *data=new T[count]; + memcpy(data,src,count*sizeof(T)); + return data; + } /** * 鎸囧畾绫诲瀷鏁版嵁娓0