test_depth_buffer.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. from panda3d import core
  2. import pytest
  3. @pytest.fixture(scope='module', params=[32, 24, 16])
  4. def depth_region(request, graphics_pipe):
  5. """Creates and returns a DisplayRegion with a depth buffer."""
  6. engine = core.GraphicsEngine()
  7. engine.set_threading_model("")
  8. host_fbprops = core.FrameBufferProperties()
  9. host_fbprops.force_hardware = True
  10. host = engine.make_output(
  11. graphics_pipe,
  12. 'host',
  13. 0,
  14. host_fbprops,
  15. core.WindowProperties.size(32, 32),
  16. core.GraphicsPipe.BF_refuse_window,
  17. )
  18. engine.open_windows()
  19. if host is None:
  20. pytest.skip("GraphicsPipe cannot make offscreen buffers")
  21. fbprops = core.FrameBufferProperties()
  22. fbprops.force_hardware = True
  23. fbprops.depth_bits = request.param
  24. if fbprops.depth_bits >= 32:
  25. fbprops.float_depth = True
  26. buffer = engine.make_output(
  27. graphics_pipe,
  28. 'buffer',
  29. 0,
  30. fbprops,
  31. core.WindowProperties.size(32, 32),
  32. core.GraphicsPipe.BF_refuse_window,
  33. host.gsg,
  34. host
  35. )
  36. engine.open_windows()
  37. if buffer is None:
  38. pytest.skip("Cannot make depth buffer")
  39. if buffer.get_fb_properties().depth_bits != request.param:
  40. pytest.skip("Could not make buffer with desired bit count")
  41. yield buffer.make_display_region()
  42. if buffer is not None:
  43. engine.remove_window(buffer)
  44. def render_depth_pixel(region, distance, near, far, clear=None, write=True, state=None):
  45. """Renders a fragment at the specified distance using the specified render
  46. settings, and returns the resulting depth value."""
  47. # Set up the scene with a blank card rendering at specified distance.
  48. scene = core.NodePath("root")
  49. scene.set_attrib(core.DepthTestAttrib.make(core.RenderAttrib.M_always))
  50. scene.set_depth_write(write)
  51. if state:
  52. scene.set_state(scene.get_state().compose(state))
  53. camera = scene.attach_new_node(core.Camera("camera"))
  54. camera.node().get_lens(0).set_near_far(near, far)
  55. camera.node().set_cull_bounds(core.OmniBoundingVolume())
  56. if distance is not None:
  57. cm = core.CardMaker("card")
  58. cm.set_frame(-1, 1, -1, 1)
  59. card = scene.attach_new_node(cm.generate())
  60. card.set_pos(0, distance, 0)
  61. card.set_scale(60)
  62. region.active = True
  63. region.camera = camera
  64. if clear is not None:
  65. region.set_clear_depth_active(True)
  66. region.set_clear_depth(clear)
  67. depth_texture = core.Texture("depth")
  68. region.window.add_render_texture(depth_texture,
  69. core.GraphicsOutput.RTM_copy_ram,
  70. core.GraphicsOutput.RTP_depth)
  71. region.window.engine.render_frame()
  72. region.window.clear_render_textures()
  73. col = core.LColor()
  74. depth_texture.peek().lookup(col, 0.5, 0.5)
  75. return col[0]
  76. def test_depth_clear(depth_region):
  77. assert 1.0 == render_depth_pixel(depth_region, None, near=1, far=10, clear=1.0)
  78. assert 0.0 == render_depth_pixel(depth_region, None, near=1, far=10, clear=0.0)
  79. def test_depth_write(depth_region):
  80. assert 1.0 == render_depth_pixel(depth_region, 5.0, near=1, far=10, clear=1.0, write=False)
  81. assert 0.99 > render_depth_pixel(depth_region, 5.0, near=1, far=10, clear=1.0, write=True)
  82. def test_depth_far_inf(depth_region):
  83. inf = float("inf")
  84. assert 0.99 > render_depth_pixel(depth_region, 10.0, near=1, far=inf, clear=1.0)
  85. def test_depth_near_inf(depth_region):
  86. inf = float("inf")
  87. assert 0.01 < render_depth_pixel(depth_region, 10.0, near=inf, far=1, clear=0.0)
  88. def test_depth_clipping(depth_region):
  89. # Get the actual depth resulting from the clear value.
  90. clr = render_depth_pixel(depth_region, None, near=1, far=10, clear=0.5)
  91. # We try rendering something at various distances to make sure that the
  92. # resulting depth value matches our expectations.
  93. # Too close; read clear value.
  94. assert clr == render_depth_pixel(depth_region, 0.999, near=1, far=10, clear=0.5)
  95. # Too far; read clear value.
  96. assert clr == render_depth_pixel(depth_region, 10.01, near=1, far=10, clear=0.5)
  97. # Just close enough; read a value close to 0.0.
  98. assert 0.01 > render_depth_pixel(depth_region, 1.001, near=1, far=10, clear=0.5)
  99. # Just far enough; read 1.0.
  100. assert 0.99 < render_depth_pixel(depth_region, 9.999, near=1, far=10, clear=0.5)
  101. def test_inverted_depth_clipping(depth_region):
  102. # Get the actual depth resulting from the clear value.
  103. clr = render_depth_pixel(depth_region, None, near=1, far=10, clear=0.5)
  104. # Too close; read clear value.
  105. assert clr == render_depth_pixel(depth_region, 0.999, near=10, far=1, clear=0.5)
  106. # Too far; read clear value.
  107. assert clr == render_depth_pixel(depth_region, 10.01, near=10, far=1, clear=0.5)
  108. # Just close enough; read a value close to 1.0.
  109. assert 0.99 < render_depth_pixel(depth_region, 1.001, near=10, far=1, clear=0.5)
  110. # Just far enough; read a value close to 0.0.
  111. assert 0.01 > render_depth_pixel(depth_region, 9.999, near=10, far=1, clear=0.5)
  112. def test_depth_range(depth_region):
  113. try:
  114. depth_region.set_depth_range(0.25, 0.75)
  115. z = render_depth_pixel(depth_region, 1.00001, near=1, far=10, clear=0.0)
  116. assert z == pytest.approx(0.25, rel=0.01)
  117. z = render_depth_pixel(depth_region, 9.99999, near=1, far=10, clear=0.0)
  118. assert z == pytest.approx(0.75, rel=0.01)
  119. # Combines with DepthOffsetAttrib range.
  120. state = core.RenderState.make(core.DepthOffsetAttrib.make(0, 0.25, 0.75))
  121. z = render_depth_pixel(depth_region, 1.00001, near=1, far=10, clear=0.0, state=state)
  122. assert z == pytest.approx(0.375, rel=0.01)
  123. # Reverse the depth range.
  124. depth_region.set_depth_range(0.75, 0.25)
  125. z = render_depth_pixel(depth_region, 1.00001, near=1, far=10, clear=0.0)
  126. assert z == pytest.approx(0.75, rel=0.01)
  127. z = render_depth_pixel(depth_region, 9.99999, near=1, far=10, clear=0.0)
  128. assert z == pytest.approx(0.25, rel=0.01)
  129. finally:
  130. depth_region.set_depth_range(0, 1)
  131. def test_depth_bias(depth_region):
  132. # Without depth bias
  133. z_ref = render_depth_pixel(depth_region, 5, near=1, far=10)
  134. # With constant positive depth bias
  135. state = core.RenderState.make(core.DepthBiasAttrib.make(0, 1))
  136. z = render_depth_pixel(depth_region, 5, near=1, far=10, state=state)
  137. assert z > z_ref
  138. # With constant negative depth bias
  139. state = core.RenderState.make(core.DepthBiasAttrib.make(0, -1))
  140. z = render_depth_pixel(depth_region, 5, near=1, far=10, state=state)
  141. assert z < z_ref
  142. # With slope-scaled depth bias (our quad has no slope)
  143. state = core.RenderState.make(core.DepthBiasAttrib.make(10, 0))
  144. z = render_depth_pixel(depth_region, 5, near=1, far=10, state=state)
  145. assert z == pytest.approx(z_ref)
  146. # Same, but negative
  147. state = core.RenderState.make(core.DepthBiasAttrib.make(-10, 0))
  148. z = render_depth_pixel(depth_region, 5, near=1, far=10, state=state)
  149. assert z == pytest.approx(z_ref)
  150. def test_depth_offset(depth_region):
  151. # Without depth offset
  152. z_ref = render_depth_pixel(depth_region, 5, near=1, far=10)
  153. # With constant positive depth offset
  154. state = core.RenderState.make(core.DepthOffsetAttrib.make(1))
  155. z = render_depth_pixel(depth_region, 5, near=1, far=10, state=state)
  156. assert z < z_ref
  157. # With constant negative depth offset
  158. state = core.RenderState.make(core.DepthOffsetAttrib.make(-1))
  159. z = render_depth_pixel(depth_region, 5, near=1, far=10, state=state)
  160. assert z > z_ref