From 3b86783c53f18174b2646dddcf58c00a79693237 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Wed, 26 Jun 2019 16:03:21 +0800 Subject: [PATCH] =?UTF-8?q?Swapchain=E5=88=9B=E5=BB=BAdepth=E6=94=B9?= =?UTF-8?q?=E7=94=A8Texture2D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/hgl/graph/vulkan/VKDevice.h | 4 +- inc/hgl/graph/vulkan/VKDeviceAttribute.h | 9 +- inc/hgl/graph/vulkan/VKTexture.h | 6 +- src/RenderDevice/Vulkan/VKDevice.cpp | 5 +- src/RenderDevice/Vulkan/VKDeviceAttribute.cpp | 15 +-- src/RenderDevice/Vulkan/VKDeviceCreater.cpp | 98 ++++++++++--------- 6 files changed, 67 insertions(+), 70 deletions(-) diff --git a/inc/hgl/graph/vulkan/VKDevice.h b/inc/hgl/graph/vulkan/VKDevice.h index 4b8478e6..5cd969f5 100644 --- a/inc/hgl/graph/vulkan/VKDevice.h +++ b/inc/hgl/graph/vulkan/VKDevice.h @@ -67,8 +67,8 @@ public: public: const uint32_t GetSwapChainImageCount ()const {return attr->sc_image_views.GetCount();} - ImageView *GetColorImageView (int index) {return attr->sc_image_views[index];} - ImageView *GetDepthImageView () {return attr->depth.view;} + //ImageView *GetColorImageView (int index) {return attr->sc_image_views[index];} + //ImageView *GetDepthImageView () {return attr->depth.view;} RenderPass * GetRenderPass () {return main_rp;} Framebuffer * GetFramebuffer (int index) {return render_frame[index];} diff --git a/inc/hgl/graph/vulkan/VKDeviceAttribute.h b/inc/hgl/graph/vulkan/VKDeviceAttribute.h index f7f23d61..a8a8e885 100644 --- a/inc/hgl/graph/vulkan/VKDeviceAttribute.h +++ b/inc/hgl/graph/vulkan/VKDeviceAttribute.h @@ -39,14 +39,7 @@ struct DeviceAttribute List sc_images; ObjectList sc_image_views; - struct - { - VkFormat format; - - VkImage image =nullptr; - VkDeviceMemory mem =nullptr; - ImageView * view =nullptr; - }depth; + Texture2D * sc_depth =nullptr; VkDescriptorPool desc_pool =nullptr; diff --git a/inc/hgl/graph/vulkan/VKTexture.h b/inc/hgl/graph/vulkan/VKTexture.h index 5438e97d..d11af3be 100644 --- a/inc/hgl/graph/vulkan/VKTexture.h +++ b/inc/hgl/graph/vulkan/VKTexture.h @@ -2,6 +2,7 @@ #define HGL_GRAPH_VULKAN_TEXTURE_INCLUDE #include +#include #include VK_NAMESPACE_BEGIN struct TextureData @@ -30,11 +31,14 @@ public: operator TextureData * (){return data;} - operator Memory * (){return data?data->memory:nullptr;} + operator VkDeviceMemory (){return data?data->memory->operator VkDeviceMemory():nullptr;} operator VkImage (){return data?data->image:nullptr;} operator VkImageLayout (){return data?data->image_layout:VK_IMAGE_LAYOUT_UNDEFINED;} operator VkImageView (){return data?data->image_view->operator VkImageView():nullptr;} + operator Memory * (){return data?data->memory:nullptr;} + operator ImageView * (){return data?data->image_view:nullptr;} + const uint32 GetMipLevels()const{return data?data->mip_levels:0;} const bool IsLinear ()const{return data?data->linear:false;} diff --git a/src/RenderDevice/Vulkan/VKDevice.cpp b/src/RenderDevice/Vulkan/VKDevice.cpp index f61c1fa5..db0ce2e2 100644 --- a/src/RenderDevice/Vulkan/VKDevice.cpp +++ b/src/RenderDevice/Vulkan/VKDevice.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include //#include @@ -74,13 +75,13 @@ void Device::RecreateDevice() present_info.pSwapchains=&attr->swap_chain; - main_rp=CreateRenderPass(attr->sc_image_views[0]->GetFormat(),attr->depth.view->GetFormat()); + main_rp=CreateRenderPass(attr->sc_image_views[0]->GetFormat(),attr->sc_depth->GetFormat()); swap_chain_count=attr->sc_image_views.GetCount(); for(uint i=0;isc_image_views[i],attr->depth.view)); + render_frame.Add(vulkan::CreateFramebuffer(this,main_rp,attr->sc_image_views[i],*attr->sc_depth)); fence_list.Add(this->CreateFence(true)); } diff --git a/src/RenderDevice/Vulkan/VKDeviceAttribute.cpp b/src/RenderDevice/Vulkan/VKDeviceAttribute.cpp index 381e0b6c..2f5f9d7f 100644 --- a/src/RenderDevice/Vulkan/VKDeviceAttribute.cpp +++ b/src/RenderDevice/Vulkan/VKDeviceAttribute.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include VK_NAMESPACE_BEGIN @@ -41,19 +42,7 @@ bool DeviceAttribute::CheckMemoryType(uint32_t typeBits,VkMemoryPropertyFlags pr void DeviceAttribute::ClearSwapchain() { - SAFE_CLEAR(depth.view); - - if(depth.image) - { - vkDestroyImage(device,depth.image,nullptr); - depth.image=nullptr; - } - - if(depth.mem) - { - vkFreeMemory(device,depth.mem,nullptr); - depth.mem=nullptr; - } + SAFE_CLEAR(sc_depth); sc_image_views.Clear(); diff --git a/src/RenderDevice/Vulkan/VKDeviceCreater.cpp b/src/RenderDevice/Vulkan/VKDeviceCreater.cpp index d7e9e0cd..03c38b97 100644 --- a/src/RenderDevice/Vulkan/VKDeviceCreater.cpp +++ b/src/RenderDevice/Vulkan/VKDeviceCreater.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #ifdef _DEBUG #include @@ -182,65 +183,74 @@ namespace bool CreateDepthBuffer(DeviceAttribute *rsa) { - VkImageCreateInfo image_info={}; + //VkImageCreateInfo image_info={}; const VkFormat depth_format=VK_FORMAT_D16_UNORM; const VkFormatProperties props=rsa->physical_device->GetFormatProperties(depth_format); - if(props.linearTilingFeatures&VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) - image_info.tiling=VK_IMAGE_TILING_LINEAR; - else - if(props.optimalTilingFeatures&VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) - image_info.tiling=VK_IMAGE_TILING_OPTIMAL; - else - return(false); + //if(props.linearTilingFeatures&VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) + // image_info.tiling=VK_IMAGE_TILING_LINEAR; + //else + //if(props.optimalTilingFeatures&VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) + // image_info.tiling=VK_IMAGE_TILING_OPTIMAL; + //else + // return(false); - image_info.sType=VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; - image_info.pNext=nullptr; - image_info.imageType=VK_IMAGE_TYPE_2D; - image_info.format=depth_format; - image_info.extent.width=rsa->swapchain_extent.width; - image_info.extent.height=rsa->swapchain_extent.height; - image_info.extent.depth=1; - image_info.mipLevels=1; - image_info.arrayLayers=1; - image_info.samples=VK_SAMPLE_COUNT_1_BIT; - image_info.initialLayout=VK_IMAGE_LAYOUT_UNDEFINED; - image_info.usage=VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; - image_info.queueFamilyIndexCount=0; - image_info.pQueueFamilyIndices=nullptr; - image_info.sharingMode=VK_SHARING_MODE_EXCLUSIVE; - image_info.flags=0; + //image_info.sType=VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; + //image_info.pNext=nullptr; + //image_info.imageType=VK_IMAGE_TYPE_2D; + //image_info.format=depth_format; + //image_info.extent.width=rsa->swapchain_extent.width; + //image_info.extent.height=rsa->swapchain_extent.height; + //image_info.extent.depth=1; + //image_info.mipLevels=1; + //image_info.arrayLayers=1; + //image_info.samples=VK_SAMPLE_COUNT_1_BIT; + //image_info.initialLayout=VK_IMAGE_LAYOUT_UNDEFINED; + //image_info.usage=VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; + //image_info.queueFamilyIndexCount=0; + //image_info.pQueueFamilyIndices=nullptr; + //image_info.sharingMode=VK_SHARING_MODE_EXCLUSIVE; + //image_info.flags=0; - rsa->depth.format=depth_format; + //rsa->depth.format=depth_format; - if(vkCreateImage(rsa->device,&image_info,nullptr,&rsa->depth.image)!=VK_SUCCESS) - return(false); + //if(vkCreateImage(rsa->device,&image_info,nullptr,&rsa->depth.image)!=VK_SUCCESS) + // return(false); - VkMemoryRequirements mem_reqs; - vkGetImageMemoryRequirements(rsa->device,rsa->depth.image,&mem_reqs); + //VkMemoryRequirements mem_reqs; + //vkGetImageMemoryRequirements(rsa->device,rsa->depth.image,&mem_reqs); - VkMemoryAllocateInfo mem_alloc={}; - mem_alloc.sType=VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; - mem_alloc.pNext=nullptr; - mem_alloc.allocationSize=0; - mem_alloc.memoryTypeIndex=0; - mem_alloc.allocationSize=mem_reqs.size; + //VkMemoryAllocateInfo mem_alloc={}; + //mem_alloc.sType=VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; + //mem_alloc.pNext=nullptr; + //mem_alloc.allocationSize=0; + //mem_alloc.memoryTypeIndex=0; + //mem_alloc.allocationSize=mem_reqs.size; - if(!rsa->CheckMemoryType(mem_reqs.memoryTypeBits,VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,&mem_alloc.memoryTypeIndex)) - return(false); + //if(!rsa->CheckMemoryType(mem_reqs.memoryTypeBits,VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,&mem_alloc.memoryTypeIndex)) + // return(false); - if(vkAllocateMemory(rsa->device,&mem_alloc,nullptr,&rsa->depth.mem)!=VK_SUCCESS) - return(false); + //if(vkAllocateMemory(rsa->device,&mem_alloc,nullptr,&rsa->depth.mem)!=VK_SUCCESS) + // return(false); - if(vkBindImageMemory(rsa->device,rsa->depth.image,rsa->depth.mem,0)!=VK_SUCCESS) - return(false); + //if(vkBindImageMemory(rsa->device,rsa->depth.image,rsa->depth.mem,0)!=VK_SUCCESS) + // return(false); - rsa->depth.view=CreateDepthImageView(rsa->device,depth_format,rsa->depth.image); + //rsa->depth.view=CreateDepthImageView(rsa->device,depth_format,rsa->depth.image); - if(rsa->depth.view==nullptr) - return(false); + //if(rsa->depth.view==nullptr) + // return(false); + + rsa->sc_depth=VK_NAMESPACE::CreateTexture2D(rsa->device,rsa->physical_device, + depth_format, + rsa->swapchain_extent.width, + rsa->swapchain_extent.height, + VK_IMAGE_ASPECT_DEPTH_BIT, + VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, + VK_IMAGE_LAYOUT_UNDEFINED, + (props.optimalTilingFeatures&VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)?VK_IMAGE_TILING_OPTIMAL:VK_IMAGE_TILING_LINEAR); return(true); }