GPUDevice: createTexture() method
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The createTexture() method of the
GPUDevice interface creates a GPUTexture in which to store 1D, 2D, or 3D arrays of data, such as images, to use in GPU rendering operations.
Syntax
createTexture(descriptor)
Parameters
descriptor-
An object containing the following properties:
dimensionOptional-
An enumerated value indicating the dimension level of the texture. Possible values are:
"1d": The texture is one-dimensional."2d": The texture is two-dimensional or an array of two-dimensional layers."3d": The texture is three-dimensional.
dimensiondefaults to"2d"if the value is omitted. format-
An enumerated value specifying the format of the texture. See the Texture formats section of the specification for all the possible values.
labelOptional-
A string providing a label that can be used to identify the object, for example in
GPUErrormessages or console warnings. mipLevelCountOptional-
A number specifying the number of mip levels the texture will contain. If omitted, this defaults to 1.
sampleCountOptional-
A number specifying the texture's sample count. To be valid, the value must be 1 or 4. If omitted, this defaults to 1. A value higher than 1 indicates a multi-sampled texture.
size-
An object or array specifying the width, height, and depth/array layer count of the texture. The width value must always be specified, while the height and depth/array layer count values are optional and will default to 1 if omitted.
What follows is a sample
sizearray:jssize: [16, 16, 2];The object equivalent would look like this:
jssize: { width: 16, height: 16, depthOrArrayLayers: 2 } usage-
The bitwise flags representing the allowed usages for the
GPUTexture. The possible values are in theGPUTexture.usagevalue table.Note that multiple possible usages can be specified by separating values with pipe symbols, for example:
jsusage: GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT; viewFormatsOptional-
An array of enumerated values specifying other texture formats permitted when calling
GPUTexture.createView()on this texture, in addition to the texture format specified in itsformatvalue.
Return value
A GPUTexture object instance.
Validation
The following criteria must be met when calling createTexture(), otherwise a GPUValidationError is generated and an invalid GPUTexture object is returned:
- A valid
usageis specified. - The values specified in
size(width, height, or depth/array layer count) are greater than 0. mipLevelCountis greater than 0.sampleCountis equal to 1 or 4.- If
dimensionis set to"1d":- The
sizewidth value is less than or equal to theGPUDevice'smaxTextureDimension1Dlimit. - The
sizeheight and depth/array layer count values are equal to 1. - The
sampleCountis equal to 1. - The
formatis not equal to a compressed format or depth-or-stencil format.
- The
- If
dimensionis set to"2d": - If
dimensionis set to"3d":- The
sizewidth, and height, and depth/array layer count values are less than or equal to theGPUDevice'smaxTextureDimension3Dlimit. - The
sampleCountvalue is equal to 1. - The
formatis not equal to a compressed format or depth-or-stencil format.
- The
- The
sizewidth value is a multiple of the texel block width. - The
sizeheight value is a multiple of the texel block height. - If
sampleCountis greater than 1:mipLevelCountis equal to 1.- The
sizedepth/array layer count value is equal to 1. usageincludes theGPUTextureUsage.RENDER_ATTACHMENTflag.usagedoes not include theGPUTextureUsage.STORAGE_BINDINGflag.- The specified format supports multi-sampling.
- The
mipLevelCountvalue is less than or equal to the maximum miplevel count. - The formats specified in
formatandviewFormatsare compatible with one another. - If
usageincludes theGPUTextureUsage.RENDER_ATTACHMENTflag:formatis a renderable format (meaning a color renderable format, or a depth-or-stencil format).dimensionis set to"2d".
- If
usageincludes theGPUTextureUsage.STORAGE_BINDINGflag:- The specified
formatincludes theSTORAGE_BINDINGcapability (see the Plain color formats table for reference).
- The specified
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
HTMLImageElementand creating an image bitmap usingcreateImageBitmap(). - Creating a new texture 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
| Specification |
|---|
| WebGPU # dom-gpudevice-createtexture |
Browser compatibility
BCD tables only load in the browser
See also
- The WebGPU API