Skip to content

UWAL / Texture

Type Aliases ​

Usage ​

ts
type Usage = Readonly<Record<"RENDER" | "STORAGE", GPUTextureUsageFlags>>;

GPUTextureUsageFlags utility bitmasks:

  • Usage.RENDER is the default usage when creating a texture.
  • Usage.STORAGE is the default usage when creating a storage texture.

Type Parameters ​

Type Parameter

Classes ​

Texture ​

Utility class to create and manage textures and samplers.
Upon instantiation, it returns a promise of itself.

Param ​

Renderer

Optional Renderer instance required in some methods.

Example ​

ts
const Texture = new (await UWAL.Texture());

Methods ​

CreateTexture() ​
ts
CreateTexture(descriptor): GPUTexture;

Create a new texture from the descriptor object.

Parameters ​
ParameterTypeDescription
descriptorPick<Partial<GPUTextureDescriptor>, "format" | "usage"> & Omit<GPUTextureDescriptor, "format" | "usage">GPUTextureDescriptor object with optional format and usage properties which default to Device.PreferredCanvasFormat and Usage.RENDER, respectively.
Returns ​

GPUTexture

See ​

https://www.w3.org/TR/webgpu/#dom-gpudevice-createtexture

WriteTexture() ​
ts
WriteTexture(data, options): void;

Write data into a GPUTexture using options.

Parameters ​
ParameterTypeDescription
dataGPUAllowSharedBufferSourceBuffer data to write.
optionsGPUTexelCopyTextureInfo & Partial<GPUExtent3DDict> & Partial<Record<"size", Iterable<number, any, any>>> & GPUTexelCopyBufferLayoutA union of the destination, dataLayout and size arguments of the GPUQueue.writeTexture() method.
Returns ​

void

See ​

https://www.w3.org/TR/webgpu/#dom-gpuqueue-writetexture

CreateStorageTexture() ​
ts
CreateStorageTexture(descriptor?): GPUTexture;

Create a new storage texture.

Parameters ​
ParameterTypeDescription
descriptorPick<Partial<GPUTextureDescriptor>, "format" | "usage" | "size"> & Omit<GPUTextureDescriptor, "format" | "usage" | "size">GPUTextureDescriptor object with optional format, usage and size properties which default to Device.PreferredCanvasFormat, Usage.STORAGE, and Renderer.CanvasSize when Renderer is provided.
Returns ​

GPUTexture

Throws ​

ERROR.DESCRIPTOR_SIZE_NOT_FOUND if Renderer and size parameter are not provided.

CreateTextureFromSource() ​
ts
CreateTextureFromSource(source, descriptor?): GPUTexture;

Create a new texture using a GPUTexture or any valid image source. Texture's size and mipLevelCount properties are authomatically calculated if not defined explicitly in the descriptor object.

Parameters ​
ParameterTypeDescription
sourceGPUSourceTextureGPUTexture or an image source.
descriptor?boolean | TextureDescriptorCreateTexture descriptor object.
Returns ​

GPUTexture

CreateSampler() ​
ts
CreateSampler(descriptor?): GPUSampler;

Create a new sampler from the descriptor object.

Parameters ​
ParameterTypeDescription
descriptor?GPUSamplerDescriptor & SamplerDescriptorGPUSamplerDescriptor object extended with optional properties: addressModeUV for width and height; addressMode is for all 3 dimensions; minMagFilter for min and mag, and filter for min, mag and mipmapFilter. Defaults to {filter: "linear"}.
Returns ​

GPUSampler

See ​

https://www.w3.org/TR/webgpu/#dom-gpudevice-createsampler

ImportExternalTexture() ​
ts
ImportExternalTexture(
   source, 
   colorSpace?, 
   label?
): GPUExternalTexture;

Create a new GPUExternalTexture from a video source.

Parameters ​
ParameterTypeDescription
sourceHTMLVideoElement | VideoFrameVideo source frame or element.
colorSpace?PredefinedColorSpaceColor space to convert into.
label?stringTexture label.
Returns ​

GPUExternalTexture

See ​

CreateImageBitmap() ​
ts
CreateImageBitmap(
   source, 
   options?, 
   requestOptions?
): Promise<ImageBitmap>;

Create a bitmap image from a valid source path.

Parameters ​
ParameterTypeDescription
sourcestringImage source path.
options?ImageBitmapOptionsBitmap options, defaults to {colorSpaceConversion: "none"}.
requestOptions?RequestInitfetch request options.
Returns ​

Promise<ImageBitmap>

See ​

https://developer.mozilla.org/en-US/docs/Web/API/Window/createImageBitmap

CreateMultisampleTexture() ​
ts
CreateMultisampleTexture(
   force?, 
   sampleCount?, 
   label?
): void;

Create and assign a multisampled texture to the Renderer.MultisampleTexture.

Parameters ​
ParameterTypeDefault valueDescription
force?booleanfalseForce a new texture to be created.
sampleCount?number4Must be either 1 or 4.
label?stringundefinedTexture label.
Returns ​

void

Throws ​

ERROR.RENDERER_NOT_FOUND if Renderer is not provided.

CreateCubeTexture() ​
ts
CreateCubeTexture(
   sources, 
   bitmapOptions?, 
   textureDescriptor?, 
   copyOptions?, 
   requestOptions?
): Promise<GPUTexture>;

Create a "cube" texture from 6 image sources. All images must have the same dimensions. This method creates a bitmap image for each source and one cube texture and uses that as the destination when copying bitmaps with flipY option set to false.

Parameters ​
ParameterTypeDescription
sourcesstring[]Array of 6 image source paths.
bitmapOptions?ImageBitmapOptionsBitmap options for CreateImageBitmap method, defaults to {colorSpaceConversion: "none"}.
textureDescriptor?TextureDescriptorTexture descriptor for CreateTextureFromSource method, defaults to {mipmaps: true}.
copyOptions?CopyImageOptionsCopy options for CopyImageToTexture method, method, defaults to {flipY: false}.
requestOptions?RequestInitfetch request options for CreateImageBitmap method.
Returns ​

Promise<GPUTexture>

See ​
Throws ​

ERROR.RENDERER_NOT_FOUND if Renderer is not provided.

CopyImageToTexture() ​
ts
CopyImageToTexture(source, options?): Promise<GPUTexture>;

Copy an image source into a texture and optionally generate mipmaps for it. If options.texture is not provided, a new GPUTexture is created by default.

Parameters ​
ParameterTypeDescription
sourceGPUCopyExternalImageSourceImage source.
options?Partial<GPUExtent3DDict> & Partial<Record<"size", Iterable<number, any, any>>> & CopyImageOptionsDefaults to {create: true, flipY: true}.
Returns ​

Promise<GPUTexture>

See ​

https://www.w3.org/TR/webgpu/#dom-gpuqueue-copyexternalimagetotexture

Throws ​

ERROR.TEXTURE_NOT_FOUND if called without options.texture when options.mipmaps is true or undefined and options.create is falsy.

CopyTextureToTexture() ​
ts
CopyTextureToTexture(options): void;

Copy options.srcTexture into options.dstTexture. If options.dstTexture is omitted, a new texture is created from options.srcTexture using options.create descriptor.

Parameters ​
ParameterTypeDescription
optionsPartial<GPUExtent3DDict> & Partial<Record<"size", Iterable<number, any, any>>> & CopyTextureOptionsA union of the source, destination, and copySize arguments of the GPUCommandEncoder.copyTextureToTexture() method.
Returns ​

void

See ​

https://www.w3.org/TR/webgpu/#dom-gpucommandencoder-copytexturetotexture

Throws ​

ERROR.RENDERER_NOT_FOUND if Renderer is not provided.

CopyTextureToBuffer() ​
ts
CopyTextureToBuffer(options): void;

Copy options.texture into options.buffer.

Parameters ​
ParameterTypeDescription
optionsCopyBufferOptions & GPUTexelCopyTextureInfo & Partial<GPUExtent3DDict> & Partial<Record<"size", Iterable<number, any, any>>> & GPUTexelCopyBufferInfoA union of the source, destination, and copySize arguments of the GPUCommandEncoder.copyTextureToBuffer() method.
Returns ​

void

See ​

https://www.w3.org/TR/webgpu/#dom-gpucommandencoder-copytexturetobuffer

Throws ​

ERROR.RENDERER_NOT_FOUND if Renderer is not provided.

CopyBufferToTexture() ​
ts
CopyBufferToTexture(options): void;

Copy options.buffer into options.texture.

Parameters ​
ParameterTypeDescription
optionsGPUTexelCopyBufferInfo & CopyBufferOptions & GPUTexelCopyTextureInfo & Partial<GPUExtent3DDict> & Partial<Record<"size", Iterable<number, any, any>>>A union of the source, destination, and copySize arguments of the GPUCommandEncoder.copyBufferToTexture() method.
Returns ​

void

See ​

https://www.w3.org/TR/webgpu/#dom-gpucommandencoder-copybuffertotexture

Throws ​

ERROR.RENDERER_NOT_FOUND if Renderer is not provided.

Accessors ​

PreferredStorageFormat ​
Get Signature ​
ts
get PreferredStorageFormat(): GPUTextureFormat;

Get the optimal GPUTextureFormat for storage textures.

Returns ​

GPUTextureFormat

The only possible values are "rgba8unorm" and "bgra8unorm".

Renderer ​
Set Signature ​
ts
set Renderer(renderer): void;
Parameters ​
ParameterTypeDescription
rendererRenderStageRenderer instance required in some methods.
Returns ​

void

Released under the MIT License.