[core] Reduce number of varyings to 8 or less

For #pragmas, don't generate varyings for attributes that aren't used by the fragment shader. Pack other varyings more tightly.
This commit is contained in:
John Firebaugh
2017-06-12 16:39:59 -07:00
parent 7bfcce0266
commit 441b80d574
6 changed files with 71 additions and 81 deletions

View File

@@ -61,42 +61,31 @@ ${fragmentPrelude}
'symbol_icon',
'symbol_sdf'
].forEach(function (shaderName) {
function applyPragmas(source, pragmas) {
return source.replace(/#pragma mapbox: ([\w]+) ([\w]+) ([\w]+) ([\w]+)/g, (match, operation, precision, type, name) => {
const a_type = type === "float" ? "vec2" : "vec4";
return pragmas[operation]
.join("\n")
.replace(/\{type\}/g, type)
.replace(/\{a_type}/g, a_type)
.replace(/\{precision\}/g, precision)
.replace(/\{name\}/g, name);
});
}
const re = /#pragma mapbox: ([\w]+) ([\w]+) ([\w]+) ([\w]+)/g;
const fragmentPragmas = new Set();
function vertexSource() {
const source = fs.readFileSync(path.join(inputPath, shaderName + '.vertex.glsl'), 'utf8');
return applyPragmas(source, {
define: [
"uniform lowp float a_{name}_t;",
"attribute {precision} {a_type} a_{name};",
"varying {precision} {type} {name};"
],
initialize: [
"{name} = unpack_mix_{a_type}(a_{name}, a_{name}_t);"
]
});
}
let fragmentSource = fs.readFileSync(path.join(inputPath, shaderName + '.fragment.glsl'), 'utf8');
let vertexSource = fs.readFileSync(path.join(inputPath, shaderName + '.vertex.glsl'), 'utf8');
function fragmentSource() {
const source = fs.readFileSync(path.join(inputPath, shaderName + '.fragment.glsl'), 'utf8');
return applyPragmas(source, {
define: [
"varying {precision} {type} {name};"
],
initialize: [
]
});
}
fragmentSource = fragmentSource.replace(re, (match, operation, precision, type, name) => {
fragmentPragmas.add(name);
return operation === "define" ?
`varying ${precision} ${type} ${name};` :
``;
});
vertexSource = vertexSource.replace(re, (match, operation, precision, type, name) => {
const a_type = type === "float" ? "vec2" : "vec4";
if (fragmentPragmas.has(name)) {
return operation === "define" ?
`uniform lowp float a_${name}_t;\nattribute ${precision} ${a_type} a_${name};\nvarying ${precision} ${type} ${name};` :
`${name} = unpack_mix_${a_type}(a_${name}, a_${name}_t);`;
} else {
return operation === "define" ?
`uniform lowp float a_${name}_t;\nattribute ${precision} ${a_type} a_${name};` :
`${precision} ${type} ${name} = unpack_mix_${a_type}(a_${name}, a_${name}_t);`;
}
});
writeIfModified(path.join(outputPath, `${shaderName}.hpp`), `// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.
@@ -125,10 +114,10 @@ namespace shaders {
const char* ${shaderName}::name = "${shaderName}";
const char* ${shaderName}::vertexSource = R"MBGL_SHADER(
${vertexSource()}
${vertexSource}
)MBGL_SHADER";
const char* ${shaderName}::fragmentSource = R"MBGL_SHADER(
${fragmentSource()}
${fragmentSource}
)MBGL_SHADER";
} // namespace shaders

View File

@@ -35,8 +35,7 @@ uniform lowp float a_stroke_opacity_t;
attribute lowp vec2 a_stroke_opacity;
varying lowp float stroke_opacity;
varying vec2 v_extrude;
varying lowp float v_antialiasblur;
varying vec3 v_data;
void main(void) {
color = unpack_mix_vec4(a_color, a_color_t);
@@ -48,23 +47,24 @@ void main(void) {
stroke_opacity = unpack_mix_vec2(a_stroke_opacity, a_stroke_opacity_t);
// 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 = vec2(mod(a_pos, 2.0) * 2.0 - 1.0);
vec2 extrude = v_extrude * (radius + stroke_width) * 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);
if (u_scale_with_map) {
gl_Position.xy += extrude;
gl_Position.xy += extrude * (radius + stroke_width) * u_extrude_scale;
} else {
gl_Position.xy += extrude * gl_Position.w;
gl_Position.xy += extrude * (radius + stroke_width) * u_extrude_scale * gl_Position.w;
}
// 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 / DEVICE_PIXEL_RATIO / (radius + stroke_width);
lowp float antialiasblur = 1.0 / DEVICE_PIXEL_RATIO / (radius + stroke_width);
v_data = vec3(extrude.x, extrude.y, antialiasblur);
}
)MBGL_SHADER";
@@ -77,8 +77,7 @@ varying highp vec4 stroke_color;
varying mediump float stroke_width;
varying lowp float stroke_opacity;
varying vec2 v_extrude;
varying lowp float v_antialiasblur;
varying vec3 v_data;
void main() {
@@ -89,8 +88,11 @@ void main() {
float extrude_length = length(v_extrude);
float antialiased_blur = -max(blur, v_antialiasblur);
vec2 extrude = v_data.xy;
float extrude_length = length(extrude);
lowp float antialiasblur = v_data.z;
float antialiased_blur = -max(blur, antialiasblur);
float opacity_t = smoothstep(0.0, antialiased_blur, extrude_length - 1.0);

View File

@@ -44,17 +44,15 @@ attribute lowp vec2 a_opacity;
varying lowp float opacity;
uniform lowp float a_gapwidth_t;
attribute mediump vec2 a_gapwidth;
varying mediump float gapwidth;
uniform lowp float a_offset_t;
attribute lowp vec2 a_offset;
varying lowp float offset;
void main() {
color = unpack_mix_vec4(a_color, a_color_t);
blur = unpack_mix_vec2(a_blur, a_blur_t);
opacity = unpack_mix_vec2(a_opacity, a_opacity_t);
gapwidth = unpack_mix_vec2(a_gapwidth, a_gapwidth_t);
offset = unpack_mix_vec2(a_offset, a_offset_t);
mediump float gapwidth = unpack_mix_vec2(a_gapwidth, a_gapwidth_t);
lowp float offset = unpack_mix_vec2(a_offset, a_offset_t);
vec2 a_extrude = a_data.xy - 128.0;
float a_direction = mod(a_data.z, 4.0) - 1.0;

View File

@@ -44,16 +44,14 @@ attribute lowp vec2 a_opacity;
varying lowp float opacity;
uniform lowp float a_offset_t;
attribute lowp vec2 a_offset;
varying lowp float offset;
uniform lowp float a_gapwidth_t;
attribute mediump vec2 a_gapwidth;
varying mediump float gapwidth;
void main() {
blur = unpack_mix_vec2(a_blur, a_blur_t);
opacity = unpack_mix_vec2(a_opacity, a_opacity_t);
offset = unpack_mix_vec2(a_offset, a_offset_t);
gapwidth = unpack_mix_vec2(a_gapwidth, a_gapwidth_t);
lowp float offset = unpack_mix_vec2(a_offset, a_offset_t);
mediump float gapwidth = unpack_mix_vec2(a_gapwidth, a_gapwidth_t);
vec2 a_extrude = a_data.xy - 128.0;
float a_direction = mod(a_data.z, 4.0) - 1.0;

View File

@@ -52,17 +52,15 @@ attribute lowp vec2 a_opacity;
varying lowp float opacity;
uniform lowp float a_gapwidth_t;
attribute mediump vec2 a_gapwidth;
varying mediump float gapwidth;
uniform lowp float a_offset_t;
attribute lowp vec2 a_offset;
varying lowp float offset;
void main() {
color = unpack_mix_vec4(a_color, a_color_t);
blur = unpack_mix_vec2(a_blur, a_blur_t);
opacity = unpack_mix_vec2(a_opacity, a_opacity_t);
gapwidth = unpack_mix_vec2(a_gapwidth, a_gapwidth_t);
offset = unpack_mix_vec2(a_offset, a_offset_t);
mediump float gapwidth = unpack_mix_vec2(a_gapwidth, a_gapwidth_t);
lowp float offset = unpack_mix_vec2(a_offset, a_offset_t);
vec2 a_extrude = a_data.xy - 128.0;
float a_direction = mod(a_data.z, 4.0) - 1.0;

View File

@@ -57,10 +57,8 @@ uniform vec2 u_extrude_scale;
uniform vec2 u_texsize;
varying vec2 v_tex;
varying vec2 v_fade_tex;
varying float v_gamma_scale;
varying float v_size;
varying vec4 v_data0;
varying vec2 v_data1;
void main() {
fill_color = unpack_mix_vec4(a_fill_color, a_fill_color_t);
@@ -81,6 +79,7 @@ void main() {
mediump vec2 a_zoom = unpack_float(a_data[3]);
mediump float a_minzoom = a_zoom[0];
mediump float a_maxzoom = a_zoom[1];
float size;
// In order to accommodate placing labels around corners in
// symbol-placement: line, each glyph in a label could have multiple
@@ -92,22 +91,22 @@ void main() {
// based on the scale of rendered text size relative to layout text size.
mediump float layoutSize;
if (!u_is_size_zoom_constant && !u_is_size_feature_constant) {
v_size = mix(a_size[0], a_size[1], u_size_t) / 10.0;
size = mix(a_size[0], a_size[1], u_size_t) / 10.0;
layoutSize = a_size[2] / 10.0;
} else if (u_is_size_zoom_constant && !u_is_size_feature_constant) {
v_size = a_size[0] / 10.0;
layoutSize = v_size;
size = a_size[0] / 10.0;
layoutSize = size;
} else if (!u_is_size_zoom_constant && u_is_size_feature_constant) {
v_size = u_size;
size = u_size;
layoutSize = u_layout_size;
} else {
v_size = u_size;
size = u_size;
layoutSize = u_size;
}
float fontScale = u_is_text ? v_size / 24.0 : v_size;
float fontScale = u_is_text ? size / 24.0 : size;
mediump float zoomAdjust = log2(v_size / layoutSize);
mediump float zoomAdjust = log2(size / layoutSize);
mediump float adjustedZoom = (u_zoom - zoomAdjust) * 10.0;
// result: z = 0 if a_minzoom <= adjustedZoom < a_maxzoom, and 1 otherwise
// Used below to move the vertex out of the clip space for when the current
@@ -156,10 +155,13 @@ void main() {
gl_Position = u_matrix * vec4(a_pos, 0, 1) + vec4(extrude, 0, 0);
}
v_gamma_scale = gl_Position.w;
float gamma_scale = gl_Position.w;
v_tex = a_tex / u_texsize;
v_fade_tex = vec2(a_labelminzoom / 255.0, 0.0);
vec2 tex = a_tex / u_texsize;
vec2 fade_tex = vec2(a_labelminzoom / 255.0, 0.0);
v_data0 = vec4(tex.x, tex.y, fade_tex.x, fade_tex.y);
v_data1 = vec2(gamma_scale, size);
}
)MBGL_SHADER";
@@ -179,10 +181,8 @@ uniform sampler2D u_fadetexture;
uniform highp float u_gamma_scale;
uniform bool u_is_text;
varying vec2 v_tex;
varying vec2 v_fade_tex;
varying float v_gamma_scale;
varying float v_size;
varying vec4 v_data0;
varying vec2 v_data1;
void main() {
@@ -191,7 +191,12 @@ void main() {
float fontScale = u_is_text ? v_size / 24.0 : v_size;
vec2 tex = v_data0.xy;
vec2 fade_tex = v_data0.zw;
float gamma_scale = v_data1.x;
float size = v_data1.y;
float fontScale = u_is_text ? size / 24.0 : size;
lowp vec4 color = fill_color;
highp float gamma = EDGE_GAMMA / (fontScale * u_gamma_scale);
@@ -202,9 +207,9 @@ void main() {
buff = (6.0 - halo_width / fontScale) / SDF_PX;
}
lowp float dist = texture2D(u_texture, v_tex).a;
lowp float fade_alpha = texture2D(u_fadetexture, v_fade_tex).a;
highp float gamma_scaled = gamma * v_gamma_scale;
lowp float dist = texture2D(u_texture, tex).a;
lowp float fade_alpha = texture2D(u_fadetexture, fade_tex).a;
highp float gamma_scaled = gamma * gamma_scale;
highp float alpha = smoothstep(buff - gamma_scaled, buff + gamma_scaled, dist) * fade_alpha;
gl_FragColor = color * (alpha * opacity);