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"
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* 理论上讲,我们需要按以下顺序排序
|
|
|
|
|
*
|
|
|
|
|
* for(material)
|
|
|
|
|
* for(pipeline)
|
|
|
|
|
* for(material_instance)
|
|
|
|
|
* for(vbo)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 18:56:09 +08:00
|
|
|
|
//比较顶点输入格式
|
2023-06-01 15:08:45 +08:00
|
|
|
|
{
|
2023-09-20 18:56:09 +08:00
|
|
|
|
off=ri_one->GetMaterialInstance()->GetVIL()
|
|
|
|
|
-ri_two->GetMaterialInstance()->GetVIL();
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
|
|
|
|
if(off)
|
|
|
|
|
return off;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//比较模型
|
|
|
|
|
{
|
|
|
|
|
off=ri_one->GetPrimitive()
|
|
|
|
|
-ri_two->GetPrimitive();
|
|
|
|
|
|
|
|
|
|
if(off)
|
|
|
|
|
return off;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VK_NAMESPACE_BEGIN
|
|
|
|
|
MaterialRenderList::MaterialRenderList(GPUDevice *d,Material *m)
|
|
|
|
|
{
|
|
|
|
|
device=d;
|
|
|
|
|
cmd_buf=nullptr;
|
|
|
|
|
mtl=m;
|
2023-09-07 18:09:31 +08:00
|
|
|
|
assign_buffer=new RenderAssignBuffer(d,mtl->GetMIDataBytes());
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
2023-09-05 20:19:53 +08:00
|
|
|
|
vbo_list=new VBOList(mtl->GetVertexInput()->GetCount());
|
2023-06-01 15:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MaterialRenderList::~MaterialRenderList()
|
|
|
|
|
{
|
2023-09-05 20:19:53 +08:00
|
|
|
|
SAFE_CLEAR(vbo_list);
|
2023-09-05 20:48:47 +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
|
|
|
|
|
2023-09-19 22:04:34 +08:00
|
|
|
|
if(mtl->HasMI())
|
|
|
|
|
StatMI();
|
|
|
|
|
|
2023-06-01 15:08:45 +08:00
|
|
|
|
Stat();
|
2023-06-01 21:25:50 +08:00
|
|
|
|
|
|
|
|
|
//写入LocalToWorld数据
|
2023-09-07 18:09:31 +08:00
|
|
|
|
assign_buffer->WriteNode(rn_list.GetData(),node_count,mi_set);
|
2023-06-01 15:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MaterialRenderList::RenderItem::Set(Renderable *ri)
|
|
|
|
|
{
|
|
|
|
|
pipeline =ri->GetPipeline();
|
|
|
|
|
mi =ri->GetMaterialInstance();
|
|
|
|
|
vid =ri->GetVertexInputData();
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-19 21:45:54 +08:00
|
|
|
|
void MaterialRenderList::StatMI()
|
|
|
|
|
{
|
2023-09-20 15:55:14 +08:00
|
|
|
|
mi_set.Clear();
|
2023-09-19 21:45:54 +08:00
|
|
|
|
|
|
|
|
|
for(RenderNode &rn:rn_list)
|
2023-09-20 15:55:14 +08:00
|
|
|
|
mi_set.Add(rn.ri->GetMaterialInstance());
|
2023-09-19 21:45:54 +08:00
|
|
|
|
|
2023-09-20 15:55:14 +08:00
|
|
|
|
if(mi_set.GetCount()>mtl->GetMIMaxCount())
|
2023-09-19 21:45:54 +08:00
|
|
|
|
{
|
|
|
|
|
//超出最大数量了怎么办???
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
ri->count=1;
|
|
|
|
|
ri->Set(rn->ri);
|
|
|
|
|
|
|
|
|
|
last_pipeline =ri->pipeline;
|
2023-09-20 17:19:46 +08:00
|
|
|
|
last_vil =ri->mi->GetVIL();
|
2023-06-01 15:08:45 +08:00
|
|
|
|
last_vid =ri->vid;
|
|
|
|
|
|
|
|
|
|
++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())
|
2023-09-20 17:19:46 +08:00
|
|
|
|
if(last_vil==rn->ri->GetMaterialInstance()->GetVIL())
|
2023-09-20 21:53:30 +08:00
|
|
|
|
if(last_vid->Comp(rn->ri->GetVertexInputData()))
|
2023-06-01 15:08:45 +08:00
|
|
|
|
{
|
|
|
|
|
++ri->count;
|
|
|
|
|
++rn;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
++ri_count;
|
|
|
|
|
++ri;
|
|
|
|
|
|
|
|
|
|
ri->first=i;
|
|
|
|
|
ri->count=1;
|
|
|
|
|
ri->Set(rn->ri);
|
|
|
|
|
|
|
|
|
|
last_pipeline =ri->pipeline;
|
2023-09-20 17:19:46 +08:00
|
|
|
|
last_vil =ri->mi->GetVIL();
|
2023-06-01 15:08:45 +08:00
|
|
|
|
last_vid =ri->vid;
|
|
|
|
|
|
|
|
|
|
++rn;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MaterialRenderList::Bind(MaterialInstance *mi)
|
|
|
|
|
{
|
2023-09-05 20:19:53 +08:00
|
|
|
|
if(!mi)return;
|
|
|
|
|
|
2023-09-06 16:24:05 +08:00
|
|
|
|
assign_buffer->Bind(mi);
|
2023-09-05 20:19:53 +08:00
|
|
|
|
|
|
|
|
|
cmd_buf->BindDescriptorSets(mi->GetMaterial());
|
2023-06-01 15:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-01 21:25:50 +08:00
|
|
|
|
bool MaterialRenderList::Bind(const VertexInputData *vid,const uint ri_index)
|
2023-06-01 15:08:45 +08:00
|
|
|
|
{
|
|
|
|
|
//binding号都是在VertexInput::CreateVIL时连续紧密排列生成的,所以bind时first_binding写0就行了。
|
|
|
|
|
|
2023-09-20 17:19:46 +08:00
|
|
|
|
const VIL *vil=last_vil;
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
|
|
|
|
if(vil->GetCount(VertexInputGroup::Basic)!=vid->binding_count)
|
|
|
|
|
return(false); //这里基本不太可能,因为CreateRenderable时就会检查值是否一样
|
|
|
|
|
|
2023-09-05 20:19:53 +08:00
|
|
|
|
vbo_list->Restart();
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
|
|
|
|
//Basic组,它所有的VBO信息均来自于Primitive,由vid参数传递进来
|
|
|
|
|
{
|
2023-09-05 20:19:53 +08:00
|
|
|
|
vbo_list->Add(vid->buffer_list,vid->buffer_offset,vid->binding_count);
|
2023-06-01 15:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-05 20:19:53 +08:00
|
|
|
|
//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<binding_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);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
|
2023-09-07 18:09:31 +08:00
|
|
|
|
if(!vbo_list->IsFull()) //Assign组
|
2023-06-01 15:08:45 +08:00
|
|
|
|
{
|
2023-09-05 20:19:53 +08:00
|
|
|
|
const uint assign_binding_count=vil->GetCount(VertexInputGroup::Assign);
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
2023-09-05 20:19:53 +08:00
|
|
|
|
if(assign_binding_count>0)
|
2023-06-01 15:08:45 +08:00
|
|
|
|
{
|
2023-09-05 20:19:53 +08:00
|
|
|
|
if(assign_binding_count!=1)
|
2023-06-01 15:08:45 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2023-09-06 17:06:12 +08:00
|
|
|
|
vbo_list->Add(assign_buffer->GetAssignVBO(),ASSIGN_VBO_STRIDE_BYTES*ri_index);
|
2023-06-01 15:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-05 20:19:53 +08:00
|
|
|
|
//if(count!=binding_count)
|
|
|
|
|
//{
|
|
|
|
|
// //还有没支持的绑定组????
|
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
|
|
|
|
|
2023-09-05 20:19:53 +08:00
|
|
|
|
cmd_buf->BindVBO(vbo_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;
|
|
|
|
|
|
|
|
|
|
last_vid=nullptr;
|
|
|
|
|
|
|
|
|
|
//这里未来尝试换pipeline同时不换mi/primitive是否需要重新绑定mi/primitive
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 17:19:46 +08:00
|
|
|
|
if(last_vil!=ri->mi->GetVIL())
|
2023-06-01 15:08:45 +08:00
|
|
|
|
{
|
|
|
|
|
Bind(ri->mi);
|
2023-09-20 17:19:46 +08:00
|
|
|
|
last_vil=ri->mi->GetVIL();
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
|
|
|
|
last_vid=nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!ri->vid->Comp(last_vid))
|
|
|
|
|
{
|
|
|
|
|
Bind(ri->vid,ri->first);
|
|
|
|
|
last_vid=ri->vid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const IndexBufferData *ibd=last_vid->index_buffer;
|
|
|
|
|
|
|
|
|
|
if(ibd->buffer)
|
|
|
|
|
{
|
|
|
|
|
cmd_buf->BindIBO(ibd);
|
|
|
|
|
|
|
|
|
|
cmd_buf->DrawIndexed(ibd->buffer->GetCount(),ri->count);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
cmd_buf->Draw(last_vid->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;
|
|
|
|
|
|
2023-09-05 10:28:03 +08:00
|
|
|
|
RenderItem *ri=ri_array.GetData();
|
2023-06-01 15:08:45 +08:00
|
|
|
|
|
|
|
|
|
last_pipeline =nullptr;
|
2023-09-20 17:19:46 +08:00
|
|
|
|
last_vil =nullptr;
|
2023-06-01 15:08:45 +08:00
|
|
|
|
last_vid =nullptr;
|
|
|
|
|
|
|
|
|
|
for(uint i=0;i<ri_count;i++)
|
|
|
|
|
{
|
|
|
|
|
Render(ri);
|
|
|
|
|
++ri;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
VK_NAMESPACE_END
|