velo-plot 3.0.0 → 4.0.0-alpha.1
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/README.md +22 -15
- package/dist/ChartCore-KYG2HTcK.js +7474 -0
- package/dist/ChartCore-KYG2HTcK.js.map +1 -0
- package/dist/GpuChartRenderer-Dxc0hrQA.js +75 -0
- package/dist/GpuChartRenderer-Dxc0hrQA.js.map +1 -0
- package/dist/{index.core-BmZe2jRQ.js → PluginRegistry-CDf_jcnp.js} +19 -33
- package/dist/PluginRegistry-CDf_jcnp.js.map +1 -0
- package/dist/SVGDocumentBuilder-C-uLkcFW.js +83 -0
- package/dist/SVGDocumentBuilder-C-uLkcFW.js.map +1 -0
- package/dist/angular-hooks.js +1 -1
- package/dist/angular.js +8 -8
- package/dist/{axisFormat-CzumxpCL.js → axisFormat-Sz4WIJfQ.js} +2 -2
- package/dist/{axisFormat-CzumxpCL.js.map → axisFormat-Sz4WIJfQ.js.map} +1 -1
- package/dist/{chartSyncBridge-DbWrwtyP.js → chartSyncBridge-C1uRy0ZS.js} +12 -11
- package/dist/{chartSyncBridge-DbWrwtyP.js.map → chartSyncBridge-C1uRy0ZS.js.map} +1 -1
- package/dist/core/ChartStatistics.d.ts +1 -1
- package/dist/core/chart/ChartCore.d.ts +19 -20
- package/dist/core/chart/ChartFeatureHooks.d.ts +7 -0
- package/dist/core/chart/ChartRenderLoop.d.ts +5 -0
- package/dist/core/chart/ChartRenderer.d.ts +9 -0
- package/dist/core/chart/ChartSetup.d.ts +1 -0
- package/dist/core/chart/chartExportPatch.d.ts +2 -0
- package/dist/core/chart/exporter/SVGExporter.d.ts +9 -7
- package/dist/core/chart/exporter/svg/SVGDocumentBuilder.d.ts +27 -0
- package/dist/core/chart/exporter/svg/SVGExportContext.d.ts +147 -0
- package/dist/core/chart/exporter/svg/SVGOrchestrator.d.ts +3 -0
- package/dist/core/chart/exporter/svg/SVGThemeAdapter.d.ts +22 -0
- package/dist/core/chart/exporter/svg/__tests__/testFixtures.d.ts +46 -0
- package/dist/core/chart/exporter/svg/__tests__/visualTestUtils.d.ts +12 -0
- package/dist/core/chart/exporter/svg/overlay/axes.d.ts +4 -0
- package/dist/core/chart/exporter/svg/overlay/border.d.ts +4 -0
- package/dist/core/chart/exporter/svg/overlay/errorBars.d.ts +7 -0
- package/dist/core/chart/exporter/svg/overlay/grid.d.ts +4 -0
- package/dist/core/chart/exporter/svg/overlay/index.d.ts +12 -0
- package/dist/core/chart/exporter/svg/overlay/legend.d.ts +4 -0
- package/dist/core/chart/exporter/svg/overlay/pluginCharts.d.ts +6 -0
- package/dist/core/chart/exporter/svg/overlay/polarGrid.d.ts +4 -0
- package/dist/core/chart/exporter/svg/overlay/specialChart.d.ts +15 -0
- package/dist/core/chart/exporter/svg/overlay/title.d.ts +4 -0
- package/dist/core/chart/exporter/svg/plugins/annotations.d.ts +5 -0
- package/dist/core/chart/exporter/svg/plugins/brokenAxis.d.ts +20 -0
- package/dist/core/chart/exporter/svg/plugins/latex.d.ts +9 -0
- package/dist/core/chart/exporter/svg/plugins/register.d.ts +8 -0
- package/dist/core/chart/exporter/svg/plugins/regression.d.ts +33 -0
- package/dist/core/chart/exporter/svg/plugins/roi.d.ts +12 -0
- package/dist/core/chart/exporter/svg/plugins/tradeMarkers.d.ts +3 -0
- package/dist/core/chart/exporter/svg/plugins/types.d.ts +28 -0
- package/dist/core/chart/exporter/svg/plugins/watermark.d.ts +13 -0
- package/dist/core/chart/exporter/svg/series/band.d.ts +6 -0
- package/dist/core/chart/exporter/svg/series/bar.d.ts +6 -0
- package/dist/core/chart/exporter/svg/series/boxplot.d.ts +6 -0
- package/dist/core/chart/exporter/svg/series/candlestick.d.ts +6 -0
- package/dist/core/chart/exporter/svg/series/finitePoints.d.ts +6 -0
- package/dist/core/chart/exporter/svg/series/gauge.d.ts +5 -0
- package/dist/core/chart/exporter/svg/series/heatmap.d.ts +6 -0
- package/dist/core/chart/exporter/svg/series/indicator.d.ts +6 -0
- package/dist/core/chart/exporter/svg/series/line.d.ts +12 -0
- package/dist/core/chart/exporter/svg/series/polar.d.ts +6 -0
- package/dist/core/chart/exporter/svg/series/radar.d.ts +5 -0
- package/dist/core/chart/exporter/svg/series/registry.d.ts +10 -0
- package/dist/core/chart/exporter/svg/series/sankey.d.ts +5 -0
- package/dist/core/chart/exporter/svg/series/scatter.d.ts +1 -0
- package/dist/core/chart/exporter/svg/series/ternary.d.ts +7 -0
- package/dist/core/chart/exporter/svg/series/waterfall.d.ts +6 -0
- package/dist/core/chart/exporter/svg/seriesAtTimestamp.d.ts +4 -0
- package/dist/core/chart/exporter/svg/seriesUtils.d.ts +3 -0
- package/dist/core/chart/exporter/svg/symbols.d.ts +5 -0
- package/dist/core/chart/exporter/svg/tickUtils.d.ts +9 -0
- package/dist/core/chart/mountSVGString.d.ts +5 -0
- package/dist/core/chart/overlaySeriesRegistry.d.ts +7 -0
- package/dist/core/chart/series/SeriesActions.d.ts +1 -0
- package/dist/core/chart/series/SeriesBufferLite.d.ts +13 -0
- package/dist/core/chart/series/seriesOptionsRegistry.d.ts +8 -0
- package/dist/core/chart/series/seriesTypeRegistry.d.ts +5 -0
- package/dist/core/chart/types.d.ts +2 -2
- package/dist/core/stacked/StackSVGComposer.d.ts +14 -0
- package/dist/core/stacked/index.d.ts +3 -2
- package/dist/core/stacked/types.d.ts +12 -4
- package/dist/createStackedChart-DjA3aJAu.js +2761 -0
- package/dist/createStackedChart-DjA3aJAu.js.map +1 -0
- package/dist/{hooks-CYPCxoZx.js → hooks-4wQH27r9.js} +7 -7
- package/dist/{hooks-CYPCxoZx.js.map → hooks-4wQH27r9.js.map} +1 -1
- package/dist/{index-qunX30Xu.js → index-B7PTX94B.js} +2133 -2133
- package/dist/index-B7PTX94B.js.map +1 -0
- package/dist/{index-BMiZoKmm.js → index-BgrvqltV.js} +67 -67
- package/dist/index-BgrvqltV.js.map +1 -0
- package/dist/index-Bj-xyXY6.js +582 -0
- package/dist/index-Bj-xyXY6.js.map +1 -0
- package/dist/index-qhscKTDy.js.map +1 -1
- package/dist/index.core.d.ts +8 -6
- package/dist/index.d.ts +1 -14
- package/dist/plugins/3d.js +3 -1
- package/dist/plugins/3d.js.map +1 -1
- package/dist/plugins/PluginManager.d.ts +5 -0
- package/dist/plugins/analysis.js.map +1 -1
- package/dist/plugins/annotations.js +8 -7
- package/dist/plugins/annotations.js.map +1 -1
- package/dist/plugins/broken-axis.js +198 -152
- package/dist/plugins/broken-axis.js.map +1 -1
- package/dist/plugins/createPlugin.d.ts +8 -0
- package/dist/plugins/debug.js +32 -32
- package/dist/plugins/debug.js.map +1 -1
- package/dist/plugins/forecasting.js +273 -258
- package/dist/plugins/forecasting.js.map +1 -1
- package/dist/plugins/gpu.js +2033 -209
- package/dist/plugins/gpu.js.map +1 -1
- package/dist/plugins/i18n.js +286 -23
- package/dist/plugins/i18n.js.map +1 -1
- package/dist/plugins/latex.js +766 -207
- package/dist/plugins/latex.js.map +1 -1
- package/dist/plugins/loading/index.d.ts +1 -2
- package/dist/plugins/loading.js +3 -4
- package/dist/plugins/ml-integration.js +107 -93
- package/dist/plugins/ml-integration.js.map +1 -1
- package/dist/plugins/offscreen.js +11 -10
- package/dist/plugins/offscreen.js.map +1 -1
- package/dist/plugins/radar/types.d.ts +11 -0
- package/dist/plugins/radar.js +64 -53
- package/dist/plugins/radar.js.map +1 -1
- package/dist/plugins/regression.js +254 -256
- package/dist/plugins/regression.js.map +1 -1
- package/dist/plugins/roi/index.d.ts +2 -0
- package/dist/plugins/roi.js +184 -145
- package/dist/plugins/roi.js.map +1 -1
- package/dist/plugins/snapshot.js +20 -15
- package/dist/plugins/snapshot.js.map +1 -1
- package/dist/plugins/streaming.js +1 -1
- package/dist/plugins/tools.js +1 -1
- package/dist/plugins/types.d.ts +4 -0
- package/dist/react.js +55 -58
- package/dist/react.js.map +1 -1
- package/dist/regression-HNdyk1Dm.js +28 -0
- package/dist/regression-HNdyk1Dm.js.map +1 -0
- package/dist/renderer/ChartSeriesRenderer.d.ts +3 -2
- package/dist/renderer/NativeWebGLRenderer.d.ts +2 -1
- package/dist/renderer/SVGChartRenderer.d.ts +15 -0
- package/dist/renderer/SankeyRenderer.d.ts +1 -0
- package/dist/renderer/native/NativeWebGLRenderer.d.ts +5 -1
- package/dist/renderer/native/draw.d.ts +2 -47
- package/dist/renderer/native/drawCore.d.ts +27 -0
- package/dist/renderer/native/drawExtended.d.ts +29 -0
- package/dist/renderer/native/frameRenderExtended.d.ts +1 -0
- package/dist/renderer/native/frameRenderRegistry.d.ts +27 -0
- package/dist/renderer/native/programFactoryCore.d.ts +6 -0
- package/dist/renderer/native/programFactoryExtended.d.ts +4 -0
- package/dist/renderer/native/utils.d.ts +3 -29
- package/dist/renderer/native/utilsCore.d.ts +14 -0
- package/dist/renderer/native/utilsExtended.d.ts +17 -0
- package/dist/renderer/overlaySeriesExtended.d.ts +1 -0
- package/dist/renderer/registerExtendedSeries.d.ts +1 -0
- package/dist/renderer/registerSVG.d.ts +5 -0
- package/dist/renderer/registerWebGPU.d.ts +2 -0
- package/dist/renderer/sankeyLayout.d.ts +29 -0
- package/dist/renderer/seriesBufferExtended.d.ts +4 -0
- package/dist/scientific/index.d.ts +1 -5
- package/dist/scientific/registerScientific.d.ts +1 -0
- package/dist/scientific.js +41 -41
- package/dist/solid.js +56 -59
- package/dist/solid.js.map +1 -1
- package/dist/svelte.js +26 -29
- package/dist/svelte.js.map +1 -1
- package/dist/tickUtils-B_X-ha13.js +41 -0
- package/dist/tickUtils-B_X-ha13.js.map +1 -0
- package/dist/trading/index.d.ts +1 -4
- package/dist/trading/registerTrading.d.ts +3 -0
- package/dist/trading.js +42 -42
- package/dist/types.d.ts +5 -4
- package/dist/velo-plot.full.js +1951 -1275
- package/dist/velo-plot.full.js.map +1 -1
- package/dist/velo-plot.js +33 -28
- package/dist/velo-plot.js.map +1 -1
- package/dist/vue/VeloPlot.vue.d.ts +1 -1
- package/dist/vue/index.d.ts +33 -33
- package/dist/vue.js +17 -20
- package/dist/vue.js.map +1 -1
- package/package.json +14 -1
- package/dist/ChartCore-BpzRvN4Y.js +0 -8467
- package/dist/ChartCore-BpzRvN4Y.js.map +0 -1
- package/dist/createStackedChart-Dm-YqUT7.js +0 -756
- package/dist/createStackedChart-Dm-YqUT7.js.map +0 -1
- package/dist/gpuRenderer-BpaMYz-k.js +0 -1848
- package/dist/gpuRenderer-BpaMYz-k.js.map +0 -1
- package/dist/index-BMiZoKmm.js.map +0 -1
- package/dist/index-qunX30Xu.js.map +0 -1
- package/dist/index-x4stH3jD.js +0 -282
- package/dist/index-x4stH3jD.js.map +0 -1
- package/dist/index.core-BmZe2jRQ.js.map +0 -1
- package/dist/mockDatafeed-DHIDPcpX.js +0 -515
- package/dist/mockDatafeed-DHIDPcpX.js.map +0 -1
- package/dist/symbols-DT0eLlpW.js +0 -568
- package/dist/symbols-DT0eLlpW.js.map +0 -1
package/dist/plugins/gpu.js
CHANGED
|
@@ -1,10 +1,1836 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
var L = Object.defineProperty;
|
|
2
|
+
var F = (s, e, t) => e in s ? L(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t;
|
|
3
|
+
var c = (s, e, t) => F(s, typeof e != "symbol" ? e + "" : e, t);
|
|
4
|
+
const I = `
|
|
5
|
+
struct VSOut {
|
|
6
|
+
@builtin(position) pos: vec4<f32>,
|
|
7
|
+
@location(0) color: vec4<f32>,
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
@vertex
|
|
11
|
+
fn vs_main(
|
|
12
|
+
@location(0) position: vec2<f32>,
|
|
13
|
+
@location(1) color: vec4<f32>
|
|
14
|
+
) -> VSOut {
|
|
15
|
+
var out: VSOut;
|
|
16
|
+
out.pos = vec4<f32>(position, 0.0, 1.0);
|
|
17
|
+
out.color = color;
|
|
18
|
+
return out;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
@fragment
|
|
22
|
+
fn fs_main(in: VSOut) -> @location(0) vec4<f32> {
|
|
23
|
+
return in.color;
|
|
24
|
+
}
|
|
25
|
+
`;
|
|
26
|
+
function O(s, e) {
|
|
27
|
+
const t = s.createShaderModule({ code: I }), i = 6 * 4;
|
|
28
|
+
return { pipeline: s.createRenderPipeline({
|
|
29
|
+
layout: "auto",
|
|
30
|
+
vertex: {
|
|
31
|
+
module: t,
|
|
32
|
+
entryPoint: "vs_main",
|
|
33
|
+
buffers: [
|
|
34
|
+
{
|
|
35
|
+
arrayStride: i,
|
|
36
|
+
attributes: [
|
|
37
|
+
{ shaderLocation: 0, offset: 0, format: "float32x2" },
|
|
38
|
+
{ shaderLocation: 1, offset: 2 * 4, format: "float32x4" }
|
|
39
|
+
]
|
|
40
|
+
}
|
|
41
|
+
]
|
|
42
|
+
},
|
|
43
|
+
fragment: {
|
|
44
|
+
module: t,
|
|
45
|
+
entryPoint: "fs_main",
|
|
46
|
+
targets: [{ format: e }]
|
|
47
|
+
},
|
|
48
|
+
primitive: {
|
|
49
|
+
topology: "triangle-list",
|
|
50
|
+
cullMode: "none"
|
|
51
|
+
}
|
|
52
|
+
}), vertexStride: i };
|
|
53
|
+
}
|
|
54
|
+
const D = `
|
|
55
|
+
// Uniform buffer for transforms and color
|
|
56
|
+
struct Uniforms {
|
|
57
|
+
scale: vec2<f32>,
|
|
58
|
+
translate: vec2<f32>,
|
|
59
|
+
color: vec4<f32>,
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
@group(0) @binding(0)
|
|
63
|
+
var<uniform> uniforms: Uniforms;
|
|
64
|
+
|
|
65
|
+
struct VSInput {
|
|
66
|
+
@location(0) position: vec2<f32>,
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
struct VSOutput {
|
|
70
|
+
@builtin(position) pos: vec4<f32>,
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
@vertex
|
|
74
|
+
fn vs_main(in: VSInput) -> VSOutput {
|
|
75
|
+
var out: VSOutput;
|
|
76
|
+
let transformed = in.position * uniforms.scale + uniforms.translate;
|
|
77
|
+
out.pos = vec4<f32>(transformed, 0.0, 1.0);
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
@fragment
|
|
82
|
+
fn fs_main(in: VSOutput) -> @location(0) vec4<f32> {
|
|
83
|
+
return uniforms.color;
|
|
84
|
+
}
|
|
85
|
+
`, S = 2 * 4, z = 32;
|
|
86
|
+
function V(s, e) {
|
|
87
|
+
const t = s.createShaderModule({ code: D }), i = globalThis.GPUShaderStage, r = globalThis.GPUBufferUsage, a = s.createBindGroupLayout({
|
|
88
|
+
entries: [
|
|
89
|
+
{
|
|
90
|
+
binding: 0,
|
|
91
|
+
visibility: i.VERTEX | i.FRAGMENT,
|
|
92
|
+
buffer: { type: "uniform" }
|
|
93
|
+
}
|
|
94
|
+
]
|
|
95
|
+
}), n = s.createBuffer({
|
|
96
|
+
size: z,
|
|
97
|
+
usage: r.UNIFORM | r.COPY_DST
|
|
98
|
+
}), o = s.createBindGroup({
|
|
99
|
+
layout: a,
|
|
100
|
+
entries: [
|
|
101
|
+
{
|
|
102
|
+
binding: 0,
|
|
103
|
+
resource: { buffer: n }
|
|
104
|
+
}
|
|
105
|
+
]
|
|
106
|
+
}), u = s.createPipelineLayout({
|
|
107
|
+
bindGroupLayouts: [a]
|
|
108
|
+
});
|
|
109
|
+
return {
|
|
110
|
+
pipeline: s.createRenderPipeline({
|
|
111
|
+
layout: u,
|
|
112
|
+
vertex: {
|
|
113
|
+
module: t,
|
|
114
|
+
entryPoint: "vs_main",
|
|
115
|
+
buffers: [
|
|
116
|
+
{
|
|
117
|
+
arrayStride: S,
|
|
118
|
+
attributes: [
|
|
119
|
+
{ shaderLocation: 0, offset: 0, format: "float32x2" }
|
|
120
|
+
]
|
|
121
|
+
}
|
|
122
|
+
]
|
|
123
|
+
},
|
|
124
|
+
fragment: {
|
|
125
|
+
module: t,
|
|
126
|
+
entryPoint: "fs_main",
|
|
127
|
+
targets: [
|
|
128
|
+
{
|
|
129
|
+
format: e,
|
|
130
|
+
blend: {
|
|
131
|
+
color: {
|
|
132
|
+
srcFactor: "src-alpha",
|
|
133
|
+
dstFactor: "one-minus-src-alpha",
|
|
134
|
+
operation: "add"
|
|
135
|
+
},
|
|
136
|
+
alpha: {
|
|
137
|
+
srcFactor: "one",
|
|
138
|
+
dstFactor: "one-minus-src-alpha",
|
|
139
|
+
operation: "add"
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
]
|
|
144
|
+
},
|
|
145
|
+
primitive: {
|
|
146
|
+
topology: "line-strip",
|
|
147
|
+
stripIndexFormat: void 0
|
|
148
|
+
}
|
|
149
|
+
}),
|
|
150
|
+
bindGroupLayout: a,
|
|
151
|
+
uniformBuffer: n,
|
|
152
|
+
bindGroup: o,
|
|
153
|
+
vertexStride: S
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
function k(s, e, t) {
|
|
157
|
+
const i = new Float32Array([
|
|
158
|
+
t.scale[0],
|
|
159
|
+
t.scale[1],
|
|
160
|
+
t.translate[0],
|
|
161
|
+
t.translate[1],
|
|
162
|
+
t.color[0],
|
|
163
|
+
t.color[1],
|
|
164
|
+
t.color[2],
|
|
165
|
+
t.color[3]
|
|
166
|
+
]);
|
|
167
|
+
s.queue.writeBuffer(e, 0, i);
|
|
168
|
+
}
|
|
169
|
+
const W = `
|
|
170
|
+
// Uniform buffer for transforms, color, point properties
|
|
171
|
+
struct Uniforms {
|
|
172
|
+
scale: vec2<f32>,
|
|
173
|
+
translate: vec2<f32>,
|
|
174
|
+
color: vec4<f32>,
|
|
175
|
+
pointSize: f32,
|
|
176
|
+
symbol: i32,
|
|
177
|
+
viewport: vec2<f32>, // viewport dimensions for proper sizing
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
@group(0) @binding(0)
|
|
181
|
+
var<uniform> uniforms: Uniforms;
|
|
182
|
+
|
|
183
|
+
struct VSInput {
|
|
184
|
+
@location(0) position: vec2<f32>, // point center position
|
|
185
|
+
@location(1) quadOffset: vec2<f32>, // quad vertex offset (-1 to 1)
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
struct VSOutput {
|
|
189
|
+
@builtin(position) pos: vec4<f32>,
|
|
190
|
+
@location(0) uv: vec2<f32>, // -0.5 to 0.5 for SDF
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
@vertex
|
|
194
|
+
fn vs_main(in: VSInput) -> VSOutput {
|
|
195
|
+
var out: VSOutput;
|
|
196
|
+
|
|
197
|
+
// Transform point position to NDC
|
|
198
|
+
let transformed = in.position * uniforms.scale + uniforms.translate;
|
|
199
|
+
|
|
200
|
+
// Calculate point size in NDC
|
|
201
|
+
let pointSizeNDC = vec2<f32>(
|
|
202
|
+
uniforms.pointSize / uniforms.viewport.x * 2.0,
|
|
203
|
+
uniforms.pointSize / uniforms.viewport.y * 2.0
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
// Apply quad offset
|
|
207
|
+
let offset = in.quadOffset * pointSizeNDC * 0.5;
|
|
208
|
+
|
|
209
|
+
out.pos = vec4<f32>(transformed + offset, 0.0, 1.0);
|
|
210
|
+
out.uv = in.quadOffset * 0.5; // -0.5 to 0.5
|
|
211
|
+
|
|
212
|
+
return out;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// SDF functions
|
|
216
|
+
fn sdCircle(p: vec2<f32>, r: f32) -> f32 {
|
|
217
|
+
return length(p) - r;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
fn sdBox(p: vec2<f32>, b: vec2<f32>) -> f32 {
|
|
221
|
+
let d = abs(p) - b;
|
|
222
|
+
return length(max(d, vec2<f32>(0.0))) + min(max(d.x, d.y), 0.0);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
fn sdDiamond(p: vec2<f32>, r: f32) -> f32 {
|
|
226
|
+
return (abs(p.x) + abs(p.y)) - r;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
fn sdTriangle(p_in: vec2<f32>, r: f32) -> f32 {
|
|
230
|
+
let k = sqrt(3.0);
|
|
231
|
+
var p = p_in;
|
|
232
|
+
p.x = abs(p.x) - r;
|
|
233
|
+
p.y = p.y + r / k;
|
|
234
|
+
if (p.x + k * p.y > 0.0) {
|
|
235
|
+
p = vec2<f32>(p.x - k * p.y, -k * p.x - p.y) / 2.0;
|
|
236
|
+
}
|
|
237
|
+
p.x = p.x - clamp(p.x, -2.0 * r, 0.0);
|
|
238
|
+
return -length(p) * sign(p.y);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
fn sdCross(p: vec2<f32>, r: f32, thickness: f32) -> f32 {
|
|
242
|
+
let d = abs(p);
|
|
243
|
+
let s1 = sdBox(d, vec2<f32>(r, thickness));
|
|
244
|
+
let s2 = sdBox(d, vec2<f32>(thickness, r));
|
|
245
|
+
return min(s1, s2);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
fn sdX(p: vec2<f32>, r: f32, thickness: f32) -> f32 {
|
|
249
|
+
let c = cos(0.785398);
|
|
250
|
+
let s = sin(0.785398);
|
|
251
|
+
let m = mat2x2<f32>(c, -s, s, c);
|
|
252
|
+
return sdCross(m * p, r, thickness);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
fn sdStar(p_in: vec2<f32>, r: f32, rf: f32) -> f32 {
|
|
256
|
+
let k1 = vec2<f32>(0.80901699, -0.58778525);
|
|
257
|
+
let k2 = vec2<f32>(-k1.x, k1.y);
|
|
258
|
+
var p = p_in;
|
|
259
|
+
p.x = abs(p.x);
|
|
260
|
+
p = p - 2.0 * max(dot(k1, p), 0.0) * k1;
|
|
261
|
+
p = p - 2.0 * max(dot(k2, p), 0.0) * k2;
|
|
262
|
+
p.x = abs(p.x);
|
|
263
|
+
p.y = p.y - r;
|
|
264
|
+
let ba = rf * vec2<f32>(-k1.y, k1.x) - vec2<f32>(0.0, 1.0);
|
|
265
|
+
let h = clamp(dot(p, ba) / dot(ba, ba), 0.0, r);
|
|
266
|
+
return length(p - ba * h) * sign(p.y * ba.x - p.x * ba.y);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
@fragment
|
|
270
|
+
fn fs_main(in: VSOutput) -> @location(0) vec4<f32> {
|
|
271
|
+
var d: f32 = 0.0;
|
|
272
|
+
|
|
273
|
+
if (uniforms.symbol == 0) {
|
|
274
|
+
d = sdCircle(in.uv, 0.45);
|
|
275
|
+
} else if (uniforms.symbol == 1) {
|
|
276
|
+
d = sdBox(in.uv, vec2<f32>(0.35));
|
|
277
|
+
} else if (uniforms.symbol == 2) {
|
|
278
|
+
d = sdDiamond(in.uv, 0.45);
|
|
279
|
+
} else if (uniforms.symbol == 3) {
|
|
280
|
+
d = sdTriangle(vec2<f32>(in.uv.x, in.uv.y + 0.1), 0.4);
|
|
281
|
+
} else if (uniforms.symbol == 4) {
|
|
282
|
+
d = sdTriangle(vec2<f32>(in.uv.x, -in.uv.y + 0.1), 0.4);
|
|
283
|
+
} else if (uniforms.symbol == 5) {
|
|
284
|
+
d = sdCross(in.uv, 0.45, 0.15);
|
|
285
|
+
} else if (uniforms.symbol == 6) {
|
|
286
|
+
d = sdX(in.uv, 0.45, 0.15);
|
|
287
|
+
} else if (uniforms.symbol == 7) {
|
|
288
|
+
d = sdStar(in.uv, 0.45, 0.4);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if (d > 0.02) {
|
|
292
|
+
discard;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
let alpha = 1.0 - smoothstep(0.0, 0.02, d);
|
|
296
|
+
return vec4<f32>(uniforms.color.rgb, uniforms.color.a * alpha);
|
|
297
|
+
}
|
|
298
|
+
`, A = 2 * 4, N = 48, C = new Float32Array([
|
|
299
|
+
-1,
|
|
300
|
+
-1,
|
|
301
|
+
// bottom-left
|
|
302
|
+
1,
|
|
303
|
+
-1,
|
|
304
|
+
// bottom-right
|
|
305
|
+
-1,
|
|
306
|
+
1,
|
|
307
|
+
// top-left
|
|
308
|
+
-1,
|
|
309
|
+
1,
|
|
310
|
+
// top-left
|
|
311
|
+
1,
|
|
312
|
+
-1,
|
|
313
|
+
// bottom-right
|
|
314
|
+
1,
|
|
315
|
+
1
|
|
316
|
+
// top-right
|
|
317
|
+
]);
|
|
318
|
+
function q(s, e) {
|
|
319
|
+
const t = s.createShaderModule({ code: W }), i = globalThis.GPUShaderStage, r = globalThis.GPUBufferUsage, a = s.createBindGroupLayout({
|
|
320
|
+
entries: [
|
|
321
|
+
{
|
|
322
|
+
binding: 0,
|
|
323
|
+
visibility: i.VERTEX | i.FRAGMENT,
|
|
324
|
+
buffer: { type: "uniform" }
|
|
325
|
+
}
|
|
326
|
+
]
|
|
327
|
+
}), n = s.createBuffer({
|
|
328
|
+
size: N,
|
|
329
|
+
usage: r.UNIFORM | r.COPY_DST
|
|
330
|
+
}), o = s.createBuffer({
|
|
331
|
+
size: C.byteLength,
|
|
332
|
+
usage: r.VERTEX | r.COPY_DST,
|
|
333
|
+
mappedAtCreation: !0
|
|
334
|
+
});
|
|
335
|
+
new Float32Array(o.getMappedRange()).set(C), o.unmap();
|
|
336
|
+
const u = s.createBindGroup({
|
|
337
|
+
layout: a,
|
|
338
|
+
entries: [
|
|
339
|
+
{
|
|
340
|
+
binding: 0,
|
|
341
|
+
resource: { buffer: n }
|
|
342
|
+
}
|
|
343
|
+
]
|
|
344
|
+
}), l = s.createPipelineLayout({
|
|
345
|
+
bindGroupLayouts: [a]
|
|
346
|
+
});
|
|
347
|
+
return {
|
|
348
|
+
pipeline: s.createRenderPipeline({
|
|
349
|
+
layout: l,
|
|
350
|
+
vertex: {
|
|
351
|
+
module: t,
|
|
352
|
+
entryPoint: "vs_main",
|
|
353
|
+
buffers: [
|
|
354
|
+
// Point positions (instanced)
|
|
355
|
+
{
|
|
356
|
+
arrayStride: A,
|
|
357
|
+
stepMode: "instance",
|
|
358
|
+
attributes: [
|
|
359
|
+
{ shaderLocation: 0, offset: 0, format: "float32x2" }
|
|
360
|
+
]
|
|
361
|
+
},
|
|
362
|
+
// Quad vertex offsets (per-vertex)
|
|
363
|
+
{
|
|
364
|
+
arrayStride: 8,
|
|
365
|
+
// 2 floats
|
|
366
|
+
stepMode: "vertex",
|
|
367
|
+
attributes: [
|
|
368
|
+
{ shaderLocation: 1, offset: 0, format: "float32x2" }
|
|
369
|
+
]
|
|
370
|
+
}
|
|
371
|
+
]
|
|
372
|
+
},
|
|
373
|
+
fragment: {
|
|
374
|
+
module: t,
|
|
375
|
+
entryPoint: "fs_main",
|
|
376
|
+
targets: [
|
|
377
|
+
{
|
|
378
|
+
format: e,
|
|
379
|
+
blend: {
|
|
380
|
+
color: {
|
|
381
|
+
srcFactor: "src-alpha",
|
|
382
|
+
dstFactor: "one-minus-src-alpha",
|
|
383
|
+
operation: "add"
|
|
384
|
+
},
|
|
385
|
+
alpha: {
|
|
386
|
+
srcFactor: "one",
|
|
387
|
+
dstFactor: "one-minus-src-alpha",
|
|
388
|
+
operation: "add"
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
]
|
|
393
|
+
},
|
|
394
|
+
primitive: {
|
|
395
|
+
topology: "triangle-list",
|
|
396
|
+
cullMode: "none"
|
|
397
|
+
}
|
|
398
|
+
}),
|
|
399
|
+
bindGroupLayout: a,
|
|
400
|
+
uniformBuffer: n,
|
|
401
|
+
bindGroup: u,
|
|
402
|
+
quadBuffer: o,
|
|
403
|
+
vertexStride: A
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
function Y(s, e, t) {
|
|
407
|
+
const i = new Float32Array([
|
|
408
|
+
t.scale[0],
|
|
409
|
+
t.scale[1],
|
|
410
|
+
t.translate[0],
|
|
411
|
+
t.translate[1],
|
|
412
|
+
t.color[0],
|
|
413
|
+
t.color[1],
|
|
414
|
+
t.color[2],
|
|
415
|
+
t.color[3],
|
|
416
|
+
t.pointSize,
|
|
417
|
+
t.symbol,
|
|
418
|
+
// Will be cast to i32 in shader via bitcast
|
|
419
|
+
t.viewport[0],
|
|
420
|
+
t.viewport[1]
|
|
421
|
+
]);
|
|
422
|
+
s.queue.writeBuffer(e, 0, i);
|
|
423
|
+
}
|
|
424
|
+
const X = {
|
|
425
|
+
circle: 0,
|
|
426
|
+
square: 1,
|
|
427
|
+
diamond: 2,
|
|
428
|
+
triangle: 3,
|
|
429
|
+
triangleDown: 4,
|
|
430
|
+
cross: 5,
|
|
431
|
+
x: 6,
|
|
432
|
+
star: 7
|
|
433
|
+
}, $ = 32;
|
|
434
|
+
function H(s, e) {
|
|
435
|
+
const t = s.createShaderModule({ code: D }), i = globalThis.GPUShaderStage, r = globalThis.GPUBufferUsage, a = s.createBindGroupLayout({
|
|
436
|
+
entries: [
|
|
437
|
+
{
|
|
438
|
+
binding: 0,
|
|
439
|
+
visibility: i.VERTEX | i.FRAGMENT,
|
|
440
|
+
buffer: { type: "uniform" }
|
|
441
|
+
}
|
|
442
|
+
]
|
|
443
|
+
}), n = s.createBuffer({
|
|
444
|
+
size: $,
|
|
445
|
+
usage: r.UNIFORM | r.COPY_DST
|
|
446
|
+
}), o = s.createBindGroup({
|
|
447
|
+
layout: a,
|
|
448
|
+
entries: [
|
|
449
|
+
{
|
|
450
|
+
binding: 0,
|
|
451
|
+
resource: { buffer: n }
|
|
452
|
+
}
|
|
453
|
+
]
|
|
454
|
+
}), u = s.createPipelineLayout({
|
|
455
|
+
bindGroupLayouts: [a]
|
|
456
|
+
});
|
|
457
|
+
return {
|
|
458
|
+
pipeline: s.createRenderPipeline({
|
|
459
|
+
layout: u,
|
|
460
|
+
vertex: {
|
|
461
|
+
module: t,
|
|
462
|
+
entryPoint: "vs_main",
|
|
463
|
+
buffers: [
|
|
464
|
+
{
|
|
465
|
+
arrayStride: S,
|
|
466
|
+
attributes: [
|
|
467
|
+
{ shaderLocation: 0, offset: 0, format: "float32x2" }
|
|
468
|
+
]
|
|
469
|
+
}
|
|
470
|
+
]
|
|
471
|
+
},
|
|
472
|
+
fragment: {
|
|
473
|
+
module: t,
|
|
474
|
+
entryPoint: "fs_main",
|
|
475
|
+
targets: [
|
|
476
|
+
{
|
|
477
|
+
format: e,
|
|
478
|
+
blend: {
|
|
479
|
+
color: {
|
|
480
|
+
srcFactor: "src-alpha",
|
|
481
|
+
dstFactor: "one-minus-src-alpha",
|
|
482
|
+
operation: "add"
|
|
483
|
+
},
|
|
484
|
+
alpha: {
|
|
485
|
+
srcFactor: "one",
|
|
486
|
+
dstFactor: "one-minus-src-alpha",
|
|
487
|
+
operation: "add"
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
]
|
|
492
|
+
},
|
|
493
|
+
primitive: {
|
|
494
|
+
topology: "triangle-strip",
|
|
495
|
+
// Key difference from line pipeline
|
|
496
|
+
stripIndexFormat: void 0
|
|
497
|
+
}
|
|
498
|
+
}),
|
|
499
|
+
bindGroupLayout: a,
|
|
500
|
+
uniformBuffer: n,
|
|
501
|
+
bindGroup: o,
|
|
502
|
+
vertexStride: S
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
function K(s, e, t) {
|
|
506
|
+
const i = new Float32Array([
|
|
507
|
+
t.scale[0],
|
|
508
|
+
t.scale[1],
|
|
509
|
+
t.translate[0],
|
|
510
|
+
t.translate[1],
|
|
511
|
+
t.color[0],
|
|
512
|
+
t.color[1],
|
|
513
|
+
t.color[2],
|
|
514
|
+
t.color[3]
|
|
515
|
+
]);
|
|
516
|
+
s.queue.writeBuffer(e, 0, i);
|
|
517
|
+
}
|
|
518
|
+
const Z = `
|
|
519
|
+
// Uniform buffer for transforms and value range
|
|
520
|
+
struct Uniforms {
|
|
521
|
+
scale: vec2<f32>,
|
|
522
|
+
translate: vec2<f32>,
|
|
523
|
+
minValue: f32,
|
|
524
|
+
maxValue: f32,
|
|
525
|
+
_padding: vec2<f32>,
|
|
526
|
+
};
|
|
527
|
+
|
|
528
|
+
@group(0) @binding(0)
|
|
529
|
+
var<uniform> uniforms: Uniforms;
|
|
530
|
+
|
|
531
|
+
@group(0) @binding(1)
|
|
532
|
+
var colormapSampler: sampler;
|
|
533
|
+
|
|
534
|
+
@group(0) @binding(2)
|
|
535
|
+
var colormapTexture: texture_1d<f32>;
|
|
536
|
+
|
|
537
|
+
struct VSInput {
|
|
538
|
+
@location(0) position: vec2<f32>,
|
|
539
|
+
@location(1) value: f32,
|
|
540
|
+
};
|
|
541
|
+
|
|
542
|
+
struct VSOutput {
|
|
543
|
+
@builtin(position) pos: vec4<f32>,
|
|
544
|
+
@location(0) value: f32,
|
|
545
|
+
};
|
|
546
|
+
|
|
547
|
+
@vertex
|
|
548
|
+
fn vs_main(in: VSInput) -> VSOutput {
|
|
549
|
+
var out: VSOutput;
|
|
550
|
+
let transformed = in.position * uniforms.scale + uniforms.translate;
|
|
551
|
+
out.pos = vec4<f32>(transformed, 0.0, 1.0);
|
|
552
|
+
out.value = in.value;
|
|
553
|
+
return out;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
@fragment
|
|
557
|
+
fn fs_main(in: VSOutput) -> @location(0) vec4<f32> {
|
|
558
|
+
let range = uniforms.maxValue - uniforms.minValue;
|
|
559
|
+
var t: f32;
|
|
560
|
+
if (range != 0.0) {
|
|
561
|
+
t = (in.value - uniforms.minValue) / range;
|
|
562
|
+
} else {
|
|
563
|
+
t = 0.0;
|
|
564
|
+
}
|
|
565
|
+
t = clamp(t, 0.0, 1.0);
|
|
566
|
+
|
|
567
|
+
return textureSample(colormapTexture, colormapSampler, t);
|
|
568
|
+
}
|
|
569
|
+
`, G = 3 * 4, Q = 32;
|
|
570
|
+
function j(s, e) {
|
|
571
|
+
const t = s.createShaderModule({ code: Z }), i = globalThis.GPUShaderStage, r = globalThis.GPUBufferUsage, a = s.createBindGroupLayout({
|
|
572
|
+
entries: [
|
|
573
|
+
{
|
|
574
|
+
binding: 0,
|
|
575
|
+
visibility: i.VERTEX | i.FRAGMENT,
|
|
576
|
+
buffer: { type: "uniform" }
|
|
577
|
+
},
|
|
578
|
+
{
|
|
579
|
+
binding: 1,
|
|
580
|
+
visibility: i.FRAGMENT,
|
|
581
|
+
sampler: {}
|
|
582
|
+
},
|
|
583
|
+
{
|
|
584
|
+
binding: 2,
|
|
585
|
+
visibility: i.FRAGMENT,
|
|
586
|
+
texture: {
|
|
587
|
+
sampleType: "float",
|
|
588
|
+
viewDimension: "1d"
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
]
|
|
592
|
+
}), n = s.createBuffer({
|
|
593
|
+
size: Q,
|
|
594
|
+
usage: r.UNIFORM | r.COPY_DST
|
|
595
|
+
}), o = s.createSampler({
|
|
596
|
+
magFilter: "linear",
|
|
597
|
+
minFilter: "linear",
|
|
598
|
+
addressModeU: "clamp-to-edge"
|
|
599
|
+
}), u = s.createPipelineLayout({
|
|
600
|
+
bindGroupLayouts: [a]
|
|
601
|
+
});
|
|
602
|
+
return {
|
|
603
|
+
pipeline: s.createRenderPipeline({
|
|
604
|
+
layout: u,
|
|
605
|
+
vertex: {
|
|
606
|
+
module: t,
|
|
607
|
+
entryPoint: "vs_main",
|
|
608
|
+
buffers: [
|
|
609
|
+
{
|
|
610
|
+
arrayStride: G,
|
|
611
|
+
attributes: [
|
|
612
|
+
{ shaderLocation: 0, offset: 0, format: "float32x2" },
|
|
613
|
+
// position
|
|
614
|
+
{ shaderLocation: 1, offset: 8, format: "float32" }
|
|
615
|
+
// value
|
|
616
|
+
]
|
|
617
|
+
}
|
|
618
|
+
]
|
|
619
|
+
},
|
|
620
|
+
fragment: {
|
|
621
|
+
module: t,
|
|
622
|
+
entryPoint: "fs_main",
|
|
623
|
+
targets: [
|
|
624
|
+
{
|
|
625
|
+
format: e,
|
|
626
|
+
blend: {
|
|
627
|
+
color: {
|
|
628
|
+
srcFactor: "src-alpha",
|
|
629
|
+
dstFactor: "one-minus-src-alpha",
|
|
630
|
+
operation: "add"
|
|
631
|
+
},
|
|
632
|
+
alpha: {
|
|
633
|
+
srcFactor: "one",
|
|
634
|
+
dstFactor: "one-minus-src-alpha",
|
|
635
|
+
operation: "add"
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
]
|
|
640
|
+
},
|
|
641
|
+
primitive: {
|
|
642
|
+
topology: "triangle-list",
|
|
643
|
+
cullMode: "none"
|
|
644
|
+
}
|
|
645
|
+
}),
|
|
646
|
+
bindGroupLayout: a,
|
|
647
|
+
uniformBuffer: n,
|
|
648
|
+
sampler: o,
|
|
649
|
+
vertexStride: G
|
|
650
|
+
};
|
|
651
|
+
}
|
|
652
|
+
function J(s, e, t, i, r) {
|
|
653
|
+
return s.createBindGroup({
|
|
654
|
+
layout: e,
|
|
655
|
+
entries: [
|
|
656
|
+
{ binding: 0, resource: { buffer: t } },
|
|
657
|
+
{ binding: 1, resource: i },
|
|
658
|
+
{ binding: 2, resource: r.createView() }
|
|
659
|
+
]
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
function ee(s, e, t) {
|
|
663
|
+
const i = new Float32Array([
|
|
664
|
+
t.scale[0],
|
|
665
|
+
t.scale[1],
|
|
666
|
+
t.translate[0],
|
|
667
|
+
t.translate[1],
|
|
668
|
+
t.minValue,
|
|
669
|
+
t.maxValue,
|
|
670
|
+
0,
|
|
671
|
+
// padding
|
|
672
|
+
0
|
|
673
|
+
// padding
|
|
674
|
+
]);
|
|
675
|
+
s.queue.writeBuffer(e, 0, i);
|
|
676
|
+
}
|
|
677
|
+
function te(s) {
|
|
678
|
+
const e = s.xMax - s.xMin, t = s.yMax - s.yMin, i = e > 0 ? 2 / e : 1, r = t > 0 ? 2 / t : 1, a = -1 - s.xMin * i, n = -1 - s.yMin * r;
|
|
679
|
+
return {
|
|
680
|
+
scale: [i, r],
|
|
681
|
+
translate: [a, n]
|
|
682
|
+
};
|
|
683
|
+
}
|
|
684
|
+
function ie(s) {
|
|
685
|
+
const e = (s == null ? void 0 : s.color) ?? [1, 0, 0.3, 1], t = (s == null ? void 0 : s.opacity) ?? 1;
|
|
686
|
+
return Array.isArray(e) ? [e[0], e[1], e[2], (e[3] ?? 1) * t] : [1, 0, 0.3, t];
|
|
687
|
+
}
|
|
688
|
+
class w {
|
|
689
|
+
constructor(e, t = {}) {
|
|
690
|
+
c(this, "info");
|
|
691
|
+
c(this, "canvas");
|
|
692
|
+
c(this, "opts");
|
|
693
|
+
c(this, "viewport", null);
|
|
694
|
+
c(this, "adapter");
|
|
695
|
+
c(this, "device");
|
|
696
|
+
c(this, "context");
|
|
697
|
+
c(this, "format", null);
|
|
698
|
+
c(this, "buffers", /* @__PURE__ */ new Map());
|
|
699
|
+
c(this, "textures", /* @__PURE__ */ new Map());
|
|
700
|
+
// Pipelines for different draw types
|
|
701
|
+
c(this, "trianglePipeline", null);
|
|
702
|
+
c(this, "linePipeline", null);
|
|
703
|
+
c(this, "pointPipeline", null);
|
|
704
|
+
c(this, "bandPipeline", null);
|
|
705
|
+
c(this, "heatmapPipeline", null);
|
|
706
|
+
// Heatmap bind groups (per texture)
|
|
707
|
+
c(this, "heatmapBindGroups", /* @__PURE__ */ new Map());
|
|
708
|
+
this.canvas = e, this.opts = t, this.info = {
|
|
709
|
+
type: "webgpu",
|
|
710
|
+
available: w.isSupported()
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
static isSupported() {
|
|
714
|
+
return typeof globalThis.navigator < "u" && typeof globalThis.navigator.gpu < "u";
|
|
715
|
+
}
|
|
716
|
+
async init() {
|
|
717
|
+
if (!this.info.available)
|
|
718
|
+
throw new Error("[gpu] WebGPU not supported");
|
|
719
|
+
const e = globalThis.navigator.gpu;
|
|
720
|
+
if (this.adapter = await e.requestAdapter({
|
|
721
|
+
powerPreference: this.opts.powerPreference ?? "high-performance"
|
|
722
|
+
}), !this.adapter)
|
|
723
|
+
throw new Error("[gpu] Failed to request WebGPU adapter");
|
|
724
|
+
this.device = await this.adapter.requestDevice();
|
|
725
|
+
const t = this.canvas.getContext("webgpu");
|
|
726
|
+
if (!t)
|
|
727
|
+
throw new Error("[gpu] Failed to get WebGPU canvas context");
|
|
728
|
+
this.context = t;
|
|
729
|
+
const i = this.opts.preferredFormat ?? (e.getPreferredCanvasFormat ? e.getPreferredCanvasFormat() : "bgra8unorm");
|
|
730
|
+
this.format = i, this.context.configure({
|
|
731
|
+
device: this.device,
|
|
732
|
+
format: i,
|
|
733
|
+
alphaMode: "premultiplied"
|
|
734
|
+
});
|
|
735
|
+
}
|
|
736
|
+
setViewport(e) {
|
|
737
|
+
this.viewport = e;
|
|
738
|
+
const t = Math.max(1, Math.floor(e.width * e.dpr)), i = Math.max(1, Math.floor(e.height * e.dpr));
|
|
739
|
+
this.canvas.width !== t && (this.canvas.width = t), this.canvas.height !== i && (this.canvas.height = i);
|
|
740
|
+
}
|
|
741
|
+
createOrUpdateBuffer(e, t, i) {
|
|
742
|
+
if (!this.device)
|
|
743
|
+
throw new Error("[gpu] WebGPUBackend not initialized");
|
|
744
|
+
const r = (i == null ? void 0 : i.usage) ?? "vertex", a = globalThis.GPUBufferUsage;
|
|
745
|
+
if (!a)
|
|
746
|
+
throw new Error(
|
|
747
|
+
"[gpu] GPUBufferUsage not available (missing WebGPU runtime)"
|
|
748
|
+
);
|
|
749
|
+
const n = r === "index" ? a.INDEX | a.COPY_DST : r === "uniform" ? a.UNIFORM | a.COPY_DST : a.VERTEX | a.COPY_DST, o = t.byteLength;
|
|
750
|
+
let u = this.buffers.get(e);
|
|
751
|
+
if (!u || u.size < o) {
|
|
752
|
+
if (u)
|
|
753
|
+
try {
|
|
754
|
+
u.destroy();
|
|
755
|
+
} catch {
|
|
756
|
+
}
|
|
757
|
+
u = this.device.createBuffer({
|
|
758
|
+
size: Math.max(4, o),
|
|
759
|
+
usage: n
|
|
760
|
+
}), this.buffers.set(e, u);
|
|
761
|
+
}
|
|
762
|
+
this.device.queue.writeBuffer(
|
|
763
|
+
u,
|
|
764
|
+
0,
|
|
765
|
+
t.buffer,
|
|
766
|
+
t.byteOffset,
|
|
767
|
+
t.byteLength
|
|
768
|
+
);
|
|
769
|
+
}
|
|
770
|
+
deleteBuffer(e) {
|
|
771
|
+
const t = this.buffers.get(e);
|
|
772
|
+
if (t) {
|
|
773
|
+
try {
|
|
774
|
+
t.destroy();
|
|
775
|
+
} catch {
|
|
776
|
+
}
|
|
777
|
+
this.buffers.delete(e);
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
createOrUpdateTexture1D(e, t, i) {
|
|
781
|
+
if (!this.device)
|
|
782
|
+
throw new Error("[gpu] WebGPUBackend not initialized");
|
|
783
|
+
const r = (i == null ? void 0 : i.width) ?? Math.floor(t.length / 4), a = (i == null ? void 0 : i.format) ?? "rgba8unorm", n = globalThis.GPUTextureUsage;
|
|
784
|
+
if (!n)
|
|
785
|
+
throw new Error(
|
|
786
|
+
"[gpu] GPUTextureUsage not available (missing WebGPU runtime)"
|
|
787
|
+
);
|
|
788
|
+
let o = this.textures.get(e);
|
|
789
|
+
o || (o = this.device.createTexture({
|
|
790
|
+
size: { width: r, height: 1, depthOrArrayLayers: 1 },
|
|
791
|
+
format: a,
|
|
792
|
+
dimension: "1d",
|
|
793
|
+
usage: n.TEXTURE_BINDING | n.COPY_DST
|
|
794
|
+
}), this.textures.set(e, o), this.heatmapBindGroups.delete(e)), this.device.queue.writeTexture(
|
|
795
|
+
{ texture: o },
|
|
796
|
+
t,
|
|
797
|
+
{ bytesPerRow: r * 4 },
|
|
798
|
+
{ width: r, height: 1, depthOrArrayLayers: 1 }
|
|
799
|
+
);
|
|
800
|
+
}
|
|
801
|
+
deleteTexture(e) {
|
|
802
|
+
const t = this.textures.get(e);
|
|
803
|
+
if (t) {
|
|
804
|
+
try {
|
|
805
|
+
t.destroy();
|
|
806
|
+
} catch {
|
|
807
|
+
}
|
|
808
|
+
this.textures.delete(e), this.heatmapBindGroups.delete(e);
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
/**
|
|
812
|
+
* Initialize all pipelines lazily
|
|
813
|
+
*/
|
|
814
|
+
ensurePipelines() {
|
|
815
|
+
this.trianglePipeline || (this.trianglePipeline = O(this.device, this.format)), this.linePipeline || (this.linePipeline = V(this.device, this.format)), this.pointPipeline || (this.pointPipeline = q(this.device, this.format)), this.bandPipeline || (this.bandPipeline = H(this.device, this.format)), this.heatmapPipeline || (this.heatmapPipeline = j(this.device, this.format));
|
|
816
|
+
}
|
|
817
|
+
render(e, t) {
|
|
818
|
+
if (!this.device || !this.context || !this.format)
|
|
819
|
+
throw new Error("[gpu] WebGPUBackend not initialized");
|
|
820
|
+
this.viewport || this.setViewport(t.viewport), this.ensurePipelines();
|
|
821
|
+
const i = this.device.createCommandEncoder(), r = this.context.getCurrentTexture().createView(), a = i.beginRenderPass({
|
|
822
|
+
colorAttachments: [
|
|
823
|
+
{
|
|
824
|
+
view: r,
|
|
825
|
+
clearValue: {
|
|
826
|
+
r: t.clearColor[0],
|
|
827
|
+
g: t.clearColor[1],
|
|
828
|
+
b: t.clearColor[2],
|
|
829
|
+
a: t.clearColor[3]
|
|
830
|
+
},
|
|
831
|
+
loadOp: "clear",
|
|
832
|
+
storeOp: "store"
|
|
833
|
+
}
|
|
834
|
+
]
|
|
835
|
+
}), n = { xMin: -1, xMax: 1, yMin: -1, yMax: 1 };
|
|
836
|
+
for (const o of e.items) {
|
|
837
|
+
if (!o.visible) continue;
|
|
838
|
+
const u = this.buffers.get(o.bufferId);
|
|
839
|
+
u && this.renderDrawCall(a, o, u, n);
|
|
840
|
+
}
|
|
841
|
+
a.end(), this.device.queue.submit([i.finish()]);
|
|
842
|
+
}
|
|
843
|
+
/**
|
|
844
|
+
* Render with explicit bounds (for integration with chart system)
|
|
845
|
+
*/
|
|
846
|
+
renderWithBounds(e, t, i) {
|
|
847
|
+
if (!this.device || !this.context || !this.format)
|
|
848
|
+
throw new Error("[gpu] WebGPUBackend not initialized");
|
|
849
|
+
this.viewport || this.setViewport(t.viewport), this.ensurePipelines();
|
|
850
|
+
const r = this.device.createCommandEncoder(), a = this.context.getCurrentTexture().createView(), n = r.beginRenderPass({
|
|
851
|
+
colorAttachments: [
|
|
852
|
+
{
|
|
853
|
+
view: a,
|
|
854
|
+
clearValue: {
|
|
855
|
+
r: t.clearColor[0],
|
|
856
|
+
g: t.clearColor[1],
|
|
857
|
+
b: t.clearColor[2],
|
|
858
|
+
a: t.clearColor[3]
|
|
859
|
+
},
|
|
860
|
+
loadOp: "clear",
|
|
861
|
+
storeOp: "store"
|
|
862
|
+
}
|
|
863
|
+
]
|
|
864
|
+
});
|
|
865
|
+
for (const o of e.items) {
|
|
866
|
+
if (!o.visible) continue;
|
|
867
|
+
const u = this.buffers.get(o.bufferId);
|
|
868
|
+
if (!u) continue;
|
|
869
|
+
const l = o.yBounds ? { ...i, yMin: o.yBounds.min, yMax: o.yBounds.max } : i;
|
|
870
|
+
this.renderDrawCall(n, o, u, l);
|
|
871
|
+
}
|
|
872
|
+
n.end(), this.device.queue.submit([r.finish()]);
|
|
873
|
+
}
|
|
874
|
+
/**
|
|
875
|
+
* Render a single draw call
|
|
876
|
+
*/
|
|
877
|
+
renderDrawCall(e, t, i, r) {
|
|
878
|
+
const a = te(r), n = ie(t.style);
|
|
879
|
+
switch (t.kind) {
|
|
880
|
+
case "triangles":
|
|
881
|
+
case "bar":
|
|
882
|
+
this.renderSolidTriangles(e, i, t.count, a, n);
|
|
883
|
+
break;
|
|
884
|
+
case "line":
|
|
885
|
+
this.renderLine(e, i, t.count, a, n);
|
|
886
|
+
break;
|
|
887
|
+
case "scatter":
|
|
888
|
+
case "points":
|
|
889
|
+
this.renderPoints(e, i, t.count, a, n, t.style);
|
|
890
|
+
break;
|
|
891
|
+
case "line+scatter":
|
|
892
|
+
this.renderLine(e, i, t.count, a, n), this.renderPoints(e, i, t.count, a, n, t.style);
|
|
893
|
+
break;
|
|
894
|
+
case "step":
|
|
895
|
+
if (t.stepBufferId && t.stepCount) {
|
|
896
|
+
const u = this.buffers.get(t.stepBufferId);
|
|
897
|
+
u && this.renderLine(e, u, t.stepCount, a, n);
|
|
898
|
+
} else
|
|
899
|
+
this.renderLine(e, i, t.count, a, n);
|
|
900
|
+
break;
|
|
901
|
+
case "step+scatter":
|
|
902
|
+
if (t.stepBufferId && t.stepCount) {
|
|
903
|
+
const u = this.buffers.get(t.stepBufferId);
|
|
904
|
+
u && this.renderLine(e, u, t.stepCount, a, n);
|
|
905
|
+
} else
|
|
906
|
+
this.renderLine(e, i, t.count, a, n);
|
|
907
|
+
this.renderPoints(e, i, t.count, a, n, t.style);
|
|
908
|
+
break;
|
|
909
|
+
case "band":
|
|
910
|
+
const o = [
|
|
911
|
+
n[0],
|
|
912
|
+
n[1],
|
|
913
|
+
n[2],
|
|
914
|
+
n[3] * 0.4
|
|
915
|
+
];
|
|
916
|
+
this.renderBand(e, i, t.count, a, o);
|
|
917
|
+
break;
|
|
918
|
+
case "heatmap":
|
|
919
|
+
this.renderHeatmap(e, i, t.count, a, t.style, t.textureId);
|
|
920
|
+
break;
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
/** Triangle list with line-pipeline uniforms (xy vertices, uniform color). */
|
|
924
|
+
renderSolidTriangles(e, t, i, r, a) {
|
|
925
|
+
k(this.device, this.linePipeline.uniformBuffer, {
|
|
926
|
+
scale: r.scale,
|
|
927
|
+
translate: r.translate,
|
|
928
|
+
color: a
|
|
929
|
+
}), e.setPipeline(this.linePipeline.pipeline), e.setBindGroup(0, this.linePipeline.bindGroup), e.setVertexBuffer(0, t), e.draw(i);
|
|
930
|
+
}
|
|
931
|
+
renderLine(e, t, i, r, a) {
|
|
932
|
+
k(this.device, this.linePipeline.uniformBuffer, {
|
|
933
|
+
scale: r.scale,
|
|
934
|
+
translate: r.translate,
|
|
935
|
+
color: a
|
|
936
|
+
}), e.setPipeline(this.linePipeline.pipeline), e.setBindGroup(0, this.linePipeline.bindGroup), e.setVertexBuffer(0, t), e.draw(i);
|
|
937
|
+
}
|
|
938
|
+
renderPoints(e, t, i, r, a, n) {
|
|
939
|
+
var f;
|
|
940
|
+
const o = ((n == null ? void 0 : n.pointSize) ?? 4) * (((f = this.viewport) == null ? void 0 : f.dpr) ?? 1), u = X[(n == null ? void 0 : n.symbol) ?? "circle"] ?? 0, l = [
|
|
941
|
+
this.canvas.width,
|
|
942
|
+
this.canvas.height
|
|
943
|
+
];
|
|
944
|
+
Y(this.device, this.pointPipeline.uniformBuffer, {
|
|
945
|
+
scale: r.scale,
|
|
946
|
+
translate: r.translate,
|
|
947
|
+
color: a,
|
|
948
|
+
pointSize: o,
|
|
949
|
+
symbol: u,
|
|
950
|
+
viewport: l
|
|
951
|
+
}), e.setPipeline(this.pointPipeline.pipeline), e.setBindGroup(0, this.pointPipeline.bindGroup), e.setVertexBuffer(0, t), e.setVertexBuffer(1, this.pointPipeline.quadBuffer), e.draw(6, i);
|
|
952
|
+
}
|
|
953
|
+
renderBand(e, t, i, r, a) {
|
|
954
|
+
K(this.device, this.bandPipeline.uniformBuffer, {
|
|
955
|
+
scale: r.scale,
|
|
956
|
+
translate: r.translate,
|
|
957
|
+
color: a
|
|
958
|
+
}), e.setPipeline(this.bandPipeline.pipeline), e.setBindGroup(0, this.bandPipeline.bindGroup), e.setVertexBuffer(0, t), e.draw(i);
|
|
959
|
+
}
|
|
960
|
+
renderHeatmap(e, t, i, r, a, n) {
|
|
961
|
+
if (!n) return;
|
|
962
|
+
const o = this.textures.get(n);
|
|
963
|
+
if (!o) return;
|
|
964
|
+
const u = (a == null ? void 0 : a.zBounds) ?? { min: 0, max: 1 };
|
|
965
|
+
ee(this.device, this.heatmapPipeline.uniformBuffer, {
|
|
966
|
+
scale: r.scale,
|
|
967
|
+
translate: r.translate,
|
|
968
|
+
minValue: u.min,
|
|
969
|
+
maxValue: u.max
|
|
970
|
+
});
|
|
971
|
+
let l = this.heatmapBindGroups.get(n);
|
|
972
|
+
l || (l = J(
|
|
973
|
+
this.device,
|
|
974
|
+
this.heatmapPipeline.bindGroupLayout,
|
|
975
|
+
this.heatmapPipeline.uniformBuffer,
|
|
976
|
+
this.heatmapPipeline.sampler,
|
|
977
|
+
o
|
|
978
|
+
), this.heatmapBindGroups.set(n, l)), e.setPipeline(this.heatmapPipeline.pipeline), e.setBindGroup(0, l), e.setVertexBuffer(0, t), e.draw(i);
|
|
979
|
+
}
|
|
980
|
+
destroy() {
|
|
981
|
+
for (const e of Array.from(this.buffers.keys()))
|
|
982
|
+
this.deleteBuffer(e);
|
|
983
|
+
for (const e of Array.from(this.textures.keys()))
|
|
984
|
+
this.deleteTexture(e);
|
|
985
|
+
this.trianglePipeline = null, this.linePipeline = null, this.pointPipeline = null, this.bandPipeline = null, this.heatmapPipeline = null, this.heatmapBindGroups.clear(), this.device = void 0, this.adapter = void 0, this.context = void 0, this.format = null;
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
const re = `
|
|
989
|
+
precision highp float;
|
|
990
|
+
attribute vec2 aPosition;
|
|
991
|
+
uniform vec2 uScale;
|
|
992
|
+
uniform vec2 uTranslate;
|
|
993
|
+
|
|
994
|
+
void main() {
|
|
995
|
+
vec2 pos = aPosition * uScale + uTranslate;
|
|
996
|
+
gl_Position = vec4(pos, 0.0, 1.0);
|
|
997
|
+
}
|
|
998
|
+
`, ae = `
|
|
999
|
+
precision highp float;
|
|
1000
|
+
uniform vec4 uColor;
|
|
1001
|
+
|
|
1002
|
+
void main() {
|
|
1003
|
+
gl_FragColor = uColor;
|
|
1004
|
+
}
|
|
1005
|
+
`, ne = `
|
|
1006
|
+
precision highp float;
|
|
1007
|
+
attribute vec2 aPosition;
|
|
1008
|
+
uniform vec2 uScale;
|
|
1009
|
+
uniform vec2 uTranslate;
|
|
1010
|
+
uniform float uPointSize;
|
|
1011
|
+
|
|
1012
|
+
void main() {
|
|
1013
|
+
vec2 pos = aPosition * uScale + uTranslate;
|
|
1014
|
+
gl_Position = vec4(pos, 0.0, 1.0);
|
|
1015
|
+
gl_PointSize = uPointSize;
|
|
1016
|
+
}
|
|
1017
|
+
`, se = `
|
|
1018
|
+
precision highp float;
|
|
1019
|
+
uniform vec4 uColor;
|
|
1020
|
+
uniform int uSymbol;
|
|
1021
|
+
|
|
1022
|
+
float sdCircle(vec2 p, float r) {
|
|
1023
|
+
return length(p) - r;
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
float sdBox(vec2 p, vec2 b) {
|
|
1027
|
+
vec2 d = abs(p) - b;
|
|
1028
|
+
return length(max(d, 0.0)) + min(max(d.x, d.y), 0.0);
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
float sdTriangle(vec2 p, float r) {
|
|
1032
|
+
const float k = sqrt(3.0);
|
|
1033
|
+
p.x = abs(p.x) - r;
|
|
1034
|
+
p.y = p.y + r / k;
|
|
1035
|
+
if (p.x + k * p.y > 0.0) p = vec2(p.x - k * p.y, -k * p.x - p.y) / 2.0;
|
|
1036
|
+
p.x -= clamp(p.x, -2.0 * r, 0.0);
|
|
1037
|
+
return -length(p) * sign(p.y);
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
float sdDiamond(vec2 p, float r) {
|
|
1041
|
+
return (abs(p.x) + abs(p.y)) - r;
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
float sdCross(vec2 p, float r, float thickness) {
|
|
1045
|
+
vec2 d = abs(p);
|
|
1046
|
+
float s1 = sdBox(d, vec2(r, thickness));
|
|
1047
|
+
float s2 = sdBox(d, vec2(thickness, r));
|
|
1048
|
+
return min(s1, s2);
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
float sdX(vec2 p, float r, float thickness) {
|
|
1052
|
+
float c = cos(0.785398);
|
|
1053
|
+
float s = sin(0.785398);
|
|
1054
|
+
mat2 m = mat2(c, -s, s, c);
|
|
1055
|
+
return sdCross(m * p, r, thickness);
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
float sdStar(vec2 p, float r, float rf) {
|
|
1059
|
+
const vec2 k1 = vec2(0.80901699, -0.58778525);
|
|
1060
|
+
const vec2 k2 = vec2(-k1.x, k1.y);
|
|
1061
|
+
p.x = abs(p.x);
|
|
1062
|
+
p -= 2.0 * max(dot(k1, p), 0.0) * k1;
|
|
1063
|
+
p -= 2.0 * max(dot(k2, p), 0.0) * k2;
|
|
1064
|
+
p.x = abs(p.x);
|
|
1065
|
+
p.y -= r;
|
|
1066
|
+
vec2 ba = rf * vec2(-k1.y, k1.x) - vec2(0, 1);
|
|
1067
|
+
float h = clamp(dot(p, ba) / dot(ba, ba), 0.0, r);
|
|
1068
|
+
return length(p - ba * h) * sign(p.y * ba.x - p.x * ba.y);
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
void main() {
|
|
1072
|
+
vec2 p = gl_PointCoord - vec2(0.5);
|
|
1073
|
+
float d = 0.0;
|
|
1074
|
+
|
|
1075
|
+
if (uSymbol == 0) {
|
|
1076
|
+
d = sdCircle(p, 0.45);
|
|
1077
|
+
} else if (uSymbol == 1) {
|
|
1078
|
+
d = sdBox(p, vec2(0.35));
|
|
1079
|
+
} else if (uSymbol == 2) {
|
|
1080
|
+
d = sdDiamond(p, 0.45);
|
|
1081
|
+
} else if (uSymbol == 3) {
|
|
1082
|
+
d = sdTriangle(vec2(p.x, p.y + 0.1), 0.4);
|
|
1083
|
+
} else if (uSymbol == 4) {
|
|
1084
|
+
d = sdTriangle(vec2(p.x, -p.y + 0.1), 0.4);
|
|
1085
|
+
} else if (uSymbol == 5) {
|
|
1086
|
+
d = sdCross(p, 0.45, 0.15);
|
|
1087
|
+
} else if (uSymbol == 6) {
|
|
1088
|
+
d = sdX(p, 0.45, 0.15);
|
|
1089
|
+
} else if (uSymbol == 7) {
|
|
1090
|
+
d = sdStar(p, 0.45, 0.4);
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
if (d > 0.02) discard;
|
|
1094
|
+
|
|
1095
|
+
float alpha = 1.0 - smoothstep(0.0, 0.02, d);
|
|
1096
|
+
gl_FragColor = vec4(uColor.rgb, uColor.a * alpha);
|
|
1097
|
+
}
|
|
1098
|
+
`, oe = `
|
|
1099
|
+
precision highp float;
|
|
1100
|
+
attribute vec2 aPosition;
|
|
1101
|
+
attribute float aValue;
|
|
1102
|
+
uniform vec2 uScale;
|
|
1103
|
+
uniform vec2 uTranslate;
|
|
1104
|
+
varying float vValue;
|
|
1105
|
+
|
|
1106
|
+
void main() {
|
|
1107
|
+
gl_Position = vec4(aPosition * uScale + uTranslate, 0.0, 1.0);
|
|
1108
|
+
vValue = aValue;
|
|
1109
|
+
}
|
|
1110
|
+
`, ue = `
|
|
1111
|
+
precision highp float;
|
|
1112
|
+
varying float vValue;
|
|
1113
|
+
uniform float uMinValue;
|
|
1114
|
+
uniform float uMaxValue;
|
|
1115
|
+
uniform sampler2D uColormap;
|
|
1116
|
+
|
|
1117
|
+
void main() {
|
|
1118
|
+
float range = uMaxValue - uMinValue;
|
|
1119
|
+
float t = (vValue - uMinValue) / (range != 0.0 ? range : 1.0);
|
|
1120
|
+
t = clamp(t, 0.0, 1.0);
|
|
1121
|
+
gl_FragColor = texture2D(uColormap, vec2(t, 0.5));
|
|
1122
|
+
}
|
|
1123
|
+
`;
|
|
1124
|
+
function E(s, e, t) {
|
|
1125
|
+
const i = s.createShader(e);
|
|
1126
|
+
if (!i)
|
|
1127
|
+
throw new Error("[WebGL] Failed to create shader");
|
|
1128
|
+
if (s.shaderSource(i, t), s.compileShader(i), !s.getShaderParameter(i, s.COMPILE_STATUS)) {
|
|
1129
|
+
const r = s.getShaderInfoLog(i);
|
|
1130
|
+
throw s.deleteShader(i), new Error(`[WebGL] Shader compilation error: ${r}`);
|
|
1131
|
+
}
|
|
1132
|
+
return i;
|
|
1133
|
+
}
|
|
1134
|
+
function le(s, e, t) {
|
|
1135
|
+
const i = s.createProgram();
|
|
1136
|
+
if (!i)
|
|
1137
|
+
throw new Error("[WebGL] Failed to create program");
|
|
1138
|
+
if (s.attachShader(i, e), s.attachShader(i, t), s.linkProgram(i), !s.getProgramParameter(i, s.LINK_STATUS)) {
|
|
1139
|
+
const r = s.getProgramInfoLog(i);
|
|
1140
|
+
throw s.deleteProgram(i), new Error(`[WebGL] Program link error: ${r}`);
|
|
1141
|
+
}
|
|
1142
|
+
return i;
|
|
1143
|
+
}
|
|
1144
|
+
function T(s, e, t, i, r) {
|
|
1145
|
+
const a = E(s, s.VERTEX_SHADER, e), n = E(s, s.FRAGMENT_SHADER, t), o = le(s, a, n);
|
|
1146
|
+
s.deleteShader(a), s.deleteShader(n);
|
|
1147
|
+
const u = {};
|
|
1148
|
+
for (const f of i)
|
|
1149
|
+
u[f] = s.getAttribLocation(o, f);
|
|
1150
|
+
const l = {};
|
|
1151
|
+
for (const f of r)
|
|
1152
|
+
l[f] = s.getUniformLocation(o, f);
|
|
1153
|
+
return { program: o, attributes: u, uniforms: l };
|
|
1154
|
+
}
|
|
1155
|
+
function ce(s) {
|
|
1156
|
+
const e = T(
|
|
1157
|
+
s,
|
|
1158
|
+
re,
|
|
1159
|
+
ae,
|
|
1160
|
+
["aPosition"],
|
|
1161
|
+
["uScale", "uTranslate", "uColor"]
|
|
1162
|
+
), t = T(
|
|
1163
|
+
s,
|
|
1164
|
+
ne,
|
|
1165
|
+
se,
|
|
1166
|
+
["aPosition"],
|
|
1167
|
+
["uScale", "uTranslate", "uColor", "uPointSize", "uSymbol"]
|
|
1168
|
+
), i = T(
|
|
1169
|
+
s,
|
|
1170
|
+
oe,
|
|
1171
|
+
ue,
|
|
1172
|
+
["aPosition", "aValue"],
|
|
1173
|
+
["uScale", "uTranslate", "uMinValue", "uMaxValue", "uColormap"]
|
|
1174
|
+
);
|
|
1175
|
+
return { line: e, point: t, heatmap: i };
|
|
1176
|
+
}
|
|
1177
|
+
function pe(s, e) {
|
|
1178
|
+
s.deleteProgram(e.line.program), s.deleteProgram(e.point.program), s.deleteProgram(e.heatmap.program);
|
|
1179
|
+
}
|
|
1180
|
+
const fe = {
|
|
1181
|
+
circle: 0,
|
|
1182
|
+
square: 1,
|
|
1183
|
+
diamond: 2,
|
|
1184
|
+
triangle: 3,
|
|
1185
|
+
triangleDown: 4,
|
|
1186
|
+
cross: 5,
|
|
1187
|
+
x: 6,
|
|
1188
|
+
star: 7
|
|
1189
|
+
};
|
|
1190
|
+
function de(s) {
|
|
1191
|
+
const e = s.xMax - s.xMin, t = s.yMax - s.yMin, i = e > 0 ? 2 / e : 1, r = t > 0 ? 2 / t : 1, a = -1 - s.xMin * i, n = -1 - s.yMin * r;
|
|
1192
|
+
return {
|
|
1193
|
+
scale: [i, r],
|
|
1194
|
+
translate: [a, n]
|
|
1195
|
+
};
|
|
1196
|
+
}
|
|
1197
|
+
function he(s) {
|
|
1198
|
+
const e = (s == null ? void 0 : s.color) ?? [1, 0, 0.3, 1], t = (s == null ? void 0 : s.opacity) ?? 1;
|
|
1199
|
+
return Array.isArray(e) ? [e[0], e[1], e[2], (e[3] ?? 1) * t] : [1, 0, 0.3, t];
|
|
1200
|
+
}
|
|
1201
|
+
class P {
|
|
1202
|
+
constructor(e, t = {}) {
|
|
1203
|
+
c(this, "info");
|
|
1204
|
+
c(this, "canvas");
|
|
1205
|
+
c(this, "opts");
|
|
1206
|
+
c(this, "gl", null);
|
|
1207
|
+
c(this, "viewport", null);
|
|
1208
|
+
c(this, "dpr", 1);
|
|
1209
|
+
c(this, "programs", null);
|
|
1210
|
+
c(this, "buffers", /* @__PURE__ */ new Map());
|
|
1211
|
+
c(this, "textures", /* @__PURE__ */ new Map());
|
|
1212
|
+
this.canvas = e, this.opts = t, this.info = {
|
|
1213
|
+
type: "webgl",
|
|
1214
|
+
available: P.isSupported()
|
|
1215
|
+
};
|
|
1216
|
+
}
|
|
1217
|
+
static isSupported() {
|
|
1218
|
+
if (typeof document > "u") return !1;
|
|
1219
|
+
try {
|
|
1220
|
+
const e = document.createElement("canvas");
|
|
1221
|
+
return !!(e.getContext("webgl") || e.getContext("experimental-webgl"));
|
|
1222
|
+
} catch {
|
|
1223
|
+
return !1;
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
async init() {
|
|
1227
|
+
if (!this.info.available)
|
|
1228
|
+
throw new Error("[gpu] WebGL not supported");
|
|
1229
|
+
const e = this.canvas.getContext("webgl", {
|
|
1230
|
+
alpha: !0,
|
|
1231
|
+
antialias: this.opts.antialias ?? !0,
|
|
1232
|
+
preserveDrawingBuffer: this.opts.preserveDrawingBuffer ?? !0,
|
|
1233
|
+
powerPreference: this.opts.powerPreference ?? "high-performance"
|
|
1234
|
+
});
|
|
1235
|
+
if (!e)
|
|
1236
|
+
throw new Error("[gpu] Failed to get WebGL context");
|
|
1237
|
+
this.gl = e, e.enable(e.BLEND), e.blendFunc(e.SRC_ALPHA, e.ONE_MINUS_SRC_ALPHA), this.programs = ce(e);
|
|
1238
|
+
}
|
|
1239
|
+
setViewport(e) {
|
|
1240
|
+
this.viewport = e, this.dpr = e.dpr;
|
|
1241
|
+
const t = Math.max(1, Math.floor(e.width * e.dpr)), i = Math.max(1, Math.floor(e.height * e.dpr));
|
|
1242
|
+
this.canvas.width !== t && (this.canvas.width = t), this.canvas.height !== i && (this.canvas.height = i), this.gl && this.gl.viewport(0, 0, t, i);
|
|
1243
|
+
}
|
|
1244
|
+
createOrUpdateBuffer(e, t, i) {
|
|
1245
|
+
if (!this.gl)
|
|
1246
|
+
throw new Error("[gpu] WebGLBackend not initialized");
|
|
1247
|
+
const r = this.gl;
|
|
1248
|
+
let a = this.buffers.get(e);
|
|
1249
|
+
a || (a = r.createBuffer(), this.buffers.set(e, a));
|
|
1250
|
+
const n = (i == null ? void 0 : i.usage) === "index" ? r.ELEMENT_ARRAY_BUFFER : r.ARRAY_BUFFER;
|
|
1251
|
+
r.bindBuffer(n, a), r.bufferData(n, t, r.DYNAMIC_DRAW), r.bindBuffer(n, null);
|
|
1252
|
+
}
|
|
1253
|
+
deleteBuffer(e) {
|
|
1254
|
+
if (!this.gl) return;
|
|
1255
|
+
const t = this.buffers.get(e);
|
|
1256
|
+
t && (this.gl.deleteBuffer(t), this.buffers.delete(e));
|
|
1257
|
+
}
|
|
1258
|
+
createOrUpdateTexture1D(e, t, i) {
|
|
1259
|
+
if (!this.gl)
|
|
1260
|
+
throw new Error("[gpu] WebGLBackend not initialized");
|
|
1261
|
+
const r = this.gl, a = (i == null ? void 0 : i.width) ?? Math.floor(t.length / 4);
|
|
1262
|
+
let n = this.textures.get(e);
|
|
1263
|
+
n || (n = r.createTexture(), this.textures.set(e, n)), r.bindTexture(r.TEXTURE_2D, n), r.texImage2D(r.TEXTURE_2D, 0, r.RGBA, a, 1, 0, r.RGBA, r.UNSIGNED_BYTE, t), r.texParameteri(r.TEXTURE_2D, r.TEXTURE_MIN_FILTER, r.LINEAR), r.texParameteri(r.TEXTURE_2D, r.TEXTURE_MAG_FILTER, r.LINEAR), r.texParameteri(r.TEXTURE_2D, r.TEXTURE_WRAP_S, r.CLAMP_TO_EDGE), r.texParameteri(r.TEXTURE_2D, r.TEXTURE_WRAP_T, r.CLAMP_TO_EDGE), r.bindTexture(r.TEXTURE_2D, null);
|
|
1264
|
+
}
|
|
1265
|
+
deleteTexture(e) {
|
|
1266
|
+
if (!this.gl) return;
|
|
1267
|
+
const t = this.textures.get(e);
|
|
1268
|
+
t && (this.gl.deleteTexture(t), this.textures.delete(e));
|
|
1269
|
+
}
|
|
1270
|
+
render(e, t) {
|
|
1271
|
+
this.renderWithBounds(e, t, { xMin: -1, xMax: 1, yMin: -1, yMax: 1 });
|
|
1272
|
+
}
|
|
1273
|
+
renderWithBounds(e, t, i) {
|
|
1274
|
+
if (!this.gl || !this.programs)
|
|
1275
|
+
throw new Error("[gpu] WebGLBackend not initialized");
|
|
1276
|
+
const r = this.gl;
|
|
1277
|
+
this.viewport || this.setViewport(t.viewport), r.clearColor(
|
|
1278
|
+
t.clearColor[0],
|
|
1279
|
+
t.clearColor[1],
|
|
1280
|
+
t.clearColor[2],
|
|
1281
|
+
t.clearColor[3]
|
|
1282
|
+
), r.clear(r.COLOR_BUFFER_BIT);
|
|
1283
|
+
for (const a of e.items) {
|
|
1284
|
+
if (!a.visible) continue;
|
|
1285
|
+
const n = this.buffers.get(a.bufferId);
|
|
1286
|
+
if (!n) continue;
|
|
1287
|
+
const o = a.yBounds ? { ...i, yMin: a.yBounds.min, yMax: a.yBounds.max } : i;
|
|
1288
|
+
this.renderDrawCall(a, n, o);
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
renderDrawCall(e, t, i) {
|
|
1292
|
+
const r = de(i), a = he(e.style);
|
|
1293
|
+
switch (e.kind) {
|
|
1294
|
+
case "line":
|
|
1295
|
+
case "step":
|
|
1296
|
+
this.renderLine(t, e.count, r, a);
|
|
1297
|
+
break;
|
|
1298
|
+
case "scatter":
|
|
1299
|
+
case "points":
|
|
1300
|
+
this.renderPoints(t, e.count, r, a, e.style);
|
|
1301
|
+
break;
|
|
1302
|
+
case "line+scatter":
|
|
1303
|
+
this.renderLine(t, e.count, r, a), this.renderPoints(t, e.count, r, a, e.style);
|
|
1304
|
+
break;
|
|
1305
|
+
case "step+scatter":
|
|
1306
|
+
if (e.stepBufferId && e.stepCount) {
|
|
1307
|
+
const o = this.buffers.get(e.stepBufferId);
|
|
1308
|
+
o && this.renderLine(o, e.stepCount, r, a);
|
|
1309
|
+
} else
|
|
1310
|
+
this.renderLine(t, e.count, r, a);
|
|
1311
|
+
this.renderPoints(t, e.count, r, a, e.style);
|
|
1312
|
+
break;
|
|
1313
|
+
case "band":
|
|
1314
|
+
const n = [
|
|
1315
|
+
a[0],
|
|
1316
|
+
a[1],
|
|
1317
|
+
a[2],
|
|
1318
|
+
a[3] * 0.4
|
|
1319
|
+
];
|
|
1320
|
+
this.renderBand(t, e.count, r, n);
|
|
1321
|
+
break;
|
|
1322
|
+
case "triangles":
|
|
1323
|
+
case "bar":
|
|
1324
|
+
this.renderTriangles(t, e.count, r, a);
|
|
1325
|
+
break;
|
|
1326
|
+
case "heatmap":
|
|
1327
|
+
this.renderHeatmap(t, e.count, r, e.style, e.textureId);
|
|
1328
|
+
break;
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
renderLine(e, t, i, r) {
|
|
1332
|
+
const a = this.gl, n = this.programs.line;
|
|
1333
|
+
a.useProgram(n.program), a.bindBuffer(a.ARRAY_BUFFER, e), a.enableVertexAttribArray(n.attributes.aPosition), a.vertexAttribPointer(n.attributes.aPosition, 2, a.FLOAT, !1, 0, 0), a.uniform2f(n.uniforms.uScale, i.scale[0], i.scale[1]), a.uniform2f(n.uniforms.uTranslate, i.translate[0], i.translate[1]), a.uniform4f(n.uniforms.uColor, r[0], r[1], r[2], r[3]), a.drawArrays(a.LINE_STRIP, 0, t), a.disableVertexAttribArray(n.attributes.aPosition);
|
|
1334
|
+
}
|
|
1335
|
+
renderPoints(e, t, i, r, a) {
|
|
1336
|
+
const n = this.gl, o = this.programs.point, u = ((a == null ? void 0 : a.pointSize) ?? 4) * this.dpr, l = fe[(a == null ? void 0 : a.symbol) ?? "circle"] ?? 0;
|
|
1337
|
+
n.useProgram(o.program), n.bindBuffer(n.ARRAY_BUFFER, e), n.enableVertexAttribArray(o.attributes.aPosition), n.vertexAttribPointer(o.attributes.aPosition, 2, n.FLOAT, !1, 0, 0), n.uniform2f(o.uniforms.uScale, i.scale[0], i.scale[1]), n.uniform2f(o.uniforms.uTranslate, i.translate[0], i.translate[1]), n.uniform4f(o.uniforms.uColor, r[0], r[1], r[2], r[3]), n.uniform1f(o.uniforms.uPointSize, u), n.uniform1i(o.uniforms.uSymbol, l), n.drawArrays(n.POINTS, 0, t), n.disableVertexAttribArray(o.attributes.aPosition);
|
|
1338
|
+
}
|
|
1339
|
+
renderBand(e, t, i, r) {
|
|
1340
|
+
const a = this.gl, n = this.programs.line;
|
|
1341
|
+
a.useProgram(n.program), a.bindBuffer(a.ARRAY_BUFFER, e), a.enableVertexAttribArray(n.attributes.aPosition), a.vertexAttribPointer(n.attributes.aPosition, 2, a.FLOAT, !1, 0, 0), a.uniform2f(n.uniforms.uScale, i.scale[0], i.scale[1]), a.uniform2f(n.uniforms.uTranslate, i.translate[0], i.translate[1]), a.uniform4f(n.uniforms.uColor, r[0], r[1], r[2], r[3]), a.drawArrays(a.TRIANGLE_STRIP, 0, t), a.disableVertexAttribArray(n.attributes.aPosition);
|
|
1342
|
+
}
|
|
1343
|
+
renderTriangles(e, t, i, r) {
|
|
1344
|
+
const a = this.gl, n = this.programs.line;
|
|
1345
|
+
a.useProgram(n.program), a.bindBuffer(a.ARRAY_BUFFER, e), a.enableVertexAttribArray(n.attributes.aPosition), a.vertexAttribPointer(n.attributes.aPosition, 2, a.FLOAT, !1, 0, 0), a.uniform2f(n.uniforms.uScale, i.scale[0], i.scale[1]), a.uniform2f(n.uniforms.uTranslate, i.translate[0], i.translate[1]), a.uniform4f(n.uniforms.uColor, r[0], r[1], r[2], r[3]), a.drawArrays(a.TRIANGLES, 0, t), a.disableVertexAttribArray(n.attributes.aPosition);
|
|
1346
|
+
}
|
|
1347
|
+
renderHeatmap(e, t, i, r, a) {
|
|
1348
|
+
if (!a) return;
|
|
1349
|
+
const n = this.textures.get(a);
|
|
1350
|
+
if (!n) return;
|
|
1351
|
+
const o = this.gl, u = this.programs.heatmap, l = (r == null ? void 0 : r.zBounds) ?? { min: 0, max: 1 };
|
|
1352
|
+
o.useProgram(u.program), o.bindBuffer(o.ARRAY_BUFFER, e), o.enableVertexAttribArray(u.attributes.aPosition), o.vertexAttribPointer(u.attributes.aPosition, 2, o.FLOAT, !1, 12, 0), u.attributes.aValue !== -1 && (o.enableVertexAttribArray(u.attributes.aValue), o.vertexAttribPointer(u.attributes.aValue, 1, o.FLOAT, !1, 12, 8)), o.uniform2f(u.uniforms.uScale, i.scale[0], i.scale[1]), o.uniform2f(u.uniforms.uTranslate, i.translate[0], i.translate[1]), o.uniform1f(u.uniforms.uMinValue, l.min), o.uniform1f(u.uniforms.uMaxValue, l.max), o.activeTexture(o.TEXTURE0), o.bindTexture(o.TEXTURE_2D, n), o.uniform1i(u.uniforms.uColormap, 0), o.drawArrays(o.TRIANGLES, 0, t), o.disableVertexAttribArray(u.attributes.aPosition), u.attributes.aValue !== -1 && o.disableVertexAttribArray(u.attributes.aValue);
|
|
1353
|
+
}
|
|
1354
|
+
destroy() {
|
|
1355
|
+
if (!this.gl) return;
|
|
1356
|
+
const e = this.gl;
|
|
1357
|
+
for (const t of this.buffers.values())
|
|
1358
|
+
e.deleteBuffer(t);
|
|
1359
|
+
this.buffers.clear();
|
|
1360
|
+
for (const t of this.textures.values())
|
|
1361
|
+
e.deleteTexture(t);
|
|
1362
|
+
this.textures.clear(), this.programs && (pe(e, this.programs), this.programs = null), this.gl = null;
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1365
|
+
function B(s) {
|
|
1366
|
+
if (!s)
|
|
1367
|
+
return [1, 0, 0.3, 1];
|
|
1368
|
+
if (Array.isArray(s))
|
|
1369
|
+
return [s[0], s[1], s[2], s[3] ?? 1];
|
|
1370
|
+
const e = s;
|
|
1371
|
+
if (e.startsWith("#")) {
|
|
1372
|
+
const i = e.slice(1);
|
|
1373
|
+
if (i.length === 3) {
|
|
1374
|
+
const r = parseInt(i[0] + i[0], 16) / 255, a = parseInt(i[1] + i[1], 16) / 255, n = parseInt(i[2] + i[2], 16) / 255;
|
|
1375
|
+
return [r, a, n, 1];
|
|
1376
|
+
}
|
|
1377
|
+
if (i.length === 6) {
|
|
1378
|
+
const r = parseInt(i.slice(0, 2), 16) / 255, a = parseInt(i.slice(2, 4), 16) / 255, n = parseInt(i.slice(4, 6), 16) / 255;
|
|
1379
|
+
return [r, a, n, 1];
|
|
1380
|
+
}
|
|
1381
|
+
if (i.length === 8) {
|
|
1382
|
+
const r = parseInt(i.slice(0, 2), 16) / 255, a = parseInt(i.slice(2, 4), 16) / 255, n = parseInt(i.slice(4, 6), 16) / 255, o = parseInt(i.slice(6, 8), 16) / 255;
|
|
1383
|
+
return [r, a, n, o];
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
const t = e.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?\)/);
|
|
1387
|
+
if (t) {
|
|
1388
|
+
const i = parseInt(t[1]) / 255, r = parseInt(t[2]) / 255, a = parseInt(t[3]) / 255, n = t[4] ? parseFloat(t[4]) : 1;
|
|
1389
|
+
return [i, r, a, n];
|
|
1390
|
+
}
|
|
1391
|
+
return [1, 0, 0.3, 1];
|
|
1392
|
+
}
|
|
1393
|
+
function me(s, e) {
|
|
1394
|
+
const t = B(s.color), i = s.opacity ?? 1, r = [t[0], t[1], t[2], t[3] * i];
|
|
1395
|
+
return e === "scatter" || e === "line+scatter" || e === "step+scatter" ? {
|
|
1396
|
+
color: r,
|
|
1397
|
+
opacity: i,
|
|
1398
|
+
pointSize: s.pointSize ?? 4,
|
|
1399
|
+
symbol: s.symbol ?? "circle"
|
|
1400
|
+
} : {
|
|
1401
|
+
color: r,
|
|
1402
|
+
opacity: i,
|
|
1403
|
+
lineWidth: s.lineWidth ?? 1
|
|
1404
|
+
};
|
|
1405
|
+
}
|
|
1406
|
+
class U {
|
|
1407
|
+
constructor(e) {
|
|
1408
|
+
c(this, "backend");
|
|
1409
|
+
c(this, "seriesBufferMap", /* @__PURE__ */ new Map());
|
|
1410
|
+
c(this, "seriesStepBufferMap", /* @__PURE__ */ new Map());
|
|
1411
|
+
c(this, "seriesTextureMap", /* @__PURE__ */ new Map());
|
|
1412
|
+
this.backend = e;
|
|
1413
|
+
}
|
|
1414
|
+
/**
|
|
1415
|
+
* Get buffer ID for a series
|
|
1416
|
+
*/
|
|
1417
|
+
getBufferId(e) {
|
|
1418
|
+
return this.seriesBufferMap.has(e) || this.seriesBufferMap.set(e, `series:${e}`), this.seriesBufferMap.get(e);
|
|
1419
|
+
}
|
|
1420
|
+
/**
|
|
1421
|
+
* Get step buffer ID for a series
|
|
1422
|
+
*/
|
|
1423
|
+
getStepBufferId(e) {
|
|
1424
|
+
return this.seriesStepBufferMap.has(e) || this.seriesStepBufferMap.set(e, `series:${e}:step`), this.seriesStepBufferMap.get(e);
|
|
1425
|
+
}
|
|
1426
|
+
/**
|
|
1427
|
+
* Get texture ID for a series
|
|
1428
|
+
*/
|
|
1429
|
+
getTextureId(e) {
|
|
1430
|
+
return this.seriesTextureMap.has(e) || this.seriesTextureMap.set(e, `texture:${e}`), this.seriesTextureMap.get(e);
|
|
1431
|
+
}
|
|
1432
|
+
/**
|
|
1433
|
+
* Update series data in GPU buffers
|
|
1434
|
+
*/
|
|
1435
|
+
updateSeries(e) {
|
|
1436
|
+
const t = this.getBufferId(e.id);
|
|
1437
|
+
if (this.backend.createOrUpdateBuffer(t, e.data, { usage: "vertex" }), e.stepData && (e.type === "step" || e.type === "step+scatter")) {
|
|
1438
|
+
const i = this.getStepBufferId(e.id);
|
|
1439
|
+
this.backend.createOrUpdateBuffer(i, e.stepData, { usage: "vertex" });
|
|
1440
|
+
}
|
|
1441
|
+
if (e.type === "heatmap" && e.colormapData) {
|
|
1442
|
+
const i = this.getTextureId(e.id);
|
|
1443
|
+
this.backend.createOrUpdateTexture1D(i, e.colormapData, {
|
|
1444
|
+
width: e.colormapData.length / 4
|
|
1445
|
+
});
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
/**
|
|
1449
|
+
* Remove a series from GPU resources
|
|
1450
|
+
*/
|
|
1451
|
+
removeSeries(e) {
|
|
1452
|
+
const t = this.seriesBufferMap.get(e);
|
|
1453
|
+
t && (this.backend.deleteBuffer(t), this.seriesBufferMap.delete(e));
|
|
1454
|
+
const i = this.seriesStepBufferMap.get(e);
|
|
1455
|
+
i && (this.backend.deleteBuffer(i), this.seriesStepBufferMap.delete(e));
|
|
1456
|
+
const r = this.seriesTextureMap.get(e);
|
|
1457
|
+
r && (this.backend.deleteTexture(r), this.seriesTextureMap.delete(e));
|
|
1458
|
+
}
|
|
1459
|
+
/**
|
|
1460
|
+
* Build draw list from series array
|
|
1461
|
+
*/
|
|
1462
|
+
buildDrawList(e) {
|
|
1463
|
+
const t = [];
|
|
1464
|
+
for (const i of e) {
|
|
1465
|
+
this.updateSeries(i);
|
|
1466
|
+
const r = this.getBufferId(i.id), a = i.data.length / 2, n = {
|
|
1467
|
+
id: i.id,
|
|
1468
|
+
kind: i.type,
|
|
1469
|
+
bufferId: r,
|
|
1470
|
+
count: i.type === "heatmap" ? i.data.length / 3 : a,
|
|
1471
|
+
visible: i.visible,
|
|
1472
|
+
style: me(i.style, i.type),
|
|
1473
|
+
yBounds: i.yBounds
|
|
1474
|
+
};
|
|
1475
|
+
i.stepData && (i.type === "step" || i.type === "step+scatter") && (n.stepBufferId = this.getStepBufferId(i.id), n.stepCount = i.stepData.length / 2), i.type === "heatmap" && (n.textureId = this.getTextureId(i.id), n.style = {
|
|
1476
|
+
zBounds: i.zBounds,
|
|
1477
|
+
colormap: i.colormap
|
|
1478
|
+
}), t.push(n);
|
|
1479
|
+
}
|
|
1480
|
+
return { items: t };
|
|
1481
|
+
}
|
|
1482
|
+
/**
|
|
1483
|
+
* Cleanup all resources
|
|
1484
|
+
*/
|
|
1485
|
+
destroy() {
|
|
1486
|
+
for (const e of this.seriesBufferMap.values())
|
|
1487
|
+
this.backend.deleteBuffer(e);
|
|
1488
|
+
for (const e of this.seriesStepBufferMap.values())
|
|
1489
|
+
this.backend.deleteBuffer(e);
|
|
1490
|
+
for (const e of this.seriesTextureMap.values())
|
|
1491
|
+
this.backend.deleteTexture(e);
|
|
1492
|
+
this.seriesBufferMap.clear(), this.seriesStepBufferMap.clear(), this.seriesTextureMap.clear();
|
|
1493
|
+
}
|
|
1494
|
+
}
|
|
1495
|
+
class ge {
|
|
1496
|
+
constructor(e, t = {}) {
|
|
1497
|
+
c(this, "canvas");
|
|
1498
|
+
c(this, "options");
|
|
1499
|
+
c(this, "backend", null);
|
|
1500
|
+
c(this, "adapter", null);
|
|
1501
|
+
c(this, "dpr");
|
|
1502
|
+
c(this, "isInitialized", !1);
|
|
1503
|
+
c(this, "backendType", null);
|
|
1504
|
+
// Buffer management
|
|
1505
|
+
c(this, "bufferDataMap", /* @__PURE__ */ new Map());
|
|
1506
|
+
c(this, "stepBufferDataMap", /* @__PURE__ */ new Map());
|
|
1507
|
+
c(this, "colormapDataMap", /* @__PURE__ */ new Map());
|
|
1508
|
+
this.canvas = e, this.options = t, this.dpr = window.devicePixelRatio || 1;
|
|
1509
|
+
}
|
|
1510
|
+
/**
|
|
1511
|
+
* Initialize the renderer
|
|
1512
|
+
*/
|
|
1513
|
+
async init() {
|
|
1514
|
+
const e = this.options.backend ?? "auto";
|
|
1515
|
+
if ((e === "webgpu" || e === "auto") && w.isSupported())
|
|
1516
|
+
try {
|
|
1517
|
+
const t = new w(this.canvas, {
|
|
1518
|
+
powerPreference: this.options.powerPreference ?? "high-performance"
|
|
1519
|
+
});
|
|
1520
|
+
return await t.init(), this.backend = t, this.backendType = "webgpu", this.adapter = new U(t), this.isInitialized = !0, this.resize(), !0;
|
|
1521
|
+
} catch (t) {
|
|
1522
|
+
console.warn("[GpuRenderer] WebGPU init failed:", t);
|
|
1523
|
+
}
|
|
1524
|
+
if ((e === "webgl" || e === "auto") && P.isSupported())
|
|
1525
|
+
try {
|
|
1526
|
+
const t = new P(this.canvas, {
|
|
1527
|
+
powerPreference: this.options.powerPreference ?? "high-performance"
|
|
1528
|
+
});
|
|
1529
|
+
return await t.init(), this.backend = t, this.backendType = "webgl", this.adapter = new U(t), this.isInitialized = !0, this.resize(), console.info("[GpuRenderer] Using WebGL backend (WebGPU not available)"), !0;
|
|
1530
|
+
} catch (t) {
|
|
1531
|
+
console.warn("[GpuRenderer] WebGL init failed:", t);
|
|
1532
|
+
}
|
|
1533
|
+
return !1;
|
|
1534
|
+
}
|
|
1535
|
+
/**
|
|
1536
|
+
* Check if renderer is available
|
|
1537
|
+
*/
|
|
1538
|
+
get available() {
|
|
1539
|
+
return this.isInitialized;
|
|
1540
|
+
}
|
|
1541
|
+
/**
|
|
1542
|
+
* Get the active backend type
|
|
1543
|
+
*/
|
|
1544
|
+
get activeBackend() {
|
|
1545
|
+
return this.backendType;
|
|
1546
|
+
}
|
|
1547
|
+
/**
|
|
1548
|
+
* Set device pixel ratio
|
|
1549
|
+
*/
|
|
1550
|
+
setDPR(e) {
|
|
1551
|
+
this.dpr = e, this.resize();
|
|
1552
|
+
}
|
|
1553
|
+
/**
|
|
1554
|
+
* Check if vertex data exists for a buffer id
|
|
1555
|
+
*/
|
|
1556
|
+
hasBuffer(e) {
|
|
1557
|
+
return this.bufferDataMap.has(e) || this.stepBufferDataMap.has(e);
|
|
1558
|
+
}
|
|
1559
|
+
hasColormap(e) {
|
|
1560
|
+
return this.colormapDataMap.has(e);
|
|
1561
|
+
}
|
|
1562
|
+
/**
|
|
1563
|
+
* Render from NativeWebGLRenderer-compatible series descriptors (chart integration path).
|
|
1564
|
+
*/
|
|
1565
|
+
renderNativeSeries(e, t) {
|
|
1566
|
+
var n, o, u, l, f, d;
|
|
1567
|
+
if (!this.isInitialized || !this.backend) return;
|
|
1568
|
+
const i = [];
|
|
1569
|
+
for (const p of e) {
|
|
1570
|
+
if (!p.visible) continue;
|
|
1571
|
+
if (p.type === "boxplot") {
|
|
1572
|
+
this.appendBoxplotDraws(p, i);
|
|
1573
|
+
continue;
|
|
1574
|
+
}
|
|
1575
|
+
if (p.type === "waterfall") {
|
|
1576
|
+
this.appendWaterfallDraws(p, i);
|
|
1577
|
+
continue;
|
|
1578
|
+
}
|
|
1579
|
+
if (p.count === 0) continue;
|
|
1580
|
+
const h = this.bufferDataMap.get(p.id);
|
|
1581
|
+
if (!h) continue;
|
|
1582
|
+
this.backend.createOrUpdateBuffer(p.id, h, { usage: "vertex" });
|
|
1583
|
+
const v = B(
|
|
1584
|
+
typeof ((n = p.style) == null ? void 0 : n.color) == "string" ? p.style.color : "#00f2ff"
|
|
1585
|
+
), y = ((o = p.style) == null ? void 0 : o.opacity) ?? 1, m = [
|
|
1586
|
+
v[0],
|
|
1587
|
+
v[1],
|
|
1588
|
+
v[2],
|
|
1589
|
+
v[3] * y
|
|
1590
|
+
], g = this.mapNativeDrawKind(p.type), b = {
|
|
1591
|
+
id: p.id,
|
|
1592
|
+
kind: g,
|
|
1593
|
+
bufferId: p.id,
|
|
1594
|
+
count: p.count,
|
|
1595
|
+
visible: !0,
|
|
1596
|
+
yBounds: p.yBounds
|
|
1597
|
+
};
|
|
1598
|
+
if (g === "scatter" || g === "line+scatter" || g === "step+scatter" || g === "points")
|
|
1599
|
+
b.style = {
|
|
1600
|
+
color: m,
|
|
1601
|
+
opacity: y,
|
|
1602
|
+
pointSize: ((u = p.style) == null ? void 0 : u.pointSize) ?? 4,
|
|
1603
|
+
symbol: ((l = p.style) == null ? void 0 : l.symbol) ?? "circle"
|
|
1604
|
+
};
|
|
1605
|
+
else if (g === "heatmap") {
|
|
1606
|
+
b.textureId = `${p.id}_colormap`, b.style = {
|
|
1607
|
+
zBounds: p.zBounds,
|
|
1608
|
+
colormap: p.colormap
|
|
1609
|
+
};
|
|
1610
|
+
const x = this.colormapDataMap.get(`${p.id}_colormap`);
|
|
1611
|
+
x && this.backend.createOrUpdateTexture1D(b.textureId, x, {
|
|
1612
|
+
width: x.length / 4
|
|
1613
|
+
});
|
|
1614
|
+
} else
|
|
1615
|
+
b.style = {
|
|
1616
|
+
color: m,
|
|
1617
|
+
opacity: y,
|
|
1618
|
+
lineWidth: ((f = p.style) == null ? void 0 : f.width) ?? ((d = p.style) == null ? void 0 : d.lineWidth) ?? 1
|
|
1619
|
+
};
|
|
1620
|
+
if ((g === "step" || g === "step+scatter") && p.stepBuffer && p.stepCount) {
|
|
1621
|
+
const x = `${p.id}_step`, _ = this.stepBufferDataMap.get(x);
|
|
1622
|
+
_ && (this.backend.createOrUpdateBuffer(x, _, { usage: "vertex" }), b.stepBufferId = x, b.stepCount = p.stepCount);
|
|
1623
|
+
}
|
|
1624
|
+
i.push(b);
|
|
1625
|
+
}
|
|
1626
|
+
const r = t.dpr ?? this.dpr, a = {
|
|
1627
|
+
viewport: {
|
|
1628
|
+
width: this.canvas.clientWidth,
|
|
1629
|
+
height: this.canvas.clientHeight,
|
|
1630
|
+
dpr: r
|
|
1631
|
+
},
|
|
1632
|
+
clearColor: t.backgroundColor ?? [0.1, 0.1, 0.18, 1],
|
|
1633
|
+
bounds: t.bounds,
|
|
1634
|
+
plotArea: t.plotArea
|
|
1635
|
+
};
|
|
1636
|
+
"renderWithBounds" in this.backend ? this.backend.renderWithBounds({ items: i }, a, t.bounds) : this.backend.render({ items: i }, a);
|
|
1637
|
+
}
|
|
1638
|
+
appendBoxplotDraws(e, t) {
|
|
1639
|
+
var n, o, u;
|
|
1640
|
+
const i = this.seriesColor(e.style), r = { color: i, opacity: ((n = e.style) == null ? void 0 : n.opacity) ?? 1, lineWidth: ((o = e.style) == null ? void 0 : o.width) ?? 1 }, a = { color: i, opacity: (((u = e.style) == null ? void 0 : u.opacity) ?? 1) * 0.85, lineWidth: 1 };
|
|
1641
|
+
this.pushDraw(t, `${e.id}_box_faces`, "bar", e.boxCount ?? 0, a, e.yBounds), this.pushDraw(t, `${e.id}_box_lines`, "line", e.boxLinesCount ?? 0, r, e.yBounds);
|
|
1642
|
+
}
|
|
1643
|
+
appendWaterfallDraws(e, t) {
|
|
1644
|
+
const i = e.style ?? {}, r = this.colorFromHex(i.positiveColor ?? "#22c55e", 0.9), a = this.colorFromHex(i.negativeColor ?? "#ef4444", 0.9), n = this.colorFromHex(i.subtotalColor ?? "#3b82f6", 0.9), o = this.colorFromHex(i.connectorColor ?? "#64748b", 0.6);
|
|
1645
|
+
this.pushDraw(t, `${e.id}_wf_positive`, "bar", e.wfPositiveCount ?? 0, { color: r, opacity: 0.9, lineWidth: 1 }, e.yBounds), this.pushDraw(t, `${e.id}_wf_negative`, "bar", e.wfNegativeCount ?? 0, { color: a, opacity: 0.9, lineWidth: 1 }, e.yBounds), this.pushDraw(t, `${e.id}_wf_subtotal`, "bar", e.wfSubtotalCount ?? 0, { color: n, opacity: 0.9, lineWidth: 1 }, e.yBounds), i.showConnectors !== !1 && this.pushDraw(t, `${e.id}_wf_connectors`, "line", e.wfConnectorCount ?? 0, { color: o, opacity: 0.6, lineWidth: 1 }, e.yBounds);
|
|
1646
|
+
}
|
|
1647
|
+
pushDraw(e, t, i, r, a, n) {
|
|
1648
|
+
if (r <= 0) return;
|
|
1649
|
+
const o = this.bufferDataMap.get(t);
|
|
1650
|
+
!o || !this.backend || (this.backend.createOrUpdateBuffer(t, o, { usage: "vertex" }), e.push({
|
|
1651
|
+
id: t,
|
|
1652
|
+
kind: i,
|
|
1653
|
+
bufferId: t,
|
|
1654
|
+
count: r,
|
|
1655
|
+
visible: !0,
|
|
1656
|
+
style: a,
|
|
1657
|
+
yBounds: n
|
|
1658
|
+
}));
|
|
1659
|
+
}
|
|
1660
|
+
seriesColor(e) {
|
|
1661
|
+
const t = B(typeof (e == null ? void 0 : e.color) == "string" ? e.color : "#00f2ff"), i = (e == null ? void 0 : e.opacity) ?? 1;
|
|
1662
|
+
return [t[0], t[1], t[2], t[3] * i];
|
|
1663
|
+
}
|
|
1664
|
+
colorFromHex(e, t) {
|
|
1665
|
+
const i = B(e);
|
|
1666
|
+
return [i[0], i[1], i[2], i[3] * t];
|
|
1667
|
+
}
|
|
1668
|
+
mapNativeDrawKind(e) {
|
|
1669
|
+
switch (e) {
|
|
1670
|
+
case "scatter":
|
|
1671
|
+
return "scatter";
|
|
1672
|
+
case "line+scatter":
|
|
1673
|
+
return "line+scatter";
|
|
1674
|
+
case "step":
|
|
1675
|
+
return "step";
|
|
1676
|
+
case "step+scatter":
|
|
1677
|
+
return "step+scatter";
|
|
1678
|
+
case "band":
|
|
1679
|
+
return "band";
|
|
1680
|
+
case "bar":
|
|
1681
|
+
return "bar";
|
|
1682
|
+
case "heatmap":
|
|
1683
|
+
return "heatmap";
|
|
1684
|
+
default:
|
|
1685
|
+
return "line";
|
|
1686
|
+
}
|
|
1687
|
+
}
|
|
1688
|
+
/**
|
|
1689
|
+
* Create or update a buffer
|
|
1690
|
+
*/
|
|
1691
|
+
createBuffer(e, t) {
|
|
1692
|
+
this.bufferDataMap.set(e, t);
|
|
1693
|
+
}
|
|
1694
|
+
/**
|
|
1695
|
+
* Update a buffer partially
|
|
1696
|
+
*/
|
|
1697
|
+
updateBuffer(e, t, i) {
|
|
1698
|
+
const r = this.bufferDataMap.get(e);
|
|
1699
|
+
if (!r) return !1;
|
|
1700
|
+
const a = i / 4;
|
|
1701
|
+
return r.set(t, a), !0;
|
|
1702
|
+
}
|
|
1703
|
+
/**
|
|
1704
|
+
* Get a buffer's data
|
|
1705
|
+
*/
|
|
1706
|
+
getBuffer(e) {
|
|
1707
|
+
return this.bufferDataMap.get(e);
|
|
1708
|
+
}
|
|
1709
|
+
/**
|
|
1710
|
+
* Delete a buffer
|
|
1711
|
+
*/
|
|
1712
|
+
deleteBuffer(e) {
|
|
1713
|
+
this.bufferDataMap.delete(e), this.backend && this.backend.deleteBuffer(e);
|
|
1714
|
+
}
|
|
1715
|
+
/**
|
|
1716
|
+
* Create or update a step buffer
|
|
1717
|
+
*/
|
|
1718
|
+
createStepBuffer(e, t) {
|
|
1719
|
+
this.stepBufferDataMap.set(e, t);
|
|
1720
|
+
}
|
|
1721
|
+
/**
|
|
1722
|
+
* Create colormap texture
|
|
1723
|
+
*/
|
|
1724
|
+
createColormapTexture(e, t) {
|
|
1725
|
+
this.colormapDataMap.set(e, t), this.backend && this.backend.createOrUpdateTexture1D(e, t, {
|
|
1726
|
+
width: t.length / 4
|
|
1727
|
+
});
|
|
1728
|
+
}
|
|
1729
|
+
/**
|
|
1730
|
+
* Render a frame
|
|
1731
|
+
*/
|
|
1732
|
+
render(e, t) {
|
|
1733
|
+
if (!this.isInitialized || !this.backend) return;
|
|
1734
|
+
const i = this.buildDrawList(e), r = {
|
|
1735
|
+
viewport: {
|
|
1736
|
+
width: this.canvas.clientWidth,
|
|
1737
|
+
height: this.canvas.clientHeight,
|
|
1738
|
+
dpr: this.dpr
|
|
1739
|
+
},
|
|
1740
|
+
clearColor: t.backgroundColor ?? [0.1, 0.1, 0.18, 1],
|
|
1741
|
+
bounds: t.bounds,
|
|
1742
|
+
plotArea: t.plotArea
|
|
1743
|
+
};
|
|
1744
|
+
"renderWithBounds" in this.backend ? this.backend.renderWithBounds(i, r, t.bounds) : this.backend.render(i, r);
|
|
1745
|
+
}
|
|
1746
|
+
/**
|
|
1747
|
+
* Build draw list from series data
|
|
1748
|
+
*/
|
|
1749
|
+
buildDrawList(e) {
|
|
1750
|
+
const t = [];
|
|
1751
|
+
for (const i of e) {
|
|
1752
|
+
const r = this.bufferDataMap.get(i.id);
|
|
1753
|
+
if (!r) continue;
|
|
1754
|
+
this.backend.createOrUpdateBuffer(i.id, r, { usage: "vertex" });
|
|
1755
|
+
const a = i.type === "heatmap" ? r.length / 3 : r.length / 2, n = B(i.style.color), o = i.style.opacity ?? 1, u = [
|
|
1756
|
+
n[0],
|
|
1757
|
+
n[1],
|
|
1758
|
+
n[2],
|
|
1759
|
+
n[3] * o
|
|
1760
|
+
], l = {
|
|
1761
|
+
id: i.id,
|
|
1762
|
+
kind: i.type,
|
|
1763
|
+
bufferId: i.id,
|
|
1764
|
+
count: a,
|
|
1765
|
+
visible: i.visible,
|
|
1766
|
+
yBounds: i.yBounds
|
|
1767
|
+
};
|
|
1768
|
+
if (i.type === "scatter" || i.type === "line+scatter" || i.type === "step+scatter")
|
|
1769
|
+
l.style = {
|
|
1770
|
+
color: u,
|
|
1771
|
+
opacity: o,
|
|
1772
|
+
pointSize: i.style.pointSize ?? 4,
|
|
1773
|
+
symbol: i.style.symbol ?? "circle"
|
|
1774
|
+
};
|
|
1775
|
+
else if (i.type === "heatmap") {
|
|
1776
|
+
l.textureId = `colormap:${i.id}`, l.style = {
|
|
1777
|
+
zBounds: i.zBounds,
|
|
1778
|
+
colormap: i.colormap
|
|
1779
|
+
};
|
|
1780
|
+
const f = this.colormapDataMap.get(i.id);
|
|
1781
|
+
f && this.backend.createOrUpdateTexture1D(l.textureId, f, {
|
|
1782
|
+
width: f.length / 4
|
|
1783
|
+
});
|
|
1784
|
+
} else
|
|
1785
|
+
l.style = {
|
|
1786
|
+
color: u,
|
|
1787
|
+
opacity: o,
|
|
1788
|
+
lineWidth: i.style.lineWidth ?? 1
|
|
1789
|
+
};
|
|
1790
|
+
if ((i.type === "step" || i.type === "step+scatter") && this.stepBufferDataMap.has(i.id)) {
|
|
1791
|
+
const f = this.stepBufferDataMap.get(i.id), d = `${i.id}:step`;
|
|
1792
|
+
this.backend.createOrUpdateBuffer(d, f, { usage: "vertex" }), l.stepBufferId = d, l.stepCount = f.length / 2;
|
|
1793
|
+
}
|
|
1794
|
+
t.push(l);
|
|
1795
|
+
}
|
|
1796
|
+
return { items: t };
|
|
1797
|
+
}
|
|
1798
|
+
/**
|
|
1799
|
+
* Handle canvas resize
|
|
1800
|
+
*/
|
|
1801
|
+
resize() {
|
|
1802
|
+
if (!this.backend) return;
|
|
1803
|
+
const e = this.canvas.getBoundingClientRect();
|
|
1804
|
+
this.backend.setViewport({
|
|
1805
|
+
width: e.width,
|
|
1806
|
+
height: e.height,
|
|
1807
|
+
dpr: this.dpr
|
|
1808
|
+
});
|
|
1809
|
+
}
|
|
1810
|
+
/**
|
|
1811
|
+
* Get renderer limits
|
|
1812
|
+
*/
|
|
1813
|
+
getLimits() {
|
|
1814
|
+
return {
|
|
1815
|
+
backend: this.backendType,
|
|
1816
|
+
available: this.isInitialized
|
|
1817
|
+
};
|
|
1818
|
+
}
|
|
1819
|
+
/**
|
|
1820
|
+
* Destroy the renderer
|
|
1821
|
+
*/
|
|
1822
|
+
destroy() {
|
|
1823
|
+
var e, t;
|
|
1824
|
+
(e = this.adapter) == null || e.destroy(), (t = this.backend) == null || t.destroy(), this.bufferDataMap.clear(), this.stepBufferDataMap.clear(), this.colormapDataMap.clear(), this.adapter = null, this.backend = null, this.isInitialized = !1, this.backendType = null;
|
|
1825
|
+
}
|
|
1826
|
+
}
|
|
1827
|
+
async function Be(s, e) {
|
|
1828
|
+
const t = new ge(s, e);
|
|
1829
|
+
return await t.init() ? t : null;
|
|
1830
|
+
}
|
|
1831
|
+
class Se {
|
|
6
1832
|
constructor() {
|
|
7
|
-
|
|
1833
|
+
c(this, "entries", /* @__PURE__ */ new Map());
|
|
8
1834
|
}
|
|
9
1835
|
getInfo(e) {
|
|
10
1836
|
return this.entries.get(e);
|
|
@@ -19,9 +1845,9 @@ class z {
|
|
|
19
1845
|
return this.entries.size;
|
|
20
1846
|
}
|
|
21
1847
|
}
|
|
22
|
-
class
|
|
1848
|
+
class Te {
|
|
23
1849
|
constructor() {
|
|
24
|
-
|
|
1850
|
+
c(this, "entries", /* @__PURE__ */ new Map());
|
|
25
1851
|
}
|
|
26
1852
|
getInfo(e) {
|
|
27
1853
|
return this.entries.get(e);
|
|
@@ -36,26 +1862,26 @@ class R {
|
|
|
36
1862
|
return this.entries.size;
|
|
37
1863
|
}
|
|
38
1864
|
}
|
|
39
|
-
function
|
|
40
|
-
return
|
|
1865
|
+
function M(s) {
|
|
1866
|
+
return s.variant ? `${s.kind}:${s.variant}` : s.kind;
|
|
41
1867
|
}
|
|
42
|
-
class
|
|
1868
|
+
class Me {
|
|
43
1869
|
constructor() {
|
|
44
|
-
|
|
1870
|
+
c(this, "pipelines", /* @__PURE__ */ new Map());
|
|
45
1871
|
}
|
|
46
|
-
getOrCreate(e,
|
|
47
|
-
const
|
|
48
|
-
if (!this.pipelines.has(
|
|
49
|
-
const
|
|
50
|
-
this.pipelines.set(
|
|
1872
|
+
getOrCreate(e, t) {
|
|
1873
|
+
const i = M(e);
|
|
1874
|
+
if (!this.pipelines.has(i)) {
|
|
1875
|
+
const r = t();
|
|
1876
|
+
this.pipelines.set(i, r);
|
|
51
1877
|
}
|
|
52
|
-
return this.pipelines.get(
|
|
1878
|
+
return this.pipelines.get(i);
|
|
53
1879
|
}
|
|
54
1880
|
has(e) {
|
|
55
|
-
return this.pipelines.has(
|
|
1881
|
+
return this.pipelines.has(M(e));
|
|
56
1882
|
}
|
|
57
1883
|
delete(e) {
|
|
58
|
-
this.pipelines.delete(
|
|
1884
|
+
this.pipelines.delete(M(e));
|
|
59
1885
|
}
|
|
60
1886
|
get size() {
|
|
61
1887
|
return this.pipelines.size;
|
|
@@ -64,63 +1890,62 @@ class E {
|
|
|
64
1890
|
this.pipelines.clear();
|
|
65
1891
|
}
|
|
66
1892
|
}
|
|
67
|
-
class
|
|
1893
|
+
class ke {
|
|
68
1894
|
constructor(e) {
|
|
69
|
-
|
|
70
|
-
|
|
1895
|
+
c(this, "canvas");
|
|
1896
|
+
c(this, "results", []);
|
|
71
1897
|
this.canvas = e ?? document.createElement("canvas"), e || (this.canvas.width = 800, this.canvas.height = 600);
|
|
72
1898
|
}
|
|
73
1899
|
/**
|
|
74
1900
|
* Generate random line data
|
|
75
1901
|
*/
|
|
76
1902
|
generateLineData(e) {
|
|
77
|
-
const
|
|
78
|
-
for (let
|
|
79
|
-
i
|
|
80
|
-
return
|
|
1903
|
+
const t = new Float32Array(e * 2);
|
|
1904
|
+
for (let i = 0; i < e; i++)
|
|
1905
|
+
t[i * 2] = i / (e - 1), t[i * 2 + 1] = Math.random();
|
|
1906
|
+
return t;
|
|
81
1907
|
}
|
|
82
1908
|
/**
|
|
83
1909
|
* Measure frame times
|
|
84
1910
|
*/
|
|
85
|
-
async measureFrameTimes(e,
|
|
86
|
-
const
|
|
87
|
-
let
|
|
1911
|
+
async measureFrameTimes(e, t, i = 10, r) {
|
|
1912
|
+
const a = [];
|
|
1913
|
+
let n = performance.now(), o = 0;
|
|
88
1914
|
const u = performance.now();
|
|
89
1915
|
return new Promise((l) => {
|
|
90
|
-
const
|
|
91
|
-
const
|
|
92
|
-
if (
|
|
93
|
-
l({ frameTimes:
|
|
1916
|
+
const f = () => {
|
|
1917
|
+
const d = performance.now(), p = d - u;
|
|
1918
|
+
if (p >= t + i * 16) {
|
|
1919
|
+
l({ frameTimes: a, duration: p - i * 16 });
|
|
94
1920
|
return;
|
|
95
1921
|
}
|
|
96
|
-
if (e(), o >=
|
|
97
|
-
if (
|
|
98
|
-
const
|
|
99
|
-
|
|
1922
|
+
if (e(), o >= i) {
|
|
1923
|
+
if (a.push(d - n), r) {
|
|
1924
|
+
const h = Math.min(1, p / t);
|
|
1925
|
+
r(h);
|
|
100
1926
|
}
|
|
101
1927
|
} else
|
|
102
1928
|
o++;
|
|
103
|
-
|
|
1929
|
+
n = d, requestAnimationFrame(f);
|
|
104
1930
|
};
|
|
105
|
-
requestAnimationFrame(
|
|
1931
|
+
requestAnimationFrame(f);
|
|
106
1932
|
});
|
|
107
1933
|
}
|
|
108
1934
|
/**
|
|
109
1935
|
* Run benchmark with WebGPU backend
|
|
110
1936
|
*/
|
|
111
1937
|
async benchmarkWebGPU(e = {}) {
|
|
112
|
-
|
|
113
|
-
if (!i.isSupported())
|
|
1938
|
+
if (!w.isSupported())
|
|
114
1939
|
return console.warn("[Benchmark] WebGPU not supported"), null;
|
|
115
|
-
const t = e.pointCount ?? 1e5,
|
|
116
|
-
await
|
|
1940
|
+
const t = e.pointCount ?? 1e5, i = e.durationMs ?? 5e3, r = e.warmupFrames ?? 30, a = new w(this.canvas);
|
|
1941
|
+
await a.init(), a.setViewport({
|
|
117
1942
|
width: this.canvas.width,
|
|
118
1943
|
height: this.canvas.height,
|
|
119
1944
|
dpr: 1
|
|
120
1945
|
});
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
const
|
|
1946
|
+
const n = this.generateLineData(t);
|
|
1947
|
+
a.createOrUpdateBuffer("bench-line", n, { usage: "vertex" });
|
|
1948
|
+
const o = {
|
|
124
1949
|
items: [{
|
|
125
1950
|
id: "bench-line",
|
|
126
1951
|
kind: "line",
|
|
@@ -129,37 +1954,36 @@ class F {
|
|
|
129
1954
|
visible: !0,
|
|
130
1955
|
style: { color: [0, 0.9, 1, 1] }
|
|
131
1956
|
}]
|
|
132
|
-
},
|
|
1957
|
+
}, u = {
|
|
133
1958
|
viewport: { width: this.canvas.width, height: this.canvas.height, dpr: 1 },
|
|
134
1959
|
clearColor: [0.1, 0.1, 0.15, 1]
|
|
135
|
-
},
|
|
136
|
-
|
|
137
|
-
}, { frameTimes:
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
1960
|
+
}, l = { xMin: 0, xMax: 1, yMin: 0, yMax: 1 }, f = () => {
|
|
1961
|
+
a.renderWithBounds(o, u, l);
|
|
1962
|
+
}, { frameTimes: d, duration: p } = await this.measureFrameTimes(
|
|
1963
|
+
f,
|
|
1964
|
+
i,
|
|
1965
|
+
r,
|
|
141
1966
|
e.onProgress
|
|
142
1967
|
);
|
|
143
|
-
|
|
144
|
-
const
|
|
145
|
-
return this.results.push(
|
|
1968
|
+
a.destroy();
|
|
1969
|
+
const h = this.calculateResult("webgpu", t, d, p);
|
|
1970
|
+
return this.results.push(h), h;
|
|
146
1971
|
}
|
|
147
1972
|
/**
|
|
148
1973
|
* Run benchmark with WebGL backend
|
|
149
1974
|
*/
|
|
150
1975
|
async benchmarkWebGL(e = {}) {
|
|
151
|
-
|
|
152
|
-
if (!i.isSupported())
|
|
1976
|
+
if (!P.isSupported())
|
|
153
1977
|
return console.warn("[Benchmark] WebGL not supported"), null;
|
|
154
|
-
const t = e.pointCount ?? 1e5,
|
|
155
|
-
await
|
|
1978
|
+
const t = e.pointCount ?? 1e5, i = e.durationMs ?? 5e3, r = e.warmupFrames ?? 30, a = new P(this.canvas);
|
|
1979
|
+
await a.init(), a.setViewport({
|
|
156
1980
|
width: this.canvas.width,
|
|
157
1981
|
height: this.canvas.height,
|
|
158
1982
|
dpr: 1
|
|
159
1983
|
});
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
const
|
|
1984
|
+
const n = this.generateLineData(t);
|
|
1985
|
+
a.createOrUpdateBuffer("bench-line", n, { usage: "vertex" });
|
|
1986
|
+
const o = {
|
|
163
1987
|
items: [{
|
|
164
1988
|
id: "bench-line",
|
|
165
1989
|
kind: "line",
|
|
@@ -168,55 +1992,55 @@ class F {
|
|
|
168
1992
|
visible: !0,
|
|
169
1993
|
style: { color: [0, 0.9, 1, 1] }
|
|
170
1994
|
}]
|
|
171
|
-
},
|
|
1995
|
+
}, u = {
|
|
172
1996
|
viewport: { width: this.canvas.width, height: this.canvas.height, dpr: 1 },
|
|
173
1997
|
clearColor: [0.1, 0.1, 0.15, 1]
|
|
174
|
-
},
|
|
175
|
-
|
|
176
|
-
}, { frameTimes:
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
1998
|
+
}, l = { xMin: 0, xMax: 1, yMin: 0, yMax: 1 }, f = () => {
|
|
1999
|
+
a.renderWithBounds(o, u, l);
|
|
2000
|
+
}, { frameTimes: d, duration: p } = await this.measureFrameTimes(
|
|
2001
|
+
f,
|
|
2002
|
+
i,
|
|
2003
|
+
r,
|
|
180
2004
|
e.onProgress
|
|
181
2005
|
);
|
|
182
|
-
|
|
183
|
-
const
|
|
184
|
-
return this.results.push(
|
|
2006
|
+
a.destroy();
|
|
2007
|
+
const h = this.calculateResult("webgl", t, d, p);
|
|
2008
|
+
return this.results.push(h), h;
|
|
185
2009
|
}
|
|
186
2010
|
/**
|
|
187
2011
|
* Calculate benchmark result from frame times
|
|
188
2012
|
*/
|
|
189
|
-
calculateResult(e,
|
|
190
|
-
if (
|
|
2013
|
+
calculateResult(e, t, i, r) {
|
|
2014
|
+
if (i.length === 0)
|
|
191
2015
|
return {
|
|
192
2016
|
backend: e,
|
|
193
|
-
pointCount:
|
|
2017
|
+
pointCount: t,
|
|
194
2018
|
fps: 0,
|
|
195
2019
|
avgFrameTime: 0,
|
|
196
2020
|
minFrameTime: 0,
|
|
197
2021
|
maxFrameTime: 0,
|
|
198
2022
|
totalFrames: 0,
|
|
199
|
-
duration:
|
|
2023
|
+
duration: r
|
|
200
2024
|
};
|
|
201
|
-
const
|
|
2025
|
+
const n = i.reduce((l, f) => l + f, 0) / i.length, o = Math.min(...i), u = Math.max(...i);
|
|
202
2026
|
return {
|
|
203
2027
|
backend: e,
|
|
204
|
-
pointCount:
|
|
205
|
-
fps: Math.round(1e3 /
|
|
206
|
-
avgFrameTime: Math.round(
|
|
2028
|
+
pointCount: t,
|
|
2029
|
+
fps: Math.round(1e3 / n),
|
|
2030
|
+
avgFrameTime: Math.round(n * 100) / 100,
|
|
207
2031
|
minFrameTime: Math.round(o * 100) / 100,
|
|
208
2032
|
maxFrameTime: Math.round(u * 100) / 100,
|
|
209
|
-
totalFrames:
|
|
210
|
-
duration: Math.round(
|
|
2033
|
+
totalFrames: i.length,
|
|
2034
|
+
duration: Math.round(r)
|
|
211
2035
|
};
|
|
212
2036
|
}
|
|
213
2037
|
/**
|
|
214
2038
|
* Run comparative benchmark
|
|
215
2039
|
*/
|
|
216
2040
|
async runComparison(e = {}) {
|
|
217
|
-
const
|
|
218
|
-
let
|
|
219
|
-
return
|
|
2041
|
+
const t = await this.benchmarkWebGPU(e), i = await this.benchmarkWebGL(e);
|
|
2042
|
+
let r = "unknown", a = 1;
|
|
2043
|
+
return t && i ? t.fps > i.fps * 1.05 ? (r = "webgpu", a = t.fps / i.fps) : i.fps > t.fps * 1.05 ? (r = "webgl", a = i.fps / t.fps) : (r = "tie", a = 1) : t ? r = "webgpu" : i && (r = "webgl"), { webgpu: t, webgl: i, winner: r, speedup: Math.round(a * 100) / 100 };
|
|
220
2044
|
}
|
|
221
2045
|
/**
|
|
222
2046
|
* Get all results
|
|
@@ -245,7 +2069,7 @@ class F {
|
|
|
245
2069
|
].join(" | ");
|
|
246
2070
|
}
|
|
247
2071
|
}
|
|
248
|
-
const
|
|
2072
|
+
const be = `
|
|
249
2073
|
struct StatsResult {
|
|
250
2074
|
min_val: f32,
|
|
251
2075
|
max_val: f32,
|
|
@@ -315,7 +2139,7 @@ fn main(
|
|
|
315
2139
|
result.count = local_count[0];
|
|
316
2140
|
}
|
|
317
2141
|
}
|
|
318
|
-
`,
|
|
2142
|
+
`, ve = `
|
|
319
2143
|
struct MinMax {
|
|
320
2144
|
min_x: f32,
|
|
321
2145
|
max_x: f32,
|
|
@@ -372,7 +2196,7 @@ fn main(
|
|
|
372
2196
|
result.max_y = local_max_y[0];
|
|
373
2197
|
}
|
|
374
2198
|
}
|
|
375
|
-
`,
|
|
2199
|
+
`, xe = `
|
|
376
2200
|
struct Params {
|
|
377
2201
|
input_count: u32,
|
|
378
2202
|
output_count: u32,
|
|
@@ -428,7 +2252,7 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
|
|
428
2252
|
output_points[out_base + 1u] = input_points[min_idx];
|
|
429
2253
|
}
|
|
430
2254
|
}
|
|
431
|
-
`,
|
|
2255
|
+
`, ye = `
|
|
432
2256
|
struct Params {
|
|
433
2257
|
data_count: u32,
|
|
434
2258
|
threshold: f32,
|
|
@@ -485,14 +2309,14 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
|
|
485
2309
|
}
|
|
486
2310
|
}
|
|
487
2311
|
`;
|
|
488
|
-
class
|
|
2312
|
+
class R {
|
|
489
2313
|
constructor(e = {}) {
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
2314
|
+
c(this, "device", null);
|
|
2315
|
+
c(this, "statsPipeline", null);
|
|
2316
|
+
c(this, "minmaxPipeline", null);
|
|
2317
|
+
c(this, "downsamplePipeline", null);
|
|
2318
|
+
c(this, "peaksPipeline", null);
|
|
2319
|
+
c(this, "isInitialized", !1);
|
|
496
2320
|
this.options = e;
|
|
497
2321
|
}
|
|
498
2322
|
/**
|
|
@@ -508,35 +2332,35 @@ class P {
|
|
|
508
2332
|
if (this.options.device)
|
|
509
2333
|
this.device = this.options.device;
|
|
510
2334
|
else {
|
|
511
|
-
if (!
|
|
2335
|
+
if (!R.isSupported())
|
|
512
2336
|
throw new Error("[GpuCompute] WebGPU not supported");
|
|
513
|
-
const
|
|
514
|
-
if (!
|
|
2337
|
+
const t = await globalThis.navigator.gpu.requestAdapter({ powerPreference: "high-performance" });
|
|
2338
|
+
if (!t)
|
|
515
2339
|
throw new Error("[GpuCompute] Failed to get adapter");
|
|
516
|
-
this.device = await
|
|
2340
|
+
this.device = await t.requestDevice();
|
|
517
2341
|
}
|
|
518
2342
|
await this.createPipelines(), this.isInitialized = !0;
|
|
519
2343
|
}
|
|
520
2344
|
async createPipelines() {
|
|
521
|
-
const e = this.device.createShaderModule({ code:
|
|
2345
|
+
const e = this.device.createShaderModule({ code: be });
|
|
522
2346
|
this.statsPipeline = await this.device.createComputePipelineAsync({
|
|
523
2347
|
layout: "auto",
|
|
524
2348
|
compute: { module: e, entryPoint: "main" }
|
|
525
2349
|
});
|
|
526
|
-
const
|
|
2350
|
+
const t = this.device.createShaderModule({ code: ve });
|
|
527
2351
|
this.minmaxPipeline = await this.device.createComputePipelineAsync({
|
|
528
2352
|
layout: "auto",
|
|
529
|
-
compute: { module:
|
|
2353
|
+
compute: { module: t, entryPoint: "main" }
|
|
530
2354
|
});
|
|
531
|
-
const
|
|
2355
|
+
const i = this.device.createShaderModule({ code: xe });
|
|
532
2356
|
this.downsamplePipeline = await this.device.createComputePipelineAsync({
|
|
533
2357
|
layout: "auto",
|
|
534
|
-
compute: { module:
|
|
2358
|
+
compute: { module: i, entryPoint: "main" }
|
|
535
2359
|
});
|
|
536
|
-
const
|
|
2360
|
+
const r = this.device.createShaderModule({ code: ye });
|
|
537
2361
|
this.peaksPipeline = await this.device.createComputePipelineAsync({
|
|
538
2362
|
layout: "auto",
|
|
539
|
-
compute: { module:
|
|
2363
|
+
compute: { module: r, entryPoint: "main" }
|
|
540
2364
|
});
|
|
541
2365
|
}
|
|
542
2366
|
/**
|
|
@@ -545,31 +2369,31 @@ class P {
|
|
|
545
2369
|
async calculateStats(e) {
|
|
546
2370
|
if (!this.isInitialized)
|
|
547
2371
|
throw new Error("[GpuCompute] Not initialized");
|
|
548
|
-
const
|
|
2372
|
+
const t = globalThis.GPUBufferUsage, i = this.device.createBuffer({
|
|
549
2373
|
size: e.byteLength,
|
|
550
|
-
usage:
|
|
2374
|
+
usage: t.STORAGE | t.COPY_DST
|
|
551
2375
|
});
|
|
552
|
-
this.device.queue.writeBuffer(
|
|
553
|
-
const
|
|
2376
|
+
this.device.queue.writeBuffer(i, 0, e);
|
|
2377
|
+
const r = this.device.createBuffer({
|
|
554
2378
|
size: 32,
|
|
555
|
-
usage:
|
|
556
|
-
}),
|
|
2379
|
+
usage: t.STORAGE | t.COPY_SRC
|
|
2380
|
+
}), a = this.device.createBuffer({
|
|
557
2381
|
size: 32,
|
|
558
|
-
usage:
|
|
559
|
-
}),
|
|
2382
|
+
usage: t.MAP_READ | t.COPY_DST
|
|
2383
|
+
}), n = this.device.createBindGroup({
|
|
560
2384
|
layout: this.statsPipeline.getBindGroupLayout(0),
|
|
561
2385
|
entries: [
|
|
562
|
-
{ binding: 0, resource: { buffer:
|
|
563
|
-
{ binding: 1, resource: { buffer:
|
|
2386
|
+
{ binding: 0, resource: { buffer: i } },
|
|
2387
|
+
{ binding: 1, resource: { buffer: r } }
|
|
564
2388
|
]
|
|
565
2389
|
}), o = this.device.createCommandEncoder(), u = o.beginComputePass();
|
|
566
|
-
u.setPipeline(this.statsPipeline), u.setBindGroup(0,
|
|
2390
|
+
u.setPipeline(this.statsPipeline), u.setBindGroup(0, n);
|
|
567
2391
|
const l = Math.ceil(e.length / 256);
|
|
568
|
-
u.dispatchWorkgroups(l), u.end(), o.copyBufferToBuffer(
|
|
569
|
-
const
|
|
570
|
-
|
|
571
|
-
const [
|
|
572
|
-
return { min:
|
|
2392
|
+
u.dispatchWorkgroups(l), u.end(), o.copyBufferToBuffer(r, 0, a, 0, 32), this.device.queue.submit([o.finish()]), await a.mapAsync(globalThis.GPUMapMode.READ);
|
|
2393
|
+
const f = new Float32Array(a.getMappedRange().slice(0));
|
|
2394
|
+
a.unmap(), i.destroy(), r.destroy(), a.destroy();
|
|
2395
|
+
const [d, p, h, v, y] = f, m = Math.floor(y), g = m > 0 ? h / m : 0, b = m > 1 ? (v - h * h / m) / (m - 1) : 0, x = Math.sqrt(Math.max(0, b));
|
|
2396
|
+
return { min: d, max: p, mean: g, std: x, count: m };
|
|
573
2397
|
}
|
|
574
2398
|
/**
|
|
575
2399
|
* Calculate bounds for 2D point data
|
|
@@ -577,124 +2401,124 @@ class P {
|
|
|
577
2401
|
async calculateBounds(e) {
|
|
578
2402
|
if (!this.isInitialized)
|
|
579
2403
|
throw new Error("[GpuCompute] Not initialized");
|
|
580
|
-
const
|
|
2404
|
+
const t = globalThis.GPUBufferUsage, i = this.device.createBuffer({
|
|
581
2405
|
size: e.byteLength,
|
|
582
|
-
usage:
|
|
2406
|
+
usage: t.STORAGE | t.COPY_DST
|
|
583
2407
|
});
|
|
584
|
-
this.device.queue.writeBuffer(
|
|
585
|
-
const
|
|
2408
|
+
this.device.queue.writeBuffer(i, 0, e);
|
|
2409
|
+
const r = this.device.createBuffer({
|
|
586
2410
|
size: 16,
|
|
587
|
-
usage:
|
|
588
|
-
}),
|
|
2411
|
+
usage: t.STORAGE | t.COPY_SRC
|
|
2412
|
+
}), a = this.device.createBuffer({
|
|
589
2413
|
size: 16,
|
|
590
|
-
usage:
|
|
591
|
-
}),
|
|
2414
|
+
usage: t.MAP_READ | t.COPY_DST
|
|
2415
|
+
}), n = this.device.createBindGroup({
|
|
592
2416
|
layout: this.minmaxPipeline.getBindGroupLayout(0),
|
|
593
2417
|
entries: [
|
|
594
|
-
{ binding: 0, resource: { buffer:
|
|
595
|
-
{ binding: 1, resource: { buffer:
|
|
2418
|
+
{ binding: 0, resource: { buffer: i } },
|
|
2419
|
+
{ binding: 1, resource: { buffer: r } }
|
|
596
2420
|
]
|
|
597
2421
|
}), o = this.device.createCommandEncoder(), u = o.beginComputePass();
|
|
598
|
-
u.setPipeline(this.minmaxPipeline), u.setBindGroup(0,
|
|
599
|
-
const l = e.length / 2,
|
|
600
|
-
u.dispatchWorkgroups(
|
|
601
|
-
const
|
|
602
|
-
return
|
|
603
|
-
xMin:
|
|
604
|
-
xMax:
|
|
605
|
-
yMin:
|
|
606
|
-
yMax:
|
|
2422
|
+
u.setPipeline(this.minmaxPipeline), u.setBindGroup(0, n);
|
|
2423
|
+
const l = e.length / 2, f = Math.ceil(l / 256);
|
|
2424
|
+
u.dispatchWorkgroups(f), u.end(), o.copyBufferToBuffer(r, 0, a, 0, 16), this.device.queue.submit([o.finish()]), await a.mapAsync(globalThis.GPUMapMode.READ);
|
|
2425
|
+
const d = new Float32Array(a.getMappedRange().slice(0));
|
|
2426
|
+
return a.unmap(), i.destroy(), r.destroy(), a.destroy(), {
|
|
2427
|
+
xMin: d[0],
|
|
2428
|
+
xMax: d[1],
|
|
2429
|
+
yMin: d[2],
|
|
2430
|
+
yMax: d[3]
|
|
607
2431
|
};
|
|
608
2432
|
}
|
|
609
2433
|
/**
|
|
610
2434
|
* Downsample point data using min-max algorithm
|
|
611
2435
|
*/
|
|
612
|
-
async downsample(e,
|
|
2436
|
+
async downsample(e, t) {
|
|
613
2437
|
if (!this.isInitialized)
|
|
614
2438
|
throw new Error("[GpuCompute] Not initialized");
|
|
615
|
-
const
|
|
616
|
-
if (
|
|
2439
|
+
const i = e.length / 2;
|
|
2440
|
+
if (i <= t)
|
|
617
2441
|
return e;
|
|
618
|
-
const
|
|
2442
|
+
const r = globalThis.GPUBufferUsage, a = Math.ceil(t / 2), n = Math.ceil(i / a), o = a * 2, u = new Uint32Array([i, o, n, 0]), l = this.device.createBuffer({
|
|
619
2443
|
size: 16,
|
|
620
|
-
usage:
|
|
2444
|
+
usage: r.UNIFORM | r.COPY_DST
|
|
621
2445
|
});
|
|
622
2446
|
this.device.queue.writeBuffer(l, 0, u);
|
|
623
|
-
const
|
|
2447
|
+
const f = this.device.createBuffer({
|
|
624
2448
|
size: e.byteLength,
|
|
625
|
-
usage:
|
|
2449
|
+
usage: r.STORAGE | r.COPY_DST
|
|
626
2450
|
});
|
|
627
|
-
this.device.queue.writeBuffer(
|
|
628
|
-
const
|
|
629
|
-
size:
|
|
630
|
-
usage:
|
|
631
|
-
}),
|
|
632
|
-
size:
|
|
633
|
-
usage:
|
|
634
|
-
}),
|
|
2451
|
+
this.device.queue.writeBuffer(f, 0, e);
|
|
2452
|
+
const d = o * 2 * 4, p = this.device.createBuffer({
|
|
2453
|
+
size: d,
|
|
2454
|
+
usage: r.STORAGE | r.COPY_SRC
|
|
2455
|
+
}), h = this.device.createBuffer({
|
|
2456
|
+
size: d,
|
|
2457
|
+
usage: r.MAP_READ | r.COPY_DST
|
|
2458
|
+
}), v = this.device.createBindGroup({
|
|
635
2459
|
layout: this.downsamplePipeline.getBindGroupLayout(0),
|
|
636
2460
|
entries: [
|
|
637
2461
|
{ binding: 0, resource: { buffer: l } },
|
|
638
|
-
{ binding: 1, resource: { buffer:
|
|
639
|
-
{ binding: 2, resource: { buffer:
|
|
2462
|
+
{ binding: 1, resource: { buffer: f } },
|
|
2463
|
+
{ binding: 2, resource: { buffer: p } }
|
|
640
2464
|
]
|
|
641
|
-
}),
|
|
642
|
-
|
|
643
|
-
const
|
|
644
|
-
|
|
645
|
-
const
|
|
646
|
-
return
|
|
2465
|
+
}), y = this.device.createCommandEncoder(), m = y.beginComputePass();
|
|
2466
|
+
m.setPipeline(this.downsamplePipeline), m.setBindGroup(0, v);
|
|
2467
|
+
const g = Math.ceil(a / 64);
|
|
2468
|
+
m.dispatchWorkgroups(g), m.end(), y.copyBufferToBuffer(p, 0, h, 0, d), this.device.queue.submit([y.finish()]), await h.mapAsync(globalThis.GPUMapMode.READ);
|
|
2469
|
+
const b = new Float32Array(h.getMappedRange().slice(0));
|
|
2470
|
+
return h.unmap(), l.destroy(), f.destroy(), p.destroy(), h.destroy(), b;
|
|
647
2471
|
}
|
|
648
2472
|
/**
|
|
649
2473
|
* Detect peaks in 1D data
|
|
650
2474
|
*/
|
|
651
|
-
async detectPeaks(e,
|
|
2475
|
+
async detectPeaks(e, t = {}) {
|
|
652
2476
|
if (!this.isInitialized)
|
|
653
2477
|
throw new Error("[GpuCompute] Not initialized");
|
|
654
|
-
const
|
|
2478
|
+
const i = t.threshold ?? 0, r = t.minDistance ?? 1, a = globalThis.GPUBufferUsage, n = new Float32Array([
|
|
655
2479
|
e.length,
|
|
656
|
-
|
|
657
|
-
|
|
2480
|
+
i,
|
|
2481
|
+
r,
|
|
658
2482
|
0
|
|
659
2483
|
// padding
|
|
660
2484
|
]), o = this.device.createBuffer({
|
|
661
2485
|
size: 16,
|
|
662
|
-
usage:
|
|
2486
|
+
usage: a.UNIFORM | a.COPY_DST
|
|
663
2487
|
});
|
|
664
|
-
this.device.queue.writeBuffer(o, 0, new Uint8Array(
|
|
2488
|
+
this.device.queue.writeBuffer(o, 0, new Uint8Array(n.buffer));
|
|
665
2489
|
const u = this.device.createBuffer({
|
|
666
2490
|
size: e.byteLength,
|
|
667
|
-
usage:
|
|
2491
|
+
usage: a.STORAGE | a.COPY_DST
|
|
668
2492
|
});
|
|
669
2493
|
this.device.queue.writeBuffer(u, 0, e);
|
|
670
|
-
const l = e.length * 16,
|
|
2494
|
+
const l = e.length * 16, f = this.device.createBuffer({
|
|
671
2495
|
size: l,
|
|
672
|
-
usage:
|
|
673
|
-
}),
|
|
2496
|
+
usage: a.STORAGE | a.COPY_SRC
|
|
2497
|
+
}), d = this.device.createBuffer({
|
|
674
2498
|
size: l,
|
|
675
|
-
usage:
|
|
676
|
-
}),
|
|
2499
|
+
usage: a.MAP_READ | a.COPY_DST
|
|
2500
|
+
}), p = this.device.createBindGroup({
|
|
677
2501
|
layout: this.peaksPipeline.getBindGroupLayout(0),
|
|
678
2502
|
entries: [
|
|
679
2503
|
{ binding: 0, resource: { buffer: o } },
|
|
680
2504
|
{ binding: 1, resource: { buffer: u } },
|
|
681
|
-
{ binding: 2, resource: { buffer:
|
|
2505
|
+
{ binding: 2, resource: { buffer: f } }
|
|
682
2506
|
]
|
|
683
|
-
}),
|
|
684
|
-
|
|
685
|
-
const
|
|
686
|
-
|
|
687
|
-
const
|
|
688
|
-
new Uint8Array(
|
|
689
|
-
const
|
|
690
|
-
for (let
|
|
691
|
-
const
|
|
692
|
-
|
|
693
|
-
index:
|
|
694
|
-
value:
|
|
2507
|
+
}), h = this.device.createCommandEncoder(), v = h.beginComputePass();
|
|
2508
|
+
v.setPipeline(this.peaksPipeline), v.setBindGroup(0, p);
|
|
2509
|
+
const y = Math.ceil(e.length / 64);
|
|
2510
|
+
v.dispatchWorkgroups(y), v.end(), h.copyBufferToBuffer(f, 0, d, 0, l), this.device.queue.submit([h.finish()]), await d.mapAsync(globalThis.GPUMapMode.READ);
|
|
2511
|
+
const m = new ArrayBuffer(l);
|
|
2512
|
+
new Uint8Array(m).set(new Uint8Array(d.getMappedRange())), d.unmap();
|
|
2513
|
+
const g = new DataView(m), b = [];
|
|
2514
|
+
for (let x = 0; x < e.length; x++) {
|
|
2515
|
+
const _ = x * 16;
|
|
2516
|
+
g.getUint32(_ + 8, !0) && b.push({
|
|
2517
|
+
index: g.getUint32(_, !0),
|
|
2518
|
+
value: g.getFloat32(_ + 4, !0)
|
|
695
2519
|
});
|
|
696
2520
|
}
|
|
697
|
-
return o.destroy(), u.destroy(),
|
|
2521
|
+
return o.destroy(), u.destroy(), f.destroy(), d.destroy(), b;
|
|
698
2522
|
}
|
|
699
2523
|
/**
|
|
700
2524
|
* Cleanup resources
|
|
@@ -703,16 +2527,16 @@ class P {
|
|
|
703
2527
|
this.device = null, this.statsPipeline = null, this.minmaxPipeline = null, this.downsamplePipeline = null, this.peaksPipeline = null, this.isInitialized = !1;
|
|
704
2528
|
}
|
|
705
2529
|
}
|
|
706
|
-
const
|
|
2530
|
+
const _e = {
|
|
707
2531
|
name: "velo-plot-gpu",
|
|
708
2532
|
version: "1.0.0",
|
|
709
2533
|
description: "GPU acceleration and WebGPU support for velo-plot",
|
|
710
2534
|
provides: ["renderer", "compute"],
|
|
711
2535
|
tags: ["gpu", "webgpu", "webgl2", "performance"]
|
|
712
2536
|
};
|
|
713
|
-
function
|
|
2537
|
+
function Ae(s = {}) {
|
|
714
2538
|
return {
|
|
715
|
-
manifest:
|
|
2539
|
+
manifest: _e,
|
|
716
2540
|
onInit(e) {
|
|
717
2541
|
},
|
|
718
2542
|
onDestroy(e) {
|
|
@@ -720,18 +2544,18 @@ function D(p = {}) {
|
|
|
720
2544
|
};
|
|
721
2545
|
}
|
|
722
2546
|
export {
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
2547
|
+
Se as BaseBufferStore,
|
|
2548
|
+
Te as BaseTextureStore,
|
|
2549
|
+
ke as GpuBenchmark,
|
|
2550
|
+
R as GpuCompute,
|
|
2551
|
+
ge as GpuRenderer,
|
|
2552
|
+
Me as PipelineCache,
|
|
2553
|
+
Ae as PluginGpu,
|
|
2554
|
+
U as SeriesAdapter,
|
|
2555
|
+
P as WebGLBackend,
|
|
2556
|
+
w as WebGPUBackend,
|
|
2557
|
+
Be as createGpuRenderer,
|
|
2558
|
+
Ae as default,
|
|
2559
|
+
B as parseColorToRGBA
|
|
736
2560
|
};
|
|
737
2561
|
//# sourceMappingURL=gpu.js.map
|