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)
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)
def test_int(self): code = """ temp = 5.6 p1 = int(temp) p2 = int(8.8) p3 = int() p4 = int(11) """ p1 = IntArg('p1', 333) p2 = IntArg('p2', 555) p3 = IntArg('p3', 555) p4 = IntArg('p4', 555) shader = Shader(code=code, args=[p1, p2, p3, p4]) shader.compile() shader.prepare([Runtime()]) shader.execute() p = shader.get_value('p1') self.assertEqual(p, 5) p = shader.get_value('p2') self.assertEqual(p, 8) p = shader.get_value('p3') self.assertEqual(p, 0) p = shader.get_value('p4') self.assertEqual(p, 11)
def test_rgb_arithmetic(self): code = """ tmp = p1 + p2 p3 = tmp p4 = 2 * p3 p5 = p3 * 2.66 """ p1 = RGBArg('p1', RGBSpectrum(0.36, 0.85, 0.14)) p2 = RGBArg('p2', RGBSpectrum(0.22, 0.11, 0.58)) p3 = RGBArg('p3', RGBSpectrum(0.0, 0.0, 0.0)) p4 = RGBArg('p4', RGBSpectrum(0.0, 0.0, 0.0)) p5 = RGBArg('p5', RGBSpectrum(0.0, 0.0, 0.0)) shader = Shader(code=code, args=[p1, p2, p3, p4, p5]) shader.compile() shader.prepare([Runtime()]) shader.execute() v = shader.get_value('p3') self.assertAlmostEqual(v.r, 0.36 + 0.22) self.assertAlmostEqual(v.g, 0.85 + 0.11) self.assertAlmostEqual(v.b, 0.14 + 0.58) v = shader.get_value('p4') self.assertAlmostEqual(v.r, (0.36 + 0.22) * 2, places=6) self.assertAlmostEqual(v.g, (0.85 + 0.11) * 2, places=6) self.assertAlmostEqual(v.b, (0.14 + 0.58) * 2, places=6) v = shader.get_value('p5') self.assertAlmostEqual(v.r, (0.36 + 0.22) * 2.66, places=6) self.assertAlmostEqual(v.g, (0.85 + 0.11) * 2.66, places=6) self.assertAlmostEqual(v.b, (0.14 + 0.58) * 2.66, places=6)
def test_expr(self): #TODO code = """ """ shader = Shader(code=code, args=[]) shader.compile() shader.prepare([Runtime()]) shader.execute()
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_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_rgb_spectrum(self): code = """ spec = Spectrum(0.23) """ spec = RGBArg('spec', RGBSpectrum(0.2, 0.3, 0.4)) shader = Shader(code=code, args=[spec]) shader.compile() runtime = Runtime() shader.prepare([runtime]) shader.execute() val = shader.get_value('spec') self.assertAlmostEqual(val.r, 0.23) self.assertAlmostEqual(val.g, 0.23) self.assertAlmostEqual(val.b, 0.23)
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)
def test_set_prgba(self): code = """ val = (0.67, 0.88, 0.11, 0.55) v1 = set_rgba(image1, 10, 10, val) """ image = ImagePRGBA(200, 200) p1 = StructArg('image1', image) shader = Shader(code=code, args=[p1]) shader.compile() shader.prepare([Runtime()]) shader.execute() r, g, b, a = image.get_pixel(10, 10) self.assertAlmostEqual(r, 0.67) self.assertAlmostEqual(g, 0.88) self.assertAlmostEqual(b, 0.11) self.assertAlmostEqual(a, 0.55)
def test_float4(self): code = """ temp = 5 temp2 = 9.9 p1 = float4(temp, temp2, 2, 5.5) """ p1 = Vec4Arg('p1', Vector4(0.0, 1.0, 0.0, 0.0)) shader = Shader(code=code, args=[p1]) 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) self.assertAlmostEqual(p.w, 5.5)
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_get_rgba(self): code = """ v1 = get_rgba(image1, 10, 10) """ image = ImageRGBA(200, 200) image.set_pixel(10, 10, 25, 77, 142, 185) p1 = StructArg('image1', image) p2 = Vec4Arg('v1', Vector4(0.0, 0.0, 0.0, 0.0)) shader = Shader(code=code, args=[p1, p2]) shader.compile() shader.prepare([Runtime()]) shader.execute() val = shader.get_value('v1') self.assertAlmostEqual(val.x, 25 * 0.0039, places=6) self.assertAlmostEqual(val.y, 77 * 0.0039) self.assertAlmostEqual(val.z, 142 * 0.0039) self.assertAlmostEqual(val.w, 185 * 0.0039)
def test_get_prgba(self): code = """ v1 = get_rgba(image1, 10, 10) """ image = ImagePRGBA(200, 200) image.set_pixel(10, 10, 0.23, 0.28, 0.55, 0.8) p1 = StructArg('image1', image) p2 = Vec4Arg('v1', Vector4(0.0, 0.0, 0.0, 0.0)) shader = Shader(code=code, args=[p1, p2]) shader.compile() shader.prepare([Runtime()]) shader.execute() val = shader.get_value('v1') self.assertAlmostEqual(val.x, 0.23) self.assertAlmostEqual(val.y, 0.28) self.assertAlmostEqual(val.z, 0.55) self.assertAlmostEqual(val.w, 0.8)
def test_sampled_spectrum(self): code = """ spec = Spectrum(0.23) """ vals = [(450, 0.13), (480, 0.45), (620, 0.58)] samples = create_samples(vals, 32, 400, 700) sam_spec = SampledSpectrum(samples) spec = SampledArg('spec', sam_spec) shader = Shader(code=code, args=[spec]) shader.compile(color_mgr=SampledManager()) runtime = Runtime() shader.prepare([runtime]) shader.execute() val = shader.get_value('spec') for i in range(len(val.samples)): self.assertAlmostEqual(val.samples[i], 0.23)
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_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_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_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_float2(self): code = """ temp = 5 temp2 = 9.9 p1 = float2(temp, temp2) p2 = float2(2, 3.3) """ p1 = Vec2Arg('p1', Vector2(0.0, 1.0)) p2 = Vec2Arg('p2', Vector2(0.0, 1.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) p = shader.get_value('p2') self.assertAlmostEqual(p.x, 2.0) self.assertAlmostEqual(p.y, 3.3)
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_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)
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)
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'))
def test_sampled_arithmetic(self): code = """ tmp = p2 + p1 p3 = tmp p4 = p1 * 0.3 p5 = 0.22 * p2 p6 = p1 * 8 p7 = 5 * p2 """ vals = [(450, 0.33), (480, 0.45)] samples = create_samples(vals, 32, 400, 700) p1 = SampledArg('p1', SampledSpectrum(samples)) vals = [(430, 0.23), (480, 0.55), (600, 0.77)] samples2 = create_samples(vals, 32, 400, 700) p2 = SampledArg('p2', SampledSpectrum(samples2)) vals = [(430, 0.1), (480, 0.1), (600, 0.1)] samples3 = create_samples(vals, 32, 400, 700) p3 = SampledArg('p3', SampledSpectrum(samples3)) vals = [(430, 0.1), (480, 0.1), (600, 0.1)] samples4 = create_samples(vals, 32, 400, 700) p4 = SampledArg('p4', SampledSpectrum(samples4)) vals = [(430, 0.1), (480, 0.1), (600, 0.1)] samples5 = create_samples(vals, 32, 400, 700) p5 = SampledArg('p5', SampledSpectrum(samples5)) vals = [(430, 0.1), (480, 0.1), (600, 0.1)] samples6 = create_samples(vals, 32, 400, 700) p6 = SampledArg('p6', SampledSpectrum(samples6)) vals = [(430, 0.1), (480, 0.1), (600, 0.1)] samples7 = create_samples(vals, 32, 400, 700) p7 = SampledArg('p7', SampledSpectrum(samples7)) shader = Shader(code=code, args=[p1, p2, p3, p4, p5, p6, p7]) shader.compile(color_mgr=SampledManager()) shader.prepare([Runtime()]) shader.execute() v = shader.get_value('p3') for i in range(32): val = samples[i] + samples2[i] self.assertAlmostEqual(v.samples[i], val, places=6) v = shader.get_value('p4') for i in range(32): self.assertAlmostEqual(v.samples[i], samples[i] * 0.3) v = shader.get_value('p5') for i in range(32): self.assertAlmostEqual(v.samples[i], samples2[i] * 0.22) v = shader.get_value('p6') for i in range(32): self.assertAlmostEqual(v.samples[i], samples[i] * 8, places=6) v = shader.get_value('p7') for i in range(32): self.assertAlmostEqual(v.samples[i], samples2[i] * 5, places=6)