Mercurial > games > semicongine
changeset 730:88ab3e6dcaa0
add: simpler shader construction
author | Sam <sam@basx.dev> |
---|---|
date | Sun, 28 May 2023 18:36:11 +0700 |
parents | 7b72da29ad8d |
children | c12b11a5f112 |
files | src/semicongine/vulkan/shader.nim tests/test_mesh.nim |
diffstat | 2 files changed, 70 insertions(+), 21 deletions(-) [+] |
line wrap: on
line diff
--- 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*(
--- 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; """