changeset 107:2d0351a68a4e

add: working render loop
author Sam <sam@basx.dev>
date Mon, 27 Mar 2023 21:01:32 +0700
parents 9c3e3dcb0328
children 70f92081d2c1
files src/semicongine/vulkan/device.nim src/semicongine/vulkan/pipeline.nim src/semicongine/vulkan/renderpass.nim src/semicongine/vulkan/swapchain.nim src/semicongine/vulkan/utils.nim tests/test_vulkan_wrapper.nim
diffstat 6 files changed, 33 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/src/semicongine/vulkan/device.nim	Sun Mar 26 01:56:19 2023 +0700
+++ b/src/semicongine/vulkan/device.nim	Mon Mar 27 21:01:32 2023 +0700
@@ -18,6 +18,9 @@
     presentation: bool
     graphics: bool
 
+proc `$`*(device: Device): string =
+  "vk: " & $device.vk & "physicalDevice: " & $device.physicalDevice
+
 proc createDevice*(
   instance: Instance,
   physicalDevice: PhysicalDevice,
--- a/src/semicongine/vulkan/pipeline.nim	Sun Mar 26 01:56:19 2023 +0700
+++ b/src/semicongine/vulkan/pipeline.nim	Mon Mar 27 21:01:32 2023 +0700
@@ -103,11 +103,9 @@
     )
     colorBlending = VkPipelineColorBlendStateCreateInfo(
       sType: VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
-      logicOpEnable: VK_TRUE,
-      logicOp: VK_LOGIC_OP_COPY,
+      logicOpEnable: false,
       attachmentCount: 1,
       pAttachments: addr(colorBlendAttachment),
-      blendConstants: [0.0'f, 0.0'f, 0.0'f, 0.0'f],
     )
     dynamicStates = @[VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR]
     dynamicState = VkPipelineDynamicStateCreateInfo(
@@ -135,7 +133,7 @@
       basePipelineIndex: -1,
     )
   checkVkResult vkCreateGraphicsPipelines(
-    renderpass.device.vk,
+    renderPass.device.vk,
     VkPipelineCache(0),
     1,
     addr(createInfo),
--- a/src/semicongine/vulkan/renderpass.nim	Sun Mar 26 01:56:19 2023 +0700
+++ b/src/semicongine/vulkan/renderpass.nim	Mon Mar 27 21:01:32 2023 +0700
@@ -22,14 +22,17 @@
 
 proc createRenderPass*(
   device: Device,
-  attachments: var seq[VkAttachmentDescription],
-  subpasses: var seq[Subpass],
-  dependencies: var seq[VkSubpassDependency]
+  attachments: seq[VkAttachmentDescription],
+  subpasses: seq[Subpass],
+  dependencies: seq[VkSubpassDependency]
 ): RenderPass =
   assert device.vk.valid
+  var pAttachments = attachments
+  var pSubpasses = subpasses
+  var pDependencies = dependencies
 
-  var subpassesList = newSeq[VkSubpassDescription](subpasses.len)
-  for subpass in subpasses.mitems:
+  var subpassesList: seq[VkSubpassDescription]
+  for subpass in pSubpasses.mitems:
     subpassesList.add VkSubpassDescription(
       flags: subpass.flags,
       pipelineBindPoint: subpass.pipelineBindPoint,
@@ -45,52 +48,29 @@
 
   var createInfo = VkRenderPassCreateInfo(
       sType: VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
-      attachmentCount: uint32(attachments.len),
-      pAttachments: attachments.toCPointer,
+      attachmentCount: uint32(pAttachments.len),
+      pAttachments: pAttachments.toCPointer,
       subpassCount: uint32(subpassesList.len),
       pSubpasses: subpassesList.toCPointer,
-      dependencyCount: uint32(dependencies.len),
-      pDependencies: dependencies.toCPointer,
+      dependencyCount: uint32(pDependencies.len),
+      pDependencies: pDependencies.toCPointer,
     )
   result.device = device
-  result.subpasses = subpasses
+  result.subpasses = pSubpasses
   checkVkResult device.vk.vkCreateRenderPass(addr(createInfo), nil, addr(result.vk))
 
-proc createRenderAttachment(
-  format: VkFormat,
-  flags = VkAttachmentDescriptionFlags(0),
-  samples = VK_SAMPLE_COUNT_1_BIT,
-  loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
-  storeOp = VK_ATTACHMENT_STORE_OP_STORE,
-  stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
-  stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
-  initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
-  finalLayout = VK_IMAGE_LAYOUT_GENERAL,
-): auto =
-  VkAttachmentDescription(
-    format: format,
-    flags: flags,
-    samples: samples,
-    loadOp: loadOp,
-    storeOp: storeOp,
-    stencilLoadOp: stencilLoadOp,
-    stencilStoreOp: stencilStoreOp,
-    initialLayout: initialLayout,
-    finalLayout: finalLayout,
-  )
-
 proc simpleForwardRenderPass*(device: Device, format: VkFormat, clearColor=Vec4([0.5'f32, 0.5'f32, 0.5'f32, 1'f32])): RenderPass =
   assert device.vk.valid
   var
-    attachments = @[createRenderAttachment(
-      format = format,
-      samples = VK_SAMPLE_COUNT_1_BIT,
-      loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
-      storeOp = VK_ATTACHMENT_STORE_OP_STORE,
-      stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
-      stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
-      initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
-      finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
+    attachments = @[VkAttachmentDescription(
+        format: format,
+        samples: VK_SAMPLE_COUNT_1_BIT,
+        loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR,
+        storeOp: VK_ATTACHMENT_STORE_OP_STORE,
+        stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
+        stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
+        initialLayout: VK_IMAGE_LAYOUT_UNDEFINED,
+        finalLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
     )]
     subpasses = @[
       Subpass(
@@ -99,17 +79,8 @@
         clearColor: clearColor
       )
     ]
-    dependencies = @[
-      VkSubpassDependency(
-        srcSubpass: VK_SUBPASS_EXTERNAL,
-        dstSubpass: 0,
-        srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT],
-        srcAccessMask: VkAccessFlags(0),
-        dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT],
-        dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT],
-      )
-    ]
-  device.createRenderPass(attachments=attachments, subpasses=subpasses, dependencies=dependencies)
+    dependencies: seq[VkSubpassDependency]
+  result = device.createRenderPass(attachments=attachments, subpasses=subpasses, dependencies=dependencies)
 
 proc destroy*(renderpass: var RenderPass) =
   assert renderpass.device.vk.valid
--- a/src/semicongine/vulkan/swapchain.nim	Sun Mar 26 01:56:19 2023 +0700
+++ b/src/semicongine/vulkan/swapchain.nim	Mon Mar 27 21:01:32 2023 +0700
@@ -127,8 +127,9 @@
   renderCommands(
     swapchain.commandBufferPool.buffers[swapchain.currentInFlight],
     swapchain.renderpass,
-    swapchain.framebuffers[swapChain.currentInFlight]
+    swapchain.framebuffers[currentFramebufferIndex]
   ):
+    swapchain.commandBufferPool.buffers[swapchain.currentInFlight].vkCmdBindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.vk)
     echo "TODO: Draw calls here"
 
   var
--- a/src/semicongine/vulkan/utils.nim	Sun Mar 26 01:56:19 2023 +0700
+++ b/src/semicongine/vulkan/utils.nim	Mon Mar 27 21:01:32 2023 +0700
@@ -1,5 +1,4 @@
 import std/strutils
-import std/os
 import std/strformat
 
 func cleanString*(str: openArray[char]): string =
--- a/tests/test_vulkan_wrapper.nim	Sun Mar 26 01:56:19 2023 +0700
+++ b/tests/test_vulkan_wrapper.nim	Mon Mar 27 21:01:32 2023 +0700
@@ -64,14 +64,13 @@
     selectedPhysicalDevice.filterForGraphicsPresentationQueues()
   )
 
-  # setup render pipeline
   var surfaceFormat = device.physicalDevice.getSurfaceFormats().filterSurfaceFormat()
   var renderpass = device.simpleForwardRenderPass(surfaceFormat.format)
+  echo renderpass
   var (swapchain, res) = device.createSwapchain(renderpass, surfaceFormat, device.firstGraphicsQueue().get().family)
   if res != VK_SUCCESS:
     raise newException(Exception, "Unable to create swapchain")
 
-  # todo: could be create inside "device", but it would be nice to have nim v2 with support for circular dependencies first
   const vertexBinary = shaderCode[Vertex, Uniforms, FragmentInput](stage=VK_SHADER_STAGE_VERTEX_BIT, version=450, entrypoint="main", "fragpos = pos;")
   const fragmentBinary = shaderCode[FragmentInput, void, Pixel](stage=VK_SHADER_STAGE_FRAGMENT_BIT, version=450, entrypoint="main", "color = vec4(1, 1, 1, 0);")
   var
@@ -82,7 +81,7 @@
     descriptorSet = descriptorPool.allocateDescriptorSet(pipeline.descriptorSetLayout, 1)
 
   echo "All successfull"
-  echo swapchain.drawNextFrame(pipeline)
+  discard swapchain.drawNextFrame(pipeline)
   echo "Start cleanup"
 
   # cleanup