Mercurial > games > semicongine
changeset 485:b4a972bd37d5
did: change shader compilation to run during program compilation, maybe add dynamic version later
author | Sam <sam@basx.dev> |
---|---|
date | Wed, 11 Jan 2023 11:15:02 +0700 |
parents | 04605ca6dd8a |
children | 9231df12b222 |
files | Makefile config.nims examples/hello_triangle.nim src/zamikongine/engine.nim src/zamikongine/math/matrix.nim src/zamikongine/shader.nim |
diffstat | 6 files changed, 33 insertions(+), 69 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile Wed Jan 11 00:43:43 2023 +0700 +++ b/Makefile Wed Jan 11 11:15:02 2023 +0700 @@ -1,30 +1,29 @@ SOURCES := $(shell find src -name '*.nim') # build hello_triangle -build/debug/linux/hello_triangle: ${SOURCES} +build/debug/linux/hello_triangle: ${SOURCES} thirdparty/bin/linux nim build_linux_debug -o:$@ examples/hello_triangle.nim -build/release/linux/hello_triangle: ${SOURCES} +build/release/linux/hello_triangle: ${SOURCES} thirdparty/bin/linux nim build_linux_release -o:$@ examples/hello_triangle.nim -build/debug/windows/hello_triangle.exe: ${SOURCES} build/nim_windows - # ${WINE_NIM} build_windows_debug -o:$@ examples/hello_triangle.nim +build/debug/windows/hello_triangle.exe: ${SOURCES} thirdparty/bin/windows nim build_windows_debug -o:$@ examples/hello_triangle.nim -build/release/windows/hello_triangle.exe: ${SOURCES} build/nim_windows +build/release/windows/hello_triangle.exe: ${SOURCES} thirdparty/bin/windows nim build_windows_release -o:$@ examples/hello_triangle.nim -# build alotof_triangles -build/debug/linux/alotof_triangles: ${SOURCES} - nim build_linux_debug -o:$@ examples/alotof_triangles.nim -build/release/linux/alotof_triangles: ${SOURCES} - nim build_linux_release -o:$@ examples/alotof_triangles.nim -build/debug/windows/alotof_triangles.exe: ${SOURCES} build/nim_windows - nim build_windows_debug -o:$@ examples/alotof_triangles.nim -build/release/windows/alotof_triangles.exe: ${SOURCES} build/nim_windows - nim build_windows_release -o:$@ examples/alotof_triangles.nim - build_all_linux_hello_triangle: build/debug/linux/hello_triangle build/release/linux/hello_triangle build_all_windows_hello_triangle: build/debug/windows/hello_triangle.exe build/release/windows/hello_triangle.exe build_all_hello_triangle: build_all_linux_hello_triangle build_all_windows_hello_triangle +# build alotof_triangles +build/debug/linux/alotof_triangles: ${SOURCES} thirdparty/bin/linux + nim build_linux_debug -o:$@ examples/alotof_triangles.nim +build/release/linux/alotof_triangles: ${SOURCES} thirdparty/bin/linux + nim build_linux_release -o:$@ examples/alotof_triangles.nim +build/debug/windows/alotof_triangles.exe: ${SOURCES} thirdparty/bin/windows + nim build_windows_debug -o:$@ examples/alotof_triangles.nim +build/release/windows/alotof_triangles.exe: ${SOURCES} thirdparty/bin/windows + nim build_windows_release -o:$@ examples/alotof_triangles.nim + build_all_linux_alotof_triangles: build/debug/linux/alotof_triangles build/release/linux/alotof_triangles build_all_windows_alotof_triangles: build/debug/windows/alotof_triangles.exe build/release/windows/alotof_triangles.exe build_all_alotof_triangles: build_all_linux_alotof_triangles build_all_windows_alotof_triangles
--- a/config.nims Wed Jan 11 00:43:43 2023 +0700 +++ b/config.nims Wed Jan 11 11:15:02 2023 +0700 @@ -19,16 +19,6 @@ switch("checks", "off") switch("assertions", "off") -proc compilerFlagsDebugWindows() = - switch("cc", "vcc") - switch("passC", "/MDd") - switch("passL", "ucrtd.lib") - -proc compilerFlagsReleaseWindows() = - switch("cc", "vcc") - switch("passC", "/MD") - switch("passL", "ucrt.lib") - task build_linux_debug, "build linux debug": compilerFlags() compilerFlagsDebug() @@ -44,7 +34,6 @@ task build_windows_debug, "build windows debug": compilerFlags() compilerFlagsDebug() - # compilerFlagsDebugWindows() switch("define", "mingw") buildbase.joinPath("debug/windows").mkDir() setCommand "c" @@ -52,7 +41,6 @@ task build_windows_release, "build windows release": compilerFlags() compilerFlagsRelease() - # compilerFlagsReleaseWindows() switch("define", "mingw") buildbase.joinPath("release/windows").mkDir() setCommand "c"
--- a/examples/hello_triangle.nim Wed Jan 11 00:43:43 2023 +0700 +++ b/examples/hello_triangle.nim Wed Jan 11 11:15:02 2023 +0700 @@ -14,14 +14,14 @@ # vertex data (types must match the above VertexAttributes) const triangle_pos = @[ - Vec2([-0.5'f32, -0.5'f32]), + Vec2([ 0.0'f32, -0.5'f32]), Vec2([ 0.5'f32, 0.5'f32]), Vec2([-0.5'f32, 0.5'f32]), ] triangle_color = @[ - Vec3([1.0'f32, 1.0'f32, 0.0'f32]), + Vec3([1.0'f32, 0.0'f32, 0.0'f32]), Vec3([0.0'f32, 1.0'f32, 0.0'f32]), - Vec3([0.0'f32, 1.0'f32, 1.0'f32]), + Vec3([0.0'f32, 0.0'f32, 1.0'f32]), ] when isMainModule:
--- a/src/zamikongine/engine.nim Wed Jan 11 00:43:43 2023 +0700 +++ b/src/zamikongine/engine.nim Wed Jan 11 11:15:02 2023 +0700 @@ -1,4 +1,3 @@ -import std/sequtils import std/typetraits import std/strformat import std/enumerate @@ -247,7 +246,7 @@ ) checkVkResult device.vkCreateRenderPass(addr(renderPassCreateInfo), nil, addr(result)) -proc setupRenderPipeline[T](device: VkDevice, frameDimension: VkExtent2D, renderPass: VkRenderPass, vertexShader, fragmentShader: string): RenderPipeline = +proc setupRenderPipeline[T](device: VkDevice, frameDimension: VkExtent2D, renderPass: VkRenderPass, vertexShader, fragmentShader: static string): RenderPipeline = # load shaders result.shaders.add(device.initShaderProgram(VK_SHADER_STAGE_VERTEX_BIT, vertexShader)) result.shaders.add(device.initShaderProgram(VK_SHADER_STAGE_FRAGMENT_BIT, fragmentShader)) @@ -489,7 +488,7 @@ ) = result.vulkan.device.device.setupSyncPrimitives() -proc setupPipeline*[T: object, U: uint16|uint32](engine: var Engine, scenedata: ref Thing, vertexShader, fragmentShader: string) = +proc setupPipeline*[T: object, U: uint16|uint32](engine: var Engine, scenedata: ref Thing, vertexShader, fragmentShader: static string) = engine.currentscenedata = scenedata engine.vulkan.pipeline = setupRenderPipeline[T]( engine.vulkan.device.device,
--- a/src/zamikongine/math/matrix.nim Wed Jan 11 00:43:43 2023 +0700 +++ b/src/zamikongine/math/matrix.nim Wed Jan 11 11:15:02 2023 +0700 @@ -31,8 +31,6 @@ MatMM* = Mat22|Mat33|Mat44 MatMN* = Mat23|Mat32|Mat34|Mat43 Mat* = MatMM|MatMN - IntegerMat = Mat22[SomeInteger]|Mat33[SomeInteger]|Mat44[SomeInteger]|Mat23[SomeInteger]|Mat32[SomeInteger]|Mat34[SomeInteger]|Mat43[SomeInteger] - FloatMat = Mat22[SomeFloat]|Mat33[SomeFloat]|Mat44[SomeFloat]|Mat23[SomeFloat]|Mat32[SomeFloat]|Mat34[SomeFloat]|Mat43[SomeFloat] func unit22[T: SomeNumber](): auto {.compiletime.} = Mat22[T](data:[ T(1), T(0), @@ -204,31 +202,6 @@ resultVec ) -proc createVecMatMultiplicationOperator1(vecType: typedesc, matType: typedesc): NimNode = - var data = nnkBracket.newTree() - for i in 0 ..< matType.columnCount: - data.add(newCall( - ident("sum"), - infix( - ident("v"), - "*", - newCall(newDotExpr(ident("m"), ident("col")), newLit(i)) - ) - )) - let resultVec = nnkObjConstr.newTree( - nnkBracketExpr.newTree(ident(vecType.name), ident("float")), - nnkExprColonExpr.newTree(ident("data"), data) - ) - - return nnkFuncDef.newTree( - ident("test"), - newEmptyNode(), - newEmptyNode(), - newEmptyNode(), - newEmptyNode(), - newEmptyNode(), - resultVec, - ) proc createMatScalarOperator(matType: typedesc, op: string): NimNode = result = newStmtList()
--- a/src/zamikongine/shader.nim Wed Jan 11 00:43:43 2023 +0700 +++ b/src/zamikongine/shader.nim Wed Jan 11 11:15:02 2023 +0700 @@ -1,7 +1,9 @@ -import std/osproc +import std/tempfiles import std/strformat import std/strutils import std/tables +import std/compilesettings + import ./vulkan_helpers import ./vulkan @@ -13,7 +15,7 @@ programType*: VkShaderStageFlagBits shader*: VkPipelineShaderStageCreateInfo -func stage2string(stage: VkShaderStageFlagBits): string = +func stage2string(stage: VkShaderStageFlagBits): string {.compileTime.} = case stage of VK_SHADER_STAGE_VERTEX_BIT: "vert" of VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT: "tesc" @@ -24,12 +26,14 @@ of VK_SHADER_STAGE_COMPUTE_BIT: "comp" of VK_SHADER_STAGE_ALL: "" -proc compileGLSLToSPIRV(stage: VkShaderStageFlagBits, shaderSource: string, entrypoint: string): seq[uint32] = +proc compileGLSLToSPIRV(stage: VkShaderStageFlagBits, shaderSource: string, entrypoint: string): seq[uint32] {.compileTime.} = let stagename = stage2string(stage) - let (output, exitCode) = execCmdEx(command=fmt"./glslangValidator --entry-point {entrypoint} -V --stdin -S {stagename}", input=shaderSource) + 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) if exitCode != 0: raise newException(Exception, output) - let shaderbinary = readFile fmt"{stagename}.spv" + let shaderbinary = staticRead tmpfilename var i = 0 while i < shaderbinary.len: result.add( @@ -40,11 +44,12 @@ ) i += 4 -proc initShaderProgram*(device: VkDevice, programType: VkShaderStageFlagBits, shader: string, entryPoint: string="main"): ShaderProgram = +proc initShaderProgram*(device: VkDevice, programType: static VkShaderStageFlagBits, shader: static string, entryPoint: static string="main"): ShaderProgram = result.entryPoint = entryPoint result.programType = programType - var code = compileGLSLToSPIRV(result.programType, shader, result.entryPoint) + const constcode = compileGLSLToSPIRV(programType, shader, entryPoint) + var code = constcode var createInfo = VkShaderModuleCreateInfo( sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, codeSize: uint(code.len * sizeof(uint32)), @@ -60,7 +65,7 @@ pName: cstring(result.entryPoint), # entry point for shader ) -func generateVertexShaderCode*[T](entryPoint, positionAttrName, colorAttrName: static string): string = +func generateVertexShaderCode*[T](entryPoint, positionAttrName, colorAttrName: static string): string {.compileTime.} = var lines: seq[string] lines.add "#version 450" lines.add generateGLSLDeclarations[T]() @@ -75,7 +80,7 @@ lines.add "}" return lines.join("\n") -func generateFragmentShaderCode*[T](entryPoint: static string): string = +func generateFragmentShaderCode*[T](entryPoint: static string): string {.compileTime.} = var lines: seq[string] lines.add "#version 450" lines.add "layout(location = 0) in vec3 fragColor;"