fixed RenderList2D and third_triangle example.
This commit is contained in:
parent
12e64d7ce0
commit
abfc84feff
@ -45,7 +45,7 @@ private:
|
||||
|
||||
bool InitMaterial()
|
||||
{
|
||||
AutoDelete<MaterialCreateInfo> mci=mtl::CreateVertexColor2D(CoordinateSystem2D::ZeroToOne);
|
||||
AutoDelete<MaterialCreateInfo> mci=mtl::CreateVertexColor2D(mtl::CoordinateSystem2D::ZeroToOne);
|
||||
|
||||
material_instance=db->CreateMaterialInstance(mci);
|
||||
|
||||
|
@ -19,7 +19,7 @@
|
||||
#include<hgl/graph/VKMaterialInstance.h>
|
||||
#include<hgl/graph/VKRenderTarget.h>
|
||||
#include<hgl/graph/VKRenderResource.h>
|
||||
//#include<hgl/graph/RenderList2D.h>
|
||||
#include<hgl/graph/RenderList2D.h>
|
||||
#include<hgl/graph/mtl/StdMaterial.h>
|
||||
#include<hgl/color/Color.h>
|
||||
#include<hgl/Time.h>
|
||||
@ -195,7 +195,7 @@ public:
|
||||
{
|
||||
if(!ri)return(false);
|
||||
|
||||
const IndexBuffer *ib=ri->GetIndexBuffer();
|
||||
const VertexInputData *vid=ri->GetVertexInputData();
|
||||
|
||||
cb->Begin();
|
||||
cb->BindFramebuffer(rp,fb);
|
||||
@ -205,10 +205,10 @@ public:
|
||||
cb->BindDescriptorSets(ri);
|
||||
cb->BindVBO(ri);
|
||||
|
||||
if (ib)
|
||||
cb->DrawIndexed(ib->GetCount(),ri->GetInstanceCount());
|
||||
if (vid->index_buffer)
|
||||
cb->DrawIndexed(vid->index_buffer->buffer->GetCount());
|
||||
else
|
||||
cb->Draw(ri->GetDrawCount(),ri->GetInstanceCount());
|
||||
cb->Draw(vid->vertex_count);
|
||||
|
||||
cb->EndRenderPass();
|
||||
cb->End();
|
||||
@ -248,31 +248,31 @@ public:
|
||||
return BuildCommandBuffer(sc_render_target->GetCurrentFrameIndices(),ri);
|
||||
}
|
||||
|
||||
//void BuildCommandBuffer(uint32_t index,RenderList2D *rl)
|
||||
//{
|
||||
// if(!rl)return;
|
||||
void BuildCommandBuffer(uint32_t index,RenderList2D *rl)
|
||||
{
|
||||
if(!rl)return;
|
||||
|
||||
// RenderCmdBuffer *cb=cmd_buf[index];
|
||||
RenderCmdBuffer *cb=cmd_buf[index];
|
||||
|
||||
// cb->Begin();
|
||||
// cb->BindFramebuffer(sc_render_target->GetRenderPass(),sc_render_target->GetFramebuffer(index));
|
||||
// cb->SetClearColor(0,clear_color);
|
||||
// cb->BeginRenderPass();
|
||||
// rl->Render(cb);
|
||||
// cb->EndRenderPass();
|
||||
// cb->End();
|
||||
//}
|
||||
cb->Begin();
|
||||
cb->BindFramebuffer(sc_render_target->GetRenderPass(),sc_render_target->GetFramebuffer(index));
|
||||
cb->SetClearColor(0,clear_color);
|
||||
cb->BeginRenderPass();
|
||||
rl->Render(cb);
|
||||
cb->EndRenderPass();
|
||||
cb->End();
|
||||
}
|
||||
|
||||
//void BuildCommandBuffer(RenderList2D *rl)
|
||||
//{
|
||||
// for(int32_t i=0;i<swap_chain_count;i++)
|
||||
// BuildCommandBuffer(i,rl);
|
||||
//}
|
||||
void BuildCommandBuffer(RenderList2D *rl)
|
||||
{
|
||||
for(int32_t i=0;i<swap_chain_count;i++)
|
||||
BuildCommandBuffer(i,rl);
|
||||
}
|
||||
|
||||
//void BuildCurrentCommandBuffer(RenderList2D *rl)
|
||||
//{
|
||||
// BuildCommandBuffer(sc_render_target->GetCurrentFrameIndices(),rl);
|
||||
//}
|
||||
void BuildCurrentCommandBuffer(RenderList2D *rl)
|
||||
{
|
||||
BuildCommandBuffer(sc_render_target->GetCurrentFrameIndices(),rl);
|
||||
}
|
||||
|
||||
template<typename ...ARGS>
|
||||
Pipeline *CreatePipeline(ARGS...args){return device_render_pass->CreatePipeline(args...);}
|
||||
|
@ -12,50 +12,31 @@ namespace hgl
|
||||
{
|
||||
/**
|
||||
* 渲染对象列表<br>
|
||||
* 已经展开的渲染对象列表,产生mvp用UBO/SSBO等数据,最终创建RenderCommandBuffer
|
||||
* 该类会长期保存使用过的材质信息,避重新分配造成的时间和空间浪费。如需彻底清空列表请使用Clear()函数
|
||||
*/
|
||||
class RenderList2D
|
||||
{
|
||||
protected:
|
||||
|
||||
GPUDevice * device;
|
||||
RenderCmdBuffer * cmd_buf;
|
||||
|
||||
private:
|
||||
|
||||
uint renderable_count; ///<可渲染对象数量
|
||||
|
||||
MaterialRenderMap2D mrl_map; ///<按材质分类的渲染列表
|
||||
|
||||
RenderNode2DComparator render_node_comparator;
|
||||
|
||||
private:
|
||||
|
||||
VkDescriptorSet ds_list[DESCRIPTOR_SET_TYPE_COUNT];
|
||||
DescriptorSet *renderable_desc_sets;
|
||||
|
||||
protected:
|
||||
|
||||
virtual bool Begin();
|
||||
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:
|
||||
|
||||
RenderList2D(GPUDevice *);
|
||||
virtual ~RenderList2D();
|
||||
|
||||
virtual bool Expend(SceneNode *);
|
||||
virtual bool Expend(SceneNode *); ///<展开场景树到渲染列表
|
||||
|
||||
virtual bool Render(RenderCmdBuffer *);
|
||||
virtual bool Render(RenderCmdBuffer *); ///<渲染所有对象
|
||||
|
||||
virtual void Clear(); ///<彻底清理
|
||||
};//class RenderList2D
|
||||
}//namespace graph
|
||||
}//namespace hgl
|
||||
|
@ -78,7 +78,7 @@ namespace hgl
|
||||
|
||||
public:
|
||||
|
||||
MaterialRenderList2D(GPUDevice *d,RenderCmdBuffer *,Material *m);
|
||||
MaterialRenderList2D(GPUDevice *d,Material *m);
|
||||
~MaterialRenderList2D();
|
||||
|
||||
void Add(Renderable *ri,const Matrix3x4f &mat);
|
||||
@ -90,7 +90,7 @@ namespace hgl
|
||||
|
||||
void End();
|
||||
|
||||
void Render();
|
||||
void Render(RenderCmdBuffer *);
|
||||
};
|
||||
|
||||
class MaterialRenderMap2D:public ObjectMap<Material *,MaterialRenderList2D>
|
||||
@ -111,6 +111,14 @@ namespace hgl
|
||||
for(auto *it:data_list)
|
||||
it->value->End();
|
||||
}
|
||||
|
||||
void Render(RenderCmdBuffer *rcb)
|
||||
{
|
||||
if(!rcb)return;
|
||||
|
||||
for(auto *it:data_list)
|
||||
it->value->Render(rcb);
|
||||
}
|
||||
};
|
||||
}//namespace graph
|
||||
}//namespace hgl
|
||||
|
@ -26,6 +26,8 @@ public:
|
||||
|
||||
const bool Comp(const VertexInputData *vid)const
|
||||
{
|
||||
if(!vid)return(false);
|
||||
|
||||
if(binding_count!=vid->binding_count)return(false);
|
||||
|
||||
for(uint32_t i=0;i<binding_count;i++)
|
||||
@ -75,10 +77,6 @@ public:
|
||||
const VertexInputData * GetVertexInputData ()const{return vertex_input;}
|
||||
|
||||
MaterialParameters *GetMP (const DescriptorSetType &type){return mat_inst->GetMP(type);}
|
||||
|
||||
public: //instance support
|
||||
|
||||
virtual const uint32_t GetInstanceCount ()const{return 1;}
|
||||
};//class Renderable
|
||||
|
||||
Renderable *CreateRenderable(Primitive *,MaterialInstance *,Pipeline *);
|
||||
|
@ -15,81 +15,13 @@ namespace hgl
|
||||
RenderList2D::RenderList2D(GPUDevice *dev)
|
||||
{
|
||||
device =dev;
|
||||
cmd_buf =nullptr;
|
||||
|
||||
last_pipeline =nullptr;
|
||||
hgl_zero(last_mp);
|
||||
last_vbo =0;
|
||||
renderable_count=0;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if(!sn)return(false);
|
||||
@ -123,145 +55,29 @@ namespace hgl
|
||||
{
|
||||
if(!device|!sn)return(false);
|
||||
|
||||
Begin();
|
||||
mrl_map.Begin();
|
||||
ExpendNode(sn);
|
||||
// End();
|
||||
mrl_map.End();
|
||||
|
||||
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)
|
||||
{
|
||||
if(!cb)
|
||||
return(false);
|
||||
|
||||
if(ri_list.GetCount()<=0)
|
||||
if(renderable_count<=0)
|
||||
return(true);
|
||||
|
||||
cmd_buf=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);
|
||||
mrl_map.Render(cb);
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
void RenderList2D::Clear()
|
||||
{
|
||||
mrl_map.Clear();
|
||||
}
|
||||
}//namespace graph
|
||||
}//namespace hgl
|
||||
|
@ -123,10 +123,10 @@ namespace hgl
|
||||
}
|
||||
};//struct RenderNode2DExtraBuffer
|
||||
|
||||
MaterialRenderList2D::MaterialRenderList2D(GPUDevice *d,RenderCmdBuffer *rcb,Material *m)
|
||||
MaterialRenderList2D::MaterialRenderList2D(GPUDevice *d,Material *m)
|
||||
{
|
||||
device=d;
|
||||
cmd_buf=rcb;
|
||||
cmd_buf=nullptr;
|
||||
mtl=m;
|
||||
extra_buffer=nullptr;
|
||||
|
||||
@ -256,13 +256,10 @@ namespace hgl
|
||||
hgl_cpy(buffer_list,vid->buffer_list,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;
|
||||
}
|
||||
|
||||
//Bone组,暂未支持
|
||||
if(count<binding_count) //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);
|
||||
|
||||
@ -293,7 +290,6 @@ namespace hgl
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
if(count!=binding_count)
|
||||
{
|
||||
//还有没支持的绑定组????
|
||||
@ -301,6 +297,8 @@ namespace hgl
|
||||
return(false);
|
||||
}
|
||||
|
||||
cmd_buf->BindVBO(0,count,buffer_list,buffer_offset);
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
@ -314,7 +312,7 @@ namespace hgl
|
||||
last_mi=nullptr;
|
||||
last_vid=nullptr;
|
||||
|
||||
//这里未来尝试换pipeline同时不换mi/primitive是否需要重新绑定primitive
|
||||
//这里未来尝试换pipeline同时不换mi/primitive是否需要重新绑定mi/primitive
|
||||
}
|
||||
|
||||
if(last_mi!=ri->mi)
|
||||
@ -325,22 +323,29 @@ namespace hgl
|
||||
last_vid=nullptr;
|
||||
}
|
||||
|
||||
if(!last_vid->Comp(ri->vid))
|
||||
if(!ri->vid->Comp(last_vid))
|
||||
{
|
||||
Bind(ri->vid,ri->first);
|
||||
last_vid=ri->vid;
|
||||
}
|
||||
|
||||
const IndexBufferData *ibd=last_primitive->GetIndexBuffer();
|
||||
const IndexBufferData *ibd=last_vid->index_buffer;
|
||||
|
||||
if(ib)
|
||||
cmd_buf->DrawIndexed(ib->GetCount(),ri->count);
|
||||
if(ibd->buffer)
|
||||
{
|
||||
cmd_buf->BindIBO(ibd);
|
||||
|
||||
cmd_buf->DrawIndexed(ibd->buffer->GetCount(),ri->count);
|
||||
}
|
||||
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();
|
||||
|
||||
if(count<=0)return;
|
||||
@ -349,6 +354,8 @@ namespace hgl
|
||||
|
||||
if(ri_count<=0)return;
|
||||
|
||||
cmd_buf=rcb;
|
||||
|
||||
RenderItem *ri=ri_list.GetData();
|
||||
|
||||
last_pipeline =nullptr;
|
||||
|
@ -1,5 +1,4 @@
|
||||
#include<hgl/graph/SceneNode.h>
|
||||
#include<hgl/graph/RenderList.h>
|
||||
#include<hgl/graph/VKRenderable.h>
|
||||
namespace hgl
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user