summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/mozilla/tests/webgpu/webgpu/shader/validation/expression/binary/comparison.spec.js
blob: 75ab1b6daa7ccd39c3e6978aab0b3d74582481b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* AUTO-GENERATED - DO NOT EDIT. Source: https://github.com/gpuweb/cts
**/export const description = `
Validation tests for comparison expressions.
`;import { makeTestGroup } from '../../../../../common/framework/test_group.js';
import { keysOf, objectsToRecord } from '../../../../../common/util/data_tables.js';
import {
  isFloatType,
  kAllScalarsAndVectors,
  ScalarType,
  scalarTypeOf,
  Type,
  VectorType } from
'../../../../util/conversion.js';
import { ShaderValidationTest } from '../../shader_validation_test.js';

export const g = makeTestGroup(ShaderValidationTest);

// A list of scalar and vector types.
const kScalarAndVectorTypes = objectsToRecord(kAllScalarsAndVectors);

// A list of comparison operators and a flag for whether they support boolean values or not.
const kComparisonOperators = {
  eq: { op: '==', supportsBool: true },
  ne: { op: '!=', supportsBool: true },
  gt: { op: '>', supportsBool: false },
  ge: { op: '>=', supportsBool: false },
  lt: { op: '<', supportsBool: false },
  le: { op: '<=', supportsBool: false }
};

g.test('scalar_vector').
desc(
  `
  Validates that scalar and vector comparison expressions are only accepted for compatible types.
  `
).
params((u) =>
u.
combine('lhs', keysOf(kScalarAndVectorTypes)).
combine(
  'rhs',
  // Skip vec3 and vec4 on the RHS to keep the number of subcases down.
  keysOf(kScalarAndVectorTypes).filter(
    (value) => !(value.startsWith('vec3') || value.startsWith('vec4'))
  )
).
beginSubcases().
combine('op', keysOf(kComparisonOperators))
).
beforeAllSubcases((t) => {
  if (
  scalarTypeOf(kScalarAndVectorTypes[t.params.lhs]) === Type.f16 ||
  scalarTypeOf(kScalarAndVectorTypes[t.params.rhs]) === Type.f16)
  {
    t.selectDeviceOrSkipTestCase('shader-f16');
  }
}).
fn((t) => {
  const lhs = kScalarAndVectorTypes[t.params.lhs];
  const rhs = kScalarAndVectorTypes[t.params.rhs];
  const lhsElement = scalarTypeOf(lhs);
  const rhsElement = scalarTypeOf(rhs);
  const hasF16 = lhsElement === Type.f16 || rhsElement === Type.f16;
  const code = `
${hasF16 ? 'enable f16;' : ''}
const lhs = ${lhs.create(0).wgsl()};
const rhs = ${rhs.create(0).wgsl()};
const foo = lhs ${kComparisonOperators[t.params.op].op} rhs;
`;

  let valid = false;

  // Determine if the element types are comparable.
  let elementIsCompatible = false;
  if (lhsElement === Type.abstractInt) {
    // Abstract integers are comparable to any other numeric type.
    elementIsCompatible = rhsElement !== Type.bool;
  } else if (rhsElement === Type.abstractInt) {
    // Abstract integers are comparable to any other numeric type.
    elementIsCompatible = lhsElement !== Type.bool;
  } else if (lhsElement === Type.abstractFloat) {
    // Abstract floats are comparable to any other float type.
    elementIsCompatible = isFloatType(rhsElement);
  } else if (rhsElement === Type.abstractFloat) {
    // Abstract floats are comparable to any other float type.
    elementIsCompatible = isFloatType(lhsElement);
  } else {
    // Non-abstract types are only comparable to values with the exact same type.
    elementIsCompatible = lhsElement === rhsElement;
  }

  // Determine if the full type is comparable.
  if (lhs instanceof ScalarType && rhs instanceof ScalarType) {
    valid = elementIsCompatible;
  } else if (lhs instanceof VectorType && rhs instanceof VectorType) {
    // Vectors are only comparable if the vector widths match.
    valid = lhs.width === rhs.width && elementIsCompatible;
  }

  if (lhsElement === Type.bool) {
    valid &&= kComparisonOperators[t.params.op].supportsBool;
  }

  t.expectCompileResult(valid, code);
});







const kInvalidTypes = {
  mat2x2f: {
    expr: 'm',
    control: (e) => `${e}[0]`
  },

  array: {
    expr: 'arr',
    control: (e) => `${e}[0]`
  },

  ptr: {
    expr: '(&u)',
    control: (e) => `*${e}`
  },

  atomic: {
    expr: 'a',
    control: (e) => `atomicLoad(&${e})`
  },

  texture: {
    expr: 't',
    control: (e) => `textureLoad(${e}, vec2(), 0)`
  },

  sampler: {
    expr: 's',
    control: (e) => `textureSampleLevel(t, ${e}, vec2(), 0)`
  },

  struct: {
    expr: 'str',
    control: (e) => `${e}.u`
  }
};

g.test('invalid_types').
desc(
  `
  Validates that comparison expressions are never accepted for non-scalar and non-vector types.
  `
).
params((u) =>
u.
combine('op', keysOf(kComparisonOperators)).
combine('type', keysOf(kInvalidTypes)).
combine('control', [true, false]).
beginSubcases()
).
fn((t) => {
  const type = kInvalidTypes[t.params.type];
  const expr = t.params.control ? type.control(type.expr) : type.expr;
  const code = `
@group(0) @binding(0) var t : texture_2d<f32>;
@group(0) @binding(1) var s : sampler;
@group(0) @binding(2) var<storage, read_write> a : atomic<i32>;

struct S { u : u32 }

var<private> u : u32;
var<private> m : mat2x2f;
var<private> arr : array<i32, 4>;
var<private> str : S;

@compute @workgroup_size(1)
fn main() {
  let foo = ${expr} ${kComparisonOperators[t.params.op].op} ${expr};
}
`;

  t.expectCompileResult(t.params.control, code);
});