diff --git a/CMakeLists.txt b/CMakeLists.txt index af1b37ed91..806f7dde6c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,9 @@ option(TANGRAM_MBTILES_DATASOURCE "Build MBTiles Datasource" ON) option(TANGRAM_BUILD_TESTS "Build unit tests" OFF) option(TANGRAM_BUNDLE_TESTS "Compile all tests into a single binary" ON) option(TANGRAM_BUILD_BENCHMARKS "Build benchmarks" OFF) + option(TANGRAM_DEV_MODE "For developers only: Don't omit the frame pointer" OFF) +option(TANGRAM_DEV_DEBUG_RENDERER "Build debug TextDisplay, RenderLayers and other things to toggle via DebugFlags" OFF) message(STATUS "Build type configuration: ${CMAKE_BUILD_TYPE}") @@ -75,6 +77,10 @@ if (TANGRAM_JSCORE_AVAILABLE AND TANGRAM_USE_JSCORE) set(TANGRAM_JSCORE_ENABLED ON) endif() +if (TANGRAM_OSX OR TANGRAM_LINUX) + set(TANGRAM_DEV_DEBUG_RENDERER ON) +endif() + # Add core library. add_subdirectory(core) diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index b13b90fe57..6d6992fe35 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -34,10 +34,6 @@ add_library(tangram-core src/data/formats/mvt.cpp src/data/formats/topoJson.h src/data/formats/topoJson.cpp - src/debug/frameInfo.h - src/debug/frameInfo.cpp - src/debug/textDisplay.h - src/debug/textDisplay.cpp src/gl/framebuffer.h src/gl/framebuffer.cpp src/gl/glError.h @@ -48,8 +44,6 @@ add_library(tangram-core src/gl/hardware.cpp src/gl/mesh.h src/gl/mesh.cpp - src/gl/primitives.h - src/gl/primitives.cpp src/gl/renderState.h src/gl/renderState.cpp src/gl/shaderProgram.h @@ -122,10 +116,6 @@ add_library(tangram-core src/selection/featureSelection.cpp src/selection/selectionQuery.h src/selection/selectionQuery.cpp - src/style/debugStyle.h - src/style/debugStyle.cpp - src/style/debugTextStyle.h - src/style/debugTextStyle.cpp src/style/material.h src/style/material.cpp src/style/pointStyle.h @@ -248,6 +238,22 @@ else() target_link_libraries(tangram-core PRIVATE duktape) endif() +if(TANGRAM_DEV_DEBUG_RENDERER) + target_compile_definitions(tangram-core PRIVATE TANGRAM_DEBUG_RENDERER=1) + target_sources(tangram-core + PRIVATE + src/debug/frameInfo.h + src/debug/frameInfo.cpp + src/debug/textDisplay.h + src/debug/textDisplay.cpp + src/gl/primitives.h + src/gl/primitives.cpp + src/style/debugStyle.h + src/style/debugStyle.cpp + src/style/debugTextStyle.h + src/style/debugTextStyle.cpp) +endif() + # Add MBTiles implementation. if(TANGRAM_MBTILES_DATASOURCE) target_sources(tangram-core PRIVATE src/data/mbtilesDataSource.cpp) diff --git a/core/src/debug/frameInfo.cpp b/core/src/debug/frameInfo.cpp index 00fc6d46b5..f12a88379e 100644 --- a/core/src/debug/frameInfo.cpp +++ b/core/src/debug/frameInfo.cpp @@ -18,6 +18,7 @@ #define DEBUG_STATS_MAX_SIZE 128 namespace Tangram { +namespace Debug { static float s_lastUpdateTime = 0.0; @@ -154,3 +155,4 @@ void FrameInfo::draw(RenderState& rs, const View& _view, const TileManager& _til } } +} diff --git a/core/src/debug/frameInfo.h b/core/src/debug/frameInfo.h index c2a8e92535..b694cda5af 100644 --- a/core/src/debug/frameInfo.h +++ b/core/src/debug/frameInfo.h @@ -6,14 +6,20 @@ class RenderState; class TileManager; class View; -struct FrameInfo { +namespace Debug { +struct FrameInfo { +#ifdef TANGRAM_DEBUG_RENDERER static void beginUpdate(); static void beginFrame(); - static void endUpdate(); - static void draw(RenderState& rs, const View& _view, const TileManager& _tileManager); +#else + static void beginUpdate(){} + static void beginFrame(){} + static void endUpdate(){} + static void draw(RenderState&, const View&, TileManager&){} +#endif }; - +} } diff --git a/core/src/debug/textDisplay.cpp b/core/src/debug/textDisplay.cpp index 5b3166ee30..9151be3b01 100644 --- a/core/src/debug/textDisplay.cpp +++ b/core/src/debug/textDisplay.cpp @@ -12,6 +12,7 @@ #include "stb_easy_font.h" namespace Tangram { +namespace Debug { TextDisplay::TextDisplay() : m_textDisplayResolution(350.0), m_initialized(false) { m_vertexBuffer = new char[VERTEX_BUFFER_SIZE]; @@ -142,7 +143,6 @@ void TextDisplay::draw(RenderState& rs, const std::vector& _infos) rs.culling(GL_TRUE); rs.vertexBuffer(boundbuffer); } - } - +} diff --git a/core/src/debug/textDisplay.h b/core/src/debug/textDisplay.h index 51787c0faf..dbfc4c3f2e 100644 --- a/core/src/debug/textDisplay.h +++ b/core/src/debug/textDisplay.h @@ -19,6 +19,8 @@ namespace Tangram { typedef int FontID; class RenderState; +namespace Debug { + template std::string to_string_with_precision(const T a_value, const int n = 6) { std::ostringstream out; @@ -36,8 +38,9 @@ class TextDisplay { return instance; } - ~TextDisplay(); +#ifdef TANGRAM_DEBUG_RENDERER + ~TextDisplay(); void setResolution(glm::vec2 _textDisplayResolution) { m_textDisplayResolution = _textDisplayResolution; } void init(); @@ -45,14 +48,11 @@ class TextDisplay { /* Draw stacked messages added through log and draw _infos string list */ void draw(RenderState& rs, const std::vector& _infos); - /* Stack the log message to be displayed in the screen log */ void log(const char* fmt, ...); private: - TextDisplay(); - void draw(RenderState& rs, const std::string& _text, int _posx, int _posy); glm::vec2 m_textDisplayResolution; @@ -64,10 +64,24 @@ class TextDisplay { UniformLocation m_uOrthoProj{"u_orthoProj"}; UniformLocation m_uColor{"u_color"}; - +#else + ~TextDisplay() {} + void setResolution(glm::vec2) {} + void init() {} + void deinit() {} + void draw(RenderState&, const std::vector&) {} + void log(const char* fmt, ...) {} +private: + TextDisplay(){} +#endif }; +} +#ifdef TANGRAM_DEBUG_RENDERER #define LOGS(fmt, ...) \ -do { Tangram::TextDisplay::Instance().log(fmt, ## __VA_ARGS__); } while(0) + do { Tangram::Debug::TextDisplay::Instance().log(fmt, ## __VA_ARGS__); } while(0) +#else +#define LOGS(...) +#endif } diff --git a/core/src/gl/framebuffer.cpp b/core/src/gl/framebuffer.cpp index a8886e198d..86eb8f8ccf 100644 --- a/core/src/gl/framebuffer.cpp +++ b/core/src/gl/framebuffer.cpp @@ -185,7 +185,7 @@ FrameBuffer::~FrameBuffer() { void FrameBuffer::drawDebug(RenderState& _rs, glm::vec2 _dim) { if (m_texture) { - Primitives::drawTexture(_rs, *m_texture, glm::vec2{}, _dim); + Debug::Primitives::drawTexture(_rs, *m_texture, glm::vec2{}, _dim); } } diff --git a/core/src/gl/primitives.cpp b/core/src/gl/primitives.cpp index 2524c25229..d150d62f10 100644 --- a/core/src/gl/primitives.cpp +++ b/core/src/gl/primitives.cpp @@ -16,8 +16,7 @@ #include "debugTexture_fs.h" namespace Tangram { - -namespace Primitives { +namespace Debug { static bool s_initialized; static std::unique_ptr s_shader; @@ -32,7 +31,7 @@ static std::unique_ptr s_textureLayout; static UniformLocation s_uTextureProj{"u_proj"}; -void init() { +void Primitives::init() { // lazy init if (!s_initialized) { @@ -60,7 +59,7 @@ void init() { } } -void deinit() { +void Primitives::deinit() { s_shader.reset(nullptr); s_layout.reset(nullptr); @@ -70,7 +69,7 @@ void deinit() { } -void drawLine(RenderState& rs, const glm::vec2& _origin, const glm::vec2& _destination) { +void Primitives::drawLine(RenderState& rs, const glm::vec2& _origin, const glm::vec2& _destination) { init(); @@ -94,14 +93,14 @@ void drawLine(RenderState& rs, const glm::vec2& _origin, const glm::vec2& _desti rs.vertexBuffer(boundBuffer); } -void drawRect(RenderState& rs, const glm::vec2& _origin, const glm::vec2& _destination) { +void Primitives::drawRect(RenderState& rs, const glm::vec2& _origin, const glm::vec2& _destination) { drawLine(rs, _origin, {_destination.x, _origin.y}); drawLine(rs, {_destination.x, _origin.y}, _destination); drawLine(rs, _destination, {_origin.x, _destination.y}); drawLine(rs, {_origin.x,_destination.y}, _origin); } -void drawPoly(RenderState& rs, const glm::vec2* _polygon, size_t _n) { +void Primitives::drawPoly(RenderState& rs, const glm::vec2* _polygon, size_t _n) { init(); if (!s_shader->use(rs)) { return; } @@ -119,7 +118,7 @@ void drawPoly(RenderState& rs, const glm::vec2* _polygon, size_t _n) { rs.vertexBuffer(boundBuffer); } -void drawTexture(RenderState& rs, Texture& _tex, glm::vec2 _pos, glm::vec2 _dim) { +void Primitives::drawTexture(RenderState& rs, Texture& _tex, glm::vec2 _pos, glm::vec2 _dim) { init(); if (!s_textureShader->use(rs)) { return; } @@ -156,7 +155,7 @@ void drawTexture(RenderState& rs, Texture& _tex, glm::vec2 _pos, glm::vec2 _dim) rs.vertexBuffer(boundBuffer); } -void setColor(RenderState& rs, unsigned int _color) { +void Primitives::setColor(RenderState& rs, unsigned int _color) { init(); float r = (_color >> 16 & 0xff) / 255.0; @@ -166,7 +165,7 @@ void setColor(RenderState& rs, unsigned int _color) { s_shader->setUniformf(rs, s_uColor, r, g, b); } -void setResolution(RenderState& rs, float _width, float _height) { +void Primitives::setResolution(RenderState& rs, float _width, float _height) { init(); glm::mat4 proj = glm::ortho(0.f, _width, _height, 0.f, -1.f, 1.f); @@ -175,5 +174,5 @@ void setResolution(RenderState& rs, float _width, float _height) { } } - } + diff --git a/core/src/gl/primitives.h b/core/src/gl/primitives.h index 1e3388e678..732dc90306 100644 --- a/core/src/gl/primitives.h +++ b/core/src/gl/primitives.h @@ -7,28 +7,39 @@ namespace Tangram { class RenderState; class Texture; -namespace Primitives { +namespace Debug { -void init(); -void deinit(); +struct Primitives { +#ifdef TANGRAM_DEBUG_RENDERER +static void init(); +static void deinit(); /* Setup the debug resolution size */ -void setResolution(RenderState& rs, float _width, float _height); +static void setResolution(RenderState& rs, float _width, float _height); /* Sets the current primitive color */ -void setColor(RenderState& rs, unsigned int _color); +static void setColor(RenderState& rs, unsigned int _color); /* Draws a line from _origin to _destination for the screen resolution _resolution */ -void drawLine(RenderState& rs, const glm::vec2& _origin, const glm::vec2& _destination); +static void drawLine(RenderState& rs, const glm::vec2& _origin, const glm::vec2& _destination); /* Draws a rect from _origin to _destination for the screen resolution _resolution */ -void drawRect(RenderState& rs, const glm::vec2& _origin, const glm::vec2& _destination); +static void drawRect(RenderState& rs, const glm::vec2& _origin, const glm::vec2& _destination); /* Draws a polyon of containing _n points in screen space for the screen resolution _resolution */ -void drawPoly(RenderState& rs, const glm::vec2* _polygon, size_t _n); - -void drawTexture(RenderState& rs, Texture& _tex, glm::vec2 _pos, glm::vec2 _dim); - +static void drawPoly(RenderState& rs, const glm::vec2* _polygon, size_t _n); + +static void drawTexture(RenderState& rs, Texture& _tex, glm::vec2 _pos, glm::vec2 _dim); +#else +static void init(){} +static void deinit(){} +static void setResolution(RenderState& rs, float _width, float _height){} +static void setColor(RenderState& rs, unsigned int _color){} +static void drawLine(RenderState& rs, const glm::vec2& _origin, const glm::vec2& _destination){} +static void drawRect(RenderState& rs, const glm::vec2& _origin, const glm::vec2& _destination){} +static void drawPoly(RenderState& rs, const glm::vec2* _polygon, size_t _n){} +static void drawTexture(RenderState& rs, Texture& _tex, glm::vec2 _pos, glm::vec2 _dim){} +#endif +}; } - } diff --git a/core/src/labels/labelManager.cpp b/core/src/labels/labelManager.cpp index e180554e49..3df9af3451 100644 --- a/core/src/labels/labelManager.cpp +++ b/core/src/labels/labelManager.cpp @@ -495,6 +495,8 @@ void LabelManager::updateLabelSet(const ViewState& _viewState, float _dt, const } void LabelManager::drawDebug(RenderState& rs, const View& _view) { +#ifdef TAGRAM_DEBUG_RENDERER + using Primitives = Debug::Primitives; if (!Tangram::getDebugFlag(Tangram::DebugFlags::labels)) { return; @@ -622,6 +624,7 @@ void LabelManager::drawDebug(RenderState& rs, const View& _view) { } } } +#endif // DEBUG_RENDERER } } diff --git a/core/src/map.cpp b/core/src/map.cpp index 8646b288f3..447bedf292 100644 --- a/core/src/map.cpp +++ b/core/src/map.cpp @@ -126,8 +126,8 @@ Map::~Map() { // All jobs will be executed immediately on add() afterwards. impl->jobQueue.stop(); - TextDisplay::Instance().deinit(); - Primitives::deinit(); + Debug::TextDisplay::Instance().deinit(); + Debug::Primitives::deinit(); } @@ -183,7 +183,7 @@ SceneID Map::Impl::loadSceneAsync(SceneOptions&& _sceneOptions) { // Disposing TileWorker is blocking: Do this async just in case asyncWorker->enqueue([s = std::move(oldScene)]() mutable { - LOG("ASYNC DISPOSE OF OLD SCENE - %d", s.use_count() == 1); + LOGD("Async dispose old Scene - %d", s.use_count() == 1); }); return scene->id; @@ -213,7 +213,7 @@ void Map::resize(int _newWidth, int _newHeight) { MapState Map::update(float _dt) { - FrameInfo::beginUpdate(); + Debug::FrameInfo::beginUpdate(); impl->jobQueue.runJobs(); @@ -250,7 +250,7 @@ MapState Map::update(float _dt) { } } - FrameInfo::endUpdate(); + Debug::FrameInfo::endUpdate(); return { state }; } @@ -281,8 +281,8 @@ void Map::render() { return; } - Primitives::setResolution(renderState, view.getWidth(), view.getHeight()); - FrameInfo::beginFrame(); + Debug::Primitives::setResolution(renderState, view.getWidth(), view.getHeight()); + Debug::FrameInfo::beginFrame(); scene.renderBeginFrame(renderState); @@ -309,7 +309,7 @@ void Map::render() { if (drawSelectionDebug) { impl->selectionBuffer->drawDebug(renderState, viewport); - FrameInfo::draw(renderState, view, *scene.tileManager()); + Debug::FrameInfo::draw(renderState, view, *scene.tileManager()); return; } @@ -321,7 +321,7 @@ void Map::render() { platform->setContinuousRendering(drawnAnimatedStyle); } - FrameInfo::draw(renderState, view, *scene.tileManager()); + Debug::FrameInfo::draw(renderState, view, *scene.tileManager()); } int Map::getViewportHeight() { @@ -884,7 +884,7 @@ void Map::setupGL() { } // Set default primitive render color - Primitives::setColor(impl->renderState, 0xffffff); + Debug::Primitives::setColor(impl->renderState, 0xffffff); // Load GL extensions and capabilities Hardware::loadExtensions(); diff --git a/core/src/scene/scene.cpp b/core/src/scene/scene.cpp index 8a824df223..c3505c63af 100644 --- a/core/src/scene/scene.cpp +++ b/core/src/scene/scene.cpp @@ -155,10 +155,12 @@ bool Scene::load() { m_styles = SceneLoader::applyStyles(m_config["styles"], m_textures, m_jsFunctions, m_stops, m_names); +#ifdef TAGRAM_DEBUG_RENDERER if (m_options.debugStyles) { m_styles.emplace_back(new DebugTextStyle("debugtext", true)); m_styles.emplace_back(new DebugStyle("debug")); } +#endif /// Styles that are opaque must be ordered first in the scene so that /// they are rendered 'under' styles that require blending std::sort(m_styles.begin(), m_styles.end(), Style::compare);