Creating mipmaps down to 1x1 / visibility check

I’d like to reduce a render-to-texture result to a 1x1 texture, and pass that to a shader. I read around that this can be efficiently done by instructing the graphics driver to create mipmap levels all the way down to 1x1. Is this possible with Panda?

Roughly, if I have a code like:

tbuffer = window.getEngine().makeOutput(...)
main_tex = Texture()
tbuffer.addRenderTexture(main_tex, ...)
aux0_tex = Texture()
tbuffer.addRenderTexture(aux0_tex, ..., GraphicsOutput.RTPAuxRgba0)
screen_quad = NodePath(cm.generate())
screen_quad.setTexture(TextureStage("main"), main_tex)
screen_quad.setTexture(TextureStage("aux0"), aux0_tex) # this line
screen_quad.setShader(...)
screen_quad.reparentTo(render)

in “this line” above I’d like to pass the 1x1 reduction of aux0_tex instead of the aux0_tex itself.

The background is that I want to use this auxiliary buffer for a high-fidelity visibility check. The thing that is checked is to render all white, and everything else all black. Then if the single pixel in 1x1 reduction is not full black, the thing is visible. If there is a better way to do a visibility check, please suggest.

That’s a pretty cool idea, taking advantage of mipmapping to achieve this. This should be possible - I think Panda should be able to generate mipmaps for a texture, just enable it for the texture that you’re rendering into.

Panda generates mipmaps itself for some textures by default, so you may have to look through the list of config options to find the one that tells it to ask the driver to generate the mipmaps.

Making mipmaps for render-to-texture works using the default GL renderer, but I’m pretty sure that it does not work with the DirectX renderer. Just something to keep in mind of you want to offer DirectX as an option to your users.

By the way, in order to access a specific mipmap level, you have to use something like tex2Dlod or textureLod instead of the regular tex2D/texture2D in your shader:
http.developer.nvidia.com/Cg/tex2Dlod.html
opengl.org/sdk/docs/manglsl/ … ureLod.xml

This tex2Dlod looks exactly like what is needed, but unfortunately it won’t work on AMD cards. There Cg falls back to arbfp1 profile, in which tex2Dlod is not available. I would have to rewrite shaders to GLSL then. (This is probably a good idea anyway, only the last time I tried, in 1.7.x releases, I couldn’t make them work properly.)

DirectX renderer is not important for me, so no problem if mipmaping of render-to-texture doesn’t work with it.

Or you can use Cg with the “glslv/glslf” profile, which basically translates your shader to GLSL. However, I’ve heard that it’s not all that reliable, so it may be better to use GLSL instead. The GLSL support in 1.8.1 is better than in previous versions.