diff --git a/Source/Engine/Threading/JobSystem.cpp b/Source/Engine/Threading/JobSystem.cpp index 08bbef410..c89afed8f 100644 --- a/Source/Engine/Threading/JobSystem.cpp +++ b/Source/Engine/Threading/JobSystem.cpp @@ -87,6 +87,7 @@ namespace JobSystemService JobSystemInstance; Thread* Threads[32] = {}; int32 ThreadsCount = 0; + bool JobStartingOnDispatch = true; volatile int64 ExitFlag = 0; volatile int64 DoneLabel = 0; volatile int64 NextLabel = 0; @@ -234,10 +235,13 @@ int64 JobSystem::Dispatch(const Function& job, int32 jobCount) LOG(Info, "Job enqueue time: {0} cycles", (int64)(Platform::GetTimeCycles() - start)); #endif - if (jobCount == 1) - JobsSignal.NotifyOne(); - else - JobsSignal.NotifyAll(); + if (JobStartingOnDispatch) + { + if (jobCount == 1) + JobsSignal.NotifyOne(); + else + JobsSignal.NotifyAll(); + } return label; #else @@ -278,3 +282,21 @@ void JobSystem::Wait(int64 label) #endif #endif } + +void JobSystem::SetJobStartingOnDispatch(bool value) +{ +#if JOB_SYSTEM_ENABLED + JobStartingOnDispatch = value; + + if (value) + { + JobsLocker.Lock(); + const int32 count = Jobs.Count(); + JobsLocker.Unlock(); + if (count == 1) + JobsSignal.NotifyOne(); + else if (count != 0) + JobsSignal.NotifyAll(); + } +#endif +} diff --git a/Source/Engine/Threading/JobSystem.h b/Source/Engine/Threading/JobSystem.h index cf9b79073..7a7e8cf7f 100644 --- a/Source/Engine/Threading/JobSystem.h +++ b/Source/Engine/Threading/JobSystem.h @@ -29,4 +29,9 @@ DECLARE_SCRIPTING_TYPE_MINIMAL(JobSystem); /// /// The label. API_FUNCTION() static void Wait(int64 label); + + /// + /// Sets whether automatically start jobs execution on Dispatch. If disabled jobs won't be executed until it gets re-enabled. Can be used to optimize execution of multiple dispatches that should overlap. + /// + API_FUNCTION() static void SetJobStartingOnDispatch(bool value); }; diff --git a/Source/Engine/Threading/TaskGraph.cpp b/Source/Engine/Threading/TaskGraph.cpp index 621636203..b36a85e31 100644 --- a/Source/Engine/Threading/TaskGraph.cpp +++ b/Source/Engine/Threading/TaskGraph.cpp @@ -94,6 +94,7 @@ void TaskGraph::Execute() // Execute in order Sorting::QuickSort(_queue.Get(), _queue.Count(), &SortTaskGraphSystem); + JobSystem::SetJobStartingOnDispatch(false); _currentLabel = 0; for (int32 i = 0; i < _queue.Count(); i++) { @@ -104,6 +105,7 @@ void TaskGraph::Execute() _queue.Clear(); // Wait for async jobs to finish + JobSystem::SetJobStartingOnDispatch(true); JobSystem::Wait(_currentLabel); }