大网数据广告活动
Web 标准与前端开发

GPUDevice: createShaderModule() method

· 52CE 我爱测

The **`createShaderModule()`** method of the GPUDevice interface creates a GPUShaderModule from a string of WGSL source code.

WebGPU API

The createShaderModule() method of the GPUDevice interface creates a GPUShaderModule from a string of WGSL source code.

Syntax

createShaderModule(descriptor)

Parameters

  • descriptor
    • : An object containing the following properties:
      • code

        • : A string representing the WGSL source code for the shader module.
      • hints

        • : A sequence of record types, with the structure ("string", compilationHint). These behave like ordered maps. In each case, the "string" is a key used to identify or select the record, and the compilationHint is either a GPUPipelineLayout object instance or an enumerated value of "auto".

          The point of hints is to provide information about the pipeline layout as early as possible to improve performance. The idea is to maximize the amount of compilation that can be done once by createShaderModule(), rather than multiple times in multiple calls to GPUDevice.createComputePipeline() and GPUDevice.createRenderPipeline().

          [!NOTE] Different implementations may handle hints in different ways, including possibly ignoring them entirely. Providing hints does not guarantee improved shader compilation performance on all browsers/systems.

      • label

        • : A string providing a label that can be used to identify the object, for example in GPUError messages or console warnings.
      • sourceMap

        • : A source map definition to provide developer tool integration such as source-language debugging. WGSL names (identifiers) in source maps should follow the rules defined in WGSL identifier comparison. If defined, the source map may be interpreted as a source-map-v3 format.

          [!NOTE] Different implementations may handle sourceMaps in different ways, including possibly ignoring them entirely.

Return value

A GPUShaderModule object instance.

Validation

The following criteria must be met when calling createShaderModule(), otherwise a GPUValidationError is generated and an invalid GPUShaderModule object is returned:

  • If your shader's WGSL code uses the half-precision floating-point type f16, it includes enable f16; at the top, and the associated GPUDevice is created with the shader-f16 feature enabled.

Examples

In our basic render demo, our shader module is created using the following code:

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

  // …
}

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。