engine icon indicating copy to clipboard operation
engine copied to clipboard

Use gamma textures format (WebGl2)

Open mvaligursky opened this issue 4 years ago • 2 comments

Currently, when the engine samples a texture stored in gamma space, it uses gammaCorrectInput function to bring the values to linear space for lighting, costing us a pow instruction per sample, and also bilinear interpolation when sampling takes place in sRGB space, which is slightly incorrect.

On Webgl2 devices, and also on Webgl1 devices with EXT_sRGB (https://developer.mozilla.org/en-US/docs/Web/API/EXT_sRGB), we could use hardware support for sRGB textures.

See here: (http://www.realtimerendering.com/blog/webgl-2-new-features/) The sRGB texture will be automatically converted to linear space when being fetched in the shader.

The disadvantage here is that the texture needs to be marked as sRGB when we create a WebGl texture, meaning likely that we would need to expose a texture import setting in the Editor to allow textures to be marked as sRGB (diffuse and similar texture need sRGB, normal map, roughness and others don't as they are in linear space).

mvaligursky avatar Nov 18 '21 11:11 mvaligursky

Our gamma space textures also currently have their mipmaps generated in gamma space, which is wrong. Marking texture sRGB will result in linear-space generated mipmaps.

We also need to address compressed textures in the editor. Textures are assumed gamma space (and converted to linear for mipmaps) - so linear-space textures currently have the inverse mipmap problem. (Though I believe normal maps are correctly handled).

slimbuck avatar May 11 '22 09:05 slimbuck

Note that ideally we'd use sRGB framebuffers as well, which automatically encode linear -> sRGB on write. https://hackmd.io/@jgilbert/sRGB-WebGL

mvaligursky avatar Sep 22 '23 13:09 mvaligursky