insomni-plot 0.1.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +674 -0
- package/README.md +81 -0
- package/dist/core.d.mts +340 -0
- package/dist/core.mjs +1047 -0
- package/dist/index.d.mts +3426 -0
- package/dist/index.mjs +12762 -0
- package/dist/interactions-DEFL_F4E.mjs +5395 -0
- package/dist/range-presets-CzECsu3V.d.mts +1523 -0
- package/package.json +34 -0
- package/src/annotations.d.ts +121 -0
- package/src/annotations.ts +438 -0
- package/src/axis.d.ts +184 -0
- package/src/axis.test.ts +131 -0
- package/src/axis.ts +765 -0
- package/src/colorbar.d.ts +69 -0
- package/src/colorbar.ts +294 -0
- package/src/colors.d.ts +57 -0
- package/src/colors.test.ts +28 -0
- package/src/colors.ts +486 -0
- package/src/core.ts +299 -0
- package/src/format.d.ts +54 -0
- package/src/format.ts +138 -0
- package/src/grammar/accessibility.d.ts +147 -0
- package/src/grammar/accessibility.test.ts +199 -0
- package/src/grammar/accessibility.ts +443 -0
- package/src/grammar/aes.d.ts +35 -0
- package/src/grammar/aes.test.ts +75 -0
- package/src/grammar/aes.ts +120 -0
- package/src/grammar/annotations.d.ts +86 -0
- package/src/grammar/annotations.test.ts +68 -0
- package/src/grammar/annotations.ts +336 -0
- package/src/grammar/attach-brush.d.ts +44 -0
- package/src/grammar/attach-brush.test.ts +214 -0
- package/src/grammar/attach-brush.ts +111 -0
- package/src/grammar/attach-presets.d.ts +33 -0
- package/src/grammar/attach-presets.test.ts +106 -0
- package/src/grammar/attach-presets.ts +215 -0
- package/src/grammar/chart.d.ts +952 -0
- package/src/grammar/chart.test.ts +118 -0
- package/src/grammar/chart.ts +1172 -0
- package/src/grammar/color-utils.d.ts +29 -0
- package/src/grammar/color-utils.test.ts +53 -0
- package/src/grammar/color-utils.ts +66 -0
- package/src/grammar/constants.d.ts +45 -0
- package/src/grammar/constants.ts +61 -0
- package/src/grammar/coord.d.ts +183 -0
- package/src/grammar/coord.test.ts +355 -0
- package/src/grammar/coord.ts +619 -0
- package/src/grammar/data/pivot.d.ts +57 -0
- package/src/grammar/data/pivot.ts +107 -0
- package/src/grammar/emphasis-driver.d.ts +69 -0
- package/src/grammar/emphasis-driver.test.ts +199 -0
- package/src/grammar/emphasis-driver.ts +205 -0
- package/src/grammar/equality.d.ts +3 -0
- package/src/grammar/equality.ts +40 -0
- package/src/grammar/facet.d.ts +63 -0
- package/src/grammar/facet.test.ts +60 -0
- package/src/grammar/facet.ts +175 -0
- package/src/grammar/geoms/_categorical.d.ts +94 -0
- package/src/grammar/geoms/_categorical.ts +0 -0
- package/src/grammar/geoms/_distribution.d.ts +52 -0
- package/src/grammar/geoms/_distribution.ts +125 -0
- package/src/grammar/geoms/_mark.d.ts +69 -0
- package/src/grammar/geoms/_mark.ts +136 -0
- package/src/grammar/geoms/_shape.d.ts +41 -0
- package/src/grammar/geoms/_shape.ts +74 -0
- package/src/grammar/geoms/aggregate.d.ts +95 -0
- package/src/grammar/geoms/aggregate.test.ts +554 -0
- package/src/grammar/geoms/aggregate.ts +840 -0
- package/src/grammar/geoms/area.d.ts +32 -0
- package/src/grammar/geoms/area.test.ts +165 -0
- package/src/grammar/geoms/area.ts +578 -0
- package/src/grammar/geoms/band.d.ts +27 -0
- package/src/grammar/geoms/band.test.ts +57 -0
- package/src/grammar/geoms/band.ts +126 -0
- package/src/grammar/geoms/bar.d.ts +56 -0
- package/src/grammar/geoms/bar.test.ts +367 -0
- package/src/grammar/geoms/bar.ts +1054 -0
- package/src/grammar/geoms/boxplot.d.ts +129 -0
- package/src/grammar/geoms/boxplot.test.ts +299 -0
- package/src/grammar/geoms/boxplot.ts +834 -0
- package/src/grammar/geoms/connected-scatter.d.ts +27 -0
- package/src/grammar/geoms/connected-scatter.test.ts +157 -0
- package/src/grammar/geoms/connected-scatter.ts +63 -0
- package/src/grammar/geoms/emphasis.d.ts +76 -0
- package/src/grammar/geoms/emphasis.test.ts +135 -0
- package/src/grammar/geoms/emphasis.ts +162 -0
- package/src/grammar/geoms/histogram.d.ts +75 -0
- package/src/grammar/geoms/histogram.test.ts +262 -0
- package/src/grammar/geoms/histogram.ts +740 -0
- package/src/grammar/geoms/index.d.ts +20 -0
- package/src/grammar/geoms/index.ts +77 -0
- package/src/grammar/geoms/interval.d.ts +31 -0
- package/src/grammar/geoms/interval.test.ts +154 -0
- package/src/grammar/geoms/interval.ts +342 -0
- package/src/grammar/geoms/line.d.ts +38 -0
- package/src/grammar/geoms/line.test.ts +247 -0
- package/src/grammar/geoms/line.ts +659 -0
- package/src/grammar/geoms/point.d.ts +57 -0
- package/src/grammar/geoms/point.test.ts +163 -0
- package/src/grammar/geoms/point.ts +545 -0
- package/src/grammar/geoms/polar.test.ts +216 -0
- package/src/grammar/geoms/ribbon.d.ts +21 -0
- package/src/grammar/geoms/ribbon.test.ts +170 -0
- package/src/grammar/geoms/ribbon.ts +87 -0
- package/src/grammar/geoms/ridgeline.d.ts +89 -0
- package/src/grammar/geoms/ridgeline.test.ts +247 -0
- package/src/grammar/geoms/ridgeline.ts +1164 -0
- package/src/grammar/geoms/rolling.d.ts +43 -0
- package/src/grammar/geoms/rolling.test.ts +217 -0
- package/src/grammar/geoms/rolling.ts +387 -0
- package/src/grammar/geoms/rug.d.ts +28 -0
- package/src/grammar/geoms/rug.test.ts +126 -0
- package/src/grammar/geoms/rug.ts +214 -0
- package/src/grammar/geoms/rule.d.ts +23 -0
- package/src/grammar/geoms/rule.test.ts +69 -0
- package/src/grammar/geoms/rule.ts +212 -0
- package/src/grammar/geoms/smooth.d.ts +54 -0
- package/src/grammar/geoms/smooth.test.ts +78 -0
- package/src/grammar/geoms/smooth.ts +337 -0
- package/src/grammar/geoms/text.d.ts +29 -0
- package/src/grammar/geoms/text.test.ts +64 -0
- package/src/grammar/geoms/text.ts +234 -0
- package/src/grammar/geoms/tile.d.ts +61 -0
- package/src/grammar/geoms/tile.test.ts +157 -0
- package/src/grammar/geoms/tile.ts +621 -0
- package/src/grammar/geoms/types.d.ts +319 -0
- package/src/grammar/geoms/types.ts +362 -0
- package/src/grammar/geoms/violin.d.ts +85 -0
- package/src/grammar/geoms/violin.test.ts +187 -0
- package/src/grammar/geoms/violin.ts +672 -0
- package/src/grammar/index.d.ts +22 -0
- package/src/grammar/index.ts +269 -0
- package/src/grammar/interactions/_disposable.d.ts +5 -0
- package/src/grammar/interactions/_disposable.ts +23 -0
- package/src/grammar/interactions/_z.d.ts +4 -0
- package/src/grammar/interactions/_z.ts +16 -0
- package/src/grammar/interactions/brush-selection.test.ts +262 -0
- package/src/grammar/interactions/brush.d.ts +63 -0
- package/src/grammar/interactions/brush.test.ts +483 -0
- package/src/grammar/interactions/brush.ts +452 -0
- package/src/grammar/interactions/crosshair.d.ts +19 -0
- package/src/grammar/interactions/crosshair.test.ts +127 -0
- package/src/grammar/interactions/crosshair.ts +76 -0
- package/src/grammar/interactions/hit-layer.d.ts +64 -0
- package/src/grammar/interactions/hit-layer.ts +246 -0
- package/src/grammar/interactions/legend.d.ts +19 -0
- package/src/grammar/interactions/legend.ts +101 -0
- package/src/grammar/interactions/menu.d.ts +93 -0
- package/src/grammar/interactions/menu.test.ts +373 -0
- package/src/grammar/interactions/menu.ts +342 -0
- package/src/grammar/interactions/selection.d.ts +25 -0
- package/src/grammar/interactions/selection.test.ts +289 -0
- package/src/grammar/interactions/selection.ts +142 -0
- package/src/grammar/interactions/series-readout.d.ts +91 -0
- package/src/grammar/interactions/series-readout.test.ts +668 -0
- package/src/grammar/interactions/series-readout.ts +422 -0
- package/src/grammar/interactions/series-snap.d.ts +70 -0
- package/src/grammar/interactions/series-snap.test.ts +214 -0
- package/src/grammar/interactions/series-snap.ts +218 -0
- package/src/grammar/interactions/tooltip-axis.test.ts +176 -0
- package/src/grammar/interactions/tooltip-touch.browser.test.ts +49 -0
- package/src/grammar/interactions/tooltip-touch.test.ts +161 -0
- package/src/grammar/interactions/tooltip.d.ts +140 -0
- package/src/grammar/interactions/tooltip.test.ts +406 -0
- package/src/grammar/interactions/tooltip.ts +622 -0
- package/src/grammar/interactions/transitions.d.ts +34 -0
- package/src/grammar/interactions/transitions.test.ts +172 -0
- package/src/grammar/interactions/transitions.ts +160 -0
- package/src/grammar/layout.d.ts +68 -0
- package/src/grammar/layout.ts +186 -0
- package/src/grammar/legend-merge.test.ts +332 -0
- package/src/grammar/mount.d.ts +78 -0
- package/src/grammar/mount.test.ts +479 -0
- package/src/grammar/mount.ts +2112 -0
- package/src/grammar/palettes.d.ts +54 -0
- package/src/grammar/palettes.test.ts +80 -0
- package/src/grammar/palettes.ts +167 -0
- package/src/grammar/pan-zoom.test.ts +398 -0
- package/src/grammar/phylo.d.ts +65 -0
- package/src/grammar/phylo.test.ts +59 -0
- package/src/grammar/phylo.ts +112 -0
- package/src/grammar/pipeline.auto-ticks.test.ts +40 -0
- package/src/grammar/pipeline.d.ts +158 -0
- package/src/grammar/pipeline.test.ts +463 -0
- package/src/grammar/pipeline.ts +1233 -0
- package/src/grammar/profiling.d.ts +8 -0
- package/src/grammar/profiling.ts +24 -0
- package/src/grammar/scales.d.ts +188 -0
- package/src/grammar/scales.test.ts +181 -0
- package/src/grammar/scales.ts +800 -0
- package/src/grammar/svg.d.ts +3 -0
- package/src/grammar/svg.ts +39 -0
- package/src/grammar/theme.d.ts +261 -0
- package/src/grammar/theme.test.ts +105 -0
- package/src/grammar/theme.ts +490 -0
- package/src/heatmap/cpu.ts +109 -0
- package/src/heatmap/gpu.ts +565 -0
- package/src/heatmap/types.ts +177 -0
- package/src/heatmap.browser.test.ts +308 -0
- package/src/heatmap.test.ts +320 -0
- package/src/heatmap.ts +123 -0
- package/src/index.d.ts +1 -0
- package/src/index.ts +8 -0
- package/src/interactions.d.ts +48 -0
- package/src/interactions.test.ts +226 -0
- package/src/interactions.ts +394 -0
- package/src/layout/box.d.ts +48 -0
- package/src/layout/box.test.ts +107 -0
- package/src/layout/box.ts +143 -0
- package/src/legend.d.ts +115 -0
- package/src/legend.ts +422 -0
- package/src/marks/curve.d.ts +43 -0
- package/src/marks/curve.ts +244 -0
- package/src/marks/stack.d.ts +53 -0
- package/src/marks/stack.ts +184 -0
- package/src/marks.d.ts +273 -0
- package/src/marks.test.ts +541 -0
- package/src/marks.ts +1292 -0
- package/src/navigator.test.ts +174 -0
- package/src/navigator.ts +393 -0
- package/src/range-presets.d.ts +113 -0
- package/src/range-presets.test.ts +345 -0
- package/src/range-presets.ts +349 -0
- package/src/scales.d.ts +98 -0
- package/src/scales.test.ts +103 -0
- package/src/scales.ts +695 -0
- package/src/stats/index.d.ts +200 -0
- package/src/stats/index.test.ts +349 -0
- package/src/stats/index.ts +740 -0
- package/src/stats/regression.d.ts +38 -0
- package/src/stats/regression.test.ts +56 -0
- package/src/stats/regression.ts +396 -0
- package/src/stats/rolling-window.d.ts +55 -0
- package/src/stats/rolling-window.test.ts +237 -0
- package/src/stats/rolling-window.ts +256 -0
- package/src/test-setup.ts +19 -0
- package/src/viewport/axis-state.d.ts +72 -0
- package/src/viewport/axis-state.ts +476 -0
- package/src/viewport.d.ts +170 -0
- package/src/viewport.test.ts +363 -0
- package/src/viewport.ts +510 -0
|
@@ -0,0 +1,565 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// GPU heatmap renderer — used by Renderer2D / WebGPU path.
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
|
|
5
|
+
import type { TgpuRoot } from "typegpu";
|
|
6
|
+
import { createLayer, Layer, type Renderer2D, Texture } from "insomni";
|
|
7
|
+
|
|
8
|
+
import type { ContinuousPalette } from "../colors.ts";
|
|
9
|
+
import { LUT_SIZE, WG_1D, WG_2D, clamp01, nextPow2, type ResolvedSpec } from "./types.ts";
|
|
10
|
+
|
|
11
|
+
interface GpuState {
|
|
12
|
+
root: TgpuRoot;
|
|
13
|
+
device: GPUDevice;
|
|
14
|
+
dataBuffer: GPUBuffer;
|
|
15
|
+
dataCapacity: number; // datums
|
|
16
|
+
binBuffer: GPUBuffer; // atomic<i32>[nx*ny]
|
|
17
|
+
maxBuffer: GPUBuffer; // atomic<i32>[1]
|
|
18
|
+
paramsBuffer: GPUBuffer; // uniform
|
|
19
|
+
lutTexture: GPUTexture;
|
|
20
|
+
lutView: GPUTextureView;
|
|
21
|
+
outputTexture: GPUTexture;
|
|
22
|
+
outputView: GPUTextureView;
|
|
23
|
+
outputWrapper: Texture;
|
|
24
|
+
clearPipeline: GPUComputePipeline;
|
|
25
|
+
splatPipeline: GPUComputePipeline;
|
|
26
|
+
reducePipeline: GPUComputePipeline;
|
|
27
|
+
colormapPipeline: GPUComputePipeline;
|
|
28
|
+
splatLayout: GPUBindGroupLayout;
|
|
29
|
+
clearBindGroup: GPUBindGroup;
|
|
30
|
+
splatBindGroup: GPUBindGroup;
|
|
31
|
+
reduceBindGroup: GPUBindGroup;
|
|
32
|
+
colormapBindGroup: GPUBindGroup;
|
|
33
|
+
spriteLayer: Layer;
|
|
34
|
+
dataCount: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export class HeatmapGpuRenderer<T> {
|
|
38
|
+
private readonly spec: ResolvedSpec<T>;
|
|
39
|
+
private data: readonly T[];
|
|
40
|
+
private state: GpuState | null = null;
|
|
41
|
+
private dirty = true;
|
|
42
|
+
|
|
43
|
+
constructor(spec: ResolvedSpec<T>, data: readonly T[]) {
|
|
44
|
+
this.spec = spec;
|
|
45
|
+
this.data = data;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
setData(data: readonly T[]): void {
|
|
49
|
+
this.data = data;
|
|
50
|
+
this.dirty = true;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
markDirty(): void {
|
|
54
|
+
this.dirty = true;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Build (or refresh) the GPU heatmap and return the drawable that renders it.
|
|
59
|
+
*
|
|
60
|
+
* `root` owns the GPU buffers/textures/pipelines; `renderer` is the v3
|
|
61
|
+
* {@link Renderer2D} whose `compute(cb)` seam the binning passes are recorded
|
|
62
|
+
* onto. The returned {@link Layer} is a `space:"ui"` layer with a single sprite
|
|
63
|
+
* pointing at the compute-written output texture, drawn in submission order
|
|
64
|
+
* (the same post-main slot as before). The sprite layer is cleared and
|
|
65
|
+
* re-pushed each frame so the pack content naturally changes when data is
|
|
66
|
+
* dirty — no separate `bumpVersion` needed.
|
|
67
|
+
*
|
|
68
|
+
* The compute callback is queued (not submitted) here; it runs when the
|
|
69
|
+
* caller invokes `renderer.render(...)`, immediately before the render pass,
|
|
70
|
+
* so the colormap output is visible to this layer's draw in the same frame.
|
|
71
|
+
*/
|
|
72
|
+
build(root: TgpuRoot, renderer: Renderer2D): Layer {
|
|
73
|
+
const state = this.ensureState(root);
|
|
74
|
+
this.updateSprite(state);
|
|
75
|
+
|
|
76
|
+
if (this.dirty) {
|
|
77
|
+
this.uploadDataBuffer(state);
|
|
78
|
+
renderer.compute((encoder) => {
|
|
79
|
+
const pass = encoder.beginComputePass({ label: "heatmap-pass" });
|
|
80
|
+
this.runPasses(state, pass);
|
|
81
|
+
pass.end();
|
|
82
|
+
});
|
|
83
|
+
this.dirty = false;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return state.spriteLayer;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
destroy(): void {
|
|
90
|
+
if (!this.state) return;
|
|
91
|
+
this.state.dataBuffer.destroy();
|
|
92
|
+
this.state.binBuffer.destroy();
|
|
93
|
+
this.state.maxBuffer.destroy();
|
|
94
|
+
this.state.paramsBuffer.destroy();
|
|
95
|
+
this.state.lutTexture.destroy();
|
|
96
|
+
this.state.outputWrapper.destroy();
|
|
97
|
+
this.state = null;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private ensureState(root: TgpuRoot): GpuState {
|
|
101
|
+
if (this.state) return this.state;
|
|
102
|
+
const spec = this.spec;
|
|
103
|
+
const device = root.device;
|
|
104
|
+
const nx = spec.nx;
|
|
105
|
+
const ny = spec.ny;
|
|
106
|
+
|
|
107
|
+
const binBuffer = device.createBuffer({
|
|
108
|
+
label: "heatmap-bins",
|
|
109
|
+
size: Math.max(nx * ny * 4, 16),
|
|
110
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
|
|
111
|
+
});
|
|
112
|
+
const maxBuffer = device.createBuffer({
|
|
113
|
+
label: "heatmap-max",
|
|
114
|
+
size: 16,
|
|
115
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
|
|
116
|
+
});
|
|
117
|
+
const paramsBuffer = device.createBuffer({
|
|
118
|
+
label: "heatmap-params",
|
|
119
|
+
size: 48, // padded — see params layout below
|
|
120
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const initialCapacity = Math.max(1, this.data.length);
|
|
124
|
+
const dataBuffer = device.createBuffer({
|
|
125
|
+
label: "heatmap-data",
|
|
126
|
+
size: initialCapacity * 16, // vec4<f32> per datum
|
|
127
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const lutTexture = device.createTexture({
|
|
131
|
+
label: "heatmap-lut",
|
|
132
|
+
size: [LUT_SIZE, 1, 1],
|
|
133
|
+
format: "rgba8unorm",
|
|
134
|
+
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST,
|
|
135
|
+
});
|
|
136
|
+
const lutView = lutTexture.createView();
|
|
137
|
+
|
|
138
|
+
const outputTexture = device.createTexture({
|
|
139
|
+
label: "heatmap-output",
|
|
140
|
+
size: [nx, ny, 1],
|
|
141
|
+
format: "rgba8unorm",
|
|
142
|
+
usage:
|
|
143
|
+
GPUTextureUsage.TEXTURE_BINDING |
|
|
144
|
+
GPUTextureUsage.STORAGE_BINDING |
|
|
145
|
+
GPUTextureUsage.COPY_DST,
|
|
146
|
+
});
|
|
147
|
+
const outputView = outputTexture.createView();
|
|
148
|
+
const outputWrapper = new Texture(outputTexture, nx, ny);
|
|
149
|
+
|
|
150
|
+
const clearModule = device.createShaderModule({ label: "heatmap-clear", code: CLEAR_WGSL });
|
|
151
|
+
const splatModule = device.createShaderModule({ label: "heatmap-splat", code: SPLAT_WGSL });
|
|
152
|
+
const reduceModule = device.createShaderModule({
|
|
153
|
+
label: "heatmap-reduce",
|
|
154
|
+
code: REDUCE_WGSL,
|
|
155
|
+
});
|
|
156
|
+
const colormapModule = device.createShaderModule({
|
|
157
|
+
label: "heatmap-colormap",
|
|
158
|
+
code: COLORMAP_WGSL,
|
|
159
|
+
});
|
|
160
|
+
const clearLayout = device.createBindGroupLayout({
|
|
161
|
+
label: "heatmap-clear",
|
|
162
|
+
entries: [
|
|
163
|
+
{ binding: 0, visibility: GPUShaderStage.COMPUTE, buffer: { type: "storage" } },
|
|
164
|
+
{ binding: 1, visibility: GPUShaderStage.COMPUTE, buffer: { type: "storage" } },
|
|
165
|
+
{ binding: 2, visibility: GPUShaderStage.COMPUTE, buffer: { type: "uniform" } },
|
|
166
|
+
],
|
|
167
|
+
});
|
|
168
|
+
const splatLayout = device.createBindGroupLayout({
|
|
169
|
+
label: "heatmap-splat",
|
|
170
|
+
entries: [
|
|
171
|
+
{ binding: 0, visibility: GPUShaderStage.COMPUTE, buffer: { type: "read-only-storage" } },
|
|
172
|
+
{ binding: 1, visibility: GPUShaderStage.COMPUTE, buffer: { type: "storage" } },
|
|
173
|
+
{ binding: 2, visibility: GPUShaderStage.COMPUTE, buffer: { type: "uniform" } },
|
|
174
|
+
],
|
|
175
|
+
});
|
|
176
|
+
const reduceLayout = device.createBindGroupLayout({
|
|
177
|
+
label: "heatmap-reduce",
|
|
178
|
+
entries: [
|
|
179
|
+
{ binding: 0, visibility: GPUShaderStage.COMPUTE, buffer: { type: "storage" } },
|
|
180
|
+
{ binding: 1, visibility: GPUShaderStage.COMPUTE, buffer: { type: "storage" } },
|
|
181
|
+
{ binding: 2, visibility: GPUShaderStage.COMPUTE, buffer: { type: "uniform" } },
|
|
182
|
+
],
|
|
183
|
+
});
|
|
184
|
+
const colormapLayout = device.createBindGroupLayout({
|
|
185
|
+
label: "heatmap-colormap",
|
|
186
|
+
entries: [
|
|
187
|
+
{ binding: 0, visibility: GPUShaderStage.COMPUTE, buffer: { type: "storage" } },
|
|
188
|
+
{ binding: 1, visibility: GPUShaderStage.COMPUTE, buffer: { type: "storage" } },
|
|
189
|
+
{ binding: 2, visibility: GPUShaderStage.COMPUTE, buffer: { type: "uniform" } },
|
|
190
|
+
{
|
|
191
|
+
binding: 3,
|
|
192
|
+
visibility: GPUShaderStage.COMPUTE,
|
|
193
|
+
texture: { sampleType: "float", viewDimension: "2d" },
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
binding: 4,
|
|
197
|
+
visibility: GPUShaderStage.COMPUTE,
|
|
198
|
+
storageTexture: { access: "write-only", format: "rgba8unorm", viewDimension: "2d" },
|
|
199
|
+
},
|
|
200
|
+
],
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
const clearPipeline = device.createComputePipeline({
|
|
204
|
+
label: "heatmap-clear-pipeline",
|
|
205
|
+
layout: device.createPipelineLayout({ bindGroupLayouts: [clearLayout] }),
|
|
206
|
+
compute: { module: clearModule, entryPoint: "main" },
|
|
207
|
+
});
|
|
208
|
+
const splatPipeline = device.createComputePipeline({
|
|
209
|
+
label: "heatmap-splat-pipeline",
|
|
210
|
+
layout: device.createPipelineLayout({ bindGroupLayouts: [splatLayout] }),
|
|
211
|
+
compute: { module: splatModule, entryPoint: "main" },
|
|
212
|
+
});
|
|
213
|
+
const reducePipeline = device.createComputePipeline({
|
|
214
|
+
label: "heatmap-reduce-pipeline",
|
|
215
|
+
layout: device.createPipelineLayout({ bindGroupLayouts: [reduceLayout] }),
|
|
216
|
+
compute: { module: reduceModule, entryPoint: "main" },
|
|
217
|
+
});
|
|
218
|
+
const colormapPipeline = device.createComputePipeline({
|
|
219
|
+
label: "heatmap-colormap-pipeline",
|
|
220
|
+
layout: device.createPipelineLayout({ bindGroupLayouts: [colormapLayout] }),
|
|
221
|
+
compute: { module: colormapModule, entryPoint: "main" },
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
const clearBindGroup = device.createBindGroup({
|
|
225
|
+
layout: clearLayout,
|
|
226
|
+
entries: [
|
|
227
|
+
{ binding: 0, resource: { buffer: binBuffer } },
|
|
228
|
+
{ binding: 1, resource: { buffer: maxBuffer } },
|
|
229
|
+
{ binding: 2, resource: { buffer: paramsBuffer } },
|
|
230
|
+
],
|
|
231
|
+
});
|
|
232
|
+
const splatBindGroup = device.createBindGroup({
|
|
233
|
+
layout: splatLayout,
|
|
234
|
+
entries: [
|
|
235
|
+
{ binding: 0, resource: { buffer: dataBuffer } },
|
|
236
|
+
{ binding: 1, resource: { buffer: binBuffer } },
|
|
237
|
+
{ binding: 2, resource: { buffer: paramsBuffer } },
|
|
238
|
+
],
|
|
239
|
+
});
|
|
240
|
+
const reduceBindGroup = device.createBindGroup({
|
|
241
|
+
layout: reduceLayout,
|
|
242
|
+
entries: [
|
|
243
|
+
{ binding: 0, resource: { buffer: binBuffer } },
|
|
244
|
+
{ binding: 1, resource: { buffer: maxBuffer } },
|
|
245
|
+
{ binding: 2, resource: { buffer: paramsBuffer } },
|
|
246
|
+
],
|
|
247
|
+
});
|
|
248
|
+
const colormapBindGroup = device.createBindGroup({
|
|
249
|
+
layout: colormapLayout,
|
|
250
|
+
entries: [
|
|
251
|
+
{ binding: 0, resource: { buffer: binBuffer } },
|
|
252
|
+
{ binding: 1, resource: { buffer: maxBuffer } },
|
|
253
|
+
{ binding: 2, resource: { buffer: paramsBuffer } },
|
|
254
|
+
{ binding: 3, resource: lutView },
|
|
255
|
+
{ binding: 4, resource: outputView },
|
|
256
|
+
],
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
// Pre-fill the LUT from the palette.
|
|
260
|
+
writeLut(device, lutTexture, spec.colorMap);
|
|
261
|
+
// Pre-fill the params uniform once; `updateSprite` keeps it in sync if the
|
|
262
|
+
// spec ever mutates (v1 treats spec as immutable, but params buffer still
|
|
263
|
+
// needs a first write).
|
|
264
|
+
writeParams(device, paramsBuffer, spec, this.data.length);
|
|
265
|
+
|
|
266
|
+
const spriteLayer = createLayer({ space: "ui" });
|
|
267
|
+
|
|
268
|
+
this.state = {
|
|
269
|
+
root,
|
|
270
|
+
device,
|
|
271
|
+
dataBuffer,
|
|
272
|
+
dataCapacity: initialCapacity,
|
|
273
|
+
binBuffer,
|
|
274
|
+
maxBuffer,
|
|
275
|
+
paramsBuffer,
|
|
276
|
+
lutTexture,
|
|
277
|
+
lutView,
|
|
278
|
+
outputTexture,
|
|
279
|
+
outputView,
|
|
280
|
+
outputWrapper,
|
|
281
|
+
clearPipeline,
|
|
282
|
+
splatPipeline,
|
|
283
|
+
reducePipeline,
|
|
284
|
+
colormapPipeline,
|
|
285
|
+
splatLayout,
|
|
286
|
+
clearBindGroup,
|
|
287
|
+
splatBindGroup,
|
|
288
|
+
reduceBindGroup,
|
|
289
|
+
colormapBindGroup,
|
|
290
|
+
spriteLayer,
|
|
291
|
+
dataCount: this.data.length,
|
|
292
|
+
};
|
|
293
|
+
return this.state;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
private uploadDataBuffer(state: GpuState): void {
|
|
297
|
+
const spec = this.spec;
|
|
298
|
+
const n = this.data.length;
|
|
299
|
+
state.dataCount = n;
|
|
300
|
+
|
|
301
|
+
if (n > state.dataCapacity) {
|
|
302
|
+
state.dataBuffer.destroy();
|
|
303
|
+
const next = nextPow2(n);
|
|
304
|
+
state.dataBuffer = state.device.createBuffer({
|
|
305
|
+
label: "heatmap-data",
|
|
306
|
+
size: next * 16,
|
|
307
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
|
|
308
|
+
});
|
|
309
|
+
state.dataCapacity = next;
|
|
310
|
+
// Rebuild the splat bind group since the buffer identity changed.
|
|
311
|
+
state.splatBindGroup = state.device.createBindGroup({
|
|
312
|
+
layout: state.splatLayout,
|
|
313
|
+
entries: [
|
|
314
|
+
{ binding: 0, resource: { buffer: state.dataBuffer } },
|
|
315
|
+
{ binding: 1, resource: { buffer: state.binBuffer } },
|
|
316
|
+
{ binding: 2, resource: { buffer: state.paramsBuffer } },
|
|
317
|
+
],
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
if (n > 0) {
|
|
322
|
+
const packed = new Float32Array(n * 4);
|
|
323
|
+
for (let i = 0; i < n; i++) {
|
|
324
|
+
const datum = this.data[i]!;
|
|
325
|
+
packed[i * 4 + 0] = spec.x(datum);
|
|
326
|
+
packed[i * 4 + 1] = spec.y(datum);
|
|
327
|
+
packed[i * 4 + 2] = spec.weight(datum);
|
|
328
|
+
packed[i * 4 + 3] = 0;
|
|
329
|
+
}
|
|
330
|
+
state.device.queue.writeBuffer(state.dataBuffer, 0, packed);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
writeParams(state.device, state.paramsBuffer, spec, n);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
private updateSprite(state: GpuState): void {
|
|
337
|
+
// Clear and re-push the single sprite every frame so the frame rect
|
|
338
|
+
// (caller may move it) is always current.
|
|
339
|
+
const spec = this.spec;
|
|
340
|
+
state.spriteLayer.clear();
|
|
341
|
+
state.spriteLayer.pushSprite({
|
|
342
|
+
pos: { x: spec.frame.x + spec.frame.width * 0.5, y: spec.frame.y + spec.frame.height * 0.5 },
|
|
343
|
+
size: { x: spec.frame.width, y: spec.frame.height },
|
|
344
|
+
texture: state.outputWrapper,
|
|
345
|
+
anchor: { x: 0.5, y: 0.5 },
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
private runPasses(state: GpuState, pass: GPUComputePassEncoder): void {
|
|
350
|
+
const spec = this.spec;
|
|
351
|
+
const cellCount = spec.nx * spec.ny;
|
|
352
|
+
|
|
353
|
+
// Clear: one thread per cell; also resets max to 0.
|
|
354
|
+
pass.setPipeline(state.clearPipeline);
|
|
355
|
+
pass.setBindGroup(0, state.clearBindGroup);
|
|
356
|
+
pass.dispatchWorkgroups(Math.ceil(cellCount / WG_1D));
|
|
357
|
+
|
|
358
|
+
// Splat: one thread per datum.
|
|
359
|
+
if (state.dataCount > 0) {
|
|
360
|
+
pass.setPipeline(state.splatPipeline);
|
|
361
|
+
pass.setBindGroup(0, state.splatBindGroup);
|
|
362
|
+
pass.dispatchWorkgroups(Math.ceil(state.dataCount / WG_1D));
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// Reduce max across all cells. One workgroup, 256 threads, each thread
|
|
366
|
+
// strides through cells and atomicMax's into a single atomic<i32>.
|
|
367
|
+
pass.setPipeline(state.reducePipeline);
|
|
368
|
+
pass.setBindGroup(0, state.reduceBindGroup);
|
|
369
|
+
pass.dispatchWorkgroups(1);
|
|
370
|
+
|
|
371
|
+
// Colormap: one thread per output pixel.
|
|
372
|
+
pass.setPipeline(state.colormapPipeline);
|
|
373
|
+
pass.setBindGroup(0, state.colormapBindGroup);
|
|
374
|
+
pass.dispatchWorkgroups(Math.ceil(spec.nx / WG_2D), Math.ceil(spec.ny / WG_2D));
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// ---------------------------------------------------------------------------
|
|
379
|
+
// GPU helpers
|
|
380
|
+
// ---------------------------------------------------------------------------
|
|
381
|
+
|
|
382
|
+
function writeParams<T>(
|
|
383
|
+
device: GPUDevice,
|
|
384
|
+
buffer: GPUBuffer,
|
|
385
|
+
spec: ResolvedSpec<T>,
|
|
386
|
+
count: number,
|
|
387
|
+
): void {
|
|
388
|
+
// Params layout (48 bytes):
|
|
389
|
+
// bins: vec2u @ 0 (8)
|
|
390
|
+
// count: u32 @ 8 (4)
|
|
391
|
+
// weightScale: f32 @ 12 (4)
|
|
392
|
+
// xDomain: vec2f @ 16 (8)
|
|
393
|
+
// yDomain: vec2f @ 24 (8)
|
|
394
|
+
// cellCount: u32 @ 32 (4)
|
|
395
|
+
// normalize: u32 @ 36 (4) — 0=max, 1=log, 2=sqrt
|
|
396
|
+
// _pad1: vec2f @ 40 (8)
|
|
397
|
+
const bytes = new ArrayBuffer(48);
|
|
398
|
+
const u32 = new Uint32Array(bytes);
|
|
399
|
+
const f32 = new Float32Array(bytes);
|
|
400
|
+
u32[0] = spec.nx;
|
|
401
|
+
u32[1] = spec.ny;
|
|
402
|
+
u32[2] = count;
|
|
403
|
+
f32[3] = spec.weightScale;
|
|
404
|
+
f32[4] = spec.x0;
|
|
405
|
+
f32[5] = spec.x1;
|
|
406
|
+
f32[6] = spec.y0;
|
|
407
|
+
f32[7] = spec.y1;
|
|
408
|
+
u32[8] = spec.nx * spec.ny;
|
|
409
|
+
u32[9] = spec.normalize === "log" ? 1 : spec.normalize === "sqrt" ? 2 : 0;
|
|
410
|
+
device.queue.writeBuffer(buffer, 0, bytes);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
function writeLut(device: GPUDevice, texture: GPUTexture, palette: ContinuousPalette): void {
|
|
414
|
+
const bytes = new Uint8Array(LUT_SIZE * 4);
|
|
415
|
+
for (let i = 0; i < LUT_SIZE; i++) {
|
|
416
|
+
const t = i / (LUT_SIZE - 1);
|
|
417
|
+
const c = palette(t);
|
|
418
|
+
// Premultiplied alpha (matches sprite sampler convention).
|
|
419
|
+
const a = clamp01(c.a);
|
|
420
|
+
bytes[i * 4 + 0] = Math.round(clamp01(c.r) * a * 255);
|
|
421
|
+
bytes[i * 4 + 1] = Math.round(clamp01(c.g) * a * 255);
|
|
422
|
+
bytes[i * 4 + 2] = Math.round(clamp01(c.b) * a * 255);
|
|
423
|
+
bytes[i * 4 + 3] = Math.round(a * 255);
|
|
424
|
+
}
|
|
425
|
+
device.queue.writeTexture({ texture }, bytes, { bytesPerRow: LUT_SIZE * 4, rowsPerImage: 1 }, [
|
|
426
|
+
LUT_SIZE,
|
|
427
|
+
1,
|
|
428
|
+
1,
|
|
429
|
+
]);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// ---------------------------------------------------------------------------
|
|
433
|
+
// WGSL
|
|
434
|
+
// ---------------------------------------------------------------------------
|
|
435
|
+
|
|
436
|
+
const PARAMS_WGSL = /* wgsl */ `
|
|
437
|
+
struct Params {
|
|
438
|
+
bins: vec2u,
|
|
439
|
+
count: u32,
|
|
440
|
+
weightScale: f32,
|
|
441
|
+
xDomain: vec2f,
|
|
442
|
+
yDomain: vec2f,
|
|
443
|
+
cellCount: u32,
|
|
444
|
+
normalize: u32, // 0=max, 1=log, 2=sqrt
|
|
445
|
+
};
|
|
446
|
+
`;
|
|
447
|
+
|
|
448
|
+
const CLEAR_WGSL = /* wgsl */ `${PARAMS_WGSL}
|
|
449
|
+
@group(0) @binding(0) var<storage, read_write> bins: array<atomic<i32>>;
|
|
450
|
+
@group(0) @binding(1) var<storage, read_write> maxCell: array<atomic<i32>, 1>;
|
|
451
|
+
@group(0) @binding(2) var<uniform> params: Params;
|
|
452
|
+
|
|
453
|
+
@compute @workgroup_size(${WG_1D})
|
|
454
|
+
fn main(@builtin(global_invocation_id) gid: vec3u) {
|
|
455
|
+
let i = gid.x;
|
|
456
|
+
if (i >= params.cellCount) {
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
atomicStore(&bins[i], 0);
|
|
460
|
+
if (i == 0u) {
|
|
461
|
+
atomicStore(&maxCell[0], 0);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
`;
|
|
465
|
+
|
|
466
|
+
const SPLAT_WGSL = /* wgsl */ `${PARAMS_WGSL}
|
|
467
|
+
@group(0) @binding(0) var<storage, read> data: array<vec4f>;
|
|
468
|
+
@group(0) @binding(1) var<storage, read_write> bins: array<atomic<i32>>;
|
|
469
|
+
@group(0) @binding(2) var<uniform> params: Params;
|
|
470
|
+
|
|
471
|
+
@compute @workgroup_size(${WG_1D})
|
|
472
|
+
fn main(@builtin(global_invocation_id) gid: vec3u) {
|
|
473
|
+
let i = gid.x;
|
|
474
|
+
if (i >= params.count) {
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
let datum = data[i];
|
|
478
|
+
let xv = datum.x;
|
|
479
|
+
let yv = datum.y;
|
|
480
|
+
let w = datum.z;
|
|
481
|
+
|
|
482
|
+
let dx = params.xDomain.y - params.xDomain.x;
|
|
483
|
+
let dy = params.yDomain.y - params.yDomain.x;
|
|
484
|
+
if (dx == 0.0 || dy == 0.0) {
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
let tx = (xv - params.xDomain.x) / dx;
|
|
488
|
+
let ty = (yv - params.yDomain.x) / dy;
|
|
489
|
+
if (tx < 0.0 || tx >= 1.0 || ty < 0.0 || ty >= 1.0) {
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
let nx = params.bins.x;
|
|
493
|
+
let ny = params.bins.y;
|
|
494
|
+
let ix = min(nx - 1u, u32(tx * f32(nx)));
|
|
495
|
+
// Flip iy: row 0 represents the *top* of the frame (highest data-y).
|
|
496
|
+
let iy = ny - 1u - min(ny - 1u, u32(ty * f32(ny)));
|
|
497
|
+
let idx = iy * nx + ix;
|
|
498
|
+
let quant = i32(round(w * params.weightScale));
|
|
499
|
+
atomicAdd(&bins[idx], quant);
|
|
500
|
+
}
|
|
501
|
+
`;
|
|
502
|
+
|
|
503
|
+
const REDUCE_WGSL = /* wgsl */ `${PARAMS_WGSL}
|
|
504
|
+
@group(0) @binding(0) var<storage, read_write> bins: array<atomic<i32>>;
|
|
505
|
+
@group(0) @binding(1) var<storage, read_write> maxCell: array<atomic<i32>, 1>;
|
|
506
|
+
@group(0) @binding(2) var<uniform> params: Params;
|
|
507
|
+
|
|
508
|
+
@compute @workgroup_size(${WG_1D})
|
|
509
|
+
fn main(@builtin(local_invocation_id) lid: vec3u) {
|
|
510
|
+
let threads = ${WG_1D}u;
|
|
511
|
+
let total = params.cellCount;
|
|
512
|
+
var localMax: i32 = 0;
|
|
513
|
+
var i = lid.x;
|
|
514
|
+
loop {
|
|
515
|
+
if (i >= total) {
|
|
516
|
+
break;
|
|
517
|
+
}
|
|
518
|
+
let v = atomicLoad(&bins[i]);
|
|
519
|
+
if (v > localMax) {
|
|
520
|
+
localMax = v;
|
|
521
|
+
}
|
|
522
|
+
i = i + threads;
|
|
523
|
+
}
|
|
524
|
+
atomicMax(&maxCell[0], localMax);
|
|
525
|
+
}
|
|
526
|
+
`;
|
|
527
|
+
|
|
528
|
+
const COLORMAP_WGSL = /* wgsl */ `${PARAMS_WGSL}
|
|
529
|
+
@group(0) @binding(0) var<storage, read_write> bins: array<atomic<i32>>;
|
|
530
|
+
@group(0) @binding(1) var<storage, read_write> maxCell: array<atomic<i32>, 1>;
|
|
531
|
+
@group(0) @binding(2) var<uniform> params: Params;
|
|
532
|
+
@group(0) @binding(3) var lut: texture_2d<f32>;
|
|
533
|
+
@group(0) @binding(4) var outputTex: texture_storage_2d<rgba8unorm, write>;
|
|
534
|
+
|
|
535
|
+
@compute @workgroup_size(${WG_2D}, ${WG_2D})
|
|
536
|
+
fn main(@builtin(global_invocation_id) gid: vec3u) {
|
|
537
|
+
let nx = params.bins.x;
|
|
538
|
+
let ny = params.bins.y;
|
|
539
|
+
if (gid.x >= nx || gid.y >= ny) {
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
let idx = gid.y * nx + gid.x;
|
|
543
|
+
let raw = atomicLoad(&bins[idx]);
|
|
544
|
+
let maxRaw = atomicLoad(&maxCell[0]);
|
|
545
|
+
var t: f32 = 0.0;
|
|
546
|
+
if (maxRaw > 0) {
|
|
547
|
+
let fraw = f32(raw);
|
|
548
|
+
let fmax = f32(maxRaw);
|
|
549
|
+
if (params.normalize == 1u) {
|
|
550
|
+
// log: log(1+v) / log(1+max)
|
|
551
|
+
t = clamp(log(1.0 + fraw) / log(1.0 + fmax), 0.0, 1.0);
|
|
552
|
+
} else if (params.normalize == 2u) {
|
|
553
|
+
// sqrt
|
|
554
|
+
t = clamp(sqrt(fraw / fmax), 0.0, 1.0);
|
|
555
|
+
} else {
|
|
556
|
+
// max (default)
|
|
557
|
+
t = clamp(fraw / fmax, 0.0, 1.0);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
let lutWidth = ${LUT_SIZE};
|
|
561
|
+
let u = i32(round(t * f32(lutWidth - 1)));
|
|
562
|
+
let color = textureLoad(lut, vec2i(u, 0), 0);
|
|
563
|
+
textureStore(outputTex, vec2i(i32(gid.x), i32(gid.y)), color);
|
|
564
|
+
}
|
|
565
|
+
`;
|