ULRE/src/SceneGraph/RenderList.cpp

226 lines
5.8 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/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
{
2020-12-18 16:52:45 +08:00
constexpr uint32_t L2WItemBytes=sizeof(Matrix4f); ///<单个L2W数据字节数
struct L2WArrays
{
uint alloc_count;
uint count;
List<Matrix4f> items;
GPUBuffer *buffer;
Matrix4f *buffer_address;
List<uint32_t> offset;
public:
L2WArrays()
{
alloc_count=0;
count=0;
buffer=nullptr;
buffer_address=nullptr;
}
~L2WArrays()
{
if(buffer)
{
buffer->Unmap();
delete buffer;
}
}
void Init(const uint32_t c)
{
count=c;
items.SetCount(count);
offset.SetCount(count);
}
};//
2019-05-24 21:43:59 +08:00
float CameraLengthComp(Camera *cam,SceneNode *obj_one,SceneNode *obj_two)
2019-05-21 21:28:33 +08:00
{
if(!cam||!obj_one||!obj_two)
return(0);
2020-11-17 17:20:59 +08:00
return( length_squared(obj_one->GetCenter(),cam->pos)-
length_squared(obj_two->GetCenter(),cam->pos));
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
LocalToWorld=new L2WArrays;
}
RenderList::~RenderList()
{
delete LocalToWorld;
}
void RenderList::Begin()
{
scene_node_list.ClearData();
last_pipeline =nullptr;
last_ri =nullptr;
}
void RenderList::Add(SceneNode *node)
{
if(!node)return;
scene_node_list.Add(node);
}
void RenderList::End()
{
2020-12-18 16:52:45 +08:00
//清0计数器
uint32_t l2w_count=0; //local to world矩阵总数量
//统计Render
const uint32_t count=scene_node_list.GetCount();
2020-12-18 16:52:45 +08:00
LocalToWorld->Init(count);
Matrix4f *mp=LocalToWorld->items.GetData();
uint32_t *op=LocalToWorld->offset.GetData();
for(SceneNode *node:scene_node_list)
{
2020-12-18 16:52:45 +08:00
const Matrix4f &mat=node->GetLocalToWorldMatrix();
if(!mat.IsIdentity())
{
hgl_cpy(mp,&mat);
++mp;
*op=(l2w_count)*L2WItemBytes;
++l2w_count;
}
else
{
*op=l2w_count;
}
++op;
}
2020-12-18 16:52:45 +08:00
if(LocalToWorld->buffer)
{
if(LocalToWorld->alloc_count<l2w_count)
{
LocalToWorld->buffer->Unmap();
delete LocalToWorld->buffer;
LocalToWorld->buffer=nullptr;
// LocalToWorld->buffer_address=nullptr; //下面一定会重写,所以这一行没必要操作
}
}
if(!LocalToWorld->buffer)
{
LocalToWorld->alloc_count=power_to_2(l2w_count);
LocalToWorld->buffer=device->CreateUBO(LocalToWorld->alloc_count*L2WItemBytes);
LocalToWorld->buffer_address=(Matrix4f *)(LocalToWorld->buffer->Map());
}
hgl_cpy(LocalToWorld->buffer_address,LocalToWorld->items.GetData(),l2w_count);
}
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