-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.mjs
245 lines (231 loc) · 7.58 KB
/
buffer.mjs
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import { datatypeToTypedArray, datatypeToBytes } from "./util.mjs";
/** Buffer class
* Goal: handles all buffer-related tasks
* Required: GPUBuffer, which handles all queries (e.g., size)
* Can be a GPUBuffer only, or also have a CPU backing buffer
* Needs to be able to export necessary info for binding
*
* Members:
* - #gpuBuffer (GPUBufferBinding)
* - #cpuBuffer (optional)
*
* size: number of elements (not number of bytes)
*
* TODO: initialize call could take different strings to
* do different initalizations (all 1s, random, distribution, etc.)
*/
export class Buffer {
#gpuBuffer; /* this ALWAYS stores a GPUBufferBinding */
#mappableGPUBuffer;
#cpuBuffer;
constructor(args) {
this.args = { ...args };
/* generally expect args to contain datatype and size */
this.label =
args.label ?? `Buffer (datatype: ${this.datatype}; size: ${this.size})`;
/* we can optionally create (and optionally initialize) a CPUBuffer too */
/* we do this first in case we need to initialize the GPUBuffer */
if (this.args.createCPUBuffer) {
if (!("size" in args)) {
console.error(
`Buffer: if createCPUBuffer is true, must also specify size`
);
}
if (!("datatype" in args)) {
console.error(
`Buffer: if createCPUBuffer is true, must also specify datatype`
);
}
this.#cpuBuffer = new (datatypeToTypedArray(this.datatype))(
this.args.size
);
if (this.args.initializeCPUBuffer) {
// since we're input, fill the buffer with useful data
for (let i = 0; i < this.args.size; i++) {
if (this.datatype == "f32") {
this.#cpuBuffer[i] =
this.args.initializeCPUBuffer === "randomize"
? Math.random() * 2.0 - 1.0
: i & (2 ** 22 - 1);
// Rand: [-1,1]; non-rand: roughly, range of 32b significand
} else if (this.datatype == "u32") {
this.#cpuBuffer[i] = i == 0 ? 0 : this.#cpuBuffer[i - 1] + 1; // trying to get u32s
}
// otherwise, initialize nothing
}
}
}
if (this.args.createGPUBuffer) {
if ("buffer" in this.args) {
console.error(
"Buffer: don't pass in a buffer AND specify createGPUBuffer"
);
}
if (!("device" in this.args)) {
console.error("Buffer: must specify device if createGPUBuffer is true");
}
if (!("size" in this.args)) {
console.error(
`Buffer: if createGPUBuffer is true, must also specify size`
);
}
if (!("datatype" in this.args)) {
console.error(
`Buffer: if createGPUBuffer is true, must also specify datatype`
);
}
this.buffer = this.device.createBuffer({
label: this.label,
size: this.args.size * datatypeToBytes(this.args.datatype),
/** Output-only buffers may not need COPY_DST but we might
* initialize them with a buffer copy, so be safe and set it
*
* This might be a performance issue if COPY_DST costs something
*/
usage:
this.args.usage ??
GPUBufferUsage.STORAGE |
GPUBufferUsage.COPY_SRC |
GPUBufferUsage.COPY_DST,
});
if (this.args.initializeGPUBuffer) {
if (this.#cpuBuffer == undefined) {
console.error("Buffer: initializeGPUBuffer requires a CPUBuffer");
}
this.device.queue.writeBuffer(
this.buffer.buffer,
this.buffer.offset ?? 0,
this.#cpuBuffer
);
}
if (this.args.createMappableGPUBuffer) {
this.#mappableGPUBuffer = this.device.createBuffer({
label: "mappable |" + this.label,
size: this.size,
usage:
this.args.mappableGPUBufferUsage ??
GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
});
}
} else {
this.buffer = args.buffer;
}
if (this.#gpuBuffer === undefined) {
console.warn(
"Buffer constructor:",
this.label,
"GPU buffer is undefined"
);
}
}
async copyGPUToCPU() {
// copy buffer into mappable buffer ...
const copyEncoder = this.device.createCommandEncoder({
label: "encoder: GPU output buffer data -> mappable buffers",
});
copyEncoder.copyBufferToBuffer(
this.buffer.buffer,
this.buffer.offset ?? 0,
this.#mappableGPUBuffer,
0,
this.#mappableGPUBuffer.size
);
const copyCommandBuffer = copyEncoder.finish();
this.device.queue.submit([copyCommandBuffer]);
// ... then back to host
await this.#mappableGPUBuffer.mapAsync(GPUMapMode.READ);
this.#cpuBuffer = new (datatypeToTypedArray(this.datatype))(
this.#mappableGPUBuffer.getMappedRange().slice()
);
this.#mappableGPUBuffer.unmap();
}
set buffer(b) {
if (b?.buffer) {
/* this is already a GPUBufferBinding */
this.#gpuBuffer = b;
} else {
/* this is a GPUBuffer or undefined, make it a GPUBufferBinding */
this.#gpuBuffer = { buffer: b };
}
}
get buffer() {
return this.#gpuBuffer;
}
get cpuBuffer() {
return this.#cpuBuffer;
}
get datatype() {
return this.args.datatype;
}
get size() {
/* should be bytes */
return (
this.#gpuBuffer?.size ?? this.#gpuBuffer?.buffer?.size ?? this.args.size
);
}
get device() {
return this.args.device;
}
}
class BufferWithCPU extends Buffer {
/* this class uses this.cpuBuffer */
constructor(args) {
/** don't pass "size" to parent constructor, the gpuBuffer constructor
* has no notion of that, an arg of size is only used for the CPU buffer
*/
if (!("label" in args)) {
console.error(
"A CreateXBuffer must be initialized with a label, likely one of the knownBuffers associated with the primitive"
);
}
const { size, ...argsNotSize } = args;
super(argsNotSize);
this.cpuBuffer = new (datatypeToTypedArray(this.datatype))(size);
}
}
export class CreateInputBufferWithCPU extends BufferWithCPU {
constructor(args) {
super(args);
// since we're input, fill the buffer with useful data
for (let i = 0; i < args.size; i++) {
if (this.datatype == "f32") {
this.cpuBuffer[i] = args?.randomizeInput
? Math.random() * 2.0 - 1.0
: i & (2 ** 22 - 1);
// Rand: [-1,1]; non-rand: roughly, range of 32b significand
} else if (this.datatype == "u32") {
this.cpuBuffer[i] = i == 0 ? 0 : this.cpuBuffer[i - 1] + 1; // trying to get u32s
}
// otherwise, initialize nothing
}
this.gpuBuffer = this.device.createBuffer({
label: this.label,
size: this.cpuBuffer.byteLength,
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
});
this.device.queue.writeBuffer(this.gpuBuffer, 0, this.cpuBuffer);
}
}
export class CreateOutputBufferWithCPU extends BufferWithCPU {
constructor(args) {
super(args);
this.gpuBuffer = this.device.createBuffer({
label: this.label,
size: this.cpuBuffer.byteLength,
/** Output-only buffers may not need COPY_DST but we might
* initialize them with a buffer copy, so be safe and set it
*
* This might be a performance issue if COPY_DST costs something
*/
usage:
GPUBufferUsage.STORAGE |
GPUBufferUsage.COPY_SRC |
GPUBufferUsage.COPY_DST,
});
this.mappableGPUBuffer = this.device.createBuffer({
label: "mappable memory destination buffer",
size: this.cpuBuffer.byteLength,
usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
});
}
}