diff --git a/Source/Engine/Profiler/ProfilerCPU.cpp b/Source/Engine/Profiler/ProfilerCPU.cpp
index cab429d2c..e3b1caad4 100644
--- a/Source/Engine/Profiler/ProfilerCPU.cpp
+++ b/Source/Engine/Profiler/ProfilerCPU.cpp
@@ -122,6 +122,14 @@ void ProfilerCPU::Thread::EndEvent(int32 index)
e.End = time;
}
+void ProfilerCPU::Thread::EndEvent()
+{
+ const double time = Platform::GetTimeSeconds() * 1000.0;
+ _depth--;
+ Event& e = Buffer.Get(Buffer.GetCount() - 1);
+ e.End = time;
+}
+
bool ProfilerCPU::IsProfilingCurrentThread()
{
return Enabled && Thread::Current != nullptr;
@@ -194,11 +202,14 @@ int32 ProfilerCPU::BeginEvent(const char* name)
void ProfilerCPU::EndEvent(int32 index)
{
- if (!Enabled)
- return;
+ if (Enabled && Thread::Current)
+ Thread::Current->EndEvent(index);
+}
- ASSERT(Thread::Current);
- Thread::Current->EndEvent(index);
+void ProfilerCPU::EndEvent()
+{
+ if (Enabled && Thread::Current)
+ Thread::Current->EndEvent();
}
void ProfilerCPU::Dispose()
diff --git a/Source/Engine/Profiler/ProfilerCPU.h b/Source/Engine/Profiler/ProfilerCPU.h
index feccdd79b..2a48e6c15 100644
--- a/Source/Engine/Profiler/ProfilerCPU.h
+++ b/Source/Engine/Profiler/ProfilerCPU.h
@@ -289,6 +289,11 @@ public:
///
/// The event index returned by the BeginEvent method.
void EndEvent(int32 index);
+
+ ///
+ /// Ends the last event running on a this thread.
+ ///
+ void EndEvent();
};
public:
@@ -341,6 +346,11 @@ public:
/// The event index returned by the BeginEvent method.
static void EndEvent(int32 index);
+ ///
+ /// Ends the last event.
+ ///
+ static void EndEvent();
+
///
/// Releases resources. Calls to the profiling API after Dispose are not valid.
///
diff --git a/Source/Engine/Scripting/Scripting.Internal.cpp b/Source/Engine/Scripting/Scripting.Internal.cpp
index 79e578401..781dc161e 100644
--- a/Source/Engine/Scripting/Scripting.Internal.cpp
+++ b/Source/Engine/Scripting/Scripting.Internal.cpp
@@ -14,29 +14,21 @@
namespace ProfilerInternal
{
- ///
- /// The managed events IDs.
- ///
- Array ManagedEvents;
-
- ///
- /// The managed events IDs for GPU profiling.
- ///
+#if COMPILE_WITH_PROFILER
Array ManagedEventsGPU;
+#endif
void BeginEvent(MonoString* nameObj)
{
#if COMPILE_WITH_PROFILER
- const auto index = ProfilerCPU::BeginEvent((const Char*)mono_string_chars(nameObj));
- ManagedEvents.Push(index);
+ ProfilerCPU::BeginEvent((const Char*)mono_string_chars(nameObj));
#endif
}
void EndEvent()
{
#if COMPILE_WITH_PROFILER
- const auto index = ManagedEvents.Pop();
- ProfilerCPU::EndEvent(index);
+ ProfilerCPU::EndEvent();
#endif
}