From cef5ad073bd641e720c4369ec4f01869d1c2f737 Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Thu, 7 Sep 2023 18:09:31 +0800 Subject: [PATCH] a few update --- inc/hgl/graph/MaterialRenderList.h | 2 +- inc/hgl/graph/VKMaterial.h | 3 + src/SceneGraph/MaterialRenderList.cpp | 21 +------ src/SceneGraph/RenderAssignBuffer.cpp | 72 +++++++++++++----------- src/SceneGraph/RenderAssignBuffer.h | 48 ++++++++-------- src/SceneGraph/StaticRenderManage.md | 4 +- src/SceneGraph/Vulkan/VKDeviceBuffer.cpp | 8 +++ 7 files changed, 80 insertions(+), 78 deletions(-) diff --git a/inc/hgl/graph/MaterialRenderList.h b/inc/hgl/graph/MaterialRenderList.h index f1314af5..4fd0db66 100644 --- a/inc/hgl/graph/MaterialRenderList.h +++ b/inc/hgl/graph/MaterialRenderList.h @@ -3,7 +3,7 @@ #include VK_NAMESPACE_BEGIN -struct RenderAssignBuffer; +class RenderAssignBuffer; /** * 同一材质的对象渲染列表 diff --git a/inc/hgl/graph/VKMaterial.h b/inc/hgl/graph/VKMaterial.h index f68874f8..6c709431 100644 --- a/inc/hgl/graph/VKMaterial.h +++ b/inc/hgl/graph/VKMaterial.h @@ -68,6 +68,9 @@ public: public: + const uint32_t GetMIDataBytes ()const{return mi_data_bytes;} + const uint32_t GetMIMaxCount ()const{return mi_max_count;} + MaterialInstance *CreateMI(const VILConfig *vil_cfg=nullptr); };//class Material diff --git a/src/SceneGraph/MaterialRenderList.cpp b/src/SceneGraph/MaterialRenderList.cpp index ce0d0022..417fe4d9 100644 --- a/src/SceneGraph/MaterialRenderList.cpp +++ b/src/SceneGraph/MaterialRenderList.cpp @@ -61,7 +61,7 @@ MaterialRenderList::MaterialRenderList(GPUDevice *d,Material *m) device=d; cmd_buf=nullptr; mtl=m; - assign_buffer=nullptr; + assign_buffer=new RenderAssignBuffer(d,mtl->GetMIDataBytes()); vbo_list=new VBOList(mtl->GetVertexInput()->GetCount()); } @@ -91,24 +91,10 @@ void MaterialRenderList::End() if(node_count<=0)return; - if(!assign_buffer) - assign_buffer=new RenderAssignBuffer; - - if(assign_buffer->node_countNodeAlloc(device,node_count); - Stat(); //写入LocalToWorld数据 - assign_buffer->WriteLocalToWorld(rn_list.GetData(),node_count); - - const uint mi_count=mi_set.GetCount(); - - if(mi_count<=0)return; - - //if(assign_buffer->mi_countMIAlloc(device,mi_count,); - //assign_buffer->WriteMaterialInstance(rn_list.GetData(),node_count,mi_set); + assign_buffer->WriteNode(rn_list.GetData(),node_count,mi_set); } void MaterialRenderList::RenderItem::Set(Renderable *ri) @@ -226,7 +212,7 @@ bool MaterialRenderList::Bind(const VertexInputData *vid,const uint ri_index) // } //} - if(!vbo_list->IsFull()) + if(!vbo_list->IsFull()) //Assign组 { const uint assign_binding_count=vil->GetCount(VertexInputGroup::Assign); @@ -239,7 +225,6 @@ bool MaterialRenderList::Bind(const VertexInputData *vid,const uint ri_index) } } - //if(count!=binding_count) //{ // //还有没支持的绑定组???? diff --git a/src/SceneGraph/RenderAssignBuffer.cpp b/src/SceneGraph/RenderAssignBuffer.cpp index 47585530..c33b2959 100644 --- a/src/SceneGraph/RenderAssignBuffer.cpp +++ b/src/SceneGraph/RenderAssignBuffer.cpp @@ -25,7 +25,7 @@ void RenderAssignBuffer::Bind(MaterialInstance *mi)const // mi->BindUBO(DescriptorSetType::PerFrame,"Assign",assign_buffer->ubo_mi); } -void RenderAssignBuffer::ClearNode() +void RenderAssignBuffer::Clear() { SAFE_CLEAR(ubo_l2w); SAFE_CLEAR(ubo_mi); @@ -34,47 +34,47 @@ void RenderAssignBuffer::ClearNode() node_count=0; } -void RenderAssignBuffer::Clear() +void RenderAssignBuffer::Alloc(const uint nc,const uint mc) { - ClearNode(); -// ClearMI(); + Clear(); -// SAFE_CLEAR(bone_id) -// SAFE_CLEAR(bone_weight) + { + node_count=power_to_2(nc); + + ubo_l2w=device->CreateUBO(node_count*sizeof(Matrix4f)); + } + + { + mi_count=power_to_2(mc); + + ubo_mi=device->CreateUBO(mi_count*mi_data_bytes); + } + + vbo_assigns=device->CreateVBO(ASSIGN_VBO_FMT,node_count); } -void RenderAssignBuffer::NodeAlloc(GPUDevice *dev,const uint c) -{ - ClearNode(); - node_count=power_to_2(c); - - ubo_l2w=dev->CreateUBO(node_count*sizeof(Matrix4f)); - //ubo_mi=dev->CreateUBO(node_count*sizeof(uint8)); - vbo_assigns=dev->CreateVBO(ASSIGN_VBO_FMT,node_count); -} - -//void MIAlloc(GPUDevice *dev,const uint c,const uint mis) -//{ -// ClearMI(); -// if(c<=0||mi_size<=0)return; -// -// mi_count=power_to_2(c); -// mi_size=mis; - -// mi_id=dev->CreateVBO(VF_V1U8,mi_count); -// mi_id_buffer=mi_id->GetBuffer(); - -// mi_data_buffer=dev->CreateUBO(mi_count*mi_size); -//} - -void RenderAssignBuffer::WriteLocalToWorld(RenderNode *render_node,const uint count) +void RenderAssignBuffer::WriteNode(RenderNode *render_node,const uint count,const MaterialInstanceSets &mi_set) { RenderNode *rn; - //new l2w array in ubo + Alloc(count,mi_set.GetCount()); + + { + uint8 *mip=(uint8 *)(ubo_mi->Map()); + + for(MaterialInstance *mi:mi_set) + { +// memcpy(mip,mi->GetData(),mi_data_bytes); + mip+=mi_data_bytes; + } + + ubo_mi->Unmap(); + } + + uint16 *idp=(uint16 *)(vbo_assigns->Map()); + { Matrix4f *tp=(hgl::Matrix4f *)(ubo_l2w->Map()); - uint16 *idp=(uint16 *)(vbo_assigns->Map()); rn=render_node; @@ -86,12 +86,16 @@ void RenderAssignBuffer::WriteLocalToWorld(RenderNode *render_node,const uint co *idp=i; ++idp; + //*idp=mi_set.Find(rn->ri->GetMaterialInstance()); + //++idp; + ++rn; } - vbo_assigns->Unmap(); ubo_l2w->Unmap(); } + + vbo_assigns->Unmap(); } //void WriteMaterialInstance(RenderNode *render_node,const uint count,const MaterialInstanceSets &mi_set) diff --git a/src/SceneGraph/RenderAssignBuffer.h b/src/SceneGraph/RenderAssignBuffer.h index abad0ee3..da9c785f 100644 --- a/src/SceneGraph/RenderAssignBuffer.h +++ b/src/SceneGraph/RenderAssignBuffer.h @@ -1,5 +1,5 @@ #pragma once -#include +#include VK_NAMESPACE_BEGIN // ubo_range大致分为三档: @@ -28,29 +28,28 @@ class MaterialInstance; /* * 渲染节点额外提供的数据 */ -struct RenderAssignBuffer +class RenderAssignBuffer { - uint node_count; ///<渲染节点数量 -// uint mi_count; ///<材质实例数量 +private: - //uint mi_size; ///<单个材质实例数量长度 - //DeviceBuffer *mi_data_buffer; ///<材质实例数据UBO/SSBO + GPUDevice *device; - //VBO *mi_id; - //VkBuffer mi_id_buffer; + uint node_count; ///<渲染节点数量 + DeviceBuffer *ubo_l2w; ///physical_device->Get bool GPUDevice::CreateBuffer(DeviceBufferData *buf,VkBufferUsageFlags buf_usage,VkDeviceSize range,VkDeviceSize size,const void *data,SharingMode sharing_mode) { + if(size<=0)return(false); + BufferCreateInfo buf_info; buf_info.usage = buf_usage; @@ -55,6 +57,8 @@ bool GPUDevice::CreateBuffer(DeviceBufferData *buf,VkBufferUsageFlags buf_usage, VBO *GPUDevice::CreateVBO(VkFormat format,uint32_t count,const void *data,SharingMode sharing_mode) { + if(count==0)return(nullptr); + const uint32_t stride=GetStrideByFormat(format); if(stride==0) @@ -75,6 +79,8 @@ VBO *GPUDevice::CreateVBO(VkFormat format,uint32_t count,const void *data,Sharin IndexBuffer *GPUDevice::CreateIBO(IndexType index_type,uint32_t count,const void *data,SharingMode sharing_mode) { + if(count==0)return(nullptr); + uint32_t stride; if(index_type==IndexType::U8 )stride=1;else @@ -94,6 +100,8 @@ IndexBuffer *GPUDevice::CreateIBO(IndexType index_type,uint32_t count,const void DeviceBuffer *GPUDevice::CreateBuffer(VkBufferUsageFlags buf_usage,VkDeviceSize range,VkDeviceSize size,const void *data,SharingMode sharing_mode) { + if(size<=0)return(nullptr); + DeviceBufferData buf; if(!CreateBuffer(&buf,buf_usage,range,size,data,sharing_mode))