GPUCompilationInfo

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Note: This feature is available in Web Workers.

Giao diện GPUCompilationInfo của WebGPU API đại diện cho một mảng các đối tượng GPUCompilationMessage được tạo ra bởi trình biên dịch shader GPU để giúp chẩn đoán các vấn đề với mã shader.

GPUCompilationInfo được truy cập thông qua GPUShaderModule.getCompilationInfo().

Thuộc tính phiên bản

messages Read only

Một mảng các đối tượng GPUCompilationMessage, mỗi đối tượng chứa chi tiết của một thông báo biên dịch shader riêng lẻ. Các thông báo có thể mang tính thông tin, cảnh báo hoặc lỗi.

Ví dụ

Trong ví dụ bên dưới, chúng ta đã cố ý bỏ sót một dấu ngoặc đơn trong khai báo hàm của mã shader:

js
const shaders = `
struct VertexOut {
  @builtin(position) position : vec4f,
  @location(0) color : vec4f
}

@vertex
fn vertex_main(@location(0) position: vec4f,
               @location(1) color: vec4f -> VertexOut
{
  var output : VertexOut;
  output.position = position;
  output.color = color;
  return output;
}

@fragment
fn fragment_main(fragData: VertexOut) -> @location(0) vec4f
{
  return fragData.color;
}
`;

Khi biên dịch module shader, chúng ta sử dụng getCompilationInfo() để lấy thông tin về lỗi kết quả:

js
async function init() {
  // …

  const shaderModule = device.createShaderModule({
    code: shaders,
  });

  const shaderInfo = await shaderModule.getCompilationInfo();
  const firstMessage = shaderInfo.messages[0];

  console.log(firstMessage.lineNum); // 9
  console.log(firstMessage.message); // "expected ')' for function declaration"
  console.log(firstMessage.type); // "error"
  // …
}

Thông số kỹ thuật

Specification
WebGPU
# gpucompilationinfo

Tương thích trình duyệt

Xem thêm