GPUShaderModule

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 GPUShaderModule của WebGPU API đại diện cho một đối tượng shader module nội bộ, là container chứa code shader WGSL có thể được gửi đến GPU để thực thi bởi một pipeline.

Một phiên bản đối tượng GPUShaderModule được tạo bằng GPUDevice.createShaderModule().

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

label

Một chuỗi cung cấp nhãn có thể được dùng để nhận dạng đối tượng, ví dụ trong thông báo GPUError hoặc cảnh báo console.

Phương thức phiên bản

getCompilationInfo()

Trả về một Promise thực hiện với một đối tượng GPUCompilationInfo chứa các thông báo được tạo ra trong quá trình biên dịch GPUShaderModule.

Ví dụ

Trong basic render demo của chúng ta, shader module được tạo bằng code sau:

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,
  });

  // …
}

Thông số kỹ thuật

Specification
WebGPU
# gpushadermodule

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

Xem thêm