From e5e03d46a0ce92655f96077d679ec01977ebc37f Mon Sep 17 00:00:00 2001 From: Olivier Guyot Date: Thu, 31 Oct 2019 10:50:24 +0100 Subject: [PATCH] Webgl points renderer / more optimizations Simplify calls in the attributes callback, also less stress on garbage collection. --- src/ol/style/expressions.js | 18 ++++++++++++++---- src/ol/webgl/ShaderBuilder.js | 10 +++++----- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/ol/style/expressions.js b/src/ol/style/expressions.js index 889e33b921..6527164bf2 100644 --- a/src/ol/style/expressions.js +++ b/src/ol/style/expressions.js @@ -203,6 +203,19 @@ export function colorToGlsl(color) { ); } +/** + * Returns a stable equivalent number for the string literal. + * @param {ParsingContext} context Parsing context + * @param {string} string String literal value + * @returns {number} Number equivalent + */ +export function getStringNumberEquivalent(context, string) { + if (context.stringLiteralsMap[string] === undefined) { + context.stringLiteralsMap[string] = Object.keys(context.stringLiteralsMap).length; + } + return context.stringLiteralsMap[string]; +} + /** * Returns a stable equivalent number for the string literal, for use in shaders. This number is then * converted to be a GLSL-compatible string. @@ -211,10 +224,7 @@ export function colorToGlsl(color) { * @returns {string} GLSL-compatible string containing a number */ export function stringToGlsl(context, string) { - if (context.stringLiteralsMap[string] === undefined) { - context.stringLiteralsMap[string] = Object.keys(context.stringLiteralsMap).length; - } - return numberToGlsl(context.stringLiteralsMap[string]); + return numberToGlsl(getStringNumberEquivalent(context, string)); } /** diff --git a/src/ol/webgl/ShaderBuilder.js b/src/ol/webgl/ShaderBuilder.js index bea9a05926..1ccd598583 100644 --- a/src/ol/webgl/ShaderBuilder.js +++ b/src/ol/webgl/ShaderBuilder.js @@ -3,7 +3,7 @@ * @module ol/webgl/ShaderBuilder */ -import {expressionToGlsl, stringToGlsl, ValueTypes} from '../style/expressions.js'; +import {expressionToGlsl, getStringNumberEquivalent, ValueTypes} from '../style/expressions.js'; /** * @typedef {Object} VaryingDescription @@ -450,7 +450,7 @@ export function parseLiteralStyle(style) { } let value = style.variables[varName]; if (typeof value === 'string') { - value = parseFloat(stringToGlsl(vertContext, value)); + value = getStringNumberEquivalent(vertContext, value); } return value !== undefined ? value : -9999999; // to avoid matching with the first string literal }; @@ -484,10 +484,10 @@ export function parseLiteralStyle(style) { attributes: vertContext.attributes.map(function(attributeName) { return { name: attributeName, - callback: function(feature) { - let value = feature.get(attributeName); + callback: function(feature, props) { + let value = props[attributeName]; if (typeof value === 'string') { - value = parseFloat(stringToGlsl(vertContext, value)); + value = getStringNumberEquivalent(vertContext, value); } return value !== undefined ? value : -9999999; // to avoid matching with the first string literal }