first step OK that it's to resumed to use L2W in VertexInputStream.

This commit is contained in:
hyzboy 2024-03-26 01:20:20 +08:00
parent b48ef1ac30
commit a60ffbb7da
24 changed files with 171 additions and 122 deletions

2
CMCore

@ -1 +1 @@
Subproject commit 003bae2c2488ae32027db43e6573a5174794ae03
Subproject commit cf13064ceada8814ced7aac89eaac3dd2d681d45

@ -1 +1 @@
Subproject commit c302137467f4332263f9b9e142c0b7ba36d61e74
Subproject commit d395ee152f64b1eb7d058c355767f25c44ed8dcb

View File

@ -39,6 +39,10 @@ private:
DataArray<RenderItem> ri_array;
uint ri_count;
bool has_l2w;
bool has_mi;
VkDeviceSize l2w_buffer_size[4];
void StatMI();
void Stat();
@ -56,7 +60,7 @@ protected:
public:
MaterialRenderList(GPUDevice *d,Material *m);
MaterialRenderList(GPUDevice *d,bool l2w,Material *m);
~MaterialRenderList();
void Add(Renderable *ri,const Matrix4f &mat);

View File

@ -69,7 +69,6 @@ public:
}
const bool hasSet (const DescriptorSetType &type)const;
const bool hasAssign ()const;
const VIL * GetDefaultVIL()const;
VIL * CreateVIL(const VILConfig *format_map=nullptr);

View File

@ -35,6 +35,8 @@ public:
void Free() override {/* DON'T RUN ANY OPERATION.*/}
void Flush(const VkDeviceSize);
bool Write(const void *source,const uint64 offset,const uint64 size) override;
};//class VKMemoryAllocator:public AbstractMemoryAllocator
VK_NAMESPACE_END
#endif//HGL_GRAPH_VULKAN_MEMORY_ALLOCATOR_INCLUDE

View File

@ -45,15 +45,6 @@ constexpr const ShaderBufferSource SBS_CameraInfo=
// UBO必须严格指定数组的大小
// SSBO则不需要使用[]方式指定为动态大小数组
constexpr const ShaderBufferSource SBS_LocalToWorld=
{
"LocalToWorldData",
"l2w",
R"(
mat4 mats[L2W_MAX_COUNT];)"
};
constexpr const char MaterialInstanceStruct[]="MaterialInstance";
constexpr const ShaderBufferSource SBS_MaterialInstance=

View File

@ -34,8 +34,6 @@ namespace hgl{namespace graph
UBODescriptor *mi_ubo;
uint32_t l2w_shader_stage;
uint32_t l2w_max_count;
UBODescriptor *l2w_ubo;
ShaderCreateInfoMap shader_map; ///<着色器列表

View File

@ -84,8 +84,6 @@ public:
void SetMaterialInstance(UBODescriptor *,const AnsiString &);
void AddMaterialInstanceOutput();
void SetLocalToWorld(UBODescriptor *);
void SetMain(const AnsiString &str){main_function=str;}
void SetMain(const char *str,const int len)
{

View File

@ -21,8 +21,9 @@ namespace hgl
int hasInput(const char *);
void AddLocalToWorld();
void AddMaterialInstanceID();
void AddJoint();
void AddAssign();
};//class ShaderCreateInfoVertex:public ShaderCreateInfo
}//namespace graph
}//namespace hgl::graph

View File

@ -47,14 +47,17 @@ int Comparator<hgl::graph::RenderNode>::compare(const hgl::graph::RenderNode &ob
}
VK_NAMESPACE_BEGIN
MaterialRenderList::MaterialRenderList(GPUDevice *d,Material *m)
MaterialRenderList::MaterialRenderList(GPUDevice *d,bool l2w,Material *m)
{
device=d;
cmd_buf=nullptr;
material=m;
if(material->hasAssign())
assign_buffer=new RenderAssignBuffer(d,material->GetMIDataBytes());
has_l2w=l2w;
has_mi=material->HasMI();
if(has_l2w||has_mi)
assign_buffer=new RenderAssignBuffer(d,has_l2w,material->GetMIDataBytes());
else
assign_buffer=nullptr;
@ -90,7 +93,7 @@ void MaterialRenderList::End()
if(assign_buffer)
{
if(material->HasMI())
if(has_mi)
StatMI();
//写入LocalToWorld数据
@ -179,6 +182,17 @@ bool MaterialRenderList::Bind(const VertexInputData *vid,const uint ri_index)
vbo_list->Add(vid->buffer_list,vid->buffer_offset,vid->binding_count);
}
if(has_l2w)//LocalToWorld组由RenderList合成
{
for(uint i=0;i<4;i++)
l2w_buffer_size[i]=ri_index*16; //mat4每列都是rgba32f自然是16字节
vbo_list->Add(assign_buffer->GetLocalToWorldVBO(),l2w_buffer_size,4);
}
if(has_mi) //材质实例组
vbo_list->Add(assign_buffer->GetMIVBO(),MI_VBO_STRIDE_BYTES*ri_index);
//if(!vbo_list.IsFull()) //Joint组暂未支持
//{
// const uint joint_id_binding_count=vil->GetCount(VertexInputGroup::JointID);
@ -207,12 +221,6 @@ bool MaterialRenderList::Bind(const VertexInputData *vid,const uint ri_index)
// }
//}
//if(!vbo_list->IsFull()) //Assign组
{
if(assign_buffer)
vbo_list->Add(assign_buffer->GetAssignVBO(),ASSIGN_VBO_STRIDE_BYTES*ri_index);
}
//if(count!=binding_count)
//{
// //还有没支持的绑定组????

View File

@ -8,44 +8,60 @@
#include<hgl/graph/mtl/UBOCommon.h>
VK_NAMESPACE_BEGIN
RenderAssignBuffer::RenderAssignBuffer(GPUDevice *dev,const uint mi_bytes)
RenderAssignBuffer::RenderAssignBuffer(GPUDevice *dev,const bool has_l2w,const uint mi_bytes)
{
hgl_zero(*this);
device=dev;
mi_data_bytes=mi_bytes;
ubo_mi=nullptr;
mi_count=0;
}
VkBuffer RenderAssignBuffer::GetAssignVBO()const
{
return vbo_assigns->GetBuffer();
}
void RenderAssignBuffer::Bind(Material *mtl)const
{
if(!mtl)return;
if(!mtl->hasAssign())
if(!mtl->HasMI())
return;
mtl->BindUBO(DescriptorSetType::PerFrame,mtl::SBS_LocalToWorld.name,ubo_l2w);
mtl->BindUBO(DescriptorSetType::PerMaterial,mtl::SBS_MaterialInstance.name,ubo_mi);
}
void RenderAssignBuffer::Clear()
{
SAFE_CLEAR(ubo_l2w);
SAFE_CLEAR(ubo_mi);
SAFE_CLEAR(vbo_assigns);
SAFE_CLEAR(vbo_mi);
SAFE_CLEAR(l2w_vbo[0])
SAFE_CLEAR(l2w_vbo[1])
SAFE_CLEAR(l2w_vbo[2])
SAFE_CLEAR(l2w_vbo[3])
node_count=0;
mi_count=0;
}
#ifdef _DEBUG
namespace
{
constexpr const char *l2w_buffer_name[]=
{
"UBO:Buffer:LocalToWorld:0",
"UBO:Buffer:LocalToWorld:1",
"UBO:Buffer:LocalToWorld:2",
"UBO:Buffer:LocalToWorld:3"
};
constexpr const char *l2w_memory_name[]=
{
"UBO:Memory:LocalToWorld:0",
"UBO:Memory:LocalToWorld:1",
"UBO:Memory:LocalToWorld:2",
"UBO:Memory:LocalToWorld:3"
};
}
#endif//_DEBUG
void RenderAssignBuffer::Alloc(const uint nc,const uint mc)
{
Clear();
@ -53,7 +69,11 @@ void RenderAssignBuffer::Alloc(const uint nc,const uint mc)
{
node_count=nc;
ubo_l2w=device->CreateUBO(node_count*sizeof(Matrix4f));
for(uint i=0;i<4;i++)
{
l2w_vbo[i]=device->CreateVBO(VF_V4F,node_count);
l2w_buffer[i]=l2w_vbo[i]->GetBuffer();
}
}
if(mi_data_bytes>0&&mc>0)
@ -63,15 +83,22 @@ void RenderAssignBuffer::Alloc(const uint nc,const uint mc)
ubo_mi=device->CreateUBO(mi_data_bytes*mi_count);
}
vbo_assigns=device->CreateVBO(ASSIGN_VBO_FMT,node_count);
vbo_mi=device->CreateVBO(MI_VBO_FMT,node_count);
mi_buffer=vbo_mi->GetBuffer();
#ifdef _DEBUG
DebugUtils *du=device->GetDebugUtils();
if(du)
{
du->SetBuffer(ubo_l2w->GetBuffer(),"UBO:Buffer:LocalToWorld");
du->SetDeviceMemory(ubo_l2w->GetVkMemory(),"UBO:Memory:LocalToWorld");
if(l2w_buffer[0])
{
for(int i=0;i<4;i++)
{
du->SetBuffer(l2w_buffer[i],l2w_buffer_name[i]);
du->SetDeviceMemory(l2w_vbo[i]->GetVkMemory(),l2w_memory_name[i]);
}
}
if(ubo_mi)
{
@ -79,8 +106,11 @@ void RenderAssignBuffer::Alloc(const uint nc,const uint mc)
du->SetDeviceMemory(ubo_mi->GetVkMemory(),"UBO:Memory:MaterialInstance");
}
du->SetBuffer(vbo_assigns->GetBuffer(),"VBO:Buffer:Assign");
du->SetDeviceMemory(vbo_assigns->GetVkMemory(),"VBO:Memory:Assign");
if(vbo_mi)
{
du->SetBuffer(vbo_mi->GetBuffer(),"VBO:Buffer:MaterialInstanceID");
du->SetDeviceMemory(vbo_mi->GetVkMemory(),"VBO:Memory:MaterialInstanceID");
}
}
#endif//_DEBUG
}
@ -91,6 +121,27 @@ void RenderAssignBuffer::WriteNode(RenderNode *render_node,const uint count,cons
Alloc(count,mi_set.GetCount());
if(l2w_buffer[0])
{
glm::vec4 *tp;
for(uint col=0;col<4;col++)
{
tp=(glm::vec4 *)(l2w_vbo[col]->Map());
rn=render_node;
for(uint i=0;i<count;i++)
{
*tp=rn->local_to_world[col];
++tp;
++rn;
}
l2w_vbo[col]->Unmap();
}
}
if(ubo_mi)
{
uint8 *mip=(uint8 *)(ubo_mi->Map());
@ -102,33 +153,22 @@ void RenderAssignBuffer::WriteNode(RenderNode *render_node,const uint count,cons
}
ubo_mi->Unmap();
}
uint16 *idp=(uint16 *)(vbo_assigns->Map());
uint16 *idp=(uint16 *)(vbo_mi->Map());
{
Matrix4f *tp=(hgl::Matrix4f *)(ubo_l2w->Map());
Matrix4f *l2w=tp;
rn=render_node;
for(uint i=0;i<count;i++)
{
*l2w=rn->local_to_world;
++l2w;
rn=render_node;
*idp=i;
++idp;
for(uint i=0;i<count;i++)
{
*idp=mi_set.Find(rn->ri->GetMaterialInstance());
++idp;
*idp=mi_set.Find(rn->ri->GetMaterialInstance());
++idp;
++rn;
++rn;
}
}
ubo_l2w->Unmap();
vbo_mi->Unmap();
}
vbo_assigns->Unmap();
}
VK_NAMESPACE_END

View File

@ -35,14 +35,16 @@ private:
GPUDevice *device;
uint node_count; ///<渲染节点数量
DeviceBuffer *ubo_l2w; ///<Local2World数据
VBO *l2w_vbo[4];
VkBuffer l2w_buffer[4];
uint32_t mi_data_bytes; ///<材质实例数据字节数
uint32_t mi_count; ///<材质实例数量
DeviceBuffer *ubo_mi; ///<材质实例数据
//Assign VBO
VBO *vbo_assigns; ///<RG16UI格式的VertexInputStream,,,,X:L2W ID,Y:MI ID
VBO *vbo_mi; ///<材质实例ID(R16UI格式)
VkBuffer mi_buffer;
private:
@ -52,13 +54,14 @@ private:
public:
VkBuffer GetAssignVBO()const;
const VkBuffer *GetLocalToWorldVBO()const{return l2w_buffer;}
const VkBuffer GetMIVBO ()const{return mi_buffer;}
void Bind(Material *)const;
public:
RenderAssignBuffer(GPUDevice *dev,const uint32_t mi_bytes);
RenderAssignBuffer(GPUDevice *dev,const bool has_l2w,const uint32_t mi_bytes);
~RenderAssignBuffer(){Clear();}
void WriteNode(RenderNode *render_node,const uint count,const MaterialInstanceSets &mi_set);

View File

@ -35,7 +35,7 @@ namespace hgl
if(!mrl_map.Get(mtl,mrl))
{
mrl=new MaterialRenderList(device,mtl);
mrl=new MaterialRenderList(device,true,mtl);
mrl_map.Add(mtl,mrl);
}

View File

@ -47,11 +47,6 @@ const bool Material::hasSet(const DescriptorSetType &dst)const
return desc_manager->hasSet(dst);
}
const bool Material::hasAssign()const
{
return vertex_input->hasAssign();
}
const VIL *Material::GetDefaultVIL()const
{
return vertex_input->GetDefaultVIL();

View File

@ -45,4 +45,12 @@ void VKMemoryAllocator::Flush(const VkDeviceSize size)
{
gpu_buffer->Flush(size);
}
bool VKMemoryAllocator::Write(const void *source,const uint64 offset,const uint64 size)
{
if(!source||size==0)
return(false);
return gpu_buffer->Write(source,offset,size);
}
VK_NAMESPACE_END

View File

@ -39,6 +39,7 @@ void main()
if(!Std2DMaterial::CustomVertexShader(vsc))
return(false);
vsc->AddMaterialInstanceID();
vsc->SetMain(vs_main);
return(true);
}

View File

@ -16,10 +16,9 @@ bool Std2DMaterial::CustomVertexShader(ShaderCreateInfoVertex *vsc)
if(cfg->local_to_world)
{
vsc->AddLocalToWorld();
mci->SetLocalToWorld(VK_SHADER_STAGE_ALL_GRAPHICS);
vsc->AddAssign();
if(is_rect)
vsc->AddFunction(func::GetPosition2DRectL2W[size_t(cfg->coordinate_system)]);
else

View File

@ -76,6 +76,9 @@ namespace
for(auto &ua:mfd->vi)
vsc->AddInput(ua.vat,ua.name);
if(mfd->mi.mi_bytes>0)
vsc->AddMaterialInstanceID();
if(!Std2DMaterial::CustomVertexShader(vsc))
return(false);

View File

@ -24,7 +24,7 @@ bool Std3DMaterial::CustomVertexShader(ShaderCreateInfoVertex *vsc)
{
mci->SetLocalToWorld(VK_SHADER_STAGE_ALL_GRAPHICS);
vsc->AddAssign();
vsc->AddLocalToWorld();
vsc->AddFunction(cfg->camera?func::GetPosition3DL2WCamera:func::GetPosition3DL2W);
}
else

View File

@ -76,6 +76,9 @@ public:
for (auto& ua : mfd->vi)
vsc->AddInput(ua.vat, ua.name);
if(mfd->mi.mi_bytes>0)
vsc->AddMaterialInstanceID();
if (!Std3DMaterial::CustomVertexShader(vsc))
return (false);

View File

@ -26,13 +26,6 @@ MaterialCreateInfo::MaterialCreateInfo(const MaterialCreateConfig *mc)
mi_max_count=0;
mi_ubo=nullptr;
}
{
l2w_max_count=hgl_min<uint32_t>(ubo_range/sizeof(Matrix4f),HGL_U16_MAX);
l2w_shader_stage=0;
l2w_ubo=nullptr;
}
}
bool MaterialCreateInfo::AddStruct(const AnsiString &struct_name,const AnsiString &codes)
@ -205,27 +198,6 @@ bool MaterialCreateInfo::SetLocalToWorld(const uint32_t shader_stage_flag_bits)
{
if(shader_stage_flag_bits==0)return(false);
mdi.AddStruct(SBS_LocalToWorld);
l2w_ubo=CreateUBODescriptor(SBS_LocalToWorld,shader_stage_flag_bits);
mdi.AddUBO(shader_stage_flag_bits,DescriptorSetType::PerFrame,l2w_ubo);
const AnsiString L2W_MAX_COUNT=AnsiString::numberOf(l2w_max_count);
auto *it=shader_map.GetDataList();
for(int i=0;i<shader_map.GetCount();i++)
{
if((*it)->key&shader_stage_flag_bits)
{
(*it)->value->AddDefine("L2W_MAX_COUNT",L2W_MAX_COUNT);
(*it)->value->SetLocalToWorld(l2w_ubo);
}
++it;
}
l2w_shader_stage=shader_stage_flag_bits;
return(true);

View File

@ -156,10 +156,10 @@ bool ShaderCreateInfo::ProcSubpassInput()
namespace
{
constexpr const char MF_GetMI_VS []="\nMaterialInstance GetMI(){return mtl.mi[Assign.y];}\n";
constexpr const char MF_GetMI_VS []="\nMaterialInstance GetMI(){return mtl.mi[MaterialInstanceID];}\n";
constexpr const char MF_GetMI_Other []="\nMaterialInstance GetMI(){return mtl.mi[Input.MaterialInstanceID];}\n";
constexpr const char MF_HandoverMI_VS[]= "\nvoid HandoverMI(){Output.MaterialInstanceID=Assign.y;}\n";
constexpr const char MF_HandoverMI_VS[]= "\nvoid HandoverMI(){Output.MaterialInstanceID=MaterialInstanceID;}\n";
constexpr const char MF_HandoverMI_GS[]= "\nvoid HandoverMI(){Output.MaterialInstanceID=Input[0].MaterialInstanceID;}\n";
constexpr const char MF_HandoverMI_OTHER[]= "\nvoid HandoverMI(){Output.MaterialInstanceID=Input.MaterialInstanceID;}\n";
}//namespace
@ -183,11 +183,6 @@ void ShaderCreateInfo::AddMaterialInstanceOutput()
AddFunction(MF_HandoverMI_OTHER);
}
void ShaderCreateInfo::SetLocalToWorld(UBODescriptor *ubo)
{
sdm->AddUBO(DescriptorSetType::PerFrame,ubo);
}
bool ShaderCreateInfo::ProcInput(ShaderCreateInfo *last_sc)
{
if(!last_sc)

View File

@ -50,19 +50,39 @@ void ShaderCreateInfoVertex::AddJoint()
namespace
{
constexpr const char MF_GetLocalToWorld[]="\nmat4 GetLocalToWorld(){return l2w.mats[Assign.x];}\n";
constexpr const char MF_GetLocalToWorld[]=R"(
mat4 GetLocalToWorld()
{
return mat4(LocalToWorld_0,
LocalToWorld_1,
LocalToWorld_2,
LocalToWorld_3);
}
)";
}
void ShaderCreateInfoVertex::AddAssign()
void ShaderCreateInfoVertex::AddLocalToWorld()
{
AddInput( ASSIGN_VAT_FMT,
ASSIGN_VIS_NAME,
VK_VERTEX_INPUT_RATE_INSTANCE,
VertexInputGroup::Assign);
char name[]= "LocalToWorld_?";
for(uint i=0;i<4;i++)
{
name[sizeof(name)-2]='0'+i;
AddInput(VAT_VEC4,name,VK_VERTEX_INPUT_RATE_INSTANCE,VertexInputGroup::LocalToWorld);
}
AddFunction(MF_GetLocalToWorld);
}
void ShaderCreateInfoVertex::AddMaterialInstanceID()
{
AddInput( MI_VAT_FMT,
MI_VIS_NAME,
VK_VERTEX_INPUT_RATE_INSTANCE,
VertexInputGroup::MaterialInstanceID);
}
bool ShaderCreateInfoVertex::ProcInput(ShaderCreateInfo *)
{
const auto &input=sdm->GetShaderStageIO().input;

View File

@ -9,5 +9,14 @@ namespace func
constexpr const char MaterialInstanceID[]="MaterialInstanceID";
constexpr const char GetLocalToWorld[]=R"(
mat4 GetLocalToWorld()
{
return mat4(LocalToWorld_0,
LocalToWorld_1,
LocalToWorld_2,
LocalToWorld_3);
}
)";
}//namespace func
STD_MTL_NAMESPACE_END