diff --git a/inc/hgl/graph/PrimitiveCreater.h b/inc/hgl/graph/PrimitiveCreater.h index 70670e87..7f5780c8 100644 --- a/inc/hgl/graph/PrimitiveCreater.h +++ b/inc/hgl/graph/PrimitiveCreater.h @@ -35,9 +35,11 @@ namespace hgl protected: - uint32 vertices_number; + VkDeviceSize vertices_number; + VkDeviceSize index_number; IndexBuffer * ibo; + void * ibo_map; PVBMap vbo_map; protected: @@ -50,9 +52,9 @@ namespace hgl PrimitiveCreater(RenderResource *sdb,const VIL *); PrimitiveCreater(VertexDataManager *); - virtual ~PrimitiveCreater()=default; + virtual ~PrimitiveCreater(); - virtual bool Init(const uint32 vertices_count,const uint32 index_count,IndexType it=IndexType::ERR); ///<初始化,参数为顶点数量 + virtual bool Init(const uint32 vertices_count,const uint32 index_count,IndexType it=IndexType::AUTO); ///<初始化,参数为顶点数量 template T * AccessVBO(const AnsiString &name) ///<创建一个顶点属性数据缓冲区以及访问器 @@ -76,23 +78,42 @@ namespace hgl bool WriteVBO(const AnsiString &name,const void *data,const uint32_t bytes); ///<直接写入顶点属性数据 - template - T * CreateIBO(const uint count,const T *data=nullptr) ///<创建索引缓冲区 + const IndexType GetIndexType()const{return ibo?ibo->GetType():IndexType::ERR;} ///<取得索引数据类型 + template T *AccessIBO() { - if(ibo) - return(nullptr); - - ibo=db->CreateIBO(IT,count,data); + if(!ibo)return(nullptr); + if(ibo->GetStride()!=sizeof(T))return(nullptr); - if(!ibo) - return(nullptr); - - return (T *)ibo->Map(); + return (T *)ibo_map; } - uint8 * CreateIBO8 (uint count,const uint8 *data=nullptr){return CreateIBO(count,data);} ///<创建8位的索引缓冲区 - uint16 * CreateIBO16(uint count,const uint16 *data=nullptr){return CreateIBO(count,data);} ///<创建16位的索引缓冲区 - uint32 * CreateIBO32(uint count,const uint32 *data=nullptr){return CreateIBO(count,data);} ///<创建32位的索引缓冲区 + 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); + + return(true); + } + + //template + //T * CreateIBO(const uint count,const T *data=nullptr) ///<创建索引缓冲区 + //{ + // if(ibo) + // return(nullptr); + // + // ibo=db->CreateIBO(IT,count,data); + + // if(!ibo) + // return(nullptr); + + // return (T *)ibo->Map(); + //} + + //uint8 * CreateIBO8 (uint count,const uint8 *data=nullptr){return CreateIBO(count,data);} ///<创建8位的索引缓冲区 + //uint16 * CreateIBO16(uint count,const uint16 *data=nullptr){return CreateIBO(count,data);} ///<创建16位的索引缓冲区 + //uint32 * CreateIBO32(uint count,const uint32 *data=nullptr){return CreateIBO(count,data);} ///<创建32位的索引缓冲区 virtual Primitive * Finish(const AnsiString &); ///<结束并创建可渲染对象 };//class PrimitiveCreater diff --git a/inc/hgl/graph/VKIndexBuffer.h b/inc/hgl/graph/VKIndexBuffer.h index ff29484b..5c01dca7 100644 --- a/inc/hgl/graph/VKIndexBuffer.h +++ b/inc/hgl/graph/VKIndexBuffer.h @@ -9,8 +9,9 @@ namespace hgl { class IndexBuffer:public DeviceBuffer { - IndexType index_type; - uint32_t count; + IndexType index_type; + uint stride; + uint32_t count; private: @@ -20,14 +21,20 @@ namespace hgl { index_type=it; count=_count; + + if(index_type==IndexType::U16)stride=2;else + if(index_type==IndexType::U32)stride=4;else + if(index_type==IndexType::U8)stride=1;else + stride=0; } public: ~IndexBuffer()=default; - const IndexType GetType ()const{return index_type;} - const uint32 GetCount()const{return count;} + const IndexType GetType ()const{return index_type;} + const uint GetStride ()const{return stride;} + const uint32 GetCount ()const{return count;} };//class IndexBuffer:public DeviceBuffer }//namespace graph }//namespace hgl diff --git a/src/SceneGraph/InlineGeometry.cpp b/src/SceneGraph/InlineGeometry.cpp index 2c8cd17b..1437ea9a 100644 --- a/src/SceneGraph/InlineGeometry.cpp +++ b/src/SceneGraph/InlineGeometry.cpp @@ -166,7 +166,7 @@ namespace hgl { PrimitiveCreater rc(db,vil); - if(!rc.Init(((pgci->grid_size.Width()+1)+(pgci->grid_size.Height()+1))*2,8)) + if(!rc.Init(((pgci->grid_size.Width()+1)+(pgci->grid_size.Height()+1))*2,0)) return(nullptr); AutoDelete vertex=rc.AccessVBO(VAN::Position); @@ -303,7 +303,7 @@ namespace hgl PrimitiveCreater rc(db,vil); - if(!rc.Init(24,6*2*3)) + if(!rc.Init(24,6*2*3,IndexType::U16)) return(nullptr); rc.WriteVBO(VAN::Position,positions,sizeof(positions)); @@ -341,7 +341,8 @@ namespace hgl } } - rc.CreateIBO16(6*2*3,indices); + //rc.CreateIBO16(6*2*3,indices); + rc.WriteIBO(indices); return rc.Finish("Cube"); } @@ -508,10 +509,15 @@ namespace hgl } } - if(numberVertices<=0xffff) - CreateSphereIndices(rc.CreateIBO16(numberIndices),numberParallels,numberSlices); - else - CreateSphereIndices(rc.CreateIBO32(numberIndices),numberParallels,numberSlices); + //索引 + { + const IndexType index_type=rc.GetIndexType(); + + if(index_type==IndexType::U16)CreateSphereIndices(rc.AccessIBO(),numberParallels,numberSlices);else + if(index_type==IndexType::U32)CreateSphereIndices(rc.AccessIBO(),numberParallels,numberSlices);else + if(index_type==IndexType::U8 )CreateSphereIndices(rc.AccessIBO(),numberParallels,numberSlices);else + return(nullptr); + } return rc.Finish("Sphere"); } @@ -594,10 +600,15 @@ namespace hgl } } - if(numberVertices<=0xffff) - CreateSphereIndices(rc.CreateIBO16(numberIndices),numberParallels,numberSlices); - else - CreateSphereIndices(rc.CreateIBO32(numberIndices),numberParallels,numberSlices); + //索引 + { + const IndexType index_type=rc.GetIndexType(); + + if(index_type==IndexType::U16)CreateSphereIndices(rc.AccessIBO(),numberParallels,numberSlices);else + if(index_type==IndexType::U32)CreateSphereIndices(rc.AccessIBO(),numberParallels,numberSlices);else + if(index_type==IndexType::U8 )CreateSphereIndices(rc.AccessIBO(),numberParallels,numberSlices);else + return(nullptr); + } return rc.Finish("Dome"); } @@ -735,11 +746,15 @@ namespace hgl } } - if(numberVertices<=0xffff) - CreateTorusIndices(rc.CreateIBO16(numberIndices),tci->numberSlices,tci->numberStacks); - else - CreateTorusIndices(rc.CreateIBO32(numberIndices),tci->numberSlices,tci->numberStacks); + //索引 + { + const IndexType index_type=rc.GetIndexType(); + if(index_type==IndexType::U16)CreateTorusIndices(rc.AccessIBO(),tci->numberSlices,tci->numberStacks);else + if(index_type==IndexType::U32)CreateTorusIndices(rc.AccessIBO(),tci->numberSlices,tci->numberStacks);else + if(index_type==IndexType::U8 )CreateTorusIndices(rc.AccessIBO(),tci->numberSlices,tci->numberStacks);else + return(nullptr); + } return rc.Finish("Torus"); } @@ -964,10 +979,15 @@ namespace hgl } } - if(numberVertices<=0xffff) - CreateCylinderIndices(rc.CreateIBO16(numberIndices),cci->numberSlices); - else - CreateCylinderIndices(rc.CreateIBO32(numberIndices),cci->numberSlices); + //索引 + { + const IndexType index_type=rc.GetIndexType(); + + if(index_type==IndexType::U16)CreateCylinderIndices(rc.AccessIBO(),cci->numberSlices);else + if(index_type==IndexType::U32)CreateCylinderIndices(rc.AccessIBO(),cci->numberSlices);else + if(index_type==IndexType::U8 )CreateCylinderIndices(rc.AccessIBO(),cci->numberSlices);else + return(nullptr); + } return rc.Finish("Cylinder"); } @@ -1130,10 +1150,15 @@ namespace hgl } } - if(numberVertices<=0xffff) - CreateConeIndices(rc.CreateIBO16(numberIndices),cci->numberSlices,cci->numberStacks); - else - CreateConeIndices(rc.CreateIBO32(numberIndices),cci->numberSlices,cci->numberStacks); + //索引 + { + const IndexType index_type=rc.GetIndexType(); + + if(index_type==IndexType::U16)CreateConeIndices(rc.AccessIBO(),cci->numberSlices,cci->numberStacks);else + if(index_type==IndexType::U32)CreateConeIndices(rc.AccessIBO(),cci->numberSlices,cci->numberStacks);else + if(index_type==IndexType::U8 )CreateConeIndices(rc.AccessIBO(),cci->numberSlices,cci->numberStacks);else + return(nullptr); + } return rc.Finish("Cone"); } @@ -1189,7 +1214,7 @@ namespace hgl PrimitiveCreater rc(db,vil); - if(!rc.Init(8,24)) + if(!rc.Init(8,24,IndexType::U16)) return(nullptr); AutoDelete vertex=rc.AccessVBO(VAN::Position); @@ -1214,7 +1239,7 @@ namespace hgl } } - rc.CreateIBO16(24,indices); + rc.WriteIBO(indices); return rc.Finish("BoundingBox"); } diff --git a/src/SceneGraph/PrimitiveCreater.cpp b/src/SceneGraph/PrimitiveCreater.cpp index 260fa77b..1fe5885b 100644 --- a/src/SceneGraph/PrimitiveCreater.cpp +++ b/src/SceneGraph/PrimitiveCreater.cpp @@ -17,7 +17,9 @@ namespace hgl vil =v; vertices_number =0; + index_number =0; ibo =nullptr; + ibo_map =nullptr; } PrimitiveCreater::PrimitiveCreater(VertexDataManager *_vdm) @@ -30,7 +32,18 @@ namespace hgl vil =vdm->GetVIL(); vertices_number =0; + index_number =0; ibo =nullptr; + ibo_map =nullptr; + } + + PrimitiveCreater::~PrimitiveCreater() + { + if(ibo) + { + ibo->Unmap(); + delete ibo; + } } bool PrimitiveCreater::Init(const uint32 vertex_count,const uint32 index_count,IndexType it) @@ -41,24 +54,20 @@ namespace hgl if(index_count>0) { + index_number=index_count; + if(vdm) { - + } else { - if(it==IndexType::ERR) + if(it==IndexType::AUTO) { - if(vertex_count<=0xFF&&phy_device->SupportU8Index()) - it=IndexType::U8; - else - if(vertex_count<=0xFFFF) - it=IndexType::U16; - else - if(phy_device->SupportU32Index()) - it=IndexType::U32; - else - return(false); + it=device->GetIndexType(vertex_count); + + if(!IsIndexType(it)) + return(false); } else if(it==IndexType::U8) { @@ -80,11 +89,12 @@ namespace hgl return(false); } - ibo=db->CreateIBO(it,index_count); + ibo=device->CreateIBO(it,index_count); + //ibo=db->CreateIBO(it,index_count); if(!ibo)return(false); } - ibo->Map(); + ibo_map=ibo->Map(); } return(true); @@ -171,12 +181,6 @@ namespace hgl Primitive *primitive=db->CreatePrimitive(prim_name,vertices_number); - if(ibo) - { - ibo->Unmap(); - primitive->Set(ibo); - } - const auto *sp=vbo_map.GetDataList(); for(uint i=0;iUnmap(); + primitive->Set(ibo); + + db->Add(ibo); + ibo=nullptr; //避免释构函数删除 + } + db->Add(primitive); return primitive;