2023-04-26 20:52:31 +08:00
|
|
|
|
#include<hgl/graph/RenderNode2D.h>
|
2023-04-25 19:18:07 +08:00
|
|
|
|
#include<hgl/graph/VKRenderable.h>
|
2023-05-04 22:01:00 +08:00
|
|
|
|
#include<hgl/graph/VKDevice.h>
|
|
|
|
|
#include<hgl/graph/VKVertexAttribBuffer.h>
|
|
|
|
|
#include<hgl/graph/VKVertexInput.h>
|
|
|
|
|
#include<hgl/graph/VKCommandBuffer.h>
|
2023-04-25 19:18:07 +08:00
|
|
|
|
#include<hgl/util/sort/Sort.h>
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
2023-04-26 20:52:31 +08:00
|
|
|
|
* 理论上讲,我们需要按以下顺序排序
|
2023-04-25 19:18:07 +08:00
|
|
|
|
*
|
|
|
|
|
* for(material)
|
|
|
|
|
* for(pipeline)
|
|
|
|
|
* for(material_instance)
|
|
|
|
|
* for(vbo)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
int Comparator<hgl::graph::RenderNode2D>::compare(const hgl::graph::RenderNode2D &obj_one,const hgl::graph::RenderNode2D &obj_two) const
|
|
|
|
|
{
|
|
|
|
|
int off;
|
|
|
|
|
|
|
|
|
|
hgl::graph::Renderable *ri_one=obj_one.ri;
|
|
|
|
|
hgl::graph::Renderable *ri_two=obj_two.ri;
|
|
|
|
|
|
2023-04-26 20:52:31 +08:00
|
|
|
|
//比较管线
|
2023-04-25 19:18:07 +08:00
|
|
|
|
{
|
|
|
|
|
off=ri_one->GetPipeline()
|
|
|
|
|
-ri_two->GetPipeline();
|
|
|
|
|
|
|
|
|
|
if(off)
|
|
|
|
|
return off;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-26 20:52:31 +08:00
|
|
|
|
//比较材质实例
|
2023-04-28 11:10:50 +08:00
|
|
|
|
{
|
|
|
|
|
off=ri_one->GetMaterialInstance()
|
|
|
|
|
-ri_two->GetMaterialInstance();
|
|
|
|
|
|
|
|
|
|
if(off)
|
|
|
|
|
return off;
|
|
|
|
|
}
|
2023-04-25 19:18:07 +08:00
|
|
|
|
|
2023-05-04 22:01:00 +08:00
|
|
|
|
//比较模型
|
2023-04-25 19:18:07 +08:00
|
|
|
|
{
|
2023-05-04 22:01:00 +08:00
|
|
|
|
off=ri_one->GetPrimitive()
|
|
|
|
|
-ri_two->GetPrimitive();
|
2023-04-25 19:18:07 +08:00
|
|
|
|
|
|
|
|
|
if(off)
|
|
|
|
|
return off;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace hgl
|
|
|
|
|
{
|
|
|
|
|
namespace graph
|
|
|
|
|
{
|
2023-05-04 22:01:00 +08:00
|
|
|
|
/*
|
|
|
|
|
* 2D渲染节点额外提供的VBO数据
|
|
|
|
|
*/
|
|
|
|
|
struct RenderNode2DExtraBuffer
|
|
|
|
|
{
|
|
|
|
|
uint count;
|
|
|
|
|
|
2023-05-06 22:19:50 +08:00
|
|
|
|
VBO *l2w_vbo[4];
|
|
|
|
|
VkBuffer l2w_buffer[4];
|
2023-05-05 21:12:53 +08:00
|
|
|
|
|
2023-05-04 22:01:00 +08:00
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
RenderNode2DExtraBuffer()
|
|
|
|
|
{
|
|
|
|
|
hgl_zero(*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~RenderNode2DExtraBuffer()
|
|
|
|
|
{
|
|
|
|
|
Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Clear()
|
|
|
|
|
{
|
2023-05-06 10:38:35 +08:00
|
|
|
|
SAFE_CLEAR(l2w_vbo[0])
|
|
|
|
|
SAFE_CLEAR(l2w_vbo[1])
|
|
|
|
|
SAFE_CLEAR(l2w_vbo[2])
|
2023-05-06 22:19:50 +08:00
|
|
|
|
SAFE_CLEAR(l2w_vbo[3])
|
2023-05-04 22:01:00 +08:00
|
|
|
|
count=0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Alloc(GPUDevice *dev,const uint c)
|
|
|
|
|
{
|
|
|
|
|
Clear();
|
|
|
|
|
count=power_to_2(c);
|
|
|
|
|
|
2023-05-06 22:19:50 +08:00
|
|
|
|
for(uint i=0;i<4;i++)
|
2023-05-05 21:12:53 +08:00
|
|
|
|
{
|
2023-05-06 10:38:35 +08:00
|
|
|
|
l2w_vbo[i]=dev->CreateVBO(VF_V4F,count);
|
|
|
|
|
l2w_buffer[i]=l2w_vbo[i]->GetBuffer();
|
2023-05-05 21:12:53 +08:00
|
|
|
|
}
|
2023-05-04 22:01:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WriteData(RenderNode2D *render_node,const uint count)
|
|
|
|
|
{
|
|
|
|
|
RenderNode2D *rn;
|
|
|
|
|
glm::vec4 *tp;
|
|
|
|
|
|
2023-05-06 22:19:50 +08:00
|
|
|
|
for(uint col=0;col<4;col++)
|
2023-05-04 22:01:00 +08:00
|
|
|
|
{
|
2023-05-06 10:38:35 +08:00
|
|
|
|
tp=(glm::vec4 *)(l2w_vbo[col]->Map());
|
2023-05-04 22:01:00 +08:00
|
|
|
|
|
|
|
|
|
rn=render_node;
|
|
|
|
|
|
|
|
|
|
for(uint i=0;i<count;i++)
|
|
|
|
|
{
|
|
|
|
|
*tp=rn->local_to_world[col];
|
|
|
|
|
++tp;
|
|
|
|
|
++rn;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-06 10:38:35 +08:00
|
|
|
|
l2w_vbo[col]->Unmap();
|
2023-05-04 22:01:00 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};//struct RenderNode2DExtraBuffer
|
|
|
|
|
|
2023-05-06 19:30:08 +08:00
|
|
|
|
MaterialRenderList2D::MaterialRenderList2D(GPUDevice *d,Material *m)
|
2023-05-04 22:01:00 +08:00
|
|
|
|
{
|
|
|
|
|
device=d;
|
2023-05-06 19:30:08 +08:00
|
|
|
|
cmd_buf=nullptr;
|
2023-05-04 22:01:00 +08:00
|
|
|
|
mtl=m;
|
|
|
|
|
extra_buffer=nullptr;
|
|
|
|
|
|
|
|
|
|
const VertexInput *vi=mtl->GetVertexInput();
|
|
|
|
|
|
|
|
|
|
binding_count=vi->GetCount();
|
|
|
|
|
buffer_list=new VkBuffer[binding_count];
|
|
|
|
|
buffer_offset=new VkDeviceSize[binding_count];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MaterialRenderList2D::~MaterialRenderList2D()
|
|
|
|
|
{
|
|
|
|
|
delete[] buffer_offset;
|
|
|
|
|
delete[] buffer_list;
|
|
|
|
|
|
|
|
|
|
SAFE_CLEAR(extra_buffer)
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-06 22:19:50 +08:00
|
|
|
|
void MaterialRenderList2D::Add(Renderable *ri,const Matrix4f &mat)
|
2023-04-25 19:18:07 +08:00
|
|
|
|
{
|
|
|
|
|
RenderNode2D rn;
|
|
|
|
|
|
|
|
|
|
rn.local_to_world=mat;
|
|
|
|
|
rn.ri=ri;
|
|
|
|
|
|
|
|
|
|
rn_list.Add(rn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MaterialRenderList2D::End()
|
|
|
|
|
{
|
2023-04-26 20:52:31 +08:00
|
|
|
|
//排序
|
|
|
|
|
{
|
|
|
|
|
Comparator<hgl::graph::RenderNode2D> rnc;
|
2023-04-25 19:18:07 +08:00
|
|
|
|
|
2023-04-26 20:52:31 +08:00
|
|
|
|
Sort(rn_list,&rnc);
|
|
|
|
|
}
|
2023-04-25 19:18:07 +08:00
|
|
|
|
|
2023-05-04 22:01:00 +08:00
|
|
|
|
//写入LocalToWorld数据
|
|
|
|
|
{
|
|
|
|
|
uint count=rn_list.GetCount();
|
|
|
|
|
|
|
|
|
|
if(count<=0)return;
|
|
|
|
|
|
|
|
|
|
if(!extra_buffer)
|
|
|
|
|
extra_buffer=new RenderNode2DExtraBuffer;
|
|
|
|
|
|
|
|
|
|
if(extra_buffer->count<count)
|
|
|
|
|
extra_buffer->Alloc(device,count);
|
|
|
|
|
|
|
|
|
|
//写入数据
|
|
|
|
|
extra_buffer->WriteData(rn_list.GetData(),count);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-05 21:12:53 +08:00
|
|
|
|
void MaterialRenderList2D::RenderItem::Set(Renderable *ri)
|
|
|
|
|
{
|
|
|
|
|
pipeline =ri->GetPipeline();
|
|
|
|
|
mi =ri->GetMaterialInstance();
|
|
|
|
|
vid =ri->GetVertexInputData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MaterialRenderList2D::Stat()
|
2023-05-04 22:01:00 +08:00
|
|
|
|
{
|
2023-05-05 21:12:53 +08:00
|
|
|
|
const uint count=rn_list.GetCount();
|
|
|
|
|
RenderNode2D *rn=rn_list.GetData();
|
|
|
|
|
|
|
|
|
|
ri_list.ClearData();
|
|
|
|
|
ri_list.PreMalloc(count);
|
|
|
|
|
|
|
|
|
|
RenderItem *ri=ri_list.GetData();
|
|
|
|
|
|
|
|
|
|
ri_count=1;
|
|
|
|
|
|
|
|
|
|
ri->first=0;
|
|
|
|
|
ri->count=1;
|
|
|
|
|
ri->Set(rn->ri);
|
|
|
|
|
|
|
|
|
|
last_pipeline =ri->pipeline;
|
|
|
|
|
last_mi =ri->mi;
|
|
|
|
|
last_vid =ri->vid;
|
|
|
|
|
|
|
|
|
|
++rn;
|
|
|
|
|
|
|
|
|
|
for(uint i=1;i<count;i++)
|
|
|
|
|
{
|
|
|
|
|
if(last_pipeline==rn->ri->GetPipeline())
|
|
|
|
|
if(last_mi==rn->ri->GetMaterialInstance())
|
|
|
|
|
if(last_vid==rn->ri->GetVertexInputData())
|
|
|
|
|
{
|
|
|
|
|
++ri->count;
|
|
|
|
|
++rn;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
++ri_count;
|
|
|
|
|
|
|
|
|
|
++ri;
|
|
|
|
|
ri->first=i;
|
|
|
|
|
ri->count=1;
|
|
|
|
|
ri->Set(rn->ri);
|
|
|
|
|
|
|
|
|
|
last_pipeline =ri->pipeline;
|
|
|
|
|
last_mi =ri->mi;
|
|
|
|
|
last_vid =ri->vid;
|
|
|
|
|
|
|
|
|
|
++rn;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MaterialRenderList2D::Bind(MaterialInstance *mi)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MaterialRenderList2D::Bind(const VertexInputData *vid,const uint first)
|
|
|
|
|
{
|
|
|
|
|
//binding号都是在VertexInput::CreateVIL时连续紧密排列生成的,所以bind时first_binding写0就行了。
|
|
|
|
|
|
|
|
|
|
const VIL *vil=last_mi->GetVIL();
|
|
|
|
|
|
|
|
|
|
if(vil->GetCount(VertexInputGroup::Basic)!=vid->binding_count)
|
2023-05-06 10:38:35 +08:00
|
|
|
|
return(false); //这里基本不太可能,因为CreateRenderable时就会检查值是否一样
|
2023-05-05 21:12:53 +08:00
|
|
|
|
|
|
|
|
|
uint count=0;
|
|
|
|
|
|
2023-05-06 10:38:35 +08:00
|
|
|
|
//Basic组,它所有的VBO信息均来自于Primitive,由vid参数传递进来
|
|
|
|
|
{
|
|
|
|
|
hgl_cpy(buffer_list,vid->buffer_list,vid->binding_count);
|
|
|
|
|
hgl_cpy(buffer_offset,vid->buffer_offset,vid->binding_count);
|
2023-05-05 21:12:53 +08:00
|
|
|
|
|
2023-05-06 10:38:35 +08:00
|
|
|
|
count=vid->binding_count;
|
|
|
|
|
}
|
2023-05-05 21:12:53 +08:00
|
|
|
|
|
2023-05-06 19:30:08 +08:00
|
|
|
|
if(count<binding_count) //Bone组,暂未支持
|
2023-05-04 22:01:00 +08:00
|
|
|
|
{
|
2023-05-06 10:38:35 +08:00
|
|
|
|
const uint bone_binding_count=vil->GetCount(VertexInputGroup::Bone);
|
2023-05-04 22:01:00 +08:00
|
|
|
|
|
2023-05-06 10:38:35 +08:00
|
|
|
|
if(bone_binding_count>0) //有骨骼矩阵信息
|
|
|
|
|
{
|
|
|
|
|
if(bone_binding_count!=2) //只有BoneID/BondWeight,,,不是2的话根本就不对
|
|
|
|
|
return(false);
|
2023-05-04 22:01:00 +08:00
|
|
|
|
|
2023-05-06 10:38:35 +08:00
|
|
|
|
count+=bone_binding_count;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-05 21:12:53 +08:00
|
|
|
|
|
2023-05-06 19:30:08 +08:00
|
|
|
|
if(count<binding_count)//LocalToWorld组,由RenderList合成
|
2023-05-04 22:01:00 +08:00
|
|
|
|
{
|
2023-05-06 10:38:35 +08:00
|
|
|
|
const uint l2w_binding_count=vil->GetCount(VertexInputGroup::LocalToWorld);
|
|
|
|
|
|
|
|
|
|
if(l2w_binding_count>0) //有变换矩阵信息
|
|
|
|
|
{
|
2023-05-06 22:19:50 +08:00
|
|
|
|
if(l2w_binding_count!=4)
|
2023-05-06 10:38:35 +08:00
|
|
|
|
return(false);
|
2023-05-04 22:01:00 +08:00
|
|
|
|
|
2023-05-06 22:19:50 +08:00
|
|
|
|
hgl_cpy(buffer_list+count,extra_buffer->l2w_buffer,4);
|
2023-05-04 22:01:00 +08:00
|
|
|
|
|
2023-05-06 22:19:50 +08:00
|
|
|
|
for(uint i=0;i<4;i++)
|
2023-05-06 10:38:35 +08:00
|
|
|
|
buffer_offset[count+i]=first*16; //mat3x4f每列都是rgba32f,自然是16字节
|
2023-05-05 21:12:53 +08:00
|
|
|
|
|
2023-05-06 10:38:35 +08:00
|
|
|
|
count+=l2w_binding_count;
|
|
|
|
|
}
|
2023-05-04 22:01:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-05 21:12:53 +08:00
|
|
|
|
if(count!=binding_count)
|
2023-05-04 22:01:00 +08:00
|
|
|
|
{
|
2023-05-05 21:12:53 +08:00
|
|
|
|
//还有没支持的绑定组????
|
2023-05-04 22:01:00 +08:00
|
|
|
|
|
2023-05-05 21:12:53 +08:00
|
|
|
|
return(false);
|
|
|
|
|
}
|
2023-05-04 22:01:00 +08:00
|
|
|
|
|
2023-05-06 19:30:08 +08:00
|
|
|
|
cmd_buf->BindVBO(0,count,buffer_list,buffer_offset);
|
|
|
|
|
|
2023-05-05 21:12:53 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MaterialRenderList2D::Render(RenderItem *ri)
|
|
|
|
|
{
|
|
|
|
|
if(last_pipeline!=ri->pipeline)
|
|
|
|
|
{
|
|
|
|
|
cmd_buf->BindPipeline(ri->pipeline);
|
|
|
|
|
last_pipeline=ri->pipeline;
|
|
|
|
|
|
|
|
|
|
last_mi=nullptr;
|
|
|
|
|
last_vid=nullptr;
|
|
|
|
|
|
2023-05-06 19:30:08 +08:00
|
|
|
|
//这里未来尝试换pipeline同时不换mi/primitive是否需要重新绑定mi/primitive
|
2023-05-04 22:01:00 +08:00
|
|
|
|
}
|
2023-05-05 21:12:53 +08:00
|
|
|
|
|
|
|
|
|
if(last_mi!=ri->mi)
|
|
|
|
|
{
|
|
|
|
|
Bind(ri->mi);
|
|
|
|
|
last_mi=ri->mi;
|
|
|
|
|
|
|
|
|
|
last_vid=nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-06 19:30:08 +08:00
|
|
|
|
if(!ri->vid->Comp(last_vid))
|
2023-05-05 21:12:53 +08:00
|
|
|
|
{
|
|
|
|
|
Bind(ri->vid,ri->first);
|
|
|
|
|
last_vid=ri->vid;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-06 19:30:08 +08:00
|
|
|
|
const IndexBufferData *ibd=last_vid->index_buffer;
|
|
|
|
|
|
|
|
|
|
if(ibd->buffer)
|
|
|
|
|
{
|
|
|
|
|
cmd_buf->BindIBO(ibd);
|
2023-05-05 21:12:53 +08:00
|
|
|
|
|
2023-05-06 19:30:08 +08:00
|
|
|
|
cmd_buf->DrawIndexed(ibd->buffer->GetCount(),ri->count);
|
|
|
|
|
}
|
2023-05-05 21:12:53 +08:00
|
|
|
|
else
|
2023-05-06 19:30:08 +08:00
|
|
|
|
{
|
|
|
|
|
cmd_buf->Draw(last_vid->vertex_count,ri->count);
|
|
|
|
|
}
|
2023-05-04 22:01:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-06 19:30:08 +08:00
|
|
|
|
void MaterialRenderList2D::Render(RenderCmdBuffer *rcb)
|
2023-05-04 22:01:00 +08:00
|
|
|
|
{
|
2023-05-06 19:30:08 +08:00
|
|
|
|
if(!rcb)return;
|
2023-05-04 22:01:00 +08:00
|
|
|
|
const uint count=rn_list.GetCount();
|
|
|
|
|
|
|
|
|
|
if(count<=0)return;
|
|
|
|
|
|
2023-05-05 21:12:53 +08:00
|
|
|
|
Stat();
|
|
|
|
|
|
|
|
|
|
if(ri_count<=0)return;
|
2023-05-04 22:01:00 +08:00
|
|
|
|
|
2023-05-06 19:30:08 +08:00
|
|
|
|
cmd_buf=rcb;
|
|
|
|
|
|
2023-05-05 21:12:53 +08:00
|
|
|
|
RenderItem *ri=ri_list.GetData();
|
|
|
|
|
|
|
|
|
|
last_pipeline =nullptr;
|
|
|
|
|
last_mi =nullptr;
|
|
|
|
|
last_vid =nullptr;
|
|
|
|
|
|
|
|
|
|
for(uint i=0;i<ri_count;i++)
|
2023-04-26 20:52:31 +08:00
|
|
|
|
{
|
2023-05-05 21:12:53 +08:00
|
|
|
|
Render(ri);
|
|
|
|
|
++ri;
|
2023-04-26 20:52:31 +08:00
|
|
|
|
}
|
2023-04-25 19:18:07 +08:00
|
|
|
|
}
|
|
|
|
|
}//namespace graph
|
|
|
|
|
}//namespace hgl
|