From 1e64a2dedd522638ce3f635214a96e867155989e Mon Sep 17 00:00:00 2001 From: "steve.chen" Date: Wed, 28 May 2025 11:21:29 +0800 Subject: [PATCH 1/2] fix: use argumentIndex for calleeNode.argumentBitRatios in assignArgumentBitRatio The previous implementation incorrectly used the caller's argument index to update the callee's argumentBitRatios, which could cause mismatches if the parameter order differs. Now it uses the correct argumentIndex as intended. --- src/backend/function-builder.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/backend/function-builder.js b/src/backend/function-builder.js index 1633f0e8..fa391768 100644 --- a/src/backend/function-builder.js +++ b/src/backend/function-builder.js @@ -569,14 +569,14 @@ class FunctionBuilder { if (!calleeNode.argumentBitRatios) { calleeNode.argumentBitRatios = new Array(calleeNode.argumentNames.length); } - const calleeBitRatio = calleeNode.argumentBitRatios[i]; + const calleeBitRatio = calleeNode.argumentBitRatios[argumentIndex]; if (typeof calleeBitRatio === 'number') { if (calleeBitRatio !== bitRatio) { throw new Error(`Incompatible bit ratio found at function ${functionName} at argument ${argumentName}`); } return calleeBitRatio; } - calleeNode.argumentBitRatios[i] = bitRatio; + calleeNode.argumentBitRatios[argumentIndex] = bitRatio; return bitRatio; } From 59c8fc2604be2d182923e65326e335d46bf90744 Mon Sep 17 00:00:00 2001 From: "steve.chen" Date: Thu, 5 Jun 2025 10:37:02 +0800 Subject: [PATCH 2/2] test: additional test code, get the argumentbitRatio correctly when argumentTypes is declared --- test/features/add-typed-functions.js | 66 ++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 3 deletions(-) diff --git a/test/features/add-typed-functions.js b/test/features/add-typed-functions.js index 72ec14ec..6a8f9065 100644 --- a/test/features/add-typed-functions.js +++ b/test/features/add-typed-functions.js @@ -10,7 +10,7 @@ function vec2Test(mode) { gpu.addFunction(typedFunction, { returnType: 'Array(2)' }); - const kernel = gpu.createKernel(function() { + const kernel = gpu.createKernel(function () { const result = typedFunction(); return result[0] + result[1]; }) @@ -47,7 +47,7 @@ function vec3Test(mode) { gpu.addFunction(typedFunction, { returnType: 'Array(3)' }); - const kernel = gpu.createKernel(function() { + const kernel = gpu.createKernel(function () { const result = typedFunction(); return result[0] + result[1] + result[2]; }) @@ -82,7 +82,7 @@ function vec4Test(mode) { gpu.addFunction(typedFunction, { returnType: 'Array(4)' }); - const kernel = gpu.createKernel(function() { + const kernel = gpu.createKernel(function () { const result = typedFunction(); return result[0] + result[1] + result[2] + result[3]; }) @@ -107,3 +107,63 @@ test('Array(4) - gpu', () => { (GPU.isHeadlessGLSupported ? test : skip)('Array(4) - headlessgl', () => { vec4Test('headlessgl'); }); + + +describe('features: get the argument bitRatio correctly when argumentTypes is declared.'); +function getArgumentBitRatioTest(mode) { + const gpu = new GPU({ mode }); + function getColor(array1, array2, index) { + const start = index; + const r = array1[start + 0]; + const g = array1[start + 1]; + const b = array1[start + 2]; + const a = array2[start + 3]; + return [r, g, b, a]; + } + gpu.addFunction(getColor, { + argumentTypes: { + array1: 'Array', + array2: 'Array', + index: 'Number', + }, + returnType: 'Array(4)', + }); + const kernel = gpu.createKernel(function (array1, array2, array3, array4) { + const result = getColor(array4, array1, 0); + return result; + }, { + argumentTypes: { + array1: 'Array', + array2: 'Array', + array3: 'Array', + array4: 'Array', + }, + precision: 'unsigned', + }) + .setOutput([1, 1]); + const inputArray = [ + 1, 0, 0, 1, + 0, 1, 0, 1, + 0, 0, 1, 1, + ]; + const result = kernel(inputArray, inputArray, inputArray, inputArray); + const expected = [1, 0, 0, 1,]; + assert.deepEqual(Array.from(result[0][0]), expected); + gpu.destroy(); +} + +test('myTest - auto', () => { + getArgumentBitRatioTest(null); +}); +test('myTest - gpu', () => { + getArgumentBitRatioTest('gpu'); +}); +(GPU.isWebGLSupported ? test : skip)('Array(4) - webgl', () => { + getArgumentBitRatioTest('webgl'); +}); +(GPU.isWebGL2Supported ? test : skip)('Array(4) - webgl2', () => { + getArgumentBitRatioTest('webgl2'); +}); +(GPU.isHeadlessGLSupported ? test : skip)('Array(4) - headlessgl', () => { + getArgumentBitRatioTest('headlessgl'); +}); \ No newline at end of file