From 0dfb7220b51d26cf95b5ecf9dd58a20bb0b498ed Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sat, 25 May 2024 04:11:38 +0800 Subject: [PATCH] finish BIG VBO.Test OK!,Can RUN!..next step is merge drawcall. --- CMCore | 2 +- .../LightBasic/BlinnPhongDirectionLight.cpp | 34 +-- inc/hgl/graph/InlineGeometry.h | 30 +-- inc/hgl/graph/PrimitiveCreater.h | 8 +- inc/hgl/graph/VertexDataManager.h | 2 +- src/SceneGraph/InlineGeometry.cpp | 238 +++++++++--------- src/SceneGraph/PrimitiveCreater.cpp | 31 ++- src/SceneGraph/VertexDataManager.cpp | 8 +- .../Vulkan/VKCommandBufferRender.cpp | 2 +- src/SceneGraph/Vulkan/VKPrimitiveData.cpp | 25 +- src/SceneGraph/Vulkan/VKPrimitiveData.h | 4 +- src/SceneGraph/Vulkan/VKRenderable.cpp | 2 +- 12 files changed, 189 insertions(+), 197 deletions(-) diff --git a/CMCore b/CMCore index 8f184a87..3d27b60c 160000 --- a/CMCore +++ b/CMCore @@ -1 +1 @@ -Subproject commit 8f184a87fbd630da45b6708ee0fcfcb8372fdef1 +Subproject commit 3d27b60c13162ac2167cc84e9846088a18664312 diff --git a/example/LightBasic/BlinnPhongDirectionLight.cpp b/example/LightBasic/BlinnPhongDirectionLight.cpp index aadcb930..f1b29cf0 100644 --- a/example/LightBasic/BlinnPhongDirectionLight.cpp +++ b/example/LightBasic/BlinnPhongDirectionLight.cpp @@ -11,6 +11,7 @@ #include #include #include +#include using namespace hgl; using namespace hgl::graph; @@ -49,6 +50,7 @@ private: private: //sphere Material * mtl_blinnphong =nullptr; + PrimitiveCreater * pc_blinnphong =nullptr; VertexDataManager * vdm_blinnphong =nullptr; MaterialInstance * mi_blinnphong[4]{}; @@ -119,7 +121,7 @@ private: return(true); } - bool InitVDM() + bool InitVDMAndPC() { vdm_blinnphong=new VertexDataManager(device,mtl_blinnphong->GetDefaultVIL()); if(!vdm_blinnphong->Init( 1024*1024, //VAB最大容量 @@ -132,6 +134,9 @@ private: return(false); } + pc_blinnphong=new PrimitiveCreater(vdm_blinnphong); + //pc_blinnphong=new PrimitiveCreater(device,mtl_blinnphong->GetDefaultVIL()); + return(true); } @@ -140,20 +145,20 @@ private: using namespace inline_geometry; //Plane Grid - { - struct PlaneGridCreateInfo pgci; + //{ + // struct PlaneGridCreateInfo pgci; - pgci.grid_size.Set(32,32); - pgci.sub_count.Set(8,8); + // pgci.grid_size.Set(32,32); + // pgci.sub_count.Set(8,8); - pgci.lum=0.5; - pgci.sub_lum=0.75; + // pgci.lum=0.5; + // pgci.sub_lum=0.75; - prim_plane_grid=CreatePlaneGrid(device,mtl_vertex_lum->GetDefaultVIL(),&pgci); - } + // prim_plane_grid=CreatePlaneGrid(pc_blinnphong,mtl_vertex_lum->GetDefaultVIL(),&pgci); + //} //Sphere - prim_sphere=CreateSphere(device,mi_blinnphong[0]->GetVIL(),16); + prim_sphere=CreateSphere(pc_blinnphong,16); //Cone { @@ -164,7 +169,7 @@ private: cci.numberSlices=16; //圆锥底部分割数 cci.numberStacks=8; //圆锥高度分割数 - prim_cone=CreateCone(device,mi_blinnphong[1]->GetVIL(),&cci); + prim_cone=CreateCone(pc_blinnphong,&cci); } //Cyliner @@ -175,7 +180,7 @@ private: cci.numberSlices=16; //圆柱底部分割数 cci.radius =0.25f; //圆柱半径 - prim_cylinder=CreateCylinder(device,mi_blinnphong[2]->GetVIL(),&cci); + prim_cylinder=CreateCylinder(pc_blinnphong,&cci); } return(true); @@ -205,7 +210,7 @@ private: bool InitScene() { - Add(prim_plane_grid,mi_plane_grid,p_line,Identity4f); + //Add(prim_plane_grid,mi_plane_grid,p_line,Identity4f); Add(prim_sphere, mi_blinnphong[0],p_blinnphong,translate(Vector3f(0,0,2))); @@ -238,7 +243,7 @@ public: if(!InitBlinnPhongSunLightMP()) return(false); - if(!InitVDM()) + if(!InitVDMAndPC()) return(false); if(!CreateRenderObject()) @@ -257,6 +262,7 @@ public: SAFE_CLEAR(prim_sphere) SAFE_CLEAR(prim_plane_grid) + SAFE_CLEAR(pc_blinnphong) SAFE_CLEAR(vdm_blinnphong) } };//class TestApp:public CameraAppFramework diff --git a/inc/hgl/graph/InlineGeometry.h b/inc/hgl/graph/InlineGeometry.h index edec354b..a371ba44 100644 --- a/inc/hgl/graph/InlineGeometry.h +++ b/inc/hgl/graph/InlineGeometry.h @@ -11,6 +11,8 @@ namespace hgl { namespace graph { + class PrimitiveCreater; + namespace inline_geometry { /** @@ -21,12 +23,12 @@ namespace hgl RectScope2f scope; };//struct RectangleCreateInfo - Primitive *CreateRectangle(GPUDevice *device,const VIL *vil,const RectangleCreateInfo *rci); + Primitive *CreateRectangle(PrimitiveCreater *pc,const RectangleCreateInfo *rci); /** * 创建延迟渲染用全屏平面 */ - Primitive *CreateGBufferCompositionRectangle(GPUDevice *device,const VIL *vil); + Primitive *CreateGBufferCompositionRectangle(PrimitiveCreater *pc); /** * 圆角矩形创建信息(扇形/线圈) @@ -37,7 +39,7 @@ namespace hgl uint32_t round_per; ///<圆角精度 };//struct RoundRectangleCreateInfo:public RectangleCreateInfo - Primitive *CreateRoundRectangle(GPUDevice *device,const VIL *vil,const RoundRectangleCreateInfo *rci); + Primitive *CreateRoundRectangle(PrimitiveCreater *pc,const RoundRectangleCreateInfo *rci); /** * 圆形创建信息 @@ -57,7 +59,7 @@ namespace hgl /** * 创建一个2D圆形(扇形/线圈) */ - Primitive *CreateCircle(GPUDevice *device,const VIL *vil,const CircleCreateInfo *cci); + Primitive *CreateCircle(PrimitiveCreater *pc,const CircleCreateInfo *cci); /** * 平面网格创建信息
@@ -76,12 +78,12 @@ namespace hgl /** * 创建一个平面网格(线条) */ - Primitive *CreatePlaneGrid(GPUDevice *device,const VIL *vil,const PlaneGridCreateInfo *pgci); + Primitive *CreatePlaneGrid(PrimitiveCreater *pc,const PlaneGridCreateInfo *pgci); /** * 创建一个平面(三角形) */ - Primitive *CreatePlane(GPUDevice *device,const VIL *vil); + Primitive *CreatePlane(PrimitiveCreater *pc); struct CubeCreateInfo { @@ -117,7 +119,7 @@ namespace hgl /** * 创建一个立方体(三角形) */ - Primitive *CreateCube(GPUDevice *device,const VIL *vil,const CubeCreateInfo *cci); + Primitive *CreateCube(PrimitiveCreater *pc,const CubeCreateInfo *cci); struct BoundingBoxCreateInfo { @@ -148,17 +150,17 @@ namespace hgl /** * 创建一个绑定盒(线条) */ - Primitive *CreateBoundingBox(GPUDevice *device,const VIL *vil,const BoundingBoxCreateInfo *cci); + Primitive *CreateBoundingBox(PrimitiveCreater *pc,const BoundingBoxCreateInfo *cci); /** * 创建一个球心坐标为0,0,0,半径为1的球体(三角形) */ - Primitive *CreateSphere(GPUDevice *device,const VIL *vil,const uint numberSlices); + Primitive *CreateSphere(PrimitiveCreater *,const uint numberSlices); /** * 创建一个穹顶(三角形) */ - Primitive *CreateDome(GPUDevice *device,const VIL *vil, const uint numberSlices); + Primitive *CreateDome(PrimitiveCreater *pc, const uint numberSlices); struct TorusCreateInfo { @@ -174,7 +176,7 @@ namespace hgl /** * 创建一个圆环(三角形) */ - Primitive *CreateTorus(GPUDevice *device,const VIL *vil,const TorusCreateInfo *tci); + Primitive *CreateTorus(PrimitiveCreater *pc,const TorusCreateInfo *tci); struct CylinderCreateInfo { @@ -186,7 +188,7 @@ namespace hgl /** * 创建一个圆柱(三角形) */ - Primitive *CreateCylinder(GPUDevice *device,const VIL *vil,const CylinderCreateInfo *cci); + Primitive *CreateCylinder(PrimitiveCreater *,const CylinderCreateInfo *cci); struct ConeCreateInfo { @@ -199,7 +201,7 @@ namespace hgl /** * 创建一个圆锥(三角形) */ - Primitive *CreateCone(GPUDevice *device,const VIL *vil,const ConeCreateInfo *cci); + Primitive *CreateCone(PrimitiveCreater *,const ConeCreateInfo *cci); struct AxisCreateInfo { @@ -220,7 +222,7 @@ namespace hgl /** * 创建一个坐标线(线条) */ - Primitive *CreateAxis(GPUDevice *device,const VIL *vil,const AxisCreateInfo *aci); + Primitive *CreateAxis(PrimitiveCreater *pc,const AxisCreateInfo *aci); }//namespace inline_geometry }//namespace graph };//namespace hgl diff --git a/inc/hgl/graph/PrimitiveCreater.h b/inc/hgl/graph/PrimitiveCreater.h index 61db0d38..dbbba4dc 100644 --- a/inc/hgl/graph/PrimitiveCreater.h +++ b/inc/hgl/graph/PrimitiveCreater.h @@ -32,7 +32,7 @@ protected: public: PrimitiveCreater(GPUDevice *,const VIL *); - PrimitiveCreater(VertexDataManager *,const VIL *); + PrimitiveCreater(VertexDataManager *); virtual ~PrimitiveCreater(); virtual bool Init(const AnsiString &name,const VkDeviceSize vertices_count,const VkDeviceSize index_count,IndexType it=IndexType::AUTO); ///<初始化,参数为顶点数量 @@ -43,10 +43,10 @@ public: //顶点缓冲区 const VkDeviceSize GetVertexCount()const{ return vertices_number; } ///<取得顶点数量 - VABAccess * AcquireVAB (const AnsiString &name,const VkFormat &format,const void *data=nullptr,const VkDeviceSize bytes=0); ///<请求一个顶点属性数据区 - bool WriteVAB (const AnsiString &name,const VkFormat &format,const void *data,const uint32_t bytes) ///<直接写入顶点属性数据 + VABAccess * AcquireVAB (const AnsiString &name,const VkFormat &format,const void *data=nullptr); ///<请求一个顶点属性数据区 + bool WriteVAB (const AnsiString &name,const VkFormat &format,const void *data) ///<直接写入顶点属性数据 { - return AcquireVAB(name,format,data,bytes); + return AcquireVAB(name,format,data); } public: //索引缓冲区 diff --git a/inc/hgl/graph/VertexDataManager.h b/inc/hgl/graph/VertexDataManager.h index 2b2529f0..e2c140a2 100644 --- a/inc/hgl/graph/VertexDataManager.h +++ b/inc/hgl/graph/VertexDataManager.h @@ -16,7 +16,7 @@ protected: uint vi_count; ///<顶点输入流数量 const VIF * vif_list; ///<顶点输入格式列表 - VkDeviceSize vab_max_size; ///<顶点缓冲区分配空间大小 + VkDeviceSize vab_max_size; ///<顶点缓冲区分配空间大小(顶点数) VkDeviceSize vab_cur_size; ///<顶点缓冲区当前使用大小 VAB ** vab; ///<顶点缓冲区列表 diff --git a/src/SceneGraph/InlineGeometry.cpp b/src/SceneGraph/InlineGeometry.cpp index fd1a377d..21dd4430 100644 --- a/src/SceneGraph/InlineGeometry.cpp +++ b/src/SceneGraph/InlineGeometry.cpp @@ -13,42 +13,42 @@ namespace hgl { namespace inline_geometry { - Primitive *CreateRectangle(GPUDevice *device,const VIL *vil,const RectangleCreateInfo *rci) + Primitive *CreateRectangle(PrimitiveCreater *pc,const RectangleCreateInfo *rci) { - PrimitiveCreater rc(device,vil); + if(!pc)return(nullptr); - if(!rc.Init("Rectangle",4,0)) + if(!pc->Init("Rectangle",4,0)) return(nullptr); - VABMap2f vertex(&rc,VAN::Position); + VABMap2f vertex(pc,VAN::Position); if(!vertex.IsValid()) return(nullptr); vertex->WriteRectFan(rci->scope); - return rc.Create(); + return pc->Create(); } - Primitive *CreateGBufferCompositionRectangle(GPUDevice *device,const VIL *vil) + Primitive *CreateGBufferCompositionRectangle(PrimitiveCreater *pc) { RectangleCreateInfo rci; rci.scope.Set(-1,-1,2,2); - return CreateRectangle(device,vil,&rci); + return CreateRectangle(pc,&rci); } - Primitive *CreateRoundRectangle(GPUDevice *device,const VIL *vil,const RoundRectangleCreateInfo *rci) + Primitive *CreateRoundRectangle(PrimitiveCreater *pc,const RoundRectangleCreateInfo *rci) { - PrimitiveCreater rc(device,vil); + if(!pc)return(nullptr); if(rci->radius==0||rci->round_per<=1) //这是要画矩形 { - if(!rc.Init("RoundRectangle",4,0)) + if(!pc->Init("RoundRectangle",4,0)) return(nullptr); - VABMap2f vertex(&rc,VAN::Position); + VABMap2f vertex(pc,VAN::Position); vertex->WriteRectFan(rci->scope); } @@ -59,10 +59,10 @@ 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("RoundRectangle",rci->round_per*4,8)) + if(!pc->Init("RoundRectangle",rci->round_per*4,8)) return(nullptr); - VABMap2f vertex(&rc,VAN::Position); + VABMap2f vertex(pc,VAN::Position); Vector2f *coord=new Vector2f[rci->round_per]; @@ -110,12 +110,12 @@ namespace hgl delete[] coord; } - return rc.Create(); + return pc->Create(); } - Primitive *CreateCircle(GPUDevice *device,const VIL *vil,const CircleCreateInfo *cci) + Primitive *CreateCircle(PrimitiveCreater *pc,const CircleCreateInfo *cci) { - PrimitiveCreater rc(device,vil); + if(!pc)return(nullptr); uint edge; uint vertex_count; @@ -131,10 +131,10 @@ namespace hgl vertex_count=cci->field_count; } - if(!rc.Init("Circle",vertex_count,0))return(nullptr); + if(!pc->Init("Circle",vertex_count,0))return(nullptr); - VABMap2f vertex(&rc,VAN::Position); - VABMap4f color(&rc,VAN::Color); + VABMap2f vertex(pc,VAN::Position); + VABMap4f color(pc,VAN::Color); if(!vertex.IsValid()) return(nullptr); @@ -161,17 +161,15 @@ namespace hgl color->Write(cci->border_color); } - return rc.Create(); + return pc->Create(); } - Primitive *CreatePlaneGrid(GPUDevice *device,const VIL *vil,const PlaneGridCreateInfo *pgci) + Primitive *CreatePlaneGrid(PrimitiveCreater *pc,const PlaneGridCreateInfo *pgci) { - PrimitiveCreater rc(device,vil); - - if(!rc.Init("PlaneGrid",((pgci->grid_size.Width()+1)+(pgci->grid_size.Height()+1))*2,0)) + if(!pc->Init("PlaneGrid",((pgci->grid_size.Width()+1)+(pgci->grid_size.Height()+1))*2,0)) return(nullptr); - VABMap3f vertex(&rc,VAN::Position); + VABMap3f vertex(pc,VAN::Position); const float right=float(pgci->grid_size.Width())/2.0f; const float left =-right; @@ -191,7 +189,7 @@ namespace hgl Vector3f(left+col,bottom,0)); } - VABMap1f lum(&rc,VAN::Luminance); + VABMap1f lum(pc,VAN::Luminance); if(lum.IsValid()) { @@ -212,49 +210,49 @@ namespace hgl } } - return rc.Create(); + return pc->Create(); } - Primitive *CreatePlane(GPUDevice *device,const VIL *vil) + Primitive *CreatePlane(PrimitiveCreater *pc) { const float xy_vertices [] = { -0.5f,-0.5f,0.0f, +0.5f,-0.5f,0.0f, +0.5f,+0.5f,0.0f, -0.5f,+0.5f,0.0f }; float xy_tex_coord[] = { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f }; const Vector3f xy_normal(0.0f,0.0f,1.0f); const Vector3f xy_tangent(1.0f,0.0f,0.0f); - PrimitiveCreater rc(device,vil); + if(!pc)return(nullptr); - if(!rc.Init("Plane",4,8)) + if(!pc->Init("Plane",4,8)) return(nullptr); - if(!rc.WriteVAB(VAN::Position,VF_V3F,xy_vertices,sizeof(xy_vertices))) + if(!pc->WriteVAB(VAN::Position,VF_V3F,xy_vertices)) return(nullptr); { - VABMap3f normal(&rc,VAN::Normal); + VABMap3f normal(pc,VAN::Normal); if(normal.IsValid()) normal->RepeatWrite(xy_normal,4); } { - VABMap3f tangent(&rc,VAN::Tangent); + VABMap3f tangent(pc,VAN::Tangent); if(tangent.IsValid()) tangent->RepeatWrite(xy_tangent,4); } { - VABMap2f tex_coord(&rc,VAN::TexCoord); + VABMap2f tex_coord(pc,VAN::TexCoord); if(tex_coord.IsValid()) tex_coord->Write(xy_tex_coord,4); } - return rc.Create(); + return pc->Create(); } - Primitive *CreateCube(GPUDevice *device,const VIL *vil,const CubeCreateInfo *cci) + Primitive *CreateCube(PrimitiveCreater *pc,const CubeCreateInfo *cci) { /** * 4 5 @@ -307,31 +305,31 @@ namespace hgl 16, 17, 18, 16, 18, 19, 20, 23, 22, 20, 22, 21}; - PrimitiveCreater rc(device,vil); + if(!pc)return(nullptr); - if(!rc.Init("Cube",24,6*2*3,IndexType::U16)) + if(!pc->Init("Cube",24,6*2*3,IndexType::U16)) return(nullptr); - if(!rc.WriteVAB(VAN::Position,VF_V3F,positions,sizeof(positions))) + if(!pc->WriteVAB(VAN::Position,VF_V3F,positions)) return(nullptr); if(cci->normal) - if(!rc.WriteVAB(VAN::Normal,VF_V3F,normals,sizeof(normals))) + if(!pc->WriteVAB(VAN::Normal,VF_V3F,normals)) return(nullptr); if(cci->tangent) - if(!rc.WriteVAB(VAN::Tangent,VF_V3F,tangents,sizeof(tangents))) + if(!pc->WriteVAB(VAN::Tangent,VF_V3F,tangents)) return(nullptr); if(cci->tex_coord) - if(!rc.WriteVAB(VAN::TexCoord,VF_V2F,tex_coords,sizeof(tex_coords))) + if(!pc->WriteVAB(VAN::TexCoord,VF_V2F,tex_coords)) return(nullptr); if(cci->color_type!=CubeCreateInfo::ColorType::NoColor) { RANGE_CHECK_RETURN_NULLPTR(cci->color_type); - VABMap4f color(&rc,VAN::Color); + VABMap4f color(pc,VAN::Color); if(color.IsValid()) { @@ -351,9 +349,9 @@ namespace hgl } } - //rc.CreateIBO16(6*2*3,indices); - rc.WriteIBO(indices); - return rc.Create(); + //pc->CreateIBO16(6*2*3,indices); + pc->WriteIBO(indices); + return pc->Create(); } template @@ -455,10 +453,8 @@ namespace hgl * @param numberSlices 切片数 * @return 可渲染数据 */ - Primitive *CreateSphere(GPUDevice *device,const VIL *vil,const uint numberSlices) + Primitive *CreateSphere(PrimitiveCreater *pc,const uint numberSlices) { - PrimitiveCreater rc(device,vil); - uint numberParallels = (numberSlices+1) / 2; uint numberVertices = (numberParallels + 1) * (numberSlices + 1); uint numberIndices = numberParallels * numberSlices * 6; @@ -471,13 +467,13 @@ namespace hgl float helpMatrix[16]; float tex_x; - if(!rc.Init("Sphere",numberVertices,numberIndices)) + if(!pc->Init("Sphere",numberVertices,numberIndices)) return(nullptr); - VABRawMapFloat vertex (&rc,VF_V3F,VAN::Position); - VABRawMapFloat normal (&rc,VF_V3F,VAN::Normal); - VABRawMapFloat tangent (&rc,VF_V3F,VAN::Tangent); - VABRawMapFloat tex_coord(&rc,VF_V2F,VAN::TexCoord); + VABRawMapFloat vertex (pc,VF_V3F,VAN::Position); + VABRawMapFloat normal (pc,VF_V3F,VAN::Normal); + VABRawMapFloat tangent (pc,VF_V3F,VAN::Tangent); + VABRawMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord); float *vp=vertex; float *np=normal; @@ -528,20 +524,20 @@ namespace hgl //索引 { - const IndexType index_type=rc.GetIndexType(); + const IndexType index_type=pc->GetIndexType(); - 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 + if(index_type==IndexType::U16)CreateSphereIndices(pc,numberParallels,numberSlices);else + if(index_type==IndexType::U32)CreateSphereIndices(pc,numberParallels,numberSlices);else + if(index_type==IndexType::U8 )CreateSphereIndices(pc,numberParallels,numberSlices);else return(nullptr); } - return rc.Create(); + return pc->Create(); } - Primitive *CreateDome(GPUDevice *device,const VIL *vil,const uint numberSlices) + Primitive *CreateDome(PrimitiveCreater *pc,const uint numberSlices) { - PrimitiveCreater rc(device,vil); + if(!pc)return(nullptr); uint i, j; @@ -560,13 +556,13 @@ namespace hgl if (numberSlices < 3 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES) return nullptr; - if(!rc.Init("Dome",numberVertices,numberIndices)) + if(!pc->Init("Dome",numberVertices,numberIndices)) return(nullptr); - VABRawMapFloat vertex (&rc,VF_V3F,VAN::Position); - VABRawMapFloat normal (&rc,VF_V3F,VAN::Normal); - VABRawMapFloat tangent (&rc,VF_V3F,VAN::Tangent); - VABRawMapFloat tex_coord(&rc,VF_V2F,VAN::TexCoord); + VABRawMapFloat vertex (pc,VF_V3F,VAN::Position); + VABRawMapFloat normal (pc,VF_V3F,VAN::Normal); + VABRawMapFloat tangent (pc,VF_V3F,VAN::Tangent); + VABRawMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord); float *vp=vertex; float *np=normal; @@ -622,15 +618,15 @@ namespace hgl //索引 { - const IndexType index_type=rc.GetIndexType(); + const IndexType index_type=pc->GetIndexType(); - 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 + if(index_type==IndexType::U16)CreateSphereIndices(pc,numberParallels,numberSlices);else + if(index_type==IndexType::U32)CreateSphereIndices(pc,numberParallels,numberSlices);else + if(index_type==IndexType::U8 )CreateSphereIndices(pc,numberParallels,numberSlices);else return(nullptr); } - return rc.Create(); + return pc->Create(); } namespace @@ -671,9 +667,9 @@ namespace hgl } }//namespace - Primitive *CreateTorus(GPUDevice *device,const VIL *vil,const TorusCreateInfo *tci) + Primitive *CreateTorus(PrimitiveCreater *pc,const TorusCreateInfo *tci) { - PrimitiveCreater rc(device,vil); + if(!pc)return(nullptr); // s, t = parametric values of the equations, in the range [0,1] float s = 0; @@ -708,13 +704,13 @@ namespace hgl sIncr = 1.0f / (float) tci->numberSlices; tIncr = 1.0f / (float) tci->numberStacks; - if(!rc.Init("Torus",numberVertices,numberIndices)) + if(!pc->Init("Torus",numberVertices,numberIndices)) return(nullptr); - VABRawMapFloat vertex (&rc,VF_V3F,VAN::Position); - VABRawMapFloat normal (&rc,VF_V3F,VAN::Normal); - VABRawMapFloat tangent (&rc,VF_V3F,VAN::Tangent); - VABRawMapFloat tex_coord(&rc,VF_V2F,VAN::TexCoord); + VABRawMapFloat vertex (pc,VF_V3F,VAN::Position); + VABRawMapFloat normal (pc,VF_V3F,VAN::Normal); + VABRawMapFloat tangent (pc,VF_V3F,VAN::Tangent); + VABRawMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord); float *vp=vertex; float *np=normal; @@ -774,14 +770,14 @@ namespace hgl //索引 { - const IndexType index_type=rc.GetIndexType(); + const IndexType index_type=pc->GetIndexType(); - 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 + if(index_type==IndexType::U16)CreateTorusIndices(pc,tci->numberSlices,tci->numberStacks);else + if(index_type==IndexType::U32)CreateTorusIndices(pc,tci->numberSlices,tci->numberStacks);else + if(index_type==IndexType::U8 )CreateTorusIndices(pc,tci->numberSlices,tci->numberStacks);else return(nullptr); } - return rc.Create(); + return pc->Create(); } namespace @@ -836,18 +832,16 @@ namespace hgl } }//namespace - Primitive *CreateCylinder(GPUDevice *device,const VIL *vil,const CylinderCreateInfo *cci) + Primitive *CreateCylinder(PrimitiveCreater *pc,const CylinderCreateInfo *cci) { uint numberIndices = cci->numberSlices * 3 * 2 + cci->numberSlices * 6; if(numberIndices<=0) return(nullptr); - PrimitiveCreater rc(device,vil); - uint numberVertices = (cci->numberSlices + 2) * 2 + (cci->numberSlices + 1) * 2; - if(!rc.Init("Cylinder",numberVertices,numberIndices)) + if(!pc->Init("Cylinder",numberVertices,numberIndices)) return(nullptr); float angleStep = (2.0f * HGL_PI) / ((float) cci->numberSlices); @@ -855,10 +849,10 @@ namespace hgl if (cci->numberSlices < 3 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES) return nullptr; - VABRawMapFloat vertex (&rc,VF_V3F,VAN::Position); - VABRawMapFloat normal (&rc,VF_V3F,VAN::Normal); - VABRawMapFloat tangent (&rc,VF_V3F,VAN::Tangent); - VABRawMapFloat tex_coord(&rc,VF_V2F,VAN::TexCoord); + VABRawMapFloat vertex (pc,VF_V3F,VAN::Position); + VABRawMapFloat normal (pc,VF_V3F,VAN::Normal); + VABRawMapFloat tangent (pc,VF_V3F,VAN::Tangent); + VABRawMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord); float *vp=vertex; float *np=normal; @@ -1012,15 +1006,15 @@ namespace hgl //索引 { - const IndexType index_type=rc.GetIndexType(); + const IndexType index_type=pc->GetIndexType(); - 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 + if(index_type==IndexType::U16)CreateCylinderIndices(pc,cci->numberSlices);else + if(index_type==IndexType::U32)CreateCylinderIndices(pc,cci->numberSlices);else + if(index_type==IndexType::U8 )CreateCylinderIndices(pc,cci->numberSlices);else return(nullptr); } - return rc.Create(); + return pc->Create(); } namespace @@ -1066,16 +1060,14 @@ namespace hgl } }//namespace - Primitive *CreateCone(GPUDevice *device,const VIL *vil,const ConeCreateInfo *cci) + Primitive *CreateCone(PrimitiveCreater *pc,const ConeCreateInfo *cci) { - 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("Cone",numberVertices,numberIndices)) + if(!pc->Init("Cone",numberVertices,numberIndices)) return(nullptr); float angleStep = (2.0f * HGL_PI) / ((float) cci->numberSlices); @@ -1087,10 +1079,10 @@ namespace hgl if (cci->numberSlices < 3 || cci->numberStacks < 1 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES) return nullptr; - VABRawMapFloat vertex (&rc,VF_V3F,VAN::Position); - VABRawMapFloat normal (&rc,VF_V3F,VAN::Normal); - VABRawMapFloat tangent (&rc,VF_V3F,VAN::Tangent); - VABRawMapFloat tex_coord(&rc,VF_V2F,VAN::TexCoord); + VABRawMapFloat vertex (pc,VF_V3F,VAN::Position); + VABRawMapFloat normal (pc,VF_V3F,VAN::Normal); + VABRawMapFloat tangent (pc,VF_V3F,VAN::Tangent); + VABRawMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord); float *vp=vertex; float *np=normal; @@ -1189,28 +1181,28 @@ namespace hgl //索引 { - const IndexType index_type=rc.GetIndexType(); + const IndexType index_type=pc->GetIndexType(); - 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 + if(index_type==IndexType::U16)CreateConeIndices(pc,cci->numberSlices,cci->numberStacks);else + if(index_type==IndexType::U32)CreateConeIndices(pc,cci->numberSlices,cci->numberStacks);else + if(index_type==IndexType::U8 )CreateConeIndices(pc,cci->numberSlices,cci->numberStacks);else return(nullptr); } - return rc.Create(); + return pc->Create(); } - Primitive *CreateAxis(GPUDevice *device,const VIL *vil,const AxisCreateInfo *aci) + Primitive *CreateAxis(PrimitiveCreater *pc,const AxisCreateInfo *aci) { - if(!device||!vil||!aci)return(nullptr); + if(!pc||!aci)return(nullptr); - PrimitiveCreater rc(device,vil); + if(!pc)return(nullptr); - if(!rc.Init("Axis",6,0)) + if(!pc->Init("Axis",6,0)) return(nullptr); - VABMap3f vertex(&rc,VAN::Position); - VABMap4f color(&rc,VAN::Color); + VABMap3f vertex(pc,VAN::Position); + VABMap4f color(pc,VAN::Color); if(!vertex.IsValid()||!color.IsValid()) return(nullptr); @@ -1224,10 +1216,10 @@ namespace hgl vertex->Write(0,0,0);color->Write(aci->color[2]); vertex->Write(0,0,s);color->Write(aci->color[2]); - return rc.Create(); + return pc->Create(); } - Primitive *CreateBoundingBox(GPUDevice *device,const VIL *vil,const BoundingBoxCreateInfo *cci) + Primitive *CreateBoundingBox(PrimitiveCreater *pc,const BoundingBoxCreateInfo *cci) { // Points of a cube. /* 4 5 */ const float points[]={ -0.5,-0.5, 0.5, 0.5,-0.5,0.5, 0.5,-0.5,-0.5, -0.5,-0.5,-0.5, @@ -1249,19 +1241,19 @@ namespace hgl 0,4, 1,5, 2,6, 3,7 }; - PrimitiveCreater rc(device,vil); + if(!pc)return(nullptr); - if(!rc.Init("BoundingBox",8,24,IndexType::U16)) + if(!pc->Init("BoundingBox",8,24,IndexType::U16)) return(nullptr); - if(!rc.WriteVAB(VAN::Position,VF_V3F,points,sizeof(points))) + if(!pc->WriteVAB(VAN::Position,VF_V3F,points)) return(nullptr); if(cci->color_type!=BoundingBoxCreateInfo::ColorType::NoColor) { RANGE_CHECK_RETURN_NULLPTR(cci->color_type); - VABMap4f color(&rc,VAN::Color); + VABMap4f color(pc,VAN::Color); if(color.IsValid()) { @@ -1273,9 +1265,9 @@ namespace hgl } } - rc.WriteIBO(indices); + pc->WriteIBO(indices); - return rc.Create(); + return pc->Create(); } }//namespace inline_geometry }//namespace graph diff --git a/src/SceneGraph/PrimitiveCreater.cpp b/src/SceneGraph/PrimitiveCreater.cpp index dbce8287..635e4024 100644 --- a/src/SceneGraph/PrimitiveCreater.cpp +++ b/src/SceneGraph/PrimitiveCreater.cpp @@ -18,10 +18,12 @@ PrimitiveCreater::PrimitiveCreater(GPUDevice *dev,const VIL *v) Clear(); } -PrimitiveCreater::PrimitiveCreater(VertexDataManager *_vdm,const VIL *v) - :PrimitiveCreater(_vdm->GetDevice(),v) +PrimitiveCreater::PrimitiveCreater(VertexDataManager *_vdm) + :PrimitiveCreater(_vdm->GetDevice(),_vdm->GetVIL()) { vdm=_vdm; + + index_type=vdm->GetIndexType(); } PrimitiveCreater::~PrimitiveCreater() @@ -71,7 +73,7 @@ bool PrimitiveCreater::Init(const AnsiString &pname,const VkDeviceSize vertex_co if(vdm) { - prim_data=CreatePrimitiveData(vdm,vil,vertices_number); + prim_data=CreatePrimitiveData(vdm,vertices_number); index_type=vdm->GetIndexType(); } @@ -91,12 +93,15 @@ bool PrimitiveCreater::Init(const AnsiString &pname,const VkDeviceSize vertex_co } #ifdef _DEBUG - DebugUtils *du=device->GetDebugUtils(); - - if(du) + if(!vdm) { - du->SetBuffer( iba->buffer->GetBuffer(), prim_name+":IndexBuffer:Buffer"); - du->SetDeviceMemory(iba->buffer->GetVkMemory(), prim_name+":IndexBuffer:Memory"); + DebugUtils *du=device->GetDebugUtils(); + + if(du) + { + du->SetBuffer( iba->buffer->GetBuffer(), prim_name+":IndexBuffer:Buffer"); + du->SetDeviceMemory(iba->buffer->GetVkMemory(), prim_name+":IndexBuffer:Memory"); + } } #endif//_DEBUG } @@ -106,19 +111,19 @@ bool PrimitiveCreater::Init(const AnsiString &pname,const VkDeviceSize vertex_co return(true); } -VABAccess *PrimitiveCreater::AcquireVAB(const AnsiString &name,const VkFormat &acquire_format,const void *data,const VkDeviceSize bytes) +VABAccess *PrimitiveCreater::AcquireVAB(const AnsiString &name,const VkFormat &acquire_format,const void *data) { if(!prim_data)return(nullptr); if(name.IsEmpty())return(nullptr); - VABAccess *vab_access=prim_data->InitVAB(name,acquire_format,data,bytes); + VABAccess *vab_access=prim_data->InitVAB(name,acquire_format,data); if(!vab_access) return(nullptr); - if(vab_access->vab) - { #ifdef _DEBUG + if(!vdm&&vab_access->vab) + { DebugUtils *du=device->GetDebugUtils(); if(du) @@ -126,8 +131,8 @@ VABAccess *PrimitiveCreater::AcquireVAB(const AnsiString &name,const VkFormat &a du->SetBuffer( vab_access->vab->GetBuffer(), prim_name+":VAB:Buffer:"+name); du->SetDeviceMemory(vab_access->vab->GetVkMemory(), prim_name+":VAB:Memory:"+name); } - #endif//_DEBUG } + #endif//_DEBUG return vab_access; } diff --git a/src/SceneGraph/VertexDataManager.cpp b/src/SceneGraph/VertexDataManager.cpp index 153b2da5..1a91f06f 100644 --- a/src/SceneGraph/VertexDataManager.cpp +++ b/src/SceneGraph/VertexDataManager.cpp @@ -75,11 +75,11 @@ namespace hgl { if(count<=0)return(nullptr); - DataChain::UserNode *un=ibo_data_chain.Acquire(count); + DataChain::UserNode *un=ibo_data_chain.Acquire(hgl_align(count,4)); if(!un)return(false); - ibo_cur_size+=count; + ibo_cur_size+=un->GetCount(); return(un); } @@ -101,11 +101,11 @@ namespace hgl { if(count<=0)return(nullptr); - DataChain::UserNode *un=vbo_data_chain.Acquire(count); + DataChain::UserNode *un=vbo_data_chain.Acquire(hgl_align(count,4)); if(!un)return(nullptr); - vab_cur_size+=count; + vab_cur_size+=un->GetCount(); return(un); } diff --git a/src/SceneGraph/Vulkan/VKCommandBufferRender.cpp b/src/SceneGraph/Vulkan/VKCommandBufferRender.cpp index 741b697f..ae5257b7 100644 --- a/src/SceneGraph/Vulkan/VKCommandBufferRender.cpp +++ b/src/SceneGraph/Vulkan/VKCommandBufferRender.cpp @@ -199,7 +199,7 @@ void RenderCmdBuffer::DrawIndexed(const IBAccess *iba,const uint32_t instance_co vkCmdBindIndexBuffer(cmd_buf, iba->buffer->GetBuffer(), - iba->start, + iba->start*iba->buffer->GetStride(), VkIndexType(iba->buffer->GetType())); vkCmdDrawIndexed(cmd_buf,iba->count,instance_count,0,0,0); diff --git a/src/SceneGraph/Vulkan/VKPrimitiveData.cpp b/src/SceneGraph/Vulkan/VKPrimitiveData.cpp index b4be84e8..57ac1de7 100644 --- a/src/SceneGraph/Vulkan/VKPrimitiveData.cpp +++ b/src/SceneGraph/Vulkan/VKPrimitiveData.cpp @@ -146,7 +146,7 @@ namespace return(&ib_access); } - VABAccess *InitVAB(const AnsiString &name,const VkFormat &format,const void *data,const VkDeviceSize bytes) + VABAccess *InitVAB(const AnsiString &name,const VkFormat &format,const void *data) { if(!device)return(nullptr); if(!vil)return(nullptr); @@ -164,12 +164,6 @@ namespace if(vif->format!=format) return(nullptr); - if(data) - { - if(vif->stride*vertex_count!=bytes) - return(nullptr); - } - VABAccess *vaba=vab_access+index; if(!vaba->vab) @@ -203,7 +197,7 @@ namespace public: - PrimitiveDataVDM(VertexDataManager *_vdm,const VIL *_vil,const VkDeviceSize vc):PrimitiveData(_vil,vc) + PrimitiveDataVDM(VertexDataManager *_vdm,const VkDeviceSize vc):PrimitiveData(_vdm->GetVIL(),vc) { vdm=_vdm; @@ -240,7 +234,7 @@ namespace return &ib_access; } - VABAccess *InitVAB(const AnsiString &name,const VkFormat &format,const void *data,const VkDeviceSize bytes) + VABAccess *InitVAB(const AnsiString &name,const VkFormat &format,const void *data) { if(!vdm)return(nullptr); if(!vil)return(nullptr); @@ -258,12 +252,6 @@ namespace if(vif->format!=format) return(nullptr); - if(data) - { - if(vif->stride*vertex_count!=bytes) - return(nullptr); - } - VABAccess *vaba=vab_access+index; if(!vaba->vab) @@ -277,7 +265,7 @@ namespace vaba->count=vab_node->GetCount(); } - if(vaba->vab) + if(vaba->vab&&data) vaba->vab->Write(data,vaba->start,vaba->count); return vaba; @@ -294,12 +282,11 @@ PrimitiveData *CreatePrimitiveData(GPUDevice *dev,const VIL *_vil,const VkDevice return(new PrimitiveDataPrivateBuffer(dev,_vil,vc)); } -PrimitiveData *CreatePrimitiveData(VertexDataManager *vdm,const VIL *_vil,const VkDeviceSize vc) +PrimitiveData *CreatePrimitiveData(VertexDataManager *vdm,const VkDeviceSize vc) { if(!vdm)return(nullptr); - if(!_vil)return(nullptr); if(vc<=0)return(nullptr); - return(new PrimitiveDataVDM(vdm,_vil,vc)); + return(new PrimitiveDataVDM(vdm,vc)); } VK_NAMESPACE_END diff --git a/src/SceneGraph/Vulkan/VKPrimitiveData.h b/src/SceneGraph/Vulkan/VKPrimitiveData.h index 161cbd2d..5af5e47e 100644 --- a/src/SceneGraph/Vulkan/VKPrimitiveData.h +++ b/src/SceneGraph/Vulkan/VKPrimitiveData.h @@ -43,9 +43,9 @@ public: public: virtual IBAccess * InitIBO(const VkDeviceSize index_count,IndexType it)=0; - virtual VABAccess *InitVAB(const AnsiString &name,const VkFormat &format,const void *data,const VkDeviceSize bytes)=0; + virtual VABAccess *InitVAB(const AnsiString &name,const VkFormat &format,const void *data)=0; };//class PrimitiveData PrimitiveData *CreatePrimitiveData(GPUDevice *dev,const VIL *_vil,const VkDeviceSize vc); -PrimitiveData *CreatePrimitiveData(VertexDataManager *vdm,const VIL *_vil,const VkDeviceSize vc); +PrimitiveData *CreatePrimitiveData(VertexDataManager *vdm,const VkDeviceSize vc); VK_NAMESPACE_END \ No newline at end of file diff --git a/src/SceneGraph/Vulkan/VKRenderable.cpp b/src/SceneGraph/Vulkan/VKRenderable.cpp index 92e0b90b..870d6c3e 100644 --- a/src/SceneGraph/Vulkan/VKRenderable.cpp +++ b/src/SceneGraph/Vulkan/VKRenderable.cpp @@ -96,7 +96,7 @@ Renderable *CreateRenderable(Primitive *prim,MaterialInstance *mi,Pipeline *p) return(nullptr); } - vid->buffer_offset[i]=vab_access->start; + vid->buffer_offset[i]=vab_access->start*vif->stride; vid->buffer_list[i]=vab_access->vab->GetBuffer(); ++vif; }