Browse Source

Exposed internal profiler callbacks.

Branimir Karadžić 8 years ago
parent
commit
4cdab3e971
6 changed files with 143 additions and 32 deletions
  1. 12 0
      examples/07-callback/callback.cpp
  2. 47 0
      include/bgfx/bgfx.h
  3. 3 0
      include/bgfx/c99/bgfx.h
  4. 55 16
      src/bgfx.cpp
  5. 23 5
      src/bgfx_p.h
  6. 3 11
      src/config.h

+ 12 - 0
examples/07-callback/callback.cpp

@@ -112,6 +112,18 @@ struct BgfxCallback : public bgfx::CallbackI
 		bx::debugPrintfVargs(_format, _argList);
 		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
 	virtual uint32_t cacheReadSize(uint64_t _id) override
 	{
 	{
 		char filePath[256];
 		char filePath[256];

+ 47 - 0
include/bgfx/bgfx.h

@@ -423,6 +423,53 @@ namespace bgfx
 			, va_list _argList
 			, va_list _argList
 			) = 0;
 			) = 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
 		/// Return size of for cached item. Return 0 if no cached item was
 		/// found.
 		/// found.
 		///
 		///

+ 3 - 0
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 (*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 (*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);
     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);
     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);
     void (*cache_write)(bgfx_callback_interface_t* _this, uint64_t _id, const void* _data, uint32_t _size);

+ 55 - 16
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
 		virtual void traceVargs(const char* _filePath, uint16_t _line, const char* _format, va_list _argList) override
 		{
 		{
 			char temp[2048];
 			char temp[2048];
@@ -73,18 +87,16 @@ namespace bgfx
 			bx::debugOutput(out);
 			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
 		virtual uint32_t cacheReadSize(uint64_t /*_id*/) override
@@ -1000,6 +1012,8 @@ namespace bgfx
 
 
 	void Frame::sort()
 	void Frame::sort()
 	{
 	{
+		BGFX_PROFILER_SCOPE("bgfx/Sort", 0xff2040ff);
+
 		uint8_t viewRemap[BGFX_CONFIG_MAX_VIEWS];
 		uint8_t viewRemap[BGFX_CONFIG_MAX_VIEWS];
 		for (uint32_t ii = 0; ii < BGFX_CONFIG_MAX_VIEWS; ++ii)
 		for (uint32_t ii = 0; ii < BGFX_CONFIG_MAX_VIEWS; ++ii)
 		{
 		{
@@ -1634,7 +1648,7 @@ namespace bgfx
 
 
 		m_submit->m_capture = _capture;
 		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
 		// wait for render thread to finish
 		renderSemWait();
 		renderSemWait();
 		frameNoRenderWait();
 		frameNoRenderWait();
@@ -1729,28 +1743,38 @@ namespace bgfx
 
 
 	RenderFrame::Enum Context::renderFrame(int32_t _msecs)
 	RenderFrame::Enum Context::renderFrame(int32_t _msecs)
 	{
 	{
-		BGFX_PROFILER_SCOPE(bgfx, render_frame, 0xff2040ff);
+		BGFX_PROFILER_SCOPE("bgfx::renderFrame", 0xff2040ff);
 
 
 		if (!m_flipAfterRender)
 		if (!m_flipAfterRender)
 		{
 		{
+			BGFX_PROFILER_SCOPE("bgfx/flip", 0xff2040ff);
 			flip();
 			flip();
 		}
 		}
 
 
 		if (apiSemWait(_msecs) )
 		if (apiSemWait(_msecs) )
 		{
 		{
-			rendererExecCommands(m_render->m_cmdPre);
+			{
+				BGFX_PROFILER_SCOPE("bgfx/Exec commands pre", 0xff2040ff);
+				rendererExecCommands(m_render->m_cmdPre);
+			}
+
 			if (m_rendererInitialized)
 			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_renderCtx->submit(m_render, m_clearQuad, m_textVideoMemBlitter);
 				m_flipped = false;
 				m_flipped = false;
 			}
 			}
-			rendererExecCommands(m_render->m_cmdPost);
+
+			{
+				BGFX_PROFILER_SCOPE("bgfx/Exec commands post", 0xff2040ff);
+				rendererExecCommands(m_render->m_cmdPost);
+			}
 
 
 			renderSemPost();
 			renderSemPost();
 
 
 			if (m_flipAfterRender)
 			if (m_flipAfterRender)
 			{
 			{
+				BGFX_PROFILER_SCOPE("bgfx/flip", 0xff2040ff);
 				flip();
 				flip();
 			}
 			}
 		}
 		}
@@ -4168,6 +4192,21 @@ namespace bgfx
 			m_interface->vtbl->trace_vargs(m_interface, _filePath, _line, _format, _argList);
 			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
 		virtual uint32_t cacheReadSize(uint64_t _id) override
 		{
 		{
 			return m_interface->vtbl->cache_read_size(m_interface, _id);
 			return m_interface->vtbl->cache_read_size(m_interface, _id);

+ 23 - 5
src/bgfx_p.h

@@ -44,9 +44,14 @@
 				, _handleAlloc.getMaxHandles() \
 				, _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_END() BX_NOOP()
 #	define BGFX_PROFILER_SET_CURRENT_THREAD_NAME(_name) BX_NOOP()
 #	define BGFX_PROFILER_SET_CURRENT_THREAD_NAME(_name) BX_NOOP()
 #endif // BGFX_PROFILER_SCOPE
 #endif // BGFX_PROFILER_SCOPE
@@ -364,6 +369,19 @@ namespace bgfx
 
 
 	typedef bx::StringT<&g_allocator> String;
 	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);
 	void setGraphicsDebuggerPresent(bool _present);
 	bool isGraphicsDebuggerPresent();
 	bool isGraphicsDebuggerPresent();
 	void release(const Memory* _mem);
 	void release(const Memory* _mem);
@@ -4337,7 +4355,7 @@ namespace bgfx
 				return true;
 				return true;
 			}
 			}
 
 
-			BGFX_PROFILER_SCOPE(bgfx, main_thread_wait, 0xff2040ff);
+			BGFX_PROFILER_SCOPE("bgfx/API thread wait", 0xff2040ff);
 			int64_t start = bx::getHPCounter();
 			int64_t start = bx::getHPCounter();
 			bool ok = m_apiSem.wait(_msecs);
 			bool ok = m_apiSem.wait(_msecs);
 			if (ok)
 			if (ok)
@@ -4362,7 +4380,7 @@ namespace bgfx
 		{
 		{
 			if (!m_singleThreaded)
 			if (!m_singleThreaded)
 			{
 			{
-				BGFX_PROFILER_SCOPE(bgfx, render_thread_wait, 0xff2040ff);
+				BGFX_PROFILER_SCOPE("bgfx/Render thread wait", 0xff2040ff);
 				int64_t start = bx::getHPCounter();
 				int64_t start = bx::getHPCounter();
 				bool ok = m_renderSem.wait();
 				bool ok = m_renderSem.wait();
 				BX_CHECK(ok, "Semaphore wait failed."); BX_UNUSED(ok);
 				BX_CHECK(ok, "Semaphore wait failed."); BX_UNUSED(ok);

+ 3 - 11
src/config.h

@@ -304,17 +304,9 @@
 
 
 #define BGFX_CONFIG_DRAW_INDIRECT_STRIDE 32
 #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
 #ifndef BGFX_CONFIG_RENDERDOC_LOG_FILEPATH
 #	define BGFX_CONFIG_RENDERDOC_LOG_FILEPATH "temp/bgfx"
 #	define BGFX_CONFIG_RENDERDOC_LOG_FILEPATH "temp/bgfx"