Skip to content

Commit

Permalink
Add shader validation tests about built-in variable clip_distances (g…
Browse files Browse the repository at this point in the history
…puweb#3949)

* Add shader validation tests about built-in variable `clip_distances`

This patch adds the shader validation tests about the built-in
variable `clip_distances`.

* Address reviewer's comment

* Test both enabling and disabling extension
  • Loading branch information
Jiawei-Shao authored Sep 14, 2024
1 parent e321371 commit 383aa28
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
54 changes: 52 additions & 2 deletions src/webgpu/shader/validation/shader_io/builtins.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const g = makeTestGroup(ShaderValidationTest);

// List of all built-in variables and their stage, in|out usage, and type.
// Taken from table in Section 15:
// https://www.w3.org/TR/2021/WD-WGSL-20211013/#builtin-variables
// https://www.w3.org/TR/WGSL/#builtin-inputs-outputs
export const kBuiltins = [
{ name: 'vertex_index', stage: 'vertex', io: 'in', type: 'u32' },
{ name: 'instance_index', stage: 'vertex', io: 'in', type: 'u32' },
Expand All @@ -30,6 +30,14 @@ export const kBuiltins = [
{ name: 'subgroup_size', stage: 'compute', io: 'in', type: 'u32' },
{ name: 'subgroup_invocation_id', stage: 'fragment', io: 'in', type: 'u32' },
{ name: 'subgroup_size', stage: 'fragment', io: 'in', type: 'u32' },
{ name: 'clip_distances', stage: 'vertex', io: 'out', type: 'array<f32,1>' },
{ name: 'clip_distances', stage: 'vertex', io: 'out', type: 'array<f32,2>' },
{ name: 'clip_distances', stage: 'vertex', io: 'out', type: 'array<f32,3>' },
{ name: 'clip_distances', stage: 'vertex', io: 'out', type: 'array<f32,4>' },
{ name: 'clip_distances', stage: 'vertex', io: 'out', type: 'array<f32,5>' },
{ name: 'clip_distances', stage: 'vertex', io: 'out', type: 'array<f32,6>' },
{ name: 'clip_distances', stage: 'vertex', io: 'out', type: 'array<f32,7>' },
{ name: 'clip_distances', stage: 'vertex', io: 'out', type: 'array<f32,8>' },
] as const;

// List of types to test against.
Expand Down Expand Up @@ -64,7 +72,15 @@ const kTestTypes = [
'array<bool,4>',
'array<u32,4>',
'array<i32,4>',
'array<f32,1>',
'array<f32,2>',
'array<f32,3>',
'array<f32,4>',
'array<f32,5>',
'array<f32,6>',
'array<f32,7>',
'array<f32,8>',
'array<f32,9>',
'MyStruct',
] as const;

Expand All @@ -87,7 +103,16 @@ g.test('stage_inout')
);
if (t.params.name.includes('subgroup')) {
t.selectDeviceOrSkipTestCase('subgroups' as GPUFeatureName);
} else if (t.params.name === 'clip_distances') {
t.selectDeviceOrSkipTestCase('clip-distances' as GPUFeatureName);
}
t.skipIf(
t.params.name !== 'position' &&
t.params.target_stage === 'vertex' &&
t.params.target_io === 'out' &&
!t.params.use_struct,
'missing @builtin(position) in the vertex output when the vertex output is not a struct'
);
})
.fn(t => {
const code = generateShader({
Expand Down Expand Up @@ -117,9 +142,9 @@ g.test('type')
.params(u =>
u
.combineWithParams(kBuiltins)
.combine('use_struct', [true, false] as const)
.beginSubcases()
.combine('target_type', kTestTypes)
.combine('use_struct', [true, false] as const)
)
.beforeAllSubcases(t => {
t.skipIf(
Expand All @@ -128,7 +153,16 @@ g.test('type')
);
if (t.params.name.includes('subgroup')) {
t.selectDeviceOrSkipTestCase('subgroups' as GPUFeatureName);
} else if (t.params.name === 'clip_distances') {
t.selectDeviceOrSkipTestCase('clip-distances' as GPUFeatureName);
}
t.skipIf(
t.params.name !== 'position' &&
t.params.stage === 'vertex' &&
t.params.io === 'out' &&
!t.params.use_struct,
'missing @builtin(position) in the vertex output'
);
})
.fn(t => {
let code = '';
Expand Down Expand Up @@ -297,14 +331,30 @@ g.test('reuse_builtin_name')
u
.combineWithParams(kBuiltins)
.combine('use', ['alias', 'struct', 'function', 'module-var', 'function-var'])
.combine('enable_extension', [true, false])
.unless(
t => t.enable_extension && !(t.name.includes('subgroup') || t.name === 'clip_distances')
)
)
.beforeAllSubcases(t => {
if (!t.params.enable_extension) {
return;
}
if (t.params.name.includes('subgroup')) {
t.selectDeviceOrSkipTestCase('subgroups' as GPUFeatureName);
} else if (t.params.name === 'clip_distances') {
t.selectDeviceOrSkipTestCase('clip-distances' as GPUFeatureName);
}
})
.fn(t => {
let code = '';
if (t.params.enable_extension) {
if (t.params.name.includes('subgroups')) {
code += 'enable subgroup;\n';
} else if (t.params.name === 'clip_distances') {
code += 'enable clip_distances;\n';
}
}
if (t.params.use === 'alias') {
code += `alias ${t.params.name} = i32;`;
} else if (t.params.use === `struct`) {
Expand Down
3 changes: 3 additions & 0 deletions src/webgpu/shader/validation/shader_io/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export function generateShader({
if (attribute.includes('subgroup')) {
code += 'enable subgroups;\n';
}
if (attribute.includes('clip_distances')) {
code += 'enable clip_distances;\n';
}

if (use_struct) {
// Generate a struct that wraps the entry point IO variable.
Expand Down

0 comments on commit 383aa28

Please sign in to comment.