|
| 1 | +import {GPUPrimitivesDocsTabs} from '@site/src/components/docs/gpu-primitives-docs-tabs'; |
| 2 | + |
| 3 | +# GPUFFT2D |
| 4 | + |
| 5 | +<GPUPrimitivesDocsTabs active="fft2d" /> |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +`GPUFFT2D` records a bounded, out-of-place two-dimensional complex fast Fourier transform on |
| 10 | +WebGPU. It accepts caller-owned row-major storage buffers, owns one equally sized scratch buffer, |
| 11 | +and records every bit-reversal and radix-2 butterfly pass onto the application's |
| 12 | +`CommandEncoder`. It never submits commands or reads values back to the CPU. |
| 13 | + |
| 14 | +The initial implementation targets reusable simulation and signal-processing foundations such as |
| 15 | +spectral oceans, frequency-domain filters, convolution, and procedural fields. It deliberately |
| 16 | +does not own textures, convert real-valued inputs, select padding dimensions, or hide command |
| 17 | +submission. |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +Each complex value occupies two consecutive `float32` components: real followed by imaginary. |
| 22 | +Values are row-major, so the complete field contains `width * height * 2` floats. |
| 23 | + |
| 24 | +```ts |
| 25 | +import {Buffer} from '@luma.gl/core'; |
| 26 | +import {GPUFFT2D} from '@luma.gl/experimental'; |
| 27 | + |
| 28 | +const width = 256; |
| 29 | +const height = 256; |
| 30 | +const complexByteLength = width * height * 2 * Float32Array.BYTES_PER_ELEMENT; |
| 31 | + |
| 32 | +const inputBuffer = device.createBuffer({ |
| 33 | + data: initialComplexValues, |
| 34 | + usage: Buffer.STORAGE | Buffer.COPY_DST |
| 35 | +}); |
| 36 | +const frequencyBuffer = device.createBuffer({ |
| 37 | + byteLength: complexByteLength, |
| 38 | + usage: Buffer.STORAGE |
| 39 | +}); |
| 40 | +const reconstructedBuffer = device.createBuffer({ |
| 41 | + byteLength: complexByteLength, |
| 42 | + usage: Buffer.STORAGE |
| 43 | +}); |
| 44 | +const transform = new GPUFFT2D(device, {width, height}); |
| 45 | + |
| 46 | +const commandEncoder = device.createCommandEncoder({id: 'spectral-step'}); |
| 47 | +transform.encode(commandEncoder, { |
| 48 | + inputBuffer, |
| 49 | + outputBuffer: frequencyBuffer, |
| 50 | + direction: 'forward' |
| 51 | +}); |
| 52 | +transform.encode(commandEncoder, { |
| 53 | + inputBuffer: frequencyBuffer, |
| 54 | + outputBuffer: reconstructedBuffer, |
| 55 | + direction: 'inverse' |
| 56 | +}); |
| 57 | +device.submit(commandEncoder.finish()); |
| 58 | +``` |
| 59 | + |
| 60 | +The two calls above compose in one command buffer. The second transform observes the first |
| 61 | +transform's output through ordinary WebGPU command ordering; no intermediate submission or CPU |
| 62 | +synchronization is required. |
| 63 | + |
| 64 | +## Constructor |
| 65 | + |
| 66 | +### `new GPUFFT2D(device, props)` |
| 67 | + |
| 68 | +```ts |
| 69 | +type GPUFFT2DProps = { |
| 70 | + id?: string; |
| 71 | + width: number; |
| 72 | + height: number; |
| 73 | +}; |
| 74 | +``` |
| 75 | + |
| 76 | +`width` and `height` must each be powers of two from 2 through 2048. Rectangular transforms are |
| 77 | +supported. The bound keeps allocation and dispatch costs predictable: the maximum field contains |
| 78 | +4,194,304 complex values and occupies 32 MiB per complex buffer. |
| 79 | + |
| 80 | +Construction allocates one field-sized scratch buffer, one compute pipeline, and two immutable |
| 81 | +32-byte parameter buffers per pass so forward and inverse encodings never race through rewritten |
| 82 | +uniforms. Input and output storage remain caller-owned. |
| 83 | + |
| 84 | +## Encoding |
| 85 | + |
| 86 | +### `encode(commandEncoder, options): Buffer` |
| 87 | + |
| 88 | +```ts |
| 89 | +type GPUFFT2DEncodeOptions = { |
| 90 | + inputBuffer: Buffer; |
| 91 | + outputBuffer: Buffer; |
| 92 | + direction?: 'forward' | 'inverse'; |
| 93 | +}; |
| 94 | +``` |
| 95 | + |
| 96 | +Both buffers must belong to the transform's device, declare `Buffer.STORAGE`, and contain at least |
| 97 | +`stats.complexBufferByteLength` bytes. They must be separate allocations; the source is never |
| 98 | +modified. `encode()` returns `outputBuffer` for convenient downstream binding. |
| 99 | + |
| 100 | +The normalization convention is: |
| 101 | + |
| 102 | +- `forward`: negative complex exponent and no normalization; |
| 103 | +- `inverse`: positive complex exponent and division by `width * height` on the final pass. |
| 104 | + |
| 105 | +The transform first bit-reverses and evaluates every row, then does the same for every column. |
| 106 | +Passes ping-pong between the class-owned scratch field and the caller's output so the final pass |
| 107 | +always lands in `outputBuffer`. |
| 108 | + |
| 109 | +## Support query |
| 110 | + |
| 111 | +### `getGPUFFT2DSupport(device, props): GPUFFT2DSupport` |
| 112 | + |
| 113 | +The support query validates dimensions before allocation and reports WebGPU compute, workgroup, |
| 114 | +dispatch, storage-binding, and buffer-size limits. A valid plan is included in `stats` even when a |
| 115 | +device limit prevents execution. |
| 116 | + |
| 117 | +```ts |
| 118 | +const support = getGPUFFT2DSupport(device, {width: 512, height: 256}); |
| 119 | +if (!support.supported) { |
| 120 | + console.warn(support.reason); |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +## Statistics |
| 125 | + |
| 126 | +`transform.stats` is an immutable `GPUFFT2DStats` object: |
| 127 | + |
| 128 | +| Field | Meaning | |
| 129 | +| --- | --- | |
| 130 | +| `width`, `height`, `elementCount` | Logical complex-field dimensions and value count. | |
| 131 | +| `complexBufferByteLength` | Minimum byte length of input, output, and scratch fields. | |
| 132 | +| `horizontalStageCount`, `verticalStageCount` | Radix-2 butterfly stages per axis. | |
| 133 | +| `passCount`, `dispatchCountPerEncode` | Two bit-reversal passes plus all butterfly stages. | |
| 134 | +| `workgroupSize`, `workgroupCount` | Fixed 8-by-8 invocation tile and dispatch grid. | |
| 135 | +| `scratchBufferByteLength` | Class-owned transform scratch. | |
| 136 | +| `parameterBufferCount`, `parameterBufferByteLength` | Immutable forward/inverse pass metadata. | |
| 137 | + |
| 138 | +`makeGPUFFT2DStats(width, height)` computes the same plan without a device or GPU allocation. |
| 139 | + |
| 140 | +## Ownership and lifecycle |
| 141 | + |
| 142 | +`GPUFFT2D` owns only its compute pipeline, scratch buffer, and parameter buffers. The caller owns |
| 143 | +input buffers, output buffers, command encoders, submission, and any optional readback. Destroying |
| 144 | +the transform releases only class-owned resources and is idempotent. Previously supplied caller |
| 145 | +buffers remain valid. |
| 146 | + |
| 147 | +The same instance may encode more than one ordered transform into a command encoder. Do not encode |
| 148 | +the same instance concurrently into command buffers that may execute simultaneously because those |
| 149 | +encodings share its scratch field. Use separate instances when independent queues or overlapping |
| 150 | +submissions need the same dimensions. |
| 151 | + |
| 152 | +## Current limits |
| 153 | + |
| 154 | +- WebGPU only; no WebGL fallback. |
| 155 | +- Power-of-two dimensions from 2 through 2048. |
| 156 | +- Packed row-major complex `float32` fields only. |
| 157 | +- Out-of-place input and output only. |
| 158 | +- One shared scratch field per instance; no concurrent execution contract. |
| 159 | +- No hidden padding, real-to-complex packing, texture conversion, submission, or readback. |
| 160 | + |
| 161 | +These constraints keep the primitive small and predictable while allowing higher-level systems to |
| 162 | +define spectrum generation, texture outputs, cascade policy, and scheduling separately. |
0 commit comments