refactor RenderList and SceneTreeToRenderList

This commit is contained in:
hyzboy 2021-06-15 15:36:30 +08:00
parent e26f0b5698
commit 983202966e
3 changed files with 50 additions and 91 deletions

View File

@ -13,10 +13,6 @@ namespace hgl
{ {
namespace graph namespace graph
{ {
// using SceneNodeList=List<SceneNode *>;
using MVPArrayBuffer=GPUArrayBuffer<MVPMatrix>;
using MVPOffsetBuffer=List<uint32_t>;
/** /**
* <br> * <br>
* mvp用UBO/SSBO等数据RenderCommandBuffer * mvp用UBO/SSBO等数据RenderCommandBuffer
@ -30,15 +26,19 @@ namespace hgl
Camera *camera; Camera *camera;
RenderNodeList render_node_list; GPUBuffer *mvp_buffer;
List<RenderableInstance *> *ri_list;
MVPArrayBuffer *mvp_array; DescriptorSets *renderable_desc_sets;
MVPOffsetBuffer mvp_offset;
uint32_t ubo_offset;
uint32_t ubo_align;
private: private:
Pipeline * last_pipeline; Pipeline * last_pipeline;
RenderableInstance *last_ri; MaterialInstance * last_mi;
uint32_t last_vbo;
void Render(RenderableInstance *); void Render(RenderableInstance *);
@ -48,14 +48,14 @@ namespace hgl
RenderList(GPUDevice *); RenderList(GPUDevice *);
friend class SceneTreeToRenderList;
void Set(List<RenderableInstance *> *,GPUBuffer *,const uint32_t);
public: public:
virtual ~RenderList(); virtual ~RenderList()=default;
void Begin ();
void Add (SceneNode *);
void End (CameraInfo *);
bool Render (RenderCmdBuffer *); bool Render (RenderCmdBuffer *);
};//class RenderList };//class RenderList
}//namespace graph }//namespace graph

View File

@ -13,85 +13,27 @@ namespace hgl
{ {
namespace graph namespace graph
{ {
constexpr size_t MVPMatrixBytes=sizeof(MVPMatrix);
//bool FrustumClipFilter(const SceneNode *node,void *fc)
//{
// if(!node||!fc)return(false);
// return (((Frustum *)fc)->BoxIn(node->GetWorldBoundingBox())!=Frustum::OUTSIDE);
//}
RenderList::RenderList(GPUDevice *gd) RenderList::RenderList(GPUDevice *gd)
{ {
device =gd; device =gd;
cmd_buf =nullptr; cmd_buf =nullptr;
last_pipeline =nullptr; mvp_buffer =nullptr;
last_ri =nullptr; ri_list =nullptr;
mvp_array =new MVPArrayBuffer(gd,VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT); ubo_offset =0;
} ubo_align =0;
RenderList::~RenderList()
{
delete mvp_array;
}
void RenderList::Begin()
{
scene_node_list.ClearData();
mvp_array->Clear();
mvp_offset.ClearData();
last_pipeline =nullptr; last_pipeline =nullptr;
last_ri =nullptr; last_mi =nullptr;
last_vbo =0;
} }
void RenderList::Add(SceneNode *node) void RenderList::Set(List<RenderableInstance *> *ril,GPUBuffer *buf,const uint32_t align)
{ {
if(!node)return; ri_list=ril;
mvp_buffer=buf;
scene_node_list.Add(node); ubo_align=align;
}
void RenderList::End(CameraInfo *camera_info)
{
//清0计数器
uint32_t mvp_count=0; //local to world矩阵总数量
//统计Render
const uint32_t count=scene_node_list.GetCount();
mvp_array->Alloc(count);
mvp_array->Clear();
mvp_offset.PreMalloc(count);
mvp_offset.ClearData();
MVPMatrix *mp=mvp_array->Map(0,count);
uint32_t *op=mvp_offset.GetData();
for(SceneNode *node:scene_node_list)
{
const Matrix4f &l2w=node->GetLocalToWorldMatrix();
if(!l2w.IsIdentity())
{
mp->Set(l2w,camera_info->vp);
++mp;
*op=mvp_count*MVPMatrixBytes;
++mvp_count;
}
else
{
*op=mvp_count*MVPMatrixBytes;
}
++op;
}
} }
void RenderList::Render(RenderableInstance *ri) void RenderList::Render(RenderableInstance *ri)
@ -106,16 +48,22 @@ namespace hgl
{ {
MaterialInstance *mi=ri->GetMaterialInstance(); MaterialInstance *mi=ri->GetMaterialInstance();
cmd_buf->BindDescriptorSets(mi->GetDescriptorSets()); if(mi!=last_mi)
{
last_mi=mi;
cmd_buf->BindDescriptorSets(mi->GetDescriptorSets());
}
} }
//更新fin_mvp
if(ri!=last_ri)
{ {
cmd_buf->BindVAB(ri);
}
last_ri=ri; if(last_vbo!=ri->GetBufferHash())
{
last_vbo=ri->GetBufferHash();
cmd_buf->BindVAB(ri);
} }
const IndexBuffer *ib=ri->GetIndexBuffer(); const IndexBuffer *ib=ri->GetIndexBuffer();
@ -135,13 +83,22 @@ namespace hgl
if(!cb) if(!cb)
return(false); return(false);
if(!mvp_buffer
||!ri_list)
return(false);
if(ri_list->GetCount()<=0)
return(true);
cmd_buf=cb; cmd_buf=cb;
last_pipeline=nullptr; last_pipeline=nullptr;
last_ri=nullptr; last_mi=nullptr;
last_vbo=0;
ubo_offset=0;
for(SceneNode *sn:scene_node_list) for(RenderableInstance *ri:*ri_list)
Render(sn->render_obj); Render(ri);
return(true); return(true);
} }

View File

@ -130,7 +130,9 @@ namespace hgl
} }
//写入RenderList //写入RenderList
render_list->Set(mvp_array->GetBuffer(),&ri_list); render_list->Set( &ri_list,
mvp_array->GetBuffer(),
mvp_array->GetUnitSize());
} }
bool SceneTreeToRenderList::Expend(SceneNode *sn) bool SceneTreeToRenderList::Expend(SceneNode *sn)