floooh / sokol

minimal cross-platform standalone C headers
https://floooh.github.io/sokol-html5
zlib License
7.05k stars 494 forks source link

Auto-update mipmaps for render targets #598

Open SanderMertens opened 2 years ago

SanderMertens commented 2 years ago

Hi, I was trying to use mipmapped images with render targets & noticed that mipmaps aren't generated for those. I was able to make it work by adding this snippet:

if (img->cmn.render_target && (img->cmn.num_mipmaps > 1)) {
  glGenerateMipmap(GL_TEXTURE_2D);   
}

to _sg_gl_create_image (right after the for (int slot = 0; slot < img->cmn.num_slots; slot++) { loop) and _sg_gl_apply_bindings (right after _sg_gl_cache_bind_texture).

I verified it works on macOS (SOKOL_GLCORE33) and webgl (SOKOL_GLES3, chrome, firefox, safari and MS edge). Would be cool if this could be supported, it's a cheap way to downsample render targets.

floooh commented 2 years ago

IME glGenerateMipmaps() is one of those GL functions which can cause trouble in poorly implemented GL drivers (e.g. on mobile), but maybe this has improved. Just something to keep in mind (for instance currently there is this unfixed crash on macOS Catalina: https://twitter.com/Akien/status/1459120447228989440)

IFIR there are a number of similar requests in the ticket list though... I think the consensus was that the best solution would be to add a boolean flag to sg_image_desc, and that glGenerateMipmaps (or the equivalent for D3D11 and Metal) would be called "when needed" (e.g. for render targets at the end of their render pass, and for immutable textures once during creation).

I'll work on smaller things next, and this would be a good candidate.