test_cg_shader.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import os
  2. import platform
  3. import pytest
  4. from _pytest.outcomes import Failed
  5. from panda3d import core
  6. SHADERS_DIR = core.Filename.from_os_specific(os.path.dirname(__file__))
  7. def run_cg_compile_check(shader_path, expect_fail=False):
  8. """Compile supplied Cg shader path and check for errors"""
  9. shader = core.Shader.load(shader_path, core.Shader.SL_Cg)
  10. # assert shader.is_prepared(gsg.prepared_objects)
  11. if expect_fail:
  12. assert shader is None
  13. else:
  14. assert shader is not None
  15. def test_cg_compile_error():
  16. """Test getting compile errors from bad Cg shaders"""
  17. shader_path = core.Filename(SHADERS_DIR, 'cg_bad.sha')
  18. run_cg_compile_check(shader_path, expect_fail=True)
  19. def test_cg_from_file():
  20. """Test compiling Cg shaders from files"""
  21. shader_path = core.Filename(SHADERS_DIR, 'cg_simple.sha')
  22. run_cg_compile_check(shader_path)
  23. def test_cg_test(env):
  24. "Test to make sure that the Cg tests work correctly."
  25. env.run_cg("assert(true);")
  26. def test_cg_test_fail(env):
  27. "Same as above, but making sure that the failure case works correctly."
  28. with pytest.raises(Failed):
  29. env.run_cg("assert(false);")
  30. def test_cg_sampler(env):
  31. tex1 = core.Texture("tex1-ubyte-rgba8")
  32. tex1.setup_1d_texture(1, core.Texture.T_unsigned_byte, core.Texture.F_rgba8)
  33. tex1.set_clear_color((0, 2 / 255.0, 1, 1))
  34. tex2 = core.Texture("tex2-float-rgba32")
  35. tex2.setup_2d_texture(1, 1, core.Texture.T_float, core.Texture.F_rgba32)
  36. tex2.set_clear_color((1.0, 2.0, -3.14, 0.0))
  37. tex3 = core.Texture("tex3-float-r32")
  38. tex3.setup_3d_texture(1, 1, 1, core.Texture.T_float, core.Texture.F_r32)
  39. tex3.set_clear_color((0.5, 0.0, 0.0, 1.0))
  40. preamble = """
  41. uniform sampler1D tex1;
  42. uniform sampler2D tex2;
  43. uniform sampler3D tex3;
  44. """
  45. code = """
  46. assert(tex1D(tex1, 0) == float4(0, 2 / 255.0, 1, 1));
  47. assert(tex2D(tex2, float2(0, 0)) == float4(1.0, 2.0, -3.14, 0.0));
  48. assert(abs(tex3D(tex3, float3(0, 0, 0)).r - 0.5) < 0.01);
  49. """
  50. env.run_cg(code, preamble, {'tex1': tex1, 'tex2': tex2, 'tex3': tex3})
  51. def test_cg_int(env):
  52. inputs = dict(
  53. zero=0,
  54. ten=10,
  55. intmax=0x7fffffff,
  56. intmin=-0x7fffffff,
  57. )
  58. preamble = """
  59. uniform int zero;
  60. uniform int intmax;
  61. uniform int intmin;
  62. """
  63. code = """
  64. assert(zero == 0);
  65. assert(intmax == 0x7fffffff);
  66. assert(intmin == -0x7fffffff);
  67. """
  68. env.run_cg(code, preamble, inputs)
  69. def test_cg_state_material(env):
  70. mat = core.Material("mat")
  71. mat.ambient = (1, 2, 3, 4)
  72. mat.diffuse = (5, 6, 7, 8)
  73. mat.emission = (9, 10, 11, 12)
  74. mat.specular = (13, 14, 15, 0)
  75. mat.shininess = 16
  76. preamble = """
  77. uniform float4x4 attr_material;
  78. """
  79. code = """
  80. assert(attr_material[0] == float4(1, 2, 3, 4));
  81. assert(attr_material[1] == float4(5, 6, 7, 8));
  82. assert(attr_material[2] == float4(9, 10, 11, 12));
  83. assert(attr_material[3].rgb == float3(13, 14, 15));
  84. assert(attr_material[3].w == 16);
  85. """
  86. node = core.NodePath("state")
  87. node.set_material(mat)
  88. env.run_cg(code, preamble, state=node.get_state())
  89. def test_cg_state_color(env):
  90. node = core.NodePath("state")
  91. node.set_color((1, 2, 3, 4))
  92. preamble = """
  93. uniform float4 attr_color;
  94. """
  95. code = """
  96. assert(attr_color == float4(1, 2, 3, 4));
  97. """
  98. env.run_cg(code, preamble, state=node.get_state())
  99. preamble = """
  100. uniform float3 attr_color;
  101. """
  102. code = """
  103. assert(attr_color == float3(1, 2, 3));
  104. """
  105. env.run_cg(code, preamble, state=node.get_state())
  106. def test_cg_state_color_scale(env):
  107. node = core.NodePath("state")
  108. node.set_color_scale((1, 2, 3, 4))
  109. preamble = """
  110. uniform float4 attr_colorscale;
  111. """
  112. code = """
  113. assert(attr_colorscale == float4(1, 2, 3, 4));
  114. """
  115. env.run_cg(code, preamble, state=node.get_state())
  116. preamble = """
  117. uniform float3 attr_colorscale;
  118. """
  119. code = """
  120. assert(attr_colorscale == float3(1, 2, 3));
  121. """
  122. env.run_cg(code, preamble, state=node.get_state())
  123. def test_cg_state_fog(env):
  124. fog = core.Fog("fog")
  125. fog.color = (1, 2, 3, 4)
  126. fog.exp_density = 0.5
  127. fog.set_linear_range(6, 10)
  128. preamble = """
  129. uniform float4 attr_fog;
  130. uniform float4 attr_fogcolor;
  131. """
  132. code = """
  133. assert(attr_fogcolor == float4(1, 2, 3, 4));
  134. assert(attr_fog[0] == 0.5);
  135. assert(attr_fog[1] == 6);
  136. assert(attr_fog[2] == 10);
  137. assert(attr_fog[3] == 0.25);
  138. """
  139. node = core.NodePath("state")
  140. node.set_fog(fog)
  141. env.run_cg(code, preamble, state=node.get_state())
  142. def test_cg_state_ambient(env):
  143. alight1 = core.AmbientLight("alight1")
  144. alight1.set_color((1, 0, 0, 0))
  145. alight2 = core.AmbientLight("alight2")
  146. alight2.set_color((0, 2, 0, 0))
  147. preamble = """
  148. uniform float3 attr_ambient;
  149. """
  150. code = """
  151. assert(attr_ambient == float3(1, 2, 0));
  152. """
  153. node = core.NodePath("state")
  154. node.set_light(node.attach_new_node(alight1))
  155. node.set_light(node.attach_new_node(alight2))
  156. env.run_cg(code, preamble, state=node.get_state())
  157. def test_cg_state_light(env):
  158. alight1 = core.AmbientLight("alight1")
  159. alight1.set_color((1, 0, 0, 0))
  160. alight2 = core.AmbientLight("alight2")
  161. alight2.set_color((0, 2, 0, 0))
  162. preamble = """
  163. uniform float4x4 attr_light0;
  164. uniform float4x4 attr_light1;
  165. uniform float4 attr_lspec0;
  166. uniform float4 attr_lspec1;
  167. """
  168. code = """
  169. assert(attr_light0[0] == float4(1, 2, 3, 4));
  170. assert(attr_light0[1] == float4(12, 13, 14, 100));
  171. assert(attr_light0[3] == float4(9, 10, 11, 1));
  172. assert(attr_light1[0] == float4(15, 16, 17, 18));
  173. assert(attr_light1[3] == float4(0, 1, 0, 0));
  174. assert(attr_light1[1] == float4(1, 0, 0, 0));
  175. assert(attr_lspec0 == float4(5, 6, 7, 8));
  176. assert(attr_lspec1 == float4(19, 20, 21, 22));
  177. """
  178. plight = core.PointLight("plight")
  179. plight.priority = 0
  180. plight.color = (1, 2, 3, 4)
  181. plight.specular_color = (5, 6, 7, 8)
  182. plight.transform = core.TransformState.make_pos((9, 10, 11))
  183. plight.attenuation = (12, 13, 14)
  184. plight.get_lens().set_far(100)
  185. plight_path = core.NodePath(plight)
  186. dlight = core.DirectionalLight("dlight")
  187. dlight.priority = -1
  188. dlight.direction = (0, -1, 0)
  189. dlight.color = (15, 16, 17, 18)
  190. dlight.specular_color = (19, 20, 21, 22)
  191. dlight.transform = core.TransformState.make_pos((23, 24, 25))
  192. dlight_path = core.NodePath(dlight)
  193. lattr = core.LightAttrib.make()
  194. lattr = lattr.add_on_light(plight_path)
  195. lattr = lattr.add_on_light(dlight_path)
  196. state = core.RenderState.make(lattr)
  197. env.run_cg(code, preamble, state=state)
  198. def test_cg_alight(env):
  199. alight = core.AmbientLight("alight")
  200. alight.set_color((1, 2, 3, 4))
  201. np = core.NodePath(alight)
  202. preamble = """
  203. uniform float4 alight_test;
  204. """
  205. code = """
  206. assert(alight_test == float4(1, 2, 3, 4));
  207. """
  208. env.run_cg(code, preamble, inputs={"test": np})
  209. def test_cg_satten(env):
  210. spot = core.Spotlight("spot")
  211. spot.set_attenuation((1, 2, 3))
  212. spot.set_exponent(4)
  213. np = core.NodePath(spot)
  214. preamble = """
  215. uniform float4 satten_test;
  216. """
  217. code = """
  218. assert(satten_test == float4(1, 2, 3, 4));
  219. """
  220. env.run_cg(code, preamble, inputs={"test": np})
  221. def test_cg_texpad_texpix(env):
  222. tex = core.Texture("test")
  223. tex.setup_2d_texture(16, 32, core.Texture.T_unsigned_byte, core.Texture.F_rgba)
  224. tex.auto_texture_scale = core.ATS_pad
  225. tex.set_size_padded(10, 30)
  226. preamble = """
  227. uniform float3 texpad_test;
  228. uniform float2 texpix_test;
  229. """
  230. code = """
  231. assert(texpad_test == float3(10 * 0.5 / 16, 30 * 0.5 / 32, 0.5));
  232. assert(texpix_test == float2(1.0 / 16, 1.0 / 32));
  233. """
  234. env.run_cg(code, preamble, inputs={"test": tex})
  235. def test_cg_k(env):
  236. inputs = dict(
  237. ten=10,
  238. vec=(1, 2, 3, 4),
  239. )
  240. preamble = """
  241. uniform int ten;
  242. uniform float4 vec;
  243. """
  244. code = """
  245. assert(ten == 10);
  246. assert(vec == float4(1, 2, 3, 4));
  247. """
  248. env.run_cg(code, preamble, inputs)