test_color_buffer.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. from panda3d import core
  2. import pytest
  3. TEST_COLOR = core.LColor(1, 127/255.0, 0, 127/255.0)
  4. TEST_COLOR_SCALE = core.LVecBase4(0.5, 0.5, 0.5, 0.5)
  5. TEST_SCALED_COLOR = core.LColor(TEST_COLOR)
  6. TEST_SCALED_COLOR.componentwise_mult(TEST_COLOR_SCALE)
  7. FUZZ = 0.02
  8. @pytest.fixture(scope='session', params=[False, True], ids=["shader:off", "shader:auto"])
  9. def shader_attrib(request):
  10. """Returns two ShaderAttribs: one with auto shader, one without."""
  11. if request.param:
  12. return core.ShaderAttrib.make_default().set_shader_auto(True)
  13. else:
  14. return core.ShaderAttrib.make_off()
  15. @pytest.fixture(scope='session', params=["mat:off", "mat:empty", "mat:amb", "mat:diff", "mat:both"])
  16. def material_attrib(request):
  17. """Returns two MaterialAttribs: one with material, one without. It
  18. shouldn't really matter what we set them to, since the tests in here do
  19. not use lighting, and therefore the material should be ignored."""
  20. if request.param == "mat:off":
  21. return core.MaterialAttrib.make_off()
  22. elif request.param == "mat:empty":
  23. return core.MaterialAttrib.make(core.Material())
  24. elif request.param == "mat:amb":
  25. mat = core.Material()
  26. mat.ambient = (0.1, 1, 0.5, 1)
  27. return core.MaterialAttrib.make(mat)
  28. elif request.param == "mat:diff":
  29. mat = core.Material()
  30. mat.diffuse = (0.1, 1, 0.5, 1)
  31. return core.MaterialAttrib.make(mat)
  32. elif request.param == "mat:both":
  33. mat = core.Material()
  34. mat.diffuse = (0.1, 1, 0.5, 1)
  35. mat.ambient = (0.1, 1, 0.5, 1)
  36. return core.MaterialAttrib.make(mat)
  37. @pytest.fixture(scope='module', params=[False, True], ids=["srgb:off", "srgb:on"])
  38. def color_region(request, graphics_pipe):
  39. """Creates and returns a DisplayRegion with a depth buffer."""
  40. engine = core.GraphicsEngine()
  41. engine.set_threading_model("")
  42. host_fbprops = core.FrameBufferProperties()
  43. host_fbprops.force_hardware = True
  44. host = engine.make_output(
  45. graphics_pipe,
  46. 'host',
  47. 0,
  48. host_fbprops,
  49. core.WindowProperties.size(32, 32),
  50. core.GraphicsPipe.BF_refuse_window,
  51. )
  52. engine.open_windows()
  53. if host is None:
  54. pytest.skip("GraphicsPipe cannot make offscreen buffers")
  55. fbprops = core.FrameBufferProperties()
  56. fbprops.force_hardware = True
  57. fbprops.set_rgba_bits(8, 8, 8, 8)
  58. fbprops.srgb_color = request.param
  59. buffer = engine.make_output(
  60. graphics_pipe,
  61. 'buffer',
  62. 0,
  63. fbprops,
  64. core.WindowProperties.size(32, 32),
  65. core.GraphicsPipe.BF_refuse_window,
  66. host.gsg,
  67. host
  68. )
  69. engine.open_windows()
  70. if buffer is None:
  71. pytest.skip("Cannot make color buffer")
  72. if fbprops.srgb_color != buffer.get_fb_properties().srgb_color:
  73. pytest.skip("Cannot make buffer with required srgb_color setting")
  74. buffer.set_clear_color_active(True)
  75. buffer.set_clear_color((0, 0, 0, 1))
  76. yield buffer.make_display_region()
  77. if buffer is not None:
  78. engine.remove_window(buffer)
  79. def render_color_pixel(region, state, vertex_color=None):
  80. """Renders a fragment using the specified render settings, and returns the
  81. resulting color value."""
  82. # Set up the scene with a blank card rendering at specified distance.
  83. scene = core.NodePath("root")
  84. scene.set_attrib(core.DepthTestAttrib.make(core.RenderAttrib.M_always))
  85. camera = scene.attach_new_node(core.Camera("camera"))
  86. camera.node().get_lens(0).set_near_far(1, 3)
  87. camera.node().set_cull_bounds(core.OmniBoundingVolume())
  88. if vertex_color is not None:
  89. format = core.GeomVertexFormat.get_v3cp()
  90. else:
  91. format = core.GeomVertexFormat.get_v3()
  92. vdata = core.GeomVertexData("card", format, core.Geom.UH_static)
  93. vdata.unclean_set_num_rows(4)
  94. vertex = core.GeomVertexWriter(vdata, "vertex")
  95. vertex.set_data3(core.Vec3.rfu(-1, 0, 1))
  96. vertex.set_data3(core.Vec3.rfu(-1, 0, -1))
  97. vertex.set_data3(core.Vec3.rfu(1, 0, 1))
  98. vertex.set_data3(core.Vec3.rfu(1, 0, -1))
  99. if vertex_color is not None:
  100. color = core.GeomVertexWriter(vdata, "color")
  101. color.set_data4(vertex_color)
  102. color.set_data4(vertex_color)
  103. color.set_data4(vertex_color)
  104. color.set_data4(vertex_color)
  105. strip = core.GeomTristrips(core.Geom.UH_static)
  106. strip.set_shade_model(core.Geom.SM_uniform)
  107. strip.add_next_vertices(4)
  108. strip.close_primitive()
  109. geom = core.Geom(vdata)
  110. geom.add_primitive(strip)
  111. gnode = core.GeomNode("card")
  112. gnode.add_geom(geom, state)
  113. card = scene.attach_new_node(gnode)
  114. card.set_pos(0, 2, 0)
  115. card.set_scale(60)
  116. region.active = True
  117. region.camera = camera
  118. color_texture = core.Texture("color")
  119. region.window.add_render_texture(color_texture,
  120. core.GraphicsOutput.RTM_copy_ram,
  121. core.GraphicsOutput.RTP_color)
  122. region.window.engine.render_frame()
  123. region.window.clear_render_textures()
  124. col = core.LColor()
  125. color_texture.peek().lookup(col, 0.5, 0.5)
  126. return col
  127. def test_color_write_mask(color_region):
  128. state = core.RenderState.make(
  129. core.ColorWriteAttrib.make(core.ColorWriteAttrib.C_green),
  130. )
  131. result = render_color_pixel(color_region, state)
  132. assert result == (0, 1, 0, 1)
  133. def test_color_empty(color_region, shader_attrib, material_attrib):
  134. state = core.RenderState.make(
  135. shader_attrib,
  136. material_attrib,
  137. )
  138. result = render_color_pixel(color_region, state)
  139. assert result == (1, 1, 1, 1)
  140. def test_color_off(color_region, shader_attrib, material_attrib):
  141. state = core.RenderState.make(
  142. core.ColorAttrib.make_off(),
  143. shader_attrib,
  144. material_attrib,
  145. )
  146. result = render_color_pixel(color_region, state)
  147. assert result == (1, 1, 1, 1)
  148. def test_color_flat(color_region, shader_attrib, material_attrib):
  149. state = core.RenderState.make(
  150. core.ColorAttrib.make_flat(TEST_COLOR),
  151. shader_attrib,
  152. material_attrib,
  153. )
  154. result = render_color_pixel(color_region, state)
  155. assert result.almost_equal(TEST_COLOR, FUZZ)
  156. def test_color_vertex(color_region, shader_attrib, material_attrib):
  157. state = core.RenderState.make(
  158. core.ColorAttrib.make_vertex(),
  159. shader_attrib,
  160. material_attrib,
  161. )
  162. result = render_color_pixel(color_region, state, vertex_color=TEST_COLOR)
  163. assert result.almost_equal(TEST_COLOR, FUZZ)
  164. def test_color_empty_vertex(color_region, shader_attrib, material_attrib):
  165. state = core.RenderState.make(
  166. shader_attrib,
  167. material_attrib,
  168. )
  169. result = render_color_pixel(color_region, state, vertex_color=TEST_COLOR)
  170. assert result.almost_equal(TEST_COLOR, FUZZ)
  171. def test_color_off_vertex(color_region, shader_attrib, material_attrib):
  172. state = core.RenderState.make(
  173. core.ColorAttrib.make_off(),
  174. shader_attrib,
  175. material_attrib,
  176. )
  177. result = render_color_pixel(color_region, state, vertex_color=TEST_COLOR)
  178. assert result == (1, 1, 1, 1)
  179. def test_scaled_color_empty(color_region, shader_attrib, material_attrib):
  180. state = core.RenderState.make(
  181. shader_attrib,
  182. material_attrib,
  183. )
  184. result = render_color_pixel(color_region, state)
  185. assert result == (1, 1, 1, 1)
  186. def test_scaled_color_off(color_region, shader_attrib, material_attrib):
  187. state = core.RenderState.make(
  188. core.ColorAttrib.make_off(),
  189. shader_attrib,
  190. material_attrib,
  191. )
  192. result = render_color_pixel(color_region, state)
  193. assert result == (1, 1, 1, 1)
  194. def test_scaled_color_flat(color_region, shader_attrib, material_attrib):
  195. state = core.RenderState.make(
  196. core.ColorAttrib.make_flat(TEST_COLOR),
  197. core.ColorScaleAttrib.make(TEST_COLOR_SCALE),
  198. shader_attrib,
  199. material_attrib,
  200. )
  201. result = render_color_pixel(color_region, state)
  202. assert result.almost_equal(TEST_SCALED_COLOR, FUZZ)
  203. def test_scaled_color_vertex(color_region, shader_attrib, material_attrib):
  204. state = core.RenderState.make(
  205. core.ColorAttrib.make_vertex(),
  206. core.ColorScaleAttrib.make(TEST_COLOR_SCALE),
  207. shader_attrib,
  208. material_attrib,
  209. )
  210. result = render_color_pixel(color_region, state, vertex_color=TEST_COLOR)
  211. assert result.almost_equal(TEST_SCALED_COLOR, FUZZ)
  212. def test_scaled_color_empty_vertex(color_region, shader_attrib, material_attrib):
  213. state = core.RenderState.make(
  214. core.ColorScaleAttrib.make(TEST_COLOR_SCALE),
  215. shader_attrib,
  216. material_attrib,
  217. )
  218. result = render_color_pixel(color_region, state, vertex_color=TEST_COLOR)
  219. assert result.almost_equal(TEST_SCALED_COLOR, FUZZ)
  220. def test_scaled_color_off_vertex(color_region, shader_attrib, material_attrib):
  221. state = core.RenderState.make(
  222. core.ColorAttrib.make_off(),
  223. core.ColorScaleAttrib.make(TEST_COLOR_SCALE),
  224. shader_attrib,
  225. material_attrib,
  226. )
  227. result = render_color_pixel(color_region, state, vertex_color=TEST_COLOR)
  228. assert result.almost_equal(TEST_COLOR_SCALE, FUZZ)