add SceneGraph/SceneTreeToRenderList.cpp, GPUDataArray.h, SceneRoot.h

This commit is contained in:
hyzboy 2021-01-29 20:50:47 +08:00
parent ee35354d43
commit 07c761eab9
9 changed files with 205 additions and 70 deletions

2
CMCore

@ -1 +1 @@
Subproject commit d065f24770fae3132012f11312495b3dc5ff3892 Subproject commit 74f7f146ba6b79fd904f6fd47f4dbee0750831f6

@ -1 +1 @@
Subproject commit 275dce7c49fd0d1b5ab61a51d18d356ddd2526d1 Subproject commit fd6fefe93ebf844945c6366d6927447549727bf1

View File

@ -0,0 +1,77 @@
#ifndef HGL_GRAPH_GPU_DATA_ARRAY_INCLUDE
#define HGL_GRAPH_GPU_DATA_ARRAY_INCLUDE
#include<hgl/graph/VKBuffer.h>
namespace hgl
{
namespace graph
{
/**
* GPU数据阵列
**/
class GPUDataArray
{
uint32_t alloc_count;
uint32_t count;
uint32_t item_size;
GPUBuffer * buf_gpu;
uint8 * buf_cpu;
uint32_t * offset;
public:
GPUDataArray(const uint32_t s=0)
{
alloc_count=0;
count=0;
item_size=s;
buf_gpu=nullptr;
buf_cpu=nullptr;
offset=nullptr;
}
virtual ~GPUDataArray()
{
Clear();
}
virtual void Clear()
{
alloc_count=0;
count=0;
if(buf_gpu)
{
buf_gpu->Unmap();
delete buf_gpu;
buf_gpu=nullptr;
}
SAFE_CLEAR_ARRAY(buf_cpu);
SAFE_CLEAR_ARRAY(offset);
}
bool Init(const uint32_t c,const uint32_t s=0)
{
if(s
&&item_size<=0)
{
if(s>HGL_SIZE_1KB*64)
return(false);
item_size=s;
}
count=c;
if(count*item_size<=0)
return(false);
buf_cpu=new
}
};//class GPUDataArray
}//namespace graph
}//namespace hgl
#endif//HGL_GRAPH_GPU_DATA_ARRAY_INCLUDE

View File

@ -9,7 +9,7 @@ namespace hgl
{ {
namespace graph namespace graph
{ {
struct L2WArrays; struct MVPArray;
class RenderList class RenderList
{ {
@ -18,7 +18,9 @@ namespace hgl
private: private:
L2WArrays *LocalToWorld; Camera *camera;
MVPArray *mvp_array;
List<SceneNode *> scene_node_list; List<SceneNode *> scene_node_list;
@ -40,10 +42,38 @@ namespace hgl
void Begin (); void Begin ();
void Add (SceneNode *); void Add (SceneNode *);
void End (); void End (CameraMatrix *);
bool Render (RenderCmdBuffer *); bool Render (RenderCmdBuffer *);
};//class RenderList };//class RenderList
class SceneTreeToRenderList
{
GPUDevice *device;
public:
Camera * camera;
CameraMatrix * camera_matrix;
Frustum frustum;
public:
virtual uint32 CameraLength(SceneNode *,SceneNode *); ///<摄像机距离比较函数
virtual bool InFrustum(const SceneNode *,void *); ///<平截头截剪函数
public:
SceneTreeToRenderList(GPUDevice *d)
{
device=d;
camera=nullptr;
camera_matrix=nullptr;
}
virtual bool Expend(RenderList *,Camera *,SceneNode *);
};//class SceneTreeToRenderList
}//namespace graph }//namespace graph
}//namespace hgl }//namespace hgl
#endif//HGL_GRAPH_RENDER_LIST_INCLUDE #endif//HGL_GRAPH_RENDER_LIST_INCLUDE

View File

@ -8,18 +8,6 @@ namespace hgl
{ {
namespace graph namespace graph
{ {
class SceneNode;
struct Camera;
class RenderList;
using RenderListCompFunc=float (*)(Camera *,SceneNode *,SceneNode *); ///<渲染列表排序比较函数
float CameraLengthComp(Camera *,SceneNode *,SceneNode *); ///<摄像机距离比较函数
using FilterSceneNodeFunc=bool (*)(const SceneNode *,void *); ///<场景节点过滤函数重定义
bool FrustumClipFilter(const SceneNode *,void *); ///<平截头截剪函数
/** /**
* <br> * <br>
* (SceneOrient) * (SceneOrient)

23
inc/hgl/graph/SceneRoot.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef HGL_GRAPH_SCENE_ROOT_INCLUDE
#define HGL_GRAPH_SCENE_ROOT_INCLUDE
#include<hgl/graph/Camera.h>
namespace hgl
{
namespace graph
{
/**
*
*/
class SceneRoot
{
Camera *camera; ///<根相机
public:
};//class SceneRoot
}//namespace graph
}//namespace hgl
#endif//HGL_GRAPH_SCENE_ROOT_INCLUDE

View File

@ -42,6 +42,7 @@ SET(SCENE_GRAPH_SOURCE RenderList.cpp
SceneNode.cpp SceneNode.cpp
SceneOrient.cpp SceneOrient.cpp
InlineGeometry.cpp InlineGeometry.cpp
SceneTreeToRenderList.cpp
#InlinePipeline.cpp #InlinePipeline.cpp
#Material.cpp #Material.cpp
#Mesh.cpp #Mesh.cpp

View File

@ -12,44 +12,31 @@ namespace hgl
{ {
namespace graph namespace graph
{ {
constexpr uint32_t L2WItemBytes=sizeof(Matrix4f); ///<单个L2W数据字节数 /**
* MVP矩阵
struct L2WArrays */
struct MVPMatrix
{ {
uint alloc_count; Matrix4f l2w; ///< Local to World
uint count; Matrix4f inverse_l2w;
List<Matrix4f> items; Matrix4f mvp; ///< projection * view * local_to_world
GPUBuffer *buffer; Matrix4f inverse_mvp;
Matrix4f *buffer_address;
List<uint32_t> offset;
public: public:
L2WArrays() void Set(const Matrix4f &w,const Matrix4f &vp)
{ {
alloc_count=0; l2w=w;
count=0; inverse_l2w=l2w.Inverted();
buffer=nullptr;
buffer_address=nullptr;
}
~L2WArrays() mvp=vp*l2w;
{ inverse_mvp=mvp.Inverted();
if(buffer)
{
buffer->Unmap();
delete buffer;
}
} }
};//struct MVPMatrix
constexpr size_t MVPMatrixBytes=sizeof(MVPMatrix);
void Init(const uint32_t c)
{
count=c;
items.SetCount(count);
offset.SetCount(count);
}
};//
float CameraLengthComp(Camera *cam,SceneNode *obj_one,SceneNode *obj_two) float CameraLengthComp(Camera *cam,SceneNode *obj_one,SceneNode *obj_two)
{ {
@ -75,12 +62,12 @@ namespace hgl
last_pipeline =nullptr; last_pipeline =nullptr;
last_ri =nullptr; last_ri =nullptr;
LocalToWorld=new L2WArrays; mvp_array=new MVPArray;
} }
RenderList::~RenderList() RenderList::~RenderList()
{ {
delete LocalToWorld; delete mvp_array;
} }
void RenderList::Begin() void RenderList::Begin()
@ -98,58 +85,58 @@ namespace hgl
scene_node_list.Add(node); scene_node_list.Add(node);
} }
void RenderList::End() void RenderList::End(CameraMatrix *camera_matrix)
{ {
//清0计数器 //清0计数器
uint32_t l2w_count=0; //local to world矩阵总数量 uint32_t mvp_count=0; //local to world矩阵总数量
//统计Render //统计Render
const uint32_t count=scene_node_list.GetCount(); const uint32_t count=scene_node_list.GetCount();
LocalToWorld->Init(count); mvp_array->Init(count);
Matrix4f *mp=LocalToWorld->items.GetData(); MVPMatrix *mp=mvp_array->items.GetData();
uint32_t *op=LocalToWorld->offset.GetData(); uint32_t *op=mvp_array->offset.GetData();
for(SceneNode *node:scene_node_list) for(SceneNode *node:scene_node_list)
{ {
const Matrix4f &mat=node->GetLocalToWorldMatrix(); const Matrix4f &l2w=node->GetLocalToWorldMatrix();
if(!mat.IsIdentity()) if(!l2w.IsIdentity())
{ {
hgl_cpy(mp,&mat); mp->Set(l2w,camera_matrix->vp);
++mp; ++mp;
*op=(l2w_count)*L2WItemBytes; *op=(mvp_count)*MVPMatrixBytes;
++l2w_count; ++mvp_count;
} }
else else
{ {
*op=l2w_count; *op=mvp_count;
} }
++op; ++op;
} }
if(LocalToWorld->buffer) if(mvp_array->buffer)
{ {
if(LocalToWorld->alloc_count<l2w_count) if(mvp_array->alloc_count<mvp_count)
{ {
LocalToWorld->buffer->Unmap(); mvp_array->buffer->Unmap();
delete LocalToWorld->buffer; delete mvp_array->buffer;
LocalToWorld->buffer=nullptr; mvp_array->buffer=nullptr;
// LocalToWorld->buffer_address=nullptr; //下面一定会重写,所以这一行没必要操作 // mvp_array->buffer_address=nullptr; //下面一定会重写,所以这一行没必要操作
} }
} }
if(!LocalToWorld->buffer) if(!mvp_array->buffer)
{ {
LocalToWorld->alloc_count=power_to_2(l2w_count); mvp_array->alloc_count=power_to_2(mvp_count);
LocalToWorld->buffer=device->CreateUBO(LocalToWorld->alloc_count*L2WItemBytes); mvp_array->buffer=device->CreateUBO(mvp_array->alloc_count*MVPMatrixBytes);
LocalToWorld->buffer_address=(Matrix4f *)(LocalToWorld->buffer->Map()); mvp_array->buffer_address=(MVPMatrix *)(mvp_array->buffer->Map());
} }
hgl_cpy(LocalToWorld->buffer_address,LocalToWorld->items.GetData(),l2w_count); hgl_cpy(mvp_array->buffer_address,mvp_array->items.GetData(),mvp_count);
} }
void RenderList::Render(SceneNode *node,RenderableInstance *ri) void RenderList::Render(SceneNode *node,RenderableInstance *ri)

View File

@ -0,0 +1,29 @@
#include<hgl/graph/RenderList.h>
namespace hgl
{
namespace graph
{
uint32 SceneTreeToRenderList::CameraLength(SceneNode *,SceneNode *)
{
}
bool SceneTreeToRenderList::InFrustum(const SceneNode *,void *)
{
}
bool SceneTreeToRenderList::Expend(RenderList *rl,Camera *c,SceneNode *sn)
{
if(!device)return(false);
if(!rl||!c||sn)return(false);
camera=c;
camera->Refresh();
camera_matrix=&(camera->matrix);
//Frustum f;
}
}//namespace graph
}//namespace hgl