-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathframework.ts
40 lines (33 loc) · 1.09 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
import { copyToBuffer, createCapture, createPng, Dimensions } from "./utils.ts";
export class Framework {
device: GPUDevice;
dimensions: Dimensions;
static async getDevice(features: GPUFeatureName[] = []): Promise<GPUDevice> {
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter?.requestDevice({
requiredFeatures: features,
});
if (!device) {
throw new Error("no suitable adapter found");
}
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,
);
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);
}
}