changeset 565:a0b01e84607d

did: some refactoring
author Sam <sam@basx.dev>
date Mon, 20 Mar 2023 23:48:46 +0700
parents 20837928dc63
children cc7ba46fe3c4
files src/semicongine/engine.nim src/semicongine/vulkan/pipeline.nim src/semicongine/vulkan/shader.nim tests/test_vulkan_wrapper.nim
diffstat 4 files changed, 41 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/src/semicongine/engine.nim	Mon Mar 20 10:25:50 2023 +0700
+++ b/src/semicongine/engine.nim	Mon Mar 20 23:48:46 2023 +0700
@@ -572,8 +572,7 @@
 
   # uniform buffers
   when not (UniformType is void):
-    result.uniformBuffers = createUniformBuffers[MAX_FRAMES_IN_FLIGHT,
-        UniformType](
+    result.uniformBuffers = createUniformBuffers[MAX_FRAMES_IN_FLIGHT, UniformType](
       result.device,
       engine.vulkan.device.physicalDevice.device
     )
@@ -589,8 +588,7 @@
       pPoolSizes: addr(poolSize),
       maxSets: uint32(MAX_FRAMES_IN_FLIGHT),
     )
-  checkVkResult vkCreateDescriptorPool(result.device, addr(poolInfo), nil, addr(
-      result.descriptorPool))
+  checkVkResult vkCreateDescriptorPool(result.device, addr(poolInfo), nil, addr(result.descriptorPool))
 
   var layouts: array[MAX_FRAMES_IN_FLIGHT, VkDescriptorSetLayout]
   for i in 0 ..< MAX_FRAMES_IN_FLIGHT:
@@ -602,12 +600,10 @@
     pSetLayouts: addr(layouts[0]),
   )
 
-  checkVkResult vkAllocateDescriptorSets(result.device, addr(allocInfo), addr(
-      result.descriptors[0]))
+  checkVkResult vkAllocateDescriptorSets(result.device, addr(allocInfo), addr(result.descriptors[0]))
 
   when not (UniformType is void):
-    var bufferInfos: array[MAX_FRAMES_IN_FLIGHT, array[1,
-        VkDescriptorBufferInfo]] # because we use only one Uniform atm
+    var bufferInfos: array[MAX_FRAMES_IN_FLIGHT, array[1, VkDescriptorBufferInfo]] # because we use only one Uniform atm
     for i in 0 ..< MAX_FRAMES_IN_FLIGHT:
       bufferInfos[i][0] = VkDescriptorBufferInfo(
         buffer: result.uniformBuffers[i].vkBuffer,
--- a/src/semicongine/vulkan/pipeline.nim	Mon Mar 20 10:25:50 2023 +0700
+++ b/src/semicongine/vulkan/pipeline.nim	Mon Mar 20 23:48:46 2023 +0700
@@ -16,6 +16,8 @@
 proc createPipeline*[VertexShader: Shader, FragmentShader: Shader](renderPass: RenderPass, vertexShader: VertexShader, fragmentShader: FragmentShader): Pipeline =
   assert renderPass.vk.valid
   assert renderPass.device.vk.valid
+  assert vertexShader.stage == VK_SHADER_STAGE_VERTEX_BIT
+  assert fragmentShader.stage == VK_SHADER_STAGE_FRAGMENT_BIT
   result.device = renderPass.device
 
   var descriptorType: VkDescriptorType
--- a/src/semicongine/vulkan/shader.nim	Mon Mar 20 10:25:50 2023 +0700
+++ b/src/semicongine/vulkan/shader.nim	Mon Mar 20 23:48:46 2023 +0700
@@ -20,6 +20,7 @@
 type
   Shader*[Inputs, Uniforms, Outputs] = object
     device: Device
+    stage*: VkShaderStageFlagBits
     vk*: VkShaderModule
     entrypoint*: string
 
@@ -46,12 +47,13 @@
     shaderfile = getTempDir() / &"shader_{shaderHash}.{stagename}"
     projectPath = querySetting(projectPath)
 
-  echo "shader of type ", stage
+  echo "shader of type ", stage, ", entrypoint ", entrypoint
   for i, line in enumerate(shaderSource.splitlines()):
     echo "  ", i + 1, " ", line
+  let command = &"{projectPath}/glslangValidator --entry-point {entrypoint} -V --stdin -S {stagename} -o {shaderfile}"
 
   discard staticExecChecked(
-      command = &"{projectPath}/glslangValidator --entry-point {entrypoint} -V --stdin -S {stagename} -o {shaderfile}",
+      command = command,
       input = shaderSource
   )
 
@@ -71,7 +73,7 @@
     i += 4
 
 
-proc shaderCode*[Inputs, Uniforms, Outputs](shadertype: VkShaderStageFlagBits, version: int, entrypoint: string, body: seq[string]): seq[uint32] {.compileTime.} =
+proc shaderCode*[Inputs, Uniforms, Outputs](stage: VkShaderStageFlagBits, version: int, entrypoint: string, body: seq[string]): seq[uint32] {.compileTime.} =
   var code = @[&"#version {version}", ""] &
     glslInput[Inputs]() & @[""] &
     glslUniforms[Uniforms]() & @[""] &
@@ -79,19 +81,20 @@
     @[&"void {entrypoint}(){{"] &
     body &
     @[&"}}"]
-  compileGLSLToSPIRV(shadertype, code.join("\n"), entrypoint)
+  compileGLSLToSPIRV(stage, code.join("\n"), entrypoint)
 
 
-proc shaderCode*[Inputs, Uniforms, Outputs](shadertype: VkShaderStageFlagBits, version: int, entrypoint: string, body: string): seq[uint32] {.compileTime.} =
-  return shaderCode[Inputs, Uniforms, Outputs](shadertype, version, entrypoint, @[body])
+proc shaderCode*[Inputs, Uniforms, Outputs](stage: VkShaderStageFlagBits, version: int, entrypoint: string, body: string): seq[uint32] {.compileTime.} =
+  return shaderCode[Inputs, Uniforms, Outputs](stage, version, entrypoint, @[body])
 
 
-proc createShader*[Inputs, Uniforms, Outputs](device: Device, entrypoint: string, binary: seq[uint32]): Shader[Inputs, Uniforms, Outputs] =
+proc createShader*[Inputs, Uniforms, Outputs](device: Device, stage: VkShaderStageFlagBits, entrypoint: string, binary: seq[uint32]): Shader[Inputs, Uniforms, Outputs] =
   assert device.vk.valid
   assert len(binary) > 0
 
   result.device = device
   result.entrypoint = entrypoint
+  result.stage = stage
   var bin = binary
   var createInfo = VkShaderModuleCreateInfo(
     sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
@@ -103,7 +106,7 @@
 proc getPipelineInfo*(shader: Shader): VkPipelineShaderStageCreateInfo =
   VkPipelineShaderStageCreateInfo(
     sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
-    stage: VK_SHADER_STAGE_VERTEX_BIT,
+    stage: shader.stage,
     module: shader.vk,
     pName: cstring(shader.entrypoint),
   )
--- a/tests/test_vulkan_wrapper.nim	Mon Mar 20 10:25:50 2023 +0700
+++ b/tests/test_vulkan_wrapper.nim	Mon Mar 20 23:48:46 2023 +0700
@@ -14,25 +14,7 @@
   Pixel = object
     color: Vec4
 
-
-when isMainModule:
-  # print basic driver infos
-  echo "Layers"
-  for layer in getLayers():
-    echo "  " & layer
-  echo "Instance extensions"
-  for extension in getInstanceExtensions():
-    echo "  " & extension
-
-  # create instance
-  var thewindow = createWindow("Test")
-  var instance = thewindow.createInstance(
-    vulkanVersion=VK_MAKE_API_VERSION(0, 1, 3, 0),
-    instanceExtensions= @["VK_EXT_debug_utils"],
-    layers= @["VK_LAYER_KHRONOS_validation"]
-  )
-  var debugger = instance.createDebugMessenger()
-
+proc diagnostics(instance: Instance) =
   # diagnostic output
   echo "Devices"
   for device in instance.getPhysicalDevices():
@@ -55,6 +37,24 @@
     for format in device.getSurfaceFormats():
       echo "    " & $format
 
+when isMainModule:
+  # print basic driver infos
+  echo "Layers"
+  for layer in getLayers():
+    echo "  " & layer
+  echo "Instance extensions"
+  for extension in getInstanceExtensions():
+    echo "  " & extension
+
+  # create instance
+  var thewindow = createWindow("Test")
+  var instance = thewindow.createInstance(
+    vulkanVersion=VK_MAKE_API_VERSION(0, 1, 3, 0),
+    instanceExtensions= @["VK_EXT_debug_utils"],
+    layers= @["VK_LAYER_KHRONOS_validation"]
+  )
+  var debugger = instance.createDebugMessenger()
+
   # create devices
   let selectedPhysicalDevice = instance.getPhysicalDevices().filterBestGraphics()
   var device = instance.createDevice(
@@ -80,10 +80,10 @@
     renderFinished = device.createSemaphore()
     inflight = device.createFence()
 
-  const vertexBinary = shaderCode[Vertex, Uniforms, FragmentInput](shadertype=VK_SHADER_STAGE_VERTEX_BIT, version=450, entrypoint="main", "fragpos = pos;")
-  const fragmentBinary = shaderCode[FragmentInput, void, Pixel](shadertype=VK_SHADER_STAGE_FRAGMENT_BIT, version=450, entrypoint="main", "color = vec4(1, 1, 1, 0);")
-  var vertexshader = createShader[Vertex, Uniforms, FragmentInput](device, "main", vertexBinary)
-  var fragmentshader = createShader[FragmentInput, void, Pixel](device, "main", fragmentBinary)
+  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 vertexshader = createShader[Vertex, Uniforms, FragmentInput](device, VK_SHADER_STAGE_VERTEX_BIT, "main", vertexBinary)
+  var fragmentshader = createShader[FragmentInput, void, Pixel](device, VK_SHADER_STAGE_FRAGMENT_BIT, "main", fragmentBinary)
 
   var pipeline = renderpass.createPipeline(vertexshader, fragmentshader)
 
@@ -91,11 +91,9 @@
   echo "Start cleanup"
 
   # cleanup
-  #pipeline.destroy()
-  #vertexshader.destroy()
-  #fragmentshader.destroy()
   vertexshader.destroy()
   fragmentshader.destroy()
+  pipeline.destroy()
   inflight.destroy()
   imageAvailable.destroy()
   renderFinished.destroy()