changeset 507:15e78601b390

add: some changes to build on windows host
author Sam <sam@basx.dev>
date Thu, 19 Jan 2023 00:42:40 +0700
parents b159fcdd36b9
children 6b811b45a427
files config.nims src/semicongine/buffer.nim src/semicongine/engine.nim src/semicongine/shader.nim
diffstat 4 files changed, 46 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/config.nims	Thu Jan 19 00:41:41 2023 +0700
+++ b/config.nims	Thu Jan 19 00:42:40 2023 +0700
@@ -42,22 +42,26 @@
 task single_windows_debug, "build windows debug":
   compilerFlags()
   compilerFlagsDebug()
-  # for some the --define:mingw does not work from inside here...
-  # so we need to set it when calling the task and use "/" to prevent
-  # the use of backslash while crosscompiling
-  switch("define", "mingw")
   switch("outdir", BUILDBASE & "/" & DEBUG & "/" & WINDOWS)
   setCommand "c"
   mkDir(BUILDBASE & "/" & DEBUG & "/" & WINDOWS)
 
+
 task single_windows_release, "build windows release":
   compilerFlags()
   compilerFlagsRelease()
   switch("outdir", BUILDBASE & "/" & RELEASE & "/" & WINDOWS)
-  switch("define", "mingw")
   setCommand "c"
   mkDir(BUILDBASE & "/" & RELEASE & "/" & WINDOWS)
 
+task single_crosscompile_windows_debug, "build crosscompile windows debug":
+  switch("define", "mingw")
+  single_windows_debugTask()
+
+task single_crosscompile_windows_release, "build crosscompile windows release":
+  switch("define", "mingw")
+  single_windows_releaseTask()
+
 task build_all_linux_debug, "build all examples with linux/debug":
   for file in listFiles("examples"):
     if file.endsWith(".nim"):
@@ -120,9 +124,6 @@
   exec &"mv {dirname}/bin/glslangValidator.exe examples/"
   exec &"rm -rf {dirname}"
 
-if getCommand() in ["c", "compile", "r", "dump", "check", "idetools"]:
-  compilerFlags()
-
 task run_all , "Run all binaries":
   for file in listFiles("build/debug/linux"):
     exec file
@@ -132,3 +133,6 @@
     exec &"wine {file}"
   for file in listFiles("build/release/windows"):
     exec &"wine {file}"
+
+if getCommand() in ["c", "compile", "r", "dump", "check", "idetools"]:
+  compilerFlags()
--- a/src/semicongine/buffer.nim	Thu Jan 19 00:41:41 2023 +0700
+++ b/src/semicongine/buffer.nim	Thu Jan 19 00:42:40 2023 +0700
@@ -20,14 +20,12 @@
     mapped: pointer
 
 proc trash*(buffer: var Buffer) =
-  assert int64(buffer.vkBuffer) != 0
-  vkDestroyBuffer(buffer.device, buffer.vkBuffer, nil)
-  buffer.vkBuffer = VkBuffer(0)
-  if buffer.size == 0: # for zero-size buffers there are no memory allocations
-    return
-  assert int64(buffer.memory) != 0
-  vkFreeMemory(buffer.device, buffer.memory, nil)
-  buffer.memory = VkDeviceMemory(0)
+  if int64(buffer.vkBuffer) != 0:
+    vkDestroyBuffer(buffer.device, buffer.vkBuffer, nil)
+    buffer.vkBuffer = VkBuffer(0)
+  if int64(buffer.memory) != 0:
+    vkFreeMemory(buffer.device, buffer.memory, nil)
+    buffer.memory = VkDeviceMemory(0)
 
 proc findMemoryType(buffer: Buffer, physicalDevice: VkPhysicalDevice, properties: VkMemoryPropertyFlags): uint32 =
   var physicalProperties: VkPhysicalDeviceMemoryProperties
--- a/src/semicongine/engine.nim	Thu Jan 19 00:41:41 2023 +0700
+++ b/src/semicongine/engine.nim	Thu Jan 19 00:42:40 2023 +0700
@@ -416,6 +416,8 @@
   device.vkDestroySwapchainKHR(swapchain.swapchain, nil)
 
 proc recreateSwapchain(vulkan: Vulkan): (Swapchain, seq[VkFramebuffer]) =
+  if vulkan.frameDimension.width == 0 or vulkan.frameDimension.height == 0:
+    return (vulkan.swapchain, vulkan.framebuffers)
   debug(&"Recreate swapchain with dimension {vulkan.frameDimension}")
   checkVkResult vulkan.device.device.vkDeviceWaitIdle()
 
@@ -538,10 +540,11 @@
       result.indexedVertexBuffers.add createIndexedVertexBuffers(indexedubermesh, result.device, engine.vulkan.device.physicalDevice.device, engine.vulkan.commandPool, engine.vulkan.device.graphicsQueue)
 
   # uniform buffers
-  result.uniformBuffers = createUniformBuffers[MAX_FRAMES_IN_FLIGHT, UniformType](
-    result.device,
-    engine.vulkan.device.physicalDevice.device
-  )
+  when not (UniformType is void):
+    result.uniformBuffers = createUniformBuffers[MAX_FRAMES_IN_FLIGHT, UniformType](
+      result.device,
+      engine.vulkan.device.physicalDevice.device
+    )
 
   var
     poolSize = VkDescriptorPoolSize(
@@ -568,23 +571,24 @@
 
   checkVkResult vkAllocateDescriptorSets(result.device, addr(allocInfo), addr(result.descriptors[0]))
 
-  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,
-      offset: VkDeviceSize(0),
-      range: VkDeviceSize(sizeof(UniformType)),
-    )
-    var descriptorWrite = VkWriteDescriptorSet(
-        sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
-        dstSet: result.descriptors[i],
-        dstBinding: 0,
-        dstArrayElement: 0,
-        descriptorType: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
-        descriptorCount: 1,
-        pBufferInfo: cast[ptr ptr VkDescriptorBufferInfo](addr(bufferInfos[i][0])),
+  when not (UniformType is void):
+    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,
+        offset: VkDeviceSize(0),
+        range: VkDeviceSize(sizeof(UniformType)),
       )
-    vkUpdateDescriptorSets(result.device, 1, addr(descriptorWrite), 0, nil)
+      var descriptorWrite = VkWriteDescriptorSet(
+          sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
+          dstSet: result.descriptors[i],
+          dstBinding: 0,
+          dstArrayElement: 0,
+          descriptorType: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
+          descriptorCount: 1,
+          pBufferInfo: cast[ptr ptr VkDescriptorBufferInfo](addr(bufferInfos[i][0])),
+        )
+      vkUpdateDescriptorSets(result.device, 1, addr(descriptorWrite), 0, nil)
 
 
 proc runPipeline[VertexType; Uniforms](commandBuffer: VkCommandBuffer, pipeline: var RenderPipeline[VertexType, Uniforms], currentFrame: int) =
--- a/src/semicongine/shader.nim	Thu Jan 19 00:41:41 2023 +0700
+++ b/src/semicongine/shader.nim	Thu Jan 19 00:42:40 2023 +0700
@@ -39,17 +39,14 @@
     stagename = stage2string(stage)
     shaderHash = hash(shaderSource)
     # cross compilation for windows workaround, sorry computer
-    shaderout = getTempDir().replace("\\", "/") & "/" & fmt"shader_{shaderHash}.{stagename}"
+    shaderfile = getTempDir().replace("\\", "/") & "/" & fmt"shader_{shaderHash}.{stagename}"
     projectPath = querySetting(projectPath)
 
-  let (output, exitCode_glsl) = gorgeEx(command=fmt"{projectPath}/glslangValidator --entry-point {entrypoint} -V --stdin -S {stagename} -o {shaderout}", input=shaderSource)
+  let (output, exitCode_glsl) = gorgeEx(command=fmt"{projectPath}/glslangValidator --entry-point {entrypoint} -V --stdin -S {stagename} -o {shaderfile}", input=shaderSource)
   if exitCode_glsl != 0:
     raise newException(Exception, output)
-  let shaderbinary = staticRead shaderout
-
-  let (output_rm, exitCode_rm) = gorgeEx(command=fmt"rm {shaderout}")
-  if exitCode_rm != 0:
-    raise newException(Exception, output_rm)
+  let shaderbinary = staticRead shaderfile
+  removeFile(shaderfile)
 
   var i = 0
   while i < shaderbinary.len: