增加创建深度缓冲区
This commit is contained in:
parent
3871125498
commit
2e1f624090
@ -129,6 +129,16 @@ RenderSurfaceAttribute::RenderSurfaceAttribute(VkInstance inst,VkPhysicalDevice
|
|||||||
}
|
}
|
||||||
|
|
||||||
RenderSurfaceAttribute::~RenderSurfaceAttribute()
|
RenderSurfaceAttribute::~RenderSurfaceAttribute()
|
||||||
|
{
|
||||||
|
if(depth.view)
|
||||||
|
vkDestroyImageView(device,depth.view,nullptr);
|
||||||
|
|
||||||
|
if(depth.image)
|
||||||
|
vkDestroyImage(device,depth.image,nullptr);
|
||||||
|
|
||||||
|
if(depth.mem)
|
||||||
|
vkFreeMemory(device,depth.mem,nullptr);
|
||||||
|
|
||||||
{
|
{
|
||||||
const uint32_t iv_count=sc_image_views.GetCount();
|
const uint32_t iv_count=sc_image_views.GetCount();
|
||||||
|
|
||||||
@ -142,6 +152,7 @@ RenderSurfaceAttribute::~RenderSurfaceAttribute()
|
|||||||
++iv;
|
++iv;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(swap_chain)
|
if(swap_chain)
|
||||||
vkDestroySwapchainKHR(device,swap_chain,nullptr);
|
vkDestroySwapchainKHR(device,swap_chain,nullptr);
|
||||||
|
@ -38,6 +38,15 @@ struct RenderSurfaceAttribute
|
|||||||
List<VkImage> sc_images;
|
List<VkImage> sc_images;
|
||||||
List<VkImageView> sc_image_views;
|
List<VkImageView> sc_image_views;
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
VkFormat format;
|
||||||
|
|
||||||
|
VkImage image =nullptr;
|
||||||
|
VkDeviceMemory mem =nullptr;
|
||||||
|
VkImageView view =nullptr;
|
||||||
|
}depth;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
RenderSurfaceAttribute(VkInstance inst,VkPhysicalDevice pd,VkSurfaceKHR s);
|
RenderSurfaceAttribute(VkInstance inst,VkPhysicalDevice pd,VkSurfaceKHR s);
|
||||||
|
@ -171,6 +171,107 @@ namespace
|
|||||||
|
|
||||||
return(true);
|
return(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool memory_type_from_properties(VkPhysicalDeviceMemoryProperties memory_properties,uint32_t typeBits,VkFlags requirements_mask,uint32_t *typeIndex)
|
||||||
|
{
|
||||||
|
// Search memtypes to find first index with those properties
|
||||||
|
for(uint32_t i=0; i<memory_properties.memoryTypeCount; i++)
|
||||||
|
{
|
||||||
|
if((typeBits&1)==1)
|
||||||
|
{
|
||||||
|
// Type is available, does it match user properties?
|
||||||
|
if((memory_properties.memoryTypes[i].propertyFlags&requirements_mask)==requirements_mask)
|
||||||
|
{
|
||||||
|
*typeIndex=i;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
typeBits>>=1;
|
||||||
|
}
|
||||||
|
// No memory types matched, return failure
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CreateDepthBuffer(RenderSurfaceAttribute *rsa)
|
||||||
|
{
|
||||||
|
VkImageCreateInfo image_info={};
|
||||||
|
|
||||||
|
const VkFormat depth_format=VK_FORMAT_D16_UNORM;
|
||||||
|
VkFormatProperties props;
|
||||||
|
|
||||||
|
vkGetPhysicalDeviceFormatProperties(rsa->physical_device,depth_format,&props);
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
VkImageViewCreateInfo view_info={};
|
||||||
|
view_info.sType=VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
||||||
|
view_info.pNext=nullptr;
|
||||||
|
view_info.image=VK_NULL_HANDLE;
|
||||||
|
view_info.format=depth_format;
|
||||||
|
view_info.components.r=VK_COMPONENT_SWIZZLE_R;
|
||||||
|
view_info.components.g=VK_COMPONENT_SWIZZLE_G;
|
||||||
|
view_info.components.b=VK_COMPONENT_SWIZZLE_B;
|
||||||
|
view_info.components.a=VK_COMPONENT_SWIZZLE_A;
|
||||||
|
view_info.subresourceRange.aspectMask=VK_IMAGE_ASPECT_DEPTH_BIT;
|
||||||
|
view_info.subresourceRange.baseMipLevel=0;
|
||||||
|
view_info.subresourceRange.levelCount=1;
|
||||||
|
view_info.subresourceRange.baseArrayLayer=0;
|
||||||
|
view_info.subresourceRange.layerCount=1;
|
||||||
|
view_info.viewType=VK_IMAGE_VIEW_TYPE_2D;
|
||||||
|
view_info.flags=0;
|
||||||
|
|
||||||
|
rsa->depth.format=depth_format;
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
bool pass=memory_type_from_properties(rsa->memory_properties,mem_reqs.memoryTypeBits,VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,&mem_alloc.memoryTypeIndex);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
view_info.image=rsa->depth.image;
|
||||||
|
if(vkCreateImageView(rsa->device,&view_info,nullptr,&rsa->depth.view)!=VK_SUCCESS)
|
||||||
|
return(false);
|
||||||
|
|
||||||
|
return(true);
|
||||||
|
}
|
||||||
}//namespace
|
}//namespace
|
||||||
|
|
||||||
RenderSurface *CreateRenderSuface(VkInstance inst,VkPhysicalDevice physical_device,Window *win)
|
RenderSurface *CreateRenderSuface(VkInstance inst,VkPhysicalDevice physical_device,Window *win)
|
||||||
@ -205,6 +306,9 @@ RenderSurface *CreateRenderSuface(VkInstance inst,VkPhysicalDevice physical_devi
|
|||||||
if(!CreateImageView(rsa))
|
if(!CreateImageView(rsa))
|
||||||
return(nullptr);
|
return(nullptr);
|
||||||
|
|
||||||
|
if(!CreateDepthBuffer(rsa))
|
||||||
|
return(nullptr);
|
||||||
|
|
||||||
return(new RenderSurface(rsa));
|
return(new RenderSurface(rsa));
|
||||||
}
|
}
|
||||||
VK_NAMESPACE_END
|
VK_NAMESPACE_END
|
||||||
|
Loading…
x
Reference in New Issue
Block a user