diff --git a/src/shady/backends/shared.nim b/src/shady/backends/shared.nim index 8d0fd7f..bca13fa 100644 --- a/src/shady/backends/shared.nim +++ b/src/shady/backends/shared.nim @@ -115,9 +115,11 @@ proc typeString(n: NimNode): string = of "GVec2[int32]": typeRename("IVec2") of "GVec3[int32]": typeRename("IVec3") of "GVec4[int32]": typeRename("IVec4") - of "Uniform[float32]": typeRename("float32") - of "Uniform[int]": typeRename("int") else: + if n[0].repr in ["Uniform", "UniformWriteOnly", "Attribute"]: + # Uniform[T], UniformWriteOnly[T] and Attribute[T] are just T + # in the shader, so use the inner type. + return typeString(n[1]) if n[0].repr == "array": var length = 0 if n[1].kind == nnkIntLit: @@ -983,7 +985,22 @@ proc toCodeTopLevel( res.add "out " res.add typeRename(typeNode[0].strVal) else: - if typeNode.kind == nnkBracketExpr: + if typeNode.kind == nnkBracketExpr and + typeNode[0].repr in ["Uniform", "UniformWriteOnly", "Attribute"]: + # Uniform[T] params become uniform globals, not "in" params. + res.add typeRename(typeNode[0].repr) + res.add " " + let (innerType, arraySuffix) = splitArrayType(typeNode[1]) + if shaderTarget == glslES3 and glsl3NeedsHighp(innerType): + res.add "highp " + res.add innerType + res.add " " + res.add param.strVal + res.add arraySuffix + res.addSmart ';' + res.add "\n" + continue + elif typeNode.kind == nnkBracketExpr: if isVulkan(): res.add "layout(location = " res.add $inLocation diff --git a/tests/test_shady.nim b/tests/test_shady.nim index a864bbd..ed0ba21 100644 --- a/tests/test_shady.nim +++ b/tests/test_shady.nim @@ -384,6 +384,31 @@ block: fragColor = vec4(float(tile.x), 0.0, 0.0, 1.0) log toGLSL(desktopSamplerFrag, glslDesktop) +block: + log "--------------------------------------------------" + log "Uniform params on the entry point (issue #32):" + proc uniformParamVert( + gl_Position: var Vec4, + MVP: Uniform[Mat4], + vCol: Vec3, + vPos: Vec3, + vertColor: var Vec3 + ) = + gl_Position = MVP * vec4(vPos.x, vPos.y, 0.0, 1.0) + vertColor = vCol + log toGLSL(uniformParamVert) + +block: + log "--------------------------------------------------" + log "Uniform float param on the entry point:" + proc uniformParamFrag( + fragColor: var Vec4, + uv: Vec2, + time: Uniform[float32] + ) = + fragColor = vec4(uv.x, uv.y, sin(time), 1.0) + log toGLSL(uniformParamFrag) + when defined(gen_master): writeFile(goldMasterPath, masterOutput) else: diff --git a/tests/test_shady.txt b/tests/test_shady.txt index ec67fe6..7d4a27b 100644 --- a/tests/test_shady.txt +++ b/tests/test_shady.txt @@ -507,3 +507,33 @@ void main() { fragColor = vec4(float(float(tile.x)), 0.0, 0.0, 1.0); } +-------------------------------------------------- +Uniform params on the entry point (issue #32): +#version 410 +precision highp float; +// from uniformParamVert + +uniform mat4 MVP; +in vec3 vCol; +in vec3 vPos; +out vec3 vertColor; + +void main() { + gl_Position = MVP * vec4(vPos.x, vPos.y, 0.0, 1.0); + vertColor = vCol; +} + +-------------------------------------------------- +Uniform float param on the entry point: +#version 410 +precision highp float; +// from uniformParamFrag + +out vec4 fragColor; +in vec2 uv; +uniform float time; + +void main() { + fragColor = vec4(uv.x, uv.y, sin(time), 1.0); +} +