Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify data-driven paint attributes implementation #3527

Merged
merged 20 commits into from
Nov 8, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
unify uniform pragmas configuration
mourner committed Nov 8, 2016
commit e1a6d5947abcb2d478c25d6b3b52a6e823d809d1
48 changes: 26 additions & 22 deletions js/data/program_configuration.js
Original file line number Diff line number Diff line change
@@ -41,17 +41,12 @@ class ProgramConfiguration {
const name = attribute.name.slice(2);
const multiplier = attribute.multiplier || (isColor ? 255 : 1);

const frag = self.fragmentPragmas[name] = {define: [], initialize: []};
const vert = self.vertexPragmas[name] = {define: [], initialize: []};
const frag = self.getFragmentPragmas(name);
const vert = self.getVertexPragmas(name);

if (layer.isPaintValueFeatureConstant(attribute.paintProperty)) {
self.uniforms.push(util.extend({}, attribute, {isColor}));

frag.define.push(`uniform {precision} {type} ${inputName};`);
vert.define.push(`uniform {precision} {type} ${inputName};`);

frag.initialize.push(`{precision} {type} ${name} = ${inputName};`);
vert.initialize.push(`{precision} {type} ${name} = ${inputName};`);
self.addUniform(name, inputName);

} else if (layer.isPaintValueZoomConstant(attribute.paintProperty)) {
self.attributes.push(util.extend({}, attribute, {components: isColor ? 4 : 1, multiplier}));
@@ -80,6 +75,7 @@ class ProgramConfiguration {

frag.define.push(`varying {precision} {type} ${name};`);
vert.define.push(`varying {precision} {type} ${name};`);

vert.define.push(`uniform lowp float ${tName};`);

self.uniforms.push({
@@ -120,28 +116,36 @@ class ProgramConfiguration {
return self;
}

static createStatic(uniforms) {
static createStatic(uniformNames) {
const self = new ProgramConfiguration();

for (const uniform of uniforms) {
assert(uniform.name.indexOf('u_') === 0);
const name = uniform.name.slice(2);
for (const name of uniformNames) {
self.addUniform(name, `u_${name}`);
}
self.cacheKey = JSON.stringify(self.fragmentPragmas);

const frag = self.fragmentPragmas[name] = {define: [], initialize: []};
const vert = self.vertexPragmas[name] = {define: [], initialize: []};
return self;
}

const type = `${uniform.components === 1 ? 'float' : `vec${uniform.components}`}`;
addUniform(name, inputName) {
const frag = this.getFragmentPragmas(name);
const vert = this.getVertexPragmas(name);

frag.define.push(`uniform {precision} ${type} u_${name};`);
vert.define.push(`uniform {precision} ${type} u_${name};`);
frag.define.push(`uniform {precision} {type} ${inputName};`);
vert.define.push(`uniform {precision} {type} ${inputName};`);

frag.initialize.push(`{precision} ${type} ${name} = u_${name};`);
vert.initialize.push(`{precision} ${type} ${name} = u_${name};`);
}
frag.initialize.push(`{precision} {type} ${name} = ${inputName};`);
vert.initialize.push(`{precision} {type} ${name} = ${inputName};`);
}

self.cacheKey = JSON.stringify(self.fragmentPragmas);
getFragmentPragmas(name) {
this.fragmentPragmas[name] = this.fragmentPragmas[name] || {define: [], initialize: []};
return this.fragmentPragmas[name];
}

return self;
getVertexPragmas(name) {
this.vertexPragmas[name] = this.vertexPragmas[name] || {define: [], initialize: []};
return this.vertexPragmas[name];
}

populatePaintArray(layer, paintArray, length, globalProperties, featureProperties) {
16 changes: 6 additions & 10 deletions js/render/painter.js
Original file line number Diff line number Diff line change
@@ -49,10 +49,7 @@ class Painter {

this.lineWidthRange = gl.getParameter(gl.ALIASED_LINE_WIDTH_RANGE);

this.basicFillProgramConfiguration = ProgramConfiguration.createStatic([
{name: 'u_color', components: 4},
{name: 'u_opacity', components: 1}
]);
this.basicFillProgramConfiguration = ProgramConfiguration.createStatic(['color', 'opacity']);
this.emptyProgramConfiguration = ProgramConfiguration.createStatic([]);
}

@@ -359,20 +356,19 @@ class Painter {
}
}

_createProgramCached(name, paintAttributeSet) {
_createProgramCached(name, programConfiguration) {
this.cache = this.cache || {};
const key = `${name}${paintAttributeSet.cacheKey}${!!this._showOverdrawInspector}`;
const key = `${name}${programConfiguration.cacheKey}${!!this._showOverdrawInspector}`;
if (!this.cache[key]) {
this.cache[key] = paintAttributeSet.createProgram(name, this._showOverdrawInspector, this.gl);
this.cache[key] = programConfiguration.createProgram(name, this._showOverdrawInspector, this.gl);
}
return this.cache[key];
}

useProgram(name, paintAttributeSet) {
useProgram(name, programConfiguration) {
const gl = this.gl;

const nextProgram = this._createProgramCached(name,
paintAttributeSet || this.emptyProgramConfiguration);
const nextProgram = this._createProgramCached(name, programConfiguration || this.emptyProgramConfiguration);
const previousProgram = this.currentProgram;

if (previousProgram !== nextProgram) {