-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathFlattenRenderGraph.cpp
302 lines (265 loc) · 13.3 KB
/
FlattenRenderGraph.cpp
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
///////////////////////////////////////////////////////////////////////////////
// Gigi Rapid Graphics Prototyping and Code Generation Suite //
// Copyright (c) 2024 Electronic Arts Inc. All rights reserved. //
///////////////////////////////////////////////////////////////////////////////
#include "FlattenRenderGraph.h"
#include "Schemas/Types.h"
#include <vector>
#include "GigiCompilerLib/Backends/Shared.h"
static float CalculateRenderGraphScore(const RenderGraph& renderGraph)
{
size_t ret = 0;
for (const auto& it : renderGraph.transitions)
ret += it.transitions.size();
return (float)ret;
}
static void CalculateResourceTransitions(RenderGraph& renderGraph)
{
// Initialize the states of every node (we only care about resource nodes but this is easier to program)
// Leave an extra slot at the beginning for starting states.
std::vector<std::vector<ShaderResourceAccessType>> resourceStatesAtEachStep(renderGraph.nodes.size());
for (auto& states : resourceStatesAtEachStep)
states.resize(renderGraph.nodes.size(), ShaderResourceAccessType::Count);
// Set the state of each resource at each step of the render graph
for (int stepIndex = 0; stepIndex < renderGraph.flattenedNodeList.size(); ++stepIndex)
{
auto& states = resourceStatesAtEachStep[stepIndex];
int nodeIndex = renderGraph.flattenedNodeList[stepIndex];
RenderGraphNode& node = renderGraph.nodes[nodeIndex];
int pinCount = GetNodePinCount(node);
for (int pinIndex = 0; pinIndex < pinCount; ++pinIndex)
{
InputNodeInfo pinInfo = GetNodePinInputNodeInfo(node, pinIndex);
int resourceNodeIndex = GetResourceNodeForPin(renderGraph, node, pinIndex);
if (resourceNodeIndex != -1)
{
ShaderResourceType resourceType = ShaderResourceType::Count;
switch (renderGraph.nodes[resourceNodeIndex]._index)
{
case RenderGraphNode::c_index_resourceBuffer: resourceType = ShaderResourceType::Buffer; break;
case RenderGraphNode::c_index_resourceShaderConstants: resourceType = ShaderResourceType::ConstantBuffer; break;
case RenderGraphNode::c_index_resourceTexture: resourceType = ShaderResourceType::Texture; break;
default:
{
Assert(false, "Unhandled render graph node type");
break;
}
}
states[resourceNodeIndex] = pinInfo.access;
AddResourceNodeAccessedAs(renderGraph.nodes[resourceNodeIndex], pinInfo.access);
AddResourceDependency(node, pinIndex, resourceNodeIndex, resourceType, pinInfo.access);
}
}
}
// set the final states on the resource nodes. Useful for creating textures in these states.
// also set the starting states on the resource nodes. Useful for transitioning imported textures to this state before first use
for (size_t nodeIndex = 0; nodeIndex < renderGraph.nodes.size(); ++nodeIndex)
{
if (!GetNodeIsResourceNode(renderGraph.nodes[nodeIndex]))
continue;
for (const auto& states : resourceStatesAtEachStep)
{
if (states[nodeIndex] != ShaderResourceAccessType::Count)
{
SetResourceNodeStartingState(renderGraph.nodes[nodeIndex], states[nodeIndex]);
break;
}
}
for (const auto& states : resourceStatesAtEachStep)
{
if (states[nodeIndex] != ShaderResourceAccessType::Count)
SetResourceNodeFinalState(renderGraph.nodes[nodeIndex], states[nodeIndex]);
}
}
// make the transitions, with the states starting at whatever their ending state is (aka previous frame ending state)
std::vector<ShaderResourceAccessType> lastSetStates(renderGraph.nodes.size());
for (int stepIndex = 0; stepIndex < renderGraph.flattenedNodeList.size(); ++stepIndex)
{
int nodeIndex = renderGraph.flattenedNodeList[stepIndex];
if (!GetNodeIsResourceNode(renderGraph.nodes[nodeIndex]))
continue;
lastSetStates[nodeIndex] = GetResourceNodeFinalState(renderGraph.nodes[nodeIndex]);
}
std::vector<bool> firstTransition(renderGraph.nodes.size(), true);
renderGraph.transitions.resize(renderGraph.nodes.size());
for (int stepIndex = 0; stepIndex < renderGraph.flattenedNodeList.size(); ++stepIndex)
{
int nodeIndex = renderGraph.flattenedNodeList[stepIndex];
// skip nodes that are resources
if (GetNodeIsResourceNode(renderGraph.nodes[nodeIndex]))
continue;
// get our states required at this step
const auto& states = resourceStatesAtEachStep[stepIndex];
// make transitions for any resources that want them
for (size_t resourceIndex = 0; resourceIndex < renderGraph.nodes.size(); ++resourceIndex)
{
// skip nodes that arent resources
if (!GetNodeIsResourceNode(renderGraph.nodes[resourceIndex]))
continue;
ShaderResourceAccessType lastState = lastSetStates[resourceIndex];
ShaderResourceAccessType nextState = states[resourceIndex];
// skip this resource node if it isn't referenced by this step
if (nextState == ShaderResourceAccessType::Count)
continue;
// if the state changed, or it goes from uavrw to uavrw, we need to emit a transition
if ((lastState != nextState) || lastState == ShaderResourceAccessType::UAV)
{
// imported resources should skip their first transition, because we explicitly set them to the right state after importing
if (GetNodeResourceVisibility(renderGraph.nodes[resourceIndex]) != ResourceVisibility::Imported || !firstTransition[resourceIndex])
{
ResourceTransition newTransition;
newTransition.nodeIndex = (int)resourceIndex;
newTransition.oldState = lastState;
newTransition.newState = nextState;
renderGraph.transitions[stepIndex].transitions.push_back(newTransition);
}
else
{
firstTransition[resourceIndex] = false;
}
lastSetStates[resourceIndex] = nextState;
}
}
}
}
void OptimizeAndFlattenRenderGraph(RenderGraph& renderGraph)
{
struct DAGNode
{
int nodeIndex;
std::vector<int> dependentOn;
};
struct DAG
{
std::vector<int> flattenedList;
std::vector<DAGNode> dag;
};
// build the render graph DAG
DAG rgdag;
rgdag.dag.resize(renderGraph.nodes.size());
for (int index = 0; index < (int)renderGraph.nodes.size(); ++index)
{
rgdag.dag[index].nodeIndex = index;
// skip resource nodes
if (GetNodeIsResourceNode(renderGraph.nodes[index]))
continue;
int pinCount = GetNodePinCount(renderGraph.nodes[index]);
// Get the input dependencies of the node
for (int pinIndex = 0; pinIndex < pinCount; ++pinIndex)
{
// Get what we this pin is connected to
InputNodeInfo connectionInfo = GetNodePinInputNodeInfo(renderGraph.nodes[index], pinIndex);
if (connectionInfo.nodeIndex == -1)
{
if (connectionInfo.required)
Assert(false, "A pin is missing a connection: %s %s", GetNodeName(renderGraph.nodes[index]).c_str(), GetNodePinName(renderGraph.nodes[index], pinIndex).c_str());
continue;
}
rgdag.dag[index].dependentOn.push_back(connectionInfo.nodeIndex);
// TODO: need to sort out false dependency ordering problem from read only access
// TOOD: when fixed, make sure barrier nodes are honored
#if 0
// if this is not a read only access type, take it as is to keep the ordering constraint
// TODO: need to think about this. I think that we SHOULD allow this, but if multiple things want read access to the same thing before a write, it needs to copy it and make a temporary resource.
// TODO: we may actually need to disable this optimization until it's reworked, i don't think this fix is enough
if (!ShaderResourceTypeIsReadOnly(connectionInfo.access))
{
rgdag.dag[index].dependentOn.push_back(connectionInfo.nodeIndex);
continue;
}
// If what we are conncected to has read access to what is plugged into it, follow the path back
// to the source resource until you hit a write, or the resource node itself.
// This removes false ordering constraints in the DAG, allowing for more DAG parallelism
// which means there are more possible ways to reorder the nodes, to find orderings that
// score better in the optimization function and so are thus more optimized.
int parentNodeIndex = connectionInfo.nodeIndex;
int parentPinIndex = connectionInfo.pinIndex;
while (1)
{
// If we found a resource node, we are done.
if (GetNodeIsResourceNode(renderGraph.nodes[parentNodeIndex]))
{
rgdag.dag[index].dependentOn.push_back(parentNodeIndex);
break;
}
// if we found a non read only connection, the parent of this connection is the one to be dependent on.
Assert(parentPinIndex != -1, "Error");
InputNodeInfo inputNodeInfo = GetNodePinInputNodeInfo(renderGraph.nodes[parentNodeIndex], parentPinIndex);
Assert(inputNodeInfo.nodeIndex != -1, "Error");
if (!ShaderResourceTypeIsReadOnly(inputNodeInfo.access))
{
rgdag.dag[index].dependentOn.push_back(parentNodeIndex);
break;
}
// iterate to next connection
parentNodeIndex = inputNodeInfo.nodeIndex;
parentPinIndex = inputNodeInfo.pinIndex;
}
#endif
}
}
// Do topological sorting to flatten the DAG
// Process all possible orderings of the nodes in the DAG to find the best one
std::vector<DAG> processing;
processing.push_back(rgdag);
RenderGraph bestRenderGraph = renderGraph;
float bestRenderGraphScore = FLT_MAX;
while (!processing.empty())
{
// get the next partially flattened DAG to process
DAG dag = processing.back();
processing.pop_back();
// make a dag on the processing stack, taking each choice we have for the next item in the flattened list
for (size_t index = 0; index < dag.dag.size(); ++index)
{
if (!dag.dag[index].dependentOn.empty())
continue;
int chosenNodeIndex = dag.dag[index].nodeIndex;
// make the choice
// 1) Add it to the flattened list
// 2) Remove the node from the dag
// 3) Clear any dependencies on this node index
DAG newDag = dag;
newDag.flattenedList.push_back(chosenNodeIndex);
newDag.dag.erase(newDag.dag.begin() + index);
for (DAGNode& dagNode : newDag.dag)
dagNode.dependentOn.erase(std::remove(dagNode.dependentOn.begin(), dagNode.dependentOn.end(), chosenNodeIndex), dagNode.dependentOn.end());
// if there is more processing to do, add it to the queue
if (!newDag.dag.empty())
{
processing.push_back(newDag);
}
// else we are done so consider keeping it
else
{
// reverse the order of the flattened lists since we built it backwards
std::reverse(newDag.dag.begin(), newDag.dag.end());
// score this proposed flattened render graph
RenderGraph candidate = renderGraph;
candidate.flattenedNodeList = newDag.flattenedList;
// Remove all barrier nodes from the list now that the render graph is flattened.
// We don't want it adding extra resource transitions, and it is a no-op at runtime.
candidate.flattenedNodeList.erase(
std::remove_if(candidate.flattenedNodeList.begin(), candidate.flattenedNodeList.end(),
[&candidate] (int nodeIndex)
{
return candidate.nodes[nodeIndex]._index == RenderGraphNode::c_index_actionBarrier;
}
),
candidate.flattenedNodeList.end()
);
// calculate the render graph score and keep it if it's the best we've seen so far
CalculateResourceTransitions(candidate);
float candidateScore = CalculateRenderGraphScore(candidate);
if (candidateScore < bestRenderGraphScore)
{
bestRenderGraphScore = candidateScore;
bestRenderGraph = candidate;
}
}
break;
}
}
// Keep the best render graph we found
renderGraph = bestRenderGraph;
}