Skip to content

GLES3: Change default texture repeat to disabled#79566

Closed
LRFLEW wants to merge 1 commit into
godotengine:masterfrom
LRFLEW:glinitrep
Closed

GLES3: Change default texture repeat to disabled#79566
LRFLEW wants to merge 1 commit into
godotengine:masterfrom
LRFLEW:glinitrep

Conversation

@LRFLEW

@LRFLEW LRFLEW commented Jul 17, 2023

Copy link
Copy Markdown
Contributor

This is another attempt to fix #79315 (the previous one being #79367, which was declined for not being a direct fix for the issue).

I used RenderDoc to analyze how the texture is being initialized to see if I could get any insights into what's going on. (Side note: I was having trouble getting RenderDoc to work with Godot, and I eventually figured out that GLManager_Windows::_nvapi_disable_threaded_optimization() was crashing when running the editor through RenderDoc. If you want to use RenderDoc with Godot, you need to stub this function if you're using Windows and an Nvidia GPU.) Looking through the "Resource Initialization Parameters" for a texture exhibiting this bug, I noticed that it was first initializing the filtering mode to one that the final texture didn't use, set the texture (and mipmap) data with glTexImage2D, and then set the actual filter mode. The repeat mode was similarly enabled before the calls to glTexImage2D, but wasn't set (either enabled or disabled) again after. This seemed to confirm the second of the theories I mentioned in the description for #79367, which is that calling glTexImage2D was disabling the texture repeat.

I looked through the code and figured out that the initial calls to set the repeat mode before calling glTexImage2D was happening here:

// Set filtering and repeat state to default.
if (mipmaps > 1) {
texture->gl_set_filter(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
} else {
texture->gl_set_filter(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST);
}
texture->gl_set_repeat(RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);

The comment states that it's setting these to "default" values, which seems to indicate to me that it's here specifically to deal with glTexImage2D resetting parameters to default values. The problem is that the actual default appears to be to disable texture repeat, not enable it. Changing RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED to RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED in these defaults makes it so that Texture::state_repeat matches the state of the texture after calling glTexImage2D, which fixes the bug.

One part of this I'm not sure of is whether this behavior of glTexImage2D resetting the texture repeat state is part of the OpenGL / OpenGL ES standard, or is an implementation detail that may vary between drivers. I tried looking it up online, but I couldn't find any relevant documentation about this. While I did test this on the machines I had access to, and they all pointed to a repeat-disabled default, I can't say for certain that there aren't platforms out there where the opposite is true, which will make setting the repeat state to a "default" value here a challenge, and may require a different solution to fix this issue.

This PR targets the master branch, but this should be trivial to cherry-pick for a 4.1.X release.

@LRFLEW

LRFLEW commented Jul 17, 2023

Copy link
Copy Markdown
Contributor Author

I finally found documentation that seems to directly address the default value for texture repeating, and found this

Initially, GL_TEXTURE_WRAP_S is set to GL_REPEAT.
Initially, GL_TEXTURE_WRAP_T is set to GL_REPEAT.
Initially, GL_TEXTURE_WRAP_R is set to GL_REPEAT.

https://registry.khronos.org/OpenGL-Refpages/gl4/html/glTexParameter.xhtml

So using RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED seems accurate to the documentation, and using this PR would likely result in the reverse problem (i.e. non-repeating textures repeating). I'm gonna close this issue and bring the discussion back to #79315, as this is too problematic to consider merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Texture Repeat sometimes ignored on Compatibility renderer

2 participants