summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/tests/cts/checkout/src/webgpu/shader/execution/expression/call/builtin/pack4x8unorm.spec.ts
blob: b670e92fbbbfb8e62416f7c6b12b9a8aae1e6929 (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
export const description = `
Converts four normalized floating point values to 8-bit unsigned integers, and then combines them into one u32 value.
Component e[i] of the input is converted to an 8-bit unsigned integer value
⌊ 0.5 + 255 × min(1, max(0, e[i])) ⌋ which is then placed in
bits 8 × i through 8 × i + 7 of the result.
`;

import { makeTestGroup } from '../../../../../../common/framework/test_group.js';
import { GPUTest } from '../../../../../gpu_test.js';
import { kValue } from '../../../../../util/constants.js';
import {
  f32,
  pack4x8unorm,
  Scalar,
  TypeF32,
  TypeU32,
  TypeVec,
  u32,
  vec4,
} from '../../../../../util/conversion.js';
import { quantizeToF32, vectorF32Range } from '../../../../../util/math.js';
import { allInputSources, Case, run } from '../../expression.js';

import { builtin } from './builtin.js';

export const g = makeTestGroup(GPUTest);

g.test('pack')
  .specURL('https://www.w3.org/TR/WGSL/#pack-builtin-functions')
  .desc(
    `
@const fn pack4x8unorm(e: vec4<f32>) -> u32
`
  )
  .params(u => u.combine('inputSource', allInputSources))
  .fn(async t => {
    const makeCase = (vals: [number, number, number, number]): Case => {
      const vals_f32 = new Array<Scalar>(4) as [Scalar, Scalar, Scalar, Scalar];
      for (const idx in vals) {
        vals[idx] = quantizeToF32(vals[idx]);
        vals_f32[idx] = f32(vals[idx]);
      }

      return { input: [vec4(...vals_f32)], expected: u32(pack4x8unorm(...vals)) };
    };

    // Returns a value normalized to [0, 1].
    const normalizeF32 = (n: number): number => {
      return n > 0 ? n / kValue.f32.positive.max : n / kValue.f32.negative.min;
    };

    const cases: Array<Case> = vectorF32Range(4).flatMap(v => {
      return [
        makeCase(v as [number, number, number, number]),
        makeCase(v.map(normalizeF32) as [number, number, number, number]),
      ];
    });

    await run(t, builtin('pack4x8unorm'), [TypeVec(4, TypeF32)], TypeU32, t.params, cases);
  });