Layout codes.
This commit is contained in:
parent
40094a7529
commit
03491dbed8
@ -1,98 +1,92 @@
|
|||||||
#ifndef HGL_GRAPH_PRIMITIVE_CREATER_INCLUDE
|
#pragma once
|
||||||
#define HGL_GRAPH_PRIMITIVE_CREATER_INCLUDE
|
|
||||||
|
|
||||||
#include<hgl/graph/VKRenderResource.h>
|
#include<hgl/graph/VKRenderResource.h>
|
||||||
#include<hgl/graph/VertexAttribDataAccess.h>
|
#include<hgl/graph/VertexAttribDataAccess.h>
|
||||||
#include<hgl/graph/VKShaderModule.h>
|
#include<hgl/graph/VKShaderModule.h>
|
||||||
#include<hgl/graph/VKIndexBuffer.h>
|
#include<hgl/graph/VKIndexBuffer.h>
|
||||||
|
|
||||||
namespace hgl
|
VK_NAMESPACE_BEGIN
|
||||||
|
class VertexDataManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 可绘制图元创建器
|
||||||
|
*/
|
||||||
|
class PrimitiveCreater
|
||||||
{
|
{
|
||||||
namespace graph
|
protected:
|
||||||
{
|
|
||||||
class VertexDataManager;
|
|
||||||
|
|
||||||
/**
|
GPUDevice *device;
|
||||||
* 可绘制图元创建器
|
const GPUPhysicalDevice *phy_device;
|
||||||
*/
|
|
||||||
class PrimitiveCreater
|
|
||||||
{
|
|
||||||
protected:
|
|
||||||
|
|
||||||
GPUDevice *device;
|
VertexDataManager *vdm;
|
||||||
const GPUPhysicalDevice *phy_device;
|
RenderResource *db;
|
||||||
|
|
||||||
VertexDataManager *vdm;
|
const VIL *vil;
|
||||||
RenderResource *db;
|
|
||||||
|
|
||||||
const VIL *vil;
|
protected:
|
||||||
|
|
||||||
protected:
|
VkDeviceSize vertices_number;
|
||||||
|
VkDeviceSize index_number;
|
||||||
|
|
||||||
VkDeviceSize vertices_number;
|
IndexBuffer * ibo;
|
||||||
VkDeviceSize index_number;
|
void * ibo_map;
|
||||||
|
VABAccessMap vab_map;
|
||||||
|
|
||||||
IndexBuffer * ibo;
|
protected:
|
||||||
void * ibo_map;
|
|
||||||
VABAccessMap vab_map;
|
|
||||||
|
|
||||||
protected:
|
bool AcquirePVB(VABAccess *,const AnsiString &,const void *data); ///<请求一个顶点属性数据区
|
||||||
|
|
||||||
bool AcquirePVB(VABAccess *,const AnsiString &,const void *data); ///<请求一个顶点属性数据区
|
void ClearAllData();
|
||||||
|
|
||||||
void ClearAllData();
|
public:
|
||||||
|
|
||||||
public:
|
PrimitiveCreater(RenderResource *sdb,const VIL *);
|
||||||
|
PrimitiveCreater(VertexDataManager *);
|
||||||
|
virtual ~PrimitiveCreater();
|
||||||
|
|
||||||
PrimitiveCreater(RenderResource *sdb,const VIL *);
|
virtual bool Init(const uint32 vertices_count,const uint32 index_count,IndexType it=IndexType::AUTO); ///<初始化,参数为顶点数量
|
||||||
PrimitiveCreater(VertexDataManager *);
|
|
||||||
virtual ~PrimitiveCreater();
|
|
||||||
|
|
||||||
virtual bool Init(const uint32 vertices_count,const uint32 index_count,IndexType it=IndexType::AUTO); ///<初始化,参数为顶点数量
|
template<typename T>
|
||||||
|
T * AccessVAB(const AnsiString &name) ///<创建一个顶点属性数据缓冲区以及访问器
|
||||||
|
{
|
||||||
|
const VkFormat format=vil->GetVulkanFormat(name);
|
||||||
|
|
||||||
template<typename T>
|
if(format!=T::GetVulkanFormat())
|
||||||
T * AccessVAB(const AnsiString &name) ///<创建一个顶点属性数据缓冲区以及访问器
|
return(nullptr);
|
||||||
{
|
|
||||||
const VkFormat format=vil->GetVulkanFormat(name);
|
|
||||||
|
|
||||||
if(format!=T::GetVulkanFormat())
|
VABAccess vad;
|
||||||
return(nullptr);
|
if(!this->AcquirePVB(&vad,name,nullptr))
|
||||||
|
return(nullptr);
|
||||||
|
|
||||||
VABAccess vad;
|
T *access=T::Create(vertices_number,vad.map_ptr);
|
||||||
if(!this->AcquirePVB(&vad,name,nullptr))
|
|
||||||
return(nullptr);
|
|
||||||
|
|
||||||
T *access=T::Create(vertices_number,vad.map_ptr);
|
access->Begin();
|
||||||
|
|
||||||
access->Begin();
|
return access;
|
||||||
|
}
|
||||||
|
|
||||||
return access;
|
bool WriteVAB(const AnsiString &name,const void *data,const uint32_t bytes); ///<直接写入顶点属性数据
|
||||||
}
|
|
||||||
|
|
||||||
bool WriteVAB(const AnsiString &name,const void *data,const uint32_t bytes); ///<直接写入顶点属性数据
|
const IndexType GetIndexType()const{return ibo?ibo->GetType():IndexType::ERR;} ///<取得索引数据类型
|
||||||
|
|
||||||
const IndexType GetIndexType()const{return ibo?ibo->GetType():IndexType::ERR;} ///<取得索引数据类型
|
template<typename T> T *AccessIBO()
|
||||||
|
{
|
||||||
|
if(!ibo)return(nullptr);
|
||||||
|
if(ibo->GetStride()!=sizeof(T))return(nullptr);
|
||||||
|
|
||||||
template<typename T> T *AccessIBO()
|
return (T *)ibo_map;
|
||||||
{
|
}
|
||||||
if(!ibo)return(nullptr);
|
|
||||||
if(ibo->GetStride()!=sizeof(T))return(nullptr);
|
|
||||||
|
|
||||||
return (T *)ibo_map;
|
template<typename T> bool WriteIBO(const T *data)
|
||||||
}
|
{
|
||||||
|
if(!ibo)return(false);
|
||||||
|
if(ibo->GetStride()!=sizeof(T))return(false);
|
||||||
|
|
||||||
template<typename T> bool WriteIBO(const T *data)
|
hgl_cpy<T>((T *)ibo_map,data,index_number);
|
||||||
{
|
|
||||||
if(!ibo)return(false);
|
|
||||||
if(ibo->GetStride()!=sizeof(T))return(false);
|
|
||||||
|
|
||||||
hgl_cpy<T>((T *)ibo_map,data,index_number);
|
return(true);
|
||||||
|
}
|
||||||
|
|
||||||
return(true);
|
virtual Primitive * Finish(const AnsiString &); ///<结束并创建可渲染对象
|
||||||
}
|
};//class PrimitiveCreater
|
||||||
|
VK_NAMESPACE_END
|
||||||
virtual Primitive * Finish(const AnsiString &); ///<结束并创建可渲染对象
|
|
||||||
};//class PrimitiveCreater
|
|
||||||
}//namespace graph
|
|
||||||
}//namespace hgl
|
|
||||||
#endif//HGL_GRAPH_PRIMITIVE_CREATER_INCLUDE
|
|
@ -4,199 +4,195 @@
|
|||||||
#include<hgl/graph/VKPrimitive.h>
|
#include<hgl/graph/VKPrimitive.h>
|
||||||
#include<hgl/graph/VertexDataManager.h>
|
#include<hgl/graph/VertexDataManager.h>
|
||||||
|
|
||||||
namespace hgl
|
VK_NAMESPACE_BEGIN
|
||||||
|
PrimitiveCreater::PrimitiveCreater(RenderResource *sdb,const VIL *v)
|
||||||
{
|
{
|
||||||
namespace graph
|
device =sdb->GetDevice();
|
||||||
|
phy_device =device->GetPhysicalDevice();
|
||||||
|
|
||||||
|
vdm =nullptr;
|
||||||
|
db =sdb;
|
||||||
|
vil =v;
|
||||||
|
|
||||||
|
vertices_number =0;
|
||||||
|
index_number =0;
|
||||||
|
ibo =nullptr;
|
||||||
|
ibo_map =nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
PrimitiveCreater::PrimitiveCreater(VertexDataManager *_vdm)
|
||||||
|
{
|
||||||
|
device =_vdm->GetDevice();
|
||||||
|
phy_device =device->GetPhysicalDevice();
|
||||||
|
|
||||||
|
vdm =_vdm;
|
||||||
|
db =nullptr;
|
||||||
|
vil =vdm->GetVIL();
|
||||||
|
|
||||||
|
vertices_number =0;
|
||||||
|
index_number =0;
|
||||||
|
ibo =nullptr;
|
||||||
|
ibo_map =nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
PrimitiveCreater::~PrimitiveCreater()
|
||||||
|
{
|
||||||
|
if(ibo)
|
||||||
{
|
{
|
||||||
PrimitiveCreater::PrimitiveCreater(RenderResource *sdb,const VIL *v)
|
ibo->Unmap();
|
||||||
|
delete ibo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PrimitiveCreater::Init(const uint32 vertex_count,const uint32 index_count,IndexType it)
|
||||||
|
{
|
||||||
|
if(vertex_count<=0)return(false);
|
||||||
|
|
||||||
|
vertices_number=vertex_count;
|
||||||
|
|
||||||
|
if(index_count>0)
|
||||||
|
{
|
||||||
|
index_number=index_count;
|
||||||
|
|
||||||
|
if(vdm)
|
||||||
{
|
{
|
||||||
device =sdb->GetDevice();
|
|
||||||
phy_device =device->GetPhysicalDevice();
|
|
||||||
|
|
||||||
vdm =nullptr;
|
|
||||||
db =sdb;
|
|
||||||
vil =v;
|
|
||||||
|
|
||||||
vertices_number =0;
|
|
||||||
index_number =0;
|
|
||||||
ibo =nullptr;
|
|
||||||
ibo_map =nullptr;
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
PrimitiveCreater::PrimitiveCreater(VertexDataManager *_vdm)
|
|
||||||
{
|
{
|
||||||
device =_vdm->GetDevice();
|
if(it==IndexType::AUTO)
|
||||||
phy_device =device->GetPhysicalDevice();
|
|
||||||
|
|
||||||
vdm =_vdm;
|
|
||||||
db =nullptr;
|
|
||||||
vil =vdm->GetVIL();
|
|
||||||
|
|
||||||
vertices_number =0;
|
|
||||||
index_number =0;
|
|
||||||
ibo =nullptr;
|
|
||||||
ibo_map =nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
PrimitiveCreater::~PrimitiveCreater()
|
|
||||||
{
|
|
||||||
if(ibo)
|
|
||||||
{
|
{
|
||||||
ibo->Unmap();
|
it=device->ChooseIndexType(vertex_count);
|
||||||
delete ibo;
|
|
||||||
|
if(!IsIndexType(it))
|
||||||
|
return(false);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
bool PrimitiveCreater::Init(const uint32 vertex_count,const uint32 index_count,IndexType it)
|
|
||||||
{
|
|
||||||
if(vertex_count<=0)return(false);
|
|
||||||
|
|
||||||
vertices_number=vertex_count;
|
|
||||||
|
|
||||||
if(index_count>0)
|
|
||||||
{
|
|
||||||
index_number=index_count;
|
|
||||||
|
|
||||||
if(vdm)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if(it==IndexType::AUTO)
|
|
||||||
{
|
|
||||||
it=device->ChooseIndexType(vertex_count);
|
|
||||||
|
|
||||||
if(!IsIndexType(it))
|
|
||||||
return(false);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if(!device->CheckIndexType(it,vertex_count))
|
|
||||||
return(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
ibo=device->CreateIBO(it,index_count);
|
|
||||||
//ibo=db->CreateIBO(it,index_count);
|
|
||||||
if(!ibo)return(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
ibo_map=ibo->Map();
|
|
||||||
}
|
|
||||||
|
|
||||||
return(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool PrimitiveCreater::AcquirePVB(VABAccess *vad,const AnsiString &name,const void *data)
|
|
||||||
{
|
|
||||||
if(!vad)return(false);
|
|
||||||
if(!vil)return(false);
|
|
||||||
if(name.IsEmpty())return(false);
|
|
||||||
|
|
||||||
const VertexInputFormat *vif=vil->GetConfig(name);
|
|
||||||
|
|
||||||
if(!vif)
|
|
||||||
return(false);
|
|
||||||
|
|
||||||
if(vab_map.Get(name,*vad))
|
|
||||||
return true;
|
|
||||||
|
|
||||||
vad->vab =db->CreateVAB(vif->format,vertices_number,data);
|
|
||||||
|
|
||||||
if(!data)
|
|
||||||
vad->map_ptr=vad->vab->Map();
|
|
||||||
else
|
else
|
||||||
vad->map_ptr=nullptr;
|
{
|
||||||
|
if(!device->CheckIndexType(it,vertex_count))
|
||||||
|
return(false);
|
||||||
|
}
|
||||||
|
|
||||||
vab_map.Add(name,*vad);
|
ibo=device->CreateIBO(it,index_count);
|
||||||
|
//ibo=db->CreateIBO(it,index_count);
|
||||||
return true;
|
if(!ibo)return(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PrimitiveCreater::WriteVAB(const AnsiString &name,const void *data,const uint32_t bytes)
|
ibo_map=ibo->Map();
|
||||||
|
}
|
||||||
|
|
||||||
|
return(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PrimitiveCreater::AcquirePVB(VABAccess *vad,const AnsiString &name,const void *data)
|
||||||
|
{
|
||||||
|
if(!vad)return(false);
|
||||||
|
if(!vil)return(false);
|
||||||
|
if(name.IsEmpty())return(false);
|
||||||
|
|
||||||
|
const VertexInputFormat *vif=vil->GetConfig(name);
|
||||||
|
|
||||||
|
if(!vif)
|
||||||
|
return(false);
|
||||||
|
|
||||||
|
if(vab_map.Get(name,*vad))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
vad->vab =db->CreateVAB(vif->format,vertices_number,data);
|
||||||
|
|
||||||
|
if(!data)
|
||||||
|
vad->map_ptr=vad->vab->Map();
|
||||||
|
else
|
||||||
|
vad->map_ptr=nullptr;
|
||||||
|
|
||||||
|
vab_map.Add(name,*vad);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PrimitiveCreater::WriteVAB(const AnsiString &name,const void *data,const uint32_t bytes)
|
||||||
|
{
|
||||||
|
if(!vil)return(false);
|
||||||
|
if(name.IsEmpty())return(false);
|
||||||
|
if(!data)return(false);
|
||||||
|
if(!bytes)return(false);
|
||||||
|
|
||||||
|
const VertexInputFormat *vif=vil->GetConfig(name);
|
||||||
|
|
||||||
|
if(!vif)
|
||||||
|
return(false);
|
||||||
|
|
||||||
|
if(vif->stride*vertices_number!=bytes)
|
||||||
|
return(false);
|
||||||
|
|
||||||
|
VABAccess vad;
|
||||||
|
|
||||||
|
return AcquirePVB(&vad,name,data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrimitiveCreater::ClearAllData()
|
||||||
|
{
|
||||||
|
if(vab_map.GetCount()>0)
|
||||||
|
{
|
||||||
|
const auto *sp=vab_map.GetDataList();
|
||||||
|
for(int i=0;i<vab_map.GetCount();i++)
|
||||||
{
|
{
|
||||||
if(!vil)return(false);
|
if((*sp)->value.vab)
|
||||||
if(name.IsEmpty())return(false);
|
{
|
||||||
if(!data)return(false);
|
(*sp)->value.vab->Unmap();
|
||||||
if(!bytes)return(false);
|
delete (*sp)->value.vab;
|
||||||
|
}
|
||||||
|
|
||||||
const VertexInputFormat *vif=vil->GetConfig(name);
|
++sp;
|
||||||
|
|
||||||
if(!vif)
|
|
||||||
return(false);
|
|
||||||
|
|
||||||
if(vif->stride*vertices_number!=bytes)
|
|
||||||
return(false);
|
|
||||||
|
|
||||||
VABAccess vad;
|
|
||||||
|
|
||||||
return AcquirePVB(&vad,name,data);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void PrimitiveCreater::ClearAllData()
|
if(ibo)
|
||||||
|
{
|
||||||
|
ibo->Unmap();
|
||||||
|
delete ibo;
|
||||||
|
ibo=nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Primitive *PrimitiveCreater::Finish(const AnsiString &prim_name)
|
||||||
|
{
|
||||||
|
const uint si_count=vil->GetCount(VertexInputGroup::Basic);
|
||||||
|
|
||||||
|
if(vab_map.GetCount()!=si_count)
|
||||||
|
return(nullptr);
|
||||||
|
|
||||||
|
Primitive *primitive=db->CreatePrimitive(prim_name,vertices_number);
|
||||||
|
|
||||||
|
const auto *sp=vab_map.GetDataList();
|
||||||
|
for(uint i=0;i<si_count;i++)
|
||||||
|
{
|
||||||
|
if((*sp)->value.vab)
|
||||||
{
|
{
|
||||||
if(vab_map.GetCount()>0)
|
if((*sp)->value.map_ptr)
|
||||||
{
|
(*sp)->value.vab->Unmap();
|
||||||
const auto *sp=vab_map.GetDataList();
|
|
||||||
for(int i=0;i<vab_map.GetCount();i++)
|
|
||||||
{
|
|
||||||
if((*sp)->value.vab)
|
|
||||||
{
|
|
||||||
(*sp)->value.vab->Unmap();
|
|
||||||
delete (*sp)->value.vab;
|
|
||||||
}
|
|
||||||
|
|
||||||
++sp;
|
primitive->SetVAB((*sp)->key,(*sp)->value.vab);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(ibo)
|
|
||||||
{
|
|
||||||
ibo->Unmap();
|
|
||||||
delete ibo;
|
|
||||||
ibo=nullptr;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
Primitive *PrimitiveCreater::Finish(const AnsiString &prim_name)
|
|
||||||
{
|
{
|
||||||
const uint si_count=vil->GetCount(VertexInputGroup::Basic);
|
//ClearAllData();
|
||||||
|
return(nullptr);
|
||||||
if(vab_map.GetCount()!=si_count)
|
|
||||||
return(nullptr);
|
|
||||||
|
|
||||||
Primitive *primitive=db->CreatePrimitive(prim_name,vertices_number);
|
|
||||||
|
|
||||||
const auto *sp=vab_map.GetDataList();
|
|
||||||
for(uint i=0;i<si_count;i++)
|
|
||||||
{
|
|
||||||
if((*sp)->value.vab)
|
|
||||||
{
|
|
||||||
if((*sp)->value.map_ptr)
|
|
||||||
(*sp)->value.vab->Unmap();
|
|
||||||
|
|
||||||
primitive->SetVAB((*sp)->key,(*sp)->value.vab);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//ClearAllData();
|
|
||||||
return(nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
++sp;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(ibo)
|
|
||||||
{
|
|
||||||
ibo->Unmap();
|
|
||||||
primitive->SetIndex(ibo,0,index_number);
|
|
||||||
|
|
||||||
db->Add(ibo);
|
|
||||||
ibo=nullptr; //避免释构函数删除
|
|
||||||
}
|
|
||||||
|
|
||||||
db->Add(primitive);
|
|
||||||
|
|
||||||
return primitive;
|
|
||||||
}
|
}
|
||||||
}//namespace graph
|
|
||||||
}//namespace hgl
|
++sp;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(ibo)
|
||||||
|
{
|
||||||
|
ibo->Unmap();
|
||||||
|
primitive->SetIndex(ibo,0,index_number);
|
||||||
|
|
||||||
|
db->Add(ibo);
|
||||||
|
ibo=nullptr; //避免释构函数删除
|
||||||
|
}
|
||||||
|
|
||||||
|
db->Add(primitive);
|
||||||
|
|
||||||
|
return primitive;
|
||||||
|
}
|
||||||
|
VK_NAMESPACE_END
|
Loading…
x
Reference in New Issue
Block a user