WwiseTask.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*******************************************************************************
  2. The content of this file includes portions of the proprietary AUDIOKINETIC Wwise
  3. Technology released in source code form as part of the game integration package.
  4. The content of this file may not be used without valid licenses to the
  5. AUDIOKINETIC Wwise Technology.
  6. Note that the use of the game engine is subject to the Unreal(R) Engine End User
  7. License Agreement at https://www.unrealengine.com/en-US/eula/unreal
  8. License Usage
  9. Licensees holding valid licenses to the AUDIOKINETIC Wwise Technology may use
  10. this file in accordance with the end user license agreement provided with the
  11. software or, alternatively, in accordance with the terms contained
  12. in a written agreement between you and Audiokinetic Inc.
  13. Copyright (c) 2023 Audiokinetic Inc.
  14. *******************************************************************************/
  15. #pragma once
  16. #include "WwiseUnrealDefines.h"
  17. #include "Wwise/Stats/NamedEvents.h"
  18. #if UE_5_1_OR_LATER
  19. #include "Tasks/Task.h"
  20. #include "Async/Async.h"
  21. using EWwiseTaskPriority = UE::Tasks::ETaskPriority;
  22. static void LaunchWwiseTask(
  23. const TCHAR* DebugName,
  24. EWwiseTaskPriority InPriority,
  25. TUniqueFunction<void()>&& InFunction)
  26. {
  27. UE::Tasks::Launch(DebugName, MoveTemp(InFunction), InPriority);
  28. }
  29. static void LaunchWwiseTask(
  30. const TCHAR* DebugName,
  31. TUniqueFunction<void()>&& InFunction)
  32. {
  33. UE::Tasks::Launch(DebugName, MoveTemp(InFunction), EWwiseTaskPriority::Default);
  34. }
  35. #else // UE4.27 & UE5.0
  36. // Adaptor for missing SCOPED_NAMED_EVENT_TCHAR_CONDITIONAL in UE4.27 and UE5.0. Equivalent to HAL/PlatformMisc.h and ProfilingDebugging/CpuProfilerTrace.h from UE5.3.
  37. #if ENABLE_NAMED_EVENTS
  38. class FWwiseScopedNamedEventConditional
  39. {
  40. public:
  41. FWwiseScopedNamedEventConditional(const struct FColor& Color, const TCHAR* Text, bool bCondition)
  42. : bStarted(bCondition)
  43. {
  44. if (bCondition)
  45. {
  46. FPlatformMisc::BeginNamedEvent(Color, Text);
  47. }
  48. }
  49. FWwiseScopedNamedEventConditional(const struct FColor& Color, const ANSICHAR* Text, bool bCondition)
  50. : bStarted(bCondition)
  51. {
  52. if (bCondition)
  53. {
  54. FPlatformMisc::BeginNamedEvent(Color, Text);
  55. }
  56. }
  57. ~FWwiseScopedNamedEventConditional()
  58. {
  59. if (bStarted)
  60. {
  61. FPlatformMisc::EndNamedEvent();
  62. }
  63. }
  64. private:
  65. bool bStarted;
  66. };
  67. #endif
  68. #if CPUPROFILERTRACE_ENABLED
  69. struct FWwiseDynamicEventScope
  70. {
  71. FORCEINLINE FWwiseDynamicEventScope(const ANSICHAR* InEventName, bool bInCondition, const ANSICHAR* InFile = nullptr, uint32 InLine = 0)
  72. : bEnabled(bInCondition && CpuChannel)
  73. {
  74. if (bEnabled)
  75. {
  76. FCpuProfilerTrace::OutputBeginDynamicEvent(InEventName);
  77. }
  78. }
  79. FORCEINLINE FWwiseDynamicEventScope(const TCHAR* InEventName, bool bInCondition, const ANSICHAR* InFile = nullptr, uint32 InLine = 0)
  80. : bEnabled(bInCondition && CpuChannel)
  81. {
  82. if (bEnabled)
  83. {
  84. FCpuProfilerTrace::OutputBeginDynamicEvent(InEventName);
  85. }
  86. }
  87. FORCEINLINE ~FWwiseDynamicEventScope()
  88. {
  89. if (bEnabled)
  90. {
  91. FCpuProfilerTrace::OutputEndEvent();
  92. }
  93. }
  94. bool bEnabled;
  95. };
  96. #define TRACE_CPUPROFILER_EVENT_SCOPE_TEXT_CONDITIONAL(Name, Condition) \
  97. FWwiseDynamicEventScope PREPROCESSOR_JOIN(__CpuProfilerEventScope, __LINE__)(Name, (Condition), __FILE__, __LINE__);
  98. #else
  99. #define TRACE_CPUPROFILER_EVENT_SCOPE_TEXT_CONDITIONAL(Name, Condition)
  100. #endif
  101. #if ENABLE_NAMED_EVENTS
  102. #define SCOPED_NAMED_EVENT_TCHAR_CONDITIONAL(Text, Color, bCondition)\
  103. FWwiseScopedNamedEventConditional ANONYMOUS_VARIABLE(NamedEvent_)(Color, Text, (bCondition));\
  104. TRACE_CPUPROFILER_EVENT_SCOPE_TEXT_CONDITIONAL(Text, (bCondition));
  105. #else
  106. #define SCOPED_NAMED_EVENT_TCHAR_CONDITIONAL(Text, Color, bCondition)
  107. #endif
  108. /// Adaptor between UE::Tasks::ETaskPriority and ENamedThreads::Type
  109. enum class EWwiseTaskPriority : int32
  110. {
  111. High = ENamedThreads::AnyHiPriThreadNormalTask,
  112. Normal = ENamedThreads::AnyNormalThreadNormalTask,
  113. Default = EWwiseTaskPriority::Normal,
  114. BackgroundHigh = ENamedThreads::AnyBackgroundHiPriTask,
  115. BackgroundNormal = ENamedThreads::AnyBackgroundThreadNormalTask,
  116. BackgroundLow = ENamedThreads::AnyBackgroundThreadNormalTask
  117. };
  118. static void LaunchWwiseTask(
  119. const TCHAR* DebugName,
  120. EWwiseTaskPriority InPriority,
  121. TUniqueFunction<void()>&& InFunction)
  122. {
  123. #if !ENABLE_NAMED_EVENTS
  124. DebugName = nullptr;
  125. #endif
  126. FFunctionGraphTask::CreateAndDispatchWhenReady([DebugName, InFunction = MoveTemp(InFunction)]
  127. {
  128. SCOPED_NAMED_EVENT_TCHAR_CONDITIONAL(DebugName, WwiseNamedEvents::Color3, DebugName != nullptr);
  129. InFunction();
  130. },
  131. {},
  132. nullptr,
  133. static_cast<ENamedThreads::Type>(InPriority));
  134. }
  135. static void LaunchWwiseTask(
  136. const TCHAR* DebugName,
  137. TUniqueFunction<void()>&& InFunction)
  138. {
  139. #if !ENABLE_NAMED_EVENTS
  140. DebugName = nullptr;
  141. #endif
  142. FFunctionGraphTask::CreateAndDispatchWhenReady([DebugName, InFunction = MoveTemp(InFunction)]
  143. {
  144. SCOPED_NAMED_EVENT_TCHAR_CONDITIONAL(DebugName, WwiseNamedEvents::Color1, DebugName != nullptr);
  145. InFunction();
  146. },
  147. {},
  148. nullptr,
  149. static_cast<ENamedThreads::Type>(EWwiseTaskPriority::Default));
  150. }
  151. #endif