-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathDrawRenderer2DPass.cs
More file actions
225 lines (191 loc) · 10.2 KB
/
Copy pathDrawRenderer2DPass.cs
File metadata and controls
225 lines (191 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System;
using System.Collections.Generic;
using UnityEngine.Rendering.RenderGraphModule;
using CommonResourceData = UnityEngine.Rendering.Universal.UniversalResourceData;
namespace UnityEngine.Rendering.Universal
{
internal class DrawRenderer2DPass : ScriptableRenderPass
{
static readonly string k_RenderPass = "Renderer2D Pass";
static readonly string k_SetLightBlendTexture = "SetLightBlendTextures";
private static readonly ProfilingSampler m_ProfilingSampler = new ProfilingSampler(k_RenderPass);
private static readonly ProfilingSampler m_SetLightBlendTextureProfilingSampler = new ProfilingSampler(k_SetLightBlendTexture);
private static readonly ShaderTagId k_CombinedRenderingPassName = new ShaderTagId("Universal2D");
private static readonly ShaderTagId k_LegacyPassName = new ShaderTagId("SRPDefaultUnlit");
private static readonly List<ShaderTagId> k_ShaderTags =
new List<ShaderTagId>() {k_LegacyPassName, k_CombinedRenderingPassName};
private static readonly int k_HDREmulationScaleID = Shader.PropertyToID("_HDREmulationScale");
private static readonly int k_RendererColorID = Shader.PropertyToID("_RendererColor");
#if URP_COMPATIBILITY_MODE
[Obsolete(DeprecationMessage.CompatibilityScriptingAPIObsoleteFrom2023_3)]
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
throw new NotImplementedException();
}
#endif
private static void Execute(RasterGraphContext context, PassData passData)
{
var cmd = context.cmd;
var blendStylesCount = passData.blendStyleIndices.Length;
cmd.SetGlobalFloat(k_HDREmulationScaleID, passData.hdrEmulationScale);
cmd.SetGlobalColor(k_RendererColorID, Color.white);
RendererLighting.SetLightShaderGlobals(cmd, passData.lightBlendStyles, passData.blendStyleIndices);
#if UNITY_EDITOR
if (passData.isLitView)
#endif
{
if (passData.layerUseLights)
{
for (var i = 0; i < blendStylesCount; i++)
{
var blendStyleIndex = passData.blendStyleIndices[i];
RendererLighting.EnableBlendStyle(cmd, blendStyleIndex, true);
}
}
else if (passData.isSceneLit)
{
RendererLighting.EnableBlendStyle(cmd, 0, true);
}
}
if (passData.activeDebugHandler)
{
passData.debugRendererLists.DrawWithRendererList(cmd);
}
else
{
// Draw all renderers in layer batch
cmd.DrawRendererList(passData.rendererList);
}
RendererLighting.DisableAllKeywords(cmd);
}
class SetGlobalPassData
{
internal TextureHandle[] lightTextures;
}
class PassData
{
internal Light2DBlendStyle[] lightBlendStyles;
internal int[] blendStyleIndices;
internal float hdrEmulationScale;
internal bool isSceneLit;
internal bool layerUseLights;
internal TextureHandle[] lightTextures;
internal RendererListHandle rendererList;
internal DebugRendererLists debugRendererLists;
internal bool activeDebugHandler;
#if UNITY_EDITOR
internal bool isLitView; // Required for prefab view and preview camera
#endif
}
public void Render(RenderGraph graph, ContextContainer frameData, Renderer2DData rendererData, ref LayerBatch[] layerBatches, int batchIndex, ref FilteringSettings filterSettings)
{
UniversalRenderingData renderingData = frameData.Get<UniversalRenderingData>();
UniversalCameraData cameraData = frameData.Get<UniversalCameraData>();
UniversalLightData lightData = frameData.Get<UniversalLightData>();
Universal2DResourceData universal2DResourceData = frameData.Get<Universal2DResourceData>();
CommonResourceData commonResourceData = frameData.Get<CommonResourceData>();
var layerBatch = layerBatches[batchIndex];
bool isLitView = true;
#if UNITY_EDITOR
// Early out for prefabs
if (cameraData.isSceneViewCamera && UnityEditor.SceneView.currentDrawingSceneView != null)
isLitView = UnityEditor.SceneView.currentDrawingSceneView.sceneLighting;
// Early out for preview camera
if (cameraData.cameraType == CameraType.Preview)
isLitView = false;
DebugHandler debugHandler = GetActiveDebugHandler(cameraData);
if (debugHandler != null)
isLitView = debugHandler.IsLightingActive;
#endif
// Preset global light textures for first batch
if (batchIndex == 0)
{
using (var builder = graph.AddRasterRenderPass<SetGlobalPassData>(k_SetLightBlendTexture, out var passData, m_SetLightBlendTextureProfilingSampler))
{
if (layerBatch.lightStats.useLights && isLitView)
{
passData.lightTextures = universal2DResourceData.lightTextures[batchIndex];
for (var i = 0; i < passData.lightTextures.Length; i++)
builder.UseTexture(passData.lightTextures[i]);
}
SetGlobalLightTextures(graph, builder, passData.lightTextures, ref layerBatch, rendererData, isLitView);
builder.AllowGlobalStateModification(true);
builder.SetRenderFunc((SetGlobalPassData data, RasterGraphContext context) =>
{
});
}
}
// Renderer Pass
using (var builder = graph.AddRasterRenderPass<PassData>(k_RenderPass, out var passData, m_ProfilingSampler))
{
passData.lightBlendStyles = rendererData.lightBlendStyles;
passData.blendStyleIndices = layerBatch.activeBlendStylesIndices;
passData.hdrEmulationScale = rendererData.hdrEmulationScale;
passData.isSceneLit = rendererData.lightCullResult.IsSceneLit();
passData.layerUseLights = layerBatch.lightStats.useLights;
#if UNITY_EDITOR
passData.isLitView = isLitView;
#endif
var drawSettings = CreateDrawingSettings(k_ShaderTags, renderingData, cameraData, lightData, SortingCriteria.CommonTransparent);
var sortSettings = drawSettings.sortingSettings;
RendererLighting.GetTransparencySortingMode(rendererData, cameraData.camera, ref sortSettings);
drawSettings.sortingSettings = sortSettings;
var activeDebugHandler = GetActiveDebugHandler(cameraData);
passData.activeDebugHandler = activeDebugHandler != null;
if (activeDebugHandler != null)
{
var renderStateBlock = new RenderStateBlock(RenderStateMask.Nothing);
passData.debugRendererLists = activeDebugHandler.CreateRendererListsWithDebugRenderState(graph,
ref renderingData.cullResults, ref drawSettings, ref filterSettings, ref renderStateBlock);
passData.debugRendererLists.PrepareRendererListForRasterPass(builder);
}
else
{
var param = new RendererListParams(renderingData.cullResults, drawSettings, filterSettings);
passData.rendererList = graph.CreateRendererList(param);
builder.UseRendererList(passData.rendererList);
}
if (passData.layerUseLights && isLitView)
{
passData.lightTextures = universal2DResourceData.lightTextures[batchIndex];
for (var i = 0; i < passData.lightTextures.Length; i++)
builder.UseTexture(passData.lightTextures[i]);
}
if (rendererData.useCameraSortingLayerTexture)
builder.UseTexture(universal2DResourceData.cameraSortingLayerTexture);
// Set color and depth attachments
builder.SetRenderAttachment(commonResourceData.activeColorTexture, 0);
if (Renderer2D.IsDepthUsageAllowed(frameData, rendererData))
builder.SetRenderAttachmentDepth(commonResourceData.activeDepthTexture);
builder.AllowGlobalStateModification(true);
// Post set global light textures for next renderer pass
var nextBatch = batchIndex + 1;
if (nextBatch < universal2DResourceData.lightTextures.Length)
SetGlobalLightTextures(graph, builder, universal2DResourceData.lightTextures[nextBatch], ref layerBatches[nextBatch], rendererData, isLitView);
builder.SetRenderFunc((PassData data, RasterGraphContext context) =>
{
Execute(context, data);
});
}
}
void SetGlobalLightTextures(RenderGraph graph, IRasterRenderGraphBuilder builder, TextureHandle[] lightTextures, ref LayerBatch layerBatch, Renderer2DData rendererData, bool isLitView)
{
if (isLitView)
{
if (layerBatch.lightStats.useLights)
{
for (var i = 0; i < lightTextures.Length; i++)
{
var blendStyleIndex = layerBatch.activeBlendStylesIndices[i];
builder.SetGlobalTextureAfterPass(lightTextures[i], Shader.PropertyToID(RendererLighting.k_ShapeLightTextureIDs[blendStyleIndex]));
}
}
else if (rendererData.lightCullResult.IsSceneLit())
{
for (var i = 0; i < RendererLighting.k_ShapeLightTextureIDs.Length; i++)
builder.SetGlobalTextureAfterPass(graph.defaultResources.blackTexture, Shader.PropertyToID(RendererLighting.k_ShapeLightTextureIDs[i]));
}
}
}
}
}