diff --git a/com.unity.render-pipelines.core/ShaderLibrary/EntityLighting.hlsl b/com.unity.render-pipelines.core/ShaderLibrary/EntityLighting.hlsl index 55c0ccf99f9..1e0dac36490 100644 --- a/com.unity.render-pipelines.core/ShaderLibrary/EntityLighting.hlsl +++ b/com.unity.render-pipelines.core/ShaderLibrary/EntityLighting.hlsl @@ -78,6 +78,7 @@ half3 SampleSH9(half4 SHCoefficients[7], half3 N) return res; } #endif + float3 SampleSH9(float4 SHCoefficients[7], float3 N) { float4 shAr = SHCoefficients[0]; @@ -105,8 +106,10 @@ float3 SampleSH9(float4 SHCoefficients[7], float3 N) // TODO: the packing here is inefficient as we will fetch values far away from each other and they may not fit into the cache - Suggest we pack RGB continuously // TODO: The calcul of texcoord could be perform with a single matrix multicplication calcualted on C++ side that will fold probeVolumeMin and probeVolumeSizeInv into it and handle the identity case, no reasons to do it in C++ (ask Ionut about it) // It should also handle the camera relative path (if the render pipeline use it) -float3 SampleProbeVolumeSH4(TEXTURE3D_PARAM(SHVolumeTexture, SHVolumeSampler), float3 positionWS, float3 normalWS, float4x4 WorldToTexture, - float transformToLocal, float texelSizeX, float3 probeVolumeMin, float3 probeVolumeSizeInv) +// bakeDiffuseLighting and backBakeDiffuseLighting must be initialize outside the function +void SampleProbeVolumeSH4(TEXTURE3D_PARAM(SHVolumeTexture, SHVolumeSampler), float3 positionWS, float3 normalWS, float3 backNormalWS, float4x4 WorldToTexture, + float transformToLocal, float texelSizeX, float3 probeVolumeMin, float3 probeVolumeSizeInv, + inout float3 bakeDiffuseLighting, inout float3 backBakeDiffuseLighting) { float3 position = (transformToLocal == 1.0) ? mul(WorldToTexture, float4(positionWS, 1.0)).xyz : positionWS; float3 texCoord = (position - probeVolumeMin) * probeVolumeSizeInv.xyz; @@ -123,14 +126,30 @@ float3 SampleProbeVolumeSH4(TEXTURE3D_PARAM(SHVolumeTexture, SHVolumeSampler), f texCoord.x += 0.25; float4 shAb = SAMPLE_TEXTURE3D_LOD(SHVolumeTexture, SHVolumeSampler, texCoord, 0); - return SHEvalLinearL0L1(normalWS, shAr, shAg, shAb); + bakeDiffuseLighting += SHEvalLinearL0L1(normalWS, shAr, shAg, shAb); + backBakeDiffuseLighting += SHEvalLinearL0L1(backNormalWS, shAr, shAg, shAb); +} + +// Just a shortcut that call function above +float3 SampleProbeVolumeSH4(TEXTURE3D_PARAM(SHVolumeTexture, SHVolumeSampler), float3 positionWS, float3 normalWS, float4x4 WorldToTexture, + float transformToLocal, float texelSizeX, float3 probeVolumeMin, float3 probeVolumeSizeInv) +{ + float3 backNormalWSUnused = 0.0; + float3 bakeDiffuseLighting = 0.0; + float3 backBakeDiffuseLightingUnused = 0.0; + SampleProbeVolumeSH4(TEXTURE3D_ARGS(SHVolumeTexture, SHVolumeSampler), positionWS, normalWS, backNormalWSUnused, WorldToTexture, + transformToLocal, texelSizeX, probeVolumeMin, probeVolumeSizeInv, + bakeDiffuseLighting, backBakeDiffuseLightingUnused); + return bakeDiffuseLighting; } // The SphericalHarmonicsL2 coefficients are packed into 7 coefficients per color channel instead of 9. // The packing from 9 to 7 is done from engine code and will use the alpha component of the pixel to store an additional SH coefficient. // The 3D atlas texture will contain 7 SH coefficient parts. -float3 SampleProbeVolumeSH9(TEXTURE3D_PARAM(SHVolumeTexture, SHVolumeSampler), float3 positionWS, float3 normalWS, float4x4 WorldToTexture, - float transformToLocal, float texelSizeX, float3 probeVolumeMin, float3 probeVolumeSizeInv) +// bakeDiffuseLighting and backBakeDiffuseLighting must be initialize outside the function +void SampleProbeVolumeSH9(TEXTURE3D_PARAM(SHVolumeTexture, SHVolumeSampler), float3 positionWS, float3 normalWS, float3 backNormalWS, float4x4 WorldToTexture, + float transformToLocal, float texelSizeX, float3 probeVolumeMin, float3 probeVolumeSizeInv, + inout float3 bakeDiffuseLighting, inout float3 backBakeDiffuseLighting) { float3 position = (transformToLocal == 1.0f) ? mul(WorldToTexture, float4(positionWS, 1.0)).xyz : positionWS; float3 texCoord = (position - probeVolumeMin) * probeVolumeSizeInv; @@ -152,8 +171,23 @@ float3 SampleProbeVolumeSH9(TEXTURE3D_PARAM(SHVolumeTexture, SHVolumeSampler), f SHCoefficients[i] = SAMPLE_TEXTURE3D_LOD(SHVolumeTexture, SHVolumeSampler, texCoord, 0); } - return SampleSH9(SHCoefficients, normalize(normalWS)); + bakeDiffuseLighting += SampleSH9(SHCoefficients, normalize(normalWS)); + backBakeDiffuseLighting += SampleSH9(SHCoefficients, normalize(backNormalWS)); } + +// Just a shortcut that call function above +float3 SampleProbeVolumeSH9(TEXTURE3D_PARAM(SHVolumeTexture, SHVolumeSampler), float3 positionWS, float3 normalWS, float4x4 WorldToTexture, + float transformToLocal, float texelSizeX, float3 probeVolumeMin, float3 probeVolumeSizeInv) +{ + float3 backNormalWSUnused = 0.0; + float3 bakeDiffuseLighting = 0.0; + float3 backBakeDiffuseLightingUnused = 0.0; + SampleProbeVolumeSH9(TEXTURE3D_ARGS(SHVolumeTexture, SHVolumeSampler), positionWS, normalWS, backNormalWSUnused, WorldToTexture, + transformToLocal, texelSizeX, probeVolumeMin, probeVolumeSizeInv, + bakeDiffuseLighting, backBakeDiffuseLightingUnused); + return bakeDiffuseLighting; +} + #endif float4 SampleProbeOcclusion(TEXTURE3D_PARAM(SHVolumeTexture, SHVolumeSampler), float3 positionWS, float4x4 WorldToTexture, @@ -252,7 +286,8 @@ real3 SampleSingleLightmap(TEXTURE2D_PARAM(lightmapTex, lightmapSampler), float2 return illuminance; } -real3 SampleDirectionalLightmap(TEXTURE2D_PARAM(lightmapTex, lightmapSampler), TEXTURE2D_PARAM(lightmapDirTex, lightmapDirSampler), float2 uv, float4 transform, float3 normalWS, bool encodedLightmap, real4 decodeInstructions) +void SampleDirectionalLightmap(TEXTURE2D_PARAM(lightmapTex, lightmapSampler), TEXTURE2D_PARAM(lightmapDirTex, lightmapDirSampler), float2 uv, float4 transform, + float3 normalWS, float3 backNormalWS, bool encodedLightmap, real4 decodeInstructions, inout real3 bakeDiffuseLighting, inout real3 backBakeDiffuseLighting) { // In directional mode Enlighten bakes dominant light direction // in a way, that using it for half Lambert and then dividing by a "rebalancing coefficient" @@ -276,8 +311,24 @@ real3 SampleDirectionalLightmap(TEXTURE2D_PARAM(lightmapTex, lightmapSampler), T { illuminance = SAMPLE_TEXTURE2D(lightmapTex, lightmapSampler, uv).rgb; } + real halfLambert = dot(normalWS, direction.xyz - 0.5) + 0.5; - return illuminance * halfLambert / max(1e-4, direction.w); + bakeDiffuseLighting += illuminance * halfLambert / max(1e-4, direction.w); + + real backHalfLambert = dot(backNormalWS, direction.xyz - 0.5) + 0.5; + backBakeDiffuseLighting += illuminance * backHalfLambert / max(1e-4, direction.w); +} + +// Just a shortcut that call function above +real3 SampleDirectionalLightmap(TEXTURE2D_PARAM(lightmapTex, lightmapSampler), TEXTURE2D_PARAM(lightmapDirTex, lightmapDirSampler), float2 uv, float4 transform, float3 normalWS, bool encodedLightmap, real4 decodeInstructions) +{ + float3 backNormalWSUnused = 0.0; + real3 bakeDiffuseLighting = 0.0; + real3 backBakeDiffuseLightingUnused = 0.0; + SampleDirectionalLightmap( lightmapTex, lightmapSampler, lightmapDirTex, lightmapDirSampler, uv, transform, + normalWS, backNormalWSUnused, encodedLightmap, decodeInstructions, bakeDiffuseLighting, backBakeDiffuseLightingUnused); + + return bakeDiffuseLighting; } #endif // UNITY_ENTITY_LIGHTING_INCLUDED diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolume.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolume.hlsl index 75a0432cae0..b26f72f11c7 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolume.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolume.hlsl @@ -487,9 +487,8 @@ float3 EvaluateProbeVolumesLightLoop(inout float probeVolumeHierarchyWeight, Pos } // Fallback to global ambient probe lighting when probe volume lighting weight is not fully saturated. -float3 EvaluateProbeVolumeAmbientProbeFallback(inout float probeVolumeHierarchyWeight, float3 normalWS) +void EvaluateProbeVolumeAmbientProbeFallback(float3 normalWS, float3 backNormalWS, inout float3 bakeDiffuseLighting, inout float3 backBakeDiffuseLighting, inout float probeVolumeHierarchyWeight) { - float3 sampleAmbientProbeOutgoingRadiance = float3(0.0, 0.0, 0.0); if (probeVolumeHierarchyWeight < 1.0 #ifdef DEBUG_DISPLAY && (_DebugProbeVolumeMode != PROBEVOLUMEDEBUGMODE_VISUALIZE_DEBUG_COLORS) @@ -497,10 +496,11 @@ float3 EvaluateProbeVolumeAmbientProbeFallback(inout float probeVolumeHierarchyW #endif ) { - sampleAmbientProbeOutgoingRadiance = SampleSH9(_ProbeVolumeAmbientProbeFallbackPackedCoeffs, normalWS) * (1.0 - probeVolumeHierarchyWeight); + float fallbackWeight = 1.0 - probeVolumeHierarchyWeight; + bakeDiffuseLighting += SampleSH9(_ProbeVolumeAmbientProbeFallbackPackedCoeffs, normalWS) * fallbackWeight; + backBakeDiffuseLighting += SampleSH9(_ProbeVolumeAmbientProbeFallbackPackedCoeffs, backNormalWS) * fallbackWeight; probeVolumeHierarchyWeight = 1.0; } - return sampleAmbientProbeOutgoingRadiance; } #endif // __PROBEVOLUME_HLSL__ 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 f5ad182e0de..0fc5da7c729 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/BuiltinGIUtilities.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/BuiltinGIUtilities.hlsl @@ -25,10 +25,8 @@ float4x4 GetProbeVolumeWorldToObject() return ApplyCameraTranslationToInverseMatrix(unity_ProbeVolumeWorldToObject); } -float3 EvaluateLightmap(float3 positionRWS, float3 normalWS, float2 uvStaticLightmap, float2 uvDynamicLightmap) +void EvaluateLightmap(float3 positionRWS, float3 normalWS, float3 backNormalWS, float2 uvStaticLightmap, float2 uvDynamicLightmap, inout float3 bakeDiffuseLighting, inout float3 backBakeDiffuseLighting) { - float3 bakeDiffuseLighting = float3(0.0, 0.0, 0.0); - #ifdef UNITY_LIGHTMAP_FULL_HDR bool useRGBMLightmap = false; float4 decodeInstructions = float4(0.0, 0.0, 0.0, 0.0); // Never used but needed for the interface since it supports gamma lightmaps @@ -43,28 +41,30 @@ float3 EvaluateLightmap(float3 positionRWS, float3 normalWS, float2 uvStaticLigh #ifdef LIGHTMAP_ON #ifdef DIRLIGHTMAP_COMBINED - bakeDiffuseLighting += SampleDirectionalLightmap(TEXTURE2D_ARGS(unity_Lightmap, samplerunity_Lightmap), + SampleDirectionalLightmap(TEXTURE2D_ARGS(unity_Lightmap, samplerunity_Lightmap), TEXTURE2D_ARGS(unity_LightmapInd, samplerunity_Lightmap), - uvStaticLightmap, unity_LightmapST, normalWS, useRGBMLightmap, decodeInstructions); + uvStaticLightmap, unity_LightmapST, normalWS, backNormalWS, useRGBMLightmap, decodeInstructions, bakeDiffuseLighting, backBakeDiffuseLighting); #else - bakeDiffuseLighting += SampleSingleLightmap(TEXTURE2D_ARGS(unity_Lightmap, samplerunity_Lightmap), uvStaticLightmap, unity_LightmapST, useRGBMLightmap, decodeInstructions); + float3 illuminance = SampleSingleLightmap(TEXTURE2D_ARGS(unity_Lightmap, samplerunity_Lightmap), uvStaticLightmap, unity_LightmapST, useRGBMLightmap, decodeInstructions); + bakeDiffuseLighting += illuminance; + backBakeDiffuseLighting += illuminance; #endif #endif #ifdef DYNAMICLIGHTMAP_ON #ifdef DIRLIGHTMAP_COMBINED - bakeDiffuseLighting += SampleDirectionalLightmap(TEXTURE2D_ARGS(unity_DynamicLightmap, samplerunity_DynamicLightmap), + SampleDirectionalLightmap(TEXTURE2D_ARGS(unity_DynamicLightmap, samplerunity_DynamicLightmap), TEXTURE2D_ARGS(unity_DynamicDirectionality, samplerunity_DynamicLightmap), - uvDynamicLightmap, unity_DynamicLightmapST, normalWS, false, decodeInstructions); + uvDynamicLightmap, unity_DynamicLightmapST, normalWS, backNormalWS, false, decodeInstructions, bakeDiffuseLighting, backBakeDiffuseLighting); #else - bakeDiffuseLighting += SampleSingleLightmap(TEXTURE2D_ARGS(unity_DynamicLightmap, samplerunity_DynamicLightmap), uvDynamicLightmap, unity_DynamicLightmapST, false, decodeInstructions); + float3 illuminance += SampleSingleLightmap(TEXTURE2D_ARGS(unity_DynamicLightmap, samplerunity_DynamicLightmap), uvDynamicLightmap, unity_DynamicLightmapST, false, decodeInstructions); + bakeDiffuseLighting += illuminance; + backBakeDiffuseLighting += illuminance; #endif #endif - - return bakeDiffuseLighting; } -float3 EvaluateProbeVolumeLegacy(float3 positionRWS, float3 normalWS) +void EvaluateLightProbeBuiltin(float3 positionRWS, float3 normalWS, float3 backNormalWS, inout float3 bakeDiffuseLighting, inout float3 backBakeDiffuseLighting) { if (unity_ProbeVolumeParams.x == 0.0) { @@ -78,22 +78,24 @@ float3 EvaluateProbeVolumeLegacy(float3 positionRWS, float3 normalWS) SHCoefficients[5] = unity_SHBb; SHCoefficients[6] = unity_SHC; - return SampleSH9(SHCoefficients, normalWS); + bakeDiffuseLighting += SampleSH9(SHCoefficients, normalWS); + backBakeDiffuseLighting += SampleSH9(SHCoefficients, backNormalWS); } else { #if RAYTRACING_ENABLED if (unity_ProbeVolumeParams.w == 1.0) - return SampleProbeVolumeSH9(TEXTURE3D_ARGS(unity_ProbeVolumeSH, samplerunity_ProbeVolumeSH), positionRWS, normalWS, GetProbeVolumeWorldToObject(), - unity_ProbeVolumeParams.y, unity_ProbeVolumeParams.z, unity_ProbeVolumeMin.xyz, unity_ProbeVolumeSizeInv.xyz); + 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 - return SampleProbeVolumeSH4(TEXTURE3D_ARGS(unity_ProbeVolumeSH, samplerunity_ProbeVolumeSH), positionRWS, normalWS, GetProbeVolumeWorldToObject(), - unity_ProbeVolumeParams.y, unity_ProbeVolumeParams.z, unity_ProbeVolumeMin.xyz, unity_ProbeVolumeSizeInv.xyz); + 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); } } -float3 EvaluateProbeVolumes(inout float probeVolumeHierarchyWeight, PositionInputs posInputs, float3 normalWS, uint renderingLayers) +void EvaluateProbeVolumes( PositionInputs posInputs, float3 normalWS, float3 backNormalWS, uint renderingLayers, + inout float3 bakeDiffuseLighting, inout float3 backBakeDiffuseLighting, inout float probeVolumeHierarchyWeight) { // SHADEROPTIONS_PROBE_VOLUMES can be defined in ShaderConfig.cs.hlsl but set to 0 for disabled. #if SHADEROPTIONS_PROBE_VOLUMES_EVALUATION_MODE == PROBEVOLUMESEVALUATIONMODES_LIGHT_LOOP @@ -113,46 +115,49 @@ float3 EvaluateProbeVolumes(inout float probeVolumeHierarchyWeight, PositionInpu posInputs.tileCoord = tileCoord; #endif - float3 combinedGI = EvaluateProbeVolumesMaterialPass(probeVolumeHierarchyWeight, posInputs, normalWS, renderingLayers); - combinedGI += EvaluateProbeVolumeAmbientProbeFallback(probeVolumeHierarchyWeight, normalWS); - return combinedGI; + // TODO: In a future PR, we will update EvaluateProbeVolumes to support a single call that evaluates front and back facing normals. + // For now, we simply call Evaluate 2x, and pay the additional cost when backBakeDiffuseLighting is in use. + float backProbeVolumeHierarchyWeight = probeVolumeHierarchyWeight; + bakeDiffuseLighting += EvaluateProbeVolumesMaterialPass(posInputs, normalWS, renderingLayers, probeVolumeHierarchyWeight); + backBakeDiffuseLighting += EvaluateProbeVolumesMaterialPass(posInputs, backNormalWS, renderingLayers, backProbeVolumeHierarchyWeight); - #else - // !(SHADERPASS == SHADERPASS_GBUFFER || SHADERPASS == SHADERPASS_FORWARD) - return float3(0, 0, 0); + EvaluateProbeVolumeAmbientProbeFallback(normalWS, backNormalWS, bakeDiffuseLighting, backBakeDiffuseLighting, probeVolumeHierarchyWeight); #endif - #else - return float3(0, 0, 0); #endif // #ifdef SHADERPASS - #else - // SHADEROPTIONS_PROBE_VOLUMES_EVALUATION_MODE == PROBEVOLUMESEVALUATIONMODES_DISABLED - return float3(0, 0, 0); #endif } -// In unity we can have a mix of fully baked lightmap (static lightmap) + enlighten realtime lightmap (dynamic lightmap) -// for each case we can have directional lightmap or not. -// Else we have lightprobe for dynamic/moving entity. Either SH9 per object lightprobe or SH4 per pixel per object volume probe -float3 SampleBakedGI(PositionInputs posInputs, float3 normalWS, uint renderingLayers, float2 uvStaticLightmap, float2 uvDynamicLightmap) +// No need to initialize bakeDiffuseLighting and backBakeDiffuseLighting must be initialize outside the function +void SampleBakedGI( + PositionInputs posInputs, + float3 normalWS, + float3 backNormalWS, + uint renderingLayers, + float2 uvStaticLightmap, + float2 uvDynamicLightmap, + out float3 bakeDiffuseLighting, + out float3 backBakeDiffuseLighting) { float3 positionRWS = posInputs.positionWS; #define SAMPLE_LIGHTMAP (defined(LIGHTMAP_ON) || defined(DYNAMICLIGHTMAP_ON)) #define SAMPLE_PROBEVOLUME (SHADEROPTIONS_PROBE_VOLUMES_EVALUATION_MODE != PROBEVOLUMESEVALUATIONMODES_DISABLED) \ && (!SAMPLE_LIGHTMAP || SHADEROPTIONS_PROBE_VOLUMES_ADDITIVE_BLENDING) -#define SAMPLE_PROBEVOLUME_LEGACY (!SAMPLE_LIGHTMAP && !SAMPLE_PROBEVOLUME) +#define SAMPLE_PROBEVOLUME_BUILTIN (!SAMPLE_LIGHTMAP && !SAMPLE_PROBEVOLUME) - float3 combinedGI = float3(0, 0, 0); + bakeDiffuseLighting = float3(0, 0, 0); + backBakeDiffuseLighting = float3(0, 0, 0); #if SAMPLE_LIGHTMAP - combinedGI += EvaluateLightmap(positionRWS, normalWS, uvStaticLightmap, uvDynamicLightmap); + EvaluateLightmap(positionRWS, normalWS, backNormalWS, uvStaticLightmap, uvDynamicLightmap, bakeDiffuseLighting, backBakeDiffuseLighting); #endif #if SHADEROPTIONS_PROBE_VOLUMES_EVALUATION_MODE == PROBEVOLUMESEVALUATIONMODES_LIGHT_LOOP // If probe volumes are evaluated in the lightloop, we place a sentinel value to detect that no lightmap data is present at the current pixel, // and we can safely overwrite baked data value with value from probe volume evaluation in light loop. #if !SAMPLE_LIGHTMAP - return UNINITIALIZED_GI; + bakeDiffuseLighting = UNINITIALIZED_GI; + return; #endif #else // PROBEVOLUMESEVALUATIONMODES_MATERIAL_PASS || PROBEVOLUMESEVALUATIONMODES_DISABLED @@ -162,23 +167,20 @@ float3 SampleBakedGI(PositionInputs posInputs, float3 normalWS, uint renderingLa #else float probeVolumeHierarchyWeight = 0.0f; #endif - combinedGI += EvaluateProbeVolumes(probeVolumeHierarchyWeight, posInputs, normalWS, renderingLayers); + EvaluateProbeVolumes(posInputs, normalWS, backNormalWS, renderingLayers, bakeDiffuseLighting, backBakeDiffuseLighting, probeVolumeHierarchyWeight); #endif -#if SAMPLE_PROBEVOLUME_LEGACY - combinedGI += EvaluateProbeVolumeLegacy(positionRWS, normalWS); +#if SAMPLE_PROBEVOLUME_BUILTIN + EvaluateLightProbeBuiltin(positionRWS, normalWS, backNormalWS, bakeDiffuseLighting, backBakeDiffuseLighting); #endif #endif -return combinedGI; - #undef SAMPLE_LIGHTMAP #undef SAMPLE_PROBEVOLUME -#undef SAMPLE_PROBEVOLUME_LEGACY +#undef SAMPLE_PROBEVOLUME_BUILTIN } -// Function signature of SampleBakedGI changed when probe volumes we added, as they require full PositionInputs. -// This legacy function signature is exposed in a shader graph node, so must continue to be supported. +// Function signature exposed in a shader graph node, to keep float3 SampleBakedGI(float3 positionRWS, float3 normalWS, float2 uvStaticLightmap, float2 uvDynamicLightmap) { // Need PositionInputs for indexing probe volume clusters, but they are not availbile from the current SampleBakedGI() function signature. @@ -208,7 +210,12 @@ float3 SampleBakedGI(float3 positionRWS, float3 normalWS, float2 uvStaticLightma #endif // #ifdef SHADERPASS #endif - return SampleBakedGI(posInputs, normalWS, renderingLayers, uvStaticLightmap, uvDynamicLightmap); + const float3 backNormalWSUnused = 0.0; + float3 bakeDiffuseLighting; + float3 backBakeDiffuseLightingUnused; + SampleBakedGI(posInputs, normalWS, backNormalWSUnused, renderingLayers, uvStaticLightmap, uvDynamicLightmap, bakeDiffuseLighting, backBakeDiffuseLightingUnused); + + return bakeDiffuseLighting; } float4 SampleShadowMask(float3 positionRWS, float2 uvStaticLightmap) // normalWS not use for now 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 efd7a3a5096..7e380e98bac 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/BuiltinUtilities.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/BuiltinUtilities.hlsl @@ -40,6 +40,12 @@ void InitBuiltinData(PositionInputs posInput, float alpha, float3 normalWS, floa // Use uniform directly - The float need to be cast to uint (as unity don't support to set a uint as uniform) builtinData.renderingLayers = _EnableLightLayers ? asuint(unity_RenderingLayer.x) : DEFAULT_LIGHT_LAYERS; + + // Sample lightmap/probevolume/lightprobe/volume proxy + builtinData.bakeDiffuseLighting = 0.0; + 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) @@ -55,18 +61,7 @@ void InitBuiltinData(PositionInputs posInput, float alpha, float3 normalWS, floa builtinData.bakeDiffuseLighting *= GetInverseCurrentExposureMultiplier(); #endif } - else #endif - { - // Sample lightmap/probevolume/lightprobe/volume proxy - builtinData.bakeDiffuseLighting = SampleBakedGI(posInput, normalWS, builtinData.renderingLayers, texCoord1.xy, texCoord2.xy); - } - - // We also sample the back lighting in case we have transmission. If not use this will be optimize out by the compiler - // For now simply recall the function with inverted normal, the compiler should be able to optimize the lightmap case to not resample the directional lightmap - // however it may not optimize the lightprobe case due to the proxy volume relying on dynamic if (to verify), not a problem for SH9, but a problem for proxy volume. - // TODO: optimize more this code. - builtinData.backBakeDiffuseLighting = SampleBakedGI(posInput, backNormalWS, builtinData.renderingLayers, texCoord1.xy, texCoord2.xy); #ifdef SHADOWS_SHADOWMASK float4 shadowMask = SampleShadowMask(posInput.positionWS, texCoord1.xy);