diff src/zamikongine/shader.nim @ 486:9231df12b222

fix: build from scratch not working, remove temp shader files from compilation
author Sam <sam@basx.dev>
date Wed, 11 Jan 2023 11:43:22 +0700
parents b4a972bd37d5
children 54a1f8ee208e
line wrap: on
line diff
--- a/src/zamikongine/shader.nim	Wed Jan 11 11:15:02 2023 +0700
+++ b/src/zamikongine/shader.nim	Wed Jan 11 11:43:22 2023 +0700
@@ -1,4 +1,3 @@
-import std/tempfiles
 import std/strformat
 import std/strutils
 import std/tables
@@ -27,13 +26,22 @@
   of VK_SHADER_STAGE_ALL: ""
 
 proc compileGLSLToSPIRV(stage: VkShaderStageFlagBits, shaderSource: string, entrypoint: string): seq[uint32] {.compileTime.} =
+  # TODO: compiles only on linux for now (because we don't have compile-time functionality in std/tempfile)
   let stagename = stage2string(stage)
-  let (tmpfile, tmpfilename) = createTempFile("shader", stage2string(stage))
-  tmpfile.close()
-  let (output, exitCode) = gorgeEx(command=fmt"{querySetting(projectPath)}/glslangValidator --entry-point {entrypoint} -V --stdin -S {stagename} -o {tmpfilename}", input=shaderSource)
+
+  let (tmpfile, exitCode) = gorgeEx(command=fmt"mktemp --tmpdir shader_XXXXXXX.{stagename}")
   if exitCode != 0:
+    raise newException(Exception, tmpfile)
+
+  let (output, exitCode_glsl) = gorgeEx(command=fmt"{querySetting(projectPath)}/glslangValidator --entry-point {entrypoint} -V --stdin -S {stagename} -o {tmpfile}", input=shaderSource)
+  if exitCode_glsl != 0:
     raise newException(Exception, output)
-  let shaderbinary = staticRead tmpfilename
+  let shaderbinary = staticRead tmpfile
+
+  let (output_rm, exitCode_rm) = gorgeEx(command=fmt"rm {tmpfile}")
+  if exitCode_rm != 0:
+    raise newException(Exception, output_rm)
+
   var i = 0
   while i < shaderbinary.len:
     result.add(