2023-06-01 15:08:45 +08:00
|
|
|
|
#include<hgl/graph/MaterialRenderList.h>
|
|
|
|
|
#include<hgl/graph/VKRenderable.h>
|
|
|
|
|
#include<hgl/graph/VKDevice.h>
|
|
|
|
|
#include<hgl/graph/VKCommandBuffer.h>
|
|
|
|
|
#include<hgl/graph/VKVertexInput.h>
|
2023-09-06 16:55:04 +08:00
|
|
|
|
#include<hgl/graph/VKRenderAssign.h>
|
2023-06-01 15:08:45 +08:00
|
|
|
|
#include<hgl/util/sort/Sort.h>
|
2023-09-05 20:48:47 +08:00
|
|
|
|
#include"RenderAssignBuffer.h"
|
2024-05-28 23:49:28 +08:00
|
|
|
|
#include<hgl/graph/VertexDataManager.h>
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* 理论上讲,我们需要按以下顺序排序
|
|
|
|
|
*
|
|
|
|
|
* for(material)
|
|
|
|
|
* for(pipeline)
|
|
|
|
|
* for(material_instance)
|
2024-04-24 01:38:55 +08:00
|
|
|
|
* for(vab)
|
2023-06-01 15:08:45 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
int Comparator<hgl::graph::RenderNode>::compare(const hgl::graph::RenderNode &obj_one,const hgl::graph::RenderNode &obj_two) const
|
|
|
|
|
{
|
2024-05-28 23:49:28 +08:00
|
|
|
|
hgl::int64 off;
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
|
|
|
|
hgl::graph::Renderable *ri_one=obj_one.ri;
|
|
|
|
|
hgl::graph::Renderable *ri_two=obj_two.ri;
|
|
|
|
|
|
|
|
|
|
//比较管线
|
|
|
|
|
{
|
|
|
|
|
off=ri_one->GetPipeline()
|
|
|
|
|
-ri_two->GetPipeline();
|
|
|
|
|
|
|
|
|
|
if(off)
|
|
|
|
|
return off;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-28 23:49:28 +08:00
|
|
|
|
auto *prim_one=ri_one->GetPrimitive();
|
|
|
|
|
auto *prim_two=ri_two->GetPrimitive();
|
|
|
|
|
|
|
|
|
|
//比如VDM
|
|
|
|
|
{
|
|
|
|
|
off=prim_one->GetVDM()
|
|
|
|
|
-prim_two->GetVDM();
|
|
|
|
|
|
|
|
|
|
if(off)
|
|
|
|
|
return off;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-01 15:08:45 +08:00
|
|
|
|
//比较模型
|
|
|
|
|
{
|
2024-05-28 23:49:28 +08:00
|
|
|
|
off=prim_one
|
|
|
|
|
-prim_two;
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
|
|
|
|
if(off)
|
2024-05-28 23:49:28 +08:00
|
|
|
|
{
|
|
|
|
|
off=prim_one->GetVertexOffset()-prim_two->GetVertexOffset(); //保存vertex offset小的在前面
|
|
|
|
|
|
2023-06-01 15:08:45 +08:00
|
|
|
|
return off;
|
2024-05-28 23:49:28 +08:00
|
|
|
|
}
|
2023-06-01 15:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VK_NAMESPACE_BEGIN
|
2024-03-26 01:20:20 +08:00
|
|
|
|
MaterialRenderList::MaterialRenderList(GPUDevice *d,bool l2w,Material *m)
|
2023-06-01 15:08:45 +08:00
|
|
|
|
{
|
|
|
|
|
device=d;
|
|
|
|
|
cmd_buf=nullptr;
|
2023-10-12 02:24:58 +08:00
|
|
|
|
material=m;
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
2024-05-25 17:58:39 +08:00
|
|
|
|
assign_buffer=new RenderAssignBuffer(device,material);
|
2023-10-12 02:24:58 +08:00
|
|
|
|
|
2024-05-28 23:10:50 +08:00
|
|
|
|
vab_list=new VABList(material->GetVertexInput()->GetCount());
|
2023-06-01 15:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MaterialRenderList::~MaterialRenderList()
|
|
|
|
|
{
|
2024-05-28 23:10:50 +08:00
|
|
|
|
SAFE_CLEAR(vab_list);
|
2024-05-25 17:58:39 +08:00
|
|
|
|
SAFE_CLEAR(assign_buffer);
|
2023-06-01 15:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MaterialRenderList::Add(Renderable *ri,const Matrix4f &mat)
|
|
|
|
|
{
|
|
|
|
|
RenderNode rn;
|
|
|
|
|
|
|
|
|
|
rn.local_to_world=mat;
|
|
|
|
|
rn.ri=ri;
|
|
|
|
|
|
|
|
|
|
rn_list.Add(rn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MaterialRenderList::End()
|
|
|
|
|
{
|
|
|
|
|
//排序
|
2023-07-28 20:41:02 +08:00
|
|
|
|
Sort(rn_list.GetArray());
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
2023-06-01 21:25:50 +08:00
|
|
|
|
const uint node_count=rn_list.GetCount();
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
2023-06-01 21:25:50 +08:00
|
|
|
|
if(node_count<=0)return;
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
|
|
|
|
Stat();
|
2023-06-01 21:25:50 +08:00
|
|
|
|
|
2024-05-25 17:58:39 +08:00
|
|
|
|
if(assign_buffer)
|
|
|
|
|
assign_buffer->WriteNode(rn_list);
|
2023-06-01 15:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MaterialRenderList::RenderItem::Set(Renderable *ri)
|
|
|
|
|
{
|
2024-05-27 01:42:10 +08:00
|
|
|
|
pipeline=ri->GetPipeline();
|
|
|
|
|
mi =ri->GetMaterialInstance();
|
2024-05-29 00:55:12 +08:00
|
|
|
|
pdb =ri->GetDataBuffer();
|
2024-05-27 01:42:10 +08:00
|
|
|
|
prd =ri->GetRenderData();
|
2023-06-01 15:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MaterialRenderList::Stat()
|
|
|
|
|
{
|
|
|
|
|
const uint count=rn_list.GetCount();
|
|
|
|
|
RenderNode *rn=rn_list.GetData();
|
|
|
|
|
|
2023-09-05 10:28:03 +08:00
|
|
|
|
ri_array.Clear();
|
|
|
|
|
ri_array.Alloc(count);
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
2023-09-05 10:28:03 +08:00
|
|
|
|
RenderItem *ri=ri_array.GetData();
|
2023-09-20 15:55:14 +08:00
|
|
|
|
|
2023-06-01 15:08:45 +08:00
|
|
|
|
ri_count=1;
|
|
|
|
|
|
|
|
|
|
ri->first=0;
|
2024-05-28 23:10:50 +08:00
|
|
|
|
ri->instance_count=1;
|
2023-06-01 15:08:45 +08:00
|
|
|
|
ri->Set(rn->ri);
|
|
|
|
|
|
|
|
|
|
last_pipeline =ri->pipeline;
|
2024-05-28 23:49:28 +08:00
|
|
|
|
last_data_buffer=ri->pdb;
|
|
|
|
|
last_vdm =ri->pdb->vdm;
|
2024-05-27 01:42:10 +08:00
|
|
|
|
last_render_data=ri->prd;
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
|
|
|
|
++rn;
|
|
|
|
|
|
|
|
|
|
for(uint i=1;i<count;i++)
|
2023-09-19 21:45:54 +08:00
|
|
|
|
{
|
2023-06-01 15:08:45 +08:00
|
|
|
|
if(last_pipeline==rn->ri->GetPipeline())
|
2024-05-29 00:55:12 +08:00
|
|
|
|
{
|
|
|
|
|
if(last_data_buffer->vdm&&last_data_buffer->vdm==rn->ri->GetDataBuffer()->vdm)
|
|
|
|
|
{
|
2024-05-28 23:10:50 +08:00
|
|
|
|
if(last_render_data->_Comp(rn->ri->GetRenderData())==0)
|
2024-05-25 22:08:01 +08:00
|
|
|
|
{
|
2024-05-28 23:10:50 +08:00
|
|
|
|
++ri->instance_count;
|
2024-05-25 22:08:01 +08:00
|
|
|
|
++rn;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-05-29 00:55:12 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if(last_data_buffer->Comp(rn->ri->GetDataBuffer()))
|
|
|
|
|
if(last_render_data->_Comp(rn->ri->GetRenderData())==0)
|
|
|
|
|
{
|
|
|
|
|
++ri->instance_count;
|
|
|
|
|
++rn;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
|
|
|
|
++ri_count;
|
|
|
|
|
++ri;
|
|
|
|
|
|
|
|
|
|
ri->first=i;
|
2024-05-28 23:10:50 +08:00
|
|
|
|
ri->instance_count=1;
|
2023-06-01 15:08:45 +08:00
|
|
|
|
ri->Set(rn->ri);
|
|
|
|
|
|
|
|
|
|
last_pipeline =ri->pipeline;
|
2024-05-29 00:55:12 +08:00
|
|
|
|
last_data_buffer=ri->pdb;
|
|
|
|
|
last_vdm =ri->pdb->vdm;
|
2024-05-27 01:42:10 +08:00
|
|
|
|
last_render_data=ri->prd;
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
|
|
|
|
++rn;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-28 23:10:50 +08:00
|
|
|
|
bool MaterialRenderList::BindVAB(const PrimitiveDataBuffer *pdb,const uint ri_index)
|
2023-06-01 15:08:45 +08:00
|
|
|
|
{
|
|
|
|
|
//binding号都是在VertexInput::CreateVIL时连续紧密排列生成的,所以bind时first_binding写0就行了。
|
|
|
|
|
|
2023-10-12 02:24:58 +08:00
|
|
|
|
//const VIL *vil=last_vil;
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
2024-05-27 01:42:10 +08:00
|
|
|
|
//if(vil->GetCount(VertexInputGroup::Basic)!=prb->vab_count)
|
2023-10-12 02:24:58 +08:00
|
|
|
|
// return(false); //这里基本不太可能,因为CreateRenderable时就会检查值是否一样
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
2024-05-28 23:10:50 +08:00
|
|
|
|
vab_list->Restart();
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
2024-04-24 01:44:01 +08:00
|
|
|
|
//Basic组,它所有的VAB信息均来自于Primitive,由vid参数传递进来
|
2023-06-01 15:08:45 +08:00
|
|
|
|
{
|
2024-05-28 23:10:50 +08:00
|
|
|
|
vab_list->Add(pdb->vab_list,
|
|
|
|
|
nullptr,//prb->vab_offset,
|
|
|
|
|
pdb->vab_count);
|
2023-06-01 15:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-25 17:58:39 +08:00
|
|
|
|
if(assign_buffer) //L2W/MI分发组
|
2024-05-28 23:10:50 +08:00
|
|
|
|
vab_list->Add(assign_buffer->GetVAB(),0);//ASSIGN_VAB_STRIDE_BYTES*ri_index);
|
2024-03-26 01:20:20 +08:00
|
|
|
|
|
2024-05-28 23:10:50 +08:00
|
|
|
|
//if(!vab_list.IsFull()) //Joint组,暂未支持
|
2023-09-05 20:19:53 +08:00
|
|
|
|
//{
|
|
|
|
|
// const uint joint_id_binding_count=vil->GetCount(VertexInputGroup::JointID);
|
|
|
|
|
|
|
|
|
|
// if(joint_id_binding_count>0) //有矩阵信息
|
|
|
|
|
// {
|
|
|
|
|
// count+=joint_id_binding_count;
|
|
|
|
|
|
2024-05-25 22:08:01 +08:00
|
|
|
|
// if(count<vab_count) //JointWeight组
|
2023-09-05 20:19:53 +08:00
|
|
|
|
// {
|
|
|
|
|
// const uint joing_weight_binding_count=vil->GetCount(VertexInputGroup::JointWeight);
|
|
|
|
|
|
|
|
|
|
// if(joing_weight_binding_count!=1)
|
|
|
|
|
// {
|
|
|
|
|
// ++count;
|
|
|
|
|
// }
|
|
|
|
|
// else //JointWieght不为1是个bug,除非未来支持8权重
|
|
|
|
|
// {
|
|
|
|
|
// return(false);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// else //有JointID没有JointWeight? 这是个BUG
|
|
|
|
|
// {
|
|
|
|
|
// return(false);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
|
2024-05-25 22:08:01 +08:00
|
|
|
|
//if(count!=vab_count)
|
2023-09-05 20:19:53 +08:00
|
|
|
|
//{
|
|
|
|
|
// //还有没支持的绑定组????
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
2023-09-05 20:19:53 +08:00
|
|
|
|
// return(false);
|
|
|
|
|
//}
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
2024-05-28 23:10:50 +08:00
|
|
|
|
cmd_buf->BindVAB(vab_list);
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MaterialRenderList::Render(RenderItem *ri)
|
|
|
|
|
{
|
|
|
|
|
if(last_pipeline!=ri->pipeline)
|
|
|
|
|
{
|
|
|
|
|
cmd_buf->BindPipeline(ri->pipeline);
|
|
|
|
|
last_pipeline=ri->pipeline;
|
|
|
|
|
|
2024-05-28 23:10:50 +08:00
|
|
|
|
last_data_buffer=nullptr;
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
|
|
|
|
//这里未来尝试换pipeline同时不换mi/primitive是否需要重新绑定mi/primitive
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-28 23:10:50 +08:00
|
|
|
|
if(!ri->pdb->Comp(last_data_buffer))
|
2023-06-01 15:08:45 +08:00
|
|
|
|
{
|
2024-05-28 23:10:50 +08:00
|
|
|
|
last_data_buffer=ri->pdb;
|
2024-05-27 01:42:10 +08:00
|
|
|
|
last_render_data=nullptr;
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
2024-05-28 23:10:50 +08:00
|
|
|
|
BindVAB(ri->pdb,ri->first);
|
2024-05-28 02:21:33 +08:00
|
|
|
|
|
2024-05-28 23:10:50 +08:00
|
|
|
|
if(ri->pdb->ibo)
|
|
|
|
|
cmd_buf->BindIBO(ri->pdb->ibo);
|
2024-05-25 22:08:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-28 23:10:50 +08:00
|
|
|
|
cmd_buf->Draw(ri->pdb,ri->prd,ri->instance_count,ri->first);
|
2023-06-01 15:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MaterialRenderList::Render(RenderCmdBuffer *rcb)
|
|
|
|
|
{
|
|
|
|
|
if(!rcb)return;
|
|
|
|
|
const uint count=rn_list.GetCount();
|
|
|
|
|
|
|
|
|
|
if(count<=0)return;
|
|
|
|
|
|
|
|
|
|
if(ri_count<=0)return;
|
|
|
|
|
|
|
|
|
|
cmd_buf=rcb;
|
|
|
|
|
|
|
|
|
|
last_pipeline =nullptr;
|
2024-05-28 23:49:28 +08:00
|
|
|
|
last_data_buffer=nullptr;
|
|
|
|
|
last_vdm =nullptr;
|
|
|
|
|
last_render_data=nullptr;
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
2024-05-25 17:58:39 +08:00
|
|
|
|
if(assign_buffer)
|
|
|
|
|
assign_buffer->Bind(material);
|
2023-10-12 02:24:58 +08:00
|
|
|
|
|
|
|
|
|
cmd_buf->BindDescriptorSets(material);
|
|
|
|
|
|
2024-05-29 00:55:12 +08:00
|
|
|
|
RenderItem *ri=ri_array.GetData();
|
2023-06-01 15:08:45 +08:00
|
|
|
|
for(uint i=0;i<ri_count;i++)
|
|
|
|
|
{
|
|
|
|
|
Render(ri);
|
|
|
|
|
++ri;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
VK_NAMESPACE_END
|