From c006e6dc6d7f32b956fa079897cb310bdc850e97 Mon Sep 17 00:00:00 2001 From: sebastienlagarde Date: Wed, 17 Jun 2020 02:28:34 +0200 Subject: [PATCH 1/8] Change code to have a indirectDiffuseMode and start to clean RaytracingMacro --- .../Runtime/ShaderConfig.cs.hlsl | 8 --- .../Runtime/Lighting/LightEvaluation.hlsl | 17 +++--- .../Runtime/Lighting/LightLoop/LightLoop.cs | 5 -- .../Lighting/LightLoop/LightLoop.cs.hlsl | 3 -- .../Runtime/Lighting/LightLoop/LightLoop.hlsl | 27 +++++++--- .../ScreenSpaceGlobalIllumination.cs | 29 +++++++--- .../ScreenSpaceGlobalIllumination.cs.hlsl | 15 ++++++ ...ScreenSpaceGlobalIllumination.cs.hlsl.meta | 10 ++++ .../Runtime/Material/BuiltinGIUtilities.hlsl | 28 ++++++---- .../Runtime/Material/BuiltinUtilities.hlsl | 10 ---- .../SubsurfaceScatteringManager.cs | 2 +- .../HDRenderPipeline.RenderGraph.cs | 1 + .../RenderPipeline/HDRenderPipeline.cs | 53 +++++++------------ .../Raytracing/HDRaytracingIndirectDiffuse.cs | 12 ----- .../RenderPipeline/Settings/FrameSettings.cs | 3 +- .../ShaderLibrary/ShaderVariablesGlobal.cs | 4 +- .../ShaderVariablesGlobal.cs.hlsl | 4 +- 17 files changed, 118 insertions(+), 113 deletions(-) create mode 100644 com.unity.render-pipelines.high-definition/Runtime/Lighting/ScreenSpaceLighting/ScreenSpaceGlobalIllumination.cs.hlsl create mode 100644 com.unity.render-pipelines.high-definition/Runtime/Lighting/ScreenSpaceLighting/ScreenSpaceGlobalIllumination.cs.hlsl.meta diff --git a/TestProjects/HDRP_DXR_Tests/LocalPackages/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs.hlsl b/TestProjects/HDRP_DXR_Tests/LocalPackages/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs.hlsl index a593822cbef..6b354cd380c 100644 --- a/TestProjects/HDRP_DXR_Tests/LocalPackages/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs.hlsl +++ b/TestProjects/HDRP_DXR_Tests/LocalPackages/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs.hlsl @@ -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 // @@ -45,7 +38,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) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightEvaluation.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightEvaluation.hlsl index 706a9e1787b..3e93f753279 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightEvaluation.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightEvaluation.hlsl @@ -526,20 +526,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 diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoop.cs b/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoop.cs index 950f7f75f05..e5cfe8f807a 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoop.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoop.cs @@ -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] diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoop.cs.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoop.cs.hlsl index 63b4a6b8c86..b13cb8c12e9 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoop.cs.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoop.cs.hlsl @@ -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 diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoop.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoop.hlsl index a182d07ec53..1ffc924088f 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoop.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoop.hlsl @@ -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. @@ -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 diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/ScreenSpaceLighting/ScreenSpaceGlobalIllumination.cs b/com.unity.render-pipelines.high-definition/Runtime/Lighting/ScreenSpaceLighting/ScreenSpaceGlobalIllumination.cs index 4d48d36f971..fa7a5956b10 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Lighting/ScreenSpaceLighting/ScreenSpaceGlobalIllumination.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/ScreenSpaceLighting/ScreenSpaceGlobalIllumination.cs @@ -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 @@ -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(); - 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(); + 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 diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/ScreenSpaceLighting/ScreenSpaceGlobalIllumination.cs.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Lighting/ScreenSpaceLighting/ScreenSpaceGlobalIllumination.cs.hlsl new file mode 100644 index 00000000000..f592dfe6892 --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/ScreenSpaceLighting/ScreenSpaceGlobalIllumination.cs.hlsl @@ -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 diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/ScreenSpaceLighting/ScreenSpaceGlobalIllumination.cs.hlsl.meta b/com.unity.render-pipelines.high-definition/Runtime/Lighting/ScreenSpaceLighting/ScreenSpaceGlobalIllumination.cs.hlsl.meta new file mode 100644 index 00000000000..08a3fdb9904 --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/ScreenSpaceLighting/ScreenSpaceGlobalIllumination.cs.hlsl.meta @@ -0,0 +1,10 @@ +fileFormatVersion: 2 +guid: 27b3afeb6fce1d4498775965a8582439 +ShaderImporter: + externalObjects: {} + defaultTextures: [] + nonModifiableTextures: [] + preprocessorOverride: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/BuiltinGIUtilities.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/BuiltinGIUtilities.hlsl index 1bba1ad2a07..cab6d66d264 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/BuiltinGIUtilities.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/BuiltinGIUtilities.hlsl @@ -1,6 +1,9 @@ #ifndef __BUILTINGIUTILITIES_HLSL__ #define __BUILTINGIUTILITIES_HLSL__ +// Include the IndirectDiffuseMode enum +#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/ScreenSpaceLighting/ScreenSpaceGlobalIllumination.cs.hlsl" + #ifdef SHADERPASS #if ((SHADEROPTIONS_PROBE_VOLUMES_EVALUATION_MODE == PROBEVOLUMESEVALUATIONMODES_MATERIAL_PASS) && (SHADERPASS == SHADERPASS_GBUFFER || SHADERPASS == SHADERPASS_FORWARD)) || \ ((SHADEROPTIONS_PROBE_VOLUMES_EVALUATION_MODE == PROBEVOLUMESEVALUATIONMODES_LIGHT_LOOP) && (SHADERPASS == SHADERPASS_DEFERRED_LIGHTING || SHADERPASS == SHADERPASS_FORWARD)) @@ -83,14 +86,8 @@ void EvaluateLightProbeBuiltin(float3 positionRWS, float3 normalWS, float3 backN } else { -#if RAYTRACING_ENABLED - if (unity_ProbeVolumeParams.w == 1.0) - SampleProbeVolumeSH9(TEXTURE3D_ARGS(unity_ProbeVolumeSH, samplerunity_ProbeVolumeSH), positionRWS, normalWS, backNormalWS, GetProbeVolumeWorldToObject(), - unity_ProbeVolumeParams.y, unity_ProbeVolumeParams.z, unity_ProbeVolumeMin.xyz, unity_ProbeVolumeSizeInv.xyz, bakeDiffuseLighting, backBakeDiffuseLighting); - else -#endif - SampleProbeVolumeSH4(TEXTURE3D_ARGS(unity_ProbeVolumeSH, samplerunity_ProbeVolumeSH), positionRWS, normalWS, backNormalWS, GetProbeVolumeWorldToObject(), - unity_ProbeVolumeParams.y, unity_ProbeVolumeParams.z, unity_ProbeVolumeMin.xyz, unity_ProbeVolumeSizeInv.xyz, bakeDiffuseLighting, backBakeDiffuseLighting); + SampleProbeVolumeSH4(TEXTURE3D_ARGS(unity_ProbeVolumeSH, samplerunity_ProbeVolumeSH), positionRWS, normalWS, backNormalWS, GetProbeVolumeWorldToObject(), + unity_ProbeVolumeParams.y, unity_ProbeVolumeParams.z, unity_ProbeVolumeMin.xyz, unity_ProbeVolumeSizeInv.xyz, bakeDiffuseLighting, backBakeDiffuseLighting); } } @@ -105,6 +102,18 @@ void SampleBakedGI( out float3 bakeDiffuseLighting, out float3 backBakeDiffuseLighting) { + bakeDiffuseLighting = float3(0, 0, 0); + backBakeDiffuseLighting = float3(0, 0, 0); + + // Check if we are RTGI in which case we don't want to read GI at all (We rely fully on the raytrace effect) + // The check need to be here to work with both regular shader and shader graph + // Note: with Probe volume it will prevent to add the UNINITIALIZED_GI tag and + // the ProbeVolume will not be evaluate in the lightloop which is the desired behavior +#if !defined(_SURFACE_TYPE_TRANSPARENT) + if (_IndirectDiffuseMode == INDIRECTDIFFUSEMODE_RAYTRACE) + return ; +#endif + float3 positionRWS = posInputs.positionWS; #define SAMPLE_LIGHTMAP (defined(LIGHTMAP_ON) || defined(DYNAMICLIGHTMAP_ON)) @@ -112,9 +121,6 @@ void SampleBakedGI( && (!SAMPLE_LIGHTMAP || SHADEROPTIONS_PROBE_VOLUMES_ADDITIVE_BLENDING) #define SAMPLE_PROBEVOLUME_BUILTIN (!SAMPLE_LIGHTMAP && !SAMPLE_PROBEVOLUME) - bakeDiffuseLighting = float3(0, 0, 0); - backBakeDiffuseLighting = float3(0, 0, 0); - #if SAMPLE_LIGHTMAP EvaluateLightmap(positionRWS, normalWS, backNormalWS, uvStaticLightmap, uvDynamicLightmap, bakeDiffuseLighting, backBakeDiffuseLighting); #endif diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/BuiltinUtilities.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/BuiltinUtilities.hlsl index 60ec640bba0..5b2bd9f44c4 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/BuiltinUtilities.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/BuiltinUtilities.hlsl @@ -46,16 +46,6 @@ void InitBuiltinData(PositionInputs posInput, float alpha, float3 normalWS, floa builtinData.backBakeDiffuseLighting = 0.0; SampleBakedGI( posInput, normalWS, backNormalWS, builtinData.renderingLayers, texCoord1.xy, texCoord2.xy, builtinData.bakeDiffuseLighting, builtinData.backBakeDiffuseLighting); - - // We only want to read the screen space buffer that holds the indirect diffuse signal if this is not a transparent surface -#if RAYTRACING_ENABLED && ((SHADERPASS == SHADERPASS_GBUFFER) || (SHADERPASS == SHADERPASS_FORWARD)) && !defined(_SURFACE_TYPE_TRANSPARENT) - if (_UseIndirectDiffuse == RAY_TRACED_INDIRECT_DIFFUSE_FLAG) - { - // Incase we shall be using raytraced indirect diffuse, we want to make sure to not add the other forms of indirect lighting to avoid - // double contribution. - builtinData.bakeDiffuseLighting = float3(0.0, 0.0, 0.0); - } -#endif #ifdef SHADOWS_SHADOWMASK float4 shadowMask = SampleShadowMask(posInput.positionWS, texCoord1.xy); diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/SubsurfaceScattering/SubsurfaceScatteringManager.cs b/com.unity.render-pipelines.high-definition/Runtime/Material/SubsurfaceScattering/SubsurfaceScatteringManager.cs index 05a93901fbd..250732fcf0e 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/SubsurfaceScattering/SubsurfaceScatteringManager.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/SubsurfaceScattering/SubsurfaceScatteringManager.cs @@ -390,7 +390,7 @@ void RenderSubsurfaceScattering(HDCamera hdCamera, CommandBuffer cmd, RTHandle c // Now based on the mask, we need to blend the subsurface and the diffuse lighting - bool validSSGI = ValidIndirectDiffuseState(hdCamera); + bool validSSGI = GetIndirectDiffuseMode(hdCamera) != IndirectDiffuseMode.Off; int m_CombineSubSurfaceKernel = rayTracingSubSurfaceCS.FindKernel(validSSGI ? "BlendSubSurfaceDataWithGI" : "BlendSubSurfaceData"); cmd.SetComputeTextureParam(rayTracingSubSurfaceCS, m_CombineSubSurfaceKernel, HDShaderIDs._SubSurfaceLightingBuffer, intermediateBuffer0); cmd.SetComputeTextureParam(rayTracingSubSurfaceCS, m_CombineSubSurfaceKernel, HDShaderIDs._DiffuseLightingTextureRW, diffuseBufferRT); diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraph.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraph.cs index fd432eb5872..a3c5b74b9f4 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraph.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraph.cs @@ -102,6 +102,7 @@ void ExecuteWithRenderGraph( RenderRequest renderRequest, // lightCluster.EvaluateClusterDebugView(cmd, hdCamera); // } + // TODO: check code, everything have change // bool validIndirectDiffuse = ValidIndirectDiffuseState(hdCamera); // if (validIndirectDiffuse) // { diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs index 58a8e2fdd1b..9f40df332eb 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs @@ -1266,21 +1266,21 @@ void UpdateShaderVariablesGlobalCB(HDCamera hdCamera, CommandBuffer cmd) m_ShaderVariablesGlobalCB._CoarseStencilBufferSize = new Vector4(coarseStencilWidth, coarseStencilHeight, 1.0f / coarseStencilWidth, 1.0f / coarseStencilHeight); m_ShaderVariablesGlobalCB._RaytracingFrameIndex = RayTracingFrameIndex(hdCamera); + m_ShaderVariablesGlobalCB._IndirectDiffuseMode = (int)GetIndirectDiffuseMode(hdCamera); + if (hdCamera.frameSettings.IsEnabled(FrameSettingsField.RayTracing)) { // Check if recursive rendering is enabled or not. This will control the cull of primitive // during the gbuffer and forward pass - RecursiveRendering recursiveSettings = hdCamera.volumeStack.GetComponent(); ScreenSpaceReflection settings = hdCamera.volumeStack.GetComponent(); - bool usesRaytracedReflections = hdCamera.frameSettings.IsEnabled(FrameSettingsField.RayTracing) && settings.rayTracing.value; - m_ShaderVariablesGlobalCB._UseRayTracedReflections = usesRaytracedReflections ? 1 : 0; - m_ShaderVariablesGlobalCB._UseIndirectDiffuse = ValidIndirectDiffuseState(hdCamera) ? (RayTracedIndirectDiffuseState(hdCamera) ? LightDefinitions.k_RayTracedIndirectDiffuseFlag : LightDefinitions.k_ScreenSpaceIndirectDiffuseFlag) : LightDefinitions.k_IndirectDiffuseFlagOff; + bool enableRaytracedReflections = hdCamera.frameSettings.IsEnabled(FrameSettingsField.RayTracing) && settings.rayTracing.value; + m_ShaderVariablesGlobalCB._EnableRayTracedReflections = enableRaytracedReflections ? 1 : 0; + RecursiveRendering recursiveSettings = hdCamera.volumeStack.GetComponent(); m_ShaderVariablesGlobalCB._EnableRecursiveRayTracing = recursiveSettings.enable.value ? 1u : 0u; } else { - m_ShaderVariablesGlobalCB._UseRayTracedReflections = 0; - m_ShaderVariablesGlobalCB._UseIndirectDiffuse = ValidIndirectDiffuseState(hdCamera) ? LightDefinitions.k_ScreenSpaceIndirectDiffuseFlag : LightDefinitions.k_IndirectDiffuseFlagOff; + m_ShaderVariablesGlobalCB._EnableRayTracedReflections = 0; m_ShaderVariablesGlobalCB._EnableRecursiveRayTracing = 0; } @@ -2603,37 +2603,22 @@ void AsyncSSAODispatch(CommandBuffer c, HDGPUAsyncTaskParams a) HDRaytracingLightCluster lightCluster = RequestLightCluster(); lightCluster.EvaluateClusterDebugView(cmd, hdCamera); } - - bool validIndirectDiffuse = ValidIndirectDiffuseState(hdCamera); - if (validIndirectDiffuse) - { - if (RayTracedIndirectDiffuseState(hdCamera)) - { - RenderRayTracedIndirectDiffuse(hdCamera, cmd, renderContext, m_FrameCount); - } - else - { - RenderSSGI(hdCamera, cmd, renderContext, m_FrameCount); - BindIndirectDiffuseTexture(cmd); - } - } - else - { - BindBlackIndirectDiffuseTexture(cmd); - } } - else + + switch (GetIndirectDiffuseMode(hdCamera)) { - bool validIndirectDiffuse = ValidIndirectDiffuseState(hdCamera); - if (validIndirectDiffuse) - { + case IndirectDiffuseMode.Off: + BindBlackIndirectDiffuseTexture(cmd); + break; + + case IndirectDiffuseMode.ScreenSpace: RenderSSGI(hdCamera, cmd, renderContext, m_FrameCount); BindIndirectDiffuseTexture(cmd); - } - else - { - BindBlackIndirectDiffuseTexture(cmd); - } + break; + + case IndirectDiffuseMode.Raytrace: + RenderRayTracedIndirectDiffuse(hdCamera, cmd, renderContext, m_FrameCount); + break; } if (!hdCamera.frameSettings.SSRRunsAsync()) @@ -5308,7 +5293,7 @@ void SendGeometryGraphicsBuffers(CommandBuffer cmd, HDCamera hdCamera) VFXCameraBufferTypes neededVFXBuffers = VFXManager.IsCameraBufferNeeded(hdCamera.camera); needNormalBuffer |= ((neededVFXBuffers & VFXCameraBufferTypes.Normal) != 0 || (externalAccess & HDAdditionalCameraData.BufferAccessType.Normal) != 0); needDepthBuffer |= ((neededVFXBuffers & VFXCameraBufferTypes.Depth) != 0 || (externalAccess & HDAdditionalCameraData.BufferAccessType.Depth) != 0); - if (hdCamera.frameSettings.IsEnabled(FrameSettingsField.RayTracing) && GetRayTracingState() || ValidIndirectDiffuseState(hdCamera)) + if (hdCamera.frameSettings.IsEnabled(FrameSettingsField.RayTracing) && GetRayTracingState() || GetIndirectDiffuseMode(hdCamera) != IndirectDiffuseMode.Off) { needNormalBuffer = true; needDepthBuffer = true; diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/HDRaytracingIndirectDiffuse.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/HDRaytracingIndirectDiffuse.cs index fd34ac79eb1..e9d5614e5f0 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/HDRaytracingIndirectDiffuse.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/HDRaytracingIndirectDiffuse.cs @@ -15,14 +15,6 @@ void ReleaseRayTracedIndirectDiffuse() { } - bool RayTracedIndirectDiffuseState(HDCamera hdCamera) - { - var settings = hdCamera.volumeStack.GetComponent(); - return ValidIndirectDiffuseState(hdCamera) - && settings.rayTracing.value - && hdCamera.frameSettings.IsEnabled(FrameSettingsField.RayTracing); - } - RTHandle IndirectDiffuseHistoryBufferAllocatorFunction(string viewName, int frameIndex, RTHandleSystem rtHandleSystem) { return rtHandleSystem.Alloc(Vector2.one, TextureXR.slices, colorFormat: GraphicsFormat.R16G16B16A16_SFloat, dimension: TextureXR.dimension, @@ -32,10 +24,6 @@ RTHandle IndirectDiffuseHistoryBufferAllocatorFunction(string viewName, int fram void RenderRayTracedIndirectDiffuse(HDCamera hdCamera, CommandBuffer cmd, ScriptableRenderContext renderContext, int frameCount) { - // If we are not supposed to evaluate the ray traced indirect diffuse term, quit right away - if (!RayTracedIndirectDiffuseState(hdCamera)) - return; - GlobalIllumination giSettings = hdCamera.volumeStack.GetComponent(); // Based on what the asset supports, follow the volume or force the right mode. diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Settings/FrameSettings.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Settings/FrameSettings.cs index 8a73cbd7881..4fa21c32f16 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Settings/FrameSettings.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Settings/FrameSettings.cs @@ -745,7 +745,8 @@ internal static void Sanitize(ref FrameSettings sanitizedFrameSettings, Camera c sanitizedFrameSettings.bitDatas[(int)FrameSettingsField.TransparentSSR] &= renderPipelineSettings.supportSSRTransparent && sanitizedFrameSettings.bitDatas[(int)FrameSettingsField.SSR]; sanitizedFrameSettings.bitDatas[(int)FrameSettingsField.Refraction] &= !preview; sanitizedFrameSettings.bitDatas[(int)FrameSettingsField.SSAO] &= renderPipelineSettings.supportSSAO && !preview; - sanitizedFrameSettings.bitDatas[(int)FrameSettingsField.SSGI] &= renderPipelineSettings.supportSSGI && !preview; + // SSGI is disabled for reflection camera + sanitizedFrameSettings.bitDatas[(int)FrameSettingsField.SSGI] &= renderPipelineSettings.supportSSGI && !preview && !reflection; sanitizedFrameSettings.bitDatas[(int)FrameSettingsField.SubsurfaceScattering] &= renderPipelineSettings.supportSubsurfaceScattering; // We must take care of the scene view fog flags in the editor diff --git a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesGlobal.cs b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesGlobal.cs index 11c4a407f0e..f6a6201fcdf 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesGlobal.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesGlobal.cs @@ -240,8 +240,8 @@ unsafe struct ShaderVariablesGlobal public Vector4 _CoarseStencilBufferSize; - public int _UseIndirectDiffuse; // Uniform variables that defines if we should be using the raytraced indirect diffuse - public int _UseRayTracedReflections; + public int _IndirectDiffuseMode; // Match IndirectDiffuseMode enum in LightLoop.cs + public int _EnableRayTracedReflections; public int _RaytracingFrameIndex; // Index of the current frame [0, 7] public uint _EnableRecursiveRayTracing; diff --git a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesGlobal.cs.hlsl b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesGlobal.cs.hlsl index ebe3ecad8b0..15c4a6b84f3 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesGlobal.cs.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesGlobal.cs.hlsl @@ -133,8 +133,8 @@ GLOBAL_CBUFFER_START(ShaderVariablesGlobal, b0) uint _XRViewCount; int _FrameCount; float4 _CoarseStencilBufferSize; - int _UseIndirectDiffuse; - int _UseRayTracedReflections; + int _IndirectDiffuseMode; + int _EnableRayTracedReflections; int _RaytracingFrameIndex; uint _EnableRecursiveRayTracing; float4 _ProbeVolumeAtlasResolutionAndSliceCount; From 9c1b65761ce14c1ae8cc0221a24caf30fdfcaaf7 Mon Sep 17 00:00:00 2001 From: sebastienlagarde Date: Wed, 17 Jun 2020 11:15:00 +0200 Subject: [PATCH 2/8] Little extra cleanup --- .../Runtime/RenderPipeline/HDRenderPipeline.cs | 11 ++++++++++- .../Runtime/RenderPipeline/Settings/FrameSettings.cs | 3 +-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs index 9f40df332eb..7a1a6f77ef6 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs @@ -5293,7 +5293,16 @@ void SendGeometryGraphicsBuffers(CommandBuffer cmd, HDCamera hdCamera) VFXCameraBufferTypes neededVFXBuffers = VFXManager.IsCameraBufferNeeded(hdCamera.camera); needNormalBuffer |= ((neededVFXBuffers & VFXCameraBufferTypes.Normal) != 0 || (externalAccess & HDAdditionalCameraData.BufferAccessType.Normal) != 0); needDepthBuffer |= ((neededVFXBuffers & VFXCameraBufferTypes.Depth) != 0 || (externalAccess & HDAdditionalCameraData.BufferAccessType.Depth) != 0); - if (hdCamera.frameSettings.IsEnabled(FrameSettingsField.RayTracing) && GetRayTracingState() || GetIndirectDiffuseMode(hdCamera) != IndirectDiffuseMode.Off) + IndirectDiffuseMode indirectDiffuseMode = GetIndirectDiffuseMode(hdCamera); + + // SSGI required the depth of the previous frame + if (indirectDiffuseMode == IndirectDiffuseMode.ScreenSpace) + { + needDepthBuffer = true; + } + + // Raytracing require both normal and depth from previous frame. + if (hdCamera.frameSettings.IsEnabled(FrameSettingsField.RayTracing) && GetRayTracingState()) { needNormalBuffer = true; needDepthBuffer = true; diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Settings/FrameSettings.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Settings/FrameSettings.cs index 4fa21c32f16..8a73cbd7881 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Settings/FrameSettings.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Settings/FrameSettings.cs @@ -745,8 +745,7 @@ internal static void Sanitize(ref FrameSettings sanitizedFrameSettings, Camera c sanitizedFrameSettings.bitDatas[(int)FrameSettingsField.TransparentSSR] &= renderPipelineSettings.supportSSRTransparent && sanitizedFrameSettings.bitDatas[(int)FrameSettingsField.SSR]; sanitizedFrameSettings.bitDatas[(int)FrameSettingsField.Refraction] &= !preview; sanitizedFrameSettings.bitDatas[(int)FrameSettingsField.SSAO] &= renderPipelineSettings.supportSSAO && !preview; - // SSGI is disabled for reflection camera - sanitizedFrameSettings.bitDatas[(int)FrameSettingsField.SSGI] &= renderPipelineSettings.supportSSGI && !preview && !reflection; + sanitizedFrameSettings.bitDatas[(int)FrameSettingsField.SSGI] &= renderPipelineSettings.supportSSGI && !preview; sanitizedFrameSettings.bitDatas[(int)FrameSettingsField.SubsurfaceScattering] &= renderPipelineSettings.supportSubsurfaceScattering; // We must take care of the scene view fog flags in the editor From 60f5c5d64b36401c5f11b596772712df1c2e5e1a Mon Sep 17 00:00:00 2001 From: sebastienlagarde Date: Wed, 17 Jun 2020 14:16:32 +0200 Subject: [PATCH 3/8] Clean area shadow code and remove usless macro --- .../Runtime/ShaderConfig.cs | 1 + .../Runtime/ShaderConfig.cs.hlsl | 1 + .../Runtime/Debug/DebugDisplay.hlsl | 2 +- .../Runtime/Lighting/LightEvaluation.hlsl | 65 +++++++++++++++---- .../Runtime/Lighting/LightLoop/HDShadow.hlsl | 2 +- .../Lighting/LightLoop/HDShadowLoop.hlsl | 2 +- .../Runtime/Lighting/LightLoop/LightLoop.hlsl | 4 +- .../Lighting/LightLoop/LightLoopDef.hlsl | 4 +- .../Runtime/Lighting/SurfaceShading.hlsl | 4 +- .../VolumetricLighting.compute | 6 +- .../Runtime/Material/Eye/Eye.hlsl | 38 +---------- .../Material/LayeredLit/LayeredLit.shader | 1 - .../Runtime/Material/Lit/Lit.hlsl | 53 ++------------- .../Runtime/Material/StackLit/StackLit.hlsl | 45 +------------ .../Shaders/Shadows/RaytracingShadow.compute | 2 +- .../Shaders/Shadows/RaytracingShadow.raytrace | 4 +- .../ShaderLibrary/ShaderVariables.hlsl | 10 ++- 17 files changed, 93 insertions(+), 151 deletions(-) diff --git a/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs b/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs index f08a3d2a206..ac8b3435e33 100644 --- a/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs +++ b/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs @@ -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 diff --git a/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs.hlsl b/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs.hlsl index 445d13dda83..76dee93d683 100644 --- a/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs.hlsl +++ b/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs.hlsl @@ -35,6 +35,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) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Debug/DebugDisplay.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Debug/DebugDisplay.hlsl index 064a1b0fb3d..f2d3722fb16 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Debug/DebugDisplay.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Debug/DebugDisplay.hlsl @@ -13,7 +13,7 @@ // Local shader variables -static DirectionalShadowType g_DebugShadowAttenuation = 0; +static SHADOW_TYPE g_DebugShadowAttenuation = 0; StructuredBuffer _DebugDepthPyramidOffsets; diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightEvaluation.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightEvaluation.hlsl index 3e93f753279..63d5f6bf80f 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightEvaluation.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightEvaluation.hlsl @@ -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 @@ -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 @@ -472,6 +471,50 @@ 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)) + { + // light.positionRWS now contains the Light vector. + 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 //----------------------------------------------------------------------------- diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/HDShadow.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/HDShadow.hlsl index 2800a261dfa..a277098a46f 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/HDShadow.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/HDShadow.hlsl @@ -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); diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/HDShadowLoop.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/HDShadowLoop.hlsl index 6013592f3c0..467bfae44e8 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/HDShadowLoop.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/HDShadowLoop.hlsl @@ -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); diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoop.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoop.hlsl index 1ffc924088f..a708eb4fc20 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoop.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoop.hlsl @@ -113,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]; @@ -193,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 diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoopDef.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoopDef.hlsl index bb43e4b44f7..5d9ed86d962 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoopDef.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoopDef.hlsl @@ -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. diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/SurfaceShading.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Lighting/SurfaceShading.hlsl index dd52a6f9e94..bd1e63138f7 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Lighting/SurfaceShading.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/SurfaceShading.hlsl @@ -92,7 +92,7 @@ DirectLighting ShadeSurface_Directional(LightLoopContext lightLoopContext, else #endif { - DirectionalShadowType shadow = EvaluateShadow_Directional(lightLoopContext, posInput, light, builtinData, GetNormalForShadowBias(bsdfData)); + SHADOW_TYPE shadow = EvaluateShadow_Directional(lightLoopContext, posInput, light, builtinData, GetNormalForShadowBias(bsdfData)); float NdotL = dot(bsdfData.normalWS, L); // No microshadowing when facing away from light (use for thin transmission as well) shadow *= NdotL >= 0.0 ? ComputeMicroShadowing(GetAmbientOcclusionForMicroShadowing(bsdfData), NdotL, _MicroShadowOpacity) : 1.0; lightColor.rgb *= ComputeShadowColor(shadow, light.shadowTint, light.penumbraTint); @@ -176,7 +176,7 @@ DirectLighting ShadeSurface_Punctual(LightLoopContext lightLoopContext, #endif { // This code works for both surface reflection and thin object transmission. - float shadow = EvaluateShadow_Punctual(lightLoopContext, posInput, light, builtinData, GetNormalForShadowBias(bsdfData), L, distances); + SHADOW_TYPE shadow = EvaluateShadow_Punctual(lightLoopContext, posInput, light, builtinData, GetNormalForShadowBias(bsdfData), L, distances); lightColor.rgb *= ComputeShadowColor(shadow, light.shadowTint, light.penumbraTint); #ifdef DEBUG_DISPLAY diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/VolumetricLighting/VolumetricLighting.compute b/com.unity.render-pipelines.high-definition/Runtime/Lighting/VolumetricLighting/VolumetricLighting.compute index 7c36569d84d..b9be1a26c11 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Lighting/VolumetricLighting/VolumetricLighting.compute +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/VolumetricLighting/VolumetricLighting.compute @@ -203,7 +203,7 @@ VoxelLighting EvaluateVoxelLightingDirectional(LightLoopContext context, uint fe #endif // SHADOW_VIEW_BIAS // This code works for both surface reflection and thin object transmission. - DirectionalShadowType shadow = EvaluateShadow_Directional(context, posInput, light, unused, shadowN); + SHADOW_TYPE shadow = EvaluateShadow_Directional(context, posInput, light, unused, shadowN); lightColor.rgb *= ComputeShadowColor(shadow, light.shadowTint, light.penumbraTint); // Important: @@ -322,7 +322,7 @@ VoxelLighting EvaluateVoxelLightingLocal(LightLoopContext context, uint featureF float3 shadowN = 0; // No bias #endif // SHADOW_VIEW_BIAS - float shadow = EvaluateShadow_Punctual(context, posInput, light, unused, shadowN, L, distances); + SHADOW_TYPE shadow = EvaluateShadow_Punctual(context, posInput, light, unused, shadowN, L, distances); lightColor.rgb *= ComputeShadowColor(shadow, light.shadowTint, light.penumbraTint); @@ -407,7 +407,7 @@ VoxelLighting EvaluateVoxelLightingLocal(LightLoopContext context, uint featureF float3 shadowN = 0; // No bias #endif // SHADOW_VIEW_BIAS - float shadow = EvaluateShadow_Punctual(context, posInput, light, unused, shadowN, L, distances); + SHADOW_TYPE shadow = EvaluateShadow_Punctual(context, posInput, light, unused, shadowN, L, distances); lightColor.rgb *= ComputeShadowColor(shadow, light.shadowTint, light.penumbraTint); diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Eye/Eye.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/Eye/Eye.hlsl index 0cf77d6a9c1..8c036dbb548 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Eye/Eye.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Eye/Eye.hlsl @@ -647,6 +647,9 @@ DirectLighting EvaluateBSDF_Rect( LightLoopContext lightLoopContext, // This value is what we store in specularFGD, so reuse it lighting.specular += preLightData.specularFGD * ltcValue; + SHADOW_TYPE shadow = EvaluateShadow_RectArea(lightLoopContext, posInput, lightData, builtinData, bsdfData.normalWS, normalize(lightData.positionRWS), length(lightData.positionRWS)); + lightData.color.rgb *= ComputeShadowColor(shadow, lightData.shadowTint, lightData.penumbraTint); + // Save ALU by applying 'lightData.color' only once. lighting.diffuse *= lightData.color; lighting.specular *= lightData.color; @@ -664,41 +667,6 @@ DirectLighting EvaluateBSDF_Rect( LightLoopContext lightLoopContext, } - float shadow = 1.0; - float shadowMask = 1.0; -#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 = (lightData.shadowMaskSelector.x >= 0.0) ? dot(BUILTIN_DATA_SHADOW_MASK, lightData.shadowMaskSelector) : 1.0; -#endif - - #if defined(SCREEN_SPACE_SHADOWS) && !defined(_SURFACE_TYPE_TRANSPARENT) - if (lightData.screenSpaceShadowIndex >= 0) - { - shadow = GetScreenSpaceShadow(posInput, lightData.screenSpaceShadowIndex); - } - else -#endif // ENABLE_RAYTRACING - if (lightData.shadowIndex != -1) - { -#if RASTERIZED_AREA_LIGHT_SHADOWS - // lightData.positionRWS now contains the Light vector. - shadow = GetAreaLightAttenuation(lightLoopContext.shadowContext, posInput.positionSS, posInput.positionWS, bsdfData.normalWS, lightData.shadowIndex, normalize(lightData.positionRWS), length(lightData.positionRWS)); -#ifdef SHADOWS_SHADOWMASK - // See comment for punctual light shadow mask - shadow = lightData.nonLightMappedOnly ? min(shadowMask, shadow) : shadow; -#endif - shadow = lerp(shadowMask, shadow, lightData.shadowDimmer); - - #endif - } - - #if RASTERIZED_AREA_LIGHT_SHADOWS || SUPPORTS_RAYTRACED_AREA_SHADOWS - float3 shadowColor = ComputeShadowColor(shadow, lightData.shadowTint, lightData.penumbraTint); - lighting.diffuse *= shadowColor; - lighting.specular *= shadowColor; -#endif - return lighting; } diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/LayeredLit/LayeredLit.shader b/com.unity.render-pipelines.high-definition/Runtime/Material/LayeredLit/LayeredLit.shader index 62c1c36601e..aaae7ac5a08 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/LayeredLit/LayeredLit.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/LayeredLit/LayeredLit.shader @@ -1077,7 +1077,6 @@ Shader "HDRP/LayeredLit" #pragma multi_compile _ DIRLIGHTMAP_COMBINED #define SHADERPASS SHADERPASS_PATH_TRACING - #define SKIP_RASTERIZED_SHADOWS // We use the low shadow maps for raytracing #define SHADOW_LOW diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.hlsl index 89b05cde86c..1a9d2e4202e 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.hlsl @@ -28,12 +28,6 @@ // #define LIT_DISPLAY_REFERENCE_IBL #endif -#ifndef SKIP_RASTERIZED_SHADOWS -#define RASTERIZED_AREA_LIGHT_SHADOWS 1 -#else -#define RASTERIZED_AREA_LIGHT_SHADOWS 0 -#endif - //----------------------------------------------------------------------------- // Texture and constant buffer declaration //----------------------------------------------------------------------------- @@ -80,8 +74,6 @@ TEXTURE2D_X(_ShadowMaskTexture); // Alias for shadow mask, so we don't need to k #define HAS_REFRACTION (defined(_REFRACTION_PLANE) || defined(_REFRACTION_SPHERE) || defined(_REFRACTION_THIN)) -#define SUPPORTS_RAYTRACED_AREA_SHADOWS (RAYTRACING_ENABLED && (SHADERPASS == SHADERPASS_DEFERRED_LIGHTING)) - // It is safe to include this file after the G-Buffer macros above. #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/MaterialGBufferMacros.hlsl" @@ -1532,7 +1524,6 @@ DirectLighting EvaluateBSDF_Rect( LightLoopContext lightLoopContext, if (dot(lightData.forward, unL) < FLT_EPS) { - // Rotate the light direction into the light space. float3x3 lightToWorld = float3x3(lightData.right, lightData.up, -lightData.forward); unL = mul(unL, transpose(lightToWorld)); @@ -1671,6 +1662,13 @@ DirectLighting EvaluateBSDF_Rect( LightLoopContext lightLoopContext, lighting.specular += preLightData.coatIblF * ltcValue; } + // Raytracing shadow algorithm require to evaluate lighting without shadow, so it defined SKIP_AREA_RASTERIZED_SHADOWS + // This is only present in Lit Material as it is the only one using the improved shadow algorithm. + #ifndef SKIP_AREA_RASTERIZED_SHADOWS + SHADOW_TYPE shadow = EvaluateShadow_RectArea(lightLoopContext, posInput, lightData, builtinData, bsdfData.normalWS, normalize(lightData.positionRWS), length(lightData.positionRWS)); + lightData.color.rgb *= ComputeShadowColor(shadow, lightData.shadowTint, lightData.penumbraTint); + #endif + // Save ALU by applying 'lightData.color' only once. lighting.diffuse *= lightData.color; lighting.specular *= lightData.color; @@ -1685,45 +1683,8 @@ DirectLighting EvaluateBSDF_Rect( LightLoopContext lightLoopContext, } #endif } - - } - - float shadow = 1.0; - float shadowMask = 1.0; -#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 = (lightData.shadowMaskSelector.x >= 0.0) ? dot(BUILTIN_DATA_SHADOW_MASK, lightData.shadowMaskSelector) : 1.0; -#endif - -#if defined(SCREEN_SPACE_SHADOWS) && !defined(_SURFACE_TYPE_TRANSPARENT) - if ((lightData.screenSpaceShadowIndex & SCREEN_SPACE_SHADOW_INDEX_MASK) != INVALID_SCREEN_SPACE_SHADOW) - { - shadow = GetScreenSpaceShadow(posInput, lightData.screenSpaceShadowIndex); - } - else -#endif // ENABLE_RAYTRACING - if (lightData.shadowIndex != -1) - { -#if RASTERIZED_AREA_LIGHT_SHADOWS - // lightData.positionRWS now contains the Light vector. - shadow = GetAreaLightAttenuation(lightLoopContext.shadowContext, posInput.positionSS, posInput.positionWS, bsdfData.normalWS, lightData.shadowIndex, normalize(lightData.positionRWS), length(lightData.positionRWS)); -#ifdef SHADOWS_SHADOWMASK - // See comment for punctual light shadow mask - shadow = lightData.nonLightMappedOnly ? min(shadowMask, shadow) : shadow; -#endif - shadow = lerp(shadowMask, shadow, lightData.shadowDimmer); - -#endif } -#if RASTERIZED_AREA_LIGHT_SHADOWS || SUPPORTS_RAYTRACED_AREA_SHADOWS - float3 shadowColor = ComputeShadowColor(shadow, lightData.shadowTint, lightData.penumbraTint); - lighting.diffuse *= shadowColor; - lighting.specular *= shadowColor; -#endif - - #endif // LIT_DISPLAY_REFERENCE_AREA return lighting; diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/StackLit/StackLit.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/StackLit/StackLit.hlsl index 07e07bb258d..ed41218715a 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/StackLit/StackLit.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/StackLit/StackLit.hlsl @@ -31,12 +31,6 @@ // #define STACK_LIT_DISPLAY_REFERENCE_IBL #endif -#ifndef SKIP_RASTERIZED_SHADOWS -#define RASTERIZED_AREA_LIGHT_SHADOWS 1 -#else -#define RASTERIZED_AREA_LIGHT_SHADOWS 0 -#endif - //----------------------------------------------------------------------------- // Texture and constant buffer declaration //----------------------------------------------------------------------------- @@ -4111,7 +4105,9 @@ DirectLighting EvaluateBSDF_Rect( LightLoopContext lightLoopContext, } } lighting.specular *= lightData.specularDimmer; - + + SHADOW_TYPE shadow = EvaluateShadow_RectArea(lightLoopContext, posInput, lightData, builtinData, bsdfData.normalWS, normalize(lightData.positionRWS), length(lightData.positionRWS)); + lightData.color.rgb *= ComputeShadowColor(shadow, lightData.shadowTint, lightData.penumbraTint); // Save ALU by applying 'lightData.color' only once. lighting.diffuse *= lightData.color; @@ -4140,41 +4136,6 @@ DirectLighting EvaluateBSDF_Rect( LightLoopContext lightLoopContext, } // if light not back-facing - float shadow = 1.0; - float shadowMask = 1.0; -#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 = (lightData.shadowMaskSelector.x >= 0.0) ? dot(BUILTIN_DATA_SHADOW_MASK, lightData.shadowMaskSelector) : 1.0; -#endif - -#if defined(SCREEN_SPACE_SHADOWS) && !defined(_SURFACE_TYPE_TRANSPARENT) - if ((lightData.screenSpaceShadowIndex & SCREEN_SPACE_SHADOW_INDEX_MASK) != INVALID_SCREEN_SPACE_SHADOW) - { - shadow = GetScreenSpaceShadow(posInput, lightData.screenSpaceShadowIndex); - } - else -#endif // ENABLE_RAYTRACING - if (lightData.shadowIndex != -1) - { -#if RASTERIZED_AREA_LIGHT_SHADOWS - // lightData.positionRWS now contains the Light vector. - shadow = GetAreaLightAttenuation(lightLoopContext.shadowContext, posInput.positionSS, posInput.positionWS, bsdfData.normalWS, lightData.shadowIndex, normalize(lightData.positionRWS), length(lightData.positionRWS)); -#ifdef SHADOWS_SHADOWMASK - // See comment for punctual light shadow mask - shadow = lightData.nonLightMappedOnly ? min(shadowMask, shadow) : shadow; -#endif - shadow = lerp(shadowMask, shadow, lightData.shadowDimmer); - -#endif - } - -#if RASTERIZED_AREA_LIGHT_SHADOWS || SUPPORTS_RAYTRACED_AREA_SHADOWS - float3 shadowColor = ComputeShadowColor(shadow, lightData.shadowTint, lightData.penumbraTint); - lighting.diffuse *= shadowColor; - lighting.specular *= shadowColor; -#endif - #endif // STACK_LIT_DISPLAY_REFERENCE_AREA return lighting; diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/Shadows/RaytracingShadow.compute b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/Shadows/RaytracingShadow.compute index 65f45e2f235..c8e67f8ed1e 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/Shadows/RaytracingShadow.compute +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/Shadows/RaytracingShadow.compute @@ -11,7 +11,7 @@ #define HAS_LIGHTLOOP // Given that the algorithm requires BSDF evaluation, we need to define this macro -#define SKIP_RASTERIZED_SHADOWS +#define SKIP_AREA_RASTERIZED_SHADOWS // Given that this pass does not use the shadow algorithm multi-compile, we need to define SHADOW_LOW to quite the shadow algorithm error #define SHADOW_LOW diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/Shadows/RaytracingShadow.raytrace b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/Shadows/RaytracingShadow.raytrace index fea469a6773..f6b00e3940a 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/Shadows/RaytracingShadow.raytrace +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/Shadows/RaytracingShadow.raytrace @@ -8,7 +8,7 @@ #define HAS_LIGHTLOOP // Given that the algorithm requires BSDF evaluation, we need to define this macro -#define SKIP_RASTERIZED_SHADOWS +#define SKIP_AREA_RASTERIZED_SHADOWS // Given that this pass does not use the shadow algorithm multi-compile, we need to define SHADOW_LOW to quite the shadow algorithm error #define SHADOW_LOW @@ -63,6 +63,8 @@ void MissShaderShadowsColor(inout RayIntersection rayIntersection : SV_RayPayloa // Does nothing intentionally } +// This code below is the "high quality shadow" but it is not used currently. We use the compute version which is more efficient +// and which do a good enough approximation. [shader("raygeneration")] void RayGenAreaShadows() { diff --git a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl index 373797bb73d..ca0ec010edf 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl @@ -29,10 +29,16 @@ // This only defines the ray tracing macro on the platforms that support ray tracing this should be dx12 #if (SHADEROPTIONS_RAYTRACING && (defined(SHADER_API_D3D11) || defined(SHADER_API_D3D12)) && !defined(SHADER_API_XBOXONE) && !defined(SHADER_API_PSSL)) #define RAYTRACING_ENABLED (1) -#define DirectionalShadowType float3 #else #define RAYTRACING_ENABLED (0) -#define DirectionalShadowType float +#endif + +#if SHADEROPTIONS_COLORED_SHADOW +#define SHADOW_TYPE real3 +#define SHADOW_TYPE_SWIZZLE xyz +#else +#define SHADOW_TYPE real +#define SHADOW_TYPE_SWIZZLE x #endif #if defined(SHADER_STAGE_RAY_TRACING) From 0b50b338a698d5c6f44fc56778af9c0cada03fba Mon Sep 17 00:00:00 2001 From: sebastienlagarde Date: Wed, 17 Jun 2020 14:35:00 +0200 Subject: [PATCH 4/8] update shader config files for projects --- .../Runtime/ShaderConfig.cs | 1 + .../Runtime/ShaderConfig.cs.hlsl | 1 + .../Runtime/ShaderConfig.cs.hlsl | 8 -------- 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/TestProjects/HDRP_DXR_Tests/LocalPackages/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs b/TestProjects/HDRP_DXR_Tests/LocalPackages/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs index f08a3d2a206..ac8b3435e33 100644 --- a/TestProjects/HDRP_DXR_Tests/LocalPackages/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs +++ b/TestProjects/HDRP_DXR_Tests/LocalPackages/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs @@ -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 diff --git a/TestProjects/HDRP_DXR_Tests/LocalPackages/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs.hlsl b/TestProjects/HDRP_DXR_Tests/LocalPackages/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs.hlsl index 6b354cd380c..b21ea94c319 100644 --- a/TestProjects/HDRP_DXR_Tests/LocalPackages/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs.hlsl +++ b/TestProjects/HDRP_DXR_Tests/LocalPackages/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs.hlsl @@ -28,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) diff --git a/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs.hlsl b/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs.hlsl index 76dee93d683..efc6d322dac 100644 --- a/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs.hlsl +++ b/com.unity.render-pipelines.high-definition-config/Runtime/ShaderConfig.cs.hlsl @@ -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 // @@ -46,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) From 29b9a121563b57e518f95bbf8102631d2a690f55 Mon Sep 17 00:00:00 2001 From: sebastienlagarde Date: Wed, 17 Jun 2020 14:38:18 +0200 Subject: [PATCH 5/8] Better naming for SKIP_RASTERIZED_AREA_SHADOWS --- .../Runtime/Material/Lit/Lit.hlsl | 4 ++-- .../Raytracing/Shaders/Shadows/RaytracingShadow.compute | 2 +- .../Raytracing/Shaders/Shadows/RaytracingShadow.raytrace | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.hlsl index 1a9d2e4202e..f944e94b1be 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.hlsl @@ -1662,9 +1662,9 @@ DirectLighting EvaluateBSDF_Rect( LightLoopContext lightLoopContext, lighting.specular += preLightData.coatIblF * ltcValue; } - // Raytracing shadow algorithm require to evaluate lighting without shadow, so it defined SKIP_AREA_RASTERIZED_SHADOWS + // Raytracing shadow algorithm require to evaluate lighting without shadow, so it defined SKIP_RASTERIZED_AREA_SHADOWS // This is only present in Lit Material as it is the only one using the improved shadow algorithm. - #ifndef SKIP_AREA_RASTERIZED_SHADOWS + #ifndef SKIP_RASTERIZED_AREA_SHADOWS SHADOW_TYPE shadow = EvaluateShadow_RectArea(lightLoopContext, posInput, lightData, builtinData, bsdfData.normalWS, normalize(lightData.positionRWS), length(lightData.positionRWS)); lightData.color.rgb *= ComputeShadowColor(shadow, lightData.shadowTint, lightData.penumbraTint); #endif diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/Shadows/RaytracingShadow.compute b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/Shadows/RaytracingShadow.compute index c8e67f8ed1e..d7b340f136d 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/Shadows/RaytracingShadow.compute +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/Shadows/RaytracingShadow.compute @@ -11,7 +11,7 @@ #define HAS_LIGHTLOOP // Given that the algorithm requires BSDF evaluation, we need to define this macro -#define SKIP_AREA_RASTERIZED_SHADOWS +#define SKIP_RASTERIZED_AREA_SHADOWS // Given that this pass does not use the shadow algorithm multi-compile, we need to define SHADOW_LOW to quite the shadow algorithm error #define SHADOW_LOW diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/Shadows/RaytracingShadow.raytrace b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/Shadows/RaytracingShadow.raytrace index f6b00e3940a..ee2b27af396 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/Shadows/RaytracingShadow.raytrace +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/Shadows/RaytracingShadow.raytrace @@ -8,7 +8,7 @@ #define HAS_LIGHTLOOP // Given that the algorithm requires BSDF evaluation, we need to define this macro -#define SKIP_AREA_RASTERIZED_SHADOWS +#define SKIP_RASTERIZED_AREA_SHADOWS // Given that this pass does not use the shadow algorithm multi-compile, we need to define SHADOW_LOW to quite the shadow algorithm error #define SHADOW_LOW From 3bc4d52d84e52b9ff2c59de0e7c8ca17ddb616bc Mon Sep 17 00:00:00 2001 From: sebastienlagarde Date: Wed, 17 Jun 2020 14:47:03 +0200 Subject: [PATCH 6/8] Update Upgrading-from-2020.1-to-2020.2.md --- .../Documentation~/Upgrading-from-2020.1-to-2020.2.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/com.unity.render-pipelines.high-definition/Documentation~/Upgrading-from-2020.1-to-2020.2.md b/com.unity.render-pipelines.high-definition/Documentation~/Upgrading-from-2020.1-to-2020.2.md index 54f5a4e40be..86cc8613c78 100644 --- a/com.unity.render-pipelines.high-definition/Documentation~/Upgrading-from-2020.1-to-2020.2.md +++ b/com.unity.render-pipelines.high-definition/Documentation~/Upgrading-from-2020.1-to-2020.2.md @@ -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. ## Shader code @@ -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. + ## Custom pass API The signature of the Execute function has changed to simplify the parameters, now it only takes a CustomPassContext as its input: From 547f9a492b21cf0e3a4024c230eac5d9a44ffb74 Mon Sep 17 00:00:00 2001 From: sebastienlagarde Date: Wed, 17 Jun 2020 15:41:22 +0200 Subject: [PATCH 7/8] remove useless comment --- .../Runtime/Lighting/LightEvaluation.hlsl | 1 - 1 file changed, 1 deletion(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightEvaluation.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightEvaluation.hlsl index 63d5f6bf80f..c3497fd9162 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightEvaluation.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightEvaluation.hlsl @@ -495,7 +495,6 @@ SHADOW_TYPE EvaluateShadow_RectArea( LightLoopContext lightLoopContext, Position #endif if ((light.shadowIndex >= 0) && (light.shadowDimmer > 0)) { - // light.positionRWS now contains the Light vector. shadow = GetRectAreaShadowAttenuation(lightLoopContext.shadowContext, posInput.positionSS, posInput.positionWS, N, light.shadowIndex, L, dist); #ifdef SHADOWS_SHADOWMASK From 133d9fcda0514a00b5111557d80ae9268ff734ad Mon Sep 17 00:00:00 2001 From: sebastienlagarde Date: Wed, 17 Jun 2020 18:15:05 +0200 Subject: [PATCH 8/8] cleanup have deferred rendering --- .../ShaderPass/ShaderPassDepthOnly.hlsl | 17 ++++- .../ShaderPass/ShaderPassForward.hlsl | 16 ++++- .../ShaderPass/ShaderPassGBuffer.hlsl | 17 ++++- .../RenderPipeline/ShaderPass/VertMesh.hlsl | 63 +++++++------------ 4 files changed, 69 insertions(+), 44 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassDepthOnly.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassDepthOnly.hlsl index ea39ff3d17d..dc1efab57c5 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassDepthOnly.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassDepthOnly.hlsl @@ -7,7 +7,22 @@ PackedVaryingsType Vert(AttributesMesh inputMesh) { VaryingsType varyingsType; - varyingsType.vmesh = VertMesh(inputMesh); + +#if (SHADERPASS == SHADERPASS_DEPTH_ONLY) && defined(HAVE_RECURSIVE_RENDERING) && !defined(SCENESELECTIONPASS) + // If we have a recursive raytrace object, we will not render it. + // As we don't want to rely on renderqueue to exclude the object from the list, + // we cull it by settings position to NaN value. + // TODO: provide a solution to filter dyanmically recursive raytrace object in the DrawRenderer + if (_EnableRecursiveRayTracing && _RayTracing > 0.0) + { + ZERO_INITIALIZE(VaryingsType, varyingsType); // Divide by 0 should produce a NaN and thus cull the primitive. + } + else +#endif + { + varyingsType.vmesh = VertMesh(inputMesh); + } + return PackVaryingsType(varyingsType); } diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassForward.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassForward.hlsl index b9d548d1443..4a392bdfc40 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassForward.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassForward.hlsl @@ -35,7 +35,21 @@ PackedVaryingsToPS VertTesselation(VaryingsToDS input) PackedVaryingsType Vert(AttributesMesh inputMesh) { VaryingsType varyingsType; - varyingsType.vmesh = VertMesh(inputMesh); + +#if defined(HAVE_RECURSIVE_RENDERING) + // If we have a recursive raytrace object, we will not render it. + // As we don't want to rely on renderqueue to exclude the object from the list, + // we cull it by settings position to NaN value. + // TODO: provide a solution to filter dyanmically recursive raytrace object in the DrawRenderer + if (_EnableRecursiveRayTracing && _RayTracing > 0.0) + { + ZERO_INITIALIZE(VaryingsType, varyingsType); // Divide by 0 should produce a NaN and thus cull the primitive. + } + else +#endif + { + varyingsType.vmesh = VertMesh(inputMesh); + } return PackVaryingsType(varyingsType); } diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassGBuffer.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassGBuffer.hlsl index aacfad5bb7a..852cb2afa6d 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassGBuffer.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassGBuffer.hlsl @@ -7,7 +7,22 @@ PackedVaryingsType Vert(AttributesMesh inputMesh) { VaryingsType varyingsType; - varyingsType.vmesh = VertMesh(inputMesh); + +#if defined(HAVE_RECURSIVE_RENDERING) + // If we have a recursive raytrace object, we will not render it. + // As we don't want to rely on renderqueue to exclude the object from the list, + // we cull it by settings position to NaN value. + // TODO: provide a solution to filter dyanmically recursive raytrace object in the DrawRenderer + if (_EnableRecursiveRayTracing && _RayTracing > 0.0) + { + ZERO_INITIALIZE(VaryingsType, varyingsType); // Divide by 0 should produce a NaN and thus cull the primitive. + } + else +#endif + { + varyingsType.vmesh = VertMesh(inputMesh); + } + return PackVaryingsType(varyingsType); } diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/VertMesh.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/VertMesh.hlsl index e902906a95c..61d5ddbcb91 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/VertMesh.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/VertMesh.hlsl @@ -98,86 +98,67 @@ VaryingsToDS InterpolateWithBaryCoordsToDS(VaryingsToDS input0, VaryingsToDS inp #define PackVaryingsType PackVaryingsToPS #endif -#define TEST_RECURSIVE_RENDERING (SHADEROPTIONS_RAYTRACING && defined(HAVE_RECURSIVE_RENDERING) && (SHADERPASS == SHADERPASS_GBUFFER || SHADERPASS == SHADERPASS_FORWARD || (SHADERPASS == SHADERPASS_DEPTH_ONLY && !defined(SCENESELECTIONPASS)))) // TODO: Here we will also have all the vertex deformation (GPU skinning, vertex animation, morph target...) or we will need to generate a compute shaders instead (better! but require work to deal with unpacking like fp16) // Make it inout so that MotionVectorPass can get the modified input values later. VaryingsMeshType VertMesh(AttributesMesh input) { VaryingsMeshType output; - // If we have a recursive raytrace object, we will not render it. - // As we don't want to rely on renderqueue to exclude the object from the list, - // we cull it by settings position to NaN value. - // TODO: provide a solution to filter dyanmically recursive raytrace object in the DrawRenderer -#if TEST_RECURSIVE_RENDERING - // Note: Raytrace object can output in motion vector pass and shadow pass - if (_EnableRecursiveRayTracing && _RayTracing > 0.0) - { - ZERO_INITIALIZE(VaryingsMeshType, output); // Divide by 0 should produce a NaN and thus cull the primitive. - } - else - { -#endif - - UNITY_SETUP_INSTANCE_ID(input); - UNITY_TRANSFER_INSTANCE_ID(input, output); + UNITY_SETUP_INSTANCE_ID(input); + UNITY_TRANSFER_INSTANCE_ID(input, output); #if defined(HAVE_MESH_MODIFICATION) - input = ApplyMeshModification(input, _TimeParameters.xyz); + input = ApplyMeshModification(input, _TimeParameters.xyz); #endif - // This return the camera relative position (if enable) - float3 positionRWS = TransformObjectToWorld(input.positionOS); + // This return the camera relative position (if enable) + float3 positionRWS = TransformObjectToWorld(input.positionOS); #ifdef ATTRIBUTES_NEED_NORMAL - float3 normalWS = TransformObjectToWorldNormal(input.normalOS); + float3 normalWS = TransformObjectToWorldNormal(input.normalOS); #else - float3 normalWS = float3(0.0, 0.0, 0.0); // We need this case to be able to compile ApplyVertexModification that doesn't use normal. + float3 normalWS = float3(0.0, 0.0, 0.0); // We need this case to be able to compile ApplyVertexModification that doesn't use normal. #endif #ifdef ATTRIBUTES_NEED_TANGENT - float4 tangentWS = float4(TransformObjectToWorldDir(input.tangentOS.xyz), input.tangentOS.w); + float4 tangentWS = float4(TransformObjectToWorldDir(input.tangentOS.xyz), input.tangentOS.w); #endif - // Do vertex modification in camera relative space (if enable) + // Do vertex modification in camera relative space (if enable) #if defined(HAVE_VERTEX_MODIFICATION) - ApplyVertexModification(input, normalWS, positionRWS, _TimeParameters.xyz); + ApplyVertexModification(input, normalWS, positionRWS, _TimeParameters.xyz); #endif #ifdef TESSELLATION_ON - output.positionRWS = positionRWS; - output.normalWS = normalWS; + output.positionRWS = positionRWS; + output.normalWS = normalWS; #if defined(VARYINGS_NEED_TANGENT_TO_WORLD) || defined(VARYINGS_DS_NEED_TANGENT) - output.tangentWS = tangentWS; + output.tangentWS = tangentWS; #endif #else #ifdef VARYINGS_NEED_POSITION_WS - output.positionRWS = positionRWS; + output.positionRWS = positionRWS; #endif - output.positionCS = TransformWorldToHClip(positionRWS); + output.positionCS = TransformWorldToHClip(positionRWS); #ifdef VARYINGS_NEED_TANGENT_TO_WORLD - output.normalWS = normalWS; - output.tangentWS = tangentWS; + output.normalWS = normalWS; + output.tangentWS = tangentWS; #endif #endif #if defined(VARYINGS_NEED_TEXCOORD0) || defined(VARYINGS_DS_NEED_TEXCOORD0) - output.texCoord0 = input.uv0; + output.texCoord0 = input.uv0; #endif #if defined(VARYINGS_NEED_TEXCOORD1) || defined(VARYINGS_DS_NEED_TEXCOORD1) - output.texCoord1 = input.uv1; + output.texCoord1 = input.uv1; #endif #if defined(VARYINGS_NEED_TEXCOORD2) || defined(VARYINGS_DS_NEED_TEXCOORD2) - output.texCoord2 = input.uv2; + output.texCoord2 = input.uv2; #endif #if defined(VARYINGS_NEED_TEXCOORD3) || defined(VARYINGS_DS_NEED_TEXCOORD3) - output.texCoord3 = input.uv3; + output.texCoord3 = input.uv3; #endif #if defined(VARYINGS_NEED_COLOR) || defined(VARYINGS_DS_NEED_COLOR) - output.color = input.color; -#endif - -#if TEST_RECURSIVE_RENDERING - } + output.color = input.color; #endif return output;