ULRE/src/SceneGraph/RenderList.cpp

169 lines
4.1 KiB
C++
Raw Normal View History

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>
2020-12-18 16:52:45 +08:00
#include<hgl/graph/VKDevice.h>
#include<hgl/graph/VKBuffer.h>
#include<hgl/graph/VKRenderable.h>
#include<hgl/graph/VKCommandBuffer.h>
#include<hgl/graph/VertexAttribDataAccess.h>
#include<hgl/graph/VKMaterialInstance.h>
#include<hgl/graph/VKRenderableInstance.h>
2019-05-25 00:50:04 +08:00
2019-05-21 21:28:33 +08:00
namespace hgl
{
namespace graph
{
constexpr size_t MVPMatrixBytes=sizeof(MVPMatrix);
2020-12-18 16:52:45 +08:00
2019-05-21 21:28:33 +08:00
2019-05-24 21:43:59 +08:00
//bool FrustumClipFilter(const SceneNode *node,void *fc)
//{
// if(!node||!fc)return(false);
2019-05-21 21:28:33 +08:00
2019-05-24 21:43:59 +08:00
// return (((Frustum *)fc)->BoxIn(node->GetWorldBoundingBox())!=Frustum::OUTSIDE);
//}
2019-05-21 21:28:33 +08:00
2020-12-18 16:52:45 +08:00
RenderList::RenderList(GPUDevice *gd)
{
2020-12-18 16:52:45 +08:00
device =gd;
cmd_buf =nullptr;
last_pipeline =nullptr;
last_ri =nullptr;
2020-12-18 16:52:45 +08:00
mvp_array =new MVPArrayBuffer(gd,VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT);
2020-12-18 16:52:45 +08:00
}
RenderList::~RenderList()
{
delete mvp_array;
}
void RenderList::Begin()
{
scene_node_list.ClearData();
mvp_array->Clear();
mvp_offset.ClearData();
last_pipeline =nullptr;
last_ri =nullptr;
}
void RenderList::Add(SceneNode *node)
{
if(!node)return;
scene_node_list.Add(node);
}
2021-05-08 18:14:44 +08:00
void RenderList::End(CameraInfo *camera_matrix)
{
2020-12-18 16:52:45 +08:00
//清0计数器
uint32_t mvp_count=0; //local to world矩阵总数量
2020-12-18 16:52:45 +08:00
//统计Render
const uint32_t count=scene_node_list.GetCount();
mvp_array->Alloc(count);
mvp_array->Clear();
2020-12-18 16:52:45 +08:00
mvp_offset.PreMalloc(count);
mvp_offset.ClearData();
MVPMatrix *mp=mvp_array->Map(0,count);
uint32_t *op=mvp_offset.GetData();
2020-12-18 16:52:45 +08:00
for(SceneNode *node:scene_node_list)
{
const Matrix4f &l2w=node->GetLocalToWorldMatrix();
2020-12-18 16:52:45 +08:00
if(!l2w.IsIdentity())
2020-12-18 16:52:45 +08:00
{
mp->Set(l2w,camera_matrix->vp);
2020-12-18 16:52:45 +08:00
++mp;
*op=mvp_count*MVPMatrixBytes;
++mvp_count;
2020-12-18 16:52:45 +08:00
}
else
{
*op=mvp_count*MVPMatrixBytes;
2020-12-18 16:52:45 +08:00
}
++op;
}
}
2020-10-21 12:47:06 +08:00
void RenderList::Render(SceneNode *node,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();
cmd_buf->BindPipeline(last_pipeline);
2019-05-25 00:50:04 +08:00
}
2019-05-25 00:50:04 +08:00
{
2020-12-18 16:52:45 +08:00
MaterialInstance *mi=ri->GetMaterialInstance();
2020-12-18 16:52:45 +08:00
cmd_buf->BindDescriptorSets(mi->GetDescriptorSets());
}
2019-05-25 00:50:04 +08:00
//更新fin_mvp
if(ri!=last_ri)
2019-05-25 00:50:04 +08:00
{
cmd_buf->BindVAB(ri);
2019-05-25 00:50:04 +08:00
last_ri=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
{
cmd_buf->Draw(ri->GetDrawCount());
2019-05-25 00:50:04 +08:00
}
}
2020-10-21 12:47:06 +08:00
void RenderList::Render(SceneNode *node,List<RenderableInstance *> &ri_list)
2019-05-25 00:50:04 +08:00
{
const int count=ri_list.GetCount();
2020-10-21 12:47:06 +08:00
RenderableInstance **ri=ri_list.GetData();
for(int i=0;i<count;i++)
{
Render(node,*ri);
++ri;
}
}
bool RenderList::Render(RenderCmdBuffer *cb)
{
if(!cb)
2019-05-21 21:28:33 +08:00
return(false);
cmd_buf=cb;
2019-05-25 00:50:04 +08:00
last_pipeline=nullptr;
last_ri=nullptr;
2019-05-25 00:50:04 +08:00
const int count=scene_node_list.GetCount();
SceneNode **node=scene_node_list.GetData();
2019-05-21 21:28:33 +08:00
for(int i=0;i<count;i++)
{
Render(*node,(*node)->renderable_instances);
++node;
2019-05-21 21:28:33 +08:00
}
return(true);
}
}//namespace graph
}//namespace hgl