test_cg_shader.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_fog(env):
  90. fog = core.Fog("fog")
  91. fog.color = (1, 2, 3, 4)
  92. fog.exp_density = 0.5
  93. fog.set_linear_range(6, 10)
  94. preamble = """
  95. uniform float4 attr_fog;
  96. uniform float4 attr_fogcolor;
  97. """
  98. code = """
  99. assert(attr_fogcolor == float4(1, 2, 3, 4));
  100. assert(attr_fog[0] == 0.5);
  101. assert(attr_fog[1] == 6);
  102. assert(attr_fog[2] == 10);
  103. assert(attr_fog[3] == 0.25);
  104. """
  105. node = core.NodePath("state")
  106. node.set_fog(fog)
  107. env.run_cg(code, preamble, state=node.get_state())
  108. def test_cg_texpad_texpix(env):
  109. tex = core.Texture("test")
  110. tex.setup_2d_texture(16, 32, core.Texture.T_unsigned_byte, core.Texture.F_rgba)
  111. tex.auto_texture_scale = core.ATS_pad
  112. tex.set_size_padded(10, 30)
  113. preamble = """
  114. uniform float3 texpad_test;
  115. uniform float2 texpix_test;
  116. """
  117. code = """
  118. assert(texpad_test == float3(10 * 0.5 / 16, 30 * 0.5 / 32, 0.5));
  119. assert(texpix_test == float2(1.0 / 16, 1.0 / 32));
  120. """
  121. env.run_cg(code, preamble, inputs={"test": tex})
  122. def test_cg_alight(env):
  123. alight = core.AmbientLight("alight")
  124. alight.set_color((1, 2, 3, 4))
  125. np = core.NodePath(alight)
  126. preamble = """
  127. uniform float4 alight_test;
  128. """
  129. code = """
  130. assert(alight_test == float4(1, 2, 3, 4));
  131. """
  132. env.run_cg(code, preamble, inputs={"test": np})
  133. def test_cg_satten(env):
  134. spot = core.Spotlight("spot")
  135. spot.set_attenuation((1, 2, 3))
  136. spot.set_exponent(4)
  137. np = core.NodePath(spot)
  138. preamble = """
  139. uniform float4 satten_test;
  140. """
  141. code = """
  142. assert(satten_test == float4(1, 2, 3, 4));
  143. """
  144. env.run_cg(code, preamble, inputs={"test": np})