changeset 1067:559ade263dda

add: some checks for image creation
author sam <sam@basx.dev>
date Tue, 02 Apr 2024 14:05:22 +0700
parents 92f0db6bbd69
children 97d63dc643b2
files semicongine/vulkan/image.nim
diffstat 1 files changed, 18 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/semicongine/vulkan/image.nim	Tue Apr 02 00:37:42 2024 +0700
+++ b/semicongine/vulkan/image.nim	Tue Apr 02 14:05:22 2024 +0700
@@ -41,7 +41,6 @@
   PixelDepth(4): VK_FORMAT_R8G8B8A8_SRGB,
 }.toTable
 
-
 proc requirements(image: VulkanImage): MemoryRequirements =
   assert image.vk.valid
   assert image.device.vk.valid
@@ -159,12 +158,28 @@
   result.format = DEPTH_FORMAT_MAP[depth]
   result.usage = @[VK_IMAGE_USAGE_TRANSFER_DST_BIT, VK_IMAGE_USAGE_SAMPLED_BIT]
 
+  var formatInfo = VkPhysicalDeviceImageFormatInfo2(
+    sType: VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
+    format: result.format,
+    thetype: VK_IMAGE_TYPE_2D,
+    tiling: VK_IMAGE_TILING_OPTIMAL,
+    usage: toBits result.usage,
+  )
+  var formatProperties = VkImageFormatProperties2(sType: VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2)
+  checkVkResult device.physicalDevice.vk.vkGetPhysicalDeviceImageFormatProperties2(
+    addr formatInfo,
+    addr formatProperties,
+  )
+  assert size <= int(formatProperties.imageFormatProperties.maxResourceSize)
+  assert width <= int(formatProperties.imageFormatProperties.maxExtent.width)
+  assert height <= int(formatProperties.imageFormatProperties.maxExtent.height)
+
   var imageInfo = VkImageCreateInfo(
     sType: VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
     imageType: VK_IMAGE_TYPE_2D,
     extent: VkExtent3D(width: uint32(width), height: uint32(height), depth: 1),
-    mipLevels: 1,
-    arrayLayers: 1,
+    mipLevels: min(1'u32, formatProperties.imageFormatProperties.maxMipLevels),
+    arrayLayers: min(1'u32, formatProperties.imageFormatProperties.maxArrayLayers),
     format: result.format,
     tiling: VK_IMAGE_TILING_OPTIMAL,
     initialLayout: VK_IMAGE_LAYOUT_UNDEFINED,