fixed RenderList2D and third_triangle example.

This commit is contained in:
HuYingzhuo(hugo/hyzboy) 2023-05-06 19:30:08 +08:00
parent 12e64d7ce0
commit abfc84feff
8 changed files with 79 additions and 270 deletions

View File

@ -45,7 +45,7 @@ private:
bool InitMaterial() bool InitMaterial()
{ {
AutoDelete<MaterialCreateInfo> mci=mtl::CreateVertexColor2D(CoordinateSystem2D::ZeroToOne); AutoDelete<MaterialCreateInfo> mci=mtl::CreateVertexColor2D(mtl::CoordinateSystem2D::ZeroToOne);
material_instance=db->CreateMaterialInstance(mci); material_instance=db->CreateMaterialInstance(mci);

View File

@ -19,7 +19,7 @@
#include<hgl/graph/VKMaterialInstance.h> #include<hgl/graph/VKMaterialInstance.h>
#include<hgl/graph/VKRenderTarget.h> #include<hgl/graph/VKRenderTarget.h>
#include<hgl/graph/VKRenderResource.h> #include<hgl/graph/VKRenderResource.h>
//#include<hgl/graph/RenderList2D.h> #include<hgl/graph/RenderList2D.h>
#include<hgl/graph/mtl/StdMaterial.h> #include<hgl/graph/mtl/StdMaterial.h>
#include<hgl/color/Color.h> #include<hgl/color/Color.h>
#include<hgl/Time.h> #include<hgl/Time.h>
@ -195,7 +195,7 @@ public:
{ {
if(!ri)return(false); if(!ri)return(false);
const IndexBuffer *ib=ri->GetIndexBuffer(); const VertexInputData *vid=ri->GetVertexInputData();
cb->Begin(); cb->Begin();
cb->BindFramebuffer(rp,fb); cb->BindFramebuffer(rp,fb);
@ -205,10 +205,10 @@ public:
cb->BindDescriptorSets(ri); cb->BindDescriptorSets(ri);
cb->BindVBO(ri); cb->BindVBO(ri);
if (ib) if (vid->index_buffer)
cb->DrawIndexed(ib->GetCount(),ri->GetInstanceCount()); cb->DrawIndexed(vid->index_buffer->buffer->GetCount());
else else
cb->Draw(ri->GetDrawCount(),ri->GetInstanceCount()); cb->Draw(vid->vertex_count);
cb->EndRenderPass(); cb->EndRenderPass();
cb->End(); cb->End();
@ -248,31 +248,31 @@ public:
return BuildCommandBuffer(sc_render_target->GetCurrentFrameIndices(),ri); return BuildCommandBuffer(sc_render_target->GetCurrentFrameIndices(),ri);
} }
//void BuildCommandBuffer(uint32_t index,RenderList2D *rl) void BuildCommandBuffer(uint32_t index,RenderList2D *rl)
//{ {
// if(!rl)return; if(!rl)return;
// RenderCmdBuffer *cb=cmd_buf[index]; RenderCmdBuffer *cb=cmd_buf[index];
// cb->Begin(); cb->Begin();
// cb->BindFramebuffer(sc_render_target->GetRenderPass(),sc_render_target->GetFramebuffer(index)); cb->BindFramebuffer(sc_render_target->GetRenderPass(),sc_render_target->GetFramebuffer(index));
// cb->SetClearColor(0,clear_color); cb->SetClearColor(0,clear_color);
// cb->BeginRenderPass(); cb->BeginRenderPass();
// rl->Render(cb); rl->Render(cb);
// cb->EndRenderPass(); cb->EndRenderPass();
// cb->End(); cb->End();
//} }
//void BuildCommandBuffer(RenderList2D *rl) void BuildCommandBuffer(RenderList2D *rl)
//{ {
// for(int32_t i=0;i<swap_chain_count;i++) for(int32_t i=0;i<swap_chain_count;i++)
// BuildCommandBuffer(i,rl); BuildCommandBuffer(i,rl);
//} }
//void BuildCurrentCommandBuffer(RenderList2D *rl) void BuildCurrentCommandBuffer(RenderList2D *rl)
//{ {
// BuildCommandBuffer(sc_render_target->GetCurrentFrameIndices(),rl); BuildCommandBuffer(sc_render_target->GetCurrentFrameIndices(),rl);
//} }
template<typename ...ARGS> template<typename ...ARGS>
Pipeline *CreatePipeline(ARGS...args){return device_render_pass->CreatePipeline(args...);} Pipeline *CreatePipeline(ARGS...args){return device_render_pass->CreatePipeline(args...);}

View File

@ -12,50 +12,31 @@ namespace hgl
{ {
/** /**
* <br> * <br>
* mvp用UBO/SSBO等数据RenderCommandBuffer * 使使Clear()
*/ */
class RenderList2D class RenderList2D
{ {
protected: protected:
GPUDevice * device; GPUDevice * device;
RenderCmdBuffer * cmd_buf;
private:
uint renderable_count; ///<可渲染对象数量 uint renderable_count; ///<可渲染对象数量
MaterialRenderMap2D mrl_map; ///<按材质分类的渲染列表 MaterialRenderMap2D mrl_map; ///<按材质分类的渲染列表
RenderNode2DComparator render_node_comparator;
private:
VkDescriptorSet ds_list[DESCRIPTOR_SET_TYPE_COUNT];
DescriptorSet *renderable_desc_sets;
protected: protected:
virtual bool Begin();
virtual bool ExpendNode(SceneNode *); virtual bool ExpendNode(SceneNode *);
virtual void End();
private:
Pipeline * last_pipeline;
MaterialParameters *last_mp[DESCRIPTOR_SET_TYPE_COUNT];
uint32_t last_vbo;
void Render(Renderable *);
public: public:
RenderList2D(GPUDevice *); RenderList2D(GPUDevice *);
virtual ~RenderList2D(); virtual ~RenderList2D();
virtual bool Expend(SceneNode *); virtual bool Expend(SceneNode *); ///<展开场景树到渲染列表
virtual bool Render(RenderCmdBuffer *); virtual bool Render(RenderCmdBuffer *); ///<渲染所有对象
virtual void Clear(); ///<彻底清理
};//class RenderList2D };//class RenderList2D
}//namespace graph }//namespace graph
}//namespace hgl }//namespace hgl

View File

@ -78,7 +78,7 @@ namespace hgl
public: public:
MaterialRenderList2D(GPUDevice *d,RenderCmdBuffer *,Material *m); MaterialRenderList2D(GPUDevice *d,Material *m);
~MaterialRenderList2D(); ~MaterialRenderList2D();
void Add(Renderable *ri,const Matrix3x4f &mat); void Add(Renderable *ri,const Matrix3x4f &mat);
@ -90,7 +90,7 @@ namespace hgl
void End(); void End();
void Render(); void Render(RenderCmdBuffer *);
}; };
class MaterialRenderMap2D:public ObjectMap<Material *,MaterialRenderList2D> class MaterialRenderMap2D:public ObjectMap<Material *,MaterialRenderList2D>
@ -111,6 +111,14 @@ namespace hgl
for(auto *it:data_list) for(auto *it:data_list)
it->value->End(); it->value->End();
} }
void Render(RenderCmdBuffer *rcb)
{
if(!rcb)return;
for(auto *it:data_list)
it->value->Render(rcb);
}
}; };
}//namespace graph }//namespace graph
}//namespace hgl }//namespace hgl

View File

@ -26,6 +26,8 @@ public:
const bool Comp(const VertexInputData *vid)const const bool Comp(const VertexInputData *vid)const
{ {
if(!vid)return(false);
if(binding_count!=vid->binding_count)return(false); if(binding_count!=vid->binding_count)return(false);
for(uint32_t i=0;i<binding_count;i++) for(uint32_t i=0;i<binding_count;i++)
@ -75,10 +77,6 @@ public:
const VertexInputData * GetVertexInputData ()const{return vertex_input;} const VertexInputData * GetVertexInputData ()const{return vertex_input;}
MaterialParameters *GetMP (const DescriptorSetType &type){return mat_inst->GetMP(type);} MaterialParameters *GetMP (const DescriptorSetType &type){return mat_inst->GetMP(type);}
public: //instance support
virtual const uint32_t GetInstanceCount ()const{return 1;}
};//class Renderable };//class Renderable
Renderable *CreateRenderable(Primitive *,MaterialInstance *,Pipeline *); Renderable *CreateRenderable(Primitive *,MaterialInstance *,Pipeline *);

View File

@ -15,81 +15,13 @@ namespace hgl
RenderList2D::RenderList2D(GPUDevice *dev) RenderList2D::RenderList2D(GPUDevice *dev)
{ {
device =dev; device =dev;
cmd_buf =nullptr; renderable_count=0;
last_pipeline =nullptr;
hgl_zero(last_mp);
last_vbo =0;
} }
RenderList2D::~RenderList2D() RenderList2D::~RenderList2D()
{ {
} }
bool RenderList2D::Begin()
{
renderable_count=0;
mrl_map.Begin();
return(true);
}
void RenderList2D::End()
{
if(renderable_count<=0)return;
mrl_map.End();
//
// //排序
// Sort(render_node_list,&render_node_comparator);
//
// //产生MVP矩阵UBO数据
// {
// const uint32_t count=render_node_list.GetCount();
//
// {
// //按当前总节点数量分配UBO
//// mvp_array->Alloc(count);
//// mvp_array->Clear();
//
// ri_list.ClearData();
// ri_list.SetCount(count);
// }
//
// {
//// ubo_align=mvp_array->GetAlignSize();
//
//// char *mp=(char *)(mvp_array->Map(0,count));
// Renderable **ri=ri_list.GetData();
//
// for(RenderNode2D *node:render_node_list) //未来可能要在Expend处考虑做去重
// {
//// memcpy(mp,&(node->matrix),MVPMatrixBytes);
//// mp+=ubo_align;
//
// (*ri)=node->ri;
// ++ri;
// }
//
//// mvp_array->Flush(count);
// }
// }
//
// //为所有的材质绑定
// //for(Material *mtl:material_sets)
// //{
// // MaterialParameters *mp=mtl->GetMP(DescriptorSetType::PerObject);
//
// // if(mp)
// // {
// // if(mp->BindUBO("r_scene_info",mvp_array->GetBuffer(),true))
// // mp->Update();
// // }
// //}
}
bool RenderList2D::ExpendNode(SceneNode *sn) bool RenderList2D::ExpendNode(SceneNode *sn)
{ {
if(!sn)return(false); if(!sn)return(false);
@ -123,145 +55,29 @@ namespace hgl
{ {
if(!device|!sn)return(false); if(!device|!sn)return(false);
Begin(); mrl_map.Begin();
ExpendNode(sn); ExpendNode(sn);
// End(); mrl_map.End();
return(true); return(true);
} }
//bool RenderList2D::BindPerFrameDescriptor()
//{
// if(!cmd_buf)return(false);
// for(Material *mtl:material_sets)
// {
// MaterialParameters *mp=mtl->GetMP(DescriptorSetType::PerFrame);
// if(!mp)continue;
//
// //if(mp->BindUBO("r_scene_info",mvp_array->GetBuffer(),true))
// // mp->Update();
// }
// return(true);
//}
//bool RenderList2D::BindPerMaterialDescriptor()
//{
// //为每个材质实例,绑定它们的描述符
// if(!cmd_buf)return(false);
// for(Material *mtl:material_sets)
// {
// MaterialParameters *mp=mtl->GetMP(DescriptorSetType::PerMaterial);
// if(!mp)continue;
//
// //if(mp->BindUBO("r_scene_info",mvp_array->GetBuffer(),true))
// // mp->Update();
// }
// return(true);
//}
void RenderList2D::Render(Renderable *ri)
{
if(last_pipeline!=ri->GetPipeline())
{
last_pipeline=ri->GetPipeline();
cmd_buf->BindPipeline(last_pipeline);
}
{
uint32_t ds_count=0;
uint32_t first_set=0;
MaterialParameters *mp;
ENUM_CLASS_FOR(DescriptorSetType,int,i)
{
if(i==(int)DescriptorSetType::PerObject)continue;
mp=ri->GetMP((DescriptorSetType)i);
if(last_mp[i]!=mp)
{
last_mp[i]=mp;
if(mp)
{
ds_list[ds_count]=mp->GetVkDescriptorSet();
++ds_count;
}
}
else
{
if(mp)
++first_set;
}
}
{
mp=ri->GetMP(DescriptorSetType::PerObject);
if(mp)
{
ds_list[ds_count]=mp->GetVkDescriptorSet();
++ds_count;
cmd_buf->BindDescriptorSets(ri->GetPipelineLayout(),first_set,ds_list,ds_count,&ubo_offset,1);
}
else
{
cmd_buf->BindDescriptorSets(ri->GetPipelineLayout(),first_set,ds_list,ds_count,nullptr,0);
}
ubo_offset+=ubo_align;
}
}
if(last_vbo!=ri->GetBufferHash())
{
last_vbo=ri->GetBufferHash();
cmd_buf->BindVBO(ri);
}
const IndexBuffer *ib=ri->GetIndexBuffer();
if(ib)
{
cmd_buf->DrawIndexed(ib->GetCount());
}
else
{
cmd_buf->Draw(ri->GetDrawCount());
}
}
bool RenderList2D::Render(RenderCmdBuffer *cb) bool RenderList2D::Render(RenderCmdBuffer *cb)
{ {
if(!cb) if(!cb)
return(false); return(false);
if(ri_list.GetCount()<=0) if(renderable_count<=0)
return(true); return(true);
cmd_buf=cb; mrl_map.Render(cb);
last_mtl=nullptr;
last_pipeline=nullptr;
hgl_zero(last_mp);
last_vbo=0;
ubo_offset=0;
BindPerFrameDescriptor();
for(Renderable *ri:ri_list)
Render(ri);
return(true); return(true);
} }
void RenderList2D::Clear()
{
mrl_map.Clear();
}
}//namespace graph }//namespace graph
}//namespace hgl }//namespace hgl

View File

@ -123,10 +123,10 @@ namespace hgl
} }
};//struct RenderNode2DExtraBuffer };//struct RenderNode2DExtraBuffer
MaterialRenderList2D::MaterialRenderList2D(GPUDevice *d,RenderCmdBuffer *rcb,Material *m) MaterialRenderList2D::MaterialRenderList2D(GPUDevice *d,Material *m)
{ {
device=d; device=d;
cmd_buf=rcb; cmd_buf=nullptr;
mtl=m; mtl=m;
extra_buffer=nullptr; extra_buffer=nullptr;
@ -256,13 +256,10 @@ namespace hgl
hgl_cpy(buffer_list,vid->buffer_list,vid->binding_count); hgl_cpy(buffer_list,vid->buffer_list,vid->binding_count);
hgl_cpy(buffer_offset,vid->buffer_offset,vid->binding_count); hgl_cpy(buffer_offset,vid->buffer_offset,vid->binding_count);
if(binding_count==vid->binding_count)
return(true);
count=vid->binding_count; count=vid->binding_count;
} }
//Bone组暂未支持 if(count<binding_count) //Bone组暂未支持
{ {
const uint bone_binding_count=vil->GetCount(VertexInputGroup::Bone); const uint bone_binding_count=vil->GetCount(VertexInputGroup::Bone);
@ -275,7 +272,7 @@ namespace hgl
} }
} }
//LocalToWorld组由RenderList合成 if(count<binding_count)//LocalToWorld组由RenderList合成
{ {
const uint l2w_binding_count=vil->GetCount(VertexInputGroup::LocalToWorld); const uint l2w_binding_count=vil->GetCount(VertexInputGroup::LocalToWorld);
@ -293,7 +290,6 @@ namespace hgl
} }
} }
//
if(count!=binding_count) if(count!=binding_count)
{ {
//还有没支持的绑定组???? //还有没支持的绑定组????
@ -301,6 +297,8 @@ namespace hgl
return(false); return(false);
} }
cmd_buf->BindVBO(0,count,buffer_list,buffer_offset);
return(true); return(true);
} }
@ -314,7 +312,7 @@ namespace hgl
last_mi=nullptr; last_mi=nullptr;
last_vid=nullptr; last_vid=nullptr;
//这里未来尝试换pipeline同时不换mi/primitive是否需要重新绑定primitive //这里未来尝试换pipeline同时不换mi/primitive是否需要重新绑定mi/primitive
} }
if(last_mi!=ri->mi) if(last_mi!=ri->mi)
@ -325,22 +323,29 @@ namespace hgl
last_vid=nullptr; last_vid=nullptr;
} }
if(!last_vid->Comp(ri->vid)) if(!ri->vid->Comp(last_vid))
{ {
Bind(ri->vid,ri->first); Bind(ri->vid,ri->first);
last_vid=ri->vid; last_vid=ri->vid;
} }
const IndexBufferData *ibd=last_primitive->GetIndexBuffer(); const IndexBufferData *ibd=last_vid->index_buffer;
if(ib) if(ibd->buffer)
cmd_buf->DrawIndexed(ib->GetCount(),ri->count); {
cmd_buf->BindIBO(ibd);
cmd_buf->DrawIndexed(ibd->buffer->GetCount(),ri->count);
}
else else
cmd_buf->Draw(last_primitive->GetDrawCount(),ri->count); {
cmd_buf->Draw(last_vid->vertex_count,ri->count);
}
} }
void MaterialRenderList2D::Render() void MaterialRenderList2D::Render(RenderCmdBuffer *rcb)
{ {
if(!rcb)return;
const uint count=rn_list.GetCount(); const uint count=rn_list.GetCount();
if(count<=0)return; if(count<=0)return;
@ -349,6 +354,8 @@ namespace hgl
if(ri_count<=0)return; if(ri_count<=0)return;
cmd_buf=rcb;
RenderItem *ri=ri_list.GetData(); RenderItem *ri=ri_list.GetData();
last_pipeline =nullptr; last_pipeline =nullptr;

View File

@ -1,5 +1,4 @@
#include<hgl/graph/SceneNode.h> #include<hgl/graph/SceneNode.h>
#include<hgl/graph/RenderList.h>
#include<hgl/graph/VKRenderable.h> #include<hgl/graph/VKRenderable.h>
namespace hgl namespace hgl
{ {