diff --git a/example/Vulkan/RenderSurfaceAttribute.cpp b/example/Vulkan/RenderSurfaceAttribute.cpp index 320059eb..551779b1 100644 --- a/example/Vulkan/RenderSurfaceAttribute.cpp +++ b/example/Vulkan/RenderSurfaceAttribute.cpp @@ -130,16 +130,27 @@ RenderSurfaceAttribute::RenderSurfaceAttribute(VkInstance inst,VkPhysicalDevice RenderSurfaceAttribute::~RenderSurfaceAttribute() { - const uint32_t iv_count=sc_image_views.GetCount(); + if(depth.view) + vkDestroyImageView(device,depth.view,nullptr); + + if(depth.image) + vkDestroyImage(device,depth.image,nullptr); + + if(depth.mem) + vkFreeMemory(device,depth.mem,nullptr); - if(iv_count>0) { - VkImageView *iv=sc_image_views.GetData(); + const uint32_t iv_count=sc_image_views.GetCount(); - for(uint32_t i=0;i0) { - vkDestroyImageView(device,*iv,nullptr); - ++iv; + VkImageView *iv=sc_image_views.GetData(); + + for(uint32_t i=0;i sc_images; List sc_image_views; + struct + { + VkFormat format; + + VkImage image =nullptr; + VkDeviceMemory mem =nullptr; + VkImageView view =nullptr; + }depth; + public: RenderSurfaceAttribute(VkInstance inst,VkPhysicalDevice pd,VkSurfaceKHR s); diff --git a/example/Vulkan/RenderSurfaceCreater.cpp b/example/Vulkan/RenderSurfaceCreater.cpp index 024e31f4..85fa7a60 100644 --- a/example/Vulkan/RenderSurfaceCreater.cpp +++ b/example/Vulkan/RenderSurfaceCreater.cpp @@ -171,6 +171,107 @@ namespace 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>=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 RenderSurface *CreateRenderSuface(VkInstance inst,VkPhysicalDevice physical_device,Window *win) @@ -205,6 +306,9 @@ RenderSurface *CreateRenderSuface(VkInstance inst,VkPhysicalDevice physical_devi if(!CreateImageView(rsa)) return(nullptr); + if(!CreateDepthBuffer(rsa)) + return(nullptr); + return(new RenderSurface(rsa)); } VK_NAMESPACE_END