Skip to content

Cleanup area shadow code and Macro + remove more Raytracing macro #945

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 8 commits into from
Jun 17, 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 @@ -31,6 +31,7 @@ public enum ProbeVolumesBilateralFilteringModes
[GenerateHLSL(PackingRules.Exact)]
public enum ShaderOptions
{
ColoredShadow = 1, // Allow to defined if colored shadow are supported in shaders or not
CameraRelativeRendering = 1, // Rendering sets the origin of the world to the position of the primary (scene view) camera
PreExposition = 1,
PrecomputedAtmosphericAttenuation = 0, // Precomputes atmospheric attenuation for the directional light on the CPU, which makes it independent from the fragment's position, which is faster but wrong
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@

#ifndef SHADERCONFIG_CS_HLSL
#define SHADERCONFIG_CS_HLSL
//
// UnityEngine.Rendering.HighDefinition.HDShadowFilteringQuality: static fields
//
#define HDSHADOWFILTERINGQUALITY_LOW (0)
#define HDSHADOWFILTERINGQUALITY_MEDIUM (1)
#define HDSHADOWFILTERINGQUALITY_HIGH (2)

//
// UnityEngine.Rendering.HighDefinition.ProbeVolumesEvaluationModes: static fields
//
Expand All @@ -35,6 +28,7 @@
//
// UnityEngine.Rendering.HighDefinition.ShaderOptions: static fields
//
#define SHADEROPTIONS_COLORED_SHADOW (1)
#define SHADEROPTIONS_CAMERA_RELATIVE_RENDERING (1)
#define SHADEROPTIONS_PRE_EXPOSITION (1)
#define SHADEROPTIONS_PRECOMPUTED_ATMOSPHERIC_ATTENUATION (0)
Expand All @@ -45,7 +39,6 @@
#define SHADEROPTIONS_PROBE_VOLUMES_BILATERAL_FILTERING_MODE (1)
#define SHADEROPTIONS_PROBE_VOLUMES_ENCODING_MODE (1)
#define SHADEROPTIONS_AREA_LIGHTS (1)
#define SHADEROPTIONS_DEFERRED_SHADOW_FILTERING (1)
#define SHADEROPTIONS_BARN_DOOR (0)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public enum ProbeVolumesBilateralFilteringModes
[GenerateHLSL(PackingRules.Exact)]
public enum ShaderOptions
{
ColoredShadow = 1, // Allow to defined if colored shadow are supported in shaders or not
CameraRelativeRendering = 1, // Rendering sets the origin of the world to the position of the primary (scene view) camera
PreExposition = 1,
PrecomputedAtmosphericAttenuation = 0, // Precomputes atmospheric attenuation for the directional light on the CPU, which makes it independent from the fragment's position, which is faster but wrong
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@

#ifndef SHADERCONFIG_CS_HLSL
#define SHADERCONFIG_CS_HLSL
//
// UnityEngine.Rendering.HighDefinition.HDShadowFilteringQuality: static fields
//
#define HDSHADOWFILTERINGQUALITY_LOW (0)
#define HDSHADOWFILTERINGQUALITY_MEDIUM (1)
#define HDSHADOWFILTERINGQUALITY_HIGH (2)

//
// UnityEngine.Rendering.HighDefinition.ProbeVolumesEvaluationModes: static fields
//
Expand All @@ -35,6 +28,7 @@
//
// UnityEngine.Rendering.HighDefinition.ShaderOptions: static fields
//
#define SHADEROPTIONS_COLORED_SHADOW (1)
#define SHADEROPTIONS_CAMERA_RELATIVE_RENDERING (1)
#define SHADEROPTIONS_PRE_EXPOSITION (1)
#define SHADEROPTIONS_PRECOMPUTED_ATMOSPHERIC_ATTENUATION (0)
Expand All @@ -45,7 +39,6 @@
#define SHADEROPTIONS_PROBE_VOLUMES_BILATERAL_FILTERING_MODE (1)
#define SHADEROPTIONS_PROBE_VOLUMES_ENCODING_MODE (1)
#define SHADEROPTIONS_AREA_LIGHTS (1)
#define SHADEROPTIONS_DEFERRED_SHADOW_FILTERING (1)
#define SHADEROPTIONS_BARN_DOOR (0)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ The shader function `SampleShadow_PCSS` now requires you to pass in an additiona

From Unity 2020.2, due to the change of shadow map, the enum HDShadowFilteringQuality have been moved to HDShadowManager.cs and the variables ShaderConfig.s_DeferredShadowFiltering as well as the option ShaderOptions.DeferredShadowFiltering have been removed from the code as they have no impact anymore.

From Unity 2020.2, a new option is available name ColoredShadow. It allow to control if the shadow will be chromatic or monochrome. ColoredShadow have a performance cost. ColoredShadow are the default and currently only work with Raytrace shadow.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From Unity 2020.2, a new option is available named ColoredShadow. It allows you to control whether a shadow is chromatic or monochrome. ColoredShadow is enabled by default and currently only works with Ray-traced shadows. Note that colored shadows are more resource-intensive to process than standard shadows.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks


## Shader code

Expand Down Expand Up @@ -72,6 +73,9 @@ BSDFData bsdfData = ConvertSurfaceDataToBSDFData(posInput.positionSS, surfaceDat
PreLightData preLightData = GetPreLightData(V, posInput, bsdfData);
```


From Unity 2020.2, a new rectangular area shadow have been introduce EvaluateShadow_RectArea and the function GetAreaLightAttenuation() have been rename to GetRectAreaShadowAttenuation(). Also the type DirectionalShadowType have been renamed SHADOW_TYPE.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From Unity 2020.2, HDRP includes a new rectangular area shadow, EvaluateShadow_RectArea. The GetAreaLightAttenuation() function has been renamed to GetRectAreaShadowAttenuation(). Also the type DirectionalShadowType have been renamed SHADOW_TYPE.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks


## Custom pass API

The signature of the Execute function has changed to simplify the parameters, now it only takes a CustomPassContext as its input:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


// Local shader variables
static DirectionalShadowType g_DebugShadowAttenuation = 0;
static SHADOW_TYPE g_DebugShadowAttenuation = 0;

StructuredBuffer<int2> _DebugDepthPyramidOffsets;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,13 @@ float4 EvaluateLight_Directional(LightLoopContext lightLoopContext, PositionInpu
return color;
}

DirectionalShadowType EvaluateShadow_Directional(LightLoopContext lightLoopContext, PositionInputs posInput,
DirectionalLightData light, BuiltinData builtinData, float3 N)
SHADOW_TYPE EvaluateShadow_Directional( LightLoopContext lightLoopContext, PositionInputs posInput,
DirectionalLightData light, BuiltinData builtinData, float3 N)
{
#ifndef LIGHT_EVALUATION_NO_SHADOWS
DirectionalShadowType shadow = 1.0;
float shadowMask = 1.0;
float NdotL = dot(N, -light.forward); // Disable contact shadow and shadow mask when facing away from light (i.e transmission)
SHADOW_TYPE shadow = 1.0;
float shadowMask = 1.0;
float NdotL = dot(N, -light.forward); // Disable contact shadow and shadow mask when facing away from light (i.e transmission)

#ifdef SHADOWS_SHADOWMASK
// shadowMaskSelector.x is -1 if there is no shadow mask
Expand Down Expand Up @@ -417,14 +417,13 @@ float4 EvaluateLight_Punctual(LightLoopContext lightLoopContext, PositionInputs
}

// distances = {d, d^2, 1/d, d_proj}, where d_proj = dot(lightToSample, light.forward).
float EvaluateShadow_Punctual(LightLoopContext lightLoopContext, PositionInputs posInput,
LightData light, BuiltinData builtinData, float3 N, float3 L, float4 distances)
SHADOW_TYPE EvaluateShadow_Punctual(LightLoopContext lightLoopContext, PositionInputs posInput,
LightData light, BuiltinData builtinData, float3 N, float3 L, float4 distances)
{
#ifndef LIGHT_EVALUATION_NO_SHADOWS
float shadow = 1.0;
float shadowMask = 1.0;
float NdotL = dot(N, L); // Disable contact shadow and shadow mask when facing away from light (i.e transmission)

SHADOW_TYPE shadow = 1.0;
float shadowMask = 1.0;
float NdotL = dot(N, L); // Disable contact shadow and shadow mask when facing away from light (i.e transmission)

#ifdef SHADOWS_SHADOWMASK
// shadowMaskSelector.x is -1 if there is no shadow mask
Expand Down Expand Up @@ -472,6 +471,49 @@ float EvaluateShadow_Punctual(LightLoopContext lightLoopContext, PositionInputs
#endif
}


SHADOW_TYPE EvaluateShadow_RectArea( LightLoopContext lightLoopContext, PositionInputs posInput,
LightData light, BuiltinData builtinData, float3 N, float3 L, float dist)
{
#ifndef LIGHT_EVALUATION_NO_SHADOWS
SHADOW_TYPE shadow = 1.0;
float shadowMask = 1.0;
float NdotL = dot(N, L); // Disable contact shadow and shadow mask when facing away from light (i.e transmission)

#ifdef SHADOWS_SHADOWMASK
// shadowMaskSelector.x is -1 if there is no shadow mask
// Note that we override shadow value (in case we don't have any dynamic shadow)
shadow = shadowMask = (light.shadowMaskSelector.x >= 0.0 && NdotL > 0.0) ? dot(BUILTIN_DATA_SHADOW_MASK, light.shadowMaskSelector) : 1.0;
#endif

#if defined(SCREEN_SPACE_SHADOWS) && !defined(_SURFACE_TYPE_TRANSPARENT) && (SHADERPASS != SHADERPASS_VOLUMETRIC_LIGHTING)
if ((light.screenSpaceShadowIndex & SCREEN_SPACE_SHADOW_INDEX_MASK) != INVALID_SCREEN_SPACE_SHADOW)
{
shadow = GetScreenSpaceShadow(posInput, light.screenSpaceShadowIndex);
}
else
#endif
if ((light.shadowIndex >= 0) && (light.shadowDimmer > 0))
{
shadow = GetRectAreaShadowAttenuation(lightLoopContext.shadowContext, posInput.positionSS, posInput.positionWS, N, light.shadowIndex, L, dist);

#ifdef SHADOWS_SHADOWMASK
// See comment for punctual light shadow mask
shadow = light.nonLightMappedOnly ? min(shadowMask, shadow) : shadow;
#endif
shadow = lerp(shadowMask, shadow, light.shadowDimmer);
}

#ifdef DEBUG_DISPLAY
if (_DebugShadowMapMode == SHADOWMAPDEBUGMODE_SINGLE_SHADOW && light.shadowIndex == _DebugSingleShadowIndex)
g_DebugShadowAttenuation = shadow;
#endif
return shadow;
#else // LIGHT_EVALUATION_NO_SHADOWS
return 1.0;
#endif
}

//-----------------------------------------------------------------------------
// Reflection probe evaluation helper
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -526,20 +568,17 @@ void EvaluateLight_EnvIntersection(float3 positionWS, float3 normalWS, EnvLightD

void InversePreExposeSsrLighting(inout float4 ssrLighting)
{
float prevExposureInvMultiplier = GetInversePreviousExposureMultiplier();

#if SHADEROPTIONS_RAYTRACING
if (!_UseRayTracedReflections)
#endif
ssrLighting.rgb *= prevExposureInvMultiplier;
// Raytrace reflection use the current frame exposure - TODO: currently the buffer don't use pre-exposure.
// Screen space reflection reuse color buffer from previous frame
float exposureMultiplier = _EnableRayTracedReflections ? 1.0 : GetInversePreviousExposureMultiplier();
ssrLighting.rgb *= exposureMultiplier;
}

void ApplyScreenSpaceReflectionWeight(inout float4 ssrLighting)
{
// Note: RGB is already premultiplied by A for SSR
#if SHADEROPTIONS_RAYTRACING
if (_UseRayTracedReflections)
ssrLighting.rgb *= ssrLighting.a;
#endif
// TODO: check why it isn't consistent between SSR and RTR
float weight = _EnableRayTracedReflections ? 1.0 : ssrLighting.a;
ssrLighting.rgb *= ssrLighting.a;
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ float GetPunctualShadowClosestDistance(HDShadowContext shadowContext, SamplerSta
}
}

float GetAreaLightAttenuation(HDShadowContext shadowContext, float2 positionSS, float3 positionWS, float3 normalWS, int shadowDataIndex, float3 L, float L_dist)
float GetRectAreaShadowAttenuation(HDShadowContext shadowContext, float2 positionSS, float3 positionWS, float3 normalWS, int shadowDataIndex, float3 L, float L_dist)
{
#if (defined(PLATFORM_SUPPORTS_WAVE_INTRINSICS) && !defined(LIGHTLOOP_DISABLE_TILE_AND_CLUSTER))
shadowDataIndex = WaveReadLaneFirst(shadowDataIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void ShadowLoopMin(HDShadowContext shadowContext, PositionInputs posInput, float

if (distances.x < lightData.range && coef > 0.0)
{
float shadowA = GetAreaLightAttenuation(shadowContext, posInput.positionSS, posInput.positionWS, normalWS, lightData.shadowIndex, normalize(lightData.positionRWS), length(lightData.positionRWS));
float shadowA = GetRectAreaShadowAttenuation(shadowContext, posInput.positionSS, posInput.positionWS, normalWS, lightData.shadowIndex, normalize(lightData.positionRWS), length(lightData.positionRWS));

#ifdef SHADOW_LOOP_MULTIPLY
shadow *= lerp(lightData.shadowTint, float3(1, 1, 1), shadowA);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,6 @@ class LightDefinitions
public static uint s_ScreenSpaceColorShadowFlag = 0x100;
public static uint s_InvalidScreenSpaceShadow = 0xff;
public static uint s_ScreenSpaceShadowIndexMask = 0xff;

// Indirect diffuse flags
public static int k_IndirectDiffuseFlagOff = 0x00;
public static int k_ScreenSpaceIndirectDiffuseFlag = 0x01;
public static int k_RayTracedIndirectDiffuseFlag = 0x02;
}

[GenerateHLSL]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@
#define SCREEN_SPACE_COLOR_SHADOW_FLAG (256)
#define INVALID_SCREEN_SPACE_SHADOW (255)
#define SCREEN_SPACE_SHADOW_INDEX_MASK (255)
#define INDIRECT_DIFFUSE_FLAG_OFF (0)
#define SCREEN_SPACE_INDIRECT_DIFFUSE_FLAG (1)
#define RAY_TRACED_INDIRECT_DIFFUSE_FLAG (2)

// Generated from UnityEngine.Rendering.HighDefinition.SFiniteLightBound
// PackingRules = Exact
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#if SHADEROPTIONS_PROBE_VOLUMES_EVALUATION_MODE == PROBEVOLUMESEVALUATIONMODES_LIGHT_LOOP
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/BuiltinUtilities.hlsl"
#else
// Required to have access to the indirectDiffuseMode enum in forward pass where we don't include BuiltinUtilities
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/ScreenSpaceLighting/ScreenSpaceGlobalIllumination.cs.hlsl"
#endif

// We perform scalarization only for forward rendering as for deferred loads will already be scalar since tiles will match waves and therefore all threads will read from the same tile.
Expand Down Expand Up @@ -110,7 +113,7 @@ void ApplyDebug(LightLoopContext context, PositionInputs posInput, BSDFData bsdf
int shadowSplitIndex = EvalShadow_GetSplitIndex(context.shadowContext, _DirectionalShadowIndex, posInput.positionWS, alpha, cascadeCount);
if (shadowSplitIndex >= 0)
{
DirectionalShadowType shadow = 1.0;
SHADOW_TYPE shadow = 1.0;
if (_DirectionalShadowIndex >= 0)
{
DirectionalLightData light = _DirectionalLightDatas[_DirectionalShadowIndex];
Expand Down Expand Up @@ -190,7 +193,7 @@ void LightLoop( float3 V, PositionInputs posInput, PreLightData preLightData, BS
#if defined(SCREEN_SPACE_SHADOWS) && !defined(_SURFACE_TYPE_TRANSPARENT)
if ((light.screenSpaceShadowIndex & SCREEN_SPACE_SHADOW_INDEX_MASK) != INVALID_SCREEN_SPACE_SHADOW)
{
context.shadowValue = GetScreenSpaceColorShadow(posInput, light.screenSpaceShadowIndex);
context.shadowValue = GetScreenSpaceColorShadow(posInput, light.screenSpaceShadowIndex).SHADOW_TYPE_SWIZZLE;
}
else
#endif
Expand Down Expand Up @@ -534,15 +537,23 @@ void LightLoop( float3 V, PositionInputs posInput, PreLightData preLightData, BS

#if !defined(_SURFACE_TYPE_TRANSPARENT)
// If we use the texture ssgi for ssgi or rtgi, we want to combine it with the value in the bake diffuse lighting value
if (_UseIndirectDiffuse != INDIRECT_DIFFUSE_FLAG_OFF)
if (_IndirectDiffuseMode != INDIRECTDIFFUSEMODE_OFF)
{
BuiltinData builtInDataSSGI;
ZERO_INITIALIZE(BuiltinData, builtInDataSSGI);
builtInDataSSGI.bakeDiffuseLighting = LOAD_TEXTURE2D_X(_IndirectDiffuseTexture, posInput.positionSS).xyz * GetInverseCurrentExposureMultiplier();
float indirectDiffuseMultiplier = GetIndirectDiffuseMultiplier(builtinData.renderingLayers);
builtInDataSSGI.bakeDiffuseLighting *= indirectDiffuseMultiplier;
ModifyBakedDiffuseLighting(V, posInput, preLightData, bsdfData, builtInDataSSGI);
builtinData.bakeDiffuseLighting += builtInDataSSGI.bakeDiffuseLighting;
BuiltinData builtinDataSSGI;
ZERO_INITIALIZE(BuiltinData, builtinDataSSGI);
builtinDataSSGI.bakeDiffuseLighting = LOAD_TEXTURE2D_X(_IndirectDiffuseTexture, posInput.positionSS).xyz * GetInverseCurrentExposureMultiplier();
builtinDataSSGI.bakeDiffuseLighting *= GetIndirectDiffuseMultiplier(builtinData.renderingLayers);

// TODO: try to see if we can share code with probe volume
#ifdef MODIFY_BAKED_DIFFUSE_LIGHTING
#ifdef DEBUG_DISPLAY
// When the lux meter is enabled, we don't want the albedo of the material to modify the diffuse baked lighting
if (_DebugLightingMode != DEBUGLIGHTINGMODE_LUX_METER)
#endif
ModifyBakedDiffuseLighting(V, posInput, preLightData, bsdfData, builtinDataSSGI);

#endif
builtinData.bakeDiffuseLighting += builtinDataSSGI.bakeDiffuseLighting;
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ struct LightLoopContext
HDShadowContext shadowContext;

uint contactShadow; // a bit mask of 24 bits that tell if the pixel is in a contact shadow or not
real contactShadowFade; // combined fade factor of all contact shadows
DirectionalShadowType shadowValue; // Stores the value of the cascade shadow map
real contactShadowFade; // combined fade factor of all contact shadows
SHADOW_TYPE shadowValue; // Stores the value of the cascade shadow map
};

// LightLoopOutput is the output of the LightLoop fuction call.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

namespace UnityEngine.Rendering.HighDefinition
{
[GenerateHLSL]
// Define if we use SSGI, RTGI or none
enum IndirectDiffuseMode
{
Off,
ScreenSpace,
Raytrace
}

public partial class HDRenderPipeline
{
// Buffers used for the evaluation
Expand Down Expand Up @@ -52,13 +61,21 @@ void ReleaseScreenSpaceGlobalIllumination()
}

// This is shared between SSGI and RTGI
bool ValidIndirectDiffuseState(HDCamera hdCamera)
IndirectDiffuseMode GetIndirectDiffuseMode(HDCamera hdCamera)
{
var settings = hdCamera.volumeStack.GetComponent<GlobalIllumination>();
return m_Asset.currentPlatformRenderPipelineSettings.supportSSGI
&& hdCamera.camera.cameraType != CameraType.Reflection
&& hdCamera.frameSettings.IsEnabled(FrameSettingsField.SSGI)
&& settings.enable.value;
IndirectDiffuseMode mode = IndirectDiffuseMode.Off;

if (hdCamera.frameSettings.IsEnabled(FrameSettingsField.SSGI))
{
var settings = hdCamera.volumeStack.GetComponent<GlobalIllumination>();
if (settings.enable.value)
{
// RTGI is only valid if raytracing is enabled
bool raytracing = hdCamera.frameSettings.IsEnabled(FrameSettingsField.RayTracing) && settings.rayTracing.value;
mode = raytracing ? IndirectDiffuseMode.Raytrace : IndirectDiffuseMode.ScreenSpace;
}
}
return mode;
}

// Bind the indirect diffuse texture for the lightloop to read from it
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// This file was automatically generated. Please don't edit by hand.
//

#ifndef SCREENSPACEGLOBALILLUMINATION_CS_HLSL
#define SCREENSPACEGLOBALILLUMINATION_CS_HLSL
//
// UnityEngine.Rendering.HighDefinition.IndirectDiffuseMode: static fields
//
#define INDIRECTDIFFUSEMODE_OFF (0)
#define INDIRECTDIFFUSEMODE_SCREEN_SPACE (1)
#define INDIRECTDIFFUSEMODE_RAYTRACE (2)


#endif
Loading