diff --git a/js/data/program_configuration.js b/js/data/program_configuration.js index 72184412f47..c0d4085cc20 100644 --- a/js/data/program_configuration.js +++ b/js/data/program_configuration.js @@ -25,8 +25,7 @@ class ProgramConfiguration { this.attributes = []; this.uniforms = []; this.interpolationUniforms = []; - this.vertexPragmas = {}; - this.fragmentPragmas = {}; + this.pragmas = {vertex: {}, fragment: {}}; this.cacheKey = ''; } @@ -149,13 +148,24 @@ class ProgramConfiguration { } getFragmentPragmas(name) { - this.fragmentPragmas[name] = this.fragmentPragmas[name] || {define: [], initialize: []}; - return this.fragmentPragmas[name]; + const frag = this.pragmas.fragment; + frag[name] = frag[name] || {define: [], initialize: []}; + return frag[name]; } getVertexPragmas(name) { - this.vertexPragmas[name] = this.vertexPragmas[name] || {define: [], initialize: []}; - return this.vertexPragmas[name]; + const vert = this.pragmas.vertex; + vert[name] = vert[name] || {define: [], initialize: []}; + return vert[name]; + } + + applyPragmas(source, shaderType) { + return source.replace(/#pragma mapbox: ([\w]+) ([\w]+) ([\w]+) ([\w]+)/g, (match, operation, precision, type, name) => { + return this.pragmas[shaderType][name][operation] + .join('\n') + .replace(/{type}/g, type) + .replace(/{precision}/g, precision); + }); } populatePaintArray(layer, paintArray, length, globalProperties, featureProperties) { diff --git a/js/render/painter.js b/js/render/painter.js index 9c3850be088..f3e7c01cbe4 100644 --- a/js/render/painter.js +++ b/js/render/painter.js @@ -384,13 +384,13 @@ class Painter { } const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER); - gl.shaderSource(fragmentShader, applyPragmas(definesSource + definition.fragmentSource, configuration.fragmentPragmas)); + gl.shaderSource(fragmentShader, configuration.applyPragmas(definesSource + definition.fragmentSource, 'fragment')); gl.compileShader(fragmentShader); assert(gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS), gl.getShaderInfoLog(fragmentShader)); gl.attachShader(program, fragmentShader); const vertexShader = gl.createShader(gl.VERTEX_SHADER); - gl.shaderSource(vertexShader, applyPragmas(definesSource + shaders.util + definition.vertexSource, configuration.vertexPragmas)); + gl.shaderSource(vertexShader, configuration.applyPragmas(definesSource + shaders.util + definition.vertexSource, 'vertex')); gl.compileShader(vertexShader); assert(gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS), gl.getShaderInfoLog(vertexShader)); gl.attachShader(program, vertexShader); @@ -435,13 +435,4 @@ class Painter { } } -function applyPragmas(source, pragmas) { - return source.replace(/#pragma mapbox: ([\w]+) ([\w]+) ([\w]+) ([\w]+)/g, (match, operation, precision, type, name) => { - return pragmas[name][operation] - .join('\n') - .replace(/{type}/g, type) - .replace(/{precision}/g, precision); - }); -} - module.exports = Painter;