From 8a0215aee6a8bb98e32b091c7d945d9fd70ceef6 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Thu, 23 May 2024 14:00:23 +0800 Subject: [PATCH] supported newly IBMap,can RUN OK!!! --- inc/hgl/graph/PrimitiveCreater.h | 73 +++++++++++++---------- src/SceneGraph/InlineGeometry.cpp | 50 ++++++++++------ src/SceneGraph/PrimitiveCreater.cpp | 48 +++++++-------- src/SceneGraph/Vulkan/VKPrimitiveData.cpp | 16 ++--- 4 files changed, 106 insertions(+), 81 deletions(-) diff --git a/inc/hgl/graph/PrimitiveCreater.h b/inc/hgl/graph/PrimitiveCreater.h index 5f1f1e82..5b7fca7f 100644 --- a/inc/hgl/graph/PrimitiveCreater.h +++ b/inc/hgl/graph/PrimitiveCreater.h @@ -28,8 +28,7 @@ protected: uint vab_proc_count; ///<操作的vab数量 VkDeviceSize index_number; ///<索引数量 - IndexBuffer * ibo; - void * ibo_map; + IBAccess * iba; protected: @@ -54,36 +53,13 @@ public: return AcquirePVB(name,format,data,bytes); } - const IndexType GetIndexType()const{return ibo?ibo->GetType():IndexType::ERR;} ///<取得索引数据类型 + const IndexType GetIndexType()const{return iba->buffer->GetType();} + IBAccess * AcquireIBO(){return iba;} - template - T * AccessIBO() - { - if(!ibo)return(nullptr); - if(ibo->GetStride()!=sizeof(T))return(nullptr); + bool WriteIBO(const void *data,const VkDeviceSize bytes); - if(!ibo_map) - ibo_map=ibo->Map(); - - return (T *)ibo_map; - } - - template bool WriteIBO(const T *data) - { - if(!ibo)return(false); - if(ibo->GetStride()!=sizeof(T))return(false); - - if(ibo_map) - { - hgl_cpy((T *)ibo_map,data,index_number); - ibo->Unmap(); - ibo_map=nullptr; - } - else - ibo->Write(data); - - return(true); - } + template + bool WriteIBO(const T *data){return WriteIBO(data,index_number*sizeof(T));} virtual Primitive * Finish(RenderResource *); ///<结束并创建可渲染对象 };//class PrimitiveCreater @@ -205,4 +181,41 @@ typedef VABMap VABMap4u32,VABMap4ui; typedef VABMap VABMap4f; typedef VABMap VABMap4d; +/** +* 索引缓冲区映射访问 +*/ +template class IBMap +{ + IBAccess *iba; + T *map_ptr; + +public: + + IBMap(IBAccess *a) + { + iba=a; + + if(iba) + map_ptr=(T *)(iba->buffer->Map(iba->start,iba->count)); + else + map_ptr=nullptr; + } + + IBMap(PrimitiveCreater *pc):IBMap(pc->AcquireIBO()){} + + ~IBMap() + { + if(iba) + iba->buffer->Unmap(); + } + + const bool IsValid()const{ return iba; } + + operator T *(){ return map_ptr; } +};//template class IBMap + +using IBMapU8=IBMap; +using IBMapU16=IBMap; +using IBMapU32=IBMap; + VK_NAMESPACE_END \ No newline at end of file diff --git a/src/SceneGraph/InlineGeometry.cpp b/src/SceneGraph/InlineGeometry.cpp index 1487d418..bc60b030 100644 --- a/src/SceneGraph/InlineGeometry.cpp +++ b/src/SceneGraph/InlineGeometry.cpp @@ -354,8 +354,12 @@ namespace hgl return rc.Finish(db); } - template void CreateSphereIndices(T *tp,uint numberParallels,const uint numberSlices) + template + void CreateSphereIndices(PrimitiveCreater *pc,uint numberParallels,const uint numberSlices) { + IBMap ib_map(pc); + T *tp=ib_map; + for (uint i = 0; i < numberParallels; i++) { for (uint j = 0; j < numberSlices; j++) @@ -524,9 +528,9 @@ namespace hgl { const IndexType index_type=rc.GetIndexType(); - if(index_type==IndexType::U16)CreateSphereIndices(rc.AccessIBO(),numberParallels,numberSlices);else - if(index_type==IndexType::U32)CreateSphereIndices(rc.AccessIBO(),numberParallels,numberSlices);else - if(index_type==IndexType::U8 )CreateSphereIndices(rc.AccessIBO(),numberParallels,numberSlices);else + if(index_type==IndexType::U16)CreateSphereIndices(&rc,numberParallels,numberSlices);else + if(index_type==IndexType::U32)CreateSphereIndices(&rc,numberParallels,numberSlices);else + if(index_type==IndexType::U8 )CreateSphereIndices(&rc,numberParallels,numberSlices);else return(nullptr); } @@ -618,9 +622,9 @@ namespace hgl { const IndexType index_type=rc.GetIndexType(); - if(index_type==IndexType::U16)CreateSphereIndices(rc.AccessIBO(),numberParallels,numberSlices);else - if(index_type==IndexType::U32)CreateSphereIndices(rc.AccessIBO(),numberParallels,numberSlices);else - if(index_type==IndexType::U8 )CreateSphereIndices(rc.AccessIBO(),numberParallels,numberSlices);else + if(index_type==IndexType::U16)CreateSphereIndices(&rc,numberParallels,numberSlices);else + if(index_type==IndexType::U32)CreateSphereIndices(&rc,numberParallels,numberSlices);else + if(index_type==IndexType::U8 )CreateSphereIndices(&rc,numberParallels,numberSlices);else return(nullptr); } @@ -630,8 +634,11 @@ namespace hgl namespace { template - void CreateTorusIndices(T *tp,uint numberSlices,uint numberStacks) + void CreateTorusIndices(PrimitiveCreater *pc,uint numberSlices,uint numberStacks) { + IBMap ib_map(pc); + T *tp=ib_map; + // loop counters uint sideCount, faceCount; @@ -767,9 +774,9 @@ namespace hgl { const IndexType index_type=rc.GetIndexType(); - if(index_type==IndexType::U16)CreateTorusIndices(rc.AccessIBO(),tci->numberSlices,tci->numberStacks);else - if(index_type==IndexType::U32)CreateTorusIndices(rc.AccessIBO(),tci->numberSlices,tci->numberStacks);else - if(index_type==IndexType::U8 )CreateTorusIndices(rc.AccessIBO(),tci->numberSlices,tci->numberStacks);else + if(index_type==IndexType::U16)CreateTorusIndices(&rc,tci->numberSlices,tci->numberStacks);else + if(index_type==IndexType::U32)CreateTorusIndices(&rc,tci->numberSlices,tci->numberStacks);else + if(index_type==IndexType::U8 )CreateTorusIndices(&rc,tci->numberSlices,tci->numberStacks);else return(nullptr); } return rc.Finish(db); @@ -778,8 +785,10 @@ namespace hgl namespace { template - void CreateCylinderIndices(T *tp,const uint numberSlices) + void CreateCylinderIndices(PrimitiveCreater *pc,const uint numberSlices) { + IBMap ib_map(pc); + T *tp=ib_map; uint i; T centerIndex = 0; @@ -1003,9 +1012,9 @@ namespace hgl { const IndexType index_type=rc.GetIndexType(); - if(index_type==IndexType::U16)CreateCylinderIndices(rc.AccessIBO(),cci->numberSlices);else - if(index_type==IndexType::U32)CreateCylinderIndices(rc.AccessIBO(),cci->numberSlices);else - if(index_type==IndexType::U8 )CreateCylinderIndices(rc.AccessIBO(),cci->numberSlices);else + if(index_type==IndexType::U16)CreateCylinderIndices(&rc,cci->numberSlices);else + if(index_type==IndexType::U32)CreateCylinderIndices(&rc,cci->numberSlices);else + if(index_type==IndexType::U8 )CreateCylinderIndices(&rc,cci->numberSlices);else return(nullptr); } @@ -1015,8 +1024,11 @@ namespace hgl namespace { template - void CreateConeIndices(T *tp,const uint numberSlices,const uint numberStacks) + void CreateConeIndices(PrimitiveCreater *pc,const uint numberSlices,const uint numberStacks) { + IBMap ib_map(pc); + T *tp=ib_map; + // Bottom uint centerIndex = 0; uint indexCounter = 1; @@ -1177,9 +1189,9 @@ namespace hgl { const IndexType index_type=rc.GetIndexType(); - if(index_type==IndexType::U16)CreateConeIndices(rc.AccessIBO(),cci->numberSlices,cci->numberStacks);else - if(index_type==IndexType::U32)CreateConeIndices(rc.AccessIBO(),cci->numberSlices,cci->numberStacks);else - if(index_type==IndexType::U8 )CreateConeIndices(rc.AccessIBO(),cci->numberSlices,cci->numberStacks);else + if(index_type==IndexType::U16)CreateConeIndices(&rc,cci->numberSlices,cci->numberStacks);else + if(index_type==IndexType::U32)CreateConeIndices(&rc,cci->numberSlices,cci->numberStacks);else + if(index_type==IndexType::U8 )CreateConeIndices(&rc,cci->numberSlices,cci->numberStacks);else return(nullptr); } diff --git a/src/SceneGraph/PrimitiveCreater.cpp b/src/SceneGraph/PrimitiveCreater.cpp index c2203e62..2138419a 100644 --- a/src/SceneGraph/PrimitiveCreater.cpp +++ b/src/SceneGraph/PrimitiveCreater.cpp @@ -7,6 +7,7 @@ VK_NAMESPACE_BEGIN PrimitiveData *CreatePrimitiveData(const VIL *_vil,const VkDeviceSize vc); +IBAccess * GetIBAccess( PrimitiveData *pd); void SetIndexBuffer( PrimitiveData *pd,IndexBuffer *ib,const VkDeviceSize ic); VABAccess * GetVABAccess( PrimitiveData *pd,const int); void Destory( PrimitiveData *pd); @@ -22,10 +23,9 @@ PrimitiveCreater::PrimitiveCreater(GPUDevice *dev,const VIL *v,const AnsiString vertices_number =0; vab_proc_count =0; - index_number =0; - ibo =nullptr; - ibo_map =nullptr; + index_number =0; + iba =nullptr; } //PrimitiveCreater::PrimitiveCreater(VertexDataManager *_vdm) @@ -67,21 +67,24 @@ bool PrimitiveCreater::Init(const VkDeviceSize vertex_count,const VkDeviceSize i return(false); } - ibo=device->CreateIBO(it,index_count); + iba=GetIBAccess(prim_data); - if(!ibo)return(false); + iba->buffer=device->CreateIBO(it,index_count); + + if(!iba->buffer)return(false); + + iba->start=0; + iba->count=index_count; index_number=index_count; - - SetIndexBuffer(prim_data,ibo,index_count); #ifdef _DEBUG DebugUtils *du=device->GetDebugUtils(); if(du) { - du->SetBuffer( ibo->GetBuffer(), prim_name+":IndexBuffer:Buffer"); - du->SetDeviceMemory(ibo->GetVkMemory(), prim_name+":IndexBuffer:Memory"); + du->SetBuffer( iba->buffer->GetBuffer(), prim_name+":IndexBuffer:Buffer"); + du->SetDeviceMemory(iba->buffer->GetVkMemory(), prim_name+":IndexBuffer:Memory"); } #endif//_DEBUG } @@ -141,15 +144,19 @@ VABAccess *PrimitiveCreater::AcquirePVB(const AnsiString &name,const VkFormat &a return vab_access; } +bool PrimitiveCreater::WriteIBO(const void *data,const VkDeviceSize bytes) +{ + if(!data)return(false); + + if(bytes>0) + if(iba->buffer->GetStride()*index_numberbuffer->Write(data,bytes); +} + void PrimitiveCreater::ClearAllData() { - if(ibo_map) - { - ibo->Unmap(); - ibo_map=nullptr; - } - - ibo=nullptr; + iba=nullptr; if(prim_data) { @@ -165,12 +172,6 @@ Primitive *PrimitiveCreater::Finish(RenderResource *rr) if(vab_proc_count!=si_count) return(nullptr); - if(ibo_map) - { - ibo->Unmap(); - ibo_map=nullptr; - } - Primitive *primitive=rr->CreatePrimitive(prim_name,prim_data); if(!primitive) @@ -179,8 +180,7 @@ Primitive *PrimitiveCreater::Finish(RenderResource *rr) return(nullptr); } - ibo_map=nullptr; - ibo=nullptr; + iba=nullptr; prim_data=nullptr; return primitive; diff --git a/src/SceneGraph/Vulkan/VKPrimitiveData.cpp b/src/SceneGraph/Vulkan/VKPrimitiveData.cpp index c09807be..57e437d6 100644 --- a/src/SceneGraph/Vulkan/VKPrimitiveData.cpp +++ b/src/SceneGraph/Vulkan/VKPrimitiveData.cpp @@ -144,14 +144,14 @@ VABAccess *GetVABAccess(PrimitiveData *pd,const AnsiString &name) // 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; -} +//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; +//} IBAccess *GetIBAccess(PrimitiveData *pd) {