Skip to content

Commit 390aace

Browse files
elliomanEvergreen
authored andcommitted
[2023.3][URP] Fixing an issue with shadow keywords for transparent objects (UUM-64416)
Fixes UUM-64416.
1 parent c693b76 commit 390aace

File tree

9 files changed

+7998
-24
lines changed

9 files changed

+7998
-24
lines changed

Packages/com.unity.render-pipelines.universal/Runtime/Passes/AdditionalLightsShadowCasterPass.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ private static class AdditionalShadowsConstantBuffer
3434
const int k_ShadowmapBufferBits = 16;
3535
private int m_AdditionalLightsShadowmapID;
3636
internal RTHandle m_AdditionalLightsShadowmapHandle;
37-
internal RTHandle m_EmptyAdditionalLightShadowmapTexture;
37+
38+
private bool m_CreateEmptyShadowmap;
39+
private bool m_EmptyShadowmapNeedsClear = false;
40+
private RTHandle m_EmptyAdditionalLightShadowmapTexture;
41+
private const int k_EmptyShadowMapDimensions = 1;
42+
private const string k_EmptyShadowMapName = "_EmptyAdditionalLightShadowmapTexture";
43+
internal static Vector4[] s_EmptyAdditionalLightIndexToShadowParams = null;
3844

3945
float m_MaxShadowDistanceSq;
4046
float m_CascadeBorder;
@@ -49,9 +55,6 @@ private static class AdditionalShadowsConstantBuffer
4955
Vector4[] m_AdditionalLightIndexToShadowParams = null; // per-additional-light shadow info passed to the lighting shader (x: shadowStrength, y: softShadows, z: light type, w: perLightFirstShadowSliceIndex)
5056
Matrix4x4[] m_AdditionalLightShadowSliceIndexTo_WorldShadowMatrix = null; // per-shadow-slice info passed to the lighting shader
5157

52-
bool m_CreateEmptyShadowmap;
53-
bool m_EmptyShadowmapNeedsClear = false;
54-
5558
int renderTargetWidth;
5659
int renderTargetHeight;
5760

@@ -95,13 +98,17 @@ public AdditionalLightsShadowCasterPass(RenderPassEvent evt)
9598
m_VisibleLightIndexToAdditionalLightIndex = new int[maxVisibleLights];
9699
m_AdditionalLightIndexToShadowParams = new Vector4[maxAdditionalLightShadowParams];
97100

101+
s_EmptyAdditionalLightIndexToShadowParams = new Vector4[maxAdditionalLightShadowParams];
102+
for (int i = 0; i < s_EmptyAdditionalLightIndexToShadowParams.Length; i++)
103+
s_EmptyAdditionalLightIndexToShadowParams[i] = c_DefaultShadowParams;
104+
98105
if (!m_UseStructuredBuffer)
99106
{
100107
// Uniform buffers are faster on some platforms, but they have stricter size limitations
101108
m_AdditionalLightShadowSliceIndexTo_WorldShadowMatrix = new Matrix4x4[maxVisibleAdditionalLights];
102109
}
103110

104-
m_EmptyAdditionalLightShadowmapTexture = ShadowUtils.AllocShadowRT(1, 1, k_ShadowmapBufferBits, 1, 0, name: "_EmptyAdditionalLightShadowmapTexture");
111+
m_EmptyAdditionalLightShadowmapTexture = ShadowUtils.AllocShadowRT(k_EmptyShadowMapDimensions, k_EmptyShadowMapDimensions, k_ShadowmapBufferBits, 1, 0, name: k_EmptyShadowMapName);
105112
m_EmptyShadowmapNeedsClear = true;
106113
}
107114

@@ -601,7 +608,7 @@ bool SetupForEmptyRendering(bool stripShadowsOffVariants, UniversalShadowData sh
601608
useNativeRenderPass = false;
602609

603610
// Required for scene view camera(URP renderer not initialized)
604-
if(ShadowUtils.ShadowRTReAllocateIfNeeded(ref m_EmptyAdditionalLightShadowmapTexture, 1, 1, k_ShadowmapBufferBits, name: "_EmptyAdditionalLightShadowmapTexture"))
611+
if (ShadowUtils.ShadowRTReAllocateIfNeeded(ref m_EmptyAdditionalLightShadowmapTexture, k_EmptyShadowMapDimensions, k_EmptyShadowMapDimensions, k_ShadowmapBufferBits, name: k_EmptyShadowMapName))
605612
m_EmptyShadowmapNeedsClear = true;
606613

607614
// initialize _AdditionalShadowParams
@@ -678,17 +685,21 @@ void Clear()
678685

679686
void SetEmptyAdditionalShadowmapAtlas(RasterCommandBuffer cmd)
680687
{
681-
cmd.SetKeyword(ShaderGlobalKeywords.AdditionalLightShadows, true);
688+
cmd.EnableKeyword(ShaderGlobalKeywords.AdditionalLightShadows);
689+
SetEmptyAdditionalLightShadowParams(cmd, m_AdditionalLightIndexToShadowParams);
690+
}
682691

692+
internal static void SetEmptyAdditionalLightShadowParams(RasterCommandBuffer cmd, Vector4[] lightIndexToShadowParams)
693+
{
683694
if (RenderingUtils.useStructuredBuffer)
684695
{
685-
var shadowParamsBuffer = ShaderData.instance.GetAdditionalLightShadowParamsStructuredBuffer(m_AdditionalLightIndexToShadowParams.Length);
686-
shadowParamsBuffer.SetData(m_AdditionalLightIndexToShadowParams);
696+
ComputeBuffer shadowParamsBuffer = ShaderData.instance.GetAdditionalLightShadowParamsStructuredBuffer(lightIndexToShadowParams.Length);
697+
shadowParamsBuffer.SetData(lightIndexToShadowParams);
687698
cmd.SetGlobalBuffer(m_AdditionalShadowParams_SSBO, shadowParamsBuffer);
688699
}
689700
else
690701
{
691-
cmd.SetGlobalVectorArray(AdditionalShadowsConstantBuffer._AdditionalShadowParams, m_AdditionalLightIndexToShadowParams);
702+
cmd.SetGlobalVectorArray(AdditionalShadowsConstantBuffer._AdditionalShadowParams, lightIndexToShadowParams);
692703
}
693704
}
694705

Packages/com.unity.render-pipelines.universal/Runtime/Passes/MainLightShadowCasterPass.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ private static class MainLightShadowConstantBuffer
3232

3333
int m_MainLightShadowmapID;
3434
internal RTHandle m_MainLightShadowmapTexture;
35-
internal RTHandle m_EmptyLightShadowmapTexture;
35+
private RTHandle m_EmptyLightShadowmapTexture;
36+
private const int k_EmptyShadowMapDimensions = 1;
37+
private const string k_EmptyShadowMapName = "_EmptyLightShadowmapTexture";
38+
private static readonly Vector4 s_EmptyShadowParams = new Vector4(1, 0, 1, 0);
39+
private static readonly Vector4 s_EmptyShadowmapSize = s_EmptyShadowmapSize = new Vector4(k_EmptyShadowMapDimensions, 1f / k_EmptyShadowMapDimensions, k_EmptyShadowMapDimensions, k_EmptyShadowMapDimensions);
3640

3741
Matrix4x4[] m_MainLightShadowMatrices;
3842
ShadowSliceData[] m_CascadeSlices;
@@ -74,7 +78,7 @@ public MainLightShadowCasterPass(RenderPassEvent evt)
7478

7579
m_MainLightShadowmapID = Shader.PropertyToID("_MainLightShadowmapTexture");
7680

77-
m_EmptyLightShadowmapTexture = ShadowUtils.AllocShadowRT(1, 1, k_ShadowmapBufferBits, 1, 0, name: "_EmptyLightShadowmapTexture");
81+
m_EmptyLightShadowmapTexture = ShadowUtils.AllocShadowRT(k_EmptyShadowMapDimensions, k_EmptyShadowMapDimensions, k_ShadowmapBufferBits, 1, 0, name: k_EmptyShadowMapName);
7882
m_EmptyShadowmapNeedsClear = true;
7983
}
8084

@@ -176,7 +180,7 @@ bool SetupForEmptyRendering(bool stripShadowsOffVariants)
176180
useNativeRenderPass = false;
177181

178182
// Required for scene view camera(URP renderer not initialized)
179-
if(ShadowUtils.ShadowRTReAllocateIfNeeded(ref m_EmptyLightShadowmapTexture, 1, 1, k_ShadowmapBufferBits, name: "_EmptyLightShadowmapTexture"))
183+
if(ShadowUtils.ShadowRTReAllocateIfNeeded(ref m_EmptyLightShadowmapTexture, k_EmptyShadowMapDimensions, k_EmptyShadowMapDimensions, k_ShadowmapBufferBits, name: k_EmptyShadowMapName))
180184
m_EmptyShadowmapNeedsClear = true;
181185

182186
return true;
@@ -242,11 +246,14 @@ void Clear()
242246

243247
void SetEmptyMainLightCascadeShadowmap(RasterCommandBuffer cmd)
244248
{
245-
cmd.SetKeyword(ShaderGlobalKeywords.MainLightShadows, true);
246-
cmd.SetGlobalVector(MainLightShadowConstantBuffer._ShadowParams,
247-
new Vector4(1, 0, 1, 0));
248-
cmd.SetGlobalVector(MainLightShadowConstantBuffer._ShadowmapSize,
249-
new Vector4(1f / m_EmptyLightShadowmapTexture.rt.width, 1f / m_EmptyLightShadowmapTexture.rt.height, m_EmptyLightShadowmapTexture.rt.width, m_EmptyLightShadowmapTexture.rt.height));
249+
cmd.EnableKeyword(ShaderGlobalKeywords.MainLightShadows);
250+
SetEmptyMainLightShadowParams(cmd);
251+
}
252+
253+
internal static void SetEmptyMainLightShadowParams(RasterCommandBuffer cmd)
254+
{
255+
cmd.SetGlobalVector(MainLightShadowConstantBuffer._ShadowParams, s_EmptyShadowParams);
256+
cmd.SetGlobalVector(MainLightShadowConstantBuffer._ShadowmapSize, s_EmptyShadowmapSize);
250257
}
251258

252259
void RenderMainLightCascadeShadowmap(RasterCommandBuffer cmd, ref PassData data, bool isRenderGraph)
@@ -448,7 +455,6 @@ internal TextureHandle Render(RenderGraph graph, ContextContainer frameData)
448455
builder.AllowPassCulling(false);
449456
builder.AllowGlobalStateModification(true);
450457

451-
452458
if (shadowTexture.IsValid())
453459
builder.SetGlobalTextureAfterPass(shadowTexture, m_MainLightShadowmapID);
454460

Packages/com.unity.render-pipelines.universal/Runtime/Passes/TransparentSettingsPass.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System;
2-
using UnityEngine.Experimental.Rendering;
2+
using UnityEngine.Rendering.Universal.Internal;
33

44
namespace UnityEngine.Rendering.Universal
55
{
@@ -40,10 +40,10 @@ public static void ExecutePass(RasterCommandBuffer cmd, bool shouldReceiveShadow
4040
{
4141
using (new ProfilingScope(cmd, m_ProfilingSampler))
4242
{
43-
// Toggle light shadows enabled based on the renderer setting set in the constructor
44-
cmd.SetKeyword(ShaderGlobalKeywords.MainLightShadows, shouldReceiveShadows);
45-
cmd.SetKeyword(ShaderGlobalKeywords.MainLightShadowCascades, shouldReceiveShadows);
46-
cmd.SetKeyword(ShaderGlobalKeywords.AdditionalLightShadows, shouldReceiveShadows);
43+
// This pass is only used when transparent objects should not
44+
// receive shadows using the setting on the URP Renderer.
45+
MainLightShadowCasterPass.SetEmptyMainLightShadowParams(cmd);
46+
AdditionalLightsShadowCasterPass.SetEmptyAdditionalLightShadowParams(cmd, AdditionalLightsShadowCasterPass.s_EmptyAdditionalLightIndexToShadowParams);
4747
}
4848
}
4949
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_GameObject: {fileID: 0}
10+
m_Enabled: 1
11+
m_EditorHideFlags: 0
12+
m_Script: {fileID: 11500000, guid: de640fe3d0db1804a85f9fc8f5cadab6, type: 3}
13+
m_Name: ForwardRenderer_TransparentShadowsOff
14+
m_EditorClassIdentifier:
15+
debugShaders:
16+
debugReplacementPS: {fileID: 4800000, guid: cf852408f2e174538bcd9b7fda1c5ae7,
17+
type: 3}
18+
hdrDebugViewPS: {fileID: 4800000, guid: 573620ae32aec764abd4d728906d2587, type: 3}
19+
probeVolumeSamplingDebugComputeShader: {fileID: 7200000, guid: 53626a513ea68ce47b59dc1299fe3959,
20+
type: 3}
21+
probeVolumeResources:
22+
probeVolumeDebugShader: {fileID: 4800000, guid: e5c6678ed2aaa91408dd3df699057aae,
23+
type: 3}
24+
probeVolumeFragmentationDebugShader: {fileID: 4800000, guid: 03cfc4915c15d504a9ed85ecc404e607,
25+
type: 3}
26+
probeVolumeOffsetDebugShader: {fileID: 4800000, guid: 53a11f4ebaebf4049b3638ef78dc9664,
27+
type: 3}
28+
probeVolumeSamplingDebugShader: {fileID: 4800000, guid: 8f96cd657dc40064aa21efcc7e50a2e7,
29+
type: 3}
30+
probeSamplingDebugMesh: {fileID: -3555484719484374845, guid: 57d7c4c16e2765b47a4d2069b311bffe,
31+
type: 3}
32+
probeSamplingDebugTexture: {fileID: 2800000, guid: 24ec0e140fb444a44ab96ee80844e18e,
33+
type: 3}
34+
probeVolumeBlendStatesCS: {fileID: 0}
35+
m_RendererFeatures: []
36+
m_RendererFeatureMap:
37+
m_UseNativeRenderPass: 0
38+
xrSystemData: {fileID: 11400000, guid: 60e1133243b97e347b653163a8c01b64, type: 2}
39+
postProcessData: {fileID: 0}
40+
m_AssetVersion: 2
41+
m_OpaqueLayerMask:
42+
serializedVersion: 2
43+
m_Bits: 4294967295
44+
m_TransparentLayerMask:
45+
serializedVersion: 2
46+
m_Bits: 4294967295
47+
m_DefaultStencilState:
48+
overrideStencilState: 0
49+
stencilReference: 0
50+
stencilCompareFunction: 8
51+
passOperation: 2
52+
failOperation: 0
53+
zFailOperation: 0
54+
m_ShadowTransparentReceive: 0
55+
m_RenderingMode: 0
56+
m_DepthPrimingMode: 0
57+
m_CopyDepthMode: 1
58+
m_AccurateGbufferNormals: 0
59+
m_IntermediateTextureMode: 0

Tests/SRPTests/Projects/UniversalGraphicsTest_Foundation/Assets/CommonAssets/ForwardRenderer_TransparentShadowsOff.asset.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tests/SRPTests/Projects/UniversalGraphicsTest_Foundation/Assets/CommonAssets/URPAssets/SoftShadows.asset

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ MonoBehaviour:
1818
m_RendererData: {fileID: 0}
1919
m_RendererDataList:
2020
- {fileID: 11400000, guid: f59607d3476b54858a594ea904187fb5, type: 2}
21+
- {fileID: 11400000, guid: e8524593a16fffa47986908e0f2cd022, type: 2}
2122
m_DefaultRendererIndex: 0
2223
m_RequireDepthTexture: 1
2324
m_RequireOpaqueTexture: 1

0 commit comments

Comments
 (0)