Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Factor out packUint8Pair() helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
Anand Thakker committed Mar 29, 2017
1 parent db8c77a commit 551828d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
10 changes: 10 additions & 0 deletions src/mbgl/programs/attributes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
namespace mbgl {
namespace attributes {

/*
* Pack a pair of values, interpreted as uint8's, into a single float.
* Used to conserve vertex attributes. Values are unpacked in the vertex
* shader using the `unpack_float()` function, defined in _prelude.vertex.glsl.
*/
template <typename T>
inline uint16_t packUint8Pair(T a, T b) {
return static_cast<uint16_t>(a) * 256 + static_cast<uint16_t>(b);
}

// Layout attributes

MBGL_DEFINE_ATTRIBUTE(int16_t, 2, a_pos);
Expand Down
14 changes: 9 additions & 5 deletions src/mbgl/programs/symbol_program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,17 @@ struct SymbolLayoutAttributes : gl::Attributes<
}},
{{
static_cast<uint16_t>(tx / 4),
static_cast<uint16_t>(ty / 4)
static_cast<uint16_t>(ty / 4),
mbgl::attributes::packUint8Pair(
static_cast<uint8_t>(labelminzoom * 10), // 1/10 zoom levels: z16 == 160
static_cast<uint8_t>(labelangle)
),
mbgl::attributes::packUint8Pair(
static_cast<uint8_t>(minzoom * 10),
static_cast<uint8_t>(::fmin(maxzoom, 25) * 10)
)
}},
{{
static_cast<uint8_t>(labelminzoom * 10), // 1/10 zoom levels: z16 == 160
static_cast<uint8_t>(labelangle),
static_cast<uint8_t>(minzoom * 10),
static_cast<uint8_t>(::fmin(maxzoom, 25) * 10)
}}
};
}
Expand Down
10 changes: 3 additions & 7 deletions src/mbgl/style/paint_property_binder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,12 @@ inline std::array<float, 1> attributeValue(float v) {
uses 8-bit precision for each color component, for each float we use the upper 8
bits for one component (e.g. (color.r * 255) * 256), and the lower 8 for another.
Also note:
- Colors come in as floats 0..1, so we scale by 255.
- Casting the scaled values to ints is important: without doing this, e.g., the
fractional part of the `r` component would corrupt the lower-8 bits of the encoded
value, which must be reserved for the `g` component.
Also note that colors come in as floats 0..1, so we scale by 255.
*/
inline std::array<float, 2> attributeValue(const Color& color) {
return {{
static_cast<float>(static_cast<uint16_t>(color.r * 255) * 256 + static_cast<uint16_t>(color.g * 255)),
static_cast<float>(static_cast<uint16_t>(color.b * 255) * 256 + static_cast<uint16_t>(color.a * 255))
static_cast<float>(mbgl::attributes::packUint8Pair(255 * color.r, 255 * color.g)),
static_cast<float>(mbgl::attributes::packUint8Pair(255 * color.b, 255 * color.a))
}};
}

Expand Down

0 comments on commit 551828d

Please sign in to comment.