-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathtexture_utils.spec.ts
More file actions
435 lines (392 loc) · 15.5 KB
/
texture_utils.spec.ts
File metadata and controls
435 lines (392 loc) · 15.5 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
export const description = `
Tests for texture_utils.ts
`;
import { makeTestGroup } from '../../../../../../common/framework/test_group.js';
import { assert } from '../../../../../../common/util/util.js';
import {
getTextureFormatType,
isTextureFormatPossiblyMultisampled,
kDepthStencilFormats,
} from '../../../../../format_info.js';
import { AllFeaturesMaxLimitsGPUTest, GPUTest } from '../../../../../gpu_test.js';
import { getTextureDimensionFromView, virtualMipSize } from '../../../../../util/texture/base.js';
import {
kTexelRepresentationInfo,
PerTexelComponent,
TexelRepresentationInfo,
} from '../../../../../util/texture/texel_data.js';
import { kShaderStages } from '../../../../validation/decl/util.js';
import {
chooseTextureSize,
convertPerTexelComponentToResultFormat,
createTextureWithRandomDataAndGetTexels,
graphWeights,
makeRandomDepthComparisonTexelGenerator,
queryMipLevelMixWeightsForDevice,
readTextureToTexelViews,
SoftwareTexture,
softwareTextureRead,
swizzleTexel,
texelsApproximatelyEqual,
TextureCall,
vec2,
} from './texture_utils.js';
export const g = makeTestGroup(AllFeaturesMaxLimitsGPUTest);
function texelFormat(texel: Readonly<PerTexelComponent<number>>, rep: TexelRepresentationInfo) {
return rep.componentOrder.map(component => `${component}: ${texel[component]}`).join(', ');
}
g.test('createTextureWithRandomDataAndGetTexels_with_generator')
.desc(
`
Test createTextureWithRandomDataAndGetTexels with a generator. Generators
are only used with textureXXXCompare builtins as we need specific random
values to test these builtins with a depth reference value.
`
)
.params(u =>
u
.combine('format', kDepthStencilFormats)
.combine('viewDimension', ['2d', '2d-array', 'cube', 'cube-array'] as const)
)
.fn(async t => {
const { format, viewDimension } = t.params;
t.skipIfTextureFormatNotSupported(format);
t.skipIfTextureViewDimensionNotSupported(viewDimension);
t.skipIfTextureFormatAndViewDimensionNotCompatible(format, viewDimension);
// choose an odd size (9) so we're more likely to test alignment issue.
const size = chooseTextureSize({ minSize: 9, minBlocks: 4, format, viewDimension });
t.debug(`size: ${size.map(v => v.toString()).join(', ')}`);
const descriptor: GPUTextureDescriptor = {
format,
dimension: getTextureDimensionFromView(viewDimension),
size,
mipLevelCount: 3,
usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.TEXTURE_BINDING,
...(t.isCompatibility && { textureBindingViewDimension: viewDimension }),
};
await createTextureWithRandomDataAndGetTexels(t, descriptor, {
generator: makeRandomDepthComparisonTexelGenerator(descriptor, 'equal'),
});
// We don't expect any particular results. We just expect no validation errors.
});
g.test('readTextureToTexelViews')
.desc('test readTextureToTexelViews for various formats and dimensions')
.params(u =>
u
.combineWithParams([
{ srcFormat: 'r8unorm', texelViewFormat: 'rgba32float' },
{ srcFormat: 'r8sint', texelViewFormat: 'rgba32sint' },
{ srcFormat: 'r8uint', texelViewFormat: 'rgba32uint' },
{ srcFormat: 'rgba32float', texelViewFormat: 'rgba32float' },
{ srcFormat: 'rgba32uint', texelViewFormat: 'rgba32uint' },
{ srcFormat: 'rgba32sint', texelViewFormat: 'rgba32sint' },
{ srcFormat: 'depth24plus', texelViewFormat: 'rgba32float' },
{ srcFormat: 'depth24plus-stencil8', texelViewFormat: 'rgba32float' },
{ srcFormat: 'stencil8', texelViewFormat: 'stencil8' },
] as const)
.combine('viewDimension', ['1d', '2d', '2d-array', '3d', 'cube', 'cube-array'] as const)
.combine('sampleCount', [1, 4] as const)
.unless(
t =>
t.sampleCount > 1 &&
(!isTextureFormatPossiblyMultisampled(t.srcFormat) || t.viewDimension !== '2d')
)
)
.fn(async t => {
const { srcFormat, texelViewFormat, viewDimension, sampleCount } = t.params;
t.skipIfTextureViewDimensionNotSupported(viewDimension);
t.skipIfTextureFormatAndViewDimensionNotCompatible(srcFormat, viewDimension);
if (sampleCount > 1) {
t.skipIfTextureFormatNotMultisampled(srcFormat);
}
// choose an odd size (9) so we're more likely to test alignment issue.
const size = chooseTextureSize({ minSize: 9, minBlocks: 4, format: srcFormat, viewDimension });
t.debug(`size: ${size.map(v => v.toString()).join(', ')}`);
const descriptor: GPUTextureDescriptor = {
format: srcFormat,
dimension: getTextureDimensionFromView(viewDimension),
size,
mipLevelCount: viewDimension === '1d' || sampleCount > 1 ? 1 : 3,
usage:
sampleCount > 1
? GPUTextureUsage.COPY_DST |
GPUTextureUsage.TEXTURE_BINDING |
GPUTextureUsage.RENDER_ATTACHMENT
: GPUTextureUsage.COPY_DST | GPUTextureUsage.TEXTURE_BINDING,
sampleCount,
...(t.isCompatibility && { textureBindingViewDimension: viewDimension }),
};
const { texels: expectedTexelViews, texture } = await createTextureWithRandomDataAndGetTexels(
t,
descriptor
);
const actualTexelViews = await readTextureToTexelViews(t, texture, descriptor, texelViewFormat);
assert(actualTexelViews.length === expectedTexelViews.length, 'num mip levels match');
const type = getTextureFormatType(srcFormat, 'all');
const textureType =
type === 'depth'
? 'texture_depth_2d'
: type === 'uint'
? 'texture_2d<u32>'
: type === 'sint'
? 'texture_2d<i32>'
: 'texture_2d<f32>';
const errors = [];
for (let mipLevel = 0; mipLevel < actualTexelViews.length; ++mipLevel) {
const actualMipLevelTexelView = actualTexelViews[mipLevel];
const expectedMipLevelTexelView = expectedTexelViews[mipLevel];
const mipLevelSize = virtualMipSize(texture.dimension, size, mipLevel);
const actualRep = kTexelRepresentationInfo[actualMipLevelTexelView.format];
const expectedRep = kTexelRepresentationInfo[expectedMipLevelTexelView.format];
for (let z = 0; z < mipLevelSize[2]; ++z) {
for (let y = 0; y < mipLevelSize[1]; ++y) {
for (let x = 0; x < mipLevelSize[0]; ++x) {
for (let sampleIndex = 0; sampleIndex < sampleCount; ++sampleIndex) {
const actual = actualMipLevelTexelView.color({ x, y, z, sampleIndex });
const expected = expectedMipLevelTexelView.color({ x, y, z, sampleIndex });
const actualRGBA = convertPerTexelComponentToResultFormat(
actual,
actualMipLevelTexelView.format
);
const expectedRGBA = convertPerTexelComponentToResultFormat(
expected,
expectedMipLevelTexelView.format
);
// This currently expects the exact same values in actual vs expected.
// It's possible this needs to be relaxed slightly but only for non-integer formats.
// For now, if the tests pass everywhere, we'll keep it at 0 tolerance.
const maxFractionalDiff = 0;
if (
!texelsApproximatelyEqual(
t.device,
'textureLoad',
textureType,
actualRGBA,
actualMipLevelTexelView.format,
expectedRGBA,
expectedMipLevelTexelView.format,
maxFractionalDiff
)
) {
const actualStr = texelFormat(actual, actualRep);
const expectedStr = texelFormat(expected, expectedRep);
errors.push(
`texel at ${x}, ${y}, ${z}, sampleIndex: ${sampleIndex} expected: ${expectedStr}, actual: ${actualStr}`
);
}
}
}
}
}
assert(errors.length === 0, errors.join('\n'));
}
});
function validateWeights(t: GPUTest, stage: string, builtin: string, weights: number[]) {
const kNumMixSteps = weights.length - 1;
const showWeights = () => `
${weights.map((v, i) => `${i.toString().padStart(2)}: ${v}`).join('\n')}
e = expected
A = actual
${graphWeights(32, weights)}
`;
t.expect(
weights[0] === 0,
`stage: ${stage}, ${builtin}, weight 0 expected 0 but was ${weights[0]}\n${showWeights()}`
);
t.expect(
weights[kNumMixSteps] === 1,
`stage: ${stage}, ${builtin}, top weight expected 1 but was ${
weights[kNumMixSteps]
}\n${showWeights()}`
);
const dx = 1 / kNumMixSteps;
for (let i = 0; i < kNumMixSteps; ++i) {
const dy = weights[i + 1] - weights[i];
// dy / dx because dy might be 0
const slope = dy / dx;
// Validate the slope is not going down.
assert(
slope >= 0,
`stage: ${stage}, ${builtin}, weight[${i}] was not <= weight[${i + 1}]\n${showWeights()}`
);
// Validate the slope is not going up too steeply.
// The correct slope is 1 / kNumMixSteps but Mac AMD and Mac Intel
// have the wrong mix weights. 2 is enough to pass Mac AMD which we
// decided is ok but will fail on Mac Intel in compute stage which we
// decides is not ok.
assert(
slope <= 2,
`stage: ${stage}, ${builtin}, slope from weight[${i}] to weight[${
i + 1
}] is > 2.\n${showWeights()}`
);
}
// Test that we don't have a mostly flat set of weights.
// Note: Ideally every value is unique but 66% is enough to pass AMD Mac
// which we decided was ok but high enough to fail Intel Mac in a compute stage
// which we decided is not ok.
const kMinPercentUniqueWeights = 66;
t.expect(
new Set(weights).size >= ((weights.length * kMinPercentUniqueWeights * 0.01) | 0),
`stage: ${stage}, ${builtin}, expected at least ~${kMinPercentUniqueWeights}% unique weights\n${showWeights()}`
);
}
g.test('weights')
.desc(
`
Test the mip level weights are linear.
Given 2 mip levels, textureSampleLevel(....., mipLevel) should return
mix(colorFromLevel0, colorFromLevel1, mipLevel).
Similarly, textureSampleGrad(...., ddx, ...) where ddx is
vec2(mix(1.0, 2.0, mipLevel) / textureWidth, 0) should so return
mix(colorFromLevel0, colorFromLevel1, mipLevel).
If we put 0,0,0,0 in level 0 and 1,1,1,1 in level 1 then we should arguably
be able to assert
for (mipLevel = 0; mipLevel <= 1, mipLevel += 0.01) {
assert(textureSampleLevel(t, s, vec2f(0.5), mipLevel) === mipLevel)
ddx = vec2(mix(1.0, 2.0, mipLevel) / textureWidth, 0)
assert(textureSampleGrad(t, s, vec2f(0.5), ddx, vec2f(0)) === mipLevel)
}
Unfortunately, the GPUs do not do this. In particular:
AMD Mac goes like this: Not great but we allow it
+----------------+
| ***|
| ** |
| * |
| ** |
| ** |
| * |
| ** |
|*** |
+----------------+
Intel Mac goes like this in a compute stage
+----------------+
| *******|
| * |
| * |
| * |
| * |
| * |
| * |
|******* |
+----------------+
Where as they should go like this
+----------------+
| **|
| ** |
| ** |
| ** |
| ** |
| ** |
| ** |
|** |
+----------------+
To make the texture builtin tests pass, they use the mix weights we query from the GPU
even if they are arguably bad. This test is to surface the failure of the GPU
to use mix weights the approximate a linear interpolation.
We allow the AMD case as but disallow extreme Intel case. WebGPU implementations
are supposed to work around this issue by poly-filling on devices that fail this test.
`
)
.params(u => u.combine('stage', kShaderStages))
.fn(async t => {
const { stage } = t.params;
const weights = await queryMipLevelMixWeightsForDevice(t, t.params.stage);
validateWeights(t, stage, 'textureSampleLevel', weights.sampleLevelWeights);
validateWeights(t, stage, 'textureSampleGrad', weights.softwareMixToGPUMixGradWeights);
});
g.test('softwareSamplerSwizzle')
.desc('test the software sampler swizzles')
.params(u =>
u
.combine('srcFormat', ['rgba8unorm', 'bgra8unorm', 'depth24plus', 'stencil8'] as const)
.beginSubcases()
.combine('swizzle', ['rgba', 'abgr', '10gr'] as const)
.expand('textureType', function* (t) {
const type = getTextureFormatType(t.srcFormat, 'all');
switch (type) {
case 'depth':
yield 'texture_2d<f32>';
yield 'texture_depth_2d';
break;
case 'uint':
yield 'texture_2d<u32>';
break;
case 'sint':
yield 'texture_2d<i32>';
break;
default:
yield 'texture_2d<f32>';
}
})
)
.fn(async t => {
t.skipIfDeviceDoesNotHaveFeature('texture-component-swizzle');
const { srcFormat, swizzle, textureType } = t.params;
const size = chooseTextureSize({ minSize: 9, minBlocks: 4, format: srcFormat });
t.debug(`size: ${size.map(v => v.toString()).join(', ')}`);
const descriptor: GPUTextureDescriptor = {
format: srcFormat,
size,
usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.TEXTURE_BINDING,
};
const { texels, texture } = await createTextureWithRandomDataAndGetTexels(t, descriptor);
const viewDescriptor = { swizzle };
const softwareTexture: SoftwareTexture = {
texels,
descriptor,
viewDescriptor,
};
const call: TextureCall<vec2> = {
builtin: 'textureLoad',
coordType: 'u',
mipLevel: 0,
coords: [0, 0],
};
const type = getTextureFormatType(srcFormat, 'all');
const resultFormat =
type === 'uint' ? 'rgba32uint' : type === 'sint' ? 'rgba32sint' : 'rgba32float';
const aspect = srcFormat === 'stencil8' ? 'stencil-only' : 'all';
// For each pixel in the texelView, convert it to RGBA and swizzle.
// Then, call softwareTextureRead for that texel. The results should
// be the same.
const stage = 'compute';
const errors = [];
for (let mipLevel = 0; mipLevel < texels.length; ++mipLevel) {
const expectedMipLevelTexelView = texels[mipLevel];
const mipLevelSize = virtualMipSize(texture.dimension, size, mipLevel);
const rep = kTexelRepresentationInfo[expectedMipLevelTexelView.format];
for (let y = 0; y < mipLevelSize[1]; ++y) {
for (let x = 0; x < mipLevelSize[0]; ++x) {
const expectedRaw = expectedMipLevelTexelView.color({ x, y, z: 0 });
const expectedRGBA = convertPerTexelComponentToResultFormat(
expectedRaw,
resultFormat,
aspect
);
const expected = swizzleTexel(expectedRGBA, swizzle);
call.coords = [x, y];
call.mipLevel = mipLevel;
const actual = softwareTextureRead<vec2>(t, stage, call, softwareTexture);
const maxFractionalDiff = 0;
if (
!texelsApproximatelyEqual(
t.device,
call.builtin,
textureType,
actual,
resultFormat,
expected,
resultFormat,
maxFractionalDiff
)
) {
const actualStr = texelFormat(actual, rep);
const expectedStr = texelFormat(expected, rep);
errors.push(`texel at ${x}, ${y}, expected: ${expectedStr}, actual: ${actualStr}`);
}
}
}
assert(errors.length === 0, errors.join('\n'));
}
});