Skip to content

Commit

Permalink
optimize data transfer from the worker
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Nov 9, 2016
1 parent ba1ee50 commit 148144b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
21 changes: 14 additions & 7 deletions js/data/array_group.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const util = require('../util/util');
const ProgramConfiguration = require('./program_configuration');

class Segment {
Expand Down Expand Up @@ -80,6 +79,7 @@ class ArrayGroup {
populatePaintArrays(featureProperties) {
for (const key in this.layerData) {
const layerData = this.layerData[key];
if (layerData.paintVertexArray.bytesPerElement === 0) continue;
layerData.programConfiguration.populatePaintArray(
layerData.layer,
layerData.paintVertexArray,
Expand All @@ -98,18 +98,25 @@ class ArrayGroup {
layoutVertexArray: this.layoutVertexArray.serialize(transferables),
elementArray: this.elementArray && this.elementArray.serialize(transferables),
elementArray2: this.elementArray2 && this.elementArray2.serialize(transferables),
paintVertexArrays: util.mapObject(this.layerData, (layerData) => {
return {
array: layerData.paintVertexArray.serialize(transferables),
type: layerData.paintVertexArray.constructor.serialize()
};
}),
paintVertexArrays: serializePaintVertexArrays(this.layerData, transferables),
segments: this.segments,
segments2: this.segments2
};
}
}

function serializePaintVertexArrays(layerData, transferables) {
const paintVertexArrays = {};
for (const layerId in layerData) {
const inputArray = layerData[layerId].paintVertexArray;
if (inputArray.length === 0) continue;
const array = inputArray.serialize(transferables);
const type = inputArray.constructor.serialize();
paintVertexArrays[layerId] = {array, type};
}
return paintVertexArrays;
}

/**
* The maximum size of a vertex array. This limit is imposed by WebGL's 16 bit
* addressing of vertex buffers.
Expand Down
11 changes: 7 additions & 4 deletions js/data/bucket/symbol_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,12 @@ class SymbolBucket {
this.fontstack = options.fontstack;

if (options.arrays) {
this.buffers = util.mapObject(options.arrays, (arrays, key) => {
return new BufferGroup(symbolInterfaces[key], options.layers, options.zoom, options.arrays[key]);
});
this.buffers = {};
for (const id in options.arrays) {
if (options.arrays[id]) {
this.buffers[id] = new BufferGroup(symbolInterfaces[id], options.layers, options.zoom, options.arrays[id]);
}
}
}
}

Expand Down Expand Up @@ -190,7 +193,7 @@ class SymbolBucket {
adjustedTextSize: this.adjustedTextSize,
adjustedIconSize: this.adjustedIconSize,
fontstack: this.fontstack,
arrays: util.mapObject(this.arrays, (a) => a.serialize(transferables))
arrays: util.mapObject(this.arrays, (a) => a.isEmpty() ? null : a.serialize(transferables))
};
}

Expand Down
22 changes: 11 additions & 11 deletions js/data/buffer_group.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,36 @@ class BufferGroup {

this.layerData = {};
for (const layer of layers) {
const array = arrays.paintVertexArrays[layer.id];
this.layerData[layer.id] = {
programConfiguration: ProgramConfiguration.createDynamic(
programInterface.paintAttributes || [], layer, zoom),
paintVertexBuffer: new Buffer(array.array, array.type, Buffer.BufferType.VERTEX)
};
const array = arrays.paintVertexArrays && arrays.paintVertexArrays[layer.id];
const programConfiguration = ProgramConfiguration.createDynamic(programInterface.paintAttributes || [], layer, zoom);
const paintVertexBuffer = array ? new Buffer(array.array, array.type, Buffer.BufferType.VERTEX) : null;
this.layerData[layer.id] = {programConfiguration, paintVertexBuffer};
}

this.segments = arrays.segments;
this.segments2 = arrays.segments2;

for (const segments of [this.segments, this.segments2]) {
for (const segment of segments || []) {
segment.vaos = util.mapObject(arrays.paintVertexArrays, () => {
return new VertexArrayObject();
});
segment.vaos = util.mapObject(this.layerData, () => new VertexArrayObject());
}
}
}

destroy() {
this.layoutVertexBuffer.destroy();

if (this.elementBuffer) {
this.elementBuffer.destroy();
}
if (this.elementBuffer2) {
this.elementBuffer2.destroy();
}
for (const n in this.layerData) {
this.layerData[n].paintVertexBuffer.destroy();
for (const layerId in this.layerData) {
const paintVertexBuffer = this.layerData[layerId].paintVertexBuffer;
if (paintVertexBuffer) {
paintVertexBuffer.destroy();
}
}
for (const segments of [this.segments, this.segments2]) {
for (const segment of segments || []) {
Expand Down
2 changes: 1 addition & 1 deletion js/render/draw_symbol.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function drawLayerSymbols(painter, sourceCache, layer, coords, isText, translate
const bucket = tile.getBucket(layer);
if (!bucket) continue;
const buffers = isText ? bucket.buffers.glyph : bucket.buffers.icon;
if (!buffers.segments.length) continue;
if (!buffers || !buffers.segments.length) continue;

const isSDF = isText || bucket.sdfIcons;

Expand Down

0 comments on commit 148144b

Please sign in to comment.