-
Notifications
You must be signed in to change notification settings - Fork 21
/
utils.ts
97 lines (84 loc) · 2.01 KB
/
utils.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
import { getRowPadding } from "std/webgpu";
import * as gmath from "gmath";
import * as png from "png";
export interface Dimensions {
width: number;
height: number;
}
export function copyToBuffer(
encoder: GPUCommandEncoder,
texture: GPUTexture,
outputBuffer: GPUBuffer,
dimensions: Dimensions,
): void {
const { padded } = getRowPadding(dimensions.width);
encoder.copyTextureToBuffer(
{
texture,
},
{
buffer: outputBuffer,
bytesPerRow: padded,
},
dimensions,
);
}
export async function createPng(
buffer: GPUBuffer,
dimensions: Dimensions,
): Promise<void> {
await buffer.mapAsync(1);
const inputBuffer = new Uint8Array(buffer.getMappedRange());
const { padded, unpadded } = getRowPadding(dimensions.width);
const outputBuffer = new Uint8Array(unpadded * dimensions.height);
for (let i = 0; i < dimensions.height; i++) {
const slice = inputBuffer
.slice(i * padded, (i + 1) * padded)
.slice(0, unpadded);
outputBuffer.set(slice, i * unpadded);
}
const image = png.encode(
outputBuffer,
dimensions.width,
dimensions.height,
{
stripAlpha: true,
color: 2,
},
);
Deno.writeFileSync("./output.png", image);
buffer.unmap();
}
interface BufferInit {
label?: string;
usage: number;
contents: ArrayBuffer;
}
export function createBufferInit(
device: GPUDevice,
descriptor: BufferInit,
): GPUBuffer {
const contents = new Uint8Array(descriptor.contents);
const alignMask = 4 - 1;
const paddedSize = Math.max(
(contents.byteLength + alignMask) & ~alignMask,
4,
);
const buffer = device.createBuffer({
label: descriptor.label,
usage: descriptor.usage,
mappedAtCreation: true,
size: paddedSize,
});
const data = new Uint8Array(buffer.getMappedRange());
data.set(contents);
buffer.unmap();
return buffer;
}
// deno-fmt-ignore
export const OPENGL_TO_WGPU_MATRIX = gmath.Matrix4.from(
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.0, 0.0, 0.5, 1.0,
);