From f738a9e19ed95d9fdc845dbc471ac21062e762f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Wed, 5 Feb 2014 13:59:02 +0100 Subject: [PATCH 1/8] initialize variables --- include/llmr/util/pbf.hpp | 16 ++++++++++------ src/map/map.cpp | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/include/llmr/util/pbf.hpp b/include/llmr/util/pbf.hpp index 1813b254f..5585d5c15 100644 --- a/include/llmr/util/pbf.hpp +++ b/include/llmr/util/pbf.hpp @@ -46,20 +46,24 @@ struct pbf { inline void skipValue(uint32_t val); inline void skipBytes(uint32_t bytes); - const uint8_t *data; - const uint8_t *end; - uint32_t value; - uint32_t tag; + const uint8_t *data = NULL; + const uint8_t *end = NULL; + uint32_t value = 0; + uint32_t tag = 0; }; pbf::pbf(const unsigned char *data, uint32_t length) : data(data), - end(data + length) { + end(data + length), + value(0), + tag(0) { } pbf::pbf() : data(NULL), - end(NULL) { + end(NULL), + value(0), + tag(0) { } diff --git a/src/map/map.cpp b/src/map/map.cpp index ab29425d9..2828b9d9b 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -94,7 +94,7 @@ void Map::setScale(double scale) { } void Map::setAngle(double angle, double x, double y) { - double dx, dy; + double dx = 0, dy = 0; if (x >= 0 && y >= 0) { dx = (transform.width / 2) - x; From b629f56f6859714a8a0990336da21423b937405f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Wed, 5 Feb 2014 13:59:26 +0100 Subject: [PATCH 2/8] add aliases for VAOs --- include/llmr/platform/gl.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/llmr/platform/gl.hpp b/include/llmr/platform/gl.hpp index a86113d7f..529a6d307 100644 --- a/include/llmr/platform/gl.hpp +++ b/include/llmr/platform/gl.hpp @@ -14,6 +14,9 @@ #elif TARGET_OS_MAC #include #include + #define glGenVertexArrays glGenVertexArraysAPPLE + #define glBindVertexArray glBindVertexArrayAPPLE + #define glDeleteVertexArrays glDeleteVertexArraysAPPLE #else #error Unsupported Apple platform #endif From 0a053d944a0d8ff8373f8091c9738e68080d34eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Wed, 5 Feb 2014 13:59:38 +0100 Subject: [PATCH 3/8] set framerate to 60fps --- ios/MBXViewController.mm | 1 + 1 file changed, 1 insertion(+) diff --git a/ios/MBXViewController.mm b/ios/MBXViewController.mm index 48ec2d1c7..560cbd292 100644 --- a/ios/MBXViewController.mm +++ b/ios/MBXViewController.mm @@ -72,6 +72,7 @@ class MBXMapView GLKView *view = (GLKView *)self.view; view.context = self.context; view.drawableStencilFormat = GLKViewDrawableStencilFormat8; + self.preferredFramesPerSecond = 60; [view bindDrawable]; [EAGLContext setCurrentContext:self.context]; From e30d8eb48f89367f27a138fda5fdf41b78200e6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Wed, 5 Feb 2014 18:06:16 +0100 Subject: [PATCH 4/8] move to vertex array objects --- bin/style.js | 4 +- include/llmr/geometry/vao.hpp | 42 ++++++ include/llmr/map/tile.hpp | 7 +- include/llmr/platform/gl.hpp | 3 + include/llmr/platform/platform.hpp | 4 +- include/llmr/renderer/fill_bucket.hpp | 43 +++++- include/llmr/renderer/line_bucket.hpp | 11 +- include/llmr/renderer/painter.hpp | 6 + include/llmr/renderer/shader-fill.hpp | 2 + include/llmr/renderer/shader-line.hpp | 2 + include/llmr/renderer/shader-outline.hpp | 2 + include/llmr/renderer/shader-plain.hpp | 2 + include/llmr/util/std.hpp | 16 +++ resources/style.pbf | Bin 1050 -> 1032 bytes src/geometry/vertex_buffer.cpp | 2 - src/map/tile.cpp | 4 +- src/renderer/fill_bucket.cpp | 22 --- src/renderer/line_bucket.cpp | 20 ++- src/renderer/painter.cpp | 176 ++++++++++++----------- src/renderer/shader-fill.cpp | 5 + src/renderer/shader-line.cpp | 11 ++ src/renderer/shader-outline.cpp | 5 + src/renderer/shader-plain.cpp | 5 + src/renderer/shader.cpp | 2 +- src/style/resources.cpp | 45 +++--- 25 files changed, 294 insertions(+), 147 deletions(-) create mode 100644 include/llmr/geometry/vao.hpp create mode 100644 include/llmr/util/std.hpp diff --git a/bin/style.js b/bin/style.js index 4099b420d..ae2c353d2 100644 --- a/bin/style.js +++ b/bin/style.js @@ -83,7 +83,7 @@ module.exports = { "type": "fill", "color": "#c8df9f", "antialias": true, - "image": "park" + // "image": "park" }, "wood": { "type": "fill", @@ -96,7 +96,7 @@ module.exports = { "color": "#bae0e9", "stroke": "#73b6e6", "antialias": true, - "image": "water_etsy" + // "image": "water_etsy" }, "building": { "type": "fill", diff --git a/include/llmr/geometry/vao.hpp b/include/llmr/geometry/vao.hpp new file mode 100644 index 000000000..5e0b54ba6 --- /dev/null +++ b/include/llmr/geometry/vao.hpp @@ -0,0 +1,42 @@ +#ifndef LLMR_GEOMETRY_VAO +#define LLMR_GEOMETRY_VAO + +#include + +#include + +namespace llmr { + +class VertexArrayObject { +public: + template + inline void setup(Shader& shader, VertexBuffer& vertex_buffer, char *vertex_offset) { + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + + vertex_buffer.bind(); + shader.bind_vertex(vertex_offset); + } + + inline ~VertexArrayObject() { + if (vao) { + glDeleteVertexArrays(1, &vao); + } + } + + inline operator bool() const { + return vao != 0; + } + + inline void bind() { + assert(vao); + glBindVertexArray(vao); + } + +private: + GLuint vao = 0; +}; + +} + +#endif diff --git a/include/llmr/map/tile.hpp b/include/llmr/map/tile.hpp index 2184af9bd..a6457a58a 100644 --- a/include/llmr/map/tile.hpp +++ b/include/llmr/map/tile.hpp @@ -2,6 +2,8 @@ #define LLMR_MAP_TILE #include +#include +#include #include #include @@ -18,7 +20,6 @@ class LayerDescription; class BucketDescription; class VectorTile; class VectorTileLayer; -class DebugFontBuffer; class FillBuffer; class LineBuffer; @@ -71,7 +72,9 @@ public: state state; // Holds the actual geometries in this tile. - std::shared_ptr debugFontBuffer; + DebugFontBuffer debugFontBuffer; + VertexArrayObject debugFontArray; + std::shared_ptr fillBuffer; std::shared_ptr lineBuffer; diff --git a/include/llmr/platform/gl.hpp b/include/llmr/platform/gl.hpp index 529a6d307..4074fa081 100644 --- a/include/llmr/platform/gl.hpp +++ b/include/llmr/platform/gl.hpp @@ -8,6 +8,9 @@ #if TARGET_OS_IPHONE #include #include + #define glGenVertexArrays glGenVertexArraysOES + #define glBindVertexArray glBindVertexArrayOES + #define glDeleteVertexArrays glDeleteVertexArraysOES #elif TARGET_IPHONE_SIMULATOR #include #include diff --git a/include/llmr/platform/platform.hpp b/include/llmr/platform/platform.hpp index b31a112b8..4957f7042 100644 --- a/include/llmr/platform/platform.hpp +++ b/include/llmr/platform/platform.hpp @@ -5,8 +5,8 @@ #include #include -#define kTileURL "http://mapbox:magic@kkaefer.net/gl/tiles/plain/%d-%d-%d.vector.pbf" -#define kSpriteURL "http://mapbox:magic@kkaefer.net/gl/debug/img/sprite" +#define kTileURL "http://pepper.local:3333/gl/tiles/plain/%d-%d-%d.vector.pbf" +#define kSpriteURL "http://pepper.local:3333/gl/debug/img/sprite" namespace llmr { diff --git a/include/llmr/renderer/fill_bucket.hpp b/include/llmr/renderer/fill_bucket.hpp index 4ab950731..774b00811 100644 --- a/include/llmr/renderer/fill_bucket.hpp +++ b/include/llmr/renderer/fill_bucket.hpp @@ -3,10 +3,16 @@ #include "bucket.hpp" #include +#include +#include #include #include +#ifndef BUFFER_OFFSET +#define BUFFER_OFFSET(i) ((char *)NULL + (i)) +#endif + namespace llmr { class Style; @@ -24,8 +30,9 @@ public: void addGeometry(pbf& data); void addGeometry(const std::vector& line); uint32_t size() const; - void drawElements(int32_t attrib); - void drawVertices(int32_t attrib); + + template void drawElements(Shader& shader); + template void drawVertices(Shader& shader); public: const BucketGeometryDescription geom_desc; @@ -34,6 +41,7 @@ private: std::shared_ptr buffer; struct group { + VertexArrayObject array; uint32_t vertex_length; uint32_t elements_length; @@ -48,9 +56,40 @@ private: uint32_t vertex_start; uint32_t elements_start; uint32_t length; + VertexArrayObject array; std::vector groups; }; + +template +void FillBucket::drawElements(Shader& shader) { + char *vertex_index = BUFFER_OFFSET(vertex_start * 2 * sizeof(int16_t)); + char *elements_index = BUFFER_OFFSET(elements_start * 3 * sizeof(int16_t)); + for (group& group : groups) { + if (!group.array) { + group.array.setup(shader, *buffer, vertex_index); + } else { + group.array.bind(); + } + glDrawElements(GL_TRIANGLES, group.elements_length * 3 - 3, GL_UNSIGNED_SHORT, elements_index); + vertex_index += group.vertex_length * 2 * sizeof(uint16_t); + elements_index += group.elements_length * 3 * sizeof(uint16_t); + } +} + +template +void FillBucket::drawVertices(Shader& shader) { + // Draw the entire line + if (!array) { + char *vertex_index = BUFFER_OFFSET(vertex_start * 2 * sizeof(int16_t)); + array.setup(shader, *buffer, vertex_index); + } else { + array.bind(); + } + glDrawArrays(GL_LINE_STRIP, 0, length); +} + + } #endif diff --git a/include/llmr/renderer/line_bucket.hpp b/include/llmr/renderer/line_bucket.hpp index f6caededa..677adbdd4 100644 --- a/include/llmr/renderer/line_bucket.hpp +++ b/include/llmr/renderer/line_bucket.hpp @@ -3,6 +3,7 @@ #include "bucket.hpp" #include +#include #include #include @@ -10,6 +11,7 @@ namespace llmr { class Style; class LineBuffer; +class LineShader; struct Coordinate; struct pbf; @@ -21,10 +23,12 @@ public: void addGeometry(pbf& data); void addGeometry(const std::vector& line); - uint32_t size() const; - void bind(); - char *vertexOffset() const; + size_t size() const; + + void bind(LineShader& shader); + void drawLines(); + void drawPoints(); public: const BucketGeometryDescription geometry; @@ -33,6 +37,7 @@ private: std::shared_ptr buffer; uint32_t start; uint32_t length; + VertexArrayObject array; }; } diff --git a/include/llmr/renderer/painter.hpp b/include/llmr/renderer/painter.hpp index 108eb2f38..7688763ac 100644 --- a/include/llmr/renderer/painter.hpp +++ b/include/llmr/renderer/painter.hpp @@ -2,6 +2,7 @@ #define LLMR_RENDERER_PAINTER #include +#include #include #include @@ -86,6 +87,8 @@ private: 4096, 4096 }; + VertexArrayObject tileStencilArray; + // Set up the tile boundary lines we're using to draw the tile outlines. VertexBuffer tileBorderBuffer = { 0, 0, @@ -95,6 +98,8 @@ private: 0, 0 }; + VertexArrayObject tileBorderArray; + // Set up the matte buffer we're using to draw the filling background. VertexBuffer matteBuffer = { // top left triangle @@ -108,6 +113,7 @@ private: 16384, 16384 }; + VertexArrayObject matteArray; }; } diff --git a/include/llmr/renderer/shader-fill.hpp b/include/llmr/renderer/shader-fill.hpp index 440f3011c..beff61592 100644 --- a/include/llmr/renderer/shader-fill.hpp +++ b/include/llmr/renderer/shader-fill.hpp @@ -9,6 +9,8 @@ class FillShader : public Shader { public: FillShader(); + void bind_vertex(char *offset); + int32_t a_pos; int32_t u_matrix; int32_t u_color; diff --git a/include/llmr/renderer/shader-line.hpp b/include/llmr/renderer/shader-line.hpp index 1045a97f3..f33e26e51 100644 --- a/include/llmr/renderer/shader-line.hpp +++ b/include/llmr/renderer/shader-line.hpp @@ -9,6 +9,8 @@ class LineShader : public Shader { public: LineShader(); + void bind_vertex(char *offset); + int32_t a_pos; int32_t a_extrude; int32_t a_linesofar; diff --git a/include/llmr/renderer/shader-outline.hpp b/include/llmr/renderer/shader-outline.hpp index f37c54d5b..eda1bb88b 100644 --- a/include/llmr/renderer/shader-outline.hpp +++ b/include/llmr/renderer/shader-outline.hpp @@ -9,6 +9,8 @@ class OutlineShader : public Shader { public: OutlineShader(); + void bind_vertex(char *offset); + int32_t a_pos; int32_t u_matrix; int32_t u_color; diff --git a/include/llmr/renderer/shader-plain.hpp b/include/llmr/renderer/shader-plain.hpp index d1ccdeff0..dd3a57102 100644 --- a/include/llmr/renderer/shader-plain.hpp +++ b/include/llmr/renderer/shader-plain.hpp @@ -9,6 +9,8 @@ class PlainShader : public Shader { public: PlainShader(); + void bind_vertex(char *offset); + int32_t a_pos; int32_t u_matrix; int32_t u_color; diff --git a/include/llmr/util/std.hpp b/include/llmr/util/std.hpp new file mode 100644 index 000000000..4ef28d5f8 --- /dev/null +++ b/include/llmr/util/std.hpp @@ -0,0 +1,16 @@ +#ifndef LLMR_UTIL_STD +#define LLMR_UTIL_STD + +#include + +namespace std { + +// C++14 backfill from http://herbsutter.com/gotw/_102/ +template +::std::unique_ptr make_unique(Args&& ...args) { + return ::std::unique_ptr(new T(::std::forward(args)...)); +} + +} + +#endif diff --git a/resources/style.pbf b/resources/style.pbf index d87f896d1a0fb748df9b35975d0a89243905cc9b..69475487d5afbc5de084fa8754d05505ae815014 100644 GIT binary patch delta 79 zcmbQm(ZR7{50la init) : array(init) {} VertexBuffer::~VertexBuffer() { if (buffer != 0) { glDeleteBuffers(1, &buffer); - buffer = 0; } } @@ -26,5 +25,4 @@ void VertexBuffer::bind() { } else { glBindBuffer(GL_ARRAY_BUFFER, buffer); } - } diff --git a/src/map/tile.cpp b/src/map/tile.cpp index dccde9383..b7cf468d0 100644 --- a/src/map/tile.cpp +++ b/src/map/tile.cpp @@ -2,7 +2,6 @@ #include #include -#include #include #include #include @@ -43,7 +42,6 @@ std::forward_list Tile::children(const ID& id, int32_t z) { Tile::Tile(ID id, const Style& style) : id(id), state(initial), - debugFontBuffer(std::make_shared()), fillBuffer(std::make_shared()), lineBuffer(std::make_shared()), style(style) { @@ -51,7 +49,7 @@ Tile::Tile(ID id, const Style& style) // Initialize tile debug coordinates char coord[32]; snprintf(coord, sizeof(coord), "%d/%d/%d", id.z, id.x, id.y); - debugFontBuffer->addText(coord, 50, 200, 5); + debugFontBuffer.addText(coord, 50, 200, 5); } Tile::~Tile() { diff --git a/src/renderer/fill_bucket.cpp b/src/renderer/fill_bucket.cpp index 69208ae20..f0226f134 100644 --- a/src/renderer/fill_bucket.cpp +++ b/src/renderer/fill_bucket.cpp @@ -8,8 +8,6 @@ #include -#define BUFFER_OFFSET(i) ((char *)NULL + (i)) - #include struct geometry_too_long_exception : std::exception {}; @@ -97,26 +95,6 @@ void FillBucket::addGeometry(const std::vector& line) { } -void FillBucket::drawElements(int32_t attrib) { - char *vertex_index = BUFFER_OFFSET(vertex_start * 2 * sizeof(int16_t)); - char *elements_index = BUFFER_OFFSET(elements_start * 3 * sizeof(int16_t)); - buffer->bind(); - for (const auto& group : groups) { - glVertexAttribPointer(attrib, 2, GL_SHORT, GL_FALSE, 0, vertex_index); - glDrawElements(GL_TRIANGLES, group.elements_length * 3, GL_UNSIGNED_SHORT, elements_index); - vertex_index += group.vertex_length * 2 * sizeof(uint16_t); - elements_index += group.elements_length * 3 * sizeof(uint16_t); - } -} - -void FillBucket::drawVertices(int32_t attrib) { - // Draw the entire line - char *vertex_index = BUFFER_OFFSET(vertex_start * 2 * sizeof(int16_t)); - buffer->bind(); - glVertexAttribPointer(attrib, 2, GL_SHORT, GL_FALSE, 0, vertex_index); - glDrawArrays(GL_LINE_STRIP, 0, length); -} - void FillBucket::render(Painter& painter, const std::string& layer_name, const Tile::ID& id) { painter.renderFill(*this, layer_name, id); } diff --git a/src/renderer/line_bucket.cpp b/src/renderer/line_bucket.cpp index 70f766d9d..5dc0710b8 100644 --- a/src/renderer/line_bucket.cpp +++ b/src/renderer/line_bucket.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -264,14 +265,23 @@ void LineBucket::render(Painter& painter, const std::string& layer_name, const T painter.renderLine(*this, layer_name, id); } -uint32_t LineBucket::size() const { +size_t LineBucket::size() const { return length; } -void LineBucket::bind() { - buffer->bind(); +void LineBucket::bind(LineShader& shader) { + if (!array) { + char *vertex_index = BUFFER_OFFSET(start * 4 * sizeof(int16_t)); + array.setup(shader, *buffer, vertex_index); + } else { + array.bind(); + } } -char *LineBucket::vertexOffset() const { - return BUFFER_OFFSET(start * 4 * sizeof(int16_t)); +void LineBucket::drawLines() { + glDrawArrays(GL_TRIANGLE_STRIP, 0, length); +} + +void LineBucket::drawPoints() { + glDrawArrays(GL_POINTS, 0, length); } diff --git a/src/renderer/painter.cpp b/src/renderer/painter.cpp index 3b5c83caf..3ef7fbe22 100644 --- a/src/renderer/painter.cpp +++ b/src/renderer/painter.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include @@ -76,7 +77,7 @@ void Painter::changeMatrix(const Tile::ID& id) { } void Painter::drawClippingMask() { - switchShader(plainShader); + glUseProgram(plainShader->program); glUniformMatrix4fv(plainShader->u_matrix, 1, GL_FALSE, matrix.data()); glColorMask(false, false, false, false); @@ -96,9 +97,13 @@ void Painter::drawClippingMask() { glStencilMask(0xC0); glStencilOp(GL_REPLACE, GL_KEEP, GL_KEEP); + if (!tileStencilArray) { + tileStencilArray.setup(*plainShader, tileStencilBuffer, BUFFER_OFFSET(0)); + } else { + tileStencilArray.bind(); + } + // Draw the clipping mask - tileStencilBuffer.bind(); - glVertexAttribPointer(plainShader->a_pos, 2, GL_SHORT, false, 0, BUFFER_OFFSET(0)); glUniform4f(plainShader->u_color, 1.0f, 0.0f, 1.0f, 1.0f); glDrawArrays(GL_TRIANGLES, 0, tileStencilBuffer.length()); @@ -197,11 +202,11 @@ void Painter::renderFill(FillBucket& bucket, const std::string& layer_name, cons glColorMask(false, false, false, false); // Draw the actual triangle fan into the stencil buffer. - switchShader(fillShader); + glUseProgram(fillShader->program); glUniformMatrix4fv(fillShader->u_matrix, 1, GL_FALSE, matrix.data()); // Draw all groups - bucket.drawElements(fillShader->a_pos); + bucket.drawElements(*fillShader); // Now that we have the stencil mask in the stencil buffer, we can start // writing to the color buffer. @@ -215,7 +220,7 @@ void Painter::renderFill(FillBucket& bucket, const std::string& layer_name, cons // Because we're drawing top-to-bottom, and we update the stencil mask // below, we have to draw the outline first (!) if (properties.antialias) { - switchShader(outlineShader); + glUseProgram(outlineShader->program); glUniformMatrix4fv(outlineShader->u_matrix, 1, GL_FALSE, matrix.data()); glLineWidth(2); @@ -238,49 +243,53 @@ void Painter::renderFill(FillBucket& bucket, const std::string& layer_name, cons // Draw the entire line glUniform2f(outlineShader->u_world, transform.fb_width, transform.fb_height); glLineWidth(2.0f); - bucket.drawVertices(outlineShader->a_pos); + bucket.drawVertices(*outlineShader); } // Only draw regions that we marked glStencilFunc(GL_NOTEQUAL, 0x0, 0x3F); if (properties.image.size() && style.sprite) { - // Draw texture fill - ImagePosition imagePos = style.sprite->getPosition(properties.image, true); + // TODO: + // // Draw texture fill + // ImagePosition imagePos = style.sprite->getPosition(properties.image, true); - double factor = 8.0 / pow(2, transform.getIntegerZoom() - id.z); - double mix = fmod(transform.getZoom(), 1.0); - vec2 imageSize { imagePos.size.x * factor, imagePos.size.y * factor }; + // double factor = 8.0 / pow(2, transform.getIntegerZoom() - id.z); + // double mix = fmod(transform.getZoom(), 1.0); + // vec2 imageSize { imagePos.size.x * factor, imagePos.size.y * factor }; - vec2 offset { - fmod(id.x * 4096, imageSize.x), - fmod(id.y * 4096, imageSize.y) - }; + // vec2 offset { + // fmod(id.x * 4096, imageSize.x), + // fmod(id.y * 4096, imageSize.y) + // }; - switchShader(patternShader); - glUniformMatrix4fv(patternShader->u_matrix, 1, GL_FALSE, matrix.data()); - glUniform2f(patternShader->u_pattern_size, imageSize.x, imageSize.y); - glUniform2f(patternShader->u_offset, offset.x, offset.y); - glUniform2f(patternShader->u_pattern_tl, imagePos.tl.x, imagePos.tl.y); - glUniform2f(patternShader->u_pattern_br, imagePos.br.x, imagePos.br.y); - glUniform4fv(patternShader->u_color, 1, fill_color.data()); - glUniform1f(patternShader->u_mix, mix); - style.sprite->bind(true); + // switchShader(patternShader); + // glUniformMatrix4fv(patternShader->u_matrix, 1, GL_FALSE, matrix.data()); + // glUniform2f(patternShader->u_pattern_size, imageSize.x, imageSize.y); + // glUniform2f(patternShader->u_offset, offset.x, offset.y); + // glUniform2f(patternShader->u_pattern_tl, imagePos.tl.x, imagePos.tl.y); + // glUniform2f(patternShader->u_pattern_br, imagePos.br.x, imagePos.br.y); + // glUniform4fv(patternShader->u_color, 1, fill_color.data()); + // glUniform1f(patternShader->u_mix, mix); + // style.sprite->bind(true); - // Draw a rectangle that covers the entire viewport. - tileStencilBuffer.bind(); - glVertexAttribPointer(patternShader->a_pos, 2, GL_SHORT, false, 0, BUFFER_OFFSET(0)); - glDrawArrays(GL_TRIANGLES, 0, tileStencilBuffer.length()); + // // Draw a rectangle that covers the entire viewport. + // tileStencilBuffer.bind(); + // glVertexAttribPointer(patternShader->a_pos, 2, GL_SHORT, false, 0, BUFFER_OFFSET(0)); + // glDrawArrays(GL_TRIANGLES, 0, tileStencilBuffer.length()); } else { // Draw filling rectangle. - switchShader(fillShader); + glUseProgram(fillShader->program); glUniformMatrix4fv(fillShader->u_matrix, 1, GL_FALSE, matrix.data()); glUniform4fv(fillShader->u_color, 1, fill_color.data()); // Draw a rectangle that covers the entire viewport. - tileStencilBuffer.bind(); - glVertexAttribPointer(fillShader->a_pos, 2, GL_SHORT, false, 0, BUFFER_OFFSET(0)); + if (!tileStencilArray) { + tileStencilArray.setup(*plainShader, tileStencilBuffer, BUFFER_OFFSET(0)); + } else { + tileStencilArray.bind(); + } glDrawArrays(GL_TRIANGLES, 0, tileStencilBuffer.length()); } @@ -324,42 +333,37 @@ void Painter::renderLine(LineBucket& bucket, const std::string& layer_name, cons // glUniform1f(painter.linepatternShader.u_fade, painter.transform.z % 1.0); } else { - switchShader(lineShader); + glUseProgram(lineShader->program); glUniformMatrix4fv(lineShader->u_matrix, 1, GL_FALSE, matrix.data()); glUniformMatrix4fv(lineShader->u_exmatrix, 1, GL_FALSE, exMatrix.data()); // glUniform2fv(painter.lineShader.u_dasharray, properties.dasharray || [1, -1]); glUniform2f(lineShader->u_dasharray, 1, -1); - } - // TODO: Move this to transform? - const double tilePixelRatio = transform.getScale() / (1 << transform.getIntegerZoom()) / 8; + // TODO: Move this to transform? + const double tilePixelRatio = transform.getScale() / (1 << transform.getIntegerZoom()) / 8; - glUniform2f(lineShader->u_linewidth, outset, inset); - glUniform1f(lineShader->u_ratio, tilePixelRatio); - glUniform1f(lineShader->u_gamma, transform.pixelRatio); + glUniform2f(lineShader->u_linewidth, outset, inset); + glUniform1f(lineShader->u_ratio, tilePixelRatio); + glUniform1f(lineShader->u_gamma, transform.pixelRatio); - // const Color& color = properties.color; - // if (!params.antialiasing) { - // color[3] = Infinity; - // glUniform4fv(lineShader->u_color, color); - // } else { - glUniform4fv(lineShader->u_color, 1, color.data()); - // } + // const Color& color = properties.color; + // if (!params.antialiasing) { + // color[3] = Infinity; + // glUniform4fv(lineShader->u_color, color); + // } else { + glUniform4fv(lineShader->u_color, 1, color.data()); + // } - glUniform1f(lineShader->u_point, 0); + glUniform1f(lineShader->u_point, 0); + bucket.bind(*lineShader); + bucket.drawLines(); - bucket.bind(); - char *vertex_index = bucket.vertexOffset(); - glVertexAttribPointer(lineShader->a_pos, 2, GL_SHORT, false, 8, vertex_index); - glVertexAttribPointer(lineShader->a_extrude, 2, GL_BYTE, false, 8, vertex_index + 6); - glVertexAttribPointer(lineShader->a_linesofar, 1, GL_SHORT, false, 8, vertex_index + 4); - glDrawArrays(GL_TRIANGLE_STRIP, 0, bucket.size()); - - if (bucket.geometry.join == JoinType::Round) { - glUniform1f(lineShader->u_point, 1); - glDrawArrays(GL_POINTS, 0, bucket.size()); + if (bucket.geometry.join == JoinType::Round) { + glUniform1f(lineShader->u_point, 1); + bucket.drawPoints(); + } } // statistics @@ -369,27 +373,31 @@ void Painter::renderLine(LineBucket& bucket, const std::string& layer_name, cons void Painter::renderDebug(const Tile::Ptr& tile) { // Blend to the front, not the back. glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + glUseProgram(plainShader->program); + glUniformMatrix4fv(plainShader->u_matrix, 1, GL_FALSE, matrix.data()); // draw tile outline - switchShader(plainShader); - glUniformMatrix4fv(plainShader->u_matrix, 1, GL_FALSE, matrix.data()); - tileBorderBuffer.bind(); - glVertexAttribPointer(plainShader->a_pos, 2, GL_SHORT, false, 0, BUFFER_OFFSET(0)); - glUniform4f(plainShader->u_color, 1.0f, 1.0f, 1.0f, 1.0f); + if (!tileBorderArray) { + tileBorderArray.setup(*plainShader, tileBorderBuffer, BUFFER_OFFSET(0)); + } else { + tileBorderArray.bind(); + } + glUniform4f(plainShader->u_color, 1.0f, 0.0f, 0.0f, 1.0f); glLineWidth(4.0f); glDrawArrays(GL_LINE_STRIP, 0, tileBorderBuffer.length()); // draw debug info - switchShader(plainShader); - glUniformMatrix4fv(plainShader->u_matrix, 1, GL_FALSE, matrix.data()); - tile->debugFontBuffer->bind(); - glVertexAttribPointer(plainShader->a_pos, 2, GL_SHORT, GL_FALSE, 0, BUFFER_OFFSET(0)); + if (!tile->debugFontArray) { + tile->debugFontArray.setup(*plainShader, tile->debugFontBuffer, BUFFER_OFFSET(0)); + } else { + tile->debugFontArray.bind(); + } glUniform4f(plainShader->u_color, 1.0f, 1.0f, 1.0f, 1.0f); glLineWidth(4.0f); - glDrawArrays(GL_LINES, 0, tile->debugFontBuffer->length()); + glDrawArrays(GL_LINES, 0, tile->debugFontBuffer.length()); glUniform4f(plainShader->u_color, 0.0f, 0.0f, 0.0f, 1.0f); glLineWidth(2.0f); - glDrawArrays(GL_LINES, 0, tile->debugFontBuffer->length()); + glDrawArrays(GL_LINES, 0, tile->debugFontBuffer.length()); // Revert blending mode to blend to the back. glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ONE); @@ -398,30 +406,37 @@ void Painter::renderDebug(const Tile::Ptr& tile) { void Painter::renderMatte() { glDisable(GL_STENCIL_TEST); - switchShader(fillShader); - glUniformMatrix4fv(fillShader->u_matrix, 1, GL_FALSE, nativeMatrix.data()); + Color white = {{ 0.9, 0.9, 0.9, 1 }}; - Color white = {{ 1, 1, 1, 1 }}; + glUseProgram(plainShader->program); + glUniformMatrix4fv(plainShader->u_matrix, 1, GL_FALSE, nativeMatrix.data()); + + if (!matteArray) { + matteArray.setup(*plainShader, matteBuffer, BUFFER_OFFSET(0)); + } else { + matteArray.bind(); + } // Draw the clipping mask - matteBuffer.bind(); - glVertexAttribPointer(fillShader->a_pos, 2, GL_SHORT, false, 0, BUFFER_OFFSET(0)); - glUniform4fv(fillShader->u_color, 1, white.data()); + glUniform4fv(plainShader->u_color, 1, white.data()); glDrawArrays(GL_TRIANGLES, 0, matteBuffer.length()); glEnable(GL_STENCIL_TEST); } void Painter::renderBackground() { - switchShader(fillShader); - glUniformMatrix4fv(fillShader->u_matrix, 1, GL_FALSE, matrix.data()); - Color white = {{ 1, 1, 1, 1 }}; + glUseProgram(plainShader->program); + glUniformMatrix4fv(plainShader->u_matrix, 1, GL_FALSE, matrix.data()); + // Draw the clipping mask - tileStencilBuffer.bind(); - glVertexAttribPointer(fillShader->a_pos, 2, GL_SHORT, false, 0, BUFFER_OFFSET(0)); - glUniform4fv(fillShader->u_color, 1, white.data()); + if (!tileStencilArray) { + tileStencilArray.setup(*plainShader, tileStencilBuffer, BUFFER_OFFSET(0)); + } else { + tileStencilArray.bind(); + } + glUniform4fv(plainShader->u_color, 1, white.data()); glDrawArrays(GL_TRIANGLES, 0, tileStencilBuffer.length()); } @@ -431,6 +446,7 @@ void Painter::renderBackground() { * @return boolean whether the shader was actually switched */ bool Painter::switchShader(std::shared_ptr shader) { + assert(false); if (currentShader != shader) { glUseProgram(shader->program); diff --git a/src/renderer/shader-fill.cpp b/src/renderer/shader-fill.cpp index 623d06118..a5536bf83 100644 --- a/src/renderer/shader-fill.cpp +++ b/src/renderer/shader-fill.cpp @@ -20,3 +20,8 @@ FillShader::FillShader() u_matrix = glGetUniformLocation(program, "u_matrix"); u_color = glGetUniformLocation(program, "u_color"); } + +void FillShader::bind_vertex(char *offset) { + glEnableVertexAttribArray(a_pos); + glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 0, offset); +} diff --git a/src/renderer/shader-line.cpp b/src/renderer/shader-line.cpp index ae73dbb4f..7a1c0b5a3 100644 --- a/src/renderer/shader-line.cpp +++ b/src/renderer/shader-line.cpp @@ -31,3 +31,14 @@ LineShader::LineShader() u_point = glGetUniformLocation(program, "u_point"); u_gamma = glGetUniformLocation(program, "u_gamma"); } + +void LineShader::bind_vertex(char *offset) { + glEnableVertexAttribArray(a_pos); + glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 8, offset); + + glEnableVertexAttribArray(a_extrude); + glVertexAttribPointer(a_extrude, 2, GL_BYTE, false, 8, offset + 6); + + glEnableVertexAttribArray(a_linesofar); + glVertexAttribPointer(a_linesofar, 1, GL_SHORT, false, 8, offset + 4); +} \ No newline at end of file diff --git a/src/renderer/shader-outline.cpp b/src/renderer/shader-outline.cpp index ea27b2698..82f31e1dc 100644 --- a/src/renderer/shader-outline.cpp +++ b/src/renderer/shader-outline.cpp @@ -21,3 +21,8 @@ OutlineShader::OutlineShader() u_color = glGetUniformLocation(program, "u_color"); u_world = glGetUniformLocation(program, "u_world"); } + +void OutlineShader::bind_vertex(char *offset) { + glEnableVertexAttribArray(a_pos); + glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 0, offset); +} diff --git a/src/renderer/shader-plain.cpp b/src/renderer/shader-plain.cpp index 8e767b6dc..42c5ea564 100644 --- a/src/renderer/shader-plain.cpp +++ b/src/renderer/shader-plain.cpp @@ -20,3 +20,8 @@ PlainShader::PlainShader() u_matrix = glGetUniformLocation(program, "u_matrix"); u_color = glGetUniformLocation(program, "u_color"); } + +void PlainShader::bind_vertex(char *offset) { + glEnableVertexAttribArray(a_pos); + glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 0, offset); +} diff --git a/src/renderer/shader.cpp b/src/renderer/shader.cpp index f2326efd7..fd74281bf 100644 --- a/src/renderer/shader.cpp +++ b/src/renderer/shader.cpp @@ -107,7 +107,7 @@ bool Shader::compileShader(GLuint *shader, GLenum type, const GLchar *source) { #if defined(EMSCRIPTEN) || defined(GL_ES_VERSION_2_0) // Add WebGL GLSL / OpenGL ES precision premable - const GLchar *preamble = "precision mediump float;\n\n"; + const GLchar *preamble = "precision highp float;\n\n"; #else // Desktop GLSL const GLchar *preamble = "#version 120\n\n"; diff --git a/src/style/resources.cpp b/src/style/resources.cpp index def1933ff..9380bf025 100644 --- a/src/style/resources.cpp +++ b/src/style/resources.cpp @@ -47,28 +47,27 @@ const unsigned char resources::style[] = { 12, 114, 111, 97, 100, 95, 114, 101, 103, 117, 108, 97, 114, 18, 24, 10, 10, 114, 111, 97, 100, 95, 108, 97, 114, 103, 101, 18, 10, 114, 111, 97, 100, 95, 108, 97, 114, 103, 101, 18, 18, 10, 7, 97, 108, 99, 111, 104, - 111, 108, 18, 7, 97, 108, 99, 111, 104, 111, 108, 26, 220, 2, 10, 7, - 100, 101, 102, 97, 117, 108, 116, 18, 27, 10, 4, 112, 97, 114, 107, 34, - 8, 8, 2, 18, 4, 0, 0, 128, 63, 45, 255, 159, 223, 200, 66, 4, - 112, 97, 114, 107, 18, 31, 10, 4, 119, 111, 111, 100, 34, 8, 8, 2, - 18, 4, 0, 0, 128, 63, 45, 255, 102, 170, 51, 58, 8, 8, 2, 18, - 4, 205, 204, 204, 61, 18, 39, 10, 5, 119, 97, 116, 101, 114, 34, 8, - 8, 2, 18, 4, 0, 0, 128, 63, 45, 255, 233, 224, 186, 53, 255, 230, - 182, 115, 66, 10, 119, 97, 116, 101, 114, 95, 101, 116, 115, 121, 18, 51, - 10, 8, 98, 117, 105, 108, 100, 105, 110, 103, 34, 8, 8, 2, 18, 4, - 0, 0, 128, 63, 45, 255, 0, 0, 0, 58, 24, 8, 4, 18, 20, 0, - 0, 80, 65, 0, 0, 0, 0, 205, 204, 204, 61, 0, 0, 0, 0, 205, - 204, 204, 61, 26, 41, 10, 12, 114, 111, 97, 100, 95, 108, 105, 109, 105, - 116, 101, 100, 29, 255, 187, 187, 187, 34, 20, 8, 3, 18, 16, 0, 0, - 0, 0, 0, 0, 128, 63, 0, 0, 240, 65, 0, 0, 128, 63, 26, 65, - 10, 12, 114, 111, 97, 100, 95, 114, 101, 103, 117, 108, 97, 114, 29, 255, - 153, 153, 153, 34, 44, 8, 3, 18, 40, 0, 0, 0, 0, 0, 0, 0, - 63, 0, 0, 80, 65, 0, 0, 0, 63, 0, 0, 128, 65, 0, 0, 0, - 64, 0, 0, 160, 65, 0, 0, 0, 66, 0, 0, 240, 65, 0, 0, 0, - 66, 26, 71, 10, 10, 114, 111, 97, 100, 95, 108, 97, 114, 103, 101, 29, - 255, 102, 102, 102, 34, 52, 8, 3, 18, 48, 0, 0, 0, 0, 0, 0, - 0, 63, 0, 0, 48, 65, 0, 0, 0, 63, 0, 0, 80, 65, 0, 0, - 128, 63, 0, 0, 128, 65, 0, 0, 128, 64, 0, 0, 160, 65, 0, 0, - 128, 66, 0, 0, 240, 65, 0, 0, 128, 66 + 111, 108, 18, 7, 97, 108, 99, 111, 104, 111, 108, 26, 202, 2, 10, 7, + 100, 101, 102, 97, 117, 108, 116, 18, 21, 10, 4, 112, 97, 114, 107, 34, + 8, 8, 2, 18, 4, 0, 0, 128, 63, 45, 255, 159, 223, 200, 18, 31, + 10, 4, 119, 111, 111, 100, 34, 8, 8, 2, 18, 4, 0, 0, 128, 63, + 45, 255, 102, 170, 51, 58, 8, 8, 2, 18, 4, 205, 204, 204, 61, 18, + 27, 10, 5, 119, 97, 116, 101, 114, 34, 8, 8, 2, 18, 4, 0, 0, + 128, 63, 45, 255, 233, 224, 186, 53, 255, 230, 182, 115, 18, 51, 10, 8, + 98, 117, 105, 108, 100, 105, 110, 103, 34, 8, 8, 2, 18, 4, 0, 0, + 128, 63, 45, 255, 0, 0, 0, 58, 24, 8, 4, 18, 20, 0, 0, 80, + 65, 0, 0, 0, 0, 205, 204, 204, 61, 0, 0, 0, 0, 205, 204, 204, + 61, 26, 41, 10, 12, 114, 111, 97, 100, 95, 108, 105, 109, 105, 116, 101, + 100, 29, 255, 187, 187, 187, 34, 20, 8, 3, 18, 16, 0, 0, 0, 0, + 0, 0, 128, 63, 0, 0, 240, 65, 0, 0, 128, 63, 26, 65, 10, 12, + 114, 111, 97, 100, 95, 114, 101, 103, 117, 108, 97, 114, 29, 255, 153, 153, + 153, 34, 44, 8, 3, 18, 40, 0, 0, 0, 0, 0, 0, 0, 63, 0, + 0, 80, 65, 0, 0, 0, 63, 0, 0, 128, 65, 0, 0, 0, 64, 0, + 0, 160, 65, 0, 0, 0, 66, 0, 0, 240, 65, 0, 0, 0, 66, 26, + 71, 10, 10, 114, 111, 97, 100, 95, 108, 97, 114, 103, 101, 29, 255, 102, + 102, 102, 34, 52, 8, 3, 18, 48, 0, 0, 0, 0, 0, 0, 0, 63, + 0, 0, 48, 65, 0, 0, 0, 63, 0, 0, 80, 65, 0, 0, 128, 63, + 0, 0, 128, 65, 0, 0, 128, 64, 0, 0, 160, 65, 0, 0, 128, 66, + 0, 0, 240, 65, 0, 0, 128, 66 }; const unsigned long resources::style_size = sizeof(resources::style); From c922980eebc6d32e68c8cfcd5f90eeb767791549 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Wed, 5 Feb 2014 18:15:18 +0100 Subject: [PATCH 5/8] change back to use web-provided version of resources --- include/llmr/platform/platform.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/llmr/platform/platform.hpp b/include/llmr/platform/platform.hpp index 4957f7042..b31a112b8 100644 --- a/include/llmr/platform/platform.hpp +++ b/include/llmr/platform/platform.hpp @@ -5,8 +5,8 @@ #include #include -#define kTileURL "http://pepper.local:3333/gl/tiles/plain/%d-%d-%d.vector.pbf" -#define kSpriteURL "http://pepper.local:3333/gl/debug/img/sprite" +#define kTileURL "http://mapbox:magic@kkaefer.net/gl/tiles/plain/%d-%d-%d.vector.pbf" +#define kSpriteURL "http://mapbox:magic@kkaefer.net/gl/debug/img/sprite" namespace llmr { From e94cb1b00b81685a958219e749ea638dc087adb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Wed, 5 Feb 2014 18:35:19 +0100 Subject: [PATCH 6/8] remove switchShader() --- include/llmr/renderer/painter.hpp | 40 +++++++---------- include/llmr/renderer/shader.hpp | 3 +- src/renderer/painter.cpp | 72 +++++-------------------------- src/renderer/shader-fill.cpp | 3 +- src/renderer/shader-line.cpp | 5 +-- src/renderer/shader-outline.cpp | 3 +- src/renderer/shader-pattern.cpp | 3 +- src/renderer/shader-plain.cpp | 3 +- 8 files changed, 36 insertions(+), 96 deletions(-) diff --git a/include/llmr/renderer/painter.hpp b/include/llmr/renderer/painter.hpp index 7688763ac..b638fcca1 100644 --- a/include/llmr/renderer/painter.hpp +++ b/include/llmr/renderer/painter.hpp @@ -6,20 +6,19 @@ #include #include +#include +#include +#include +#include +#include + + namespace llmr { class Transform; class Settings; class Style; class Tile; -// class LayerDescription; - -class Shader; -class PlainShader; -class OutlineShader; -class FillShader; -class LineShader; -class PatternShader; class FillBucket; class LineBucket; @@ -34,29 +33,21 @@ public: Painter& operator=(const Painter&) = delete; Painter& operator=(const Painter && ) = delete; - void setup(); - void clear(); void changeMatrix(const Tile::ID& id); void render(const std::shared_ptr& tile); - void renderLayers(const std::shared_ptr& tile, const std::vector& layers); - void renderDebug(const std::shared_ptr& tile); - void renderMatte(); void renderBackground(); void renderFill(FillBucket& bucket, const std::string& layer_name, const Tile::ID& id); void renderLine(LineBucket& bucket, const std::string& layer_name, const Tile::ID& id); - - void drawClippingMask(); - bool switchShader(std::shared_ptr shader); - private: void setupShaders(); - -public: + void drawClippingMask(); + void renderLayers(const std::shared_ptr& tile, const std::vector& layers); + void renderDebug(const std::shared_ptr& tile); private: Transform& transform; @@ -67,12 +58,11 @@ private: mat4 matrix; mat4 exMatrix; - std::shared_ptr currentShader; - std::shared_ptr fillShader; - std::shared_ptr plainShader; - std::shared_ptr outlineShader; - std::shared_ptr lineShader; - std::shared_ptr patternShader; + std::unique_ptr fillShader; + std::unique_ptr plainShader; + std::unique_ptr outlineShader; + std::unique_ptr lineShader; + std::unique_ptr patternShader; // Set up the stencil quad we're using to generate the stencil mask. VertexBuffer tileStencilBuffer = { diff --git a/include/llmr/renderer/shader.hpp b/include/llmr/renderer/shader.hpp index a32d0b647..26674d2d6 100644 --- a/include/llmr/renderer/shader.hpp +++ b/include/llmr/renderer/shader.hpp @@ -1,7 +1,7 @@ #ifndef LLMR_RENDERER_SHADER #define LLMR_RENDERER_SHADER -#include +#include namespace llmr { @@ -18,7 +18,6 @@ public: bool valid; uint32_t program; - std::forward_list attributes; private: bool compileShader(uint32_t *shader, uint32_t type, const char *source); diff --git a/src/renderer/painter.cpp b/src/renderer/painter.cpp index 3ef7fbe22..4a3a4456f 100644 --- a/src/renderer/painter.cpp +++ b/src/renderer/painter.cpp @@ -1,16 +1,13 @@ #include #include -#include -#include -#include -#include -#include -#include -#include +#include +#include + #include #include + #include #include #include @@ -25,8 +22,7 @@ using namespace llmr; Painter::Painter(Transform& transform, Settings& settings, Style& style) : transform(transform), settings(settings), - style(style), - currentShader(NULL) { + style(style) { } @@ -48,11 +44,11 @@ void Painter::setup() { } void Painter::setupShaders() { - fillShader = std::make_shared(); - plainShader = std::make_shared(); - outlineShader = std::make_shared(); - lineShader = std::make_shared(); - patternShader = std::make_shared(); + fillShader = std::make_unique(); + plainShader = std::make_unique(); + outlineShader = std::make_unique(); + lineShader = std::make_unique(); + patternShader = std::make_unique(); } void Painter::changeMatrix(const Tile::ID& id) { @@ -439,51 +435,3 @@ void Painter::renderBackground() { glUniform4fv(plainShader->u_color, 1, white.data()); glDrawArrays(GL_TRIANGLES, 0, tileStencilBuffer.length()); } - - -// Switches to a different shader program. -/** - * @return boolean whether the shader was actually switched - */ -bool Painter::switchShader(std::shared_ptr shader) { - assert(false); - if (currentShader != shader) { - glUseProgram(shader->program); - - // Disable all attributes from the existing shader that aren't used in - // the new shader. Note: attribute indices are *not* program specific! - if (currentShader) { - const std::forward_list& hitherto = currentShader->attributes; - const std::forward_list& henceforth = shader->attributes; - - // Find all attribute indices that are used in the old shader, - // but unused in the new one. - for (uint32_t i : hitherto) { - if (std::find(henceforth.begin(), henceforth.end(), i) == henceforth.end()) { - glDisableVertexAttribArray(i); - } - } - - // Enable all attributes for the new shader that were not already in - // use in the old one. - for (uint32_t i : henceforth) { - if (std::find(hitherto.begin(), hitherto.end(), i) == hitherto.end()) { - glEnableVertexAttribArray(i); - } - } - - currentShader = shader; - } else { - // Enable all attributes for the new shader (== first shader). - for (GLuint index : shader->attributes) { - glEnableVertexAttribArray(index); - } - } - - currentShader = shader; - - return true; - } else { - return false; - } -} diff --git a/src/renderer/shader-fill.cpp b/src/renderer/shader-fill.cpp index a5536bf83..12e851de8 100644 --- a/src/renderer/shader-fill.cpp +++ b/src/renderer/shader-fill.cpp @@ -2,6 +2,8 @@ #include #include +#include + using namespace llmr; FillShader::FillShader() @@ -15,7 +17,6 @@ FillShader::FillShader() } a_pos = glGetAttribLocation(program, "a_pos"); - attributes.emplace_front(a_pos); u_matrix = glGetUniformLocation(program, "u_matrix"); u_color = glGetUniformLocation(program, "u_color"); diff --git a/src/renderer/shader-line.cpp b/src/renderer/shader-line.cpp index 7a1c0b5a3..cbd2d15d7 100644 --- a/src/renderer/shader-line.cpp +++ b/src/renderer/shader-line.cpp @@ -2,6 +2,8 @@ #include #include +#include + using namespace llmr; LineShader::LineShader() @@ -15,11 +17,8 @@ LineShader::LineShader() } a_pos = glGetAttribLocation(program, "a_pos"); - attributes.emplace_front(a_pos); a_extrude = glGetAttribLocation(program, "a_extrude"); - attributes.emplace_front(a_extrude); a_linesofar = glGetAttribLocation(program, "a_linesofar"); - attributes.emplace_front(a_linesofar); u_matrix = glGetUniformLocation(program, "u_matrix"); u_exmatrix = glGetUniformLocation(program, "u_exmatrix"); diff --git a/src/renderer/shader-outline.cpp b/src/renderer/shader-outline.cpp index 82f31e1dc..6add2d231 100644 --- a/src/renderer/shader-outline.cpp +++ b/src/renderer/shader-outline.cpp @@ -2,6 +2,8 @@ #include #include +#include + using namespace llmr; OutlineShader::OutlineShader() @@ -15,7 +17,6 @@ OutlineShader::OutlineShader() } a_pos = glGetAttribLocation(program, "a_pos"); - attributes.emplace_front(a_pos); u_matrix = glGetUniformLocation(program, "u_matrix"); u_color = glGetUniformLocation(program, "u_color"); diff --git a/src/renderer/shader-pattern.cpp b/src/renderer/shader-pattern.cpp index 4a921c665..4f3c252f4 100644 --- a/src/renderer/shader-pattern.cpp +++ b/src/renderer/shader-pattern.cpp @@ -2,6 +2,8 @@ #include #include +#include + using namespace llmr; PatternShader::PatternShader() @@ -15,7 +17,6 @@ PatternShader::PatternShader() } a_pos = glGetAttribLocation(program, "a_pos"); - attributes.emplace_front(a_pos); u_matrix = glGetUniformLocation(program, "u_matrix"); u_color = glGetUniformLocation(program, "u_color"); diff --git a/src/renderer/shader-plain.cpp b/src/renderer/shader-plain.cpp index 42c5ea564..33cf17227 100644 --- a/src/renderer/shader-plain.cpp +++ b/src/renderer/shader-plain.cpp @@ -2,6 +2,8 @@ #include #include +#include + using namespace llmr; PlainShader::PlainShader() @@ -15,7 +17,6 @@ PlainShader::PlainShader() } a_pos = glGetAttribLocation(program, "a_pos"); - attributes.emplace_front(a_pos); u_matrix = glGetUniformLocation(program, "u_matrix"); u_color = glGetUniformLocation(program, "u_color"); From eabd99d8f6d00523e56fd8d329dc011bf22d51dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Wed, 5 Feb 2014 18:46:23 +0100 Subject: [PATCH 7/8] always pass the parameters and verify that they're correct --- include/llmr/geometry/vao.hpp | 44 +++++++++++++++++---------- include/llmr/renderer/fill_bucket.hpp | 14 ++------- src/renderer/line_bucket.cpp | 8 ++--- src/renderer/painter.cpp | 38 +++++------------------ 4 files changed, 40 insertions(+), 64 deletions(-) diff --git a/include/llmr/geometry/vao.hpp b/include/llmr/geometry/vao.hpp index 5e0b54ba6..d99ae6c78 100644 --- a/include/llmr/geometry/vao.hpp +++ b/include/llmr/geometry/vao.hpp @@ -3,38 +3,50 @@ #include -#include +#include namespace llmr { class VertexArrayObject { public: template - inline void setup(Shader& shader, VertexBuffer& vertex_buffer, char *vertex_offset) { - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); + void bind(Shader& shader, VertexBuffer& vertex_buffer, char *vertex_offset) { + if (!vao) { + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); - vertex_buffer.bind(); - shader.bind_vertex(vertex_offset); + vertex_buffer.bind(); + shader.bind_vertex(vertex_offset); + + program = shader.program; + buffer = &vertex_buffer; + offset = vertex_offset; + } else if (program != shader.program) { + throw std::runtime_error("trying to bind VAO to different program"); + } else if (buffer != &vertex_buffer) { + throw std::runtime_error("trying to bind VAO to another buffer"); + } else if (offset != vertex_offset) { + throw std::runtime_error("trying to bind VAO to another offset"); + } else { + // This is the correct program for this VAO. + glBindVertexArray(vao); + } } - inline ~VertexArrayObject() { + ~VertexArrayObject() { if (vao) { glDeleteVertexArrays(1, &vao); } } - inline operator bool() const { - return vao != 0; - } - - inline void bind() { - assert(vao); - glBindVertexArray(vao); - } - private: GLuint vao = 0; + + // For debug reasons, we're storing the bind information so that we can + // detect errors and report + GLuint program = 0; + void *buffer = NULL; + char *offset = 0; }; } diff --git a/include/llmr/renderer/fill_bucket.hpp b/include/llmr/renderer/fill_bucket.hpp index 774b00811..6c54107c3 100644 --- a/include/llmr/renderer/fill_bucket.hpp +++ b/include/llmr/renderer/fill_bucket.hpp @@ -66,11 +66,7 @@ void FillBucket::drawElements(Shader& shader) { char *vertex_index = BUFFER_OFFSET(vertex_start * 2 * sizeof(int16_t)); char *elements_index = BUFFER_OFFSET(elements_start * 3 * sizeof(int16_t)); for (group& group : groups) { - if (!group.array) { - group.array.setup(shader, *buffer, vertex_index); - } else { - group.array.bind(); - } + group.array.bind(shader, *buffer, vertex_index); glDrawElements(GL_TRIANGLES, group.elements_length * 3 - 3, GL_UNSIGNED_SHORT, elements_index); vertex_index += group.vertex_length * 2 * sizeof(uint16_t); elements_index += group.elements_length * 3 * sizeof(uint16_t); @@ -80,12 +76,8 @@ void FillBucket::drawElements(Shader& shader) { template void FillBucket::drawVertices(Shader& shader) { // Draw the entire line - if (!array) { - char *vertex_index = BUFFER_OFFSET(vertex_start * 2 * sizeof(int16_t)); - array.setup(shader, *buffer, vertex_index); - } else { - array.bind(); - } + char *vertex_index = BUFFER_OFFSET(vertex_start * 2 * sizeof(int16_t)); + array.bind(shader, *buffer, vertex_index); glDrawArrays(GL_LINE_STRIP, 0, length); } diff --git a/src/renderer/line_bucket.cpp b/src/renderer/line_bucket.cpp index 5dc0710b8..6e95a7bc6 100644 --- a/src/renderer/line_bucket.cpp +++ b/src/renderer/line_bucket.cpp @@ -270,12 +270,8 @@ size_t LineBucket::size() const { } void LineBucket::bind(LineShader& shader) { - if (!array) { - char *vertex_index = BUFFER_OFFSET(start * 4 * sizeof(int16_t)); - array.setup(shader, *buffer, vertex_index); - } else { - array.bind(); - } + char *vertex_index = BUFFER_OFFSET(start * 4 * sizeof(int16_t)); + array.bind(shader, *buffer, vertex_index); } void LineBucket::drawLines() { diff --git a/src/renderer/painter.cpp b/src/renderer/painter.cpp index 4a3a4456f..3187739e8 100644 --- a/src/renderer/painter.cpp +++ b/src/renderer/painter.cpp @@ -93,11 +93,8 @@ void Painter::drawClippingMask() { glStencilMask(0xC0); glStencilOp(GL_REPLACE, GL_KEEP, GL_KEEP); - if (!tileStencilArray) { - tileStencilArray.setup(*plainShader, tileStencilBuffer, BUFFER_OFFSET(0)); - } else { - tileStencilArray.bind(); - } + + tileStencilArray.bind(*plainShader, tileStencilBuffer, BUFFER_OFFSET(0)); // Draw the clipping mask glUniform4f(plainShader->u_color, 1.0f, 0.0f, 1.0f, 1.0f); @@ -281,11 +278,7 @@ void Painter::renderFill(FillBucket& bucket, const std::string& layer_name, cons glUniform4fv(fillShader->u_color, 1, fill_color.data()); // Draw a rectangle that covers the entire viewport. - if (!tileStencilArray) { - tileStencilArray.setup(*plainShader, tileStencilBuffer, BUFFER_OFFSET(0)); - } else { - tileStencilArray.bind(); - } + tileStencilArray.bind(*plainShader, tileStencilBuffer, BUFFER_OFFSET(0)); glDrawArrays(GL_TRIANGLES, 0, tileStencilBuffer.length()); } @@ -373,21 +366,13 @@ void Painter::renderDebug(const Tile::Ptr& tile) { glUniformMatrix4fv(plainShader->u_matrix, 1, GL_FALSE, matrix.data()); // draw tile outline - if (!tileBorderArray) { - tileBorderArray.setup(*plainShader, tileBorderBuffer, BUFFER_OFFSET(0)); - } else { - tileBorderArray.bind(); - } + tileBorderArray.bind(*plainShader, tileBorderBuffer, BUFFER_OFFSET(0)); glUniform4f(plainShader->u_color, 1.0f, 0.0f, 0.0f, 1.0f); glLineWidth(4.0f); glDrawArrays(GL_LINE_STRIP, 0, tileBorderBuffer.length()); // draw debug info - if (!tile->debugFontArray) { - tile->debugFontArray.setup(*plainShader, tile->debugFontBuffer, BUFFER_OFFSET(0)); - } else { - tile->debugFontArray.bind(); - } + tile->debugFontArray.bind(*plainShader, tile->debugFontBuffer, BUFFER_OFFSET(0)); glUniform4f(plainShader->u_color, 1.0f, 1.0f, 1.0f, 1.0f); glLineWidth(4.0f); glDrawArrays(GL_LINES, 0, tile->debugFontBuffer.length()); @@ -407,13 +392,8 @@ void Painter::renderMatte() { glUseProgram(plainShader->program); glUniformMatrix4fv(plainShader->u_matrix, 1, GL_FALSE, nativeMatrix.data()); - if (!matteArray) { - matteArray.setup(*plainShader, matteBuffer, BUFFER_OFFSET(0)); - } else { - matteArray.bind(); - } - // Draw the clipping mask + matteArray.bind(*plainShader, matteBuffer, BUFFER_OFFSET(0)); glUniform4fv(plainShader->u_color, 1, white.data()); glDrawArrays(GL_TRIANGLES, 0, matteBuffer.length()); @@ -427,11 +407,7 @@ void Painter::renderBackground() { glUniformMatrix4fv(plainShader->u_matrix, 1, GL_FALSE, matrix.data()); // Draw the clipping mask - if (!tileStencilArray) { - tileStencilArray.setup(*plainShader, tileStencilBuffer, BUFFER_OFFSET(0)); - } else { - tileStencilArray.bind(); - } + tileStencilArray.bind(*plainShader, tileStencilBuffer, BUFFER_OFFSET(0)); glUniform4fv(plainShader->u_color, 1, white.data()); glDrawArrays(GL_TRIANGLES, 0, tileStencilBuffer.length()); } From cf31a809207d6cd27255ce68eb07e53b8dc898fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Wed, 5 Feb 2014 19:03:44 +0100 Subject: [PATCH 8/8] textured fills & enforce vao program with templates --- bin/style.js | 4 +- include/llmr/geometry/vao.hpp | 27 +++++----- include/llmr/map/tile.hpp | 3 +- include/llmr/renderer/fill_bucket.hpp | 32 +++--------- include/llmr/renderer/line_bucket.hpp | 2 +- include/llmr/renderer/painter.hpp | 9 ++-- include/llmr/renderer/shader-fill.hpp | 21 -------- include/llmr/renderer/shader-line.hpp | 2 +- include/llmr/renderer/shader-outline.hpp | 2 +- include/llmr/renderer/shader-pattern.hpp | 2 + include/llmr/renderer/shader-plain.hpp | 2 +- include/llmr/shader/shaders.hpp | 1 - resources/style.pbf | Bin 1032 -> 1050 bytes src/renderer/fill_bucket.cpp | 21 ++++++++ src/renderer/painter.cpp | 64 +++++++++++------------ src/renderer/shader-fill.cpp | 28 ---------- src/renderer/shader-line.cpp | 2 +- src/renderer/shader-outline.cpp | 2 +- src/renderer/shader-pattern.cpp | 5 ++ src/renderer/shader-plain.cpp | 2 +- src/shader/fill.fragment.glsl | 5 -- src/shader/fill.vertex.glsl | 7 --- src/shader/shaders.cpp | 4 -- src/style/resources.cpp | 45 ++++++++-------- 24 files changed, 114 insertions(+), 178 deletions(-) delete mode 100644 include/llmr/renderer/shader-fill.hpp delete mode 100644 src/renderer/shader-fill.cpp delete mode 100644 src/shader/fill.fragment.glsl delete mode 100644 src/shader/fill.vertex.glsl diff --git a/bin/style.js b/bin/style.js index ae2c353d2..4099b420d 100644 --- a/bin/style.js +++ b/bin/style.js @@ -83,7 +83,7 @@ module.exports = { "type": "fill", "color": "#c8df9f", "antialias": true, - // "image": "park" + "image": "park" }, "wood": { "type": "fill", @@ -96,7 +96,7 @@ module.exports = { "color": "#bae0e9", "stroke": "#73b6e6", "antialias": true, - // "image": "water_etsy" + "image": "water_etsy" }, "building": { "type": "fill", diff --git a/include/llmr/geometry/vao.hpp b/include/llmr/geometry/vao.hpp index d99ae6c78..6d710d831 100644 --- a/include/llmr/geometry/vao.hpp +++ b/include/llmr/geometry/vao.hpp @@ -7,28 +7,26 @@ namespace llmr { +template class VertexArrayObject { public: - template - void bind(Shader& shader, VertexBuffer& vertex_buffer, char *vertex_offset) { + template + void bind(Shader& shader, Buffer& buffer, char *offset) { if (!vao) { glGenVertexArrays(1, &vao); glBindVertexArray(vao); - vertex_buffer.bind(); - shader.bind_vertex(vertex_offset); + buffer.bind(); + shader.bind(offset); - program = shader.program; - buffer = &vertex_buffer; - offset = vertex_offset; - } else if (program != shader.program) { - throw std::runtime_error("trying to bind VAO to different program"); - } else if (buffer != &vertex_buffer) { + buffer_ptr = &buffer; + offset_ptr = offset; + } else if (buffer_ptr != &buffer) { throw std::runtime_error("trying to bind VAO to another buffer"); - } else if (offset != vertex_offset) { + } else if (offset_ptr != offset) { throw std::runtime_error("trying to bind VAO to another offset"); } else { - // This is the correct program for this VAO. + // We have been given the correct information. glBindVertexArray(vao); } } @@ -44,9 +42,8 @@ private: // For debug reasons, we're storing the bind information so that we can // detect errors and report - GLuint program = 0; - void *buffer = NULL; - char *offset = 0; + void *buffer_ptr = NULL; + char *offset_ptr = 0; }; } diff --git a/include/llmr/map/tile.hpp b/include/llmr/map/tile.hpp index a6457a58a..2569764cc 100644 --- a/include/llmr/map/tile.hpp +++ b/include/llmr/map/tile.hpp @@ -22,6 +22,7 @@ class VectorTile; class VectorTileLayer; class FillBuffer; class LineBuffer; +class PlainShader; class Tile : public std::enable_shared_from_this { public: @@ -73,7 +74,7 @@ public: // Holds the actual geometries in this tile. DebugFontBuffer debugFontBuffer; - VertexArrayObject debugFontArray; + VertexArrayObject debugFontArray; std::shared_ptr fillBuffer; std::shared_ptr lineBuffer; diff --git a/include/llmr/renderer/fill_bucket.hpp b/include/llmr/renderer/fill_bucket.hpp index 6c54107c3..e980125b1 100644 --- a/include/llmr/renderer/fill_bucket.hpp +++ b/include/llmr/renderer/fill_bucket.hpp @@ -18,6 +18,8 @@ namespace llmr { class Style; class FillBuffer; class BucketDescription; +class OutlineShader; +class PlainShader; struct Coordinate; struct pbf; @@ -31,8 +33,8 @@ public: void addGeometry(const std::vector& line); uint32_t size() const; - template void drawElements(Shader& shader); - template void drawVertices(Shader& shader); + void drawElements(PlainShader& shader); + void drawVertices(OutlineShader& shader); public: const BucketGeometryDescription geom_desc; @@ -41,7 +43,7 @@ private: std::shared_ptr buffer; struct group { - VertexArrayObject array; + VertexArrayObject array; uint32_t vertex_length; uint32_t elements_length; @@ -56,32 +58,10 @@ private: uint32_t vertex_start; uint32_t elements_start; uint32_t length; - VertexArrayObject array; + VertexArrayObject array; std::vector groups; }; - -template -void FillBucket::drawElements(Shader& shader) { - char *vertex_index = BUFFER_OFFSET(vertex_start * 2 * sizeof(int16_t)); - char *elements_index = BUFFER_OFFSET(elements_start * 3 * sizeof(int16_t)); - for (group& group : groups) { - group.array.bind(shader, *buffer, vertex_index); - glDrawElements(GL_TRIANGLES, group.elements_length * 3 - 3, GL_UNSIGNED_SHORT, elements_index); - vertex_index += group.vertex_length * 2 * sizeof(uint16_t); - elements_index += group.elements_length * 3 * sizeof(uint16_t); - } -} - -template -void FillBucket::drawVertices(Shader& shader) { - // Draw the entire line - char *vertex_index = BUFFER_OFFSET(vertex_start * 2 * sizeof(int16_t)); - array.bind(shader, *buffer, vertex_index); - glDrawArrays(GL_LINE_STRIP, 0, length); -} - - } #endif diff --git a/include/llmr/renderer/line_bucket.hpp b/include/llmr/renderer/line_bucket.hpp index 677adbdd4..7d61af15e 100644 --- a/include/llmr/renderer/line_bucket.hpp +++ b/include/llmr/renderer/line_bucket.hpp @@ -37,7 +37,7 @@ private: std::shared_ptr buffer; uint32_t start; uint32_t length; - VertexArrayObject array; + VertexArrayObject array; }; } diff --git a/include/llmr/renderer/painter.hpp b/include/llmr/renderer/painter.hpp index b638fcca1..b991c9592 100644 --- a/include/llmr/renderer/painter.hpp +++ b/include/llmr/renderer/painter.hpp @@ -7,7 +7,6 @@ #include #include -#include #include #include #include @@ -58,7 +57,6 @@ private: mat4 matrix; mat4 exMatrix; - std::unique_ptr fillShader; std::unique_ptr plainShader; std::unique_ptr outlineShader; std::unique_ptr lineShader; @@ -77,7 +75,8 @@ private: 4096, 4096 }; - VertexArrayObject tileStencilArray; + VertexArrayObject coveringPlainArray; + VertexArrayObject coveringPatternArray; // Set up the tile boundary lines we're using to draw the tile outlines. VertexBuffer tileBorderBuffer = { @@ -88,7 +87,7 @@ private: 0, 0 }; - VertexArrayObject tileBorderArray; + VertexArrayObject tileBorderArray; // Set up the matte buffer we're using to draw the filling background. VertexBuffer matteBuffer = { @@ -103,7 +102,7 @@ private: 16384, 16384 }; - VertexArrayObject matteArray; + VertexArrayObject matteArray; }; } diff --git a/include/llmr/renderer/shader-fill.hpp b/include/llmr/renderer/shader-fill.hpp deleted file mode 100644 index beff61592..000000000 --- a/include/llmr/renderer/shader-fill.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef LLMR_RENDERER_SHADER_FILL -#define LLMR_RENDERER_SHADER_FILL - -#include "shader.hpp" - -namespace llmr { - -class FillShader : public Shader { -public: - FillShader(); - - void bind_vertex(char *offset); - - int32_t a_pos; - int32_t u_matrix; - int32_t u_color; -}; - -} - -#endif diff --git a/include/llmr/renderer/shader-line.hpp b/include/llmr/renderer/shader-line.hpp index f33e26e51..00e50c1b1 100644 --- a/include/llmr/renderer/shader-line.hpp +++ b/include/llmr/renderer/shader-line.hpp @@ -9,7 +9,7 @@ class LineShader : public Shader { public: LineShader(); - void bind_vertex(char *offset); + void bind(char *offset); int32_t a_pos; int32_t a_extrude; diff --git a/include/llmr/renderer/shader-outline.hpp b/include/llmr/renderer/shader-outline.hpp index eda1bb88b..c584487e3 100644 --- a/include/llmr/renderer/shader-outline.hpp +++ b/include/llmr/renderer/shader-outline.hpp @@ -9,7 +9,7 @@ class OutlineShader : public Shader { public: OutlineShader(); - void bind_vertex(char *offset); + void bind(char *offset); int32_t a_pos; int32_t u_matrix; diff --git a/include/llmr/renderer/shader-pattern.hpp b/include/llmr/renderer/shader-pattern.hpp index 5b295cfff..26dac99fa 100644 --- a/include/llmr/renderer/shader-pattern.hpp +++ b/include/llmr/renderer/shader-pattern.hpp @@ -9,6 +9,8 @@ class PatternShader : public Shader { public: PatternShader(); + void bind(char *offset); + int32_t a_pos; int32_t u_matrix; int32_t u_color; diff --git a/include/llmr/renderer/shader-plain.hpp b/include/llmr/renderer/shader-plain.hpp index dd3a57102..d38ae5798 100644 --- a/include/llmr/renderer/shader-plain.hpp +++ b/include/llmr/renderer/shader-plain.hpp @@ -9,7 +9,7 @@ class PlainShader : public Shader { public: PlainShader(); - void bind_vertex(char *offset); + void bind(char *offset); int32_t a_pos; int32_t u_matrix; diff --git a/include/llmr/shader/shaders.hpp b/include/llmr/shader/shaders.hpp index 065b8d47f..d3007b2cc 100644 --- a/include/llmr/shader/shaders.hpp +++ b/include/llmr/shader/shaders.hpp @@ -11,7 +11,6 @@ struct shader_source { }; enum { - FILL_SHADER, LINE_SHADER, OUTLINE_SHADER, PATTERN_SHADER, diff --git a/resources/style.pbf b/resources/style.pbf index 69475487d5afbc5de084fa8754d05505ae815014..d87f896d1a0fb748df9b35975d0a89243905cc9b 100644 GIT binary patch delta 75 zcmeC+n8mSS50l0nCNB1r)U?FXoDv~vE|!ACqHHA&4kjTM28ITE-T(9NpKt;TPQJ&a c#Hc=*ky)C@iK{%ZB(*3$wWPRmvJCSZ0Ahs~p#T5? delta 79 zcmbQm(ZR7{50la(); plainShader = std::make_unique(); outlineShader = std::make_unique(); lineShader = std::make_unique(); @@ -94,7 +92,7 @@ void Painter::drawClippingMask() { glStencilOp(GL_REPLACE, GL_KEEP, GL_KEEP); - tileStencilArray.bind(*plainShader, tileStencilBuffer, BUFFER_OFFSET(0)); + coveringPlainArray.bind(*plainShader, tileStencilBuffer, BUFFER_OFFSET(0)); // Draw the clipping mask glUniform4f(plainShader->u_color, 1.0f, 0.0f, 1.0f, 1.0f); @@ -195,11 +193,11 @@ void Painter::renderFill(FillBucket& bucket, const std::string& layer_name, cons glColorMask(false, false, false, false); // Draw the actual triangle fan into the stencil buffer. - glUseProgram(fillShader->program); - glUniformMatrix4fv(fillShader->u_matrix, 1, GL_FALSE, matrix.data()); + glUseProgram(plainShader->program); + glUniformMatrix4fv(plainShader->u_matrix, 1, GL_FALSE, matrix.data()); // Draw all groups - bucket.drawElements(*fillShader); + bucket.drawElements(*plainShader); // Now that we have the stencil mask in the stencil buffer, we can start // writing to the color buffer. @@ -243,42 +241,40 @@ void Painter::renderFill(FillBucket& bucket, const std::string& layer_name, cons glStencilFunc(GL_NOTEQUAL, 0x0, 0x3F); if (properties.image.size() && style.sprite) { - // TODO: - // // Draw texture fill - // ImagePosition imagePos = style.sprite->getPosition(properties.image, true); + // Draw texture fill + ImagePosition imagePos = style.sprite->getPosition(properties.image, true); - // double factor = 8.0 / pow(2, transform.getIntegerZoom() - id.z); - // double mix = fmod(transform.getZoom(), 1.0); - // vec2 imageSize { imagePos.size.x * factor, imagePos.size.y * factor }; + double factor = 8.0 / pow(2, transform.getIntegerZoom() - id.z); + double mix = fmod(transform.getZoom(), 1.0); + vec2 imageSize { imagePos.size.x * factor, imagePos.size.y * factor }; - // vec2 offset { - // fmod(id.x * 4096, imageSize.x), - // fmod(id.y * 4096, imageSize.y) - // }; + vec2 offset { + fmod(id.x * 4096, imageSize.x), + fmod(id.y * 4096, imageSize.y) + }; - // switchShader(patternShader); - // glUniformMatrix4fv(patternShader->u_matrix, 1, GL_FALSE, matrix.data()); - // glUniform2f(patternShader->u_pattern_size, imageSize.x, imageSize.y); - // glUniform2f(patternShader->u_offset, offset.x, offset.y); - // glUniform2f(patternShader->u_pattern_tl, imagePos.tl.x, imagePos.tl.y); - // glUniform2f(patternShader->u_pattern_br, imagePos.br.x, imagePos.br.y); - // glUniform4fv(patternShader->u_color, 1, fill_color.data()); - // glUniform1f(patternShader->u_mix, mix); - // style.sprite->bind(true); + glUseProgram(patternShader->program); + glUniformMatrix4fv(patternShader->u_matrix, 1, GL_FALSE, matrix.data()); + glUniform2f(patternShader->u_pattern_size, imageSize.x, imageSize.y); + glUniform2f(patternShader->u_offset, offset.x, offset.y); + glUniform2f(patternShader->u_pattern_tl, imagePos.tl.x, imagePos.tl.y); + glUniform2f(patternShader->u_pattern_br, imagePos.br.x, imagePos.br.y); + glUniform4fv(patternShader->u_color, 1, fill_color.data()); + glUniform1f(patternShader->u_mix, mix); + style.sprite->bind(true); - // // Draw a rectangle that covers the entire viewport. - // tileStencilBuffer.bind(); - // glVertexAttribPointer(patternShader->a_pos, 2, GL_SHORT, false, 0, BUFFER_OFFSET(0)); - // glDrawArrays(GL_TRIANGLES, 0, tileStencilBuffer.length()); + // Draw a rectangle that covers the entire viewport. + coveringPatternArray.bind(*patternShader, tileStencilBuffer, BUFFER_OFFSET(0)); + glDrawArrays(GL_TRIANGLES, 0, tileStencilBuffer.length()); } else { // Draw filling rectangle. - glUseProgram(fillShader->program); - glUniformMatrix4fv(fillShader->u_matrix, 1, GL_FALSE, matrix.data()); - glUniform4fv(fillShader->u_color, 1, fill_color.data()); + glUseProgram(plainShader->program); + glUniformMatrix4fv(plainShader->u_matrix, 1, GL_FALSE, matrix.data()); + glUniform4fv(plainShader->u_color, 1, fill_color.data()); // Draw a rectangle that covers the entire viewport. - tileStencilArray.bind(*plainShader, tileStencilBuffer, BUFFER_OFFSET(0)); + coveringPlainArray.bind(*plainShader, tileStencilBuffer, BUFFER_OFFSET(0)); glDrawArrays(GL_TRIANGLES, 0, tileStencilBuffer.length()); } @@ -407,7 +403,7 @@ void Painter::renderBackground() { glUniformMatrix4fv(plainShader->u_matrix, 1, GL_FALSE, matrix.data()); // Draw the clipping mask - tileStencilArray.bind(*plainShader, tileStencilBuffer, BUFFER_OFFSET(0)); + coveringPlainArray.bind(*plainShader, tileStencilBuffer, BUFFER_OFFSET(0)); glUniform4fv(plainShader->u_color, 1, white.data()); glDrawArrays(GL_TRIANGLES, 0, tileStencilBuffer.length()); } diff --git a/src/renderer/shader-fill.cpp b/src/renderer/shader-fill.cpp deleted file mode 100644 index 12e851de8..000000000 --- a/src/renderer/shader-fill.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include -#include - -#include - -using namespace llmr; - -FillShader::FillShader() - : Shader( - shaders[FILL_SHADER].vertex, - shaders[FILL_SHADER].fragment - ) { - if (!valid) { - fprintf(stderr, "invalid fill shader\n"); - return; - } - - a_pos = glGetAttribLocation(program, "a_pos"); - - u_matrix = glGetUniformLocation(program, "u_matrix"); - u_color = glGetUniformLocation(program, "u_color"); -} - -void FillShader::bind_vertex(char *offset) { - glEnableVertexAttribArray(a_pos); - glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 0, offset); -} diff --git a/src/renderer/shader-line.cpp b/src/renderer/shader-line.cpp index cbd2d15d7..feed1626e 100644 --- a/src/renderer/shader-line.cpp +++ b/src/renderer/shader-line.cpp @@ -31,7 +31,7 @@ LineShader::LineShader() u_gamma = glGetUniformLocation(program, "u_gamma"); } -void LineShader::bind_vertex(char *offset) { +void LineShader::bind(char *offset) { glEnableVertexAttribArray(a_pos); glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 8, offset); diff --git a/src/renderer/shader-outline.cpp b/src/renderer/shader-outline.cpp index 6add2d231..82d24b316 100644 --- a/src/renderer/shader-outline.cpp +++ b/src/renderer/shader-outline.cpp @@ -23,7 +23,7 @@ OutlineShader::OutlineShader() u_world = glGetUniformLocation(program, "u_world"); } -void OutlineShader::bind_vertex(char *offset) { +void OutlineShader::bind(char *offset) { glEnableVertexAttribArray(a_pos); glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 0, offset); } diff --git a/src/renderer/shader-pattern.cpp b/src/renderer/shader-pattern.cpp index 4f3c252f4..22afd677e 100644 --- a/src/renderer/shader-pattern.cpp +++ b/src/renderer/shader-pattern.cpp @@ -26,3 +26,8 @@ PatternShader::PatternShader() u_pattern_br = glGetUniformLocation(program, "u_pattern_br"); u_mix = glGetUniformLocation(program, "u_mix"); } + +void PatternShader::bind(char *offset) { + glEnableVertexAttribArray(a_pos); + glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 0, offset); +} diff --git a/src/renderer/shader-plain.cpp b/src/renderer/shader-plain.cpp index 33cf17227..e9d06540b 100644 --- a/src/renderer/shader-plain.cpp +++ b/src/renderer/shader-plain.cpp @@ -22,7 +22,7 @@ PlainShader::PlainShader() u_color = glGetUniformLocation(program, "u_color"); } -void PlainShader::bind_vertex(char *offset) { +void PlainShader::bind(char *offset) { glEnableVertexAttribArray(a_pos); glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 0, offset); } diff --git a/src/shader/fill.fragment.glsl b/src/shader/fill.fragment.glsl deleted file mode 100644 index 8df552c17..000000000 --- a/src/shader/fill.fragment.glsl +++ /dev/null @@ -1,5 +0,0 @@ -uniform vec4 u_color; - -void main() { - gl_FragColor = u_color; -} diff --git a/src/shader/fill.vertex.glsl b/src/shader/fill.vertex.glsl deleted file mode 100644 index 866c3cd2f..000000000 --- a/src/shader/fill.vertex.glsl +++ /dev/null @@ -1,7 +0,0 @@ -attribute vec2 a_pos; - -uniform mat4 u_matrix; - -void main() { - gl_Position = u_matrix * vec4(a_pos, 0, 1); -} diff --git a/src/shader/shaders.cpp b/src/shader/shaders.cpp index 3d0b98bc6..94d1759fe 100644 --- a/src/shader/shaders.cpp +++ b/src/shader/shaders.cpp @@ -4,10 +4,6 @@ using namespace llmr; const shader_source llmr::shaders[SHADER_COUNT] = { - { - "attribute vec2 a_pos;\n\nuniform mat4 u_matrix;\n\nvoid main() {\n gl_Position = u_matrix * vec4(a_pos, 0, 1);\n}\n", - "uniform vec4 u_color;\n\nvoid main() {\n gl_FragColor = u_color;\n}\n", - }, { "// these are the shaders for rendering antialiased lines\n\n// floor(127 / 2) == 63.0\n// the maximum allowed miter limit is 2.0 at the moment. the extrude normal is\n// stored in a byte (-128..127). we scale regular normals up to length 63, but\n// there are also \"special\" normals that have a bigger length (of up to 126 in\n// this case).\n#define scale 63.0\n\nattribute vec2 a_pos;\nattribute vec2 a_extrude;\nattribute float a_linesofar;\n\n// matrix is for the vertex position, exmatrix is for rotating and projecting\n// the extrusion vector.\nuniform mat4 u_matrix;\nuniform mat4 u_exmatrix;\n\n\nuniform float u_debug;\n\n// shared\nuniform float u_ratio;\nuniform vec2 u_linewidth;\nuniform vec4 u_color;\nuniform float u_point;\n\nvarying vec2 v_normal;\nvarying float v_linesofar;\n\nvoid main() {\n // We store the texture normals in the most insignificant bit\n // transform y so that 0 => -1 and 1 => 1\n // In the texture normal, x is 0 if the normal points straight up/down and 1 if it's a round cap\n // y is 1 if the normal points up, and -1 if it points down\n vec2 normal = mod(a_pos, 2.0);\n normal.y = sign(normal.y - 0.5);\n v_normal = normal;\n\n // Scale the extrusion vector down to a normal and then up by the line width\n // of this vertex.\n vec2 extrude = a_extrude / scale;\n vec2 dist = u_linewidth.s * extrude * (1.0 - u_point);\n\n // If the x coordinate is the maximum integer, we move the z coordinates out\n // of the view plane so that the triangle gets clipped. This makes it easier\n // for us to create degenerate triangle strips.\n float z = step(32767.0, a_pos.x);\n\n // When drawing points, skip every other vertex\n z += u_point * step(1.0, v_normal.y);\n\n // Remove the texture normal bit of the position before scaling it with the\n // model/view matrix. Add the extrusion vector *after* the model/view matrix\n // because we're extruding the line in pixel space, regardless of the current\n // tile's zoom level.\n gl_Position = u_matrix * vec4(floor(a_pos / 2.0), 0.0, 1.0) + u_exmatrix * vec4(dist, z, 0.0);\n v_linesofar = a_linesofar * u_ratio;\n\n\n gl_PointSize = 2.0 * u_linewidth.s - 1.0;\n}\n", "// shared\nuniform float u_debug;\nuniform vec2 u_linewidth;\nuniform vec4 u_color;\nuniform float u_point;\nuniform float u_gamma;\n\nuniform vec2 u_dasharray;\n\nvarying vec2 v_normal;\nvarying float v_linesofar;\n\nvoid main() {\n // Calculate the distance of the pixel from the line in pixels.\n float dist = length(v_normal) * (1.0 - u_point) + u_point * length(gl_PointCoord * 2.0 - 1.0);\n\n dist *= u_linewidth.s;\n\n // Calculate the antialiasing fade factor. This is either when fading in\n // the line in case of an offset line (v_linewidth.t) or when fading out\n // (v_linewidth.s)\n float alpha = clamp(min(dist - (u_linewidth.t - 1.0), u_linewidth.s - dist) * u_gamma, 0.0, 1.0);\n\n // Calculate the antialiasing fade factor based on distance to the dash.\n // Only affects alpha when line is dashed\n float pos = mod(v_linesofar, u_dasharray.x + u_dasharray.y);\n alpha *= max(step(0.0, -u_dasharray.y), clamp(min(pos, u_dasharray.x - pos), 0.0, 1.0));\n\n gl_FragColor = u_color * alpha;\n\n if (u_debug > 0.0) {\n gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n }\n}\n", diff --git a/src/style/resources.cpp b/src/style/resources.cpp index 9380bf025..def1933ff 100644 --- a/src/style/resources.cpp +++ b/src/style/resources.cpp @@ -47,27 +47,28 @@ const unsigned char resources::style[] = { 12, 114, 111, 97, 100, 95, 114, 101, 103, 117, 108, 97, 114, 18, 24, 10, 10, 114, 111, 97, 100, 95, 108, 97, 114, 103, 101, 18, 10, 114, 111, 97, 100, 95, 108, 97, 114, 103, 101, 18, 18, 10, 7, 97, 108, 99, 111, 104, - 111, 108, 18, 7, 97, 108, 99, 111, 104, 111, 108, 26, 202, 2, 10, 7, - 100, 101, 102, 97, 117, 108, 116, 18, 21, 10, 4, 112, 97, 114, 107, 34, - 8, 8, 2, 18, 4, 0, 0, 128, 63, 45, 255, 159, 223, 200, 18, 31, - 10, 4, 119, 111, 111, 100, 34, 8, 8, 2, 18, 4, 0, 0, 128, 63, - 45, 255, 102, 170, 51, 58, 8, 8, 2, 18, 4, 205, 204, 204, 61, 18, - 27, 10, 5, 119, 97, 116, 101, 114, 34, 8, 8, 2, 18, 4, 0, 0, - 128, 63, 45, 255, 233, 224, 186, 53, 255, 230, 182, 115, 18, 51, 10, 8, - 98, 117, 105, 108, 100, 105, 110, 103, 34, 8, 8, 2, 18, 4, 0, 0, - 128, 63, 45, 255, 0, 0, 0, 58, 24, 8, 4, 18, 20, 0, 0, 80, - 65, 0, 0, 0, 0, 205, 204, 204, 61, 0, 0, 0, 0, 205, 204, 204, - 61, 26, 41, 10, 12, 114, 111, 97, 100, 95, 108, 105, 109, 105, 116, 101, - 100, 29, 255, 187, 187, 187, 34, 20, 8, 3, 18, 16, 0, 0, 0, 0, - 0, 0, 128, 63, 0, 0, 240, 65, 0, 0, 128, 63, 26, 65, 10, 12, - 114, 111, 97, 100, 95, 114, 101, 103, 117, 108, 97, 114, 29, 255, 153, 153, - 153, 34, 44, 8, 3, 18, 40, 0, 0, 0, 0, 0, 0, 0, 63, 0, - 0, 80, 65, 0, 0, 0, 63, 0, 0, 128, 65, 0, 0, 0, 64, 0, - 0, 160, 65, 0, 0, 0, 66, 0, 0, 240, 65, 0, 0, 0, 66, 26, - 71, 10, 10, 114, 111, 97, 100, 95, 108, 97, 114, 103, 101, 29, 255, 102, - 102, 102, 34, 52, 8, 3, 18, 48, 0, 0, 0, 0, 0, 0, 0, 63, - 0, 0, 48, 65, 0, 0, 0, 63, 0, 0, 80, 65, 0, 0, 128, 63, - 0, 0, 128, 65, 0, 0, 128, 64, 0, 0, 160, 65, 0, 0, 128, 66, - 0, 0, 240, 65, 0, 0, 128, 66 + 111, 108, 18, 7, 97, 108, 99, 111, 104, 111, 108, 26, 220, 2, 10, 7, + 100, 101, 102, 97, 117, 108, 116, 18, 27, 10, 4, 112, 97, 114, 107, 34, + 8, 8, 2, 18, 4, 0, 0, 128, 63, 45, 255, 159, 223, 200, 66, 4, + 112, 97, 114, 107, 18, 31, 10, 4, 119, 111, 111, 100, 34, 8, 8, 2, + 18, 4, 0, 0, 128, 63, 45, 255, 102, 170, 51, 58, 8, 8, 2, 18, + 4, 205, 204, 204, 61, 18, 39, 10, 5, 119, 97, 116, 101, 114, 34, 8, + 8, 2, 18, 4, 0, 0, 128, 63, 45, 255, 233, 224, 186, 53, 255, 230, + 182, 115, 66, 10, 119, 97, 116, 101, 114, 95, 101, 116, 115, 121, 18, 51, + 10, 8, 98, 117, 105, 108, 100, 105, 110, 103, 34, 8, 8, 2, 18, 4, + 0, 0, 128, 63, 45, 255, 0, 0, 0, 58, 24, 8, 4, 18, 20, 0, + 0, 80, 65, 0, 0, 0, 0, 205, 204, 204, 61, 0, 0, 0, 0, 205, + 204, 204, 61, 26, 41, 10, 12, 114, 111, 97, 100, 95, 108, 105, 109, 105, + 116, 101, 100, 29, 255, 187, 187, 187, 34, 20, 8, 3, 18, 16, 0, 0, + 0, 0, 0, 0, 128, 63, 0, 0, 240, 65, 0, 0, 128, 63, 26, 65, + 10, 12, 114, 111, 97, 100, 95, 114, 101, 103, 117, 108, 97, 114, 29, 255, + 153, 153, 153, 34, 44, 8, 3, 18, 40, 0, 0, 0, 0, 0, 0, 0, + 63, 0, 0, 80, 65, 0, 0, 0, 63, 0, 0, 128, 65, 0, 0, 0, + 64, 0, 0, 160, 65, 0, 0, 0, 66, 0, 0, 240, 65, 0, 0, 0, + 66, 26, 71, 10, 10, 114, 111, 97, 100, 95, 108, 97, 114, 103, 101, 29, + 255, 102, 102, 102, 34, 52, 8, 3, 18, 48, 0, 0, 0, 0, 0, 0, + 0, 63, 0, 0, 48, 65, 0, 0, 0, 63, 0, 0, 80, 65, 0, 0, + 128, 63, 0, 0, 128, 65, 0, 0, 128, 64, 0, 0, 160, 65, 0, 0, + 128, 66, 0, 0, 240, 65, 0, 0, 128, 66 }; const unsigned long resources::style_size = sizeof(resources::style);