Creates a new WebGPUSinglePassDownsampler. On its own, WebGPUSinglePassDownsampler does not allocate any GPU resources. Optionally, prepare GPU resources for a given SPDPrepareDeviceDescriptor.
Optional
prepareDescriptor: SPDPrepareDeviceDescriptorAn optional descriptor for preparing GPU resources
WebGPUSinglePassDownsampler.prepareDeviceResources
Private
devicePrivate
filtersReadonly
supportedThe set of formats supported by WebGPU SPD.
Note that bgra8unorm
is only supported if the device feature bgra8unorm-storage
is enabled.
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.
The device to use for downsampling the texture
The texture to generate mipmaps for. Must support generating a GPUTextureView with GPUTextureViewDimension:"2d-array".
Optional
config: SPDPassConfigThe config for mipmap generation
True if mipmaps were generated, false otherwise
If WebGPUSinglePassDownsampler.preparePass threw an error.
WebGPUSinglePassDownsampler.preparePass
Private
getPrepares GPU resources required by WebGPUSinglePassDownsampler to downsample textures for a given SPDPrepareDeviceDescriptor.
a descriptor for preparing GPU resources
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.
The device the SPDPass should be prepared for
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: SPDPassConfigThe config for the SPDPass
The prepared SPDPass or undefined if preparation failed or if no mipmaps would be generated.
If the GPUTextureFormat of SPDPassConfig.target is not supported (does not support GPUStorageTextureAccess:"write-only" on the given device).
If the size of SPDPassConfig.target is too small to store the first mip level generated for texture
If texture or SPDPassConfig.target is not a 2d texture.
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>
The unique name of the filter operation
The WGSL code to inject into the downsampling shader as the filter operation
Static
setSets 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.
Optional
limits: Record<string, number>A record of device limits set to update with the preferred limits for WebGPUSinglePassDownsampler
Optional
adapter: GPUAdapterIf this is set, the preferred limits that are set by this function will be clamped to GPUAdapter.limits.
The updated or created set of device limits with all preferred limits for WebGPUSinglePassDownsampler set
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.