Swapchain创建depth改用Texture2D

This commit is contained in:
hyzboy 2019-06-26 16:03:21 +08:00
parent 6cdb573221
commit 3b86783c53
6 changed files with 67 additions and 70 deletions

View File

@ -67,8 +67,8 @@ public:
public: public:
const uint32_t GetSwapChainImageCount ()const {return attr->sc_image_views.GetCount();} const uint32_t GetSwapChainImageCount ()const {return attr->sc_image_views.GetCount();}
ImageView *GetColorImageView (int index) {return attr->sc_image_views[index];} //ImageView *GetColorImageView (int index) {return attr->sc_image_views[index];}
ImageView *GetDepthImageView () {return attr->depth.view;} //ImageView *GetDepthImageView () {return attr->depth.view;}
RenderPass * GetRenderPass () {return main_rp;} RenderPass * GetRenderPass () {return main_rp;}
Framebuffer * GetFramebuffer (int index) {return render_frame[index];} Framebuffer * GetFramebuffer (int index) {return render_frame[index];}

View File

@ -39,14 +39,7 @@ struct DeviceAttribute
List<VkImage> sc_images; List<VkImage> sc_images;
ObjectList<ImageView> sc_image_views; ObjectList<ImageView> sc_image_views;
struct Texture2D * sc_depth =nullptr;
{
VkFormat format;
VkImage image =nullptr;
VkDeviceMemory mem =nullptr;
ImageView * view =nullptr;
}depth;
VkDescriptorPool desc_pool =nullptr; VkDescriptorPool desc_pool =nullptr;

View File

@ -2,6 +2,7 @@
#define HGL_GRAPH_VULKAN_TEXTURE_INCLUDE #define HGL_GRAPH_VULKAN_TEXTURE_INCLUDE
#include<hgl/graph/vulkan/VK.h> #include<hgl/graph/vulkan/VK.h>
#include<hgl/graph/vulkan/VKMemory.h>
#include<hgl/graph/vulkan/VKImageView.h> #include<hgl/graph/vulkan/VKImageView.h>
VK_NAMESPACE_BEGIN VK_NAMESPACE_BEGIN
struct TextureData struct TextureData
@ -30,11 +31,14 @@ public:
operator TextureData * (){return data;} 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 VkImage (){return data?data->image:nullptr;}
operator VkImageLayout (){return data?data->image_layout:VK_IMAGE_LAYOUT_UNDEFINED;} operator VkImageLayout (){return data?data->image_layout:VK_IMAGE_LAYOUT_UNDEFINED;}
operator VkImageView (){return data?data->image_view->operator VkImageView():nullptr;} 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 uint32 GetMipLevels()const{return data?data->mip_levels:0;}
const bool IsLinear ()const{return data?data->linear:false;} const bool IsLinear ()const{return data?data->linear:false;}

View File

@ -1,5 +1,6 @@
#include<hgl/graph/vulkan/VKDevice.h> #include<hgl/graph/vulkan/VKDevice.h>
#include<hgl/type/Pair.h> #include<hgl/type/Pair.h>
#include<hgl/graph/vulkan/VKTexture.h>
#include<hgl/graph/vulkan/VKImageView.h> #include<hgl/graph/vulkan/VKImageView.h>
#include<hgl/graph/vulkan/VKCommandBuffer.h> #include<hgl/graph/vulkan/VKCommandBuffer.h>
//#include<hgl/graph/vulkan/VKDescriptorSet.h> //#include<hgl/graph/vulkan/VKDescriptorSet.h>
@ -74,13 +75,13 @@ void Device::RecreateDevice()
present_info.pSwapchains=&attr->swap_chain; 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(); swap_chain_count=attr->sc_image_views.GetCount();
for(uint i=0;i<swap_chain_count;i++) for(uint i=0;i<swap_chain_count;i++)
{ {
render_frame.Add(vulkan::CreateFramebuffer(this,main_rp,attr->sc_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)); fence_list.Add(this->CreateFence(true));
} }

View File

@ -1,6 +1,7 @@
#include<hgl/graph/vulkan/VKDeviceAttribute.h> #include<hgl/graph/vulkan/VKDeviceAttribute.h>
#include<hgl/graph/vulkan/VKPhysicalDevice.h> #include<hgl/graph/vulkan/VKPhysicalDevice.h>
#include<hgl/graph/vulkan/VKImageView.h> #include<hgl/graph/vulkan/VKImageView.h>
#include<hgl/graph/vulkan/VKTexture.h>
#include<iostream> #include<iostream>
VK_NAMESPACE_BEGIN VK_NAMESPACE_BEGIN
@ -41,19 +42,7 @@ bool DeviceAttribute::CheckMemoryType(uint32_t typeBits,VkMemoryPropertyFlags pr
void DeviceAttribute::ClearSwapchain() void DeviceAttribute::ClearSwapchain()
{ {
SAFE_CLEAR(depth.view); SAFE_CLEAR(sc_depth);
if(depth.image)
{
vkDestroyImage(device,depth.image,nullptr);
depth.image=nullptr;
}
if(depth.mem)
{
vkFreeMemory(device,depth.mem,nullptr);
depth.mem=nullptr;
}
sc_image_views.Clear(); sc_image_views.Clear();

View File

@ -2,6 +2,7 @@
#include<hgl/graph/vulkan/VKInstance.h> #include<hgl/graph/vulkan/VKInstance.h>
#include<hgl/graph/vulkan/VKPhysicalDevice.h> #include<hgl/graph/vulkan/VKPhysicalDevice.h>
#include<hgl/graph/vulkan/VKFramebuffer.h> #include<hgl/graph/vulkan/VKFramebuffer.h>
#include<hgl/graph/vulkan/VKTexture.h>
#ifdef _DEBUG #ifdef _DEBUG
#include<iostream> #include<iostream>
@ -182,65 +183,74 @@ namespace
bool CreateDepthBuffer(DeviceAttribute *rsa) bool CreateDepthBuffer(DeviceAttribute *rsa)
{ {
VkImageCreateInfo image_info={}; //VkImageCreateInfo image_info={};
const VkFormat depth_format=VK_FORMAT_D16_UNORM; const VkFormat depth_format=VK_FORMAT_D16_UNORM;
const VkFormatProperties props=rsa->physical_device->GetFormatProperties(depth_format); const VkFormatProperties props=rsa->physical_device->GetFormatProperties(depth_format);
if(props.linearTilingFeatures&VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) //if(props.linearTilingFeatures&VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)
image_info.tiling=VK_IMAGE_TILING_LINEAR; // image_info.tiling=VK_IMAGE_TILING_LINEAR;
else //else
if(props.optimalTilingFeatures&VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) //if(props.optimalTilingFeatures&VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)
image_info.tiling=VK_IMAGE_TILING_OPTIMAL; // image_info.tiling=VK_IMAGE_TILING_OPTIMAL;
else //else
return(false); // return(false);
image_info.sType=VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; //image_info.sType=VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
image_info.pNext=nullptr; //image_info.pNext=nullptr;
image_info.imageType=VK_IMAGE_TYPE_2D; //image_info.imageType=VK_IMAGE_TYPE_2D;
image_info.format=depth_format; //image_info.format=depth_format;
image_info.extent.width=rsa->swapchain_extent.width; //image_info.extent.width=rsa->swapchain_extent.width;
image_info.extent.height=rsa->swapchain_extent.height; //image_info.extent.height=rsa->swapchain_extent.height;
image_info.extent.depth=1; //image_info.extent.depth=1;
image_info.mipLevels=1; //image_info.mipLevels=1;
image_info.arrayLayers=1; //image_info.arrayLayers=1;
image_info.samples=VK_SAMPLE_COUNT_1_BIT; //image_info.samples=VK_SAMPLE_COUNT_1_BIT;
image_info.initialLayout=VK_IMAGE_LAYOUT_UNDEFINED; //image_info.initialLayout=VK_IMAGE_LAYOUT_UNDEFINED;
image_info.usage=VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; //image_info.usage=VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
image_info.queueFamilyIndexCount=0; //image_info.queueFamilyIndexCount=0;
image_info.pQueueFamilyIndices=nullptr; //image_info.pQueueFamilyIndices=nullptr;
image_info.sharingMode=VK_SHARING_MODE_EXCLUSIVE; //image_info.sharingMode=VK_SHARING_MODE_EXCLUSIVE;
image_info.flags=0; //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) //if(vkCreateImage(rsa->device,&image_info,nullptr,&rsa->depth.image)!=VK_SUCCESS)
return(false); // return(false);
VkMemoryRequirements mem_reqs; //VkMemoryRequirements mem_reqs;
vkGetImageMemoryRequirements(rsa->device,rsa->depth.image,&mem_reqs); //vkGetImageMemoryRequirements(rsa->device,rsa->depth.image,&mem_reqs);
VkMemoryAllocateInfo mem_alloc={}; //VkMemoryAllocateInfo mem_alloc={};
mem_alloc.sType=VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; //mem_alloc.sType=VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
mem_alloc.pNext=nullptr; //mem_alloc.pNext=nullptr;
mem_alloc.allocationSize=0; //mem_alloc.allocationSize=0;
mem_alloc.memoryTypeIndex=0; //mem_alloc.memoryTypeIndex=0;
mem_alloc.allocationSize=mem_reqs.size; //mem_alloc.allocationSize=mem_reqs.size;
if(!rsa->CheckMemoryType(mem_reqs.memoryTypeBits,VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,&mem_alloc.memoryTypeIndex)) //if(!rsa->CheckMemoryType(mem_reqs.memoryTypeBits,VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,&mem_alloc.memoryTypeIndex))
return(false); // return(false);
if(vkAllocateMemory(rsa->device,&mem_alloc,nullptr,&rsa->depth.mem)!=VK_SUCCESS) //if(vkAllocateMemory(rsa->device,&mem_alloc,nullptr,&rsa->depth.mem)!=VK_SUCCESS)
return(false); // return(false);
if(vkBindImageMemory(rsa->device,rsa->depth.image,rsa->depth.mem,0)!=VK_SUCCESS) //if(vkBindImageMemory(rsa->device,rsa->depth.image,rsa->depth.mem,0)!=VK_SUCCESS)
return(false); // 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) //if(rsa->depth.view==nullptr)
return(false); // 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); return(true);
} }