-
Notifications
You must be signed in to change notification settings - Fork 21
/
framework.ts
58 lines (49 loc) · 1.58 KB
/
framework.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
import { copyToBuffer, createPng, Dimensions } from "./utils.ts";
import { createCapture } from "std/webgpu";
export class Framework {
device: GPUDevice;
dimensions: Dimensions;
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");
}
device.addEventListener("uncaughterror", (e) => {
throw e.error;
});
return device;
}
constructor(dimensions: Dimensions, device: GPUDevice) {
this.dimensions = dimensions;
this.device = device;
}
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);
}
}