See https://toji.dev/webgpu-best-practices/buffer-uploads - from "Buffers that are written once and never change".
Lots of geometry has this property, but it's unclear whether switching from writeBuffer to this would be worth it. Benchmarks?
// Creates a grid of vertices on the X, Y plane
function createXYPlaneVertexBuffer(width, height) {
const vertexSize = 3 * Float32Array.BYTES_PER_ELEMENT; // Each vertex is 3 floats (X,Y,Z position)
const vertexBuffer = gpuDevice.createBuffer({
size: width * height * vertexSize, // Allocate enough space for all the vertices
usage: GPUBufferUsage.VERTEX, // COPY_DST is not required!
mappedAtCreation: true,
});
const vertexPositions = new Float32Array(vertexBuffer.getMappedRange()),
// Build the vertex grid
for (let y = 0; y < height; ++y) {
for (let x = 0; x < width; ++x) {
const vertexIndex = y * width + x;
const offset = vertexIndex * 3;
vertexPositions[offset + 0] = x;
vertexPositions[offset + 1] = y;
vertexPositions[offset + 2] = 0;
}
}
// Commit the buffer contents to the GPU
vertexBuffer.unmap();
return vertexBuffer;
}
See https://toji.dev/webgpu-best-practices/buffer-uploads - from "Buffers that are written once and never change".
Lots of geometry has this property, but it's unclear whether switching from
writeBufferto this would be worth it. Benchmarks?