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
8 changes: 7 additions & 1 deletion src/shady/backends/dx12.nim
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ proc hlslProcRename*(t: string): string =
of "div": "/"
of "gl_Position": "gl_Position"
of "gl_FrontFacing": "gl_FrontFacing"
of "linear": "shady_linear"
of "point", "line", "triangle", "lineadj", "triangleadj",
"linear", "centroid", "nointerpolation", "noperspective", "sample":
"shady_" & t
else: t.replace("`", "_")

proc hlslTypeDefault*(t: string): string =
Expand Down Expand Up @@ -210,6 +212,10 @@ proc hlslResourceCall*(
of "textureLod":
if args.len != 3: return ""
args[0] & ".SampleLevel(" & args[0] & "Sampler, " & args[1] & ", " & args[2] & ")"
of "textureGrad":
if args.len != 4: return ""
args[0] & ".SampleGrad(" & args[0] & "Sampler, " & args[1] & ", " &
args[2] & ", " & args[3] & ")"
of "textureSize":
if args.len < 1: return ""
"shadyTextureSize(" & args[0] & ", " & (if args.len > 1: args[1] else: "0") & ")"
Expand Down
2 changes: 1 addition & 1 deletion src/shady/backends/glsl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,5 @@ const glslFunctions* = [
"lessThan", "lessThanEqual", "greaterThan", "greaterThanEqual",
"equal", "notEqual",
"dFdx", "dFdy", "fract", "fwidth",
"smoothstep", "inc", "dec", "discardFragment"
"step", "smoothstep", "inc", "dec", "discardFragment"
]
12 changes: 11 additions & 1 deletion src/shady/backends/metal4.nim
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ proc metalResourceCall*(
name: string,
args: openArray[string],
firstArgIsSamplerBuffer: bool,
firstArgIsShadowSampler: bool
firstArgIsShadowSampler: bool,
firstArgIsSamplerArray = false
): string =
case name
of "texture":
Expand All @@ -167,6 +168,15 @@ proc metalResourceCall*(
of "textureLod":
if args.len != 3: return ""
args[0] & ".sample(" & args[0] & "Sampler, " & args[1] & ", metal::level(" & args[2] & "))"
of "textureGrad":
if args.len != 4: return ""
let coordinates =
if firstArgIsSamplerArray:
"(" & args[1] & ").xy, uint((" & args[1] & ").z)"
else:
args[1]
args[0] & ".sample(" & args[0] & "Sampler, " & coordinates &
", metal::gradient2d(" & args[2] & ", " & args[3] & "))"
of "textureSize":
if args.len < 1: return ""
if firstArgIsSamplerBuffer:
Expand Down
28 changes: 20 additions & 8 deletions src/shady/backends/shared.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Shader macro, converts Nim code into shader source.

import macros, pixie, strutils, tables, vmath
import macros, math, pixie, strutils, tables, vmath
import shady/backends/[glsl, glsl3, glsl4, dx12, metal4, vulkan]
from chroma import ColorRGBX

Expand Down Expand Up @@ -320,6 +320,10 @@ proc isSamplerBufferExpr(n: NimNode): bool =
proc isShadowSamplerExpr(n: NimNode): bool =
"Sampler2dShadow" in n.typeInstRepr()

proc isSamplerArrayExpr(n: NimNode): bool =
## Distinguishes layered texture coordinates from ordinary 2D coordinates.
"sampler2darray" in n.typeInstRepr().toLowerAscii()

proc codeExpr(n: NimNode): string =
n.toCode(result)

Expand All @@ -330,7 +334,7 @@ proc emitBackendCall(n: NimNode, res: var string): bool =

let name = n[0].strVal
if name notin [
"texture", "textureLod", "textureSize", "texelFetch", "imageLoad",
"texture", "textureLod", "textureGrad", "textureSize", "texelFetch", "imageLoad",
"imageStore"
]:
return false
Expand All @@ -351,7 +355,8 @@ proc emitBackendCall(n: NimNode, res: var string): bool =
name,
args,
n.len > 1 and n[1].isSamplerBufferExpr,
n.len > 1 and n[1].isShadowSamplerExpr
n.len > 1 and n[1].isShadowSamplerExpr,
n.len > 1 and n[1].isSamplerArrayExpr
)
if call.len == 0:
return false
Expand Down Expand Up @@ -1153,7 +1158,7 @@ proc gatherTypes(
var fieldName = field[i].repr
if fieldName.endsWith("*"):
fieldName = fieldName[0 .. ^2]
if fieldName in ["input", "output"]: fieldName = procRename(fieldName)
fieldName = procRename(fieldName)
def.add " " & typeString(fieldType) & " " & fieldName & ";\n"
def.add "};"
types[name] = def
Expand All @@ -1179,8 +1184,9 @@ proc gatherFunction(
for n in topLevelNode:
if n.kind == nnkSym:
# Looking for globals.
let name = if n.strVal in ["input", "output"]: procRename(n.strVal) else: n.strVal
if name notin glslGlobals and name notin glslFunctions and name notin globals:
let name = procRename(n.strVal)
if n.strVal notin glslGlobals and n.strVal notin glslFunctions and
name notin globals:
if n.owner().symKind == nskModule:
let impl = n.getImpl()
if impl.kind notin {
Expand Down Expand Up @@ -1797,13 +1803,19 @@ proc textureSize*(buffer: Uniform[SamplerCube], level: int): Vec2 =
proc textureSize*(buffer: Uniform[Sampler2dShadow], level: int): Vec2 =
vec2(buffer.image.width.float32, buffer.image.height.float32)

proc pow*(base, exponent: Vec3): Vec3 =
## Raises each RGB channel to its corresponding exponent.
vec3(pow(base.x, exponent.x), pow(base.y, exponent.y),
pow(base.z, exponent.z))

proc textureGrad*(
s: Uniform[Sampler2D],
uv: Vec3,
uv: Vec2,
dUVdx: Vec2,
dUVdy: Vec2
): Vec4 =
texture(s, uv.xy)
## Samples a 2D texture with explicit screen-space coordinate derivatives.
texture(s, uv)

proc textureGrad*(
s: Uniform[Sampler2DArray],
Expand Down
64 changes: 64 additions & 0 deletions tests/test_backends.nim
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ block:
let vulkan = toShader(instancedVertex, vulkanGlsl450, shaderVertex)
doAssert "gl_VertexIndex" in vulkan
doAssert "gl_InstanceIndex" in vulkan
doAssert "int gl_VertexIndex" notin vulkan
doAssert "int gl_InstanceIndex" notin vulkan

let hlsl = toHLSL(instancedVertex, shaderVertex)
doAssert "uint gl_VertexID : SV_VertexID" in hlsl
Expand Down Expand Up @@ -313,3 +315,65 @@ block:
doAssert "in vec2 shady_input;" in source
doAssert "out vec4 shady_output;" in source
doAssert "shady_output = vec4(shady_input.x, shady_input.y" in source

block:
var bitmap: Uniform[Sampler2D]
proc explicitGradient(uv: Vec2, fragColor: var Vec4) =
## Retains explicit image derivatives on every source target.
fragColor = textureGrad(bitmap, uv, dFdx(uv), dFdy(uv))
let
glsl = toShader(explicitGradient, glsl4Desktop, shaderFragment)
hlsl = toHLSL(explicitGradient, shaderFragment)
msl = toMSL(explicitGradient, shaderFragment)
doAssert "textureGrad(bitmap, uv, dFdx(uv), dFdy(uv))" in glsl
doAssert "bitmap.SampleGrad(bitmapSampler, uv, ddx(uv), ddy(uv))" in hlsl
doAssert "bitmap.sample(bitmapSampler, uv, metal::gradient2d(" in msl
doAssert "dfdx(uv), -dfdy(uv)" in msl
doAssert "textureGrad(" notin hlsl and "textureGrad(" notin msl

block:
var layers: Uniform[Sampler2DArray]
proc layerGradient(uvw: Vec3, fragColor: var Vec4) =
## Separates an array layer from two-dimensional sampling derivatives.
fragColor = textureGrad(layers, uvw, dFdx(uvw.xy), dFdy(uvw.xy))
let
hlsl = toHLSL(layerGradient, shaderFragment)
msl = toMSL(layerGradient, shaderFragment)
doAssert "layers.SampleGrad(layersSampler, uvw," in hlsl
doAssert "layers.sample(layersSampler, (uvw).xy, uint((uvw).z)," in msl
doAssert "metal::gradient2d(" in msl

block:
type ShapeData = object
point: Vec2
var line: Uniform[Vec2]
proc pointValue(shape: ShapeData): Vec2 =
## Reads a field whose ordinary Nim name is a target keyword.
shape.point
proc reservedNames(point: Vec2, fragColor: var Vec4) =
## Uses HLSL primitive keywords as Nim fields, uniforms and local names.
var triangle: ShapeData
triangle.point = point + line
let value = pointValue(triangle)
fragColor = vec4(value.x, value.y, 0'f, 1'f)
let hlsl = toHLSL(reservedNames, shaderFragment)
doAssert "float2 shady_point;" in hlsl
doAssert "float2 shady_line;" in hlsl
doAssert "ShapeData shady_triangle;" in hlsl
doAssert "shady_triangle.shady_point = shady_point + shady_line;" in hlsl

block:
proc colorTransfer(rgb: Vec3, fragColor: var Vec4) =
## Expresses piecewise SVG color transfer through vector intrinsics.
let linear = mix(rgb / 12.92'f,
pow((rgb + 0.055'f) / 1.055'f, vec3(2.4'f)),
step(vec3(0.04045'f), rgb))
fragColor = vec4(linear, 1'f)
let
glsl = toShader(colorTransfer, glsl4Desktop, shaderFragment)
hlsl = toHLSL(colorTransfer, shaderFragment)
msl = toMSL(colorTransfer, shaderFragment)
doAssert "pow(" in glsl and "step(" in glsl
doAssert "pow(" in hlsl and "step(" in hlsl
doAssert "pow(" in msl and "step(" in msl
doAssert pow(vec3(2, 3, 4), vec3(2)) == vec3(4, 9, 16)
Loading