Class WebGPUSinglePassDownsampler

A helper class for downsampling 2D GPUTexture (& arrays) using as few passes as possible on a GPUDevice depending on its GPUSupportedLimits. Up to 12 mip levels can be generated within a single pass, if GPUSupportedLimits.maxStorageTexturesPerShaderStage supports it.

Constructors

Properties

devicePipelines: WeakMap<GPUDevice, DevicePipelines>
filters: Map<string, string>
supportedFormats: Set<string> = ...

The set of formats supported by WebGPU SPD.

Note that bgra8unorm is only supported if the device feature bgra8unorm-storage is enabled.

Methods

  • Deregisters all resources stored for a given device.

    Parameters

    • device: GPUDevice

      The device resources should be deregistered for

    Returns void

  • Generates mipmaps for the given texture. For textures that will be downsampled more than once, consider generating a SPDPass using WebGPUSinglePassDownsampler.preparePass and calling its SPDPass.encode method. This way, allocated GPU resources for downsampling the texture can be reused.

    Parameters

    • device: GPUDevice

      The device to use for downsampling the texture

    • texture: GPUTexture

      The texture to generate mipmaps for. Must support generating a GPUTextureView with GPUTextureViewDimension:"2d-array".

    • Optional config: SPDPassConfig

      The config for mipmap generation

    Returns boolean

    True if mipmaps were generated, false otherwise

    Throws

    If WebGPUSinglePassDownsampler.preparePass threw an error.

    See

    WebGPUSinglePassDownsampler.preparePass

  • Parameters

    • device: GPUDevice
    • Optional maxArrayLayers: number
    • Optional maxMipsPerPass: number

    Returns undefined | DevicePipelines

  • Prepares a pass to downsample a 2d texture / 2d texture array. The produced SPDPass can be used multiple times to repeatedly downsampling a texture, e.g., for downsampling the depth buffer each frame. For one-time use, WebGPUSinglePassDownsampler.generateMipmaps can be used instead.

    By default, the texture is downsampled texture.mipLevelCount - 1 times using an averaging filter, i.e., 4 pixel values from the parent level are averaged to produce a single pixel in the current mip level. This behavior can be configured using the optional config parameter. For example, instead of writing the mip levels into the input texture itself, a separate target texture can be specified using SPDPassConfig.target. Other configuration options include using a different (possibly custom) filter, only downsampling a subregion of the input texture, and limiting the number of mip levels to generate, e.g., if a min-max pyramid is only needed up to a certain tile resolution. If the given filter does not exist, an averaging filter will be used as a fallback. The image region to downsample and the number of mip levels to generate are clamped to the input texture's size, and the output texture's mipLevelCount.

    Depending on the number of mip levels to generate and the device's maxStorageTexturesPerShaderStage limit, the SPDPass will internally consist of multiple passes, each generating up to min(maxStorageTexturesPerShaderStage, 12) mip levels.

    Parameters

    • device: GPUDevice

      The device the SPDPass should be prepared for

    • texture: GPUTexture

      The texture that is to be processed by the SPDPass. Must support generating a GPUTextureView with GPUTextureViewDimension:"2d-array". Must support GPUTextureUsage.TEXTURE_BINDING, and, if no other target is given, GPUTextureUsage.STORAGE_BINDING.

    • Optional config: SPDPassConfig

      The config for the SPDPass

    Returns undefined | SPDPass

    The prepared SPDPass or undefined if preparation failed or if no mipmaps would be generated.

    Throws

    If the GPUTextureFormat of SPDPassConfig.target is not supported (does not support GPUStorageTextureAccess:"write-only" on the given device).

    Throws

    If the size of SPDPassConfig.target is too small to store the first mip level generated for texture

    Throws

    If texture or SPDPassConfig.target is not a 2d texture.

    See

    • WebGPUSinglePassDownsampler.generateMipmaps
    • WebGPUSinglePassDownsampler.registerFilter
    • WebGPUSinglePassDownsampler.setPreferredLimits
  • Registers a new downsampling filter operation that can be injected into the downsampling shader for new pipelines.

    The given WGSL code must (at least) specify a function to reduce four values into one with the following name and signature:

    spd_reduce_4(v0: vec4<SPDScalar>, v1: vec4<SPDScalar>, v2: vec4<SPDScalar>, v3: vec4<SPDScalar>) -> vec4<SPDScalar>

    Parameters

    • name: string

      The unique name of the filter operation

    • wgsl: string

      The WGSL code to inject into the downsampling shader as the filter operation

    Returns void

  • Sets the preferred device limits for WebGPUSinglePassDownsampler in a given record of limits. Existing preferred device limits are either increased or left untouched. If limits is undefined, creates a new record of preferred device limits for WebGPUSinglePassDownsampler. The result can be used to set GPUDeviceDescriptor.requiredLimits when requesting a device.

    Parameters

    • Optional limits: Record<string, number>

      A record of device limits set to update with the preferred limits for WebGPUSinglePassDownsampler

    • Optional adapter: GPUAdapter

      If this is set, the preferred limits that are set by this function will be clamped to GPUAdapter.limits.

    Returns Record<string, number>

    The updated or created set of device limits with all preferred limits for WebGPUSinglePassDownsampler set