Skip to content

Commit 87fba8c

Browse files
author
Don McCurdy
committed
GPUGridAggregator updates
1 parent d01accf commit 87fba8c

File tree

3 files changed

+95
-88
lines changed

3 files changed

+95
-88
lines changed

modules/aggregation-layers/src/utils/gpu-grid-aggregation/gpu-grid-aggregator-constants.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ export const DEFAULT_RUN_PARAMS = {
99
};
1010

1111
export const MAX_32_BIT_FLOAT = 3.402823466e38;
12-
export const MIN_BLEND_EQUATION = [GL.MIN, GL.FUNC_ADD];
13-
export const MAX_BLEND_EQUATION = [GL.MAX, GL.FUNC_ADD];
14-
export const MAX_MIN_BLEND_EQUATION = [GL.MAX, GL.MIN];
15-
export const EQUATION_MAP = {
16-
[AGGREGATION_OPERATION.SUM]: GL.FUNC_ADD,
17-
[AGGREGATION_OPERATION.MEAN]: GL.FUNC_ADD,
18-
[AGGREGATION_OPERATION.MIN]: MIN_BLEND_EQUATION,
19-
[AGGREGATION_OPERATION.MAX]: MAX_BLEND_EQUATION
20-
};
12+
// export const MIN_BLEND_EQUATION = [GL.MIN, GL.FUNC_ADD];
13+
// export const MAX_BLEND_EQUATION = [GL.MAX, GL.FUNC_ADD];
14+
// export const MAX_MIN_BLEND_EQUATION = [GL.MAX, GL.MIN];
15+
// export const EQUATION_MAP = {
16+
// [AGGREGATION_OPERATION.SUM]: GL.FUNC_ADD,
17+
// [AGGREGATION_OPERATION.MEAN]: GL.FUNC_ADD,
18+
// [AGGREGATION_OPERATION.MIN]: MIN_BLEND_EQUATION,
19+
// [AGGREGATION_OPERATION.MAX]: MAX_BLEND_EQUATION
20+
// };
2121

2222
export const ELEMENTCOUNT = 4;
2323
export const DEFAULT_WEIGHT_PARAMS = {

modules/aggregation-layers/src/utils/gpu-grid-aggregation/gpu-grid-aggregator.ts

Lines changed: 84 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,16 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
// THE SOFTWARE.
2020

21-
import type {Device, DeviceFeature} from '@luma.gl/core';
21+
import type {BlendFactor, BlendOperation, Device, DeviceFeature, Texture} from '@luma.gl/core';
2222
import {Model, TextureTransform} from '@luma.gl/engine';
2323
import {fp64arithmetic} from '@luma.gl/shadertools';
24-
import {readPixelsToBuffer, withGLParameters, clear} from '@luma.gl/webgl';
24+
import {readPixelsToBuffer} from '@luma.gl/webgl';
2525
import {GL} from '@luma.gl/constants';
2626
import {log, project32, _mergeShaders as mergeShaders, getShaderAssembler} from '@deck.gl/core';
2727

2828
import {
2929
DEFAULT_RUN_PARAMS,
3030
MAX_32_BIT_FLOAT,
31-
MIN_BLEND_EQUATION,
32-
MAX_BLEND_EQUATION,
33-
MAX_MIN_BLEND_EQUATION,
34-
EQUATION_MAP,
3531
DEFAULT_WEIGHT_PARAMS,
3632
PIXEL_SIZE
3733
} from './gpu-grid-aggregator-constants';
@@ -79,17 +75,17 @@ export default class GPUGridAggregator {
7975
pixelIndex: number;
8076
}) {
8177
const index = pixelIndex * PIXEL_SIZE;
82-
const results: {cellCount?; cellWeight?; maxCellWieght?; minCellWeight?; totalCount?} = {};
78+
const results: {cellCount?; cellWeight?; maxCellWeight?; minCellWeight?; totalCount?} = {};
8379
if (aggregationData) {
8480
results.cellCount = aggregationData[index + 3];
8581
results.cellWeight = aggregationData[index];
8682
}
8783
if (maxMinData) {
88-
results.maxCellWieght = maxMinData[0];
84+
results.maxCellWeight = maxMinData[0];
8985
results.minCellWeight = maxMinData[3];
9086
} else {
9187
if (maxData) {
92-
results.maxCellWieght = maxData[0];
88+
results.maxCellWeight = maxData[0];
9389
results.totalCount = maxData[3];
9490
}
9591
if (minData) {
@@ -149,13 +145,13 @@ export default class GPUGridAggregator {
149145
// per weight GPU resources
150146
weightAttributes: {},
151147
textures: {},
152-
meanTextures: {},
148+
meanTextures: {} as Record<string, Texture>,
149+
meanTransforms: {} as Record<string, TextureTransform>,
153150
buffers: {},
154151
framebuffers: {},
155152
maxMinFramebuffers: {},
156153
minFramebuffers: {},
157154
maxFramebuffers: {},
158-
equations: {},
159155

160156
shaderOptions: {},
161157
modelDirty: false,
@@ -171,9 +167,9 @@ export default class GPUGridAggregator {
171167
device: Device;
172168
_hasGPUSupport: boolean;
173169

174-
gridAggregationModel;
175-
allAggregationModel;
176-
meanTransform;
170+
// TODO(donmccurdy): Validate forward declaration or remove.
171+
gridAggregationModel: Model = undefined!;
172+
allAggregationModel: Model = undefined!;
177173

178174
constructor(device: Device, props: GPUGridAggregatorProps = {}) {
179175
this.id = props.id || 'gpu-grid-aggregator';
@@ -196,20 +192,20 @@ export default class GPUGridAggregator {
196192

197193
// Delete owned resources.
198194
delete() {
199-
const {gridAggregationModel, allAggregationModel, meanTransform} = this;
195+
const {gridAggregationModel, allAggregationModel} = this;
200196
const {
201197
textures,
202198
framebuffers,
203199
maxMinFramebuffers,
204200
minFramebuffers,
205201
maxFramebuffers,
206202
meanTextures,
203+
meanTransforms,
207204
resources
208205
} = this.state;
209206

210207
gridAggregationModel?.destroy();
211208
allAggregationModel?.destroy();
212-
meanTransform?.destroy();
213209

214210
deleteResources([
215211
framebuffers,
@@ -218,6 +214,7 @@ export default class GPUGridAggregator {
218214
minFramebuffers,
219215
maxFramebuffers,
220216
meanTextures,
217+
meanTransforms,
221218
resources
222219
]);
223220
}
@@ -411,95 +408,105 @@ export default class GPUGridAggregator {
411408
_renderToMaxMinTexture(opts) {
412409
const {id, parameters, gridSize, minOrMaxFb, combineMaxMin, clearParams = {}} = opts;
413410
const {framebuffers} = this.state;
414-
const {allAggregationModel} = this;
415-
416-
withGLParameters(
417-
this.device,
418-
{
419-
...clearParams,
420-
framebuffer: minOrMaxFb,
421-
viewport: [0, 0, gridSize[0], gridSize[1]]
422-
},
423-
() => {
424-
clear(this.device, {color: true});
425-
426-
// allAggregationModel.setParameters(parameters);
427-
allAggregationModel.setUniforms({gridSize, combineMaxMin});
428-
allAggregationModel.setBindings({uSampler: framebuffers[id].texture});
429-
allAggregationModel.draw();
430-
// TODO - we need to create a render pass for the aggregation
431-
// allAggregationModel.draw(renderPass);
411+
const model = this.allAggregationModel;
412+
const renderPass = this.device.beginRenderPass({
413+
...clearParams,
414+
framebuffer: minOrMaxFb,
415+
parameters: {
416+
viewport: [0, 0, gridSize[0], gridSize[1]],
417+
...parameters
432418
}
433-
);
419+
});
420+
model.setUniforms({gridSize, combineMaxMin});
421+
model.setBindings({uSampler: framebuffers[id].texture});
422+
model.draw(renderPass);
423+
renderPass.end();
434424
}
435425

436426
// render all data points to aggregate weights
437427
_renderToWeightsTexture(opts) {
438428
const {id, parameters, moduleSettings, uniforms, gridSize, weights} = opts;
439-
const {framebuffers, equations, weightAttributes} = this.state;
440-
const {gridAggregationModel} = this;
429+
const {framebuffers, weightAttributes} = this.state;
441430
const {operation} = weights[id];
442431

443432
const clearColor =
444433
operation === AGGREGATION_OPERATION.MIN
445434
? [MAX_32_BIT_FLOAT, MAX_32_BIT_FLOAT, MAX_32_BIT_FLOAT, 0]
446435
: [0, 0, 0, 0];
447-
withGLParameters(
448-
this.device,
449-
{
450-
framebuffer: framebuffers[id],
451-
viewport: [0, 0, gridSize[0], gridSize[1]],
452-
clearColor
453-
},
454-
() => {
455-
clear(this.device, {color: true});
456-
457-
const attributes = {weights: weightAttributes[id]};
458-
gridAggregationModel.draw({
459-
parameters: {...parameters, blendEquation: equations[id]},
460-
moduleSettings,
461-
uniforms,
462-
attributes
463-
});
436+
437+
const renderPass = this.device.beginRenderPass({
438+
framebuffer: framebuffers[id],
439+
clearColor,
440+
parameters: {
441+
...parameters,
442+
viewport: [0, 0, gridSize[0], gridSize[1]]
464443
}
465-
);
444+
// TODO(donmccurdy): What are these?
445+
// moduleSettings
446+
// uniforms,
447+
// attributes: {weights: weightAttributes[id]}
448+
});
449+
this.gridAggregationModel.draw(renderPass);
450+
renderPass.end();
466451

467452
if (operation === AGGREGATION_OPERATION.MEAN) {
468-
const {meanTextures, textures} = this.state;
469-
const transformOptions = {
470-
bindings: {aggregationValues: meanTextures[id]}, // contains aggregated data
471-
targetTexture: textures[id], // store mean values,
472-
elementCount: textures[id].width * textures[id].height
473-
};
453+
const {meanTextures, meanTransforms, textures} = this.state;
454+
455+
let meanTransform = meanTransforms[id];
456+
474457
// TODO(donmccurdy): Avoid .update(), but don't recreate on every frame.
475458
// if (this.meanTransform) {
476459
// this.meanTransform.update(transformOptions);
477460
// } else {
478461
// this.meanTransform = getMeanTransform(this.device, transformOptions);
479462
// }
480-
if (this.meanTransform) {
481-
this.meanTransform.destroy();
463+
464+
if (!meanTransform) {
465+
// TODO(donmccurdy): Unclear lifecycle.
466+
console.warn('[gpu-grid-agg] BuildingTextureTransform');
467+
meanTransform = meanTransforms[id] = new TextureTransform(this.device, {
468+
vs: TRANSFORM_MEAN_VS,
469+
bindings: {aggregationValues: meanTextures[id]}, // contains aggregated data
470+
targetTexture: textures[id], // store mean values,
471+
targetTextureVarying: 'meanValues',
472+
targetTextureChannels: 4, // TODO(donmccurdy): Correct?
473+
elementCount: textures[id].width * textures[id].height,
474+
parameters: {
475+
depthCompare: 'always'
476+
// TODO(donmccurdy): Why was `blend: false` previously here?
477+
}
478+
});
482479
}
483480

484-
this.meanTransform = new TextureTransform(this.device, {
485-
vs: TRANSFORM_MEAN_VS,
486-
targetTextureVarying: 'meanValues',
487-
targetTextureChannels: 4, // TODO(donmccurdy): Correct?
488-
...transformOptions
489-
});
490-
491-
this.meanTransform.run({
492-
parameters: {
493-
blend: false,
494-
depthTest: false
495-
}
496-
});
481+
meanTransform.run();
497482

483+
// TODO(v9): Unlikely to be portable.
498484
// update framebuffer with mean results so readPixelsToBuffer returns mean values
499485
framebuffers[id].attach({[GL.COLOR_ATTACHMENT0]: textures[id]});
500486
}
501487
}
502488

489+
_getBlendParameters(weights: Record<string, any>): ColorParameters {
490+
let blendColorOperation: BlendOperation;
491+
let blendColorSrcFactor: BlendFactor;
492+
let blendColorDstFactor: BlendFactor;
493+
494+
let blendAlphaOperation: BlendOperation;
495+
let blendAlphaSrcFactor: BlendFactor;
496+
let blendAlphaDstFactor: BlendFactor;
497+
498+
for (const id in weights) {
499+
const {needMin, needMax, combineMaxMin} = weights[id];
500+
if (combineMaxMin) {
501+
blendColorOperation = 'max';
502+
blendAlphaOperation = 'min';
503+
} else {
504+
if (
505+
}
506+
507+
}
508+
}
509+
503510
_runAggregation(opts) {
504511
this._updateModels(opts);
505512
this._setupFramebuffers(opts);
@@ -518,8 +525,7 @@ export default class GPUGridAggregator {
518525
maxMinFramebuffers,
519526
minFramebuffers,
520527
maxFramebuffers,
521-
meanTextures,
522-
equations
528+
meanTextures
523529
} = this.state;
524530
const {weights} = opts;
525531
const {numCol, numRow} = opts;
@@ -551,7 +557,6 @@ export default class GPUGridAggregator {
551557
});
552558
}
553559
framebuffers[id].resize(framebufferSize);
554-
equations[id] = EQUATION_MAP[operation] || EQUATION_MAP[AGGREGATION_OPERATION.SUM];
555560
// For min/max framebuffers will use default size 1X1
556561
if (needMin || needMax) {
557562
if (needMin && needMax && combineMaxMin) {

modules/aggregation-layers/src/utils/resource-utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ export function getFloatTexture(device: Device, opts: FloatTextureOptions) {
3939
return texture;
4040
}
4141

42+
// TODO(donmccurdy): Consider removing this utility and explicitly creating
43+
// framebuffers. Allows stricter types and clearer responsibility for disposal.
4244
export function getFramebuffer(device: Device, opts) {
4345
const {id, width = 1, height = 1, texture} = opts;
4446
const fb = device.createFramebuffer({

0 commit comments

Comments
 (0)