split two files.they are MVPMatrix.h and SceneTreeToRenderList.h

This commit is contained in:
hyzboy 2021-04-26 20:36:56 +08:00
parent d0d00b235c
commit f1fcfe5cd4
9 changed files with 156 additions and 120 deletions

2
CMCore

@ -1 +1 @@
Subproject commit ec23a526afb1543236817a930c2f8a57b0b37570
Subproject commit 0a7b1ff3afd95e2c41a4476c1c00dec6511ad447

36
inc/hgl/graph/MVPMatrix.h Normal file
View File

@ -0,0 +1,36 @@
#ifndef HGL_GRAPH_MVP_MATRIX_INCLUDE
#define HGL_GRAPH_MVP_MATRIX_INCLUDE
#include<hgl/math/Matrix.h>
#include<hgl/CompOperator.h>
namespace hgl
{
namespace graph
{
/**
* MVP¾ØÕó
*/
struct MVPMatrix
{
Matrix4f l2w; ///< Local to World
Matrix4f inverse_l2w;
Matrix4f mvp; ///< projection * view * local_to_world
Matrix4f inverse_mvp;
public:
void Set(const Matrix4f &w,const Matrix4f &vp)
{
l2w=w;
inverse_l2w=l2w.Inverted();
mvp=vp*l2w;
inverse_mvp=mvp.Inverted();
}
CompOperatorMemcmp(const MVPMatrix &);
};//struct MVPMatrix
}//namespace graph
//namespace hgl
#endif//HGL_GRAPH_MVP_MATRIX_INCLUDE

View File

@ -4,15 +4,17 @@
#include<hgl/graph/VK.h>
#include<hgl/graph/Camera.h>
#include<hgl/graph/SceneNode.h>
#include<hgl/graph/VKArrayBuffer.h>
#include<hgl/graph/MVPMatrix.h>
#include<hgl/type/Color4f.h>
#include<hgl/type/Sets.h>
namespace hgl
{
namespace graph
{
struct MVPArray;
using SceneNodeList=List<SceneNode *>;
using MVPArrayBuffer=GPUArrayBuffer<MVPMatrix>;
using MVPOffsetBuffer=List<uint32_t>;
class RenderList
{
@ -23,9 +25,12 @@ namespace hgl
Camera *camera;
MVPArray *mvp_array;
SceneNodeList scene_node_list;
SceneNodeList *scene_node_list;
MVPArrayBuffer *mvp_array;
MVPOffsetBuffer mvp_offset;
private:
Pipeline * last_pipeline;
RenderableInstance *last_ri;
@ -49,59 +54,6 @@ namespace hgl
bool Render (RenderCmdBuffer *);
};//class RenderList
class SceneTreeToRenderList
{
using PipelineSets=Sets<Pipeline *>;
using MaterialSets=Sets<Material *>;
using MatInstanceSets=Sets<MaterialInstance *>;
protected:
GPUDevice *device;
protected:
Camera * camera;
CameraMatrix * camera_matrix;
Frustum frustum;
protected:
SceneNodeList * scene_node_list; ///<场景节点列表
PipelineSets pipeline_sets; ///<管线合集
MaterialSets material_sets; ///<材质合集
MatInstanceSets mat_instance_sets; ///<材质实例合集
RenderList * render_list;
protected:
virtual uint32 CameraLength(SceneNode *,SceneNode *); ///<摄像机距离比较函数
virtual bool InFrustum(const SceneNode *,void *); ///<平截头截剪函数
virtual bool Begin();
virtual bool Expend(SceneNode *);
virtual bool End();
public:
SceneTreeToRenderList(GPUDevice *d)
{
device=d;
camera=nullptr;
camera_matrix=nullptr;
scene_node_list=nullptr;
render_list=nullptr;
}
virtual ~SceneTreeToRenderList();
virtual bool Expend(RenderList *,Camera *,SceneNode *);
};//class SceneTreeToRenderList
}//namespace graph
}//namespace hgl
#endif//HGL_GRAPH_RENDER_LIST_INCLUDE

View File

@ -0,0 +1,63 @@
#ifndef HGL_GRAPH_SCENE_TREE_TO_RENDER_LIST_INCLUDE
#define HGL_GRAPH_SCENE_TREE_TO_RENDER_LIST_INCLUDE
#include<hgl/graph/RenderList.h>
namespace hgl
{
namespace graph
{
class SceneTreeToRenderList
{
using PipelineSets=Sets<Pipeline *>;
using MaterialSets=Sets<Material *>;
using MatInstanceSets=Sets<MaterialInstance *>;
protected:
GPUDevice *device;
protected:
Camera * camera;
CameraMatrix * camera_matrix;
Frustum frustum;
protected:
SceneNodeList * scene_node_list; ///<场景节点列表
PipelineSets pipeline_sets; ///<管线合集
MaterialSets material_sets; ///<材质合集
MatInstanceSets mat_instance_sets; ///<材质实例合集
RenderList * render_list;
protected:
virtual uint32 CameraLength(SceneNode *,SceneNode *); ///<摄像机距离比较函数
virtual bool InFrustum(const SceneNode *,void *); ///<平截头截剪函数
virtual bool Begin();
virtual bool Expend(SceneNode *);
virtual bool End();
public:
SceneTreeToRenderList(GPUDevice *d)
{
device=d;
camera=nullptr;
camera_matrix=nullptr;
scene_node_list=nullptr;
render_list=nullptr;
}
virtual ~SceneTreeToRenderList();
virtual bool Expend(RenderList *,Camera *,SceneNode *);
};//class SceneTreeToRenderList
}//namespace graph
}//namespace hgl
#endif//HGL_GRAPH_SCENE_TREE_TO_RENDER_LIST_INCLUDE

View File

@ -3,6 +3,7 @@
#include<hgl/graph/VKBuffer.h>
#include<hgl/graph/VKDevice.h>
#include<hgl/graph/VKMemoryAllocator.h>
#include<hgl/type/Collection.h>
namespace hgl
{
@ -17,25 +18,42 @@ namespace hgl
protected:
GPUDevice *device;
VkBufferUsageFlags buffer_usage_flags;
Collection<T> *coll;
private:
GPUArrayBuffer(GPUDevice *device,const uint32_t s,const uint32_t c)
{
}
friend class GPUDevice;
public:
virtual ~GPUArrayBuffer();
GPUArrayBuffer(GPUDevice *dev,VkBufferUsageFlags flags)
{
device=dev;
buffer_usage_flags=flags;
void Clear(); ///<清空缓冲区
coll=new Collection<T>(new MemoryBlock(new VKMemoryAllocator(device,buffer_usage_flags)));
}
bool Init(const uint32_t); ///<初始化并分配空间
virtual ~GPUArrayBuffer()
{
delete coll;
}
uint32 Alloc(const uint32 max_count) ///<预分配空间
{
if(!coll->Alloc(max_count))
return(0);
return coll->GetAllocCount();
}
void Clear()
{
coll->Clear();
}
T *Map(const uint32 start,const uint32 count)
{
return coll->Map(start,count);
}
};//class GPUArrayBuffer
}//namespace graph
}//namespace hgl

View File

@ -14,6 +14,7 @@
#include<hgl/graph/VKRenderTarget.h>
#include<hgl/graph/VertexAttribData.h>
#include<hgl/graph/VKShaderModuleMap.h>
#include<hgl/graph/VKArrayBuffer.h>
namespace hgl
{

View File

@ -25,7 +25,7 @@ public:
public:
VKMemoryAllocator(GPUDevice *,const uint32_t);
VKMemoryAllocator(GPUDevice *,const uint32_t flags);
~VKMemoryAllocator();
void Free() override {/* DON'T RUN ANY OPERATION.*/}

View File

@ -29,9 +29,11 @@ SET(TILE_SOURCE ${SG_INCLUDE_PATH}/TileData.h
SOURCE_GROUP("Tile" FILES ${TILE_SOURCE})
SET(SCENE_GRAPH_HEADER ${SG_INCLUDE_PATH}/Light.h
${SG_INCLUDE_PATH}/MVPMatrix.h
${SG_INCLUDE_PATH}/SceneNode.h
${SG_INCLUDE_PATH}/SceneOrient.h
${SG_INCLUDE_PATH}/RenderList.h
${SG_INCLUDE_PATH}/SceneTreeToRenderList.h
${SG_INCLUDE_PATH}/InlineGeometry.h
#${SG_INCLUDE_PATH}/Mesh.h
#${SG_INCLUDE_PATH}/Material.h

View File

@ -2,6 +2,7 @@
#include<hgl/graph/Camera.h>
#include<hgl/graph/SceneNode.h>
#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>
@ -12,32 +13,8 @@ namespace hgl
{
namespace graph
{
/**
* MVP矩阵
*/
struct MVPMatrix
{
Matrix4f l2w; ///< Local to World
Matrix4f inverse_l2w;
Matrix4f mvp; ///< projection * view * local_to_world
Matrix4f inverse_mvp;
public:
void Set(const Matrix4f &w,const Matrix4f &vp)
{
l2w=w;
inverse_l2w=l2w.Inverted();
mvp=vp*l2w;
inverse_mvp=mvp.Inverted();
}
};//struct MVPMatrix
constexpr size_t MVPMatrixBytes=sizeof(MVPMatrix);
float CameraLengthComp(Camera *cam,SceneNode *obj_one,SceneNode *obj_two)
{
if(!cam||!obj_one||!obj_two)
@ -62,7 +39,7 @@ namespace hgl
last_pipeline =nullptr;
last_ri =nullptr;
mvp_array=new MVPArray;
mvp_array =new MVPArrayBuffer(gd,VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT);
}
RenderList::~RenderList()
@ -74,6 +51,9 @@ namespace hgl
{
scene_node_list.ClearData();
mvp_array->Clear();
mvp_offset.ClearData();
last_pipeline =nullptr;
last_ri =nullptr;
}
@ -93,10 +73,14 @@ namespace hgl
//统计Render
const uint32_t count=scene_node_list.GetCount();
mvp_array->Init(count);
mvp_array->Alloc(count);
mvp_array->Clear();
MVPMatrix *mp=mvp_array->items.GetData();
uint32_t *op=mvp_array->offset.GetData();
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)
{
@ -107,36 +91,16 @@ namespace hgl
mp->Set(l2w,camera_matrix->vp);
++mp;
*op=(mvp_count)*MVPMatrixBytes;
*op=mvp_count*MVPMatrixBytes;
++mvp_count;
}
else
{
*op=mvp_count;
*op=mvp_count*MVPMatrixBytes;
}
++op;
}
if(mvp_array->buffer)
{
if(mvp_array->alloc_count<mvp_count)
{
mvp_array->buffer->Unmap();
delete mvp_array->buffer;
mvp_array->buffer=nullptr;
// mvp_array->buffer_address=nullptr; //下面一定会重写,所以这一行没必要操作
}
}
if(!mvp_array->buffer)
{
mvp_array->alloc_count=power_to_2(mvp_count);
mvp_array->buffer=device->CreateUBO(mvp_array->alloc_count*MVPMatrixBytes);
mvp_array->buffer_address=(MVPMatrix *)(mvp_array->buffer->Map());
}
hgl_cpy(mvp_array->buffer_address,mvp_array->items.GetData(),mvp_count);
}
void RenderList::Render(SceneNode *node,RenderableInstance *ri)