From df7067aedbba2558f15ada4c338483ed861bb1d2 Mon Sep 17 00:00:00 2001 From: Iwo Plaza Date: Wed, 29 Jul 2026 10:45:13 +0200 Subject: [PATCH] fix: Lazy buffer views in bitcast to allow for polyfill --- packages/typegpu/src/std/bitcast.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/typegpu/src/std/bitcast.ts b/packages/typegpu/src/std/bitcast.ts index 755ee9ee54..aa42d7defb 100644 --- a/packages/typegpu/src/std/bitcast.ts +++ b/packages/typegpu/src/std/bitcast.ts @@ -201,11 +201,25 @@ const bitcastAllowedSchemas = [ type BitcastAllowedTypes = (typeof bitcastAllowedSchemas)[number]; const buffer = new ArrayBuffer(16); +const bufViewCache = { + f32: undefined as Float32Array | undefined, + u32: undefined as Uint32Array | undefined, + i32: undefined as Int32Array | undefined, + f16: undefined as Float16Array | undefined, +}; const bufViews = { - f32: new Float32Array(buffer), - u32: new Uint32Array(buffer), - i32: new Int32Array(buffer), - f16: new Float16Array(buffer), + get f32() { + return (bufViewCache['f32'] ??= new Float32Array(buffer)); + }, + get u32() { + return (bufViewCache['u32'] ??= new Uint32Array(buffer)); + }, + get i32() { + return (bufViewCache['i32'] ??= new Int32Array(buffer)); + }, + get f16() { + return (bufViewCache['f16'] ??= new Float16Array(buffer)); + }, }; function writeToBuffer(