@motion-core/motion-gpu
Version:
Framework-agnostic WebGPU runtime for fullscreen WGSL shaders with explicit Svelte, React, and Vue adapter entrypoints.
95 lines (94 loc) • 3.37 kB
JavaScript
import { assertUniformName } from "./uniforms.js";
//#region src/lib/core/storage-buffers.ts
/**
* Valid WGSL storage buffer element types.
*/
var VALID_STORAGE_BUFFER_TYPES = new Set([
"array<f32>",
"array<vec2f>",
"array<vec3f>",
"array<vec4f>",
"array<u32>",
"array<i32>",
"array<vec4u>",
"array<vec4i>"
]);
/**
* Storage-compatible texture formats for `texture_storage_2d`.
*/
var STORAGE_TEXTURE_FORMATS = new Set([
"r32float",
"r32sint",
"r32uint",
"rg32float",
"rg32sint",
"rg32uint",
"rgba8unorm",
"rgba8snorm",
"rgba8uint",
"rgba8sint",
"rgba16float",
"rgba16uint",
"rgba16sint",
"rgba32float",
"rgba32uint",
"rgba32sint",
"bgra8unorm"
]);
/**
* Validates a single storage buffer definition.
*
* @param name - Buffer identifier.
* @param definition - Storage buffer definition to validate.
* @throws {Error} When any field is invalid.
*/
function assertStorageBufferDefinition(name, definition) {
assertUniformName(name);
if (!Number.isFinite(definition.size) || definition.size <= 0) throw new Error(`Storage buffer "${name}" size must be a finite number greater than 0, got ${definition.size}`);
if (definition.size % 4 !== 0) throw new Error(`Storage buffer "${name}" size must be a multiple of 4, got ${definition.size}`);
if (!VALID_STORAGE_BUFFER_TYPES.has(definition.type)) throw new Error(`Storage buffer "${name}" has unknown type "${definition.type}". Supported types: ${[...VALID_STORAGE_BUFFER_TYPES].join(", ")}`);
if (definition.access !== void 0 && definition.access !== "read" && definition.access !== "read-write") throw new Error(`Storage buffer "${name}" has invalid access mode "${definition.access}". Use 'read' or 'read-write'.`);
if (definition.initialData !== void 0) {
if (definition.initialData.byteLength > definition.size) throw new Error(`Storage buffer "${name}" initialData byte length (${definition.initialData.byteLength}) exceeds buffer size (${definition.size})`);
}
}
/**
* Validates and returns sorted storage buffer keys.
*
* @param definitions - Storage buffer definition map.
* @returns Lexicographically sorted buffer keys.
*/
function resolveStorageBufferKeys(definitions) {
const keys = Object.keys(definitions).sort();
for (const key of keys) {
const definition = definitions[key];
if (definition) assertStorageBufferDefinition(key, definition);
}
return keys;
}
/**
* Normalizes a storage buffer definition with defaults applied.
*
* @param definition - Raw definition.
* @returns Normalized definition with access default.
*/
function normalizeStorageBufferDefinition(definition) {
return {
size: definition.size,
type: definition.type,
access: definition.access ?? "read-write"
};
}
/**
* Validates that a texture format is storage-compatible.
*
* @param name - Texture identifier.
* @param format - GPU texture format.
* @throws {Error} When format is not storage-compatible.
*/
function assertStorageTextureFormat(name, format) {
if (!STORAGE_TEXTURE_FORMATS.has(format)) throw new Error(`Texture "${name}" with storage:true requires a storage-compatible format, but got "${format}". Supported formats: ${[...STORAGE_TEXTURE_FORMATS].join(", ")}`);
}
//#endregion
export { STORAGE_TEXTURE_FORMATS, assertStorageBufferDefinition, assertStorageTextureFormat, normalizeStorageBufferDefinition, resolveStorageBufferKeys };
//# sourceMappingURL=storage-buffers.js.map