Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/shady/backends/shared.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions tests/test_shady.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 30 additions & 0 deletions tests/test_shady.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}