test_color_buffer.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. # Vulkan needs no host window.
  43. if graphics_pipe.get_interface_name() != "Vulkan":
  44. host_fbprops = core.FrameBufferProperties()
  45. host_fbprops.force_hardware = True
  46. host = engine.make_output(
  47. graphics_pipe,
  48. 'host',
  49. 0,
  50. host_fbprops,
  51. core.WindowProperties.size(32, 32),
  52. core.GraphicsPipe.BF_refuse_window,
  53. )
  54. engine.open_windows()
  55. if host is None:
  56. pytest.skip("GraphicsPipe cannot make offscreen buffers")
  57. host_gsg = host.gsg
  58. else:
  59. host = None
  60. host_gsg = None
  61. fbprops = core.FrameBufferProperties()
  62. fbprops.force_hardware = True
  63. fbprops.set_rgba_bits(8, 8, 8, 8)
  64. fbprops.srgb_color = request.param
  65. buffer = engine.make_output(
  66. graphics_pipe,
  67. 'buffer',
  68. 0,
  69. fbprops,
  70. core.WindowProperties.size(32, 32),
  71. core.GraphicsPipe.BF_refuse_window,
  72. host_gsg,
  73. host
  74. )
  75. engine.open_windows()
  76. if buffer is None:
  77. pytest.skip("Cannot make color buffer")
  78. if fbprops.srgb_color != buffer.get_fb_properties().srgb_color:
  79. pytest.skip("Cannot make buffer with required srgb_color setting")
  80. buffer.set_clear_color_active(True)
  81. buffer.set_clear_color((0, 0, 0, 1))
  82. yield buffer.make_display_region()
  83. if buffer is not None:
  84. engine.remove_window(buffer)
  85. def render_color_pixel(region, state, vertex_color=None):
  86. """Renders a fragment using the specified render settings, and returns the
  87. resulting color value."""
  88. # Set up the scene with a blank card rendering at specified distance.
  89. scene = core.NodePath("root")
  90. scene.set_attrib(core.DepthTestAttrib.make(core.RenderAttrib.M_always))
  91. camera = scene.attach_new_node(core.Camera("camera"))
  92. camera.node().get_lens(0).set_near_far(1, 3)
  93. camera.node().set_cull_bounds(core.OmniBoundingVolume())
  94. if vertex_color is not None:
  95. format = core.GeomVertexFormat.get_v3cp()
  96. else:
  97. format = core.GeomVertexFormat.get_v3()
  98. vdata = core.GeomVertexData("card", format, core.Geom.UH_static)
  99. vdata.unclean_set_num_rows(4)
  100. vertex = core.GeomVertexWriter(vdata, "vertex")
  101. vertex.set_data3(core.Vec3.rfu(-1, 0, 1))
  102. vertex.set_data3(core.Vec3.rfu(-1, 0, -1))
  103. vertex.set_data3(core.Vec3.rfu(1, 0, 1))
  104. vertex.set_data3(core.Vec3.rfu(1, 0, -1))
  105. if vertex_color is not None:
  106. color = core.GeomVertexWriter(vdata, "color")
  107. color.set_data4(vertex_color)
  108. color.set_data4(vertex_color)
  109. color.set_data4(vertex_color)
  110. color.set_data4(vertex_color)
  111. strip = core.GeomTristrips(core.Geom.UH_static)
  112. strip.set_shade_model(core.Geom.SM_uniform)
  113. strip.add_next_vertices(4)
  114. strip.close_primitive()
  115. geom = core.Geom(vdata)
  116. geom.add_primitive(strip)
  117. gnode = core.GeomNode("card")
  118. gnode.add_geom(geom, state)
  119. card = scene.attach_new_node(gnode)
  120. card.set_pos(0, 2, 0)
  121. card.set_scale(60)
  122. region.active = True
  123. region.camera = camera
  124. color_texture = core.Texture("color")
  125. region.window.add_render_texture(color_texture,
  126. core.GraphicsOutput.RTM_copy_ram,
  127. core.GraphicsOutput.RTP_color)
  128. region.window.engine.render_frame()
  129. region.window.clear_render_textures()
  130. col = core.LColor()
  131. color_texture.peek().lookup(col, 0.5, 0.5)
  132. return col
  133. def test_color_write_mask(color_region):
  134. state = core.RenderState.make(
  135. core.ColorWriteAttrib.make(core.ColorWriteAttrib.C_green),
  136. )
  137. result = render_color_pixel(color_region, state)
  138. assert result == (0, 1, 0, 1)
  139. def test_color_empty(color_region, shader_attrib, material_attrib):
  140. state = core.RenderState.make(
  141. shader_attrib,
  142. material_attrib,
  143. )
  144. result = render_color_pixel(color_region, state)
  145. assert result == (1, 1, 1, 1)
  146. def test_color_off(color_region, shader_attrib, material_attrib):
  147. state = core.RenderState.make(
  148. core.ColorAttrib.make_off(),
  149. shader_attrib,
  150. material_attrib,
  151. )
  152. result = render_color_pixel(color_region, state)
  153. assert result == (1, 1, 1, 1)
  154. def test_color_flat(color_region, shader_attrib, material_attrib):
  155. state = core.RenderState.make(
  156. core.ColorAttrib.make_flat(TEST_COLOR),
  157. shader_attrib,
  158. material_attrib,
  159. )
  160. result = render_color_pixel(color_region, state)
  161. assert result.almost_equal(TEST_COLOR, FUZZ)
  162. def test_color_vertex(color_region, shader_attrib, material_attrib):
  163. state = core.RenderState.make(
  164. core.ColorAttrib.make_vertex(),
  165. shader_attrib,
  166. material_attrib,
  167. )
  168. result = render_color_pixel(color_region, state, vertex_color=TEST_COLOR)
  169. assert result.almost_equal(TEST_COLOR, FUZZ)
  170. def test_color_empty_vertex(color_region, shader_attrib, material_attrib):
  171. state = core.RenderState.make(
  172. shader_attrib,
  173. material_attrib,
  174. )
  175. result = render_color_pixel(color_region, state, vertex_color=TEST_COLOR)
  176. assert result.almost_equal(TEST_COLOR, FUZZ)
  177. def test_color_off_vertex(color_region, shader_attrib, material_attrib):
  178. state = core.RenderState.make(
  179. core.ColorAttrib.make_off(),
  180. shader_attrib,
  181. material_attrib,
  182. )
  183. result = render_color_pixel(color_region, state, vertex_color=TEST_COLOR)
  184. assert result == (1, 1, 1, 1)
  185. def test_scaled_color_empty(color_region, shader_attrib, material_attrib):
  186. state = core.RenderState.make(
  187. shader_attrib,
  188. material_attrib,
  189. )
  190. result = render_color_pixel(color_region, state)
  191. assert result == (1, 1, 1, 1)
  192. def test_scaled_color_off(color_region, shader_attrib, material_attrib):
  193. state = core.RenderState.make(
  194. core.ColorAttrib.make_off(),
  195. shader_attrib,
  196. material_attrib,
  197. )
  198. result = render_color_pixel(color_region, state)
  199. assert result == (1, 1, 1, 1)
  200. def test_scaled_color_flat(color_region, shader_attrib, material_attrib):
  201. state = core.RenderState.make(
  202. core.ColorAttrib.make_flat(TEST_COLOR),
  203. core.ColorScaleAttrib.make(TEST_COLOR_SCALE),
  204. shader_attrib,
  205. material_attrib,
  206. )
  207. result = render_color_pixel(color_region, state)
  208. assert result.almost_equal(TEST_SCALED_COLOR, FUZZ)
  209. def test_scaled_color_vertex(color_region, shader_attrib, material_attrib):
  210. state = core.RenderState.make(
  211. core.ColorAttrib.make_vertex(),
  212. core.ColorScaleAttrib.make(TEST_COLOR_SCALE),
  213. shader_attrib,
  214. material_attrib,
  215. )
  216. result = render_color_pixel(color_region, state, vertex_color=TEST_COLOR)
  217. assert result.almost_equal(TEST_SCALED_COLOR, FUZZ)
  218. def test_scaled_color_empty_vertex(color_region, shader_attrib, material_attrib):
  219. state = core.RenderState.make(
  220. core.ColorScaleAttrib.make(TEST_COLOR_SCALE),
  221. shader_attrib,
  222. material_attrib,
  223. )
  224. result = render_color_pixel(color_region, state, vertex_color=TEST_COLOR)
  225. assert result.almost_equal(TEST_SCALED_COLOR, FUZZ)
  226. def test_scaled_color_off_vertex(color_region, shader_attrib, material_attrib):
  227. state = core.RenderState.make(
  228. core.ColorAttrib.make_off(),
  229. core.ColorScaleAttrib.make(TEST_COLOR_SCALE),
  230. shader_attrib,
  231. material_attrib,
  232. )
  233. result = render_color_pixel(color_region, state, vertex_color=TEST_COLOR)
  234. assert result.almost_equal(TEST_COLOR_SCALE, FUZZ)