DirectXTK12 icon indicating copy to clipboard operation
DirectXTK12 copied to clipboard

GenerateMips on sRGB Textures averaging in gamma space?

Open StephenMLucas opened this issue 3 years ago • 4 comments

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

StephenMLucas avatar Dec 02 '22 19:12 StephenMLucas

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

walbourn avatar Dec 02 '22 22:12 walbourn

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.

StephenMLucas avatar Dec 03 '22 00:12 StephenMLucas

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.

walbourn avatar Mar 26 '23 19:03 walbourn