From 86ff7517d93de2e6fb3f521237384c0cd740c0bf Mon Sep 17 00:00:00 2001 From: hyzboy Date: Tue, 28 May 2024 02:21:33 +0800 Subject: [PATCH] [WIP] optimizing VDM Support --- example/common/VulkanAppFramework.h | 2 +- inc/hgl/graph/MaterialRenderList.h | 6 +- inc/hgl/graph/PrimitiveCreater.h | 50 +++--- inc/hgl/graph/VK.h | 22 +-- inc/hgl/graph/VKCommandBuffer.h | 10 +- inc/hgl/graph/VKMaterial.h | 4 +- inc/hgl/graph/VKPrimitive.h | 16 +- inc/hgl/graph/VKRenderable.h | 46 +++-- src/SceneGraph/MaterialRenderList.cpp | 16 +- src/SceneGraph/PrimitiveCreater.cpp | 89 +++++++--- .../Vulkan/VKCommandBufferRender.cpp | 22 ++- src/SceneGraph/Vulkan/VKMaterial.cpp | 12 +- src/SceneGraph/Vulkan/VKPrimitiveData.cpp | 157 +++++------------- src/SceneGraph/Vulkan/VKPrimitiveData.h | 29 ++-- .../Vulkan/VKRenderResourceMaterial.cpp | 2 +- src/SceneGraph/Vulkan/VKRenderable.cpp | 53 ++---- 16 files changed, 242 insertions(+), 294 deletions(-) diff --git a/example/common/VulkanAppFramework.h b/example/common/VulkanAppFramework.h index f92376db..c7256799 100644 --- a/example/common/VulkanAppFramework.h +++ b/example/common/VulkanAppFramework.h @@ -216,7 +216,7 @@ public: { if(!ri)return(false); - const PrimitiveRenderBuffer *prb=ri->GetRenderBuffer(); + const PrimitiveDataBuffer *prb=ri->GetRenderBuffer(); cb->Begin(); cb->BindFramebuffer(rp,fb); diff --git a/inc/hgl/graph/MaterialRenderList.h b/inc/hgl/graph/MaterialRenderList.h index 3e1daf90..322b7c91 100644 --- a/inc/hgl/graph/MaterialRenderList.h +++ b/inc/hgl/graph/MaterialRenderList.h @@ -28,7 +28,7 @@ private: Pipeline * pipeline; MaterialInstance * mi; - const PrimitiveRenderBuffer * prb; + const PrimitiveDataBuffer * prb; const PrimitiveRenderData * prd; public: @@ -46,10 +46,10 @@ protected: VABList * vbo_list; Pipeline * last_pipeline; - const PrimitiveRenderBuffer * last_render_buf; + const PrimitiveDataBuffer * last_render_buf; const PrimitiveRenderData * last_render_data; - bool BindVAB(const PrimitiveRenderBuffer *,const PrimitiveRenderData *,const uint); + bool BindVAB(const PrimitiveDataBuffer *,const uint); void Render(RenderItem *); diff --git a/inc/hgl/graph/PrimitiveCreater.h b/inc/hgl/graph/PrimitiveCreater.h index 30f09f04..8124aeca 100644 --- a/inc/hgl/graph/PrimitiveCreater.h +++ b/inc/hgl/graph/PrimitiveCreater.h @@ -27,7 +27,7 @@ protected: uint32_t index_number; ///<索引数量 IndexType index_type; ///<索引类型 - IBAccess * iba; ///<索引缓冲区 + IndexBuffer * ibo; ///<索引缓冲区 public: @@ -52,11 +52,12 @@ public: //顶点缓冲区 const uint32_t GetVertexCount()const{ return vertices_number; } ///<取得顶点数量 - VABAccess * AcquireVAB (const AnsiString &name,const VkFormat &format,const void *data=nullptr); ///<请求一个顶点属性数据区 - bool WriteVAB (const AnsiString &name,const VkFormat &format,const void *data) ///<直接写入顶点属性数据 - { - return AcquireVAB(name,format,data); - } + const int GetVABIndex (const AnsiString &name,const VkFormat &format); ///<取得顶点属性索引 + + void * MapVAB (const int vab_index); ///<映射一个顶点属性数据区 + void UnmapVAB (const int vab_index); ///<取消映射 + + bool WriteVAB (const AnsiString &name,const VkFormat &format,const void *data); ///<直接写入顶点属性数据 public: //索引缓冲区 @@ -81,28 +82,27 @@ public: //创建可渲染对象 */ template class VABRawMap { - VABAccess *vaba; + PrimitiveCreater *pc; + int vab_index; T *map_ptr; public: - VABRawMap(PrimitiveCreater *pc,const VkFormat &format,const AnsiString &name) + VABRawMap(PrimitiveCreater *c,const VkFormat &format,const AnsiString &name) { - vaba=pc->AcquireVAB(name,format); - - if(vaba) - map_ptr=(T *)(vaba->vab->Map(vaba->start,vaba->count)); - else - map_ptr=nullptr; + pc=c; + vab_index=pc->GetVABIndex(name,format); + + map_ptr=(T *)(pc->MapVAB(vab_index)); } ~VABRawMap() { - if(vaba) - vaba->vab->Unmap(); + if(map_ptr) + pc->UnmapVAB(vab_index); } - const bool IsValid()const{ return vaba; } + const bool IsValid()const{ return map_ptr; } operator T *(){ return map_ptr; } };//template class VABRawMap @@ -121,19 +121,21 @@ typedef VABRawMap VABRawMapDouble; */ template class VABMap { - VABAccess *vaba; + PrimitiveCreater *pc; + int vab_index; T *vb; public: VABMap(PrimitiveCreater *pc,const AnsiString &name) { - vaba=pc->AcquireVAB(name,T::GetVulkanFormat(),nullptr); + pc=c; + vab_index=pc->GetVABIndex(name,T::GetVulkanFormat(),nullptr); - if(vaba) + void *map_ptr=(T *)(pc->MapVAB(vab_index)); + + if(map_ptr) { - void *map_ptr=vaba->vab->Map(vaba->start,vaba->count); - vb=T::Create(pc->GetVertexCount(),map_ptr); vb->Begin(); @@ -146,8 +148,8 @@ public: ~VABMap() { - if(vaba) - vaba->vab->Unmap(); + if(vab) + pc->UnmapVAB(vab_index); } void Restart() diff --git a/inc/hgl/graph/VK.h b/inc/hgl/graph/VK.h index b830391a..f3a26911 100644 --- a/inc/hgl/graph/VK.h +++ b/inc/hgl/graph/VK.h @@ -54,34 +54,14 @@ class DeviceMemory; class DeviceBuffer; struct DeviceBufferData; -struct PrimitiveRenderBuffer; +struct PrimitiveDataBuffer; struct PrimitiveRenderData; class VertexAttribBuffer; using VAB=VertexAttribBuffer; -struct VABAccess -{ - VAB *vab; - uint32_t start; - uint32_t count; - -public: - - CompOperatorMemcmp(const VABAccess &); -};//class VABAccess - class IndexBuffer; -struct IndexBufferAccess -{ - IndexBuffer *buffer; - uint32_t start; - uint32_t count; -}; - -using IBAccess=IndexBufferAccess; - class GPUCmdBuffer; class RenderCmdBuffer; class TextureCmdBuffer; diff --git a/inc/hgl/graph/VKCommandBuffer.h b/inc/hgl/graph/VKCommandBuffer.h index 95ddbd22..a8ac408c 100644 --- a/inc/hgl/graph/VKCommandBuffer.h +++ b/inc/hgl/graph/VKCommandBuffer.h @@ -165,12 +165,16 @@ public: if(!vab_list->IsFull())return(false); - vkCmdBindVertexBuffers(cmd_buf,0,vab_list->vab_count,vab_list->vab_list,vab_list->vab_offset); + vkCmdBindVertexBuffers(cmd_buf, + 0, //first binding + vab_list->vab_count, //binding count + vab_list->vab_list, //buffers + vab_list->vab_offset); //buffer offsets return(true); } - void BindIBO(IndexBuffer *ibo,VkDeviceSize offset); ///<绑定IBO,注意offset意为索引偏移量,不是字节 + void BindIBO(IBAccess *); bool BindVAB(Renderable *); @@ -209,7 +213,7 @@ public: //draw void DrawIndirect (VkBuffer buf, uint32_t drawCount,uint32_t stride=sizeof(VkDrawIndirectCommand )){return DrawIndirect( buf,0,drawCount,stride);} void DrawIndexedIndirect(VkBuffer buf, uint32_t drawCount,uint32_t stride=sizeof(VkDrawIndexedIndirectCommand )){return DrawIndexedIndirect( buf,0,drawCount,stride);} - void Draw (const PrimitiveRenderBuffer *prb,const PrimitiveRenderData *prd); + void Draw (const PrimitiveDataBuffer *prb,const PrimitiveRenderData *prd); // void DrawIndexed (const IBAccess *iba,const uint32_t instance_count); public: //dynamic state diff --git a/inc/hgl/graph/VKMaterial.h b/inc/hgl/graph/VKMaterial.h index 88691238..b22ce3fb 100644 --- a/inc/hgl/graph/VKMaterial.h +++ b/inc/hgl/graph/VKMaterial.h @@ -22,7 +22,7 @@ class Material { AnsiString name; - VertexInput *primitive_render_buffer; + VertexInput *vertex_input; ShaderModuleMap *shader_maps; @@ -51,7 +51,7 @@ public: const AnsiString & GetName ()const{return name;} - const VertexInput * GetVertexInput ()const{return primitive_render_buffer;} + const VertexInput * GetVertexInput ()const{return vertex_input;} const ShaderStageCreateInfoList & GetStageList ()const{return shader_stage_list;} diff --git a/inc/hgl/graph/VKPrimitive.h b/inc/hgl/graph/VKPrimitive.h index 77516d85..78e614d2 100644 --- a/inc/hgl/graph/VKPrimitive.h +++ b/inc/hgl/graph/VKPrimitive.h @@ -7,18 +7,8 @@ VK_NAMESPACE_BEGIN /** - * 单一图元数据访问接口
- * - * 这个只是单纯的提供原始VAB/IB数据,派生为两类 - * - * 一类是传统的,使用独统的独立VAB的 - * 一类是使用VDM的 - * - * - * - * WIP: *** 1.将数据全部转移到PrimitiveData,完成旧的渲染测试 - * 2.改成抽象类,将独立VAB的做成一个实现 - * 3.实现VDM支持 + * 原始图元数据访问接口
+ * Primitive的存为是为了屏蔽PrimitiveData的初始化之类的访问接口,以便于更好的管理和使用 */ class Primitive { @@ -43,9 +33,9 @@ public: const VkDeviceSize GetVertexCount ()const; const int GetVABCount ()const; + VABAccess * GetVABAccess (const AnsiString &); - IBAccess * GetIBAccess (); IndexBuffer * GetIBO (); const AABB & GetBoundingBox ()const{return BoundingBox;} diff --git a/inc/hgl/graph/VKRenderable.h b/inc/hgl/graph/VKRenderable.h index 4324af67..2da85927 100644 --- a/inc/hgl/graph/VKRenderable.h +++ b/inc/hgl/graph/VKRenderable.h @@ -9,32 +9,50 @@ #include #include VK_NAMESPACE_BEGIN -struct PrimitiveRenderBuffer +/** +* 原始图元数据缓冲区
+* 提供在渲染之前的数据绑定信息 +*/ +struct PrimitiveDataBuffer { uint32_t vab_count; VkBuffer * vab_list; + + // 理论上讲,每个VAB绑定时都是可以指定byte offsets的。但是随后Draw时,又可以指定vertexOffset。 + // 在我们支持的两种draw模式中,一种是每个模型一批VAB,所有VAB的offset都是0。 + // 另一种是使用VDM,为了批量渲染,所有的VAB又必须对齐,所以每个VAB单独指定offset也不可行。 + // 所以干脆不支持VAB的offset,只支持vertexOffset。 + +// uint32_t * vab_offset; //注意:这里的offset是相对于vertex的,代表第几个顶点,不是字节偏移 + + // IndexBuffer 同理也不再支持buffer的offset + IndexBuffer * ibo; public: - PrimitiveRenderBuffer(const uint32_t,const uint32_t,const IBAccess *iba); - ~PrimitiveRenderBuffer(); + PrimitiveDataBuffer(const uint32_t,const uint32_t, IndexBuffer *); + ~PrimitiveDataBuffer(); - const bool Comp(const PrimitiveRenderBuffer *prb)const; -};//struct PrimitiveRenderBuffer + const bool Comp(const PrimitiveDataBuffer *prb)const; +};//struct PrimitiveDataBuffer +/** +* 原始图元渲染数据
+* 提供在渲染时的数据 +*/ struct PrimitiveRenderData { uint vab_count; - VkDeviceSize * vab_offset; uint32_t vertex_count; - - uint32_t index_start; uint32_t index_count; + int32_t vertex_offset; //注意:这里的offset是相对于vertex的,代表第几个顶点,不是字节偏移 + uint32_t first_index; + public: - PrimitiveRenderData(const uint32_t bc,const uint32_t vc,const IBAccess *iba); + PrimitiveRenderData(const uint32_t bc,const uint32_t vc); ~PrimitiveRenderData(); const bool Comp(const PrimitiveRenderData *)const; @@ -49,14 +67,14 @@ class Renderable MaterialInstance * mat_inst; Primitive * primitive; - PrimitiveRenderBuffer * primitive_render_buffer; - PrimitiveRenderData * primitive_render_data; + PrimitiveDataBuffer * primitive_data_buffer; + PrimitiveRenderData * primitive_render_data; private: friend Renderable *CreateRenderable(Primitive *,MaterialInstance *,Pipeline *); - Renderable(Primitive *,MaterialInstance *,Pipeline *,PrimitiveRenderBuffer *,PrimitiveRenderData *); + Renderable(Primitive *,MaterialInstance *,Pipeline *,PrimitiveDataBuffer *,PrimitiveRenderData *); public: @@ -64,7 +82,7 @@ public: { //需要在这里添加删除pipeline/desc_sets/primitive引用计数的代码 - SAFE_CLEAR(primitive_render_buffer); + SAFE_CLEAR(primitive_data_buffer); SAFE_CLEAR(primitive_render_data); } @@ -77,7 +95,7 @@ public: Primitive * GetPrimitive (){return primitive;} const AABB & GetBoundingBox ()const{return primitive->GetBoundingBox();} - const PrimitiveRenderBuffer * GetRenderBuffer ()const{return primitive_render_buffer;} + const PrimitiveDataBuffer * GetRenderBuffer ()const{return primitive_data_buffer;} const PrimitiveRenderData * GetRenderData ()const{return primitive_render_data;} };//class Renderable diff --git a/src/SceneGraph/MaterialRenderList.cpp b/src/SceneGraph/MaterialRenderList.cpp index 73de7727..a4458d57 100644 --- a/src/SceneGraph/MaterialRenderList.cpp +++ b/src/SceneGraph/MaterialRenderList.cpp @@ -145,7 +145,7 @@ void MaterialRenderList::Stat() } } -bool MaterialRenderList::BindVAB(const PrimitiveRenderBuffer *prb,const PrimitiveRenderData *prd,const uint ri_index) +bool MaterialRenderList::BindVAB(const PrimitiveDataBuffer *prb,const uint ri_index) { //binding号都是在VertexInput::CreateVIL时连续紧密排列生成的,所以bind时first_binding写0就行了。 @@ -159,7 +159,7 @@ bool MaterialRenderList::BindVAB(const PrimitiveRenderBuffer *prb,const Primitiv //Basic组,它所有的VAB信息均来自于Primitive,由vid参数传递进来 { vbo_list->Add(prb->vab_list, - nullptr,//prd->vab_offset, //暂时不用dd->vab_offset,全部写0,测试一下是否可以使用Draw时的firstVertex或vertexOffset + prb->vab_offset, prb->vab_count); } @@ -223,16 +223,18 @@ void MaterialRenderList::Render(RenderItem *ri) last_render_buf=ri->prb; last_render_data=nullptr; - BindVAB(ri->prb,ri->prd,ri->first); - cmd_buf->BindIBO(ri->prb->ibo,0); + BindVAB(ri->prb,ri->first); + + if(ri->prb->ib_access->buffer) + cmd_buf->BindIBO(ri->prb->ib_access); } - if(last_render_buf->ibo) + if(last_render_buf->ib_access->buffer) { cmd_buf->DrawIndexed(ri->prd->index_count, ri->count, - ri->prd->index_start, - ri->prd->vab_offset[0], //因为vkCmdDrawIndexed的vertexOffset是针对所有VAB的,所以所有的VAB数据都必须是对齐的, + ri->prd->first_index, + ri->prd->vertex_offset, //因为vkCmdDrawIndexed的vertexOffset是针对所有VAB的,所以所有的VAB数据都必须是对齐的, //最终这里使用vab_offset[0]是可以的,因为它也等于其它所有的vab_offset。未来考虑统一成一个。 ri->first); //这里vkCmdDrawIndexed的firstInstance参数指的是instance Rate更新的VAB的起始实例数,不是指instance批量渲染。 //所以这里使用ri->first是对的。 diff --git a/src/SceneGraph/PrimitiveCreater.cpp b/src/SceneGraph/PrimitiveCreater.cpp index 887f1da1..f3a0e234 100644 --- a/src/SceneGraph/PrimitiveCreater.cpp +++ b/src/SceneGraph/PrimitiveCreater.cpp @@ -39,7 +39,7 @@ void PrimitiveCreater::Clear() index_number =0; index_type =IndexType::ERR; - iba =nullptr; + ibo =nullptr; } bool PrimitiveCreater::Init(const AnsiString &pname,const uint32_t vertex_count,const uint32_t index_count,IndexType it) @@ -84,9 +84,9 @@ bool PrimitiveCreater::Init(const AnsiString &pname,const uint32_t vertex_count, if(index_number>0) { - iba=prim_data->InitIBO(index_number,index_type); + ibo=prim_data->InitIBO(index_number,index_type); - if(!iba) + if(!ibo) { delete prim_data; return(false); @@ -99,8 +99,8 @@ bool PrimitiveCreater::Init(const AnsiString &pname,const uint32_t vertex_count, if(du) { - du->SetBuffer( iba->buffer->GetBuffer(), prim_name+":IndexBuffer:Buffer"); - du->SetDeviceMemory(iba->buffer->GetVkMemory(), prim_name+":IndexBuffer:Memory"); + du->SetBuffer( ibo->GetBuffer(), prim_name+":IndexBuffer:Buffer"); + du->SetDeviceMemory(ibo->GetVkMemory(), prim_name+":IndexBuffer:Memory"); } } #endif//_DEBUG @@ -111,44 +111,83 @@ bool PrimitiveCreater::Init(const AnsiString &pname,const uint32_t vertex_count, return(true); } -VABAccess *PrimitiveCreater::AcquireVAB(const AnsiString &name,const VkFormat &acquire_format,const void *data) +const int PrimitiveCreater::GetVABIndex(const AnsiString &name,const VkFormat &acquire_format) { - if(!prim_data)return(nullptr); - if(name.IsEmpty())return(nullptr); + if(!prim_data)return(-1); - VABAccess *vab_access=prim_data->InitVAB(name,acquire_format,data); + const int vab_index=prim_data->GetVABIndex(name); - if(!vab_access) - return(nullptr); + VAB *vab=prim_data->GetVAB(vab_index); - #ifdef _DEBUG - if(!vdm&&vab_access->vab) - { + if(!vab) + return(-1); + + vab=prim_data->InitVAB(vab_index,acquire_format,nullptr); + + if(!vab) + return(-1); + +#ifdef _DEBUG + if (!vdm) + { DebugUtils *du=device->GetDebugUtils(); - if(du) + if (du) { - du->SetBuffer( vab_access->vab->GetBuffer(), prim_name+":VAB:Buffer:"+name); - du->SetDeviceMemory(vab_access->vab->GetVkMemory(), prim_name+":VAB:Memory:"+name); + du->SetBuffer(vab->GetBuffer(), prim_name+":VAB:Buffer:"+name); + du->SetDeviceMemory(vab->GetVkMemory(), prim_name+":VAB:Memory:"+name); } } - #endif//_DEBUG +#endif//_DEBUG - return vab_access; + return(vab_index); +} + +void *PrimitiveCreater::MapVAB(const int vab_index) +{ + VAB *vab=prim_data->GetVAB(vab_index); + + if(!vab) + return(nullptr); + + return vab->Map(prim_data->GetVertexOffset(),vertices_number); +} + +void PrimitiveCreater::UnmapVAB(const int vab_index) +{ + VAB *vab=prim_data->GetVAB(vab_index); + + if(!vab)return; + + vab->Unmap(); +} + +bool PrimitiveCreater::WriteVAB(const AnsiString &name,const VkFormat &format, const void *data) +{ + if(!prim_data)return(false); + + const int vab_index=GetVABIndex(name,format); + + VAB *vab=prim_data->GetVAB(vab_index); + + if(!vab) + return(false); + + return vab->Write(data,prim_data->GetVertexOffset(),vertices_number); } void *PrimitiveCreater::MapIBO() { if(!prim_data)return(nullptr); - if(!iba)return(nullptr); + if(!ibo)return(nullptr); - return iba->buffer->Map(iba->start,iba->count); + return ibo->Map(prim_data->GetFirstIndex(),index_number); } void PrimitiveCreater::UnmapIBO() { - if(iba) - iba->buffer->Unmap(); + if(ibo) + ibo->Unmap(); } bool PrimitiveCreater::WriteIBO(const void *data,const uint32_t count) @@ -156,12 +195,12 @@ bool PrimitiveCreater::WriteIBO(const void *data,const uint32_t count) if(!data)return(false); if(!prim_data)return(false); - IBAccess *iba=prim_data->GetIBAccess(); + IndexBuffer *ibo=prim_data->GetIBO(); if(count>0&&count>index_number) return(false); - return iba->buffer->Write(data,iba->start,count); + return ibo->Write(data,prim_data->GetFirstIndex(),count); } Primitive *PrimitiveCreater::Create() diff --git a/src/SceneGraph/Vulkan/VKCommandBufferRender.cpp b/src/SceneGraph/Vulkan/VKCommandBufferRender.cpp index b5fbe009..837fe54e 100644 --- a/src/SceneGraph/Vulkan/VKCommandBufferRender.cpp +++ b/src/SceneGraph/Vulkan/VKCommandBufferRender.cpp @@ -130,11 +130,17 @@ bool RenderCmdBuffer::BindDescriptorSets(Material *mtl) return(true); } -void RenderCmdBuffer::BindIBO(IndexBuffer *ibo,VkDeviceSize offset) +void RenderCmdBuffer::BindIBO(IBAccess *iba) { + if(!iba)return; + + IndexBuffer *ibo=iba->buffer; + + if(!ibo)return; + vkCmdBindIndexBuffer(cmd_buf, ibo->GetBuffer(), - offset*ibo->GetStride(), + iba->start*ibo->GetStride(), VkIndexType(ibo->GetType())); } @@ -143,7 +149,7 @@ bool RenderCmdBuffer::BindVAB(Renderable *ri) if(!ri) return(false); - const PrimitiveRenderBuffer *prb=ri->GetRenderBuffer(); + const PrimitiveDataBuffer *prb=ri->GetRenderBuffer(); if(prb->vab_count<=0) return(false); @@ -153,10 +159,10 @@ bool RenderCmdBuffer::BindVAB(Renderable *ri) if(prd->vertex_count<=0) return(false); - vkCmdBindVertexBuffers(cmd_buf,0,prb->vab_count,prb->vab_list,prd->vab_offset); + vkCmdBindVertexBuffers(cmd_buf,0,prb->vab_count,prb->vab_list,prb->vab_offset); - if(prb->ibo&&prd->index_count) - BindIBO(prb->ibo,prd->index_start); + if(prb->ib_access->buffer) + BindIBO(prb->ib_access); return(true); } @@ -185,12 +191,12 @@ void RenderCmdBuffer::DrawIndexedIndirect( VkBuffer buffer, vkCmdDrawIndexedIndirect(cmd_buf,buffer,offset+i*stride,1,stride); } -void RenderCmdBuffer::Draw(const PrimitiveRenderBuffer *prb,const PrimitiveRenderData *prd) +void RenderCmdBuffer::Draw(const PrimitiveDataBuffer *prb,const PrimitiveRenderData *prd) { if(!prb||!prd) return; - if (prb->ibo) + if (prb->ib_access->buffer) DrawIndexed(prd->index_count); else Draw(prd->vertex_count); diff --git a/src/SceneGraph/Vulkan/VKMaterial.cpp b/src/SceneGraph/Vulkan/VKMaterial.cpp index 0204bef3..794f1fa6 100644 --- a/src/SceneGraph/Vulkan/VKMaterial.cpp +++ b/src/SceneGraph/Vulkan/VKMaterial.cpp @@ -13,7 +13,7 @@ Material::Material(const AnsiString &n) { name=n; - primitive_render_buffer=nullptr; + vertex_input=nullptr; shader_maps=new ShaderModuleMap; desc_manager=nullptr; pipeline_layout_data=nullptr; @@ -28,7 +28,7 @@ Material::~Material() { SAFE_CLEAR(mi_data_manager); - ReleaseVertexInput(primitive_render_buffer); + ReleaseVertexInput(vertex_input); delete shader_maps; //不用SAFE_CLEAR是因为这个一定会有 SAFE_CLEAR(desc_manager); SAFE_CLEAR(pipeline_layout_data); @@ -49,22 +49,22 @@ const bool Material::hasSet(const DescriptorSetType &dst)const const VIL *Material::GetDefaultVIL()const { - return primitive_render_buffer->GetDefaultVIL(); + return vertex_input->GetDefaultVIL(); } VIL *Material::CreateVIL(const VILConfig *format_map) { - return primitive_render_buffer->CreateVIL(format_map); + return vertex_input->CreateVIL(format_map); } bool Material::Release(VIL *vil) { - return primitive_render_buffer->Release(vil); + return vertex_input->Release(vil); } const uint Material::GetVILCount() { - return primitive_render_buffer->GetInstanceCount(); + return vertex_input->GetInstanceCount(); } bool Material::BindUBO(const DescriptorSetType &type,const AnsiString &name,DeviceBuffer *ubo,bool dynamic) diff --git a/src/SceneGraph/Vulkan/VKPrimitiveData.cpp b/src/SceneGraph/Vulkan/VKPrimitiveData.cpp index c9b80e86..1c4eace2 100644 --- a/src/SceneGraph/Vulkan/VKPrimitiveData.cpp +++ b/src/SceneGraph/Vulkan/VKPrimitiveData.cpp @@ -13,14 +13,14 @@ PrimitiveData::PrimitiveData(const VIL *_vil,const uint32_t vc) vertex_count=vc; - vab_access=hgl_zero_new(_vil->GetCount()); + vab_list=hgl_zero_new(_vil->GetCount()); - hgl_zero(ib_access); + ibo=nullptr; } PrimitiveData::~PrimitiveData() { - SAFE_CLEAR_ARRAY(vab_access); //注意:这里并不释放VAB,在派生类中释放 + delete[] vab_list; //注意:这里并不释放VAB,在派生类中释放 } const int PrimitiveData::GetVABCount()const @@ -35,58 +35,13 @@ const int PrimitiveData::GetVABIndex(const AnsiString &name) const return vil->GetIndex(name); } -VABAccess *PrimitiveData::GetVABAccess(const int index) +VAB *PrimitiveData::GetVAB(const int index) { if(index<0||index>=vil->GetCount())return(nullptr); - return vab_access+index; + return vab_list[index]; } -VABAccess *PrimitiveData::GetVABAccess(const AnsiString &name) -{ - if(name.IsEmpty())return(nullptr); - - const int index=vil->GetIndex(name); - - if(index<0)return(nullptr); - - return vab_access+index; -} - -//VABAccess *SetVAB(PrimitiveData *pd,const int index,VAB *vab,VkDeviceSize start,VkDeviceSize count) -//{ -// if(!pd)return(nullptr); -// if(!pd->vil)return(nullptr); -// if(index<0||index>=pd->vil->GetCount())return(nullptr); -// -// VABAccess *vaba=pd->vab_access+index; -// -// vaba->vab=vab; -// vaba->start=start; -// vaba->count=count; -// -// //#ifdef _DEBUG -// // DebugUtils *du=device->GetDebugUtils(); -// -// // if(du) -// // { -// // du->SetBuffer(vab->GetBuffer(),prim_name+":VAB:Buffer:"+name); -// // du->SetDeviceMemory(vab->GetVkMemory(),prim_name+":VAB:Memory:"+name); -// // } -// //#endif//_DEBUG -// -// return vaba; -//} - -//void SetIndexBuffer(PrimitiveData *pd,IndexBuffer *ib,const VkDeviceSize ic) -//{ -// if(!pd)return; -// -// pd->ib_access.buffer=ib; -// pd->ib_access.start=0; -// pd->ib_access.count=ic; -//} - namespace { /** @@ -98,7 +53,10 @@ namespace public: - VertexDataManager *GetVDM(){return nullptr;} + int32_t GetVertexOffset ()const override{return 0;} + uint32_t GetFirstIndex ()const override{return 0;} + + public: PrimitiveDataPrivateBuffer(GPUDevice *dev,const VIL *_vil,const uint32_t vc):PrimitiveData(_vil,vc) { @@ -107,83 +65,65 @@ namespace ~PrimitiveDataPrivateBuffer() override { - VABAccess *vab=vab_access; + VAB **vab=vab_list; for(uint i=0;iGetCount();i++) { - if(vab->vab) - { - delete vab->vab; - vab->vab=nullptr; - } + if(*vab) + delete *vab; ++vab; } - if(ib_access.buffer) - { - delete ib_access.buffer; - ib_access.buffer=nullptr; - } + if(ibo) + delete ibo; } - IBAccess *InitIBO(const uint32_t index_count,IndexType it) override + IndexBuffer *InitIBO(const uint32_t ic,IndexType it) override { if(!device)return(nullptr); - if(ib_access.buffer) - { - delete ib_access.buffer; - ib_access.buffer=nullptr; - } + if(ibo)delete ibo; - ib_access.buffer=device->CreateIBO(it,index_count); + ibo=device->CreateIBO(it,ic); - if(!ib_access.buffer) + if(!ibo) return(nullptr); - ib_access.start=0; - ib_access.count=index_count; + index_count=ic; - return(&ib_access); + return(ibo); } - VABAccess *InitVAB(const AnsiString &name,const VkFormat &format,const void *data) + VAB *InitVAB(const int vab_index,const VkFormat &format,const void *data) { if(!device)return(nullptr); if(!vil)return(nullptr); - if(name.IsEmpty())return(nullptr); - - const int index=vil->GetIndex(name); - if(index<0||index>=vil->GetCount()) + if(vab_index<0||vab_index>=vil->GetCount()) return(nullptr); - const VertexInputFormat *vif=vil->GetConfig(index); + const VertexInputFormat *vif=vil->GetConfig(vab_index); if(!vif)return(nullptr); if(vif->format!=format) return(nullptr); - VABAccess *vaba=vab_access+index; - - if(!vaba->vab) + if(!vab_list[vab_index]) { - vaba->vab=device->CreateVAB(format,vertex_count,data); + vab_list[vab_index]=device->CreateVAB(format,vertex_count,data); - if(!vaba->vab) + if(!vab_list[vab_index]) return(nullptr); - - vaba->start=0; - vaba->count=vertex_count; } - else + + if(vab_list[vab_index]&&data) { - vaba->vab->Write(data,vertex_count); + vab_list[vab_index]->Write(data,vertex_count); } - return vaba; + return vab_list[vab_index]; } };//class PrimitiveDataPrivateBuffer:public PrimitiveData @@ -199,7 +139,8 @@ namespace public: - VertexDataManager *GetVDM(){return vdm;} + int32_t GetVertexOffset()const override { return vab_node->GetStart(); } + uint32_t GetFirstIndex ()const override { return ib_node->GetStart(); } PrimitiveDataVDM(VertexDataManager *_vdm,const uint32_t vc):PrimitiveData(_vdm->GetVIL(),vc) { @@ -218,7 +159,7 @@ namespace vdm->ReleaseVAB(vab_node); } - IBAccess *InitIBO(const uint32_t index_count,IndexType it) override + IndexBuffer *InitIBO(const uint32_t index_count,IndexType it) override { if(index_count<=0)return(nullptr); if(!vdm)return(nullptr); @@ -230,49 +171,39 @@ namespace if(!ib_node) return(nullptr); - ib_access.buffer=vdm->GetIBO(); - ib_access.start =ib_node->GetStart(); - ib_access.count =ib_node->GetCount(); + ibo=vdm->GetIBO(); } - return &ib_access; + return ibo; } - VABAccess *InitVAB(const AnsiString &name,const VkFormat &format,const void *data) + VAB *InitVAB(const int vab_index,const VkFormat &format,const void *data) { if(!vdm)return(nullptr); if(!vil)return(nullptr); - if(name.IsEmpty())return(nullptr); - - const int index=vil->GetIndex(name); - if(index<0||index>=vil->GetCount()) + if (vab_index<0||vab_index>=vil->GetCount()) return(nullptr); - const VertexInputFormat *vif=vil->GetConfig(index); + const VertexInputFormat *vif=vil->GetConfig(vab_index); if(!vif)return(nullptr); if(vif->format!=format) return(nullptr); - VABAccess *vaba=vab_access+index; - - if(!vaba->vab) + if (!vab_list[vab_index]) { - vaba->vab=vdm->GetVAB(index); + vab_list[vab_index]=vdm->GetVAB(vab_index); - if(!vaba->vab) + if(!vab_list[vab_index]) return(nullptr); - - vaba->start=vab_node->GetStart(); - vaba->count=vab_node->GetCount(); } - if(vaba->vab&&data) - vaba->vab->Write(data,vaba->start,vaba->count); + if(vab_list[vab_index]&&data) + vab_list[vab_index]->Write(data,vab_node->GetStart(),vertex_count); - return vaba; + return vab_list[vab_index]; } };//class PrimitiveDataVDM:public PrimitiveData }//namespace diff --git a/src/SceneGraph/Vulkan/VKPrimitiveData.h b/src/SceneGraph/Vulkan/VKPrimitiveData.h index 4e3f29eb..150a3a99 100644 --- a/src/SceneGraph/Vulkan/VKPrimitiveData.h +++ b/src/SceneGraph/Vulkan/VKPrimitiveData.h @@ -19,12 +19,13 @@ class PrimitiveData { protected: - const VIL * vil; + const VIL * vil; - uint32_t vertex_count; + uint32_t vertex_count; + uint32_t index_count; - VABAccess * vab_access; - IBAccess ib_access; + VAB ** vab_list; + IndexBuffer * ibo; public: @@ -36,22 +37,20 @@ public: const uint32_t GetVertexCount ()const{return vertex_count;} const int GetVABCount ()const; const int GetVABIndex (const AnsiString &name)const; - VABAccess * GetVABAccess (const int index); - VABAccess * GetVABAccess (const AnsiString &name); + VAB * GetVAB (const int index); - IBAccess * GetIBAccess (){return &ib_access;} - IndexBuffer * GetIBO (){return ib_access.buffer;} + IndexBuffer * GetIBO (){return ibo;} + uint32_t GetIndexCount ()const{return index_count;} + + virtual int32_t GetVertexOffset ()const=0; ///<取得顶点偏移(注意是顶点不是字节) + virtual uint32_t GetFirstIndex ()const=0; ///<取得第一个索引 public: - virtual VertexDataManager *GetVDM()=0; - -public: - - virtual IBAccess * InitIBO(const uint32_t index_count,IndexType it)=0; - virtual VABAccess *InitVAB(const AnsiString &name,const VkFormat &format,const void *data)=0; + virtual IndexBuffer * InitIBO(const uint32_t index_count,IndexType it)=0; + virtual VAB * InitVAB(const int vab_index,const VkFormat &format,const void *data)=0; };//class PrimitiveData PrimitiveData *CreatePrimitiveData(GPUDevice *dev,const VIL *_vil,const uint32_t vc); PrimitiveData *CreatePrimitiveData(VertexDataManager *vdm,const uint32_t vc); -VK_NAMESPACE_END \ No newline at end of file +VK_NAMESPACE_END diff --git a/src/SceneGraph/Vulkan/VKRenderResourceMaterial.cpp b/src/SceneGraph/Vulkan/VKRenderResourceMaterial.cpp index 003d5492..295e6241 100644 --- a/src/SceneGraph/Vulkan/VKRenderResourceMaterial.cpp +++ b/src/SceneGraph/Vulkan/VKRenderResourceMaterial.cpp @@ -124,7 +124,7 @@ Material *RenderResource::CreateMaterial(const mtl::MaterialCreateInfo *mci) ShaderCreateInfoVertex *vert=mci->GetVS(); if(vert) - mtl->primitive_render_buffer=GetVertexInput(vert->sdm->GetShaderStageIO().input); + mtl->vertex_input=GetVertexInput(vert->sdm->GetShaderStageIO().input); } { diff --git a/src/SceneGraph/Vulkan/VKRenderable.cpp b/src/SceneGraph/Vulkan/VKRenderable.cpp index e5ac8af6..896fc46f 100644 --- a/src/SceneGraph/Vulkan/VKRenderable.cpp +++ b/src/SceneGraph/Vulkan/VKRenderable.cpp @@ -7,24 +7,20 @@ #include VK_NAMESPACE_BEGIN -PrimitiveRenderBuffer::PrimitiveRenderBuffer(const uint32_t c,const uint32_t vc,const IBAccess *iba) +PrimitiveDataBuffer::PrimitiveDataBuffer(const uint32_t c,const uint32_t vc, IndexBuffer *ib) { vab_count=c; vab_list=hgl_zero_new(vab_count); - - if(iba&&iba->buffer) - ibo=iba->buffer; - else - ibo=nullptr; + ibo=ib; } -PrimitiveRenderBuffer::~PrimitiveRenderBuffer() +PrimitiveDataBuffer::~PrimitiveDataBuffer() { delete[] vab_list; } -const bool PrimitiveRenderBuffer::Comp(const PrimitiveRenderBuffer *prb)const +const bool PrimitiveDataBuffer::Comp(const PrimitiveDataBuffer *prb)const { if(!prb)return(false); @@ -35,58 +31,41 @@ const bool PrimitiveRenderBuffer::Comp(const PrimitiveRenderBuffer *prb)const if(vab_list[i]!=prb->vab_list[i])return(false); } - if(ibo!=prb->ibo)return(false); + if(ibo!=prb->ibo) + return(false); return(true); } -Renderable::Renderable(Primitive *r,MaterialInstance *mi,Pipeline *p,PrimitiveRenderBuffer *prb,PrimitiveRenderData *prd) +Renderable::Renderable(Primitive *r,MaterialInstance *mi,Pipeline *p,PrimitiveDataBuffer *prb,PrimitiveRenderData *prd) { primitive=r; pipeline=p; mat_inst=mi; - primitive_render_buffer=prb; + primitive_data_buffer=prb; primitive_render_data=prd; } -PrimitiveRenderData::PrimitiveRenderData(const uint32_t bc,const uint32_t vc,const IBAccess *iba) +PrimitiveRenderData::PrimitiveRenderData(const uint32_t bc,const uint32_t vc) { vab_count=bc; - vab_offset=new VkDeviceSize[vab_count]; - vertex_count=vc; + vertex_offset=0; - if(iba&&iba->buffer) - { - index_start=iba->start; - index_count=iba->count; - } + first_index=0; } PrimitiveRenderData::~PrimitiveRenderData() { - delete[] vab_offset; } const bool PrimitiveRenderData::Comp(const PrimitiveRenderData *prd)const { if(!prd)return(false); - if(vab_count!=prd->vab_count)return(false); - - for(uint i=0;ivab_offset[i])return(false); - } - - if(vertex_count!=prd->vertex_count)return(false); - - if(index_start!=prd->index_start)return(false); - if(index_count!=prd->index_count)return(false); - - return(true); + return !memcmp(this,prd,sizeof(PrimitiveRenderData)); } Renderable *CreateRenderable(Primitive *prim,MaterialInstance *mi,Pipeline *p) @@ -104,10 +83,8 @@ Renderable *CreateRenderable(Primitive *prim,MaterialInstance *mi,Pipeline *p) return(nullptr); } - const IBAccess *iba=prim->GetIBAccess(); - - PrimitiveRenderBuffer * prb=new PrimitiveRenderBuffer( input_count,prim->GetVertexCount(),iba); - PrimitiveRenderData * prd=new PrimitiveRenderData( input_count,prim->GetVertexCount(),iba); + PrimitiveDataBuffer *prb=new PrimitiveDataBuffer(input_count,prim->GetVertexCount(),prim->GetIBO()); + PrimitiveRenderData *prd=new PrimitiveRenderData(input_count,prim->GetVertexCount()); const VertexInputFormat *vif=vil->GetVIFList(VertexInputGroup::Basic); @@ -146,7 +123,7 @@ Renderable *CreateRenderable(Primitive *prim,MaterialInstance *mi,Pipeline *p) return(nullptr); } - prd->vab_offset[i]=vab_access->start; + prb->vab_offset[i]=vab_access->start; prb->vab_list[i]=vab_access->vab->GetBuffer(); ++vif; }