GPUTexture

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

安全上下文: 此功能仅在安全上下文(HTTPS)中可用,且支持此功能的浏览器数量有限。

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

GPUTexture 接口是 WebGPU API 的一部分,它表示一个容器,用于存储用于 GPU 渲染操作的 1D、2D 或 3D 数据数组,例如图像。

GPUTexture 对象实例是通过 GPUDevice.createTexture() 方法创建的。

实例属性

depthOrArrayLayers 只读

一个表示 GPUTexture 的深度或层数的数字(像素或层数)。

dimension 只读

一个枚举值,表示每个 GPUTexture 子资源纹素集的维度。

format 只读

一个表示 GPUTexture 格式的枚举值。有关所有可能值的详细信息,请参阅规范的 纹理格式 部分。

height 只读

一个表示 GPUTexture 高度的数字(像素)。

label

一个字符串,提供可用于识别对象的标签,例如在 GPUError 消息或控制台警告中。

mipLevelCount 只读

一个表示 GPUTexture 的 mip 级别数量的数字。

sampleCount 只读

一个表示 GPUTexture 的采样数的数字。

usage 只读

表示 GPUTexture 允许用法的 按位标志

width 只读

一个表示 GPUTexture 宽度的数字(像素)。

实例方法

createView()

创建一个 GPUTextureView,表示 GPUTexture 的特定视图。

destroy()

销毁 GPUTexture

示例

在 WebGPU 示例 纹理立方体示例 中,通过以下方式创建用于立方体面的纹理:

js
// …
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],
  );
}
// …

规范

规范
WebGPU
# gputexture

浏览器兼容性

另见