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
98 changes: 78 additions & 20 deletions src/shady/backends/glsl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,90 @@ proc glslTypeDefault*(t: string): string =
else: ""

const glslGlobals* = [
"gl_Position", "gl_FragCoord", "gl_GlobalInvocationID",
"gl_VertexID", "gl_InstanceID", "gl_FrontFacing",
"gl_Position",
"gl_FragCoord",
"gl_FragDepth",
"gl_GlobalInvocationID",
"gl_VertexID",
"gl_InstanceID",
"gl_FrontFacing",
]

const glslFunctions* = [
"bool", "array",
"vec2", "vec3", "vec4", "mat2", "mat3", "mat4",
"Vec2", "Vec3", "Vec4", "mat2", "Mat3", "Mat4",
"uvec2", "uvec3", "uvec4",
"UVec2", "UVec3", "UVec4",
"ivec2", "ivec3", "ivec4",
"IVec2", "IVec3", "IVec4",

"abs", "clamp", "min", "max", "dot", "sqrt", "mix", "length",
"cross", "reflect", "log2",
"texelFetch", "imageStore", "imageLoad", "texture", "textureLod",
"bool",
"array",
"vec2",
"vec3",
"vec4",
"mat2",
"mat3",
"mat4",
"Vec2",
"Vec3",
"Vec4",
"mat2",
"Mat3",
"Mat4",
"uvec2",
"uvec3",
"uvec4",
"UVec2",
"UVec3",
"UVec4",
"ivec2",
"ivec3",
"ivec4",
"IVec2",
"IVec3",
"IVec4",
"abs",
"clamp",
"min",
"max",
"dot",
"sqrt",
"mix",
"length",
"cross",
"reflect",
"log2",
"texelFetch",
"imageStore",
"imageLoad",
"texture",
"textureLod",
"textureSize",
"textureGrad",
"normalize",
"floor", "ceil", "round", "exp", "inversesqrt",
"[]", "[]=",
"floor",
"ceil",
"round",
"exp",
"inversesqrt",
"[]",
"[]=",
"inverse",
"sin", "cos", "tan", "acos", "atan", "pow",
"sin",
"cos",
"tan",
"asin",
"acos",
"atan",
"pow",
"fmod",
"lessThan", "lessThanEqual", "greaterThan", "greaterThanEqual",
"equal", "notEqual",
"dFdx", "dFdy", "fract", "fwidth",
"step", "smoothstep", "inc", "dec", "discardFragment"
"lessThan",
"lessThanEqual",
"greaterThan",
"greaterThanEqual",
"equal",
"notEqual",
"dFdx",
"dFdy",
"fract",
"fwidth",
"step",
"smoothstep",
"inc",
"dec",
"discardFragment",
]
2 changes: 1 addition & 1 deletion src/shady/backends/glsl3.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const
glsl3DesktopVersion* = "330"
glsl3DesktopExtra* = ""
glsl3WebGLVersion* = "300 es"
glsl3WebGLExtra* = "precision highp float;\n"
glsl3WebGLExtra* = "precision highp float;\nprecision highp int;\n"

proc glsl3NeedsHighp*(samplerType: string): bool =
samplerType in [
Expand Down
11 changes: 8 additions & 3 deletions src/shady/backends/shared.nim
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,8 @@ proc toCodeTopLevel(
let typeNode = paramDefs[^2]
for i in 0 ..< paramDefs.len - 2:
let param = paramDefs[i]
if param.strVal in ["gl_FragColor", "gl_Position"]:
if param.strVal in glslGlobals or param.strVal == "gl_FragColor":
# Builtins retain the target's native declarations and origin.
continue
if typeNode.kind == nnkVarTy:
if typeNode[0].repr == "seq":
Expand Down Expand Up @@ -1026,8 +1027,6 @@ proc toCodeTopLevel(
res.add "in "
res.add typeString(typeNode)
else:
if shaderTarget != glslES3 and param.strVal == "gl_FragCoord":
res.add "layout(origin_upper_left) "
if typeNode.strVal == "int":
res.add "flat "
if isVulkan():
Expand Down Expand Up @@ -1604,6 +1603,8 @@ type
var
## GLSL globals.
gl_Position*: Vec4
gl_FragCoord*: Vec4
gl_FragDepth*: float32
gl_VertexID*: int32
gl_InstanceID*: int32
gl_FrontFacing*: bool
Expand Down Expand Up @@ -1745,6 +1746,10 @@ proc fmod*(x, y: Vec3): Vec3 =
proc fmod*(x, y: Vec4): Vec4 =
vec4(fmod(x.x, y.x), fmod(x.y, y.y), fmod(x.z, y.z), fmod(x.w, y.w))

proc asin*(a: float32): float32 =
## Evaluates the shader inverse sine builtin on the CPU.
arcsin(a)

proc fract*(a: float32): float32 =
a - floor(a)

Expand Down
84 changes: 84 additions & 0 deletions tests/test_backends.nim
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,90 @@ block:
doAssert "float4 fragColor = float4(0.0, 0.0, 0.0, 0.0);" in msl
doAssert "return fragColor;" in msl

block:
proc fragmentDepth(
gl_FragCoord: Vec4,
gl_FragDepth: var float32,
fragColor: var Vec4
) =
## Preserves native pixel coordinates while writing an adjusted depth.
gl_FragDepth = clamp(gl_FragCoord.z - 0.01'f, 0.0'f, 1.0'f)
fragColor = gl_FragCoord

for source in [
toShader(fragmentDepth, glsl4Desktop, shaderFragment),
toShader(fragmentDepth, glsl3WebGL, shaderFragment),
toShader(fragmentDepth, vulkanGlsl450, shaderFragment)
]:
doAssert "origin_upper_left" notin source
doAssert "in vec4 gl_FragCoord" notin source
doAssert "out float gl_FragDepth" notin source
doAssert "gl_FragDepth = clamp(gl_FragCoord.z - " in source
doAssert "fragColor = gl_FragCoord;" in source

var depth: float32
var color: Vec4
fragmentDepth(vec4(8.5'f, 16.5'f, 0.5'f, 1.0'f), depth, color)
doAssert depth == 0.49'f
doAssert color == vec4(8.5'f, 16.5'f, 0.5'f, 1.0'f)

proc globalDepth(fragColor: var Vec4) =
## Supports builtin globals when helpers share fragment state.
gl_FragDepth = gl_FragCoord.z
fragColor = vec4(gl_FragCoord.xy, vec2(gl_FragDepth, 1.0'f))

for source in [
toShader(globalDepth, glsl4Desktop, shaderFragment),
toShader(globalDepth, glsl3WebGL, shaderFragment)
]:
doAssert "float gl_FragDepth" notin source
doAssert "vec4 gl_FragCoord" notin source
doAssert "gl_FragDepth = gl_FragCoord.z;" in source

block:
proc integerVertex(
gl_VertexID: int32,
count: Uniform[int],
index: var int,
gl_Position: var Vec4
) =
## Exercises builtin identifiers with integer values shared across stages.
index = count
gl_Position = vec4(float32(gl_VertexID), 0.0'f, 0.0'f, 1.0'f)

proc integerFragment(
count: Uniform[int],
index: int,
fragColor: var Vec4
) =
## Requires the same integer precision as the vertex shader.
fragColor = vec4(float32(count + index))

let
vertex = toShader(integerVertex, glsl3WebGL, shaderVertex)
fragment = toShader(integerFragment, glsl3WebGL, shaderFragment)
doAssert "precision highp int;" in vertex
doAssert "precision highp int;" in fragment
doAssert "in int gl_VertexID" notin vertex
doAssert "uniform int count;" in vertex
doAssert "uniform int count;" in fragment
doAssert "flat out int index;" in vertex
doAssert "flat in int index;" in fragment

block:
proc inverseSine(uv: Vec2, fragColor: var Vec4) =
## Uses inverse sine directly rather than emitting a CPU wrapper.
fragColor = vec4(asin(uv.x))

for source in [
toShader(inverseSine, glsl4Desktop, shaderFragment),
toShader(inverseSine, glsl3WebGL, shaderFragment)
]:
doAssert "asin(uv.x)" in source
doAssert "arcsin" notin source
doAssert "float asin(" notin source
doAssert asin(0.0'f) == 0.0'f

block:
proc vertexPass(
vPos: Vec3,
Expand Down
6 changes: 3 additions & 3 deletions tests/test_shady.txt
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ gl_FragCoord and flat int attributes:
precision highp float;
// from complexShader

layout(origin_upper_left) in vec4 gl_FragCoord;
flat in int someInt;
out vec4 fragColor;

Expand Down Expand Up @@ -431,7 +430,6 @@ GlslTarget: Desktop with gl_FragCoord:
#version 410
// from desktopFragCoord

layout(origin_upper_left) in vec4 gl_FragCoord;
out vec4 fragColor;

void main() {
Expand All @@ -442,9 +440,9 @@ void main() {
GlslTarget: ES3 with gl_FragCoord:
#version 300 es
precision highp float;
precision highp int;
// from es3FragCoord

in vec4 gl_FragCoord;
out vec4 fragColor;

void main() {
Expand All @@ -455,6 +453,7 @@ void main() {
GlslTarget: ES3 vertex shader with integer mod:
#version 300 es
precision highp float;
precision highp int;
// from es3VertShader

uniform vec2 atlasSizeE;
Expand All @@ -478,6 +477,7 @@ void main() {
GlslTarget: ES3 sampler uniforms get highp:
#version 300 es
precision highp float;
precision highp int;
// from es3SamplerFrag

uniform highp usampler2D tileData;
Expand Down