diff --git a/inc/hgl/graph/PrimitiveCreater.h b/inc/hgl/graph/PrimitiveCreater.h index 44da7c9b..1901217f 100644 --- a/inc/hgl/graph/PrimitiveCreater.h +++ b/inc/hgl/graph/PrimitiveCreater.h @@ -56,7 +56,7 @@ public: //顶点缓冲区 const uint32_t GetVertexCount()const{ return vertices_number; } ///<取得顶点数量 - VABMap * MapVAB (const AnsiString &name,const VkFormat format=VK_FORMAT_UNDEFINED); + VABMap * GetVABMap (const AnsiString &name,const VkFormat format=VK_FORMAT_UNDEFINED); bool WriteVAB (const AnsiString &name,const VkFormat format,const void *data); ///<直接写入顶点属性数据 @@ -65,7 +65,7 @@ public: //索引缓冲区 const IndexType GetIndexType()const{return index_type;} ///<取得索引类型 const uint32_t GetIndexCount()const{return index_number;} ///<取得索引数量 - IBMap * MapIBO(); + IBMap * GetIBMap(); bool WriteIBO(const void *data,const uint32_t count); diff --git a/inc/hgl/graph/VK.h b/inc/hgl/graph/VK.h index c7070b52..551e3022 100644 --- a/inc/hgl/graph/VK.h +++ b/inc/hgl/graph/VK.h @@ -62,6 +62,9 @@ using VAB=VertexAttribBuffer; class IndexBuffer; +class VABMap; +class IBMap; + class GPUCmdBuffer; class RenderCmdBuffer; class TextureCmdBuffer; diff --git a/inc/hgl/graph/VKPrimitive.h b/inc/hgl/graph/VKPrimitive.h index 3f2527cd..8f8569f8 100644 --- a/inc/hgl/graph/VKPrimitive.h +++ b/inc/hgl/graph/VKPrimitive.h @@ -6,6 +6,7 @@ #include VK_NAMESPACE_BEGIN + /** * 原始图元数据访问接口
* Primitive的存为是为了屏蔽PrimitiveData的初始化之类的访问接口,以便于更好的管理和使用 @@ -37,10 +38,13 @@ public: VAB * GetVAB (const int); VAB * GetVAB (const AnsiString &name){return GetVAB(GetVABIndex(name));} const int32_t GetVertexOffset ()const; ///<取得顶点偏移(注意是顶点不是字节) + VABMap * GetVABMap (const int); ///<取得VAB映射器 + VABMap * GetVABMap (const AnsiString &name){return GetVABMap(GetVABIndex(name));} const uint32_t GetIndexCount ()const; IndexBuffer * GetIBO (); const uint32_t GetFirstIndex ()const; ///<取得第一个索引 + IBMap * GetIBMap (); ///<取得索引缓冲区映射器 VertexDataManager * GetVDM (); ///<取得顶点数据管理器 diff --git a/inc/hgl/graph/VKVertexAttribBuffer.h b/inc/hgl/graph/VKVertexAttribBuffer.h index 278ff86d..46d499de 100644 --- a/inc/hgl/graph/VKVertexAttribBuffer.h +++ b/inc/hgl/graph/VKVertexAttribBuffer.h @@ -68,15 +68,24 @@ template class VABRawMap public: - VABRawMap(VABMap *map,const VkFormat check_format=VK_FORMAT_UNDEFINED) + VABRawMap(VABMap *map,const VkFormat check_format=VK_FORMAT_UNDEFINED,bool now_map=true) { vab_map=map; map_ptr=nullptr; if(vab_map) + { if(check_format==VK_FORMAT_UNDEFINED ||check_format==vab_map->GetFormat()) - map_ptr=(T *)(vab_map->Map()); + { + if(now_map) + map_ptr=(T *)(vab_map->Map()); + } + else + { + vab_map=nullptr; + } + } } ~VABRawMap() @@ -87,6 +96,26 @@ public: const bool IsValid()const{ return vab_map?vab_map->IsValid():false; } + T *Map() + { + if(!vab_map) + return(nullptr); + + if(!map_ptr) + map_ptr=(T *)(vab_map->Map()); + + return map_ptr; + } + + void Unmap() + { + if(vab_map) + { + if(map_ptr) + vab_map->Unmap(); + } + } + operator T *(){ return map_ptr; } T *operator->(){ return map_ptr; } };//template class VABRawMap @@ -111,14 +140,21 @@ template class VABFormatMap public: - VABFormatMap(VABMap *map) + VABFormatMap(VABMap *map,bool now_map=true) { vab_map=map; if(vab_map&&vab_map->GetFormat()==T::GetVulkanFormat()) { - map_ptr=T::Create(vab_map->GetCount(),vab_map->Map()); - map_ptr->Begin(); + if(now_map) + { + map_ptr=T::Create(vab_map->GetCount(),vab_map->Map()); + map_ptr->Begin(); + } + else + { + map_ptr=T::Create(vab_map->GetCount(),nullptr); + } } else map_ptr=nullptr; @@ -133,15 +169,48 @@ public: } } - const bool IsValid()const{ return map_ptr; } + const bool IsValid()const{ return map_ptr?map_ptr->IsValid():false; } + + T *Map() + { + if(!vab_map) + return(nullptr); + + if(!map_ptr) + { + map_ptr=T::Create(vab_map->GetCount(),vab_map->Map()); + } + else + { + if(!map_ptr->IsValid()) + map_ptr->SetData(vab_map->Map()); + } + + map_ptr->Begin(); + return map_ptr; + } + + void Unmap() + { + if(vab_map) + { + if(map_ptr&&map_ptr->IsValid()) + { + vab_map->Unmap(); + map_ptr->SetData(nullptr); + } + } + } void Restart() { - if(map_ptr) - vab_map->Begin(); + return Map(); } - T *operator->(){ return map_ptr; } + T *operator->() + { + return map_ptr; + } };//template class VABFormatMap typedef VABFormatMap VABMap1i8 ,VABMap1b; diff --git a/inc/hgl/graph/VertexAttribDataAccess.h b/inc/hgl/graph/VertexAttribDataAccess.h index 848544c6..b7f535ef 100644 --- a/inc/hgl/graph/VertexAttribDataAccess.h +++ b/inc/hgl/graph/VertexAttribDataAccess.h @@ -44,6 +44,17 @@ namespace hgl virtual ~VertexAttribDataAccess()=default; + void SetData(T *_data) + { + data =_data; + data_end=_data+count*C; + } + + const bool IsValid()const + { + return data; + } + void Write(const T *ptr) { if(!ptr)return; diff --git a/src/SceneGraph/InlineGeometry.cpp b/src/SceneGraph/InlineGeometry.cpp index 8e11b191..c0997718 100644 --- a/src/SceneGraph/InlineGeometry.cpp +++ b/src/SceneGraph/InlineGeometry.cpp @@ -20,7 +20,7 @@ namespace hgl if(!pc->Init("Rectangle",4,0)) return(nullptr); - VABMap2f vertex(pc->MapVAB(VAN::Position)); + VABMap2f vertex(pc->GetVABMap(VAN::Position)); if(!vertex.IsValid()) return(nullptr); @@ -48,7 +48,7 @@ namespace hgl if(!pc->Init("RoundRectangle",4,0)) return(nullptr); - VABMap2f vertex(pc->MapVAB(VAN::Position)); + VABMap2f vertex(pc->GetVABMap(VAN::Position)); vertex->WriteRectFan(rci->scope); } @@ -62,7 +62,7 @@ namespace hgl if(!pc->Init("RoundRectangle",rci->round_per*4,8)) return(nullptr); - VABMap2f vertex(pc->MapVAB(VAN::Position)); + VABMap2f vertex(pc->GetVABMap(VAN::Position)); Vector2f *coord=new Vector2f[rci->round_per]; @@ -133,8 +133,8 @@ namespace hgl if(!pc->Init("Circle",vertex_count,0))return(nullptr); - VABMap2f vertex(pc->MapVAB(VAN::Position)); - VABMap4f color(pc->MapVAB(VAN::Color)); + VABMap2f vertex(pc->GetVABMap(VAN::Position)); + VABMap4f color(pc->GetVABMap(VAN::Color)); if(!vertex.IsValid()) return(nullptr); @@ -169,7 +169,7 @@ namespace hgl if(!pc->Init("PlaneGrid",((pgci->grid_size.Width()+1)+(pgci->grid_size.Height()+1))*2,0)) return(nullptr); - VABMap2f vertex(pc->MapVAB(VAN::Position)); + VABMap2f vertex(pc->GetVABMap(VAN::Position)); if(!vertex.IsValid()) return(nullptr); @@ -192,7 +192,7 @@ namespace hgl Vector2f(left+col,bottom)); } - VABMap1f lum(pc->MapVAB(VAN::Luminance)); + VABMap1f lum(pc->GetVABMap(VAN::Luminance)); if(lum.IsValid()) { @@ -232,21 +232,21 @@ namespace hgl return(nullptr); { - VABMap3f normal(pc->MapVAB(VAN::Normal)); + VABMap3f normal(pc->GetVABMap(VAN::Normal)); if(normal.IsValid()) normal->RepeatWrite(xy_normal,4); } { - VABMap3f tangent(pc->MapVAB(VAN::Tangent)); + VABMap3f tangent(pc->GetVABMap(VAN::Tangent)); if(tangent.IsValid()) tangent->RepeatWrite(xy_tangent,4); } { - VABMap2f tex_coord(pc->MapVAB(VAN::TexCoord)); + VABMap2f tex_coord(pc->GetVABMap(VAN::TexCoord)); if(tex_coord.IsValid()) tex_coord->Write(xy_tex_coord,4); @@ -332,7 +332,7 @@ namespace hgl { RANGE_CHECK_RETURN_NULLPTR(cci->color_type); - VABMap4f color(pc->MapVAB(VAN::Color)); + VABMap4f color(pc->GetVABMap(VAN::Color)); if(color.IsValid()) { @@ -360,7 +360,7 @@ namespace hgl template void CreateSphereIndices(PrimitiveCreater *pc,uint numberParallels,const uint numberSlices) { - IBTypeMap ib_map(pc->MapIBO()); + IBTypeMap ib_map(pc->GetIBMap()); T *tp=ib_map; for (uint i = 0; i < numberParallels; i++) @@ -473,10 +473,10 @@ namespace hgl if(!pc->Init("Sphere",numberVertices,numberIndices)) return(nullptr); - VABMapFloat vertex (pc->MapVAB(VAN::Position),VF_V3F); - VABMapFloat normal (pc->MapVAB(VAN::Normal),VF_V3F); - VABMapFloat tangent (pc->MapVAB(VAN::Tangent),VF_V3F); - VABMapFloat tex_coord(pc->MapVAB(VAN::TexCoord),VF_V2F); + VABMapFloat vertex (pc->GetVABMap(VAN::Position),VF_V3F); + VABMapFloat normal (pc->GetVABMap(VAN::Normal),VF_V3F); + VABMapFloat tangent (pc->GetVABMap(VAN::Tangent),VF_V3F); + VABMapFloat tex_coord(pc->GetVABMap(VAN::TexCoord),VF_V2F); float *vp=vertex; float *np=normal; @@ -562,10 +562,10 @@ namespace hgl if(!pc->Init("Dome",numberVertices,numberIndices)) return(nullptr); - VABMapFloat vertex (pc->MapVAB(VAN::Position),VF_V3F); - VABMapFloat normal (pc->MapVAB(VAN::Normal),VF_V3F); - VABMapFloat tangent (pc->MapVAB(VAN::Tangent),VF_V3F); - VABMapFloat tex_coord(pc->MapVAB(VAN::TexCoord),VF_V2F); + VABMapFloat vertex (pc->GetVABMap(VAN::Position),VF_V3F); + VABMapFloat normal (pc->GetVABMap(VAN::Normal),VF_V3F); + VABMapFloat tangent (pc->GetVABMap(VAN::Tangent),VF_V3F); + VABMapFloat tex_coord(pc->GetVABMap(VAN::TexCoord),VF_V2F); float *vp=vertex; float *np=normal; @@ -637,7 +637,7 @@ namespace hgl template void CreateTorusIndices(PrimitiveCreater *pc,uint numberSlices,uint numberStacks) { - IBTypeMap ib_map(pc->MapIBO()); + IBTypeMap ib_map(pc->GetIBMap()); T *tp=ib_map; // loop counters @@ -710,10 +710,10 @@ namespace hgl if(!pc->Init("Torus",numberVertices,numberIndices)) return(nullptr); - VABMapFloat vertex (pc->MapVAB(VAN::Position),VF_V3F); - VABMapFloat normal (pc->MapVAB(VAN::Normal),VF_V3F); - VABMapFloat tangent (pc->MapVAB(VAN::Tangent),VF_V3F); - VABMapFloat tex_coord(pc->MapVAB(VAN::TexCoord),VF_V2F); + VABMapFloat vertex (pc->GetVABMap(VAN::Position),VF_V3F); + VABMapFloat normal (pc->GetVABMap(VAN::Normal),VF_V3F); + VABMapFloat tangent (pc->GetVABMap(VAN::Tangent),VF_V3F); + VABMapFloat tex_coord(pc->GetVABMap(VAN::TexCoord),VF_V2F); float *vp=vertex; float *np=normal; @@ -788,7 +788,7 @@ namespace hgl template void CreateCylinderIndices(PrimitiveCreater *pc,const uint numberSlices) { - IBTypeMap ib_map(pc->MapIBO()); + IBTypeMap ib_map(pc->GetIBMap()); T *tp=ib_map; uint i; @@ -852,10 +852,10 @@ namespace hgl if (cci->numberSlices < 3 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES) return nullptr; - VABMapFloat vertex (pc->MapVAB(VAN::Position),VF_V3F); - VABMapFloat normal (pc->MapVAB(VAN::Normal),VF_V3F); - VABMapFloat tangent (pc->MapVAB(VAN::Tangent),VF_V3F); - VABMapFloat tex_coord(pc->MapVAB(VAN::TexCoord),VF_V2F); + VABMapFloat vertex (pc->GetVABMap(VAN::Position),VF_V3F); + VABMapFloat normal (pc->GetVABMap(VAN::Normal),VF_V3F); + VABMapFloat tangent (pc->GetVABMap(VAN::Tangent),VF_V3F); + VABMapFloat tex_coord(pc->GetVABMap(VAN::TexCoord),VF_V2F); float *vp=vertex; float *np=normal; @@ -1025,7 +1025,7 @@ namespace hgl template void CreateConeIndices(PrimitiveCreater *pc,const uint numberSlices,const uint numberStacks) { - IBTypeMap ib_map(pc->MapIBO()); + IBTypeMap ib_map(pc->GetIBMap()); T *tp=ib_map; // Bottom @@ -1082,10 +1082,10 @@ namespace hgl if (cci->numberSlices < 3 || cci->numberStacks < 1 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES) return nullptr; - VABMapFloat vertex (pc->MapVAB(VAN::Position),VF_V3F); - VABMapFloat normal (pc->MapVAB(VAN::Normal),VF_V3F); - VABMapFloat tangent (pc->MapVAB(VAN::Tangent),VF_V3F); - VABMapFloat tex_coord(pc->MapVAB(VAN::TexCoord),VF_V2F); + VABMapFloat vertex (pc->GetVABMap(VAN::Position),VF_V3F); + VABMapFloat normal (pc->GetVABMap(VAN::Normal),VF_V3F); + VABMapFloat tangent (pc->GetVABMap(VAN::Tangent),VF_V3F); + VABMapFloat tex_coord(pc->GetVABMap(VAN::TexCoord),VF_V2F); float *vp=vertex; float *np=normal; @@ -1204,8 +1204,8 @@ namespace hgl if(!pc->Init("Axis",6,0)) return(nullptr); - VABMap3f vertex(pc->MapVAB(VAN::Position)); - VABMap4f color(pc->MapVAB(VAN::Color)); + VABMap3f vertex(pc->GetVABMap(VAN::Position)); + VABMap4f color(pc->GetVABMap(VAN::Color)); if(!vertex.IsValid()||!color.IsValid()) return(nullptr); @@ -1256,7 +1256,7 @@ namespace hgl { RANGE_CHECK_RETURN_NULLPTR(cci->color_type); - VABMap4f color(pc->MapVAB(VAN::Color)); + VABMap4f color(pc->GetVABMap(VAN::Color)); if(color.IsValid()) { diff --git a/src/SceneGraph/PrimitiveCreater.cpp b/src/SceneGraph/PrimitiveCreater.cpp index fc4b3a5e..61dec20d 100644 --- a/src/SceneGraph/PrimitiveCreater.cpp +++ b/src/SceneGraph/PrimitiveCreater.cpp @@ -154,7 +154,7 @@ const int PrimitiveCreater::InitVAB(const AnsiString &name,const VkFormat format return(vab_index); } -VABMap *PrimitiveCreater::MapVAB(const AnsiString &name,const VkFormat format) +VABMap *PrimitiveCreater::GetVABMap(const AnsiString &name,const VkFormat format) { const int vab_index=InitVAB(name,format,nullptr); @@ -171,12 +171,12 @@ bool PrimitiveCreater::WriteVAB(const AnsiString &name,const VkFormat format,con return InitVAB(name,format,data)>0; } -IBMap *PrimitiveCreater::MapIBO() +IBMap *PrimitiveCreater::GetIBMap() { if(!prim_data) return(nullptr); - return prim_data->MapIBO(); + return prim_data->GetIBMap(); } bool PrimitiveCreater::WriteIBO(const void *data,const uint32_t count) diff --git a/src/SceneGraph/Vulkan/VKPrimitive.cpp b/src/SceneGraph/Vulkan/VKPrimitive.cpp index c2462bc7..ab20c55f 100644 --- a/src/SceneGraph/Vulkan/VKPrimitive.cpp +++ b/src/SceneGraph/Vulkan/VKPrimitive.cpp @@ -66,6 +66,11 @@ const int32_t Primitive::GetVertexOffset()const return prim_data->GetVertexOffset(); } +VABMap *Primitive::GetVABMap(const int vab_index) +{ + return prim_data->GetVABMap(vab_index); +} + const uint32_t Primitive::GetIndexCount()const { return prim_data->GetIndexCount(); @@ -81,6 +86,11 @@ const uint32_t Primitive::GetFirstIndex()const return prim_data->GetFirstIndex(); } +IBMap *Primitive::GetIBMap() +{ + return prim_data->GetIBMap(); +} + VertexDataManager *Primitive::GetVDM() { return prim_data->GetVDM(); diff --git a/src/SceneGraph/Vulkan/VKPrimitiveData.h b/src/SceneGraph/Vulkan/VKPrimitiveData.h index 49360c30..378c9904 100644 --- a/src/SceneGraph/Vulkan/VKPrimitiveData.h +++ b/src/SceneGraph/Vulkan/VKPrimitiveData.h @@ -56,7 +56,7 @@ public: IndexBuffer * InitIBO(const uint32_t index_count,IndexType it); IndexBuffer * GetIBO (){return ibo;} - IBMap * MapIBO (){return &ibo_map;} + IBMap * GetIBMap (){return &ibo_map;} uint32_t GetIndexCount ()const{return index_count;} virtual int32_t GetVertexOffset ()const=0; ///<取得顶点偏移(注意是顶点不是字节)