全面使用MaterialInstance封装Material与DescriptorSets

This commit is contained in:
hyzboy 2020-01-20 20:00:03 +08:00
parent 7a0c4e9423
commit c2b7f196ed
21 changed files with 120 additions and 106 deletions

2
CMCore

@ -1 +1 @@
Subproject commit 8f024e159807b3d4e98c474636fda2b555a364e2 Subproject commit 6107f34e21e5f104091d28b3db17e6cbeb7dc967

View File

@ -16,7 +16,7 @@ constexpr uint32_t SCREEN_HEIGHT=128;
struct AtomsphereData struct AtomsphereData
{ {
alignas(16) Vector3f position; Vector3f position;
float intensity; float intensity;
float scattering_direction; float scattering_direction;
};// };//
@ -29,11 +29,11 @@ private:
RenderList render_list; RenderList render_list;
vulkan::Material * material =nullptr; vulkan::Material * material =nullptr;
vulkan::DescriptorSets * descriptor_sets =nullptr; vulkan::MaterialInstance * material_instance =nullptr;
vulkan::Renderable *ro_sphere; vulkan::Renderable * ro_sphere =nullptr;
vulkan::Pipeline *pipeline_solid =nullptr; vulkan::Pipeline * pipeline_solid =nullptr;
vulkan::Buffer * ubo_atomsphere =nullptr; vulkan::Buffer * ubo_atomsphere =nullptr;
AtomsphereData atomsphere_data; AtomsphereData atomsphere_data;
@ -47,10 +47,10 @@ private:
if(!material) if(!material)
return(false); return(false);
descriptor_sets=material->CreateDescriptorSets(); material_instance=material->CreateInstance();
db->Add(material); db->Add(material);
db->Add(descriptor_sets); db->Add(material_instance);
return(true); return(true);
} }
@ -59,7 +59,7 @@ private:
ro_sphere=CreateRenderableSphere(db,material,128); ro_sphere=CreateRenderableSphere(db,material,128);
} }
bool InitAtomsphereUBO(vulkan::DescriptorSets *desc_set,uint bindpoint) bool InitAtomsphereUBO(vulkan::MaterialInstance *mi,const UTF8String &sun_node_name)
{ {
atomsphere_data.position.Set(0,0.1f,-1.0f); atomsphere_data.position.Set(0,0.1f,-1.0f);
atomsphere_data.intensity=22.0f; atomsphere_data.intensity=22.0f;
@ -70,18 +70,18 @@ private:
if(!ubo_atomsphere) if(!ubo_atomsphere)
return(false); return(false);
return desc_set->BindUBO(bindpoint,ubo_atomsphere); return mi->BindUBO(sun_node_name,ubo_atomsphere);
} }
bool InitUBO() bool InitUBO()
{ {
if(!InitCameraUBO(descriptor_sets,material->GetUBO("world"))) if(!InitCameraUBO(material_instance,"world"))
return(false); return(false);
if(!InitAtomsphereUBO(descriptor_sets,material->GetUBO("sun"))) if(!InitAtomsphereUBO(material_instance,"sun"))
return(false); return(false);
descriptor_sets->Update(); material_instance->Update();
return(true); return(true);
} }
@ -104,7 +104,7 @@ private:
bool InitScene() bool InitScene()
{ {
render_root.Add(db->CreateRenderableInstance(pipeline_solid,descriptor_sets,ro_sphere),scale(100)); render_root.Add(db->CreateRenderableInstance(pipeline_solid,material_instance,ro_sphere),scale(100));
render_root.RefreshMatrix(); render_root.RefreshMatrix();
render_root.ExpendToList(&render_list); render_root.ExpendToList(&render_list);

View File

@ -82,10 +82,10 @@ private:
struct SubpassParam struct SubpassParam
{ {
vulkan::Material * material; vulkan::Material * material;
vulkan::DescriptorSets *desc_sets; vulkan::MaterialInstance * material_instance;
vulkan::Pipeline * pipeline_fan; vulkan::Pipeline * pipeline_fan;
vulkan::Pipeline * pipeline_triangles; vulkan::Pipeline * pipeline_triangles;
};// };//
SubpassParam sp_gbuffer; SubpassParam sp_gbuffer;

View File

@ -122,7 +122,7 @@ public:
if(!InitPipeline()) if(!InitPipeline())
return(false); return(false);
BuildCommandBuffer(pipeline,material_instance->GetDescriptorSets(),render_obj); BuildCommandBuffer(pipeline,material_instance,render_obj);
return(true); return(true);
} }
@ -136,7 +136,7 @@ public:
ubo_mvp->Write(&cam.matrix); ubo_mvp->Write(&cam.matrix);
BuildCommandBuffer(pipeline,material_instance->GetDescriptorSets(),render_obj); BuildCommandBuffer(pipeline,material_instance,render_obj);
} }
};//class TestApp:public VulkanApplicationFramework };//class TestApp:public VulkanApplicationFramework

View File

@ -142,9 +142,9 @@ private:
bool InitScene() bool InitScene()
{ {
render_root.Add(db->CreateRenderableInstance(pipeline,material_instance->GetDescriptorSets(),ro_rectangle)); render_root.Add(db->CreateRenderableInstance(pipeline,material_instance,ro_rectangle));
render_root.Add(db->CreateRenderableInstance(pipeline,material_instance->GetDescriptorSets(),ro_round_rectangle)); render_root.Add(db->CreateRenderableInstance(pipeline,material_instance,ro_round_rectangle));
render_root.Add(db->CreateRenderableInstance(pipeline,material_instance->GetDescriptorSets(),ro_circle)); render_root.Add(db->CreateRenderableInstance(pipeline,material_instance,ro_circle));
render_root.ExpendToList(&render_list); render_root.ExpendToList(&render_list);
BuildCommandBuffer(&render_list); BuildCommandBuffer(&render_list);

View File

@ -22,9 +22,9 @@ private:
struct MDP struct MDP
{ {
vulkan::Material * material =nullptr; vulkan::Material * material =nullptr;
vulkan::DescriptorSets *descriptor_sets =nullptr; vulkan::MaterialInstance * material_instance =nullptr;
vulkan::Pipeline * pipeline =nullptr; vulkan::Pipeline * pipeline =nullptr;
}m3d,m2d; }m3d,m2d;
vulkan::Renderable *ro_plane_grid[3], vulkan::Renderable *ro_plane_grid[3],
@ -39,19 +39,19 @@ private:
if(!mdp->material) if(!mdp->material)
return(false); return(false);
mdp->descriptor_sets=mdp->material->CreateDescriptorSets(); mdp->material_instance=mdp->material->CreateInstance();
db->Add(mdp->material); db->Add(mdp->material);
db->Add(mdp->descriptor_sets); db->Add(mdp->material_instance);
return(true); return(true);
} }
bool InitUBO(MDP *mdp) bool InitUBO(MDP *mdp)
{ {
if(!InitCameraUBO(mdp->descriptor_sets,mdp->material->GetUBO("world"))) if(!InitCameraUBO(mdp->material_instance,mdp->material->GetUBO("world")))
return(false); return(false);
mdp->descriptor_sets->Update(); mdp->material_instance->Update();
return(true); return(true);
} }
@ -87,11 +87,11 @@ private:
bool InitScene() bool InitScene()
{ {
render_root.Add(db->CreateRenderableInstance(m2d.pipeline,m2d.descriptor_sets,ro_round_rectangle)); render_root.Add(db->CreateRenderableInstance(m2d.pipeline,m2d.material_instance,ro_round_rectangle));
render_root.Add(db->CreateRenderableInstance(m3d.pipeline,m3d.descriptor_sets,ro_plane_grid[0])); render_root.Add(db->CreateRenderableInstance(m3d.pipeline,m3d.material_instance,ro_plane_grid[0]));
render_root.Add(db->CreateRenderableInstance(m3d.pipeline,m3d.descriptor_sets,ro_plane_grid[1]),rotate(HGL_RAD_90,0,1,0)); render_root.Add(db->CreateRenderableInstance(m3d.pipeline,m3d.material_instance,ro_plane_grid[1]),rotate(HGL_RAD_90,0,1,0));
render_root.Add(db->CreateRenderableInstance(m3d.pipeline,m3d.descriptor_sets,ro_plane_grid[2]),rotate(HGL_RAD_90,1,0,0)); render_root.Add(db->CreateRenderableInstance(m3d.pipeline,m3d.material_instance,ro_plane_grid[2]),rotate(HGL_RAD_90,1,0,0));
render_root.RefreshMatrix(); render_root.RefreshMatrix();
render_root.ExpendToList(&render_list); render_root.ExpendToList(&render_list);

View File

@ -164,7 +164,7 @@ private:
bool Add(struct MPD *mpd,const Matrix4f &offset) bool Add(struct MPD *mpd,const Matrix4f &offset)
{ {
RenderableInstance *ri=db->CreateRenderableInstance(mpd->pipeline,mpd->material_instance->GetDescriptorSets(),mpd->render_obj); RenderableInstance *ri=db->CreateRenderableInstance(mpd->pipeline,mpd->material_instance,mpd->render_obj);
if(!ri)return(false); if(!ri)return(false);

View File

@ -22,7 +22,7 @@ private:
RenderList render_list; RenderList render_list;
vulkan::Material * material =nullptr; vulkan::Material * material =nullptr;
vulkan::DescriptorSets * descriptor_sets =nullptr; vulkan::MaterialInstance * material_instance =nullptr;
vulkan::Renderable *ro_plane_grid, vulkan::Renderable *ro_plane_grid,
*ro_cube, *ro_cube,
@ -44,10 +44,10 @@ private:
if(!material) if(!material)
return(false); return(false);
descriptor_sets=material->CreateDescriptorSets(); material_instance=material->CreateInstance();
db->Add(material); db->Add(material);
db->Add(descriptor_sets); db->Add(material_instance);
return(true); return(true);
} }
@ -127,10 +127,10 @@ private:
bool InitUBO() bool InitUBO()
{ {
if(!InitCameraUBO(descriptor_sets,material->GetUBO("world"))) if(!InitCameraUBO(material_instance,"world"))
return(false); return(false);
descriptor_sets->Update(); material_instance->Update();
return(true); return(true);
} }
@ -160,13 +160,13 @@ private:
bool InitScene() bool InitScene()
{ {
render_root.Add(db->CreateRenderableInstance(pipeline_line,descriptor_sets,ro_plane_grid)); render_root.Add(db->CreateRenderableInstance(pipeline_line,material_instance,ro_plane_grid));
//render_root.Add(db->CreateRenderableInstance(pipeline_solid,descriptor_sets,ro_dome)); //render_root.Add(db->CreateRenderableInstance(pipeline_solid,material_instance,ro_dome));
render_root.Add(db->CreateRenderableInstance(pipeline_solid,descriptor_sets,ro_torus)); render_root.Add(db->CreateRenderableInstance(pipeline_solid,material_instance,ro_torus));
render_root.Add(db->CreateRenderableInstance(pipeline_solid,descriptor_sets,ro_cube ),translate(-10, 0, 5)*scale(10,10,10)); render_root.Add(db->CreateRenderableInstance(pipeline_solid,material_instance,ro_cube ),translate(-10, 0, 5)*scale(10,10,10));
render_root.Add(db->CreateRenderableInstance(pipeline_solid,descriptor_sets,ro_sphere ),translate( 10, 0, 5)*scale(10,10,10)); render_root.Add(db->CreateRenderableInstance(pipeline_solid,material_instance,ro_sphere ),translate( 10, 0, 5)*scale(10,10,10));
render_root.Add(db->CreateRenderableInstance(pipeline_solid,descriptor_sets,ro_cylinder ),translate( 0, 16, 0)); render_root.Add(db->CreateRenderableInstance(pipeline_solid,material_instance,ro_cylinder ),translate( 0, 16, 0));
render_root.Add(db->CreateRenderableInstance(pipeline_solid,descriptor_sets,ro_cone ),translate( 0,-16, 0)); render_root.Add(db->CreateRenderableInstance(pipeline_solid,material_instance,ro_cone ),translate( 0,-16, 0));
render_root.RefreshMatrix(); render_root.RefreshMatrix();
render_root.ExpendToList(&render_list); render_root.ExpendToList(&render_list);

View File

@ -81,7 +81,7 @@ class TestApp:public ViewModelFramework
private: private:
vulkan::Material * material =nullptr; vulkan::Material * material =nullptr;
vulkan::DescriptorSets * descriptor_sets =nullptr; vulkan::MaterialInstance * material_instance =nullptr;
vulkan::Pipeline * pipeline_wireframe =nullptr; vulkan::Pipeline * pipeline_wireframe =nullptr;
vulkan::Pipeline * pipeline_lines =nullptr; vulkan::Pipeline * pipeline_lines =nullptr;
@ -108,10 +108,10 @@ private:
if(!material) if(!material)
return(false); return(false);
descriptor_sets=material->CreateDescriptorSets(); material_instance=material->CreateInstance();
db->Add(material); db->Add(material);
db->Add(descriptor_sets); db->Add(material_instance);
return(true); return(true);
} }
@ -126,7 +126,7 @@ private:
for(uint i=0;i<count;i++) for(uint i=0;i<count;i++)
{ {
mesh_renderable[i]=CreateMeshRenderable(db,material,*md); mesh_renderable[i]=CreateMeshRenderable(db,material,*md);
mesh_renderable_instance[i]=db->CreateRenderableInstance(pipeline_wireframe,descriptor_sets,mesh_renderable[i]); mesh_renderable_instance[i]=db->CreateRenderableInstance(pipeline_wireframe,material_instance,mesh_renderable[i]);
++md; ++md;
} }
@ -137,7 +137,7 @@ private:
aci.size=model_data->bounding_box.HalfSize().xyz(); aci.size=model_data->bounding_box.HalfSize().xyz();
axis_renderable=CreateRenderableAxis(db,material,&aci); axis_renderable=CreateRenderableAxis(db,material,&aci);
axis_renderable_instance=db->CreateRenderableInstance(pipeline_lines,descriptor_sets,axis_renderable); axis_renderable_instance=db->CreateRenderableInstance(pipeline_lines,material_instance,axis_renderable);
} }
{ {
@ -147,16 +147,16 @@ private:
cci.size=model_data->bounding_box.Size().xyz(); cci.size=model_data->bounding_box.Size().xyz();
bbox_renderable=CreateRenderableBoundingBox(db,material,&cci); bbox_renderable=CreateRenderableBoundingBox(db,material,&cci);
bbox_renderable_instance=db->CreateRenderableInstance(pipeline_lines,descriptor_sets,bbox_renderable); bbox_renderable_instance=db->CreateRenderableInstance(pipeline_lines,material_instance,bbox_renderable);
} }
} }
bool InitUBO() bool InitUBO()
{ {
if(!InitCameraUBO(descriptor_sets,material->GetUBO("world"))) if(!InitCameraUBO(material_instance,"world"))
return(false); return(false);
descriptor_sets->Update(); material_instance->Update();
return(true); return(true);
} }

View File

@ -25,7 +25,7 @@ private:
RenderList render_list; RenderList render_list;
vulkan::Material * material =nullptr; vulkan::Material * material =nullptr;
vulkan::DescriptorSets * descriptor_sets =nullptr; vulkan::MaterialInstance * material_instance =nullptr;
vulkan::Renderable * renderable_object =nullptr; vulkan::Renderable * renderable_object =nullptr;
@ -49,10 +49,10 @@ private:
if(!material) if(!material)
return(false); return(false);
descriptor_sets=material->CreateDescriptorSets(); material_instance=material->CreateInstance();
db->Add(material); db->Add(material);
db->Add(descriptor_sets); db->Add(material_instance);
return(true); return(true);
} }
@ -65,10 +65,10 @@ private:
bool InitUBO() bool InitUBO()
{ {
if(!InitCameraUBO(descriptor_sets,material->GetUBO("world"))) if(!InitCameraUBO(material_instance,"world"))
return(false); return(false);
descriptor_sets->Update(); material_instance->Update();
return(true); return(true);
} }
@ -97,7 +97,7 @@ private:
uint count; uint count;
float size; float size;
RenderableInstance *ri=db->CreateRenderableInstance(pipeline_line,descriptor_sets,renderable_object); RenderableInstance *ri=db->CreateRenderableInstance(pipeline_line,material_instance,renderable_object);
for(uint i=0;i<360;i++) for(uint i=0;i<360;i++)
{ {

View File

@ -69,14 +69,14 @@ public:
origin_matrix=object_matrix; origin_matrix=object_matrix;
} }
bool InitCameraUBO(vulkan::DescriptorSets *desc_set,uint world_matrix_bindpoint) bool InitCameraUBO(vulkan::MaterialInstance *mi,const UTF8String &world_matrix_name)
{ {
ubo_world_matrix=db->CreateUBO(sizeof(WorldMatrix),&camera.matrix); ubo_world_matrix=db->CreateUBO(sizeof(WorldMatrix),&camera.matrix);
if(!ubo_world_matrix) if(!ubo_world_matrix)
return(false); return(false);
return desc_set->BindUBO(world_matrix_bindpoint,ubo_world_matrix); return mi->BindUBO(world_matrix_name,ubo_world_matrix);
} }
void Resize(int w,int h)override void Resize(int w,int h)override

View File

@ -54,17 +54,17 @@ protected:
vulkan::Semaphore * present_complete_semaphore =nullptr, vulkan::Semaphore * present_complete_semaphore =nullptr,
* render_complete_semaphore =nullptr; * render_complete_semaphore =nullptr;
vulkan::ShaderModuleManage * shader_manage =nullptr; vulkan::ShaderModuleManage * shader_manage =nullptr;
protected: protected:
uint32_t swap_chain_count=0; int32_t swap_chain_count =0;
vulkan::CommandBuffer ** cmd_buf =nullptr; vulkan::CommandBuffer ** cmd_buf =nullptr;
protected: protected:
SceneDB * db =nullptr; SceneDB * db =nullptr;
bool key_status[kbRangeSize]; bool key_status[kbRangeSize];
@ -181,9 +181,9 @@ public:
} }
} }
void BuildCommandBuffer(uint32_t index,vulkan::Pipeline *p,vulkan::DescriptorSets *ds,vulkan::Renderable *r) void BuildCommandBuffer(uint32_t index,vulkan::Pipeline *p,vulkan::MaterialInstance *mi,vulkan::Renderable *r)
{ {
if(!p||!ds||!r) if(!p||!mi||!r)
return; return;
const vulkan::IndexBuffer *ib=r->GetIndexBuffer(); const vulkan::IndexBuffer *ib=r->GetIndexBuffer();
@ -193,7 +193,7 @@ public:
cb->Begin(); cb->Begin();
cb->BeginRenderPass(sc_render_target->GetRenderPass(),sc_render_target->GetFramebuffer(index)); cb->BeginRenderPass(sc_render_target->GetRenderPass(),sc_render_target->GetFramebuffer(index));
cb->Bind(p); cb->Bind(p);
cb->Bind(ds); cb->Bind(mi->GetDescriptorSets());
cb->Bind(r); cb->Bind(r);
if (ib) if (ib)
@ -205,15 +205,15 @@ public:
cb->End(); cb->End();
} }
void BuildCommandBuffer(vulkan::Pipeline *p,vulkan::DescriptorSets *ds,vulkan::Renderable *r) void BuildCommandBuffer(vulkan::Pipeline *p,vulkan::MaterialInstance *mi,vulkan::Renderable *r)
{ {
for(uint32_t i=0;i<swap_chain_count;i++) for(uint32_t i=0;i<swap_chain_count;i++)
BuildCommandBuffer(i,p,ds,r); BuildCommandBuffer(i,p,mi,r);
} }
void BuildCurrentCommandBuffer(vulkan::Pipeline *p,vulkan::DescriptorSets *ds,vulkan::Renderable *r) void BuildCurrentCommandBuffer(vulkan::Pipeline *p,vulkan::MaterialInstance *mi,vulkan::Renderable *r)
{ {
BuildCommandBuffer(sc_render_target->GetCurrentFrameIndices(),p,ds,r); BuildCommandBuffer(sc_render_target->GetCurrentFrameIndices(),p,mi,r);
} }
void BuildCommandBuffer(uint32_t index,RenderList *rl) void BuildCommandBuffer(uint32_t index,RenderList *rl)
@ -321,14 +321,14 @@ public:
camera.height=h; camera.height=h;
} }
bool InitCameraUBO(vulkan::DescriptorSets *desc_set,uint world_matrix_bindpoint) bool InitCameraUBO(vulkan::MaterialInstance *mi,const UTF8String &node_name)
{ {
ubo_world_matrix=db->CreateUBO(sizeof(WorldMatrix),&camera.matrix); ubo_world_matrix=db->CreateUBO(sizeof(WorldMatrix),&camera.matrix);
if(!ubo_world_matrix) if(!ubo_world_matrix)
return(false); return(false);
return desc_set->BindUBO(world_matrix_bindpoint,ubo_world_matrix); return mi->BindUBO(node_name,ubo_world_matrix);
} }
virtual void BuildCommandBuffer(uint32_t index)=0; virtual void BuildCommandBuffer(uint32_t index)=0;

View File

@ -143,7 +143,7 @@ public:
if(!InitPipeline()) if(!InitPipeline())
return(false); return(false);
BuildCommandBuffer(pipeline,material_instance->GetDescriptorSets(),render_obj); BuildCommandBuffer(pipeline,material_instance,render_obj);
return(true); return(true);
} }
@ -157,7 +157,7 @@ public:
ubo_mvp->Write(&cam.matrix); ubo_mvp->Write(&cam.matrix);
BuildCommandBuffer(pipeline,material_instance->GetDescriptorSets(),render_obj); BuildCommandBuffer(pipeline,material_instance,render_obj);
} }
};//class TestApp:public VulkanApplicationFramework };//class TestApp:public VulkanApplicationFramework

View File

@ -146,14 +146,14 @@ public:
if(!InitPipeline()) if(!InitPipeline())
return(false); return(false);
BuildCommandBuffer(pipeline,material_instance->GetDescriptorSets(),render_obj); BuildCommandBuffer(pipeline,material_instance,render_obj);
return(true); return(true);
} }
void Resize(int,int)override void Resize(int,int)override
{ {
BuildCommandBuffer(pipeline,material_instance->GetDescriptorSets(),render_obj); BuildCommandBuffer(pipeline,material_instance,render_obj);
} }
};//class TestApp:public VulkanApplicationFramework };//class TestApp:public VulkanApplicationFramework

View File

@ -179,7 +179,7 @@ public:
if(!InitPipeline()) if(!InitPipeline())
return(false); return(false);
BuildCommandBuffer(pipeline,material_instance->GetDescriptorSets(),render_obj); BuildCommandBuffer(pipeline,material_instance,render_obj);
return(true); return(true);
} }
@ -193,7 +193,7 @@ public:
ubo_mvp->Write(&cam.matrix); ubo_mvp->Write(&cam.matrix);
BuildCommandBuffer(pipeline,material_instance->GetDescriptorSets(),render_obj); BuildCommandBuffer(pipeline,material_instance,render_obj);
} }
};//class TestApp:public VulkanApplicationFramework };//class TestApp:public VulkanApplicationFramework

View File

@ -19,10 +19,10 @@ namespace hgl
List<SceneNode *> scene_node_list; List<SceneNode *> scene_node_list;
vulkan::PushConstant * last_pc; vulkan::PushConstant * last_pc;
vulkan::Pipeline * last_pipeline; vulkan::Pipeline * last_pipeline;
vulkan::DescriptorSets *last_desc_sets; vulkan::MaterialInstance * last_mat_inst;
vulkan::Renderable * last_renderable; vulkan::Renderable * last_renderable;
void Render(SceneNode *,RenderableInstance *); void Render(SceneNode *,RenderableInstance *);
void Render(SceneNode *,List<RenderableInstance *> &); void Render(SceneNode *,List<RenderableInstance *> &);
@ -34,7 +34,7 @@ namespace hgl
cmd_buf=nullptr; cmd_buf=nullptr;
last_pc=nullptr; last_pc=nullptr;
last_pipeline=nullptr; last_pipeline=nullptr;
last_desc_sets=nullptr; last_mat_inst=nullptr;
last_renderable=nullptr; last_renderable=nullptr;
} }

View File

@ -14,21 +14,21 @@ namespace hgl
class RenderableInstance class RenderableInstance
{ {
vulkan::Pipeline * pipeline; vulkan::Pipeline * pipeline;
vulkan::DescriptorSets * desc_sets; vulkan::MaterialInstance *mat_inst;
vulkan::Renderable * render_obj; vulkan::Renderable * render_obj;
public: public:
RenderableInstance(vulkan::Pipeline *p,vulkan::DescriptorSets *ds,vulkan::Renderable *r):pipeline(p),desc_sets(ds),render_obj(r){} RenderableInstance(vulkan::Pipeline *p,vulkan::MaterialInstance *mi,vulkan::Renderable *r):pipeline(p),mat_inst(mi),render_obj(r){}
virtual ~RenderableInstance() virtual ~RenderableInstance()
{ {
//需要在这里添加删除pipeline/desc_sets/render_obj引用计数的代码 //需要在这里添加删除pipeline/desc_sets/render_obj引用计数的代码
} }
vulkan::Pipeline * GetPipeline (){return pipeline;} vulkan::Pipeline * GetPipeline (){return pipeline;}
vulkan::DescriptorSets *GetDescriptorSets (){return desc_sets;} vulkan::MaterialInstance * GetMaterialInstance (){return mat_inst;}
vulkan::Renderable * GetRenderable (){return render_obj;} vulkan::Renderable * GetRenderable (){return render_obj;}
const AABB & GetBoundingBox ()const{return render_obj->GetBoundingBox();} const AABB & GetBoundingBox ()const{return render_obj->GetBoundingBox();}
const int Comp(const RenderableInstance *ri)const const int Comp(const RenderableInstance *ri)const
{ {
@ -63,8 +63,8 @@ namespace hgl
if(pipeline!=ri->pipeline) if(pipeline!=ri->pipeline)
return pipeline-ri->pipeline; return pipeline-ri->pipeline;
if(desc_sets!=ri->desc_sets) if(mat_inst!=ri->mat_inst)
return desc_sets-ri->desc_sets; return int64(mat_inst)-int64(ri->mat_inst);
return render_obj-ri->render_obj; return render_obj-ri->render_obj;
} }

View File

@ -6,6 +6,7 @@
#include<hgl/graph/vulkan/VKDescriptorSets.h> #include<hgl/graph/vulkan/VKDescriptorSets.h>
#include<hgl/graph/vulkan/VKRenderable.h> #include<hgl/graph/vulkan/VKRenderable.h>
#include<hgl/graph/vulkan/VKBuffer.h> #include<hgl/graph/vulkan/VKBuffer.h>
#include<hgl/graph/vulkan/VKMaterialInstance.h>
#include<hgl/graph/VertexBufferCreater.h> #include<hgl/graph/VertexBufferCreater.h>
#include<hgl/graph/RenderableInstance.h> #include<hgl/graph/RenderableInstance.h>
#include<hgl/type/ResManage.h> #include<hgl/type/ResManage.h>
@ -73,7 +74,7 @@ namespace hgl
vulkan::IndexBuffer *CreateIBO16(uint32_t count,VkSharingMode sharing_mode=VK_SHARING_MODE_EXCLUSIVE){return CreateIBO(VK_INDEX_TYPE_UINT16,count,nullptr,sharing_mode);} vulkan::IndexBuffer *CreateIBO16(uint32_t count,VkSharingMode sharing_mode=VK_SHARING_MODE_EXCLUSIVE){return CreateIBO(VK_INDEX_TYPE_UINT16,count,nullptr,sharing_mode);}
vulkan::IndexBuffer *CreateIBO32(uint32_t count,VkSharingMode sharing_mode=VK_SHARING_MODE_EXCLUSIVE){return CreateIBO(VK_INDEX_TYPE_UINT32,count,nullptr,sharing_mode);} vulkan::IndexBuffer *CreateIBO32(uint32_t count,VkSharingMode sharing_mode=VK_SHARING_MODE_EXCLUSIVE){return CreateIBO(VK_INDEX_TYPE_UINT32,count,nullptr,sharing_mode);}
RenderableInstance * CreateRenderableInstance(vulkan::Pipeline *p,vulkan::DescriptorSets *ds,vulkan::Renderable *r); RenderableInstance * CreateRenderableInstance(vulkan::Pipeline *p,vulkan::MaterialInstance *mi,vulkan::Renderable *r);
vulkan::Material * GetMaterial (const MaterialID &id){return rm_material.Get(id);} vulkan::Material * GetMaterial (const MaterialID &id){return rm_material.Get(id);}
vulkan::MaterialInstance * GetMaterialInstance (const MaterialInstanceID &id){return rm_material_instance.Get(id);} vulkan::MaterialInstance * GetMaterialInstance (const MaterialInstanceID &id){return rm_material_instance.Get(id);}

View File

@ -3,18 +3,30 @@
layout(location = 0) in vec3 Vertex; layout(location = 0) in vec3 Vertex;
layout(location = 1) in vec4 Color; layout(location = 1) in vec4 Color;
layout(binding = 0) uniform WorldMatrix layout(binding=0) uniform WorldMatrix // hgl/math/Math.h
{ {
mat4 ortho; mat4 ortho;
mat4 projection; mat4 projection;
mat4 inverse_projection;
mat4 modelview; mat4 modelview;
mat4 inverse_modelview;
mat4 mvp; mat4 mvp;
mat4 inverse_mvp;
vec4 view_pos; vec4 view_pos;
vec2 resolution;
} world; } world;
layout(push_constant) uniform Consts { layout(push_constant) uniform Consts
{
mat4 local_to_world; mat4 local_to_world;
} pc; mat3 normal;
vec3 object_position;
vec3 object_size;
}pc;
layout(location = 0) out vec4 FragmentColor; layout(location = 0) out vec4 FragmentColor;

View File

@ -5,6 +5,7 @@
#include<hgl/graph/vulkan/VKCommandBuffer.h> #include<hgl/graph/vulkan/VKCommandBuffer.h>
#include<hgl/graph/VertexBuffer.h> #include<hgl/graph/VertexBuffer.h>
#include<hgl/graph/RenderableInstance.h> #include<hgl/graph/RenderableInstance.h>
#include<hgl/graph/vulkan/VKMaterialInstance.h>
namespace hgl namespace hgl
{ {
@ -34,14 +35,14 @@ namespace hgl
cmd_buf->Bind(last_pipeline); cmd_buf->Bind(last_pipeline);
last_desc_sets=nullptr; last_mat_inst=nullptr;
} }
if(last_desc_sets!=ri->GetDescriptorSets()) if(last_mat_inst!=ri->GetMaterialInstance())
{ {
last_desc_sets=ri->GetDescriptorSets(); last_mat_inst=ri->GetMaterialInstance();
cmd_buf->Bind(last_desc_sets); cmd_buf->Bind(last_mat_inst->GetDescriptorSets());
} }
if(last_pc!=node->GetPushConstant()) if(last_pc!=node->GetPushConstant())
@ -94,7 +95,7 @@ namespace hgl
cmd_buf=cb; cmd_buf=cb;
last_pipeline=nullptr; last_pipeline=nullptr;
last_desc_sets=nullptr; last_mat_inst=nullptr;
last_renderable=nullptr; last_renderable=nullptr;
last_pc=nullptr; last_pc=nullptr;

View File

@ -51,12 +51,12 @@ namespace hgl
return(buf); return(buf);
} }
RenderableInstance *SceneDB::CreateRenderableInstance(vulkan::Pipeline *p,vulkan::DescriptorSets *ds,vulkan::Renderable *r) RenderableInstance *SceneDB::CreateRenderableInstance(vulkan::Pipeline *p,vulkan::MaterialInstance *mi,vulkan::Renderable *r)
{ {
if(!p||!ds||!r) if(!p||!mi||!r)
return(nullptr); return(nullptr);
RenderableInstance *ri=new RenderableInstance(p,ds,r); RenderableInstance *ri=new RenderableInstance(p,mi,r);
Add(ri); Add(ri);