11 static constexpr
const char* vertex = R
"(#ifdef GL_ES
12 precision highp float;
29 // Unpack a pair of values that have been packed into a single float.
30 // The packed values are assumed to be 8-bit unsigned integers, and are
32 // packedValue = floor(input[0]) * 256 + input[1],
33 vec2 unpack_float(const float packedValue) {
34 int packedIntValue = int(packedValue);
35 int v0 = packedIntValue / 256;
36 return vec2(v0, packedIntValue - v0 * 256);
39 vec2 unpack_opacity(const float packedOpacity) {
40 int intOpacity = int(packedOpacity) / 2;
41 return vec2(float(intOpacity) / 127.0, mod(packedOpacity, 2.0));
44 // To minimize the number of attributes needed, we encode a 4-component
45 // color into a pair of floats (i.e. a vec2) as follows:
46 // [ floor(color.r * 255) * 256 + color.g * 255,
47 // floor(color.b * 255) * 256 + color.g * 255 ]
48 vec4 decode_color(const vec2 encodedColor) {
50 unpack_float(encodedColor[0]) / 255.0,
51 unpack_float(encodedColor[1]) / 255.0
55 // Unpack a pair of paint values and interpolate between them.
56 float unpack_mix_vec2(const vec2 packedValue, const float t) {
57 return mix(packedValue[0], packedValue[1], t);
60 // Unpack a pair of paint values and interpolate between them.
61 vec4 unpack_mix_color(const vec4 packedColors, const float t) {
62 vec4 minColor = decode_color(vec2(packedColors[0], packedColors[1]));
63 vec4 maxColor = decode_color(vec2(packedColors[2], packedColors[3]));
64 return mix(minColor, maxColor, t);
67 // The offset depends on how many pixels are between the world origin and the edge of the tile:
68 // vec2 offset = mod(pixel_coord, size)
70 // At high zoom levels there are a ton of pixels between the world origin and the edge of the tile.
71 // The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that.
73 // The pixel_coord is passed in as two 16 bit values:
74 // pixel_coord_upper = floor(pixel_coord / 2^16)
75 // pixel_coord_lower = mod(pixel_coord, 2^16)
77 // The offset is calculated in a series of steps that should preserve this precision:
78 vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower,
79 const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) {
81 vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size);
82 return (tile_units_to_pixels * pos + offset) / pattern_size;
85 static constexpr
const char* fragment = R
"(#ifdef GL_ES
86 precision mediump float;
103 out highp vec4 fragColor;
@ OpenGL
The OpenGL API backend.
BuiltIn
This enum is used with the ShaderSource template to select source code for the desired program and gr...
Select shader source based on a program type and a desired graphics API.