diff --git a/example/2dVector/line.cpp b/example/2dVector/line.cpp index da5f181d..f5d02669 100644 --- a/example/2dVector/line.cpp +++ b/example/2dVector/line.cpp @@ -1,6 +1,3 @@ -// 0.triangle -// 该范例主要演示直接绘制一个渐变色的三角形 - #include"VulkanAppFramework.h" #include #include diff --git a/example/Vulkan/CMakeLists.txt b/example/Vulkan/CMakeLists.txt index f37ad34f..d23b00ab 100644 --- a/example/Vulkan/CMakeLists.txt +++ b/example/Vulkan/CMakeLists.txt @@ -42,17 +42,11 @@ CreateProject(13.DrawTile DrawTile.cpp) CreateProject(14.DrawText DrawText.cpp) -IF(SUPPORT_QT_VULKAN) -include(QtCommon) -CreateQtProject(14.VulkanQT VulkanQtApp.cpp - QtVulkanWindow.cpp - QtVulkanWindow.h - QtVulkanMainWindow.h - QtVulkanMainWindow.cpp) -ENDIF(SUPPORT_QT_VULKAN) +CreateProject(15.OffscreenRender OffscreenRender.cpp) + #CreateProject(12.PBRBasic PBRBasic.cpp) #CreateProject(12.Deferred Deferred.cpp) -#CreateProject(13.DeferredModel DeferredModel.cpp) +CreateProject(16.DeferredModel DeferredModel.cpp) #CreateProject(14.AutoMaterial auto_material.cpp) diff --git a/example/Vulkan/DeferredModel.cpp b/example/Vulkan/DeferredModel.cpp index 7826ccbe..978e518c 100644 --- a/example/Vulkan/DeferredModel.cpp +++ b/example/Vulkan/DeferredModel.cpp @@ -4,8 +4,8 @@ #include"VulkanAppFramework.h" #include #include -#include -#include +#include +#include #include #include #include @@ -70,7 +70,7 @@ private: struct { List desc_list; - List color_ref_list; + VkAttachmentReference *color_ref_list; VkAttachmentReference depth_ref; }attachment; @@ -187,23 +187,21 @@ private: gbuffer.image_view_list.Add(gbuffer.texture_list[i]->GetImageView()); } - device->CreateColorAttachmentReference(gbuffer.attachment.color_ref_list,0,3); - device->CreateDepthAttachmentReference(&gbuffer.attachment.depth_ref,3); + gbuffer.attachment.color_ref_list=new VkAttachmentReference[3]; - if(!device->CreateAttachment( gbuffer.attachment.desc_list, - gbuffer.gbuffer_format_list, - gbuffer.depth->GetFormat())) + vulkan::CreateColorAttachmentReference(gbuffer.attachment.color_ref_list,3); + vulkan::CreateDepthAttachmentReference(&gbuffer.attachment.depth_ref); + + if(!vulkan::CreateAttachment( gbuffer.attachment.desc_list, + gbuffer.gbuffer_format_list, + gbuffer.depth->GetFormat())) return(false); - VkSubpassDescription desc; - - device->CreateSubpassDescription(desc, - gbuffer.attachment.color_ref_list, - &gbuffer.attachment.depth_ref); + vulkan::SubpassDescription desc(gbuffer.attachment.color_ref_list,3,&gbuffer.attachment.depth_ref); gbuffer.subpass.desc.Add(desc); - device->CreateSubpassDependency(gbuffer.subpass.dependency,2); //为啥要2个还不清楚 + vulkan::CreateSubpassDependency(gbuffer.subpass.dependency,2); //为啥要2个还不清楚 gbuffer.renderpass=device->CreateRenderPass(gbuffer.attachment.desc_list, gbuffer.subpass.desc, @@ -214,7 +212,7 @@ private: if(!gbuffer.renderpass) return(false); - gbuffer.framebuffer=vulkan::CreateFramebuffer(device,gbuffer.renderpass,gbuffer.image_view_list,gbuffer.depth->GetImageView()); + gbuffer.framebuffer=vulkan::CreateFramebuffer(device->GetDevice(),gbuffer.renderpass,gbuffer.image_view_list,gbuffer.depth->GetImageView()); if(!gbuffer.framebuffer) return(false); @@ -226,15 +224,12 @@ private: bool InitSubpass(SubpassParam *sp,const OSString &vs,const OSString &fs) { - sp->material=shader_manage->CreateMaterial(vs,fs); + sp->material=db->CreateMaterial(vs,fs); if(!sp->material) return(false); - sp->material_instance=sp->material->CreateInstance(); - - db->Add(sp->material); - db->Add(sp->material_instance); + sp->material_instance=db->CreateMaterialInstance(sp->material); return(true); } diff --git a/example/Vulkan/OffscreenRender.cpp b/example/Vulkan/OffscreenRender.cpp new file mode 100644 index 00000000..ca144d45 --- /dev/null +++ b/example/Vulkan/OffscreenRender.cpp @@ -0,0 +1,48 @@ +#include +#include"VulkanAppFramework.h" + +using namespace hgl; +using namespace hgl::graph; + +constexpr uint OFFSCREEN_SIZE =512; +constexpr uint SCREEN_WIDTH =1024; +constexpr uint SCREEN_HEIGHT =(SCREEN_WIDTH/16)*9; + +class OffscreenRender:public VulkanApplicationFramework +{ + vulkan::RenderTarget *os_rt; + +public: + + ~OffscreenRender() + { + } + + bool InitOffscreenRT() + { + os_rt=vulkan::CreateColorFramebuffer( + } + + bool Init() + { + if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT)) + return(false); + + if(!InitOffscreenRT()) + return(false); + + return(true); + } +};//class OffscreenRender:public VulkanApplicationFramework + +int main(int,char **) +{ + OffscreenRender app; + + if(!app.Init()) + return(-1); + + while(app.Run()); + + return 0; +} diff --git a/inc/hgl/graph/vulkan/VKDevice.h b/inc/hgl/graph/vulkan/VKDevice.h index 6341495d..433c673f 100644 --- a/inc/hgl/graph/vulkan/VKDevice.h +++ b/inc/hgl/graph/vulkan/VKDevice.h @@ -55,7 +55,7 @@ public: DeviceAttribute * GetDeviceAttribute () {return attr;} VkSurfaceKHR GetSurface () {return attr->surface;} - VkDevice GetDevice () {return attr->device;} + VkDevice GetDevice ()const {return attr->device;} const PhysicalDevice * GetPhysicalDevice ()const {return attr->physical_device;} VkDescriptorPool GetDescriptorPool () {return attr->desc_pool;} @@ -122,9 +122,9 @@ public: //Image public: //Texture - bool CheckFormatSupport(const VkFormat,const uint32_t bits,ImageTiling tiling=ImageTiling::Optimal); + bool CheckFormatSupport(const VkFormat,const uint32_t bits,ImageTiling tiling=ImageTiling::Optimal)const; - bool CheckTextureFormatSupport(const VkFormat fmt,ImageTiling tiling=ImageTiling::Optimal){return CheckFormatSupport(fmt,VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT,tiling);} + bool CheckTextureFormatSupport(const VkFormat fmt,ImageTiling tiling=ImageTiling::Optimal)const{return CheckFormatSupport(fmt,VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT,tiling);} Texture2D *CreateTexture2D(TextureData *); Texture2D *CreateTexture2D(TextureCreateInfo *ci); @@ -242,37 +242,18 @@ public: //Command Buffer 相关 CommandBuffer * CreateCommandBuffer(const VkExtent2D &extent,const uint32_t atta_count); - void CreateAttachmentReference(VkAttachmentReference *ref_list,uint start,uint count,VkImageLayout layout)const; - - void CreateColorAttachmentReference(List &ref_list, uint start,uint count )const{ref_list.SetCount(count); CreateAttachmentReference(ref_list.GetData(), start,count,VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);} - void CreateDepthAttachmentReference( VkAttachmentReference *depth_ref, uint index )const{ CreateAttachmentReference(depth_ref, index,1 ,VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);} - void CreateInputAttachment( List &ref_list, uint start,uint count )const{ref_list.SetCount(count); CreateAttachmentReference(ref_list.GetData(), start,count,VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);} - - bool CreateAttachment( List &color_output_desc_list, - const List &color_format, - const VkFormat depth_format, - const VkImageLayout color_final_layout=VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, - const VkImageLayout depth_final_layout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL)const; - - bool CreateColorAttachment( List &ref_list,List &desc_list,const List &color_format,const VkImageLayout color_final_layout=VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)const; - bool CreateDepthAttachment( List &ref_list,List &desc_list,const VkFormat &depth_format,const VkImageLayout depth_final_layout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL)const; - - void CreateSubpassDependency(VkSubpassDependency *); - void CreateSubpassDependency(List &dependency,const uint32_t count)const; - void CreateSubpassDescription(VkSubpassDescription &,const List &color_ref_list,VkAttachmentReference *depth_ref=nullptr)const; - RenderPass * CreateRenderPass( const List &desc_list, const List &subpass, const List &dependency, const List &color_format, const VkFormat depth_format, const VkImageLayout color_final_layout=VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, - const VkImageLayout depth_final_layout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL)const; + const VkImageLayout depth_final_layout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); RenderPass * CreateRenderPass( const VkFormat color_format, const VkFormat depth_format, const VkImageLayout color_final_layout=VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, - const VkImageLayout depth_final_layout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL)const; + const VkImageLayout depth_final_layout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); Fence * CreateFence(bool); vulkan::Semaphore * CreateSem(); @@ -283,9 +264,17 @@ public: RenderTarget *CreateRenderTarget(Framebuffer *); - RenderTarget *CreateRenderTarget(const uint,const uint,const List &); - RenderTarget *CreateRenderTarget(const uint,const uint,const VkFormat); - RenderTarget *CreateRenderTarget(const uint,const uint,const VkFormat,const VkFormat); + RenderTarget *CreateRenderTarget( const uint,const uint,const List &, + const VkImageLayout color_final_layout=VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, + const VkImageLayout depth_final_layout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); + + RenderTarget *CreateRenderTarget( const uint,const uint,const VkFormat,const VkImageLayout final_layout); + + RenderTarget *CreateRenderTarget( const uint,const uint, + const VkFormat color_format, + const VkFormat depth_format, + const VkImageLayout color_final_layout=VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, + const VkImageLayout depth_final_layout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); Pipeline *CreatePipeline(PipelineData *,const Material *,const RenderTarget *); @@ -294,6 +283,25 @@ public: TileFont *CreateTileFont(FontSource *fs,int limit_count=-1); ///<创建一个Tile字体 };//class Device +void CreateSubpassDependency(VkSubpassDependency *); +void CreateSubpassDependency(List &dependency,const uint32_t count); + +void CreateAttachmentReference(VkAttachmentReference *ref_list,uint count,VkImageLayout layout); + +inline void CreateColorAttachmentReference(VkAttachmentReference *ref_list,uint count ){CreateAttachmentReference(ref_list, count,VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);} +inline void CreateDepthAttachmentReference(VkAttachmentReference *depth_ref) {CreateAttachmentReference(depth_ref, 1 ,VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);} +inline void CreateInputAttachmentReference(VkAttachmentReference *ref_list,uint count ){CreateAttachmentReference(ref_list, count,VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);} + + +bool CreateColorAttachment( List &ref_list,List &desc_list,const List &color_format,const VkImageLayout color_final_layout=VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); +bool CreateDepthAttachment( List &ref_list,List &desc_list,const VkFormat &depth_format,const VkImageLayout depth_final_layout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); + +bool CreateAttachment( List &color_output_desc_list, + const List &color_format, + const VkFormat depth_format, + const VkImageLayout color_final_layout=VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, + const VkImageLayout depth_final_layout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); + Device *CreateRenderDevice(Instance *inst,Window *win,const PhysicalDevice *physical_device=nullptr); VK_NAMESPACE_END #endif//HGL_GRAPH_RENDER_SURFACE_INCLUDE diff --git a/inc/hgl/graph/vulkan/VKFramebuffer.h b/inc/hgl/graph/vulkan/VKFramebuffer.h index 073b0135..e280ddc9 100644 --- a/inc/hgl/graph/vulkan/VKFramebuffer.h +++ b/inc/hgl/graph/vulkan/VKFramebuffer.h @@ -13,25 +13,14 @@ class Framebuffer uint32_t color_count; bool has_depth; + ObjectList color_texture; + Texture2D *depth_texture; + private: - friend Framebuffer *CreateFramebuffer(Device *dev,RenderPass *rp,ImageView **color_list,const uint color_count,ImageView *depth); + friend Framebuffer *CreateFramebuffer(VkDevice dev,RenderPass *rp,ImageView **color_list,const uint color_count,ImageView *depth); - Framebuffer(VkDevice dev,VkFramebuffer fb,VkFramebufferCreateInfo *fb_create_info,bool depth) - { - device=dev; - frame_buffer=fb; - fb_info=fb_create_info; - - extent.width=fb_info->width; - extent.height=fb_info->height; - - has_depth=depth; - if(has_depth) - color_count=fb_info->attachmentCount-1; - else - color_count=fb_info->attachmentCount; - } + Framebuffer(VkDevice dev,VkFramebuffer fb,VkFramebufferCreateInfo *fb_create_info,bool depth); public: @@ -45,11 +34,13 @@ public: const uint32_t GetAttachmentCount ()const{return fb_info->attachmentCount;} ///<获取渲染目标成分数量 const uint32_t GetColorCount ()const{return color_count;} ///<取得颜色成分数量 const bool HasDepth ()const{return has_depth;} ///<是否包含深度成分 + + Texture2D * GetColorTexture (const int index=0){return color_texture[index];} + Texture2D * GetDepthTexture (){return depth_texture;} };//class Framebuffer -Framebuffer *CreateFramebuffer(Device *,RenderPass *,List &color,ImageView *depth); -Framebuffer *CreateFramebuffer(Device *,RenderPass *,List &image_view_list); -Framebuffer *CreateColorFramebuffer(Device *,RenderPass *,ImageView *color,ImageView *depth=nullptr); -Framebuffer *CreateDepthFramebuffer(Device *,RenderPass *,ImageView *depth); +Framebuffer *CreateFramebuffer(VkDevice,RenderPass *,List &color,ImageView *depth); +Framebuffer *CreateFramebuffer(VkDevice,RenderPass *,ImageView *color,ImageView *depth); +Framebuffer *CreateFramebuffer(VkDevice,RenderPass *,ImageView *); VK_NAMESPACE_END #endif//HGL_GRAPH_VULKAN_FRAMEBUFFER_INCLUDE diff --git a/inc/hgl/graph/vulkan/VKRenderPass.h b/inc/hgl/graph/vulkan/VKRenderPass.h index e3d966da..b5d27a61 100644 --- a/inc/hgl/graph/vulkan/VKRenderPass.h +++ b/inc/hgl/graph/vulkan/VKRenderPass.h @@ -44,6 +44,12 @@ public: const uint GetColorCount()const{return color_formats.GetCount();} const List & GetColorFormat()const{return color_formats;} + const VkFormat GetColorFormat(int index)const + { + if(index<0||index>=color_formats.GetCount())return VK_FORMAT_UNDEFINED; + + return color_formats.GetData()[index]; + } const VkFormat GetDepthFormat()const{return depth_format;} };//class RenderPass VK_NAMESPACE_END diff --git a/inc/hgl/graph/vulkan/VKRenderTarget.h b/inc/hgl/graph/vulkan/VKRenderTarget.h index 39edb1d0..2278dfd2 100644 --- a/inc/hgl/graph/vulkan/VKRenderTarget.h +++ b/inc/hgl/graph/vulkan/VKRenderTarget.h @@ -50,7 +50,7 @@ protected: public: - virtual ~RenderTarget()=default; + virtual ~RenderTarget(); const VkExtent2D & GetExtent ()const {return extent;} CommandBuffer * GetCommandBuffer() {return command_buffer;} diff --git a/inc/hgl/gui/Form.h b/inc/hgl/gui/Form.h new file mode 100644 index 00000000..bd522944 --- /dev/null +++ b/inc/hgl/gui/Form.h @@ -0,0 +1,31 @@ +#ifndef HGL_GUI_FORM_INCLUDE +#define HGL_GUI_FORM_INCLUDE + +#include +namespace hgl +{ + namespace gui + { + using namespace hgl::graph; + + /** + * 窗体组件,窗体是承载所有GUI控件的基本装置 + */ + class Form + { + protected: //每个窗体独立一个FBO存在,所以每个窗体会有自己的RenderTarget与pipeline + + vulkan::Buffer *ui_matrix; + + struct + { + vulkan::Pipeline *solid; + vulkan::Pipeline *mask; + vulkan::Pipeline *alpha; + }pipeline; + + public: + };//class Form + }//namespace gui +}//namespace hgl +#endif//HGL_GUI_FORM_INCLUDE diff --git a/src/GUI/CMakeLists.txt b/src/GUI/CMakeLists.txt index a0c4f707..b89f8576 100644 --- a/src/GUI/CMakeLists.txt +++ b/src/GUI/CMakeLists.txt @@ -1,6 +1,7 @@ file(GLOB GUI_HEADER ${ROOT_INCLUDE_PATH}/hgl/gui/*.*) set(GUI_SOURCE Widget.cpp + Form.cpp Panel.cpp ThemeEngine.cpp DefaultThemeEngine.h diff --git a/src/GUI/DefaultThemeEngine.cpp b/src/GUI/DefaultThemeEngine.cpp index 055343be..00a90e56 100644 --- a/src/GUI/DefaultThemeEngine.cpp +++ b/src/GUI/DefaultThemeEngine.cpp @@ -6,7 +6,19 @@ namespace hgl { ThemeEngine *CreateDefaultThemeEngine() { - + return(new DefaultThemeEngine); + } + + bool DefaultThemeEngine::Init() + { + } + + void DefaultThemeEngine::Clear() + { + } + + void DefaultThemeEngine::DrawFrame(const Widget *w) + { } }//namespace gui }//namespace hgl \ No newline at end of file diff --git a/src/GUI/DefaultThemeEngine.h b/src/GUI/DefaultThemeEngine.h index 34df7c0a..e82efca8 100644 --- a/src/GUI/DefaultThemeEngine.h +++ b/src/GUI/DefaultThemeEngine.h @@ -1,23 +1,32 @@ #pragma once #include +#include namespace hgl { namespace gui { + using namespace hgl::graph; + /** * 缺省GUI主题引擎 */ class DefaultThemeEngine:public ThemeEngine { + + struct + { + vulkan::Material * m; + vulkan::MaterialInstance * mi; + }panel; + public: bool Init() override; void Clear() override; - void DrawFrame(const RectScope2f &) override; - + void DrawFrame(const Widget *) override; };//class DefaultThemeEngine:public ThemeEngine }//namespace gui }//namespace hgl \ No newline at end of file diff --git a/src/GUI/Form.cpp b/src/GUI/Form.cpp new file mode 100644 index 00000000..e69de29b diff --git a/src/RenderDevice/Vulkan/CMakeLists.txt b/src/RenderDevice/Vulkan/CMakeLists.txt index 862ade24..1b2aeae2 100644 --- a/src/RenderDevice/Vulkan/CMakeLists.txt +++ b/src/RenderDevice/Vulkan/CMakeLists.txt @@ -26,7 +26,8 @@ SET(VK_DEVICE_SOURCE ${RD_INCLUDE_PATH}/VKDevice.h VKDeviceImage.cpp VKDeviceTexture.cpp VKDeviceSwapchain.cpp - VKDeviceRenderPass.cpp) + VKDeviceRenderPass.cpp + VKDeviceRenderTarget.cpp) SET(VK_PHYSICAL_DEVICE_SOURCE ${RD_INCLUDE_PATH}/VKPhysicalDevice.h VKPhysicalDevice.cpp diff --git a/src/RenderDevice/Vulkan/VKDevice.cpp b/src/RenderDevice/Vulkan/VKDevice.cpp index 10bb6724..6d015979 100644 --- a/src/RenderDevice/Vulkan/VKDevice.cpp +++ b/src/RenderDevice/Vulkan/VKDevice.cpp @@ -103,16 +103,4 @@ vulkan::Semaphore *Device::CreateSem() return(new vulkan::Semaphore(attr->device,sem)); } - -RenderTarget *Device::CreateRenderTarget(Framebuffer *fb) -{ - return(new RenderTarget(this,fb)); -} - -Pipeline *CreatePipeline(VkDevice device,VkPipelineCache pipeline_cache,PipelineData *data,const Material *material,const RenderTarget *rt); - -Pipeline *Device::CreatePipeline(PipelineData *pd,const Material *mtl,const RenderTarget *rt) -{ - return VK_NAMESPACE::CreatePipeline(attr->device,attr->pipeline_cache,pd,mtl,rt); -} VK_NAMESPACE_END diff --git a/src/RenderDevice/Vulkan/VKDeviceRenderPass.cpp b/src/RenderDevice/Vulkan/VKDeviceRenderPass.cpp index c5afa911..e42470a7 100644 --- a/src/RenderDevice/Vulkan/VKDeviceRenderPass.cpp +++ b/src/RenderDevice/Vulkan/VKDeviceRenderPass.cpp @@ -3,7 +3,7 @@ #include VK_NAMESPACE_BEGIN -void Device::CreateSubpassDependency(VkSubpassDependency *dependency) +void CreateSubpassDependency(VkSubpassDependency *dependency) { dependency->srcSubpass = VK_SUBPASS_EXTERNAL; dependency->dstSubpass = 0; @@ -14,7 +14,7 @@ void Device::CreateSubpassDependency(VkSubpassDependency *dependency) dependency->dstAccessMask = VK_ACCESS_MEMORY_READ_BIT; } -void Device::CreateSubpassDependency(List &subpass_dependency_list,const uint32_t count) const +void CreateSubpassDependency(List &subpass_dependency_list,const uint32_t count) { if(count<=0)return; @@ -67,11 +67,11 @@ void Device::CreateSubpassDependency(List &subpass_dependen } } -void Device::CreateAttachmentReference(VkAttachmentReference *ref_list,uint start,uint count,VkImageLayout layout) const +void CreateAttachmentReference(VkAttachmentReference *ref_list,uint count,VkImageLayout layout) { VkAttachmentReference *ref=ref_list; - for(uint i=start;iattachment =i; ref->layout =layout; @@ -80,7 +80,7 @@ void Device::CreateAttachmentReference(VkAttachmentReference *ref_list,uint star } } -bool Device::CreateAttachment(List &desc_list,const List &color_format,const VkFormat depth_format,VkImageLayout color_final_layout,VkImageLayout depth_final_layout) const +bool CreateAttachment(List &desc_list,const List &color_format,const VkFormat depth_format,VkImageLayout color_final_layout,VkImageLayout depth_final_layout) { const uint color_count=color_format.GetCount(); @@ -118,17 +118,17 @@ bool Device::CreateAttachment(List &desc_list,const Lis return(true); } -bool Device::CreateColorAttachment( List &ref_list,List &desc_list,const List &color_format,const VkImageLayout color_final_layout) const +bool CreateColorAttachment( List &ref_list,List &desc_list,const List &color_format,const VkImageLayout color_final_layout) { - const VkFormat *cf=color_format.GetData(); + //const VkFormat *cf=color_format_list.GetData(); - for(int i=0;iphysical_device->IsColorAttachmentOptimal(*cf)) - return(false); + //for(int i=0;iphysical_device->IsColorAttachmentOptimal(*cf)) + // return(false); - ++cf; - } + // ++cf; + //} ref_list.SetCount(color_format.GetCount()); VkAttachmentReference *ref=ref_list.GetData(); @@ -156,10 +156,10 @@ bool Device::CreateColorAttachment( List &ref_list,List &ref_list,List &desc_list,const VkFormat &depth_format,const VkImageLayout depth_final_layout) const +bool CreateDepthAttachment( List &ref_list,List &desc_list,const VkFormat &depth_format,const VkImageLayout depth_final_layout) { - if(!attr->physical_device->IsDepthAttachmentOptimal(depth_format)) - return(false); + //if(!attr->physical_device->IsDepthAttachmentOptimal(depth_format)) + // return(false); { ref_list.SetCount(1); @@ -186,42 +186,19 @@ bool Device::CreateDepthAttachment( List &ref_list,List &color_ref_list,VkAttachmentReference *depth_ref) const -{ - sd.flags =0; - sd.pipelineBindPoint =VK_PIPELINE_BIND_POINT_GRAPHICS; - sd.inputAttachmentCount =0; - sd.pInputAttachments =nullptr; - - sd.pColorAttachments =color_ref_list.GetData(); - sd.colorAttachmentCount =color_ref_list.GetCount(); - - sd.pDepthStencilAttachment =depth_ref; - - sd.pResolveAttachments =nullptr; - sd.preserveAttachmentCount =0; - sd.pPreserveAttachments =nullptr; -} - RenderPass *Device::CreateRenderPass( const List &desc_list, const List &subpass, const List &dependency, - const List &color_format, + const List &color_format_list, const VkFormat depth_format, const VkImageLayout color_final_layout, - const VkImageLayout depth_final_layout) const + const VkImageLayout depth_final_layout) { + for(const VkFormat cf:color_format_list) { - const VkFormat *cf=color_format.GetData(); - - for(int i=0;iphysical_device->IsColorAttachmentOptimal(*cf) - &&!attr->physical_device->IsColorAttachmentLinear(*cf)) - return(nullptr); - - ++cf; - } + if(!attr->physical_device->IsColorAttachmentOptimal(cf) + &&!attr->physical_device->IsColorAttachmentLinear(cf)) + return(nullptr); } if(!attr->physical_device->IsDepthAttachmentOptimal(depth_format) @@ -244,10 +221,10 @@ RenderPass *Device::CreateRenderPass( const List &des if(vkCreateRenderPass(attr->device,&rp_info,nullptr,&render_pass)!=VK_SUCCESS) return(nullptr); - return(new RenderPass(attr->device,render_pass,color_format,depth_format)); + return(new RenderPass(attr->device,render_pass,color_format_list,depth_format)); } -RenderPass *Device::CreateRenderPass(VkFormat color_format,VkFormat depth_format,VkImageLayout color_final_layout,VkImageLayout depth_final_layout) const +RenderPass *Device::CreateRenderPass(VkFormat color_format,VkFormat depth_format,VkImageLayout color_final_layout,VkImageLayout depth_final_layout) { if(!attr->physical_device->IsColorAttachmentOptimal(color_format)) return(nullptr); @@ -255,7 +232,7 @@ RenderPass *Device::CreateRenderPass(VkFormat color_format,VkFormat depth_format if(!attr->physical_device->IsDepthAttachmentOptimal(depth_format)) return(nullptr); - List color_ref; + VkAttachmentReference color_ref; VkAttachmentReference depth_ref; List desc_list; @@ -263,27 +240,14 @@ RenderPass *Device::CreateRenderPass(VkFormat color_format,VkFormat depth_format color_format_list.Add(color_format); - CreateColorAttachmentReference(color_ref,0,1); - CreateDepthAttachmentReference(&depth_ref,1); + CreateColorAttachmentReference(&color_ref,1); + CreateDepthAttachmentReference(&depth_ref); CreateAttachment(desc_list,color_format_list,depth_format,color_final_layout,depth_final_layout); List subpass_desc_list; - VkSubpassDescription subpass; - - subpass.flags = 0; - subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; - subpass.inputAttachmentCount = 0; - subpass.pInputAttachments = nullptr; - subpass.colorAttachmentCount = 1; - subpass.pColorAttachments = color_ref.GetData(); - subpass.pResolveAttachments = nullptr; - subpass.pDepthStencilAttachment = &depth_ref; - subpass.preserveAttachmentCount = 0; - subpass.pPreserveAttachments = nullptr; - - subpass_desc_list.Add(subpass); + subpass_desc_list.Add(SubpassDescription(&color_ref,&depth_ref)); List subpass_dependency_list; diff --git a/src/RenderDevice/Vulkan/VKDeviceRenderTarget.cpp b/src/RenderDevice/Vulkan/VKDeviceRenderTarget.cpp new file mode 100644 index 00000000..5de79578 --- /dev/null +++ b/src/RenderDevice/Vulkan/VKDeviceRenderTarget.cpp @@ -0,0 +1,92 @@ +#include + +VK_NAMESPACE_BEGIN +RenderTarget *Device::CreateRenderTarget(Framebuffer *fb) +{ + CommandBuffer *cb=CreateCommandBuffer(fb->GetExtent(),fb->GetAttachmentCount()); + + return(new RenderTarget(this,fb,cb)); +} + +RenderTarget *Device::CreateRenderTarget( const uint w,const uint h, + const VkFormat color_format, + const VkFormat depth_format, + const VkImageLayout color_layout, + const VkImageLayout depth_layout) +{ + if(w<=0||h<=0)return(nullptr); + if(IsDepthStencilFormat(color_format))return(nullptr); + if(!IsDepthFormat(depth_format))return(nullptr); + + if(!CheckTextureFormatSupport(color_format))return(nullptr); + if(!CheckTextureFormatSupport(depth_format))return(nullptr); + + Texture2D *color_texture=CreateAttachmentTextureColor(color_format,w,h); + Texture2D *depth_texture=CreateAttachmentTextureDepth(depth_format,w,h); + + RenderPass *rp=CreateRenderPass(color_format,depth_format,color_layout,depth_layout); + + Framebuffer *fb=CreateFramebuffer(GetDevice(),rp,color_texture->GetImageView(),depth_texture->GetImageView()); + + return(CreateRenderTarget(fb)); +} + +//RenderTarget *Device::CreateRenderTarget(const uint w,const uint h,const List &fmt_list) +//{ +// if(w<=0||h<=0||fmt_list.GetCount()<=0)return(nullptr); +// +// uint color_count=0; +// uint depth_count=0; //只能有一个 +// uint stencil_count=0; +// +// for(VkFormat fmt:fmt_list) +// { +// if(IsDepthFormat(fmt))++depth_count; +// else +// if(IsStencilFormat(fmt))++stencil_count; +// else +// ++color_count; +// +// if(CheckTextureFormatSupport(fmt)) +// return(nullptr); +// } +// +// if(depth_count>1)return(nullptr); +// if(stencil_count>1)return(nullptr); +// +// List color_format_list; +// VkFormat depth_format; +// List desc_list; +// List color_ref_list; +// VkAttachmentReference depth_ref; +// List image_view_list; +// +// for(VkFormat fmt:fmt_list) +// { +// Texture2D *tex=nullptr; +// +// if(IsDepthFormat(fmt)) +// { +// tex=CreateAttachmentTextureDepth(fmt,w,h); +// +// depth_format=fmt; +// } +// else +// { +// tex=CreateAttachmentTextureColor(fmt,w,h); +// +// image_view_list.Add(tex->GetImageView()); +// color_format_list.Add(fmt); +// } +// } +// +// if(depth_count>0)CreateDepthAttachmentReference(&depth_ref,color_count); +// if(color_count>0)CreateColorAttachmentReference(color_ref_list,0,color_count); +// +// CreateAttachment(desc_list,color_format_list,depth_format,VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); +// +// VkSubpassDescription sd; +// +// CreateSubpassDescription(sd,color_ref_list,&depth_ref); +//} +VK_NAMESPACE_END \ No newline at end of file diff --git a/src/RenderDevice/Vulkan/VKDeviceTexture.cpp b/src/RenderDevice/Vulkan/VKDeviceTexture.cpp index 34e8f8c2..ecb6d1ee 100644 --- a/src/RenderDevice/Vulkan/VKDeviceTexture.cpp +++ b/src/RenderDevice/Vulkan/VKDeviceTexture.cpp @@ -21,7 +21,7 @@ namespace } }//namespace -bool Device::CheckFormatSupport(const VkFormat format,const uint32_t bits,ImageTiling tiling) +bool Device::CheckFormatSupport(const VkFormat format,const uint32_t bits,ImageTiling tiling) const { const VkFormatProperties fp=attr->physical_device->GetFormatProperties(format); diff --git a/src/RenderDevice/Vulkan/VKFramebuffer.cpp b/src/RenderDevice/Vulkan/VKFramebuffer.cpp index f748863a..d492c4d8 100644 --- a/src/RenderDevice/Vulkan/VKFramebuffer.cpp +++ b/src/RenderDevice/Vulkan/VKFramebuffer.cpp @@ -2,15 +2,38 @@ #include #include #include +#include #include VK_NAMESPACE_BEGIN + +Framebuffer::Framebuffer(VkDevice dev,VkFramebuffer fb,VkFramebufferCreateInfo *fb_create_info,bool depth) +{ + device=dev; + frame_buffer=fb; + fb_info=fb_create_info; + + extent.width=fb_info->width; + extent.height=fb_info->height; + + has_depth=depth; + if(has_depth) + color_count=fb_info->attachmentCount-1; + else + color_count=fb_info->attachmentCount; + + depth_texture=nullptr; +} + Framebuffer::~Framebuffer() { + SAFE_CLEAR(depth_texture); + color_texture.Clear(); + vkDestroyFramebuffer(device,frame_buffer,nullptr); } -Framebuffer *CreateFramebuffer(Device *dev,RenderPass *rp,ImageView **color_list,const uint color_count,ImageView *depth) +Framebuffer *CreateFramebuffer(VkDevice dev,RenderPass *rp,ImageView **color_list,const uint color_count,ImageView *depth) { uint att_count=color_count; @@ -60,13 +83,13 @@ Framebuffer *CreateFramebuffer(Device *dev,RenderPass *rp,ImageView **color_list VkFramebuffer fb; - if(vkCreateFramebuffer(dev->GetDevice(),fb_info,nullptr,&fb)!=VK_SUCCESS) + if(vkCreateFramebuffer(dev,fb_info,nullptr,&fb)!=VK_SUCCESS) return(nullptr); - return(new Framebuffer(dev->GetDevice(),fb,fb_info,depth)); + return(new Framebuffer(dev,fb,fb_info,depth)); } -Framebuffer *CreateFramebuffer(Device *dev,RenderPass *rp,List &color,ImageView *depth) +Framebuffer *CreateFramebuffer(VkDevice dev,RenderPass *rp,List &color,ImageView *depth) { if(!dev)return(nullptr); if(!rp)return(nullptr); @@ -78,19 +101,7 @@ Framebuffer *CreateFramebuffer(Device *dev,RenderPass *rp,List &col return CreateFramebuffer(dev,rp,color.GetData(),color.GetCount(),depth); } -Framebuffer *CreateFramebuffer(Device *dev,RenderPass *rp,List &image_view_list) -{ - const int count=image_view_list.GetCount(); - - ImageView *last_iv=*(image_view_list.GetData()+count-1); - - if(last_iv->GetAspectFlags()&VK_IMAGE_ASPECT_DEPTH_BIT) - return CreateFramebuffer(dev,rp,image_view_list.GetData(),count-1,last_iv); - else - return CreateFramebuffer(dev,rp,image_view_list.GetData(),count,nullptr); -} - -Framebuffer *CreateColorFramebuffer(Device *dev,RenderPass *rp,ImageView *color,ImageView *depth) +Framebuffer *CreateFramebuffer(VkDevice dev,RenderPass *rp,ImageView *color,ImageView *depth) { if(!dev)return(nullptr); if(!rp)return(nullptr); @@ -99,12 +110,18 @@ Framebuffer *CreateColorFramebuffer(Device *dev,RenderPass *rp,ImageView *color, return CreateFramebuffer(dev,rp,&color,1,depth); } -Framebuffer *CreateDepthFramebuffer(Device *dev,RenderPass *rp,ImageView *depth) +Framebuffer *CreateFramebuffer(VkDevice dev,RenderPass *rp,ImageView *iv) { if(!dev)return(nullptr); if(!rp)return(nullptr); - if(!depth)return(nullptr); + if(!iv)return(nullptr); - return CreateFramebuffer(dev,rp,nullptr,0,depth); + if(iv->hasColor()) + return CreateFramebuffer(dev,rp,&iv,1,nullptr); + else + if(iv->hasDepth()) + return CreateFramebuffer(dev,rp,nullptr,0,iv); + else + return nullptr; } VK_NAMESPACE_END diff --git a/src/RenderDevice/Vulkan/VKPipeline.cpp b/src/RenderDevice/Vulkan/VKPipeline.cpp index bbdbcf89..e7b21f83 100644 --- a/src/RenderDevice/Vulkan/VKPipeline.cpp +++ b/src/RenderDevice/Vulkan/VKPipeline.cpp @@ -39,4 +39,9 @@ Pipeline *CreatePipeline(VkDevice device,VkPipelineCache pipeline_cache,Pipeline return(new Pipeline(device,graphicsPipeline,data)); } + +Pipeline *Device::CreatePipeline(PipelineData *pd,const Material *mtl,const RenderTarget *rt) +{ + return VK_NAMESPACE::CreatePipeline(attr->device,attr->pipeline_cache,pd,mtl,rt); +} VK_NAMESPACE_END diff --git a/src/RenderDevice/Vulkan/VKRenderTarget.cpp b/src/RenderDevice/Vulkan/VKRenderTarget.cpp index 52c5dd90..cc757dd2 100644 --- a/src/RenderDevice/Vulkan/VKRenderTarget.cpp +++ b/src/RenderDevice/Vulkan/VKRenderTarget.cpp @@ -2,6 +2,7 @@ #include #include #include +#include VK_NAMESPACE_BEGIN namespace @@ -96,7 +97,12 @@ RenderTarget::RenderTarget(Device *dev,Framebuffer *_fb,CommandBuffer *_cb,const command_buffer=_cb; } -SwapchainRenderTarget::SwapchainRenderTarget(Device *dev,Swapchain *sc):RenderTarget(dev,nullptr,sc->GetImageCount()) +RenderTarget::~RenderTarget() +{ + SAFE_CLEAR(command_buffer); +} + +SwapchainRenderTarget::SwapchainRenderTarget(Device *dev,Swapchain *sc):RenderTarget(dev,nullptr,nullptr,sc->GetImageCount()) { swapchain=sc; vk_swapchain=swapchain->GetSwapchain(); @@ -118,7 +124,7 @@ SwapchainRenderTarget::SwapchainRenderTarget(Device *dev,Swapchain *sc):RenderTa for(uint i=0;iGetImageView(),sc_depth->GetImageView())); + render_frame.Add(vulkan::CreateFramebuffer(device->GetDevice(),main_rp,(*sc_color)->GetImageView(),sc_depth->GetImageView())); ++sc_color; }