diff --git a/inc/hgl/graph/PrimitiveCreater.h b/inc/hgl/graph/PrimitiveCreater.h index e5de81f9..e5e5d3ee 100644 --- a/inc/hgl/graph/PrimitiveCreater.h +++ b/inc/hgl/graph/PrimitiveCreater.h @@ -4,6 +4,7 @@ #include #include #include +#include namespace hgl { namespace graph @@ -16,16 +17,9 @@ namespace hgl struct PrimitiveVertexBuffer { AnsiString name; - uint binding; - VAD * vad =nullptr; - VBO * vbo =nullptr; - - public: - - ~PrimitiveVertexBuffer() - { - SAFE_CLEAR(vad); - } + int binding; + VBO * vbo; + void * map_data; };//struct PrimitiveVertexBuffer using PVBMap=ObjectMap; @@ -45,7 +39,9 @@ namespace hgl protected: - VAD * CreateVAD(const AnsiString &name); ///<创建一个顶点属性缓冲区 + PrimitiveVertexBuffer *CreatePVB(const AnsiString &,const void *data); ///<创建一个顶点属性数据区 + + void ClearAllData(); public: @@ -62,12 +58,12 @@ namespace hgl if(format!=T::GetVulkanFormat()) return(nullptr); - VAD *vad=this->CreateVAD(name); + PrimitiveVertexBuffer *pvb=this->CreatePVB(name,nullptr); - if(!vad) + if(!pvb) return(nullptr); - T *access=T::Create(vad); + T *access=T::Create(vertices_number,pvb->map_data); access->Begin(); diff --git a/inc/hgl/graph/VKDevice.h b/inc/hgl/graph/VKDevice.h index d68b130d..dcb9c297 100644 --- a/inc/hgl/graph/VKDevice.h +++ b/inc/hgl/graph/VKDevice.h @@ -13,7 +13,6 @@ #include #include #include -#include #include #include #include @@ -117,7 +116,6 @@ public: //Buffer相关 VBO * CreateVBO (VkFormat format, uint32_t count,const void *data, SharingMode sm=SharingMode::Exclusive); VBO * CreateVBO (VkFormat format, uint32_t count, SharingMode sm=SharingMode::Exclusive){return CreateVBO(format,count,nullptr,sm);} - VBO * CreateVBO (const VAD *vad, SharingMode sm=SharingMode::Exclusive){return CreateVBO(vad->GetFormat(),vad->GetCount(),vad->GetData(),sm);} IndexBuffer * CreateIBO (IndexType type, uint32_t count,const void * data, SharingMode sm=SharingMode::Exclusive); IndexBuffer * CreateIBO8 ( uint32_t count,const void * data, SharingMode sm=SharingMode::Exclusive){return CreateIBO(IndexType::U8, count,(void *)data,sm);} diff --git a/inc/hgl/graph/VKRenderResource.h b/inc/hgl/graph/VKRenderResource.h index 04d6d9bf..148c57c6 100644 --- a/inc/hgl/graph/VKRenderResource.h +++ b/inc/hgl/graph/VKRenderResource.h @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -106,7 +105,6 @@ public: // VBO/VAO VBO *CreateVBO(VkFormat format,uint32_t count,const void *data, SharingMode sm=SharingMode::Exclusive); VBO *CreateVBO(VkFormat format,uint32_t count, SharingMode sm=SharingMode::Exclusive){return CreateVBO(format, count, nullptr, sm);} - VBO *CreateVBO(const VAD *vad, SharingMode sm=SharingMode::Exclusive){return CreateVBO(vad->GetFormat(), vad->GetCount(),vad->GetData(), sm);} #define SCENE_DB_CREATE_FUNC(name) DeviceBuffer *Create##name(const AnsiString &buf_name,VkDeviceSize size,void *data,SharingMode sm=SharingMode::Exclusive); \ DeviceBuffer *Create##name(const AnsiString &buf_name,VkDeviceSize size,SharingMode sm=SharingMode::Exclusive){return Create##name(buf_name,size,nullptr,sm);} diff --git a/inc/hgl/graph/VertexAttribData.h b/inc/hgl/graph/VertexAttribData.h deleted file mode 100644 index 28fc001b..00000000 --- a/inc/hgl/graph/VertexAttribData.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef HGL_GRAPH_VERTEX_ATTRIB_DATA_INCLUDE -#define HGL_GRAPH_VERTEX_ATTRIB_DATA_INCLUDE - -#include -namespace hgl -{ - namespace graph - { - /** - * 顶点属性数据 - */ - class VertexAttribData ///顶点属性数据 - { - void *mem_data; ///<内存中的数据 - - protected: - - VkFormat format; - - uint32_t count; ///<数据个数 - uint32_t total_bytes; ///<字节数 - - public: - - VertexAttribData(uint32_t c,const VkFormat vf,const uint32_t t) - { - count=c; - format=vf; - total_bytes=t; - - mem_data = hgl_malloc(total_bytes); //在很多情况下,hgl_malloc分配的内存是对齐的,这样有效率上的提升 - } - - virtual ~VertexAttribData() - { - if(mem_data) - hgl_free(mem_data); - } - - const VkFormat GetFormat ()const{return format;} ///<取得数据类型 - const uint32_t GetCount ()const{return count;} ///<取得数据数量 - void * GetData ()const{return mem_data;} ///<取得数据指针 - const uint32_t GetTotalBytes ()const{return total_bytes;} ///<取得数据字节数 - };//class VertexAttribData - - using VAD=VertexAttribData; - }//namespace graph -}//namespace hgl -#endif//HGL_GRAPH_VERTEX_ATTRIB_DATA_INCLUDE diff --git a/inc/hgl/graph/VertexAttribDataAccess.h b/inc/hgl/graph/VertexAttribDataAccess.h index db79cdad..9b886107 100644 --- a/inc/hgl/graph/VertexAttribDataAccess.h +++ b/inc/hgl/graph/VertexAttribDataAccess.h @@ -5,7 +5,6 @@ #include #include #include -#include #include namespace hgl { @@ -130,14 +129,9 @@ namespace hgl static VkFormat GetVulkanFormat(){return VKFMT;} - static VertexAttribDataAccess1 * Create(VAD *vad) + static VertexAttribDataAccess1 * Create(const VkDeviceSize vertices_number,void *vbo_data) { - if(!vad)return(nullptr); - - if(vad->GetFormat()!=VKFMT) - return(nullptr); - - return(new VertexAttribDataAccess1(vad->GetCount(),(T *)vad->GetData())); + return(new VertexAttribDataAccess1(vertices_number,(T *)vbo_data)); } /** @@ -223,14 +217,9 @@ namespace hgl static VkFormat GetVulkanFormat(){return VKFMT;} - static VertexAttribDataAccess2 * Create(VAD *vad) + static VertexAttribDataAccess2 * Create(const VkDeviceSize vertices_number,void *vbo_data) { - if(!vad)return(nullptr); - - if(vad->GetFormat()!=VKFMT) - return(nullptr); - - return(new VertexAttribDataAccess2(vad->GetCount(),(T *)vad->GetData())); + return(new VertexAttribDataAccess2(vertices_number,(T *)vbo_data)); } /** @@ -539,14 +528,9 @@ namespace hgl static VkFormat GetVulkanFormat(){return VKFMT;} - static VertexAttribDataAccess3 * Create(VAD *vad) + static VertexAttribDataAccess3 * Create(const VkDeviceSize vertices_number,void *vbo_data) { - if(!vad)return(nullptr); - - if(vad->GetFormat()!=VKFMT) - return(nullptr); - - return(new VertexAttribDataAccess3(vad->GetCount(),(T *)vad->GetData())); + return(new VertexAttribDataAccess3(vertices_number,(T *)vbo_data)); } /** @@ -811,14 +795,9 @@ namespace hgl static VkFormat GetVulkanFormat(){return VKFMT;} - static VertexAttribDataAccess4 * Create(VAD *vad) + static VertexAttribDataAccess4 * Create(const VkDeviceSize vertices_number,void *vbo_data) { - if(!vad)return(nullptr); - - if(vad->GetFormat()!=VKFMT) - return(nullptr); - - return(new VertexAttribDataAccess4(vad->GetCount(),(T *)vad->GetData())); + return(new VertexAttribDataAccess4(vertices_number,(T *)vbo_data)); } /** diff --git a/src/SceneGraph/CMakeLists.txt b/src/SceneGraph/CMakeLists.txt index 25e44e34..4e8a5a28 100644 --- a/src/SceneGraph/CMakeLists.txt +++ b/src/SceneGraph/CMakeLists.txt @@ -7,12 +7,8 @@ SET(SG_TEXTURE_SOURCE ${SG_INCLUDE_PATH}/TextureLoader.h SOURCE_GROUP("Texture" FILES ${SG_TEXTURE_SOURCE}) -SET(SG_VAD_SOURCE ${SG_INCLUDE_PATH}/VertexAttribData.h - ${SG_INCLUDE_PATH}/VertexAttribDataAccess.h) - -SOURCE_GROUP("VertexAttribData" FILES ${SG_VAD_SOURCE}) - -SET(SG_VDM_SOURCE ${SG_INCLUDE_PATH}/VertexDataManager.h) +SET(SG_VDM_SOURCE ${SG_INCLUDE_PATH}/VertexAttribDataAccess.h + ${SG_INCLUDE_PATH}/VertexDataManager.h) SOURCE_GROUP("VertexDataManager" FILES ${SG_VDM_SOURCE}) @@ -305,7 +301,6 @@ add_cm_library(ULRE.SceneGraph "ULRE" ${SCENE_GRAPH_HEADER} ${SG_TEXTURE_SOURCE} ${TILE_SOURCE} - ${SG_VAD_SOURCE} ${SG_VDM_SOURCE} # ${FONT_MANAGE_SOURCE} diff --git a/src/SceneGraph/PrimitiveCreater.cpp b/src/SceneGraph/PrimitiveCreater.cpp index 74b596ef..5c12449d 100644 --- a/src/SceneGraph/PrimitiveCreater.cpp +++ b/src/SceneGraph/PrimitiveCreater.cpp @@ -5,22 +5,6 @@ namespace hgl { namespace graph { - /** - * 根据格式要求,创建对应的顶点属性数据区(VAD) - * @param vertex_count 顶点数量 - * @param vif 格式 - */ - VAD *CreateVertexAttribData(const uint32_t vertex_count,const VertexInputFormat *vif) - { - if(vertex_count<=0 - ||vif->vec_size<1||vif->vec_size>4 - ||vif->stride<1||vif->stride>8*4 - ||!CheckVulkanFormat(vif->format)) - return(nullptr); - - return(new VertexAttribData(vertex_count,vif->format,vif->stride*vertex_count)); - } - PrimitiveCreater::PrimitiveCreater(RenderResource *sdb,const VIL *v) { db =sdb; @@ -39,7 +23,7 @@ namespace hgl return(true); } - VAD *PrimitiveCreater::CreateVAD(const AnsiString &name) + PrimitiveCreater::PrimitiveVertexBuffer *PrimitiveCreater::CreatePVB(const AnsiString &name,const void *data) { if(!vil)return(nullptr); if(name.IsEmpty())return(nullptr); @@ -52,24 +36,22 @@ namespace hgl PrimitiveVertexBuffer *pvb; if(vbo_map.Get(name,pvb)) - return pvb->vad; - - VAD *vad=hgl::graph::CreateVertexAttribData(vertices_number,vif); - - if(!vad) - return(nullptr); + return pvb; pvb=new PrimitiveVertexBuffer; - pvb->vad =vad; pvb->name =name; pvb->binding=vif->binding; - - pvb->vbo =nullptr; + pvb->vbo =db->CreateVBO(vif->format,vertices_number,data); + + if(!data) + pvb->map_data=pvb->vbo->Map(); + else + pvb->map_data=nullptr; vbo_map.Add(name,pvb); - return pvb->vad; + return pvb; } bool PrimitiveCreater::WriteVAD(const AnsiString &name,const void *data,const uint32_t bytes) @@ -79,11 +61,6 @@ namespace hgl if(!data)return(false); if(!bytes)return(false); - PrimitiveVertexBuffer *pvb; - - if(vbo_map.Get(name,pvb)) - return false; - const VertexInputFormat *vif=vil->GetConfig(name); if(!vif) @@ -91,18 +68,33 @@ namespace hgl if(vif->stride*vertices_number!=bytes) return(false); - - pvb=new PrimitiveVertexBuffer; - pvb->vad =nullptr; - pvb->name =name; - pvb->binding=vif->binding; + return CreatePVB(name,data); + } - pvb->vbo =db->CreateVBO(vif->format,vertices_number,data); + void PrimitiveCreater::ClearAllData() + { + if(vbo_map.GetCount()>0) + { + const auto *sp=vbo_map.GetDataList(); + for(uint i=0;ivalue->vbo) + { + (*sp)->value->vbo->Unmap(); + delete (*sp)->value->vbo; + } + + ++sp; + } + } - vbo_map.Add(name,pvb); - - return true; + if(ibo) + { + ibo->Unmap(); + delete ibo; + ibo=nullptr; + } } Primitive *PrimitiveCreater::Finish(const AnsiString &prim_name) @@ -118,9 +110,17 @@ namespace hgl for(uint i=0;ivalue->vbo) + { + if((*sp)->value->map_data) + (*sp)->value->vbo->Unmap(); + primitive->Set((*sp)->key,(*sp)->value->vbo); + } else - primitive->Set((*sp)->key,db->CreateVBO((*sp)->value->vad)); + { + //ClearAllData(); + return(nullptr); + } ++sp; }