Skip to content

Commit

Permalink
clean up program config construction
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Feb 6, 2020
1 parent 72994a1 commit 710c666
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
24 changes: 9 additions & 15 deletions src/data/program_configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,14 +479,11 @@ export default class ProgramConfiguration {

_buffers: Array<VertexBuffer>;

constructor() {
constructor(layer: TypedStyleLayer, zoom: number, filterProperties: (string) => boolean, layoutAttributes: Array<StructArrayMember>) {
this.binders = {};
this.cacheKey = '';
this._buffers = [];
}

static createDynamic<Layer: TypedStyleLayer>(layer: Layer, zoom: number, filterProperties: (string) => boolean) {
const self = new ProgramConfiguration();
this.layoutAttributes = layoutAttributes;
const keys = [];

for (const property in layer.paint._values) {
Expand All @@ -503,30 +500,28 @@ export default class ProgramConfiguration {

if (isCrossFaded) {
if (value.value.kind === 'constant') {
self.binders[property] = new CrossFadedConstantBinder(value.value.value, names, type);
this.binders[property] = new CrossFadedConstantBinder(value.value.value, names, type);
keys.push(`/u_${property}`);
} else {
const StructArrayLayout = layoutType(property, type, 'source');
self.binders[property] = new CrossFadedCompositeBinder(value.value, names, type, useIntegerZoom, zoom, StructArrayLayout, layer.id);
this.binders[property] = new CrossFadedCompositeBinder(value.value, names, type, useIntegerZoom, zoom, StructArrayLayout, layer.id);
keys.push(`/a_${property}`);
}
} else if (value.value.kind === 'constant') {
self.binders[property] = new ConstantBinder(value.value.value, names, type);
this.binders[property] = new ConstantBinder(value.value.value, names, type);
keys.push(`/u_${property}`);
} else if (value.value.kind === 'source') {
const StructArrayLayout = layoutType(property, type, 'source');
self.binders[property] = new SourceExpressionBinder(value.value, names, type, StructArrayLayout);
this.binders[property] = new SourceExpressionBinder(value.value, names, type, StructArrayLayout);
keys.push(`/a_${property}`);
} else {
const StructArrayLayout = layoutType(property, type, 'composite');
self.binders[property] = new CompositeExpressionBinder(value.value, names, type, useIntegerZoom, zoom, StructArrayLayout);
this.binders[property] = new CompositeExpressionBinder(value.value, names, type, useIntegerZoom, zoom, StructArrayLayout);
keys.push(`/z_${property}`);
}
}

self.cacheKey = keys.sort().join('');

return self;
this.cacheKey = keys.sort().join('');
}

populatePaintArrays(newLength: number, feature: Feature, imagePositions: {[string]: ImagePosition}, formattedSection?: FormattedSection) {
Expand Down Expand Up @@ -653,8 +648,7 @@ export class ProgramConfigurationSet<Layer: TypedStyleLayer> {
constructor(layoutAttributes: Array<StructArrayMember>, layers: $ReadOnlyArray<Layer>, zoom: number, filterProperties: (string) => boolean = () => true) {
this.programConfigurations = {};
for (const layer of layers) {
this.programConfigurations[layer.id] = ProgramConfiguration.createDynamic(layer, zoom, filterProperties);
this.programConfigurations[layer.id].layoutAttributes = layoutAttributes;
this.programConfigurations[layer.id] = new ProgramConfiguration(layer, zoom, filterProperties, layoutAttributes);
}
this.needsUpload = false;
this._featureMap = new FeaturePositionMap();
Expand Down
6 changes: 2 additions & 4 deletions src/render/painter.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ class Painter {

this.depthRboNeedsClear = true;

this.emptyProgramConfiguration = new ProgramConfiguration();

this.crossTileSymbolIndex = new CrossTileSymbolIndex();

this.gpuTimers = {};
Expand Down Expand Up @@ -599,9 +597,9 @@ class Painter {
return !imagePosA || !imagePosB;
}

useProgram(name: string, programConfiguration: ProgramConfiguration = this.emptyProgramConfiguration): Program<any> {
useProgram(name: string, programConfiguration: ?ProgramConfiguration): Program<any> {
this.cache = this.cache || {};
const key = `${name}${programConfiguration.cacheKey || ''}${this._showOverdrawInspector ? '/overdraw' : ''}`;
const key = `${name}${programConfiguration ? programConfiguration.cacheKey : ''}${this._showOverdrawInspector ? '/overdraw' : ''}`;
if (!this.cache[key]) {
this.cache[key] = new Program(this.context, shaders[name], programConfiguration, programUniforms[name], this._showOverdrawInspector);
}
Expand Down
8 changes: 4 additions & 4 deletions src/render/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ class Program<Us: UniformBindings> {

constructor(context: Context,
source: {fragmentSource: string, vertexSource: string},
configuration: ProgramConfiguration,
configuration: ?ProgramConfiguration,
fixedUniforms: (Context, UniformLocations) => Us,
showOverdrawInspector: boolean) {
const gl = context.gl;
this.program = gl.createProgram();

const defines = configuration.defines();
const defines = configuration ? configuration.defines() : [];
if (showOverdrawInspector) {
defines.push('#define OVERDRAW_INSPECTOR;');
}
Expand Down Expand Up @@ -68,7 +68,7 @@ class Program<Us: UniformBindings> {
// ProgramInterface so that we don't dynamically link an unused
// attribute at position 0, which can cause rendering to fail for an
// entire layer (see #4607, #4728)
const layoutAttributes = configuration.layoutAttributes || [];
const layoutAttributes = configuration ? configuration.layoutAttributes : [];
for (let i = 0; i < layoutAttributes.length; i++) {
gl.bindAttribLocation(this.program, i, layoutAttributes[i].name);
}
Expand Down Expand Up @@ -97,7 +97,7 @@ class Program<Us: UniformBindings> {
}

this.fixedUniforms = fixedUniforms(context, uniformLocations);
this.binderUniforms = configuration.getUniforms(context, uniformLocations);
this.binderUniforms = configuration ? configuration.getUniforms(context, uniformLocations) : [];
}

draw(context: Context,
Expand Down

0 comments on commit 710c666

Please sign in to comment.