microsoft / DirectXTK12

The DirectX Tool Kit (aka DirectXTK12) is a collection of helper classes for writing DirectX 12 code in C++
https://walbourn.github.io/directx-tool-kit-for-directx-12/
MIT License
1.45k stars 369 forks source link

GenerateMips on sRGB Textures averaging in gamma space? #142

Open StephenMLucas opened 1 year ago

StephenMLucas commented 1 year ago

Hi,

Reviewing the implementation of GenerateMips for sRGB textures it looks like the implementation is doing a reinterpret copy into a linear texture format and then using a bilinear sampler in the ComputeShader to perform the average.

Unless I'm missing something about the math for this case it seems like doing the linear arithmetic on gamma encoded values in the sampler is going to result in color errors in the output mips, as demonstrated by this GLTF test case; https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/TextureLinearInterpolationTest

walbourn commented 1 year ago

Right. I should add a inverse gamma/regamma in this case to the sahder...

StephenMLucas commented 1 year ago

Presumably the other option would be to use a regular pixel shader pipeline, like the post processing functions, where the sRGB formats are directly supported as both SRVs and RenderTargets to move the conversion before the filtering in the sampler?

I don't know whether having the compute shader version do 4 point samples, gamma converting, taking the average and regammaing would result in any overall performance difference compared with using the built-in pixel shader functions though.

walbourn commented 1 year ago

GenerateMips has always been a bug-farm, and on Direct3D 11 the implementation was very driver-dependent. It does seem like the only "correct" way to solve this is to do as you say and create yet another generatemips codepath using classic texture down-sizing on the GPU.