Skip to content

Commit a142f19

Browse files
committed
Fixed incompatibility with newer nim
1 parent b4c10ed commit a142f19

File tree

4 files changed

+48
-28
lines changed

4 files changed

+48
-28
lines changed

nimsl.nimble

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Package
2-
version = "0.2"
2+
version = "0.2.1"
33
author = "Yuriy Glukhov"
44
description = "Shaders in Nim"
55
license = "MIT"

nimsl/nimsl.nim

+24-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import macros, math, strutils
22
import private.glsl_codegen
33

4-
{.pragma: glslinfix, tags: [glslinfix_t].}
5-
64
proc getShaderCode(s: NimNode, k: ShaderKind, mainProcName: string): string =
75
var ctx = newCtx()
86
ctx.mainProcName = mainProcName
@@ -192,26 +190,44 @@ proc `*`*(m: mat4, v: vec4): vec4 =
192190
result[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w
193191
result[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w
194192

195-
proc `.`*[T](v: vecBase[2, T], f: static[string]): vecBase[f.len, T] {.glslinfix.} =
193+
proc validateAttrs(s: string, a: set[char]) {.compileTime.} =
194+
for c in s:
195+
assert(c in a, "Invalid vector component: " & $c)
196+
197+
proc nimsl_deriveVectorWithComponents[T](v: vecBase[2, T], f: static[string]): vecBase[f.len, T] =
198+
glslinfix()
199+
static: validateAttrs(f, {'x', 'r', 'y', 'g'})
196200
for i, c in f:
197201
case c
198202
of 'x', 'r': result[i] = v.x
199203
of 'y', 'g': result[i] = v.y
200-
else: assert(false, "Unknown field: " & $c)
204+
else: discard # Should be prevented by validateAttrs
201205

202-
proc `.`*[T](v: vecBase[3, T], f: static[string]): vecBase[f.len, T] {.glslinfix.} =
206+
proc nimsl_deriveVectorWithComponents[T](v: vecBase[3, T], f: static[string]): vecBase[f.len, T] =
207+
glslinfix()
208+
static: validateAttrs(f, {'x', 'r', 'y', 'g', 'z', 'b'})
203209
for i, c in f:
204210
case c
205211
of 'x', 'r': result[i] = v.x
206212
of 'y', 'g': result[i] = v.y
207213
of 'z', 'b': result[i] = v.z
208-
else: assert(false, "Unknown field: " & $c)
214+
else: discard # Should be prevented by validateAttrs
209215

210-
proc `.`*[T](v: vecBase[4, T], f: static[string]): vecBase[f.len, T] {.glslinfix.} =
216+
proc nimsl_deriveVectorWithComponents[T](v: vecBase[4, T], f: static[string]): vecBase[f.len, T] =
217+
glslinfix()
218+
static: validateAttrs(f, {'x', 'r', 'y', 'g', 'z', 'b', 'w', 'a'})
211219
for i, c in f:
212220
case c
213221
of 'x', 'r': result[i] = v.x
214222
of 'y', 'g': result[i] = v.y
215223
of 'z', 'b': result[i] = v.z
216224
of 'w', 'a': result[i] = v.w
217-
else: assert(false, "Unknown field: " & $c)
225+
else: discard # Should be prevented by validateAttrs
226+
227+
when defined(nimNewDot):
228+
template `.`*[I, T](v: vecBase[I, T], f: untyped): auto =
229+
nimsl_deriveVectorWithComponents(v, astToStr(f))
230+
else:
231+
proc `.`*[I, T](v: vecBase[I, T], f: static[string]): vecBase[f.len, T] {.inline.} =
232+
glslinfix()
233+
result = nimsl_deriveVectorWithComponents(v, f)

nimsl/private/glsl_codegen.nim

+13-17
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import macros, strutils
22

33
{.push stackTrace: off.}
44
proc glslbuiltin*() {.inline.} = discard
5+
proc glslinfix*() {.inline.} = discard
56
{.pop.}
67

7-
type
8-
glslinfix_t* = object
98

109
type ShaderKind* = enum
1110
skVertexShader
@@ -38,28 +37,25 @@ proc isRange(n: NimNode, rangeLen: int = -1): bool =
3837
elif n[2].intVal - n[1].intVal + 1 == rangeLen:
3938
result = true
4039

41-
proc hasTag(n: NimNode, tag: string): bool =
42-
case n.kind
43-
of nnkProcDef:
44-
for i in n.pragma:
45-
if i.kind == nnkPragma and i[0].kind == nnkExprColonExpr and $(i[0][0]) == "tags":
46-
for j in i[0][1]:
47-
if $j == tag: return true
48-
of nnkSym:
49-
result = n.symbol.getImpl().hasTag(tag)
50-
else: discard
40+
proc hasMagicMarker(n: NimNode, marker: string): bool =
41+
template isMagicCallNode(n: NimNode): bool =
42+
n.kind == nnkCall and n[0].kind == nnkSym and $n[0] == marker
5143

52-
proc isGLSLBuiltin(n: NimNode): bool =
5344
case n.kind
5445
of nnkProcDef:
5546
let b = n.body
5647
if b.kind == nnkStmtList:
5748
let fs = b[0]
58-
result = fs.kind == nnkCall and fs[0].kind == nnkSym and $fs[0] == "glslbuiltin"
49+
result = fs.isMagicCallNode()
5950
of nnkSym:
60-
result = isGLSLBuiltin(getImpl(n.symbol))
51+
result = hasMagicMarker(getImpl(n.symbol), marker)
52+
of nnkStmtListExpr:
53+
let fs = n[0]
54+
result = fs.isMagicCallNode()
6155
else: discard
6256

57+
proc isGLSLBuiltin(n: NimNode): bool = n.hasMagicMarker("glslbuiltin")
58+
6359
proc getTypeName(ctx: var GLSLCompilerContext, t: NimNode, skipVar = false): string =
6460
case t.kind
6561
of nnkBracketExpr:
@@ -146,8 +142,8 @@ proc genSystemCall(ctx: var GLSLCompilerContext, n: NimNode, r: var string) =
146142
echo "UNKNOWN SYSTEM CALL: ", treeRepr(n)
147143

148144
proc genCall(ctx: var GLSLCompilerContext, n: NimNode, r: var string) =
149-
if n[0].hasTag("glslinfix_t"):
150-
if $(n[0]) == ".":
145+
if n[0].hasMagicMarker("glslinfix"):
146+
if $n[0] in [".", "nimsl_deriveVectorWithComponents"]:
151147
# This is a property
152148
gen(ctx, n[1], r)
153149
r &= "."

test.nim

+10-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,19 @@ proc myFragmentShader(vPos: vec2): vec4 =
3131
result.drawShape(sdEllipseInRect(vPos, bounds), uStrokeColor);
3232
result.drawShape(sdEllipseInRect(vPos, insetRect(bounds, uStrokeWidth)), uFillColor);
3333

34+
proc myVertexShader2(a: vec4): vec3 =
35+
result = a.xxz
36+
3437

3538
suite "codegen":
3639
test "vs":
37-
check(getGLSLVertexShader(myVertexShader) ==
38-
"uniform mat4 mvp;attribute vec2 a;varying vec2 vPos;void main(){vec4 result=vec4(0.0);vPos=a;result=(mvp*vec4(a,0.0,1.0));gl_Position=result;}")
40+
check getGLSLVertexShader(myVertexShader) ==
41+
"uniform mat4 mvp;attribute vec2 a;varying vec2 vPos;void main(){vec4 result=vec4(0.0);vPos=a;result=(mvp*vec4(a,0.0,1.0));gl_Position=result;}"
42+
43+
test "vector accessors":
44+
check getGLSLVertexShader(myVertexShader2) ==
45+
"attribute vec4 a;void main(){vec3 result=vec4(0.0);result=a.xxz;gl_Position=result;}"
46+
3947

4048
# import nimx.write_image_impl
4149

0 commit comments

Comments
 (0)