summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/tests/cts/checkout/src/webgpu/shader/validation/expression/call/builtin/length.spec.ts
blob: 003ad6811d72a027739afa42b7afb4d316ecdf42 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
const builtin = 'length';
export const description = `
Validation tests for the ${builtin}() builtin.
`;

import { makeTestGroup } from '../../../../../../common/framework/test_group.js';
import { keysOf, objectsToRecord } from '../../../../../../common/util/data_tables.js';
import {
  ScalarType,
  Type,
  kConcreteIntegerScalarsAndVectors,
  kConvertableToFloatScalar,
  kConvertableToFloatVec2,
  kConvertableToFloatVec3,
  kConvertableToFloatVec4,
  scalarTypeOf,
} from '../../../../../util/conversion.js';
import { isRepresentable } from '../../../../../util/floating_point.js';
import { ShaderValidationTest } from '../../../shader_validation_test.js';

import {
  fullRangeForType,
  kConstantAndOverrideStages,
  stageSupportsType,
  validateConstOrOverrideBuiltinEval,
} from './const_override_validation.js';

export const g = makeTestGroup(ShaderValidationTest);

/**
 * Evaluates the result and information about a call to length(), with a vector
 * formed from `vec` of the element type `type`.
 */
function calculate(
  vec: (number | bigint)[],
  type: ScalarType
): {
  /**
   * True iff the sum of the squares can be represented by the data type.
   * @note The specification does not enforce the method or precision of how
   * length() is calculated. If intermediate is not representable but the result
   * is representable, then the test case is skipped as it is undefined whether
   * the evaluation should error or not.
   */
  isIntermediateRepresentable: boolean;
  /** True iff the result of length() can be represented by the data type. */
  isResultRepresentable: boolean;
  /** The computed value of length(). */
  result: number;
} {
  const vec_number = vec.map(e => Number(e));
  const squareSum = vec_number.reduce((prev, curr) => prev + Number(curr) * Number(curr), 0);
  const result = Math.sqrt(squareSum);
  return {
    isIntermediateRepresentable: isRepresentable(
      squareSum,
      // AbstractInt is converted to AbstractFloat before calling into the builtin
      scalarTypeOf(type).kind === 'abstract-int' ? Type.abstractFloat : scalarTypeOf(type)
    ),
    isResultRepresentable: isRepresentable(
      result,
      // AbstractInt is converted to AbstractFloat before calling into the builtin
      scalarTypeOf(type).kind === 'abstract-int' ? Type.abstractFloat : scalarTypeOf(type)
    ),
    result,
  };
}

const kScalarTypes = objectsToRecord(kConvertableToFloatScalar);

g.test('scalar')
  .desc(
    `
Validates that constant evaluation and override evaluation of ${builtin}() with
the input scalar value always compiles without error
`
  )
  .params(u =>
    u
      .combine('stage', kConstantAndOverrideStages)
      .combine('type', keysOf(kScalarTypes))
      .filter(u => stageSupportsType(u.stage, kScalarTypes[u.type]))
      .beginSubcases()
      .expand('value', u => fullRangeForType(kScalarTypes[u.type]))
  )
  .beforeAllSubcases(t => {
    if (scalarTypeOf(kScalarTypes[t.params.type]) === Type.f16) {
      t.selectDeviceOrSkipTestCase('shader-f16');
    }
  })
  .fn(t => {
    // We only validate with numbers known to be representable by the type
    const expectedResult = true;
    validateConstOrOverrideBuiltinEval(
      t,
      builtin,
      expectedResult,
      [kScalarTypes[t.params.type].create(t.params.value)],
      t.params.stage
    );
  });

const kVec2Types = objectsToRecord(kConvertableToFloatVec2);

g.test('vec2')
  .desc(
    `
Validates that constant evaluation and override evaluation of ${builtin}() with a vec2 compiles with valid values
`
  )
  .params(u =>
    u
      .combine('stage', kConstantAndOverrideStages)
      .combine('type', keysOf(kVec2Types))
      .filter(u => stageSupportsType(u.stage, kVec2Types[u.type]))
      .beginSubcases()
      .expand('x', u => fullRangeForType(kVec2Types[u.type], 5))
      .expand('y', u => fullRangeForType(kVec2Types[u.type], 5))
      .expand('_result', u => [calculate([u.x, u.y], scalarTypeOf(kVec2Types[u.type]))])
      .filter(u => u._result.isResultRepresentable === u._result.isIntermediateRepresentable)
  )
  .beforeAllSubcases(t => {
    if (scalarTypeOf(kVec2Types[t.params.type]) === Type.f16) {
      t.selectDeviceOrSkipTestCase('shader-f16');
    }
  })
  .fn(t => {
    const expectedResult = t.params._result.isResultRepresentable;
    validateConstOrOverrideBuiltinEval(
      t,
      builtin,
      expectedResult,
      [kVec2Types[t.params.type].create([t.params.x, t.params.y])],
      t.params.stage
    );
  });

const kVec3Types = objectsToRecord(kConvertableToFloatVec3);

g.test('vec3')
  .desc(
    `
Validates that constant evaluation and override evaluation of ${builtin}() with a vec3 compiles with valid values
`
  )
  .params(u =>
    u
      .combine('stage', kConstantAndOverrideStages)
      .combine('type', keysOf(kVec3Types))
      .filter(u => stageSupportsType(u.stage, kVec3Types[u.type]))
      .beginSubcases()
      .expand('x', u => fullRangeForType(kVec3Types[u.type], 4))
      .expand('y', u => fullRangeForType(kVec3Types[u.type], 4))
      .expand('z', u => fullRangeForType(kVec3Types[u.type], 4))
      .expand('_result', u => [calculate([u.x, u.y, u.z], scalarTypeOf(kVec3Types[u.type]))])
      .filter(u => u._result.isResultRepresentable === u._result.isIntermediateRepresentable)
  )
  .beforeAllSubcases(t => {
    if (scalarTypeOf(kVec3Types[t.params.type]) === Type.f16) {
      t.selectDeviceOrSkipTestCase('shader-f16');
    }
  })
  .fn(t => {
    const expectedResult = t.params._result.isResultRepresentable;
    validateConstOrOverrideBuiltinEval(
      t,
      builtin,
      expectedResult,
      [kVec3Types[t.params.type].create([t.params.x, t.params.y, t.params.z])],
      t.params.stage
    );
  });

const kVec4Types = objectsToRecord(kConvertableToFloatVec4);

g.test('vec4')
  .desc(
    `
Validates that constant evaluation and override evaluation of ${builtin}() with a vec4 compiles with valid values
`
  )
  .params(u =>
    u
      .combine('stage', kConstantAndOverrideStages)
      .combine('type', keysOf(kVec4Types))
      .filter(u => stageSupportsType(u.stage, kVec4Types[u.type]))
      .beginSubcases()
      .expand('x', u => fullRangeForType(kVec4Types[u.type], 3))
      .expand('y', u => fullRangeForType(kVec4Types[u.type], 3))
      .expand('z', u => fullRangeForType(kVec4Types[u.type], 3))
      .expand('w', u => fullRangeForType(kVec4Types[u.type], 3))
      .expand('_result', u => [calculate([u.x, u.y, u.z, u.w], scalarTypeOf(kVec4Types[u.type]))])
      .filter(u => u._result.isResultRepresentable === u._result.isIntermediateRepresentable)
  )
  .beforeAllSubcases(t => {
    if (scalarTypeOf(kVec4Types[t.params.type]) === Type.f16) {
      t.selectDeviceOrSkipTestCase('shader-f16');
    }
  })
  .fn(t => {
    const expectedResult = t.params._result.isResultRepresentable;
    validateConstOrOverrideBuiltinEval(
      t,
      builtin,
      expectedResult,
      [kVec4Types[t.params.type].create([t.params.x, t.params.y, t.params.z, t.params.w])],
      t.params.stage
    );
  });

const kIntegerArgumentTypes = objectsToRecord([Type.f32, ...kConcreteIntegerScalarsAndVectors]);

g.test('integer_argument')
  .desc(
    `
Validates that scalar and vector integer arguments are rejected by ${builtin}()
`
  )
  .params(u => u.combine('type', keysOf(kIntegerArgumentTypes)))
  .fn(t => {
    const type = kIntegerArgumentTypes[t.params.type];
    validateConstOrOverrideBuiltinEval(
      t,
      builtin,
      /* expectedResult */ type === Type.f32,
      [type.create(1)],
      'constant'
    );
  });