Skip to content

Reduce shader boilerplate, refactor "Bucket" - #2585

Merged
lucaswoj merged 7 commits into
masterfrom
pragma
May 23, 2016
Merged

lucaswoj merged 7 commits into
masterfrom
pragma

Conversation

@lucaswoj

Copy link
Copy Markdown
Contributor

Refactoring

This pull request contains some vocabulary refactoring within bucket, aiming for conceptual correctness. For example, the paintAttributes array contained much more data than just attributes.

Pragma Statements

This pull request allows us to expand property function styling support while keeping our codebase DRY and readable. It adds #pragma statements to the shaders which generate definition and initialization boilerplate.

Before

precision highp float;

uniform mat4 u_matrix;
uniform vec2 u_extrude_scale;
uniform float u_devicepixelratio;

attribute vec2 a_pos;

#ifdef ATTRIBUTE_A_COLOR
attribute lowp vec4 a_color;
#elif defined ATTRIBUTE_ZOOM_FUNCTION_A_COLOR0
uniform lowp float u_color_t;
attribute lowp vec4 a_color0;
attribute lowp vec4 a_color1;
attribute lowp vec4 a_color2;
attribute lowp vec4 a_color3;
#else
uniform lowp vec4 a_color;
#endif

#ifdef ATTRIBUTE_A_RADIUS
attribute mediump float a_radius;
#elif defined ATTRIBUTE_ZOOM_FUNCTION_A_RADIUS
uniform lowp float u_radius_t;
attribute mediump vec4 a_radius;
#else
uniform mediump float a_radius;
#endif

varying vec2 v_extrude;
varying lowp vec4 v_color;
varying lowp float v_antialiasblur;

float evaluate_zoom_function_1(const vec4 values, const float t) {
    if (t < 1.0) {
        return mix(values[0], values[1], t);
    } else if (t < 2.0) {
        return mix(values[1], values[2], t - 1.0);
    } else {
        return mix(values[2], values[3], t - 2.0);
    }
}

vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) {
    if (t < 1.0) {
        return mix(value0, value1, t);
    } else if (t < 2.0) {
        return mix(value1, value2, t - 1.0);
    } else {
        return mix(value2, value3, t - 2.0);
    }
}

void main(void) {

#ifdef ATTRIBUTE_A_RADIUS
    mediump float radius = a_radius / 10.0;
#elif defined ATTRIBUTE_ZOOM_FUNCTION_A_RADIUS
    mediump float radius = evaluate_zoom_function_1(a_radius, u_radius_t) / 10.0;
#else
    mediump float radius = a_radius;
#endif

    // unencode the extrusion vector that we snuck into the a_pos vector
    v_extrude = vec2(mod(a_pos, 2.0) * 2.0 - 1.0);

    vec2 extrude = v_extrude * radius * u_extrude_scale;
    // multiply a_pos by 0.5, since we had it * 2 in order to sneak
    // in extrusion data
    gl_Position = u_matrix * vec4(floor(a_pos * 0.5), 0, 1);

    gl_Position.xy += extrude;

#ifdef ATTRIBUTE_A_COLOR
    v_color = a_color / 255.0;
#elif defined ATTRIBUTE_ZOOM_FUNCTION_A_COLOR0
    v_color = evaluate_zoom_function_4(a_color0, a_color1, a_color2, a_color3, u_color_t) / 255.0;
#else
    v_color = a_color;
#endif

    // This is a minimum blur distance that serves as a faux-antialiasing for
    // the circle. since blur is a ratio of the circle's size and the intent is
    // to keep the blur at roughly 1px, the two are inversely related.
    v_antialiasblur = 1.0 / u_devicepixelratio / radius;
}

After

precision highp float;

uniform mat4 u_matrix;
uniform vec2 u_extrude_scale;
uniform float u_devicepixelratio;

attribute vec2 a_pos;

#pragma mapbox: define(color)
#pragma mapbox: define(radius)

varying vec2 v_extrude;
varying lowp float v_antialiasblur;

void main(void) {

    #pragma mapbox: initialize(color)
    #pragma mapbox: initialize(radius)

    // unencode the extrusion vector that we snuck into the a_pos vector
    v_extrude = vec2(mod(a_pos, 2.0) * 2.0 - 1.0);

    vec2 extrude = v_extrude * radius * u_extrude_scale;
    // multiply a_pos by 0.5, since we had it * 2 in order to sneak
    // in extrusion data
    gl_Position = u_matrix * vec4(floor(a_pos * 0.5), 0, 1);

    gl_Position.xy += extrude;

    // This is a minimum blur distance that serves as a faux-antialiasing for
    // the circle. since blur is a ratio of the circle's size and the intent is
    // to keep the blur at roughly 1px, the two are inversely related.
    v_antialiasblur = 1.0 / u_devicepixelratio / radius;
}

cc @jfirebaugh @mourner @ansis

' }' +
'}'
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be moved to a separate .glsl file?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Great idea.

@ansis

ansis commented May 19, 2016

Copy link
Copy Markdown
Contributor

Looks great

At first I was a bit skeptical about using pragmas. They add an extra layer of abstraction and make you read a bunch of extra files to understand what's actually going on in the shaders. It does make it a lot cleaner and reduces a ton of ugly repetition though. I think it's worth it!

Besides the couple of things I commented on in the diff this looks ready

@jfirebaugh

Copy link
Copy Markdown
Contributor

Should we build on glslify explicitly for the shader rewriting functionality?

@lucaswoj

Copy link
Copy Markdown
Contributor Author

As best I can tell, glslify does not provide any functionality that affects this feature. glslify's API allows us to import and export functions from other glsl files but not inject definitions at compile time. glsl also has a system for source transforms but we don't seem to gain anything by using that vs replacing the pragmas ourselves.

@lucaswoj
lucaswoj force-pushed the pragma branch 2 times, most recently from b8be27b to 0c5fd69 Compare May 20, 2016 20:58
@lucaswoj

lucaswoj commented May 20, 2016 •

Copy link
Copy Markdown
Contributor Author

I managed to find a somewhat elegant design for parameterized pragmas, leveraging Javascript's String#replace API.

As of this commit, a pragma defined as

{
   'define mapbox': 'attribute $1 float a_mapbox;'
}

Will replace a pragma written as

#pragma mapbox: define mapbox highp

With this code

attribute highp float a_mapbox;

@lucaswoj
lucaswoj merged commit 36749dc into master May 23, 2016
@lucaswoj
lucaswoj deleted the pragma branch May 23, 2016 20:08
defines += '#define ' + macros[m] + '\n';
}
var definesSource = '';
for (var j = 0; definesSource && j < definesSource.length; j++) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

definesSource && j < definesSource.length is always false, because definesSource is an empty string. Is this intended?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line should read defines && j < defines.length. Unfortunately, this codepath is only used by the overdraw inspector, which is not tested. I'll push a fix shortly.

lucaswoj pushed a commit that referenced this pull request Dec 13, 2016
* Rename macros to defines

* Rename addPaintAttributes to populatePaintArrays

* Add support for pragmas in useProgram

* Replace shader boilerplate with pragmas

* Inject shader functions at compiletime

* Move shader utility functions into a glsl file

* Support arguments in shader pragmas, remove precision from bucket definition
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants