Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to control the number of mipmap levels generated for depth textures attached to the camera. #1306

Open
Hypo-Li opened this issue Feb 23, 2024 · 0 comments

Comments

@Hypo-Li
Copy link

Hypo-Li commented Feb 23, 2024

I created a texture in depth format and attached it to the camera. As I wanted to create an HIZ (Hierarchical Depth), I set the texture's MIN_FILTER to LINEAR_MIPMAP_LINEAR and used the setNumMipmapLevels() function to set the desired number of mipmaps. However, when I used NSight to view the results, I found that this texture generated all the mipmap levels it could generate, But when I created another color texture with the same settings, I found that it could generate the correct number of mipmap levels

This confuses me. I followed the code of OSG and found that the problem may be in the selectSizedInternalFormat function of Texture.cpp, which seems to check the InternalFormat of the texture through an array called sizedInternalFormats, but this array only contains color format. The depth and template format are in another array called sizedDepthAndStencilInternalFormats, In fact, there is also an array called compressedInternalFormats, and selectSizedInternalFormat ignores the other two arrays, resulting in the function being unable to return the correct value for depth formats;

Taking Texture2D.cpp as an example:

// Texture2D::apply
// no image present, but dimensions at set so lets create the texture
 GLExtensions * extensions = state.get<GLExtensions>();
 GLenum texStorageSizedInternalFormat = extensions->isTextureStorageEnabled && (_borderWidth==0) ? selectSizedInternalFormat() : 0;
 if (texStorageSizedInternalFormat!=0)
 {
     textureObject = generateAndAssignTextureObject(contextID, GL_TEXTURE_2D, _numMipmapLevels, texStorageSizedInternalFormat, _textureWidth, _textureHeight, 1, _borderWidth);
     textureObject->bind(state);
     applyTexParameters(GL_TEXTURE_2D, state);
     if(!textureObject->_allocated)
     {
         extensions->glTexStorage2D( GL_TEXTURE_2D, osg::maximum(_numMipmapLevels,1), texStorageSizedInternalFormat,
                  _textureWidth, _textureHeight);
     }
 }
 else
 {
     GLenum internalFormat = _sourceFormat ? _sourceFormat : _internalFormat;
     textureObject = generateAndAssignTextureObject(contextID, GL_TEXTURE_2D, _numMipmapLevels, internalFormat, _textureWidth, _textureHeight, 1, _borderWidth);
     textureObject->bind(state);
     applyTexParameters(GL_TEXTURE_2D, state);
     glTexImage2D( GL_TEXTURE_2D, 0, _internalFormat,
              _textureWidth, _textureHeight, _borderWidth,
              internalFormat,
              _sourceType ? _sourceType : GL_UNSIGNED_BYTE,
              0);
}

This resulted in the value of texStorageSizedInternalFormat being 0, making it impossible to use glTexStorage2D to create textures, instead using glTexImage2D. In this case, to control the mipmap level, I can only use glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, level). However, after browsing the code, I found that only one place called this function and it requires the texture to have Image, so my depth texture cannot set the mipmap level, When it generates mipmap, it will generate all mipmap levels

The above are all my conjectures;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant