-
Notifications
You must be signed in to change notification settings - Fork 866
Expand file tree
/
Copy pathRenderGraphDebugSession.cs
More file actions
211 lines (173 loc) · 7.37 KB
/
RenderGraphDebugSession.cs
File metadata and controls
211 lines (173 loc) · 7.37 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
using System;
using System.Collections.Generic;
using static UnityEngine.Rendering.RenderGraphModule.RenderGraph;
namespace UnityEngine.Rendering.RenderGraphModule
{
internal abstract class RenderGraphDebugSession : IDisposable
{
protected class DebugDataContainer
{
readonly Dictionary<string, Dictionary<EntityId, DebugData>> m_Container = new();
public bool AddGraph(string graphName)
{
if (m_Container.ContainsKey(graphName))
return false;
m_Container.Add(graphName, new Dictionary<EntityId, DebugData>());
return true;
}
public bool RemoveGraph(string graphName)
{
return m_Container.Remove(graphName);
}
public bool AddExecution(string graphName, EntityId executionId, string executionName)
{
Debug.Assert(m_Container.ContainsKey(graphName));
if (m_Container[graphName].ContainsKey(executionId))
return false;
m_Container[graphName][executionId] = new DebugData(executionName);
return true;
}
public List<string> GetRenderGraphs() => new(m_Container.Keys);
public List<DebugExecutionItem> GetExecutions(string graphName)
{
var executions = new List<DebugExecutionItem>();
if (!string.IsNullOrEmpty(graphName) && m_Container.TryGetValue(graphName, out var executionsDict))
{
foreach (var (executionId, debugData) in executionsDict)
{
var item = new DebugExecutionItem(executionId, debugData.executionName);
executions.Add(item);
}
}
return executions;
}
public DebugData GetDebugData(string renderGraph, EntityId executionId)
{
if (!m_Container.TryGetValue(renderGraph, out var debugDataForGraph))
throw new InvalidOperationException();
return debugDataForGraph[executionId];
}
public void SetDebugData(string renderGraph, EntityId executionId, DebugData data)
{
if (m_Container.TryGetValue(renderGraph, out var debugDataForGraph))
debugDataForGraph[executionId] = data;
}
public void DeleteExecutionIds(string renderGraph, List<EntityId> executionIds)
{
if (m_Container.TryGetValue(renderGraph, out var debugDataForGraph))
{
foreach (var executionId in executionIds)
debugDataForGraph.Remove(executionId);
}
}
public void Clear()
{
m_Container.Clear();
}
public void Invalidate()
{
foreach (var (graph, dict) in m_Container)
{
foreach (var (cam, debugData) in dict)
debugData.Clear();
}
}
}
// Session is considered active when it is collecting debug data
public abstract bool isActive { get; }
DebugDataContainer debugDataContainer { get; }
protected RenderGraphDebugSession()
{
debugDataContainer = new DebugDataContainer();
onGraphRegistered += RegisterGraph;
onGraphUnregistered += UnregisterGraph;
onExecutionRegistered += RegisterExecution;
}
protected void RegisterGraph(string graphName)
{
if (debugDataContainer.AddGraph(graphName))
onRegisteredGraphsChanged?.Invoke();
}
protected void UnregisterGraph(string graphName)
{
if (debugDataContainer.RemoveGraph(graphName))
onRegisteredGraphsChanged?.Invoke();
}
protected void RegisterExecution(string graphName, EntityId executionId, string executionName)
{
if (debugDataContainer.AddExecution(graphName, executionId, executionName))
onRegisteredGraphsChanged?.Invoke();
}
public virtual void Dispose()
{
onGraphRegistered -= RegisterGraph;
onGraphUnregistered -= UnregisterGraph;
onExecutionRegistered -= RegisterExecution;
debugDataContainer.Clear();
}
protected void InvalidateData()
{
debugDataContainer.Invalidate();
}
public static event Action onRegisteredGraphsChanged;
public static event Action<string, EntityId> onDebugDataUpdated;
static RenderGraphDebugSession s_CurrentDebugSession;
public static bool hasActiveDebugSession => s_CurrentDebugSession?.isActive ?? false;
public static RenderGraphDebugSession currentDebugSession => s_CurrentDebugSession;
public static void Create<TSession>() where TSession : RenderGraphDebugSession, new()
{
EndSession();
s_CurrentDebugSession = new TSession();
}
public static void EndSession()
{
if (s_CurrentDebugSession != null)
{
s_CurrentDebugSession.Dispose();
s_CurrentDebugSession = null;
}
}
public static List<string> s_EmptyRegisteredGraphs = new();
public static List<string> GetRegisteredGraphs()
{
if (s_CurrentDebugSession == null || s_CurrentDebugSession.debugDataContainer == null)
{
return s_EmptyRegisteredGraphs;
}
return s_CurrentDebugSession.debugDataContainer.GetRenderGraphs();
}
public static List<DebugExecutionItem> s_EmptyExecutions = new();
public static List<DebugExecutionItem> GetExecutions(string graphName)
{
if (s_CurrentDebugSession == null || s_CurrentDebugSession.debugDataContainer == null)
{
return s_EmptyExecutions;
}
return s_CurrentDebugSession.debugDataContainer.GetExecutions(graphName);
}
public static DebugData GetDebugData(string renderGraph, EntityId executionId)
{
return s_CurrentDebugSession.debugDataContainer.GetDebugData(renderGraph, executionId);
}
public static void SetDebugData(string renderGraph, EntityId executionId, DebugData data)
{
s_CurrentDebugSession.debugDataContainer.SetDebugData(renderGraph, executionId, data);
onDebugDataUpdated?.Invoke(renderGraph, executionId);
}
public static void DeleteExecutionIds(string renderGraph, List<EntityId> executionIds)
{
s_CurrentDebugSession.debugDataContainer.DeleteExecutionIds(renderGraph, executionIds);
onRegisteredGraphsChanged?.Invoke();
}
protected void RegisterAllLocallyKnownGraphsAndExecutions()
{
var registeredExecutions = GetRegisteredExecutions();
foreach (var (graph, executions) in registeredExecutions)
{
RegisterGraph(graph.name);
foreach (var executionItem in executions)
RegisterExecution(graph.name, executionItem.id, executionItem.name);
}
}
}
}