-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathframework.ts
More file actions
62 lines (53 loc) · 1.72 KB
/
framework.ts
File metadata and controls
62 lines (53 loc) · 1.72 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
import { copyToBuffer, createPng, Dimensions } from "./utils.ts";
import { createCapture } from "std/webgpu";
export class Framework {
device: GPUDevice;
dimensions: Dimensions;
errors: GPUError[] = [];
static async getDevice({
requiredFeatures,
optionalFeatures,
}: {
requiredFeatures?: GPUFeatureName[];
optionalFeatures?: GPUFeatureName[];
} = {}): Promise<GPUDevice> {
const adapter = await navigator.gpu.requestAdapter();
if (adapter === null) throw new Error(`Could not find adapter`);
const device = await adapter.requestDevice({
requiredFeatures: (requiredFeatures ?? []).concat(
optionalFeatures?.filter((feature) =>
adapter.features ? adapter.features.has(feature) : false
) ?? [],
),
});
if (!device) {
throw new Error("no suitable adapter found");
}
return device;
}
constructor(dimensions: Dimensions, device: GPUDevice) {
this.dimensions = dimensions;
this.device = device;
device.addEventListener("uncapturederror", (e) => {
this.errors.push(e.error);
});
}
async init() {}
render(_encoder: GPUCommandEncoder, _view: GPUTextureView) {}
async renderPng() {
await this.init();
const { texture, outputBuffer } = createCapture(
this.device,
this.dimensions.width,
this.dimensions.height,
);
const encoder = this.device.createCommandEncoder();
this.render(encoder, texture.createView());
copyToBuffer(encoder, texture, outputBuffer, this.dimensions);
this.device.queue.submit([encoder.finish()]);
await createPng(outputBuffer, this.dimensions);
if (this.errors.length > 0) {
throw new AggregateError(this.errors, "uncaught gpu errors");
}
}
}