GPUCompilationInfo

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

安全上下文: 此功能仅在安全上下文(HTTPS)中可用,且支持此功能的浏览器数量有限。

注意:此功能在 Web Workers 中可用。

GPUCompilationInfo 接口是 WebGPU API 的一部分,它表示一个由 GPU 着色器模块编译器生成的 GPUCompilationMessage 对象数组,用于帮助诊断着色器代码中的问题。

通过 GPUShaderModule.getCompilationInfo() 来访问 GPUCompilationInfo

实例属性

messages 只读

一个 GPUCompilationMessage 对象数组,每个对象都包含单个着色器编译消息的详细信息。消息可以是信息性的、警告或错误。

示例

在下面的示例中,我们故意在着色器代码的函数声明中遗漏了一个括号

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;
}
`;

当我们编译着色器模块时,我们使用 getCompilationInfo() 来获取一些关于由此产生的错误的附加信息

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"
  // …
}

规范

规范
WebGPU
# gpucompilationinfo

浏览器兼容性

另见