Skip to content

Commit 277ef80

Browse files
authored
Add logical operation override tests. (#4469)
Add some shader validation and execution tests for overrides with logical expressions where the expression uses another override in both sides of the logical op, just the left hand side or just the right hand side.
1 parent d822a3c commit 277ef80

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
export const description = `Test override execution`;
2+
3+
import { makeTestGroup } from '../../../common/framework/test_group.js';
4+
import { keysOf } from '../../../common/util/data_tables.js';
5+
import { AllFeaturesMaxLimitsGPUTest } from '../../gpu_test.js';
6+
import { checkElementsEqual } from '../../util/check_contents.js';
7+
8+
export const g = makeTestGroup(AllFeaturesMaxLimitsGPUTest);
9+
10+
const kOverrideCases = {
11+
logical_lhs_override: {
12+
code: `x == 2 && 1 < 2`,
13+
},
14+
logical_rhs_override: {
15+
code: `1 > 2 || x == 2`,
16+
},
17+
logical_both_override: {
18+
code: `x > 2 || x == 2`,
19+
},
20+
};
21+
22+
g.test('logical')
23+
.desc(`Test replacing an override in the LHS of a logical statement`)
24+
.params(u => u.combine('case', keysOf(kOverrideCases)))
25+
.fn(async t => {
26+
const expr = kOverrideCases[t.params.case].code;
27+
const code = `
28+
override x: u32 = 2;
29+
override y: bool = ${expr};
30+
31+
@group(0) @binding(0) var<storage, read_write> v : vec4u;
32+
33+
@compute @workgroup_size(1)
34+
fn main() {
35+
if (y) {
36+
v = vec4u(4, 4, 4, 4);
37+
} else {
38+
v = vec4u(1, 1, 1, 1);
39+
}
40+
}`;
41+
42+
const pipeline = t.device.createComputePipeline({
43+
layout: 'auto',
44+
compute: {
45+
module: t.device.createShaderModule({
46+
code,
47+
}),
48+
entryPoint: 'main',
49+
},
50+
});
51+
52+
const buffer = t.makeBufferWithContents(
53+
new Uint32Array([0, 0, 0, 0]),
54+
GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST
55+
);
56+
57+
const bg = t.device.createBindGroup({
58+
layout: pipeline.getBindGroupLayout(0),
59+
entries: [
60+
{
61+
binding: 0,
62+
resource: {
63+
buffer,
64+
},
65+
},
66+
],
67+
});
68+
69+
const encoder = t.device.createCommandEncoder();
70+
const pass = encoder.beginComputePass();
71+
pass.setPipeline(pipeline);
72+
pass.setBindGroup(0, bg);
73+
pass.dispatchWorkgroups(1, 1, 1);
74+
pass.end();
75+
t.queue.submit([encoder.finish()]);
76+
77+
const bufferReadback = await t.readGPUBufferRangeTyped(buffer, {
78+
srcByteOffset: 0,
79+
type: Uint32Array,
80+
typedLength: 4,
81+
method: 'copy',
82+
});
83+
const got: Uint32Array = bufferReadback.data;
84+
const expected = new Uint32Array([4, 4, 4, 4]);
85+
86+
t.expectOK(checkElementsEqual(got, expected));
87+
});

src/webgpu/shader/validation/decl/override.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,21 @@ const kInitCases = {
206206
override y : i32;`,
207207
valid: true,
208208
},
209+
logical_lhs_override: {
210+
code: `override x = 2;
211+
override y = x == 2 && 1 < 2;`,
212+
valid: true,
213+
},
214+
logical_rhs_override: {
215+
code: `override x = 2;
216+
override y = 1 < 2 || x == 2;`,
217+
valid: true,
218+
},
219+
logical_both_override: {
220+
code: `override x = 2;
221+
override y = x > 2 || x == 2;`,
222+
valid: true,
223+
},
209224
};
210225

211226
g.test('initializer')

0 commit comments

Comments
 (0)