GPUShaderModule: getCompilationInfo() 方法

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

安全上下文: 此功能僅在安全上下文(HTTPS)中可用,且支援此功能的瀏覽器數量有限。

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

GPUShaderModule 介面的 getCompilationInfo() 方法返回一個 Promise,該 Promise 會解析為一個 GPUCompilationInfo 物件,其中包含 GPUShaderModule 編譯過程中生成的各種訊息。

語法

js
getCompilationInfo()

引數

無。

返回值

一個 Promise,它解析為一個 GPUCompilationInfo 物件。

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
# dom-gpushadermodule-getcompilationinfo

瀏覽器相容性

另見