diff options
Diffstat (limited to 'src/mbgl/shader')
37 files changed, 424 insertions, 107 deletions
diff --git a/src/mbgl/shader/circle_shader.cpp b/src/mbgl/shader/circle_shader.cpp index b3430d4450..5ad6a73a1a 100644 --- a/src/mbgl/shader/circle_shader.cpp +++ b/src/mbgl/shader/circle_shader.cpp @@ -1,7 +1,7 @@ #include <mbgl/shader/circle_shader.hpp> #include <mbgl/shader/circle.vertex.hpp> #include <mbgl/shader/circle.fragment.hpp> -#include <mbgl/gl/gl.hpp> +#include <mbgl/shader/circle_vertex.hpp> namespace mbgl { @@ -12,9 +12,9 @@ CircleShader::CircleShader(gl::Context& context, Defines defines) context, defines) { } -void CircleShader::bind(int8_t* offset) { - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_pos)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 4, offset)); +void CircleShader::bind(const gl::VertexBuffer<CircleVertex>&, + const int8_t* offset) { + CircleVertex::bind(offset); } } // namespace mbgl diff --git a/src/mbgl/shader/circle_shader.hpp b/src/mbgl/shader/circle_shader.hpp index b87d856775..8ea13c7e00 100644 --- a/src/mbgl/shader/circle_shader.hpp +++ b/src/mbgl/shader/circle_shader.hpp @@ -6,11 +6,18 @@ namespace mbgl { +namespace gl { +template <class> class VertexBuffer; +} // namespace gl + +class CircleVertex; + class CircleShader : public Shader { public: CircleShader(gl::Context&, Defines defines = None); - void bind(int8_t* offset) final; + void bind(const gl::VertexBuffer<CircleVertex>&, + const int8_t* offset); UniformMatrix<4> u_matrix = {"u_matrix", *this}; Uniform<std::array<float, 2>> u_extrude_scale = {"u_extrude_scale", *this}; diff --git a/src/mbgl/shader/circle_vertex.cpp b/src/mbgl/shader/circle_vertex.cpp new file mode 100644 index 0000000000..3f14fc5747 --- /dev/null +++ b/src/mbgl/shader/circle_vertex.cpp @@ -0,0 +1,13 @@ +#include <mbgl/shader/circle_vertex.hpp> +#include <mbgl/shader/shader.hpp> +#include <mbgl/gl/gl.hpp> + +namespace mbgl { + +void CircleVertex::bind(const int8_t* offset) { + static_assert(sizeof(CircleVertex) == 4, "expected CircleVertex size"); + + MBGL_BIND_VERTEX_ATTRIBUTE(CircleVertex, a_pos, offset); +} + +} // namespace mbgl diff --git a/src/mbgl/shader/circle_vertex.hpp b/src/mbgl/shader/circle_vertex.hpp new file mode 100644 index 0000000000..8b4e37431b --- /dev/null +++ b/src/mbgl/shader/circle_vertex.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include <cstdint> + +namespace mbgl { + +class CircleVertex { +public: + /* + * @param {number} x vertex position + * @param {number} y vertex position + * @param {number} ex extrude normal + * @param {number} ey extrude normal + */ + CircleVertex(int16_t x, int16_t y, float ex, float ey) + : a_pos { + static_cast<int16_t>((x * 2) + ((ex + 1) / 2)), + static_cast<int16_t>((y * 2) + ((ey + 1) / 2)) + } {} + + const int16_t a_pos[2]; + + static void bind(const int8_t* offset); +}; + +} // namespace mbgl diff --git a/src/mbgl/shader/collision_box_shader.cpp b/src/mbgl/shader/collision_box_shader.cpp index c0a24f0f6f..8fed895c86 100644 --- a/src/mbgl/shader/collision_box_shader.cpp +++ b/src/mbgl/shader/collision_box_shader.cpp @@ -1,7 +1,7 @@ #include <mbgl/shader/collision_box_shader.hpp> #include <mbgl/shader/collisionbox.vertex.hpp> #include <mbgl/shader/collisionbox.fragment.hpp> -#include <mbgl/gl/gl.hpp> +#include <mbgl/shader/collision_box_vertex.hpp> namespace mbgl { @@ -12,17 +12,9 @@ CollisionBoxShader::CollisionBoxShader(gl::Context& context) context) { } -void CollisionBoxShader::bind(GLbyte *offset) { - const GLint stride = 12; - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_pos)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_pos, 2, GL_SHORT, false, stride, offset + 0)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_extrude)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_extrude, 2, GL_SHORT, false, stride, offset + 4)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_data)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_data, 2, GL_UNSIGNED_BYTE, false, stride, offset + 8)); +void CollisionBoxShader::bind(const gl::VertexBuffer<CollisionBoxVertex>&, + const int8_t* offset) { + CollisionBoxVertex::bind(offset); } } // namespace mbgl diff --git a/src/mbgl/shader/collision_box_shader.hpp b/src/mbgl/shader/collision_box_shader.hpp index 65c3ca0146..fe99ef274e 100644 --- a/src/mbgl/shader/collision_box_shader.hpp +++ b/src/mbgl/shader/collision_box_shader.hpp @@ -5,11 +5,18 @@ namespace mbgl { +namespace gl { +template <class> class VertexBuffer; +} // namespace gl + +class CollisionBoxVertex; + class CollisionBoxShader : public Shader { public: CollisionBoxShader(gl::Context&); - void bind(int8_t* offset) final; + void bind(const gl::VertexBuffer<CollisionBoxVertex>&, + const int8_t* offset); UniformMatrix<4> u_matrix = {"u_matrix", *this}; Uniform<float> u_scale = {"u_scale", *this}; diff --git a/src/mbgl/shader/collision_box_vertex.cpp b/src/mbgl/shader/collision_box_vertex.cpp new file mode 100644 index 0000000000..d12433ee7d --- /dev/null +++ b/src/mbgl/shader/collision_box_vertex.cpp @@ -0,0 +1,15 @@ +#include <mbgl/shader/collision_box_vertex.hpp> +#include <mbgl/shader/shader.hpp> +#include <mbgl/gl/gl.hpp> + +namespace mbgl { + +void CollisionBoxVertex::bind(const int8_t* offset) { + static_assert(sizeof(CollisionBoxVertex) == 10, "expected CollisionBoxVertex size"); + + MBGL_BIND_VERTEX_ATTRIBUTE(CollisionBoxVertex, a_pos, offset); + MBGL_BIND_VERTEX_ATTRIBUTE(CollisionBoxVertex, a_extrude, offset); + MBGL_BIND_VERTEX_ATTRIBUTE(CollisionBoxVertex, a_data, offset); +} + +} // namespace mbgl diff --git a/src/mbgl/shader/collision_box_vertex.hpp b/src/mbgl/shader/collision_box_vertex.hpp new file mode 100644 index 0000000000..f591a64ca1 --- /dev/null +++ b/src/mbgl/shader/collision_box_vertex.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include <cstdint> +#include <cmath> + +namespace mbgl { + +class CollisionBoxVertex { +public: + CollisionBoxVertex(int16_t x, int16_t y, float ox, float oy, float maxzoom, float placementZoom) + : a_pos { x, y }, + a_extrude { + static_cast<int16_t>(::round(ox)), + static_cast<int16_t>(::round(oy)) + }, + a_data { + static_cast<uint8_t>(maxzoom * 10), + static_cast<uint8_t>(placementZoom * 10) + } {} + + const int16_t a_pos[2]; + const int16_t a_extrude[2]; + const uint8_t a_data[2]; + + static void bind(const int8_t* offset); +}; + +} // namespace mbgl diff --git a/src/mbgl/shader/icon_shader.cpp b/src/mbgl/shader/icon_shader.cpp index d002e49a49..520c987768 100644 --- a/src/mbgl/shader/icon_shader.cpp +++ b/src/mbgl/shader/icon_shader.cpp @@ -1,7 +1,7 @@ #include <mbgl/shader/icon_shader.hpp> #include <mbgl/shader/icon.vertex.hpp> #include <mbgl/shader/icon.fragment.hpp> -#include <mbgl/gl/gl.hpp> +#include <mbgl/shader/texture_rect_vertex.hpp> namespace mbgl { @@ -12,20 +12,9 @@ IconShader::IconShader(gl::Context& context, Defines defines) context, defines) { } -void IconShader::bind(int8_t* offset) { - const GLsizei stride = 16; - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_pos)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_pos, 2, GL_SHORT, false, stride, offset + 0)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_offset)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_offset, 2, GL_SHORT, false, stride, offset + 4)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_texture_pos)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_texture_pos, 2, GL_UNSIGNED_SHORT, false, stride, offset + 8)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_data)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_data, 4, GL_UNSIGNED_BYTE, false, stride, offset + 12)); +void IconShader::bind(const gl::VertexBuffer<TextureRectVertex>&, + const int8_t* offset) { + TextureRectVertex::bind(offset); } } // namespace mbgl diff --git a/src/mbgl/shader/icon_shader.hpp b/src/mbgl/shader/icon_shader.hpp index c4f24c91f7..b7d724e05d 100644 --- a/src/mbgl/shader/icon_shader.hpp +++ b/src/mbgl/shader/icon_shader.hpp @@ -5,11 +5,18 @@ namespace mbgl { +namespace gl { +template <class> class VertexBuffer; +} // namespace gl + +class TextureRectVertex; + class IconShader : public Shader { public: IconShader(gl::Context&, Defines defines = None); - void bind(int8_t* offset) final; + void bind(const gl::VertexBuffer<TextureRectVertex>&, + const int8_t* offset); UniformMatrix<4> u_matrix = {"u_matrix", *this}; Uniform<std::array<float, 2>> u_extrude_scale = {"u_extrude_scale", *this}; diff --git a/src/mbgl/shader/line_shader.cpp b/src/mbgl/shader/line_shader.cpp index 684390815c..866c814384 100644 --- a/src/mbgl/shader/line_shader.cpp +++ b/src/mbgl/shader/line_shader.cpp @@ -1,7 +1,7 @@ #include <mbgl/shader/line_shader.hpp> #include <mbgl/shader/line.vertex.hpp> #include <mbgl/shader/line.fragment.hpp> -#include <mbgl/gl/gl.hpp> +#include <mbgl/shader/line_vertex.hpp> namespace mbgl { @@ -12,12 +12,9 @@ LineShader::LineShader(gl::Context& context, Defines defines) context, defines) { } -void LineShader::bind(int8_t* offset) { - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_pos)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 8, offset + 0)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_data)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_data, 4, GL_UNSIGNED_BYTE, false, 8, offset + 4)); +void LineShader::bind(const gl::VertexBuffer<LineVertex>&, + const int8_t* offset) { + LineVertex::bind(offset); } } // namespace mbgl diff --git a/src/mbgl/shader/line_shader.hpp b/src/mbgl/shader/line_shader.hpp index a6be32ebc3..4c1f395dfc 100644 --- a/src/mbgl/shader/line_shader.hpp +++ b/src/mbgl/shader/line_shader.hpp @@ -6,11 +6,18 @@ namespace mbgl { +namespace gl { +template <class> class VertexBuffer; +} // namespace gl + +class LineVertex; + class LineShader : public Shader { public: LineShader(gl::Context&, Defines defines = None); - void bind(int8_t* offset) final; + void bind(const gl::VertexBuffer<LineVertex>&, + const int8_t* offset); UniformMatrix<4> u_matrix = {"u_matrix", *this}; Uniform<Color> u_color = {"u_color", *this}; diff --git a/src/mbgl/shader/line_vertex.cpp b/src/mbgl/shader/line_vertex.cpp new file mode 100644 index 0000000000..a7a51d9fd7 --- /dev/null +++ b/src/mbgl/shader/line_vertex.cpp @@ -0,0 +1,14 @@ +#include <mbgl/shader/line_vertex.hpp> +#include <mbgl/shader/shader.hpp> +#include <mbgl/gl/gl.hpp> + +namespace mbgl { + +void LineVertex::bind(const int8_t* offset) { + static_assert(sizeof(LineVertex) == 8, "expected LineVertex size"); + + MBGL_BIND_VERTEX_ATTRIBUTE(LineVertex, a_pos, offset); + MBGL_BIND_VERTEX_ATTRIBUTE(LineVertex, a_data, offset); +} + +} // namespace mbgl diff --git a/src/mbgl/shader/line_vertex.hpp b/src/mbgl/shader/line_vertex.hpp new file mode 100644 index 0000000000..8a85397539 --- /dev/null +++ b/src/mbgl/shader/line_vertex.hpp @@ -0,0 +1,58 @@ +#pragma once + +#include <cstdint> +#include <cmath> + +namespace mbgl { + +class LineVertex { +public: + /* + * @param {number} x vertex position + * @param {number} y vertex position + * @param {number} ex extrude normal + * @param {number} ey extrude normal + * @param {number} tx texture normal + * @param {number} ty texture normal + * @param {number} dir direction of the line cap (-1/0/1) + */ + LineVertex(int16_t x, int16_t y, float ex, float ey, bool tx, bool ty, int8_t dir, int32_t linesofar = 0) + : a_pos { + static_cast<int16_t>((x * 2) | tx), + static_cast<int16_t>((y * 2) | ty) + }, + a_data { + // add 128 to store an byte in an unsigned byte + static_cast<uint8_t>(::round(extrudeScale * ex) + 128), + static_cast<uint8_t>(::round(extrudeScale * ey) + 128), + + // Encode the -1/0/1 direction value into the first two bits of .z of a_data. + // Combine it with the lower 6 bits of `linesofar` (shifted by 2 bites to make + // room for the direction value). The upper 8 bits of `linesofar` are placed in + // the `w` component. `linesofar` is scaled down by `LINE_DISTANCE_SCALE` so that + // we can store longer distances while sacrificing precision. + + // Encode the -1/0/1 direction value into .zw coordinates of a_data, which is normally covered + // by linesofar, so we need to merge them. + // The z component's first bit, as well as the sign bit is reserved for the direction, + // so we need to shift the linesofar. + static_cast<uint8_t>(((dir == 0 ? 0 : (dir < 0 ? -1 : 1 )) + 1) | ((linesofar & 0x3F) << 2)), + static_cast<uint8_t>(linesofar >> 6) + } {} + + const int16_t a_pos[2]; + const uint8_t a_data[4]; + + /* + * Scale the extrusion vector so that the normal length is this value. + * Contains the "texture" normals (-1..1). This is distinct from the extrude + * normals for line joins, because the x-value remains 0 for the texture + * normal array, while the extrude normal actually moves the vertex to create + * the acute/bevelled line join. + */ + static const int8_t extrudeScale = 63; + + static void bind(const int8_t* offset); +}; + +} // namespace mbgl diff --git a/src/mbgl/shader/linepattern_shader.cpp b/src/mbgl/shader/linepattern_shader.cpp index 8f755d4140..86f58d9d99 100644 --- a/src/mbgl/shader/linepattern_shader.cpp +++ b/src/mbgl/shader/linepattern_shader.cpp @@ -1,7 +1,7 @@ #include <mbgl/shader/linepattern_shader.hpp> #include <mbgl/shader/linepattern.vertex.hpp> #include <mbgl/shader/linepattern.fragment.hpp> -#include <mbgl/gl/gl.hpp> +#include <mbgl/shader/line_vertex.hpp> namespace mbgl { @@ -12,12 +12,9 @@ LinepatternShader::LinepatternShader(gl::Context& context, Defines defines) context, defines) { } -void LinepatternShader::bind(int8_t* offset) { - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_pos)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 8, offset + 0)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_data)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_data, 4, GL_UNSIGNED_BYTE, false, 8, offset + 4)); +void LinepatternShader::bind(const gl::VertexBuffer<LineVertex>&, + const int8_t* offset) { + LineVertex::bind(offset); } } // namespace mbgl diff --git a/src/mbgl/shader/linepattern_shader.hpp b/src/mbgl/shader/linepattern_shader.hpp index 89f64d3e1f..c6a30b6c47 100644 --- a/src/mbgl/shader/linepattern_shader.hpp +++ b/src/mbgl/shader/linepattern_shader.hpp @@ -5,11 +5,18 @@ namespace mbgl { +namespace gl { +template <class> class VertexBuffer; +} // namespace gl + +class LineVertex; + class LinepatternShader : public Shader { public: LinepatternShader(gl::Context&, Defines defines = None); - void bind(int8_t* offset) final; + void bind(const gl::VertexBuffer<LineVertex>&, + const int8_t* offset); UniformMatrix<4> u_matrix = {"u_matrix", *this}; Uniform<float> u_linewidth = {"u_linewidth", *this}; diff --git a/src/mbgl/shader/linesdf_shader.cpp b/src/mbgl/shader/linesdf_shader.cpp index 788239459f..5245bc71f4 100644 --- a/src/mbgl/shader/linesdf_shader.cpp +++ b/src/mbgl/shader/linesdf_shader.cpp @@ -1,7 +1,7 @@ #include <mbgl/shader/linesdf_shader.hpp> #include <mbgl/shader/linesdfpattern.vertex.hpp> #include <mbgl/shader/linesdfpattern.fragment.hpp> -#include <mbgl/gl/gl.hpp> +#include <mbgl/shader/line_vertex.hpp> namespace mbgl { @@ -12,12 +12,9 @@ LineSDFShader::LineSDFShader(gl::Context& context, Defines defines) context, defines) { } -void LineSDFShader::bind(int8_t* offset) { - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_pos)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 8, offset + 0)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_data)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_data, 4, GL_UNSIGNED_BYTE, false, 8, offset + 4)); +void LineSDFShader::bind(const gl::VertexBuffer<LineVertex>&, + const int8_t* offset) { + LineVertex::bind(offset); } } // namespace mbgl diff --git a/src/mbgl/shader/linesdf_shader.hpp b/src/mbgl/shader/linesdf_shader.hpp index 4c1fd89635..def4a2a983 100644 --- a/src/mbgl/shader/linesdf_shader.hpp +++ b/src/mbgl/shader/linesdf_shader.hpp @@ -6,11 +6,18 @@ namespace mbgl { +namespace gl { +template <class> class VertexBuffer; +} // namespace gl + +class LineVertex; + class LineSDFShader : public Shader { public: LineSDFShader(gl::Context&, Defines defines = None); - void bind(int8_t* offset) final; + void bind(const gl::VertexBuffer<LineVertex>&, + const int8_t* offset); UniformMatrix<4> u_matrix = {"u_matrix", *this}; Uniform<Color> u_color = {"u_color", *this}; diff --git a/src/mbgl/shader/outline_shader.cpp b/src/mbgl/shader/outline_shader.cpp index 94e981f4a6..3f7642b2c9 100644 --- a/src/mbgl/shader/outline_shader.cpp +++ b/src/mbgl/shader/outline_shader.cpp @@ -1,7 +1,7 @@ #include <mbgl/shader/outline_shader.hpp> #include <mbgl/shader/outline.vertex.hpp> #include <mbgl/shader/outline.fragment.hpp> -#include <mbgl/gl/gl.hpp> +#include <mbgl/shader/plain_vertex.hpp> namespace mbgl { @@ -12,9 +12,9 @@ OutlineShader::OutlineShader(gl::Context& context, Defines defines) context, defines) { } -void OutlineShader::bind(int8_t* offset) { - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_pos)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 0, offset)); +void OutlineShader::bind(const gl::VertexBuffer<PlainVertex>&, + const int8_t* offset) { + PlainVertex::bind(offset); } } // namespace mbgl diff --git a/src/mbgl/shader/outline_shader.hpp b/src/mbgl/shader/outline_shader.hpp index ccd8dc4303..09fa9431c9 100644 --- a/src/mbgl/shader/outline_shader.hpp +++ b/src/mbgl/shader/outline_shader.hpp @@ -6,11 +6,18 @@ namespace mbgl { +namespace gl { +template <class> class VertexBuffer; +} // namespace gl + +class PlainVertex; + class OutlineShader : public Shader { public: OutlineShader(gl::Context&, Defines defines = None); - void bind(int8_t* offset) final; + void bind(const gl::VertexBuffer<PlainVertex>&, + const int8_t* offset); UniformMatrix<4> u_matrix = {"u_matrix", *this}; Uniform<Color> u_outline_color = {"u_outline_color", *this}; diff --git a/src/mbgl/shader/outlinepattern_shader.cpp b/src/mbgl/shader/outlinepattern_shader.cpp index b33c2b2c1f..afe375f377 100644 --- a/src/mbgl/shader/outlinepattern_shader.cpp +++ b/src/mbgl/shader/outlinepattern_shader.cpp @@ -1,7 +1,7 @@ #include <mbgl/shader/outlinepattern_shader.hpp> #include <mbgl/shader/outlinepattern.vertex.hpp> #include <mbgl/shader/outlinepattern.fragment.hpp> -#include <mbgl/gl/gl.hpp> +#include <mbgl/shader/plain_vertex.hpp> namespace mbgl { @@ -12,9 +12,9 @@ OutlinePatternShader::OutlinePatternShader(gl::Context& context, Defines defines context, defines) { } -void OutlinePatternShader::bind(GLbyte *offset) { - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_pos)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 0, offset)); +void OutlinePatternShader::bind(const gl::VertexBuffer<PlainVertex>&, + const int8_t* offset) { + PlainVertex::bind(offset); } } // namespace mbgl diff --git a/src/mbgl/shader/outlinepattern_shader.hpp b/src/mbgl/shader/outlinepattern_shader.hpp index 795d390f19..b6b1cda422 100644 --- a/src/mbgl/shader/outlinepattern_shader.hpp +++ b/src/mbgl/shader/outlinepattern_shader.hpp @@ -5,11 +5,18 @@ namespace mbgl { +namespace gl { +template <class> class VertexBuffer; +} // namespace gl + +class PlainVertex; + class OutlinePatternShader : public Shader { public: OutlinePatternShader(gl::Context&, Defines defines = None); - void bind(int8_t* offset) final; + void bind(const gl::VertexBuffer<PlainVertex>&, + const int8_t* offset); UniformMatrix<4> u_matrix = {"u_matrix", *this}; Uniform<std::array<float, 2>> u_pattern_tl_a = {"u_pattern_tl_a", *this}; diff --git a/src/mbgl/shader/pattern_shader.cpp b/src/mbgl/shader/pattern_shader.cpp index dba12155f9..632775c181 100644 --- a/src/mbgl/shader/pattern_shader.cpp +++ b/src/mbgl/shader/pattern_shader.cpp @@ -1,7 +1,7 @@ #include <mbgl/shader/pattern_shader.hpp> #include <mbgl/shader/pattern.vertex.hpp> #include <mbgl/shader/pattern.fragment.hpp> -#include <mbgl/gl/gl.hpp> +#include <mbgl/shader/plain_vertex.hpp> namespace mbgl { @@ -12,9 +12,9 @@ PatternShader::PatternShader(gl::Context& context, Defines defines) context, defines) { } -void PatternShader::bind(GLbyte *offset) { - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_pos)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 0, offset)); +void PatternShader::bind(const gl::VertexBuffer<PlainVertex>&, + const int8_t* offset) { + PlainVertex::bind(offset); } } // namespace mbgl diff --git a/src/mbgl/shader/pattern_shader.hpp b/src/mbgl/shader/pattern_shader.hpp index 6ba141a2cb..2b8590814c 100644 --- a/src/mbgl/shader/pattern_shader.hpp +++ b/src/mbgl/shader/pattern_shader.hpp @@ -5,11 +5,18 @@ namespace mbgl { +namespace gl { +template <class> class VertexBuffer; +} // namespace gl + +class PlainVertex; + class PatternShader : public Shader { public: PatternShader(gl::Context&, Defines defines = None); - void bind(int8_t* offset) final; + void bind(const gl::VertexBuffer<PlainVertex>&, + const int8_t* offset); UniformMatrix<4> u_matrix = {"u_matrix", *this}; Uniform<std::array<float, 2>> u_pattern_tl_a = {"u_pattern_tl_a", *this}; diff --git a/src/mbgl/shader/plain_shader.cpp b/src/mbgl/shader/plain_shader.cpp index 8ea008c4ec..a6dce02a32 100644 --- a/src/mbgl/shader/plain_shader.cpp +++ b/src/mbgl/shader/plain_shader.cpp @@ -1,7 +1,7 @@ #include <mbgl/shader/plain_shader.hpp> #include <mbgl/shader/fill.vertex.hpp> #include <mbgl/shader/fill.fragment.hpp> -#include <mbgl/gl/gl.hpp> +#include <mbgl/shader/plain_vertex.hpp> namespace mbgl { @@ -12,9 +12,9 @@ PlainShader::PlainShader(gl::Context& context, Defines defines) context, defines) { } -void PlainShader::bind(int8_t* offset) { - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_pos)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 0, offset)); +void PlainShader::bind(const gl::VertexBuffer<PlainVertex>&, + const int8_t* offset) { + PlainVertex::bind(offset); } } // namespace mbgl diff --git a/src/mbgl/shader/plain_shader.hpp b/src/mbgl/shader/plain_shader.hpp index 2611a5a549..7dbb212e1b 100644 --- a/src/mbgl/shader/plain_shader.hpp +++ b/src/mbgl/shader/plain_shader.hpp @@ -6,11 +6,18 @@ namespace mbgl { +namespace gl { +template <class> class VertexBuffer; +} // namespace gl + +class PlainVertex; + class PlainShader : public Shader { public: PlainShader(gl::Context&, Defines defines = None); - void bind(int8_t* offset) final; + void bind(const gl::VertexBuffer<PlainVertex>&, + const int8_t* offset); UniformMatrix<4> u_matrix = {"u_matrix", *this}; Uniform<Color> u_color = {"u_color", *this}; diff --git a/src/mbgl/shader/plain_vertex.cpp b/src/mbgl/shader/plain_vertex.cpp new file mode 100644 index 0000000000..679ef05c39 --- /dev/null +++ b/src/mbgl/shader/plain_vertex.cpp @@ -0,0 +1,13 @@ +#include <mbgl/shader/plain_vertex.hpp> +#include <mbgl/shader/shader.hpp> +#include <mbgl/gl/gl.hpp> + +namespace mbgl { + +void PlainVertex::bind(const int8_t* offset) { + static_assert(sizeof(PlainVertex) == 4, "expected PlainVertex size"); + + MBGL_BIND_VERTEX_ATTRIBUTE(PlainVertex, a_pos, offset); +} + +} // namespace mbgl diff --git a/src/mbgl/shader/plain_vertex.hpp b/src/mbgl/shader/plain_vertex.hpp new file mode 100644 index 0000000000..46abbf737c --- /dev/null +++ b/src/mbgl/shader/plain_vertex.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include <cstdint> + +namespace mbgl { + +class PlainVertex { +public: + PlainVertex(int16_t x, int16_t y) + : a_pos { x, y } {} + + const int16_t a_pos[2]; + + static void bind(const int8_t* offset); +}; + +} // namespace mbgl diff --git a/src/mbgl/shader/raster_shader.cpp b/src/mbgl/shader/raster_shader.cpp index 8133b396c6..f3909bb2ac 100644 --- a/src/mbgl/shader/raster_shader.cpp +++ b/src/mbgl/shader/raster_shader.cpp @@ -1,7 +1,7 @@ #include <mbgl/shader/raster_shader.hpp> #include <mbgl/shader/raster.vertex.hpp> #include <mbgl/shader/raster.fragment.hpp> -#include <mbgl/gl/gl.hpp> +#include <mbgl/shader/raster_vertex.hpp> namespace mbgl { @@ -12,14 +12,9 @@ RasterShader::RasterShader(gl::Context& context, Defines defines) context, defines) { } -void RasterShader::bind(int8_t* offset) { - const GLint stride = 8; - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_pos)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_pos, 2, GL_SHORT, false, stride, offset)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_texture_pos)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_texture_pos, 2, GL_SHORT, false, stride, offset + 4)); +void RasterShader::bind(const gl::VertexBuffer<RasterVertex>&, + const int8_t* offset) { + RasterVertex::bind(offset); } } // namespace mbgl diff --git a/src/mbgl/shader/raster_shader.hpp b/src/mbgl/shader/raster_shader.hpp index e583b2337d..ca474ede15 100644 --- a/src/mbgl/shader/raster_shader.hpp +++ b/src/mbgl/shader/raster_shader.hpp @@ -5,11 +5,18 @@ namespace mbgl { +namespace gl { +template <class> class VertexBuffer; +} // namespace gl + +class RasterVertex; + class RasterShader : public Shader { public: RasterShader(gl::Context&, Defines defines = None); - void bind(int8_t* offset) final; + void bind(const gl::VertexBuffer<RasterVertex>&, + const int8_t* offset); UniformMatrix<4> u_matrix = {"u_matrix", *this}; Uniform<int32_t> u_image0 = {"u_image0", *this}; diff --git a/src/mbgl/shader/raster_vertex.cpp b/src/mbgl/shader/raster_vertex.cpp new file mode 100644 index 0000000000..c77cacef88 --- /dev/null +++ b/src/mbgl/shader/raster_vertex.cpp @@ -0,0 +1,14 @@ +#include <mbgl/shader/raster_vertex.hpp> +#include <mbgl/shader/shader.hpp> +#include <mbgl/gl/gl.hpp> + +namespace mbgl { + +void RasterVertex::bind(const int8_t* offset) { + static_assert(sizeof(RasterVertex) == 8, "expected RasterVertex size"); + + MBGL_BIND_VERTEX_ATTRIBUTE(RasterVertex, a_pos, offset); + MBGL_BIND_VERTEX_ATTRIBUTE(RasterVertex, a_texture_pos, offset); +} + +} // namespace mbgl diff --git a/src/mbgl/shader/raster_vertex.hpp b/src/mbgl/shader/raster_vertex.hpp new file mode 100644 index 0000000000..b983010d3d --- /dev/null +++ b/src/mbgl/shader/raster_vertex.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include <cstdint> + +namespace mbgl { + +class RasterVertex { +public: + RasterVertex(int16_t x, int16_t y, int16_t tx, int16_t ty) + : a_pos { + x, + y + }, + a_texture_pos { + tx, + ty + } {} + + const int16_t a_pos[2]; + const int16_t a_texture_pos[2]; + + static void bind(const int8_t* offset); +}; + +} // namespace mbgl diff --git a/src/mbgl/shader/sdf_shader.cpp b/src/mbgl/shader/sdf_shader.cpp index 3b4aa69535..804e96706f 100644 --- a/src/mbgl/shader/sdf_shader.cpp +++ b/src/mbgl/shader/sdf_shader.cpp @@ -1,7 +1,7 @@ #include <mbgl/shader/sdf_shader.hpp> #include <mbgl/shader/sdf.vertex.hpp> #include <mbgl/shader/sdf.fragment.hpp> -#include <mbgl/gl/gl.hpp> +#include <mbgl/shader/texture_rect_vertex.hpp> namespace mbgl { @@ -12,20 +12,9 @@ SDFShader::SDFShader(gl::Context& context, Defines defines) context, defines) { } -void SDFShader::bind(int8_t* offset) { - const int stride = 16; - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_pos)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_pos, 2, GL_SHORT, false, stride, offset + 0)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_offset)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_offset, 2, GL_SHORT, false, stride, offset + 4)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_texture_pos)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_texture_pos, 2, GL_UNSIGNED_SHORT, false, stride, offset + 8)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_data)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_data, 4, GL_UNSIGNED_BYTE, false, stride, offset + 12)); +void SDFShader::bind(const gl::VertexBuffer<TextureRectVertex>&, + const int8_t* offset) { + TextureRectVertex::bind(offset); } } // namespace mbgl diff --git a/src/mbgl/shader/sdf_shader.hpp b/src/mbgl/shader/sdf_shader.hpp index f7fde3e539..2e7f6d6795 100644 --- a/src/mbgl/shader/sdf_shader.hpp +++ b/src/mbgl/shader/sdf_shader.hpp @@ -6,10 +6,19 @@ namespace mbgl { +namespace gl { +template <class> class VertexBuffer; +} // namespace gl + +class TextureRectVertex; + class SDFShader : public Shader { public: SDFShader(gl::Context&, Defines defines = None); + void bind(const gl::VertexBuffer<TextureRectVertex>&, + const int8_t* offset); + UniformMatrix<4> u_matrix = {"u_matrix", *this}; Uniform<std::array<float, 2>> u_extrude_scale = {"u_extrude_scale", *this}; Uniform<Color> u_color = {"u_color", *this}; @@ -25,8 +34,6 @@ public: Uniform<int32_t> u_pitch_with_map = {"u_pitch_with_map", *this}; Uniform<int32_t> u_texture = {"u_texture", *this}; Uniform<int32_t> u_fadetexture = {"u_fadetexture", *this}; - - void bind(int8_t* offset) final; }; } // namespace mbgl diff --git a/src/mbgl/shader/shader.hpp b/src/mbgl/shader/shader.hpp index 91dd8ed518..977edebd7e 100644 --- a/src/mbgl/shader/shader.hpp +++ b/src/mbgl/shader/shader.hpp @@ -22,8 +22,6 @@ public: gl::UniformLocation getUniformLocation(const char* uniform) const; - virtual void bind(int8_t *offset) = 0; - enum Defines : bool { None = false, Overdraw = true, @@ -36,6 +34,7 @@ protected: gl::Context&, Defines defines = Defines::None); +public: static constexpr gl::AttributeLocation a_pos = 0; static constexpr gl::AttributeLocation a_extrude = 1; static constexpr gl::AttributeLocation a_offset = 2; diff --git a/src/mbgl/shader/texture_rect_vertex.cpp b/src/mbgl/shader/texture_rect_vertex.cpp new file mode 100644 index 0000000000..4693ada9e7 --- /dev/null +++ b/src/mbgl/shader/texture_rect_vertex.cpp @@ -0,0 +1,16 @@ +#include <mbgl/shader/texture_rect_vertex.hpp> +#include <mbgl/shader/shader.hpp> +#include <mbgl/gl/gl.hpp> + +namespace mbgl { + +void TextureRectVertex::bind(const int8_t* offset) { + static_assert(sizeof(TextureRectVertex) == 16, "expected TextureRectVertex size"); + + MBGL_BIND_VERTEX_ATTRIBUTE(TextureRectVertex, a_pos, offset); + MBGL_BIND_VERTEX_ATTRIBUTE(TextureRectVertex, a_offset, offset); + MBGL_BIND_VERTEX_ATTRIBUTE(TextureRectVertex, a_texture_pos, offset); + MBGL_BIND_VERTEX_ATTRIBUTE(TextureRectVertex, a_data, offset); +} + +} // namespace mbgl diff --git a/src/mbgl/shader/texture_rect_vertex.hpp b/src/mbgl/shader/texture_rect_vertex.hpp new file mode 100644 index 0000000000..e2e2526f16 --- /dev/null +++ b/src/mbgl/shader/texture_rect_vertex.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include <cstdint> +#include <cmath> + +namespace mbgl { + +// A vertex that holds a position, offset and texture coordinate. Used for symbol layer icons and glyphs. +class TextureRectVertex { +public: + TextureRectVertex(int16_t x, int16_t y, float ox, float oy, uint16_t tx, uint16_t ty, float minzoom, float maxzoom, float labelminzoom, uint8_t labelangle) + : a_pos { + x, + y + }, + a_offset { + static_cast<int16_t>(::round(ox * 64)), // use 1/64 pixels for placement + static_cast<int16_t>(::round(oy * 64)) + }, + a_texture_pos { + static_cast<uint16_t>(tx / 4), + static_cast<uint16_t>(ty / 4) + }, + a_data { + 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) + } {} + + const int16_t a_pos[2]; + const int16_t a_offset[2]; + const uint16_t a_texture_pos[2]; + const uint8_t a_data[4]; + + static void bind(const int8_t* offset); +}; + +} // namespace mbgl |