From 1e03e28f0b5e513952d4d2cdcb8a2854bf2a8714 Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Thu, 4 May 2023 22:01:00 +0800 Subject: [PATCH] updated codes of RenderNode2D/RenderList2D --- CMSceneGraph | 2 +- inc/hgl/graph/RenderNode2D.h | 30 +++++- inc/hgl/graph/VKMaterial.h | 2 +- inc/hgl/graph/VertexAttribData.h | 2 - src/SceneGraph/CMakeLists.txt | 4 +- src/SceneGraph/RenderList2D.cpp | 2 +- src/SceneGraph/RenderNode2D.cpp | 158 ++++++++++++++++++++++++++++++- 7 files changed, 184 insertions(+), 16 deletions(-) diff --git a/CMSceneGraph b/CMSceneGraph index fbf1e4a6..71b56d6d 160000 --- a/CMSceneGraph +++ b/CMSceneGraph @@ -1 +1 @@ -Subproject commit fbf1e4a625e6ff1efad4258877524b6309382aae +Subproject commit 71b56d6d7b1cff70856882351feb0040dc04461f diff --git a/inc/hgl/graph/RenderNode2D.h b/inc/hgl/graph/RenderNode2D.h index 4d62deb3..fad5b5e7 100644 --- a/inc/hgl/graph/RenderNode2D.h +++ b/inc/hgl/graph/RenderNode2D.h @@ -3,12 +3,14 @@ #include #include +#include namespace hgl { namespace graph { class Renderable; class Material; + class GPUDevice; struct RenderNode2D { @@ -19,21 +21,39 @@ namespace hgl using RenderNode2DList=List; + 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 diff --git a/inc/hgl/graph/VKMaterial.h b/inc/hgl/graph/VKMaterial.h index b129f397..354d95a6 100644 --- a/inc/hgl/graph/VKMaterial.h +++ b/inc/hgl/graph/VKMaterial.h @@ -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;} diff --git a/inc/hgl/graph/VertexAttribData.h b/inc/hgl/graph/VertexAttribData.h index af984257..e3082af7 100644 --- a/inc/hgl/graph/VertexAttribData.h +++ b/inc/hgl/graph/VertexAttribData.h @@ -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 diff --git a/src/SceneGraph/CMakeLists.txt b/src/SceneGraph/CMakeLists.txt index b4415a9c..53802284 100644 --- a/src/SceneGraph/CMakeLists.txt +++ b/src/SceneGraph/CMakeLists.txt @@ -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} diff --git a/src/SceneGraph/RenderList2D.cpp b/src/SceneGraph/RenderList2D.cpp index 037c738c..d7b8cc41 100644 --- a/src/SceneGraph/RenderList2D.cpp +++ b/src/SceneGraph/RenderList2D.cpp @@ -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); } diff --git a/src/SceneGraph/RenderNode2D.cpp b/src/SceneGraph/RenderNode2D.cpp index 68deb43d..eba1a5d5 100644 --- a/src/SceneGraph/RenderNode2D.cpp +++ b/src/SceneGraph/RenderNode2D.cpp @@ -1,5 +1,9 @@ #include #include +#include +#include +#include +#include #include /** @@ -38,10 +42,10 @@ int Comparator::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;ilocal_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->countAlloc(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;iri); + ++rn; } } }//namespace graph