chromium/third_party/blink/web_tests/wpt_internal/webgpu/fast_api/GPURenderEncoderBase.setVertexBuffer.https.html

<!DOCTYPE html>
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<script>
function optimizedMethodCall(renderEncoder, slot, buffer, offset, size) {
  renderEncoder.setVertexBuffer(slot, buffer, offset, size);
}

function test(t, hotLoop, renderEncoder, slot, buffer, offset, size) {
  try {
    hotLoop(1, renderEncoder, slot, buffer, offset, size);
  } catch(e) {
    assert_true(e instanceof TypeError);
    return;
  }
  assert_unreached("A TypeError should be thrown.");
}

async function testRenderEncoder(t, device, renderEncoder, vertexBuffer) {
  function hotLoop(count, renderEncoder, slot, buffer, offset, size) {
    for (let i = 0; i < count; ++i) {
      optimizedMethodCall(renderEncoder, slot, buffer, offset, size);
    }
  }
  hotLoop(100, renderEncoder, 1, vertexBuffer, 4, 4);

  // Wait a bit for V8 to optimize. Then call again with an out-of-bounds value.
  // An exception should be thrown.
  await new Promise(resolve => t.step_timeout(resolve, 50));

  // kJSMaxInteger = 0x20000000000000 - 1;
  test(t, hotLoop, renderEncoder, 0xFFFFFFFF + 1, vertexBuffer, 4, 4);
  test(t, hotLoop, renderEncoder, 1, vertexBuffer, 0x20000000000000, 4);
  test(t, hotLoop, renderEncoder, 1, vertexBuffer, 4, 0x20000000000000);
}

async function testRenderPassEncoder(t, device, vertexBuffer) {
  const encoder = device.createCommandEncoder();

  const colorTexture = device.createTexture({
    format: 'rgba8unorm',
    size: [4, 4, 1],
    usage: GPUTextureUsage.RENDER_ATTACHMENT,
  });
  const renderPassEncoder = encoder.beginRenderPass({
    colorAttachments: [{
      view: colorTexture.createView(),
      loadOp: 'load',
      storeOp: 'store',
    }],
  });
  await testRenderEncoder(t, device, renderPassEncoder, vertexBuffer);
}

async function testRenderBundleEncoder(t, device, vertexBuffer) {
  const renderBundleEncoder = device.createRenderBundleEncoder({
    colorFormats: ['rgba8unorm'],
  });
  await testRenderEncoder(t, device, renderBundleEncoder, vertexBuffer);
}

promise_test(async t => {
  const adapter = await navigator.gpu.requestAdapter();
  assert_true(adapter instanceof GPUAdapter, 'Failed to request WebGPU adapter');
  const device = await adapter.requestDevice();
  assert_true(device instanceof GPUDevice, 'Failed to request WebGPU device');

  const vertexBuffer = device.createBuffer({
    size: 8,
    usage: GPUBufferUsage.VERTEX,
  });
  await testRenderPassEncoder(t, device, vertexBuffer);
  await testRenderBundleEncoder(t, device, vertexBuffer);
});
</script>
</body>
</html>