comparison src/zamikongine/shader.nim @ 38:c3c963e7c1a6

did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
author Sam <sam@basx.dev>
date Wed, 18 Jan 2023 09:52:03 +0700
parents 94c38e4b5782
children
comparison
equal deleted inserted replaced
37:6859bcfabc62 38:c3c963e7c1a6
1 import std/os
2 import std/hashes
1 import std/strformat 3 import std/strformat
2 import std/strutils 4 import std/strutils
3 import std/tables 5 import std/tables
4 import std/compilesettings 6 import std/compilesettings
5
6 7
7 import ./vulkan_helpers 8 import ./vulkan_helpers
8 import ./glsl_helpers 9 import ./glsl_helpers
9 import ./vulkan 10 import ./vulkan
10 import ./vertex 11 import ./vertex
29 of VK_SHADER_STAGE_FRAGMENT_BIT: "frag" 30 of VK_SHADER_STAGE_FRAGMENT_BIT: "frag"
30 of VK_SHADER_STAGE_ALL_GRAPHICS: "" 31 of VK_SHADER_STAGE_ALL_GRAPHICS: ""
31 of VK_SHADER_STAGE_COMPUTE_BIT: "comp" 32 of VK_SHADER_STAGE_COMPUTE_BIT: "comp"
32 of VK_SHADER_STAGE_ALL: "" 33 of VK_SHADER_STAGE_ALL: ""
33 34
34 proc compileGLSLToSPIRV(stage: VkShaderStageFlagBits, shaderSource: string, entrypoint: string): seq[uint32] {.compileTime.} = 35 proc compileGLSLToSPIRV(stage: static VkShaderStageFlagBits, shaderSource: static string, entrypoint: string): seq[uint32] {.compileTime.} =
35 let stagename = stage2string(stage) 36 const
37 stagename = stage2string(stage)
38 shaderHash = hash(shaderSource)
39 # cross compilation for windows workaround, sorry computer
40 shaderout = getTempDir().replace("\\", "/") & "/" & fmt"shader_{shaderHash}.{stagename}"
41 projectPath = querySetting(projectPath)
36 42
37 # TODO: compiles only on linux for now (because we don't have compile-time functionality in std/tempfile) 43 let (output, exitCode_glsl) = gorgeEx(command=fmt"{projectPath}/glslangValidator --entry-point {entrypoint} -V --stdin -S {stagename} -o {shaderout}", input=shaderSource)
38 let (tmpfile, exitCode) = gorgeEx(command=fmt"mktemp --tmpdir shader_XXXXXXX.{stagename}")
39 if exitCode != 0:
40 raise newException(Exception, tmpfile)
41
42 let (output, exitCode_glsl) = gorgeEx(command=fmt"{querySetting(projectPath)}/glslangValidator --entry-point {entrypoint} -V --stdin -S {stagename} -o {tmpfile}", input=shaderSource)
43 if exitCode_glsl != 0: 44 if exitCode_glsl != 0:
44 raise newException(Exception, output) 45 raise newException(Exception, output)
45 let shaderbinary = staticRead tmpfile 46 if output == "": # this happens when the nim was invoked with "check" instead of "compile/c", as it prevents the gorgeEx command to really run. However, there is hope, see https://github.com/nim-lang/RFCs/issues/430
47 return result
48 let shaderbinary = staticRead shaderout
46 49
47 let (output_rm, exitCode_rm) = gorgeEx(command=fmt"rm {tmpfile}") 50 let (output_rm, exitCode_rm) = gorgeEx(command=fmt"rm {shaderout}")
48 if exitCode_rm != 0: 51 if exitCode_rm != 0:
49 raise newException(Exception, output_rm) 52 raise newException(Exception, output_rm)
50 53
51 var i = 0 54 var i = 0
52 while i < shaderbinary.len: 55 while i < shaderbinary.len: