Skip to content

fix: correct argument index usage in assignArgumentBitRatio #845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/backend/function-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
66 changes: 63 additions & 3 deletions test/features/add-typed-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
})
Expand Down Expand Up @@ -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];
})
Expand Down Expand Up @@ -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];
})
Expand All @@ -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');
});