You've already forked UnrealEngineUWP
mirror of
https://github.com/izzy2lost/UnrealEngineUWP.git
synced 2026-03-26 18:15:20 -07:00
#lockdown Nick.Penwarden ========================== MAJOR FEATURES + CHANGES ========================== Change 3315219 on 2017/02/21 by Steve.Robb Fix for FObjectAndNameAsStringProxyArchive when serializing a TWeakObjectPtr. Change 3315285 on 2017/02/21 by Steve.Robb Explicitly pass string builder into code generation functions. Change 3315341 on 2017/02/21 by Ben.Marsh UAT: Clean up some formatting in StreamCopyDescription output - remove #fyi lines, exclude merge commits, and remove some blank lines. Change 3315350 on 2017/02/21 by Ben.Marsh Fix shared resource files not being rebuilt if the version header changes. Change 3315823 on 2017/02/21 by Ben.Marsh UAT: Use a class derived from AutomationException to return information specific to commandlets failing, rather than putting it in the base class. Change 3315826 on 2017/02/21 by Ben.Marsh UAT: Move Distiller class from general use in UAT; FileFilter provides a much safer and fully featured implementation of the same concepts. Change 3315857 on 2017/02/21 by Ben.Marsh UBT: Remove the StripBaseDirectory() and MakeRerootedFilePath() utility functions from UBT. These operations can now be done more safely with FileReference objects. Change 3315942 on 2017/02/21 by Ben.Marsh UBT: Convert FileFilter to use FileReference and DirectoryReference arguments everywhere. Change3316236on 2017/02/22 by Maciej.Mroz #jira UE-42045 Nativization Fixed Warning: TEnumAsByte is not intended for use with enum Change 3316253 on 2017/02/22 by Robert.Manuszewski Fixes for the async log file writer hangs and crashes. - potential fix for the logging system hang when running out of disk space while flushing log - fix for unexpected concurrency assert when flushing the log buffer to disk Change3316293on 2017/02/22 by Steve.Robb GetTypeHash and lexicographical comparison operators (operator<() etc.) for TTuple. Change 3316342 on 2017/02/22 by Maciej.Mroz Nativization: Wrappers (stubs) required only by other wrappers are properly generated. #codereview: Mike.Beach Change 3316344 on 2017/02/22 by Maciej.Mroz Fixed crash in nativized Odin Async loading properly handles nativized structs. Change 3316359 on 2017/02/22 by Steve.Robb GitHub #3287 : Ignore #pragma in USTRUCTs #jira UE-42248 Change 3316389 on 2017/02/22 by Matthew.Griffin Switched Installed Engine Filters to multiline properties to make them more readable Added Oodle to list of excluded plugins #jira UE-42030 Change 3316392 on 2017/02/22 by Ben.Marsh UBT: Split out FileReference/DirectoryReference classes into their own file. Change 3316394 on 2017/02/22 by Ben.Marsh UBT: Move FileReference/DirectoryReference extension methods into the appropriate file. Change 3316411 on 2017/02/22 by Ben.Marsh UAT: Remove file functions that take multiple arguments. There's not really a compelling use case for these to exist over looping from the calling code. Change 3316446 on 2017/02/22 by Ben.Marsh UAT: Try disabling function name prefix to log output from UAT, to see if it improves readability. Function names are still included in the log file for debugging. Change 3316575 on 2017/02/22 by Ben.Marsh UAT: Remove unused functionality for dealing with labels, and output a more human readable list of P4 settings at startup. Change 3318481 on 2017/02/22 by Steve.Robb Use of FMath::IsPowerOfTwo in check. Static assert to ensure that an inline set allocator will have a hash size of a power of two. Change 3318496 on 2017/02/22 by Steve.Robb Fix for TSet visualizers. Change 3318919 on 2017/02/23 by Steve.Robb Fix for hot reloading UScriptStruct-derived objects in a module, where the CDOs of these objects haven't had PrepareCppStructOps() called on them. #jira UE-42178 Change 3318942 on 2017/02/23 by Steve.Robb Removal of a redundant insertion which can cause problems on reallocation of the map. Change 3319010 on 2017/02/23 by Ben.Marsh UBT: Fix exception when a file that was previously part of the working set is deleted. Change 3319134 on 2017/02/23 by Robert.Manuszewski Better fix for a deadlock when flushing log while it's already being flushed due to flush timer on the async log writer thread. Change 3319249 on 2017/02/23 by Matthew.Griffin Added a function to check if running with debug game libs instead of checking command line in multiple places Added -RunConfig parameter, which has equivalent result to -debug if value of parameter starts with 'debug' Added -RunConfig=$(Configuration) as a default commandline argument for Mac so that editor can use debug game libs Removed -Shipping argument from VCProject generation as it's not used anymore Change 3319253 on 2017/02/23 by Maciej.Mroz #jira UE-41846 New mechanism to gather modules necessary for Nativized Assets. The modules are listed based on included headers. Previously the dependencies was gathered only in FBlueprintNativeCodeGenManifest::GatherModuleDependencies. Change3319591on 2017/02/23 by Ben.Marsh Don't strip prefixes beginning with WARNING: or ERROR: using the Postp filter. Change 3320357 on 2017/02/23 by Steven.Hutton Slight changes to Add Crash method - Returning select fields instead of entity objects in queries for perf reasons. Change 3320361 on 2017/02/23 by Steven.Hutton Performance improvements subsequent to the recent database changes. Change 3320446 on 2017/02/23 by Steven.Hutton adding my temporary performance tracker class - reports to a private slack channel with add crash performance data. Change 3320479 on 2017/02/23 by Ben.Marsh Fix CIS errors. Change 3320576 on 2017/02/23 by Jin.Zhang Update CrashReporter to use AWS Change 3320742 on 2017/02/23 by Jin.Zhang Merging crash caching Change 3321119 on 2017/02/24 by Robert.Manuszewski DLL injection protection support for non-monolithic builds Change 3323308 on 2017/02/27 by Matthew.Griffin Moved compilation of SwarmInterface after its dependencies so that we will see a build failure immediately if they change version in future Change 3323423 on 2017/02/27 by Chad.Garyet Adding a script to check and warn about csproj targeted .net versions being mismatched #JIRA UE-39624 Change 3323442 on 2017/02/27 by Ben.Marsh UBT: Output an error if an engine module references a game module. Change 3323743 on 2017/02/27 by Ben.Marsh PR #3303: Resolved PVS scan issues (Contributed by projectgheist) Change 3323748 on 2017/02/27 by Ben.Marsh Convert whitespace to tabs. Change 3324851 on 2017/02/28 by Chris.Wood Add Odin symbol locations to engine config for MDD on CR server. NotForLicensees Change 3324979 on 2017/02/28 by Gil.Gribb Fixed bad merge of priority change in the EDL. Change 3326889 on 2017/03/01 by Steven.Hutton Update to buggs controller to generate faster queries. Change 3326910 on 2017/03/01 by Robert.Manuszewski Removing legacy #if from PackageFileSummary. Change 3327118 on 2017/03/01 by Gil.Gribb UE4 - Fixed race that resulted in a memory leak when reading compressed paks. Change 3327633 on 2017/03/01 by Gil.Gribb UE4 - Added a cvar to control the pak precacher thottle. Change 3327674 on 2017/03/01 by Steve.Robb Unified boilerplate between all generated code files. Change 3328544 on 2017/03/01 by Chris.Wood CrashReportProcess.config update (CRP v1.2.17) Tweaks to a few values. Update website URL to explicitly point to old, non-cloud site on devweb-02. Change 3328714 on 2017/03/01 by Chris.Wood Correct CRP config regression. Point website at new cloud site. Still v1.2.17 Change3329192on 2017/03/02 by Matthew.Griffin Added Shared Build Id file to the list of Precompiled Build Dependencies in a target receipt so that it's brought into an installed build Change 3329285 on 2017/03/02 by Ben.Marsh UGS: Allow a project to specify a filters for the streams that should be displayed for fast-switching to. The QuickSelectStreamList seting in the [Options] section of the project settings references a depot path containing a list of strings used to filter the stream list. An option is shown to switch back to showing all available streams, if desired. Change 3330636 on 2017/03/02 by Ben.Marsh UBT: Bump version number of C++ include cache to force it to be rebuilt with additional include information for the default RC files. Change 3331262 on 2017/03/03 by Robert.Manuszewski Merging Dev-LoadTimes to Dev-Core (Garbage Collection performance improvements) - Improved GC multithreading - Improved BeginDestroy performance - Introduced ULevelActorCluster for StaticMeshActor and ReflectionCapture actor clustering (can be toggled through project settings or console command gc.ActorClusterEnabled) - A few improvements to AddReferencedObjects functions - Misc improvements to GC code - Garbage Collector now properly handles clusters which had their objects marked as pending kill - Blueprints can now create clusters too (can be toggled through project settings or console command gc.BlueprintClusteringEnabled, defaults to disabled) Change 3331285 on 2017/03/03 by Robert.Manuszewski A few fixes for the previous check-in. Change 3332001 on 2017/03/03 by Ben.Marsh UBT: Add support for generating a UDN file containing the valid settings for BuildConfiguration.xml. Pass -configdoc=<filename> on the command line to generate such a file. Change 3332022 on 2017/03/03 by Ben.Marsh Update documentation for where to find the BuildConfiguration settings. Change 3332031 on 2017/03/03 by Ben.Marsh Remove documentation for Windows XP support; it has been removed in the 4.16 release. Change 3332256 on 2017/03/03 by Ben.Marsh UBT: Add support for generating a UDN page containing module and target settings. Change 3332458 on 2017/03/03 by Ben.Marsh UBT: Improvements to generated documentation. Change 3332459 on 2017/03/03 by Ben.Marsh Add generated documentation for .target.cs files, .build.cs files, and BuildConfiguration.xml files. Change 3332460 on 2017/03/03 by Ben.Marsh UBT: Make LinkTypePrivate actually private, so it doesn't show up in the docs. Change 3332899 on 2017/03/06 by Robert.Manuszewski Making sure actor clustering is not used in the editor (fix for actors being deleted when GC runs in the editor) #jira UE-42548 Change 3332955 on 2017/03/06 by Maciej.Mroz Nativization distinguishes client and server platform: - Separated lists on additional assets, additional modules, excluded assets, excluded modules, excluded paths (in config) - Context (compilation options, nativization options and platform) is deliveren to BPCOmpilerCppBackend in FCompilerNativizationOptions struct. - Wrappers (for unconverted BPs) are created only when they are directly called. - Fortnite dedicated server can be nativized Change 3332990 on 2017/03/06 by Ben.Marsh UBT: Add more comprehensive wrapper methods for System.IO.File and System.IO.Directory to FileReference and DirectoryReference. Change 3333032 on 2017/03/06 by Ben.Marsh Documentation for build tools Change 3333037 on 2017/03/06 by Ben.Marsh Add a build step to extract UAT and UBT documentation from XML comments. Change 3333089 on 2017/03/06 by Ben.Marsh UAT: Re-enable logging the calling function to the console in UAT. Needs a pass for readability first. Change 3333651 on 2017/03/06 by Gil.Gribb UE4 - Fix a werid recursive situation where StaticLoadObject could return an object that has not finished loading. Also produces a fatal error if this sometimes happens. EDL only. Change 3335236 on 2017/03/07 by Ben.Marsh UGS: Set the sync changelist separately to the compatibility changelist. Change 3335261 on 2017/03/07 by Gil.Gribb UE4 - Fixed batched render fences when BeginDestroy calls FlushRenderingCommands. Change 3335740 on 2017/03/07 by Gil.Gribb maybe fix static analysis warning Change 3335945 on 2017/03/07 by Steve.Robb Move FFindInstancedReferenceSubobjectHelper code out of header. Add map/set property support to allow instanced members of these container types to be handled during CPFUO. https://udn.unrealengine.com/questions/349232/tmap-with-instanced-object-as-value-gets-cleared-o.html Change 3336693 on 2017/03/07 by Ben.Marsh UBT: Use shared PCHs for game plugins by default, to reduce time spent generating individual PCHs. Change3336694on 2017/03/07 by Steve.Robb Static assert added to TMap to prevent the use of keys which don't implement a GetTypeHash. Fixes to types which relied on implicit conversions when calling GetTypeHash. Workaround in SAssetView.h and PropertyEditorModule.h for an apparent VC bug where the compiler wrongly instantiates TPointerIsConvertibleFromTo for certain forward-declared types, causing future TSharedPtr conversions to fail. #jira UE-42441 Change 3336698 on 2017/03/07 by Steve.Robb Hardcoded endpoint handling replaced with a generic string. Obsolete .proto and .java code generation removed. Change 3336811 on 2017/03/07 by Wes.Hunt Add a game blacklist to the crash report processor. Fixed a syntax error in Config.cs, added a XML comment to shut up a warning. Change 3336973 on 2017/03/08 by Steve.Robb Fix for missing GetTypeHash in a plugin. Change 3336996 on 2017/03/08 by Steve.Robb Significant refactor of code generation, to try and make data flow more apparent. Change 3337571 on 2017/03/08 by Steve.Robb CIS fixes for missing GetTypeHash functions. Non-unity fix. Change 3337588 on 2017/03/08 by Gil.Gribb UE4 - Fixed obscure check with flushing rhi resources. Change 3337620 on 2017/03/08 by Steve.Robb WITH_HOT_RELOAD_CTORS macros removed. UseVTableConstructors config option removed. Change 3339369 on 2017/03/09 by Steve.Robb GetTypeHash overload for nn::account::Uid. Change 3339464 on 2017/03/09 by Daniel.Lamb Fixed assert in 4.15 to do with trying to gather dependency info from invalid packages. #jira UE-42583 #test Editor + Cook + Run shootergame Change 3339465 on 2017/03/09 by Maciej.Mroz Fixed serialization issue, after UserDefinedEnum was used in EnumProperty. Change 3339469 on 2017/03/09 by Maciej.Mroz Fixed Nativization problem, when default value is passed as non-const reference. Change 3340178 on 2017/03/09 by Daniel.Lamb Added support for in memory only packages. The Cooker ignores these and added core functions to recognize these packages. Other systems will need to add support where nessisary. Change 3341002 on 2017/03/10 by Maciej.Mroz Nativization: Fixed FFindHeadersToInclude. Headers necessary for owners of subobjects are properly included. Change 3341076 on 2017/03/10 by Steve.Robb Fix for FBakedTextureSourceInfo move semantics. #jira UE-42658 Change 3341160 on 2017/03/10 by Gil.Gribb UE4 - Fix hazard with SetMaterialUsage from a thread. Change 3341409 on 2017/03/10 by Steve.Robb Reduction of the generated code size for StaticRegisterNatives functions. Change 3341523 on 2017/03/10 by Steve.Robb Code generation simplified. Change 3341800 on 2017/03/10 by Ben.Marsh UnrealVS: Fix UnrealVS compatibility with RTM version of Visual Studio 2017. 2017 toolchain for extensions is no longer able to build <= 2015 extensions due to validation of the VSIX manifest, so create a separate solution for it. Change 3342034 on 2017/03/10 by Ben.Marsh Fix compiler setting not being loaded correctly into the Windows target settings dialog. #jira UE-42746 Change 3342041 on 2017/03/10 by Ben.Marsh Fix -ErrorOnEngineContentUse not being set in the cooker options correctly. Change 3342094 on 2017/03/10 by Steve.Robb Fix to deteministic name order during code generation. Change 3342251 on 2017/03/10 by Daniel.Lamb Integrate fix for resave lightmaps commandlet when upgrading from no mapbuilddatapackages to mapbuilddatapackages. #thanks Tim.Hagberg #test None Change 3342961 on 2017/03/13 by Robert.Manuszewski Fixing memory leak when playing while running -nullrhi on the commandline in cooked games caused by shader resources not being destroyed. #jira FORT-38977 Change 3343022 on 2017/03/13 by Steve.Robb GetTypeHash fixes for FUniqueNetIdLive. #jira UE-42788 Change 3343448 on 2017/03/13 by Steve.Robb Compiled-in defer object order fixed. Debuggability of the deferred registration map improved. #jira UE-42828 [CL 3345747 by Ben Marsh in Main branch]
1177 lines
35 KiB
C++
1177 lines
35 KiB
C++
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
|
|
|
|
/*=============================================================================
|
|
RenderingThread.cpp: Rendering thread implementation.
|
|
=============================================================================*/
|
|
|
|
#include "RenderingThread.h"
|
|
#include "HAL/Runnable.h"
|
|
#include "HAL/RunnableThread.h"
|
|
#include "HAL/ExceptionHandling.h"
|
|
#include "Misc/OutputDeviceRedirector.h"
|
|
#include "Misc/CoreStats.h"
|
|
#include "Misc/TimeGuard.h"
|
|
#include "Misc/CoreDelegates.h"
|
|
#include "RenderCore.h"
|
|
#include "RenderCommandFence.h"
|
|
#include "RHI.h"
|
|
#include "TickableObjectRenderThread.h"
|
|
#include "Stats/StatsData.h"
|
|
#include "HAL/ThreadHeartBeat.h"
|
|
#include "RenderResource.h"
|
|
|
|
//
|
|
// Globals
|
|
//
|
|
|
|
|
|
|
|
RENDERCORE_API bool GIsThreadedRendering = false;
|
|
RENDERCORE_API bool GUseThreadedRendering = false;
|
|
RENDERCORE_API bool GUseRHIThread = false;
|
|
|
|
#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
|
|
RENDERCORE_API bool GMainThreadBlockedOnRenderThread = false;
|
|
#endif // #if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
|
|
|
|
static FRunnable* GRenderingThreadRunnable = NULL;
|
|
|
|
/** If the rendering thread has been terminated by an unhandled exception, this contains the error message. */
|
|
FString GRenderingThreadError;
|
|
|
|
/**
|
|
* Polled by the game thread to detect crashes in the rendering thread.
|
|
* If the rendering thread crashes, it sets this variable to false.
|
|
*/
|
|
volatile bool GIsRenderingThreadHealthy = true;
|
|
|
|
|
|
/**
|
|
* Maximum rate the rendering thread will tick tickables when idle (in Hz)
|
|
*/
|
|
float GRenderingThreadMaxIdleTickFrequency = 40.f;
|
|
|
|
/** Function to stall the rendering thread **/
|
|
static void SuspendRendering()
|
|
{
|
|
FPlatformAtomics::InterlockedIncrement(&GIsRenderingThreadSuspended);
|
|
FPlatformMisc::MemoryBarrier();
|
|
}
|
|
|
|
/** Function to wait and resume rendering thread **/
|
|
static void WaitAndResumeRendering()
|
|
{
|
|
while ( GIsRenderingThreadSuspended )
|
|
{
|
|
// Just sleep a little bit.
|
|
FPlatformProcess::Sleep( 0.001f ); //@todo this should be a more principled wait
|
|
}
|
|
|
|
// set the thread back to real time mode
|
|
FPlatformProcess::SetRealTimeMode();
|
|
}
|
|
|
|
/**
|
|
* Constructor that flushes and suspends the renderthread
|
|
* @param bRecreateThread - Whether the rendering thread should be completely destroyed and recreated, or just suspended.
|
|
*/
|
|
FSuspendRenderingThread::FSuspendRenderingThread( bool bInRecreateThread )
|
|
{
|
|
// Suspend async loading thread so that it doesn't start queueing render commands
|
|
// while the render thread is suspended.
|
|
if (IsAsyncLoadingMultithreaded())
|
|
{
|
|
SuspendAsyncLoading();
|
|
}
|
|
|
|
bRecreateThread = bInRecreateThread;
|
|
bUseRenderingThread = GUseThreadedRendering;
|
|
bWasRenderingThreadRunning = GIsThreadedRendering;
|
|
if ( bRecreateThread )
|
|
{
|
|
StopRenderingThread();
|
|
// GUseThreadedRendering should be set to false after StopRenderingThread call since
|
|
// otherwise a wrong context could be used.
|
|
GUseThreadedRendering = false;
|
|
FPlatformAtomics::InterlockedIncrement( &GIsRenderingThreadSuspended );
|
|
}
|
|
else
|
|
{
|
|
if ( GIsRenderingThreadSuspended == 0 )
|
|
{
|
|
// First tell the render thread to finish up all pending commands and then suspend its activities.
|
|
|
|
// this ensures that async stuff will be completed too
|
|
FlushRenderingCommands();
|
|
|
|
if (GIsThreadedRendering)
|
|
{
|
|
DECLARE_CYCLE_STAT(TEXT("FSimpleDelegateGraphTask.SuspendRendering"),
|
|
STAT_FSimpleDelegateGraphTask_SuspendRendering,
|
|
STATGROUP_TaskGraphTasks);
|
|
|
|
FGraphEventRef CompleteHandle = FSimpleDelegateGraphTask::CreateAndDispatchWhenReady(
|
|
FSimpleDelegateGraphTask::FDelegate::CreateStatic(&SuspendRendering),
|
|
GET_STATID(STAT_FSimpleDelegateGraphTask_SuspendRendering), NULL, ENamedThreads::RenderThread);
|
|
|
|
// Busy wait while Kismet debugging, to avoid opportunistic execution of game thread tasks
|
|
// If the game thread is already executing tasks, then we have no choice but to spin
|
|
if (GIntraFrameDebuggingGameThread || FTaskGraphInterface::Get().IsThreadProcessingTasks(ENamedThreads::GameThread) )
|
|
{
|
|
while (!GIsRenderingThreadSuspended)
|
|
{
|
|
FPlatformProcess::Sleep(0.0f);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
QUICK_SCOPE_CYCLE_COUNTER(STAT_FSuspendRenderingThread);
|
|
FTaskGraphInterface::Get().WaitUntilTaskCompletes(CompleteHandle, ENamedThreads::GameThread);
|
|
}
|
|
check(GIsRenderingThreadSuspended);
|
|
|
|
// Now tell the render thread to busy wait until it's resumed
|
|
DECLARE_CYCLE_STAT(TEXT("FSimpleDelegateGraphTask.WaitAndResumeRendering"),
|
|
STAT_FSimpleDelegateGraphTask_WaitAndResumeRendering,
|
|
STATGROUP_TaskGraphTasks);
|
|
|
|
FSimpleDelegateGraphTask::CreateAndDispatchWhenReady(
|
|
FSimpleDelegateGraphTask::FDelegate::CreateStatic(&WaitAndResumeRendering),
|
|
GET_STATID(STAT_FSimpleDelegateGraphTask_WaitAndResumeRendering), NULL, ENamedThreads::RenderThread);
|
|
}
|
|
else
|
|
{
|
|
SuspendRendering();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// The render-thread is already suspended. Just bump the ref-count.
|
|
FPlatformAtomics::InterlockedIncrement( &GIsRenderingThreadSuspended );
|
|
}
|
|
}
|
|
}
|
|
|
|
/** Destructor that starts the renderthread again */
|
|
FSuspendRenderingThread::~FSuspendRenderingThread()
|
|
{
|
|
#if PLATFORM_MAC // On OS X Apple's context sharing is a strict interpretation of the spec. so a resource is only properly visible to other contexts
|
|
// in the share group after a flush. Thus we call RHIFlushResources which will flush the current context's commands to GL (but not wait for them).
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND(FlushCommand,
|
|
RHIFlushResources();
|
|
);
|
|
#endif
|
|
|
|
if ( bRecreateThread )
|
|
{
|
|
GUseThreadedRendering = bUseRenderingThread;
|
|
FPlatformAtomics::InterlockedDecrement( &GIsRenderingThreadSuspended );
|
|
if ( bUseRenderingThread && bWasRenderingThreadRunning )
|
|
{
|
|
StartRenderingThread();
|
|
|
|
// Now tell the render thread to set it self to real time mode
|
|
DECLARE_CYCLE_STAT(TEXT("FSimpleDelegateGraphTask.SetRealTimeMode"),
|
|
STAT_FSimpleDelegateGraphTask_SetRealTimeMode,
|
|
STATGROUP_TaskGraphTasks);
|
|
|
|
FSimpleDelegateGraphTask::CreateAndDispatchWhenReady(
|
|
FSimpleDelegateGraphTask::FDelegate::CreateStatic(&FPlatformProcess::SetRealTimeMode),
|
|
GET_STATID(STAT_FSimpleDelegateGraphTask_SetRealTimeMode), NULL, ENamedThreads::RenderThread
|
|
);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Resume the render thread again.
|
|
FPlatformAtomics::InterlockedDecrement( &GIsRenderingThreadSuspended );
|
|
}
|
|
if (IsAsyncLoadingMultithreaded())
|
|
{
|
|
ResumeAsyncLoading();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Tick all rendering thread tickable objects
|
|
*/
|
|
|
|
/** Static array of tickable objects that are ticked from rendering thread*/
|
|
FTickableObjectRenderThread::FRenderingThreadTickableObjectsArray FTickableObjectRenderThread::RenderingThreadTickableObjects;
|
|
FTickableObjectRenderThread::FRenderingThreadTickableObjectsArray FTickableObjectRenderThread::RenderingThreadHighFrequencyTickableObjects;
|
|
|
|
void TickHighFrequencyTickables(double CurTime)
|
|
{
|
|
static double LastHighFreqTime = FPlatformTime::Seconds();
|
|
float DeltaSecondsHighFreq = CurTime - LastHighFreqTime;
|
|
|
|
// tick any high frequency rendering thread tickables.
|
|
for (int32 ObjectIndex = 0; ObjectIndex < FTickableObjectRenderThread::RenderingThreadHighFrequencyTickableObjects.Num(); ObjectIndex++)
|
|
{
|
|
FTickableObjectRenderThread* TickableObject = FTickableObjectRenderThread::RenderingThreadHighFrequencyTickableObjects[ObjectIndex];
|
|
// make sure it wants to be ticked and the rendering thread isn't suspended
|
|
if (TickableObject->IsTickable())
|
|
{
|
|
STAT(FScopeCycleCounter(TickableObject->GetStatId());)
|
|
TickableObject->Tick(DeltaSecondsHighFreq);
|
|
}
|
|
}
|
|
|
|
LastHighFreqTime = CurTime;
|
|
}
|
|
|
|
void TickRenderingTickables()
|
|
{
|
|
static double LastTickTime = FPlatformTime::Seconds();
|
|
|
|
|
|
// calc how long has passed since last tick
|
|
double CurTime = FPlatformTime::Seconds();
|
|
float DeltaSeconds = CurTime - LastTickTime;
|
|
|
|
TickHighFrequencyTickables(CurTime);
|
|
|
|
if (DeltaSeconds < (1.f/GRenderingThreadMaxIdleTickFrequency))
|
|
{
|
|
return;
|
|
}
|
|
|
|
// tick any rendering thread tickables
|
|
for (int32 ObjectIndex = 0; ObjectIndex < FTickableObjectRenderThread::RenderingThreadTickableObjects.Num(); ObjectIndex++)
|
|
{
|
|
FTickableObjectRenderThread* TickableObject = FTickableObjectRenderThread::RenderingThreadTickableObjects[ObjectIndex];
|
|
// make sure it wants to be ticked and the rendering thread isn't suspended
|
|
if (TickableObject->IsTickable())
|
|
{
|
|
STAT(FScopeCycleCounter(TickableObject->GetStatId());)
|
|
TickableObject->Tick(DeltaSeconds);
|
|
}
|
|
}
|
|
// update the last time we ticked
|
|
LastTickTime = CurTime;
|
|
}
|
|
|
|
/** Accumulates how many cycles the renderthread has been idle. It's defined in RenderingThread.cpp. */
|
|
uint32 GRenderThreadIdle[ERenderThreadIdleTypes::Num] = {0};
|
|
/** Accumulates how times renderthread was idle. It's defined in RenderingThread.cpp. */
|
|
uint32 GRenderThreadNumIdle[ERenderThreadIdleTypes::Num] = {0};
|
|
/** How many cycles the renderthread used (excluding idle time). It's set once per frame in FViewport::Draw. */
|
|
uint32 GRenderThreadTime = 0;
|
|
|
|
|
|
|
|
/** The RHI thread runnable object. */
|
|
class FRHIThread : public FRunnable
|
|
{
|
|
public:
|
|
FRunnableThread* Thread;
|
|
|
|
FRHIThread()
|
|
: Thread(nullptr)
|
|
{
|
|
check(IsInGameThread());
|
|
}
|
|
|
|
virtual uint32 Run() override
|
|
{
|
|
FMemory::SetupTLSCachesOnCurrentThread();
|
|
FTaskGraphInterface::Get().AttachToThread(ENamedThreads::RHIThread);
|
|
FTaskGraphInterface::Get().ProcessThreadUntilRequestReturn(ENamedThreads::RHIThread);
|
|
FMemory::ClearAndDisableTLSCachesOnCurrentThread();
|
|
return 0;
|
|
}
|
|
|
|
static FRHIThread& Get()
|
|
{
|
|
static FRHIThread Singleton;
|
|
return Singleton;
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
Thread = FRunnableThread::Create(this, TEXT("RHIThread"), 512 * 1024, TPri_SlightlyBelowNormal,
|
|
FPlatformAffinity::GetRHIThreadMask()
|
|
);
|
|
check(Thread);
|
|
}
|
|
};
|
|
|
|
/** The rendering thread main loop */
|
|
void RenderingThreadMain( FEvent* TaskGraphBoundSyncEvent )
|
|
{
|
|
ENamedThreads::RenderThread = ENamedThreads::Type(ENamedThreads::ActualRenderingThread);
|
|
ENamedThreads::RenderThread_Local = ENamedThreads::Type(ENamedThreads::ActualRenderingThread_Local);
|
|
FTaskGraphInterface::Get().AttachToThread(ENamedThreads::RenderThread);
|
|
FPlatformMisc::MemoryBarrier();
|
|
|
|
// Inform main thread that the render thread has been attached to the taskgraph and is ready to receive tasks
|
|
if( TaskGraphBoundSyncEvent != NULL )
|
|
{
|
|
TaskGraphBoundSyncEvent->Trigger();
|
|
}
|
|
|
|
// set the thread back to real time mode
|
|
FPlatformProcess::SetRealTimeMode();
|
|
|
|
#if STATS
|
|
if (FThreadStats::WillEverCollectData())
|
|
{
|
|
FThreadStats::ExplicitFlush(); // flush the stats and set update the scope so we don't flush again until a frame update, this helps prevent fragmentation
|
|
}
|
|
#endif
|
|
|
|
FCoreDelegates::PostRenderingThreadCreated.Broadcast();
|
|
check(GIsThreadedRendering);
|
|
FTaskGraphInterface::Get().ProcessThreadUntilRequestReturn(ENamedThreads::RenderThread);
|
|
FPlatformMisc::MemoryBarrier();
|
|
check(!GIsThreadedRendering);
|
|
FCoreDelegates::PreRenderingThreadDestroyed.Broadcast();
|
|
|
|
#if STATS
|
|
if (FThreadStats::WillEverCollectData())
|
|
{
|
|
FThreadStats::ExplicitFlush(); // Another explicit flush to clean up the ScopeCount established above for any stats lingering since the last frame
|
|
}
|
|
#endif
|
|
|
|
ENamedThreads::RenderThread = ENamedThreads::GameThread;
|
|
ENamedThreads::RenderThread_Local = ENamedThreads::GameThread_Local;
|
|
FPlatformMisc::MemoryBarrier();
|
|
}
|
|
|
|
/**
|
|
* Advances stats for the rendering thread.
|
|
*/
|
|
static void AdvanceRenderingThreadStats(int64 StatsFrame, int32 MasterDisableChangeTagStartFrame)
|
|
{
|
|
#if STATS
|
|
int64 Frame = StatsFrame;
|
|
if (!FThreadStats::IsCollectingData() || MasterDisableChangeTagStartFrame != FThreadStats::MasterDisableChangeTag())
|
|
{
|
|
Frame = -StatsFrame; // mark this as a bad frame
|
|
}
|
|
FThreadStats::AddMessage(FStatConstants::AdvanceFrame.GetEncodedName(), EStatOperation::AdvanceFrameEventRenderThread, Frame);
|
|
if( IsInActualRenderingThread() )
|
|
{
|
|
FThreadStats::ExplicitFlush();
|
|
}
|
|
#endif
|
|
}
|
|
|
|
/**
|
|
* Advances stats for the rendering thread. Called from the game thread.
|
|
*/
|
|
void AdvanceRenderingThreadStatsGT( bool bDiscardCallstack, int64 StatsFrame, int32 MasterDisableChangeTagStartFrame )
|
|
{
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND_TWOPARAMETER
|
|
(
|
|
RenderingThreadTickCommand,
|
|
int64, SentStatsFrame, StatsFrame,
|
|
int32, SentMasterDisableChangeTagStartFrame, MasterDisableChangeTagStartFrame,
|
|
{
|
|
AdvanceRenderingThreadStats( SentStatsFrame, SentMasterDisableChangeTagStartFrame );
|
|
}
|
|
);
|
|
if( bDiscardCallstack )
|
|
{
|
|
// we need to flush the rendering thread here, otherwise it can get behind and then the stats will get behind.
|
|
FlushRenderingCommands();
|
|
}
|
|
}
|
|
|
|
/** The rendering thread runnable object. */
|
|
class FRenderingThread : public FRunnable
|
|
{
|
|
private:
|
|
/** Tracks if we have acquired ownership */
|
|
bool bAcquiredThreadOwnership;
|
|
|
|
public:
|
|
/**
|
|
* Sync event to make sure that render thread is bound to the task graph before main thread queues work against it.
|
|
*/
|
|
FEvent* TaskGraphBoundSyncEvent;
|
|
|
|
FRenderingThread()
|
|
{
|
|
bAcquiredThreadOwnership = false;
|
|
TaskGraphBoundSyncEvent = FPlatformProcess::GetSynchEventFromPool(true);
|
|
RHIFlushResources();
|
|
}
|
|
|
|
virtual ~FRenderingThread()
|
|
{
|
|
FPlatformProcess::ReturnSynchEventToPool(TaskGraphBoundSyncEvent);
|
|
TaskGraphBoundSyncEvent = nullptr;
|
|
}
|
|
|
|
// FRunnable interface.
|
|
virtual bool Init(void) override
|
|
{
|
|
GRenderThreadId = FPlatformTLS::GetCurrentThreadId();
|
|
|
|
// Acquire rendering context ownership on the current thread, unless using an RHI thread, which will be the real owner
|
|
if (!GUseRHIThread)
|
|
{
|
|
bAcquiredThreadOwnership = true;
|
|
RHIAcquireThreadOwnership();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
virtual void Exit(void) override
|
|
{
|
|
// Release rendering context ownership on the current thread if we had acquired it
|
|
if (bAcquiredThreadOwnership)
|
|
{
|
|
bAcquiredThreadOwnership = false;
|
|
RHIReleaseThreadOwnership();
|
|
}
|
|
|
|
GRenderThreadId = 0;
|
|
}
|
|
|
|
#if PLATFORM_WINDOWS && !PLATFORM_SEH_EXCEPTIONS_DISABLED
|
|
static int32 FlushRHILogsAndReportCrash(Windows::LPEXCEPTION_POINTERS ExceptionInfo)
|
|
{
|
|
if (GDynamicRHI)
|
|
{
|
|
GDynamicRHI->FlushPendingLogs();
|
|
}
|
|
|
|
return ReportCrash(ExceptionInfo);
|
|
}
|
|
#endif
|
|
|
|
virtual uint32 Run(void) override
|
|
{
|
|
FMemory::SetupTLSCachesOnCurrentThread();
|
|
FPlatformProcess::SetupRenderThread();
|
|
|
|
#if PLATFORM_WINDOWS
|
|
if ( !FPlatformMisc::IsDebuggerPresent() || GAlwaysReportCrash )
|
|
{
|
|
#if !PLATFORM_SEH_EXCEPTIONS_DISABLED
|
|
__try
|
|
#endif
|
|
{
|
|
RenderingThreadMain( TaskGraphBoundSyncEvent );
|
|
}
|
|
#if !PLATFORM_SEH_EXCEPTIONS_DISABLED
|
|
__except(FlushRHILogsAndReportCrash(GetExceptionInformation()))
|
|
{
|
|
GRenderingThreadError = GErrorHist;
|
|
|
|
// Use a memory barrier to ensure that the game thread sees the write to GRenderingThreadError before
|
|
// the write to GIsRenderingThreadHealthy.
|
|
FPlatformMisc::MemoryBarrier();
|
|
|
|
GIsRenderingThreadHealthy = false;
|
|
}
|
|
#endif
|
|
}
|
|
else
|
|
#endif // PLATFORM_WINDOWS
|
|
{
|
|
RenderingThreadMain( TaskGraphBoundSyncEvent );
|
|
}
|
|
FMemory::ClearAndDisableTLSCachesOnCurrentThread();
|
|
return 0;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* If the rendering thread is in its idle loop (which ticks rendering tickables
|
|
*/
|
|
volatile bool GRunRenderingThreadHeartbeat = false;
|
|
|
|
FThreadSafeCounter OutstandingHeartbeats;
|
|
/** The rendering thread heartbeat runnable object. */
|
|
class FRenderingThreadTickHeartbeat : public FRunnable
|
|
{
|
|
public:
|
|
|
|
// FRunnable interface.
|
|
virtual bool Init(void)
|
|
{
|
|
OutstandingHeartbeats.Reset();
|
|
return true;
|
|
}
|
|
|
|
virtual void Exit(void)
|
|
{
|
|
}
|
|
|
|
virtual void Stop(void)
|
|
{
|
|
}
|
|
|
|
virtual uint32 Run(void)
|
|
{
|
|
while(GRunRenderingThreadHeartbeat)
|
|
{
|
|
FPlatformProcess::Sleep(1.f/(4.0f * GRenderingThreadMaxIdleTickFrequency));
|
|
if (!GIsRenderingThreadSuspended && OutstandingHeartbeats.GetValue() < 4)
|
|
{
|
|
OutstandingHeartbeats.Increment();
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND(
|
|
HeartbeatTickTickables,
|
|
{
|
|
OutstandingHeartbeats.Decrement();
|
|
// make sure that rendering thread tickables get a chance to tick, even if the render thread is starving
|
|
if (!GIsRenderingThreadSuspended)
|
|
{
|
|
TickRenderingTickables();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
};
|
|
|
|
FRunnableThread* GRenderingThreadHeartbeat = NULL;
|
|
FRunnable* GRenderingThreadRunnableHeartbeat = NULL;
|
|
|
|
// not done in the CVar system as we don't access to render thread specifics there
|
|
struct FConsoleRenderThreadPropagation : public IConsoleThreadPropagation
|
|
{
|
|
virtual void OnCVarChange(int32& Dest, int32 NewValue)
|
|
{
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND_TWOPARAMETER(
|
|
OnCVarChange1,
|
|
int32&, Dest, Dest,
|
|
int32, NewValue, NewValue,
|
|
{
|
|
Dest = NewValue;
|
|
});
|
|
}
|
|
|
|
virtual void OnCVarChange(float& Dest, float NewValue)
|
|
{
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND_TWOPARAMETER(
|
|
OnCVarChange2,
|
|
float&, Dest, Dest,
|
|
float, NewValue, NewValue,
|
|
{
|
|
Dest = NewValue;
|
|
});
|
|
}
|
|
|
|
virtual void OnCVarChange(bool& Dest, bool NewValue)
|
|
{
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND_TWOPARAMETER(
|
|
OnCVarChange2,
|
|
bool&, Dest, Dest,
|
|
bool, NewValue, NewValue,
|
|
{
|
|
Dest = NewValue;
|
|
});
|
|
}
|
|
|
|
virtual void OnCVarChange(FString& Dest, const FString& NewValue)
|
|
{
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND_TWOPARAMETER(
|
|
OnCVarChange3,
|
|
FString&, Dest, Dest,
|
|
const FString&, NewValue, NewValue,
|
|
{
|
|
Dest = NewValue;
|
|
});
|
|
}
|
|
|
|
static FConsoleRenderThreadPropagation& GetSingleton()
|
|
{
|
|
static FConsoleRenderThreadPropagation This;
|
|
|
|
return This;
|
|
}
|
|
|
|
};
|
|
|
|
static FString BuildRenderingThreadName( uint32 ThreadIndex )
|
|
{
|
|
return FString::Printf( TEXT( "%s %u" ), *FName( NAME_RenderThread ).GetPlainNameString(), ThreadIndex );
|
|
}
|
|
|
|
|
|
|
|
class FOwnershipOfRHIThreadTask : public FCustomStatIDGraphTaskBase
|
|
{
|
|
public:
|
|
/**
|
|
* Constructor
|
|
* @param StatId The stat id for this task.
|
|
* @param InDesiredThread; Thread to run on, can be ENamedThreads::AnyThread
|
|
**/
|
|
FOwnershipOfRHIThreadTask(bool bInAcquireOwnership, TStatId StatId)
|
|
: FCustomStatIDGraphTaskBase(StatId)
|
|
, bAcquireOwnership(bInAcquireOwnership)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* Retrieve the thread that this task wants to run on.
|
|
* @return the thread that this task should run on.
|
|
**/
|
|
ENamedThreads::Type GetDesiredThread()
|
|
{
|
|
return ENamedThreads::RHIThread;
|
|
}
|
|
|
|
static ESubsequentsMode::Type GetSubsequentsMode() { return ESubsequentsMode::TrackSubsequents; }
|
|
|
|
/**
|
|
* Actually execute the task.
|
|
* @param CurrentThread; the thread we are running on
|
|
* @param MyCompletionGraphEvent; my completion event. Not always useful since at the end of DoWork, you can assume you are done and hence further tasks do not need you as a prerequisite.
|
|
* However, MyCompletionGraphEvent can be useful for passing to other routines or when it is handy to set up subsequents before you actually do work.
|
|
**/
|
|
void DoTask(ENamedThreads::Type CurrentThread, const FGraphEventRef& MyCompletionGraphEvent)
|
|
{
|
|
// note that this task is the first task run on the thread, before GRHIThread is assigned, so we can't check IsInRHIThread()
|
|
|
|
if (bAcquireOwnership)
|
|
{
|
|
GDynamicRHI->RHIAcquireThreadOwnership();
|
|
}
|
|
else
|
|
{
|
|
GDynamicRHI->RHIReleaseThreadOwnership();
|
|
}
|
|
}
|
|
|
|
private:
|
|
bool bAcquireOwnership;
|
|
};
|
|
|
|
|
|
|
|
void StartRenderingThread()
|
|
{
|
|
static uint32 ThreadCount = 0;
|
|
check(!GIsThreadedRendering && GUseThreadedRendering);
|
|
|
|
check(!GRHIThread)
|
|
if (GUseRHIThread)
|
|
{
|
|
FRHICommandListExecutor::GetImmediateCommandList().ImmediateFlush(EImmediateFlushType::DispatchToRHIThread);
|
|
if (!FTaskGraphInterface::Get().IsThreadProcessingTasks(ENamedThreads::RHIThread))
|
|
{
|
|
FRHIThread::Get().Start();
|
|
}
|
|
DECLARE_CYCLE_STAT(TEXT("Wait For RHIThread"), STAT_WaitForRHIThread, STATGROUP_TaskGraphTasks);
|
|
|
|
FGraphEventRef CompletionEvent = TGraphTask<FOwnershipOfRHIThreadTask>::CreateTask(NULL, ENamedThreads::GameThread).ConstructAndDispatchWhenReady(true, GET_STATID(STAT_WaitForRHIThread));
|
|
QUICK_SCOPE_CYCLE_COUNTER(STAT_StartRenderingThread);
|
|
FTaskGraphInterface::Get().WaitUntilTaskCompletes(CompletionEvent, ENamedThreads::GameThread_Local);
|
|
GRHIThread = FRHIThread::Get().Thread;
|
|
check(GRHIThread);
|
|
GRHICommandList.LatchBypass();
|
|
}
|
|
|
|
// Turn on the threaded rendering flag.
|
|
GIsThreadedRendering = true;
|
|
|
|
// Create the rendering thread.
|
|
GRenderingThreadRunnable = new FRenderingThread();
|
|
|
|
GRenderingThread = FRunnableThread::Create(GRenderingThreadRunnable, *BuildRenderingThreadName(ThreadCount), 0, FPlatformAffinity::GetRenderingThreadPriority(), FPlatformAffinity::GetRenderingThreadMask());
|
|
|
|
// Wait for render thread to have taskgraph bound before we dispatch any tasks for it.
|
|
((FRenderingThread*)GRenderingThreadRunnable)->TaskGraphBoundSyncEvent->Wait();
|
|
|
|
// register
|
|
IConsoleManager::Get().RegisterThreadPropagation(GRenderingThread->GetThreadID(), &FConsoleRenderThreadPropagation::GetSingleton());
|
|
|
|
// ensure the thread has actually started and is idling
|
|
FRenderCommandFence Fence;
|
|
Fence.BeginFence();
|
|
Fence.Wait();
|
|
|
|
GRunRenderingThreadHeartbeat = true;
|
|
// Create the rendering thread heartbeat
|
|
GRenderingThreadRunnableHeartbeat = new FRenderingThreadTickHeartbeat();
|
|
|
|
GRenderingThreadHeartbeat = FRunnableThread::Create(GRenderingThreadRunnableHeartbeat, *FString::Printf(TEXT("RTHeartBeat %d"), ThreadCount), 16 * 1024, TPri_AboveNormal, FPlatformAffinity::GetRTHeartBeatMask());
|
|
|
|
ThreadCount++;
|
|
}
|
|
|
|
|
|
void StopRenderingThread()
|
|
{
|
|
// This function is not thread-safe. Ensure it is only called by the main game thread.
|
|
check( IsInGameThread() );
|
|
|
|
// unregister
|
|
IConsoleManager::Get().RegisterThreadPropagation();
|
|
|
|
// stop the render thread heartbeat first
|
|
if (GRunRenderingThreadHeartbeat)
|
|
{
|
|
GRunRenderingThreadHeartbeat = false;
|
|
// Wait for the rendering thread heartbeat to return.
|
|
GRenderingThreadHeartbeat->WaitForCompletion();
|
|
delete GRenderingThreadHeartbeat;
|
|
GRenderingThreadHeartbeat = NULL;
|
|
delete GRenderingThreadRunnableHeartbeat;
|
|
GRenderingThreadRunnableHeartbeat = NULL;
|
|
}
|
|
|
|
if( GIsThreadedRendering )
|
|
{
|
|
// Get the list of objects which need to be cleaned up when the rendering thread is done with them.
|
|
FPendingCleanupObjects* PendingCleanupObjects = GetPendingCleanupObjects();
|
|
|
|
// Make sure we're not in the middle of streaming textures.
|
|
(*GFlushStreamingFunc)();
|
|
|
|
// Wait for the rendering thread to finish executing all enqueued commands.
|
|
FlushRenderingCommands();
|
|
|
|
// The rendering thread may have already been stopped during the call to GFlushStreamingFunc or FlushRenderingCommands.
|
|
if ( GIsThreadedRendering )
|
|
{
|
|
if (GRHIThread)
|
|
{
|
|
DECLARE_CYCLE_STAT(TEXT("Wait For RHIThread Finish"), STAT_WaitForRHIThreadFinish, STATGROUP_TaskGraphTasks);
|
|
FGraphEventRef ReleaseTask = TGraphTask<FOwnershipOfRHIThreadTask>::CreateTask(NULL, ENamedThreads::GameThread).ConstructAndDispatchWhenReady(false, GET_STATID(STAT_WaitForRHIThreadFinish));
|
|
QUICK_SCOPE_CYCLE_COUNTER(STAT_StopRenderingThread_RHIThread);
|
|
FTaskGraphInterface::Get().WaitUntilTaskCompletes(ReleaseTask, ENamedThreads::GameThread_Local);
|
|
GRHIThread = nullptr;
|
|
}
|
|
|
|
check( GRenderingThread );
|
|
check(!GIsRenderingThreadSuspended);
|
|
|
|
// Turn off the threaded rendering flag.
|
|
GIsThreadedRendering = false;
|
|
|
|
{
|
|
FGraphEventRef QuitTask = TGraphTask<FReturnGraphTask>::CreateTask(NULL, ENamedThreads::GameThread).ConstructAndDispatchWhenReady(ENamedThreads::RenderThread);
|
|
|
|
// Busy wait while BP debugging, to avoid opportunistic execution of game thread tasks
|
|
// If the game thread is already executing tasks, then we have no choice but to spin
|
|
if (GIntraFrameDebuggingGameThread || FTaskGraphInterface::Get().IsThreadProcessingTasks(ENamedThreads::GameThread) )
|
|
{
|
|
while ((QuitTask.GetReference() != nullptr) && !QuitTask->IsComplete())
|
|
{
|
|
FPlatformProcess::Sleep(0.0f);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
QUICK_SCOPE_CYCLE_COUNTER(STAT_StopRenderingThread);
|
|
FTaskGraphInterface::Get().WaitUntilTaskCompletes(QuitTask, ENamedThreads::GameThread_Local);
|
|
}
|
|
}
|
|
|
|
// Wait for the rendering thread to return.
|
|
GRenderingThread->WaitForCompletion();
|
|
|
|
// Destroy the rendering thread objects.
|
|
delete GRenderingThread;
|
|
GRenderingThread = NULL;
|
|
|
|
GRHICommandList.LatchBypass();
|
|
|
|
delete GRenderingThreadRunnable;
|
|
GRenderingThreadRunnable = NULL;
|
|
}
|
|
|
|
// Delete the pending cleanup objects which were in use by the rendering thread.
|
|
delete PendingCleanupObjects;
|
|
}
|
|
|
|
check(!GRHIThread);
|
|
}
|
|
|
|
void CheckRenderingThreadHealth()
|
|
{
|
|
if(!GIsRenderingThreadHealthy)
|
|
{
|
|
GErrorHist[0] = 0;
|
|
GIsCriticalError = false;
|
|
UE_LOG(LogRendererCore, Fatal,TEXT("Rendering thread exception:\r\n%s"),*GRenderingThreadError);
|
|
}
|
|
|
|
|
|
if (IsInGameThread())
|
|
{
|
|
if (!GIsCriticalError)
|
|
{
|
|
GLog->FlushThreadedLogs();
|
|
}
|
|
#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
|
|
TGuardValue<bool> GuardMainThreadBlockedOnRenderThread(GMainThreadBlockedOnRenderThread,true);
|
|
#endif
|
|
SCOPE_CYCLE_COUNTER(STAT_PumpMessages);
|
|
FPlatformMisc::PumpMessages(false);
|
|
}
|
|
}
|
|
|
|
bool IsRenderingThreadHealthy()
|
|
{
|
|
return GIsRenderingThreadHealthy;
|
|
}
|
|
|
|
static FGraphEventRef BundledCompletionEvent;
|
|
static FGraphEventRef BundledCompletionEventPrereq; // We fire this when we are done, which queues the actual fence
|
|
|
|
void StartRenderCommandFenceBundler()
|
|
{
|
|
if (!GIsThreadedRendering)
|
|
{
|
|
return;
|
|
}
|
|
|
|
check(IsInGameThread() && !BundledCompletionEvent.GetReference() && !BundledCompletionEventPrereq.GetReference()); // can't use this in a nested fashion
|
|
BundledCompletionEventPrereq = FGraphEvent::CreateGraphEvent();
|
|
|
|
FGraphEventArray Prereqs;
|
|
Prereqs.Add(BundledCompletionEventPrereq);
|
|
|
|
DECLARE_CYCLE_STAT(TEXT("FNullGraphTask.FenceRenderCommandBundled"),
|
|
STAT_FNullGraphTask_FenceRenderCommandBundled,
|
|
STATGROUP_TaskGraphTasks);
|
|
|
|
BundledCompletionEvent = TGraphTask<FNullGraphTask>::CreateTask(&Prereqs, ENamedThreads::GameThread).ConstructAndDispatchWhenReady(
|
|
GET_STATID(STAT_FNullGraphTask_FenceRenderCommandBundled), ENamedThreads::RenderThread);
|
|
|
|
StartBatchedRelease();
|
|
}
|
|
|
|
void StopRenderCommandFenceBundler()
|
|
{
|
|
if (!GIsThreadedRendering || !BundledCompletionEvent.GetReference())
|
|
{
|
|
return;
|
|
}
|
|
|
|
EndBatchedRelease();
|
|
check(IsInGameThread() && BundledCompletionEvent.GetReference() && !BundledCompletionEvent->IsComplete() && BundledCompletionEventPrereq.GetReference() && !BundledCompletionEventPrereq->IsComplete()); // can't use this in a nested fashion
|
|
TArray<FBaseGraphTask*> NewTasks;
|
|
BundledCompletionEventPrereq->DispatchSubsequents(NewTasks);
|
|
BundledCompletionEventPrereq = nullptr;
|
|
BundledCompletionEvent = nullptr;
|
|
}
|
|
|
|
void FRenderCommandFence::BeginFence()
|
|
{
|
|
if (!GIsThreadedRendering)
|
|
{
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
if (BundledCompletionEvent.GetReference() && IsInGameThread())
|
|
{
|
|
CompletionEvent = BundledCompletionEvent;
|
|
return;
|
|
}
|
|
|
|
DECLARE_CYCLE_STAT(TEXT("FNullGraphTask.FenceRenderCommand"),
|
|
STAT_FNullGraphTask_FenceRenderCommand,
|
|
STATGROUP_TaskGraphTasks);
|
|
|
|
CompletionEvent = TGraphTask<FNullGraphTask>::CreateTask(NULL, ENamedThreads::GameThread).ConstructAndDispatchWhenReady(
|
|
GET_STATID(STAT_FNullGraphTask_FenceRenderCommand), ENamedThreads::RenderThread);
|
|
}
|
|
}
|
|
|
|
bool FRenderCommandFence::IsFenceComplete() const
|
|
{
|
|
if (!GIsThreadedRendering)
|
|
{
|
|
return true;
|
|
}
|
|
check(IsInGameThread() || IsInAsyncLoadingThread());
|
|
CheckRenderingThreadHealth();
|
|
if (!CompletionEvent.GetReference() || CompletionEvent->IsComplete())
|
|
{
|
|
CompletionEvent = NULL; // this frees the handle for other uses, the NULL state is considered completed
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/** How many cycles the gamethread used (excluding idle time). It's set once per frame in FViewport::Draw. */
|
|
uint32 GGameThreadTime = 0;
|
|
/** How many cycles it took to swap buffers to present the frame. */
|
|
uint32 GSwapBufferTime = 0;
|
|
|
|
static int32 GTimeToBlockOnRenderFence = 1;
|
|
static FAutoConsoleVariableRef CVarTimeToBlockOnRenderFence(
|
|
TEXT("g.TimeToBlockOnRenderFence"),
|
|
GTimeToBlockOnRenderFence,
|
|
TEXT("Number of milliseconds the game thread should block when waiting on a render thread fence.")
|
|
);
|
|
|
|
|
|
static int32 GTimeoutForBlockOnRenderFence = 30000;
|
|
static FAutoConsoleVariableRef CVarTimeoutForBlockOnRenderFence(
|
|
TEXT("g.TimeoutForBlockOnRenderFence"),
|
|
GTimeoutForBlockOnRenderFence,
|
|
TEXT("Number of milliseconds the game thread should wait before failing when waiting on a render thread fence.")
|
|
);
|
|
|
|
/**
|
|
* Block the game thread waiting for a task to finish on the rendering thread.
|
|
*/
|
|
static void GameThreadWaitForTask(const FGraphEventRef& Task, bool bEmptyGameThreadTasks = false)
|
|
{
|
|
SCOPE_TIME_GUARD(TEXT("GameThreadWaitForTask"));
|
|
|
|
check(IsInGameThread());
|
|
check(IsValidRef(Task));
|
|
|
|
if (!Task->IsComplete())
|
|
{
|
|
SCOPE_CYCLE_COUNTER(STAT_GameIdleTime);
|
|
{
|
|
static int32 NumRecursiveCalls = 0;
|
|
|
|
// Check for recursion. It's not completely safe but because we pump messages while
|
|
// blocked it is expected.
|
|
NumRecursiveCalls++;
|
|
if (NumRecursiveCalls > 1)
|
|
{
|
|
UE_LOG(LogRendererCore,Warning,TEXT("FlushRenderingCommands called recursively! %d calls on the stack."), NumRecursiveCalls);
|
|
}
|
|
if (NumRecursiveCalls > 1 || FTaskGraphInterface::Get().IsThreadProcessingTasks(ENamedThreads::GameThread))
|
|
{
|
|
bEmptyGameThreadTasks = false; // we don't do this on recursive calls or if we are at a blueprint breakpoint
|
|
}
|
|
|
|
// Grab an event from the pool and fire off a task to trigger it.
|
|
FEvent* Event = FPlatformProcess::GetSynchEventFromPool();
|
|
FTaskGraphInterface::Get().TriggerEventWhenTaskCompletes(Event, Task, ENamedThreads::GameThread);
|
|
|
|
// Check rendering thread health needs to be called from time to
|
|
// time in order to pump messages, otherwise the RHI may block
|
|
// on vsync causing a deadlock. Also we should make sure the
|
|
// rendering thread hasn't crashed :)
|
|
bool bDone;
|
|
uint32 WaitTime = FMath::Clamp<uint32>(GTimeToBlockOnRenderFence, 0, 33);
|
|
|
|
const double StartTime = FPlatformTime::Seconds();
|
|
const double EndTime = StartTime + (GTimeoutForBlockOnRenderFence / 1000.0);
|
|
|
|
do
|
|
{
|
|
CheckRenderingThreadHealth();
|
|
if (bEmptyGameThreadTasks)
|
|
{
|
|
// process gamethread tasks if there are any
|
|
FTaskGraphInterface::Get().ProcessThreadUntilIdle(ENamedThreads::GameThread);
|
|
}
|
|
bDone = Event->Wait(WaitTime);
|
|
|
|
// editor threads can block for quite a while...
|
|
if (!bDone && !WITH_EDITOR && !FPlatformMisc::IsDebuggerPresent())
|
|
{
|
|
static bool bDisabled = FParse::Param(FCommandLine::Get(), TEXT("nothreadtimeout"));
|
|
|
|
// Fatal timeout if we run out of time and this thread is being monitor for heartbeats
|
|
// (We could just let the heartbeat monitor error for us, but this leads to better diagnostics).
|
|
if (FPlatformTime::Seconds() >= EndTime && FThreadHeartBeat::Get().IsBeating() && !bDisabled)
|
|
{
|
|
UE_LOG(LogRendererCore, Fatal, TEXT("GameThread timed out waiting for RenderThread after %.02f secs"), FPlatformTime::Seconds() - StartTime);
|
|
}
|
|
}
|
|
}
|
|
while (!bDone);
|
|
|
|
// Return the event to the pool and decrement the recursion counter.
|
|
FPlatformProcess::ReturnSynchEventToPool(Event);
|
|
Event = nullptr;
|
|
NumRecursiveCalls--;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Waits for pending fence commands to retire.
|
|
*/
|
|
void FRenderCommandFence::Wait(bool bProcessGameThreadTasks) const
|
|
{
|
|
if (!IsFenceComplete())
|
|
{
|
|
StopRenderCommandFenceBundler();
|
|
#if 0
|
|
// on most platforms this is a better solution because it doesn't spin
|
|
// windows needs to pump messages
|
|
if (bProcessGameThreadTasks)
|
|
{
|
|
QUICK_SCOPE_CYCLE_COUNTER(STAT_FRenderCommandFence_Wait);
|
|
FTaskGraphInterface::Get().WaitUntilTaskCompletes(CompletionEvent, ENamedThreads::GameThread);
|
|
}
|
|
#endif
|
|
GameThreadWaitForTask(CompletionEvent, bProcessGameThreadTasks);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* List of tasks that must be completed before we start a render frame
|
|
* Note, normally, you don't need the render command themselves in this list workers that queue render commands are usually sufficient
|
|
*/
|
|
static FCompletionList FrameRenderPrerequisites;
|
|
|
|
/**
|
|
* Adds a task that must be completed either before the next scene draw or a flush rendering commands
|
|
* Note, normally, you don't need the render command themselves in this list workers that queue render commands are usually sufficient
|
|
* @param TaskToAdd, task to add as a pending render thread task
|
|
*/
|
|
void AddFrameRenderPrerequisite(const FGraphEventRef& TaskToAdd)
|
|
{
|
|
FrameRenderPrerequisites.Add(TaskToAdd);
|
|
}
|
|
|
|
/**
|
|
* Gather the frame render prerequisites and make sure all render commands are at least queued
|
|
*/
|
|
void AdvanceFrameRenderPrerequisite()
|
|
{
|
|
checkSlow(IsInGameThread());
|
|
FGraphEventRef PendingComplete = FrameRenderPrerequisites.CreatePrerequisiteCompletionHandle(ENamedThreads::GameThread);
|
|
if (PendingComplete.GetReference())
|
|
{
|
|
GameThreadWaitForTask(PendingComplete);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Waits for the rendering thread to finish executing all pending rendering commands. Should only be used from the game thread.
|
|
*/
|
|
void FlushRenderingCommands()
|
|
{
|
|
if (!GIsRHIInitialized)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND(
|
|
FlushPendingDeleteRHIResources,
|
|
{
|
|
GRHICommandList.GetImmediateCommandList().ImmediateFlush(EImmediateFlushType::FlushRHIThreadFlushResources);
|
|
}
|
|
);
|
|
|
|
AdvanceFrameRenderPrerequisite();
|
|
|
|
// Find the objects which may be cleaned up once the rendering thread command queue has been flushed.
|
|
FPendingCleanupObjects* PendingCleanupObjects = GetPendingCleanupObjects();
|
|
|
|
// Issue a fence command to the rendering thread and wait for it to complete.
|
|
FRenderCommandFence Fence;
|
|
Fence.BeginFence();
|
|
Fence.Wait();
|
|
|
|
// Delete the objects which were enqueued for deferred cleanup before the command queue flush.
|
|
delete PendingCleanupObjects;
|
|
}
|
|
|
|
void FlushPendingDeleteRHIResources_GameThread()
|
|
{
|
|
if (!GRHIThread)
|
|
{
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND(
|
|
FlushPendingDeleteRHIResources,
|
|
{
|
|
FlushPendingDeleteRHIResources_RenderThread();
|
|
}
|
|
);
|
|
}
|
|
}
|
|
void FlushPendingDeleteRHIResources_RenderThread()
|
|
{
|
|
if (!GRHIThread)
|
|
{
|
|
FRHIResource::FlushPendingDeletes();
|
|
}
|
|
}
|
|
|
|
|
|
FRHICommandListImmediate& GetImmediateCommandList_ForRenderCommand()
|
|
{
|
|
return FRHICommandListExecutor::GetImmediateCommandList();
|
|
}
|
|
|
|
/** The set of deferred cleanup objects which are pending cleanup. */
|
|
static TLockFreePointerListUnordered<FDeferredCleanupInterface, PLATFORM_CACHE_LINE_SIZE> PendingCleanupObjectsList;
|
|
|
|
FPendingCleanupObjects::FPendingCleanupObjects()
|
|
{
|
|
check(IsInGameThread());
|
|
PendingCleanupObjectsList.PopAll(CleanupArray);
|
|
}
|
|
|
|
FPendingCleanupObjects::~FPendingCleanupObjects()
|
|
{
|
|
QUICK_SCOPE_CYCLE_COUNTER(STAT_FPendingCleanupObjects_Destruct);
|
|
|
|
for(int32 ObjectIndex = 0;ObjectIndex < CleanupArray.Num();ObjectIndex++)
|
|
{
|
|
CleanupArray[ObjectIndex]->FinishCleanup();
|
|
}
|
|
}
|
|
|
|
void BeginCleanup(FDeferredCleanupInterface* CleanupObject)
|
|
{
|
|
PendingCleanupObjectsList.Push(CleanupObject);
|
|
}
|
|
|
|
FPendingCleanupObjects* GetPendingCleanupObjects()
|
|
{
|
|
return new FPendingCleanupObjects;
|
|
}
|
|
|
|
void SetRHIThreadEnabled(bool bEnable)
|
|
{
|
|
if (bEnable != GUseRHIThread)
|
|
{
|
|
if (GRHISupportsRHIThread)
|
|
{
|
|
if (!GIsThreadedRendering)
|
|
{
|
|
check(!GRHIThread);
|
|
UE_LOG(LogRendererCore, Display, TEXT("Can't switch to RHI thread mode when we are not running a multithreaded renderer."));
|
|
}
|
|
else
|
|
{
|
|
StopRenderingThread();
|
|
GUseRHIThread = bEnable;
|
|
StartRenderingThread();
|
|
}
|
|
UE_LOG(LogRendererCore, Display, TEXT("RHIThread is now %s."), GRHIThread ? TEXT("active") : TEXT("inactive"));
|
|
}
|
|
else
|
|
{
|
|
UE_LOG(LogRendererCore, Display, TEXT("This RHI does not support the RHI thread."));
|
|
}
|
|
}
|
|
}
|
|
|
|
static void HandleRHIThreadEnableChanged(const TArray<FString>& Args)
|
|
{
|
|
if (Args.Num() > 0)
|
|
{
|
|
const bool bUseRHIThread = Args[0].ToBool();
|
|
SetRHIThreadEnabled(bUseRHIThread);
|
|
}
|
|
else
|
|
{
|
|
UE_LOG(LogRendererCore, Display, TEXT("Usage: r.RHIThread.Enable 0/1; Currently %d"), (int32)GUseRHIThread);
|
|
}
|
|
}
|
|
|
|
static FAutoConsoleCommand CVarRHIThreadEnable(
|
|
TEXT("r.RHIThread.Enable"),
|
|
TEXT("Enables/disabled the RHI Thread\n"),
|
|
FConsoleCommandWithArgsDelegate::CreateStatic(&HandleRHIThreadEnableChanged)
|
|
);
|