From 546dc0f7fb63a308dcbd9d73510e809a31f087be Mon Sep 17 00:00:00 2001 From: Vladimir Agafonkin Date: Tue, 4 Feb 2020 19:07:02 +0200 Subject: [PATCH] cut out uniform logic out of binders that don't need it --- src/data/program_configuration.js | 59 ++++++++++++------------------- 1 file changed, 23 insertions(+), 36 deletions(-) diff --git a/src/data/program_configuration.js b/src/data/program_configuration.js index 445469afb46..6d2efe55153 100644 --- a/src/data/program_configuration.js +++ b/src/data/program_configuration.js @@ -76,24 +76,25 @@ function packColor(color: Color): [number, number] { * @private */ -interface Binder { - uniformNames: Array; - +interface Binder { populatePaintArray(length: number, feature: Feature, imagePositions: {[string]: ImagePosition}, formattedSection?: FormattedSection): void; updatePaintArray(start: number, length: number, feature: Feature, featureState: FeatureState, imagePositions: {[string]: ImagePosition}): void; upload(Context): void; destroy(): void; +} - setUniform(uniform: Uniform<*>, globals: GlobalProperties, currentValue: PossiblyEvaluatedPropertyValue, uniformName: string): void; +interface UniformBinder { + uniformNames: Array; + setUniform(uniform: Uniform<*>, globals: GlobalProperties, currentValue: PossiblyEvaluatedPropertyValue<*>, uniformName: string): void; getBinding(context: Context, location: WebGLUniformLocation): $Shape>; } -class ConstantBinder implements Binder { - value: T; +class ConstantBinder implements Binder, UniformBinder { + value: mixed; type: string; uniformNames: Array; - constructor(value: T, names: Array, type: string) { + constructor(value: mixed, names: Array, type: string) { this.value = value; this.uniformNames = names.map(name => `u_${name}`); this.type = type; @@ -104,7 +105,7 @@ class ConstantBinder implements Binder { upload() {} destroy() {} - setUniform(uniform: Uniform<*>, globals: GlobalProperties, currentValue: PossiblyEvaluatedPropertyValue): void { + setUniform(uniform: Uniform<*>, globals: GlobalProperties, currentValue: PossiblyEvaluatedPropertyValue): void { uniform.set(currentValue.constantOr(this.value)); } @@ -115,12 +116,12 @@ class ConstantBinder implements Binder { } } -class CrossFadedConstantBinder implements Binder { +class CrossFadedConstantBinder implements Binder, UniformBinder { uniformNames: Array; patternFrom: ?Array; patternTo: ?Array; - constructor(value: T, names: Array) { + constructor(value: mixed, names: Array) { this.uniformNames = names.map(name => `u_${name}`); this.patternFrom = null; this.patternTo = null; @@ -136,7 +137,7 @@ class CrossFadedConstantBinder implements Binder { this.patternFrom = posFrom.tlbr; } - setUniform(uniform: Uniform<*>, globals: GlobalProperties, currentValue: PossiblyEvaluatedPropertyValue, uniformName: string) { + setUniform(uniform: Uniform<*>, globals: GlobalProperties, currentValue: PossiblyEvaluatedPropertyValue, uniformName: string) { const pos = uniformName === 'u_pattern_to' ? this.patternTo : uniformName === 'u_pattern_from' ? this.patternFrom : null; @@ -148,9 +149,8 @@ class CrossFadedConstantBinder implements Binder { } } -class SourceExpressionBinder implements Binder { +class SourceExpressionBinder implements Binder { expression: SourceExpression; - uniformNames: Array; type: string; maxValue: number; @@ -161,7 +161,6 @@ class SourceExpressionBinder implements Binder { constructor(expression: SourceExpression, names: Array, type: string, PaintVertexArray: Class) { this.expression = expression; this.type = type; - this.uniformNames = []; this.maxValue = 0; this.paintVertexAttributes = names.map((name) => ({ @@ -215,15 +214,9 @@ class SourceExpressionBinder implements Binder { this.paintVertexBuffer.destroy(); } } - - setUniform() {} - - getBinding(context: Context, location: WebGLUniformLocation): Uniform1f { - return new Uniform1f(context, location); - } } -class CompositeExpressionBinder implements Binder { +class CompositeExpressionBinder implements Binder, UniformBinder { expression: CompositeExpression; uniformNames: Array; type: string; @@ -315,9 +308,8 @@ class CompositeExpressionBinder implements Binder { } } -class CrossFadedCompositeBinder implements Binder { +class CrossFadedCompositeBinder implements Binder { expression: CompositeExpression; - uniformNames: Array; type: string; useIntegerZoom: boolean; zoom: number; @@ -333,7 +325,6 @@ class CrossFadedCompositeBinder implements Binder { this.expression = expression; this.type = type; - this.uniformNames = []; this.useIntegerZoom = useIntegerZoom; this.zoom = zoom; this.layerId = layerId; @@ -398,12 +389,6 @@ class CrossFadedCompositeBinder implements Binder { if (this.zoomOutPaintVertexBuffer) this.zoomOutPaintVertexBuffer.destroy(); if (this.zoomInPaintVertexBuffer) this.zoomInPaintVertexBuffer.destroy(); } - - setUniform() {} - - getBinding(context: Context, location: WebGLUniformLocation): $Shape> { - return new Uniform1f(context, location); - } } /** @@ -427,7 +412,7 @@ class CrossFadedCompositeBinder implements Binder { * @private */ export default class ProgramConfiguration { - binders: { [string]: Binder }; + binders: { [string]: Binder }; cacheKey: string; layoutAttributes: Array; @@ -538,10 +523,12 @@ export default class ProgramConfiguration { const uniforms = []; for (const property in this.binders) { const binder = this.binders[property]; - for (const name of binder.uniformNames) { - if (locations[name]) { - const binding = binder.getBinding(context, locations[name]); - uniforms.push({name, property, binding}); + if (binder instanceof ConstantBinder || binder instanceof CrossFadedConstantBinder || binder instanceof CompositeExpressionBinder) { + for (const name of binder.uniformNames) { + if (locations[name]) { + const binding = binder.getBinding(context, locations[name]); + uniforms.push({name, property, binding}); + } } } } @@ -552,7 +539,7 @@ export default class ProgramConfiguration { // Uniform state bindings are owned by the Program, but we set them // from within the ProgramConfiguraton's binder members. for (const {name, property, binding} of binderUniforms) { - this.binders[property].setUniform(binding, globals, properties.get(property), name); + (this.binders[property]: any).setUniform(binding, globals, properties.get(property), name); } }