summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/tests/cts/checkout/src/webgpu/shader/execution/expression/call/builtin/atomics/atomicAnd.spec.ts
blob: ad05bd851dacf45a7332dbe66221af7775bad44b (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
export const description = `
Atomically read, and and store value.

* Load the original value pointed to by atomic_ptr.
* Obtains a new value by anding with the value v.
* Store the new value using atomic_ptr.

Returns the original value stored in the atomic object.
`;

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

import {
  dispatchSizes,
  workgroupSizes,
  runStorageVariableTest,
  runWorkgroupVariableTest,
  kMapId,
  typedArrayCtor,
} from './harness.js';

export const g = makeTestGroup(GPUTest);

g.test('and_storage')
  .specURL('https://www.w3.org/TR/WGSL/#atomic-rmw')
  .desc(
    `
AS is storage or workgroup
T is i32 or u32

fn atomicAnd(atomic_ptr: ptr<AS, atomic<T>, read_write>, v: T) -> T
`
  )
  .params(u =>
    u
      .combine('workgroupSize', workgroupSizes)
      .combine('dispatchSize', dispatchSizes)
      .combine('mapId', keysOf(kMapId))
      .combine('scalarType', ['u32', 'i32'] as const)
  )
  .fn(t => {
    const numInvocations = t.params.workgroupSize * t.params.dispatchSize;

    // Allocate an output buffer with bitsize of max invocations plus 1 for validation
    const bufferNumElements = Math.max(1, numInvocations / 32) + 1;

    // Start with all bits high, then using atomicAnd to set mapped global id bit off.
    // Note: Both WGSL and JS will shift left 1 by id modulo 32.
    const initValue = 0xffffffff;

    const scalarType = t.params.scalarType;
    const mapId = kMapId[t.params.mapId];
    const extra = mapId.wgsl(numInvocations); // Defines map_id()
    const op = `
      let i = map_id(u32(id));
      atomicAnd(&output[i / 32], ~(${scalarType}(1) << i))
    `;

    const expected = new (typedArrayCtor(scalarType))(bufferNumElements).fill(initValue);
    for (let id = 0; id < numInvocations; ++id) {
      const i = mapId.f(id, numInvocations);
      expected[Math.floor(i / 32)] &= ~(1 << i);
    }

    runStorageVariableTest({
      t,
      workgroupSize: t.params.workgroupSize,
      dispatchSize: t.params.dispatchSize,
      bufferNumElements,
      initValue,
      op,
      expected,
      extra,
    });
  });

g.test('and_workgroup')
  .specURL('https://www.w3.org/TR/WGSL/#atomic-rmw')
  .desc(
    `
AS is storage or workgroup
T is i32 or u32

fn atomicAnd(atomic_ptr: ptr<AS, atomic<T>, read_write>, v: T) -> T
`
  )
  .params(u =>
    u
      .combine('workgroupSize', workgroupSizes)
      .combine('dispatchSize', dispatchSizes)
      .combine('mapId', keysOf(kMapId))
      .combine('scalarType', ['u32', 'i32'] as const)
  )
  .fn(t => {
    const numInvocations = t.params.workgroupSize;

    // Allocate workgroup array with bitsize of max invocations plus 1 for validation
    const wgNumElements = Math.max(1, numInvocations / 32) + 1;

    // Start with all bits high, then using atomicAnd to set mapped global id bit off.
    // Note: Both WGSL and JS will shift left 1 by id modulo 32.
    const initValue = 0xffffffff;

    const scalarType = t.params.scalarType;
    const mapId = kMapId[t.params.mapId];
    const extra = mapId.wgsl(numInvocations); // Defines map_id()
    const op = `
      let i = map_id(u32(id));
      atomicAnd(&wg[i / 32], ~(${scalarType}(1) << i))
    `;

    const expected = new (typedArrayCtor(scalarType))(wgNumElements * t.params.dispatchSize).fill(
      initValue
    );
    for (let d = 0; d < t.params.dispatchSize; ++d) {
      for (let id = 0; id < numInvocations; ++id) {
        const wg = expected.subarray(d * wgNumElements);
        const i = mapId.f(id, numInvocations);
        wg[Math.floor(i / 32)] &= ~(1 << i);
      }
    }

    runWorkgroupVariableTest({
      t,
      workgroupSize: t.params.workgroupSize,
      dispatchSize: t.params.dispatchSize,
      wgNumElements,
      initValue,
      op,
      expected,
      extra,
    });
  });