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

GPUTexture

· 52CE 我爱测

The **`GPUTexture`** interface of the WebGPU API represents a container used to store 1D, 2D, or 3D arrays of data, such as images, to use in GPU rendering operations.

WebGPU API

The GPUTexture interface of the WebGPU API represents a container used to store 1D, 2D, or 3D arrays of data, such as images, to use in GPU rendering operations.

A GPUTexture object instance is created using the GPUDevice.createTexture() method.

Instance properties

  • GPUTexture.depthOrArrayLayers
    • : A number representing the depth or layer count of the GPUTexture (pixels, or number of layers).
  • GPUTexture.dimension
    • : An enumerated value representing the dimension of the set of texels for each GPUTexture subresource.
  • GPUTexture.format
  • GPUTexture.height
    • : A number representing the height of the GPUTexture in pixels.
  • GPUTexture.label
    • : A string providing a label that can be used to identify the object, for example in GPUError messages or console warnings.
  • GPUTexture.mipLevelCount
    • : A number representing the number of mip levels of the GPUTexture.
  • GPUTexture.sampleCount
    • : A number representing the sample count of the GPUTexture.
  • GPUTexture.usage
    • : The bitwise flags representing the allowed usages of the GPUTexture.
  • GPUTexture.width
    • : A number representing the width of the GPUTexture in pixels.

Instance methods

  • GPUTexture.createView
    • : Creates a GPUTextureView representing a specific view of the GPUTexture.
  • GPUTexture.destroy
    • : Destroys the GPUTexture.

Examples

In the WebGPU samples Textured Cube sample, a texture to use on the faces of a cube is created by:

  • Loading the image into an HTMLImageElement and creating an image bitmap using Window.createImageBitmap.
  • Creating a new GPUTexture using createTexture().
  • Copying the image bitmap into the texture using GPUQueue.copyExternalImageToTexture().
// …
let cubeTexture;
{
  const img = document.createElement("img");

  img.src = new URL(
    "../../../assets/img/Di-3d.png",
    import.meta.url,
  ).toString();

  await img.decode();

  const imageBitmap = await createImageBitmap(img);

  cubeTexture = device.createTexture({
    size: [imageBitmap.width, imageBitmap.height, 1],
    format: "rgba8unorm",
    usage:
      GPUTextureUsage.TEXTURE_BINDING |
      GPUTextureUsage.COPY_DST |
      GPUTextureUsage.RENDER_ATTACHMENT,
  });

  device.queue.copyExternalImageToTexture(
    { source: imageBitmap },
    { texture: cubeTexture },
    [imageBitmap.width, imageBitmap.height],
  );
}
// …

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

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

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