changeset 1196:82feceae80b1 compiletime-tests

sync from bedroom to office
author sam <sam@basx.dev>
date Tue, 09 Jul 2024 22:53:38 +0700
parents cfba2b7e00d0
children f6a0dc7ad052
files semicongine/rendering.nim semicongine/rendering/swapchain.nim semicongine/rendering/vulkan_wrappers.nim test1.nim
diffstat 4 files changed, 50 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/semicongine/rendering.nim	Mon Jul 08 23:47:33 2024 +0700
+++ b/semicongine/rendering.nim	Tue Jul 09 22:53:38 2024 +0700
@@ -50,7 +50,7 @@
     queueFinishedFence*: array[INFLIGHTFRAMES, VkFence]
     imageAvailableSemaphore*: array[INFLIGHTFRAMES, VkSemaphore]
     renderFinishedSemaphore*: array[INFLIGHTFRAMES, VkSemaphore]
-    currentFiF: int[0 .. INFLIGHTFRAMES - 1]
+    currentFiF: range[0 .. (INFLIGHTFRAMES - 1).int]
     # unclear as of yet
     anisotropy*: float32 = 0 # needs to be enable during device creation
 
--- a/semicongine/rendering/swapchain.nim	Mon Jul 08 23:47:33 2024 +0700
+++ b/semicongine/rendering/swapchain.nim	Tue Jul 09 22:53:38 2024 +0700
@@ -2,17 +2,13 @@
 
 proc InitSwapchain*(
   renderPass: VkRenderPass,
-  vSync: bool,
+  vSync: bool = false,
   samples = VK_SAMPLE_COUNT_1_BIT,
   nFramebuffers = N_FRAMEBUFFERS,
   oldSwapchain = VkSwapchainKHR(0),
-): Swapchain =
+): Option[Swapchain] =
   assert vulkan.instance.Valid
 
-  result.renderPass = renderPass
-  result.vSync = vSync
-  result.samples = samples
-
   var capabilities: VkSurfaceCapabilitiesKHR
   checkVkResult vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vulkan.physicalDevice, vulkan.surface, addr(capabilities))
   let
@@ -21,7 +17,7 @@
     height = capabilities.currentExtent.height
 
   if width == 0 or height == 0:
-    return VkSwapchainKHR(0)
+    return none(Swapchain)
 
   # following "count" is established according to vulkan specs
   var minFramebufferCount = N_FRAMEBUFFERS
@@ -47,47 +43,56 @@
     clipped: true,
     oldSwapchain: oldSwapchain,
   )
-  if vkCreateSwapchainKHR(vulkan.device, addr(createInfo), nil, addr(result.vk)) != VK_SUCCESS:
-    return VkSwapchainKHR(0)
+  var swapchain: Swapchain
+  if vkCreateSwapchainKHR(vulkan.device, addr(createInfo), nil, addr(swapchain.vk)) != VK_SUCCESS:
+    return none(Swapchain)
+
+  swapchain.renderPass = renderPass
+  swapchain.vSync = vSync
+  swapchain.samples = samples
 
   # create msaa image+view if desired
   if samples != VK_SAMPLE_COUNT_1_BIT:
-    let imgSize = width * height * format.size
-    result.msaaImage = svkCreate2DImage(
+    swapchain.msaaImage = svkCreate2DImage(
       width = width,
       height = height,
       format = format,
       usage = [VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT],
     )
-    result.msaaMemory = svkAllocateMemory(imgSize, BestMemory(mappable = false))
+    let requirements = svkGetImageMemoryRequirements(swapchain.msaaImage)
+    swapchain.msaaMemory = svkAllocateMemory(
+      requirements.size,
+      BestMemory(mappable = false, filter = requirements.memoryTypes)
+    )
     checkVkResult vkBindImageMemory(
       vulkan.device,
-      result.msaaImage,
-      result.msaaMemory,
+      swapchain.msaaImage,
+      swapchain.msaaMemory,
       0,
     )
-    result.msaaImageView = svkCreate2DImageView(result.msaaImage, format)
+    swapchain.msaaImageView = svkCreate2DImageView(swapchain.msaaImage, format)
 
-    # create framebuffers
-    var actualNFramebuffers: uint32
-    checkVkResult vkGetSwapchainImagesKHR(vulkan.device, result.vk, addr(actualNFramebuffers), nil)
-    var framebuffers = newSeq[VkImage](actualNFramebuffers)
-    checkVkResult vkGetSwapchainImagesKHR(vulkan.device, result.vk, addr(actualNFramebuffers), framebuffers.ToCPointer)
+  # create framebuffers
+  var actualNFramebuffers: uint32
+  checkVkResult vkGetSwapchainImagesKHR(vulkan.device, swapchain.vk, addr(actualNFramebuffers), nil)
+  var framebuffers = newSeq[VkImage](actualNFramebuffers)
+  checkVkResult vkGetSwapchainImagesKHR(vulkan.device, swapchain.vk, addr(actualNFramebuffers), framebuffers.ToCPointer)
 
-    for framebuffer in framebuffers:
-      result.framebufferViews.add svkCreate2DImageView(framebuffer, format)
-      if samples == VK_SAMPLE_COUNT_1_BIT:
-        svkCreateFramebuffer(renderPass, width, height, [result.framebufferViews[^1]])
-      else:
-        svkCreateFramebuffer(renderPass, width, height, [result.msaaImageView, result.framebufferViews[^1]])
+  for framebuffer in framebuffers:
+    swapchain.framebufferViews.add svkCreate2DImageView(framebuffer, format)
+    if samples == VK_SAMPLE_COUNT_1_BIT:
+      svkCreateFramebuffer(renderPass, width, height, [swapchain.framebufferViews[^1]])
+    else:
+      svkCreateFramebuffer(renderPass, width, height, [swapchain.msaaImageView, swapchain.framebufferViews[^1]])
 
-    # create sync primitives
-    for i in 0 ..< INFLIGHTFRAMES:
-      result.queueFinishedFence[i] = svkCreateFence(signaled = true)
-      result.imageAvailableSemaphore[i] = svkCreateSemaphore()
-      result.renderFinishedSemaphore[i] = svkCreateSemaphore()
+  # create sync primitives
+  for i in 0 ..< INFLIGHTFRAMES:
+    swapchain.queueFinishedFence[i] = svkCreateFence(signaled = true)
+    swapchain.imageAvailableSemaphore[i] = svkCreateSemaphore()
+    swapchain.renderFinishedSemaphore[i] = svkCreateSemaphore()
+  return some(swapchain)
 
-proc TryAcquireNextImage*(swapchain: var Swapchain): bool =
+proc TryAcquireNextImage*(swapchain: var Swapchain): Option[VkFramebuffer] =
   swapchain.queueFinishedFence[swapchain.currentFiF].Await()
 
   let nextImageResult = vkAcquireNextImageKHR(
@@ -101,7 +106,9 @@
 
   swapchain.queueFinishedFence[swapchain.currentFiF].Reset()
 
-  return nextImageResult == VK_SUCCESS
+  if nextImageResult != VK_SUCCESS:
+    return none(VkFramebuffer)
+  return some(swapchain.framebuffers[swapchain.currentFramebufferIndex])
 
 proc Swap*(swapchain: var Swapchain, queue: Queue, commandBuffer: VkCommandBuffer): bool =
   var
--- a/semicongine/rendering/vulkan_wrappers.nim	Mon Jul 08 23:47:33 2024 +0700
+++ b/semicongine/rendering/vulkan_wrappers.nim	Tue Jul 09 22:53:38 2024 +0700
@@ -25,9 +25,8 @@
   assert score > 0, "Unable to find integrated or discrete GPU"
 
 proc svkGetPhysicalDeviceSurfaceSupportKHR*(queueFamily: uint32): bool =
-  assert surface.Valid
   var presentation = VkBool32(false)
-  checkVkResult vkGetPhysicalDeviceSurfaceSupportKHR(vulkan.device, queueFamily, vulkan.surface, addr(presentation))
+  checkVkResult vkGetPhysicalDeviceSurfaceSupportKHR(vulkan.physicalDevice, queueFamily, vulkan.surface, addr(presentation))
   return bool(presentation)
 
 proc GetQueueFamily(pDevice: VkPhysicalDevice, qType: VkQueueFlagBits): uint32 =
@@ -202,7 +201,7 @@
 
 proc svkCreateSemaphore*(): VkSemaphore =
   var semaphoreInfo = VkSemaphoreCreateInfo(sType: VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO)
-  checkVkResult vkCreateSemaphore(vulkan.deivce, addr(semaphoreInfo), nil, addr(result))
+  checkVkResult vkCreateSemaphore(vulkan.device, addr(semaphoreInfo), nil, addr(result))
 
 proc Await*(fence: VkFence, timeout = high(uint64)) =
   checkVkResult vkWaitForFences(vulkan.device, 1, addr(fence), false, timeout)
--- a/test1.nim	Mon Jul 08 23:47:33 2024 +0700
+++ b/test1.nim	Tue Jul 09 22:53:38 2024 +0700
@@ -75,6 +75,9 @@
 )
 
 let renderpass = CreatePresentationRenderPass()
+var swapchainResult = InitSwapchain(renderpass)
+assert swapchainResult.isSome()
+var swapchain = swapchainResult.get()
 
 # shaders
 var pipeline1 = CreatePipeline[ShaderA](renderPass = renderpass)
@@ -107,6 +110,10 @@
 InitDescriptorSet(renderdata, pipeline1.GetLayoutFor(MaterialSet), uniforms1)
 
 
+
+
+
+
 # command buffer
 var
   commandBufferPool: VkCommandPool