Skip to content

Commit

Permalink
move back applyPragmas to program configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Nov 5, 2016
1 parent 91aabf4 commit 4fcb5c9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
22 changes: 16 additions & 6 deletions js/data/program_configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ class ProgramConfiguration {
this.attributes = [];
this.uniforms = [];
this.interpolationUniforms = [];
this.vertexPragmas = {};
this.fragmentPragmas = {};
this.pragmas = {vertex: {}, fragment: {}};
this.cacheKey = '';
}

Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 4 additions & 12 deletions js/render/painter.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Painter {
this.lineWidthRange = gl.getParameter(gl.ALIASED_LINE_WIDTH_RANGE);

this.basicFillProgramConfiguration = ProgramConfiguration.createStatic(['color', 'opacity']);
this.emptyProgramConfiguration = new ProgramConfiguration();
}

/*
Expand Down Expand Up @@ -384,13 +385,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);
Expand Down Expand Up @@ -424,7 +425,7 @@ class Painter {

useProgram(name, programConfiguration) {
const gl = this.gl;
const nextProgram = this._createProgramCached(name, programConfiguration || {});
const nextProgram = this._createProgramCached(name, programConfiguration || this.emptyProgramConfiguration);

if (this.currentProgram !== nextProgram) {
gl.useProgram(nextProgram.program);
Expand All @@ -435,13 +436,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;

0 comments on commit 4fcb5c9

Please sign in to comment.