GPUShaderModule

可用性有限

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

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

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

GPUShaderModule 介面是 WebGPU API 的一部分,代表一個內部著色器模組物件,它是 WGSL 著色器程式碼的容器,可以提交給 GPU 以便透過管道執行。

GPUShaderModule 物件例項是使用 GPUDevice.createShaderModule() 建立的。

例項屬性

label

一個字串,提供可用於識別物件的標籤,例如在 GPUError 訊息或控制檯警告中。

例項方法

getCompilationInfo()

返回一個 Promise,該 Promise 會以一個 GPUCompilationInfo 物件解析,該物件包含在 GPUShaderModule 編譯期間生成的各種訊息。

示例

在我們 基本的渲染演示中,我們的著色器模組是使用以下程式碼建立的:

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

async function init() {
  if (!navigator.gpu) {
    throw Error("WebGPU not supported.");
  }

  const adapter = await navigator.gpu.requestAdapter();

  if (!adapter) {
    throw Error("Couldn't request WebGPU adapter.");
  }

  const device = await adapter.requestDevice();

  // …
  // later on

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

  // …
}

規範

規範
WebGPU
# gpushadermodule

瀏覽器相容性

另見