merged codes into RenderList they from SceneTreeToRenderList.

This commit is contained in:
hyzboy 2021-06-19 20:31:07 +08:00
parent 22e37512f6
commit 3bb74b99cc
6 changed files with 204 additions and 285 deletions

View File

@ -13,20 +13,31 @@ namespace hgl
{
namespace graph
{
using MVPArrayBuffer=GPUArrayBuffer<MVPMatrix>;
using MaterialSets=Sets<Material *>;
/**
* <br>
* mvp用UBO/SSBO等数据RenderCommandBuffer
*/
class RenderList
{
GPUDevice * device;
RenderCmdBuffer *cmd_buf;
private:
Camera *camera;
CameraInfo camera_info;
RenderNodeList render_node_list; ///<场景节点列表
MaterialSets material_sets; ///<材质合集
GPUBuffer *mvp_buffer;
List<RenderableInstance *> *ri_list;
RenderNodeComparator render_node_comparator;
private:
MVPArrayBuffer *mvp_array;
List<RenderableInstance *> ri_list;
VkDescriptorSet ds_list[(size_t)DescriptorSetsType::RANGE_SIZE];
DescriptorSets *renderable_desc_sets;
@ -34,6 +45,12 @@ namespace hgl
uint32_t ubo_offset;
uint32_t ubo_align;
protected:
virtual bool Begin();
virtual bool Expend(SceneNode *);
virtual void End();
private:
Pipeline * last_pipeline;
@ -50,10 +67,12 @@ namespace hgl
public:
RenderList();
virtual ~RenderList()=default;
RenderList(GPUDevice *);
virtual ~RenderList();
bool Render(RenderCmdBuffer *);
virtual bool Expend(const CameraInfo &,SceneNode *);
virtual bool Render(RenderCmdBuffer *);
};//class RenderList
}//namespace graph
}//namespace hgl

View File

@ -32,4 +32,8 @@ namespace hgl
using RenderNodeList=List<RenderNode *>;
}//namespace graph
}//namespace hgl
using RenderNodePointer=hgl::graph::RenderNode *;
using RenderNodeComparator=Comparator<RenderNodePointer>;
#endif//HGL_GRAPH_RENDER_NODE_INCLUDE

View File

@ -1,65 +0,0 @@
#ifndef HGL_GRAPH_SCENE_TREE_TO_RENDER_LIST_INCLUDE
#define HGL_GRAPH_SCENE_TREE_TO_RENDER_LIST_INCLUDE
#include<hgl/graph/RenderList.h>
#include<hgl/graph/VKPipeline.h>
#include<hgl/graph/VKMaterial.h>
#include<hgl/graph/VKMaterialParameters.h>
using RenderNodePointer=hgl::graph::RenderNode *;
using RenderNodeComparator=Comparator<RenderNodePointer>;
namespace hgl
{
namespace graph
{
using MVPArrayBuffer=GPUArrayBuffer<MVPMatrix>;
class SceneTreeToRenderList
{
//using PipelineSets =Sets<Pipeline *>;
using MaterialSets =Sets<Material *>;
//using MatInstSets =Sets<MaterialParameters *>;
protected:
GPUDevice * device;
protected:
CameraInfo camera_info; ///<相机信息
Frustum frustum;
protected:
RenderNodeComparator render_node_comparator;
RenderNodeList render_node_list; ///<场景节点列表
//PipelineSets pipeline_sets; ///<管线合集
MaterialSets material_sets; ///<材质合集
//MatInstSets mat_inst_sets; ///<材质实例合集
RenderListtwo_triangle测试这个
MVPArrayBuffer *mvp_array;
List<RenderableInstance *> ri_list;
RenderList * render_list;
protected:
virtual bool Begin();
virtual bool Expend(SceneNode *);
virtual void End();
public:
SceneTreeToRenderList(GPUDevice *d);
virtual ~SceneTreeToRenderList();
virtual bool Expend(RenderList *,const CameraInfo &,SceneNode *);
};//class SceneTreeToRenderList
}//namespace graph
}//namespace hgl
#endif//HGL_GRAPH_SCENE_TREE_TO_RENDER_LIST_INCLUDE

View File

@ -34,7 +34,6 @@ SET(SCENE_GRAPH_HEADER ${SG_INCLUDE_PATH}/Light.h
${SG_INCLUDE_PATH}/RenderNode.h
${SG_INCLUDE_PATH}/SceneOrient.h
${SG_INCLUDE_PATH}/RenderList.h
${SG_INCLUDE_PATH}/SceneTreeToRenderList.h
${SG_INCLUDE_PATH}/InlineGeometry.h
#${SG_INCLUDE_PATH}/Mesh.h
#${SG_INCLUDE_PATH}/Material.h
@ -45,7 +44,6 @@ SET(SCENE_GRAPH_SOURCE RenderList.cpp
SceneNode.cpp
SceneOrient.cpp
InlineGeometry.cpp
SceneTreeToRenderList.cpp
#InlinePipeline.cpp
#Material.cpp
#Mesh.cpp

View File

@ -7,18 +7,84 @@
#include<hgl/graph/VertexAttribDataAccess.h>
#include<hgl/graph/VKMaterialParameters.h>
#include<hgl/graph/VKRenderableInstance.h>
#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;
}
namespace hgl
{
namespace graph
{
RenderList::RenderList()
RenderList::RenderList(GPUDevice *dev)
{
device =dev;
cmd_buf =nullptr;
hgl_zero(camera_info);
mvp_buffer =nullptr;
ri_list =nullptr;
mvp_array =new MVPArrayBuffer(device,VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT);
ubo_offset =0;
ubo_align =0;
@ -27,11 +93,110 @@ namespace hgl
last_vbo =0;
}
void RenderList::Set(List<RenderableInstance *> *ril,GPUBuffer *buf,const uint32_t align)
RenderList::~RenderList()
{
ri_list=ril;
mvp_buffer=buf;
ubo_align=align;
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);
}
void RenderList::Render(RenderableInstance *ri)
@ -108,11 +273,7 @@ namespace hgl
if(!cb)
return(false);
if(!mvp_buffer
||!ri_list)
return(false);
if(ri_list->GetCount()<=0)
if(ri_list.GetCount()<=0)
return(true);
cmd_buf=cb;
@ -122,7 +283,7 @@ namespace hgl
last_vbo=0;
ubo_offset=0;
for(RenderableInstance *ri:*ri_list)
for(RenderableInstance *ri:ri_list)
Render(ri);
return(true);

View File

@ -1,198 +0,0 @@
#include<hgl/graph/SceneTreeToRenderList.h>
#include<hgl/graph/VKRenderableInstance.h>
#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;
}
namespace hgl
{
namespace graph
{
SceneTreeToRenderList::SceneTreeToRenderList(GPUDevice *d)
{
device=d;
hgl_zero(camera_info);
mvp_array =new MVPArrayBuffer(device,VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT);
render_list =nullptr;
}
SceneTreeToRenderList::~SceneTreeToRenderList()
{
delete mvp_array;
}
bool SceneTreeToRenderList::Begin()
{
render_node_list.ClearData();
mvp_array->Clear();
ri_list.ClearData();
//pipeline_sets.ClearData();
material_sets.ClearData();
//mat_inst_sets.ClearData();
return(true);
}
void SceneTreeToRenderList::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.SetCount(count);
}
{
const uint32_t unit_offset=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+=unit_offset;
(*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);
}
//写入RenderList
render_list->Set( &ri_list,
mvp_array->GetBuffer(),
mvp_array->GetUnitSize());
}
bool SceneTreeToRenderList::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 SceneTreeToRenderList::Expend(RenderList *rl,const CameraInfo &ci,SceneNode *sn)
{
if(!device)return(false);
if(!rl||!sn)return(false);
camera_info=ci;
//Frustum f;
render_list=rl;
Begin();
Expend(sn);
End();
return(true);
}
}//namespace graph
}//namespace hgl