[core] Convert style properties to a tuple-based approach

This converts the style property classes (CirclePaintProperties and so on) to the same tuple-based approach as gl::Attribute and gl::Uniform. The approach is outlined in https://github.com/mapbox/cpp/blob/master/C%2B%2B%20Structural%20Metaprogramming.md.

The main advantage of this approach is it allows writing algorithms that work on sets of style properties, without resorting to code generation or manually repetitive code. This lets us iterate on approaches to data-driven properties more easily.

Another advantage is that the cascading, unevaluated, and evaluated states of a set of properties exist as independent structures, instead of individual properties holding their own state. This is a more functional approach that makes data flow clearer and reduces state.
This commit is contained in:
John Firebaugh
2016-10-30 11:06:59 -07:00
parent d4fc66af39
commit 38fcbe21d4
69 changed files with 1212 additions and 1058 deletions

View File

@@ -8,10 +8,12 @@
namespace mbgl {
using namespace style;
static_assert(sizeof(LineAttributes::Vertex) == 8, "expected LineVertex size");
template <class Values, class...Args>
Values makeValues(const style::LinePaintProperties& properties,
Values makeValues(const LinePaintProperties::Evaluated& properties,
const RenderTile& tile,
const TransformState& state,
Args&&... args) {
@@ -27,14 +29,16 @@ Values makeValues(const style::LinePaintProperties& properties,
float x = state.getSize().height / 2.0f * std::tan(state.getPitch());
return Values {
uniforms::u_matrix::Value{ tile.translatedMatrix(properties.lineTranslate.value,
properties.lineTranslateAnchor.value,
state) },
uniforms::u_opacity::Value{ properties.lineOpacity.value },
uniforms::u_width::Value{ properties.lineWidth.value },
uniforms::u_gapwidth::Value{ properties.lineGapWidth.value },
uniforms::u_blur::Value{ properties.lineBlur.value },
uniforms::u_offset::Value{ properties.lineOffset.value },
uniforms::u_matrix::Value{
tile.translatedMatrix(properties.get<LineTranslate>(),
properties.get<LineTranslateAnchor>(),
state)
},
uniforms::u_opacity::Value{ properties.get<LineOpacity>() },
uniforms::u_width::Value{ properties.get<LineWidth>() },
uniforms::u_gapwidth::Value{ properties.get<LineGapWidth>() },
uniforms::u_blur::Value{ properties.get<LineBlur>() },
uniforms::u_offset::Value{ properties.get<LineOffset>() },
uniforms::u_antialiasingmatrix::Value{ antialiasingMatrix },
uniforms::u_ratio::Value{ 1.0f / tile.id.pixelsToTileUnits(1.0, state.getZoom()) },
uniforms::u_extra::Value{ (topedgelength + x) / topedgelength - 1.0f },
@@ -43,19 +47,19 @@ Values makeValues(const style::LinePaintProperties& properties,
}
LineProgram::UniformValues
LineProgram::uniformValues(const style::LinePaintProperties& properties,
LineProgram::uniformValues(const LinePaintProperties::Evaluated& properties,
const RenderTile& tile,
const TransformState& state) {
return makeValues<LineProgram::UniformValues>(
properties,
tile,
state,
uniforms::u_color::Value{ properties.lineColor.value }
uniforms::u_color::Value{ properties.get<LineColor>() }
);
}
LineSDFProgram::UniformValues
LineSDFProgram::uniformValues(const style::LinePaintProperties& properties,
LineSDFProgram::uniformValues(const LinePaintProperties::Evaluated& properties,
float pixelRatio,
const RenderTile& tile,
const TransformState& state,
@@ -63,8 +67,8 @@ LineSDFProgram::uniformValues(const style::LinePaintProperties& properties,
const LinePatternPos& posB,
float dashLineWidth,
float atlasWidth) {
const float widthA = posA.width * properties.lineDasharray.value.fromScale * dashLineWidth;
const float widthB = posB.width * properties.lineDasharray.value.toScale * dashLineWidth;
const float widthA = posA.width * properties.get<LineDasharray>().fromScale * dashLineWidth;
const float widthB = posB.width * properties.get<LineDasharray>().toScale * dashLineWidth;
std::array<float, 2> scaleA {{
1.0f / tile.id.pixelsToTileUnits(widthA, state.getIntegerZoom()),
@@ -80,30 +84,30 @@ LineSDFProgram::uniformValues(const style::LinePaintProperties& properties,
properties,
tile,
state,
uniforms::u_color::Value{ properties.lineColor.value },
uniforms::u_color::Value{ properties.get<LineColor>() },
uniforms::u_patternscale_a::Value{ scaleA },
uniforms::u_patternscale_b::Value{ scaleB },
uniforms::u_tex_y_a::Value{ posA.y },
uniforms::u_tex_y_b::Value{ posB.y },
uniforms::u_mix::Value{ properties.lineDasharray.value.t },
uniforms::u_mix::Value{ properties.get<LineDasharray>().t },
uniforms::u_sdfgamma::Value{ atlasWidth / (std::min(widthA, widthB) * 256.0f * pixelRatio) / 2.0f },
uniforms::u_image::Value{ 0 }
);
}
LinePatternProgram::UniformValues
LinePatternProgram::uniformValues(const style::LinePaintProperties& properties,
LinePatternProgram::uniformValues(const LinePaintProperties::Evaluated& properties,
const RenderTile& tile,
const TransformState& state,
const SpriteAtlasPosition& posA,
const SpriteAtlasPosition& posB) {
std::array<float, 2> sizeA {{
tile.id.pixelsToTileUnits(posA.size[0] * properties.linePattern.value.fromScale, state.getIntegerZoom()),
tile.id.pixelsToTileUnits(posA.size[0] * properties.get<LinePattern>().fromScale, state.getIntegerZoom()),
posA.size[1]
}};
std::array<float, 2> sizeB {{
tile.id.pixelsToTileUnits(posB.size[0] * properties.linePattern.value.toScale, state.getIntegerZoom()),
tile.id.pixelsToTileUnits(posB.size[0] * properties.get<LinePattern>().toScale, state.getIntegerZoom()),
posB.size[1]
}};
@@ -117,7 +121,7 @@ LinePatternProgram::uniformValues(const style::LinePaintProperties& properties,
uniforms::u_pattern_br_b::Value{ posB.br },
uniforms::u_pattern_size_a::Value{ sizeA },
uniforms::u_pattern_size_b::Value{ sizeB },
uniforms::u_fade::Value{ properties.linePattern.value.t },
uniforms::u_fade::Value{ properties.get<LinePattern>().t },
uniforms::u_image::Value{ 0 }
);
}