-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathtextureSampleLevel.spec.ts
More file actions
389 lines (358 loc) · 13.4 KB
/
Copy pathtextureSampleLevel.spec.ts
File metadata and controls
389 lines (358 loc) · 13.4 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
const builtin = 'textureSampleLevel';
export const description = `
Validation tests for the ${builtin}() builtin.
* test textureSampleLevel coords parameter must be correct type
* test textureSampleLevel array_index parameter must be correct type
* test textureSampleLevel level parameter must be correct type
* test textureSampleLevel offset parameter must be correct type
* test textureSampleLevel offset parameter must be a const-expression
* test textureSampleLevel offset parameter must be between -8 and +7 inclusive
* test textureSampleLevel returns the correct type
* test textureSampleLevel doesn't work with texture types it's not supposed to
`;
import { makeTestGroup } from '../../../../../../common/framework/test_group.js';
import { keysOf, objectsToRecord } from '../../../../../../common/util/data_tables.js';
import {
Type,
kAllScalarsAndVectors,
isConvertible,
ScalarType,
VectorType,
isUnsignedType,
} from '../../../../../util/conversion.js';
import { ShaderValidationTest } from '../../../shader_validation_test.js';
import { kTestTextureTypes } from './shader_builtin_utils.js';
type TextureSampleLevelArguments = {
coordsArgType: ScalarType | VectorType;
hasArrayIndexArg?: boolean;
levelIsF32?: boolean;
offsetArgType?: VectorType;
};
const kValidTextureSampleLevelParameterTypes: { [n: string]: TextureSampleLevelArguments } = {
'texture_1d<f32>': { coordsArgType: Type.f32, levelIsF32: true },
'texture_2d<f32>': { coordsArgType: Type.vec2f, levelIsF32: true, offsetArgType: Type.vec2i },
'texture_2d_array<f32>': {
coordsArgType: Type.vec2f,
hasArrayIndexArg: true,
levelIsF32: true,
offsetArgType: Type.vec2i,
},
'texture_3d<f32>': { coordsArgType: Type.vec3f, levelIsF32: true, offsetArgType: Type.vec3i },
'texture_cube<f32>': { coordsArgType: Type.vec3f, levelIsF32: true },
'texture_cube_array<f32>': {
coordsArgType: Type.vec3f,
hasArrayIndexArg: true,
levelIsF32: true,
},
texture_depth_2d: { coordsArgType: Type.vec2f, offsetArgType: Type.vec2i },
texture_depth_2d_array: {
coordsArgType: Type.vec2f,
hasArrayIndexArg: true,
offsetArgType: Type.vec2i,
},
texture_depth_cube: { coordsArgType: Type.vec3f },
texture_depth_cube_array: { coordsArgType: Type.vec3f, hasArrayIndexArg: true },
} as const;
const kTextureTypes = keysOf(kValidTextureSampleLevelParameterTypes);
const kValuesTypes = objectsToRecord(kAllScalarsAndVectors);
export const g = makeTestGroup(ShaderValidationTest);
g.test('return_type')
.specURL('https://gpuweb.github.io/gpuweb/wgsl/#texturesamplelevel')
.desc(
`
Validates the return type of ${builtin} is the expected type.
`
)
.params(u =>
u
.combine('returnType', keysOf(kValuesTypes))
.combine('textureType', keysOf(kValidTextureSampleLevelParameterTypes))
.beginSubcases()
.expand('offset', t =>
kValidTextureSampleLevelParameterTypes[t.textureType].offsetArgType
? [false, true]
: [false]
)
)
.fn(t => {
const { returnType, textureType, offset } = t.params;
const returnVarType = kValuesTypes[returnType];
const { offsetArgType, coordsArgType, hasArrayIndexArg } =
kValidTextureSampleLevelParameterTypes[textureType];
const returnExpectedType = textureType.includes('depth') ? Type.f32 : Type.vec4f;
const varWGSL = returnVarType.toString();
const coordWGSL = coordsArgType.create(0).wgsl();
const arrayWGSL = hasArrayIndexArg ? ', 0' : '';
const offsetWGSL = offset ? `, ${offsetArgType?.create(0).wgsl()}` : '';
const code = `
@group(0) @binding(0) var s: sampler;
@group(0) @binding(1) var t: ${textureType};
@fragment fn fs() -> @location(0) vec4f {
let v: ${varWGSL} = textureSampleLevel(t, s, ${coordWGSL}${arrayWGSL}, 0${offsetWGSL});
return vec4f(0);
}
`;
const expectSuccess = isConvertible(returnExpectedType, returnVarType);
t.expectCompileResult(expectSuccess, code);
});
g.test('coords_argument')
.specURL('https://gpuweb.github.io/gpuweb/wgsl/#texturesamplelevel')
.desc(
`
Validates that only incorrect coords arguments are rejected by ${builtin}
`
)
.params(u =>
u
.combine('textureType', keysOf(kValidTextureSampleLevelParameterTypes))
.combine('coordType', keysOf(kValuesTypes))
.beginSubcases()
.combine('value', [-1, 0, 1] as const)
// filter out unsigned types with negative values
.filter(t => !isUnsignedType(kValuesTypes[t.coordType]) || t.value >= 0)
.expand('offset', t =>
kValidTextureSampleLevelParameterTypes[t.textureType].offsetArgType
? [false, true]
: [false]
)
)
.fn(t => {
const { textureType, coordType, offset, value } = t.params;
const coordArgType = kValuesTypes[coordType];
const {
offsetArgType,
coordsArgType: coordsRequiredType,
hasArrayIndexArg,
} = kValidTextureSampleLevelParameterTypes[textureType];
const coordWGSL = coordArgType.create(value).wgsl();
const arrayWGSL = hasArrayIndexArg ? ', 0' : '';
const offsetWGSL = offset ? `, ${offsetArgType?.create(0).wgsl()}` : '';
const code = `
@group(0) @binding(0) var s: sampler;
@group(0) @binding(1) var t: ${textureType};
@fragment fn fs() -> @location(0) vec4f {
let v = textureSampleLevel(t, s, ${coordWGSL}${arrayWGSL}, 0${offsetWGSL});
return vec4f(0);
}
`;
const expectSuccess = isConvertible(coordArgType, coordsRequiredType);
t.expectCompileResult(expectSuccess, code);
});
g.test('array_index_argument')
.specURL('https://gpuweb.github.io/gpuweb/wgsl/#texturesamplelevel')
.desc(
`
Validates that only incorrect array_index arguments are rejected by ${builtin}
`
)
.params(u =>
u
.combine('textureType', kTextureTypes)
// filter out types with no array_index
.filter(t => !!kValidTextureSampleLevelParameterTypes[t.textureType].hasArrayIndexArg)
.combine('arrayIndexType', keysOf(kValuesTypes))
.beginSubcases()
.combine('value', [-9, -8, 0, 7, 8])
// filter out unsigned types with negative values
.filter(t => !isUnsignedType(kValuesTypes[t.arrayIndexType]) || t.value >= 0)
.expand('offset', t =>
kValidTextureSampleLevelParameterTypes[t.textureType].offsetArgType
? [false, true]
: [false]
)
)
.fn(t => {
const { textureType, arrayIndexType, value, offset } = t.params;
const arrayIndexArgType = kValuesTypes[arrayIndexType];
const args = [arrayIndexArgType.create(value)];
const { coordsArgType, offsetArgType } = kValidTextureSampleLevelParameterTypes[textureType];
const coordWGSL = coordsArgType.create(0).wgsl();
const arrayWGSL = args.map(arg => arg.wgsl()).join(', ');
const offsetWGSL = offset ? `, ${offsetArgType!.create(0).wgsl()}` : '';
const code = `
@group(0) @binding(0) var s: sampler;
@group(0) @binding(1) var t: ${textureType};
@fragment fn fs() -> @location(0) vec4f {
let v = textureSampleLevel(t, s, ${coordWGSL}, ${arrayWGSL}, 0${offsetWGSL});
return vec4f(0);
}
`;
const expectSuccess =
isConvertible(arrayIndexArgType, Type.i32) || isConvertible(arrayIndexArgType, Type.u32);
t.expectCompileResult(expectSuccess, code);
});
g.test('level_argument')
.specURL('https://gpuweb.github.io/gpuweb/wgsl/#texturesamplelevel')
.desc(
`
Validates that only incorrect level arguments are rejected by ${builtin}
`
)
.params(u =>
u
.combine('textureType', kTextureTypes)
.combine('levelType', keysOf(kValuesTypes))
.beginSubcases()
.combine('value', [-1, 0, 1])
// filter out unsigned types with negative values
.filter(t => !isUnsignedType(kValuesTypes[t.levelType]) || t.value >= 0)
.expand('offset', t =>
kValidTextureSampleLevelParameterTypes[t.textureType].offsetArgType
? [false, true]
: [false]
)
)
.fn(t => {
const { textureType, levelType, value, offset } = t.params;
const levelArgType = kValuesTypes[levelType];
const args = [levelArgType.create(value)];
const { coordsArgType, hasArrayIndexArg, offsetArgType, levelIsF32 } =
kValidTextureSampleLevelParameterTypes[textureType];
const coordWGSL = coordsArgType.create(0).wgsl();
const arrayWGSL = hasArrayIndexArg ? ', 0' : '';
const levelWGSL = args.map(arg => arg.wgsl()).join(', ');
const offsetWGSL = offset ? `, ${offsetArgType!.create(0).wgsl()}` : '';
const code = `
@group(0) @binding(0) var s: sampler;
@group(0) @binding(1) var t: ${textureType};
@fragment fn fs() -> @location(0) vec4f {
let v = textureSampleLevel(t, s, ${coordWGSL}${arrayWGSL}, ${levelWGSL}${offsetWGSL});
return vec4f(0);
}
`;
const expectSuccess = levelIsF32
? isConvertible(levelArgType, Type.f32)
: isConvertible(levelArgType, Type.i32) || isConvertible(levelArgType, Type.u32);
t.expectCompileResult(expectSuccess, code);
});
g.test('offset_argument')
.specURL('https://gpuweb.github.io/gpuweb/wgsl/#texturesamplelevel')
.desc(
`
Validates that only incorrect offset arguments are rejected by ${builtin}
`
)
.params(u =>
u
.combine('textureType', kTextureTypes)
// filter out types with no offset
.filter(t => !!kValidTextureSampleLevelParameterTypes[t.textureType].offsetArgType)
.combine('offsetType', keysOf(kValuesTypes))
.beginSubcases()
.combine('value', [-9, -8, 0, 7, 8])
// filter out unsigned types with negative values
.filter(t => !isUnsignedType(kValuesTypes[t.offsetType]) || t.value >= 0)
)
.fn(t => {
const { textureType, offsetType, value } = t.params;
const offsetArgType = kValuesTypes[offsetType];
const args = [offsetArgType.create(value)];
const {
coordsArgType,
hasArrayIndexArg,
offsetArgType: offsetRequiredType,
} = kValidTextureSampleLevelParameterTypes[textureType];
const coordWGSL = coordsArgType.create(0).wgsl();
const arrayWGSL = hasArrayIndexArg ? ', 0' : '';
const offsetWGSL = args.map(arg => arg.wgsl()).join(', ');
const code = `
@group(0) @binding(0) var s: sampler;
@group(0) @binding(1) var t: ${textureType};
@fragment fn fs() -> @location(0) vec4f {
let v = textureSampleLevel(t, s, ${coordWGSL}${arrayWGSL}, 0, ${offsetWGSL});
return vec4f(0);
}
`;
const expectSuccess =
isConvertible(offsetArgType, offsetRequiredType!) && value >= -8 && value <= 7;
t.expectCompileResult(expectSuccess, code);
});
g.test('offset_argument,non_const')
.specURL('https://gpuweb.github.io/gpuweb/wgsl/#texturesamplelevel')
.desc(
`
Validates that only non-const offset arguments are rejected by ${builtin}
`
)
.params(u =>
u
.combine('textureType', kTextureTypes)
.combine('varType', ['c', 'u', 'l'])
// filter out types with no offset
.filter(t => !!kValidTextureSampleLevelParameterTypes[t.textureType].offsetArgType)
)
.fn(t => {
const { textureType, varType } = t.params;
const { coordsArgType, hasArrayIndexArg, offsetArgType } =
kValidTextureSampleLevelParameterTypes[textureType];
const coordWGSL = coordsArgType.create(0).wgsl();
const arrayWGSL = hasArrayIndexArg ? ', 0' : '';
const offsetWGSL = `${offsetArgType}(${varType})`;
const code = `
@group(0) @binding(0) var s: sampler;
@group(0) @binding(1) var t: ${textureType};
@group(0) @binding(2) var<uniform> u: ${offsetArgType};
@fragment fn fs() -> @location(0) vec4f {
const c = 1;
let l = ${offsetArgType!.create(0).wgsl()};
let v = textureSampleLevel(t, s, ${coordWGSL}${arrayWGSL}, 0, ${offsetWGSL});
return vec4f(0);
}
`;
const expectSuccess = varType === 'c';
t.expectCompileResult(expectSuccess, code);
});
g.test('texture_type')
.specURL('https://gpuweb.github.io/gpuweb/wgsl/#texturesamplelevel')
.desc(
`
Validates that incompatible texture types don't work with ${builtin}
`
)
.params(u =>
u
.combine('testTextureType', kTestTextureTypes)
.beginSubcases()
.combine('textureType', keysOf(kValidTextureSampleLevelParameterTypes))
.expand('offset', t =>
kValidTextureSampleLevelParameterTypes[t.textureType].offsetArgType
? [false, true]
: [false]
)
)
.fn(t => {
const { testTextureType, textureType, offset } = t.params;
const { coordsArgType, offsetArgType, hasArrayIndexArg } =
kValidTextureSampleLevelParameterTypes[textureType];
const coordWGSL = coordsArgType.create(0).wgsl();
const arrayWGSL = hasArrayIndexArg ? ', 0' : '';
const offsetWGSL = offset ? `, ${offsetArgType?.create(0).wgsl()}` : '';
const code = `
@group(0) @binding(0) var s: sampler;
@group(0) @binding(1) var t: ${testTextureType};
@fragment fn fs() -> @location(0) vec4f {
let v = textureSampleLevel(t, s, ${coordWGSL}${arrayWGSL}, 0${offsetWGSL});
return vec4f(0);
}
`;
const types = kValidTextureSampleLevelParameterTypes[testTextureType];
const typesMatch = types
? types.coordsArgType === coordsArgType &&
types.hasArrayIndexArg === hasArrayIndexArg &&
(offset ? types.offsetArgType === offsetArgType : true)
: false;
const expectSuccess = testTextureType === textureType || typesMatch;
t.expectCompileResult(expectSuccess, code);
});
g.test('must_use')
.desc('Tests that the result must be used')
.params(u => u.combine('use', [true, false] as const))
.fn(t => {
const code = `
@group(0) @binding(0) var t : texture_2d<f32>;
@group(0) @binding(1) var s : sampler;
fn foo() {
${t.params.use ? '_ =' : ''} textureSampleLevel(t,s,vec2(0,0), 0);
}`;
t.expectCompileResult(t.params.use, code);
});