ULRE/src/SceneGraph/MaterialRenderList.cpp

276 lines
6.7 KiB
C++
Raw Normal View History

#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>
#include<hgl/util/sort/Sort.h>
#include"RenderAssignBuffer.h"
/**
*
*
*
* for(material)
* for(pipeline)
* for(material_instance)
2024-04-24 01:38:55 +08:00
* for(vab)
*/
template<>
int Comparator<hgl::graph::RenderNode>::compare(const hgl::graph::RenderNode &obj_one,const hgl::graph::RenderNode &obj_two) const
{
int off;
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;
}
//比较模型
{
off=ri_one->GetPrimitive()
-ri_two->GetPrimitive();
if(off)
return off;
}
return 0;
}
VK_NAMESPACE_BEGIN
MaterialRenderList::MaterialRenderList(GPUDevice *d,bool l2w,Material *m)
{
device=d;
cmd_buf=nullptr;
material=m;
assign_buffer=new RenderAssignBuffer(device,material);
2024-05-25 22:57:29 +08:00
vbo_list=new VABList(material->GetVertexInput()->GetCount());
}
MaterialRenderList::~MaterialRenderList()
{
SAFE_CLEAR(vbo_list);
SAFE_CLEAR(assign_buffer);
}
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());
const uint node_count=rn_list.GetCount();
if(node_count<=0)return;
Stat();
if(assign_buffer)
assign_buffer->WriteNode(rn_list);
}
void MaterialRenderList::RenderItem::Set(Renderable *ri)
{
2024-05-27 01:42:10 +08:00
pipeline=ri->GetPipeline();
mi =ri->GetMaterialInstance();
prb =ri->GetRenderBuffer();
prd =ri->GetRenderData();
}
void MaterialRenderList::Stat()
{
const uint count=rn_list.GetCount();
RenderNode *rn=rn_list.GetData();
ri_array.Clear();
ri_array.Alloc(count);
RenderItem *ri=ri_array.GetData();
ri_count=1;
ri->first=0;
ri->count=1;
ri->Set(rn->ri);
last_pipeline =ri->pipeline;
2024-05-27 01:42:10 +08:00
last_render_buf =ri->prb;
last_render_data=ri->prd;
++rn;
for(uint i=1;i<count;i++)
{
if(last_pipeline==rn->ri->GetPipeline())
2024-05-27 01:42:10 +08:00
if(last_render_buf->Comp(rn->ri->GetRenderBuffer()))
if(last_render_data->Comp(rn->ri->GetRenderData()))
{
++ri->count;
++rn;
continue;
}
++ri_count;
++ri;
ri->first=i;
ri->count=1;
ri->Set(rn->ri);
last_pipeline =ri->pipeline;
2024-05-27 01:42:10 +08:00
last_render_buf =ri->prb;
last_render_data=ri->prd;
++rn;
}
}
2024-05-28 02:21:33 +08:00
bool MaterialRenderList::BindVAB(const PrimitiveDataBuffer *prb,const uint ri_index)
{
//binding号都是在VertexInput::CreateVIL时连续紧密排列生成的所以bind时first_binding写0就行了。
//const VIL *vil=last_vil;
2024-05-27 01:42:10 +08:00
//if(vil->GetCount(VertexInputGroup::Basic)!=prb->vab_count)
// return(false); //这里基本不太可能因为CreateRenderable时就会检查值是否一样
vbo_list->Restart();
2024-04-24 01:44:01 +08:00
//Basic组它所有的VAB信息均来自于Primitive由vid参数传递进来
{
2024-05-27 01:42:10 +08:00
vbo_list->Add(prb->vab_list,
2024-05-28 02:21:33 +08:00
prb->vab_offset,
2024-05-27 01:42:10 +08:00
prb->vab_count);
}
if(assign_buffer) //L2W/MI分发组
vbo_list->Add(assign_buffer->GetVAB(),0);//ASSIGN_VAB_STRIDE_BYTES*ri_index);
//if(!vbo_list.IsFull()) //Joint组暂未支持
//{
// const uint joint_id_binding_count=vil->GetCount(VertexInputGroup::JointID);
// if(joint_id_binding_count>0) //有矩阵信息
// {
// count+=joint_id_binding_count;
// if(count<vab_count) //JointWeight组
// {
// 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);
// }
// }
//}
//if(count!=vab_count)
//{
// //还有没支持的绑定组????
// return(false);
//}
2024-05-25 22:57:29 +08:00
cmd_buf->BindVAB(vbo_list);
return(true);
}
void MaterialRenderList::Render(RenderItem *ri)
{
if(last_pipeline!=ri->pipeline)
{
cmd_buf->BindPipeline(ri->pipeline);
last_pipeline=ri->pipeline;
2024-05-27 01:42:10 +08:00
last_render_buf=nullptr;
//这里未来尝试换pipeline同时不换mi/primitive是否需要重新绑定mi/primitive
}
2024-05-27 01:42:10 +08:00
if(!ri->prb->Comp(last_render_buf))
{
2024-05-27 01:42:10 +08:00
last_render_buf=ri->prb;
last_render_data=nullptr;
2024-05-28 02:21:33 +08:00
BindVAB(ri->prb,ri->first);
if(ri->prb->ib_access->buffer)
cmd_buf->BindIBO(ri->prb->ib_access);
}
2024-05-28 02:21:33 +08:00
if(last_render_buf->ib_access->buffer)
{
2024-05-27 01:42:10 +08:00
cmd_buf->DrawIndexed(ri->prd->index_count,
ri->count,
2024-05-28 02:21:33 +08:00
ri->prd->first_index,
ri->prd->vertex_offset, //因为vkCmdDrawIndexed的vertexOffset是针对所有VAB的所以所有的VAB数据都必须是对齐的
2024-05-26 15:55:28 +08:00
//最终这里使用vab_offset[0]是可以的因为它也等于其它所有的vab_offset。未来考虑统一成一个。
ri->first); //这里vkCmdDrawIndexed的firstInstance参数指的是instance Rate更新的VAB的起始实例数不是指instance批量渲染。
//所以这里使用ri->first是对的。
}
else
{
2024-05-27 01:42:10 +08:00
cmd_buf->Draw(ri->prd->vertex_count,ri->count);
}
}
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;
RenderItem *ri=ri_array.GetData();
last_pipeline =nullptr;
2024-05-27 01:42:10 +08:00
last_render_buf =nullptr;
if(assign_buffer)
assign_buffer->Bind(material);
cmd_buf->BindDescriptorSets(material);
for(uint i=0;i<ri_count;i++)
{
Render(ri);
++ri;
}
}
VK_NAMESPACE_END