forked from StrandedKitty/streets-gl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenderGraph.ts
207 lines (156 loc) · 5.28 KB
/
RenderGraph.ts
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
import Pass, {InternalResourceType} from "./Pass";
import Resource from "./Resource";
import Node from "./Node";
import {Queue} from "./Utils";
import ResourcePool from "./PhysicalResourcePool";
export default class RenderGraph {
private readonly resourcePool: ResourcePool;
public passes: Set<Pass<any>> = new Set();
public lastGraph: Set<Node> = null;
public lastSortedPassList: Pass<any>[] = null;
public indegreeSets: Map<Node, Set<Node>> = new Map();
public outdegreeSets: Map<Node, Set<Node>> = new Map();
private nextNodes: Map<Node, Set<Node>> = new Map();
private previousNodes: Map<Node, Set<Node>> = new Map();
public constructor(resourcePool: ResourcePool = new ResourcePool(2)) {
this.resourcePool = resourcePool;
}
public addPass(pass: Pass<any>): void {
this.passes.add(pass);
}
private sortRenderableNodes(nodes: Set<Node>): Node[] {
// Kahn's algorithm
const queue = new Queue<Node>();
for (const node of nodes) {
if (this.indegreeSets.get(node).size === 0) {
queue.push(node);
}
}
let visitedCount = 0;
const graphNodeCount = nodes.size;
const topOrder: Node[] = [];
while (!queue.isEmpty()) {
const node = queue.pop();
if (node.isRenderable) {
topOrder.push(node);
}
for (const adjacentNode of this.outdegreeSets.get(node)) {
const adjacentIndegreeSet = this.indegreeSets.get(adjacentNode);
adjacentIndegreeSet.delete(node);
if (adjacentIndegreeSet.size === 0) {
queue.push(adjacentNode);
}
}
++visitedCount;
}
if (visitedCount !== graphNodeCount) {
throw new Error('Render graph has a cycle');
}
return topOrder;
}
private getResourcesUsedExternally(passes: Set<Pass<any>>): Set<Resource<any, any>> {
const result: Set<Resource<any, any>> = new Set();
for (const pass of passes) {
const resources = pass.getOutputResourcesUsedExternally();
for (const resource of resources) {
result.add(resource);
}
}
return result;
}
private buildGraphWithCulling(passes: Set<Pass<any>>): Set<Node> {
const nodes: Node[] = Array.from(this.getResourcesUsedExternally(passes));
const graph: Set<Node> = new Set();
this.indegreeSets.clear();
this.outdegreeSets.clear();
for (const node of nodes) {
this.indegreeSets.set(node, new Set());
this.outdegreeSets.set(node, new Set());
graph.add(node);
}
while (nodes.length > 0) {
const node = nodes.shift();
for (const prevNode of this.previousNodes.get(node)) {
if (!graph.has(prevNode)) {
this.indegreeSets.set(prevNode, new Set());
this.outdegreeSets.set(prevNode, new Set());
graph.add(prevNode);
nodes.push(prevNode);
}
this.indegreeSets.get(node).add(prevNode);
this.outdegreeSets.get(prevNode).add(node);
}
}
return graph;
}
private updateAllNodesVertices(): void {
const allResources: Set<Resource<any, any>> = new Set();
this.nextNodes.clear();
this.previousNodes.clear();
for (const pass of this.passes) {
const inputResources = pass.getAllResourcesOfType(InternalResourceType.Input);
const outputResources = pass.getAllResourcesOfType(InternalResourceType.Output);
this.previousNodes.set(pass, inputResources);
this.nextNodes.set(pass, outputResources);
for (const resource of [...inputResources, ...outputResources]) {
allResources.add(resource);
}
}
for (const resource of allResources) {
this.nextNodes.set(resource, new Set());
this.previousNodes.set(resource, new Set());
}
for (const pass of this.passes) {
for (const resource of this.previousNodes.get(pass)) {
this.nextNodes.get(resource).add(pass);
}
for (const resource of this.nextNodes.get(pass)) {
this.previousNodes.get(resource).add(pass);
}
}
}
private attachPhysicalResources(resources: Set<Resource<any, any>>): void {
for (const resource of resources) {
const currentResourceId = resource.attachedPhysicalResourceId;
const newResourceId = resource.descriptor.deserialize();
if (resource.attachedPhysicalResource && currentResourceId === newResourceId) {
continue;
}
if (resource.attachedPhysicalResource) {
this.resourcePool.pushPhysicalResource(currentResourceId, resource.attachedPhysicalResource);
}
resource.attachPhysicalResource(this.resourcePool);
}
}
private resetPhysicalResources(resources: Set<Resource<any, any>>): void {
for (const resource of resources) {
if (resource.isTransient && resource.attachedPhysicalResource) {
this.resourcePool.pushPhysicalResource(resource.attachedPhysicalResourceId, resource.attachedPhysicalResource);
resource.resetAttachedPhysicalResource();
}
}
}
private renderPasses(passes: Pass<any>[]): void {
for (const pass of passes) {
pass.render();
}
}
public render(): void {
this.updateAllNodesVertices();
const graph = this.buildGraphWithCulling(this.passes);
const sorted = <Pass<any>[]>this.sortRenderableNodes(graph);
this.lastGraph = graph;
this.lastSortedPassList = sorted;
const allResources: Set<Resource<any, any>> = new Set();
for (const pass of sorted) {
const resources = pass.getAllResources();
for (const resource of resources) {
allResources.add(resource);
}
}
this.attachPhysicalResources(allResources);
this.renderPasses(sorted);
this.resourcePool.update();
this.resetPhysicalResources(allResources);
}
}