diff --git a/gyp/platform-android.gypi b/gyp/platform-android.gypi index bfaac7d4f..7701393bb 100644 --- a/gyp/platform-android.gypi +++ b/gyp/platform-android.gypi @@ -12,6 +12,7 @@ 'sources': [ '../platform/android/log_android.cpp', '../platform/android/asset_root.cpp', + '../platform/default/thread.cpp', '../platform/default/string_stdlib.cpp', '../platform/default/image.cpp', '../platform/default/image_reader.cpp', diff --git a/gyp/platform-ios.gypi b/gyp/platform-ios.gypi index f1f95d908..d23192c8c 100644 --- a/gyp/platform-ios.gypi +++ b/gyp/platform-ios.gypi @@ -15,6 +15,7 @@ '../platform/darwin/application_root.mm', '../platform/darwin/asset_root.mm', '../platform/darwin/image.mm', + '../platform/darwin/nsthread.mm', '../platform/darwin/reachability.m', '../include/mbgl/ios/MapboxGL.h', '../include/mbgl/ios/MGLMapboxEvents.h', diff --git a/gyp/platform-linux.gypi b/gyp/platform-linux.gypi index 30715f828..394433870 100644 --- a/gyp/platform-linux.gypi +++ b/gyp/platform-linux.gypi @@ -14,6 +14,7 @@ '../platform/default/string_stdlib.cpp', '../platform/default/application_root.cpp', '../platform/default/asset_root.cpp', + '../platform/default/thread.cpp', '../platform/default/image.cpp', '../platform/default/image_reader.cpp', '../platform/default/png_reader.cpp', diff --git a/gyp/platform-osx.gypi b/gyp/platform-osx.gypi index 6d6c35b29..ae5194761 100644 --- a/gyp/platform-osx.gypi +++ b/gyp/platform-osx.gypi @@ -15,6 +15,7 @@ '../platform/darwin/application_root.mm', '../platform/darwin/asset_root.mm', '../platform/darwin/image.mm', + '../platform/darwin/nsthread.mm', ], 'variables': { diff --git a/include/mbgl/platform/gl.hpp b/include/mbgl/platform/gl.hpp index 61701a438..5c10f1610 100644 --- a/include/mbgl/platform/gl.hpp +++ b/include/mbgl/platform/gl.hpp @@ -159,7 +159,7 @@ inline void start_group(const std::string &str) { if (gl::PushDebugGroup != nullptr) { MBGL_CHECK_ERROR(gl::PushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, GLsizei(str.size()), str.c_str())); } else if (gl::PushGroupMarkerEXT != nullptr) { - MBGL_CHECK_ERROR(gl::PushGroupMarkerEXT(GLsizei(str.size()), str.c_str())); + MBGL_CHECK_ERROR(gl::PushGroupMarkerEXT(GLsizei(str.size() + 1), str.c_str())); } // fprintf(stderr, "%s%s\n", std::string(indent * 4, ' ').c_str(), str.c_str()); // indent++; diff --git a/include/mbgl/platform/platform.hpp b/include/mbgl/platform/platform.hpp index cd87e2256..f828af37f 100644 --- a/include/mbgl/platform/platform.hpp +++ b/include/mbgl/platform/platform.hpp @@ -21,6 +21,9 @@ const std::string &applicationRoot(); // Returns the path to the asset location. const std::string &assetRoot(); +// Makes the current thread low priority. +void makeThreadLowPriority(); + // Shows an alpha image with the specified dimensions in a named window. void showDebugImage(std::string name, const char *data, size_t width, size_t height); diff --git a/include/mbgl/util/gl_helper.hpp b/include/mbgl/util/gl_helper.hpp new file mode 100644 index 000000000..cf007e8a6 --- /dev/null +++ b/include/mbgl/util/gl_helper.hpp @@ -0,0 +1,87 @@ +#ifndef MBGL_UTIL_GL_HELPER +#define MBGL_UTIL_GL_HELPER + +#include + +#include + +namespace { + +template +class Preserve { +public: + Preserve() : data(Create()) {} + ~Preserve() { Destroy(data); } + +private: + const T data; +}; + +inline bool getBlend() { + return glIsEnabled(GL_BLEND); +} + +inline void setBlend(const bool& enabled) { + enabled ? MBGL_CHECK_ERROR(glEnable(GL_BLEND)) : MBGL_CHECK_ERROR(glDisable(GL_BLEND)); +} + +inline std::array getClearColor() { + std::array color; + MBGL_CHECK_ERROR(glGetDoublev(GL_COLOR_CLEAR_VALUE, color.data())); + return color; +} + +inline void setClearColor(const std::array& color) { + MBGL_CHECK_ERROR(glClearColor(color[0], color[1], color[2], color[3])); +} + + +inline std::array getBlendFunc() { + GLint func[2]; + glGetIntegerv(GL_BLEND_SRC, &func[0]); + glGetIntegerv(GL_BLEND_DST, &func[1]); + return {{ static_cast(func[0]), static_cast(func[1]) }}; +} + +inline void setBlendFunc(const std::array& func) { + MBGL_CHECK_ERROR(glBlendFunc(func[0], func[1])); +} + + +inline std::array getPixelZoom() { + std::array zoom; + glGetDoublev(GL_ZOOM_X, &zoom[0]); + glGetDoublev(GL_ZOOM_Y, &zoom[1]); + return zoom; +} + +inline void setPixelZoom(const std::array& func) { + MBGL_CHECK_ERROR(glPixelZoom(func[0], func[1])); +} + + +inline std::array getRasterPos() { + std::array pos; + MBGL_CHECK_ERROR(glGetDoublev(GL_CURRENT_RASTER_POSITION, pos.data())); + return pos; +} + +inline void setRasterPos(const std::array& pos) { + MBGL_CHECK_ERROR(glRasterPos4d(pos[0], pos[1], pos[2], pos[3])); +} + +} // end anonymous namespace + +namespace mbgl { +namespace gl { + +using PreserveBlend = Preserve; +using PreserveClearColor = Preserve, getClearColor, setClearColor>; +using PreserveBlendFunc = Preserve, getBlendFunc, setBlendFunc>; +using PreservePixelZoom = Preserve, getPixelZoom, setPixelZoom>; +using PreserveRasterPos = Preserve, getRasterPos, setRasterPos>; + +} +} + +#endif diff --git a/platform/darwin/nsthread.mm b/platform/darwin/nsthread.mm new file mode 100644 index 000000000..9ac1d2caa --- /dev/null +++ b/platform/darwin/nsthread.mm @@ -0,0 +1,13 @@ +#import + +#include + +namespace mbgl { +namespace platform { + +void makeThreadLowPriority() { + [[NSThread currentThread] setThreadPriority:0.0]; +} + +} +} diff --git a/platform/default/glfw_view.cpp b/platform/default/glfw_view.cpp index 84de908db..56aeaf30a 100644 --- a/platform/default/glfw_view.cpp +++ b/platform/default/glfw_view.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -333,15 +334,20 @@ void showDebugImage(std::string name, const char *data, size_t width, size_t hei glfwGetFramebufferSize(debugWindow, &fbWidth, &fbHeight); float scale = static_cast(fbWidth) / static_cast(width); - MBGL_CHECK_ERROR(glPixelZoom(scale, -scale)); - MBGL_CHECK_ERROR(glRasterPos2f(-1.0f, 1.0f)); - MBGL_CHECK_ERROR(glDrawPixels(width, height, GL_LUMINANCE, GL_UNSIGNED_BYTE, data)); + { + gl::PreservePixelZoom pixelZoom; + gl::PreserveRasterPos rasterPos; + + MBGL_CHECK_ERROR(glPixelZoom(scale, -scale)); + MBGL_CHECK_ERROR(glRasterPos2f(-1.0f, 1.0f)); + MBGL_CHECK_ERROR(glDrawPixels(width, height, GL_LUMINANCE, GL_UNSIGNED_BYTE, data)); + } + glfwSwapBuffers(debugWindow); glfwMakeContextCurrent(currentWindow); } - void showColorDebugImage(std::string name, const char *data, size_t logicalWidth, size_t logicalHeight, size_t width, size_t height) { glfwInit(); @@ -365,14 +371,22 @@ void showColorDebugImage(std::string name, const char *data, size_t logicalWidth float xScale = static_cast(fbWidth) / static_cast(width); float yScale = static_cast(fbHeight) / static_cast(height); - MBGL_CHECK_ERROR(glClearColor(0.8, 0.8, 0.8, 1)); - MBGL_CHECK_ERROR(glClear(GL_COLOR_BUFFER_BIT)); - MBGL_CHECK_ERROR(glEnable(GL_BLEND)); - MBGL_CHECK_ERROR(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)); + { + gl::PreserveClearColor clearColor; + gl::PreserveBlend blend; + gl::PreserveBlendFunc blendFunc; + gl::PreservePixelZoom pixelZoom; + gl::PreserveRasterPos rasterPos; + + MBGL_CHECK_ERROR(glClearColor(0.8, 0.8, 0.8, 1)); + MBGL_CHECK_ERROR(glClear(GL_COLOR_BUFFER_BIT)); + MBGL_CHECK_ERROR(glEnable(GL_BLEND)); + MBGL_CHECK_ERROR(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)); + MBGL_CHECK_ERROR(glPixelZoom(xScale, -yScale)); + MBGL_CHECK_ERROR(glRasterPos2f(-1.0f, 1.0f)); + MBGL_CHECK_ERROR(glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, data)); + } - MBGL_CHECK_ERROR(glPixelZoom(xScale, -yScale)); - MBGL_CHECK_ERROR(glRasterPos2f(-1.0f, 1.0f)); - MBGL_CHECK_ERROR(glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, data)); glfwSwapBuffers(debugWindow); glfwMakeContextCurrent(currentWindow); diff --git a/platform/default/sqlite_cache.cpp b/platform/default/sqlite_cache.cpp index 83d42d5a5..fb4cdf74e 100644 --- a/platform/default/sqlite_cache.cpp +++ b/platform/default/sqlite_cache.cpp @@ -62,7 +62,7 @@ std::string unifyMapboxURLs(const std::string &url) { using namespace mapbox::sqlite; SQLiteCache::SQLiteCache(const std::string& path_) - : thread(util::make_unique>("SQLite Cache", path_)) { + : thread(util::make_unique>("SQLite Cache", util::ThreadPriority::Low, path_)) { } SQLiteCache::~SQLiteCache() = default; diff --git a/platform/default/thread.cpp b/platform/default/thread.cpp new file mode 100644 index 000000000..c0a1069b9 --- /dev/null +++ b/platform/default/thread.cpp @@ -0,0 +1,11 @@ +#include + +namespace mbgl { +namespace platform { + +void makeThreadLowPriority() { + // no-op +} + +} +} diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm index 99fc55e7e..fd448b899 100644 --- a/platform/ios/MGLMapView.mm +++ b/platform/ios/MGLMapView.mm @@ -249,6 +249,12 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration) gl::IsVertexArray = glIsVertexArrayOES; } + if (extensions.find("GL_EXT_debug_marker") != std::string::npos) { + gl::InsertEventMarkerEXT = glInsertEventMarkerEXT; + gl::PushGroupMarkerEXT = glPushGroupMarkerEXT; + gl::PopGroupMarkerEXT = glPopGroupMarkerEXT; + } + if (extensions.find("GL_OES_packed_depth_stencil") != std::string::npos) { gl::isPackedDepthStencilSupported = YES; } diff --git a/src/mbgl/geometry/buffer.hpp b/src/mbgl/geometry/buffer.hpp index 4198425ec..7e3ced442 100644 --- a/src/mbgl/geometry/buffer.hpp +++ b/src/mbgl/geometry/buffer.hpp @@ -2,6 +2,7 @@ #define MBGL_GEOMETRY_BUFFER #include +#include #include #include @@ -38,17 +39,16 @@ public: } // Transfers this buffer to the GPU and binds the buffer to the GL context. - void bind(bool force = false) { - if (buffer == 0) { + void bind() { + if (buffer) { + MBGL_CHECK_ERROR(glBindBuffer(bufferType, buffer)); + } else { MBGL_CHECK_ERROR(glGenBuffers(1, &buffer)); - force = true; - } - MBGL_CHECK_ERROR(glBindBuffer(bufferType, buffer)); - if (force) { + MBGL_CHECK_ERROR(glBindBuffer(bufferType, buffer)); if (array == nullptr) { - throw std::runtime_error("Buffer was already deleted or doesn't contain elements"); + Log::Debug(Event::OpenGL, "Buffer doesn't contain elements"); + pos = 0; } - MBGL_CHECK_ERROR(glBufferData(bufferType, pos, array, GL_STATIC_DRAW)); if (!retainAfterUpload) { cleanup(); @@ -67,6 +67,13 @@ public: return buffer; } + // Uploads the buffer to the GPU to be available when we need it. + inline void upload() { + if (!buffer) { + bind(); + } + } + protected: // increase the buffer size by at least /required/ bytes. inline void *addElement() { diff --git a/src/mbgl/geometry/glyph_atlas.cpp b/src/mbgl/geometry/glyph_atlas.cpp index f690004b5..f2978de98 100644 --- a/src/mbgl/geometry/glyph_atlas.cpp +++ b/src/mbgl/geometry/glyph_atlas.cpp @@ -124,8 +124,6 @@ void GlyphAtlas::removeGlyphs(uintptr_t tileUID) { } } - dirty = true; - bin.release(rect); // Make sure to post-increment the iterator: This will return the @@ -140,6 +138,47 @@ void GlyphAtlas::removeGlyphs(uintptr_t tileUID) { } } +void GlyphAtlas::upload() { + if (dirty) { + const bool first = !texture; + bind(); + + std::lock_guard lock(mtx); + + if (first) { + MBGL_CHECK_ERROR(glTexImage2D( + GL_TEXTURE_2D, // GLenum target + 0, // GLint level + GL_ALPHA, // GLint internalformat + width, // GLsizei width + height, // GLsizei height + 0, // GLint border + GL_ALPHA, // GLenum format + GL_UNSIGNED_BYTE, // GLenum type + data.get() // const GLvoid* data + )); + } else { + MBGL_CHECK_ERROR(glTexSubImage2D( + GL_TEXTURE_2D, // GLenum target + 0, // GLint level + 0, // GLint xoffset + 0, // GLint yoffset + width, // GLsizei width + height, // GLsizei height + GL_ALPHA, // GLenum format + GL_UNSIGNED_BYTE, // GLenum type + data.get() // const GLvoid* data + )); + } + + dirty = false; + +#if defined(DEBUG) + // platform::showDebugImage("Glyph Atlas", data.get(), width, height); +#endif + } +} + void GlyphAtlas::bind() { if (!texture) { MBGL_CHECK_ERROR(glGenTextures(1, &texture)); @@ -154,14 +193,4 @@ void GlyphAtlas::bind() { } else { MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture)); } - - if (dirty) { - std::lock_guard lock(mtx); - MBGL_CHECK_ERROR(glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, data.get())); - dirty = false; - -#if defined(DEBUG) - // platform::showDebugImage("Glyph Atlas", data, width, height); -#endif - } }; diff --git a/src/mbgl/geometry/glyph_atlas.hpp b/src/mbgl/geometry/glyph_atlas.hpp index a25c735a8..dfa568f0f 100644 --- a/src/mbgl/geometry/glyph_atlas.hpp +++ b/src/mbgl/geometry/glyph_atlas.hpp @@ -24,8 +24,13 @@ public: GlyphPositions&); void removeGlyphs(uintptr_t tileUID); + // Binds the atlas texture to the GPU, and uploads data if it is out of date. void bind(); + // Uploads the texture to the GPU to be available when we need it. This is a lazy operation; + // the texture is only bound when the data is out of date (=dirty). + void upload(); + const uint16_t width = 0; const uint16_t height = 0; diff --git a/src/mbgl/geometry/line_atlas.cpp b/src/mbgl/geometry/line_atlas.cpp index f64989d66..42d238077 100644 --- a/src/mbgl/geometry/line_atlas.cpp +++ b/src/mbgl/geometry/line_atlas.cpp @@ -129,6 +129,12 @@ LinePatternPos LineAtlas::addDash(const std::vector &dasharray, bool roun return position; }; +void LineAtlas::upload() { + if (dirty) { + bind(); + } +} + void LineAtlas::bind() { std::lock_guard lock(mtx); @@ -147,7 +153,7 @@ void LineAtlas::bind() { if (dirty) { if (first) { - glTexImage2D( + MBGL_CHECK_ERROR(glTexImage2D( GL_TEXTURE_2D, // GLenum target 0, // GLint level GL_ALPHA, // GLint internalformat @@ -157,9 +163,9 @@ void LineAtlas::bind() { GL_ALPHA, // GLenum format GL_UNSIGNED_BYTE, // GLenum type data // const GLvoid * data - ); + )); } else { - glTexSubImage2D( + MBGL_CHECK_ERROR(glTexSubImage2D( GL_TEXTURE_2D, // GLenum target 0, // GLint level 0, // GLint xoffset @@ -169,7 +175,7 @@ void LineAtlas::bind() { GL_ALPHA, // GLenum format GL_UNSIGNED_BYTE, // GLenum type data // const GLvoid *pixels - ); + )); } diff --git a/src/mbgl/geometry/line_atlas.hpp b/src/mbgl/geometry/line_atlas.hpp index df60a2dec..3683272f9 100644 --- a/src/mbgl/geometry/line_atlas.hpp +++ b/src/mbgl/geometry/line_atlas.hpp @@ -19,8 +19,13 @@ public: LineAtlas(uint16_t width, uint16_t height); ~LineAtlas(); + // Binds the atlas texture to the GPU, and uploads data if it is out of date. void bind(); + // Uploads the texture to the GPU to be available when we need it. This is a lazy operation; + // the texture is only bound when the data is out of date (=dirty). + void upload(); + LinePatternPos getDashPosition(const std::vector&, bool); LinePatternPos addDash(const std::vector &dasharray, bool round); diff --git a/src/mbgl/geometry/sprite_atlas.cpp b/src/mbgl/geometry/sprite_atlas.cpp index c2686e2f3..9d0aeac8b 100644 --- a/src/mbgl/geometry/sprite_atlas.cpp +++ b/src/mbgl/geometry/sprite_atlas.cpp @@ -58,6 +58,7 @@ bool SpriteAtlas::resize(const float newRatio) { ::operator delete(old_data); dirty = true; + fullUploadRequired = true; // Mark all sprite images as in need of update for (const auto &pair : images) { @@ -230,8 +231,13 @@ void SpriteAtlas::setSprite(util::ptr sprite_) { }); } +void SpriteAtlas::upload() { + if (dirty) { + bind(); + } +} + void SpriteAtlas::bind(bool linear) { - bool first = false; if (!texture) { MBGL_CHECK_ERROR(glGenTextures(1, &texture)); MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture)); @@ -242,7 +248,7 @@ void SpriteAtlas::bind(bool linear) { // We use those when the pixelRatio isn't a power of two, e.g. on iPhone 6 Plus. MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)); - first = true; + fullUploadRequired = true; } else { MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture)); } @@ -258,7 +264,7 @@ void SpriteAtlas::bind(bool linear) { std::lock_guard lock(mtx); allocate(); - if (first) { + if (fullUploadRequired) { MBGL_CHECK_ERROR(glTexImage2D( GL_TEXTURE_2D, // GLenum target 0, // GLint level @@ -270,6 +276,7 @@ void SpriteAtlas::bind(bool linear) { GL_UNSIGNED_BYTE, // GLenum type data // const GLvoid * data )); + fullUploadRequired = false; } else { MBGL_CHECK_ERROR(glTexSubImage2D( GL_TEXTURE_2D, // GLenum target diff --git a/src/mbgl/geometry/sprite_atlas.hpp b/src/mbgl/geometry/sprite_atlas.hpp index 079c15cef..6c4a381aa 100644 --- a/src/mbgl/geometry/sprite_atlas.hpp +++ b/src/mbgl/geometry/sprite_atlas.hpp @@ -50,10 +50,13 @@ public: SpriteAtlasPosition getPosition(const std::string& name, bool repeating = false); - // Binds the image buffer of this sprite atlas to the GPU, and uploads data if it is out - // of date. + // Binds the atlas texture to the GPU, and uploads data if it is out of date. void bind(bool linear = false); + // Uploads the texture to the GPU to be available when we need it. This is a lazy operation; + // the texture is only bound when the data is out of date (=dirty). + void upload(); + inline float getWidth() const { return width; } inline float getHeight() const { return height; } inline float getTextureWidth() const { return width * pixelRatio; } @@ -76,6 +79,7 @@ private: std::set uninitialized; uint32_t *data = nullptr; std::atomic dirty; + bool fullUploadRequired = true; uint32_t texture = 0; uint32_t filter = 0; static const int buffer = 1; diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index be2355206..0464ed3f1 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -11,7 +11,7 @@ namespace mbgl { Map::Map(View& view, FileSource& fileSource, MapMode mode, bool startPaused) : data(util::make_unique(view, mode)), - context(util::make_unique>("Map", view, fileSource, *data, startPaused)) + context(util::make_unique>("Map", util::ThreadPriority::Regular, view, fileSource, *data, startPaused)) { view.initialize(this); } diff --git a/src/mbgl/map/raster_tile_data.cpp b/src/mbgl/map/raster_tile_data.cpp index 77ea8db89..0b849ca7c 100644 --- a/src/mbgl/map/raster_tile_data.cpp +++ b/src/mbgl/map/raster_tile_data.cpp @@ -23,10 +23,6 @@ void RasterTileData::parse() { } } -void RasterTileData::render(Painter &painter, const StyleLayer &layer_desc, const mat4 &matrix) { - bucket.render(painter, layer_desc, id, matrix); -} - -bool RasterTileData::hasData(StyleLayer const& /*layer_desc*/) const { - return bucket.hasData(); +Bucket* RasterTileData::getBucket(StyleLayer const&) { + return &bucket; } diff --git a/src/mbgl/map/raster_tile_data.hpp b/src/mbgl/map/raster_tile_data.hpp index dab2b7b84..45aee8f74 100644 --- a/src/mbgl/map/raster_tile_data.hpp +++ b/src/mbgl/map/raster_tile_data.hpp @@ -20,8 +20,7 @@ public: ~RasterTileData(); void parse() override; - void render(Painter &painter, const StyleLayer &layer_desc, const mat4 &matrix) override; - bool hasData(StyleLayer const &layer_desc) const override; + Bucket* getBucket(StyleLayer const &layer_desc) override; protected: StyleLayoutRaster layout; diff --git a/src/mbgl/map/source.cpp b/src/mbgl/map/source.cpp index 12684bb6c..708ff6b27 100644 --- a/src/mbgl/map/source.cpp +++ b/src/mbgl/map/source.cpp @@ -184,16 +184,6 @@ void Source::drawClippingMasks(Painter &painter) { } } -void Source::render(Painter &painter, const StyleLayer &layer_desc) { - gl::group group(std::string { "layer: " } + layer_desc.id); - for (const auto& pair : tiles) { - Tile &tile = *pair.second; - if (tile.data && tile.data->state == TileData::State::parsed) { - painter.renderTileLayer(tile, layer_desc, tile.matrix); - } - } -} - void Source::finishRender(Painter &painter) { for (const auto& pair : tiles) { Tile &tile = *pair.second; @@ -212,6 +202,9 @@ std::forward_list Source::getLoadedTiles() const { return ptrs; } +const std::vector& Source::getTiles() const { + return tilePtrs; +} TileData::State Source::hasTile(const TileID& id) { auto it = tiles.find(id); @@ -455,6 +448,8 @@ void Source::update(MapData& data, } }); + updateTilePtrs(); + updated = data.getAnimationTime(); } @@ -464,6 +459,14 @@ void Source::invalidateTiles(const std::vector& ids) { tiles.erase(id); tile_data.erase(id); } + updateTilePtrs(); +} + +void Source::updateTilePtrs() { + tilePtrs.clear(); + for (const auto& pair : tiles) { + tilePtrs.push_back(pair.second.get()); + } } void Source::setCacheSize(size_t size) { diff --git a/src/mbgl/map/source.hpp b/src/mbgl/map/source.hpp index f7b675d16..b15de0e64 100644 --- a/src/mbgl/map/source.hpp +++ b/src/mbgl/map/source.hpp @@ -29,7 +29,6 @@ class Sprite; class TexturePool; class Style; class Painter; -class StyleLayer; class TransformState; class Tile; struct ClipID; @@ -76,10 +75,10 @@ public: void updateMatrices(const mat4 &projMatrix, const TransformState &transform); void drawClippingMasks(Painter &painter); - void render(Painter &painter, const StyleLayer &layer_desc); void finishRender(Painter &painter); std::forward_list getLoadedTiles() const; + const std::vector& getTiles() const; void setCacheSize(size_t); void onLowMemory(); @@ -105,6 +104,7 @@ private: std::function callback); TileData::State hasTile(const TileID& id); + void updateTilePtrs(); double getZoom(const TransformState &state) const; @@ -114,6 +114,7 @@ private: TimePoint updated = TimePoint::min(); std::map> tiles; + std::vector tilePtrs; std::map> tile_data; TileCache cache; }; diff --git a/src/mbgl/map/tile_data.hpp b/src/mbgl/map/tile_data.hpp index 92d9778fd..b85db7db7 100644 --- a/src/mbgl/map/tile_data.hpp +++ b/src/mbgl/map/tile_data.hpp @@ -47,8 +47,7 @@ public: // Override this in the child class. virtual void parse() = 0; - virtual void render(Painter &painter, const StyleLayer &layer_desc, const mat4 &matrix) = 0; - virtual bool hasData(StyleLayer const &layer_desc) const = 0; + virtual Bucket* getBucket(StyleLayer const &layer_desc) = 0; const TileID id; const std::string name; diff --git a/src/mbgl/map/tile_parser.cpp b/src/mbgl/map/tile_parser.cpp index 35c504ab2..1438bcdda 100644 --- a/src/mbgl/map/tile_parser.cpp +++ b/src/mbgl/map/tile_parser.cpp @@ -157,7 +157,7 @@ std::unique_ptr TileParser::createFillBucket(const GeometryTileLayer& la tile.triangleElementsBuffer, tile.lineElementsBuffer); addBucketGeometries(bucket, layer, bucket_desc.filter); - return std::move(bucket); + return bucket->hasData() ? std::move(bucket) : nullptr; } std::unique_ptr TileParser::createLineBucket(const GeometryTileLayer& layer, @@ -174,7 +174,7 @@ std::unique_ptr TileParser::createLineBucket(const GeometryTileLayer& la applyLayoutProperty(PropertyKey::LineRoundLimit, bucket_desc.layout, layout.round_limit, z); addBucketGeometries(bucket, layer, bucket_desc.filter); - return std::move(bucket); + return bucket->hasData() ? std::move(bucket) : nullptr; } std::unique_ptr TileParser::createSymbolBucket(const GeometryTileLayer& layer, @@ -224,6 +224,6 @@ std::unique_ptr TileParser::createSymbolBucket(const GeometryTileLayer& bucket->addFeatures( layer, bucket_desc.filter, reinterpret_cast(&tile), spriteAtlas, *sprite, glyphAtlas, glyphStore); - return std::move(bucket); + return bucket->hasData() ? std::move(bucket) : nullptr; } } diff --git a/src/mbgl/map/vector_tile_data.cpp b/src/mbgl/map/vector_tile_data.cpp index 555e1851f..4c6c1150b 100644 --- a/src/mbgl/map/vector_tile_data.cpp +++ b/src/mbgl/map/vector_tile_data.cpp @@ -55,23 +55,13 @@ void VectorTileData::parse() { } } -void VectorTileData::render(Painter &painter, const StyleLayer &layer_desc, const mat4 &matrix) { - if (state == State::parsed && layer_desc.bucket) { - auto databucket_it = buckets.find(layer_desc.bucket->name); - if (databucket_it != buckets.end()) { - assert(databucket_it->second); - databucket_it->second->render(painter, layer_desc, id, matrix); +Bucket* VectorTileData::getBucket(StyleLayer const& layer) { + if (state == State::parsed && layer.bucket) { + const auto it = buckets.find(layer.bucket->name); + if (it != buckets.end()) { + assert(it->second); + return it->second.get(); } } -} - -bool VectorTileData::hasData(const StyleLayer &layer_desc) const { - if (state == State::parsed && layer_desc.bucket) { - auto databucket_it = buckets.find(layer_desc.bucket->name); - if (databucket_it != buckets.end()) { - assert(databucket_it->second); - return databucket_it->second->hasData(); - } - } - return false; + return nullptr; } diff --git a/src/mbgl/map/vector_tile_data.hpp b/src/mbgl/map/vector_tile_data.hpp index 935ff9087..10eaf9c18 100644 --- a/src/mbgl/map/vector_tile_data.hpp +++ b/src/mbgl/map/vector_tile_data.hpp @@ -40,8 +40,7 @@ public: ~VectorTileData(); void parse() override; - void render(Painter &painter, const StyleLayer &layer_desc, const mat4 &matrix) override; - bool hasData(StyleLayer const& layer_desc) const override; + virtual Bucket* getBucket(StyleLayer const &layer_desc) override; protected: // Holds the actual geometries in this tile. diff --git a/src/mbgl/renderer/bucket.hpp b/src/mbgl/renderer/bucket.hpp index a7b0f61a3..711fc4238 100644 --- a/src/mbgl/renderer/bucket.hpp +++ b/src/mbgl/renderer/bucket.hpp @@ -1,6 +1,7 @@ #ifndef MBGL_RENDERER_BUCKET #define MBGL_RENDERER_BUCKET +#include #include #include @@ -12,10 +13,23 @@ class TileID; class Bucket : private util::noncopyable { public: + // As long as this bucket has a Prepare render pass, this function is getting called. Typically, + // this only happens once when the bucket is being rendered for the first time. + virtual void upload() = 0; + + // Every time this bucket is getting rendered, this function is called. This happens either + // once or twice (for Opaque and Transparent render passes). virtual void render(Painter&, const StyleLayer&, const TileID&, const mat4&) = 0; - virtual bool hasData() const = 0; + virtual ~Bucket() {} + inline bool needsUpload() const { + return !uploaded; + } + +protected: + bool uploaded = false; + }; } diff --git a/src/mbgl/renderer/debug_bucket.cpp b/src/mbgl/renderer/debug_bucket.cpp index 0c5fc63a2..a315d089e 100644 --- a/src/mbgl/renderer/debug_bucket.cpp +++ b/src/mbgl/renderer/debug_bucket.cpp @@ -16,13 +16,14 @@ DebugBucket::DebugBucket(DebugFontBuffer& fontBuffer_) : fontBuffer(fontBuffer_) { } -void DebugBucket::render(Painter &painter, const StyleLayer & /*layer_desc*/, - const TileID & /*id*/, const mat4 &matrix) { - painter.renderDebugText(*this, matrix); +void DebugBucket::upload() { + fontBuffer.upload(); + + uploaded = true; } -bool DebugBucket::hasData() const { - return fontBuffer.index() > 0; +void DebugBucket::render(Painter& painter, const StyleLayer&, const TileID&, const mat4& matrix) { + painter.renderDebugText(*this, matrix); } void DebugBucket::drawLines(PlainShader& shader) { diff --git a/src/mbgl/renderer/debug_bucket.hpp b/src/mbgl/renderer/debug_bucket.hpp index 9f20051e4..e3c01bbdd 100644 --- a/src/mbgl/renderer/debug_bucket.hpp +++ b/src/mbgl/renderer/debug_bucket.hpp @@ -15,9 +15,8 @@ class DebugBucket : public Bucket { public: DebugBucket(DebugFontBuffer& fontBuffer); - void render(Painter &painter, const StyleLayer &layer_desc, const TileID &id, - const mat4 &matrix) override; - bool hasData() const override; + void upload() override; + void render(Painter&, const StyleLayer&, const TileID&, const mat4&) override; void drawLines(PlainShader& shader); void drawPoints(PlainShader& shader); diff --git a/src/mbgl/renderer/fill_bucket.cpp b/src/mbgl/renderer/fill_bucket.cpp index 77b7b1188..c59b0970e 100644 --- a/src/mbgl/renderer/fill_bucket.cpp +++ b/src/mbgl/renderer/fill_bucket.cpp @@ -200,8 +200,19 @@ void FillBucket::tessellate() { lineGroup.vertex_length += total_vertex_count; } -void FillBucket::render(Painter &painter, const StyleLayer &layer_desc, const TileID &id, - const mat4 &matrix) { +void FillBucket::upload() { + vertexBuffer.upload(); + triangleElementsBuffer.upload(); + lineElementsBuffer.upload(); + + // From now on, we're going to render during the opaque and translucent pass. + uploaded = true; +} + +void FillBucket::render(Painter& painter, + const StyleLayer& layer_desc, + const TileID& id, + const mat4& matrix) { painter.renderFill(*this, layer_desc, id, matrix); } diff --git a/src/mbgl/renderer/fill_bucket.hpp b/src/mbgl/renderer/fill_bucket.hpp index 18eb5886c..2ff86ba9a 100644 --- a/src/mbgl/renderer/fill_bucket.hpp +++ b/src/mbgl/renderer/fill_bucket.hpp @@ -33,9 +33,9 @@ public: LineElementsBuffer &lineElementsBuffer); ~FillBucket() override; - void render(Painter &painter, const StyleLayer &layer_desc, const TileID &id, - const mat4 &matrix) override; - bool hasData() const override; + void upload() override; + void render(Painter&, const StyleLayer&, const TileID&, const mat4&) override; + bool hasData() const; void addGeometry(const GeometryCollection&); void tessellate(); diff --git a/src/mbgl/renderer/gl_config.cpp b/src/mbgl/renderer/gl_config.cpp new file mode 100644 index 000000000..ae648d4d3 --- /dev/null +++ b/src/mbgl/renderer/gl_config.cpp @@ -0,0 +1,19 @@ +#include "gl_config.hpp" + +namespace mbgl { +namespace gl { + +const ClearDepth::Type ClearDepth::Default = 0; +const ClearColor::Type ClearColor::Default = { 0, 0, 0, 0 }; +const ClearStencil::Type ClearStencil::Default = 0; +const StencilMask::Type StencilMask::Default = ~0u; +const DepthMask::Type DepthMask::Default = GL_TRUE; +const ColorMask::Type ColorMask::Default = { false, false, false, false }; +const StencilFunc::Type StencilFunc::Default = { GL_ALWAYS, 0, ~0u }; +const StencilTest::Type StencilTest::Default = false; +const DepthRange::Type DepthRange::Default = { 0, 1 }; +const DepthTest::Type DepthTest::Default = false; +const Blend::Type Blend::Default = false; + +} +} diff --git a/src/mbgl/renderer/gl_config.hpp b/src/mbgl/renderer/gl_config.hpp new file mode 100644 index 000000000..59507f721 --- /dev/null +++ b/src/mbgl/renderer/gl_config.hpp @@ -0,0 +1,149 @@ +#ifndef MBGL_RENDERER_GL_CONFIG +#define MBGL_RENDERER_GL_CONFIG + +#include +#include +#include + +#include + +namespace mbgl { +namespace gl { + +template +class Value { +public: + inline void operator=(const typename T::Type& value) { + if (current != value) { + current = value; + MBGL_CHECK_ERROR(T::Set(current)); + } + } + +private: + typename T::Type current = T::Default; +}; + +struct ClearDepth { + using Type = GLfloat; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(glClearDepth(value)); + } +}; + +struct ClearColor { + struct Type { float r, g, b, a; }; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(glClearColor(value.r, value.g, value.b, value.a)); + } +}; + +inline bool operator!=(const ClearColor::Type& a, const ClearColor::Type& b) { + return a.r != b.r || a.g != b.g || a.b != b.b || a.a != b.a; +} + +struct ClearStencil { + using Type = GLint; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(glClearStencil(value)); + } +}; + +struct StencilMask { + using Type = GLuint; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(glStencilMask(value)); + } +}; + +struct DepthMask { + using Type = GLboolean; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(glDepthMask(value)); + } +}; + +struct ColorMask { + struct Type { bool r, g, b, a; }; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(glColorMask(value.r, value.g, value.b, value.a)); + } +}; + +inline bool operator!=(const ColorMask::Type& a, const ColorMask::Type& b) { + return a.r != b.r || a.g != b.g || a.b != b.b || a.a != b.a; +} + +struct StencilFunc { + struct Type { GLenum func; GLint ref; GLuint mask; }; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(glStencilFunc(value.func, value.ref, value.mask)); + } +}; + +inline bool operator!=(const StencilFunc::Type& a, const StencilFunc::Type& b) { + return a.func != b.func || a.ref != b.ref || a.mask != b.mask; +} + +struct StencilTest { + using Type = bool; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(value ? glEnable(GL_STENCIL_TEST) : glDisable(GL_STENCIL_TEST)); + } +}; + +struct DepthRange { + struct Type { GLfloat near, far; }; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(glDepthRange(value.near, value.far)); + } +}; + +inline bool operator!=(const DepthRange::Type& a, const DepthRange::Type& b) { + return a.near != b.near || a.far != b.far; +} + +struct DepthTest { + using Type = bool; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(value ? glEnable(GL_DEPTH_TEST) : glDisable(GL_DEPTH_TEST)); + } +}; + +struct Blend { + using Type = bool; + static const Type Default; + inline static void Set(const Type& value) { + MBGL_CHECK_ERROR(value ? glEnable(GL_BLEND) : glDisable(GL_BLEND)); + } +}; + +class Config { +public: + Value stencilFunc; + Value stencilMask; + Value stencilTest; + Value depthRange; + Value depthMask; + Value depthTest; + Value blend; + Value colorMask; + Value clearDepth; + Value clearColor; + Value clearStencil; +}; + +} +} + +#endif diff --git a/src/mbgl/renderer/line_bucket.cpp b/src/mbgl/renderer/line_bucket.cpp index 97237901f..651b4986e 100644 --- a/src/mbgl/renderer/line_bucket.cpp +++ b/src/mbgl/renderer/line_bucket.cpp @@ -342,6 +342,14 @@ void LineBucket::addCurrentVertex(const Coordinate& currentVertex, e2 = e3; } +void LineBucket::upload() { + vertexBuffer.upload(); + triangleElementsBuffer.upload(); + + // From now on, we're only going to render during the translucent pass. + uploaded = true; +} + void LineBucket::render(Painter& painter, const StyleLayer& layer_desc, const TileID& id, diff --git a/src/mbgl/renderer/line_bucket.hpp b/src/mbgl/renderer/line_bucket.hpp index 64f353d7c..6f13e3c81 100644 --- a/src/mbgl/renderer/line_bucket.hpp +++ b/src/mbgl/renderer/line_bucket.hpp @@ -28,9 +28,9 @@ public: LineBucket(LineVertexBuffer &vertexBuffer, TriangleElementsBuffer &triangleElementsBuffer); ~LineBucket() override; - void render(Painter &painter, const StyleLayer &layer_desc, const TileID &id, - const mat4 &matrix) override; - bool hasData() const override; + void upload() override; + void render(Painter&, const StyleLayer&, const TileID&, const mat4&) override; + bool hasData() const; void addGeometry(const GeometryCollection&); void addGeometry(const std::vector& line); diff --git a/src/mbgl/renderer/painter.cpp b/src/mbgl/renderer/painter.cpp index 32d01c47f..a80fd4e8b 100644 --- a/src/mbgl/renderer/painter.cpp +++ b/src/mbgl/renderer/painter.cpp @@ -10,6 +10,8 @@ #include #include +#include +#include #include #include @@ -23,7 +25,6 @@ #include #include - #include #include #include @@ -87,16 +88,15 @@ void Painter::setup() { // We are blending new pixels on top of old pixels. Since we have depth testing // and are drawing opaque fragments first front-to-back, then translucent // fragments back-to-front, this shades the fewest fragments possible. - MBGL_CHECK_ERROR(glEnable(GL_BLEND)); + config.blend = true; MBGL_CHECK_ERROR(glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)); // Set clear values - MBGL_CHECK_ERROR(glClearColor(0.0f, 0.0f, 0.0f, 0.0f)); - MBGL_CHECK_ERROR(glClearDepth(1.0f)); - MBGL_CHECK_ERROR(glClearStencil(0x0)); + config.clearColor = { 0.0f, 0.0f, 0.0f, 0.0f }; + config.clearDepth = 1.0f; + config.clearStencil = 0x0; // Stencil test - MBGL_CHECK_ERROR(glEnable(GL_STENCIL_TEST)); MBGL_CHECK_ERROR(glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE)); // Depth test @@ -144,21 +144,6 @@ void Painter::lineWidth(float line_width) { } } -void Painter::depthMask(bool value) { - if (gl_depthMask != value) { - MBGL_CHECK_ERROR(glDepthMask(value ? GL_TRUE : GL_FALSE)); - gl_depthMask = value; - } -} - -void Painter::depthRange(const float near, const float far) { - if (gl_depthRange[0] != near || gl_depthRange[1] != far) { - MBGL_CHECK_ERROR(glDepthRange(near, far)); - gl_depthRange = {{ near, far }}; - } -} - - void Painter::changeMatrix() { // Initialize projection matrix matrix::ortho(projMatrix, 0, state.getWidth(), state.getHeight(), 0, 0, 1); @@ -176,24 +161,25 @@ void Painter::changeMatrix() { void Painter::clear() { gl::group group("clear"); - MBGL_CHECK_ERROR(glStencilMask(0xFF)); - depthMask(true); - - MBGL_CHECK_ERROR(glClearColor(0, 0, 0, 0)); + config.stencilTest = true; + config.stencilMask = 0xFF; + config.depthTest = false; + config.depthMask = GL_TRUE; + config.clearColor = { 0.0f, 0.0f, 0.0f, 0.0f }; MBGL_CHECK_ERROR(glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)); } void Painter::setOpaque() { if (pass != RenderPass::Opaque) { pass = RenderPass::Opaque; - MBGL_CHECK_ERROR(glDisable(GL_BLEND)); + config.blend = false; } } void Painter::setTranslucent() { if (pass != RenderPass::Translucent) { pass = RenderPass::Translucent; - MBGL_CHECK_ERROR(glEnable(GL_BLEND)); + config.blend = true; } } @@ -204,16 +190,12 @@ void Painter::setStrata(float value) { void Painter::prepareTile(const Tile& tile) { const GLint ref = (GLint)tile.clip.reference.to_ulong(); const GLuint mask = (GLuint)tile.clip.mask.to_ulong(); - MBGL_CHECK_ERROR(glStencilFunc(GL_EQUAL, ref, mask)); + config.stencilFunc = { GL_EQUAL, ref, mask }; } void Painter::render(const Style& style, TransformState state_, TimePoint time) { state = state_; - clear(); - resize(); - changeMatrix(); - std::set sources; for (const auto& source : style.sources) { if (source->enabled) { @@ -221,14 +203,46 @@ void Painter::render(const Style& style, TransformState state_, TimePoint time) } } - // Update all clipping IDs. - ClipIDGenerator generator; - for (const auto& source : sources) { - generator.update(source->getLoadedTiles()); - source->updateMatrices(projMatrix, state); + // Figure out what buckets we have to draw and what order we have to draw them in. + const auto order = determineRenderOrder(style); + + // - UPLOAD PASS ------------------------------------------------------------------------------- + // Uploads all required buffers and images before we do any actual rendering. + { + const gl::group upload("upload"); + + tileStencilBuffer.upload(); + tileBorderBuffer.upload(); + spriteAtlas.upload(); + lineAtlas.upload(); + glyphAtlas.upload(); + + for (const auto& item : order) { + if (item.bucket && item.bucket->needsUpload()) { + item.bucket->upload(); + } + } } - drawClippingMasks(sources); + + // - CLIPPING MASKS ---------------------------------------------------------------------------- + // Draws the clipping masks to the stencil buffer. + { + const gl::group clip("clip"); + + // Update all clipping IDs. + ClipIDGenerator generator; + for (const auto& source : sources) { + generator.update(source->getLoadedTiles()); + source->updateMatrices(projMatrix, state); + } + + clear(); + resize(); + changeMatrix(); + + drawClippingMasks(sources); + } frameHistory.record(time, state.getNormalizedZoom()); @@ -236,122 +250,169 @@ void Painter::render(const Style& style, TransformState state_, TimePoint time) if (debug::renderTree) { Log::Info(Event::Render, "{"); indent++; } // TODO: Correctly compute the number of layers recursively beforehand. - float strata_thickness = 1.0f / (style.layers.size() + 1); + const float strata_thickness = 1.0f / (order.size() + 1); - // - FIRST PASS ------------------------------------------------------------ - // Render everything top-to-bottom by using reverse iterators. Render opaque - // objects first. - - if (debug::renderTree) { - Log::Info(Event::Render, "%*s%s", indent++ * 4, "", "OPAQUE {"); - } + // Layer index int i = 0; - for (auto it = style.layers.rbegin(), end = style.layers.rend(); it != end; ++it, ++i) { + + // - OPAQUE PASS ------------------------------------------------------------------------------- + // Render everything top-to-bottom by using reverse iterators. Render opaque objects first. + { + const gl::group _("opaque"); + + if (debug::renderTree) { + Log::Info(Event::Render, "%*s%s", indent++ * 4, "", "OPAQUE {"); + } + i = 0; setOpaque(); - setStrata(i * strata_thickness); - renderLayer(**it); - } - if (debug::renderTree) { - Log::Info(Event::Render, "%*s%s", --indent * 4, "", "}"); + for (auto it = order.rbegin(), end = order.rend(); it != end; ++it, ++i) { + const auto& item = *it; + if (item.bucket && item.tile) { + if (item.hasRenderPass(RenderPass::Opaque)) { + const gl::group group(item.layer.id + " - " + std::string(item.tile->id)); + setStrata(i * strata_thickness); + prepareTile(*item.tile); + item.bucket->render(*this, item.layer, item.tile->id, item.tile->matrix); + } + } else { + const gl::group group("background"); + renderBackground(item.layer); + } + } + if (debug::renderTree) { + Log::Info(Event::Render, "%*s%s", --indent * 4, "", "}"); + } } - // - SECOND PASS ----------------------------------------------------------- - // Make a second pass, rendering translucent objects. This time, we render - // bottom-to-top. - if (debug::renderTree) { - Log::Info(Event::Render, "%*s%s", indent++ * 4, "", "TRANSLUCENT {"); - } - --i; - for (auto it = style.layers.begin(), end = style.layers.end(); it != end; ++it, --i) { + // - TRANSLUCENT PASS -------------------------------------------------------------------------- + // Make a second pass, rendering translucent objects. This time, we render bottom-to-top. + { + const gl::group _("translucent"); + + if (debug::renderTree) { + Log::Info(Event::Render, "%*s%s", indent++ * 4, "", "TRANSLUCENT {"); + } + --i; // After the last iteration, this is incremented, so we have to decrement it again. setTranslucent(); - setStrata(i * strata_thickness); - renderLayer(**it); - } - if (debug::renderTree) { - Log::Info(Event::Render, "%*s%s", --indent * 4, "", "}"); + for (auto it = order.begin(), end = order.end(); it != end; ++it, --i) { + const auto& item = *it; + if (item.bucket && item.tile) { + if (item.hasRenderPass(RenderPass::Translucent)) { + const gl::group group(item.layer.id + " - " + std::string(item.tile->id)); + setStrata(i * strata_thickness); + prepareTile(*item.tile); + item.bucket->render(*this, item.layer, item.tile->id, item.tile->matrix); + } + } + } + if (debug::renderTree) { + Log::Info(Event::Render, "%*s%s", --indent * 4, "", "}"); + } } if (debug::renderTree) { Log::Info(Event::Render, "}"); indent--; } - // Finalize the rendering, e.g. by calling debug render calls per tile. - // This guarantees that we have at least one function per tile called. - // When only rendering layers via the stylesheet, it's possible that we don't - // ever visit a tile during rendering. - for (const auto& source : sources) { - source->finishRender(*this); + // - DEBUG PASS -------------------------------------------------------------------------------- + // Renders debug overlays. + { + const gl::group _("debug"); + + // Finalize the rendering, e.g. by calling debug render calls per tile. + // This guarantees that we have at least one function per tile called. + // When only rendering layers via the stylesheet, it's possible that we don't + // ever visit a tile during rendering. + for (const auto& source : sources) { + source->finishRender(*this); + } + } + + // TODO: Find a better way to unbind VAOs after we're done with them without introducing + // unnecessary bind(0)/bind(N) sequences. + { + const gl::group _("cleanup"); + + MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, 0)); + MBGL_CHECK_ERROR(gl::BindVertexArray(0)); } } -void Painter::renderLayer(const StyleLayer &layer_desc) { - if (layer_desc.bucket->visibility == VisibilityType::None) return; - if (layer_desc.type == StyleLayerType::Background) { - // This layer defines a background color/image. +std::vector Painter::determineRenderOrder(const Style& style) { + std::vector order; - if (debug::renderTree) { - Log::Info(Event::Render, "%*s- %s (%s)", indent * 4, "", layer_desc.id.c_str(), - StyleLayerTypeClass(layer_desc.type).c_str()); + for (const auto& layerPtr : style.layers) { + const auto& layer = *layerPtr; + if (layer.bucket->visibility == VisibilityType::None) continue; + if (layer.type == StyleLayerType::Background) { + // This layer defines a background color/image. + order.emplace_back(layer); + continue; } - renderBackground(layer_desc); - } else { // This is a singular layer. - if (!layer_desc.bucket) { - Log::Warning(Event::Render, "layer '%s' is missing bucket", layer_desc.id.c_str()); - return; + if (!layer.bucket) { + Log::Warning(Event::Render, "layer '%s' is missing bucket", layer.id.c_str()); + continue; } - if (!layer_desc.bucket->source) { - Log::Warning(Event::Render, "can't find source for layer '%s'", layer_desc.id.c_str()); - return; + if (!layer.bucket->source) { + Log::Warning(Event::Render, "can't find source for layer '%s'", layer.id.c_str()); + continue; } // Skip this layer if it's outside the range of min/maxzoom. // This may occur when there /is/ a bucket created for this layer, but the min/max-zoom // is set to a fractional value, or value that is larger than the source maxzoom. const double zoom = state.getZoom(); - if (layer_desc.bucket->min_zoom > zoom || - layer_desc.bucket->max_zoom <= zoom) { - return; + if (layer.bucket->min_zoom > zoom || + layer.bucket->max_zoom <= zoom) { + continue; } - // Abort early if we can already deduce from the bucket type that - // we're not going to render anything anyway during this pass. - switch (layer_desc.type) { - case StyleLayerType::Fill: - if (!layer_desc.getProperties().isVisible()) return; - break; - case StyleLayerType::Line: - if (pass == RenderPass::Opaque) return; - if (!layer_desc.getProperties().isVisible()) return; - break; - case StyleLayerType::Symbol: - if (pass == RenderPass::Opaque) return; - if (!layer_desc.getProperties().isVisible()) return; - break; - case StyleLayerType::Raster: - if (pass == RenderPass::Opaque) return; - if (!layer_desc.getProperties().isVisible()) return; - break; - default: - break; + // Don't include invisible layers. + if (!layer.isVisible()) { + continue; } - if (debug::renderTree) { - Log::Info(Event::Render, "%*s- %s (%s)", indent * 4, "", layer_desc.id.c_str(), - StyleLayerTypeClass(layer_desc.type).c_str()); - } + // Determine what render passes we need for this layer. + const RenderPass passes = determineRenderPasses(layer); - layer_desc.bucket->source->render(*this, layer_desc); + const auto& tiles = layer.bucket->source->getTiles(); + for (auto tile : tiles) { + assert(tile); + if (!tile->data && !tile->data->ready()) { + continue; + } + + auto bucket = tile->data->getBucket(layer); + if (bucket) { + order.emplace_back(layer, tile, bucket, passes); + } + } } + + return order; } -void Painter::renderTileLayer(const Tile& tile, const StyleLayer &layer_desc, const mat4 &matrix) { - assert(tile.data); - if (tile.data->hasData(layer_desc) || layer_desc.type == StyleLayerType::Raster) { - gl::group group(std::string { "render " } + tile.data->name); - prepareTile(tile); - tile.data->render(*this, layer_desc, matrix); +RenderPass Painter::determineRenderPasses(const StyleLayer& layer) { + RenderPass passes = RenderPass::None; + + if (layer.properties.is()) { + const FillProperties &properties = layer.properties.get(); + const float alpha = properties.fill_color[3] * properties.opacity; + + if (properties.antialias) { + passes |= RenderPass::Translucent; + } + if (properties.image.from.size() || alpha < 1.0f) { + passes |= RenderPass::Translucent; + } else { + passes |= RenderPass::Opaque; + } + } else { + passes |= RenderPass::Translucent; } + + return passes; } void Painter::renderBackground(const StyleLayer &layer_desc) { @@ -428,10 +489,10 @@ void Painter::renderBackground(const StyleLayer &layer_desc) { backgroundArray.bind(*plainShader, backgroundBuffer, BUFFER_OFFSET(0)); } - MBGL_CHECK_ERROR(glDisable(GL_STENCIL_TEST)); - depthRange(strata + strata_epsilon, 1.0f); + config.stencilTest = false; + config.depthTest = true; + config.depthRange = { strata + strata_epsilon, 1.0f }; MBGL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, 4)); - MBGL_CHECK_ERROR(glEnable(GL_STENCIL_TEST)); } mat4 Painter::translatedMatrix(const mat4& matrix, const std::array &translation, const TileID &id, TranslateAnchorType anchor) { diff --git a/src/mbgl/renderer/painter.hpp b/src/mbgl/renderer/painter.hpp index e7e5557eb..54c23b393 100644 --- a/src/mbgl/renderer/painter.hpp +++ b/src/mbgl/renderer/painter.hpp @@ -4,10 +4,13 @@ #include #include +#include #include #include +#include + #include #include @@ -21,8 +24,6 @@ namespace mbgl { -enum class RenderPass : bool { Opaque, Translucent }; - class Style; class StyleLayer; class Tile; @@ -57,6 +58,24 @@ class GaussianShader; struct ClipID; +struct RenderItem { + inline RenderItem(const StyleLayer& layer_, + const Tile* tile_ = nullptr, + Bucket* bucket_ = nullptr, + RenderPass passes_ = RenderPass::Opaque) + : tile(tile_), bucket(bucket_), layer(layer_), passes(passes_) { + } + + const Tile* const tile; + Bucket* const bucket; + const StyleLayer& layer; + const RenderPass passes; + + inline bool hasRenderPass(RenderPass pass) const { + return bool(passes & pass); + } +}; + class Painter : private util::noncopyable { public: Painter(SpriteAtlas&, GlyphAtlas&, LineAtlas&); @@ -75,11 +94,6 @@ public: TransformState state, TimePoint time); - void renderLayer(const StyleLayer&); - - // Renders a particular layer from a tile. - void renderTileLayer(const Tile& tile, const StyleLayer &layer_desc, const mat4 &matrix); - // Renders debug information for a tile. void renderTileDebug(const Tile& tile); @@ -131,6 +145,9 @@ private: void setupShaders(); mat4 translatedMatrix(const mat4& matrix, const std::array &translation, const TileID &id, TranslateAnchorType anchor); + std::vector determineRenderOrder(const Style& style); + static RenderPass determineRenderPasses(const StyleLayer&); + void prepareTile(const Tile& tile); template @@ -147,8 +164,6 @@ private: public: void useProgram(uint32_t program); void lineWidth(float lineWidth); - void depthMask(bool value); - void depthRange(float near, float far); public: mat4 projMatrix; @@ -175,11 +190,11 @@ private: bool debug = false; int indent = 0; + gl::Config config; + uint32_t gl_program = 0; float gl_lineWidth = 0; - bool gl_depthMask = true; std::array gl_viewport = {{ 0, 0 }}; - std::array gl_depthRange = {{ 0, 1 }}; float strata = 0; RenderPass pass = RenderPass::Opaque; const float strata_epsilon = 1.0f / (1 << 16); diff --git a/src/mbgl/renderer/painter_clipping.cpp b/src/mbgl/renderer/painter_clipping.cpp index a8542b44c..c49535928 100644 --- a/src/mbgl/renderer/painter_clipping.cpp +++ b/src/mbgl/renderer/painter_clipping.cpp @@ -13,10 +13,11 @@ void Painter::drawClippingMasks(const std::set& sources) { gl::group group("clipping masks"); useProgram(plainShader->program); - MBGL_CHECK_ERROR(glDisable(GL_DEPTH_TEST)); - depthMask(false); - MBGL_CHECK_ERROR(glColorMask(false, false, false, false)); - depthRange(1.0f, 1.0f); + config.stencilTest = true; + config.depthTest = true; + config.depthMask = GL_FALSE; + config.colorMask = { false, false, false, false }; + config.depthRange = { 1.0f, 1.0f }; coveringPlainArray.bind(*plainShader, tileStencilBuffer, BUFFER_OFFSET(0)); @@ -24,10 +25,10 @@ void Painter::drawClippingMasks(const std::set& sources) { source->drawClippingMasks(*this); } - MBGL_CHECK_ERROR(glEnable(GL_DEPTH_TEST)); - MBGL_CHECK_ERROR(glColorMask(true, true, true, true)); - depthMask(true); - MBGL_CHECK_ERROR(glStencilMask(0x0)); + config.depthTest = true; + config.colorMask = { true, true, true, true }; + config.depthMask = GL_TRUE; + config.stencilMask = 0x0; } void Painter::drawClippingMask(const mat4& matrix, const ClipID &clip) { @@ -35,8 +36,7 @@ void Painter::drawClippingMask(const mat4& matrix, const ClipID &clip) { const GLint ref = (GLint)(clip.reference.to_ulong()); const GLuint mask = (GLuint)(clip.mask.to_ulong()); - MBGL_CHECK_ERROR(glStencilFunc(GL_ALWAYS, ref, mask)); - MBGL_CHECK_ERROR(glStencilMask(mask)); - + config.stencilFunc = { GL_ALWAYS, ref, mask }; + config.stencilMask = mask; MBGL_CHECK_ERROR(glDrawArrays(GL_TRIANGLES, 0, (GLsizei)tileStencilBuffer.index())); } diff --git a/src/mbgl/renderer/painter_debug.cpp b/src/mbgl/renderer/painter_debug.cpp index 457c1e5da..b252e2e83 100644 --- a/src/mbgl/renderer/painter_debug.cpp +++ b/src/mbgl/renderer/painter_debug.cpp @@ -24,7 +24,7 @@ void Painter::renderTileDebug(const Tile& tile) { void Painter::renderDebugText(DebugBucket& bucket, const mat4 &matrix) { gl::group group("debug text"); - MBGL_CHECK_ERROR(glDisable(GL_DEPTH_TEST)); + config.depthTest = false; useProgram(plainShader->program); plainShader->u_matrix = matrix; @@ -45,7 +45,7 @@ void Painter::renderDebugText(DebugBucket& bucket, const mat4 &matrix) { lineWidth(2.0f * state.getPixelRatio()); bucket.drawLines(*plainShader); - MBGL_CHECK_ERROR(glEnable(GL_DEPTH_TEST)); + config.depthTest = true; } void Painter::renderDebugFrame(const mat4 &matrix) { @@ -54,7 +54,8 @@ void Painter::renderDebugFrame(const mat4 &matrix) { // Disable depth test and don't count this towards the depth buffer, // but *don't* disable stencil test, as we want to clip the red tile border // to the tile viewport. - MBGL_CHECK_ERROR(glDisable(GL_DEPTH_TEST)); + config.depthTest = false; + config.stencilTest = true; useProgram(plainShader->program); plainShader->u_matrix = matrix; @@ -64,8 +65,6 @@ void Painter::renderDebugFrame(const mat4 &matrix) { plainShader->u_color = {{ 1.0f, 0.0f, 0.0f, 1.0f }}; lineWidth(4.0f * state.getPixelRatio()); MBGL_CHECK_ERROR(glDrawArrays(GL_LINE_STRIP, 0, (GLsizei)tileBorderBuffer.index())); - - MBGL_CHECK_ERROR(glEnable(GL_DEPTH_TEST)); } void Painter::renderDebugText(const std::vector &strings) { @@ -75,8 +74,9 @@ void Painter::renderDebugText(const std::vector &strings) { gl::group group("debug text"); - MBGL_CHECK_ERROR(glDisable(GL_DEPTH_TEST)); - MBGL_CHECK_ERROR(glStencilFunc(GL_ALWAYS, 0xFF, 0xFF)); + config.depthTest = false; + config.stencilTest = true; + config.stencilFunc = { GL_ALWAYS, 0xFF, 0xFF }; useProgram(plainShader->program); plainShader->u_matrix = nativeMatrix; @@ -103,6 +103,4 @@ void Painter::renderDebugText(const std::vector &strings) { lineWidth(2.0f * state.getPixelRatio()); MBGL_CHECK_ERROR(glDrawArrays(GL_LINES, 0, (GLsizei)debugFontBuffer.index())); } - - MBGL_CHECK_ERROR(glEnable(GL_DEPTH_TEST)); } diff --git a/src/mbgl/renderer/painter_fill.cpp b/src/mbgl/renderer/painter_fill.cpp index 459aa148a..6f6783f0e 100644 --- a/src/mbgl/renderer/painter_fill.cpp +++ b/src/mbgl/renderer/painter_fill.cpp @@ -15,9 +15,6 @@ using namespace mbgl; void Painter::renderFill(FillBucket& bucket, const StyleLayer &layer_desc, const TileID& id, const mat4 &matrix) { - // Abort early. - if (!bucket.hasData()) return; - const FillProperties &properties = layer_desc.getProperties(); mat4 vtxMatrix = translatedMatrix(matrix, properties.translate, id, properties.translateAnchor); @@ -42,6 +39,9 @@ void Painter::renderFill(FillBucket& bucket, const StyleLayer &layer_desc, const bool outline = properties.antialias && !pattern && stroke_color != fill_color; bool fringeline = properties.antialias && !pattern && stroke_color == fill_color; + config.stencilTest = true; + config.depthTest = true; + // Because we're drawing top-to-bottom, and we update the stencil mask // befrom, we have to draw the outline first (!) if (outline && pass == RenderPass::Translucent) { @@ -56,7 +56,7 @@ void Painter::renderFill(FillBucket& bucket, const StyleLayer &layer_desc, const static_cast(state.getFramebufferWidth()), static_cast(state.getFramebufferHeight()) }}; - depthRange(strata, 1.0f); + config.depthRange = { strata, 1.0f }; bucket.drawVertices(*outlineShader); } @@ -95,8 +95,8 @@ void Painter::renderFill(FillBucket& bucket, const StyleLayer &layer_desc, const spriteAtlas.bind(true); // Draw the actual triangles into the color & stencil buffer. - depthMask(true); - depthRange(strata, 1.0f); + config.depthMask = GL_TRUE; + config.depthRange = { strata, 1.0f }; bucket.drawElements(*patternShader); } } @@ -112,8 +112,8 @@ void Painter::renderFill(FillBucket& bucket, const StyleLayer &layer_desc, const plainShader->u_color = fill_color; // Draw the actual triangles into the color & stencil buffer. - depthMask(true); - depthRange(strata + strata_epsilon, 1.0f); + config.depthMask = GL_TRUE; + config.depthRange = { strata + strata_epsilon, 1.0f }; bucket.drawElements(*plainShader); } } @@ -133,7 +133,7 @@ void Painter::renderFill(FillBucket& bucket, const StyleLayer &layer_desc, const static_cast(state.getFramebufferHeight()) }}; - depthRange(strata + strata_epsilon + strata_epsilon, 1.0f); + config.depthRange = { strata + strata_epsilon + strata_epsilon, 1.0f }; bucket.drawVertices(*outlineShader); } } diff --git a/src/mbgl/renderer/painter_line.cpp b/src/mbgl/renderer/painter_line.cpp index 1d18b7ded..f552ea6c4 100644 --- a/src/mbgl/renderer/painter_line.cpp +++ b/src/mbgl/renderer/painter_line.cpp @@ -16,9 +16,10 @@ using namespace mbgl; void Painter::renderLine(LineBucket& bucket, const StyleLayer &layer_desc, const TileID& id, const mat4 &matrix) { // Abort early. if (pass == RenderPass::Opaque) return; - if (!bucket.hasData()) return; - depthMask(false); + config.stencilTest = true; + config.depthTest = true; + config.depthMask = GL_FALSE; const auto &properties = layer_desc.getProperties(); const auto &layout = bucket.layout; @@ -52,7 +53,7 @@ void Painter::renderLine(LineBucket& bucket, const StyleLayer &layer_desc, const float ratio = state.getPixelRatio(); mat4 vtxMatrix = translatedMatrix(matrix, properties.translate, id, properties.translateAnchor); - depthRange(strata, 1.0f); + config.depthRange = { strata, 1.0f }; if (properties.dash_array.from.size()) { @@ -110,7 +111,7 @@ void Painter::renderLine(LineBucket& bucket, const StyleLayer &layer_desc, const MBGL_CHECK_ERROR(glActiveTexture(GL_TEXTURE0)); spriteAtlas.bind(true); - MBGL_CHECK_ERROR(glDepthRange(strata + strata_epsilon, 1.0f)); // may or may not matter + config.depthRange = { strata + strata_epsilon, 1.0f }; // may or may not matter bucket.drawLinePatterns(*linepatternShader); diff --git a/src/mbgl/renderer/painter_raster.cpp b/src/mbgl/renderer/painter_raster.cpp index 513c613cb..61aff9c1a 100644 --- a/src/mbgl/renderer/painter_raster.cpp +++ b/src/mbgl/renderer/painter_raster.cpp @@ -23,8 +23,9 @@ void Painter::renderRaster(RasterBucket& bucket, const StyleLayer &layer_desc, c rasterShader->u_contrast_factor = contrastFactor(properties.contrast); rasterShader->u_spin_weights = spinWeights(properties.hue_rotate); - depthRange(strata + strata_epsilon, 1.0f); - + config.stencilTest = true; + config.depthTest = true; + config.depthRange = { strata + strata_epsilon, 1.0f }; bucket.drawRaster(*rasterShader, tileStencilBuffer, coveringRasterArray); } } diff --git a/src/mbgl/renderer/painter_symbol.cpp b/src/mbgl/renderer/painter_symbol.cpp index db3985c30..1841f2bc9 100644 --- a/src/mbgl/renderer/painter_symbol.cpp +++ b/src/mbgl/renderer/painter_symbol.cpp @@ -87,7 +87,7 @@ void Painter::renderSDF(SymbolBucket &bucket, sdfShader.u_buffer = (haloOffset - styleProperties.halo_width / fontScale) / sdfPx; - depthRange(strata, 1.0f); + config.depthRange = { strata, 1.0f }; (bucket.*drawSDF)(sdfShader); } @@ -108,7 +108,7 @@ void Painter::renderSDF(SymbolBucket &bucket, sdfShader.u_buffer = (256.0f - 64.0f) / 256.0f; - depthRange(strata + strata_epsilon, 1.0f); + config.depthRange = { strata + strata_epsilon, 1.0f }; (bucket.*drawSDF)(sdfShader); } } @@ -122,8 +122,9 @@ void Painter::renderSymbol(SymbolBucket &bucket, const StyleLayer &layer_desc, c const auto &properties = layer_desc.getProperties(); const auto &layout = bucket.layout; - MBGL_CHECK_ERROR(glDisable(GL_STENCIL_TEST)); - depthMask(false); + config.stencilTest = false; + config.depthTest = true; + config.depthMask = GL_FALSE; if (bucket.hasIconData()) { bool sdf = bucket.sdfIcons; @@ -185,7 +186,7 @@ void Painter::renderSymbol(SymbolBucket &bucket, const StyleLayer &layer_desc, c iconShader->u_fadezoom = state.getNormalizedZoom() * 10; iconShader->u_opacity = properties.icon.opacity; - depthRange(strata, 1.0f); + config.depthRange = { strata, 1.0f }; bucket.drawIcons(*iconShader); } } @@ -203,6 +204,4 @@ void Painter::renderSymbol(SymbolBucket &bucket, const StyleLayer &layer_desc, c *sdfGlyphShader, &SymbolBucket::drawGlyphs); } - - MBGL_CHECK_ERROR(glEnable(GL_STENCIL_TEST)); } diff --git a/src/mbgl/renderer/raster_bucket.cpp b/src/mbgl/renderer/raster_bucket.cpp index cf1036dbf..7ee64baaf 100644 --- a/src/mbgl/renderer/raster_bucket.cpp +++ b/src/mbgl/renderer/raster_bucket.cpp @@ -13,8 +13,17 @@ RasterBucket::RasterBucket(TexturePool& texturePool, const StyleLayoutRaster& la raster(texturePool) { } -void RasterBucket::render(Painter &painter, const StyleLayer &layer_desc, const TileID &id, - const mat4 &matrix) { +void RasterBucket::upload() { + if (hasData()) { + raster.upload(); + uploaded = true; + } +} + +void RasterBucket::render(Painter& painter, + const StyleLayer& layer_desc, + const TileID& id, + const mat4& matrix) { painter.renderRaster(*this, layer_desc, id, matrix); } @@ -29,13 +38,6 @@ void RasterBucket::drawRaster(RasterShader& shader, StaticVertexBuffer &vertices MBGL_CHECK_ERROR(glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertices.index())); } -void RasterBucket::drawRaster(RasterShader& shader, StaticVertexBuffer &vertices, VertexArrayObject &array, GLuint texture_) { - raster.bind(texture_); - shader.u_image = 0; - array.bind(shader, vertices, BUFFER_OFFSET(0)); - MBGL_CHECK_ERROR(glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertices.index())); -} - bool RasterBucket::hasData() const { return raster.isLoaded(); } diff --git a/src/mbgl/renderer/raster_bucket.hpp b/src/mbgl/renderer/raster_bucket.hpp index ed2d46bb2..70434b7d6 100644 --- a/src/mbgl/renderer/raster_bucket.hpp +++ b/src/mbgl/renderer/raster_bucket.hpp @@ -16,9 +16,9 @@ class RasterBucket : public Bucket { public: RasterBucket(TexturePool&, const StyleLayoutRaster&); - void render(Painter &painter, const StyleLayer &layer_desc, const TileID &id, - const mat4 &matrix) override; - bool hasData() const override; + void upload() override; + void render(Painter&, const StyleLayer&, const TileID&, const mat4&) override; + bool hasData() const; bool setImage(const std::string &data); @@ -26,8 +26,6 @@ public: void drawRaster(RasterShader& shader, StaticVertexBuffer &vertices, VertexArrayObject &array); - void drawRaster(RasterShader& shader, StaticVertexBuffer &vertices, VertexArrayObject &array, GLuint texture); - Raster raster; }; diff --git a/src/mbgl/renderer/render_pass.hpp b/src/mbgl/renderer/render_pass.hpp new file mode 100644 index 000000000..a298b8b57 --- /dev/null +++ b/src/mbgl/renderer/render_pass.hpp @@ -0,0 +1,31 @@ +#ifndef MBGL_RENDERER_RENDER_PASS +#define MBGL_RENDERER_RENDER_PASS + +#include +#include + +namespace mbgl { + +enum class RenderPass : uint8_t { + None = 0, + Opaque = 1 << 0, + Translucent = 1 << 1, +}; + +constexpr inline RenderPass operator|(RenderPass a, RenderPass b) { + return static_cast(static_cast::type>(a) | + static_cast::type>(b)); +} + +inline RenderPass operator|=(RenderPass& a, RenderPass b) { + return (a = a | b); +} + +constexpr inline RenderPass operator&(RenderPass a, RenderPass b) { + return static_cast(static_cast::type>(a) & + static_cast::type>(b)); +} + +} + +#endif diff --git a/src/mbgl/renderer/symbol_bucket.cpp b/src/mbgl/renderer/symbol_bucket.cpp index 6d6176196..769feb67a 100644 --- a/src/mbgl/renderer/symbol_bucket.cpp +++ b/src/mbgl/renderer/symbol_bucket.cpp @@ -36,8 +36,23 @@ SymbolBucket::~SymbolBucket() { // Do not remove. header file only contains forward definitions to unique pointers. } -void SymbolBucket::render(Painter &painter, const StyleLayer &layer_desc, const TileID &id, - const mat4 &matrix) { +void SymbolBucket::upload() { + if (hasTextData()) { + text.vertices.upload(); + text.triangles.upload(); + } + if (hasIconData()) { + icon.vertices.upload(); + icon.triangles.upload(); + } + + uploaded = true; +} + +void SymbolBucket::render(Painter& painter, + const StyleLayer& layer_desc, + const TileID& id, + const mat4& matrix) { painter.renderSymbol(*this, layer_desc, id, matrix); } diff --git a/src/mbgl/renderer/symbol_bucket.hpp b/src/mbgl/renderer/symbol_bucket.hpp index 9ddd653c5..b1dc44a11 100644 --- a/src/mbgl/renderer/symbol_bucket.hpp +++ b/src/mbgl/renderer/symbol_bucket.hpp @@ -55,9 +55,9 @@ public: SymbolBucket(Collision &collision); ~SymbolBucket() override; - void render(Painter &painter, const StyleLayer &layer_desc, const TileID &id, - const mat4 &matrix) override; - bool hasData() const override; + void upload() override; + void render(Painter&, const StyleLayer&, const TileID&, const mat4&) override; + bool hasData() const; bool hasTextData() const; bool hasIconData() const; diff --git a/src/mbgl/storage/default_file_source.cpp b/src/mbgl/storage/default_file_source.cpp index 1063b22ec..ddfcc8984 100644 --- a/src/mbgl/storage/default_file_source.cpp +++ b/src/mbgl/storage/default_file_source.cpp @@ -27,7 +27,7 @@ namespace algo = boost::algorithm; namespace mbgl { DefaultFileSource::DefaultFileSource(FileCache* cache, const std::string& root) - : thread(util::make_unique>("FileSource", cache, root)) { + : thread(util::make_unique>("FileSource", util::ThreadPriority::Low, cache, root)) { } DefaultFileSource::~DefaultFileSource() { diff --git a/src/mbgl/style/style_layer.cpp b/src/mbgl/style/style_layer.cpp index e9c301d8c..8713b73b1 100644 --- a/src/mbgl/style/style_layer.cpp +++ b/src/mbgl/style/style_layer.cpp @@ -13,6 +13,21 @@ bool StyleLayer::isBackground() const { return type == StyleLayerType::Background; } +bool StyleLayer::isVisible() const { + switch (type) { + case StyleLayerType::Fill: + return getProperties().isVisible(); + case StyleLayerType::Line: + return getProperties().isVisible(); + case StyleLayerType::Symbol: + return getProperties().isVisible(); + case StyleLayerType::Raster: + return getProperties().isVisible(); + default: + return false; + } +} + void StyleLayer::setClasses(const std::vector &class_names, const TimePoint now, const PropertyTransition &defaultTransition) { // Stores all keys that we have already added transitions for. diff --git a/src/mbgl/style/style_layer.hpp b/src/mbgl/style/style_layer.hpp index a00b7084f..774ed3901 100644 --- a/src/mbgl/style/style_layer.hpp +++ b/src/mbgl/style/style_layer.hpp @@ -35,6 +35,9 @@ public: // Determines whether this layer is the background layer. bool isBackground() const; + // Checks whether the layer is currently visible at all. + bool isVisible() const; + // Updates the StyleProperties information in this layer by evaluating all // pending transitions and applied classes in order. void updateProperties(float z, TimePoint now, ZoomHistory &zoomHistory); diff --git a/src/mbgl/util/raster.cpp b/src/mbgl/util/raster.cpp index d0e305c33..f2171a616 100644 --- a/src/mbgl/util/raster.cpp +++ b/src/mbgl/util/raster.cpp @@ -46,16 +46,7 @@ void Raster::bind(bool linear) { } if (img && !textured) { - texture = texturePool.getTextureID(); - MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture)); -#ifndef GL_ES_VERSION_2_0 - MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0)); -#endif - MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); - MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)); - MBGL_CHECK_ERROR(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img->getData())); - img.reset(); - textured = true; + upload(); } else if (textured) { MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture)); } @@ -68,24 +59,17 @@ void Raster::bind(bool linear) { } } -// overload ::bind for prerendered raster textures -void Raster::bind(const GLuint custom_texture) { +void Raster::upload() { if (img && !textured) { - MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, custom_texture)); + texture = texturePool.getTextureID(); + MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture)); +#ifndef GL_ES_VERSION_2_0 + MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0)); +#endif MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)); MBGL_CHECK_ERROR(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img->getData())); img.reset(); textured = true; - } else if (textured) { - MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, custom_texture)); } - - GLuint new_filter = GL_LINEAR; - if (new_filter != this->filter) { - MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, new_filter)); - MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, new_filter)); - filter = new_filter; - } - } diff --git a/src/mbgl/util/raster.hpp b/src/mbgl/util/raster.hpp index f19ff7178..1789ae87f 100644 --- a/src/mbgl/util/raster.hpp +++ b/src/mbgl/util/raster.hpp @@ -25,8 +25,8 @@ public: // bind current texture void bind(bool linear = false); - // bind prerendered texture - void bind(const GLuint texture); + // uploads the texture if it hasn't been uploaded yet. + void upload(); // loaded status bool isLoaded() const; diff --git a/src/mbgl/util/thread.hpp b/src/mbgl/util/thread.hpp index 55c3d4d28..e97872a50 100644 --- a/src/mbgl/util/thread.hpp +++ b/src/mbgl/util/thread.hpp @@ -7,6 +7,7 @@ #include #include +#include namespace { @@ -34,11 +35,16 @@ namespace util { // Thread<> constructor blocks until the thread and the Object are fully created, so after the // object creation, it's safe to obtain the Object stored in this thread. +enum class ThreadPriority : bool { + Regular, + Low, +}; + template class Thread { public: template - Thread(const std::string& name, Args&&... args); + Thread(const std::string& name, ThreadPriority priority, Args&&... args); ~Thread(); // Invoke object->fn(args...) in the runloop thread. @@ -97,7 +103,7 @@ private: template template -Thread::Thread(const std::string& name, Args&&... args) { +Thread::Thread(const std::string& name, ThreadPriority priority, Args&&... args) { // Note: We're using std::tuple<> to store the arguments because GCC 4.9 has a bug // when expanding parameters packs captured in lambdas. std::tuple params = std::forward_as_tuple(::std::forward(args)...); @@ -109,6 +115,10 @@ Thread::Thread(const std::string& name, Args&&... args) { (void(name)); #endif + if (priority == ThreadPriority::Low) { + platform::makeThreadLowPriority(); + } + constexpr auto seq = typename integer_sequence::type(); run(std::move(params), seq); }); diff --git a/src/mbgl/util/worker.cpp b/src/mbgl/util/worker.cpp index 1a6eaf4d9..9792f1a09 100644 --- a/src/mbgl/util/worker.cpp +++ b/src/mbgl/util/worker.cpp @@ -1,4 +1,5 @@ #include +#include #include @@ -15,7 +16,7 @@ public: Worker::Worker(std::size_t count) { for (std::size_t i = 0; i < count; i++) { - threads.emplace_back(util::make_unique>("Worker")); + threads.emplace_back(util::make_unique>("Worker", util::ThreadPriority::Low)); } }