comparison src/zamikongine/shader.nim @ 25:8f290112718a

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 71bbe11d8de8
children b1b05d4efb52
comparison
equal deleted inserted replaced
24:71bbe11d8de8 25:8f290112718a
1 import std/tempfiles
2 import std/strformat 1 import std/strformat
3 import std/strutils 2 import std/strutils
4 import std/tables 3 import std/tables
5 import std/compilesettings 4 import std/compilesettings
6 5
25 of VK_SHADER_STAGE_ALL_GRAPHICS: "" 24 of VK_SHADER_STAGE_ALL_GRAPHICS: ""
26 of VK_SHADER_STAGE_COMPUTE_BIT: "comp" 25 of VK_SHADER_STAGE_COMPUTE_BIT: "comp"
27 of VK_SHADER_STAGE_ALL: "" 26 of VK_SHADER_STAGE_ALL: ""
28 27
29 proc compileGLSLToSPIRV(stage: VkShaderStageFlagBits, shaderSource: string, entrypoint: string): seq[uint32] {.compileTime.} = 28 proc compileGLSLToSPIRV(stage: VkShaderStageFlagBits, shaderSource: string, entrypoint: string): seq[uint32] {.compileTime.} =
29 # TODO: compiles only on linux for now (because we don't have compile-time functionality in std/tempfile)
30 let stagename = stage2string(stage) 30 let stagename = stage2string(stage)
31 let (tmpfile, tmpfilename) = createTempFile("shader", stage2string(stage)) 31
32 tmpfile.close() 32 let (tmpfile, exitCode) = gorgeEx(command=fmt"mktemp --tmpdir shader_XXXXXXX.{stagename}")
33 let (output, exitCode) = gorgeEx(command=fmt"{querySetting(projectPath)}/glslangValidator --entry-point {entrypoint} -V --stdin -S {stagename} -o {tmpfilename}", input=shaderSource)
34 if exitCode != 0: 33 if exitCode != 0:
34 raise newException(Exception, tmpfile)
35
36 let (output, exitCode_glsl) = gorgeEx(command=fmt"{querySetting(projectPath)}/glslangValidator --entry-point {entrypoint} -V --stdin -S {stagename} -o {tmpfile}", input=shaderSource)
37 if exitCode_glsl != 0:
35 raise newException(Exception, output) 38 raise newException(Exception, output)
36 let shaderbinary = staticRead tmpfilename 39 let shaderbinary = staticRead tmpfile
40
41 let (output_rm, exitCode_rm) = gorgeEx(command=fmt"rm {tmpfile}")
42 if exitCode_rm != 0:
43 raise newException(Exception, output_rm)
44
37 var i = 0 45 var i = 0
38 while i < shaderbinary.len: 46 while i < shaderbinary.len:
39 result.add( 47 result.add(
40 (uint32(shaderbinary[i + 0]) shl 0) or 48 (uint32(shaderbinary[i + 0]) shl 0) or
41 (uint32(shaderbinary[i + 1]) shl 8) or 49 (uint32(shaderbinary[i + 1]) shl 8) or