-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Fix rendering images with nearest sampling and 2^n size #11162
Conversation
src/render/draw_raster.js
Outdated
|
||
context.activeTexture.set(gl.TEXTURE0); | ||
tile.texture.bind(textureFilter, gl.CLAMP_TO_EDGE, gl.LINEAR_MIPMAP_NEAREST); | ||
tile.texture.bind(textureFilter, gl.CLAMP_TO_EDGE, minFilter); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we fixing this at the correct level, have you considered whether there is something incorrect within the internal texture class?
I feel like the texture class could be safer for the developer based on intent. At the moment it's possible to call texture.bind(filter, a_mip_map_filter)
, without necessarily having created the texture with a call that falls through generateMipMap()
, resulting in an incoherent mipmap filter set on a texture that doesn't have any mipmap created.
We could assert or warn if from the callee we request that combination to prevent silent failure.
The flag this.useMipmap
in that class may be misleading, because it may be set to true
, but might not represent the internal state of whether that texture uses mipmaps. I'd suggest replacing it with this.canUseMipMaps = Boolean(options && options.useMipmap) && isPowerOfTwo()
.
Then, at binding:
bind(...):
if (minFilter is mipmap filter && !canUseMipMaps) {
incoherent filter request: no mipmap created and requested a mipmap filter, fallback to minFilter = filter
}
...
I agree @karimnaaji , this is exactly what's causing this issue. There's currently a check that's replacing mapbox-gl-js/src/render/texture.js Lines 94 to 96 in 1cf7180
In my latest commits, I've refactored this check to change I see two potential issues with my change, both seem unlikely:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice find and fix! One nit about keeping mipmap sampling when we use NEAREST after requesting a texture with mipmap as an option. Otherwise LGTM.
gl.generateMipmap(gl.TEXTURE_2D); | ||
} | ||
} | ||
|
||
bind(filter: TextureFilter, wrap: TextureWrap, minFilter: ?TextureFilter) { | ||
bind(filter: TextureFilter, wrap: TextureWrap) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 on removing the extra parameter as it really simplifies use of that class (basically infers that from the option request to use mipmaps).
src/render/texture.js
Outdated
if (filter !== this.filter) { | ||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filter); | ||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, minFilter || filter); | ||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, (this.useMipmap && filter === gl.LINEAR) ? gl.LINEAR_MIPMAP_NEAREST : filter); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 That's the correct fallback a filtering request on LINEAR.
For NEAREST, if we request texture.bind(NEAREST, ...) I would still expect as a user of this class to fallback to NEAREST_MIPMAP_NEAREST (nearest within the mip level and nearest between mip levels).
It seems that most of our use cases need to stay non-linear between mip level as a default (we're not using trilinear, but we could extend that when we need it for some cases, e.g. LINEAR_MIPMAP_LINEAR), so we can stick to that for now but still hint the requested filter for within the mip (e.g. filter_MIPMAP_NEAREST).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In practice what that means as a fallback in this case:
- LINEAR + mipmap -> LINEAR_MIPMAP_NEAREST
- NEAREST + mipmap -> NEAREST_MIPMAP_NEAREST
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! Happy to add the condition for nearest with mipmap. In practice I don't think this is ever happening, but not a bad idea to future-proof it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's a possible combination when a user chooses nearest as a paint properties with raster-resampling
because we always request mipmaps for it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense! I noticed that code path didn't run in my example, but it should now correctly use mipmaps when it is called. (Repeating textures?)
This class is only used in internal code so no worry about that. There's only one case at the moment where we explicitly request mipmaps (raster_tile_source) so this isn't breaking usage. |
* Added render tests for image with a power of 2 size * Added nearest resampling, test is failing * Fix by disabling mipmaps * Keeping same behavior with linear sampling * Correcting expected.ping to use nearest filtering * Adding check for useMipMap * Refactoring * Removing third argument from * Fix broken refactoring * Removing stray console.log * Readd mipmaps * minor refactor * Mipmaps with nearest
Closes #11132
This code appears to be attempting to use mipmaps when textures are a power of two square:
mapbox-gl-js/src/render/texture.js
Lines 94 to 96 in 1333ef1
In the case where we pass in
bind(gl.NEAREST, gl.CLAMP_TO_EDGE, gl.LINEAR_MIPMAP_NEAREST)
, we get:In this case rendering doesn't work as intended, instead drawing every pixel black.
By replacing gl.LINEAR_MIPMAP_NEAREST with gl.LINEAR this issue appears to be fixed.
Before:
Image on the right has paint property 'raster-resampling': 'nearest'
After:
Launch Checklist
mapbox-gl-js
changelog:<changelog>Fix rendering issue with power of two square images and 'raster-resampling': 'nearest'</changelog>