preparing PrimitiveCreater to support multi primitive

This commit is contained in:
hyzboy 2024-05-25 03:14:26 +08:00
parent 9a4e495027
commit 4a94d78c1f
7 changed files with 138 additions and 83 deletions

View File

@ -10,7 +10,7 @@
#include<hgl/graph/VKVertexAttribBuffer.h> #include<hgl/graph/VKVertexAttribBuffer.h>
#include<hgl/graph/mtl/Material3DCreateConfig.h> #include<hgl/graph/mtl/Material3DCreateConfig.h>
#include<hgl/graph/mtl/BlinnPhong.h> #include<hgl/graph/mtl/BlinnPhong.h>
//#include<hgl/graph/VertexDataManager.h> #include<hgl/graph/VertexDataManager.h>
using namespace hgl; using namespace hgl;
using namespace hgl::graph; using namespace hgl::graph;
@ -40,7 +40,7 @@ private: //plane grid
Material * mtl_vertex_lum =nullptr; Material * mtl_vertex_lum =nullptr;
MaterialInstance * mi_plane_grid =nullptr; MaterialInstance * mi_plane_grid =nullptr;
Pipeline * p_line =nullptr; Pipeline * p_line =nullptr;
AutoDelete<Primitive> prim_plane_grid =nullptr; Primitive * prim_plane_grid =nullptr;
private: private:
@ -49,14 +49,14 @@ private:
private: //sphere private: //sphere
Material * mtl_blinnphong =nullptr; Material * mtl_blinnphong =nullptr;
//VertexDataManager * vdm_blinnphong =nullptr; VertexDataManager * vdm_blinnphong =nullptr;
MaterialInstance * mi_blinnphong[4]{}; MaterialInstance * mi_blinnphong[4]{};
Pipeline * p_blinnphong =nullptr; Pipeline * p_blinnphong =nullptr;
AutoDelete<Primitive> prim_sphere =nullptr; Primitive * prim_sphere =nullptr;
AutoDelete<Primitive> prim_cone =nullptr; Primitive * prim_cone =nullptr;
AutoDelete<Primitive> prim_cylinder =nullptr; Primitive * prim_cylinder =nullptr;
private: private:
@ -100,8 +100,6 @@ private:
mtl_blinnphong->BindUBO(DescriptorSetType::Global,"sun",ubo_sun); mtl_blinnphong->BindUBO(DescriptorSetType::Global,"sun",ubo_sun);
mtl_blinnphong->Update(); mtl_blinnphong->Update();
//vdm_blinnphong=new VertexDataManager(device,mtl_blinnphong->GetDefaultVIL());
Color4f mi_data; Color4f mi_data;
for(uint i=0;i<4;i++) for(uint i=0;i<4;i++)
{ {
@ -121,6 +119,22 @@ private:
return(true); 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() bool CreateRenderObject()
{ {
using namespace inline_geometry; using namespace inline_geometry;
@ -224,6 +238,9 @@ public:
if(!InitBlinnPhongSunLightMP()) if(!InitBlinnPhongSunLightMP())
return(false); return(false);
if(!InitVDM())
return(false);
if(!CreateRenderObject()) if(!CreateRenderObject())
return(false); return(false);
@ -232,6 +249,16 @@ public:
return(true); 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 };//class TestApp:public CameraAppFramework
int main(int,char **) int main(int,char **)

View File

@ -18,24 +18,26 @@ protected:
const VIL * vil; const VIL * vil;
AnsiString prim_name;
PrimitiveData * prim_data;
protected: protected:
VkDeviceSize vertices_number; ///<顶点数量 AnsiString prim_name;
PrimitiveData * prim_data;
VkDeviceSize index_number; ///<索引数量 VkDeviceSize vertices_number; ///<顶点数量
IndexType index_type; ///<索引类型
IBAccess * iba; ///<索引缓冲区 VkDeviceSize index_number; ///<索引数量
IndexType index_type; ///<索引类型
IBAccess * iba; ///<索引缓冲区
public: public:
PrimitiveCreater(GPUDevice *,const VIL *,const AnsiString &name); PrimitiveCreater(GPUDevice *,const VIL *);
PrimitiveCreater(VertexDataManager *,const VIL *,const AnsiString &name); PrimitiveCreater(VertexDataManager *,const VIL *);
virtual ~PrimitiveCreater(); 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: //顶点缓冲区 public: //顶点缓冲区
@ -61,7 +63,7 @@ public: //索引缓冲区
public: //创建可渲染对象 public: //创建可渲染对象
virtual Primitive * Create(); ///<创建一个可渲染对象 virtual Primitive * Create(); ///<创建一个可渲染对象,并清除创建器数据
};//class PrimitiveCreater };//class PrimitiveCreater
/** /**

View File

@ -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 #define HGL_GRAPH_VULKAN_VERTEX_ATTRIB_BUFFER_INCLUDE
#include<hgl/graph/VKBuffer.h> #include<hgl/graph/VKBuffer.h>
@ -9,9 +9,9 @@ namespace hgl
{ {
class VertexAttribBuffer:public DeviceBuffer class VertexAttribBuffer:public DeviceBuffer
{ {
VkFormat format; ///<数据格式 VkFormat format; ///<数据格式
uint32_t stride; ///<单个数据字节数 uint32_t stride; ///<单个数据字节数
uint32_t count; ///<数据数量 uint32_t count; ///<数据数量
private: private:

View File

@ -40,9 +40,9 @@ public:
const VkDeviceSize GetVABMaxCount ()const{return vab_max_size;} ///<取得顶点属性缓冲区分配的空间最大数量 const VkDeviceSize GetVABMaxCount ()const{return vab_max_size;} ///<取得顶点属性缓冲区分配的空间最大数量
const VkDeviceSize GetVABCurCount ()const{return vab_cur_size;} ///<取得顶点属性缓冲区当前数量 const VkDeviceSize GetVABCurCount ()const{return vab_cur_size;} ///<取得顶点属性缓冲区当前数量
const IndexType GetIBOType ()const{return ibo?ibo->GetType():IndexType::ERR;} ///<取得索引缓冲区类型 const IndexType GetIndexType ()const{return ibo?ibo->GetType():IndexType::ERR;} ///<取得索引缓冲区类型
const VkDeviceSize GetIBOMaxCount ()const{return ibo?ibo->GetCount():-1;} ///<取得索引缓冲区分配的空间最大数量 const VkDeviceSize GetIndexMaxCount ()const{return ibo?ibo->GetCount():-1;} ///<取得索引缓冲区分配的空间最大数量
const VkDeviceSize GetIBOCurCount ()const{return ibo?ibo_cur_size:-1;} ///<取得索引缓冲区当前数量 const VkDeviceSize GetIndexCurCount ()const{return ibo?ibo_cur_size:-1;} ///<取得索引缓冲区当前数量
public: public:

View File

@ -15,9 +15,9 @@ namespace hgl
{ {
Primitive *CreateRectangle(GPUDevice *device,const VIL *vil,const RectangleCreateInfo *rci) 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); return(nullptr);
VABMap2f vertex(&rc,VAN::Position); VABMap2f vertex(&rc,VAN::Position);
@ -41,11 +41,11 @@ namespace hgl
Primitive *CreateRoundRectangle(GPUDevice *device,const VIL *vil,const RoundRectangleCreateInfo *rci) 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(rci->radius==0||rci->round_per<=1) //这是要画矩形
{ {
if(!rc.Init(4,0)) if(!rc.Init("RoundRectangle",4,0))
return(nullptr); return(nullptr);
VABMap2f vertex(&rc,VAN::Position); 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.GetWidth()/2.0f)radius=rci->scope.GetWidth()/2.0f;
if(radius>rci->scope.GetHeight()/2.0f)radius=rci->scope.GetHeight()/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); return(nullptr);
VABMap2f vertex(&rc,VAN::Position); VABMap2f vertex(&rc,VAN::Position);
@ -115,21 +115,24 @@ namespace hgl
Primitive *CreateCircle(GPUDevice *device,const VIL *vil,const CircleCreateInfo *cci) Primitive *CreateCircle(GPUDevice *device,const VIL *vil,const CircleCreateInfo *cci)
{ {
PrimitiveCreater rc(device,vil,"Circle"); PrimitiveCreater rc(device,vil);
uint edge; uint edge;
uint vertex_count;
if(cci->has_color) if(cci->has_color)
{ {
edge=cci->field_count+1; edge=cci->field_count+1;
if(!rc.Init(cci->field_count+2,0))return(nullptr); vertex_count=cci->field_count+2;
} }
else else
{ {
edge=cci->field_count; 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); VABMap2f vertex(&rc,VAN::Position);
VABMap4f color(&rc,VAN::Color); VABMap4f color(&rc,VAN::Color);
@ -163,9 +166,9 @@ namespace hgl
Primitive *CreatePlaneGrid(GPUDevice *device,const VIL *vil,const PlaneGridCreateInfo *pgci) 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); return(nullptr);
VABMap3f vertex(&rc,VAN::Position); VABMap3f vertex(&rc,VAN::Position);
@ -219,9 +222,9 @@ namespace hgl
const Vector3f xy_normal(0.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); 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); return(nullptr);
if(!rc.WriteVAB(VAN::Position,VF_V3F,xy_vertices,sizeof(xy_vertices))) if(!rc.WriteVAB(VAN::Position,VF_V3F,xy_vertices,sizeof(xy_vertices)))
@ -304,9 +307,9 @@ namespace hgl
16, 17, 18, 16, 18, 19, 16, 17, 18, 16, 18, 19,
20, 23, 22, 20, 22, 21}; 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); return(nullptr);
if(!rc.WriteVAB(VAN::Position,VF_V3F,positions,sizeof(positions))) 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) 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 numberParallels = (numberSlices+1) / 2;
uint numberVertices = (numberParallels + 1) * (numberSlices + 1); uint numberVertices = (numberParallels + 1) * (numberSlices + 1);
@ -468,7 +471,7 @@ namespace hgl
float helpMatrix[16]; float helpMatrix[16];
float tex_x; float tex_x;
if(!rc.Init(numberVertices,numberIndices)) if(!rc.Init("Sphere",numberVertices,numberIndices))
return(nullptr); return(nullptr);
VABRawMapFloat vertex (&rc,VF_V3F,VAN::Position); VABRawMapFloat vertex (&rc,VF_V3F,VAN::Position);
@ -538,7 +541,7 @@ namespace hgl
Primitive *CreateDome(GPUDevice *device,const VIL *vil,const uint numberSlices) Primitive *CreateDome(GPUDevice *device,const VIL *vil,const uint numberSlices)
{ {
PrimitiveCreater rc(device,vil,"Dome"); PrimitiveCreater rc(device,vil);
uint i, j; uint i, j;
@ -557,7 +560,7 @@ namespace hgl
if (numberSlices < 3 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES) if (numberSlices < 3 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES)
return nullptr; return nullptr;
if(!rc.Init(numberVertices,numberIndices)) if(!rc.Init("Dome",numberVertices,numberIndices))
return(nullptr); return(nullptr);
VABRawMapFloat vertex (&rc,VF_V3F,VAN::Position); VABRawMapFloat vertex (&rc,VF_V3F,VAN::Position);
@ -670,7 +673,7 @@ namespace hgl
Primitive *CreateTorus(GPUDevice *device,const VIL *vil,const TorusCreateInfo *tci) 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] // s, t = parametric values of the equations, in the range [0,1]
float s = 0; float s = 0;
@ -705,7 +708,7 @@ namespace hgl
sIncr = 1.0f / (float) tci->numberSlices; sIncr = 1.0f / (float) tci->numberSlices;
tIncr = 1.0f / (float) tci->numberStacks; tIncr = 1.0f / (float) tci->numberStacks;
if(!rc.Init(numberVertices,numberIndices)) if(!rc.Init("Torus",numberVertices,numberIndices))
return(nullptr); return(nullptr);
VABRawMapFloat vertex (&rc,VF_V3F,VAN::Position); VABRawMapFloat vertex (&rc,VF_V3F,VAN::Position);
@ -840,11 +843,11 @@ namespace hgl
if(numberIndices<=0) if(numberIndices<=0)
return(nullptr); return(nullptr);
PrimitiveCreater rc(device,vil,"Cylinder"); PrimitiveCreater rc(device,vil);
uint numberVertices = (cci->numberSlices + 2) * 2 + (cci->numberSlices + 1) * 2; uint numberVertices = (cci->numberSlices + 2) * 2 + (cci->numberSlices + 1) * 2;
if(!rc.Init(numberVertices,numberIndices)) if(!rc.Init("Cylinder",numberVertices,numberIndices))
return(nullptr); return(nullptr);
float angleStep = (2.0f * HGL_PI) / ((float) cci->numberSlices); 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) Primitive *CreateCone(GPUDevice *device,const VIL *vil,const ConeCreateInfo *cci)
{ {
PrimitiveCreater rc(device,vil,"Cone"); PrimitiveCreater rc(device,vil);
uint i, j; uint i, j;
uint numberVertices = (cci->numberSlices + 2) + (cci->numberSlices + 1) * (cci->numberStacks + 1); uint numberVertices = (cci->numberSlices + 2) + (cci->numberSlices + 1) * (cci->numberStacks + 1);
uint numberIndices = cci->numberSlices * 3 + cci->numberSlices * 6 * cci->numberStacks; uint numberIndices = cci->numberSlices * 3 + cci->numberSlices * 6 * cci->numberStacks;
if(!rc.Init(numberVertices,numberIndices)) if(!rc.Init("Cone",numberVertices,numberIndices))
return(nullptr); return(nullptr);
float angleStep = (2.0f * HGL_PI) / ((float) cci->numberSlices); float angleStep = (2.0f * HGL_PI) / ((float) cci->numberSlices);
@ -1201,9 +1204,9 @@ namespace hgl
{ {
if(!device||!vil||!aci)return(nullptr); 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); return(nullptr);
VABMap3f vertex(&rc,VAN::Position); VABMap3f vertex(&rc,VAN::Position);
@ -1246,9 +1249,9 @@ namespace hgl
0,4, 1,5, 2,6, 3,7 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); return(nullptr);
if(!rc.WriteVAB(VAN::Position,VF_V3F,points,sizeof(points))) if(!rc.WriteVAB(VAN::Position,VF_V3F,points,sizeof(points)))

View File

@ -7,24 +7,19 @@
#include"vulkan/VKPrimitiveData.h" #include"vulkan/VKPrimitiveData.h"
VK_NAMESPACE_BEGIN VK_NAMESPACE_BEGIN
PrimitiveCreater::PrimitiveCreater(GPUDevice *dev,const VIL *v,const AnsiString &name) PrimitiveCreater::PrimitiveCreater(GPUDevice *dev,const VIL *v)
{ {
device =dev; device =dev;
vdm =nullptr; vdm =nullptr;
vil =v; vil =v;
prim_name =name;
prim_data =nullptr; prim_data =nullptr;
vertices_number =0; Clear();
index_number =0;
index_type =IndexType::ERR;
iba =nullptr;
} }
PrimitiveCreater::PrimitiveCreater(VertexDataManager *_vdm,const VIL *v,const AnsiString &name) PrimitiveCreater::PrimitiveCreater(VertexDataManager *_vdm,const VIL *v)
:PrimitiveCreater(_vdm->GetDevice(),v,name) :PrimitiveCreater(_vdm->GetDevice(),v)
{ {
vdm=_vdm; vdm=_vdm;
} }
@ -34,19 +29,25 @@ PrimitiveCreater::~PrimitiveCreater()
SAFE_CLEAR(prim_data); 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(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(index_count>0)
{ {
if(it==IndexType::AUTO) if(it==IndexType::AUTO)
@ -62,13 +63,32 @@ bool PrimitiveCreater::Init(const VkDeviceSize vertex_count,const VkDeviceSize i
return(false); return(false);
} }
iba=prim_data->InitIBO(index_count,it);
if(!iba)
return(false);
index_type=it; index_type=it;
index_number=index_count; 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 #ifdef _DEBUG
DebugUtils *du=device->GetDebugUtils(); DebugUtils *du=device->GetDebugUtils();
@ -81,6 +101,8 @@ bool PrimitiveCreater::Init(const VkDeviceSize vertex_count,const VkDeviceSize i
#endif//_DEBUG #endif//_DEBUG
} }
prim_name=pname;
return(true); return(true);
} }
@ -148,8 +170,9 @@ Primitive *PrimitiveCreater::Create()
if(!primitive) if(!primitive)
return(nullptr); return(nullptr);
prim_data=nullptr; prim_data=nullptr; //带入Primitive后不在这里删除
iba=nullptr;
Clear();
return primitive; return primitive;
} }

View File

@ -103,7 +103,7 @@ namespace
device=dev; device=dev;
} }
~PrimitiveDataPrivateBuffer() ~PrimitiveDataPrivateBuffer() override
{ {
VABAccess *vab=vab_access; VABAccess *vab=vab_access;
@ -211,7 +211,7 @@ namespace
vab_node=vdm->AcquireVAB(vc); vab_node=vdm->AcquireVAB(vc);
} }
~PrimitiveDataVDM() ~PrimitiveDataVDM() override
{ {
if(ib_node) if(ib_node)
vdm->ReleaseIB(ib_node); vdm->ReleaseIB(ib_node);