Using SSBO for light data

Ah…ok I thought it was…but that is the explanation: thanks.

Issue shown here:

  1. With 1.10.15, textures are displayed:
  2. With 1.11, nothing is displayed on the card:

Here is a much lighter code (should run faster). I would rather use the dr.set_target_tex_page() but I can’t manage to make it work with the shadow buffers generated by P3D.

MinimalTC.zip (4.7 KB)

Again, thanks so much!

I was stumped for a while why your code would be creating a shadow buffer at all, since nothing is using the shadows, but then I realised that set_shader_auto(False) is enabling the shader generator, and that False just gets converted to a priority value of 0. This isn’t the right way to disable it, but if you did disable it, the shadow buffers would not get created.

Your Config.prc variables are not taking effect because by escaping the newline, all those lines end up on the same line. You should use a multi-line string literal with """.

However, the actual problem seems to be related to the new light culling system, which is behaving correctly, but it’s more sensitive to sorting issues. You currently have all your lights set to render their shadows after the main window, which causes Panda to think that there is actually no scene pass depending on the result of rendering the shadow maps, and therefore it can cull the lights.

If you set the sort values of the lights to negative values, then it works. That incidentally solves the problem of the shadows being delayed by 1 frame compared to the rest of the scene.

1 Like

Thanks so much, indeed it works!

Yes, I also did notice that (that is why I used it in the sample code although not intuitive…) but I agree this is a bit hacky. My goal was to generate shadows from P3D without using the shader generator. I know it works without it, provided you do not render the shadow buffer generated by P3D into a texture and use the GLSL p3d_LightSourceParameters struct…I don’t see how to work around that.

For my complete understanding:

  1. Do you see how using the dr.set_target_tex_page(n) with the shadowbuffers generated by P3D (I can’t manage to use it with these buffers…)?

  2. I did some performance tests and compare this sliced-texture version with the 2D/3D normal textures equivalent…and I saw a very small difference (1.11 sliced version seems to perform a little bit better than 1.11 with normal textures - but maybe less than 1ms): would the difference be stronger with more lights and textures?

  3. Is there a simple way to retrieve from python the shadowViewMatrix for each light?

And thanks again for your patience and time!

To get better performance (though whether it’s actually measurably faster will depend on whether this is at all a bottleneck), I think the trick is to use a single buffer, not two.

For layered rendering that requires something like a single pass to render all 6 faces of a cube map, where the geometry shader decides which layer the triangle should go to (possibly several layers at a time for ones that are in a corner). Geometry shaders have their own caveats, though, so ultimately I don’t know how much this will help.

With the set_target_tex_page approach the most efficient way is to use a single buffer as well with different display regions rendering into different pages. Then you can have the buffer clear all pages in one go without a problem too.

The reason set_target_tex_page isn’t working is because Panda notices there’s already a texture bound with RTM_bind_or_copy to the RTP_depth slot; the default shadow map that Panda creates. That causes Panda to switch that attachment to copy-texture mode rather than bind mode, which apparently doesn’t really handle target-tex-page setting well. I need to think about how we should handle this. In the meantime, binding is more efficient anyway, so call sBuffer.clear_render_textures() to clear that first.

So, something like:

            sBuffer.clear_render_textures()
            sBuffer.add_render_texture(self._tex_array, GraphicsOutput.RTM_bind_or_copy, GraphicsOutput.RTP_depth)
            for dr in sBuffer.get_display_regions():
                dr.set_target_tex_page(0)

            dr = sBuffer.make_mono_display_region((0, 1, 0, 1))
            dr.set_target_tex_page(1)
            dr.set_camera(self._light2_np)
            dr.set_clear_depth_active(True)
            dr.set_active(True)

            sBuffer2.set_active(False)

At this point, not a whole lot of Panda’s own shadow system is left. :stuck_out_tongue:

I still intend to add support for atlases and arrays to Panda’s shadow system properly.

As for retrieving the shadowViewMatrix, I loosely converted the code in GraphicsStateGuardian to Python, but I’m not 100% sure I made no mistakes:

shadow_bias_mat = Mat4(0.5, 0.0, 0.0, 0.0,
                       0.0, 0.5, 0.0, 0.0,
                       0.0, 0.0, 0.5, 0.0,
                       0.5, 0.5, 0.5, 1.0)

inv_cs_transform = Mat4.convert_mat(gsg.get_internal_coordinate_system(), gsg.coordinate_system)

t = inv_cs_transform *
    base.cam.get_transform(render).get_mat() *
    light.get_net_transform().get_inverse().get_mat() *
    Mat4.convert_mat(coordinate_system, lens.get_coordinate_system())

if not isinstance(light.node(), PointLight):
    t *= light.node().get_lens().get_projection_mat() * shadow_bias_mat

Well, this is why rdb’s superpowers are unreachable :slight_smile: Thanks so much for your insightful explanations!

You are right, sliced textures must be used that way and hence I see performance differences (30% with the same code with regular textures).

And if you combine it with the new 1.11 InstancedNode feature (thanks for that btw!), performance gain is much higher (time is divided by 3)

Yes, indeed. It works perfectly for all kind of lights (DL/SL/PL).

Thanks! I’ll test it. Could probably be interesting to make it accessible from python through a PROPERTY.

And for those who might be interested by this P3D functionality, here is a complete working example: I hope this might be useful for someone.It will require 1.11 to make it work correctly - at least for the PLs

SlicedTexturesWorkingExample.zip (9.1 KB)

1 Like