From 5972d075b3b2b98800411987645d8022a25d1f90 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Mon, 19 Oct 2020 22:26:42 +0800 Subject: [PATCH] update examples codes. --- CMSceneGraph | 2 +- example/2dVector/line.cpp | 4 +- example/Vulkan/CubeNormal.cpp | 0 example/Vulkan/DeferredModel.cpp | 224 +++---------------------- inc/hgl/graph/vulkan/VKCommandBuffer.h | 53 +----- inc/hgl/gui/Form.h | 2 - src/GUI/DefaultThemeEngine.h | 14 +- 7 files changed, 46 insertions(+), 253 deletions(-) create mode 100644 example/Vulkan/CubeNormal.cpp diff --git a/CMSceneGraph b/CMSceneGraph index 443e5d30..249c47d2 160000 --- a/CMSceneGraph +++ b/CMSceneGraph @@ -1 +1 @@ -Subproject commit 443e5d302bb1ef0b8fb2e0da6b95ff784e568354 +Subproject commit 249c47d242c2e8dc4994581720984e55c0cc0ebe diff --git a/example/2dVector/line.cpp b/example/2dVector/line.cpp index f5d02669..b9717cb4 100644 --- a/example/2dVector/line.cpp +++ b/example/2dVector/line.cpp @@ -23,8 +23,8 @@ static Vector4f color(1,1,1,1); struct Line2DConfig { - float width=2.0f; - float border=0.75f; + float width=20.0f; + float border=5.0f; }; static Line2DConfig line_2d_config; diff --git a/example/Vulkan/CubeNormal.cpp b/example/Vulkan/CubeNormal.cpp new file mode 100644 index 00000000..e69de29b diff --git a/example/Vulkan/DeferredModel.cpp b/example/Vulkan/DeferredModel.cpp index 22da1587..bfc9cedc 100644 --- a/example/Vulkan/DeferredModel.cpp +++ b/example/Vulkan/DeferredModel.cpp @@ -19,24 +19,11 @@ VK_NAMESPACE_BEGIN Texture2D *CreateTextureFromFile(Device *device,const OSString &filename); VK_NAMESPACE_END -constexpr uint32_t SCREEN_WIDTH=256; -constexpr uint32_t SCREEN_HEIGHT=256; +constexpr uint32_t SCREEN_WIDTH=512; +constexpr uint32_t SCREEN_HEIGHT=512; using Texture2DPointer=vulkan::Texture2D *; -constexpr VkFormat position_candidate_format[]={FMT_RGBA32F,FMT_RGBA16F}; -constexpr VkFormat color_candidate_format []={FMT_RGBA32F, - FMT_RGBA16F, - FMT_RGBA8UN,FMT_RGBA8SN,FMT_RGBA8U, - FMT_BGRA8UN,FMT_BGRA8SN,FMT_BGRA8U, - FMT_ABGR8UN,FMT_ABGR8SN,FMT_ABGR8U, - FMT_RGB565,FMT_BGR565}; -constexpr VkFormat normal_candidate_format []={FMT_RGBA32F, - FMT_RGBA16F, - FMT_A2RGB10UN,FMT_A2RGB10SN,FMT_A2BGR10UN, - FMT_A2BGR10SN}; -constexpr VkFormat depth_candidate_format []={FMT_D32F,FMT_D32F_S8U,FMT_X8_D24UN,FMT_D24UN_S8U,FMT_D16UN,FMT_D16UN_S8U}; - class TestApp:public CameraAppFramework { private: @@ -44,42 +31,7 @@ private: SceneNode render_root; RenderList render_list; - struct DeferredGBuffer - { - vulkan::Semaphore *render_complete_semaphore =nullptr; - - vulkan::RenderTarget *rt; - - VkExtent2D extent; - vulkan::Framebuffer *framebuffer; - vulkan::RenderPass *renderpass; - - union - { - struct - { - Texture2DPointer position,normal,color,depth; - }; - - Texture2DPointer texture_list[4]; - }; - - List gbuffer_format_list; - List image_view_list; - - struct - { - List desc_list; - VkAttachmentReference *color_ref_list; - VkAttachmentReference depth_ref; - }attachment; - - struct - { - List desc; - List dependency; - }subpass; - }gbuffer;// + vulkan::RenderTarget *gbuffer_rt; struct SubpassParam { @@ -120,104 +72,18 @@ public: SAFE_CLEAR(texture.color); SAFE_CLEAR(sampler); } + private: - const VkFormat GetCandidateFormat(const VkFormat *fmt_list,const uint count) - { - auto pd=device->GetPhysicalDevice(); - - for(uint i=0;iIsColorAttachmentOptimal(fmt_list[i])) - return fmt_list[i]; - - for(uint i=0;iIsColorAttachmentLinear(fmt_list[i])) - return fmt_list[i]; - - return FMT_UNDEFINED; - } - - const VkFormat GetDepthCandidateFormat() - { - auto pd=device->GetPhysicalDevice(); - - for(VkFormat fmt:depth_candidate_format) - if(pd->IsDepthAttachmentOptimal(fmt)) - return fmt; - - for(VkFormat fmt:depth_candidate_format) - if(pd->IsDepthAttachmentLinear(fmt)) - return fmt; - - return FMT_UNDEFINED; - } - bool InitGBuffer() { - gbuffer.extent.width =1024; - gbuffer.extent.height =1024; + List gbuffer_color_format; - gbuffer.render_complete_semaphore =device->CreateSem(); + gbuffer_color_format.Add(UFMT_RGBA32F); //position + gbuffer_color_format.Add(UFMT_RGBA32F); //color + gbuffer_color_format.Add(UFMT_RGBA32F); //normal - //根据候选格式表选择格式 - //const VkFormat position_format =GetCandidateFormat(position_candidate_format, sizeof(position_candidate_format)); - //const VkFormat color_format =GetCandidateFormat(color_candidate_format, sizeof(color_candidate_format)); - //const VkFormat normal_format =GetCandidateFormat(normal_candidate_format, sizeof(normal_candidate_format)); - //const VkFormat depth_format =GetDepthCandidateFormat(); - - //if(position_format ==FMT_UNDEFINED - // ||color_format ==FMT_UNDEFINED - // ||normal_format ==FMT_UNDEFINED - // ||depth_format ==FMT_UNDEFINED) - // return(false); - - const VkFormat position_format =FMT_RGBA32F; - const VkFormat color_format =FMT_RGBA32F; - const VkFormat normal_format =FMT_RGBA32F; - const VkFormat depth_format =FMT_D32F; - - gbuffer.position=device->CreateAttachmentTextureColor(position_format, gbuffer.extent.width,gbuffer.extent.height); - gbuffer.color =device->CreateAttachmentTextureColor(color_format, gbuffer.extent.width,gbuffer.extent.height); - gbuffer.normal =device->CreateAttachmentTextureColor(normal_format, gbuffer.extent.width,gbuffer.extent.height); - gbuffer.depth =device->CreateAttachmentTextureDepth(depth_format, gbuffer.extent.width,gbuffer.extent.height); - - for(uint i=0;i<3;i++) - { - gbuffer.gbuffer_format_list.Add(gbuffer.texture_list[i]->GetFormat()); - gbuffer.image_view_list.Add(gbuffer.texture_list[i]->GetImageView()); - } - - gbuffer.attachment.color_ref_list=new VkAttachmentReference[3]; - - vulkan::CreateColorAttachmentReference(gbuffer.attachment.color_ref_list,3); - vulkan::CreateDepthAttachmentReference(&gbuffer.attachment.depth_ref); - - if(!vulkan::CreateAttachmentDescription( gbuffer.attachment.desc_list, - gbuffer.gbuffer_format_list, - gbuffer.depth->GetFormat())) - return(false); - - vulkan::SubpassDescription desc(gbuffer.attachment.color_ref_list,3,&gbuffer.attachment.depth_ref); - - gbuffer.subpass.desc.Add(desc); - - vulkan::CreateSubpassDependency(gbuffer.subpass.dependency,2); //为啥要2个还不清楚 - - gbuffer.renderpass=device->CreateRenderPass(gbuffer.attachment.desc_list, - gbuffer.subpass.desc, - gbuffer.subpass.dependency, - gbuffer.gbuffer_format_list, - gbuffer.depth->GetFormat()); - - if(!gbuffer.renderpass) - return(false); - - gbuffer.framebuffer=vulkan::CreateFramebuffer(device->GetDevice(),gbuffer.renderpass,gbuffer.image_view_list,gbuffer.depth->GetImageView()); - - if(!gbuffer.framebuffer) - return(false); - - gbuffer.rt=device->CreateRenderTarget(gbuffer.framebuffer); + gbuffer_rt=device->CreateRenderTarget(SCREEN_WIDTH,SCREEN_HEIGHT,gbuffer_color_format,FMT_D32F); return(true); } @@ -235,47 +101,20 @@ private: bool InitGBufferPipeline(SubpassParam *sp) { - AutoDelete pipeline_creater=new vulkan::PipelineCreater(device,sp->material,gbuffer.rt); + sp->pipeline_triangles =db->CreatePipeline(sp->material,gbuffer_rt,vulkan::InlinePipeline::Solid3D,Prim::Triangles); + if(!sp->pipeline_triangles) + return(false); - { - pipeline_creater->Set(PRIM_TRIANGLES); + sp->pipeline_fan =db->CreatePipeline(sp->material,gbuffer_rt,vulkan::InlinePipeline::Solid3D,Prim::Fan); - sp->pipeline_triangles=pipeline_creater->Create(); - - if(!sp->pipeline_triangles) - return(false); - - db->Add(sp->pipeline_triangles); - } - - { - pipeline_creater->Set(PRIM_TRIANGLE_FAN); - - sp->pipeline_fan=pipeline_creater->Create(); - - if(!sp->pipeline_fan) - return(false); - - db->Add(sp->pipeline_fan); - } - return(true); + return sp->pipeline_fan; } bool InitCompositionPipeline(SubpassParam *sp) { - AutoDelete pipeline_creater=new vulkan::PipelineCreater(device,sp->material,sc_render_target); - pipeline_creater->SetDepthTest(false); - pipeline_creater->SetDepthWrite(false); - pipeline_creater->SetCullMode(VK_CULL_MODE_NONE); - pipeline_creater->Set(PRIM_TRIANGLE_FAN); + sp->pipeline_fan=db->CreatePipeline(sp->material,gbuffer_rt,vulkan::InlinePipeline::Solid2D,Prim::Fan); - sp->pipeline_triangles=pipeline_creater->Create(); - - if(!sp->pipeline_triangles) - return(false); - - db->Add(sp->pipeline_triangles); - return(true); + return sp->pipeline_fan; } bool InitMaterial() @@ -289,28 +128,7 @@ private: texture.color =vulkan::CreateTextureFromFile(device,OS_TEXT("res/image/Brickwall/Albedo.Tex2D")); texture.normal =vulkan::CreateTextureFromFile(device,OS_TEXT("res/image/Brickwall/Normal.Tex2D")); - VkSamplerCreateInfo sampler_create_info; - - sampler_create_info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; - sampler_create_info.pNext = nullptr; - sampler_create_info.flags = 0; - sampler_create_info.magFilter = VK_FILTER_LINEAR; - sampler_create_info.minFilter = VK_FILTER_LINEAR; - sampler_create_info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR; - sampler_create_info.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; - sampler_create_info.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; - sampler_create_info.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; - sampler_create_info.mipLodBias = 0.0f; - sampler_create_info.anisotropyEnable = false; - sampler_create_info.maxAnisotropy = 0; - sampler_create_info.compareEnable = false; - sampler_create_info.compareOp = VK_COMPARE_OP_NEVER; - sampler_create_info.minLod = 0.0f; - sampler_create_info.maxLod = 1.0f; - sampler_create_info.borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK; - sampler_create_info.unnormalizedCoordinates = false; - - sampler=device->CreateSampler(&sampler_create_info); + sampler=device->CreateSampler(); InitCameraUBO(sp_gbuffer.material_instance,"world"); @@ -318,9 +136,9 @@ private: sp_gbuffer.material_instance->BindSampler("TextureNormal" ,texture.normal, sampler); sp_gbuffer.material_instance->Update(); - sp_composition.material_instance->BindSampler("GB_Position" ,gbuffer.position, sampler); - sp_composition.material_instance->BindSampler("GB_Normal" ,gbuffer.normal, sampler); - sp_composition.material_instance->BindSampler("GB_Color" ,gbuffer.color, sampler); + sp_composition.material_instance->BindSampler("GB_Position" ,gbuffer_rt->GetColorTexture(0),sampler); + sp_composition.material_instance->BindSampler("GB_Normal" ,gbuffer_rt->GetColorTexture(1),sampler); + sp_composition.material_instance->BindSampler("GB_Color" ,gbuffer_rt->GetColorTexture(2),sampler); sp_composition.material_instance->Update(); return(true); @@ -459,7 +277,7 @@ public: void BuildCommandBuffer(uint32_t index) override { VulkanApplicationFramework::BuildCommandBuffer( index, - sp_composition.pipeline_triangles, + sp_composition.pipeline_fan, sp_composition.material_instance, ro_gbc_plane); } diff --git a/inc/hgl/graph/vulkan/VKCommandBuffer.h b/inc/hgl/graph/vulkan/VKCommandBuffer.h index c0c77c4b..f3a1415f 100644 --- a/inc/hgl/graph/vulkan/VKCommandBuffer.h +++ b/inc/hgl/graph/vulkan/VKCommandBuffer.h @@ -61,39 +61,9 @@ public: bool Begin(); - void PipelineBarrier( - VkPipelineStageFlags srcStageMask, - VkPipelineStageFlags dstStageMask, - VkDependencyFlags dependencyFlags, - uint32_t memoryBarrierCount, - const VkMemoryBarrier * pMemoryBarriers, - uint32_t bufferMemoryBarrierCount, - const VkBufferMemoryBarrier * pBufferMemoryBarriers, - uint32_t imageMemoryBarrierCount, - const VkImageMemoryBarrier * pImageMemoryBarriers) - { - vkCmdPipelineBarrier(cmd_buf,srcStageMask,dstStageMask,dependencyFlags,memoryBarrierCount,pMemoryBarriers,bufferMemoryBarrierCount,pBufferMemoryBarriers,imageMemoryBarrierCount,pImageMemoryBarriers); - } - - void CopyBufferToImage( - VkBuffer srcBuffer, - VkImage dstImage, - VkImageLayout dstImageLayout, - uint32_t regionCount, - const VkBufferImageCopy * pRegions) - { - vkCmdCopyBufferToImage(cmd_buf,srcBuffer,dstImage,dstImageLayout,regionCount,pRegions); - } - - void CopyImageToBuffer( - VkImage srcImage, - VkImageLayout srcImageLayout, - VkBuffer dstBuffer, - uint32_t regionCount, - const VkBufferImageCopy * pRegions) - { - vkCmdCopyImageToBuffer(cmd_buf,srcImage,srcImageLayout,dstBuffer,regionCount,pRegions); - } + template void PipelineBarrier (ARGS...args){vkCmdPipelineBarrier (cmd_buf,args...);} + template void CopyBufferToImage (ARGS...args){vkCmdCopyBufferToImage(cmd_buf,args...);} + template void CopyImageToBuffer (ARGS...args){vkCmdCopyImageToBuffer(cmd_buf,args...);} bool BindFramebuffer(VkRenderPass rp,VkFramebuffer fb); bool BindFramebuffer(Framebuffer *); @@ -140,18 +110,13 @@ public: void SetStencilWriteMask (VkStencilFaceFlags faceMask,uint32_t compareMask) {vkCmdSetStencilWriteMask(cmd_buf,faceMask,compareMask);} void SetStencilReference (VkStencilFaceFlags faceMask,uint32_t compareMask) {vkCmdSetStencilReference(cmd_buf,faceMask,compareMask);} - void Draw (const uint32_t vertex_count){vkCmdDraw(cmd_buf,vertex_count,1,0,0);} - void DrawIndexed(const uint32_t index_count ){vkCmdDrawIndexed(cmd_buf,index_count,1,0,0,0);} - - void Draw (const uint32_t vertex_count, const uint32_t instance_count,const uint32_t first_vertex, const uint32_t first_instance) - { - vkCmdDraw(cmd_buf,vertex_count,instance_count,first_vertex,first_instance); - } +public: //draw - void DrawIndexed(const uint32_t index_count, const uint32_t instance_count,const uint32_t first_index, const uint32_t vertex_offset,const uint32_t first_instance) - { - vkCmdDrawIndexed(cmd_buf,index_count,instance_count,first_index,vertex_offset,first_instance); - } + void Draw (const uint32_t vertex_count) {vkCmdDraw(cmd_buf,vertex_count,1,0,0);} + void DrawIndexed(const uint32_t index_count ) {vkCmdDrawIndexed(cmd_buf,index_count,1,0,0,0);} + + template void Draw (ARGS...args) {vkCmdDraw(cmd_buf,args...);} + template void DrawIndexed(ARGS...args) {vkCmdDrawIndexed(cmd_buf,args...);} void NextSubpass(){vkCmdNextSubpass(cmd_buf,VK_SUBPASS_CONTENTS_INLINE);} diff --git a/inc/hgl/gui/Form.h b/inc/hgl/gui/Form.h index bd522944..2b3a0461 100644 --- a/inc/hgl/gui/Form.h +++ b/inc/hgl/gui/Form.h @@ -15,8 +15,6 @@ namespace hgl { protected: //每个窗体独立一个FBO存在,所以每个窗体会有自己的RenderTarget与pipeline - vulkan::Buffer *ui_matrix; - struct { vulkan::Pipeline *solid; diff --git a/src/GUI/DefaultThemeEngine.h b/src/GUI/DefaultThemeEngine.h index e82efca8..d75e8e15 100644 --- a/src/GUI/DefaultThemeEngine.h +++ b/src/GUI/DefaultThemeEngine.h @@ -2,6 +2,7 @@ #include #include +#include namespace hgl { @@ -9,11 +10,20 @@ namespace hgl { using namespace hgl::graph; + class Form; ///<窗体 + /** * 缺省GUI主题引擎 */ class DefaultThemeEngine:public ThemeEngine { + struct IForm + { + Form *form; ///<窗体控件 + vulkan::RenderTarget *rt; ///<渲染目标 + };// + + MapObject
form_list; struct { @@ -26,7 +36,9 @@ namespace hgl bool Init() override; void Clear() override; - void DrawFrame(const Widget *) override; + bool Registry(Form *); + void Unregistry(Form *); + void Render(Form *); };//class DefaultThemeEngine:public ThemeEngine }//namespace gui }//namespace hgl \ No newline at end of file