UNPKG

typegpu

Version:

A thin layer between JS and WebGPU/WGSL that improves development experience and allows for faster iteration.

69 lines (68 loc) 3.5 kB
import { INTERNAL_applyBufferUsages, } from "../core/buffer/buffer.js"; import { TgpuBufferBindingImpl } from "../core/buffer/bufferBinding.js"; import { constant } from "../core/constant/tgpuConstant.js"; import { INTERNAL_restoreComputePipeline, } from "../core/pipeline/computePipeline.js"; import { INTERNAL_restoreRenderPipeline, } from "../core/pipeline/renderPipeline.js"; import { INTERNAL_restoreGuardedComputePipeline, INTERNAL_restoreRoot } from "../core/root/init.js"; import { accessor, mutableAccessor } from "../core/slot/accessor.js"; import { slot } from "../core/slot/slot.js"; import { INTERNAL_createTexture } from "../core/texture/texture.js"; import { vertexLayout } from "../core/vertexLayout/vertexLayout.js"; import { arrayOf } from "../data/array.js"; import { disarrayOf } from "../data/disarray.js"; import { isDisarray } from "../data/dataTypes.js"; import { isWgslArray } from "../data/wgslTypes.js"; import { INTERNAL_restoreBindGroup, INTERNAL_restoreBindGroupLayout, } from "../tgpuBindGroupLayout.js"; function restoreBufferBinding(soul) { return new TgpuBufferBindingImpl(soul.type, soul.buffer); } /** * The one place a soul turns back into a resource. Importing this module opts * into every restorer, so it is kept out of the resources themselves */ export const soulRestorers = { buffer: (soul, ctx) => { const buffer = ctx.getRoot(soul.device).createBuffer(soul.dataType, soul.raw); INTERNAL_applyBufferUsages(buffer, soul.usages); return buffer; }, uniform: restoreBufferBinding, mutable: restoreBufferBinding, readonly: restoreBufferBinding, texture: (soul, ctx) => { const texture = INTERNAL_createTexture(soul.props, ctx.getRoot(soul.device), soul.raw); if (soul.flagsOverridden) { texture.$overrideFlags(soul.flags); } else if (soul.usages.length > 0) { texture.$usage(...soul.usages); } return texture; }, sampler: (soul, ctx) => ctx.getRoot(soul.device).createSampler(soul.props), 'sampler-comparison': (soul, ctx) => ctx.getRoot(soul.device).createComparisonSampler(soul.props), 'query-set': (soul, ctx) => ctx.getRoot(soul.device).createQuerySet(soul.queryType, soul.count, soul.raw), 'bind-group-layout': (soul) => INTERNAL_restoreBindGroupLayout(soul), 'bind-group': INTERNAL_restoreBindGroup, 'vertex-layout': (soul) => { const schema = soul.schema; if (isWgslArray(schema)) { const elementType = schema.elementType; return vertexLayout((count) => arrayOf(elementType, count), soul.stepMode); } if (isDisarray(schema)) { const elementType = schema.elementType; return vertexLayout((count) => disarrayOf(elementType, count), soul.stepMode); } throw new Error('TypeGPU vertex layout payload could not be reconstructed.'); }, const: (soul) => constant(soul.dataType, soul.value), slot: (soul) => slot(soul.defaultValue), accessor: (soul) => accessor(soul.schema, soul.defaultValue), 'mutable-accessor': (soul) => mutableAccessor(soul.schema, soul.defaultValue), 'compute-pipeline': INTERNAL_restoreComputePipeline, 'render-pipeline': INTERNAL_restoreRenderPipeline, 'guarded-compute-pipeline': INTERNAL_restoreGuardedComputePipeline, root: INTERNAL_restoreRoot, // oxlint-disable-next-line typescript/no-explicit-any -- each restorer narrows its own soul };