test_depth_buffer.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. # Vulkan needs no host window.
  9. if graphics_pipe.get_interface_name() != "Vulkan":
  10. host_fbprops = core.FrameBufferProperties()
  11. host_fbprops.force_hardware = True
  12. host = engine.make_output(
  13. graphics_pipe,
  14. 'host',
  15. 0,
  16. host_fbprops,
  17. core.WindowProperties.size(32, 32),
  18. core.GraphicsPipe.BF_refuse_window,
  19. )
  20. engine.open_windows()
  21. if host is None:
  22. pytest.skip("GraphicsPipe cannot make offscreen buffers")
  23. host_gsg = host.gsg
  24. else:
  25. host = None
  26. host_gsg = None
  27. fbprops = core.FrameBufferProperties()
  28. fbprops.force_hardware = True
  29. fbprops.depth_bits = request.param
  30. if fbprops.depth_bits >= 32:
  31. fbprops.float_depth = True
  32. buffer = engine.make_output(
  33. graphics_pipe,
  34. 'buffer',
  35. 0,
  36. fbprops,
  37. core.WindowProperties.size(32, 32),
  38. core.GraphicsPipe.BF_refuse_window,
  39. host_gsg,
  40. host
  41. )
  42. engine.open_windows()
  43. if buffer is None:
  44. pytest.skip("Cannot make depth buffer")
  45. if buffer.get_fb_properties().depth_bits != request.param:
  46. pytest.skip("Could not make buffer with desired bit count")
  47. yield buffer.make_display_region()
  48. if buffer is not None:
  49. engine.remove_window(buffer)
  50. def render_depth_pixel(region, distance, near, far, clear=None, write=True):
  51. """Renders a fragment at the specified distance using the specified render
  52. settings, and returns the resulting depth value."""
  53. # Set up the scene with a blank card rendering at specified distance.
  54. scene = core.NodePath("root")
  55. scene.set_attrib(core.DepthTestAttrib.make(core.RenderAttrib.M_always))
  56. scene.set_depth_write(write)
  57. camera = scene.attach_new_node(core.Camera("camera"))
  58. camera.node().get_lens(0).set_near_far(near, far)
  59. camera.node().set_cull_bounds(core.OmniBoundingVolume())
  60. if distance is not None:
  61. cm = core.CardMaker("card")
  62. cm.set_frame(-1, 1, -1, 1)
  63. card = scene.attach_new_node(cm.generate())
  64. card.set_pos(0, distance, 0)
  65. card.set_scale(60)
  66. region.active = True
  67. region.camera = camera
  68. if clear is not None:
  69. region.set_clear_depth_active(True)
  70. region.set_clear_depth(clear)
  71. depth_texture = core.Texture("depth")
  72. region.window.add_render_texture(depth_texture,
  73. core.GraphicsOutput.RTM_copy_ram,
  74. core.GraphicsOutput.RTP_depth)
  75. region.window.engine.render_frame()
  76. region.window.clear_render_textures()
  77. col = core.LColor()
  78. depth_texture.peek().lookup(col, 0.5, 0.5)
  79. return col[0]
  80. def test_depth_clear(depth_region):
  81. assert 1.0 == render_depth_pixel(depth_region, None, near=1, far=10, clear=1.0)
  82. assert 0.0 == render_depth_pixel(depth_region, None, near=1, far=10, clear=0.0)
  83. def test_depth_write(depth_region):
  84. assert 1.0 == render_depth_pixel(depth_region, 5.0, near=1, far=10, clear=1.0, write=False)
  85. assert 0.99 > render_depth_pixel(depth_region, 5.0, near=1, far=10, clear=1.0, write=True)
  86. def test_depth_far_inf(depth_region):
  87. inf = float("inf")
  88. assert 0.99 > render_depth_pixel(depth_region, 10.0, near=1, far=inf, clear=1.0)
  89. def test_depth_near_inf(depth_region):
  90. inf = float("inf")
  91. assert 0.01 < render_depth_pixel(depth_region, 10.0, near=inf, far=1, clear=0.0)
  92. def test_depth_clipping(depth_region):
  93. # Get the actual depth resulting from the clear value.
  94. clr = render_depth_pixel(depth_region, None, near=1, far=10, clear=0.5)
  95. # We try rendering something at various distances to make sure that the
  96. # resulting depth value matches our expectations.
  97. # Too close; read clear value.
  98. assert clr == render_depth_pixel(depth_region, 0.999, near=1, far=10, clear=0.5)
  99. # Too far; read clear value.
  100. assert clr == render_depth_pixel(depth_region, 10.01, near=1, far=10, clear=0.5)
  101. # Just close enough; read a value close to 0.0.
  102. assert 0.01 > render_depth_pixel(depth_region, 1.001, near=1, far=10, clear=0.5)
  103. # Just far enough; read 1.0.
  104. assert 0.99 < render_depth_pixel(depth_region, 9.999, near=1, far=10, clear=0.5)
  105. def test_inverted_depth_clipping(depth_region):
  106. # Get the actual depth resulting from the clear value.
  107. clr = render_depth_pixel(depth_region, None, near=1, far=10, clear=0.5)
  108. # Too close; read clear value.
  109. assert clr == render_depth_pixel(depth_region, 0.999, near=10, far=1, clear=0.5)
  110. # Too far; read clear value.
  111. assert clr == render_depth_pixel(depth_region, 10.01, near=10, far=1, clear=0.5)
  112. # Just close enough; read a value close to 1.0.
  113. assert 0.99 < render_depth_pixel(depth_region, 1.001, near=10, far=1, clear=0.5)
  114. # Just far enough; read a value close to 0.0.
  115. assert 0.01 > render_depth_pixel(depth_region, 9.999, near=10, far=1, clear=0.5)