UNPKG

@motion-core/motion-gpu

Version:

Framework-agnostic WebGPU runtime for fullscreen WGSL shaders with explicit Svelte, React, and Vue adapter entrypoints.

107 lines (106 loc) 4.17 kB
import { createMotionGPUError } from "./error-report.js"; //#region src/lib/core/resource-registry.ts function assertUniqueResource(kind, logicalId, resources) { if (resources.has(logicalId)) throw createMotionGPUError("RESOURCE_REGISTRY_DUPLICATE", `Material ${kind} resource "${logicalId}" is already registered.`); } /** * Renderer-local registry of logical material resources. * * The registry owns metadata, not WebGPU objects. The renderer remains * responsible for destroying allocations in its lifecycle teardown. */ var MaterialResourceRegistry = class { textures = /* @__PURE__ */ new Map(); buffers = /* @__PURE__ */ new Map(); registerTexture(input) { assertUniqueResource("texture", input.logicalId, this.textures); const resource = { logicalId: input.logicalId, ownedTexture: input.ownedTexture ?? null, storageView: input.storageView ?? null, sampledView: input.sampledView, publishedView: input.publishedView ?? input.sampledView, format: input.format, width: input.width, height: input.height, mipLevelCount: input.mipLevelCount, sampleType: input.sampleType, usage: input.usage, resourceVersion: 0 }; this.textures.set(input.logicalId, resource); return resource; } registerStorageBuffer(input) { assertUniqueResource("storage buffer", input.logicalId, this.buffers); const resource = { ...input, resourceVersion: 0 }; this.buffers.set(input.logicalId, resource); return resource; } getTexture(logicalId) { return this.textures.get(logicalId); } requireTexture(logicalId) { const resource = this.getTexture(logicalId); if (!resource) throw createMotionGPUError("RESOURCE_REGISTRY_TEXTURE_MISSING", `Unknown material texture resource "${logicalId}".`); return resource; } getStorageBuffer(logicalId) { return this.buffers.get(logicalId); } requireStorageBuffer(logicalId) { const resource = this.getStorageBuffer(logicalId); if (!resource) throw createMotionGPUError("RESOURCE_REGISTRY_STORAGE_BUFFER_MISSING", `Unknown material storage buffer resource "${logicalId}".`); return resource; } /** * Replaces physical texture state and publishes the sampled view atomically. * Returns whether fragment/consumer bind groups need a new view reference. */ replaceTextureAllocation(logicalId, next) { const resource = this.requireTexture(logicalId); const publishedViewChanged = resource.publishedView !== next.sampledView; const allocationChanged = resource.ownedTexture !== next.ownedTexture || resource.storageView !== next.storageView || resource.sampledView !== next.sampledView || resource.format !== next.format || resource.width !== next.width || resource.height !== next.height || resource.mipLevelCount !== next.mipLevelCount || resource.usage !== next.usage; resource.ownedTexture = next.ownedTexture; resource.storageView = next.storageView; resource.sampledView = next.sampledView; resource.publishedView = next.sampledView; resource.format = next.format; resource.width = next.width; resource.height = next.height; resource.mipLevelCount = next.mipLevelCount; resource.usage = next.usage; if (allocationChanged || publishedViewChanged) resource.resourceVersion += 1; return publishedViewChanged; } /** * Publishes a renderer-produced view without changing the source allocation. */ publishTextureView(logicalId, view) { const resource = this.requireTexture(logicalId); if (resource.publishedView === view) return false; resource.publishedView = view; resource.resourceVersion += 1; return true; } markTextureWritten(logicalId, publishedView) { const resource = this.requireTexture(logicalId); const publishedViewChanged = publishedView !== void 0 && resource.publishedView !== publishedView; if (publishedView !== void 0) resource.publishedView = publishedView; resource.resourceVersion += 1; return publishedViewChanged; } markStorageBufferWritten(logicalId) { this.requireStorageBuffer(logicalId).resourceVersion += 1; } clear() { this.textures.clear(); this.buffers.clear(); } }; //#endregion export { MaterialResourceRegistry }; //# sourceMappingURL=resource-registry.js.map