/
gpu-grid-aggregator.js
1059 lines (979 loc) · 33.2 KB
/
gpu-grid-aggregator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import GL from '@luma.gl/constants';
import {
Buffer,
Model,
Transform,
FEATURES,
hasFeatures,
isWebGL2,
readPixelsToBuffer,
fp64 as fp64Utils,
withParameters
} from '@luma.gl/core';
import {log} from '@deck.gl/core';
import {worldToPixels} from 'viewport-mercator-project';
const {fp64ifyMatrix4} = fp64Utils;
import {
DEFAULT_CHANGE_FLAGS,
DEFAULT_RUN_PARAMS,
MAX_32_BIT_FLOAT,
MIN_BLEND_EQUATION,
MAX_BLEND_EQUATION,
MAX_MIN_BLEND_EQUATION,
EQUATION_MAP,
ELEMENTCOUNT,
DEFAULT_WEIGHT_PARAMS,
IDENTITY_MATRIX,
PIXEL_SIZE,
WEIGHT_SIZE
} from './gpu-grid-aggregator-constants';
import {AGGREGATION_OPERATION} from '../aggregation-operation-utils';
import AGGREGATE_TO_GRID_VS from './aggregate-to-grid-vs.glsl';
import AGGREGATE_TO_GRID_VS_FP64 from './aggregate-to-grid-vs-64.glsl';
import AGGREGATE_TO_GRID_FS from './aggregate-to-grid-fs.glsl';
import AGGREGATE_ALL_VS_FP64 from './aggregate-all-vs-64.glsl';
import AGGREGATE_ALL_FS from './aggregate-all-fs.glsl';
import TRANSFORM_MEAN_VS from './transform-mean-vs.glsl';
import {getFloatTexture, getFramebuffer, getFloatArray} from './gpu-grid-aggregator-utils.js';
const BUFFER_NAMES = ['aggregationBuffer', 'maxMinBuffer', 'minBuffer', 'maxBuffer'];
const ARRAY_BUFFER_MAP = {
maxData: 'maxBuffer',
minData: 'minBuffer',
maxMinData: 'maxMinBuffer'
};
export default class GPUGridAggregator {
// Decode and return aggregation data of given pixel.
static getAggregationData({aggregationData, maxData, minData, maxMinData, pixelIndex}) {
const index = pixelIndex * PIXEL_SIZE;
const results = {};
if (aggregationData) {
results.cellCount = aggregationData[index + 3];
results.cellWeight = aggregationData[index];
}
if (maxMinData) {
results.maxCellWieght = maxMinData[0];
results.minCellWeight = maxMinData[3];
} else {
if (maxData) {
results.maxCellWieght = maxData[0];
results.totalCount = maxData[3];
}
if (minData) {
results.minCellWeight = minData[0];
results.totalCount = maxData[3];
}
}
return results;
}
// Decodes and retuns counts and weights of all cells
static getCellData({countsData, size = 1}) {
const numCells = countsData.length / 4;
const cellWeights = new Float32Array(numCells * size);
const cellCounts = new Uint32Array(numCells);
for (let i = 0; i < numCells; i++) {
// weights in RGB channels
for (let sizeIndex = 0; sizeIndex < size; sizeIndex++) {
cellWeights[i * size + sizeIndex] = countsData[i * 4 + sizeIndex];
}
// count in Alpha channel
cellCounts[i] = countsData[i * 4 + 3];
}
return {cellCounts, cellWeights};
}
static isSupported(gl) {
return (
isWebGL2(gl) &&
hasFeatures(
gl,
FEATURES.BLEND_EQUATION_MINMAX,
FEATURES.COLOR_ATTACHMENT_RGBA32F,
FEATURES.TEXTURE_FLOAT
)
);
}
// DEBUG ONLY
// static logData({aggregationBuffer, minBuffer, maxBuffer, maxMinBuffer, limit = 10}) {
// if (aggregationBuffer) {
// console.log('Aggregation Data:');
// const agrData = aggregationBuffer.getData();
// for (let index = 0; index < agrData.length && limit > 0; index += 4) {
// if (agrData[index + 3] > 0) {
// console.log(
// `index: ${index} weights: ${agrData[index]} ${agrData[index + 1]} ${
// agrData[index + 2]
// } count: ${agrData[index + 3]}`
// );
// limit--;
// }
// }
// }
// const obj = {minBuffer, maxBuffer, maxMinBuffer};
// for (const key in obj) {
// if (obj[key]) {
// const data = obj[key].getData();
// console.log(`${key} data : R: ${data[0]} G: ${data[1]} B: ${data[2]} A: ${data[3]}`);
// }
// }
// }
constructor(gl, opts = {}) {
this.id = opts.id || 'gpu-grid-aggregator';
this.shaderCache = opts.shaderCache || null;
this.gl = gl;
this.state = {
// cache weights and position data to process when data is not changed
weights: null,
gridPositions: null,
positionsBuffer: null,
positions64xyLowBuffer: null,
vertexCount: 0,
// flags/variables that affect the aggregation
fp64: null,
useGPU: null,
numCol: 0,
numRow: 0,
windowSize: null,
cellSize: null,
// per weight GPU resources
weightAttributes: {},
textures: {},
meanTextures: {},
buffers: {},
framebuffers: {},
maxMinFramebuffers: {},
minFramebuffers: {},
maxFramebuffers: {},
equations: {},
// common resources to be deleted
resources: {},
// results
results: {}
};
this._hasGPUSupport =
isWebGL2(gl) && // gl_InstanceID usage in min/max calculation shaders
hasFeatures(
this.gl,
FEATURES.BLEND_EQUATION_MINMAX, // set min/max blend modes
FEATURES.COLOR_ATTACHMENT_RGBA32F, // render to float texture
FEATURES.TEXTURE_FLOAT // sample from a float texture
);
}
// Delete owned resources.
/* eslint no-unused-expressions: ["error", { "allowShortCircuit": true }] */
delete() {
const {gridAggregationModel, allAggregationModel, meanTransform} = this;
const {
positionsBuffer,
positions64xyLowBuffer,
textures,
framebuffers,
maxMinFramebuffers,
minFramebuffers,
maxFramebuffers,
meanTextures,
resources
} = this.state;
gridAggregationModel && gridAggregationModel.delete();
allAggregationModel && allAggregationModel.delete();
meanTransform && meanTransform.delete();
positionsBuffer && positionsBuffer.delete();
positions64xyLowBuffer && positions64xyLowBuffer.delete();
this.deleteResources([
framebuffers,
textures,
maxMinFramebuffers,
minFramebuffers,
maxFramebuffers,
meanTextures,
resources
]);
}
// Perform aggregation and retun the results
run(opts = {}) {
// reset results
this.setState({results: {}});
const aggregationParams = this.getAggregationParams(opts);
this.updateGridSize(aggregationParams);
const {useGPU} = aggregationParams;
if (this._hasGPUSupport && useGPU) {
return this.runAggregationOnGPU(aggregationParams);
}
if (useGPU) {
log.warn('ScreenGridAggregator: GPU Aggregation not supported, falling back to CPU')();
}
return this.runAggregationOnCPU(aggregationParams);
}
// Reads aggregation data into JS Array object
// For WebGL1, data is available in JS Array objects already.
// For WebGL2, data is read from Buffer objects and cached for subsequent queries.
getData(weightId) {
const data = {};
const results = this.state.results;
if (!results[weightId].aggregationData) {
// cache the results if reading from the buffer (WebGL2 path)
results[weightId].aggregationData = results[weightId].aggregationBuffer.getData();
}
data.aggregationData = results[weightId].aggregationData;
// Check for optional results
for (const arrayName in ARRAY_BUFFER_MAP) {
const bufferName = ARRAY_BUFFER_MAP[arrayName];
if (results[weightId][arrayName] || results[weightId][bufferName]) {
// cache the result
results[weightId][arrayName] =
results[weightId][arrayName] || results[weightId][bufferName].getData();
data[arrayName] = results[weightId][arrayName];
}
}
return data;
}
// PRIVATE
deleteResources(resources) {
resources = Array.isArray(resources) ? resources : [resources];
resources.forEach(obj => {
for (const name in obj) {
obj[name].delete();
}
});
}
getAggregationParams(opts) {
const aggregationParams = Object.assign({}, DEFAULT_RUN_PARAMS, opts);
const {
useGPU,
gridTransformMatrix,
viewport,
weights,
projectPoints,
cellSize
} = aggregationParams;
if (this.state.useGPU !== useGPU) {
// CPU/GPU resources need to reinitialized, force set the change flags.
aggregationParams.changeFlags = Object.assign(
{},
aggregationParams.changeFlags,
DEFAULT_CHANGE_FLAGS
);
}
if (
cellSize &&
(!this.state.cellSize ||
this.state.cellSize[0] !== cellSize[0] ||
this.state.cellSize[1] !== cellSize[1])
) {
aggregationParams.changeFlags.cellSizeChanged = true;
// For GridLayer aggregation, cellSize is calculated by parsing all input data as it depends
// on bounding box, cache cellSize
this.setState({cellSize});
}
this.validateProps(aggregationParams, opts);
this.setState({useGPU});
aggregationParams.gridTransformMatrix =
(projectPoints ? viewport.viewportMatrix : gridTransformMatrix) || IDENTITY_MATRIX;
if (weights) {
aggregationParams.weights = this.normalizeWeightParams(weights);
// cache weights to process when only cellSize or viewport is changed.
// position data is cached in Buffers for GPU case and in 'gridPositions' for CPU case.
this.setState({weights: aggregationParams.weights});
}
return aggregationParams;
}
normalizeWeightParams(weights) {
const result = {};
for (const id in weights) {
result[id] = Object.assign({}, DEFAULT_WEIGHT_PARAMS, weights[id]);
}
return result;
}
// Update priveate state
setState(updateObject) {
Object.assign(this.state, updateObject);
}
shouldTransformToGrid(opts) {
const {projectPoints, changeFlags} = opts;
if (
!this.state.gridPositions ||
changeFlags.dataChanged ||
(projectPoints && changeFlags.viewportChanged) // world space aggregation (GridLayer) doesn't change when viewport is changed.
) {
return true;
}
return false;
}
updateGridSize(opts) {
const {viewport, cellSize} = opts;
const width = opts.width || viewport.width;
const height = opts.height || viewport.height;
const numCol = Math.ceil(width / cellSize[0]);
const numRow = Math.ceil(height / cellSize[1]);
this.setState({numCol, numRow, windowSize: [width, height]});
}
/* eslint-disable complexity */
// validate and log.assert
validateProps(aggregationParams, opts) {
const {changeFlags, projectPoints, gridTransformMatrix} = aggregationParams;
log.assert(
changeFlags.dataChanged || changeFlags.viewportChanged || changeFlags.cellSizeChanged
);
// log.assert for required options
log.assert(
!changeFlags.dataChanged ||
(opts.positions &&
opts.weights &&
(!opts.projectPositions || opts.viewport) &&
opts.cellSize)
);
log.assert(!changeFlags.cellSizeChanged || opts.cellSize);
// viewport is needed only when performing screen space aggregation (projectPoints is true)
log.assert(!(changeFlags.viewportChanged && projectPoints) || opts.viewport);
if (projectPoints && gridTransformMatrix) {
log.warn('projectPoints is true, gridTransformMatrix is ignored')();
}
}
/* eslint-enable complexity */
// CPU Aggregation methods
// aggregated weight value to a cell
/* eslint-disable max-depth */
calculateAggregationData(opts) {
const {weights, results, cellIndex, posIndex} = opts;
for (const id in weights) {
const {values, size, operation} = weights[id];
const {aggregationData} = results[id];
// Fill RGB with weights
for (let sizeIndex = 0; sizeIndex < size; sizeIndex++) {
const cellElementIndex = cellIndex + sizeIndex;
const weightComponent = values[posIndex * WEIGHT_SIZE + sizeIndex];
if (aggregationData[cellIndex + 3] === 0) {
// if the cell is getting update the first time, set the value directly.
aggregationData[cellElementIndex] = weightComponent;
} else {
switch (operation) {
case AGGREGATION_OPERATION.SUM:
case AGGREGATION_OPERATION.MEAN:
aggregationData[cellElementIndex] += weightComponent;
// MEAN value is calculated during 'calculateMeanMaxMinData'
break;
case AGGREGATION_OPERATION.MIN:
aggregationData[cellElementIndex] = Math.min(
aggregationData[cellElementIndex],
weightComponent
);
break;
case AGGREGATION_OPERATION.MAX:
aggregationData[cellElementIndex] = Math.max(
aggregationData[cellElementIndex],
weightComponent
);
break;
default:
// Not a valid operation enum.
log.assert(false);
break;
}
}
}
// Track the count per grid-cell
aggregationData[cellIndex + 3]++;
}
}
/* eslint-disable max-depth, complexity */
calculateMeanMaxMinData(opts) {
const {validCellIndices, results, weights} = opts;
// collect max/min values
validCellIndices.forEach(cellIndex => {
for (const id in results) {
const {size, needMin, needMax, operation} = weights[id];
const {aggregationData, minData, maxData, maxMinData} = results[id];
const calculateMinMax = needMin || needMax;
const calculateMean = operation === AGGREGATION_OPERATION.MEAN;
const combineMaxMin = needMin && needMax && weights[id].combineMaxMin;
const count = aggregationData[cellIndex + ELEMENTCOUNT - 1];
for (
let sizeIndex = 0;
sizeIndex < size && (calculateMinMax || calculateMean);
sizeIndex++
) {
const cellElementIndex = cellIndex + sizeIndex;
let weight = aggregationData[cellElementIndex];
if (calculateMean) {
aggregationData[cellElementIndex] /= count;
weight = aggregationData[cellElementIndex];
}
if (combineMaxMin) {
// use RGB for max values for 3 weights.
maxMinData[sizeIndex] = Math.max(maxMinData[sizeIndex], weight);
} else {
if (needMin) {
minData[sizeIndex] = Math.min(minData[sizeIndex], weight);
}
if (needMax) {
maxData[sizeIndex] = Math.max(maxData[sizeIndex], weight);
}
}
}
// update total aggregation values.
if (combineMaxMin) {
// Use Alpha channel to store total min value for weight#0
maxMinData[ELEMENTCOUNT - 1] = Math.min(
maxMinData[ELEMENTCOUNT - 1],
aggregationData[cellIndex + 0]
);
} else {
// Use Alpha channel to store total counts.
if (needMin) {
minData[ELEMENTCOUNT - 1] += count;
}
if (needMax) {
maxData[ELEMENTCOUNT - 1] += count;
}
}
}
});
}
/* eslint-enable max-depth */
initCPUResults(opts) {
const weights = opts.weights || this.state.weights;
const {numCol, numRow} = this.state;
const results = {};
// setup results object
for (const id in weights) {
let {aggregationData, minData, maxData, maxMinData} = weights[id];
const {needMin, needMax} = weights[id];
const combineMaxMin = needMin && needMax && weights[id].combineMaxMin;
const aggregationSize = numCol * numRow * ELEMENTCOUNT;
aggregationData = getFloatArray(aggregationData, aggregationSize);
if (combineMaxMin) {
maxMinData = getFloatArray(maxMinData, ELEMENTCOUNT);
// RGB for max value
maxMinData.fill(-Infinity, 0, ELEMENTCOUNT - 1);
// Alpha for min value
maxMinData[ELEMENTCOUNT - 1] = Infinity;
} else {
// RGB for min/max values
// Alpha for total count
if (needMin) {
minData = getFloatArray(minData, ELEMENTCOUNT, Infinity);
minData[ELEMENTCOUNT - 1] = 0;
}
if (needMax) {
maxData = getFloatArray(maxData, ELEMENTCOUNT, -Infinity);
maxData[ELEMENTCOUNT - 1] = 0;
}
}
results[id] = Object.assign({}, weights[id], {
aggregationData,
minData,
maxData,
maxMinData
});
}
return results;
}
/* eslint-disable max-statements */
runAggregationOnCPU(opts) {
const {positions, cellSize, gridTransformMatrix, viewport, projectPoints} = opts;
let {weights} = opts;
const {numCol, numRow} = this.state;
const results = this.initCPUResults(opts);
// screen space or world space projection required
const gridTransformRequired = this.shouldTransformToGrid(opts);
let gridPositions;
const pos = [0, 0, 0];
log.assert(gridTransformRequired || opts.changeFlags.cellSizeChanged);
let posCount;
if (gridTransformRequired) {
posCount = positions.length / 2;
gridPositions = new Float64Array(positions.length);
this.setState({gridPositions});
} else {
gridPositions = this.state.gridPositions;
weights = this.state.weights;
posCount = gridPositions.length / 2;
}
const validCellIndices = new Set();
for (let posIndex = 0; posIndex < posCount; posIndex++) {
let x;
let y;
if (gridTransformRequired) {
pos[0] = positions[posIndex * 2];
pos[1] = positions[posIndex * 2 + 1];
if (projectPoints) {
[x, y] = viewport.project(pos);
} else {
[x, y] = worldToPixels(pos, gridTransformMatrix);
}
gridPositions[posIndex * 2] = x;
gridPositions[posIndex * 2 + 1] = y;
} else {
x = gridPositions[posIndex * 2];
y = gridPositions[posIndex * 2 + 1];
}
const colId = Math.floor(x / cellSize[0]);
const rowId = Math.floor(y / cellSize[1]);
if (colId >= 0 && colId < numCol && rowId >= 0 && rowId < numRow) {
const cellIndex = (colId + rowId * numCol) * ELEMENTCOUNT;
validCellIndices.add(cellIndex);
this.calculateAggregationData({weights, results, cellIndex, posIndex});
}
}
this.calculateMeanMaxMinData({validCellIndices, results, weights});
// Update buffer objects.
this.updateAggregationBuffers(opts, results);
this.setState({results});
return results;
}
/* eslint-disable max-statements */
updateCPUResultBuffer({gl, bufferName, id, data, result}) {
const {resources} = this.state;
const resourceName = `cpu-result-${id}-${bufferName}`;
result[bufferName] = result[bufferName] || resources[resourceName];
if (result[bufferName]) {
result[bufferName].setData({data});
} else {
// save resource for garbage collection
resources[resourceName] = new Buffer(gl, data);
result[bufferName] = resources[resourceName];
}
}
updateAggregationBuffers(opts, results) {
if (!opts.createBufferObjects) {
return;
}
const weights = opts.weights || this.state.weights;
for (const id in results) {
const {aggregationData, minData, maxData, maxMinData} = results[id];
const {needMin, needMax} = weights[id];
const combineMaxMin = needMin && needMax && weights[id].combineMaxMin;
this.updateCPUResultBuffer({
gl: this.gl,
bufferName: 'aggregationBuffer',
id,
data: aggregationData,
result: results[id]
});
if (combineMaxMin) {
this.updateCPUResultBuffer({
gl: this.gl,
bufferName: 'maxMinBuffer',
id,
data: maxMinData,
result: results[id]
});
} else {
if (needMin) {
this.updateCPUResultBuffer({
gl: this.gl,
bufferName: 'minBuffer',
id,
data: minData,
result: results[id]
});
}
if (needMax) {
this.updateCPUResultBuffer({
gl: this.gl,
bufferName: 'maxBuffer',
id,
data: maxData,
result: results[id]
});
}
}
}
}
// GPU Aggregation methods
getAggregateData(opts) {
const results = {};
const {
textures,
framebuffers,
maxMinFramebuffers,
minFramebuffers,
maxFramebuffers,
weights
} = this.state;
for (const id in weights) {
results[id] = {};
const {needMin, needMax, combineMaxMin} = weights[id];
results[id].aggregationTexture = textures[id];
results[id].aggregationBuffer = readPixelsToBuffer(framebuffers[id], {
target: weights[id].aggregationBuffer, // update if a buffer is provided
sourceType: GL.FLOAT
});
if (needMin && needMax && combineMaxMin) {
results[id].maxMinBuffer = readPixelsToBuffer(maxMinFramebuffers[id], {
target: weights[id].maxMinBuffer, // update if a buffer is provided
sourceType: GL.FLOAT
});
} else {
if (needMin) {
results[id].minBuffer = readPixelsToBuffer(minFramebuffers[id], {
target: weights[id].minBuffer, // update if a buffer is provided
sourceType: GL.FLOAT
});
}
if (needMax) {
results[id].maxBuffer = readPixelsToBuffer(maxFramebuffers[id], {
target: weights[id].maxBuffer, // update if a buffer is provided
sourceType: GL.FLOAT
});
}
}
}
this.trackGPUResultBuffers(results, weights);
return results;
}
getAggregationModel(fp64 = false) {
const {gl, shaderCache} = this;
return new Model(gl, {
id: 'Gird-Aggregation-Model',
vs: fp64 ? AGGREGATE_TO_GRID_VS_FP64 : AGGREGATE_TO_GRID_VS,
fs: AGGREGATE_TO_GRID_FS,
modules: fp64 ? ['fp64', 'project64'] : ['project32'],
shaderCache,
vertexCount: 0,
drawMode: GL.POINTS
});
}
getAllAggregationModel() {
const {gl, shaderCache} = this;
const {numCol, numRow} = this.state;
return new Model(gl, {
id: 'All-Aggregation-Model',
vs: AGGREGATE_ALL_VS_FP64,
fs: AGGREGATE_ALL_FS,
modules: ['fp64'],
shaderCache,
vertexCount: 1,
drawMode: GL.POINTS,
isInstanced: true,
instanceCount: numCol * numRow,
attributes: {
position: [0, 0]
}
});
}
getMeanTransform(opts) {
if (this.meanTransform) {
this.meanTransform.update(opts);
} else {
this.meanTransform = new Transform(
this.gl,
Object.assign(
{},
{
vs: TRANSFORM_MEAN_VS,
_targetTextureVarying: 'meanValues'
},
opts
)
);
}
return this.meanTransform;
}
renderAggregateData(opts) {
const {cellSize, viewport, gridTransformMatrix, projectPoints} = opts;
const {
numCol,
numRow,
windowSize,
maxMinFramebuffers,
minFramebuffers,
maxFramebuffers,
weights
} = this.state;
const uProjectionMatrixFP64 = fp64ifyMatrix4(gridTransformMatrix);
const gridSize = [numCol, numRow];
const parameters = {
blend: true,
depthTest: false,
blendFunc: [GL.ONE, GL.ONE]
};
const moduleSettings = {viewport};
const uniforms = {
windowSize,
cellSize,
gridSize,
uProjectionMatrix: gridTransformMatrix,
uProjectionMatrixFP64,
projectPoints
};
for (const id in weights) {
const {needMin, needMax} = weights[id];
const combineMaxMin = needMin && needMax && weights[id].combineMaxMin;
this.renderToWeightsTexture({id, parameters, moduleSettings, uniforms, gridSize});
if (combineMaxMin) {
this.renderToMaxMinTexture({
id,
parameters: Object.assign({}, parameters, {blendEquation: MAX_MIN_BLEND_EQUATION}),
gridSize,
minOrMaxFb: maxMinFramebuffers[id],
clearParams: {clearColor: [0, 0, 0, MAX_32_BIT_FLOAT]},
combineMaxMin
});
} else {
if (needMin) {
this.renderToMaxMinTexture({
id,
parameters: Object.assign({}, parameters, {blendEquation: MIN_BLEND_EQUATION}),
gridSize,
minOrMaxFb: minFramebuffers[id],
clearParams: {clearColor: [MAX_32_BIT_FLOAT, MAX_32_BIT_FLOAT, MAX_32_BIT_FLOAT, 0]},
combineMaxMin
});
}
if (needMax) {
this.renderToMaxMinTexture({
id,
parameters: Object.assign({}, parameters, {blendEquation: MAX_BLEND_EQUATION}),
gridSize,
minOrMaxFb: maxFramebuffers[id],
combineMaxMin
});
}
}
}
}
// render all aggregated grid-cells to generate Min, Max or MaxMin data texture
renderToMaxMinTexture(opts) {
const {id, parameters, gridSize, minOrMaxFb, combineMaxMin, clearParams = {}} = opts;
const {framebuffers} = this.state;
const {gl, allAggregationModel} = this;
minOrMaxFb.bind();
gl.viewport(0, 0, gridSize[0], gridSize[1]);
withParameters(gl, clearParams, () => {
gl.clear(gl.COLOR_BUFFER_BIT);
});
allAggregationModel.draw({
parameters,
uniforms: {
uSampler: framebuffers[id].texture,
gridSize,
combineMaxMin
}
});
minOrMaxFb.unbind();
}
// render all data points to aggregate weights
renderToWeightsTexture(opts) {
const {id, parameters, moduleSettings, uniforms, gridSize} = opts;
const {framebuffers, equations, weightAttributes, weights} = this.state;
const {gl, gridAggregationModel} = this;
const {operation} = weights[id];
framebuffers[id].bind();
gl.viewport(0, 0, gridSize[0], gridSize[1]);
const clearColor =
operation === AGGREGATION_OPERATION.MIN
? [MAX_32_BIT_FLOAT, MAX_32_BIT_FLOAT, MAX_32_BIT_FLOAT, 0]
: [0, 0, 0, 0];
withParameters(gl, {clearColor}, () => {
gl.clear(gl.COLOR_BUFFER_BIT);
});
const attributes = {weights: weightAttributes[id]};
gridAggregationModel.draw({
parameters: Object.assign({}, parameters, {blendEquation: equations[id]}),
moduleSettings,
uniforms,
attributes
});
framebuffers[id].unbind();
if (operation === AGGREGATION_OPERATION.MEAN) {
const {meanTextures, textures} = this.state;
const transformOptions = {
_sourceTextures: {aggregationValues: meanTextures[id]}, // contains aggregated data
_targetTexture: textures[id], // store mean values,
elementCount: textures[id].width * textures[id].height
};
const meanTransform = this.getMeanTransform(transformOptions);
meanTransform.run({
parameters: {
blend: false,
depthTest: false
}
});
// update framebuffer with mean results so readPixelsToBuffer returns mean values
framebuffers[id].attach({[GL.COLOR_ATTACHMENT0]: textures[id]});
}
}
runAggregationOnGPU(opts) {
this.updateModels(opts);
this.setupFramebuffers(opts);
this.renderAggregateData(opts);
const results = this.getAggregateData(opts);
this.setState({results});
return results;
}
// set up framebuffer for each weight
/* eslint-disable complexity, max-depth */
setupFramebuffers(opts) {
const {
numCol,
numRow,
textures,
framebuffers,
maxMinFramebuffers,
minFramebuffers,
maxFramebuffers,
resources,
meanTextures,
equations,
weights
} = this.state;
const framebufferSize = {width: numCol, height: numRow};
for (const id in weights) {
const {needMin, needMax, combineMaxMin, operation} = weights[id];
textures[id] =
weights[id].aggregationTexture ||
textures[id] ||
getFloatTexture(this.gl, {id: `${id}-texture`, width: numCol, height: numRow});
textures[id].resize(framebufferSize);
let texture = textures[id];
if (operation === AGGREGATION_OPERATION.MEAN) {
// For MEAN, we first aggregatet into a temp texture
meanTextures[id] =
meanTextures[id] ||
getFloatTexture(this.gl, {id: `${id}-mean-texture`, width: numCol, height: numRow});
meanTextures[id].resize(framebufferSize);
texture = meanTextures[id];
}
if (framebuffers[id]) {
framebuffers[id].attach({[GL.COLOR_ATTACHMENT0]: texture});
} else {
framebuffers[id] = getFramebuffer(this.gl, {
id: `${id}-fb`,
width: numCol,
height: numRow,
texture
});
}
framebuffers[id].resize(framebufferSize);
equations[id] = EQUATION_MAP[operation];
// For min/max framebuffers will use default size 1X1
if (needMin || needMax) {
if (needMin && needMax && combineMaxMin) {
if (!maxMinFramebuffers[id]) {
resources[`${id}-maxMin`] = getFloatTexture(this.gl, {id: `${id}-maxMinTex`});
maxMinFramebuffers[id] = getFramebuffer(this.gl, {
id: `${id}-maxMinFb`,
texture: resources[`${id}-maxMin`]
});
}
} else {
if (needMin) {
if (!minFramebuffers[id]) {
resources[`${id}-min`] = getFloatTexture(this.gl, {id: `${id}-minTex`});
minFramebuffers[id] = getFramebuffer(this.gl, {
id: `${id}-minFb`,
texture: resources[`${id}-min`]
});
}
}
if (needMax) {
if (!maxFramebuffers[id]) {
resources[`${id}-max`] = getFloatTexture(this.gl, {id: `${id}-maxTex`});
maxFramebuffers[id] = getFramebuffer(this.gl, {
id: `${id}-maxFb`,
texture: resources[`${id}-max`]
});
}
}
}
}
}
}
/* eslint-enable complexity, max-depth */
setupModels(fp64 = false) {
if (this.gridAggregationModel) {
this.gridAggregationModel.delete();
}
this.gridAggregationModel = this.getAggregationModel(fp64);
if (!this.allAggregationModel) {
// Model doesn't have to change when fp64 flag changes
this.allAggregationModel = this.getAllAggregationModel();
}
}
// set up buffers for all weights
setupWeightAttributes(opts) {
const {weightAttributes, vertexCount, weights, resources} = this.state;
for (const id in weights) {
const {values} = weights[id];
// values can be Array, Float32Array or Buffer
if (Array.isArray(values) || values.constructor === Float32Array) {
log.assert(values.length / 3 === vertexCount);
const typedArray = Array.isArray(values) ? new Float32Array(values) : values;
if (weightAttributes[id] instanceof Buffer) {
weightAttributes[id].setData(typedArray);
} else {
resources[`${id}-buffer`] = new Buffer(this.gl, typedArray);
weightAttributes[id] = resources[`${id}-buffer`];
}
} else {
// log.assert((values instanceof Attribute) || (values instanceof Buffer));
log.assert(values instanceof Buffer);
weightAttributes[id] = values;
}
}
}
// GPU Aggregation results are provided in Buffers, if new Buffer objects are created track them for later deletion.
/* eslint-disable max-depth */
trackGPUResultBuffers(results, weights) {
const {resources} = this.state;
for (const id in results) {
if (results[id]) {
for (const bufferName of BUFFER_NAMES) {
if (results[id][bufferName] && weights[id][bufferName] !== results[id][bufferName]) {
// No result buffer is provided in weights object, `readPixelsToBuffer` has created a new Buffer object
// collect the new buffer for garabge collection
const name = `gpu-result-${id}-${bufferName}`;
if (resources[name]) {
resources[name].delete();