summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/tests/cts/checkout/src/webgpu/api/operation/buffers/mapping_test.ts
blob: 733e2dcb690cfe12c0853e0b18818fbcfbd12323 (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
import { assert } from '../../../../common/util/util.js';
import { GPUTest } from '../../../gpu_test.js';

export class MappingTest extends GPUTest {
  checkMapWrite(
    buffer: GPUBuffer,
    offset: number,
    mappedContents: ArrayBuffer,
    size: number
  ): void {
    this.checkMapWriteZeroed(mappedContents, size);

    const mappedView = new Uint32Array(mappedContents);
    const expected = new Uint32Array(new ArrayBuffer(size));
    assert(mappedView.byteLength === size);
    for (let i = 0; i < mappedView.length; ++i) {
      mappedView[i] = expected[i] = i + 1;
    }
    buffer.unmap();

    this.expectGPUBufferValuesEqual(buffer, expected, offset);
  }

  checkMapWriteZeroed(arrayBuffer: ArrayBuffer, expectedSize: number): void {
    this.expect(arrayBuffer.byteLength === expectedSize);
    const view = new Uint8Array(arrayBuffer);
    this.expectZero(view);
  }

  expectZero(actual: Uint8Array): void {
    const size = actual.byteLength;
    for (let i = 0; i < size; ++i) {
      if (actual[i] !== 0) {
        this.fail(`at [${i}], expected zero, got ${actual[i]}`);
        break;
      }
    }
  }
}