/// import { HasReactive } from "@reactively/decorate"; import { Cache, ComposableShader, ValueOrFn, Vec2 } from "thimbleberry"; import { HistogramModule } from "../binop/HistogramModule.js"; import { ComponentName, LoadComponent } from "../util/GenerateLoadTexel.js"; export interface HistogramTextureParams { device: GPUDevice; /** * Source data to be reduced. * * A function returning the source buffer will be executed lazily, * and reexecuted if the function's `@reactively` source values change. */ source: ValueOrFn; /** {@inheritDoc HistogramTexture#blockSize} */ blockSize?: Vec2; /** {@inheritDoc HistogramTexture#bufferBlockLength} */ bufferBlockLength?: number; /** {@inheritDoc HistogramTexture#forceWorkgroupSize} */ forceWorkgroupSize?: Vec2; /** {@inheritDoc HistogramTexture#histogramOps} */ histogramOps: HistogramModule; /** {@inheritDoc HistogramTexture#minMaxBuffer} */ minMaxBuffer?: GPUBuffer; /** {@inheritDoc HistogramTexture#bucketSums} */ bucketSums?: boolean; /** {@inheritDoc HistogramTexture#range} */ range?: Vec2; /** {@inheritDoc HistogramTexture#sourceComponent} */ sourceComponent?: ComponentName | LoadComponent; /** cache for GPUComputePipeline */ pipelineCache?: () => Cache; /** {@inheritDoc HistogramTexture#label} */ label?: string; } /** * Calculate a histogram from a source texture. * * Internally, a cascade of shaders is run: the first shader calculates a separate * histogram for each part of the screen, and then a series of reduction shaders * combine the partial histograms. * * The number of buckets in the histogram is configurable. * * The range of values to spread across the buckets is also configurable. * The range may be specified by the cpu, or dynamically provided * some other shader writing to a two element GPUBuffer. * * Optionally, the sum for each bucket can also collected in parallel with the * frequency counts in the histogram. The sums are reported in a separate buffer. */ export declare class HistogramTexture extends HasReactive implements ComposableShader { source: GPUTexture; /** length of block to read per thread when reading from the source texture */ blockSize: Vec2; /** number of histograms to read per thread when reducing from buffer to buffer */ bufferBlockLength: number | undefined; /** wgsl macros for histogram reduction and histogram size. * Typically call `histogramModule()` */ histogramOps: HistogramModule; /** select or synthesize a component from the source texture * @defaultValue "r" */ sourceComponent: ComponentName | LoadComponent; /** range of histogram values (or provide minMaxBuffer) * @defaultValue [0, 255] */ range?: Vec2; /** buffer containing min and max values for the histogram range (or use range) */ minMaxBuffer?: GPUBuffer; /** Override to set compute workgroup size e.g. for testing. @defaultValue maxComputeInvocationsPerWorkgroup of the `GPUDevice` (256) */ forceWorkgroupSize: Vec2 | undefined; /** optinally calculate sums for each bucket in addition to counts * @defaultValue false */ bucketSums: boolean; /** Debug label attached to gpu objects for error reporting */ label?: string; device: GPUDevice; private usageContext; private pipelineCache?; constructor(params: HistogramTextureParams); commands(commandEncoder: GPUCommandEncoder): void; destroy(): void; /** Execute the histogram immediately and copy the results back to the CPU. * (results are copied from the {@link HistogramTexture.result} GPUBuffer) * * To use HistogramTexture in concert with external shaders, * instead use {@link HistogramTexture.commands} or `ShaderGroup`. * * @returns a single histogram in an array */ histogram(): Promise; /** accumulated histogram (frequency counts) */ get result(): GPUBuffer; /** accumulated bucket counts, only valid if `bucketSums` is true */ get sumsResult(): GPUBuffer; /** create a range buffer if necessary */ private get rangeBuffer(); /** create a buffer to hold the range values */ private get createdRangeBuffer(); /** all shaders needed to reduce the texture to a single reduced value */ private get shaders(); /** shader to reduce the texture to a buffer */ private get textureToHistograms(); /** created only if necessary, a shader to reduce the histograms buffer to a single element */ private get reduceBucketCounts(); /** created only if necessary, a shader to reduce the histograms buffer to a single element */ private get reduceSumCounts(); private get reduceBufferNeeded(); }