예제 #1
0
    def test_float(self):
        code = """
temp = 5
p1 = float(temp)
p2 = float(88)
p3 = float()
p4 = float(6.6)
        """
        p1 = FloatArg('p1', 333.3)
        p2 = FloatArg('p2', 333.3)
        p3 = FloatArg('p3', 333.3)
        p4 = FloatArg('p4', 333.3)

        shader = Shader(code=code, args=[p1, p2, p3, p4])
        shader.compile()
        shader.prepare([Runtime()])
        shader.execute()

        p = shader.get_value('p1')
        self.assertAlmostEqual(p, 5.0)
        p = shader.get_value('p2')
        self.assertAlmostEqual(p, 88.0)
        p = shader.get_value('p3')
        self.assertAlmostEqual(p, 0.0)
        p = shader.get_value('p4')
        self.assertAlmostEqual(p, 6.6, places=6)
예제 #2
0
    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)
예제 #3
0
    def test_array(self):

        p = SuperPoint(3.0, 4.0)
        arr = ObjArray(p)
        arr.append(p)
        arr.append(p)
        p = SuperPoint(7.0, 9.0)
        arr.append(p)

        code = """
index = 2
temp = arr[index]
p1 = temp.x
temp.y = 12.34
        """
        arg = ArrayArg('arr', arr)
        arg1 = FloatArg('p1', 4.4)
        shader = Shader(code=code, args=[arg, arg1])
        shader.compile()
        shader.prepare([Runtime()])
        shader.execute()
        val = shader.get_value('p1')
        self.assertAlmostEqual(val, 7.0)
        obj = arr[2]
        self.assertAlmostEqual(obj.y, 12.34, 6)
예제 #4
0
    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)
예제 #5
0
    def test_call(self):
        code = """
tmp = p1 + p2 + p3
p4.x = 55.99
return tmp
        """
        p1 = FloatArg('p1', 2.0)
        p2 = IntArg('p2', 0)
        p3 = FloatArg('p3', 0.0)
        p4 = StructArgPtr('p4', TestPoint(4.4, 5.5))
        shader = Shader(code=code,
                        args=[],
                        name='adding',
                        func_args=[p1, p2, p3, p4],
                        is_func=True)
        shader.compile()
        shader.prepare(self.runtimes)

        code2 = """
p1 = 44
p2 = 7
point = TestPoint()
rez = adding(55.4, p1, p2, point)
rez2 = point.x
        """
        rez = FloatArg('rez', 11.33)
        rez2 = FloatArg('rez2', 1.0)
        shader2 = Shader(code=code2, args=[rez, rez2])
        shader2.compile([shader])
        shader2.prepare(self.runtimes)
        shader2.execute()

        rez = shader2.get_value('rez')
        rez2 = shader2.get_value('rez2')
        self.assertAlmostEqual(rez, 55.4 + 44 + 7, places=5)
        self.assertAlmostEqual(rez2, 55.99, places=5)
예제 #6
0
    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)
예제 #7
0
    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)
예제 #8
0
    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)
예제 #9
0
    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)
예제 #10
0
    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)
예제 #11
0
    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)
예제 #12
0
파일: rnd_test.py 프로젝트: mario007/renmas
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'))