Преглед на файлове

Allow an external profiler to be installed dynamically (#1164)

This is required when JPH_EXTERNAL_PROFILE is defined while compiling Jolt as a dynamic linked library
Tanner Willis преди 1 година
родител
ревизия
e709a36bbc
променени са 2 файла, в които са добавени 27 реда и са изтрити 5 реда
  1. 9 4
      Jolt/Core/Profiler.cpp
  2. 18 1
      Jolt/Core/Profiler.h

+ 9 - 4
Jolt/Core/Profiler.cpp

@@ -13,10 +13,15 @@ JPH_SUPPRESS_WARNINGS_STD_BEGIN
 #include <fstream>
 JPH_SUPPRESS_WARNINGS_STD_END
 
-#ifdef JPH_PROFILE_ENABLED
-
 JPH_NAMESPACE_BEGIN
 
+#if defined(JPH_EXTERNAL_PROFILE) && defined(JPH_SHARED_LIBRARY)
+
+ProfileStartMeasurementFunction ProfileStartMeasurement = [](const char *, uint32, uint8 *) { };
+ProfileEndMeasurementFunction ProfileEndMeasurement = [](uint8 *) { };
+
+#elif defined(JPH_PROFILE_ENABLED)
+
 //////////////////////////////////////////////////////////////////////////////////////////
 // Profiler
 //////////////////////////////////////////////////////////////////////////////////////////
@@ -341,6 +346,6 @@ void Profiler::DumpChart(const char *inTag, const Threads &inThreads, const KeyT
 </tbody></table></body></html>)";
 }
 
-JPH_NAMESPACE_END
-
 #endif // JPH_PROFILE_ENABLED
+
+JPH_NAMESPACE_END

+ 18 - 1
Jolt/Core/Profiler.h

@@ -17,16 +17,31 @@ JPH_SUPPRESS_WARNINGS_STD_END
 
 JPH_NAMESPACE_BEGIN
 
+#ifdef JPH_SHARED_LIBRARY
+/// Functions called when a profiler measurement starts or stops, need to be overridden by the user.
+using ProfileStartMeasurementFunction = void (*)(const char *inName, uint32 inColor, uint8 *ioUserData);
+using ProfileEndMeasurementFunction = void (*)(uint8 *ioUserData);
+
+JPH_EXPORT extern ProfileStartMeasurementFunction ProfileStartMeasurement;
+JPH_EXPORT extern ProfileEndMeasurementFunction ProfileEndMeasurement;
+#endif // JPH_SHARED_LIBRARY
+
 /// Create this class on the stack to start sampling timing information of a particular scope.
 ///
-/// Left unimplemented intentionally. Needs to be implemented by the user of the library.
+/// For statically linked builds, this is left unimplemented intentionally. Needs to be implemented by the user of the library.
 /// On construction a measurement should start, on destruction it should be stopped.
+/// For dynamically linked builds, the user should override the ProfileStartMeasurement and ProfileEndMeasurement functions.
 class alignas(16) ExternalProfileMeasurement : public NonCopyable
 {
 public:
 	/// Constructor
+#ifdef JPH_SHARED_LIBRARY
+	JPH_INLINE						ExternalProfileMeasurement(const char *inName, uint32 inColor = 0) { ProfileStartMeasurement(inName, inColor, mUserData); }
+	JPH_INLINE						~ExternalProfileMeasurement() { ProfileEndMeasurement(mUserData); }
+#else
 									ExternalProfileMeasurement(const char *inName, uint32 inColor = 0);
 									~ExternalProfileMeasurement();
+#endif
 
 private:
 	uint8							mUserData[64];
@@ -42,6 +57,8 @@ JPH_SUPPRESS_WARNING_PUSH
 JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat-pedantic")
 
 // Dummy implementations
+#define JPH_PROFILE_START(name)
+#define JPH_PROFILE_END()
 #define JPH_PROFILE_THREAD_START(name)
 #define JPH_PROFILE_THREAD_END()
 #define JPH_PROFILE_NEXTFRAME()