2019-05-25 00:50:04 +08:00
|
|
|
|
#include<hgl/graph/RenderList.h>
|
|
|
|
|
#include<hgl/graph/Camera.h>
|
2019-05-21 21:28:33 +08:00
|
|
|
|
#include<hgl/graph/SceneNode.h>
|
2021-04-26 20:36:56 +08:00
|
|
|
|
#include<hgl/graph/VKBuffer.h>
|
2020-10-21 11:43:18 +08:00
|
|
|
|
#include<hgl/graph/VKRenderable.h>
|
|
|
|
|
#include<hgl/graph/VKCommandBuffer.h>
|
2020-07-16 20:36:54 +08:00
|
|
|
|
#include<hgl/graph/VertexAttribDataAccess.h>
|
2021-06-16 11:43:19 +08:00
|
|
|
|
#include<hgl/graph/VKMaterialParameters.h>
|
2020-10-21 11:43:18 +08:00
|
|
|
|
#include<hgl/graph/VKRenderableInstance.h>
|
2021-06-19 20:31:07 +08:00
|
|
|
|
#include<hgl/util/sort/Sort.h>
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 理论上讲,我们需要按以下顺序排序
|
|
|
|
|
*
|
|
|
|
|
* for(pipeline)
|
|
|
|
|
* for(material_instance)
|
|
|
|
|
* for(vbo)
|
|
|
|
|
* for(distance)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
int Comparator<RenderNodePointer>::compare(const RenderNodePointer &obj_one,const RenderNodePointer &obj_two) const
|
|
|
|
|
{
|
|
|
|
|
int off;
|
|
|
|
|
|
|
|
|
|
hgl::graph::RenderableInstance *ri_one=obj_one->ri;
|
|
|
|
|
hgl::graph::RenderableInstance *ri_two=obj_two->ri;
|
|
|
|
|
|
|
|
|
|
//比较管线
|
|
|
|
|
{
|
|
|
|
|
off=ri_one->GetPipeline()
|
|
|
|
|
-ri_two->GetPipeline();
|
|
|
|
|
|
|
|
|
|
if(off)
|
|
|
|
|
return off;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//比较材质实例
|
|
|
|
|
{
|
|
|
|
|
for(int i =(int)hgl::graph::DescriptorSetsType::BEGIN_RANGE;
|
|
|
|
|
i<=(int)hgl::graph::DescriptorSetsType::END_RANGE;
|
|
|
|
|
i++)
|
|
|
|
|
{
|
|
|
|
|
off=ri_one->GetMP((hgl::graph::DescriptorSetsType)i)
|
|
|
|
|
-ri_two->GetMP((hgl::graph::DescriptorSetsType)i);
|
|
|
|
|
|
|
|
|
|
if(off)
|
|
|
|
|
return off;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//比较vbo+ebo
|
|
|
|
|
{
|
|
|
|
|
off=ri_one->GetBufferHash()
|
|
|
|
|
-ri_two->GetBufferHash();
|
|
|
|
|
|
|
|
|
|
if(off)
|
|
|
|
|
return off;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//比较距离
|
|
|
|
|
{
|
|
|
|
|
const double dist=obj_one->distance_to_camera_square-
|
|
|
|
|
obj_two->distance_to_camera_square;
|
|
|
|
|
|
|
|
|
|
//由于距离差距可能会小于1,但又返回int,所以需要做如此处理
|
|
|
|
|
|
|
|
|
|
if(dist>0)return 1;else
|
|
|
|
|
if(dist<0)return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-05-25 00:50:04 +08:00
|
|
|
|
|
2019-05-21 21:28:33 +08:00
|
|
|
|
namespace hgl
|
|
|
|
|
{
|
|
|
|
|
namespace graph
|
|
|
|
|
{
|
2021-06-19 20:31:07 +08:00
|
|
|
|
RenderList::RenderList(GPUDevice *dev)
|
2020-11-26 17:51:59 +08:00
|
|
|
|
{
|
2021-06-19 20:31:07 +08:00
|
|
|
|
device =dev;
|
2020-11-26 17:51:59 +08:00
|
|
|
|
cmd_buf =nullptr;
|
2021-06-19 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
hgl_zero(camera_info);
|
2020-11-26 17:51:59 +08:00
|
|
|
|
|
2021-06-19 20:31:07 +08:00
|
|
|
|
mvp_array =new MVPArrayBuffer(device,VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT);
|
|
|
|
|
|
2021-06-15 15:36:30 +08:00
|
|
|
|
ubo_offset =0;
|
|
|
|
|
ubo_align =0;
|
2021-04-26 20:36:56 +08:00
|
|
|
|
|
2020-11-26 17:51:59 +08:00
|
|
|
|
last_pipeline =nullptr;
|
2021-06-16 20:29:25 +08:00
|
|
|
|
hgl_zero(last_mp);
|
2021-06-15 15:36:30 +08:00
|
|
|
|
last_vbo =0;
|
2020-11-26 17:51:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-19 20:31:07 +08:00
|
|
|
|
RenderList::~RenderList()
|
2020-11-26 17:51:59 +08:00
|
|
|
|
{
|
2021-06-19 20:31:07 +08:00
|
|
|
|
delete mvp_array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RenderList::Begin()
|
|
|
|
|
{
|
|
|
|
|
render_node_list.ClearData();
|
|
|
|
|
mvp_array->Clear();
|
|
|
|
|
ri_list.ClearData();
|
|
|
|
|
|
|
|
|
|
material_sets.ClearData();
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderList::End()
|
|
|
|
|
{
|
|
|
|
|
if(render_node_list.GetCount()<=0)return;
|
|
|
|
|
|
|
|
|
|
//排序
|
|
|
|
|
Sort(render_node_list,&render_node_comparator);
|
|
|
|
|
|
|
|
|
|
//产生MVP矩阵UBO数据
|
|
|
|
|
{
|
|
|
|
|
const uint32_t count=render_node_list.GetCount();
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
//按当前总节点数量分配UBO
|
|
|
|
|
mvp_array->Alloc(count);
|
|
|
|
|
mvp_array->Clear();
|
|
|
|
|
|
|
|
|
|
ri_list.ClearData();
|
|
|
|
|
ri_list.SetCount(count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
ubo_align=mvp_array->GetUnitSize();
|
|
|
|
|
|
|
|
|
|
char *mp=(char *)(mvp_array->Map(0,count));
|
|
|
|
|
RenderableInstance **ri=ri_list.GetData();
|
|
|
|
|
|
|
|
|
|
for(RenderNode *node:render_node_list) //未来可能要在Expend处考虑做去重
|
|
|
|
|
{
|
|
|
|
|
memcpy(mp,&(node->matrix),sizeof(MVPMatrix));
|
|
|
|
|
mp+=ubo_align;
|
|
|
|
|
|
|
|
|
|
(*ri)=node->ri;
|
|
|
|
|
++ri;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//为所有的材质绑定
|
|
|
|
|
for(Material *mtl:material_sets)
|
|
|
|
|
{
|
|
|
|
|
MaterialParameters *mp=mtl->GetMP(DescriptorSetsType::Renderable);
|
|
|
|
|
|
|
|
|
|
if(mp)
|
|
|
|
|
mp->BindUBO("r_scene_info",mvp_array->GetBuffer(),false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RenderList::Expend(SceneNode *sn)
|
|
|
|
|
{
|
|
|
|
|
if(!sn)return(false);
|
|
|
|
|
|
|
|
|
|
RenderableInstance *ri=sn->GetRI();
|
|
|
|
|
|
|
|
|
|
if(ri)
|
|
|
|
|
{
|
|
|
|
|
RenderNode *rn=new RenderNode;
|
|
|
|
|
|
|
|
|
|
rn->matrix.Set(sn->GetLocalToWorldMatrix(),camera_info.vp);
|
|
|
|
|
|
|
|
|
|
rn->WorldCenter=sn->GetWorldCenter();
|
|
|
|
|
|
|
|
|
|
rn->distance_to_camera_square=length_squared(rn->WorldCenter,camera_info.pos);
|
|
|
|
|
// rn->distance_to_camera=sqrtf(rn->distance_to_camera_square);
|
|
|
|
|
|
|
|
|
|
rn->ri=ri;
|
|
|
|
|
|
|
|
|
|
render_node_list.Add(rn);
|
|
|
|
|
|
|
|
|
|
material_sets.Add(ri->GetMaterial());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(SceneNode *sub:sn->SubNode)
|
|
|
|
|
Expend(sub);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RenderList::Expend(const CameraInfo &ci,SceneNode *sn)
|
|
|
|
|
{
|
|
|
|
|
if(!device|!sn)return(false);
|
|
|
|
|
|
|
|
|
|
camera_info=ci;
|
|
|
|
|
|
|
|
|
|
Begin();
|
|
|
|
|
Expend(sn);
|
|
|
|
|
End();
|
|
|
|
|
|
|
|
|
|
return(true);
|
2020-11-26 17:51:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 18:56:23 +08:00
|
|
|
|
void RenderList::Render(RenderableInstance *ri)
|
2019-05-21 21:28:33 +08:00
|
|
|
|
{
|
2019-05-25 00:50:04 +08:00
|
|
|
|
if(last_pipeline!=ri->GetPipeline())
|
|
|
|
|
{
|
|
|
|
|
last_pipeline=ri->GetPipeline();
|
|
|
|
|
|
2020-09-21 19:05:25 +08:00
|
|
|
|
cmd_buf->BindPipeline(last_pipeline);
|
2019-05-25 00:50:04 +08:00
|
|
|
|
}
|
2019-05-27 19:54:19 +08:00
|
|
|
|
|
2019-05-25 00:50:04 +08:00
|
|
|
|
{
|
2021-06-16 20:29:25 +08:00
|
|
|
|
int ds_count=0;
|
|
|
|
|
MaterialParameters *mp;
|
2019-05-29 21:48:56 +08:00
|
|
|
|
|
2021-06-16 20:29:25 +08:00
|
|
|
|
for(int i=(int)DescriptorSetsType::BEGIN_RANGE;
|
|
|
|
|
i<(int)DescriptorSetsType::Renderable;
|
|
|
|
|
i++)
|
2021-06-15 15:36:30 +08:00
|
|
|
|
{
|
2021-06-16 20:29:25 +08:00
|
|
|
|
mp=ri->GetMP((DescriptorSetsType)i);
|
|
|
|
|
|
|
|
|
|
if(last_mp[i]!=mp)
|
|
|
|
|
{
|
|
|
|
|
last_mp[i]=mp;
|
|
|
|
|
|
|
|
|
|
if(mp)
|
|
|
|
|
{
|
|
|
|
|
ds_list[ds_count]=mp->GetVkDescriptorSet();
|
|
|
|
|
++ds_count;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-15 15:36:30 +08:00
|
|
|
|
}
|
2019-05-29 21:48:56 +08:00
|
|
|
|
|
2021-06-16 20:29:25 +08:00
|
|
|
|
{
|
|
|
|
|
mp=ri->GetMP(DescriptorSetsType::Renderable);
|
|
|
|
|
|
|
|
|
|
if(mp)
|
|
|
|
|
{
|
|
|
|
|
ds_list[ds_count]=mp->GetVkDescriptorSet();
|
|
|
|
|
++ds_count;
|
2021-06-16 21:03:52 +08:00
|
|
|
|
|
|
|
|
|
cmd_buf->BindDescriptorSets(ri->GetPipelineLayout(),ds_list,ds_count,&ubo_offset,1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
cmd_buf->BindDescriptorSets(ri->GetPipelineLayout(),ds_list,ds_count,nullptr,0);
|
2021-06-16 20:29:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-16 21:03:52 +08:00
|
|
|
|
ubo_offset+=ubo_align;
|
|
|
|
|
}
|
2021-06-15 15:36:30 +08:00
|
|
|
|
}
|
2019-05-25 00:50:04 +08:00
|
|
|
|
|
2021-06-15 15:36:30 +08:00
|
|
|
|
if(last_vbo!=ri->GetBufferHash())
|
2019-05-25 00:50:04 +08:00
|
|
|
|
{
|
2021-06-15 15:36:30 +08:00
|
|
|
|
last_vbo=ri->GetBufferHash();
|
|
|
|
|
|
2020-09-21 19:05:25 +08:00
|
|
|
|
cmd_buf->BindVAB(ri);
|
2019-05-25 00:50:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 12:47:06 +08:00
|
|
|
|
const IndexBuffer *ib=ri->GetIndexBuffer();
|
2019-05-25 00:50:04 +08:00
|
|
|
|
|
|
|
|
|
if(ib)
|
|
|
|
|
{
|
|
|
|
|
cmd_buf->DrawIndexed(ib->GetCount());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-09-21 19:05:25 +08:00
|
|
|
|
cmd_buf->Draw(ri->GetDrawCount());
|
2019-05-25 00:50:04 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-09 15:37:00 +08:00
|
|
|
|
bool RenderList::Render(RenderCmdBuffer *cb)
|
2019-05-27 16:54:08 +08:00
|
|
|
|
{
|
|
|
|
|
if(!cb)
|
2019-05-21 21:28:33 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2021-06-19 20:31:07 +08:00
|
|
|
|
if(ri_list.GetCount()<=0)
|
2021-06-15 15:36:30 +08:00
|
|
|
|
return(true);
|
|
|
|
|
|
2019-05-27 16:54:08 +08:00
|
|
|
|
cmd_buf=cb;
|
|
|
|
|
|
2021-06-16 20:29:25 +08:00
|
|
|
|
last_pipeline=nullptr;
|
|
|
|
|
hgl_zero(last_mp);
|
2021-06-15 15:36:30 +08:00
|
|
|
|
last_vbo=0;
|
|
|
|
|
ubo_offset=0;
|
2019-05-25 00:50:04 +08:00
|
|
|
|
|
2021-06-19 20:31:07 +08:00
|
|
|
|
for(RenderableInstance *ri:ri_list)
|
2021-06-15 15:36:30 +08:00
|
|
|
|
Render(ri);
|
2019-05-21 21:28:33 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
}//namespace graph
|
|
|
|
|
}//namespace hgl
|