used newly VKBufferMap in PrimitiveCreater

This commit is contained in:
hyzboy 2024-06-02 12:16:33 +08:00
parent 16ae849809
commit 66ef3160e1
7 changed files with 203 additions and 160 deletions

View File

@ -1,4 +1,4 @@
// Gizmo 3D Move // Gizmo 3D Move
#include"VulkanAppFramework.h" #include"VulkanAppFramework.h"
#include<hgl/filesystem/FileSystem.h> #include<hgl/filesystem/FileSystem.h>
@ -31,7 +31,7 @@ private:
Material * mtl_vtx_color =nullptr; Material * mtl_vtx_color =nullptr;
MaterialInstance * mi_line =nullptr; MaterialInstance * mi_line =nullptr;
Pipeline * pipeline_vtx_color =nullptr; Pipeline * pipeline_vtx_color =nullptr;
Primitive * ro_line =nullptr; Primitive * prim_line =nullptr;
private: private:
@ -103,12 +103,12 @@ private:
} }
/** /**
* 线. * 线.
* *
* \param pos * \param pos
* \param max_line 线 * \param max_line 线
* \param oa1 1 * \param oa1 1
* \param oa2 2 * \param oa2 2
*/ */
void WriteAxisPosition(Vector3f *pos,const Vector3f &max_line,const Vector3f &oa1,const Vector3f &oa2) void WriteAxisPosition(Vector3f *pos,const Vector3f &max_line,const Vector3f &oa1,const Vector3f &oa2)
{ {
@ -116,15 +116,15 @@ private:
constexpr const float AXIS_MIN_STEP =1; constexpr const float AXIS_MIN_STEP =1;
constexpr const float AXIS_ARROW_SIZE=0.25; constexpr const float AXIS_ARROW_SIZE=0.25;
const Vector3f end_pos =max_line*AXIS_LENGTH; ///<最终点位置 const Vector3f end_pos =max_line*AXIS_LENGTH; ///<最终点位置
const Vector3f cross_pos=max_line*AXIS_MIN_STEP; ///<坐标轴尾部交叉线位置 const Vector3f cross_pos=max_line*AXIS_MIN_STEP; ///<坐标轴尾部交叉线位置
const Vector3f arrow_pos=max_line*(AXIS_LENGTH-AXIS_MIN_STEP); ///<箭头末端在主线上的位置 const Vector3f arrow_pos=max_line*(AXIS_LENGTH-AXIS_MIN_STEP); ///<箭头末端在主线上的位置
//主线 //主线
pos[0]=Vector3f(0, 0, 0); pos[0]=Vector3f(0, 0, 0);
pos[1]=end_pos; pos[1]=end_pos;
//四根箭头线 //四根箭头线
pos[2]=end_pos; pos[2]=end_pos;
pos[3]=arrow_pos-oa1*AXIS_ARROW_SIZE; pos[3]=arrow_pos-oa1*AXIS_ARROW_SIZE;
@ -137,7 +137,7 @@ private:
pos[8]=end_pos; pos[8]=end_pos;
pos[9]=arrow_pos+oa2*AXIS_ARROW_SIZE; pos[9]=arrow_pos+oa2*AXIS_ARROW_SIZE;
//侧边连接其它轴线 //侧边连接其它轴线
pos[10]=cross_pos; pos[10]=cross_pos;
pos[11]=cross_pos+oa1*AXIS_MIN_STEP; pos[11]=cross_pos+oa1*AXIS_MIN_STEP;
pos[12]=cross_pos; pos[12]=cross_pos;
@ -165,8 +165,8 @@ private:
constexpr const uint AXIS_MAX_LINES =7; constexpr const uint AXIS_MAX_LINES =7;
constexpr const uint AXIS_MAX_VERTICES =AXIS_MAX_LINES*2*3; constexpr const uint AXIS_MAX_VERTICES =AXIS_MAX_LINES*2*3;
ro_line=db->CreatePrimitive("Line",AXIS_MAX_VERTICES); prim_line=db->CreatePrimitive("Line",AXIS_MAX_VERTICES);
if(!ro_line)return(false); if(!prim_line)return(false);
Vector3f position_data[3][AXIS_MAX_LINES*2]; Vector3f position_data[3][AXIS_MAX_LINES*2];
@ -180,8 +180,8 @@ private:
for(Color4f &c:color_data[1])c=Color4f(0,1,0,1); for(Color4f &c:color_data[1])c=Color4f(0,1,0,1);
for(Color4f &c:color_data[2])c=Color4f(0,0,1,1); for(Color4f &c:color_data[2])c=Color4f(0,0,1,1);
if(!ro_line->Set(VAN::Position, db->CreateVAB(VF_V3F,AXIS_MAX_VERTICES,position_data)))return(false); if(!prim_line->Set(VAN::Position, db->CreateVAB(VF_V3F,AXIS_MAX_VERTICES,position_data)))return(false);
if(!ro_line->Set(VAN::Color, db->CreateVAB(VF_V4F,AXIS_MAX_VERTICES,color_data )))return(false); if(!prim_line->Set(VAN::Color, db->CreateVAB(VF_V4F,AXIS_MAX_VERTICES,color_data )))return(false);
} }
return(true); return(true);
@ -190,7 +190,7 @@ private:
bool InitScene() bool InitScene()
{ {
Add(prim_plane_grid,mi_plane_grid,pipeline_vtx_lum); Add(prim_plane_grid,mi_plane_grid,pipeline_vtx_lum);
Add(ro_line,mi_line,pipeline_vtx_color); Add(prim_line,mi_line,pipeline_vtx_color);
camera->pos=Vector3f(32,32,32); camera->pos=Vector3f(32,32,32);
camera_control->SetTarget(Vector3f(0,0,0)); camera_control->SetTarget(Vector3f(0,0,0));

View File

@ -9,6 +9,7 @@
#include<hgl/graph/Ray.h> #include<hgl/graph/Ray.h>
#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/VertexDataManager.h>
using namespace hgl; using namespace hgl;
using namespace hgl::graph; using namespace hgl::graph;
@ -38,9 +39,12 @@ private:
Pipeline * pipeline =nullptr; Pipeline * pipeline =nullptr;
VertexDataManager * vdm =nullptr;
PrimitiveCreater * prim_creater =nullptr;
Primitive * prim_plane_grid =nullptr; Primitive * prim_plane_grid =nullptr;
Primitive * ro_line =nullptr; Primitive * prim_line =nullptr;
VAB * vab_pos =nullptr; VAB * vab_pos =nullptr;
@ -71,6 +75,24 @@ private:
return(true); return(true);
} }
bool InitVDMAndPC()
{
vdm=new VertexDataManager(device,material->GetDefaultVIL());
if(!vdm->Init( 1024*1024, //VAB最大容量
1024*1024, //索引最大容量
IndexType::U16)) //索引类型
{
delete vdm;
vdm=nullptr;
return(false);
}
prim_creater=new PrimitiveCreater(vdm);
return(true);
}
Renderable *Add(Primitive *r,MaterialInstance *mi) Renderable *Add(Primitive *r,MaterialInstance *mi)
{ {
Renderable *ri=db->CreateRenderable(r,mi,pipeline); Renderable *ri=db->CreateRenderable(r,mi,pipeline);
@ -99,15 +121,19 @@ private:
pgci.lum=0.5; pgci.lum=0.5;
pgci.sub_lum=0.75; pgci.sub_lum=0.75;
prim_plane_grid=CreatePlaneGrid(db,material->GetDefaultVIL(),&pgci); prim_plane_grid=CreatePlaneGrid(prim_creater,&pgci);
} }
{ {
ro_line=db->CreatePrimitive("Line",2); if(!prim_creater->Init("Line",2))
if(!ro_line)return(false); return(false);
if(!ro_line->Set(VAN::Position, vab_pos= db->CreateVAB(VF_V3F,2,position_data )))return(false); if(!prim_creater->WriteVAB(VAN::Position, VF_V3F,position_data))return(false);
if(!ro_line->Set(VAN::Luminance, db->CreateVAB(VF_V1F,2,lumiance_data )))return(false); if(!prim_creater->WriteVAB(VAN::Luminance,VF_V1F,lumiance_data))return(false);
prim_line=prim_creater->Create();
prim_line->Getv
} }
return(true); return(true);
@ -116,7 +142,7 @@ private:
bool InitScene() bool InitScene()
{ {
Add(prim_plane_grid,mi_plane_grid); Add(prim_plane_grid,mi_plane_grid);
Add(ro_line,mi_line); Add(prim_line,mi_line);
camera->pos=Vector3f(32,32,32); camera->pos=Vector3f(32,32,32);
camera_control->SetTarget(Vector3f(0,0,0)); camera_control->SetTarget(Vector3f(0,0,0));
@ -130,6 +156,12 @@ private:
public: public:
~TestApp()
{
SAFE_CLEAR(prim_creater)
SAFE_CLEAR(vdm)
}
bool Init(uint w,uint h) bool Init(uint w,uint h)
{ {
if(!SceneAppFramework::Init(w,h)) if(!SceneAppFramework::Init(w,h))
@ -138,6 +170,9 @@ public:
if(!InitMaterialAndPipeline()) if(!InitMaterialAndPipeline())
return(false); return(false);
if(!InitVDMAndPC())
return(false);
if(!CreateRenderObject()) if(!CreateRenderObject())
return(false); return(false);

View File

@ -24,12 +24,18 @@ protected:
uint32_t vertices_number; ///<顶点数量 uint32_t vertices_number; ///<顶点数量
void_pointer *map_ptr_list; ///<映射指针列表 VKBufferMap * vab_map_list;
uint32_t index_number; ///<索引数量 uint32_t index_number; ///<索引数量
IndexType index_type; ///<索引类型 IndexType index_type; ///<索引类型
IndexBuffer * ibo; ///<索引缓冲区 IndexBuffer * ibo; ///<索引缓冲区
VKBufferMap ibo_map;
protected:
const int GetVABIndex (const AnsiString &name,const VkFormat &format); ///<取得顶点属性索引
public: public:
PrimitiveCreater(GPUDevice *,const VIL *); PrimitiveCreater(GPUDevice *,const VIL *);
@ -53,10 +59,7 @@ public: //顶点缓冲区
const uint32_t GetVertexCount()const{ return vertices_number; } ///<取得顶点数量 const uint32_t GetVertexCount()const{ return vertices_number; } ///<取得顶点数量
const int GetVABIndex (const AnsiString &name,const VkFormat &format); ///<取得顶点属性索引 VKBufferMap * MapVAB (const AnsiString &name,const VkFormat &format);
void * MapVAB (const int vab_index); ///<映射一个顶点属性数据区
void UnmapVAB (const int vab_index); ///<取消映射
bool WriteVAB (const AnsiString &name,const VkFormat &format,const void *data); ///<直接写入顶点属性数据 bool WriteVAB (const AnsiString &name,const VkFormat &format,const void *data); ///<直接写入顶点属性数据
@ -65,8 +68,7 @@ public: //索引缓冲区
const IndexType GetIndexType()const{return index_type;} ///<取得索引类型 const IndexType GetIndexType()const{return index_type;} ///<取得索引类型
const uint32_t GetIndexCount()const{return index_number;} ///<取得索引数量 const uint32_t GetIndexCount()const{return index_number;} ///<取得索引数量
void * MapIBO(); VKBufferMap * MapIBO();
void UnmapIBO();
bool WriteIBO(const void *data,const uint32_t count); bool WriteIBO(const void *data,const uint32_t count);
@ -79,89 +81,78 @@ public: //创建可渲染对象
};//class PrimitiveCreater };//class PrimitiveCreater
/** /**
* VAB原生数据访问映 * 访
*/ */
template<typename T> class VABRawMap template<typename T> class VABRawMap
{ {
PrimitiveCreater *pc; VKBufferMap *buf_map;
int vab_index;
T *map_ptr; T *map_ptr;
public: public:
VABRawMap(PrimitiveCreater *c,const VkFormat &format,const AnsiString &name) VABRawMap(PrimitiveCreater *pc,const VkFormat &format,const AnsiString &name)
{ {
pc=c; buf_map=pc->MapVAB(name,format);
vab_index=pc->GetVABIndex(name,format);
if(buf_map)
map_ptr=(T *)(pc->MapVAB(vab_index)); map_ptr=(T *)(buf_map->Map());
else
map_ptr=nullptr;
} }
~VABRawMap() ~VABRawMap()
{ {
if(map_ptr) if(map_ptr)
pc->UnmapVAB(vab_index); buf_map->Unmap();
} }
const bool IsValid()const{ return map_ptr; } const bool IsValid()const{ return buf_map?buf_map->IsValid():false; }
operator T *(){ return map_ptr; } operator T *(){ return map_ptr; }
T *operator->(){ return map_ptr; }
};//template<typename T> class VABRawMap };//template<typename T> class VABRawMap
typedef VABRawMap<int8> VABRawMapi8, VABRawMapByte; typedef VABRawMap<int8> VABMapI8, VABMapByte;
typedef VABRawMap<int16> VABRawMapi16,VABRawMapShort; typedef VABRawMap<int16> VABMapI16, VABMapShort;
typedef VABRawMap<int32> VABRawMapi32,VABRawMapInt; typedef VABRawMap<int32> VABMapI32, VABMapInt;
typedef VABRawMap<uint8> VABRawMapu8, VABRawMapUByte; typedef VABRawMap<uint8> VABMapU8, VABMapUByte;
typedef VABRawMap<uint16> VABRawMapu16,VABRawMapUShort; typedef VABRawMap<uint16> VABMapU16, VABMapUShort;
typedef VABRawMap<uint32> VABRawMapu32,VABRawMapUInt; typedef VABRawMap<uint32> VABMapU32, VABMapUInt;
typedef VABRawMap<float> VABRawMapFloat; typedef VABRawMap<float> VABMapFloat;
typedef VABRawMap<double> VABRawMapDouble; typedef VABRawMap<double> VABMapDouble;
/** /**
* VAB VertexAttribDataAccess数据访问映 * 访
*/ */
template<typename T> class VABMap template<typename T> class VABMap
{ {
PrimitiveCreater *pc; VKBufferMap *buf_map;
int vab_index;
T *vb; T *map_ptr;
public: public:
VABMap(PrimitiveCreater *c,const AnsiString &name) VABMap(PrimitiveCreater *pc,const AnsiString &name)
{ {
pc=c; buf_map=pc->MapVAB(name,T::GetVulkanFormat());
vab_index=pc->GetVABIndex(name,T::GetVulkanFormat());
void *map_ptr=(T *)(pc->MapVAB(vab_index)); if(buf_map)
map_ptr=(T *)(buf_map->Map());
if(map_ptr)
{
vb=T::Create(pc->GetVertexCount(),map_ptr);
vb->Begin();
}
else else
{ map_ptr=nullptr;
vb=nullptr;
}
} }
~VABMap() ~VABMap()
{ {
if(pc&&vab_index>=0) if(map_ptr)
pc->UnmapVAB(vab_index); buf_map->Unmap();
} }
void Restart() const bool IsValid()const{ return buf_map?buf_map->IsValid():false; }
{
if(vb)
vb->Begin();
}
const bool IsValid()const{ return vb; } operator T *(){ return map_ptr; }
T *operator->(){ return map_ptr; }
T *operator->(){ return vb; }
};//template<typename T> class VABMap };//template<typename T> class VABMap
typedef VABMap<VB1i8> VABMap1i8 ,VABMap1b; typedef VABMap<VB1i8> VABMap1i8 ,VABMap1b;
@ -197,38 +188,39 @@ typedef VABMap<VB4f> VABMap4f;
typedef VABMap<VB4d> VABMap4d; typedef VABMap<VB4d> VABMap4d;
/** /**
* 访 * 访
*/ */
template<typename T> class IBMap template<typename T> class IBMap
{ {
PrimitiveCreater *pc; VKBufferMap *buf_map;
T *map_ptr; T *map_ptr;
public: public:
IBMap(PrimitiveCreater *c) IBMap(PrimitiveCreater *pc)
{ {
pc=c; buf_map=pc->MapIBO();
if(pc) if(buf_map)
map_ptr=(T *)(pc->MapIBO()); map_ptr=(T *)(buf_map->Map());
else else
map_ptr=nullptr; map_ptr=nullptr;
} }
~IBMap() ~IBMap()
{ {
if(map_ptr&&pc) if(map_ptr)
pc->UnmapIBO(); buf_map->Unmap();
} }
const bool IsValid()const{ return map_ptr; } const bool IsValid()const{ return map_ptr; }
operator T *(){ return map_ptr; } operator T *(){ return map_ptr; }
T *operator->(){ return map_ptr; }
};//template<typename T> class IBMap };//template<typename T> class IBMap
using IBMapU8=IBMap<uint8>; using IBMapU8 =IBMap<uint8>;
using IBMapU16=IBMap<uint16>; using IBMapU16=IBMap<uint16>;
using IBMapU32=IBMap<uint32>; using IBMapU32=IBMap<uint32>;
VK_NAMESPACE_END VK_NAMESPACE_END

View File

@ -16,10 +16,14 @@ protected:
public: public:
VKBufferMap(DeviceBuffer *buf_ptr,VkDeviceSize off,VkDeviceSize s); VKBufferMap();
virtual ~VKBufferMap(); VKBufferMap(DeviceBuffer *buf,VkDeviceSize off,VkDeviceSize s);
~VKBufferMap();
void Set(DeviceBuffer *buf_ptr,VkDeviceSize off,VkDeviceSize s);
const bool IsValid()const{ return buffer; } const bool IsValid()const{ return buffer; }
void Clear();
void *Map(); void *Map();
void Unmap(); void Unmap();

View File

@ -470,10 +470,10 @@ namespace hgl
if(!pc->Init("Sphere",numberVertices,numberIndices)) if(!pc->Init("Sphere",numberVertices,numberIndices))
return(nullptr); return(nullptr);
VABRawMapFloat vertex (pc,VF_V3F,VAN::Position); VABMapFloat vertex (pc,VF_V3F,VAN::Position);
VABRawMapFloat normal (pc,VF_V3F,VAN::Normal); VABMapFloat normal (pc,VF_V3F,VAN::Normal);
VABRawMapFloat tangent (pc,VF_V3F,VAN::Tangent); VABMapFloat tangent (pc,VF_V3F,VAN::Tangent);
VABRawMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord); VABMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord);
float *vp=vertex; float *vp=vertex;
float *np=normal; float *np=normal;
@ -559,10 +559,10 @@ namespace hgl
if(!pc->Init("Dome",numberVertices,numberIndices)) if(!pc->Init("Dome",numberVertices,numberIndices))
return(nullptr); return(nullptr);
VABRawMapFloat vertex (pc,VF_V3F,VAN::Position); VABMapFloat vertex (pc,VF_V3F,VAN::Position);
VABRawMapFloat normal (pc,VF_V3F,VAN::Normal); VABMapFloat normal (pc,VF_V3F,VAN::Normal);
VABRawMapFloat tangent (pc,VF_V3F,VAN::Tangent); VABMapFloat tangent (pc,VF_V3F,VAN::Tangent);
VABRawMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord); VABMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord);
float *vp=vertex; float *vp=vertex;
float *np=normal; float *np=normal;
@ -707,10 +707,10 @@ namespace hgl
if(!pc->Init("Torus",numberVertices,numberIndices)) if(!pc->Init("Torus",numberVertices,numberIndices))
return(nullptr); return(nullptr);
VABRawMapFloat vertex (pc,VF_V3F,VAN::Position); VABMapFloat vertex (pc,VF_V3F,VAN::Position);
VABRawMapFloat normal (pc,VF_V3F,VAN::Normal); VABMapFloat normal (pc,VF_V3F,VAN::Normal);
VABRawMapFloat tangent (pc,VF_V3F,VAN::Tangent); VABMapFloat tangent (pc,VF_V3F,VAN::Tangent);
VABRawMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord); VABMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord);
float *vp=vertex; float *vp=vertex;
float *np=normal; float *np=normal;
@ -849,10 +849,10 @@ namespace hgl
if (cci->numberSlices < 3 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES) if (cci->numberSlices < 3 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES)
return nullptr; return nullptr;
VABRawMapFloat vertex (pc,VF_V3F,VAN::Position); VABMapFloat vertex (pc,VF_V3F,VAN::Position);
VABRawMapFloat normal (pc,VF_V3F,VAN::Normal); VABMapFloat normal (pc,VF_V3F,VAN::Normal);
VABRawMapFloat tangent (pc,VF_V3F,VAN::Tangent); VABMapFloat tangent (pc,VF_V3F,VAN::Tangent);
VABRawMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord); VABMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord);
float *vp=vertex; float *vp=vertex;
float *np=normal; float *np=normal;
@ -1079,10 +1079,10 @@ namespace hgl
if (cci->numberSlices < 3 || cci->numberStacks < 1 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES) if (cci->numberSlices < 3 || cci->numberStacks < 1 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES)
return nullptr; return nullptr;
VABRawMapFloat vertex (pc,VF_V3F,VAN::Position); VABMapFloat vertex (pc,VF_V3F,VAN::Position);
VABRawMapFloat normal (pc,VF_V3F,VAN::Normal); VABMapFloat normal (pc,VF_V3F,VAN::Normal);
VABRawMapFloat tangent (pc,VF_V3F,VAN::Tangent); VABMapFloat tangent (pc,VF_V3F,VAN::Tangent);
VABRawMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord); VABMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord);
float *vp=vertex; float *vp=vertex;
float *np=normal; float *np=normal;

View File

@ -15,7 +15,7 @@ PrimitiveCreater::PrimitiveCreater(GPUDevice *dev,const VIL *v)
prim_data =nullptr; prim_data =nullptr;
map_ptr_list =hgl_zero_new<void_pointer>(v->GetVertexAttribCount()); vab_map_list =new VKBufferMap[v->GetVertexAttribCount()];
Clear(); Clear();
} }
@ -30,8 +30,8 @@ PrimitiveCreater::PrimitiveCreater(VertexDataManager *_vdm)
PrimitiveCreater::~PrimitiveCreater() PrimitiveCreater::~PrimitiveCreater()
{ {
delete[] vab_map_list;
SAFE_CLEAR(prim_data); SAFE_CLEAR(prim_data);
SAFE_CLEAR_ARRAY(map_ptr_list)
} }
void PrimitiveCreater::Clear() void PrimitiveCreater::Clear()
@ -94,6 +94,8 @@ bool PrimitiveCreater::Init(const AnsiString &pname,const uint32_t vertex_count,
delete prim_data; delete prim_data;
return(false); return(false);
} }
ibo_map.Set(ibo,prim_data->GetFirstIndex(),index_number);
#ifdef _DEBUG #ifdef _DEBUG
if(!vdm) if(!vdm)
@ -120,11 +122,21 @@ const int PrimitiveCreater::GetVABIndex(const AnsiString &name,const VkFormat &a
const int vab_index=prim_data->GetVABIndex(name); const int vab_index=prim_data->GetVABIndex(name);
if(vab_index<0)
return(-1);
VAB *vab=prim_data->GetVAB(vab_index); VAB *vab=prim_data->GetVAB(vab_index);
if(!vab) if(!vab)
{
vab=prim_data->InitVAB(vab_index,acquire_format,nullptr); vab=prim_data->InitVAB(vab_index,acquire_format,nullptr);
if(vab)
vab_map_list[vab_index].Set(vab,
prim_data->GetVertexOffset(),
vertices_number);
}
if(!vab) if(!vab)
return(-1); return(-1);
@ -144,31 +156,13 @@ const int PrimitiveCreater::GetVABIndex(const AnsiString &name,const VkFormat &a
return(vab_index); return(vab_index);
} }
void *PrimitiveCreater::MapVAB(const int vab_index) VKBufferMap *PrimitiveCreater::MapVAB(const AnsiString &name,const VkFormat &format)
{ {
if(!prim_data) const int vab_index=GetVABIndex(name,format);
return(nullptr);
VAB *vab=prim_data->GetVAB(vab_index); if(vab_index<0)return nullptr;
if(!vab) return vab_map_list+vab_index;
return(nullptr);
map_ptr_list[vab_index]=vab->Map(prim_data->GetVertexOffset(),vertices_number);
return map_ptr_list[vab_index];
}
void PrimitiveCreater::UnmapVAB(const int vab_index)
{
if(!prim_data)return;
VAB *vab=prim_data->GetVAB(vab_index);
if(!vab)return;
vab->Unmap();
map_ptr_list[vab_index]=nullptr;
} }
bool PrimitiveCreater::WriteVAB(const AnsiString &name,const VkFormat &format, const void *data) bool PrimitiveCreater::WriteVAB(const AnsiString &name,const VkFormat &format, const void *data)
@ -185,18 +179,12 @@ bool PrimitiveCreater::WriteVAB(const AnsiString &name,const VkFormat &format, c
return vab->Write(data,prim_data->GetVertexOffset(),vertices_number); return vab->Write(data,prim_data->GetVertexOffset(),vertices_number);
} }
void *PrimitiveCreater::MapIBO() VKBufferMap *PrimitiveCreater::MapIBO()
{ {
if(!prim_data)return(nullptr); if(!ibo)
if(!ibo)return(nullptr); return(nullptr);
return ibo->Map(prim_data->GetFirstIndex(),index_number); return &ibo_map;
}
void PrimitiveCreater::UnmapIBO()
{
if(ibo)
ibo->Unmap();
} }
bool PrimitiveCreater::WriteIBO(const void *data,const uint32_t count) bool PrimitiveCreater::WriteIBO(const void *data,const uint32_t count)
@ -218,11 +206,9 @@ Primitive *PrimitiveCreater::Create()
return(nullptr); return(nullptr);
for(int i=0;i<vil->GetVertexAttribCount();i++) for(int i=0;i<vil->GetVertexAttribCount();i++)
if(map_ptr_list[i]) vab_map_list[i].Clear();
{
prim_data->GetVAB(i)->Unmap(); ibo_map.Clear();
map_ptr_list[i]=nullptr;
}
Primitive *primitive=new Primitive(prim_name,prim_data); Primitive *primitive=new Primitive(prim_name,prim_data);

View File

@ -1,10 +1,11 @@
#include<hgl/graph/VKBufferMap.h> #include<hgl/graph/VKBufferMap.h>
#include<hgl/graph/VKVertexAttribBuffer.h> #include<hgl/graph/VKVertexAttribBuffer.h>
#include<iostream>
VK_NAMESPACE_BEGIN VK_NAMESPACE_BEGIN
VKBufferMap::VKBufferMap(DeviceBuffer *buf,VkDeviceSize off,VkDeviceSize s) void VKBufferMap::Set(DeviceBuffer *buf,VkDeviceSize off,VkDeviceSize s)
{ {
buffer=buf; buffer=buf;
offset=off; offset=off;
size=s; size=s;
@ -12,10 +13,35 @@ VKBufferMap::VKBufferMap(DeviceBuffer *buf,VkDeviceSize off,VkDeviceSize s)
map_ptr=nullptr; map_ptr=nullptr;
} }
void VKBufferMap::Clear()
{
if(buffer&&map_ptr)
buffer->Unmap();
buffer=nullptr;
offset=0;
size=0;
map_ptr=nullptr;
}
VKBufferMap::VKBufferMap()
{
Set(nullptr,0,0);
std::cout<<"VKBufferMap Create"<<std::endl;
}
VKBufferMap::VKBufferMap(DeviceBuffer *buf,VkDeviceSize off,VkDeviceSize s)
{
Set(buf,off,s);
}
VKBufferMap::~VKBufferMap() VKBufferMap::~VKBufferMap()
{ {
if(map_ptr) if(buffer&&map_ptr)
buffer->DeviceBuffer::Unmap(); buffer->Unmap();
std::cout<<"VKBufferMap Destory"<<std::endl;
} }
void *VKBufferMap::Map() void *VKBufferMap::Map()
@ -26,18 +52,18 @@ void *VKBufferMap::Map()
if(!buffer) if(!buffer)
return(nullptr); return(nullptr);
map_ptr=buffer->DeviceBuffer::Map(offset,size); map_ptr=buffer->Map(offset,size);
return map_ptr; return map_ptr;
} }
void VKBufferMap::Unmap() void VKBufferMap::Unmap()
{ {
if(map_ptr) if(buffer&&map_ptr)
{ {
buffer->DeviceBuffer::Unmap(); buffer->Unmap();
map_ptr=nullptr; map_ptr=nullptr;
} }
} }
VK_NAMESPACE_END VK_NAMESPACE_END