Skip to content

Commit

Permalink
fix: remove all linear/srgb conversion, save for pbr albedo (#420)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pentalimbed authored Aug 15, 2024
1 parent aec4978 commit 0eb554b
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ float3 GetDynamicCubemapSpecularIrradiance(float2 uv, float3 N, float3 VN, float

float3 specularIrradiance = specularTexture.SampleLevel(SampColorSampler, R, level).xyz;
specularIrradiance *= horizon;
specularIrradiance = sRGB2Lin(specularIrradiance);

return specularIrradiance;
}
Expand Down Expand Up @@ -53,7 +52,6 @@ float3 GetDynamicCubemap(float2 uv, float3 N, float3 VN, float3 V, float roughne
return horizon * ((F0 + S) * specularBRDF.x + specularBRDF.y);
# else
float3 specularIrradiance = specularTexture.SampleLevel(SampColorSampler, R, level).xyz;
specularIrradiance = sRGB2Lin(specularIrradiance);

return specularIrradiance * ((F0 + S) * specularBRDF.x + specularBRDF.y);
# endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,10 @@ float3 GetSamplingVector(uint3 ThreadID, in RWTexture2DArray<float4> OutputTextu
}

#if defined(REFLECTIONS)
color.rgb = lerp(color.rgb, sRGB2Lin(ReflectionsTexture.SampleLevel(LinearSampler, uv, 0)), saturate(mipLevel / 8.0));
color.rgb = lerp(color.rgb, ReflectionsTexture.SampleLevel(LinearSampler, uv, 0), saturate(mipLevel / 8.0));
#else
color.rgb = lerp(color.rgb, color.rgb * sRGB2Lin(DefaultCubemap.SampleLevel(LinearSampler, uv, 0).x) * 2, saturate(mipLevel / 8.0));
color.rgb = lerp(color.rgb, color.rgb * DefaultCubemap.SampleLevel(LinearSampler, uv, 0).x * 2, saturate(mipLevel / 8.0));
#endif

color.rgb = Lin2sRGB(color.rgb);
EnvInferredTexture[ThreadID] = max(0, color);
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,6 @@ float3 tangentToWorld(const float3 v, const float3 N, const float3 S, const floa
return S * v.x + T * v.y + N * v.z;
}

float3 sRGB2Lin(float3 color)
{
return color > 0.04045 ? pow(color / 1.055 + 0.055 / 1.055, 2.4) : color / 12.92;
}

float3 Lin2sRGB(float3 color)
{
return color > 0.0031308 ? 1.055 * pow(color, 1.0 / 2.4) - 0.055 : 12.92 * color;
}

[numthreads(8, 8, 1)] void main(uint3 ThreadID
: SV_DispatchThreadID) {
// Make sure we won't write past output when computing higher mipmap levels.
Expand Down Expand Up @@ -181,11 +171,11 @@ float3 Lin2sRGB(float3 color)
// Mip level to sample from.
float mipLevel = max(0.5 * log2(ws / wt) + 1.0, 0.0);

color += sRGB2Lin(inputTexture.SampleLevel(linear_wrap_sampler, Li, mipLevel).rgb) * cosLi;
color += inputTexture.SampleLevel(linear_wrap_sampler, Li, mipLevel).rgb * cosLi;
weight += cosLi;
}
}
color /= weight;

outputTexture[ThreadID] = float4(Lin2sRGB(color), 1.0);
outputTexture[ThreadID] = float4(color, 1.0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ cbuffer UpdateData : register(b1)
bool IsSaturated(float value) { return value == saturate(value); }
bool IsSaturated(float2 value) { return IsSaturated(value.x) && IsSaturated(value.y); }

float3 sRGB2Lin(float3 color)
{
return color > 0.04045 ? pow(color / 1.055 + 0.055 / 1.055, 2.4) : color / 12.92;
}

// Inverse project UV + raw depth into world space.
float3 InverseProjectUVZ(float2 uv, float z)
{
Expand Down Expand Up @@ -172,7 +167,7 @@ float smoothbumpstep(float edge0, float edge1, float x)

if (linearDepth > 16.5) { // Ignore objects which are too close
float3 color = ColorTexture.SampleLevel(LinearSampler, uv, 0);
float4 output = float4(sRGB2Lin(color), 1.0);
float4 output = float4(color, 1.0);
float lerpFactor = 1.0 / 64.0;

half4 positionCS = half4(2 * half2(uv.x, -uv.y + 1) - 1, depth, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void readHistory(

half3 radiance = 0;
#ifdef GI
radiance = sRGB2Lin(FULLRES_LOAD(srcDiffuse, pixCoord, uv * frameScale, samplerLinearClamp).rgb * GIStrength);
radiance = FULLRES_LOAD(srcDiffuse, pixCoord, uv * frameScale, samplerLinearClamp).rgb * GIStrength;
# ifdef GI_BOUNCE
radiance += prev_ambient.rgb * GIBounceFade;
# endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ float4 SSSSBlurCS(
// Fetch color of current pixel:
float4 colorM = ColorTexture[DTid.xy];

#if defined(HORIZONTAL)
colorM.rgb = sRGB2Lin(colorM.rgb);
#endif

if (sssAmount == 0)
return colorM;

Expand Down Expand Up @@ -156,10 +152,6 @@ float4 SSSSBlurCS(

float3 color = ColorTexture[coords].rgb;

#if defined(HORIZONTAL)
color.rgb = sRGB2Lin(color.rgb);
#endif

float depth = DepthTexture[coords].r;
depth = GetScreenDepth(depth);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ cbuffer PerFrameSSS : register(b1)
bool humanProfile = MaskTexture[DTid.xy].y == sssAmount;

float4 color = SSSSBlurCS(DTid.xy, texCoord, float2(0.0, 1.0), sssAmount, humanProfile);
color.rgb = Lin2sRGB(color.rgb);
SSSRW[DTid.xy] = float4(color.rgb, 1.0);

#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ float3 GetWetnessAmbientSpecular(float2 uv, float3 N, float3 VN, float3 V, float
float3 specularIrradiance = 1.0;
# else
float level = roughness * 7.0;
float3 specularIrradiance = sRGB2Lin(specularTexture.SampleLevel(SampColorSampler, R, level));
float3 specularIrradiance = specularTexture.SampleLevel(SampColorSampler, R, level);
# endif
#else
float3 specularIrradiance = 1.0;
Expand Down
21 changes: 8 additions & 13 deletions package/Shaders/AmbientCompositeCS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ RWTexture2D<half3> DiffuseAmbientRW : register(u1);

half3 directionalAmbientColor = mul(DirectionalAmbient, half4(normalWS, 1.0));

half3 linAlbedo = sRGB2Lin(albedo);
half3 linDirectionalAmbientColor = sRGB2Lin(directionalAmbientColor);
half3 linDiffuseColor = sRGB2Lin(diffuseColor);

half3 linAmbient = lerp(sRGB2Lin(albedo * directionalAmbientColor), linAlbedo * linDirectionalAmbientColor, pbrWeight);
half3 ambient = albedo * directionalAmbientColor;

half visibility = 1.0;
#if defined(SKYLIGHTING)
Expand All @@ -76,24 +72,23 @@ RWTexture2D<half3> DiffuseAmbientRW : register(u1);

#if defined(SSGI)
half4 ssgiDiffuse = SSGITexture[dispatchID.xy];
ssgiDiffuse.rgb *= linAlbedo;
ssgiDiffuse.rgb *= albedo;
ssgiDiffuse.a = 1 - ssgiDiffuse.a;

visibility *= ssgiDiffuse.a;

DiffuseAmbientRW[dispatchID.xy] = linAlbedo * linDirectionalAmbientColor + ssgiDiffuse.rgb;
DiffuseAmbientRW[dispatchID.xy] = albedo * directionalAmbientColor + ssgiDiffuse.rgb;

# if defined(INTERIOR)
linDiffuseColor *= ssgiDiffuse.a;
diffuseColor *= ssgiDiffuse.a;
# endif
linDiffuseColor += ssgiDiffuse.rgb;
diffuseColor += ssgiDiffuse.rgb;
#endif

linAmbient *= visibility;
diffuseColor = Lin2sRGB(linDiffuseColor);
directionalAmbientColor = Lin2sRGB(linDirectionalAmbientColor * visibility);
ambient *= visibility;
directionalAmbientColor = directionalAmbientColor * visibility;

diffuseColor = lerp(diffuseColor + directionalAmbientColor * albedo, Lin2sRGB(linDiffuseColor + linAmbient), pbrWeight);
diffuseColor = diffuseColor + directionalAmbientColor * albedo;

MainRW[dispatchID.xy] = diffuseColor;
};
6 changes: 3 additions & 3 deletions package/Shaders/Common/PBR.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ namespace PBR

float3 AdjustDirectionalLightColor(float3 lightColor)
{
return pbrSettings.DirectionalLightColorMultiplier * sRGB2Lin(lightColor);
return pbrSettings.DirectionalLightColorMultiplier * lightColor;
}

float3 AdjustPointLightColor(float3 lightColor)
{
return pbrSettings.PointLightColorMultiplier * sRGB2Lin(lightColor);
return pbrSettings.PointLightColorMultiplier * lightColor;
}

float3 AdjustAmbientLightColor(float3 lightColor)
{
return pbrSettings.AmbientLightColorMultiplier * sRGB2Lin(lightColor);
return pbrSettings.AmbientLightColorMultiplier * lightColor;
}

// [Jimenez et al. 2016, "Practical Realtime Strategies for Accurate Indirect Occlusion"]
Expand Down
16 changes: 6 additions & 10 deletions package/Shaders/DeferredCompositeCS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Texture2D<half4> SpecularSSGITexture : register(t10);

half glossiness = normalGlossiness.z;

half3 color = lerp(diffuseColor + specularColor, Lin2sRGB(sRGB2Lin(diffuseColor) + sRGB2Lin(specularColor)), pbrWeight);
half3 color = diffuseColor + specularColor;

#if defined(DYNAMIC_CUBEMAPS)

Expand All @@ -73,8 +73,6 @@ Texture2D<half4> SpecularSSGITexture : register(t10);

normalWS = lerp(normalWS, float3(0, 0, 1), wetnessMask);

color = sRGB2Lin(color);

half depth = DepthTexture[dispatchID.xy];

half4 positionCS = half4(2 * half2(uv.x, -uv.y + 1) - 1, depth, 1);
Expand All @@ -89,12 +87,12 @@ Texture2D<half4> SpecularSSGITexture : register(t10);
half roughness = 1.0 - glossiness;
half level = roughness * 7.0;

half3 directionalAmbientColor = sRGB2Lin(mul(DirectionalAmbient, half4(R, 1.0)));
half3 directionalAmbientColor = mul(DirectionalAmbient, half4(R, 1.0));
half3 finalIrradiance = 0;

# if defined(INTERIOR)
half3 specularIrradiance = EnvTexture.SampleLevel(LinearSampler, R, level).xyz;
specularIrradiance = sRGB2Lin(specularIrradiance);
specularIrradiance = specularIrradiance;

finalIrradiance += specularIrradiance;
# elif defined(SKYLIGHTING)
Expand All @@ -114,19 +112,19 @@ Texture2D<half4> SpecularSSGITexture : register(t10);

if (skylightingSpecular < 1.0) {
specularIrradiance = EnvTexture.SampleLevel(LinearSampler, R, level).xyz;
specularIrradiance = sRGB2Lin(specularIrradiance);
specularIrradiance = specularIrradiance;
}

half3 specularIrradianceReflections = 1.0;

if (skylightingSpecular > 0.0) {
specularIrradianceReflections = EnvReflectionsTexture.SampleLevel(LinearSampler, R, level).xyz;
specularIrradianceReflections = sRGB2Lin(specularIrradianceReflections);
specularIrradianceReflections = specularIrradianceReflections;
}
finalIrradiance = finalIrradiance * skylightingSpecular + lerp(specularIrradiance, specularIrradianceReflections, skylightingSpecular);
# else
half3 specularIrradianceReflections = EnvReflectionsTexture.SampleLevel(LinearSampler, R, level).xyz;
specularIrradianceReflections = sRGB2Lin(specularIrradianceReflections);
specularIrradianceReflections = specularIrradianceReflections;

finalIrradiance += specularIrradianceReflections;
# endif
Expand All @@ -137,8 +135,6 @@ Texture2D<half4> SpecularSSGITexture : register(t10);
# endif

color += reflectance * finalIrradiance;

color = Lin2sRGB(color);
}

#endif
Expand Down
34 changes: 11 additions & 23 deletions package/Shaders/Lighting.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,8 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
float3 screenSpaceNormal = normalize(WorldToView(worldSpaceNormal, false, eyeIndex));

# if defined(TRUE_PBR)
baseColor.xyz = Lin2sRGB(baseColor.xyz);

PBR::SurfaceProperties pbrSurfaceProperties;

pbrSurfaceProperties.Roughness = saturate(rawRMAOS.x);
Expand Down Expand Up @@ -1966,7 +1968,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace

# if defined(WETNESS_EFFECTS)
if (waterRoughnessSpecular < 1.0)
wetnessSpecular += GetWetnessSpecular(wetnessNormal, normalizedDirLightDirectionWS, worldSpaceViewDirection, sRGB2Lin(dirLightColor * dirDetailShadow), waterRoughnessSpecular);
wetnessSpecular += GetWetnessSpecular(wetnessNormal, normalizedDirLightDirectionWS, worldSpaceViewDirection, dirLightColor * dirDetailShadow, waterRoughnessSpecular);
# endif
# endif

Expand Down Expand Up @@ -2175,7 +2177,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace

# if defined(WETNESS_EFFECTS)
if (waterRoughnessSpecular < 1.0)
wetnessSpecular += GetWetnessSpecular(wetnessNormal, normalizedLightDirection, worldSpaceViewDirection, sRGB2Lin(lightColor), waterRoughnessSpecular);
wetnessSpecular += GetWetnessSpecular(wetnessNormal, normalizedLightDirection, worldSpaceViewDirection, lightColor, waterRoughnessSpecular);
# endif
}
# endif
Expand Down Expand Up @@ -2228,13 +2230,8 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
# if defined(SKYLIGHTING)
float skylightingDiffuse = shFuncProductIntegral(skylightingSH, shEvaluateCosineLobe(skylightingSettings.DirectionalDiffuse ? worldSpaceNormal : float3(0, 0, 1))) / shPI;
skylightingDiffuse = Skylighting::mixDiffuse(skylightingSettings, skylightingDiffuse);
# if !defined(TRUE_PBR)
directionalAmbientColor = sRGB2Lin(directionalAmbientColor);
# endif

directionalAmbientColor *= skylightingDiffuse;
# if !defined(TRUE_PBR)
directionalAmbientColor = Lin2sRGB(directionalAmbientColor);
# endif
# endif

# if defined(TRUE_PBR) && defined(LOD_LAND_BLEND) && !defined(DEFERRED)
Expand Down Expand Up @@ -2264,7 +2261,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
dynamicCubemap = true;
envColorBase = TexEnvSampler.SampleLevel(SampEnvSampler, float3(1.0, 0.0, 0.0), 0);
if (envColorBase.a < 1.0) {
F0 = sRGB2Lin(envColorBase.rgb) + sRGB2Lin(baseColor.rgb);
F0 = envColorBase.rgb + baseColor.rgb;
envRoughness = envColorBase.a;
} else {
F0 = 1.0;
Expand All @@ -2275,7 +2272,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
# if defined(CREATOR)
if (cubemapCreatorSettings.Enabled) {
dynamicCubemap = true;
F0 = sRGB2Lin(cubemapCreatorSettings.CubemapColor.rgb) + sRGB2Lin(baseColor.xyz);
F0 = cubemapCreatorSettings.CubemapColor.rgb + baseColor.xyz;
envRoughness = cubemapCreatorSettings.CubemapColor.a;
}
# endif
Expand All @@ -2284,7 +2281,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
# if defined(EMAT)
envRoughness = lerp(envRoughness, 1.0 - complexMaterialColor.y, (float)complexMaterial);
envRoughness *= envRoughness;
F0 = lerp(F0, sRGB2Lin(complexSpecular), (float)complexMaterial);
F0 = lerp(F0, complexSpecular, (float)complexMaterial);
# endif

envColor = GetDynamicCubemap(screenUV, worldSpaceNormal, worldSpaceVertexNormal, worldSpaceViewDirection, envRoughness, F0, diffuseColor, viewPosition.z) * envMask;
Expand Down Expand Up @@ -2413,7 +2410,6 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
# else
diffuseColor = 1.0;
# endif
specularColor = sRGB2Lin(specularColor);
}
# endif

Expand All @@ -2428,10 +2424,6 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
# else
specularColor += envColor * diffuseColor;
# endif
# if defined(DYNAMIC_CUBEMAPS)
if (dynamicCubemap)
specularColor = Lin2sRGB(specularColor);
# endif
# endif

# if defined(EMAT) && defined(ENVMAP)
Expand All @@ -2442,8 +2434,6 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
# if !defined(DEFERRED)
color.xyz += specularColor;
# endif

color.xyz = sRGB2Lin(color.xyz);
# endif

# if defined(WETNESS_EFFECTS) && !defined(TRUE_PBR)
Expand All @@ -2454,8 +2444,6 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
color.xyz += specularColorPBR;
# endif

color.xyz = Lin2sRGB(color.xyz);

# if defined(LOD_LAND_BLEND) && defined(TRUE_PBR)
{
pbrWeight = 1 - lodLandBlendFactor;
Expand All @@ -2465,7 +2453,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace

# if defined(DEFERRED)
specularColorPBR = lerp(specularColorPBR, 0, lodLandBlendFactor);
indirectDiffuseLobeWeight = lerp(indirectDiffuseLobeWeight, sRGB2Lin(input.Color.xyz * lodLandColor * lodLandFadeFactor), lodLandBlendFactor);
indirectDiffuseLobeWeight = lerp(indirectDiffuseLobeWeight, input.Color.xyz * lodLandColor * lodLandFadeFactor, lodLandBlendFactor);
indirectSpecularLobeWeight = lerp(indirectSpecularLobeWeight, 0, lodLandBlendFactor);
pbrGlossiness = lerp(pbrGlossiness, 0, lodLandBlendFactor);
# endif
Expand Down Expand Up @@ -2606,13 +2594,13 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace

float3 outputSpecular = specularColor.xyz;
# if defined(TRUE_PBR)
outputSpecular = Lin2sRGB(specularColorPBR.xyz);
outputSpecular = specularColorPBR.xyz;
# endif
psout.Specular = float4(outputSpecular, psout.Diffuse.w);

float3 outputAlbedo = baseColor.xyz * vertexColor;
# if defined(TRUE_PBR)
outputAlbedo = Lin2sRGB(indirectDiffuseLobeWeight);
outputAlbedo = indirectDiffuseLobeWeight;
# endif
psout.Albedo = float4(outputAlbedo, psout.Diffuse.w);

Expand Down
Loading

0 comments on commit 0eb554b

Please sign in to comment.