diff --git a/inc/hgl/graph/PrimitiveCreater.h b/inc/hgl/graph/PrimitiveCreater.h index 4575e2db..057e6238 100644 --- a/inc/hgl/graph/PrimitiveCreater.h +++ b/inc/hgl/graph/PrimitiveCreater.h @@ -1,98 +1,92 @@ -#ifndef HGL_GRAPH_PRIMITIVE_CREATER_INCLUDE -#define HGL_GRAPH_PRIMITIVE_CREATER_INCLUDE +#pragma once #include #include #include #include -namespace hgl +VK_NAMESPACE_BEGIN +class VertexDataManager; + +/** + * 可绘制图元创建器 + */ +class PrimitiveCreater { - namespace graph - { - class VertexDataManager; +protected: - /** - * 可绘制图元创建器 - */ - class PrimitiveCreater - { - protected: + GPUDevice *device; + const GPUPhysicalDevice *phy_device; - GPUDevice *device; - const GPUPhysicalDevice *phy_device; + VertexDataManager *vdm; + RenderResource *db; - VertexDataManager *vdm; - RenderResource *db; + const VIL *vil; - const VIL *vil; +protected: - protected: + VkDeviceSize vertices_number; + VkDeviceSize index_number; - VkDeviceSize vertices_number; - VkDeviceSize index_number; + IndexBuffer * ibo; + void * ibo_map; + VABAccessMap vab_map; - IndexBuffer * ibo; - void * ibo_map; - VABAccessMap vab_map; +protected: - protected: + bool AcquirePVB(VABAccess *,const AnsiString &,const void *data); ///<请求一个顶点属性数据区 - bool AcquirePVB(VABAccess *,const AnsiString &,const void *data); ///<请求一个顶点属性数据区 + void ClearAllData(); - void ClearAllData(); +public: - public: + PrimitiveCreater(RenderResource *sdb,const VIL *); + PrimitiveCreater(VertexDataManager *); + virtual ~PrimitiveCreater(); - PrimitiveCreater(RenderResource *sdb,const VIL *); - PrimitiveCreater(VertexDataManager *); - virtual ~PrimitiveCreater(); + virtual bool Init(const uint32 vertices_count,const uint32 index_count,IndexType it=IndexType::AUTO); ///<初始化,参数为顶点数量 - virtual bool Init(const uint32 vertices_count,const uint32 index_count,IndexType it=IndexType::AUTO); ///<初始化,参数为顶点数量 + template + T * AccessVAB(const AnsiString &name) ///<创建一个顶点属性数据缓冲区以及访问器 + { + const VkFormat format=vil->GetVulkanFormat(name); - template - T * AccessVAB(const AnsiString &name) ///<创建一个顶点属性数据缓冲区以及访问器 - { - const VkFormat format=vil->GetVulkanFormat(name); + if(format!=T::GetVulkanFormat()) + return(nullptr); - if(format!=T::GetVulkanFormat()) - return(nullptr); + VABAccess vad; + if(!this->AcquirePVB(&vad,name,nullptr)) + return(nullptr); - VABAccess vad; - if(!this->AcquirePVB(&vad,name,nullptr)) - return(nullptr); + T *access=T::Create(vertices_number,vad.map_ptr); - T *access=T::Create(vertices_number,vad.map_ptr); + access->Begin(); - access->Begin(); + return access; + } - return access; - } + bool WriteVAB(const AnsiString &name,const void *data,const uint32_t bytes); ///<直接写入顶点属性数据 - bool WriteVAB(const AnsiString &name,const void *data,const uint32_t bytes); ///<直接写入顶点属性数据 + const IndexType GetIndexType()const{return ibo?ibo->GetType():IndexType::ERR;} ///<取得索引数据类型 - const IndexType GetIndexType()const{return ibo?ibo->GetType():IndexType::ERR;} ///<取得索引数据类型 + template T *AccessIBO() + { + if(!ibo)return(nullptr); + if(ibo->GetStride()!=sizeof(T))return(nullptr); - template T *AccessIBO() - { - if(!ibo)return(nullptr); - if(ibo->GetStride()!=sizeof(T))return(nullptr); + return (T *)ibo_map; + } - return (T *)ibo_map; - } + template bool WriteIBO(const T *data) + { + if(!ibo)return(false); + if(ibo->GetStride()!=sizeof(T))return(false); - template bool WriteIBO(const T *data) - { - if(!ibo)return(false); - if(ibo->GetStride()!=sizeof(T))return(false); + hgl_cpy((T *)ibo_map,data,index_number); - hgl_cpy((T *)ibo_map,data,index_number); + return(true); + } - return(true); - } - - virtual Primitive * Finish(const AnsiString &); ///<结束并创建可渲染对象 - };//class PrimitiveCreater - }//namespace graph -}//namespace hgl -#endif//HGL_GRAPH_PRIMITIVE_CREATER_INCLUDE + virtual Primitive * Finish(const AnsiString &); ///<结束并创建可渲染对象 +};//class PrimitiveCreater +VK_NAMESPACE_END \ No newline at end of file diff --git a/src/SceneGraph/PrimitiveCreater.cpp b/src/SceneGraph/PrimitiveCreater.cpp index 9ddd1393..5169dcd5 100644 --- a/src/SceneGraph/PrimitiveCreater.cpp +++ b/src/SceneGraph/PrimitiveCreater.cpp @@ -4,199 +4,195 @@ #include #include -namespace hgl +VK_NAMESPACE_BEGIN +PrimitiveCreater::PrimitiveCreater(RenderResource *sdb,const VIL *v) { - namespace graph + device =sdb->GetDevice(); + phy_device =device->GetPhysicalDevice(); + + vdm =nullptr; + db =sdb; + vil =v; + + vertices_number =0; + index_number =0; + ibo =nullptr; + ibo_map =nullptr; +} + +PrimitiveCreater::PrimitiveCreater(VertexDataManager *_vdm) +{ + device =_vdm->GetDevice(); + phy_device =device->GetPhysicalDevice(); + + vdm =_vdm; + db =nullptr; + vil =vdm->GetVIL(); + + vertices_number =0; + index_number =0; + ibo =nullptr; + ibo_map =nullptr; +} + +PrimitiveCreater::~PrimitiveCreater() +{ + if(ibo) { - PrimitiveCreater::PrimitiveCreater(RenderResource *sdb,const VIL *v) + ibo->Unmap(); + delete ibo; + } +} + +bool PrimitiveCreater::Init(const uint32 vertex_count,const uint32 index_count,IndexType it) +{ + if(vertex_count<=0)return(false); + + vertices_number=vertex_count; + + if(index_count>0) + { + index_number=index_count; + + if(vdm) { - device =sdb->GetDevice(); - phy_device =device->GetPhysicalDevice(); - - vdm =nullptr; - db =sdb; - vil =v; - - vertices_number =0; - index_number =0; - ibo =nullptr; - ibo_map =nullptr; } - - PrimitiveCreater::PrimitiveCreater(VertexDataManager *_vdm) + else { - device =_vdm->GetDevice(); - phy_device =device->GetPhysicalDevice(); - - vdm =_vdm; - db =nullptr; - vil =vdm->GetVIL(); - - vertices_number =0; - index_number =0; - ibo =nullptr; - ibo_map =nullptr; - } - - PrimitiveCreater::~PrimitiveCreater() - { - if(ibo) + if(it==IndexType::AUTO) { - ibo->Unmap(); - delete ibo; + it=device->ChooseIndexType(vertex_count); + + if(!IsIndexType(it)) + return(false); } - } - - bool PrimitiveCreater::Init(const uint32 vertex_count,const uint32 index_count,IndexType it) - { - if(vertex_count<=0)return(false); - - vertices_number=vertex_count; - - if(index_count>0) - { - index_number=index_count; - - if(vdm) - { - } - else - { - if(it==IndexType::AUTO) - { - it=device->ChooseIndexType(vertex_count); - - if(!IsIndexType(it)) - return(false); - } - else - { - if(!device->CheckIndexType(it,vertex_count)) - return(false); - } - - ibo=device->CreateIBO(it,index_count); - //ibo=db->CreateIBO(it,index_count); - if(!ibo)return(false); - } - - ibo_map=ibo->Map(); - } - - return(true); - } - - bool PrimitiveCreater::AcquirePVB(VABAccess *vad,const AnsiString &name,const void *data) - { - if(!vad)return(false); - if(!vil)return(false); - if(name.IsEmpty())return(false); - - const VertexInputFormat *vif=vil->GetConfig(name); - - if(!vif) - return(false); - - if(vab_map.Get(name,*vad)) - return true; - - vad->vab =db->CreateVAB(vif->format,vertices_number,data); - - if(!data) - vad->map_ptr=vad->vab->Map(); else - vad->map_ptr=nullptr; + { + if(!device->CheckIndexType(it,vertex_count)) + return(false); + } - vab_map.Add(name,*vad); - - return true; + ibo=device->CreateIBO(it,index_count); + //ibo=db->CreateIBO(it,index_count); + if(!ibo)return(false); } - bool PrimitiveCreater::WriteVAB(const AnsiString &name,const void *data,const uint32_t bytes) - { - if(!vil)return(false); - if(name.IsEmpty())return(false); - if(!data)return(false); - if(!bytes)return(false); + ibo_map=ibo->Map(); + } + + return(true); +} + +bool PrimitiveCreater::AcquirePVB(VABAccess *vad,const AnsiString &name,const void *data) +{ + if(!vad)return(false); + if(!vil)return(false); + if(name.IsEmpty())return(false); - const VertexInputFormat *vif=vil->GetConfig(name); + const VertexInputFormat *vif=vil->GetConfig(name); - if(!vif) - return(false); + if(!vif) + return(false); - if(vif->stride*vertices_number!=bytes) - return(false); + if(vab_map.Get(name,*vad)) + return true; - VABAccess vad; + vad->vab =db->CreateVAB(vif->format,vertices_number,data); - return AcquirePVB(&vad,name,data); - } + if(!data) + vad->map_ptr=vad->vab->Map(); + else + vad->map_ptr=nullptr; - void PrimitiveCreater::ClearAllData() + vab_map.Add(name,*vad); + + return true; +} + +bool PrimitiveCreater::WriteVAB(const AnsiString &name,const void *data,const uint32_t bytes) +{ + if(!vil)return(false); + if(name.IsEmpty())return(false); + if(!data)return(false); + if(!bytes)return(false); + + const VertexInputFormat *vif=vil->GetConfig(name); + + if(!vif) + return(false); + + if(vif->stride*vertices_number!=bytes) + return(false); + + VABAccess vad; + + return AcquirePVB(&vad,name,data); +} + +void PrimitiveCreater::ClearAllData() +{ + if(vab_map.GetCount()>0) + { + const auto *sp=vab_map.GetDataList(); + for(int i=0;i0) + if((*sp)->value.vab) { - const auto *sp=vab_map.GetDataList(); - for(int i=0;ivalue.vab) - { - (*sp)->value.vab->Unmap(); - delete (*sp)->value.vab; - } + (*sp)->value.vab->Unmap(); + delete (*sp)->value.vab; + } - ++sp; - } - } - - if(ibo) - { - ibo->Unmap(); - delete ibo; - ibo=nullptr; - } + ++sp; } + } - Primitive *PrimitiveCreater::Finish(const AnsiString &prim_name) + if(ibo) + { + ibo->Unmap(); + delete ibo; + ibo=nullptr; + } +} + +Primitive *PrimitiveCreater::Finish(const AnsiString &prim_name) +{ + const uint si_count=vil->GetCount(VertexInputGroup::Basic); + + if(vab_map.GetCount()!=si_count) + return(nullptr); + + Primitive *primitive=db->CreatePrimitive(prim_name,vertices_number); + + const auto *sp=vab_map.GetDataList(); + for(uint i=0;ivalue.vab) { - const uint si_count=vil->GetCount(VertexInputGroup::Basic); + if((*sp)->value.map_ptr) + (*sp)->value.vab->Unmap(); - if(vab_map.GetCount()!=si_count) - return(nullptr); - - Primitive *primitive=db->CreatePrimitive(prim_name,vertices_number); - - const auto *sp=vab_map.GetDataList(); - for(uint i=0;ivalue.vab) - { - if((*sp)->value.map_ptr) - (*sp)->value.vab->Unmap(); - - primitive->SetVAB((*sp)->key,(*sp)->value.vab); - } - else - { - //ClearAllData(); - return(nullptr); - } - - ++sp; - } - - if(ibo) - { - ibo->Unmap(); - primitive->SetIndex(ibo,0,index_number); - - db->Add(ibo); - ibo=nullptr; //避免释构函数删除 - } - - db->Add(primitive); - - return primitive; + primitive->SetVAB((*sp)->key,(*sp)->value.vab); } - }//namespace graph -}//namespace hgl + else + { + //ClearAllData(); + return(nullptr); + } + + ++sp; + } + + if(ibo) + { + ibo->Unmap(); + primitive->SetIndex(ibo,0,index_number); + + db->Add(ibo); + ibo=nullptr; //避免释构函数删除 + } + + db->Add(primitive); + + return primitive; +} +VK_NAMESPACE_END \ No newline at end of file