Skip to content

Commit

Permalink
shader/execution: Implement vector component selection tests
Browse files Browse the repository at this point in the history
Bug: #3170
  • Loading branch information
ben-clayton committed Mar 11, 2024
1 parent 5ad7589 commit d97cc72
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 7 deletions.
12 changes: 7 additions & 5 deletions src/webgpu/listing_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -899,11 +899,13 @@
"webgpu:idl,constructable:gpu_errors:*": { "subcaseMS": 0.101 },
"webgpu:idl,constructable:pipeline_errors:*": { "subcaseMS": 0.101 },
"webgpu:idl,constructable:uncaptured_error_event:*": { "subcaseMS": 0.101 },
"webgpu:shader,execution,expression,access,index,array:abstract_scalar:*": { "subcaseMS": 189.344 },
"webgpu:shader,execution,expression,access,index,array:concrete_scalar:*": { "subcaseMS": 989.619 },
"webgpu:shader,execution,expression,access,index,array:vector:*": { "subcaseMS": 1546.461 },
"webgpu:shader,execution,expression,access,index,vector:abstract_scalar:*": { "subcaseMS": 213.529 },
"webgpu:shader,execution,expression,access,index,vector:concrete_scalar:*": { "subcaseMS": 1918.096 },
"webgpu:shader,execution,expression,access,array,index:abstract_scalar:*": { "subcaseMS": 235.962 },
"webgpu:shader,execution,expression,access,array,index:concrete_scalar:*": { "subcaseMS": 1439.796 },
"webgpu:shader,execution,expression,access,array,index:vector:*": { "subcaseMS": 2137.684 },
"webgpu:shader,execution,expression,access,vector,components:abstract_scalar:*": { "subcaseMS": 533.768 },
"webgpu:shader,execution,expression,access,vector,components:concrete_scalar:*": { "subcaseMS": 11636.652 },
"webgpu:shader,execution,expression,access,vector,index:abstract_scalar:*": { "subcaseMS": 215.404 },
"webgpu:shader,execution,expression,access,vector,index:concrete_scalar:*": { "subcaseMS": 1707.582 },
"webgpu:shader,execution,expression,binary,af_addition:scalar:*": { "subcaseMS": 815.300 },
"webgpu:shader,execution,expression,binary,af_addition:scalar_vector:*": { "subcaseMS": 1803.434 },
"webgpu:shader,execution,expression,binary,af_addition:vector:*": { "subcaseMS": 719.600 },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
export const description = `
Execution Tests for vector component selection expressions
`;

import { makeTestGroup } from '../../../../../../common/framework/test_group.js';
import { GPUTest } from '../../../../../gpu_test.js';
import { ScalarValue, Type, VectorValue, f32 } from '../../../../../util/conversion.js';
import { allInputSources, basicExpressionBuilder, run } from '../../expression.js';

export const g = makeTestGroup(GPUTest);

/** @returns the full permutation of component indices used to component select a vector of width 'n' */
function indices(n: number) {
const out: number[][] = [];
for (let width = 1; width < n; width++) {
let generate = (swizzle: number[]) => {
out.push(swizzle);
};
for (let i = 0; i < width; i++) {
const next = generate;
generate = (swizzle: number[]) => {
for (let j = 0; j < width; j++) {
next([...swizzle, j]);
}
};
}
generate([]);
}
return out;
}

g.test('concrete_scalar')
.specURL('https://www.w3.org/TR/WGSL/#vector-access-expr')
.desc(`Test vector component selection of concrete vectors`)
.params(u =>
u
.combine('inputSource', allInputSources)
.combine('elementType', ['i32', 'u32', 'f32', 'f16'] as const)
.combine('width', [2, 3, 4] as const)
.combine('components', ['rgba', 'xyzw'] as const)
.beginSubcases()
.expand('indices', u => indices(u.width))
)
.beforeAllSubcases(t => {
if (t.params.elementType === 'f16') {
t.selectDeviceOrSkipTestCase('shader-f16');
}
})
.fn(async t => {
const elementType = Type[t.params.elementType];
const vectorType = Type.vec(t.params.width, elementType);
const elementValues = (i: number) => (i + 1) * 10;
const elements: ScalarValue[] = [];
for (let i = 0; i < t.params.width; i++) {
elements.push(elementType.create(elementValues(i)));
}
const result = (() => {
if (t.params.indices.length === 1) {
return { type: elementType, value: elementType.create(elementValues(0)) };
} else {
const vec = Type.vec(t.params.indices.length, elementType);
return { type: vec, value: vec.create(t.params.indices.map(i => elementValues(i))) };
}
})();

const components = t.params.indices.map(i => t.params.components[i]).join('');
await run(
t,
basicExpressionBuilder(ops => `${ops[0]}.${components}`),
[vectorType],
result.type,
t.params,
[{ input: [new VectorValue(elements)], expected: result.value }]
);
});

g.test('abstract_scalar')
.specURL('https://www.w3.org/TR/WGSL/#vector-access-expr')
.desc(`Test vector component selection of abstract numeric vectors`)
.params(u =>
u
.combine('elementType', ['abstract-int', 'abstract-float'] as const)
.combine('width', [2, 3, 4] as const)
.combine('components', ['rgba', 'xyzw'] as const)
.beginSubcases()
.expand('indices', u => indices(u.width))
)
.fn(async t => {
const elementType = Type[t.params.elementType];
const vectorType = Type.vec(t.params.width, elementType);
const elementValues = (i: number) => (i + 1) * 0x100000000;
const elements: ScalarValue[] = [];
for (let i = 0; i < t.params.width; i++) {
elements.push(elementType.create(elementValues(i)));
}
const result = (() => {
if (t.params.indices.length === 1) {
return { type: Type.f32, value: f32(elementValues(0) / 0x100000000) };
} else {
const vec = Type.vec(t.params.indices.length, Type.f32);
return {
type: vec,
value: vec.create(t.params.indices.map(i => elementValues(i) / 0x100000000)),
};
}
})();

const components = t.params.indices.map(i => t.params.components[i]).join('');
await run(
t,
basicExpressionBuilder(ops => `${ops[0]}.${components} / 0x100000000`),
[vectorType],
result.type,
{ inputSource: 'const' },
[{ input: [new VectorValue(elements)], expected: result.value }]
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const g = makeTestGroup(GPUTest);

g.test('concrete_scalar')
.specURL('https://www.w3.org/TR/WGSL/#vector-access-expr')
.desc(`Test indexing of an array of concrete scalars`)
.desc(`Test indexing of concrete vectors`)
.params(u =>
u
.combine('inputSource', allInputSources)
Expand Down Expand Up @@ -51,7 +51,7 @@ g.test('concrete_scalar')

g.test('abstract_scalar')
.specURL('https://www.w3.org/TR/WGSL/#vector-access-expr')
.desc(`Test indexing of an array of scalars`)
.desc(`Test indexing of abstract numeric vectors`)
.params(u =>
u
.combine('elementType', ['abstract-int', 'abstract-float'] as const)
Expand Down

0 comments on commit d97cc72

Please sign in to comment.