updated codes of RenderNode2D/RenderList2D

This commit is contained in:
HuYingzhuo(hugo/hyzboy) 2023-05-04 22:01:00 +08:00
parent 30c736b7c8
commit 1e03e28f0b
7 changed files with 184 additions and 16 deletions

@ -1 +1 @@
Subproject commit fbf1e4a625e6ff1efad4258877524b6309382aae
Subproject commit 71b56d6d7b1cff70856882351feb0040dc04461f

View File

@ -3,12 +3,14 @@
#include<hgl/math/Math.h>
#include<hgl/type/Map.h>
#include<hgl/graph/VK.h>
namespace hgl
{
namespace graph
{
class Renderable;
class Material;
class GPUDevice;
struct RenderNode2D
{
@ -19,21 +21,39 @@ namespace hgl
using RenderNode2DList=List<RenderNode2D>;
struct RenderNode2DExtraBuffer;
/**
*
*/
class MaterialRenderList2D
{
GPUDevice *device;
RenderCmdBuffer *cmd_buf;
Material *mtl;
RenderNode2DList rn_list;
RenderNode2DExtraBuffer *extra_buffer;
protected:
uint32_t binding_count;
VkBuffer *buffer_list;
VkDeviceSize *buffer_offset;
MaterialInstance *last_mi;
Pipeline *last_pipeline;
Primitive *last_primitive;
uint first_index;
void Render(const uint index,Renderable *);
public:
MaterialRenderList2D(Material *m)
{
mtl=m;
}
MaterialRenderList2D(GPUDevice *d,RenderCmdBuffer *,Material *m);
~MaterialRenderList2D();
void Add(Renderable *ri,const Matrix3x4f &mat);
@ -43,6 +63,8 @@ namespace hgl
}
void End();
void Render();
};
class MaterialRenderMap2D:public ObjectMap<Material *,MaterialRenderList2D>

View File

@ -55,7 +55,7 @@ public:
const UTF8String & GetName ()const{return data->name;}
// const VertexInput * GetVertexInput ()const{return data->vertex_input;}
const VertexInput * GetVertexInput ()const{return data->vertex_input;}
const ShaderStageCreateInfoList & GetStageList ()const{return data->shader_stage_list;}

View File

@ -38,9 +38,7 @@ namespace hgl
}
const VkFormat GetFormat ()const{return format;} ///<取得数据类型
// const uint32_t GetVecSize ()const{return vec_size;} ///<取数缓冲区元数据成份数量
const uint32_t GetCount ()const{return count;} ///<取得数据数量
// const uint32_t GetStride ()const{return stride;} ///<取得每一组数据字节数
void * GetData ()const{return mem_data;} ///<取得数据指针
const uint32_t GetTotalBytes ()const{return total_bytes;} ///<取得数据字节数
};//class VertexAttribData

View File

@ -268,8 +268,8 @@ SET(VULKAN_RENDER_SOURCE ${VK_RR_SOURCE}
${VK_RENDERABLE_SOURCE}
${VK_RENDER_DEVICE_SOURCE})
add_cm_library(ULRE.SceneGraph "ULRE" #${SCENE_GRAPH_HEADER}
#${SCENE_GRAPH_SOURCE}
add_cm_library(ULRE.SceneGraph "ULRE" ${SCENE_GRAPH_HEADER}
${SCENE_GRAPH_SOURCE}
${GEOMETRY_FILES}
${LIGHT_FILES}

View File

@ -103,7 +103,7 @@ namespace hgl
if(!mrl_map.Get(mtl,mrl))
{
mrl=new MaterialRenderList2D(mtl);
mrl=new MaterialRenderList2D(device,mtl);
mrl_map.Add(mtl,mrl);
}

View File

@ -1,5 +1,9 @@
#include<hgl/graph/RenderNode2D.h>
#include<hgl/graph/VKRenderable.h>
#include<hgl/graph/VKDevice.h>
#include<hgl/graph/VKVertexAttribBuffer.h>
#include<hgl/graph/VKVertexInput.h>
#include<hgl/graph/VKCommandBuffer.h>
#include<hgl/util/sort/Sort.h>
/**
@ -38,10 +42,10 @@ int Comparator<hgl::graph::RenderNode2D>::compare(const hgl::graph::RenderNode2D
return off;
}
//比较vbo+ebo
//比较模型
{
off=ri_one->GetBufferHash()
-ri_two->GetBufferHash();
off=ri_one->GetPrimitive()
-ri_two->GetPrimitive();
if(off)
return off;
@ -54,6 +58,90 @@ namespace hgl
{
namespace graph
{
/*
* 2D渲染节点额外提供的VBO数据
*/
struct RenderNode2DExtraBuffer
{
uint count;
VBO *local_to_world[3];
public:
RenderNode2DExtraBuffer()
{
hgl_zero(*this);
}
~RenderNode2DExtraBuffer()
{
Clear();
}
void Clear()
{
SAFE_CLEAR(local_to_world[0])
SAFE_CLEAR(local_to_world[1])
SAFE_CLEAR(local_to_world[2])
count=0;
}
void Alloc(GPUDevice *dev,const uint c)
{
Clear();
count=power_to_2(c);
local_to_world[0]=dev->CreateVBO(VF_V4F,count);
local_to_world[1]=dev->CreateVBO(VF_V4F,count);
local_to_world[2]=dev->CreateVBO(VF_V4F,count);
}
void WriteData(RenderNode2D *render_node,const uint count)
{
RenderNode2D *rn;
glm::vec4 *tp;
for(uint col=0;col<3;col++)
{
tp=(glm::vec4 *)(local_to_world[col]->Map());
rn=render_node;
for(uint i=0;i<count;i++)
{
*tp=rn->local_to_world[col];
++tp;
++rn;
}
local_to_world[col]->Unmap();
}
}
};//struct RenderNode2DExtraBuffer
MaterialRenderList2D::MaterialRenderList2D(GPUDevice *d,RenderCmdBuffer *rcb,Material *m)
{
device=d;
cmd_buf=rcb;
mtl=m;
extra_buffer=nullptr;
const VertexInput *vi=mtl->GetVertexInput();
binding_count=vi->GetCount();
buffer_list=new VkBuffer[binding_count];
buffer_offset=new VkDeviceSize[binding_count];
}
MaterialRenderList2D::~MaterialRenderList2D()
{
delete[] buffer_offset;
delete[] buffer_list;
SAFE_CLEAR(extra_buffer)
}
void MaterialRenderList2D::Add(Renderable *ri,const Matrix3x4f &mat)
{
RenderNode2D rn;
@ -73,9 +161,69 @@ namespace hgl
Sort(rn_list,&rnc);
}
//生成mvp数据
//写入LocalToWorld数据
{
uint count=rn_list.GetCount();
if(count<=0)return;
if(!extra_buffer)
extra_buffer=new RenderNode2DExtraBuffer;
if(extra_buffer->count<count)
extra_buffer->Alloc(device,count);
//写入数据
extra_buffer->WriteData(rn_list.GetData(),count);
}
}
void MaterialRenderList2D::Render(const uint index,Renderable *ri)
{
if(last_mi!=ri->GetMaterialInstance())
{
last_pipeline=nullptr;
last_primitive=nullptr;
}
if(last_pipeline!=ri->GetPipeline())
{
last_pipeline=ri->GetPipeline();
cmd_buf->BindPipeline(last_pipeline);
last_primitive=nullptr;
}
if(last_primitive!=ri->GetPrimitive())
{
//把之前的一次性画了
last_primitive=ri->GetPrimitive();
first_index=index;
}
}
void MaterialRenderList2D::Render()
{
last_mi=nullptr;
last_pipeline=nullptr;
last_primitive=nullptr;
first_index=0;
const uint count=rn_list.GetCount();
if(count<=0)return;
RenderNode2D *rn=rn_list.GetData();
for(uint i=0;i<count;i++)
{
Render(i,rn->ri);
++rn;
}
}
}//namespace graph