Skip to content

Various code cleanup in HDRP indirect diffuse lighting + Start remove macro RAYTRACING #937

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 3 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 @@ -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 Down Expand Up @@ -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)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

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

for SSR it is pre-multiplied in the compute shader for ray tracing it is not and there is multiples places where it could be the final input and that would be a mess to track

}
#endif
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 @@ -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 @@ -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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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))
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -105,16 +102,25 @@ 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))
#define SAMPLE_PROBEVOLUME (SHADEROPTIONS_PROBE_VOLUMES_EVALUATION_MODE == PROBEVOLUMESEVALUATIONMODES_MATERIAL_PASS) \
&& (!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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ void ExecuteWithRenderGraph( RenderRequest renderRequest,
// lightCluster.EvaluateClusterDebugView(cmd, hdCamera);
// }

// TODO: check code, everything have change
// bool validIndirectDiffuse = ValidIndirectDiffuseState(hdCamera);
// if (validIndirectDiffuse)
// {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<RecursiveRendering>();
ScreenSpaceReflection settings = hdCamera.volumeStack.GetComponent<ScreenSpaceReflection>();
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<RecursiveRendering>();
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;
}

Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -5308,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() || ValidIndirectDiffuseState(hdCamera))
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;
Expand Down
Loading