summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/tests/cts/checkout/src/webgpu/util/texture/texture_ok.spec.ts
blob: a3ebcb1f4cfe208c19ea3f15a3b3aabf518d9346 (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
export const description = 'checkPixels helpers behave as expected against real textures';

import { makeTestGroup } from '../../../common/framework/test_group.js';
import { GPUTest } from '../../gpu_test.js';

import { TexelView } from './texel_view.js';
import { textureContentIsOKByT2B } from './texture_ok.js';

export const g = makeTestGroup(GPUTest);

g.test('float32')
  .desc(`Basic test that actual/expected must match, for float32.`)
  .params(u =>
    u
      .combineWithParams([
        { format: 'rgba32float' }, //
        { format: 'rg32float' },
      ] as const)
      .beginSubcases()
      .combineWithParams([
        // Expected data is 0.6 in all channels
        { data: 0.6, opts: { maxFractionalDiff: 0.0000001 }, _ok: true },
        { data: 0.6, opts: { maxDiffULPsForFloatFormat: 1 }, _ok: true },

        { data: 0.5999, opts: { maxFractionalDiff: 0 }, _ok: false },
        { data: 0.5999, opts: { maxFractionalDiff: 0.0001001 }, _ok: true },

        { data: 0.6001, opts: { maxFractionalDiff: 0 }, _ok: false },
        { data: 0.6001, opts: { maxFractionalDiff: 0.0001001 }, _ok: true },

        { data: 0.5999, opts: { maxDiffULPsForFloatFormat: 1677 }, _ok: false },
        { data: 0.5999, opts: { maxDiffULPsForFloatFormat: 1678 }, _ok: true },

        { data: 0.6001, opts: { maxDiffULPsForFloatFormat: 1676 }, _ok: false },
        { data: 0.6001, opts: { maxDiffULPsForFloatFormat: 1677 }, _ok: true },
      ])
  )
  .fn(async t => {
    const { format, data, opts, _ok } = t.params;

    const size = [1, 1];
    const texture = t.device.createTexture({
      format,
      size,
      usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.COPY_SRC,
    });
    t.trackForCleanup(texture);
    t.device.queue.writeTexture({ texture }, new Float32Array([data, data, data, data]), {}, size);

    const expColor = { R: 0.6, G: 0.6, B: 0.6, A: 0.6 };
    const expTexelView = TexelView.fromTexelsAsColors(format, coords => expColor);

    const result = await textureContentIsOKByT2B(t, { texture }, size, { expTexelView }, opts);
    t.expect((result === undefined) === _ok, `expected ${_ok}, got ${result === undefined}`);
  });

g.test('norm')
  .desc(`Basic test that actual/expected must match, for unorm/snorm.`)
  .params(u =>
    u
      .combine('mode', ['bytes', 'colors'] as const)
      .combineWithParams([
        { format: 'r8unorm', _maxValue: 255 },
        { format: 'r8snorm', _maxValue: 127 },
      ] as const)
      .beginSubcases()
      .combineWithParams([
        // Expected data is [10, 10]
        { data: [10, 10], _ok: true },
        { data: [10, 11], _ok: false },
        { data: [11, 10], _ok: false },
        { data: [11, 11], _ok: false },
      ])
  )
  .fn(async t => {
    const { mode, format, _maxValue, data, _ok } = t.params;

    const size = [2, 1];
    const texture = t.device.createTexture({
      format,
      size,
      usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.COPY_SRC,
    });
    t.trackForCleanup(texture);
    t.device.queue.writeTexture({ texture }, new Int8Array(data), {}, size);

    let expTexelView;
    switch (mode) {
      case 'bytes':
        expTexelView = TexelView.fromTexelsAsBytes(format, coords => new Uint8Array([10]));
        break;
      case 'colors':
        expTexelView = TexelView.fromTexelsAsColors(format, coords => ({ R: 10 / _maxValue }));
        break;
    }

    const result = await textureContentIsOKByT2B(
      t,
      { texture },
      size,
      { expTexelView },
      { maxDiffULPsForNormFormat: 0 }
    );
    t.expect((result === undefined) === _ok, result?.message);
  });

g.test('snorm_min')
  .desc(
    `The minimum snorm value has two possible representations (e.g. -127 and -128). Ensure that
    actual/expected can mismatch in both directions and pass the test.`
  )
  .params(u =>
    u //
      .combine('mode', ['bytes', 'colors'] as const)
      .combineWithParams([
        //
        { format: 'r8snorm', _maxValue: 127 },
      ] as const)
  )
  .fn(async t => {
    const { mode, format, _maxValue } = t.params;

    const data = [-_maxValue, -_maxValue - 1];

    const size = [2, 1];
    const texture = t.device.createTexture({
      format,
      size,
      usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.COPY_SRC,
    });
    t.trackForCleanup(texture);
    t.device.queue.writeTexture({ texture }, new Int8Array(data), {}, size);

    let expTexelView;
    switch (mode) {
      case 'bytes':
        {
          // Actual value should be [-127,-128], expected value is [-128,-127], both should pass.
          const exp = [-_maxValue - 1, -_maxValue];
          expTexelView = TexelView.fromTexelsAsBytes(
            format,
            coords => new Uint8Array([exp[coords.x]])
          );
        }
        break;
      case 'colors':
        expTexelView = TexelView.fromTexelsAsColors(format, coords => ({ R: -1 }));
        break;
    }

    const result = await textureContentIsOKByT2B(
      t,
      { texture },
      size,
      { expTexelView },
      { maxDiffULPsForNormFormat: 0 }
    );
    t.expectOK(result);
  });