More work on swizzle.

This commit is contained in:
treeform 2022-08-08 11:03:41 -07:00
parent da90d35282
commit 005a6235ef
4 changed files with 2132 additions and 6097 deletions

File diff suppressed because it is too large Load diff

31
tests/bench_swizzle.nim Normal file
View file

@ -0,0 +1,31 @@
import benchy, vmath
block:
var
a = vec2(1, 10)
b = vec2(2, 20)
c = vec2(3, 30)
timeIt "xy":
for i in 0 ..< 1_000_000:
c.yx = a.xy + b.rg + c.xy
echo c.xy
block:
var
a = vec3(1, 10, 100)
b = vec3(2, 20, 200)
c = vec3(3, 30, 300)
timeIt "xyz":
for i in 0 ..< 1_000_000:
c.zyx = a.xyz + b.rgb + c.xyz
echo c.xyz
block:
var
a = vec4(1, 10, 100, -1)
b = vec4(2, 20, 200, -1)
c = vec4(3, 30, 300, -1)
timeIt "xyzw":
for i in 0 ..< 1_000_000:
c.wzyx = a.xyzw + b.rgba + c.xyzw
echo c.xyzw

View file

@ -356,11 +356,45 @@ block:
var b = dvec4(1, 2, 3, 4)
doAssert b == dvec4(1, 2, 3, 4)
b.wzyx = b
doAssert b == dvec4(4, 3, 2, 1)
b.g = 123
doAssert b == dvec4(4.0, 123.0, 2.0, 1.0)
doAssert b == dvec4(1.0, 123.0, 3.0, 4.0)
block:
# test swizzle self-assignment
var a = dvec2(1, 2)
a.yx = a
doAssert a == dvec2(2, 1)
var b = dvec3(1, 2, 3)
b.zyx = b
doAssert b == dvec3(3, 2, 1)
var c = dvec4(1, 2, 3, 4)
c.wzyx = c
doAssert c == dvec4(4, 3, 2, 1)
block:
# Test swizzle calls only once
var callCount = 0
proc countsCalls(): Vec2 =
inc callCount
doAssert countsCalls().yx == vec2(0, 0)
doAssert callCount == 1
callCount = 0
doAssert vec2(0, 0) == countsCalls().yx
doAssert callCount == 1
var tmp: Vec2
proc countsCalls2(): var Vec2 =
inc callCount
return tmp
callCount = 0
countsCalls2().yx = vec2(0, 0)
doAssert callCount == 1
block:
# Test swizzle with complex expressions

View file

@ -1,47 +1,49 @@
import strformat
var swizzles = @["xyzw", "rgba", "stpq"]
var
swizzles = @["xyzw", "rgba", "stpq"]
code = ""
echo "# Generated by tools/gensswizzle"
code.add "# Generated by tools/gensswizzle - don't edit manually.\n"
for swizzle in swizzles[1 .. ^1]:
echo "\n# 1 x ", swizzle
code.add "\n# 1 x " & swizzle & "\n"
for i1, s1 in swizzle:
echo &"template {s1}*[T](a: GVec234[T]): T = a[{i1}]"
echo &"template `{s1}=`*[T](a: var GVec234[T], b: T) = a[{i1}] = b"
code.add &"proc {s1}*[T](a: GVec234[T]): T = a[{i1}]\n"
for i1, s1 in swizzle:
code.add &"proc `{s1}=`*[T](a: var GVec234[T], b: T) = a[{i1}] = b\n"
for swizzle in swizzles:
echo "\n# 2 x ", swizzle
code.add "\n# 2 x " & swizzle & "\n"
for i1, s1 in swizzle:
for i2, s2 in swizzle:
echo &"template {s1}{s2}*[T](a: GVec234[T]): GVec2[T] ="
echo &" let a2 = a"
echo &" gvec2(a2[{i1}], a2[{i2}])"
echo &"func `{s1}{s2}=`*[T](a: var GVec234[T], b: GVec2[T]) ="
echo &" let b2 = b"
echo &" a[{i1}] = b2.x; a[{i2}] = b2.y"
code.add &"proc {s1}{s2}*[T](a: GVec234[T]): GVec2[T] = gvec2(a[{i1}], a[{i2}])\n"
for i1, s1 in swizzle:
for i2, s2 in swizzle:
code.add &"proc `{s1}{s2}=`*[T](a: var GVec234[T], b: GVec2[T]) = (let b = b; a[{i1}] = b.x; a[{i2}] = b.y)\n"
for swizzle in swizzles:
echo "\n# 3 x ", swizzle
code.add "\n# 3 x " & swizzle & "\n"
for i1, s1 in swizzle:
for i2, s2 in swizzle:
for i3, s3 in swizzle:
echo &"template {s1}{s2}{s3}*[T](a: GVec234[T]): GVec3[T] ="
echo &" let a2 = a"
echo &" gvec3(a2[{i1}], a2[{i2}], a2[{i3}])"
echo &"func `{s1}{s2}{s3}=`*[T](a: var GVec234[T], b: GVec3[T]) ="
echo &" let b2 = b"
echo &" a[{i1}] = b2.x; a[{i2}] = b2.y; a[{i3}] = b2.z"
code.add &"proc {s1}{s2}{s3}*[T](a: GVec234[T]): GVec3[T] = gvec3(a[{i1}], a[{i2}], a[{i3}])\n"
for i1, s1 in swizzle:
for i2, s2 in swizzle:
for i3, s3 in swizzle:
code.add &"proc `{s1}{s2}{s3}=`*[T](a: var GVec234[T], b: GVec3[T]) = (let b = b; a[{i1}] = b.x; a[{i2}] = b.y; a[{i3}] = b.z)\n"
for swizzle in swizzles:
echo "\n# 4 x ", swizzle
code.add "\n# 4 x " & swizzle & "\n"
for i1, s1 in swizzle:
for i2, s2 in swizzle:
for i3, s3 in swizzle:
for i4, s4 in swizzle:
echo &"template {s1}{s2}{s3}{s4}*[T](a: GVec234[T]): GVec4[T] ="
echo &" let a2 = a"
echo &" gvec4(a2[{i1}], a2[{i2}], a2[{i3}], a2[{i4}])"
echo &"func `{s1}{s2}{s3}{s4}=`*[T](a: var GVec234[T], b: GVec4[T]) ="
echo &" let b2 = b"
echo &" a[{i1}] = b2.x; a[{i2}] = b2.y; a[{i3}] = b2.z; a[{i4}] = b2.w"
code.add &"proc {s1}{s2}{s3}{s4}*[T](a: GVec234[T]): GVec4[T] = gvec4(a[{i1}], a[{i2}], a[{i3}], a[{i4}])\n"
for i1, s1 in swizzle:
for i2, s2 in swizzle:
for i3, s3 in swizzle:
for i4, s4 in swizzle:
code.add &"proc `{s1}{s2}{s3}{s4}=`*[T](a: var GVec234[T], b: GVec4[T]) = (let b = b; a[{i1}] = b.x; a[{i2}] = b.y; a[{i3}] = b.z; a[{i4}] = b.w)\n"
writeFile("src/vmath/swizzle.nim", code)