diff --git a/CMSceneGraph b/CMSceneGraph index 2a750452..c4642ea8 160000 --- a/CMSceneGraph +++ b/CMSceneGraph @@ -1 +1 @@ -Subproject commit 2a750452cb0e0fef7e4c7a5c55d02e2cbb45d94e +Subproject commit c4642ea87cb4abf4b68e94a63d868bdd66666dbe diff --git a/example/Vulkan/DeferredModel.cpp b/example/Vulkan/DeferredModel.cpp index ae6ff7c5..4ab2458b 100644 --- a/example/Vulkan/DeferredModel.cpp +++ b/example/Vulkan/DeferredModel.cpp @@ -50,7 +50,18 @@ private: SceneNode render_root; RenderList render_list; - RenderTarget *gbuffer_rt; + struct + { + RenderTarget *rt; + GPUCmdBuffer *cmd; + + public: + + bool Submit(GPUSemaphore *sem) + { + return rt->Submit(cmd,sem); + } + }gbuffer; PhongPointLight lights; @@ -89,23 +100,25 @@ public: ~TestApp() { - delete gbuffer_rt; + delete gbuffer.cmd; + delete gbuffer.rt; } private: bool InitGBuffer() { - List gbuffer_color_format_list(gbuffer_color_format,size_t(GBufferAttachment::RANGE_SIZE)); + FramebufferInfo fbi(gbuffer_color_format,size_t(GBufferAttachment::RANGE_SIZE),gbuffer_depth_format); - gbuffer_rt=device->CreateRenderTarget( SCREEN_WIDTH, - SCREEN_HEIGHT, - gbuffer_color_format_list, - gbuffer_depth_format, - VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, - VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); + fbi.SetExtent(SCREEN_WIDTH,SCREEN_HEIGHT); - return(gbuffer_rt); + gbuffer.rt=device->CreateRenderTarget(&fbi); + + if(!gbuffer.rt)return(false); + + gbuffer.cmd=device->CreateCommandBuffer(size_t(GBufferAttachment::RANGE_SIZE)+1); + + return(gbuffer.rt); } bool InitSubpass(SubpassParam *sp,const OSString &material_filename) @@ -121,11 +134,11 @@ private: bool InitGBufferPipeline(SubpassParam *sp) { - sp->pipeline_triangles =gbuffer_rt->CreatePipeline(sp->material,InlinePipeline::Solid3D,Prim::Triangles); + sp->pipeline_triangles =gbuffer.rt->CreatePipeline(sp->material,InlinePipeline::Solid3D,Prim::Triangles); if(!sp->pipeline_triangles) return(false); - sp->pipeline_fan =gbuffer_rt->CreatePipeline(sp->material,InlinePipeline::Solid3D,Prim::Fan); + sp->pipeline_fan =gbuffer.rt->CreatePipeline(sp->material,InlinePipeline::Solid3D,Prim::Fan); return sp->pipeline_fan; } @@ -187,9 +200,9 @@ private: sp_composition.material_instance->BindUBO("world",GetCameraMatrixBuffer()); sp_composition.material_instance->BindUBO("lights",ubo_lights); - sp_composition.material_instance->BindSampler("GB_Color" ,gbuffer_rt->GetColorTexture((uint)GBufferAttachment::Color),sampler); - sp_composition.material_instance->BindSampler("GB_Normal" ,gbuffer_rt->GetColorTexture((uint)GBufferAttachment::Normal),sampler); - sp_composition.material_instance->BindSampler("GB_Depth" ,gbuffer_rt->GetDepthTexture(),sampler); + sp_composition.material_instance->BindSampler("GB_Color" ,gbuffer.rt->GetColorTexture((uint)GBufferAttachment::Color),sampler); + sp_composition.material_instance->BindSampler("GB_Normal" ,gbuffer.rt->GetColorTexture((uint)GBufferAttachment::Normal),sampler); + sp_composition.material_instance->BindSampler("GB_Depth" ,gbuffer.rt->GetDepthTexture(),sampler); sp_composition.material_instance->Update(); return(true); @@ -278,19 +291,17 @@ private: bool InitGBufferCommandBuffer() { - GPUCmdBuffer *gbuffer_cmd=gbuffer_rt->GetCommandBuffer(); - - if(!gbuffer_cmd) + if(!gbuffer.cmd) return(false); - gbuffer_cmd->Begin(); - if(!gbuffer_cmd->BindFramebuffer(gbuffer_rt)) + gbuffer.cmd->Begin(); + if(!gbuffer.cmd->BindFramebuffer(gbuffer.rt->GetRenderPass(),gbuffer.rt->GetFramebuffer())) return(false); - render_list.Render(gbuffer_cmd); + render_list.Render(gbuffer.cmd); - gbuffer_cmd->EndRenderPass(); - gbuffer_cmd->End(); + gbuffer.cmd->EndRenderPass(); + gbuffer.cmd->End(); return(true); } @@ -337,17 +348,17 @@ public: virtual void SubmitDraw(int index) override { - gbuffer_rt->Submit(sc_render_target->GetPresentCompleteSemaphore()); + gbuffer.Submit(sc_render_target->GetPresentCompleteSemaphore()); VkCommandBuffer cb=*cmd_buf[index]; - sc_render_target->Submit(cb,gbuffer_rt->GetRenderCompleteSemaphore()); + sc_render_target->Submit(cb,gbuffer.rt->GetRenderCompleteSemaphore()); sc_render_target->PresentBackbuffer(); sc_render_target->WaitQueue(); sc_render_target->WaitFence(); - gbuffer_rt->WaitQueue(); - gbuffer_rt->WaitFence(); + gbuffer.rt->WaitQueue(); + gbuffer.rt->WaitFence(); } void BuildCommandBuffer(uint32_t index) override diff --git a/example/Vulkan/OffscreenRender.cpp b/example/Vulkan/OffscreenRender.cpp index 9b464c6a..164131a6 100644 --- a/example/Vulkan/OffscreenRender.cpp +++ b/example/Vulkan/OffscreenRender.cpp @@ -22,9 +22,22 @@ class TestApp:public CameraAppFramework struct:public RenderObject { RenderTarget * render_taget =nullptr; + GPUCmdBuffer * command_buffer =nullptr; Pipeline * pipeline =nullptr; RenderableInstance *renderable_instance =nullptr; + + public: + + bool Submit() + { + if(!render_taget->Submit(command_buffer)) + return(false); + if(!render_taget->WaitQueue()) + return(false); + + return(true); + } }os; struct:public RenderObject @@ -64,8 +77,13 @@ public: bool InitOffscreen() { - os.render_taget=device->CreateColorRenderTarget(OFFSCREEN_SIZE,OFFSCREEN_SIZE,UFMT_RGBA8); + FramebufferInfo fbi(UFMT_RGBA8,OFFSCREEN_SIZE,OFFSCREEN_SIZE); + + os.render_taget=device->CreateRenderTarget(&fbi); if(!os.render_taget)return(false); + + os.command_buffer=device->CreateCommandBuffer(fbi.GetAttachmentCount()); + if(!os.command_buffer)return(false); os.material_instance=db->CreateMaterialInstance(OS_TEXT("res/material/VertexColor2D")); if(!os.material_instance)return(false); @@ -94,10 +112,9 @@ public: if(!os.renderable_instance)return(false); } - VulkanApplicationFramework::BuildCommandBuffer(os.render_taget,os.renderable_instance); + VulkanApplicationFramework::BuildCommandBuffer(os.command_buffer,os.render_taget,os.renderable_instance); - os.render_taget->Submit(nullptr); - os.render_taget->WaitQueue(); + os.Submit(); return(true); } diff --git a/example/common/VulkanAppFramework.h b/example/common/VulkanAppFramework.h index 781f6956..4d2bf130 100644 --- a/example/common/VulkanAppFramework.h +++ b/example/common/VulkanAppFramework.h @@ -169,11 +169,11 @@ public: cmd_buf=hgl_zero_new(swap_chain_count); for(int32_t i=0;iCreateCommandBuffer(extent,2); + cmd_buf[i]=device->CreateCommandBuffer(2); } } - bool BuildCommandBuffer(GPUCmdBuffer *cb,VkRenderPass rp,VkFramebuffer fb,RenderableInstance *ri) + bool BuildCommandBuffer(GPUCmdBuffer *cb,RenderPass *rp,Framebuffer *fb,RenderableInstance *ri) { if(!ri)return(false); @@ -198,12 +198,12 @@ public: return(true); } - void BuildCommandBuffer(RenderTarget *rt,RenderableInstance *ri) + void BuildCommandBuffer(GPUCmdBuffer *cb,RenderTarget *rt,RenderableInstance *ri) { - if(!rt||!ri) + if(!cb||!rt||!ri) return; - BuildCommandBuffer(rt->GetCommandBuffer(),rt->GetRenderPass(),rt->GetFramebuffer(),ri); + BuildCommandBuffer(cb,rt->GetRenderPass(),rt->GetFramebuffer(),ri); } bool BuildCommandBuffer(uint32_t index,RenderableInstance *ri) diff --git a/inc/hgl/graph/VK.h b/inc/hgl/graph/VK.h index 220eeb17..19f05c17 100644 --- a/inc/hgl/graph/VK.h +++ b/inc/hgl/graph/VK.h @@ -8,6 +8,7 @@ #include #include #include +#include VK_NAMESPACE_BEGIN diff --git a/inc/hgl/graph/VKCommandBuffer.h b/inc/hgl/graph/VKCommandBuffer.h index 838805c2..70f042fd 100644 --- a/inc/hgl/graph/VKCommandBuffer.h +++ b/inc/hgl/graph/VKCommandBuffer.h @@ -24,7 +24,7 @@ class GPUCmdBuffer public: - GPUCmdBuffer(VkDevice dev,const VkExtent2D &extent,const uint32_t att_count,VkCommandPool cp,VkCommandBuffer cb); + GPUCmdBuffer(VkDevice dev,const uint32_t att_count,VkCommandPool cp,VkCommandBuffer cb); ~GPUCmdBuffer(); operator VkCommandBuffer(){return cmd_buf;} @@ -66,9 +66,7 @@ public: template void CopyImageToBuffer (ARGS...args){vkCmdCopyImageToBuffer(cmd_buf,args...);} template void BlitImage (ARGS...args){vkCmdBlitImage (cmd_buf,args...);} - bool BindFramebuffer(VkRenderPass rp,VkFramebuffer fb); -// bool BindFramebuffer(Framebuffer *); - bool BindFramebuffer(RenderTarget *); + bool BindFramebuffer(RenderPass *rp,Framebuffer *fb); bool BindPipeline(Pipeline *p) { diff --git a/inc/hgl/graph/VKDevice.h b/inc/hgl/graph/VKDevice.h index ffcf8211..9e7ebc67 100644 --- a/inc/hgl/graph/VKDevice.h +++ b/inc/hgl/graph/VKDevice.h @@ -180,28 +180,17 @@ public: //shader & material public: //Command GPUBuffer 相关 - GPUCmdBuffer * CreateCommandBuffer(const VkExtent2D &extent,const uint32_t atta_count); + GPUCmdBuffer * CreateCommandBuffer(const uint32_t attachment_count); 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 RenderbufferInfo *); - RenderPass * CreateRenderPass( 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); + RenderPass * CreateRenderPass( const RenderbufferInfo *); - RenderPass * CreateRenderPass( 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); - - GPUFence * CreateFence(bool); - GPUSemaphore * CreateSemaphore(); + GPUFence * CreateFence(bool); + GPUSemaphore * CreateSemaphore(); public: //FrameBuffer相关 @@ -216,31 +205,7 @@ public: RenderTarget *CreateRenderTarget( Framebuffer *,const uint32_t fence_count=1); - RenderTarget *CreateRenderTarget( const uint,const uint, - const List &color_format_list, - 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 uint32_t fence_count=1); - - 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, - const uint32_t fence_count=1); - - RenderTarget *CreateRenderTarget( const uint,const uint,const VkFormat,const VkImageLayout final_layout,const uint32_t fence_count=1); - - RenderTarget *CreateColorRenderTarget( const uint w,const uint h,const VkFormat format,const uint32_t fence_count=1) - { - return CreateRenderTarget(w,h,format,VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,fence_count); - } - - RenderTarget *CreateDepthRenderTarget( const uint w,const uint h,const VkFormat format,const uint32_t fence_count=1) - { - return CreateRenderTarget(w,h,format,VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,fence_count); - } + RenderTarget *CreateRenderTarget( const FramebufferInfo *fbi,const uint32_t fence_count=1); public: diff --git a/inc/hgl/graph/VKFramebuffer.h b/inc/hgl/graph/VKFramebuffer.h index fcbeb246..2e2c2b8f 100644 --- a/inc/hgl/graph/VKFramebuffer.h +++ b/inc/hgl/graph/VKFramebuffer.h @@ -24,6 +24,8 @@ public: ~Framebuffer(); + operator VkFramebuffer(){return frame_buffer;} + const VkFramebuffer GetFramebuffer ()const{return frame_buffer;} const VkRenderPass GetRenderPass ()const{return render_pass;} diff --git a/inc/hgl/graph/VKRenderTarget.h b/inc/hgl/graph/VKRenderTarget.h index 854cba91..4c295037 100644 --- a/inc/hgl/graph/VKRenderTarget.h +++ b/inc/hgl/graph/VKRenderTarget.h @@ -21,7 +21,6 @@ protected: VkExtent2D extent; GPUSemaphore *render_complete_semaphore =nullptr; - GPUCmdBuffer *command_buffer =nullptr; protected: @@ -37,22 +36,23 @@ protected: friend class GPUDevice; - RenderTarget(GPUDevice *dev,Framebuffer *_fb,GPUCmdBuffer *_cb,const uint32_t fence_count=1); - RenderTarget(GPUDevice *dev,RenderPass *_rp,Framebuffer *_fb,GPUCmdBuffer *_cb,Texture2D **color_texture_list,const uint32_t color_count,Texture2D *depth_texture,const uint32_t fence_count=1); + RenderTarget(GPUDevice *dev,Framebuffer *_fb,const uint32_t fence_count=1); + RenderTarget(GPUDevice *dev,RenderPass *_rp,Framebuffer *_fb,Texture2D **color_texture_list,const uint32_t color_count,Texture2D *depth_texture,const uint32_t fence_count=1); public: virtual ~RenderTarget(); const VkExtent2D & GetExtent ()const {return extent;} - virtual const VkRenderPass GetRenderPass ()const {return *render_pass;} + virtual RenderPass * GetRenderPass () {return render_pass;} + virtual const VkRenderPass GetVkRenderPass ()const {return *render_pass;} virtual const uint32_t GetColorCount ()const {return fbo->GetColorCount();} - virtual const VkFramebuffer GetFramebuffer ()const {return fbo->GetFramebuffer();} + virtual Framebuffer * GetFramebuffer () {return fbo;} virtual Texture2D * GetColorTexture (const int index=0){return color_textures[index];} virtual Texture2D * GetDepthTexture (){return depth_texture;} -public: +public: //pipeline Pipeline *CreatePipeline(Material *, const InlinePipeline &, const Prim &prim=Prim::Triangles,const bool prim_restart=false); Pipeline *CreatePipeline(MaterialInstance *, const InlinePipeline &, const Prim &prim=Prim::Triangles,const bool prim_restart=false); @@ -61,11 +61,10 @@ public: Pipeline *CreatePipeline(Material *, const OSString &, const Prim &prim=Prim::Triangles,const bool prim_restart=false); Pipeline *CreatePipeline(MaterialInstance *, const OSString &, const Prim &prim=Prim::Triangles,const bool prim_restart=false); -public: +public: // command buffer GPUSemaphore * GetRenderCompleteSemaphore (){return render_complete_semaphore;} - GPUCmdBuffer * GetCommandBuffer (){return command_buffer;} - virtual bool Submit (GPUSemaphore *present_complete_semaphore=nullptr); + virtual bool Submit (GPUCmdBuffer *,GPUSemaphore *present_complete_semaphore=nullptr); };//class RenderTarget /** @@ -89,8 +88,8 @@ public: SwapchainRenderTarget(GPUDevice *dev,Swapchain *sc); ~SwapchainRenderTarget(); - const VkFramebuffer GetFramebuffer ()const override {return render_frame[current_frame]->GetFramebuffer();} - VkFramebuffer GetFramebuffer (const uint32_t index) {return render_frame[index]->GetFramebuffer();} + Framebuffer * GetFramebuffer ()override {return render_frame[current_frame];} + Framebuffer * GetFramebuffer (const uint32_t index) {return render_frame[index];} const uint32_t GetColorCount ()const override {return 1;} const uint32_t GetImageCount ()const {return swap_chain_count;} diff --git a/inc/hgl/graph/VKTextureCreateInfo.h b/inc/hgl/graph/VKTextureCreateInfo.h index 13412267..971fbb49 100644 --- a/inc/hgl/graph/VKTextureCreateInfo.h +++ b/inc/hgl/graph/VKTextureCreateInfo.h @@ -217,6 +217,14 @@ struct ColorAttachmentTextureCreateInfo:public AttachmentTextureCreateInfo format=fmt; extent=ext; } + + ColorAttachmentTextureCreateInfo(const VkFormat fmt,const VkExtent2D &ext):ColorAttachmentTextureCreateInfo() + { + format=fmt; + extent.width=ext.width; + extent.height=ext.height; + extent.depth=1; + } }; struct DepthAttachmentTextureCreateInfo:public AttachmentTextureCreateInfo @@ -236,6 +244,14 @@ struct DepthAttachmentTextureCreateInfo:public AttachmentTextureCreateInfo format=fmt; extent=ext; } + + DepthAttachmentTextureCreateInfo(const VkFormat fmt,const VkExtent2D &ext):DepthAttachmentTextureCreateInfo() + { + format=fmt; + extent.width=ext.width; + extent.height=ext.height; + extent.depth=1; + } }; struct DepthStencilAttachmentTextureCreateInfo:public AttachmentTextureCreateInfo @@ -255,6 +271,14 @@ struct DepthStencilAttachmentTextureCreateInfo:public AttachmentTextureCreateInf format=fmt; extent=ext; } + + DepthStencilAttachmentTextureCreateInfo(const VkFormat fmt,const VkExtent2D &ext):DepthStencilAttachmentTextureCreateInfo() + { + format=fmt; + extent.width=ext.width; + extent.height=ext.height; + extent.depth=1; + } }; struct SwapchainColorTextureCreateInfo:public TextureCreateInfo diff --git a/src/RenderDevice/VKCommandBuffer.cpp b/src/RenderDevice/VKCommandBuffer.cpp index 860f6c2f..c5cdb96f 100644 --- a/src/RenderDevice/VKCommandBuffer.cpp +++ b/src/RenderDevice/VKCommandBuffer.cpp @@ -10,7 +10,7 @@ #include VK_NAMESPACE_BEGIN -GPUCmdBuffer::GPUCmdBuffer(VkDevice dev,const VkExtent2D &extent,const uint32_t atta_count,VkCommandPool cp,VkCommandBuffer cb) +GPUCmdBuffer::GPUCmdBuffer(VkDevice dev,const uint32_t atta_count,VkCommandPool cp,VkCommandBuffer cb) { device=dev; pool=cp; @@ -32,7 +32,8 @@ GPUCmdBuffer::GPUCmdBuffer(VkDevice dev,const VkExtent2D &extent,const uint32_t render_area.offset.x=0; render_area.offset.y=0; - render_area.extent=extent; + render_area.extent.width=0; + render_area.extent.height=0; default_line_width=1.0; @@ -65,12 +66,16 @@ bool GPUCmdBuffer::Begin() return(true); } -bool GPUCmdBuffer::BindFramebuffer(VkRenderPass rp,VkFramebuffer fb) +bool GPUCmdBuffer::BindFramebuffer(RenderPass *rp,Framebuffer *fb) { RenderPassBeginInfo rp_begin; + + render_area.offset.x=0; + render_area.offset.y=0; + render_area.extent=fb->GetExtent(); - rp_begin.renderPass = rp; - rp_begin.framebuffer = fb; + rp_begin.renderPass = *rp; + rp_begin.framebuffer = *fb; rp_begin.renderArea = render_area; rp_begin.clearValueCount = cv_count; rp_begin.pClearValues = clear_values; @@ -93,18 +98,6 @@ bool GPUCmdBuffer::BindFramebuffer(VkRenderPass rp,VkFramebuffer fb) return(true); } -//bool GPUCmdBuffer::BindFramebuffer(Framebuffer *fbo) -//{ -// return BindFramebuffer(fbo->GetRenderPass(),fbo->GetFramebuffer()); -//} - -bool GPUCmdBuffer::BindFramebuffer(RenderTarget *rt) -{ - if(!rt)return(false); - - return BindFramebuffer(rt->GetRenderPass(),rt->GetFramebuffer()); -} - bool GPUCmdBuffer::BindVAB(RenderableInstance *ri) { if(!ri) diff --git a/src/RenderDevice/VKDevice.cpp b/src/RenderDevice/VKDevice.cpp index 939b394e..55b75b39 100644 --- a/src/RenderDevice/VKDevice.cpp +++ b/src/RenderDevice/VKDevice.cpp @@ -47,7 +47,7 @@ bool GPUDevice::Resize(const VkExtent2D &extent) if(!CreateSwapchain(extent)) return(false); - texture_cmd_buf=CreateCommandBuffer(extent,0); + texture_cmd_buf=CreateCommandBuffer(0); textureSQ=new GPUQueue(this,attr->graphics_queue,1); swapchainRT=new SwapchainRenderTarget(this,swapchain); @@ -55,7 +55,7 @@ bool GPUDevice::Resize(const VkExtent2D &extent) return(true); } -GPUCmdBuffer *GPUDevice::CreateCommandBuffer(const VkExtent2D &extent,const uint32_t atta_count) +GPUCmdBuffer *GPUDevice::CreateCommandBuffer(const uint32_t attachment_count) { if(!attr->cmd_pool) return(nullptr); @@ -73,7 +73,7 @@ GPUCmdBuffer *GPUDevice::CreateCommandBuffer(const VkExtent2D &extent,const uint if(res!=VK_SUCCESS) return(nullptr); - return(new GPUCmdBuffer(attr->device,extent,atta_count,attr->cmd_pool,cmd_buf)); + return(new GPUCmdBuffer(attr->device,attachment_count,attr->cmd_pool,cmd_buf)); } /** diff --git a/src/RenderDevice/VKDeviceRenderPass.cpp b/src/RenderDevice/VKDeviceRenderPass.cpp index 2d88969a..1d55e3b6 100644 --- a/src/RenderDevice/VKDeviceRenderPass.cpp +++ b/src/RenderDevice/VKDeviceRenderPass.cpp @@ -80,11 +80,11 @@ void CreateAttachmentReference(VkAttachmentReference *ref_list,uint start,uint c } } -bool CreateAttachmentDescription(List &desc_list,const List &color_format,const VkFormat depth_format,VkImageLayout color_final_layout,VkImageLayout depth_final_layout) +bool CreateAttachmentDescription(List &desc_list,const RenderbufferInfo *rbi) { - const uint color_count=color_format.GetCount(); + const uint color_count=rbi->GetColorCount(); - const uint count=(depth_format!=FMT_UNDEFINED)?color_count+1:color_count; + const uint count=(rbi->HasDepthOrStencil())?color_count+1:color_count; desc_list.SetCount(count); @@ -104,21 +104,21 @@ bool CreateAttachmentDescription(List &desc_list,const } desc=desc_list.GetData(); - const VkFormat *cf=color_format.GetData(); + const VkFormat *cf=rbi->GetColorFormat(); for(uint i=0;ifinalLayout = color_final_layout; + desc->finalLayout = rbi->GetColorLayout(); desc->format = *cf; ++desc; ++cf; } - if(depth_format!=FMT_UNDEFINED) + if(rbi->GetDepthFormat()!=FMT_UNDEFINED) { - desc->finalLayout = depth_final_layout; - desc->format = depth_format; - desc->storeOp = VK_ATTACHMENT_STORE_OP_STORE; //swapchainRT最后"可能"是不需要保存深度的,,,,想想怎么最后弄成"DONT CARE" + desc->finalLayout = rbi->GetDepthLayout(); + desc->format = rbi->GetDepthFormat(); + desc->storeOp = rbi->IsSwapchain()?VK_ATTACHMENT_STORE_OP_DONT_CARE:VK_ATTACHMENT_STORE_OP_STORE; } return(true); @@ -192,22 +192,21 @@ bool CreateDepthAttachment( List &ref_list,List &desc_list, +RenderPass *GPUDevice::CreateRenderPass(const List &desc_list, const List &subpass, const List &dependency, - const List &color_format_list, - const VkFormat depth_format, - const VkImageLayout color_final_layout, - const VkImageLayout depth_final_layout) + const RenderbufferInfo *rbi) { - for(const VkFormat cf:color_format_list) + for(const VkFormat cf:rbi->GetColorFormatList()) { if(!attr->physical_device->IsColorAttachmentOptimal(cf) &&!attr->physical_device->IsColorAttachmentLinear(cf)) return(nullptr); } - if(depth_format!=FMT_UNDEFINED) + const VkFormat depth_format=rbi->GetDepthFormat(); + + if(rbi->HasDepthOrStencil()) { if(!attr->physical_device->IsDepthAttachmentOptimal(depth_format) &&!attr->physical_device->IsDepthAttachmentLinear(depth_format)) @@ -230,12 +229,12 @@ RenderPass *GPUDevice::CreateRenderPass( const List & if(vkCreateRenderPass(attr->device,&rp_info,nullptr,&render_pass)!=VK_SUCCESS) return(nullptr); - return(new RenderPass(attr->device,render_pass,color_format_list,depth_format)); + return(new RenderPass(attr->device,render_pass,rbi->GetColorFormatList(),depth_format)); } -RenderPass *GPUDevice::CreateRenderPass(const List &color_format_list,VkFormat depth_format,VkImageLayout color_final_layout,VkImageLayout depth_final_layout) +RenderPass *GPUDevice::CreateRenderPass(const RenderbufferInfo *rbi) { - for(const VkFormat &fmt:color_format_list) + for(const VkFormat &fmt:rbi->GetColorFormatList()) if(!attr->physical_device->IsColorAttachmentOptimal(fmt)) return(nullptr); @@ -245,17 +244,17 @@ RenderPass *GPUDevice::CreateRenderPass(const List &color_format_list, List subpass_desc_list; List subpass_dependency_list; - color_ref_list.SetCount(color_format_list.GetCount()); - CreateColorAttachmentReference(color_ref_list.GetData(),0,color_format_list.GetCount()); + color_ref_list.SetCount(rbi->GetColorCount()); + CreateColorAttachmentReference(color_ref_list.GetData(),0,rbi->GetColorCount()); - CreateAttachmentDescription(atta_desc_list,color_format_list,depth_format,color_final_layout,depth_final_layout); + CreateAttachmentDescription(atta_desc_list,rbi); - if(depth_format!=FMT_UNDEFINED) + if(rbi->HasDepthOrStencil()) { - if(!attr->physical_device->IsDepthAttachmentOptimal(depth_format)) + if(!attr->physical_device->IsDepthAttachmentOptimal(rbi->GetDepthFormat())) return(nullptr); - CreateDepthAttachmentReference(&depth_ref,color_format_list.GetCount()); + CreateDepthAttachmentReference(&depth_ref,rbi->GetColorCount()); subpass_desc_list.Add(SubpassDescription(color_ref_list.GetData(),color_ref_list.GetCount(),&depth_ref)); } else @@ -265,15 +264,6 @@ RenderPass *GPUDevice::CreateRenderPass(const List &color_format_list, CreateSubpassDependency(subpass_dependency_list,2); - return CreateRenderPass(atta_desc_list,subpass_desc_list,subpass_dependency_list,color_format_list,depth_format,color_final_layout,depth_final_layout); -} - -RenderPass *GPUDevice::CreateRenderPass(VkFormat color_format,VkFormat depth_format,VkImageLayout color_final_layout,VkImageLayout depth_final_layout) -{ - List color_format_list; - - color_format_list.Add(color_format); - - return CreateRenderPass(color_format_list,depth_format,color_final_layout,depth_final_layout); + return CreateRenderPass(atta_desc_list,subpass_desc_list,subpass_dependency_list,rbi); } VK_NAMESPACE_END diff --git a/src/RenderDevice/VKDeviceRenderTarget.cpp b/src/RenderDevice/VKDeviceRenderTarget.cpp index 575397a5..9267ba5c 100644 --- a/src/RenderDevice/VKDeviceRenderTarget.cpp +++ b/src/RenderDevice/VKDeviceRenderTarget.cpp @@ -3,27 +3,20 @@ VK_NAMESPACE_BEGIN RenderTarget *GPUDevice::CreateRenderTarget(Framebuffer *fb,const uint32_t fence_count) { - GPUCmdBuffer *cb=CreateCommandBuffer(fb->GetExtent(),fb->GetAttachmentCount()); - - return(new RenderTarget(this,fb,cb,fence_count)); + return(new RenderTarget(this,fb,fence_count)); } -RenderTarget *GPUDevice::CreateRenderTarget(const uint w,const uint h, - const List &color_format_list, - const VkFormat depth_format, - const VkImageLayout color_layout, - const VkImageLayout depth_layout, - const uint32_t fence_count) +RenderTarget *GPUDevice::CreateRenderTarget(const FramebufferInfo *fbi,const uint32_t fence_count) { - if(w<=0||h<=0)return(nullptr); + if(!fbi)return(nullptr); - RenderPass *rp=CreateRenderPass(color_format_list,depth_format,color_layout,depth_layout); //Renderpass内部会验证格式,所以不需要自己处理 + RenderPass *rp=CreateRenderPass(fbi); //Renderpass内部会验证格式,所以不需要自己处理 if(!rp)return(nullptr); - const uint32_t color_count=color_format_list.GetCount(); - - const VkExtent3D extent={w,h,1}; + const uint32_t color_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 @@ -31,7 +24,7 @@ RenderTarget *GPUDevice::CreateRenderTarget(const uint w,const uint h, Texture2D **tp=color_texture_list; ImageView **iv=color_iv_list; - for(const VkFormat &fmt:color_format_list) + for(const VkFormat &fmt:fbi->GetColorFormatList()) { Texture2D *color_texture=CreateTexture2D(new ColorAttachmentTextureCreateInfo(fmt,extent)); @@ -51,11 +44,11 @@ RenderTarget *GPUDevice::CreateRenderTarget(const uint w,const uint h, if(fb) { - GPUCmdBuffer *cb=CreateCommandBuffer(fb->GetExtent(),fb->GetAttachmentCount()); + GPUCmdBuffer *cb=CreateCommandBuffer(fb->GetAttachmentCount()); if(cb) { - RenderTarget *rt=new RenderTarget(this,rp,fb,cb,color_texture_list,color_count,depth_texture,fence_count); + RenderTarget *rt=new RenderTarget(this,rp,fb,color_texture_list,color_count,depth_texture,fence_count); color_texture_list.DiscardObject(); return rt; @@ -66,40 +59,4 @@ RenderTarget *GPUDevice::CreateRenderTarget(const uint w,const uint h, delete rp; return nullptr; } - -RenderTarget *GPUDevice::CreateRenderTarget(const uint w,const uint h, - const VkFormat color_format, - const VkFormat depth_format, - const VkImageLayout color_layout, - const VkImageLayout depth_layout, - const uint32_t fence_count) -{ - if(w<=0||h<=0)return(nullptr); - - List color_format_list; - - color_format_list.Add(color_format); - - return CreateRenderTarget(w,h,color_format_list,depth_format,color_layout,depth_layout,fence_count); -} - -RenderTarget *GPUDevice::CreateRenderTarget(const uint w,const uint h,const VkFormat format,const VkImageLayout final_layout,const uint32_t fence_count) -{ - if(w<=0||h<=0)return(nullptr); - - if(format==FMT_UNDEFINED)return(nullptr); - - List color_format_list; - - if(IsDepthFormat(format)) - { - return CreateRenderTarget(w,h,color_format_list,format,VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,final_layout,fence_count); - } - else - { - color_format_list.Add(format); - - return CreateRenderTarget(w,h,color_format_list,FMT_UNDEFINED,final_layout,VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,fence_count); - } -} VK_NAMESPACE_END \ No newline at end of file diff --git a/src/RenderDevice/VKPipeline.cpp b/src/RenderDevice/VKPipeline.cpp index a72fa376..45e86729 100644 --- a/src/RenderDevice/VKPipeline.cpp +++ b/src/RenderDevice/VKPipeline.cpp @@ -28,7 +28,7 @@ Pipeline *CreatePipeline(VkDevice device,VkPipelineCache pipeline_cache,Pipeline data->pipeline_info.layout = material->GetPipelineLayout(); { - data->pipeline_info.renderPass = rt->GetRenderPass(); + data->pipeline_info.renderPass = rt->GetVkRenderPass(); data->pipeline_info.subpass = 0; //subpass由于还不知道有什么用,所以暂时写0,待知道功用后,需改进 } diff --git a/src/RenderDevice/VKRenderTarget.cpp b/src/RenderDevice/VKRenderTarget.cpp index f80d6054..db081c70 100644 --- a/src/RenderDevice/VKRenderTarget.cpp +++ b/src/RenderDevice/VKRenderTarget.cpp @@ -3,13 +3,13 @@ #include #include #include +#include VK_NAMESPACE_BEGIN -RenderTarget::RenderTarget(GPUDevice *dev,Framebuffer *_fb,GPUCmdBuffer *_cb,const uint32_t fence_count):GPUQueue(dev,dev->GetGraphicsQueue(),fence_count) +RenderTarget::RenderTarget(GPUDevice *dev,Framebuffer *_fb,const uint32_t fence_count):GPUQueue(dev,dev->GetGraphicsQueue(),fence_count) { render_pass=nullptr; fbo=_fb; - command_buffer=_cb; if(fbo) color_count=fbo->GetColorCount(); @@ -21,11 +21,10 @@ RenderTarget::RenderTarget(GPUDevice *dev,Framebuffer *_fb,GPUCmdBuffer *_cb,con render_complete_semaphore=dev->CreateSemaphore(); } -RenderTarget::RenderTarget(GPUDevice *dev,RenderPass *_rp,Framebuffer *_fb,GPUCmdBuffer *_cb,Texture2D **ctl,const uint32_t cc,Texture2D *dt,const uint32_t fence_count):GPUQueue(dev,dev->GetGraphicsQueue(),fence_count) +RenderTarget::RenderTarget(GPUDevice *dev,RenderPass *_rp,Framebuffer *_fb,Texture2D **ctl,const uint32_t cc,Texture2D *dt,const uint32_t fence_count):GPUQueue(dev,dev->GetGraphicsQueue(),fence_count) { render_pass=_rp; fbo=_fb; - command_buffer=_cb; depth_texture=dt; @@ -59,12 +58,11 @@ RenderTarget::~RenderTarget() SAFE_CLEAR_OBJECT_ARRAY(color_textures,color_count); SAFE_CLEAR(render_complete_semaphore); - SAFE_CLEAR(command_buffer); SAFE_CLEAR(fbo); SAFE_CLEAR(render_pass); } -bool RenderTarget::Submit(GPUSemaphore *present_complete_semaphore) +bool RenderTarget::Submit(GPUCmdBuffer *command_buffer,GPUSemaphore *present_complete_semaphore) { return this->GPUQueue::Submit(*command_buffer,present_complete_semaphore,render_complete_semaphore); } diff --git a/src/RenderDevice/VKSwapchainRenderTarget.cpp b/src/RenderDevice/VKSwapchainRenderTarget.cpp index 25512fae..624d0f6a 100644 --- a/src/RenderDevice/VKSwapchainRenderTarget.cpp +++ b/src/RenderDevice/VKSwapchainRenderTarget.cpp @@ -3,7 +3,7 @@ #include VK_NAMESPACE_BEGIN -SwapchainRenderTarget::SwapchainRenderTarget(GPUDevice *dev,Swapchain *sc):RenderTarget(dev,nullptr,nullptr,sc->GetImageCount()) +SwapchainRenderTarget::SwapchainRenderTarget(GPUDevice *dev,Swapchain *sc):RenderTarget(dev,nullptr,sc->GetImageCount()) { swapchain=sc; vk_swapchain=swapchain->GetSwapchain(); @@ -17,7 +17,9 @@ SwapchainRenderTarget::SwapchainRenderTarget(GPUDevice *dev,Swapchain *sc):Rende Texture2D **sc_color=swapchain->GetColorTextures(); Texture2D *sc_depth=swapchain->GetDepthTexture(); - this->render_pass=device->CreateRenderPass((*sc_color)->GetFormat(),sc_depth->GetFormat(),VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); + SwapchainRenderbufferInfo rbi((*sc_color)->GetFormat(),sc_depth->GetFormat()); + + this->render_pass=device->CreateRenderPass(&rbi); swap_chain_count=swapchain->GetImageCount();