From 8a99a331c0e89f93e8f945ca5cff42813407db78 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Thu, 22 May 2025 01:11:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86WorkObject=E5=A2=9E=E5=8A=A0bool=20Ini?= =?UTF-8?q?t()=3D0=E7=BA=AF=E8=99=9A=E5=87=BD=E6=95=B0=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E8=B4=9F=E8=B4=A3=E5=88=9D=E5=A7=8B=E5=8C=96=E3=80=82=20?= =?UTF-8?q?=E8=BF=99=E4=B8=80=E5=88=86=E6=94=AF=E7=BB=93=E6=9D=9F=EF=BC=8C?= =?UTF-8?q?=E4=B8=8B=E4=B8=80=E5=88=86=E6=94=AF=E5=9F=BA=E4=BA=8E03=5Fauto?= =?UTF-8?q?=5Finstance=E8=8C=83=E4=BE=8B=E5=BC=80=E5=8F=91WorldManager?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E5=85=A8=E5=B1=80World=E7=AE=A1=E7=90=86?= =?UTF-8?q?=EF=BC=8C=E7=9B=B4=E6=8E=A5=E7=94=B1World=E6=8F=90=E4=BE=9BRend?= =?UTF-8?q?erList/SceneRoot=E7=AD=89=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Basic/BillboardTest.cpp | 35 ++++++++++------- example/Basic/auto_instance.cpp | 15 ++++++-- .../Basic/auto_merge_material_instance.cpp | 17 ++++++--- example/Basic/draw_triangle_in_NDC.cpp | 14 ++++--- example/Basic/draw_triangle_use_UBO.cpp | 12 ++++-- example/Basic/rf_test.cpp | 12 ++++-- inc/hgl/WorkManager.h | 10 ++++- inc/hgl/WorkObject.h | 5 ++- inc/hgl/graph/VKPrimitive.h | 2 +- inc/hgl/graph/VKRenderResource.h | 38 +++++++++---------- src/SceneGraph/Vulkan/VKPrimitive.cpp | 2 +- src/SceneGraph/Vulkan/VKPrimitiveData.cpp | 2 +- src/SceneGraph/Vulkan/VKPrimitiveData.h | 2 +- src/Work/WorkObject.cpp | 4 ++ 14 files changed, 109 insertions(+), 61 deletions(-) diff --git a/example/Basic/BillboardTest.cpp b/example/Basic/BillboardTest.cpp index 3d70962f..cfb3c987 100644 --- a/example/Basic/BillboardTest.cpp +++ b/example/Basic/BillboardTest.cpp @@ -1,6 +1,6 @@ // Billboard -#include"VulkanAppFramework.h" +#include #include #include #include @@ -11,6 +11,7 @@ #include #include #include +#include using namespace hgl; using namespace hgl::graph; @@ -25,12 +26,16 @@ static float lumiance_data[2]={1,1}; static Color4f white_color(1,1,1,1); static Color4f yellow_color(1,1,0,1); -class TestApp:public SceneAppFramework +class TestApp:public WorkObject { Color4f color; private: + AutoDelete render_list =nullptr; + + SceneNode render_root; + Material * mtl_plane_grid =nullptr; MaterialInstance * mi_plane_grid =nullptr; Pipeline * pipeline_plane_grid =nullptr; @@ -90,7 +95,9 @@ private: bool InitTexture() { - texture=db->LoadTexture2D(OS_TEXT("res/image/lena.Tex2D"),true); + TextureManager *tex_manager=GetTextureManager(); + + texture=tex_manager->LoadTexture2D(OS_TEXT("res/image/lena.Tex2D"),true); if(!texture)return(false); sampler=db->CreateSampler(); @@ -107,7 +114,7 @@ private: return(true); } - + Mesh *Add(Primitive *r,MaterialInstance *mi,Pipeline *p) { Mesh *ri=db->CreateMesh(r,mi,p); @@ -126,9 +133,9 @@ private: bool CreateRenderObject() { using namespace inline_geometry; - + { - PrimitiveCreater pc(device,mi_plane_grid->GetVIL()); + PrimitiveCreater pc(GetDevice(),mi_plane_grid->GetVIL()); struct PlaneGridCreateInfo pgci; @@ -142,7 +149,7 @@ private: } { - PrimitiveCreater pc(device,mi_billboard->GetVIL()); + PrimitiveCreater pc(GetDevice(),mi_billboard->GetVIL()); pc.Init("Billboard",1); @@ -150,6 +157,7 @@ private: return(false); ro_billboard=db->CreateMesh(&pc,mi_billboard,pipeline_billboard); + if(!ro_billboard) return(false); } @@ -175,16 +183,15 @@ private: public: + using WorkObject::WorkObject; + ~TestApp() { SAFE_CLEAR(prim_plane_grid); } - bool Init(uint w,uint h) + bool Init() { - if(!SceneAppFramework::Init(w,h)) - return(false); - if(!InitPlaneGridMP()) return(false); @@ -202,9 +209,9 @@ public: return(true); } -};//class TestApp:public CameraAppFramework +};//class TestApp:public WorkObject -int main(int,char **) +int os_main(int,os_char **) { - return RunApp(1920,1080); + return RunFramework(OS_TEXT("AutoInstance"),1024,1024); } diff --git a/example/Basic/auto_instance.cpp b/example/Basic/auto_instance.cpp index 605d4efd..4e64f825 100644 --- a/example/Basic/auto_instance.cpp +++ b/example/Basic/auto_instance.cpp @@ -98,15 +98,22 @@ private: public: - TestApp(RenderFramework *rf):WorkObject(rf,rf->GetSwapchainRenderTarget()) + using WorkObject::WorkObject; + + bool Init() override { - render_list=rf->CreateRenderList(); + render_list=GetRenderFramework()->CreateRenderList(); + + if(!render_list) + return(false); if(!InitMaterial()) - return; + return(false); if(!InitVBO()) - return; + return(false); + + return(true); } void Render(double delta_time,graph::RenderCmdBuffer *cmd)override diff --git a/example/Basic/auto_merge_material_instance.cpp b/example/Basic/auto_merge_material_instance.cpp index 6081023c..d578bb91 100644 --- a/example/Basic/auto_merge_material_instance.cpp +++ b/example/Basic/auto_merge_material_instance.cpp @@ -111,15 +111,22 @@ private: public: - TestApp(RenderFramework *rf):WorkObject(rf,rf->GetSwapchainRenderTarget()) + using WorkObject::WorkObject; + + bool Init() override { - render_list=rf->CreateRenderList(); + render_list=GetRenderFramework()->CreateRenderList(); + + if(!render_list) + return(false); if(!InitMaterial()) - return; + return(false); if(!InitVBOAndRenderList()) - return; + return(false); + + return(true); } void Render(double delta_time,graph::RenderCmdBuffer *cmd)override @@ -130,7 +137,7 @@ public: render_list->Render(cmd); cmd->EndRenderPass(); } -};//class TestApp:public VulkanApplicationFramework +};//class TestApp:public WorkObject int os_main(int,os_char **) { diff --git a/example/Basic/draw_triangle_in_NDC.cpp b/example/Basic/draw_triangle_in_NDC.cpp index 7a7d76c1..712d4cef 100644 --- a/example/Basic/draw_triangle_in_NDC.cpp +++ b/example/Basic/draw_triangle_in_NDC.cpp @@ -119,19 +119,23 @@ private: } public: - - TestApp(RenderFramework *rf):WorkObject(rf,rf->GetSwapchainRenderTarget()) + + using WorkObject::WorkObject; + + bool Init() override { InitVIL(); if(!InitAutoMaterial()) - return; + return(false); if(!InitPipeline()) - return; + return(false); if(!InitVBO()) - return; + return(false); + + return(true); } void Tick(double)override{} diff --git a/example/Basic/draw_triangle_use_UBO.cpp b/example/Basic/draw_triangle_use_UBO.cpp index b07a0bb7..97c272a7 100644 --- a/example/Basic/draw_triangle_use_UBO.cpp +++ b/example/Basic/draw_triangle_use_UBO.cpp @@ -90,14 +90,18 @@ private: } public: - - TestApp(RenderFramework *rf):WorkObject(rf,rf->GetSwapchainRenderTarget()) + + using WorkObject::WorkObject; + + bool Init() override { if(!InitMaterial()) - return; + return(false); if(!InitVBO()) - return; + return(false); + + return(true); } void Render(double delta_time,graph::RenderCmdBuffer *cmd)override diff --git a/example/Basic/rf_test.cpp b/example/Basic/rf_test.cpp index bcc0d88e..b4b5c8bb 100644 --- a/example/Basic/rf_test.cpp +++ b/example/Basic/rf_test.cpp @@ -71,16 +71,20 @@ private: public: - TestApp(RenderFramework *rf):WorkObject(rf,rf->GetSwapchainRenderTarget()) + using WorkObject::WorkObject; + + bool Init() override { if(!InitAutoMaterial()) - return; + return(false); if(!InitPipeline()) - return; + return(false); if(!InitVBO()) - return; + return(false); + + return(true); } void Tick(double)override{} diff --git a/inc/hgl/WorkManager.h b/inc/hgl/WorkManager.h index 6c690233..95982b99 100644 --- a/inc/hgl/WorkManager.h +++ b/inc/hgl/WorkManager.h @@ -80,7 +80,15 @@ namespace hgl SwapchainWorkManager wm(&rf); - wm.Run(new WO(&rf)); + WO *wo=new WO(&rf); + + if(!wo->Init()) + { + delete wo; + return(-2); + } + + wm.Run(wo); return 0; } diff --git a/inc/hgl/WorkObject.h b/inc/hgl/WorkObject.h index 8e5fc88e..605bc486 100644 --- a/inc/hgl/WorkObject.h +++ b/inc/hgl/WorkObject.h @@ -38,6 +38,7 @@ namespace hgl graph::RenderFramework * GetRenderFramework (){return render_framework;} graph::VulkanDevice * GetDevice (){return render_framework->GetDevice();} graph::VulkanDevAttr * GetDevAttr (){return render_framework->GetDevAttr();} + graph::TextureManager * GetTextureManager (){return render_framework->GetTextureManager();} const VkExtent2D & GetExtent2D (){return cur_render_target->GetExtent();} @@ -53,9 +54,11 @@ namespace hgl public: - WorkObject(graph::RenderFramework *,graph::IRenderTarget *); + WorkObject(graph::RenderFramework *,graph::IRenderTarget *rt=nullptr); virtual ~WorkObject()=default; + virtual bool Init()=0; + virtual void OnRenderTargetSwitch(graph::RenderFramework *rf,graph::IRenderTarget *rt); virtual void OnResize(const VkExtent2D &){} diff --git a/inc/hgl/graph/VKPrimitive.h b/inc/hgl/graph/VKPrimitive.h index 8f8569f8..4ab02764 100644 --- a/inc/hgl/graph/VKPrimitive.h +++ b/inc/hgl/graph/VKPrimitive.h @@ -33,7 +33,7 @@ public: const AnsiString & GetName ()const{ return prim_name; } const VkDeviceSize GetVertexCount ()const; - const int GetVABCount ()const; + const uint32_t GetVABCount ()const; const int GetVABIndex (const AnsiString &name)const; VAB * GetVAB (const int); VAB * GetVAB (const AnsiString &name){return GetVAB(GetVABIndex(name));} diff --git a/inc/hgl/graph/VKRenderResource.h b/inc/hgl/graph/VKRenderResource.h index 673363b5..5ebb2aa7 100644 --- a/inc/hgl/graph/VKRenderResource.h +++ b/inc/hgl/graph/VKRenderResource.h @@ -127,9 +127,9 @@ public: //Material const ShaderModule *CreateShaderModule(const AnsiString &shader_module_name,const ShaderCreateInfo *); - Material * CreateMaterial(const AnsiString &mtl_name,const mtl::MaterialCreateInfo *); - Material * LoadMaterial(const AnsiString &,mtl::Material2DCreateConfig *); - Material * LoadMaterial(const AnsiString &,mtl::Material3DCreateConfig *); + Material * CreateMaterial (const AnsiString &,const mtl::MaterialCreateInfo *); + Material * LoadMaterial (const AnsiString &,mtl::Material2DCreateConfig *); + Material * LoadMaterial (const AnsiString &,mtl::Material3DCreateConfig *); MaterialInstance * CreateMaterialInstance(Material *,const VILConfig *vil_cfg=nullptr); @@ -143,31 +143,31 @@ public: //Material MaterialInstance * CreateMaterialInstance(const AnsiString &mtl_name,const mtl::MaterialCreateInfo *,const VILConfig *vil_cfg=nullptr); - Mesh * CreateMesh(Primitive *r,MaterialInstance *mi,Pipeline *p); - Mesh * CreateMesh(PrimitiveCreater *pc,MaterialInstance *mi,Pipeline *p); + Mesh * CreateMesh(Primitive *r,MaterialInstance *mi,Pipeline *p); + Mesh * CreateMesh(PrimitiveCreater *pc,MaterialInstance *mi,Pipeline *p); Sampler * CreateSampler(VkSamplerCreateInfo *sci=nullptr); Sampler * CreateSampler(Texture *); public: //Get - Material * GetMaterial (const MaterialID &id){return rm_material.Get(id);} - MaterialInstance * GetMaterialInstance (const MaterialInstanceID &id){return rm_material_instance.Get(id);} - DescriptorSet * GetDescSets (const DescriptorSetID &id){return rm_desc_sets.Get(id);} - Primitive * GetPrimitive (const PrimitiveID &id){return rm_primitives.Get(id);} - DeviceBuffer * GetBuffer (const BufferID &id){return rm_buffers.Get(id);} - Sampler * GetSampler (const SamplerID &id){return rm_samplers.Get(id);} - Mesh * GetRenderable (const RenderableID &id){return rm_renderables.Get(id);} + Material * GetMaterial (const MaterialID &id){return rm_material.Get(id);} + MaterialInstance * GetMaterialInstance (const MaterialInstanceID &id){return rm_material_instance.Get(id);} + DescriptorSet * GetDescSets (const DescriptorSetID &id){return rm_desc_sets.Get(id);} + Primitive * GetPrimitive (const PrimitiveID &id){return rm_primitives.Get(id);} + DeviceBuffer * GetBuffer (const BufferID &id){return rm_buffers.Get(id);} + Sampler * GetSampler (const SamplerID &id){return rm_samplers.Get(id);} + Mesh * GetRenderable (const RenderableID &id){return rm_renderables.Get(id);} public: //Release - void Release(Material * mtl ){rm_material.Release(mtl);} - void Release(MaterialInstance * mi ){rm_material_instance.Release(mi);} - void Release(DescriptorSet * ds ){rm_desc_sets.Release(ds);} - void Release(Primitive * p ){rm_primitives.Release(p);} - void Release(DeviceBuffer * buf ){rm_buffers.Release(buf);} - void Release(Sampler * s ){rm_samplers.Release(s);} - void Release(Mesh * r ){rm_renderables.Release(r);} + void Release(Material * mtl ){rm_material.Release(mtl);} + void Release(MaterialInstance * mi ){rm_material_instance.Release(mi);} + void Release(DescriptorSet * ds ){rm_desc_sets.Release(ds);} + void Release(Primitive * p ){rm_primitives.Release(p);} + void Release(DeviceBuffer * buf ){rm_buffers.Release(buf);} + void Release(Sampler * s ){rm_samplers.Release(s);} + void Release(Mesh * r ){rm_renderables.Release(r);} };//class RenderResource /** diff --git a/src/SceneGraph/Vulkan/VKPrimitive.cpp b/src/SceneGraph/Vulkan/VKPrimitive.cpp index 4099f0ee..ee9c1c96 100644 --- a/src/SceneGraph/Vulkan/VKPrimitive.cpp +++ b/src/SceneGraph/Vulkan/VKPrimitive.cpp @@ -46,7 +46,7 @@ const VkDeviceSize Primitive::GetVertexCount()const return prim_data->GetVertexCount(); } -const int Primitive::GetVABCount()const +const uint32_t Primitive::GetVABCount()const { return prim_data->GetVABCount(); } diff --git a/src/SceneGraph/Vulkan/VKPrimitiveData.cpp b/src/SceneGraph/Vulkan/VKPrimitiveData.cpp index 951a34c1..72c2cbd8 100644 --- a/src/SceneGraph/Vulkan/VKPrimitiveData.cpp +++ b/src/SceneGraph/Vulkan/VKPrimitiveData.cpp @@ -25,7 +25,7 @@ PrimitiveData::~PrimitiveData() delete[] vab_list; //注意:这里并不释放VAB,在派生类中释放 } -const int PrimitiveData::GetVABCount()const +const uint32_t PrimitiveData::GetVABCount()const { return vil->GetVertexAttribCount(); } diff --git a/src/SceneGraph/Vulkan/VKPrimitiveData.h b/src/SceneGraph/Vulkan/VKPrimitiveData.h index e95ec637..c82d1995 100644 --- a/src/SceneGraph/Vulkan/VKPrimitiveData.h +++ b/src/SceneGraph/Vulkan/VKPrimitiveData.h @@ -47,7 +47,7 @@ public: public: const uint32_t GetVertexCount ()const{return vertex_count;} - const int GetVABCount ()const; + const uint32_t GetVABCount ()const; const int GetVABIndex (const AnsiString &name)const; VAB * GetVAB (const int index); diff --git a/src/Work/WorkObject.cpp b/src/Work/WorkObject.cpp index 06617cc5..a6f8a9b2 100644 --- a/src/Work/WorkObject.cpp +++ b/src/Work/WorkObject.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include //#include @@ -12,6 +13,9 @@ namespace hgl { WorkObject::WorkObject(graph::RenderFramework *rf,graph::IRenderTarget *rt) { + if(!rt) + rt=rf->GetSwapchainRenderTarget(); + OnRenderTargetSwitch(rf,rt); }