Merge branch 'new_RenderList' of https://github.com/hyzboy/ULRE into new_RenderList

This commit is contained in:
HuYingzhuo(hugo/hyzboy) 2023-05-08 18:19:15 +08:00
commit 8e64816af8
11 changed files with 154 additions and 615 deletions

View File

@ -6,21 +6,21 @@
#include<hgl/filesystem/FileSystem.h>
#include<hgl/graph/VKRenderablePrimitiveCreater.h>
#include<hgl/graph/mtl/2d/VertexColor2D.h>
#include<hgl/graph/RenderList2D.h>
#include<hgl/graph/RenderList.h>
using namespace hgl;
using namespace hgl::graph;
constexpr uint32_t SCREEN_WIDTH=1280;
constexpr uint32_t SCREEN_HEIGHT=720;
constexpr uint32_t SCREEN_WIDTH=1024;
constexpr uint32_t SCREEN_HEIGHT=1024;
constexpr uint32_t VERTEX_COUNT=3;
constexpr float position_data[VERTEX_COUNT][2]=
constexpr float position_data[VERTEX_COUNT*2]=
{
{0.5, 0.25},
{0.25, 0.75},
{0.75, 0.75}
0.0, 0.0,
-0.1, 0.9,
0.1, 0.9
};
constexpr float color_data[VERTEX_COUNT][4]=
@ -34,7 +34,7 @@ class TestApp:public VulkanApplicationFramework
private:
SceneNode render_root;
RenderList2D * render_list =nullptr;
RenderList * render_list =nullptr;
MaterialInstance * material_instance =nullptr;
Renderable * render_obj =nullptr;
@ -48,7 +48,7 @@ private:
{
mtl::Material2DConfig cfg;
cfg.coordinate_system=mtl::CoordinateSystem2D::ZeroToOne;
cfg.coordinate_system=mtl::CoordinateSystem2D::NDC;
cfg.local_to_world=true;
AutoDelete<MaterialCreateInfo> mci=mtl::CreateVertexColor2D(&cfg);
@ -76,9 +76,9 @@ private:
if(!render_obj)
return(false);
render_root.CreateSubNode(translate(-0.25,0),render_obj);
render_root.CreateSubNode(translate( 0.25,0),render_obj);
for(uint i=0;i<12;i++)
render_root.CreateSubNode(rotate(deg2rad(30*i),Vector3f(0,0,1)),render_obj);
render_root.RefreshMatrix();
@ -99,7 +99,7 @@ public:
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
return(false);
render_list=new RenderList2D(device);
render_list=new RenderList(device);
if(!InitMaterial())
return(false);

View File

@ -19,7 +19,7 @@
#include<hgl/graph/VKMaterialInstance.h>
#include<hgl/graph/VKRenderTarget.h>
#include<hgl/graph/VKRenderResource.h>
#include<hgl/graph/RenderList2D.h>
#include<hgl/graph/RenderList.h>
#include<hgl/graph/mtl/StdMaterial.h>
#include<hgl/color/Color.h>
#include<hgl/Time.h>
@ -248,7 +248,7 @@ public:
return BuildCommandBuffer(sc_render_target->GetCurrentFrameIndices(),ri);
}
void BuildCommandBuffer(uint32_t index,RenderList2D *rl)
void BuildCommandBuffer(uint32_t index,RenderList *rl)
{
if(!rl)return;
@ -263,13 +263,13 @@ public:
cb->End();
}
void BuildCommandBuffer(RenderList2D *rl)
void BuildCommandBuffer(RenderList *rl)
{
for(int32_t i=0;i<swap_chain_count;i++)
BuildCommandBuffer(i,rl);
}
void BuildCurrentCommandBuffer(RenderList2D *rl)
void BuildCurrentCommandBuffer(RenderList *rl)
{
BuildCommandBuffer(sc_render_target->GetCurrentFrameIndices(),rl);
}

View File

@ -2,69 +2,41 @@
#define HGL_GRAPH_RENDER_LIST_INCLUDE
#include<hgl/graph/VK.h>
#include<hgl/graph/Camera.h>
#include<hgl/graph/SceneNode.h>
#include<hgl/graph/RenderNode.h>
#include<hgl/graph/VKArrayBuffer.h>
#include<hgl/graph/SceneInfo.h>
#include<hgl/color/Color4f.h>
#include<hgl/graph/VKMaterial.h>
namespace hgl
{
namespace graph
{
/**
* <br>
* RenderCommandBuffer
* 使使Clear()
*/
class RenderList
{
protected:
protected:
GPUDevice * device;
RenderCmdBuffer * cmd_buf;
private:
CameraInfo camera_info;
MaterialRenderMap mrl_map;
RenderNodeComparator *render_node_comparator;
private:
List<Renderable *> ri_list;
VkDescriptorSet ds_list[DESCRIPTOR_SET_TYPE_COUNT];
DescriptorSet *renderable_desc_sets;
uint32_t ubo_offset;
uint32_t ubo_align;
uint renderable_count; ///<可渲染对象数量
MaterialRenderMap mrl_map; ///<按材质分类的渲染列表
protected:
virtual bool Begin();
virtual bool Expend(SceneNode *);
virtual void End();
private:
Pipeline * last_pipeline;
MaterialParameters *last_mp[DESCRIPTOR_SET_TYPE_COUNT];
uint32_t last_vbo;
void Render(Renderable *);
virtual bool ExpendNode(SceneNode *);
public:
RenderList(GPUDevice *);
virtual ~RenderList();
virtual bool Expend(SceneNode *); ///<展开场景树到渲染列表
virtual bool Expend(const CameraInfo &,SceneNode *);
virtual bool Render(RenderCmdBuffer *); ///<渲染所有对象
virtual bool Render(RenderCmdBuffer *);
virtual void Clear(); ///<彻底清理
};//class RenderList
}//namespace graph
}//namespace hgl

View File

@ -1,43 +0,0 @@
#ifndef HGL_GRAPH_RENDER_LIST_2D_INCLUDE
#define HGL_GRAPH_RENDER_LIST_2D_INCLUDE
#include<hgl/graph/VK.h>
#include<hgl/graph/SceneNode.h>
#include<hgl/graph/RenderNode2D.h>
#include<hgl/graph/VKArrayBuffer.h>
#include<hgl/graph/VKMaterial.h>
namespace hgl
{
namespace graph
{
/**
* <br>
* 使使Clear()
*/
class RenderList2D
{
protected:
GPUDevice * device;
uint renderable_count; ///<可渲染对象数量
MaterialRenderMap2D mrl_map; ///<按材质分类的渲染列表
protected:
virtual bool ExpendNode(SceneNode *);
public:
RenderList2D(GPUDevice *);
virtual ~RenderList2D();
virtual bool Expend(SceneNode *); ///<展开场景树到渲染列表
virtual bool Render(RenderCmdBuffer *); ///<渲染所有对象
virtual void Clear(); ///<彻底清理
};//class RenderList2D
}//namespace graph
}//namespace hgl
#endif//HGL_GRAPH_RENDER_LIST_2D_INCLUDE

View File

@ -1,53 +1,125 @@
#ifndef HGL_GRAPH_RENDER_NODE_INCLUDE
#define HGL_GRAPH_RENDER_NODE_INCLUDE
#include<hgl/math/Vector.h>
#include<hgl/type/List.h>
#include<hgl/graph/SceneInfo.h>
#include<hgl/math/Math.h>
#include<hgl/type/Map.h>
#include<hgl/graph/VK.h>
namespace hgl
{
namespace graph
{
class Renderable;
class Material;
class GPUDevice;
struct VertexInputData;
struct IndexBufferData;
struct RenderNode
{
Renderable *render_obj;
Matrix4f local_to_world;
Renderable *ri;
};
using RenderNodeList=List<RenderNode *>;
using RenderNodeList=List<RenderNode>;
struct RenderNode2D:public RenderNode
struct RenderNodeExtraBuffer;
/**
*
*/
class MaterialRenderList
{
Matrix3x4f local_to_world;
GPUDevice *device;
RenderCmdBuffer *cmd_buf;
Material *mtl;
RenderNodeList rn_list;
private:
RenderNodeExtraBuffer *extra_buffer;
struct RenderItem
{
uint32_t first;
uint32_t count;
Pipeline * pipeline;
MaterialInstance * mi;
const VertexInputData * vid;
public:
void Set(Renderable *);
};
List<RenderItem> ri_list;
uint ri_count;
void Stat();
protected:
uint32_t binding_count;
VkBuffer *buffer_list;
VkDeviceSize *buffer_offset;
MaterialInstance * last_mi;
Pipeline * last_pipeline;
const VertexInputData * last_vid;
uint last_index;
void Bind(MaterialInstance *);
bool Bind(const VertexInputData *,const uint);
void Render(RenderItem *);
public:
MaterialRenderList(GPUDevice *d,Material *m);
~MaterialRenderList();
void Add(Renderable *ri,const Matrix4f &mat);
void ClearData()
{
rn_list.ClearData();
}
void End();
void Render(RenderCmdBuffer *);
};
using RenderNode2DList=List<RenderNode2D *>;
constexpr double RenderNode3DDistanceFactor=100.0;
struct RenderNode3D:public RenderNode
class MaterialRenderMap:public ObjectMap<Material *,MaterialRenderList>
{
MVPMatrix matrix;
public:
Vector3f WorldCenter;
MaterialRenderMap()=default;
virtual ~MaterialRenderMap()=default;
double distance_to_camera_square;
void Begin()
{
for(auto *it:data_list)
it->value->ClearData();
}
double distance_to_camera;
void End()
{
for(auto *it:data_list)
it->value->End();
}
};//struct RenderNode
void Render(RenderCmdBuffer *rcb)
{
if(!rcb)return;
using RenderNode3DList=List<RenderNode3D *>;
for(auto *it:data_list)
it->value->Render(rcb);
}
};
}//namespace graph
}//namespace hgl
using RenderNodePointer=hgl::graph::RenderNode *;
using RenderNodeComparator=Comparator<RenderNodePointer>;
using RenderNode2DPointer=hgl::graph::RenderNode2D *;
using RenderNode2DComparator=Comparator<RenderNode2DPointer>;
using RenderNode3DPointer=hgl::graph::RenderNode3D *;
using RenderNode3DComparator=Comparator<RenderNode3DPointer>;
#endif//HGL_GRAPH_RENDER_NODE_INCLUDE

View File

@ -1,125 +0,0 @@
#ifndef HGL_GRAPH_RENDER_NODE_2D_INCLUDE
#define HGL_GRAPH_RENDER_NODE_2D_INCLUDE
#include<hgl/math/Math.h>
#include<hgl/type/Map.h>
#include<hgl/graph/VK.h>
namespace hgl
{
namespace graph
{
class Renderable;
class Material;
class GPUDevice;
struct VertexInputData;
struct IndexBufferData;
struct RenderNode2D
{
Matrix4f local_to_world;
Renderable *ri;
};
using RenderNode2DList=List<RenderNode2D>;
struct RenderNode2DExtraBuffer;
/**
*
*/
class MaterialRenderList2D
{
GPUDevice *device;
RenderCmdBuffer *cmd_buf;
Material *mtl;
RenderNode2DList rn_list;
private:
RenderNode2DExtraBuffer *extra_buffer;
struct RenderItem
{
uint32_t first;
uint32_t count;
Pipeline * pipeline;
MaterialInstance * mi;
const VertexInputData * vid;
public:
void Set(Renderable *);
};
List<RenderItem> ri_list;
uint ri_count;
void Stat();
protected:
uint32_t binding_count;
VkBuffer *buffer_list;
VkDeviceSize *buffer_offset;
MaterialInstance * last_mi;
Pipeline * last_pipeline;
const VertexInputData * last_vid;
uint last_index;
void Bind(MaterialInstance *);
bool Bind(const VertexInputData *,const uint);
void Render(RenderItem *);
public:
MaterialRenderList2D(GPUDevice *d,Material *m);
~MaterialRenderList2D();
void Add(Renderable *ri,const Matrix4f &mat);
void ClearData()
{
rn_list.ClearData();
}
void End();
void Render(RenderCmdBuffer *);
};
class MaterialRenderMap2D:public ObjectMap<Material *,MaterialRenderList2D>
{
public:
MaterialRenderMap2D()=default;
virtual ~MaterialRenderMap2D()=default;
void Begin()
{
for(auto *it:data_list)
it->value->ClearData();
}
void End()
{
for(auto *it:data_list)
it->value->End();
}
void Render(RenderCmdBuffer *rcb)
{
if(!rcb)return;
for(auto *it:data_list)
it->value->Render(rcb);
}
};
}//namespace graph
}//namespace hgl
#endif//HGL_GRAPH_RENDER_NODE_2D_INCLUDE

View File

@ -35,17 +35,14 @@ source_group("Light" FILES ${LIGHT_FILES})
SET(SCENE_GRAPH_HEADER ${SG_INCLUDE_PATH}/SceneInfo.h
${SG_INCLUDE_PATH}/SceneNode.h
${SG_INCLUDE_PATH}/RenderNode2D.h
${SG_INCLUDE_PATH}/RenderNode.h
${SG_INCLUDE_PATH}/SceneOrient.h
#${SG_INCLUDE_PATH}/RenderList.h
${SG_INCLUDE_PATH}/RenderList2D.h
${SG_INCLUDE_PATH}/RenderList.h
)
SET(SCENE_GRAPH_SOURCE
#RenderList.cpp
RenderNode2D.cpp
RenderList2D.cpp
#RenderList3D.cpp
RenderList.cpp
RenderNode.cpp
SceneNode.cpp
SceneOrient.cpp)

View File

@ -1,5 +1,4 @@
#include<hgl/graph/RenderList.h>
#include<hgl/graph/Camera.h>
#include<hgl/graph/SceneNode.h>
#include<hgl/graph/VKBuffer.h>
#include<hgl/graph/VKPrimitive.h>
@ -9,69 +8,6 @@
#include<hgl/graph/VKRenderable.h>
#include<hgl/util/sort/Sort.h>
/**
*
*
* for(pipeline)
* for(material_instance)
* for(vbo)
* for(distance)
*/
template<>
int Comparator<RenderNode3DPointer>::compare(const RenderNode3DPointer &obj_one,const RenderNode3DPointer &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;
}
//比较材质实例
{
for(int i =(int)hgl::graph::DescriptorSetType::BEGIN_RANGE;
i<=(int)hgl::graph::DescriptorSetType::END_RANGE;
i++)
{
off=ri_one->GetMP((hgl::graph::DescriptorSetType)i)
-ri_two->GetMP((hgl::graph::DescriptorSetType)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
@ -79,83 +15,14 @@ namespace hgl
RenderList::RenderList(GPUDevice *dev)
{
device =dev;
cmd_buf =nullptr;
ubo_offset =0;
ubo_align =0;
last_pipeline =nullptr;
hgl_zero(last_mp);
last_vbo =0;
renderable_count=0;
}
RenderList::~RenderList()
{
}
bool RenderList::Begin()
{
render_node_list.ClearData();
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->GetAlignSize();
char *mp=(char *)(mvp_array->Map(0,count));
Renderable **ri=ri_list.GetData();
for(RenderNode *node:render_node_list) //未来可能要在Expend处考虑做去重
{
memcpy(mp,&(node->matrix),MVPMatrixBytes);
mp+=ubo_align;
(*ri)=node->ri;
++ri;
}
mvp_array->Flush(count);
}
}
//为所有的材质绑定
for(Material *mtl:material_sets)
{
MaterialParameters *mp=mtl->GetMP(DescriptorSetType::PerObject);
if(mp)
{
if(mp->BindUBO("r_scene_info",mvp_array->GetBuffer(),true))
mp->Update();
}
}
}
bool RenderList::Expend(SceneNode *sn)
bool RenderList::ExpendNode(SceneNode *sn)
{
if(!sn)return(false);
@ -163,135 +30,54 @@ namespace hgl
if(ri)
{
RenderNode *rn=new RenderNode;
Material *mtl=ri->GetMaterial();
MaterialRenderList *mrl;
rn->matrix.Set(sn->GetLocalToWorldMatrix(),camera_info.vp,camera_info.view);
if(!mrl_map.Get(mtl,mrl))
{
mrl=new MaterialRenderList(device,mtl);
rn->WorldCenter=sn->GetWorldCenter();
mrl_map.Add(mtl,mrl);
}
mrl->Add(ri,sn->GetLocalToWorldMatrix());
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());
++renderable_count;
}
for(SceneNode *sub:sn->SubNode)
Expend(sub);
ExpendNode(sub);
return(true);
}
bool RenderList::Expend(const CameraInfo &ci,SceneNode *sn)
bool RenderList::Expend(SceneNode *sn)
{
if(!device|!sn)return(false);
camera_info=ci;
Begin();
Expend(sn);
End();
mrl_map.Begin();
ExpendNode(sn);
mrl_map.End();
return(true);
}
void RenderList::Render(Renderable *ri)
{
if(last_pipeline!=ri->GetPipeline())
{
last_pipeline=ri->GetPipeline();
cmd_buf->BindPipeline(last_pipeline);
}
{
uint32_t ds_count=0;
uint32_t first_set=0;
MaterialParameters *mp;
ENUM_CLASS_FOR(DescriptorSetType,int,i)
{
if(i==(int)DescriptorSetType::PerObject)continue;
mp=ri->GetMP((DescriptorSetType)i);
if(last_mp[i]!=mp)
{
last_mp[i]=mp;
if(mp)
{
ds_list[ds_count]=mp->GetVkDescriptorSet();
++ds_count;
}
}
else
{
if(mp)
++first_set;
}
}
{
mp=ri->GetMP(DescriptorSetType::PerObject);
if(mp)
{
ds_list[ds_count]=mp->GetVkDescriptorSet();
++ds_count;
cmd_buf->BindDescriptorSets(ri->GetPipelineLayout(),first_set,ds_list,ds_count,&ubo_offset,1);
}
else
{
cmd_buf->BindDescriptorSets(ri->GetPipelineLayout(),first_set,ds_list,ds_count,nullptr,0);
}
ubo_offset+=ubo_align;
}
}
if(last_vbo!=ri->GetBufferHash())
{
last_vbo=ri->GetBufferHash();
cmd_buf->BindVBO(ri);
}
const IndexBuffer *ib=ri->GetIndexBuffer();
if(ib)
{
cmd_buf->DrawIndexed(ib->GetCount());
}
else
{
cmd_buf->Draw(ri->GetDrawCount());
}
}
bool RenderList::Render(RenderCmdBuffer *cb)
{
if(!cb)
return(false);
if(ri_list.GetCount()<=0)
if(renderable_count<=0)
return(true);
cmd_buf=cb;
last_pipeline=nullptr;
hgl_zero(last_mp);
last_vbo=0;
ubo_offset=0;
for(Renderable *ri:ri_list)
Render(ri);
mrl_map.Render(cb);
return(true);
}
void RenderList::Clear()
{
mrl_map.Clear();
}
}//namespace graph
}//namespace hgl

View File

@ -1,83 +0,0 @@
#include<hgl/graph/RenderList2D.h>
#include<hgl/graph/SceneNode.h>
#include<hgl/graph/VKBuffer.h>
#include<hgl/graph/VKPrimitive.h>
#include<hgl/graph/VKCommandBuffer.h>
#include<hgl/graph/VertexAttribDataAccess.h>
#include<hgl/graph/VKMaterialParameters.h>
#include<hgl/graph/VKRenderable.h>
#include<hgl/util/sort/Sort.h>
namespace hgl
{
namespace graph
{
RenderList2D::RenderList2D(GPUDevice *dev)
{
device =dev;
renderable_count=0;
}
RenderList2D::~RenderList2D()
{
}
bool RenderList2D::ExpendNode(SceneNode *sn)
{
if(!sn)return(false);
Renderable *ri=sn->GetRenderable();
if(ri)
{
Material *mtl=ri->GetMaterial();
MaterialRenderList2D *mrl;
if(!mrl_map.Get(mtl,mrl))
{
mrl=new MaterialRenderList2D(device,mtl);
mrl_map.Add(mtl,mrl);
}
mrl->Add(ri,sn->GetLocalToWorldMatrix());
++renderable_count;
}
for(SceneNode *sub:sn->SubNode)
ExpendNode(sub);
return(true);
}
bool RenderList2D::Expend(SceneNode *sn)
{
if(!device|!sn)return(false);
mrl_map.Begin();
ExpendNode(sn);
mrl_map.End();
return(true);
}
bool RenderList2D::Render(RenderCmdBuffer *cb)
{
if(!cb)
return(false);
if(renderable_count<=0)
return(true);
mrl_map.Render(cb);
return(true);
}
void RenderList2D::Clear()
{
mrl_map.Clear();
}
}//namespace graph
}//namespace hgl

View File

@ -1,37 +0,0 @@
#include<hgl/graph/RenderList.h>
namespace hgl
{
namespace graph
{
RenderList3D::RenderList3D()
{
hgl_zero(camera_info);
mvp_array =new GPUArrayBuffer(device,VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,MVPMatrixBytes);
}
RenderList3D::~RenderList3D()
{
delete mvp_array;
}
bool RenderList3D::Begin()
{
if(!RenderList::Begin())
return(false);
mvp_array->Clear();
return(true);
}
bool RenderList3D::Expend(SceneNode *)
{
}
void RenderList3D::End()
{
}
}//namespace graph
}//namespace hgl

View File

@ -298,7 +298,7 @@ namespace hgl
hgl_cpy(buffer_list+count,extra_buffer->l2w_buffer,4);
for(uint i=0;i<4;i++)
buffer_offset[count+i]=first*16; //mat3x4f每列都是rgba32f自然是16字节
buffer_offset[count+i]=first*16; //mat4每列都是rgba32f自然是16字节
count+=l2w_binding_count;
}