summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/tests/cts/checkout/src/webgpu/api/operation/queue/writeBuffer.spec.ts
blob: eb42d1a3c30314f87ae135c512a9c1be12b54226 (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
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
export const description = 'Operation tests for GPUQueue.writeBuffer()';

import { makeTestGroup } from '../../../../common/framework/test_group.js';
import { memcpy, range } from '../../../../common/util/util.js';
import { GPUTest } from '../../../gpu_test.js';
import { align } from '../../../util/math.js';

const kTypedArrays = [
  'Uint8Array',
  'Uint16Array',
  'Uint32Array',
  'Int8Array',
  'Int16Array',
  'Int32Array',
  'Float32Array',
  'Float64Array',
] as const;

type WriteBufferSignature = {
  bufferOffset: number;
  data: readonly number[];
  arrayType: typeof kTypedArrays[number];
  useArrayBuffer: boolean;
  dataOffset?: number; // In elements when useArrayBuffer === false, bytes otherwise
  dataSize?: number; // In elements when useArrayBuffer === false, bytes otherwise
};

class F extends GPUTest {
  calculateRequiredBufferSize(writes: WriteBufferSignature[]): number {
    let bufferSize = 0;
    // Calculate size of final buffer
    for (const { bufferOffset, data, arrayType, useArrayBuffer, dataOffset, dataSize } of writes) {
      const TypedArrayConstructor = globalThis[arrayType];

      // When passing data as an ArrayBuffer, dataOffset and dataSize use byte instead of number of
      // elements. bytesPerElement is used to convert dataOffset and dataSize from elements to bytes
      // when useArrayBuffer === false.
      const bytesPerElement = useArrayBuffer ? 1 : TypedArrayConstructor.BYTES_PER_ELEMENT;

      // Calculate the number of bytes written to the buffer. data is always an array of elements.
      let bytesWritten =
        data.length * TypedArrayConstructor.BYTES_PER_ELEMENT - (dataOffset || 0) * bytesPerElement;

      if (dataSize) {
        // When defined, dataSize clamps the number of bytes written
        bytesWritten = Math.min(bytesWritten, dataSize * bytesPerElement);
      }

      // The minimum buffer size required for the write to succeed is the number of bytes written +
      // the bufferOffset
      const requiredBufferSize = bufferOffset + bytesWritten;

      // Find the largest required size by all writes
      bufferSize = Math.max(bufferSize, requiredBufferSize);
    }
    // writeBuffer requires buffers to be a multiple of 4
    return align(bufferSize, 4);
  }

  testWriteBuffer(...writes: WriteBufferSignature[]) {
    const bufferSize = this.calculateRequiredBufferSize(writes);

    // Initialize buffer to non-zero data (0xff) for easier debug.
    const expectedData = new Uint8Array(bufferSize).fill(0xff);

    const buffer = this.makeBufferWithContents(
      expectedData,
      GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST
    );

    for (const { bufferOffset, data, arrayType, useArrayBuffer, dataOffset, dataSize } of writes) {
      const TypedArrayConstructor = globalThis[arrayType];
      const writeData = new TypedArrayConstructor(data);
      const writeSrc = useArrayBuffer ? writeData.buffer : writeData;
      this.queue.writeBuffer(buffer, bufferOffset, writeSrc, dataOffset, dataSize);
      memcpy(
        { src: writeSrc, start: dataOffset, length: dataSize },
        { dst: expectedData, start: bufferOffset }
      );
    }

    this.debug(`expectedData: [${expectedData.join(', ')}]`);
    this.expectGPUBufferValuesEqual(buffer, expectedData);
  }
}

export const g = makeTestGroup(F);

const kTestData = range<number>(16, i => i);

g.test('array_types')
  .desc('Tests that writeBuffer correctly handles different TypedArrays and ArrayBuffer.')
  .params(u =>
    u //
      .combine('arrayType', kTypedArrays)
      .combine('useArrayBuffer', [false, true])
  )
  .fn(t => {
    const { arrayType, useArrayBuffer } = t.params;
    const dataOffset = 1;
    const dataSize = 8;
    t.testWriteBuffer({
      bufferOffset: 0,
      arrayType,
      data: kTestData,
      dataOffset,
      dataSize,
      useArrayBuffer,
    });
  });

g.test('multiple_writes_at_different_offsets_and_sizes')
  .desc(
    `
Tests that writeBuffer currently handles different offsets and writes. This includes:
- Non-overlapping TypedArrays and ArrayLists
- Overlapping TypedArrays and ArrayLists
- Writing zero data
- Writing on zero sized buffers
- Unaligned source
- Multiple overlapping writes with decreasing sizes
    `
  )
  .paramsSubcasesOnly([
    {
      // Concatenate 2 Uint32Arrays
      writes: [
        {
          bufferOffset: 0,
          data: kTestData,
          arrayType: 'Uint32Array',
          useArrayBuffer: false,
          dataOffset: 2,
          dataSize: 2,
        }, // [2, 3]
        {
          bufferOffset: 2 * Uint32Array.BYTES_PER_ELEMENT,
          data: kTestData,
          arrayType: 'Uint32Array',
          useArrayBuffer: false,
          dataOffset: 0,
          dataSize: 2,
        }, // [0, 1]
      ], // Expected [2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
    },
    {
      // Concatenate 2 Uint8Arrays
      writes: [
        { bufferOffset: 0, data: [0, 1, 2, 3], arrayType: 'Uint8Array', useArrayBuffer: false },
        { bufferOffset: 4, data: [4, 5, 6, 7], arrayType: 'Uint8Array', useArrayBuffer: false },
      ], // Expected [0, 1, 2, 3, 4, 5, 6, 7]
    },
    {
      // Overlap in the middle
      writes: [
        { bufferOffset: 0, data: kTestData, arrayType: 'Uint8Array', useArrayBuffer: false },
        { bufferOffset: 4, data: [0], arrayType: 'Uint32Array', useArrayBuffer: false },
      ], // Expected [0, 1, 2, 3, 0, 0 ,0 ,0, 8, 9, 10, 11, 12, 13, 14, 15]
    },
    {
      // Overlapping arrayLists
      writes: [
        {
          bufferOffset: 0,
          data: kTestData,
          arrayType: 'Uint32Array',
          useArrayBuffer: true,
          dataOffset: 2,
          dataSize: 4 * Uint32Array.BYTES_PER_ELEMENT,
        },
        { bufferOffset: 4, data: [0x04030201], arrayType: 'Uint32Array', useArrayBuffer: true },
      ], // Expected [0, 0, 1, 0, 1, 2, 3, 4, 0, 0, 3, 0, 0, 0, 4, 0]
    },
    {
      // Write over with empty buffer
      writes: [
        { bufferOffset: 0, data: kTestData, arrayType: 'Uint8Array', useArrayBuffer: false },
        { bufferOffset: 0, data: [], arrayType: 'Uint8Array', useArrayBuffer: false },
      ], // Expected [0, 1, 2, 3, 4, 5 ,6 ,7, 8, 9, 10, 11, 12, 13, 14, 15]
    },
    {
      // Zero buffer
      writes: [{ bufferOffset: 0, data: [], arrayType: 'Uint8Array', useArrayBuffer: false }],
    }, // Expected []
    {
      // Unaligned source
      writes: [
        {
          bufferOffset: 0,
          data: [0x77, ...kTestData],
          arrayType: 'Uint8Array',
          useArrayBuffer: false,
          dataOffset: 1,
        },
      ], // Expected [0, 1, 2, 3, 4, 5 ,6 ,7, 8, 9, 10, 11, 12, 13, 14, 15]
    },
    {
      // Multiple overlapping writes
      writes: [
        {
          bufferOffset: 0,
          data: [0x05050505, 0x05050505, 0x05050505, 0x05050505, 0x05050505],
          arrayType: 'Uint32Array',
          useArrayBuffer: false,
        },
        {
          bufferOffset: 0,
          data: [0x04040404, 0x04040404, 0x04040404, 0x04040404],
          arrayType: 'Uint32Array',
          useArrayBuffer: false,
        },
        {
          bufferOffset: 0,
          data: [0x03030303, 0x03030303, 0x03030303],
          arrayType: 'Uint32Array',
          useArrayBuffer: false,
        },
        {
          bufferOffset: 0,
          data: [0x02020202, 0x02020202],
          arrayType: 'Uint32Array',
          useArrayBuffer: false,
        },
        {
          bufferOffset: 0,
          data: [0x01010101],
          arrayType: 'Uint32Array',
          useArrayBuffer: false,
        },
      ], // Expected [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5]
    },
  ] as const)
  .fn(t => {
    t.testWriteBuffer(...t.params.writes);
  });