Advertisement

Gamma correct? freshen up

Started by May 27, 2018 12:11 PM
6 comments, last by Jihodg 6 years, 8 months ago

Hi all,

It's been a while since I've been working on my HLSL shaders, and found out I'm not 100% sure if I'm applying gamma correctness correctly. So here's what I do:

- create backbuffer in this format: DXGI_FORMAT_R8G8B8A8_UNORM_SRGB
- source textures (DDS) are always in SRGB format
- this way the textures should be gamma correct, because DX11 helps me out here

Now my question is about material and light colors. I'm not sure if I need to convert those to linear space. The colors are handpicked on screen, so I guess gamma correct. Below are 2 screenshots, the darker is including converting those colors (``return float4(linearColor.rgb * linearColor.rgb, linearColor.a);``), in the lighter shot I didn't do this conversion.

These are the properties of the brick material and the light source (there are no other lightsources in the scene, also no global ambient):

Material:


CR_VECTOR4(0.51f, 0.26f, 0.22f, 1.0f),		// ambient
CR_VECTOR4(0.51f, 0.26f, 0.22f, 1.0f),		// diffuse RGB + alpha
CR_VECTOR4(0.51f, 0.26f, 0.22f, 4.0f));	// specular RGB + power

Directional light:


	mDirLights[0].Ambient	= CR_VECTOR4(0.1f, 0.1f, 0.1f, 1.0f);
	mDirLights[0].Diffuse	= CR_VECTOR4(0.75f, 0.75f, 0.75f, 1.0f);
	mDirLights[0].Specular	= CR_VECTOR4(1.0f, 1.0f, 1.0f, 16.0f);
	mDirLights[0].Direction = CR_VECTOR3(0.0f, 1.0f, 0.0f);


So in short, should I or should I not do this conversion in the lighting calculation in the shader? (and/or what else are you seeing :))
Note that I don't do anything with the texture color, after it's fetched in the shader (no conversions), which I believe is correct.

 

gamma2-noconvinshader.png

gamma1-convinshader.png

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

I don't know the actual formulas your are using for your light calculations, but of course all the parameters used should be in the same linear space before operating on them!

Advertisement

OK, clear. That means that every lighting and material property containing a color, used in the HLSL shader code, needs to be in linear space. So either provide sRGB source data and do the conversion (sqrt) in the shader code, or provide linear space values directly.

I also did some more reading, the way the _SRGB buffer formats in DX11 work, is that you don't have to manually go back from linear space to sRGB/ gamma correct (in the PS), because DX11 does that for you. The same goes for using source color textures (DDS) in SRGB format, if the texture and view are in a xxxxSRGB format, conversion is also 'free'.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

35 minutes ago, cozzie said:

OK, clear. That means that every lighting and material property containing a color, used in the HLSL shader code, needs to be in linear space. So either provide sRGB source data and do the conversion (sqrt) in the shader code, or provide linear space values directly.

I also did some more reading, the way the _SRGB buffer formats in DX11 work, is that you don't have to manually go back from linear space to sRGB/ gamma correct (in the PS), because DX11 does that for you.

yes, exactly!... and you are correct, if a texture is marked as sRGB format, the color values will be converted to linear upon reading and back to sRGB upon writing.

Also, as I said before, I don't know exactly how you are calculating your lighting, but clearly if the material diffuse you showed is multiplied by the diffuse from the texture, the result will be a darker, red tinted, brick texture... and is the ambient material value also multiplied by the light ambient? if so, it would explain the quite dark overall ambient lighting

Yep, this makes sense indeed. I even wonder if it makes sense to give a lightsource different lighting colors for ambient, diffuse and specular. Most articles/ documentation I looked up, only use 1 color for the light, which is then applied to the different material properties where the light 'hits' (ambient, diffuse, specular).

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Advertisement
9 hours ago, cozzie said:

 I even wonder if it makes sense to give a lightsource different lighting colors for ambient, diffuse and specular

hahaha yes... I found it really odd, but since you are still using the classic and really "hacky" ambient+diffuse+specular lighting model anything was possible ;D

most of us have moved on to use some kind of PBR, trying to keep conservation of energy and use parameters to represent physical properties instead, hence the roughness/smoothness and metallic workflows. (even though there are still a lot of hacks and approximations obviously)

PS: the "everything in the same linear space" rule applies to whichever lighting model you choose

This topic is closed to new replies.

Advertisement