changeset 97:110ed3ee5df8

add: swapchain, images, fix some destroctors
author Sam <sam@basx.dev>
date Thu, 02 Mar 2023 23:16:45 +0700
parents b9fc90de1450
children 335a65d3f892
files src/semicongine/engine.nim src/semicongine/vulkan/api.nim src/semicongine/vulkan/buffer.nim src/semicongine/vulkan/image.nim src/semicongine/vulkan/instance.nim src/semicongine/vulkan/physicaldevice.nim src/semicongine/vulkan/swapchain.nim src/vulkan_api/vulkan_api_generator.nim tests/test_vulkan_wrapper.nim
diffstat 9 files changed, 83 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/semicongine/engine.nim	Wed Mar 01 23:58:39 2023 +0700
+++ b/src/semicongine/engine.nim	Thu Mar 02 23:16:45 2023 +0700
@@ -252,8 +252,7 @@
         layerCount: 1,
       ),
     )
-    checkVkResult device.vkCreateImageView(addr(imageViewCreateInfo), nil, addr(
-        result.imageviews[i]))
+    checkVkResult device.vkCreateImageView(addr(imageViewCreateInfo), nil, addr(result.imageviews[i]))
 
 proc setupRenderPass(device: VkDevice, format: VkFormat): VkRenderPass =
   var
--- a/src/semicongine/vulkan/api.nim	Wed Mar 01 23:58:39 2023 +0700
+++ b/src/semicongine/vulkan/api.nim	Thu Mar 02 23:16:45 2023 +0700
@@ -35,7 +35,7 @@
 macro enumFullRange(a: typed): untyped =
   newNimNode(nnkBracket).add(a.getType[1][1..^1])
 
-iterator items[T: HoleyEnum](E: typedesc[T]): T =
+iterator items*[T: HoleyEnum](E: typedesc[T]): T =
   for a in enumFullRange(E): yield a
 const
   VK_MAX_PHYSICAL_DEVICE_NAME_SIZE*: uint32 = 256
--- a/src/semicongine/vulkan/buffer.nim	Wed Mar 01 23:58:39 2023 +0700
+++ b/src/semicongine/vulkan/buffer.nim	Thu Mar 02 23:16:45 2023 +0700
@@ -27,7 +27,8 @@
     pBuffer=addr result.vk
   )
 
-proc destroy(buffer: Buffer) =
+proc destroy(buffer: var Buffer) =
   assert buffer.device.vk.valid
   assert buffer.vk.valid
   buffer.device.vk.vkDestroyBuffer(buffer.vk, nil)
+  buffer.vk.reset()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/semicongine/vulkan/image.nim	Thu Mar 02 23:16:45 2023 +0700
@@ -0,0 +1,50 @@
+import ./api
+import ./device
+
+type
+  Image* = object
+    vk*: VkImage
+    format*: VkFormat
+    device*: Device
+  ImageView* = object
+    vk*: VkImageView
+    image*: Image
+
+proc createImageView*(
+  image: Image,
+  imageviewtype=VK_IMAGE_VIEW_TYPE_2D,
+  baseMipLevel=0'u32,
+  levelCount=1'u32,
+  baseArrayLayer=0'u32,
+  layerCount=1'u32
+): ImageView =
+  assert image.device.vk.valid
+  assert image.vk.valid
+
+  var createInfo = VkImageViewCreateInfo(
+    sType: VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
+    image: image.vk,
+    viewType: imageviewtype,
+    format: image.format,
+    components: VkComponentMapping(
+      r: VK_COMPONENT_SWIZZLE_IDENTITY,
+      g: VK_COMPONENT_SWIZZLE_IDENTITY,
+      b: VK_COMPONENT_SWIZZLE_IDENTITY,
+      a: VK_COMPONENT_SWIZZLE_IDENTITY,
+    ),
+    subresourceRange: VkImageSubresourceRange(
+      aspectMask: VkImageAspectFlags(VK_IMAGE_ASPECT_COLOR_BIT),
+      baseMipLevel: baseMipLevel,
+      levelCount: levelCount,
+      baseArrayLayer: baseArrayLayer,
+      layerCount: layerCount,
+    ),
+  )
+  result.image = image
+  checkVkResult image.device.vk.vkCreateImageView(addr(createInfo), nil, addr(result.vk))
+
+proc destroy*(imageview: var ImageView) =
+  assert imageview.image.device.vk.valid
+  assert imageview.vk.valid
+  imageview.image.device.vk.vkDestroyImageView(imageview.vk, nil)
+  imageview.vk.reset()
--- a/src/semicongine/vulkan/instance.nim	Wed Mar 01 23:58:39 2023 +0700
+++ b/src/semicongine/vulkan/instance.nim	Thu Mar 02 23:16:45 2023 +0700
@@ -1,5 +1,4 @@
 import std/strformat
-import std/enumutils
 import std/sequtils
 
 import ./api
@@ -86,7 +85,7 @@
   assert instance.surface.valid
   # needs to happen after window is trashed as the driver might have a hook registered for the window destruction
   instance.vk.vkDestroySurfaceKHR(instance.surface, nil)
-  instance.surface.reset
+  instance.surface.reset()
   instance.vk.vkDestroyInstance(nil)
   instance.vk.reset()
 
--- a/src/semicongine/vulkan/physicaldevice.nim	Wed Mar 01 23:58:39 2023 +0700
+++ b/src/semicongine/vulkan/physicaldevice.nim	Thu Mar 02 23:16:45 2023 +0700
@@ -1,5 +1,4 @@
-import std/enumerate
-import std/options
+from std/enumerate import enumerate
 import std/tables
 import std/sequtils
 
--- a/src/semicongine/vulkan/swapchain.nim	Wed Mar 01 23:58:39 2023 +0700
+++ b/src/semicongine/vulkan/swapchain.nim	Thu Mar 02 23:16:45 2023 +0700
@@ -1,13 +1,16 @@
-import std/options
-
 import ./api
+import ./utils
 import ./device
 import ./physicaldevice
+import ./image
 
 type
   Swapchain = object
     vk*: VkSwapchainKHR
     device*: Device
+    images*: seq[Image]
+    imageviews*: seq[ImageView]
+    format: VkFormat
 
 
 proc createSwapchain*(device: Device, surfaceFormat: VkSurfaceFormatKHR, nBuffers=3'u32, presentationMode: VkPresentModeKHR=VK_PRESENT_MODE_MAILBOX_KHR): (Swapchain, VkResult) =
@@ -41,11 +44,31 @@
     clipped: true,
   )
   var
-    swapchain = Swapchain(device: device)
+    swapchain = Swapchain(device: device, format: surfaceFormat.format)
     createResult = device.vk.vkCreateSwapchainKHR(addr(createInfo), nil, addr(swapchain.vk))
 
+  if createResult == VK_SUCCESS:
+    var nImages: uint32
+    checkVkResult device.vk.vkGetSwapchainImagesKHR(swapChain.vk, addr(nImages), nil)
+    var images = newSeq[VkImage](nImages)
+    checkVkResult device.vk.vkGetSwapchainImagesKHR(swapChain.vk, addr(nImages), images.toCPointer)
+    for vkimage in images:
+      let image = Image(vk: vkimage, format: surfaceFormat.format, device: device)
+      swapChain.images.add image
+      swapChain.imageviews.add image.createImageView()
+
   return (swapchain, createResult)
 
+proc getImages*(device: VkDevice, swapChain: VkSwapchainKHR): seq[VkImage] =
+  var n_images: uint32
+  checkVkResult vkGetSwapchainImagesKHR(device, swapChain, addr(n_images), nil)
+  result = newSeq[VkImage](n_images)
+  checkVkResult vkGetSwapchainImagesKHR(device, swapChain, addr(n_images), addr(
+      result[0]))
+
 proc destroy*(swapchain: var Swapchain) =
   assert swapchain.vk.valid
+  for imageview in swapchain.imageviews.mitems:
+    imageview.destroy()
   swapchain.device.vk.vkDestroySwapchainKHR(swapchain.vk, nil)
+  swapchain.vk.reset()
--- a/src/vulkan_api/vulkan_api_generator.nim	Wed Mar 01 23:58:39 2023 +0700
+++ b/src/vulkan_api/vulkan_api_generator.nim	Thu Mar 02 23:16:45 2023 +0700
@@ -454,7 +454,7 @@
 macro enumFullRange(a: typed): untyped =
   newNimNode(nnkBracket).add(a.getType[1][1..^1])
 
-iterator items[T: HoleyEnum](E: typedesc[T]): T =
+iterator items*[T: HoleyEnum](E: typedesc[T]): T =
   for a in enumFullRange(E): yield a""",
     ],
     "structs": @["type"],
--- a/tests/test_vulkan_wrapper.nim	Wed Mar 01 23:58:39 2023 +0700
+++ b/tests/test_vulkan_wrapper.nim	Thu Mar 02 23:16:45 2023 +0700
@@ -1,4 +1,3 @@
-import std/tables
 import std/options
 
 import semicongine/vulkan