Skip to content

Commit

Permalink
revert: revert linearisation attempts (#426)
Browse files Browse the repository at this point in the history
* Revert "fix: remove tpbr albedo lin2srgb (#421)"

This reverts commit 605b68b.

* Revert "fix: remove all linear/srgb conversion, save for pbr albedo (#420)"

This reverts commit 0eb554b.
  • Loading branch information
Pentalimbed authored Aug 17, 2024
1 parent 6ed664e commit 17013c7
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ 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 @@ -52,6 +53,7 @@ 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,10 +90,11 @@ float3 GetSamplingVector(uint3 ThreadID, in RWTexture2DArray<float4> OutputTextu
}

#if defined(REFLECTIONS)
color.rgb = lerp(color.rgb, ReflectionsTexture.SampleLevel(LinearSampler, uv, 0), saturate(mipLevel / 8.0));
color.rgb = lerp(color.rgb, sRGB2Lin(ReflectionsTexture.SampleLevel(LinearSampler, uv, 0)), saturate(mipLevel / 8.0));
#else
color.rgb = lerp(color.rgb, color.rgb * DefaultCubemap.SampleLevel(LinearSampler, uv, 0).x * 2, saturate(mipLevel / 8.0));
color.rgb = lerp(color.rgb, color.rgb * sRGB2Lin(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,6 +118,16 @@ 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 @@ -171,11 +181,11 @@ float3 tangentToWorld(const float3 v, const float3 N, const float3 S, const floa
// Mip level to sample from.
float mipLevel = max(0.5 * log2(ws / wt) + 1.0, 0.0);

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

outputTexture[ThreadID] = float4(color, 1.0);
outputTexture[ThreadID] = float4(Lin2sRGB(color), 1.0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ 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 @@ -167,7 +172,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(color, 1.0);
float4 output = float4(sRGB2Lin(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 = FULLRES_LOAD(srcDiffuse, pixCoord, uv * frameScale, samplerLinearClamp).rgb * GIStrength;
radiance = sRGB2Lin(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,6 +99,10 @@ 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 @@ -152,6 +156,10 @@ 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,6 +39,7 @@ 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 = specularTexture.SampleLevel(SampColorSampler, R, level);
float3 specularIrradiance = sRGB2Lin(specularTexture.SampleLevel(SampColorSampler, R, level));
# endif
#else
float3 specularIrradiance = 1.0;
Expand Down
21 changes: 13 additions & 8 deletions package/Shaders/AmbientCompositeCS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ RWTexture2D<half3> DiffuseAmbientRW : register(u1);

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

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

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

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

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

visibility *= ssgiDiffuse.a;

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

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

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

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

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 @@ -87,17 +87,17 @@ namespace PBR

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

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

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

// [Jimenez et al. 2016, "Practical Realtime Strategies for Accurate Indirect Occlusion"]
Expand Down
16 changes: 10 additions & 6 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 = diffuseColor + specularColor;
half3 color = lerp(diffuseColor + specularColor, Lin2sRGB(sRGB2Lin(diffuseColor) + sRGB2Lin(specularColor)), pbrWeight);

#if defined(DYNAMIC_CUBEMAPS)

Expand All @@ -73,6 +73,8 @@ 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 @@ -87,12 +89,12 @@ Texture2D<half4> SpecularSSGITexture : register(t10);
half roughness = 1.0 - glossiness;
half level = roughness * 7.0;

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

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

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

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

half3 specularIrradianceReflections = 1.0;

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

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

color += reflectance * finalIrradiance;

color = Lin2sRGB(color);
}

#endif
Expand Down
32 changes: 23 additions & 9 deletions package/Shaders/Lighting.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -1996,7 +1996,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace

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

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

# if defined(WETNESS_EFFECTS)
if (waterRoughnessSpecular < 1.0)
wetnessSpecular += GetWetnessSpecular(wetnessNormal, normalizedLightDirection, worldSpaceViewDirection, lightColor, waterRoughnessSpecular);
wetnessSpecular += GetWetnessSpecular(wetnessNormal, normalizedLightDirection, worldSpaceViewDirection, sRGB2Lin(lightColor), waterRoughnessSpecular);
# endif
}
# endif
Expand Down Expand Up @@ -2258,8 +2258,13 @@ 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 @@ -2289,7 +2294,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 = envColorBase.rgb + baseColor.rgb;
F0 = sRGB2Lin(envColorBase.rgb) + sRGB2Lin(baseColor.rgb);
envRoughness = envColorBase.a;
} else {
F0 = 1.0;
Expand All @@ -2300,7 +2305,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
# if defined(CREATOR)
if (cubemapCreatorSettings.Enabled) {
dynamicCubemap = true;
F0 = cubemapCreatorSettings.CubemapColor.rgb + baseColor.xyz;
F0 = sRGB2Lin(cubemapCreatorSettings.CubemapColor.rgb) + sRGB2Lin(baseColor.xyz);
envRoughness = cubemapCreatorSettings.CubemapColor.a;
}
# endif
Expand All @@ -2309,7 +2314,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, complexSpecular, (float)complexMaterial);
F0 = lerp(F0, sRGB2Lin(complexSpecular), (float)complexMaterial);
# endif

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

Expand All @@ -2452,6 +2458,10 @@ 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 @@ -2462,6 +2472,8 @@ 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 @@ -2472,6 +2484,8 @@ 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 @@ -2481,7 +2495,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace

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

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

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

Expand Down
Loading

0 comments on commit 17013c7

Please sign in to comment.