test_color_buffer.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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='session')
  38. def light_attrib():
  39. light = core.AmbientLight('amb')
  40. light.color = (1, 1, 1, 1)
  41. light_attrib = core.LightAttrib.make()
  42. light_attrib = light_attrib.add_on_light(core.NodePath(light))
  43. return light_attrib
  44. @pytest.fixture(scope='module', params=[False, True], ids=["srgb:off", "srgb:on"])
  45. def color_region(request, graphics_pipe):
  46. """Creates and returns a DisplayRegion with a depth buffer."""
  47. engine = core.GraphicsEngine()
  48. engine.set_threading_model("")
  49. host_fbprops = core.FrameBufferProperties()
  50. host_fbprops.force_hardware = True
  51. host = engine.make_output(
  52. graphics_pipe,
  53. 'host',
  54. 0,
  55. host_fbprops,
  56. core.WindowProperties.size(32, 32),
  57. core.GraphicsPipe.BF_refuse_window,
  58. )
  59. engine.open_windows()
  60. if host is None:
  61. pytest.skip("GraphicsPipe cannot make offscreen buffers")
  62. fbprops = core.FrameBufferProperties()
  63. fbprops.force_hardware = True
  64. fbprops.set_rgba_bits(8, 8, 8, 8)
  65. fbprops.srgb_color = request.param
  66. buffer = engine.make_output(
  67. graphics_pipe,
  68. 'buffer',
  69. 0,
  70. fbprops,
  71. core.WindowProperties.size(32, 32),
  72. core.GraphicsPipe.BF_refuse_window,
  73. host.gsg,
  74. host
  75. )
  76. engine.open_windows()
  77. if buffer is None:
  78. pytest.skip("Cannot make color buffer")
  79. if fbprops.srgb_color != buffer.get_fb_properties().srgb_color:
  80. pytest.skip("Cannot make buffer with required srgb_color setting")
  81. buffer.set_clear_color_active(True)
  82. buffer.set_clear_color((0, 0, 0, 1))
  83. yield buffer.make_display_region()
  84. if buffer is not None:
  85. engine.remove_window(buffer)
  86. def render_color_pixel(region, state, vertex_color=None):
  87. """Renders a fragment using the specified render settings, and returns the
  88. resulting color value."""
  89. # Skip auto-shader tests if we don't support Cg shaders.
  90. if not region.window.gsg.supports_basic_shaders:
  91. sattr = state.get_attrib(core.ShaderAttrib)
  92. if sattr and sattr.auto_shader():
  93. pytest.skip("Cannot test auto-shader without Cg shader support")
  94. # Set up the scene with a blank card rendering at specified distance.
  95. scene = core.NodePath("root")
  96. scene.set_attrib(core.DepthTestAttrib.make(core.RenderAttrib.M_always))
  97. camera = scene.attach_new_node(core.Camera("camera"))
  98. camera.node().get_lens(0).set_near_far(1, 3)
  99. camera.node().set_cull_bounds(core.OmniBoundingVolume())
  100. if vertex_color is not None:
  101. format = core.GeomVertexFormat.get_v3cp()
  102. else:
  103. format = core.GeomVertexFormat.get_v3()
  104. vdata = core.GeomVertexData("card", format, core.Geom.UH_static)
  105. vdata.unclean_set_num_rows(4)
  106. vertex = core.GeomVertexWriter(vdata, "vertex")
  107. vertex.set_data3(core.Vec3.rfu(-1, 0, 1))
  108. vertex.set_data3(core.Vec3.rfu(-1, 0, -1))
  109. vertex.set_data3(core.Vec3.rfu(1, 0, 1))
  110. vertex.set_data3(core.Vec3.rfu(1, 0, -1))
  111. if vertex_color is not None:
  112. color = core.GeomVertexWriter(vdata, "color")
  113. color.set_data4(vertex_color)
  114. color.set_data4(vertex_color)
  115. color.set_data4(vertex_color)
  116. color.set_data4(vertex_color)
  117. strip = core.GeomTristrips(core.Geom.UH_static)
  118. strip.set_shade_model(core.Geom.SM_uniform)
  119. strip.add_next_vertices(4)
  120. strip.close_primitive()
  121. geom = core.Geom(vdata)
  122. geom.add_primitive(strip)
  123. gnode = core.GeomNode("card")
  124. gnode.add_geom(geom, state)
  125. card = scene.attach_new_node(gnode)
  126. card.set_pos(0, 2, 0)
  127. card.set_scale(60)
  128. region.active = True
  129. region.camera = camera
  130. color_texture = core.Texture("color")
  131. region.window.add_render_texture(color_texture,
  132. core.GraphicsOutput.RTM_copy_ram,
  133. core.GraphicsOutput.RTP_color)
  134. region.window.engine.render_frame()
  135. region.window.clear_render_textures()
  136. col = core.LColor()
  137. color_texture.peek().lookup(col, 0.5, 0.5)
  138. return col
  139. def test_color_write_mask(color_region):
  140. state = core.RenderState.make(
  141. core.ColorWriteAttrib.make(core.ColorWriteAttrib.C_green),
  142. )
  143. result = render_color_pixel(color_region, state)
  144. assert result == (0, 1, 0, 1)
  145. def test_color_empty(color_region, shader_attrib, material_attrib):
  146. state = core.RenderState.make(
  147. shader_attrib,
  148. material_attrib,
  149. )
  150. result = render_color_pixel(color_region, state)
  151. assert result == (1, 1, 1, 1)
  152. def test_color_off(color_region, shader_attrib, material_attrib):
  153. state = core.RenderState.make(
  154. core.ColorAttrib.make_off(),
  155. shader_attrib,
  156. material_attrib,
  157. )
  158. result = render_color_pixel(color_region, state)
  159. assert result == (1, 1, 1, 1)
  160. def test_color_flat(color_region, shader_attrib, material_attrib):
  161. state = core.RenderState.make(
  162. core.ColorAttrib.make_flat(TEST_COLOR),
  163. shader_attrib,
  164. material_attrib,
  165. )
  166. result = render_color_pixel(color_region, state)
  167. assert result.almost_equal(TEST_COLOR, FUZZ)
  168. def test_color_vertex(color_region, shader_attrib, material_attrib):
  169. state = core.RenderState.make(
  170. core.ColorAttrib.make_vertex(),
  171. shader_attrib,
  172. material_attrib,
  173. )
  174. result = render_color_pixel(color_region, state, vertex_color=TEST_COLOR)
  175. assert result.almost_equal(TEST_COLOR, FUZZ)
  176. def test_color_empty_vertex(color_region, shader_attrib, material_attrib):
  177. state = core.RenderState.make(
  178. shader_attrib,
  179. material_attrib,
  180. )
  181. result = render_color_pixel(color_region, state, vertex_color=TEST_COLOR)
  182. assert result.almost_equal(TEST_COLOR, FUZZ)
  183. def test_color_off_vertex(color_region, shader_attrib, material_attrib):
  184. state = core.RenderState.make(
  185. core.ColorAttrib.make_off(),
  186. shader_attrib,
  187. material_attrib,
  188. )
  189. result = render_color_pixel(color_region, state, vertex_color=TEST_COLOR)
  190. assert result == (1, 1, 1, 1)
  191. def test_scaled_color_empty(color_region, shader_attrib, material_attrib):
  192. state = core.RenderState.make(
  193. shader_attrib,
  194. material_attrib,
  195. )
  196. result = render_color_pixel(color_region, state)
  197. assert result == (1, 1, 1, 1)
  198. def test_scaled_color_off(color_region, shader_attrib, material_attrib):
  199. state = core.RenderState.make(
  200. core.ColorAttrib.make_off(),
  201. shader_attrib,
  202. material_attrib,
  203. )
  204. result = render_color_pixel(color_region, state)
  205. assert result == (1, 1, 1, 1)
  206. def test_scaled_color_flat(color_region, shader_attrib, material_attrib):
  207. state = core.RenderState.make(
  208. core.ColorAttrib.make_flat(TEST_COLOR),
  209. core.ColorScaleAttrib.make(TEST_COLOR_SCALE),
  210. shader_attrib,
  211. material_attrib,
  212. )
  213. result = render_color_pixel(color_region, state)
  214. assert result.almost_equal(TEST_SCALED_COLOR, FUZZ)
  215. def test_scaled_color_vertex(color_region, shader_attrib, material_attrib):
  216. state = core.RenderState.make(
  217. core.ColorAttrib.make_vertex(),
  218. core.ColorScaleAttrib.make(TEST_COLOR_SCALE),
  219. shader_attrib,
  220. material_attrib,
  221. )
  222. result = render_color_pixel(color_region, state, vertex_color=TEST_COLOR)
  223. assert result.almost_equal(TEST_SCALED_COLOR, FUZZ)
  224. def test_scaled_color_empty_vertex(color_region, shader_attrib, material_attrib):
  225. state = core.RenderState.make(
  226. core.ColorScaleAttrib.make(TEST_COLOR_SCALE),
  227. shader_attrib,
  228. material_attrib,
  229. )
  230. result = render_color_pixel(color_region, state, vertex_color=TEST_COLOR)
  231. assert result.almost_equal(TEST_SCALED_COLOR, FUZZ)
  232. def test_scaled_color_off_vertex(color_region, shader_attrib, material_attrib):
  233. state = core.RenderState.make(
  234. core.ColorAttrib.make_off(),
  235. core.ColorScaleAttrib.make(TEST_COLOR_SCALE),
  236. shader_attrib,
  237. material_attrib,
  238. )
  239. result = render_color_pixel(color_region, state, vertex_color=TEST_COLOR)
  240. assert result.almost_equal(TEST_COLOR_SCALE, FUZZ)
  241. def test_color_transparency(color_region, shader_attrib, light_attrib):
  242. mat = core.Material()
  243. mat.diffuse = (1, 1, 1, 0.75)
  244. material_attrib = core.MaterialAttrib.make(mat)
  245. state = core.RenderState.make(
  246. core.TransparencyAttrib.make(core.TransparencyAttrib.M_alpha),
  247. light_attrib,
  248. shader_attrib,
  249. material_attrib,
  250. )
  251. result = render_color_pixel(color_region, state)
  252. assert result.x == pytest.approx(0.75, 0.1)
  253. def test_color_transparency_flat(color_region, shader_attrib, light_attrib):
  254. mat = core.Material()
  255. mat.diffuse = (1, 1, 1, 0.75)
  256. material_attrib = core.MaterialAttrib.make(mat)
  257. state = core.RenderState.make(
  258. core.TransparencyAttrib.make(core.TransparencyAttrib.M_alpha),
  259. core.ColorAttrib.make_flat(TEST_COLOR),
  260. light_attrib,
  261. shader_attrib,
  262. material_attrib,
  263. )
  264. result = render_color_pixel(color_region, state)
  265. assert result.x == pytest.approx(0.75, 0.1)
  266. def test_color_transparency_vertex(color_region, shader_attrib, light_attrib):
  267. mat = core.Material()
  268. mat.diffuse = (1, 1, 1, 0.75)
  269. material_attrib = core.MaterialAttrib.make(mat)
  270. state = core.RenderState.make(
  271. core.TransparencyAttrib.make(core.TransparencyAttrib.M_alpha),
  272. core.ColorAttrib.make_vertex(),
  273. light_attrib,
  274. shader_attrib,
  275. material_attrib,
  276. )
  277. result = render_color_pixel(color_region, state, vertex_color=(1, 1, 1, 0.5))
  278. assert result.x == pytest.approx(0.75, 0.1)
  279. def test_color_transparency_no_light(color_region, shader_attrib):
  280. mat = core.Material()
  281. mat.diffuse = (1, 1, 1, 0.75)
  282. material_attrib = core.MaterialAttrib.make(mat)
  283. state = core.RenderState.make(
  284. core.TransparencyAttrib.make(core.TransparencyAttrib.M_alpha),
  285. shader_attrib,
  286. material_attrib,
  287. )
  288. result = render_color_pixel(color_region, state)
  289. assert result.x == pytest.approx(1.0, 0.1)