Write to mipmap levels (subimages) in a texture with a shader

Since OpenGL 3.0 you’ve been able to choose a specific mipmap level the target for a write to buffer operation, e.g.
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glFramebufferTexture.xhtml
glFramebufferTexture — attach a level of a texture object as a logical buffer of a framebuffer object

void glFramebufferTexture(GLenum target, GLenum attachment, GLuint texture, GLint level);

level here is the mipmap level. If understand correctly you can just treat it as a regular buffer with the dimensions reduced to whatever is appropriate for that mipmap level. Then you could have a fragment shader just write straight to the subimage.

I couldn’t find anything specific in the Panda3d documentation that seemed to allow anything like this. Any help would be appreciated.

Another approach would be using imageStore but that doesn’t seem to be able to access mipmap levels directly unless I am missing something. I think there a way to make a subimage the target for a uniform sampler2D in OpenGL generally (to do a specific level one at a time) but I don’t know how to do that in Panda either, e.g.

glBindImageTexture( 0, image.id_GL(),  level, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F);

Some way of writing to subimages (mipmap levels) in shaders would be really useful for generating mipmaps yourself or to modify those sub-images for another purpose. My specific use case was wanting to use a compute shader to generate mipmap levels very quickly on a per frame basis. I need to use a minimum rather than an average to create the texels on the subimages.

As a more general question, I was also wondering when Panda would generate mipmaps automatically and how much control you have over when it is done. I don’t mean when reading in image files, I am more interested in generating a texture level 0 by whatever means and then getting mipmaps for it at a specific time. Thanks.

You can bind a specific mipmap level for imageStore access with setShaderInput:

nodePath.setShaderInput("name", texture, read=False, write=True, z=-1, n=0)

where n is the mipmap level index and z is the layer/page index (eg. of cube map), where -1 means to bind all layers.

Panda will generate mipmaps automatically when either (1) uploading new texture data and some mipmap levels are missing, or (2) you are rendering a texture with mipmapping enabled. It will not automatically do so if you write to it using imageStore.

This is something like your request.

rdb: Thanks - I’ll try that. I made a mimap by hand out by partitioning a really big texture but this should be smarter and faster I hope.

serega-kkz: Thanks for the suggestion. I have used ram images before but moving the data to and from the cpu is very expensive to do every frame.