Skip to content

Introduce LightLoop output structure #754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ From Unity 2020.1, Cookie on light are not taken into account for the lightmaps

## Default Volume Profile

From Unity 2020.1, the Default Volume Profile asset has changed so that the Exposure component sets the default Compensation to 0.
From Unity 2020.1, the Default Volume Profile asset has changed so that the Exposure component sets the default Compensation to 0. This may cause a decrease of brightness of 1EV on scene that haven't change the default settings and aren't overriding it.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,28 @@

In the High Definition Render Pipeline (HDRP), some features work differently between major versions of Unity. This document helps you upgrade HDRP from Unity 2020.1 to 2020.2.

## Constant Buffer API

From Unity 2020.2, HDRP is using a new constant buffer API that allow to setup uniform used during the Frame and sent to the shader in a single transfer instead of multiple one. The consequence is that it is no longer possible to setup any of the values declare in ShaderVariablesGlobal.cs individualy with cmd.SetVectorXXX() or related function. It is now required to update the value of ShaderVariablesGlobal to be able to update the values use in the shaders.

## FrameSettings

From Unity 2020.2, "MSAA Within Forward" Camera Frame Setting is enabled by default when new Render Pipeline asset is created.

## Lighting

From Unity 2020.2, when the Sky component affected to the volume profile used for Static Lighting Sky in Environment settings of the Lighting panel is disabled. It now don't affect the bake lighting. Previously the Sky was still affecting the bake lighting even if disabled.

## Shadows

From Unity 2020.2, it is not necessary to change the [HDRP Config package](HDRP-Config-Package.html) in order to set the [Shadows filtering quality](HDRP-Asset.html#FilteringQualities) for Deferred rendering. Instead the filtering quality can be simply set on the [HDRP Asset](HDRP-Asset.html#FilteringQualities) similarly to what was previously setting only the quality for Forward. Note that if previously the Shadow filtering quality wasn't setup on medium on the HDRP Asset you will experience a change of shadow quality as now it will be taken into account.

Starting from 2020.2, HDRP now stores OnEnable and OnDemand shadows in a separate atlas and more API is available to handle them. For more information, see [Shadows in HDRP](Shadows-in-HDRP.md).

From Unity 2020.2, the shader function `SampleShadow_PCSS` now requires you to pass in an additional float2 parameter which contains the shadow atlas resolution in x and the inverse of the atlas resolution in y.
From Unity 2020.2, the shader function `SampleShadow_PCSS` now requires you to pass in an additional float2 parameter which contains the shadow atlas resolution in x and the inverse of the atlas resolution in y.

## Shader code

A new structure is use to output the information of the LightLoop. LightLoop struct is use instead of the pair (float3 diffuseLighting, float3 specularLighting). This is to allow to export more information from the LightLoop in the future without breaking the API. The function LightLoop() - For rasterization and raytracing - PostEvaluateBSDF(), ApplyDebug() and PostEvaluateBSDFDebugDisplay now pass this structure instead of the Pair. The function LightLoop() will initialize this structure to zero. To upgrade existing shader, replace the declaration "float3 diffuseLighting; float3 specularLighting;" by "LightLoopOutput lightLoopOutput;" before call of LightLoop and repalce the argument pair "out float3 diffuseLighting, out float3 specularLighting" by "out LightLoopOutput lightLoopOutput" in all the function mention.

The prototype of the function ModifyBakedDiffuseLighting() in the various material have change from "void ModifyBakedDiffuseLighting(float3 V, PositionInputs posInput, SurfaceData surfaceData, inout BuiltinData builtinData)" to "void ModifyBakedDiffuseLighting(float3 V, PositionInputs posInput, PreLightData preLightData, BSDFData bsdfData, inout BuiltinData builtinData)". There is a new ModifyBakedDiffuseLighting using the former prototype added in the file BuiltinUtilities.hlsl which will call the new function prototype with the correct arguments. The purpose of this change it to prepare for future lighting features. To update code, in addition of the prototype update it is required to remove those line "BSDFData bsdfData = ConvertSurfaceDataToBSDFData(posInput.positionSS, surfaceData); PreLightData preLightData = GetPreLightData(V, posInput, bsdfData);" as it is now perform by the common code from BuiltinUtilities.hlsl.
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,13 @@ Shader "Hidden/HDRP/Deferred"

PreLightData preLightData = GetPreLightData(V, posInput, bsdfData);

float3 diffuseLighting;
float3 specularLighting;
LightLoop(V, posInput, preLightData, bsdfData, builtinData, LIGHT_FEATURE_MASK_FLAGS_OPAQUE, diffuseLighting, specularLighting);
LightLoopOutput lightLoopOutput;
LightLoop(V, posInput, preLightData, bsdfData, builtinData, LIGHT_FEATURE_MASK_FLAGS_OPAQUE, lightLoopOutput);

// Alias
float3 diffuseLighting = lightLoopOutput.diffuseLighting;
float3 specularLighting = lightLoopOutput.specularLighting;

diffuseLighting *= GetCurrentExposureMultiplier();
specularLighting *= GetCurrentExposureMultiplier();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,12 @@ void SHADE_OPAQUE_ENTRY(uint3 dispatchThreadId : SV_DispatchThreadID, uint2 grou

PreLightData preLightData = GetPreLightData(V, posInput, bsdfData);

float3 diffuseLighting;
float3 specularLighting;
LightLoop(V, posInput, preLightData, bsdfData, builtinData, featureFlags, diffuseLighting, specularLighting);
LightLoopOutput lightLoopOutput;
LightLoop(V, posInput, preLightData, bsdfData, builtinData, featureFlags, lightLoopOutput);

// Alias
float3 diffuseLighting = lightLoopOutput.diffuseLighting;
float3 specularLighting = lightLoopOutput.specularLighting;

diffuseLighting *= GetCurrentExposureMultiplier();
specularLighting *= GetCurrentExposureMultiplier();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,12 @@ Shader "Hidden/HDRP/DeferredTile"

PreLightData preLightData = GetPreLightData(V, posInput, bsdfData);

float3 diffuseLighting;
float3 specularLighting;
LightLoop(V, posInput, preLightData, bsdfData, builtinData, featureFlags, diffuseLighting, specularLighting);
LightLoopOutput lightLoopOutput;
LightLoop(V, posInput, preLightData, bsdfData, builtinData, featureFlags, lightLoopOutput);

// Alias
float3 diffuseLighting = lightLoopOutput.diffuseLighting;
float3 specularLighting = lightLoopOutput.specularLighting;

diffuseLighting *= GetCurrentExposureMultiplier();
specularLighting *= GetCurrentExposureMultiplier();
Expand Down Expand Up @@ -405,9 +408,12 @@ Shader "Hidden/HDRP/DeferredTile"

PreLightData preLightData = GetPreLightData(V, posInput, bsdfData);

float3 diffuseLighting;
float3 specularLighting;
LightLoop(V, posInput, preLightData, bsdfData, builtinData, LIGHT_FEATURE_MASK_FLAGS_OPAQUE, diffuseLighting, specularLighting);
LightLoopOutput lightLoopOutput;
LightLoop(V, posInput, preLightData, bsdfData, builtinData, LIGHT_FEATURE_MASK_FLAGS_OPAQUE, lightLoopOutput);

// Alias
float3 diffuseLighting = lightLoopOutput.diffuseLighting;
float3 specularLighting = lightLoopOutput.specularLighting;

diffuseLighting *= GetCurrentExposureMultiplier();
specularLighting *= GetCurrentExposureMultiplier();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,23 @@ void ApplyDebugToLighting(LightLoopContext context, inout BuiltinData builtinDat
#endif
}

void ApplyDebug(LightLoopContext context, PositionInputs posInput, BSDFData bsdfData, inout float3 diffuseLighting, inout float3 specularLighting)
void ApplyDebug(LightLoopContext context, PositionInputs posInput, BSDFData bsdfData, inout LightLoopOutput lightLoopOutput)
{
#ifdef DEBUG_DISPLAY
if (_DebugLightingMode == DEBUGLIGHTINGMODE_PROBE_VOLUME)
{
// Debug info is written to diffuseColor inside of light loop.
specularLighting = float3(0.0, 0.0, 0.0);
lightLoopOutput.specularLighting = float3(0.0, 0.0, 0.0);
}
else if (_DebugLightingMode == DEBUGLIGHTINGMODE_LUX_METER)
{
specularLighting = float3(0.0, 0.0, 0.0); // Disable specular lighting
lightLoopOutput.specularLighting = float3(0.0, 0.0, 0.0); // Disable specular lighting
// Take the luminance
diffuseLighting = Luminance(diffuseLighting).xxx;
lightLoopOutput.diffuseLighting = Luminance(lightLoopOutput.diffuseLighting).xxx;
}
else if (_DebugLightingMode == DEBUGLIGHTINGMODE_VISUALIZE_CASCADE)
{
specularLighting = float3(0.0, 0.0, 0.0);
lightLoopOutput.specularLighting = float3(0.0, 0.0, 0.0);

const float3 s_CascadeColors[] = {
float3(0.5, 0.5, 0.7),
Expand All @@ -101,7 +101,7 @@ void ApplyDebug(LightLoopContext context, PositionInputs posInput, BSDFData bsdf
float3(1.0, 1.0, 1.0)
};

diffuseLighting = Luminance(diffuseLighting);
lightLoopOutput.diffuseLighting = Luminance(lightLoopOutput.diffuseLighting);
if (_DirectionalShadowIndex >= 0)
{
real alpha;
Expand Down Expand Up @@ -133,14 +133,14 @@ void ApplyDebug(LightLoopContext context, PositionInputs posInput, BSDFData bsdf
float3 cascadeShadowColor = lerp(s_CascadeColors[shadowSplitIndex], s_CascadeColors[shadowSplitIndex + 1], alpha);
// We can't mix with the lighting as it can be HDR and it is hard to find a good lerp operation for this case that is still compliant with
// exposure. So disable exposure instead and replace color.
diffuseLighting = cascadeShadowColor * Luminance(diffuseLighting) * shadow;
lightLoopOutput.diffuseLighting = cascadeShadowColor * Luminance(lightLoopOutput.diffuseLighting) * shadow;
}

}
}
else if (_DebugLightingMode == DEBUGLIGHTINGMODE_MATCAP_VIEW)
{
specularLighting = float3(0.0, 0.0, 0.0);
lightLoopOutput.specularLighting = float3(0.0, 0.0, 0.0);
float3 normalVS = mul((float3x3)UNITY_MATRIX_V, bsdfData.normalWS).xyz;

float3 V = GetWorldSpaceNormalizeViewDir(posInput.positionWS);
Expand All @@ -155,15 +155,17 @@ void ApplyDebug(LightLoopContext context, PositionInputs posInput, BSDFData bsdf
UV = saturate(R.xy * 0.5f + 0.5f);
}

diffuseLighting = SAMPLE_TEXTURE2D_LOD(_DebugMatCapTexture, s_linear_repeat_sampler, UV, 0).rgb * (_MatcapMixAlbedo > 0 ? defaultColor.rgb * _MatcapViewScale : 1.0f);
lightLoopOutput.diffuseLighting = SAMPLE_TEXTURE2D_LOD(_DebugMatCapTexture, s_linear_repeat_sampler, UV, 0).rgb * (_MatcapMixAlbedo > 0 ? defaultColor.rgb * _MatcapViewScale : 1.0f);
}
#endif
}

void LightLoop( float3 V, PositionInputs posInput, PreLightData preLightData, BSDFData bsdfData, BuiltinData builtinData, uint featureFlags,
out float3 diffuseLighting,
out float3 specularLighting)
out LightLoopOutput lightLoopOutput)
{
// Init LightLoop output structure
ZERO_INITIALIZE(LightLoopOutput, lightLoopOutput);

LightLoopContext context;

context.shadowContext = InitShadowContext();
Expand Down Expand Up @@ -541,8 +543,7 @@ void LightLoop( float3 V, PositionInputs posInput, PreLightData preLightData, BS

// Also Apply indiret diffuse (GI)
// PostEvaluateBSDF will perform any operation wanted by the material and sum everything into diffuseLighting and specularLighting
PostEvaluateBSDF( context, V, posInput, preLightData, bsdfData, builtinData, aggregateLighting,
diffuseLighting, specularLighting);
PostEvaluateBSDF( context, V, posInput, preLightData, bsdfData, builtinData, aggregateLighting, lightLoopOutput);

ApplyDebug(context, posInput, bsdfData, diffuseLighting, specularLighting);
ApplyDebug(context, posInput, bsdfData, lightLoopOutput);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ struct LightLoopContext
DirectionalShadowType shadowValue; // Stores the value of the cascade shadow map
};

// LightLoopOutput is the output of the LightLoop fuction call.
// It allow to retrieve the data output by the LightLoop
struct LightLoopOutput
{
float3 diffuseLighting;
float3 specularLighting;
};

//-----------------------------------------------------------------------------
// Reflection probe / Sky sampling function
// ----------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2584,7 +2584,7 @@ IndirectLighting EvaluateBSDF_Env( LightLoopContext lightLoopContext,
void PostEvaluateBSDF( LightLoopContext lightLoopContext,
float3 V, PositionInputs posInput,
PreLightData preLightData, BSDFData bsdfData, BuiltinData builtinData, AggregateLighting lighting,
out float3 diffuseLighting, out float3 specularLighting)
out LightLoopOutput lightLoopOutput)
{
// There is no AmbientOcclusion from data with AxF, but let's apply our SSAO
AmbientOcclusionFactor aoFactor;
Expand All @@ -2595,16 +2595,16 @@ void PostEvaluateBSDF( LightLoopContext lightLoopContext,

ApplyAmbientOcclusionFactor(aoFactor, builtinData, lighting);

diffuseLighting = bsdfData.diffuseColor * lighting.direct.diffuse + builtinData.bakeDiffuseLighting;
specularLighting = lighting.direct.specular + lighting.indirect.specularReflected;
lightLoopOutput.diffuseLighting = bsdfData.diffuseColor * lighting.direct.diffuse + builtinData.bakeDiffuseLighting;
lightLoopOutput.specularLighting = lighting.direct.specular + lighting.indirect.specularReflected;

#if !defined(_AXF_BRDF_TYPE_SVBRDF) && !defined(_AXF_BRDF_TYPE_CAR_PAINT)
// Not supported: Display a flashy color instead
diffuseLighting = 10 * float3(1, 0.3, 0.01);
lightLoopOutput.diffuseLighting = 10 * float3(1, 0.3, 0.01);
#endif

#ifdef DEBUG_DISPLAY
PostEvaluateBSDFDebugDisplay(aoFactor, builtinData, lighting, bsdfData.diffuseColor, diffuseLighting, specularLighting);
PostEvaluateBSDFDebugDisplay(aoFactor, builtinData, lighting, bsdfData.diffuseColor, lightLoopOutput);
#endif
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ IndirectLighting EvaluateBSDF_Env( LightLoopContext lightLoopContext,
void PostEvaluateBSDF( LightLoopContext lightLoopContext,
float3 V, PositionInputs posInput,
PreLightData preLightData, BSDFData bsdfData, BuiltinData builtinData, AggregateLighting lighting,
out float3 diffuseLighting, out float3 specularLighting)
out LightLoopOutput lightLoopOutput)
{
AmbientOcclusionFactor aoFactor;
GetScreenSpaceAmbientOcclusionMultibounce(posInput.positionSS, preLightData.NdotV, bsdfData.perceptualRoughness, bsdfData.ambientOcclusion, bsdfData.specularOcclusion, bsdfData.diffuseColor, bsdfData.fresnel0, aoFactor);
Expand All @@ -831,11 +831,11 @@ void PostEvaluateBSDF( LightLoopContext lightLoopContext,

// Apply the albedo to the direct diffuse lighting (only once). The indirect (baked)
// diffuse lighting has already multiply the albedo in ModifyBakedDiffuseLighting().
diffuseLighting = modifiedDiffuseColor * lighting.direct.diffuse + builtinData.bakeDiffuseLighting + builtinData.emissiveColor;
specularLighting = lighting.direct.specular + lighting.indirect.specularReflected;
lightLoopOutput.diffuseLighting = modifiedDiffuseColor * lighting.direct.diffuse + builtinData.bakeDiffuseLighting + builtinData.emissiveColor;
lightLoopOutput.specularLighting = lighting.direct.specular + lighting.indirect.specularReflected;

#ifdef DEBUG_DISPLAY
PostEvaluateBSDFDebugDisplay(aoFactor, builtinData, lighting, bsdfData.diffuseColor, diffuseLighting, specularLighting);
PostEvaluateBSDFDebugDisplay(aoFactor, builtinData, lighting, bsdfData.diffuseColor, lightLoopOutput);
#endif
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ IndirectLighting EvaluateBSDF_Env( LightLoopContext lightLoopContext,
void PostEvaluateBSDF( LightLoopContext lightLoopContext,
float3 V, PositionInputs posInput,
PreLightData preLightData, BSDFData bsdfData, BuiltinData builtinData, AggregateLighting lighting,
out float3 diffuseLighting, out float3 specularLighting)
out LightLoopOutput lightLoopOutput)
{
AmbientOcclusionFactor aoFactor;
GetScreenSpaceAmbientOcclusionMultibounce(posInput.positionSS, preLightData.NdotV, bsdfData.perceptualRoughness, bsdfData.ambientOcclusion, bsdfData.specularOcclusion, bsdfData.diffuseColor, bsdfData.fresnel0, aoFactor);
Expand All @@ -662,13 +662,13 @@ void PostEvaluateBSDF( LightLoopContext lightLoopContext,

// Apply the albedo to the direct diffuse lighting (only once). The indirect (baked)
// diffuse lighting has already multiply the albedo in ModifyBakedDiffuseLighting().
diffuseLighting = modifiedDiffuseColor * lighting.direct.diffuse + builtinData.bakeDiffuseLighting + builtinData.emissiveColor;
specularLighting = lighting.direct.specular + lighting.indirect.specularReflected;
lightLoopOutput.diffuseLighting = modifiedDiffuseColor * lighting.direct.diffuse + builtinData.bakeDiffuseLighting + builtinData.emissiveColor;
lightLoopOutput.specularLighting = lighting.direct.specular + lighting.indirect.specularReflected;

// TODO: Multiscattering for cloth?

#ifdef DEBUG_DISPLAY
PostEvaluateBSDFDebugDisplay(aoFactor, builtinData, lighting, bsdfData.diffuseColor, diffuseLighting, specularLighting);
PostEvaluateBSDFDebugDisplay(aoFactor, builtinData, lighting, bsdfData.diffuseColor, lightLoopOutput);
#endif
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,19 +593,19 @@ IndirectLighting EvaluateBSDF_Env( LightLoopContext lightLoopContext,
void PostEvaluateBSDF( LightLoopContext lightLoopContext,
float3 V, PositionInputs posInput,
PreLightData preLightData, BSDFData bsdfData, BuiltinData builtinData, AggregateLighting lighting,
out float3 diffuseLighting, out float3 specularLighting)
out LightLoopOutput lightLoopOutput))
{
AmbientOcclusionFactor aoFactor;
GetScreenSpaceAmbientOcclusionMultibounce(posInput.positionSS, preLightData.NdotV, bsdfData.perceptualRoughness, bsdfData.ambientOcclusion, bsdfData.specularOcclusion, bsdfData.diffuseColor, bsdfData.fresnel0, aoFactor);
ApplyAmbientOcclusionFactor(aoFactor, builtinData, lighting);

// Apply the albedo to the direct diffuse lighting (only once). The indirect (baked)
// diffuse lighting has already multiply the albedo in ModifyBakedDiffuseLighting().
diffuseLighting = bsdfData.diffuseColor * lighting.direct.diffuse + builtinData.bakeDiffuseLighting + builtinData.emissiveColor;
specularLighting = lighting.direct.specular + lighting.indirect.specularReflected;
lightLoopOutput.diffuseLighting = bsdfData.diffuseColor * lighting.direct.diffuse + builtinData.bakeDiffuseLighting + builtinData.emissiveColor;
lightLoopOutput.specularLighting = lighting.direct.specular + lighting.indirect.specularReflected;

#ifdef DEBUG_DISPLAY
PostEvaluateBSDFDebugDisplay(aoFactor, builtinData, lighting, bsdfData.diffuseColor, diffuseLighting, specularLighting);
PostEvaluateBSDFDebugDisplay(aoFactor, builtinData, lighting, bsdfData.diffuseColor, lightLoopOutput);
#endif
}

Expand Down
Loading