From 4cdab3e971ef69dfc87f53282ead66b0edd61e78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Fri, 6 Oct 2017 18:07:40 -0700 Subject: [PATCH] Exposed internal profiler callbacks. --- examples/07-callback/callback.cpp | 12 ++++++ include/bgfx/bgfx.h | 47 ++++++++++++++++++++ include/bgfx/c99/bgfx.h | 3 ++ src/bgfx.cpp | 71 ++++++++++++++++++++++++------- src/bgfx_p.h | 28 +++++++++--- src/config.h | 14 ++---- 6 files changed, 143 insertions(+), 32 deletions(-) diff --git a/examples/07-callback/callback.cpp b/examples/07-callback/callback.cpp index 41445bd3a..6c92e2977 100644 --- a/examples/07-callback/callback.cpp +++ b/examples/07-callback/callback.cpp @@ -112,6 +112,18 @@ struct BgfxCallback : public bgfx::CallbackI bx::debugPrintfVargs(_format, _argList); } + virtual void profilerBegin(const char* /*_name*/, uint32_t /*_abgr*/, const char* /*_filePath*/, uint16_t /*_line*/) override + { + } + + virtual void profilerBeginLiteral(const char* /*_name*/, uint32_t /*_abgr*/, const char* /*_filePath*/, uint16_t /*_line*/) override + { + } + + virtual void profilerEnd() override + { + } + virtual uint32_t cacheReadSize(uint64_t _id) override { char filePath[256]; diff --git a/include/bgfx/bgfx.h b/include/bgfx/bgfx.h index cb8a2d225..a39ddd4cd 100644 --- a/include/bgfx/bgfx.h +++ b/include/bgfx/bgfx.h @@ -423,6 +423,53 @@ namespace bgfx , va_list _argList ) = 0; + /// Profiler region begin. + /// + /// @param[in] _name Region name, contains dynamic string. + /// @param[in] _abgr Color of profiler region. + /// @param[in] _filePath File path where profilerBegin was called. + /// @param[in] _line Line where profilerBegin was called. + /// + /// @remarks + /// Not thread safe and it can be called from any thread. + /// + /// @attention C99 equivalent is `bgfx_callback_vtbl.profiler_begin`. + /// + virtual void profilerBegin( + const char* _name + , uint32_t _abgr + , const char* _filePath + , uint16_t _line + ) = 0; + + /// Profiler region begin with string literal name. + /// + /// @param[in] _name Region name, contains string literal. + /// @param[in] _abgr Color of profiler region. + /// @param[in] _filePath File path where profilerBeginLiteral was called. + /// @param[in] _line Line where profilerBeginLiteral was called. + /// + /// @remarks + /// Not thread safe and it can be called from any thread. + /// + /// @attention C99 equivalent is `bgfx_callback_vtbl.profiler_begin_literal`. + /// + virtual void profilerBeginLiteral( + const char* _name + , uint32_t _abgr + , const char* _filePath + , uint16_t _line + ) = 0; + + /// Profiler region end. + /// + /// @remarks + /// Not thread safe and it can be called from any thread. + /// + /// @attention C99 equivalent is `bgfx_callback_vtbl.profiler_end`. + /// + virtual void profilerEnd() = 0; + /// Return size of for cached item. Return 0 if no cached item was /// found. /// diff --git a/include/bgfx/c99/bgfx.h b/include/bgfx/c99/bgfx.h index 48bf83f23..5139ad644 100644 --- a/include/bgfx/c99/bgfx.h +++ b/include/bgfx/c99/bgfx.h @@ -530,6 +530,9 @@ typedef struct bgfx_callback_vtbl { void (*fatal)(bgfx_callback_interface_t* _this, bgfx_fatal_t _code, const char* _str); void (*trace_vargs)(bgfx_callback_interface_t* _this, const char* _filePath, uint16_t _line, const char* _format, va_list _argList); + void (*profiler_begin)(bgfx_callback_interface_t* _this, const char* _name, uint32_t _abgr, const char* _filePath, uint16_t _line); + void (*profiler_begin_literal)(bgfx_callback_interface_t* _this, const char* _name, uint32_t _abgr, const char* _filePath, uint16_t _line); + void (*profiler_end)(bgfx_callback_interface_t* _this); uint32_t (*cache_read_size)(bgfx_callback_interface_t* _this, uint64_t _id); bool (*cache_read)(bgfx_callback_interface_t* _this, uint64_t _id, void* _data, uint32_t _size); void (*cache_write)(bgfx_callback_interface_t* _this, uint64_t _id, const void* _data, uint32_t _size); diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 39da57300..0b5b508fa 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -54,6 +54,20 @@ namespace bgfx { } + virtual void fatal(Fatal::Enum _code, const char* _str) override + { + if (Fatal::DebugCheck == _code) + { + bx::debugBreak(); + } + else + { + BX_TRACE("0x%08x: %s", _code, _str); + BX_UNUSED(_code, _str); + abort(); + } + } + virtual void traceVargs(const char* _filePath, uint16_t _line, const char* _format, va_list _argList) override { char temp[2048]; @@ -73,18 +87,16 @@ namespace bgfx bx::debugOutput(out); } - virtual void fatal(Fatal::Enum _code, const char* _str) override + virtual void profilerBegin(const char* /*_name*/, uint32_t /*_abgr*/, const char* /*_filePath*/, uint16_t /*_line*/) override + { + } + + virtual void profilerBeginLiteral(const char* /*_name*/, uint32_t /*_abgr*/, const char* /*_filePath*/, uint16_t /*_line*/) override + { + } + + virtual void profilerEnd() override { - if (Fatal::DebugCheck == _code) - { - bx::debugBreak(); - } - else - { - BX_TRACE("0x%08x: %s", _code, _str); - BX_UNUSED(_code, _str); - abort(); - } } virtual uint32_t cacheReadSize(uint64_t /*_id*/) override @@ -1000,6 +1012,8 @@ namespace bgfx void Frame::sort() { + BGFX_PROFILER_SCOPE("bgfx/Sort", 0xff2040ff); + uint8_t viewRemap[BGFX_CONFIG_MAX_VIEWS]; for (uint32_t ii = 0; ii < BGFX_CONFIG_MAX_VIEWS; ++ii) { @@ -1634,7 +1648,7 @@ namespace bgfx m_submit->m_capture = _capture; - BGFX_PROFILER_SCOPE(bgfx, main_thread_frame, 0xff2040ff); + BGFX_PROFILER_SCOPE("bgfx/API thread frame", 0xff2040ff); // wait for render thread to finish renderSemWait(); frameNoRenderWait(); @@ -1729,28 +1743,38 @@ namespace bgfx RenderFrame::Enum Context::renderFrame(int32_t _msecs) { - BGFX_PROFILER_SCOPE(bgfx, render_frame, 0xff2040ff); + BGFX_PROFILER_SCOPE("bgfx::renderFrame", 0xff2040ff); if (!m_flipAfterRender) { + BGFX_PROFILER_SCOPE("bgfx/flip", 0xff2040ff); flip(); } if (apiSemWait(_msecs) ) { - rendererExecCommands(m_render->m_cmdPre); + { + BGFX_PROFILER_SCOPE("bgfx/Exec commands pre", 0xff2040ff); + rendererExecCommands(m_render->m_cmdPre); + } + if (m_rendererInitialized) { - BGFX_PROFILER_SCOPE(bgfx, render_submit, 0xff2040ff); + BGFX_PROFILER_SCOPE("bgfx/Render submit", 0xff2040ff); m_renderCtx->submit(m_render, m_clearQuad, m_textVideoMemBlitter); m_flipped = false; } - rendererExecCommands(m_render->m_cmdPost); + + { + BGFX_PROFILER_SCOPE("bgfx/Exec commands post", 0xff2040ff); + rendererExecCommands(m_render->m_cmdPost); + } renderSemPost(); if (m_flipAfterRender) { + BGFX_PROFILER_SCOPE("bgfx/flip", 0xff2040ff); flip(); } } @@ -4168,6 +4192,21 @@ namespace bgfx m_interface->vtbl->trace_vargs(m_interface, _filePath, _line, _format, _argList); } + virtual void profilerBegin(const char* _name, uint32_t _abgr, const char* _filePath, uint16_t _line) override + { + m_interface->vtbl->profiler_begin(m_interface, _name, _abgr, _filePath, _line); + } + + virtual void profilerBeginLiteral(const char* _name, uint32_t _abgr, const char* _filePath, uint16_t _line) override + { + m_interface->vtbl->profiler_begin_literal(m_interface, _name, _abgr, _filePath, _line); + } + + virtual void profilerEnd() override + { + m_interface->vtbl->profiler_end(m_interface); + } + virtual uint32_t cacheReadSize(uint64_t _id) override { return m_interface->vtbl->cache_read_size(m_interface, _id); diff --git a/src/bgfx_p.h b/src/bgfx_p.h index 297191bff..79c2038e2 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -44,9 +44,14 @@ , _handleAlloc.getMaxHandles() \ ) -#ifndef BGFX_PROFILER_SCOPE -# define BGFX_PROFILER_SCOPE(_group, _name, _color) BX_NOOP() -# define BGFX_PROFILER_BEGIN(_group, _name, _color) BX_NOOP() +#if BGFX_CONFIG_PROFILER +# define BGFX_PROFILER_SCOPE(_name, _abgr) ProfilerScope BX_CONCATENATE(profilerScope, __LINE__)(_name, _abgr, __FILE__, uint16_t(__LINE__) ) +# define BGFX_PROFILER_BEGIN(_name, _abgr) g_callback->profilerBeginLiteral(_name, _abgr, __FILE__, uint16_t(__LINE__) ) +# define BGFX_PROFILER_END() g_callback->profilerEnd() +# define BGFX_PROFILER_SET_CURRENT_THREAD_NAME(_name) BX_NOOP() +#else +# define BGFX_PROFILER_SCOPE(_name, _abgr) BX_NOOP() +# define BGFX_PROFILER_BEGIN(_name, _abgr) BX_NOOP() # define BGFX_PROFILER_END() BX_NOOP() # define BGFX_PROFILER_SET_CURRENT_THREAD_NAME(_name) BX_NOOP() #endif // BGFX_PROFILER_SCOPE @@ -364,6 +369,19 @@ namespace bgfx typedef bx::StringT<&g_allocator> String; + struct ProfilerScope + { + ProfilerScope(const char* _name, uint32_t _abgr, const char* _filePath, uint16_t _line) + { + g_callback->profilerBeginLiteral(_name, _abgr, _filePath, _line); + } + + ~ProfilerScope() + { + g_callback->profilerEnd(); + } + }; + void setGraphicsDebuggerPresent(bool _present); bool isGraphicsDebuggerPresent(); void release(const Memory* _mem); @@ -4337,7 +4355,7 @@ namespace bgfx return true; } - BGFX_PROFILER_SCOPE(bgfx, main_thread_wait, 0xff2040ff); + BGFX_PROFILER_SCOPE("bgfx/API thread wait", 0xff2040ff); int64_t start = bx::getHPCounter(); bool ok = m_apiSem.wait(_msecs); if (ok) @@ -4362,7 +4380,7 @@ namespace bgfx { if (!m_singleThreaded) { - BGFX_PROFILER_SCOPE(bgfx, render_thread_wait, 0xff2040ff); + BGFX_PROFILER_SCOPE("bgfx/Render thread wait", 0xff2040ff); int64_t start = bx::getHPCounter(); bool ok = m_renderSem.wait(); BX_CHECK(ok, "Semaphore wait failed."); BX_UNUSED(ok); diff --git a/src/config.h b/src/config.h index bfb3f7eeb..5b01678e1 100644 --- a/src/config.h +++ b/src/config.h @@ -304,17 +304,9 @@ #define BGFX_CONFIG_DRAW_INDIRECT_STRIDE 32 -#ifndef BGFX_CONFIG_PROFILER_MICROPROFILE -# define BGFX_CONFIG_PROFILER_MICROPROFILE 0 -#endif // BGFX_CONFIG_PROFILER_MICROPROFILE - -#ifndef BGFX_CONFIG_PROFILER_REMOTERY -# define BGFX_CONFIG_PROFILER_REMOTERY 0 -#endif // BGFX_CONFIG_PROFILER_REMOTERY - -#ifndef BGFX_CONFIG_PROFILER_REMOTERY_BUILD_LIB -# define BGFX_CONFIG_PROFILER_REMOTERY_BUILD_LIB BGFX_CONFIG_PROFILER_REMOTERY -#endif // BGFX_CONFIG_PROFILER_REMOTERY_BUILD_LIB +#ifndef BGFX_CONFIG_PROFILER +# define BGFX_CONFIG_PROFILER 0 +#endif // BGFX_CONFIG_PROFILER #ifndef BGFX_CONFIG_RENDERDOC_LOG_FILEPATH # define BGFX_CONFIG_RENDERDOC_LOG_FILEPATH "temp/bgfx"