use image_count instead of color_count in Swapchain

This commit is contained in:
hyzboy 2024-11-11 01:18:28 +08:00
parent adc3c5bd81
commit 9f37373631
18 changed files with 106 additions and 89 deletions

View File

@ -10,6 +10,7 @@
set_property(TARGET ${name} PROPERTY FOLDER "ULRE/Example/Basic") set_property(TARGET ${name} PROPERTY FOLDER "ULRE/Example/Basic")
endmacro() endmacro()
CreateProject(00_first_app first_app.cpp)
CreateProject(01_draw_triangle_in_NDC draw_triangle_in_NDC.cpp) CreateProject(01_draw_triangle_in_NDC draw_triangle_in_NDC.cpp)
CreateProject(02_draw_triangle_use_UBO draw_triangle_use_UBO.cpp) CreateProject(02_draw_triangle_use_UBO draw_triangle_use_UBO.cpp)
CreateProject(03_auto_instance auto_instance.cpp) CreateProject(03_auto_instance auto_instance.cpp)

View File

@ -0,0 +1,8 @@
#include<hgl/graph/RenderFramework.h>
using namespace hgl;
int os_main(int,os_char **)
{
}

View File

@ -38,18 +38,12 @@ using namespace hgl;
using namespace hgl::io; using namespace hgl::io;
using namespace hgl::graph; using namespace hgl::graph;
namespace hgl{namespace graph
{
bool InitShaderCompiler();
void CloseShaderCompiler();
}}//namespace hgl::graph
class VulkanApplicationFramework:WindowEvent class VulkanApplicationFramework:WindowEvent
{ {
protected: protected:
Window * win =nullptr; Window * win =nullptr;
VulkanInstance * inst =nullptr; VulkanInstance * inst =nullptr;
protected: protected:
@ -91,8 +85,6 @@ public:
virtual ~VulkanApplicationFramework() virtual ~VulkanApplicationFramework()
{ {
CloseShaderCompiler();
win->Unjoin(this); win->Unjoin(this);
SAFE_CLEAR(db); SAFE_CLEAR(db);
@ -107,9 +99,6 @@ public:
{ {
logger::InitLogger(OS_TEXT("VulkanTest")); logger::InitLogger(OS_TEXT("VulkanTest"));
if(!InitShaderCompiler())
return(false);
SetClearColor(COLOR::MozillaCharcoal); SetClearColor(COLOR::MozillaCharcoal);
#ifdef _DEBUG #ifdef _DEBUG

View File

@ -4,6 +4,7 @@
#include<hgl/type/List.h> #include<hgl/type/List.h>
#include<hgl/graph/ViewportInfo.h> #include<hgl/graph/ViewportInfo.h>
#include<hgl/graph/module/GraphModule.h> #include<hgl/graph/module/GraphModule.h>
#include<hgl/io/event/WindowEvent.h>
VK_NAMESPACE_BEGIN VK_NAMESPACE_BEGIN
@ -12,35 +13,6 @@ class TileData;
class TileFont; class TileFont;
class FontSource; 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 Window;
class VulkanInstance; class VulkanInstance;
@ -49,15 +21,10 @@ class TextureManager;
class SwapchainModule; class SwapchainModule;
struct RenderFrameworkInitConfig
{
};//struct RenderFrameworkInitConfig
/** /**
* *
*/ */
class RenderFramework class RenderFramework:public io::WindowEvent
{ {
protected: protected:
@ -72,6 +39,8 @@ protected:
ObjectList<GraphModule> module_list; ObjectList<GraphModule> module_list;
protected:
SwapchainModule * swapchain_module =nullptr; SwapchainModule * swapchain_module =nullptr;
protected: protected:
@ -103,8 +72,14 @@ public: //module
public: //manager public: //manager
RenderPassManager *GetRenderPassManager(){return render_pass_manager;} ///<取得渲染通道管理器 RenderPassManager * GetRenderPassManager(){return render_pass_manager;} ///<取得渲染通道管理器
TextureManager *GetTextureManager(){return texture_manager;} ///<取得纹理管理器 TextureManager * GetTextureManager (){return texture_manager;} ///<取得纹理管理器
public: //event
virtual void OnResize(uint,uint)override;
virtual void OnActive(bool)override;
virtual void OnClose ()override;
public: public:

View File

@ -11,14 +11,14 @@ class Framebuffer
VkExtent2D extent; VkExtent2D extent;
uint32_t attachment_count; uint32_t attachment_count;
uint32_t color_count; uint32_t image_count;
bool has_depth; bool has_depth;
private: private:
friend class TextureManager; 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: public:
@ -32,7 +32,7 @@ public:
const VkExtent2D & GetExtent ()const{return extent;} const VkExtent2D & GetExtent ()const{return extent;}
const uint32_t GetAttachmentCount ()const{return attachment_count;} ///<获取渲染目标成分数量 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;} ///<是否包含深度成分 const bool HasDepth ()const{return has_depth;} ///<是否包含深度成分
};//class Framebuffer };//class Framebuffer

View File

@ -82,7 +82,7 @@ public:
Framebuffer * GetFramebuffer (const uint32_t index) {return swapchain->sc_fbo[index];} Framebuffer * GetFramebuffer (const uint32_t index) {return swapchain->sc_fbo[index];}
const uint32_t GetColorCount ()const override {return 1;} 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 * GetColorTexture (const int index=0) override{return swapchain->sc_color[index];}
virtual Texture2D * GetDepthTexture () override{return swapchain->sc_depth;} virtual Texture2D * GetDepthTexture () override{return swapchain->sc_depth;}

View File

@ -19,7 +19,7 @@ public:
VkSurfaceFormatKHR surface_format; VkSurfaceFormatKHR surface_format;
VkFormat depth_format; VkFormat depth_format;
uint32_t color_count =0; uint32_t image_count =0;
Texture2D ** sc_color =nullptr; Texture2D ** sc_color =nullptr;
Texture2D * sc_depth =nullptr; Texture2D * sc_depth =nullptr;

View File

@ -125,7 +125,7 @@ public: // Load
public: //FrameBuffer相关 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<ImageView *> &color,ImageView *depth); // Framebuffer *CreateFBO(RenderPass *,List<ImageView *> &color,ImageView *depth);
Framebuffer *CreateFBO(RenderPass *,ImageView *color,ImageView *depth); Framebuffer *CreateFBO(RenderPass *,ImageView *color,ImageView *depth);
Framebuffer *CreateFBO(RenderPass *,ImageView *); Framebuffer *CreateFBO(RenderPass *,ImageView *);

View File

@ -101,7 +101,6 @@ public:
template<typename T> template<typename T>
T * GetModule(bool create=false){return module_manager->GetModule<T>(create);} ///<获取指定类型的模块 T * GetModule(bool create=false){return module_manager->GetModule<T>(create);} ///<获取指定类型的模块
public: //回调事件 public: //回调事件

View File

@ -21,6 +21,8 @@ protected:
public: public:
virtual void OnResize(const VkExtent2D &)override; ///<窗口大小改变 virtual void OnResize(const VkExtent2D &)override; ///<窗口大小改变
virtual void OnExecute(const double,RenderCmdBuffer *);
public: public:
@ -28,12 +30,16 @@ public:
virtual ~SwapchainModule(); virtual ~SwapchainModule();
bool Init() override; bool Init() override;
void Swap();
public:
RenderPass * GetRenderPass () {return swapchain_rp;} RenderPass * GetRenderPass () {return swapchain_rp;}
RTSwapchain * GetRenderTarget () {return swapchain_rt;} RTSwapchain * GetRenderTarget () {return swapchain_rt;}
const VkExtent2D & GetSwapchainSize()const {return swapchain_rt->GetExtent();} const VkExtent2D & GetSwapchainSize()const {return swapchain_rt->GetExtent();}
};//class SwapchainModule:public GraphModule };//class SwapchainModule:public GraphModule
VK_NAMESPACE_END VK_NAMESPACE_END

View File

@ -5,6 +5,9 @@
VK_NAMESPACE_BEGIN VK_NAMESPACE_BEGIN
bool InitShaderCompiler();
void CloseShaderCompiler();
GraphModuleManager *InitGraphModuleManager(GPUDevice *dev); GraphModuleManager *InitGraphModuleManager(GPUDevice *dev);
bool ClearGraphModuleManager(GPUDevice *dev); bool ClearGraphModuleManager(GPUDevice *dev);
@ -21,6 +24,8 @@ RenderFramework::~RenderFramework()
// if(swapchain_module)graph_module_manager->ReleaseModule(swapchain_module); // if(swapchain_module)graph_module_manager->ReleaseModule(swapchain_module);
ClearGraphModuleManager(device); ClearGraphModuleManager(device);
CloseShaderCompiler();
} }
void RenderFramework::StartTime() void RenderFramework::StartTime()
@ -56,8 +61,32 @@ void RenderFramework::MainLoop()
} }
EndFrame(); EndFrame();
swapchain_module->Swap();
last_time=cur_time; last_time=cur_time;
++frame_count; ++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 VK_NAMESPACE_END

View File

@ -21,23 +21,23 @@ VkFramebuffer CreateVulkanFramebuffer(VkDevice device,RenderPass *rp,const VkExt
return fb; 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; if(depth)++att_count;
AutoDeleteArray<VkImageView> attachments(att_count); AutoDeleteArray<VkImageView> attachments(att_count);
VkImageView *ap=attachments; VkImageView *ap=attachments;
if(color_count) if(image_count)
{ {
const List<VkFormat> &cf_list=rp->GetColorFormat(); const List<VkFormat> &cf_list=rp->GetColorFormat();
const VkFormat *cf=cf_list.GetData(); const VkFormat *cf=cf_list.GetData();
ImageView **iv=color_list; ImageView **iv=color_list;
for(uint i=0;i<color_count;i++) for(uint i=0;i<image_count;i++)
{ {
if(*cf!=(*iv)->GetFormat()) if(*cf!=(*iv)->GetFormat())
return(nullptr); return(nullptr);
@ -60,7 +60,7 @@ Framebuffer *TextureManager::CreateFBO(RenderPass *rp,ImageView **color_list,con
return(nullptr); return(nullptr);
} }
attachments[color_count]=depth->GetImageView(); attachments[image_count]=depth->GetImageView();
extent.width=depth->GetExtent().width; extent.width=depth->GetExtent().width;
extent.height=depth->GetExtent().height; extent.height=depth->GetExtent().height;
@ -76,7 +76,7 @@ Framebuffer *TextureManager::CreateFBO(RenderPass *rp,ImageView **color_list,con
if(!fbo) if(!fbo)
return(nullptr); 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<ImageView *> &color,ImageView *depth) //Framebuffer *GPUDevice::CreateFBO(RenderPass *rp,List<ImageView *> &color,ImageView *depth)

View File

@ -8,12 +8,12 @@ RenderTarget *TextureManager::CreateRT(const FramebufferInfo *fbi,RenderPass *rp
if(!fbi)return(nullptr); if(!fbi)return(nullptr);
if(!rp)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 VkExtent2D extent=fbi->GetExtent();
const VkFormat depth_format=fbi->GetDepthFormat(); const VkFormat depth_format=fbi->GetDepthFormat();
AutoDeleteObjectArray<Texture2D> color_texture_list(color_count); AutoDeleteObjectArray<Texture2D> color_texture_list(image_count);
AutoDeleteArray<ImageView *> color_iv_list(color_count); //iv只是从Texture2D中取出来的无需一个个delete AutoDeleteArray<ImageView *> color_iv_list(image_count); //iv只是从Texture2D中取出来的无需一个个delete
Texture2D **tp=color_texture_list; Texture2D **tp=color_texture_list;
ImageView **iv=color_iv_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; 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) if(fb)
{ {
@ -40,7 +40,7 @@ RenderTarget *TextureManager::CreateRT(const FramebufferInfo *fbi,RenderPass *rp
DeviceQueue *q=dev->CreateQueue(fence_count,false); DeviceQueue *q=dev->CreateQueue(fence_count,false);
Semaphore *render_complete_semaphore=dev->CreateGPUSemaphore(); 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(); color_texture_list.DiscardObject();
return rt; return rt;

View File

@ -14,10 +14,10 @@ Framebuffer::Framebuffer(VkDevice dev,VkFramebuffer fb,const VkExtent2D &ext,VkR
render_pass=rp; render_pass=rp;
extent=ext; extent=ext;
color_count=cc; image_count=cc;
has_depth=depth; has_depth=depth;
attachment_count=color_count; attachment_count=image_count;
if(has_depth) if(has_depth)
++attachment_count; ++attachment_count;

View File

@ -12,7 +12,7 @@ RenderTarget::RenderTarget(DeviceQueue *q,Semaphore *s)
render_pass=nullptr; render_pass=nullptr;
fbo=nullptr; fbo=nullptr;
color_count=0; image_count=0;
color_textures=nullptr; color_textures=nullptr;
depth_texture=nullptr; depth_texture=nullptr;
render_complete_semaphore=s; render_complete_semaphore=s;
@ -26,11 +26,11 @@ RenderTarget::RenderTarget(DeviceQueue *q,Semaphore *s,RenderPass *_rp,Framebuff
depth_texture=dt; depth_texture=dt;
color_count=cc; image_count=cc;
if(color_count>0) if(image_count>0)
{ {
color_textures=new Texture2D *[color_count]; color_textures=new Texture2D *[image_count];
hgl_cpy(color_textures,ctl,color_count); hgl_cpy(color_textures,ctl,image_count);
extent.width=color_textures[0]->GetWidth(); extent.width=color_textures[0]->GetWidth();
extent.height=color_textures[0]->GetHeight(); extent.height=color_textures[0]->GetHeight();
@ -53,7 +53,7 @@ RenderTarget::~RenderTarget()
{ {
SAFE_CLEAR(queue); SAFE_CLEAR(queue);
SAFE_CLEAR(depth_texture); 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(render_complete_semaphore);
SAFE_CLEAR(fbo); SAFE_CLEAR(fbo);

View File

@ -4,9 +4,9 @@
VK_NAMESPACE_BEGIN VK_NAMESPACE_BEGIN
Swapchain::~Swapchain() 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(sc_depth);
SAFE_CLEAR_OBJECT_ARRAY_OBJECT(sc_color,color_count) SAFE_CLEAR_OBJECT_ARRAY_OBJECT(sc_color,image_count)
if(swap_chain) if(swap_chain)
{ {
@ -14,6 +14,6 @@ Swapchain::~Swapchain()
swap_chain=VK_NULL_HANDLE; swap_chain=VK_NULL_HANDLE;
} }
color_count=0; image_count=0;
} }
VK_NAMESPACE_END VK_NAMESPACE_END

View File

@ -74,9 +74,9 @@ inline void CreateInputAttachmentReference(VkAttachmentReference *ref_list, uint
bool CreateAttachmentDescription(List<VkAttachmentDescription> &desc_list,const RenderbufferInfo *rbi) bool CreateAttachmentDescription(List<VkAttachmentDescription> &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); desc_list.SetCount(count);
@ -97,7 +97,7 @@ bool CreateAttachmentDescription(List<VkAttachmentDescription> &desc_list,const
desc=desc_list.GetData(); desc=desc_list.GetData();
const VkFormat *cf=rbi->GetColorFormat(); const VkFormat *cf=rbi->GetColorFormat();
for(uint i=0;i<color_count;i++) for(uint i=0;i<image_count;i++)
{ {
desc->finalLayout = rbi->GetColorLayout(); desc->finalLayout = rbi->GetColorLayout();
desc->format = *cf; desc->format = *cf;

View File

@ -87,12 +87,12 @@ namespace
bool SwapchainModule::CreateSwapchainFBO() 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); return(false);
AutoDeleteArray<VkImage> sc_images(swapchain->color_count); AutoDeleteArray<VkImage> 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); return(false);
TextureManager *tex_manager=GetModule<TextureManager>(); TextureManager *tex_manager=GetModule<TextureManager>();
@ -114,10 +114,10 @@ bool SwapchainModule::CreateSwapchainFBO()
// } // }
//#endif//_DEBUG //#endif//_DEBUG
swapchain->sc_color =hgl_zero_new<Texture2D *>(swapchain->color_count); swapchain->sc_color =hgl_zero_new<Texture2D *>(swapchain->image_count);
swapchain->sc_fbo =hgl_zero_new<Framebuffer *>(swapchain->color_count); swapchain->sc_fbo =hgl_zero_new<Framebuffer *>(swapchain->image_count);
for(uint32_t i=0;i<swapchain->color_count;i++) for(uint32_t i=0;i<swapchain->image_count;i++)
{ {
swapchain->sc_color[i]=tex_manager->CreateTexture2D(new SwapchainColorTextureCreateInfo(swapchain->surface_format.format,swapchain->extent,sc_images[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(); 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 *render_complete_semaphore=device->CreateGPUSemaphore();
Semaphore *present_complete_semaphore=device->CreateGPUSemaphore(); Semaphore *present_complete_semaphore=device->CreateGPUSemaphore();
@ -235,4 +235,14 @@ void SwapchainModule::OnResize(const VkExtent2D &extent)
CreateSwapchainRenderTarget(); CreateSwapchainRenderTarget();
} }
void SwapchainModule::Swap()
{
int index=swapchain_rt->AcquireNextImage();
if(index<0||index>=swapchain->image_count)
return;
}
VK_NAMESPACE_END VK_NAMESPACE_END