# HG changeset patch # User Sam # Date 1685273771 -25200 # Node ID 88ab3e6dcaa0b004f525b552a8d9a755a95de4bf # Parent 7b72da29ad8d2a72d00d89c4a50ff361875b40e0 add: simpler shader construction diff -r 7b72da29ad8d -r 88ab3e6dcaa0 src/semicongine/vulkan/shader.nim --- a/src/semicongine/vulkan/shader.nim Sun May 28 18:11:17 2023 +0700 +++ b/src/semicongine/vulkan/shader.nim Sun May 28 18:36:11 2023 +0700 @@ -91,7 +91,7 @@ outputs: seq[ShaderAttribute]= @[], version=DEFAULT_SHADER_VERSION , entrypoint=DEFAULT_SHADER_ENTRYPOINT , - main: seq[string] + main: string ): ShaderCode {.compileTime.} = var code = @[&"#version {version}", "#extension GL_EXT_scalar_block_layout : require", ""] & @@ -111,9 +111,27 @@ result.stage = stage result.binary = compileGlslToSPIRV(stage, code.join("\n"), entrypoint) +proc compileVertextShader*( + inputs: seq[ShaderAttribute]= @[], + uniforms: seq[ShaderAttribute]= @[], + samplers: seq[ShaderAttribute]= @[], + outputs: seq[ShaderAttribute]= @[], + version=DEFAULT_SHADER_VERSION , + entrypoint=DEFAULT_SHADER_ENTRYPOINT , + main: string +): ShaderCode {.compileTime.} = + compileGlslShader( + stage=VK_SHADER_STAGE_VERTEX_BIT, + inputs=inputs, + uniforms=uniforms, + samplers=samplers, + outputs=outputs, + version=version, + entrypoint=entrypoint, + main=main + ) -proc compileGlslShader*( - stage: VkShaderStageFlagBits, +proc compileFragmentShader*( inputs: seq[ShaderAttribute]= @[], uniforms: seq[ShaderAttribute]= @[], samplers: seq[ShaderAttribute]= @[], @@ -122,7 +140,47 @@ entrypoint=DEFAULT_SHADER_ENTRYPOINT , main: string ): ShaderCode {.compileTime.} = - return compileGlslShader(stage, inputs, uniforms, samplers, outputs, version, entrypoint, @[main]) + compileGlslShader( + stage=VK_SHADER_STAGE_FRAGMENT_BIT, + inputs=inputs, + uniforms=uniforms, + samplers=samplers, + outputs=outputs, + version=version, + entrypoint=entrypoint, + main=main + ) + +proc compileVertexFragmentShaderSet*( + inputs: seq[ShaderAttribute]= @[], + intermediate: seq[ShaderAttribute]= @[], + outputs: seq[ShaderAttribute]= @[], + uniforms: seq[ShaderAttribute]= @[], + samplers: seq[ShaderAttribute]= @[], + version=DEFAULT_SHADER_VERSION , + entrypoint=DEFAULT_SHADER_ENTRYPOINT , + vertexCode: string, + fragmentCode: string, +): (ShaderCode, ShaderCode) {.compileTime.} = + + result[0] = compileVertextShader( + inputs=inputs, + outputs=intermediate, + uniforms=uniforms, + samplers=samplers, + version=version, + entrypoint=entrypoint, + main=vertexCode + ) + result[1] = compileFragmentShader( + inputs=intermediate, + outputs=outputs, + uniforms=uniforms, + samplers=samplers, + version=version, + entrypoint=entrypoint, + main=fragmentCode + ) proc createShaderModule*( diff -r 7b72da29ad8d -r 88ab3e6dcaa0 tests/test_mesh.nim --- a/tests/test_mesh.nim Sun May 28 18:11:17 2023 +0700 +++ b/tests/test_mesh.nim Sun May 28 18:36:11 2023 +0700 @@ -24,7 +24,7 @@ attr[Vec2f]("texcoord_0", memoryPerformanceHint=PreferFastRead), attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true), ] - vertexOutput = @[ + intermediate = @[ attr[Vec4f]("vertexColor"), attr[Vec2f]("colorTexCoord"), attr[uint8]("materialId", noInterpolation=true) @@ -35,29 +35,20 @@ attr[Mat4]("view"), attr[Vec4f]("baseColorFactor", arrayCount=4), ] - samplers = @[ - attr[Sampler2DType]("baseColorTexture", arrayCount=4), - ] - vertexCode = compileGlslShader( - stage=VK_SHADER_STAGE_VERTEX_BIT, + samplers = @[attr[Sampler2DType]("baseColorTexture", arrayCount=4)] + (vertexCode, fragmentCode) = compileVertexFragmentShaderSet( inputs=vertexInput, - outputs=vertexOutput, + intermediate=intermediate, + outputs=fragOutput, uniforms=uniforms, samplers=samplers, - main=""" + vertexCode=""" gl_Position = vec4(position, 1.0) * (transform * Uniforms.view * Uniforms.projection); vertexColor = Uniforms.baseColorFactor[material]; colorTexCoord = texcoord_0; materialId = material; -""" - ) - fragmentCode = compileGlslShader( - stage=VK_SHADER_STAGE_FRAGMENT_BIT, - inputs=vertexOutput, - outputs=fragOutput, - uniforms=uniforms, - samplers=samplers, - main=""" +""", + fragmentCode=""" vec4 col[4] = vec4[4](vec4(1, 0, 0, 1), vec4(0, 1, 0, 1), vec4(0, 0, 1, 1), vec4(1, 1, 1, 1)); color = texture(baseColorTexture[materialId], colorTexCoord) * vertexColor; """