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
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269

content / test / data / gpu / webgpu-unittest-utils.js [blame]

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Test "enum" allows for specifying tests in webGpuUnitTests below.
const WebGpuUnitTestId = {
  RenderTest: 'render-test',
  RenderTestAsync: 'render-test-async',
  ComputeTest: 'compute-test',
  ComputeTestAsync: 'compute-test-async',
};

// Implements a set of simple standalone unit tests to test WebGPU without
// depending on the canvas. Each test returns a pair consisting of a bool
// indicating whether the test passed (true) or failed (false), and a
// potentially empty array of messages detailing why the test may have
// failed.
export const webGpuUnitTests = function() {
  //////////////////////////////////////////////////////////////////////////////
  // Private internal helpers

  // Initializes the adapter and devices for webgpu usage.
  const init = async function() {
    const adapter = navigator.gpu && await navigator.gpu.requestAdapter();
    if (!adapter) {
      console.error('navigator.gpu && navigator.gpu.requestAdapter failed');
      return [
        null,
        null,
        ['WebGPU was unavailable and/or requesting adapter failed.']
      ];
    }
    const device = await adapter.requestDevice();
    if (!device) {
      console.error('adapter.requestDevice() failed');
      return [
        adapter,
        null,
        ['Failed to request a WebGPU device.']
      ];
    }
    return [adapter, device];
  };

  // Compares an actual array (a) to an expected one (e), returning [true, []]
  // iff the type and contents of the arrays are equal, otherwise returning
  // [false, [description]].
  const compareArrays = function(e, a) {
    if (e.constructor !== a.constructor) {
      return [
        false,
        [`Expected type '${e.constructor.name}', got '${a.constructor.name}'.`]
      ];
    }
    if (e.length !== a.length) {
      return [
        false,
        [`Expected length ${e.length}, got ${a.length}.`]
      ];
    }
    var equal = true;
    for (var i = 0; i !== e.length; i++) {
      if (e[i] != a[i]) {
        success = equal;
      }
    }
    return equal ?
        [true, []] :
        [false, [`Expected [${e.toString()}], got [${a.toString()}].`]];
  }

  // Render test base which allows for specifying whether to use async pipeline
  // creation. Renders a single pixel texture, copies it to a buffer, and
  // verifies.
  const renderTestBase = async function(useAsync) {
    const [adapter, device, errors] = await init();
    if (!adapter || !device) {
      return [false, errors];
    }

    // Create the WebGPU primitives and execute the rendering and buffer copy.
    const buffer = device.createBuffer({
      size: 4,
      usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
    });
    const texture = device.createTexture({
      format: 'rgba8unorm',
      size: { width: 1, height: 1 },
      usage: GPUTextureUsage.COPY_SRC | GPUTextureUsage.RENDER_ATTACHMENT,
    });
    const view = texture.createView();
    const pipelineDesc = {
      layout: 'auto',
      vertex: {
        module: device.createShaderModule({
          code: `
            @vertex fn main(
              @builtin(vertex_index) VertexIndex : u32
              ) -> @builtin(position) vec4<f32> {
                var pos : array<vec2<f32>, 3> = array<vec2<f32>, 3>(
                    vec2<f32>(-1.0, -3.0),
                    vec2<f32>(3.0, 1.0),
                    vec2<f32>(-1.0, 1.0));
                return vec4<f32>(pos[VertexIndex], 0.0, 1.0);
              }
              `,
        }),
        entryPoint: 'main',
      },
      fragment: {
        module: device.createShaderModule({
          code: `
              @fragment fn main() -> @location(0) vec4<f32> {
                return vec4<f32>(0.0, 1.0, 0.0, 1.0);
              }
              `,
        }),
        entryPoint: 'main',
        targets: [{ format: 'rgba8unorm' }],
      },
      primitive: { topology: 'triangle-list' },
    };
    const pipeline = useAsync
          ? await device.createRenderPipelineAsync(pipelineDesc)
          : device.createRenderPipeline(pipelineDesc);
    const encoder = device.createCommandEncoder();
    const pass = encoder.beginRenderPass({
      colorAttachments: [
        {
          view,
          storeOp: 'store',
          clearValue: { r: 1.0, g: 0.0, b: 0.0, a: 1.0 },
          loadOp: 'clear',
        },
      ],
    });
    pass.setPipeline(pipeline);
    pass.draw(3);
    pass.end();
    encoder.copyTextureToBuffer(
        { texture, mipLevel: 0, origin: { x: 0, y: 0, z: 0 } },
        { buffer, bytesPerRow: 256 },
        { width: 1, height: 1, depthOrArrayLayers: 1 }
    );
    device.queue.submit([encoder.finish()]);

    // Verify the contents of the buffer that the texture was copied into.
    var success = true;
    const expected = new Uint8Array([0x00, 0xff, 0x00, 0xff]);
    await buffer.mapAsync(GPUMapMode.READ);
    const actual = new Uint8Array(buffer.getMappedRange());
    return compareArrays(expected, actual);
  };

  // Compute test base which allows for specifying whether to use async pipeline
  // creation. Fills a buffer with global_invocation_id.x and verifies the
  // contents of the buffer.
  const computeTestBase = async function(useAsync) {
    const [adapter, device, errors] = await init();
    if (!adapter || !device) {
      return [false, errors];
    }

    // Test constants.
    const n = 16;
    const size = n * 4;

    // Create the WebGPU primitives and execute the compute and buffer copy.
    const pipelineDesc = {
      layout: 'auto',
      compute: {
        module: device.createShaderModule({
          code: `
            @group(0) @binding(0) var<storage, read_write> buffer: array<u32>;

            @compute @workgroup_size(1u) fn main(
              @builtin(global_invocation_id) id: vec3<u32>
            ) {
              buffer[id.x] = id.x;
            }
            `,
        }),
        entryPoint: 'main',
      },
    };
    const pipeline = useAsync
          ? await device.createComputePipelineAsync(pipelineDesc)
          : device.createComputePipeline(pipelineDesc);
    const buffer = device.createBuffer({
      size,
      usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC,
    });
    const result = device.createBuffer({
      size,
      usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
    });
    const bindGroup = device.createBindGroup({
      layout: pipeline.getBindGroupLayout(0),
      entries: [{ binding: 0, resource: { buffer } }],
    });
    const encoder = device.createCommandEncoder();
    const pass = encoder.beginComputePass();
    pass.setPipeline(pipeline);
    pass.setBindGroup(0, bindGroup);
    pass.dispatchWorkgroups(n);
    pass.end();
    encoder.copyBufferToBuffer(buffer, 0, result, 0, size);
    device.queue.submit([encoder.finish()]);

    // Verify the contents of the buffer that was copied into.
    var success = true;
    const expected = new Uint32Array([...Array(n).keys()]);
    await result.mapAsync(GPUMapMode.READ);
    const actual = new Uint32Array(result.getMappedRange());
    return compareArrays(expected, actual);
  };

  return {
    ////////////////////////////////////////////////////////////////////////////
    // Actual unit tests

    renderTest: async function() {
      return await renderTestBase(false);
    },
    renderTestAsync: async function() {
      return await renderTestBase(true);
    },
    computeTest: async function() {
      return await computeTestBase(false);
    },
    computeTestAsync: async function() {
      return await computeTestBase(true);
    },

    ////////////////////////////////////////////////////////////////////////////
    // Test driver
    runTest: async function(testId) {
      // Test running wrapper to prefix error messages with test name.
      const wrapper = async function(testId, testFunc) {
        const [success, errors] = await testFunc();
        if (success) {
          return [true, []];
        }
        return [
          false,
          [`WebGPU test '${testId}' failed with the following errors:`] +
              errors.map(function(e) { return '    ' + e; })];
      };

      switch (testId) {
        case WebGpuUnitTestId.RenderTest:
          return await wrapper(testId, this.renderTest);
          break;
        case WebGpuUnitTestId.RenderTestAsync:
          return await wrapper(testId, this.renderTestAsync);
          break;
        case WebGpuUnitTestId.ComputeTest:
          return await wrapper(testId, this.computeTest);
          break;
        case WebGpuUnitTestId.ComputeTestAsync:
          return await wrapper(testId, this.computeTestAsync);
          break;
        default:
          // Just fail for any undefined tests.
          return [false, [`Undefined WebGPU test '${testId}' specified.`]];
      }
    },
  };
}();