Skip to content

Commit

Permalink
feat: pbr diffuse lighting in srgb. (#429)
Browse files Browse the repository at this point in the history
* feat: PBR diffuse calculated in sRGB.

* style: 🎨 apply clang-format changes

* feat: slider for base color gamma.

* feat: transform for other PBR diffuse colors.

* style: 🎨 apply clang-format changes

* fix: fix for pbr-non-pbr land blending.

* fix: default base color gamma set to 1.9.

---------

Co-authored-by: Ilya Perapechka <i_perapechka@wargaming.net>
Co-authored-by: Jonahex <Jonahex@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 18, 2024
1 parent c644210 commit 70b8a12
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 71 deletions.
5 changes: 1 addition & 4 deletions package/Shaders/AmbientCompositeCS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ RWTexture2D<half3> DiffuseAmbientRW : register(u1);
half3 linDirectionalAmbientColor = sRGB2Lin(directionalAmbientColor);
half3 linDiffuseColor = sRGB2Lin(diffuseColor);

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

half visibility = 1.0;
#if defined(SKYLIGHTING)
float rawDepth = DepthTexture[dispatchID.xy];
Expand Down Expand Up @@ -89,11 +87,10 @@ RWTexture2D<half3> DiffuseAmbientRW : register(u1);
linDiffuseColor += ssgiDiffuse.rgb;
#endif

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

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

MainRW[dispatchID.xy] = diffuseColor;
};
44 changes: 35 additions & 9 deletions package/Shaders/Common/PBR.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,32 @@ namespace PBR
return surfaceProperties;
}

struct LightProperties
{
float3 LightColor;
float3 CoatLightColor;
float3 LinearLightColor;
float3 LinearCoatLightColor;
};

LightProperties InitLightProperties(float3 lightColor, float3 nonParallaxShadow, float3 parallaxShadow)
{
LightProperties result;
result.LightColor = lightColor * nonParallaxShadow * parallaxShadow;
result.LinearLightColor = sRGB2Lin(lightColor) * nonParallaxShadow * parallaxShadow;
[branch] if ((PBRFlags & TruePBR_InterlayerParallax) != 0)
{
result.CoatLightColor = lightColor * nonParallaxShadow;
result.LinearCoatLightColor = sRGB2Lin(lightColor) * nonParallaxShadow;
}
else
{
result.CoatLightColor = result.LightColor;
result.LinearCoatLightColor = result.LinearLightColor;
}
return result;
}

float3 AdjustDirectionalLightColor(float3 lightColor)
{
return pbrSettings.DirectionalLightColorMultiplier * sRGB2Lin(lightColor);
Expand Down Expand Up @@ -372,7 +398,7 @@ namespace PBR
return color;
}

void GetDirectLightInput(out float3 diffuse, out float3 coatDiffuse, out float3 transmission, out float3 specular, float3 N, float3 coatN, float3 V, float3 coatV, float3 L, float3 coatL, float3 lightColor, float3 coatLightColor, SurfaceProperties surfaceProperties,
void GetDirectLightInput(out float3 diffuse, out float3 coatDiffuse, out float3 transmission, out float3 specular, float3 N, float3 coatN, float3 V, float3 coatV, float3 L, float3 coatL, LightProperties lightProperties, SurfaceProperties surfaceProperties,
float3x3 tbnTr, float2 uv)
{
diffuse = 0;
Expand All @@ -397,12 +423,12 @@ namespace PBR
#if !defined(LANDSCAPE) && !defined(LODLANDSCAPE)
[branch] if ((PBRFlags & TruePBR_HairMarschner) != 0)
{
transmission += PI * lightColor * GetHairColorMarschner(N, V, L, NdotL, NdotV, VdotL, 0, 1, 0, surfaceProperties);
transmission += PI * lightProperties.LightColor * GetHairColorMarschner(N, V, L, NdotL, NdotV, VdotL, 0, 1, 0, surfaceProperties);
}
else
#endif
{
diffuse += lightColor * satNdotL;
diffuse += lightProperties.LightColor * satNdotL;

#if defined(GLINT)
GlintInput glintInput;
Expand All @@ -418,9 +444,9 @@ namespace PBR

float3 F;
#if defined(GLINT)
specular += PI * GetSpecularDirectLightMultiplierMicrofacetWithGlint(surfaceProperties.Roughness, surfaceProperties.F0, satNdotL, satNdotV, satNdotH, satVdotH, glintInput, F) * lightColor * satNdotL;
specular += PI * GetSpecularDirectLightMultiplierMicrofacetWithGlint(surfaceProperties.Roughness, surfaceProperties.F0, satNdotL, satNdotV, satNdotH, satVdotH, glintInput, F) * lightProperties.LinearLightColor * satNdotL;
#else
specular += PI * GetSpecularDirectLightMultiplierMicrofacet(surfaceProperties.Roughness, surfaceProperties.F0, satNdotL, satNdotV, satNdotH, satVdotH, F) * lightColor * satNdotL;
specular += PI * GetSpecularDirectLightMultiplierMicrofacet(surfaceProperties.Roughness, surfaceProperties.F0, satNdotL, satNdotV, satNdotH, satVdotH, F) * lightProperties.LinearLightColor * satNdotL;
#endif

float2 specularBRDF = 0;
Expand All @@ -433,7 +459,7 @@ namespace PBR
#if !defined(LANDSCAPE) && !defined(LODLANDSCAPE)
[branch] if ((PBRFlags & TruePBR_Fuzz) != 0)
{
float3 fuzzSpecular = PI * GetSpecularDirectLightMultiplierMicroflakes(surfaceProperties.Roughness, surfaceProperties.FuzzColor, satNdotL, satNdotV, satNdotH, satVdotH) * lightColor * satNdotL;
float3 fuzzSpecular = PI * GetSpecularDirectLightMultiplierMicroflakes(surfaceProperties.Roughness, surfaceProperties.FuzzColor, satNdotL, satNdotV, satNdotH, satVdotH) * lightProperties.LinearLightColor * satNdotL;
[branch] if (pbrSettings.UseMultipleScattering)
{
fuzzSpecular *= 1 + surfaceProperties.FuzzColor * (1 / (specularBRDF.x + specularBRDF.y) - 1);
Expand All @@ -448,7 +474,7 @@ namespace PBR
float forwardScatter = exp2(saturate(-VdotL) * subsurfacePower - subsurfacePower);
float backScatter = saturate(satNdotL * surfaceProperties.Thickness + (1.0 - surfaceProperties.Thickness)) * 0.5;
float subsurface = lerp(backScatter, 1, forwardScatter) * (1.0 - surfaceProperties.Thickness);
transmission += surfaceProperties.SubsurfaceColor * subsurface * lightColor;
transmission += surfaceProperties.SubsurfaceColor * subsurface * lightProperties.LightColor;
}
else if ((PBRFlags & TruePBR_TwoLayer) != 0)
{
Expand All @@ -467,13 +493,13 @@ namespace PBR
}

float3 coatF;
float3 coatSpecular = PI * GetSpecularDirectLightMultiplierMicrofacet(surfaceProperties.CoatRoughness, surfaceProperties.CoatF0, coatNdotL, coatNdotV, coatNdotH, coatVdotH, coatF) * coatLightColor * coatNdotL;
float3 coatSpecular = PI * GetSpecularDirectLightMultiplierMicrofacet(surfaceProperties.CoatRoughness, surfaceProperties.CoatF0, coatNdotL, coatNdotV, coatNdotH, coatVdotH, coatF) * lightProperties.LinearCoatLightColor * coatNdotL;

float3 layerAttenuation = 1 - coatF * surfaceProperties.CoatStrength;
diffuse *= layerAttenuation;
specular *= layerAttenuation;

coatDiffuse += coatLightColor * coatNdotL;
coatDiffuse += lightProperties.CoatLightColor * coatNdotL;
specular += coatSpecular * surfaceProperties.CoatStrength;
}
#endif
Expand Down
1 change: 1 addition & 0 deletions package/Shaders/Common/SharedData.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ struct PBRSettings
float DirectionalLightColorMultiplier;
float PointLightColorMultiplier;
float AmbientLightColorMultiplier;
float BaseColorGamma;
uint UseMultipleScattering;
uint UseMultiBounceAO;
uint3 pad0;
Expand Down
Loading

0 comments on commit 70b8a12

Please sign in to comment.