From 4a94d78c1fd72b883af699e9f04b24a0ade91d78 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sat, 25 May 2024 03:14:26 +0800 Subject: [PATCH] preparing PrimitiveCreater to support multi primitive --- .../LightBasic/BlinnPhongDirectionLight.cpp | 43 +++++++++-- inc/hgl/graph/PrimitiveCreater.h | 26 ++++--- inc/hgl/graph/VKVertexAttribBuffer.h | 8 +- inc/hgl/graph/VertexDataManager.h | 6 +- src/SceneGraph/InlineGeometry.cpp | 59 ++++++++------- src/SceneGraph/PrimitiveCreater.cpp | 75 ++++++++++++------- src/SceneGraph/Vulkan/VKPrimitiveData.cpp | 4 +- 7 files changed, 138 insertions(+), 83 deletions(-) diff --git a/example/LightBasic/BlinnPhongDirectionLight.cpp b/example/LightBasic/BlinnPhongDirectionLight.cpp index 92d13e2f..aadcb930 100644 --- a/example/LightBasic/BlinnPhongDirectionLight.cpp +++ b/example/LightBasic/BlinnPhongDirectionLight.cpp @@ -10,7 +10,7 @@ #include #include #include -//#include +#include using namespace hgl; using namespace hgl::graph; @@ -40,7 +40,7 @@ private: //plane grid Material * mtl_vertex_lum =nullptr; MaterialInstance * mi_plane_grid =nullptr; Pipeline * p_line =nullptr; - AutoDelete prim_plane_grid =nullptr; + Primitive * prim_plane_grid =nullptr; private: @@ -49,14 +49,14 @@ private: private: //sphere Material * mtl_blinnphong =nullptr; - //VertexDataManager * vdm_blinnphong =nullptr; + VertexDataManager * vdm_blinnphong =nullptr; MaterialInstance * mi_blinnphong[4]{}; Pipeline * p_blinnphong =nullptr; - AutoDelete prim_sphere =nullptr; - AutoDelete prim_cone =nullptr; - AutoDelete prim_cylinder =nullptr; + Primitive * prim_sphere =nullptr; + Primitive * prim_cone =nullptr; + Primitive * prim_cylinder =nullptr; private: @@ -100,8 +100,6 @@ private: mtl_blinnphong->BindUBO(DescriptorSetType::Global,"sun",ubo_sun); mtl_blinnphong->Update(); - //vdm_blinnphong=new VertexDataManager(device,mtl_blinnphong->GetDefaultVIL()); - Color4f mi_data; for(uint i=0;i<4;i++) { @@ -121,6 +119,22 @@ private: return(true); } + bool InitVDM() + { + vdm_blinnphong=new VertexDataManager(device,mtl_blinnphong->GetDefaultVIL()); + if(!vdm_blinnphong->Init( 1024*1024, //VAB最大容量 + 1024*1024, //索引最大容量 + IndexType::U16)) //索引类型 + { + delete vdm_blinnphong; + vdm_blinnphong=nullptr; + + return(false); + } + + return(true); + } + bool CreateRenderObject() { using namespace inline_geometry; @@ -224,6 +238,9 @@ public: if(!InitBlinnPhongSunLightMP()) return(false); + if(!InitVDM()) + return(false); + if(!CreateRenderObject()) return(false); @@ -232,6 +249,16 @@ public: return(true); } + + ~TestApp() + { + SAFE_CLEAR(prim_cone) + SAFE_CLEAR(prim_cylinder) + SAFE_CLEAR(prim_sphere) + SAFE_CLEAR(prim_plane_grid) + + SAFE_CLEAR(vdm_blinnphong) + } };//class TestApp:public CameraAppFramework int main(int,char **) diff --git a/inc/hgl/graph/PrimitiveCreater.h b/inc/hgl/graph/PrimitiveCreater.h index dfbac343..61db0d38 100644 --- a/inc/hgl/graph/PrimitiveCreater.h +++ b/inc/hgl/graph/PrimitiveCreater.h @@ -18,24 +18,26 @@ protected: const VIL * vil; - AnsiString prim_name; - PrimitiveData * prim_data; - protected: - - VkDeviceSize vertices_number; ///<顶点数量 - VkDeviceSize index_number; ///<索引数量 - IndexType index_type; ///<索引类型 - IBAccess * iba; ///<索引缓冲区 + AnsiString prim_name; + PrimitiveData * prim_data; + + VkDeviceSize vertices_number; ///<顶点数量 + + VkDeviceSize index_number; ///<索引数量 + IndexType index_type; ///<索引类型 + IBAccess * iba; ///<索引缓冲区 public: - PrimitiveCreater(GPUDevice *,const VIL *,const AnsiString &name); - PrimitiveCreater(VertexDataManager *,const VIL *,const AnsiString &name); + PrimitiveCreater(GPUDevice *,const VIL *); + PrimitiveCreater(VertexDataManager *,const VIL *); virtual ~PrimitiveCreater(); - virtual bool Init(const VkDeviceSize vertices_count,const VkDeviceSize index_count,IndexType it=IndexType::AUTO); ///<初始化,参数为顶点数量 + virtual bool Init(const AnsiString &name,const VkDeviceSize vertices_count,const VkDeviceSize index_count,IndexType it=IndexType::AUTO); ///<初始化,参数为顶点数量 + + void Clear(); ///<清除创建器数据 public: //顶点缓冲区 @@ -61,7 +63,7 @@ public: //索引缓冲区 public: //创建可渲染对象 - virtual Primitive * Create(); ///<创建一个可渲染对象 + virtual Primitive * Create(); ///<创建一个可渲染对象,并清除创建器数据 };//class PrimitiveCreater /** diff --git a/inc/hgl/graph/VKVertexAttribBuffer.h b/inc/hgl/graph/VKVertexAttribBuffer.h index 8a2c914d..6cec8ec2 100644 --- a/inc/hgl/graph/VKVertexAttribBuffer.h +++ b/inc/hgl/graph/VKVertexAttribBuffer.h @@ -1,4 +1,4 @@ -#ifndef HGL_GRAPH_VULKAN_VERTEX_ATTRIB_BUFFER_INCLUDE +#ifndef HGL_GRAPH_VULKAN_VERTEX_ATTRIB_BUFFER_INCLUDE #define HGL_GRAPH_VULKAN_VERTEX_ATTRIB_BUFFER_INCLUDE #include @@ -9,9 +9,9 @@ namespace hgl { class VertexAttribBuffer:public DeviceBuffer { - VkFormat format; ///<ݸʽ - uint32_t stride; ///<ֽ - uint32_t count; ///< + VkFormat format; ///<数据格式 + uint32_t stride; ///<单个数据字节数 + uint32_t count; ///<数据数量 private: diff --git a/inc/hgl/graph/VertexDataManager.h b/inc/hgl/graph/VertexDataManager.h index 26923ec2..2b2529f0 100644 --- a/inc/hgl/graph/VertexDataManager.h +++ b/inc/hgl/graph/VertexDataManager.h @@ -40,9 +40,9 @@ public: const VkDeviceSize GetVABMaxCount ()const{return vab_max_size;} ///<取得顶点属性缓冲区分配的空间最大数量 const VkDeviceSize GetVABCurCount ()const{return vab_cur_size;} ///<取得顶点属性缓冲区当前数量 - const IndexType GetIBOType ()const{return ibo?ibo->GetType():IndexType::ERR;} ///<取得索引缓冲区类型 - const VkDeviceSize GetIBOMaxCount ()const{return ibo?ibo->GetCount():-1;} ///<取得索引缓冲区分配的空间最大数量 - const VkDeviceSize GetIBOCurCount ()const{return ibo?ibo_cur_size:-1;} ///<取得索引缓冲区当前数量 + const IndexType GetIndexType ()const{return ibo?ibo->GetType():IndexType::ERR;} ///<取得索引缓冲区类型 + const VkDeviceSize GetIndexMaxCount ()const{return ibo?ibo->GetCount():-1;} ///<取得索引缓冲区分配的空间最大数量 + const VkDeviceSize GetIndexCurCount ()const{return ibo?ibo_cur_size:-1;} ///<取得索引缓冲区当前数量 public: diff --git a/src/SceneGraph/InlineGeometry.cpp b/src/SceneGraph/InlineGeometry.cpp index b0197be0..fd1a377d 100644 --- a/src/SceneGraph/InlineGeometry.cpp +++ b/src/SceneGraph/InlineGeometry.cpp @@ -15,9 +15,9 @@ namespace hgl { Primitive *CreateRectangle(GPUDevice *device,const VIL *vil,const RectangleCreateInfo *rci) { - PrimitiveCreater rc(device,vil,"Rectangle"); + PrimitiveCreater rc(device,vil); - if(!rc.Init(4,0)) + if(!rc.Init("Rectangle",4,0)) return(nullptr); VABMap2f vertex(&rc,VAN::Position); @@ -41,11 +41,11 @@ namespace hgl Primitive *CreateRoundRectangle(GPUDevice *device,const VIL *vil,const RoundRectangleCreateInfo *rci) { - PrimitiveCreater rc(device,vil,"RoundRectangle"); + PrimitiveCreater rc(device,vil); if(rci->radius==0||rci->round_per<=1) //这是要画矩形 { - if(!rc.Init(4,0)) + if(!rc.Init("RoundRectangle",4,0)) return(nullptr); VABMap2f vertex(&rc,VAN::Position); @@ -59,7 +59,7 @@ namespace hgl if(radius>rci->scope.GetWidth()/2.0f)radius=rci->scope.GetWidth()/2.0f; if(radius>rci->scope.GetHeight()/2.0f)radius=rci->scope.GetHeight()/2.0f; - if(!rc.Init(rci->round_per*4,8)) + if(!rc.Init("RoundRectangle",rci->round_per*4,8)) return(nullptr); VABMap2f vertex(&rc,VAN::Position); @@ -115,21 +115,24 @@ namespace hgl Primitive *CreateCircle(GPUDevice *device,const VIL *vil,const CircleCreateInfo *cci) { - PrimitiveCreater rc(device,vil,"Circle"); + PrimitiveCreater rc(device,vil); uint edge; + uint vertex_count; if(cci->has_color) { edge=cci->field_count+1; - if(!rc.Init(cci->field_count+2,0))return(nullptr); + vertex_count=cci->field_count+2; } else { edge=cci->field_count; - if(!rc.Init(cci->field_count,0))return(nullptr); + vertex_count=cci->field_count; } + if(!rc.Init("Circle",vertex_count,0))return(nullptr); + VABMap2f vertex(&rc,VAN::Position); VABMap4f color(&rc,VAN::Color); @@ -163,9 +166,9 @@ namespace hgl Primitive *CreatePlaneGrid(GPUDevice *device,const VIL *vil,const PlaneGridCreateInfo *pgci) { - PrimitiveCreater rc(device,vil,"PlaneGrid"); + PrimitiveCreater rc(device,vil); - if(!rc.Init(((pgci->grid_size.Width()+1)+(pgci->grid_size.Height()+1))*2,0)) + if(!rc.Init("PlaneGrid",((pgci->grid_size.Width()+1)+(pgci->grid_size.Height()+1))*2,0)) return(nullptr); VABMap3f vertex(&rc,VAN::Position); @@ -219,9 +222,9 @@ namespace hgl const Vector3f xy_normal(0.0f,0.0f,1.0f); const Vector3f xy_tangent(1.0f,0.0f,0.0f); - PrimitiveCreater rc(device,vil,"Plane"); + PrimitiveCreater rc(device,vil); - if(!rc.Init(4,8)) + if(!rc.Init("Plane",4,8)) return(nullptr); if(!rc.WriteVAB(VAN::Position,VF_V3F,xy_vertices,sizeof(xy_vertices))) @@ -304,9 +307,9 @@ namespace hgl 16, 17, 18, 16, 18, 19, 20, 23, 22, 20, 22, 21}; - PrimitiveCreater rc(device,vil,"Cube"); + PrimitiveCreater rc(device,vil); - if(!rc.Init(24,6*2*3,IndexType::U16)) + if(!rc.Init("Cube",24,6*2*3,IndexType::U16)) return(nullptr); if(!rc.WriteVAB(VAN::Position,VF_V3F,positions,sizeof(positions))) @@ -454,7 +457,7 @@ namespace hgl */ Primitive *CreateSphere(GPUDevice *device,const VIL *vil,const uint numberSlices) { - PrimitiveCreater rc(device,vil,"Sphere"); + PrimitiveCreater rc(device,vil); uint numberParallels = (numberSlices+1) / 2; uint numberVertices = (numberParallels + 1) * (numberSlices + 1); @@ -468,7 +471,7 @@ namespace hgl float helpMatrix[16]; float tex_x; - if(!rc.Init(numberVertices,numberIndices)) + if(!rc.Init("Sphere",numberVertices,numberIndices)) return(nullptr); VABRawMapFloat vertex (&rc,VF_V3F,VAN::Position); @@ -538,7 +541,7 @@ namespace hgl Primitive *CreateDome(GPUDevice *device,const VIL *vil,const uint numberSlices) { - PrimitiveCreater rc(device,vil,"Dome"); + PrimitiveCreater rc(device,vil); uint i, j; @@ -557,7 +560,7 @@ namespace hgl if (numberSlices < 3 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES) return nullptr; - if(!rc.Init(numberVertices,numberIndices)) + if(!rc.Init("Dome",numberVertices,numberIndices)) return(nullptr); VABRawMapFloat vertex (&rc,VF_V3F,VAN::Position); @@ -670,7 +673,7 @@ namespace hgl Primitive *CreateTorus(GPUDevice *device,const VIL *vil,const TorusCreateInfo *tci) { - PrimitiveCreater rc(device,vil,"Torus"); + PrimitiveCreater rc(device,vil); // s, t = parametric values of the equations, in the range [0,1] float s = 0; @@ -705,7 +708,7 @@ namespace hgl sIncr = 1.0f / (float) tci->numberSlices; tIncr = 1.0f / (float) tci->numberStacks; - if(!rc.Init(numberVertices,numberIndices)) + if(!rc.Init("Torus",numberVertices,numberIndices)) return(nullptr); VABRawMapFloat vertex (&rc,VF_V3F,VAN::Position); @@ -840,11 +843,11 @@ namespace hgl if(numberIndices<=0) return(nullptr); - PrimitiveCreater rc(device,vil,"Cylinder"); + PrimitiveCreater rc(device,vil); uint numberVertices = (cci->numberSlices + 2) * 2 + (cci->numberSlices + 1) * 2; - if(!rc.Init(numberVertices,numberIndices)) + if(!rc.Init("Cylinder",numberVertices,numberIndices)) return(nullptr); float angleStep = (2.0f * HGL_PI) / ((float) cci->numberSlices); @@ -1065,14 +1068,14 @@ namespace hgl Primitive *CreateCone(GPUDevice *device,const VIL *vil,const ConeCreateInfo *cci) { - PrimitiveCreater rc(device,vil,"Cone"); + PrimitiveCreater rc(device,vil); uint i, j; uint numberVertices = (cci->numberSlices + 2) + (cci->numberSlices + 1) * (cci->numberStacks + 1); uint numberIndices = cci->numberSlices * 3 + cci->numberSlices * 6 * cci->numberStacks; - if(!rc.Init(numberVertices,numberIndices)) + if(!rc.Init("Cone",numberVertices,numberIndices)) return(nullptr); float angleStep = (2.0f * HGL_PI) / ((float) cci->numberSlices); @@ -1201,9 +1204,9 @@ namespace hgl { if(!device||!vil||!aci)return(nullptr); - PrimitiveCreater rc(device,vil,"Axis"); + PrimitiveCreater rc(device,vil); - if(!rc.Init(6,0)) + if(!rc.Init("Axis",6,0)) return(nullptr); VABMap3f vertex(&rc,VAN::Position); @@ -1246,9 +1249,9 @@ namespace hgl 0,4, 1,5, 2,6, 3,7 }; - PrimitiveCreater rc(device,vil,"BoundingBox"); + PrimitiveCreater rc(device,vil); - if(!rc.Init(8,24,IndexType::U16)) + if(!rc.Init("BoundingBox",8,24,IndexType::U16)) return(nullptr); if(!rc.WriteVAB(VAN::Position,VF_V3F,points,sizeof(points))) diff --git a/src/SceneGraph/PrimitiveCreater.cpp b/src/SceneGraph/PrimitiveCreater.cpp index b4b72f38..dbce8287 100644 --- a/src/SceneGraph/PrimitiveCreater.cpp +++ b/src/SceneGraph/PrimitiveCreater.cpp @@ -7,24 +7,19 @@ #include"vulkan/VKPrimitiveData.h" VK_NAMESPACE_BEGIN -PrimitiveCreater::PrimitiveCreater(GPUDevice *dev,const VIL *v,const AnsiString &name) +PrimitiveCreater::PrimitiveCreater(GPUDevice *dev,const VIL *v) { device =dev; vdm =nullptr; vil =v; - prim_name =name; prim_data =nullptr; - vertices_number =0; - - index_number =0; - index_type =IndexType::ERR; - iba =nullptr; + Clear(); } -PrimitiveCreater::PrimitiveCreater(VertexDataManager *_vdm,const VIL *v,const AnsiString &name) - :PrimitiveCreater(_vdm->GetDevice(),v,name) +PrimitiveCreater::PrimitiveCreater(VertexDataManager *_vdm,const VIL *v) + :PrimitiveCreater(_vdm->GetDevice(),v) { vdm=_vdm; } @@ -34,19 +29,25 @@ PrimitiveCreater::~PrimitiveCreater() SAFE_CLEAR(prim_data); } -bool PrimitiveCreater::Init(const VkDeviceSize vertex_count,const VkDeviceSize index_count,IndexType it) +void PrimitiveCreater::Clear() { + SAFE_CLEAR(prim_data); + + vertices_number =0; + + index_number =0; + index_type =IndexType::ERR; + iba =nullptr; +} + +bool PrimitiveCreater::Init(const AnsiString &pname,const VkDeviceSize vertex_count,const VkDeviceSize index_count,IndexType it) +{ + if(prim_data) //已经初始化过了 + return(false); + + if(pname.IsEmpty())return(false); if(vertex_count<=0)return(false); - if(vdm) - prim_data=CreatePrimitiveData(vdm,vil,vertex_count); - else - prim_data=CreatePrimitiveData(device,vil,vertex_count); - - if(!prim_data)return(false); - - vertices_number=vertex_count; - if(index_count>0) { if(it==IndexType::AUTO) @@ -62,13 +63,32 @@ bool PrimitiveCreater::Init(const VkDeviceSize vertex_count,const VkDeviceSize i return(false); } - iba=prim_data->InitIBO(index_count,it); - - if(!iba) - return(false); - index_type=it; index_number=index_count; + } + + vertices_number=vertex_count; + + if(vdm) + { + prim_data=CreatePrimitiveData(vdm,vil,vertices_number); + + index_type=vdm->GetIndexType(); + } + else + prim_data=CreatePrimitiveData(device,vil,vertices_number); + + if(!prim_data)return(false); + + if(index_number>0) + { + iba=prim_data->InitIBO(index_number,index_type); + + if(!iba) + { + delete prim_data; + return(false); + } #ifdef _DEBUG DebugUtils *du=device->GetDebugUtils(); @@ -81,6 +101,8 @@ bool PrimitiveCreater::Init(const VkDeviceSize vertex_count,const VkDeviceSize i #endif//_DEBUG } + prim_name=pname; + return(true); } @@ -148,8 +170,9 @@ Primitive *PrimitiveCreater::Create() if(!primitive) return(nullptr); - prim_data=nullptr; - iba=nullptr; + prim_data=nullptr; //带入Primitive后,不在这里删除 + + Clear(); return primitive; } diff --git a/src/SceneGraph/Vulkan/VKPrimitiveData.cpp b/src/SceneGraph/Vulkan/VKPrimitiveData.cpp index d044bbcb..b4be84e8 100644 --- a/src/SceneGraph/Vulkan/VKPrimitiveData.cpp +++ b/src/SceneGraph/Vulkan/VKPrimitiveData.cpp @@ -103,7 +103,7 @@ namespace device=dev; } - ~PrimitiveDataPrivateBuffer() + ~PrimitiveDataPrivateBuffer() override { VABAccess *vab=vab_access; @@ -211,7 +211,7 @@ namespace vab_node=vdm->AcquireVAB(vc); } - ~PrimitiveDataVDM() + ~PrimitiveDataVDM() override { if(ib_node) vdm->ReleaseIB(ib_node);