Skip to content

Commit

Permalink
Separate SymbolBuffers and CollisionBuffers types
Browse files Browse the repository at this point in the history
Using the same type for both was oversharing.
  • Loading branch information
jfirebaugh committed Dec 29, 2017
1 parent 626ecb9 commit e728172
Showing 1 changed file with 69 additions and 100 deletions.
169 changes: 69 additions & 100 deletions src/data/bucket/symbol_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,50 +117,6 @@ const shaderOpacityAttributes = [
{ name: 'a_fade_opacity', components: 1, type: 'Uint8', offset: 0 }
];

type SymbolBufferConfiguration<IndexArray> = {
LayoutArray: Class<StructArray>,
layoutAttributes: Array<StructArrayMember>,
IndexArray: Class<IndexArray>,
dynamicLayout: boolean,
opacity: boolean,
collision: boolean
}

const symbolBufferConfigurations = {
text: {
LayoutArray: SymbolLayoutArray,
layoutAttributes: symbolLayoutAttributes.members,
IndexArray: TriangleIndexArray,
dynamicLayout: true,
opacity: true,
collision: false
},
icon: {
LayoutArray: SymbolLayoutArray,
layoutAttributes: symbolLayoutAttributes.members,
IndexArray: TriangleIndexArray,
dynamicLayout: true,
opacity: true,
collision: false
},
collisionBox: { // used to render collision boxes for debugging purposes
LayoutArray: CollisionBoxLayoutArray,
layoutAttributes: collisionBoxLayout.members,
IndexArray: LineIndexArray,
dynamicLayout: false,
opacity: false,
collision: true
},
collisionCircle: { // used to render collision circles for debugging purposes
LayoutArray: CollisionCircleLayoutArray,
layoutAttributes: collisionCircleLayout.members,
IndexArray: TriangleIndexArray,
dynamicLayout: false,
opacity: false,
collision: true
}
};

function addVertex(array, anchorX, anchorY, ox, oy, tx, ty, sizeVertex) {
array.emplaceBack(
// a_pos_offset
Expand All @@ -172,8 +128,8 @@ function addVertex(array, anchorX, anchorY, ox, oy, tx, ty, sizeVertex) {
// a_data
tx, // x coordinate of symbol on glyph atlas texture
ty, // y coordinate of symbol on glyph atlas texture
sizeVertex ? sizeVertex[0] : undefined,
sizeVertex ? sizeVertex[1] : undefined
sizeVertex ? sizeVertex[0] : 0,
sizeVertex ? sizeVertex[1] : 0
);
}

Expand All @@ -184,12 +140,11 @@ function addDynamicAttributes(dynamicLayoutVertexArray: StructArray, p: Point, a
dynamicLayoutVertexArray.emplaceBack(p.x, p.y, angle);
}

class SymbolBuffers<IndexArray: TriangleIndexArray | LineIndexArray> {
layoutVertexArray: StructArray;
layoutAttributes: Array<StructArrayMember>;
class SymbolBuffers {
layoutVertexArray: SymbolLayoutArray;
layoutVertexBuffer: VertexBuffer;

indexArray: IndexArray;
indexArray: TriangleIndexArray;
indexBuffer: IndexBuffer;

programConfigurations: ProgramConfigurationSet<SymbolStyleLayer>;
Expand All @@ -204,43 +159,24 @@ class SymbolBuffers<IndexArray: TriangleIndexArray | LineIndexArray> {
collisionVertexArray: CollisionVertexArray;
collisionVertexBuffer: VertexBuffer;

constructor(configuration: SymbolBufferConfiguration<IndexArray>, programConfigurations: ProgramConfigurationSet<SymbolStyleLayer>) {
this.layoutVertexArray = new configuration.LayoutArray();
this.layoutAttributes = configuration.layoutAttributes;
this.indexArray = new configuration.IndexArray();
constructor(programConfigurations: ProgramConfigurationSet<SymbolStyleLayer>) {
this.layoutVertexArray = new SymbolLayoutArray();
this.indexArray = new TriangleIndexArray();
this.programConfigurations = programConfigurations;
this.segments = new SegmentVector();

if (configuration.dynamicLayout) {
this.dynamicLayoutVertexArray = new SymbolDynamicLayoutArray();
}

if (configuration.opacity) {
this.opacityVertexArray = new SymbolOpacityArray();
}

if (configuration.collision) {
this.collisionVertexArray = new CollisionVertexArray();
}
this.dynamicLayoutVertexArray = new SymbolDynamicLayoutArray();
this.opacityVertexArray = new SymbolOpacityArray();
}

upload(context: Context, dynamicIndexBuffer: boolean = false) {
this.layoutVertexBuffer = context.createVertexBuffer(this.layoutVertexArray, this.layoutAttributes);
upload(context: Context, dynamicIndexBuffer: boolean) {
this.layoutVertexBuffer = context.createVertexBuffer(this.layoutVertexArray, symbolLayoutAttributes.members);
this.indexBuffer = context.createIndexBuffer(this.indexArray, dynamicIndexBuffer);
this.programConfigurations.upload(context);

if (this.dynamicLayoutVertexArray) {
this.dynamicLayoutVertexBuffer = context.createVertexBuffer(this.dynamicLayoutVertexArray, dynamicLayoutAttributes.members, true);
}
if (this.opacityVertexArray) {
this.opacityVertexBuffer = context.createVertexBuffer(this.opacityVertexArray, shaderOpacityAttributes, true);
// This is a performance hack so that we can write to opacityVertexArray with uint32s
// even though the shaders read uint8s
this.opacityVertexBuffer.itemSize = 1;
}
if (this.collisionVertexArray) {
this.collisionVertexBuffer = context.createVertexBuffer(this.collisionVertexArray, collisionVertexAttributes.members, true);
}
this.dynamicLayoutVertexBuffer = context.createVertexBuffer(this.dynamicLayoutVertexArray, dynamicLayoutAttributes.members, true);
this.opacityVertexBuffer = context.createVertexBuffer(this.opacityVertexArray, shaderOpacityAttributes, true);
// This is a performance hack so that we can write to opacityVertexArray with uint32s
// even though the shaders read uint8s
this.opacityVertexBuffer.itemSize = 1;
}

destroy() {
Expand All @@ -249,20 +185,53 @@ class SymbolBuffers<IndexArray: TriangleIndexArray | LineIndexArray> {
this.indexBuffer.destroy();
this.programConfigurations.destroy();
this.segments.destroy();
if (this.dynamicLayoutVertexBuffer) {
this.dynamicLayoutVertexBuffer.destroy();
}
if (this.opacityVertexBuffer) {
this.opacityVertexBuffer.destroy();
}
if (this.collisionVertexBuffer) {
this.collisionVertexBuffer.destroy();
}
this.dynamicLayoutVertexBuffer.destroy();
this.opacityVertexBuffer.destroy();
}
}

register('SymbolBuffers', SymbolBuffers);

class CollisionBuffers {
layoutVertexArray: StructArray;
layoutAttributes: Array<StructArrayMember>;
layoutVertexBuffer: VertexBuffer;

indexArray: TriangleIndexArray | LineIndexArray;
indexBuffer: IndexBuffer;

segments: SegmentVector;

collisionVertexArray: CollisionVertexArray;
collisionVertexBuffer: VertexBuffer;

constructor(LayoutArray: Class<StructArray>,
layoutAttributes: Array<StructArrayMember>,
IndexArray: Class<TriangleIndexArray | LineIndexArray>) {
this.layoutVertexArray = new LayoutArray();
this.layoutAttributes = layoutAttributes;
this.indexArray = new IndexArray();
this.segments = new SegmentVector();
this.collisionVertexArray = new CollisionVertexArray();
}

upload(context: Context) {
this.layoutVertexBuffer = context.createVertexBuffer(this.layoutVertexArray, this.layoutAttributes);
this.indexBuffer = context.createIndexBuffer(this.indexArray);
this.collisionVertexBuffer = context.createVertexBuffer(this.collisionVertexArray, collisionVertexAttributes.members, true);
}

destroy() {
if (!this.layoutVertexBuffer) return;
this.layoutVertexBuffer.destroy();
this.indexBuffer.destroy();
this.segments.destroy();
this.collisionVertexBuffer.destroy();
}
}

register('CollisionBuffers', CollisionBuffers);

/**
* Unlike other buckets, which simply implement #addFeature with type-specific
* logic for (essentially) triangulating feature geometries, SymbolBucket
Expand Down Expand Up @@ -324,11 +293,11 @@ class SymbolBucket implements Bucket {
sortFeaturesByY: boolean;
sortedAngle: number;

text: SymbolBuffers<TriangleIndexArray>;
icon: SymbolBuffers<TriangleIndexArray>;
collisionBox: SymbolBuffers<LineIndexArray>;
text: SymbolBuffers;
icon: SymbolBuffers;
collisionBox: CollisionBuffers;
collisionCircle: CollisionBuffers;
uploaded: boolean;
collisionCircle: SymbolBuffers<TriangleIndexArray>;

constructor(options: BucketParameters<SymbolStyleLayer>) {
this.collisionBoxArray = options.collisionBoxArray;
Expand All @@ -351,10 +320,10 @@ class SymbolBucket implements Bucket {
}

createArrays() {
this.text = new SymbolBuffers(symbolBufferConfigurations.text, new ProgramConfigurationSet(symbolLayoutAttributes.members, this.layers, this.zoom, property => /^text/.test(property)));
this.icon = new SymbolBuffers(symbolBufferConfigurations.icon, new ProgramConfigurationSet(symbolLayoutAttributes.members, this.layers, this.zoom, property => /^icon/.test(property)));
this.collisionBox = new SymbolBuffers(symbolBufferConfigurations.collisionBox, new ProgramConfigurationSet(collisionBoxLayout.members, this.layers, this.zoom, () => false));
this.collisionCircle = new SymbolBuffers(symbolBufferConfigurations.collisionCircle, new ProgramConfigurationSet(collisionCircleLayout.members, this.layers, this.zoom, () => false));
this.text = new SymbolBuffers(new ProgramConfigurationSet(symbolLayoutAttributes.members, this.layers, this.zoom, property => /^text/.test(property)));
this.icon = new SymbolBuffers(new ProgramConfigurationSet(symbolLayoutAttributes.members, this.layers, this.zoom, property => /^icon/.test(property)));
this.collisionBox = new CollisionBuffers(CollisionBoxLayoutArray, collisionBoxLayout.members, LineIndexArray);
this.collisionCircle = new CollisionBuffers(CollisionCircleLayoutArray, collisionCircleLayout.members, TriangleIndexArray);

this.placedGlyphArray = new PlacedSymbolArray();
this.placedIconArray = new PlacedSymbolArray();
Expand Down Expand Up @@ -494,7 +463,7 @@ class SymbolBucket implements Bucket {
};
}

addSymbols(arrays: SymbolBuffers<*>,
addSymbols(arrays: SymbolBuffers,
quads: Array<SymbolQuad>,
sizeVertex: any,
lineOffset: [number, number],
Expand Down Expand Up @@ -565,7 +534,7 @@ class SymbolBucket implements Bucket {
}


addCollisionDebugVertices(x1: number, y1: number, x2: number, y2: number, arrays: SymbolBuffers<TriangleIndexArray> | SymbolBuffers<LineIndexArray>, boxAnchorPoint: Point, symbolInstance: SymbolInstance, isCircle: boolean) {
addCollisionDebugVertices(x1: number, y1: number, x2: number, y2: number, arrays: CollisionBuffers, boxAnchorPoint: Point, symbolInstance: SymbolInstance, isCircle: boolean) {
const segment = arrays.segments.prepareSegment(4, arrays.layoutVertexArray, arrays.indexArray);
const index = segment.vertexLength;

Expand Down

0 comments on commit e728172

Please sign in to comment.