def test_sampled_spec_to_vec(self): mgr = SampledManager() shader = sampled_to_vec_shader(mgr) shader.compile() runtime = Runtime() shader.prepare([runtime]) code = """ rez = spectrum_to_vec(r1) """ vals = [(450, 0.13), (480, 0.45), (620, 0.58)] samples = create_samples(vals, 32, 400, 700) sam_spec = SampledSpectrum(samples) s = SampledArg('r1', sam_spec) rez = Vec3Arg('rez', Vector3(0.0, 0.0, 0.0)) shader2 = Shader(code=code, args=[rez, s]) shader2.compile([shader]) shader2.prepare([runtime]) shader2.execute() val = shader2.get_value('rez') conv = mgr.sampled_to_rgb(sam_spec) self.assertAlmostEqual(val.x, conv.r, places=6) self.assertAlmostEqual(val.y, conv.g, places=6) self.assertAlmostEqual(val.z, conv.b, places=6)
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_rgb_spec_to_vec(self): shader = rgb_to_vec_shader() shader.compile() runtime = Runtime() shader.prepare([runtime]) code = """ rez = spectrum_to_vec(r1) """ s = RGBArg('r1', RGBSpectrum(0.2, 0.3, 0.4)) rez = Vec3Arg('rez', Vector3(0.0, 0.0, 0.0)) shader2 = Shader(code=code, args=[rez, s]) shader2.compile([shader]) shader2.prepare([runtime]) shader2.execute() val = shader2.get_value('rez') self.assertAlmostEqual(val.x, 0.2) self.assertAlmostEqual(val.y, 0.3) self.assertAlmostEqual(val.z, 0.4)
def test_assign(self): code = """ k = 55 p1 = k m = (4, 7) p2 = m h = (3.13, 8, 9) p3 = h t = (6.6, 2.2, 9, 1) p4 = t r = 3.3 p5 = r """ p1 = IntArg('p1', 33) p2 = Vec2Arg('p2', Vector2(9, 22)) p3 = Vec3Arg('p3', Vector3(1, 4.4, 29)) p4 = Vec4Arg('p4', Vector4(2, 3, 1.1, 2)) p5 = FloatArg('p5', 87.33) shader = Shader(code=code, args=[p1, p2, p3, p4, p5]) shader.compile() shader.prepare([Runtime()]) shader.execute() self.assertEqual(shader.get_value('p1'), 55) v = shader.get_value('p2') self.assertAlmostEqual(v.x, 4.0) self.assertAlmostEqual(v.y, 7.0) v = shader.get_value('p3') self.assertAlmostEqual(v.x, 3.13, places=6) self.assertAlmostEqual(v.y, 8.0) self.assertAlmostEqual(v.z, 9.0) v = shader.get_value('p4') self.assertAlmostEqual(v.x, 6.6, places=6) self.assertAlmostEqual(v.y, 2.2) self.assertAlmostEqual(v.z, 9.0) self.assertAlmostEqual(v.w, 1.0) v = shader.get_value('p5') self.assertAlmostEqual(v, 3.3)