UNPKG

typegpu

Version:

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

56 lines (55 loc) 1.57 kB
import {} from "../data/snippet.js"; import { inCodegenMode } from "../execMode.js"; import { add, bitShiftLeft, bitShiftRight, div, mod, mul, sub } from "../std/operators.js"; export const infixOperators = { add, sub, mul, div, mod, bitShiftLeft, bitShiftRight, }; /** * InfixDispatch is only used in codegen mode. * `lhs` may either be Numeric or Snippet. * @example * const external = d.vec2u(1); * const fn = () => { * 'use gpu'; * external.mul(2); // lhs is Numeric * d.vec2u(1).mul(2) // lhs is a snippet * } */ export class InfixDispatch { lhs; operator; constructor(lhs, operator) { this.lhs = lhs; this.operator = operator; } } export function isInfixDispatch(o) { return o instanceof InfixDispatch; } /** * This function is used on vec/mat prototypes. * This is done in runtime in order to avoid a circular dependency. */ export function assignInfixOperator(base, operator, operatorSymbol) { const opImpl = infixOperators[operator]; Object.defineProperty(base.prototype, operatorSymbol, { value: opImpl }); // Returning this from a getter will work as if this was a vector/matrix's method. function jsInfixDispatchFor(arg) { // operator will perform all necessary type checks return opImpl(this, arg); } Object.defineProperty(base.prototype, operator, { get() { if (inCodegenMode()) { return new InfixDispatch(this, opImpl); } return jsInfixDispatchFor; }, }); }