docs.rodeo

MDN Web Docs mirror

GPUTexture: createView() method

{{APIRef("WebGPU API")}} {{SeeCompatTable}} {{SecureContext_Header}} {{AvailableInWorkers}} 

The createView() method of the {{domxref("GPUTexture")}}  interface creates a {{domxref("GPUTextureView")}}  representing a specific view of the GPUTexture.

Syntax

createView()
createView(descriptor)

Parameters

Return value

A {{domxref("GPUTextureView")}}  object instance.

Validation

The following criteria must be met when calling createView(), otherwise a {{domxref("GPUValidationError")}}  is generated and an invalid {{domxref("GPUTextureView")}}  object is returned:

Examples

Typical createView() usage

In the WebGPU Samples Cubemap demo, you will see multiple examples of how createView() is used, both as to create a view resource for a {{domxref("GPUDevice.createBindGroup()")}}  call, and to provide a view in the depthStencilAttachment object of a {{domxref("GPUCommandEncoder.beginRenderPass()")}}  descriptor.

const uniformBindGroup = device.createBindGroup({
  layout: pipeline.getBindGroupLayout(0),
  entries: [
    {
      binding: 0,
      resource: {
        buffer: uniformBuffer,
        offset: 0,
        size: uniformBufferSize,
      },
    },
    {
      binding: 1,
      resource: sampler,
    },
    {
      binding: 2,
      resource: cubemapTexture.createView({
        dimension: "cube",
      }),
    },
  ],
});

const renderPassDescriptor: GPURenderPassDescriptor = {
  colorAttachments: [
    {
      view: undefined, // Assigned later
      loadOp: "clear",
      storeOp: "store",
    },
  ],
  depthStencilAttachment: {
    view: depthTexture.createView(),

    depthClearValue: 1.0,
    depthLoadOp: "clear",
    depthStoreOp: "store",
  },
};

// …

const commandEncoder = device.createCommandEncoder();
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);

// …

createView() with usage restriction

In this snippet, we create a texture and then create a view that has its usage restricted via the usage property.

const texture = myDevice.createTexture({
  size: [4, 4],
  format: "rgba8unorm",
  usage:
    GPUTextureUsage.RENDER_ATTACHMENT |
    GPUTextureUsage.TEXTURE_BINDING |
    GPUTextureUsage.STORAGE_BINDING,
  viewFormats: ["rgba8unorm-srgb"],
});

const view = texture.createView({
  format: "rgba8unorm-srgb",
  usage: GPUTextureUsage.RENDER_ATTACHMENT, // Restrict allowed usage
});

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN