diff --git a/src/webgpu/util/unions.ts b/src/webgpu/util/unions.ts index d105afb64789..23381ddee9d5 100644 --- a/src/webgpu/util/unions.ts +++ b/src/webgpu/util/unions.ts @@ -1,13 +1,39 @@ +/** + * Reifies a `GPUOrigin3D` into a `Required`. + */ +export function reifyOrigin3D( + val: Readonly | Iterable +): Required { + if (Symbol.iterator in val) { + const v = Array.from(val as Iterable); + return { + x: v[0] ?? 0, + y: v[1] ?? 0, + z: v[2] ?? 0, + }; + } else { + const v = val as Readonly; + return { + x: v.x ?? 0, + y: v.y ?? 0, + z: v.z ?? 0, + }; + } +} + /** * Reifies a `GPUExtent3D` into a `Required`. */ export function reifyExtent3D( val: Readonly | Iterable ): Required { - // TypeScript doesn't seem to want to narrow the types here properly, so hack around it. - if (typeof (val as Iterable)[Symbol.iterator] === 'function') { + if (Symbol.iterator in val) { const v = Array.from(val as Iterable); - return { width: v[0] ?? 1, height: v[1] ?? 1, depthOrArrayLayers: v[2] ?? 1 }; + return { + width: v[0] ?? 1, + height: v[1] ?? 1, + depthOrArrayLayers: v[2] ?? 1, + }; } else { const v = val as Readonly; return {