-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpersistent-state.ts
More file actions
59 lines (53 loc) · 2.06 KB
/
Copy pathpersistent-state.ts
File metadata and controls
59 lines (53 loc) · 2.06 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
/**
* Source: Original ZenFG package recipe.
* Demonstrates: Defined contents and state carried between frames.
* Flow: Import persistent buffers, record updates and retain them with a persistent-state root.
* The caller owns the device and native inputs; ZenFG owns graph execution.
* Read next: README.md for inputs and related recipes. The Playground adapter
* and recipeHost.ts provide browser setup and snapshot capture.
*/
import { FrameGraph, type FrameGraphRecording } from '@zenfg/webgpu';
export type PersistentStateRecordOptions = {
readonly recorder: FrameGraphRecording;
readonly historyTexture: GPUTexture;
readonly hasPreviousValue: boolean;
readonly encodeUpdate: (pass: GPURenderPassEncoder) => undefined;
};
export type PersistentStateOptions = {
readonly graph: FrameGraph;
readonly historyTexture: GPUTexture;
readonly hasPreviousValue: boolean;
readonly frameIndex: number;
readonly encodeUpdate: (pass: GPURenderPassEncoder) => undefined;
};
/** Declares an update of caller-owned state without compiling it. */
export function recordPersistentStateUpdate(options: PersistentStateRecordOptions): void {
const history = options.recorder.importTexture(options.historyTexture, {
label: 'temporal-history',
initialContents: options.hasPreviousValue ? 'defined' : 'undefined',
});
options.recorder.render({
label: 'update-history',
colorAttachments: [{
target: history,
loadOp: options.hasPreviousValue ? 'load' : 'clear',
storeOp: 'store',
clearValue: { r: 0, g: 0, b: 0, a: 0 },
}],
encode({ pass }) {
return options.encodeUpdate(pass);
},
});
options.recorder.markPersistentState(history);
}
/** Updates a caller-owned history texture and retains its final producer. */
export function updatePersistentState(options: PersistentStateOptions): void {
const recorder = options.graph.beginFrame();
recordPersistentStateUpdate({
recorder,
historyTexture: options.historyTexture,
hasPreviousValue: options.hasPreviousValue,
encodeUpdate: options.encodeUpdate,
});
recorder.compile().execute({ frameIndex: options.frameIndex });
}