def test_dot_fun(self): code = """ p3 = dot(p1, p2) """ v1 = Vector3(2.5, 1.8, 2.9) v2 = Vector3(2.2, 1.1, 5.22) p1 = Vec3Arg('p1', v1) p2 = Vec3Arg('p2', v2) p3 = FloatArg('p3', 2.2) shader = Shader(code=code, args=[p1, p2, p3]) shader.compile() shader.prepare([Runtime()]) shader.execute() rez = v1.dot(v2) p = shader.get_value('p3') self.assertAlmostEqual(rez, p)
def test_arithmetic(self): code = """ a1 = 33 a2 = 22 p1 = a1 + a2 a1 = 22.3 a2 = 11.1 p2 = a1 + a2 a1 = 44 a2 = -2.36 p3 = a1 * a2 a1 = (2.3, 4) a2 = 5 p4 = a1 * a2 a1 = (3, 4, 6.6) a2 = (7, 4.3, 2.6) p5 = a1 - a2 a1 = (1, 4.1, 5.5, 9.9) a2 = (0.22, 3.3, 2.6, 6.6) p6 = a1 / a2 """ p1 = IntArg('p1', 33) p2 = FloatArg('p2', 55.5) p3 = FloatArg('p3', 55.5) p4 = Vec2Arg('p4', Vector2(0.0, 0.0)) p5 = Vec3Arg('p5', Vector3(0.0, 0.0, 0.0)) p6 = Vec4Arg('p6', Vector4(0.0, 0.0, 0.0, 0.0)) shader = Shader(code=code, args=[p1, p2, p3, p4, p5, p6]) shader.compile() shader.prepare([Runtime()]) shader.execute() rez = shader.get_value('p1') self.assertEqual(rez, 33 + 22) rez = shader.get_value('p2') self.assertAlmostEqual(rez, 22.3 + 11.1, places=5) rez = shader.get_value('p3') self.assertAlmostEqual(rez, 44 * -2.36, places=5) rez = shader.get_value('p4') self.assertAlmostEqual(rez.x, 5 * 2.3, places=5) self.assertAlmostEqual(rez.y, 5 * 4, places=5) rez = shader.get_value('p5') self.assertAlmostEqual(rez.x, 3 - 7.0, places=5) self.assertAlmostEqual(rez.y, 4 - 4.3, places=5) self.assertAlmostEqual(rez.z, 6.6 - 2.6, places=5) rez = shader.get_value('p6') self.assertAlmostEqual(rez.x, 1 / 0.22, places=5) self.assertAlmostEqual(rez.y, 4.1 / 3.3, places=5) self.assertAlmostEqual(rez.z, 5.5 / 2.6, places=5) self.assertAlmostEqual(rez.w, 9.9 / 6.6, places=5)
def test_normalize_fun(self): code = """ p2 = normalize(p1) """ v1 = Vector3(2.5, 1.8, 2.9) v2 = Vector3(6.5, 9.8, 3.9) p1 = Vec3Arg('p1', v1) p2 = Vec3Arg('p2', v2) shader = Shader(code=code, args=[p1, p2]) shader.compile() shader.prepare([Runtime()]) shader.execute() v3 = Vector3(2.5, 1.8, 2.9) v3.normalize() p = shader.get_value('p2') self.assertAlmostEqual(p.x, v3.x) self.assertAlmostEqual(p.y, v3.y) self.assertAlmostEqual(p.z, v3.z)
def test_corss_fun(self): code = """ p3 = cross(p1, p2) """ v1 = Vector3(2.5, 1.8, 2.9) v2 = Vector3(2.5, 1.8, 3.9) v3 = Vector3(0.0, 0.0, 0.0) p1 = Vec3Arg('p1', v1) p2 = Vec3Arg('p2', v2) p3 = Vec3Arg('p3', v3) shader = Shader(code=code, args=[p1, p2, p3]) shader.compile() shader.prepare([Runtime()]) shader.execute() v4 = v1.cross(v2) p = shader.get_value('p3') self.assertAlmostEqual(p.x, v4.x, places=6) self.assertAlmostEqual(p.y, v4.y, places=6) self.assertAlmostEqual(p.z, v4.z, places=6)
def test_struct(self): code = """ a1 = MyPoint() tmp = 66 a1.x = tmp p1 = a1.x tmp = 4.4 a1.y = tmp p2 = a1.y tmp = (6, 7) a1.k = tmp p3 = a1.k tmp = (3, 4, 6) a1.m = tmp p4 = a1.m tmp = (2, 4, 6, 7) a1.p = tmp p5 = a1.p """ #p1 = IntArg('p1', 44) #TODO implicit conversion int to float p1 = IntArg('p1', 44) p2 = FloatArg('p2', 2.2) p3 = Vec2Arg('p3', Vector2(5.5, 7.7)) p4 = Vec3Arg('p4', Vector3(2.2, 2.2, 4.4)) p5 = Vec4Arg('p5', Vector4(8.8, 5.5, 3.3, 1.1)) shader = Shader(code=code, args=[p1, p2, p3, p4, p5]) shader.compile() shader.prepare([Runtime()]) shader.execute() v = shader.get_value('p1') self.assertEqual(v, 66) v = shader.get_value('p2') self.assertAlmostEqual(v, 4.4, places=6) v = shader.get_value('p3') self.assertAlmostEqual(v.x, 6.0, places=6) self.assertAlmostEqual(v.y, 7.0, places=6) v = shader.get_value('p4') self.assertAlmostEqual(v.x, 3.0, places=6) self.assertAlmostEqual(v.y, 4.0, places=6) self.assertAlmostEqual(v.z, 6.0, places=6) v = shader.get_value('p5') self.assertAlmostEqual(v.x, 2.0, places=6) self.assertAlmostEqual(v.y, 4.0, places=6) self.assertAlmostEqual(v.z, 6.0, places=6) self.assertAlmostEqual(v.w, 7.0, places=6)
def test_float3(self): code = """ temp = 5 temp2 = 9.9 p1 = float3(temp, temp2, 2) tmp = 4.4 p2 = float3(tmp, 33, 2.1) """ p1 = Vec3Arg('p1', Vector3(0.0, 1.0, 0.0)) p2 = Vec3Arg('p2', Vector3(0.0, 1.0, 0.0)) shader = Shader(code=code, args=[p1, p2]) shader.compile() shader.prepare([Runtime()]) shader.execute() p = shader.get_value('p1') self.assertAlmostEqual(p.x, 5.0) self.assertAlmostEqual(p.y, 9.9, places=6) self.assertAlmostEqual(p.z, 2.0) p = shader.get_value('p2') self.assertAlmostEqual(p.x, 4.4, places=6) self.assertAlmostEqual(p.y, 33.0, places=6) self.assertAlmostEqual(p.z, 2.1, places=6)
def test_acos(self): code = """ val = 0.5 t1 = acos(val) val = (0.6, 0.3, -0.3) t2 = acos(val) """ t1 = FloatArg('t1', 0.0) t2 = Vec3Arg('t2', Vector3(1, 4.4, 29)) shader = Shader(code=code, args=[t1, t2]) shader.compile() shader.prepare([Runtime()]) shader.execute() v = shader.get_value('t1') self.assertAlmostEqual(v, acos(0.5), places=2) v = shader.get_value('t2') self.assertAlmostEqual(v.x, acos(0.6), places=2) self.assertAlmostEqual(v.y, acos(0.3), places=3) self.assertAlmostEqual(v.z, acos(-0.3), places=3)
def test_log(self): code = """ val = 4.4 t1 = log(val) val = (4.6, 6.6, 9.9) t2 = log(val) """ t1 = FloatArg('t1', 0.0) t2 = Vec3Arg('t2', Vector3(1, 4.4, 29)) shader = Shader(code=code, args=[t1, t2]) shader.compile() shader.prepare([Runtime()]) shader.execute() v = shader.get_value('t1') self.assertAlmostEqual(v, log(4.4), places=5) v = shader.get_value('t2') self.assertAlmostEqual(v.x, log(4.6), places=4) self.assertAlmostEqual(v.y, log(6.6), places=4) self.assertAlmostEqual(v.z, log(9.9), places=4)
def test_exp(self): code = """ val = 4.4 t1 = exp(val) val = (4.6, 1.6, 2.9) t2 = exp(val) """ t1 = FloatArg('t1', 0.0) t2 = Vec3Arg('t2', Vector3(1, 4.4, 29)) shader = Shader(code=code, args=[t1, t2]) shader.compile() shader.prepare([Runtime()]) shader.execute() v = shader.get_value('t1') self.assertAlmostEqual(v, exp(4.4), places=2) v = shader.get_value('t2') self.assertAlmostEqual(v.x, exp(4.6), places=2) self.assertAlmostEqual(v.y, exp(1.6), places=3) self.assertAlmostEqual(v.z, exp(2.9), places=3)
def test_atanr2(self): code = """ val1 = 1.4 val2 = 1.2 t1 = atanr2(val1, val2) val1 = (1.6, 1.3, 2.2) val2 = (1.1, 1.2, 1.3) t2 = atanr2(val1, val2) """ t1 = FloatArg('t1', 0.0) t2 = Vec3Arg('t2', Vector3(1, 4.4, 29)) shader = Shader(code=code, args=[t1, t2]) shader.compile() shader.prepare([Runtime()]) shader.execute() v = shader.get_value('t1') self.assertAlmostEqual(v, atan2(1.4, 1.0 / 1.2), places=3) v = shader.get_value('t2') self.assertAlmostEqual(v.x, atan2(1.6, 1.0 / 1.1), places=3) self.assertAlmostEqual(v.y, atan2(1.3, 1.0 / 1.2), places=3) self.assertAlmostEqual(v.z, atan2(2.2, 1.0 / 1.3), places=3)
def test_abs_fun(self): code = """ p1 = abs(-41) p2 = abs(-3.66) p3 = abs((-2.5, 3.6, -1.2)) """ p1 = IntArg('p1', 0) p2 = FloatArg('p2', 0.0) p3 = Vec3Arg('p3', Vector3(0.0, 0.0, 0.0)) shader = Shader(code=code, args=[p1, p2, p3]) shader.compile() shader.prepare([Runtime()]) shader.execute() p = shader.get_value('p1') self.assertEqual(shader.get_value('p1'), 41) self.assertAlmostEqual(shader.get_value('p2'), 3.66, places=6) v = shader.get_value('p3') self.assertAlmostEqual(v.x, 2.5, places=6) self.assertAlmostEqual(v.y, 3.6, places=6) self.assertAlmostEqual(v.z, 1.2, places=6)
import unittest from tdasm import Runtime from sdl.vector import Vector2, Vector3, Vector4 from sdl.shader import Shader from sdl.args import IntArg, FloatArg, Vec2Arg, Vec3Arg, Vec4Arg code = """ p1 = rand_int() p2 = random() p3 = random2() p4 = random3() p5 = random4() """ p1 = IntArg('p1', 333) p2 = FloatArg('p2', 333.0) p3 = Vec2Arg('p3', Vector2(0.0, 0.0)) p4 = Vec3Arg('p4', Vector3(0.0, 0.0, 0.0)) p5 = Vec4Arg('p5', Vector4(0.0, 0.0, 0.0, 0.0)) shader = Shader(code=code, args=[p1, p2, p3, p4, p5]) shader.compile() shader.prepare([Runtime()]) shader.execute() print(shader.get_value('p1')) print(shader.get_value('p2')) print(shader.get_value('p3')) print(shader.get_value('p4')) print(shader.get_value('p5'))