diff --git a/example/Basic/CMakeLists.txt b/example/Basic/CMakeLists.txt index 93de6fd9..adf351aa 100644 --- a/example/Basic/CMakeLists.txt +++ b/example/Basic/CMakeLists.txt @@ -10,6 +10,7 @@ set_property(TARGET ${name} PROPERTY FOLDER "ULRE/Example/Basic") endmacro() +CreateProject(00_first_app first_app.cpp) CreateProject(01_draw_triangle_in_NDC draw_triangle_in_NDC.cpp) CreateProject(02_draw_triangle_use_UBO draw_triangle_use_UBO.cpp) CreateProject(03_auto_instance auto_instance.cpp) diff --git a/example/Basic/first_app.cpp b/example/Basic/first_app.cpp new file mode 100644 index 00000000..1b2bdca7 --- /dev/null +++ b/example/Basic/first_app.cpp @@ -0,0 +1,8 @@ +#include + +using namespace hgl; + +int os_main(int,os_char **) +{ + +} \ No newline at end of file diff --git a/example/common/VulkanAppFramework.h b/example/common/VulkanAppFramework.h index bbef2fe7..c0496f64 100644 --- a/example/common/VulkanAppFramework.h +++ b/example/common/VulkanAppFramework.h @@ -38,18 +38,12 @@ using namespace hgl; using namespace hgl::io; using namespace hgl::graph; -namespace hgl{namespace graph -{ - bool InitShaderCompiler(); - void CloseShaderCompiler(); -}}//namespace hgl::graph - class VulkanApplicationFramework:WindowEvent { protected: - Window * win =nullptr; - VulkanInstance * inst =nullptr; + Window * win =nullptr; + VulkanInstance * inst =nullptr; protected: @@ -91,8 +85,6 @@ public: virtual ~VulkanApplicationFramework() { - CloseShaderCompiler(); - win->Unjoin(this); SAFE_CLEAR(db); @@ -107,9 +99,6 @@ public: { logger::InitLogger(OS_TEXT("VulkanTest")); - if(!InitShaderCompiler()) - return(false); - SetClearColor(COLOR::MozillaCharcoal); #ifdef _DEBUG diff --git a/inc/hgl/graph/RenderFramework.h b/inc/hgl/graph/RenderFramework.h index cfb8daaf..bb00a10b 100644 --- a/inc/hgl/graph/RenderFramework.h +++ b/inc/hgl/graph/RenderFramework.h @@ -4,6 +4,7 @@ #include #include #include +#include VK_NAMESPACE_BEGIN @@ -12,35 +13,6 @@ class TileData; class TileFont; class FontSource; -// -///** -// * 渲染模块工作配置 -// */ -//class GraphModuleWorkConfig -//{ -// /** -// * 渲染模块名称 -// * 在render_module为nullptr时,用于创建或加载RenderModule。 -// * 它和RenderModule返回的名称有可能一样,但也有可能不一样。 -// */ -// AnsiString render_module_name; -// GraphModule *render_module=nullptr; -// -// BlendMode blend_mode; -// RenderOrder render_order; -// -//public: -// -// const AnsiString &GetModuleName()const{return render_module_name;} ///<取得渲染模块名称 -// -// GraphModule *GetModule(){return render_module;} ///<取得渲染模块 -// -//public: -// -// GraphModuleWorkConfig(); -// virtual ~GraphModuleWorkConfig()=default; -//};//class GraphModuleWorkConfig - class Window; class VulkanInstance; @@ -49,15 +21,10 @@ class TextureManager; class SwapchainModule; -struct RenderFrameworkInitConfig -{ - -};//struct RenderFrameworkInitConfig - /** * 渲染框架 */ -class RenderFramework +class RenderFramework:public io::WindowEvent { protected: @@ -72,6 +39,8 @@ protected: ObjectList module_list; +protected: + SwapchainModule * swapchain_module =nullptr; protected: @@ -103,8 +72,14 @@ public: //module public: //manager - RenderPassManager *GetRenderPassManager(){return render_pass_manager;} ///<取得渲染通道管理器 - TextureManager *GetTextureManager(){return texture_manager;} ///<取得纹理管理器 + RenderPassManager * GetRenderPassManager(){return render_pass_manager;} ///<取得渲染通道管理器 + TextureManager * GetTextureManager (){return texture_manager;} ///<取得纹理管理器 + +public: //event + + virtual void OnResize(uint,uint)override; + virtual void OnActive(bool)override; + virtual void OnClose ()override; public: diff --git a/inc/hgl/graph/VKFramebuffer.h b/inc/hgl/graph/VKFramebuffer.h index 7e9e4a56..fe82c6e4 100644 --- a/inc/hgl/graph/VKFramebuffer.h +++ b/inc/hgl/graph/VKFramebuffer.h @@ -11,14 +11,14 @@ class Framebuffer VkExtent2D extent; uint32_t attachment_count; - uint32_t color_count; + uint32_t image_count; bool has_depth; private: friend class TextureManager; - Framebuffer(VkDevice,VkFramebuffer,const VkExtent2D &,VkRenderPass,uint32_t color_count,bool depth); + Framebuffer(VkDevice,VkFramebuffer,const VkExtent2D &,VkRenderPass,uint32_t image_count,bool depth); public: @@ -32,7 +32,7 @@ public: const VkExtent2D & GetExtent ()const{return extent;} const uint32_t GetAttachmentCount ()const{return attachment_count;} ///<获取渲染目标成分数量 - const uint32_t GetColorCount ()const{return color_count;} ///<取得颜色成分数量 + const uint32_t GetColorCount ()const{return image_count;} ///<取得颜色成分数量 const bool HasDepth ()const{return has_depth;} ///<是否包含深度成分 };//class Framebuffer diff --git a/inc/hgl/graph/VKRenderTarget.h b/inc/hgl/graph/VKRenderTarget.h index d99ece3a..9e1a21e7 100644 --- a/inc/hgl/graph/VKRenderTarget.h +++ b/inc/hgl/graph/VKRenderTarget.h @@ -82,7 +82,7 @@ public: Framebuffer * GetFramebuffer (const uint32_t index) {return swapchain->sc_fbo[index];} const uint32_t GetColorCount ()const override {return 1;} - const uint32_t GetImageCount ()const {return swapchain->color_count;} + const uint32_t GetImageCount ()const {return swapchain->image_count;} virtual Texture2D * GetColorTexture (const int index=0) override{return swapchain->sc_color[index];} virtual Texture2D * GetDepthTexture () override{return swapchain->sc_depth;} diff --git a/inc/hgl/graph/VKSwapchain.h b/inc/hgl/graph/VKSwapchain.h index e7c5437f..991ba5a7 100644 --- a/inc/hgl/graph/VKSwapchain.h +++ b/inc/hgl/graph/VKSwapchain.h @@ -19,7 +19,7 @@ public: VkSurfaceFormatKHR surface_format; VkFormat depth_format; - uint32_t color_count =0; + uint32_t image_count =0; Texture2D ** sc_color =nullptr; Texture2D * sc_depth =nullptr; diff --git a/inc/hgl/graph/manager/TextureManager.h b/inc/hgl/graph/manager/TextureManager.h index 3a725c28..824d095b 100644 --- a/inc/hgl/graph/manager/TextureManager.h +++ b/inc/hgl/graph/manager/TextureManager.h @@ -125,7 +125,7 @@ public: // Load public: //FrameBuffer相关 - Framebuffer *CreateFBO(RenderPass *rp,ImageView **color_list,const uint color_count,ImageView *depth); + Framebuffer *CreateFBO(RenderPass *rp,ImageView **color_list,const uint image_count,ImageView *depth); // Framebuffer *CreateFBO(RenderPass *,List &color,ImageView *depth); Framebuffer *CreateFBO(RenderPass *,ImageView *color,ImageView *depth); Framebuffer *CreateFBO(RenderPass *,ImageView *); diff --git a/inc/hgl/graph/module/GraphModule.h b/inc/hgl/graph/module/GraphModule.h index 88002a00..2f21daa6 100644 --- a/inc/hgl/graph/module/GraphModule.h +++ b/inc/hgl/graph/module/GraphModule.h @@ -101,7 +101,6 @@ public: template T * GetModule(bool create=false){return module_manager->GetModule(create);} ///<获取指定类型的模块 - public: //回调事件 diff --git a/inc/hgl/graph/module/SwapchainModule.h b/inc/hgl/graph/module/SwapchainModule.h index 67701304..4683c3ed 100644 --- a/inc/hgl/graph/module/SwapchainModule.h +++ b/inc/hgl/graph/module/SwapchainModule.h @@ -21,6 +21,8 @@ protected: public: virtual void OnResize(const VkExtent2D &)override; ///<窗口大小改变 + + virtual void OnExecute(const double,RenderCmdBuffer *); public: @@ -28,12 +30,16 @@ public: virtual ~SwapchainModule(); bool Init() override; + void Swap(); + +public: RenderPass * GetRenderPass () {return swapchain_rp;} RTSwapchain * GetRenderTarget () {return swapchain_rt;} const VkExtent2D & GetSwapchainSize()const {return swapchain_rt->GetExtent();} + };//class SwapchainModule:public GraphModule VK_NAMESPACE_END diff --git a/src/SceneGraph/RenderFramework.cpp b/src/SceneGraph/RenderFramework.cpp index 60d01590..f7cb4f14 100644 --- a/src/SceneGraph/RenderFramework.cpp +++ b/src/SceneGraph/RenderFramework.cpp @@ -5,6 +5,9 @@ VK_NAMESPACE_BEGIN +bool InitShaderCompiler(); +void CloseShaderCompiler(); + GraphModuleManager *InitGraphModuleManager(GPUDevice *dev); bool ClearGraphModuleManager(GPUDevice *dev); @@ -21,6 +24,8 @@ RenderFramework::~RenderFramework() // if(swapchain_module)graph_module_manager->ReleaseModule(swapchain_module); ClearGraphModuleManager(device); + + CloseShaderCompiler(); } void RenderFramework::StartTime() @@ -56,8 +61,32 @@ void RenderFramework::MainLoop() } EndFrame(); + + swapchain_module->Swap(); last_time=cur_time; ++frame_count; } +bool RenderFramework::Init() +{ + if(!InitShaderCompiler()) + return(false); + + return(true); +} + +void RenderFramework::OnResize(uint w,uint h) +{ + viewport_info.Set(w,h); + + graph_module_manager->OnResize(VkExtent2D{.width=w,.height=h}); +} + +void RenderFramework::OnActive(bool) +{ +} + +void RenderFramework::OnClose() +{ +} VK_NAMESPACE_END diff --git a/src/SceneGraph/Vulkan/VKDeviceFramebuffer.cpp b/src/SceneGraph/Vulkan/VKDeviceFramebuffer.cpp index 6fe4f795..0550dea9 100644 --- a/src/SceneGraph/Vulkan/VKDeviceFramebuffer.cpp +++ b/src/SceneGraph/Vulkan/VKDeviceFramebuffer.cpp @@ -21,23 +21,23 @@ VkFramebuffer CreateVulkanFramebuffer(VkDevice device,RenderPass *rp,const VkExt return fb; } -Framebuffer *TextureManager::CreateFBO(RenderPass *rp,ImageView **color_list,const uint color_count,ImageView *depth) +Framebuffer *TextureManager::CreateFBO(RenderPass *rp,ImageView **color_list,const uint image_count,ImageView *depth) { - uint att_count=color_count; + uint att_count=image_count; if(depth)++att_count; AutoDeleteArray attachments(att_count); VkImageView *ap=attachments; - if(color_count) + if(image_count) { const List &cf_list=rp->GetColorFormat(); const VkFormat *cf=cf_list.GetData(); ImageView **iv=color_list; - for(uint i=0;iGetFormat()) return(nullptr); @@ -60,7 +60,7 @@ Framebuffer *TextureManager::CreateFBO(RenderPass *rp,ImageView **color_list,con return(nullptr); } - attachments[color_count]=depth->GetImageView(); + attachments[image_count]=depth->GetImageView(); extent.width=depth->GetExtent().width; extent.height=depth->GetExtent().height; @@ -76,7 +76,7 @@ Framebuffer *TextureManager::CreateFBO(RenderPass *rp,ImageView **color_list,con if(!fbo) return(nullptr); - return(new Framebuffer(GetVkDevice(),fbo,extent,rp->GetVkRenderPass(),color_count,depth)); + return(new Framebuffer(GetVkDevice(),fbo,extent,rp->GetVkRenderPass(),image_count,depth)); } // //Framebuffer *GPUDevice::CreateFBO(RenderPass *rp,List &color,ImageView *depth) diff --git a/src/SceneGraph/Vulkan/VKDeviceRenderTarget.cpp b/src/SceneGraph/Vulkan/VKDeviceRenderTarget.cpp index 0c55e78a..f0567b41 100644 --- a/src/SceneGraph/Vulkan/VKDeviceRenderTarget.cpp +++ b/src/SceneGraph/Vulkan/VKDeviceRenderTarget.cpp @@ -8,12 +8,12 @@ RenderTarget *TextureManager::CreateRT(const FramebufferInfo *fbi,RenderPass *rp if(!fbi)return(nullptr); if(!rp)return(nullptr); - const uint32_t color_count=fbi->GetColorCount(); + const uint32_t image_count=fbi->GetColorCount(); const VkExtent2D extent=fbi->GetExtent(); const VkFormat depth_format=fbi->GetDepthFormat(); - AutoDeleteObjectArray color_texture_list(color_count); - AutoDeleteArray color_iv_list(color_count); //iv只是从Texture2D中取出来的,无需一个个delete + AutoDeleteObjectArray color_texture_list(image_count); + AutoDeleteArray color_iv_list(image_count); //iv只是从Texture2D中取出来的,无需一个个delete Texture2D **tp=color_texture_list; ImageView **iv=color_iv_list; @@ -31,7 +31,7 @@ RenderTarget *TextureManager::CreateRT(const FramebufferInfo *fbi,RenderPass *rp Texture2D *depth_texture=(depth_format!=PF_UNDEFINED)?CreateTexture2D(new DepthAttachmentTextureCreateInfo(depth_format,extent)):nullptr; - Framebuffer *fb=CreateFBO(rp,color_iv_list,color_count,depth_texture?depth_texture->GetImageView():nullptr); + Framebuffer *fb=CreateFBO(rp,color_iv_list,image_count,depth_texture?depth_texture->GetImageView():nullptr); if(fb) { @@ -40,7 +40,7 @@ RenderTarget *TextureManager::CreateRT(const FramebufferInfo *fbi,RenderPass *rp DeviceQueue *q=dev->CreateQueue(fence_count,false); Semaphore *render_complete_semaphore=dev->CreateGPUSemaphore(); - RenderTarget *rt=new RenderTarget(q,render_complete_semaphore,rp,fb,color_texture_list,color_count,depth_texture); + RenderTarget *rt=new RenderTarget(q,render_complete_semaphore,rp,fb,color_texture_list,image_count,depth_texture); color_texture_list.DiscardObject(); return rt; diff --git a/src/SceneGraph/Vulkan/VKFramebuffer.cpp b/src/SceneGraph/Vulkan/VKFramebuffer.cpp index eb2d086a..c4249ce3 100644 --- a/src/SceneGraph/Vulkan/VKFramebuffer.cpp +++ b/src/SceneGraph/Vulkan/VKFramebuffer.cpp @@ -14,10 +14,10 @@ Framebuffer::Framebuffer(VkDevice dev,VkFramebuffer fb,const VkExtent2D &ext,VkR render_pass=rp; extent=ext; - color_count=cc; + image_count=cc; has_depth=depth; - attachment_count=color_count; + attachment_count=image_count; if(has_depth) ++attachment_count; diff --git a/src/SceneGraph/Vulkan/VKRenderTarget.cpp b/src/SceneGraph/Vulkan/VKRenderTarget.cpp index 6f18ef54..931bdb46 100644 --- a/src/SceneGraph/Vulkan/VKRenderTarget.cpp +++ b/src/SceneGraph/Vulkan/VKRenderTarget.cpp @@ -12,7 +12,7 @@ RenderTarget::RenderTarget(DeviceQueue *q,Semaphore *s) render_pass=nullptr; fbo=nullptr; - color_count=0; + image_count=0; color_textures=nullptr; depth_texture=nullptr; render_complete_semaphore=s; @@ -26,11 +26,11 @@ RenderTarget::RenderTarget(DeviceQueue *q,Semaphore *s,RenderPass *_rp,Framebuff depth_texture=dt; - color_count=cc; - if(color_count>0) + image_count=cc; + if(image_count>0) { - color_textures=new Texture2D *[color_count]; - hgl_cpy(color_textures,ctl,color_count); + color_textures=new Texture2D *[image_count]; + hgl_cpy(color_textures,ctl,image_count); extent.width=color_textures[0]->GetWidth(); extent.height=color_textures[0]->GetHeight(); @@ -53,7 +53,7 @@ RenderTarget::~RenderTarget() { SAFE_CLEAR(queue); SAFE_CLEAR(depth_texture); - SAFE_CLEAR_OBJECT_ARRAY_OBJECT(color_textures,color_count); + SAFE_CLEAR_OBJECT_ARRAY_OBJECT(color_textures,image_count); SAFE_CLEAR(render_complete_semaphore); SAFE_CLEAR(fbo); diff --git a/src/SceneGraph/Vulkan/VKSwapchain.cpp b/src/SceneGraph/Vulkan/VKSwapchain.cpp index f0286eba..3026a5b6 100644 --- a/src/SceneGraph/Vulkan/VKSwapchain.cpp +++ b/src/SceneGraph/Vulkan/VKSwapchain.cpp @@ -4,9 +4,9 @@ VK_NAMESPACE_BEGIN Swapchain::~Swapchain() { - SAFE_CLEAR_OBJECT_ARRAY_OBJECT(sc_fbo,color_count); + SAFE_CLEAR_OBJECT_ARRAY_OBJECT(sc_fbo,image_count); SAFE_CLEAR(sc_depth); - SAFE_CLEAR_OBJECT_ARRAY_OBJECT(sc_color,color_count) + SAFE_CLEAR_OBJECT_ARRAY_OBJECT(sc_color,image_count) if(swap_chain) { @@ -14,6 +14,6 @@ Swapchain::~Swapchain() swap_chain=VK_NULL_HANDLE; } - color_count=0; + image_count=0; } VK_NAMESPACE_END diff --git a/src/SceneGraph/manager/RenderPassManager.cpp b/src/SceneGraph/manager/RenderPassManager.cpp index d972ca68..dc16ddb0 100644 --- a/src/SceneGraph/manager/RenderPassManager.cpp +++ b/src/SceneGraph/manager/RenderPassManager.cpp @@ -74,9 +74,9 @@ inline void CreateInputAttachmentReference(VkAttachmentReference *ref_list, uint bool CreateAttachmentDescription(List &desc_list,const RenderbufferInfo *rbi) { - const uint color_count=rbi->GetColorCount(); + const uint image_count=rbi->GetColorCount(); - const uint count=(rbi->HasDepthOrStencil())?color_count+1:color_count; + const uint count=(rbi->HasDepthOrStencil())?image_count+1:image_count; desc_list.SetCount(count); @@ -97,7 +97,7 @@ bool CreateAttachmentDescription(List &desc_list,const desc=desc_list.GetData(); const VkFormat *cf=rbi->GetColorFormat(); - for(uint i=0;ifinalLayout = rbi->GetColorLayout(); desc->format = *cf; diff --git a/src/SceneGraph/module/SwapchainModule.cpp b/src/SceneGraph/module/SwapchainModule.cpp index 51e9a2ba..a75ed088 100644 --- a/src/SceneGraph/module/SwapchainModule.cpp +++ b/src/SceneGraph/module/SwapchainModule.cpp @@ -87,12 +87,12 @@ namespace bool SwapchainModule::CreateSwapchainFBO() { - if(vkGetSwapchainImagesKHR(swapchain->device,swapchain->swap_chain,&(swapchain->color_count),nullptr)!=VK_SUCCESS) + if(vkGetSwapchainImagesKHR(swapchain->device,swapchain->swap_chain,&(swapchain->image_count),nullptr)!=VK_SUCCESS) return(false); - AutoDeleteArray sc_images(swapchain->color_count); + AutoDeleteArray sc_images(swapchain->image_count); - if(vkGetSwapchainImagesKHR(swapchain->device,swapchain->swap_chain,&(swapchain->color_count),sc_images)!=VK_SUCCESS) + if(vkGetSwapchainImagesKHR(swapchain->device,swapchain->swap_chain,&(swapchain->image_count),sc_images)!=VK_SUCCESS) return(false); TextureManager *tex_manager=GetModule(); @@ -114,10 +114,10 @@ bool SwapchainModule::CreateSwapchainFBO() // } //#endif//_DEBUG - swapchain->sc_color =hgl_zero_new(swapchain->color_count); - swapchain->sc_fbo =hgl_zero_new(swapchain->color_count); + swapchain->sc_color =hgl_zero_new(swapchain->image_count); + swapchain->sc_fbo =hgl_zero_new(swapchain->image_count); - for(uint32_t i=0;icolor_count;i++) + for(uint32_t i=0;iimage_count;i++) { swapchain->sc_color[i]=tex_manager->CreateTexture2D(new SwapchainColorTextureCreateInfo(swapchain->surface_format.format,swapchain->extent,sc_images[i])); @@ -177,7 +177,7 @@ bool SwapchainModule::CreateSwapchainRenderTarget() { GPUDevice *device=GetDevice(); - DeviceQueue *q=device->CreateQueue(swapchain->color_count,false); + DeviceQueue *q=device->CreateQueue(swapchain->image_count,false); Semaphore *render_complete_semaphore=device->CreateGPUSemaphore(); Semaphore *present_complete_semaphore=device->CreateGPUSemaphore(); @@ -235,4 +235,14 @@ void SwapchainModule::OnResize(const VkExtent2D &extent) CreateSwapchainRenderTarget(); } +void SwapchainModule::Swap() +{ + int index=swapchain_rt->AcquireNextImage(); + + if(index<0||index>=swapchain->image_count) + return; + + +} + VK_NAMESPACE_END