Files
UnrealEngineUWP/Engine/Source/Runtime/RenderCore/Private/RenderingThread.cpp
Bob Tellez c3818ebea9 Merging from //UE4/Fortnite-Staging up to CL#3673800 based on CL#3664064 from //Fortnite/Main
#rb none
#lockdown Nick.Penwarden

=================================================================================================
THESE CHANGES TOUCH MULTIPLE PLATFORMS AND/OR RESTRICTED FOLDERS.
YOU MUST REVIEW THESE MANUALLY AND APPEND THEM TO THE DESCRIPTIONS FOR THE APPROPRIATE PLATFORMS.
=================================================================================================

Change 3662267 by Nick.Darnell

	Engine - Fixing a bug in GetAccurateRealTime, it wasn't subtracting GStartTime, which if you don't prevents accurate platform time when you try to store it in a float.

	#jira nojira

Change 3662176 by Ben.Marsh

	Disable image integrity report generation if a debugger is attached, and in editor builds.

	#jira FORT-55656

Change 3656958 by Luke.Thatcher

	[FORTNITE] [CONSOLE] [+] Improved frame syncing mechanism
	 - Improves input latency by allowing the game thread to sync to the swap chain flip of the previous frame.
	 - Added "r.GTSyncType" CVar to control how the game thread syncs with the rest of the pipe.
	 - r.GTSyncType 2 will sync the game thread with the flip of the swap chain, preventing the pipe from getting too long and causing excess input latency.

	Platforms are required to implement RHIWaitForFlip and RHISignalFlipEvent, and call RHIInitializeFlipTracking on RHI startup.
	A separate thread monitors the progress of frame flips and signals task graph events as they pass their corresponding frame index.
	In r.GTSyncType 2 mode, the game thread is signaled by this flip tracking thread.

	[~] Unified platform specific sync interval CVars (D3D12.SyncInterval, D3D11.SyncInterval, r.PS4FlipRate, RHI.SyncIntervalOgl) into one: rhi.SyncInterval
	 - 1 == 60Hz
	 - 2 == 30Hz
	 - 3 == 20Hz

	[-] Removed large number in XboxOneTime. Adding this arbitrary number prevents us from comparing timestamps from FPlatformTime::Seconds() and various OS callbacks (e.g. flip timings).

	#jira FORT-50803

Change 3655598 by Lukasz.Furman

	added filtering for navmesh's low height spans to fix crash on layer partitioning
	% of span reductions depends on presence of stair or roof building in navmesh tile, changed failsafes in layer code to ignore entire tile if heightfield is too complex to partition instead of reallocating memory

	#jira FORT-35375

Change 3648972 by Keith.Judge

	Add analytics to help diagnose default parameter collection buffer issue.

	+++ REMOVE ONCE CORE ISSUE IS SOLVED +++

	#jira FORT-54690

Change 3648756 by Bart.Hawthorne

	Integrate 3645298 from //UE4/Dev-Networking

	Deprecate GetNetworkObjectInfo in favor of separate FindNetworkObjectInfo and FindOrCreateNetworkObjectInfo methods.

	#jira none

Change 3643090 by Josh.Markiewicz

	#UE4 - proper handling of "pending connection lost"
	- triggered only if a connection is lost and there no "owning actor" to deal with the connection loss
	-- added Rejoin and CleanedUp states to connection to make sure that the pending connection lost delegate only fires at the appropriate time
	- delegate returns the unique id of the player if known (still possible to be unknown if connection lost after NMT_Hello)

	- changed debug output on timeout if the net connection was already in the process of being destroyed
	-- occurs when game hitches during the pending destroy 2 second wait
	-- ReceivedAcks should have been called to clean things up quietly in those 2 seconds but blocking the game thread will cause the cleanup to look like a timeout

	- added userid to UNetConnection::Describe
	- bad split screen player handling of unique id
	-- splitscreen uniqueid was overwriting the primary player id
	-- only store the id on the child connection

	- added some clarifying comments

	#review-3642816 @ryan.gerleve, @bob.tellez, @sam.zamani, @bart.hawthorne, @dave.ratti
	#jira FORT-26776

Change 3639043 by Alex.Thurman

	Fix CommonTreeView SetSelection to correctly update list navigation, and behave similarly to CommonListView's SetSelectedItem.

	#JIRA FORT-45841

Change 3632275 by Seth.Weedin

	#JIRA FORT-54203 - Add clamps to ActiveSound fade interpolation to prevent unwanted volume spikes. Remove 0.01 start time for single-fire audio cues. Should remove the sudden pops sometimes heard when firing weapons, as well as smooth out fade volume in general.

Change 3626944 by Josh.Markiewicz

	#UE4 - added "updates connection status" flag to ServiceConfigMCP
	- disable updates on Cloud and Friend services (Fortnite only)
	- removed overloaded ProcessConnectionStatus function in cloud service
	#jira FORT-53113

Change 3626226 by Stewart.Lynch

	LLM Update - Memory reductions, Summary page, enum scopes, refactor and cleanup of tags

	* Remove all static arrays and hard limits from LLM. Everything is now dynamically allocated using the internal LLM allocators. The overhead when LLM is disabled is now only 48K (was 40MB)
	* re-wrote LLMMap. Now stores an int32 index rather then pointer in the HashMap array. Also, changed the Values to be arrays for structs instead of structs of arrays. Means that the tag can be stored in a single byte. Changed the size of the allocation size from int64 to int32. All this takes the memory down from around 600MB to 100MB. It was 120 bytes per allocation, now 29 bytes.
	* changed all LLM scopes over to enums. This has a number of benefits; LLM can be enable in Test, less CPU overhead, stored in a byte (LLM overhead /= 8)
	* summary page for content creators where all lower-level stats are grouped under one Engine stat
	* renamed ELLMScopeTag enum to ELLMTag
	* renamed LLM_SCOPED_TAG_WITH_ENUM macro to LLM_SCOPE
	* removed Tracker arg from LLM_SCOPE and added LLM_PLATFORM_SCOPE macro
	* fixed GenericPlatformMallocCrash stat. Although it seems not be be used anymore
	* fixed BackupOOMMemoryPool stat (now shows in both default and platform pages)
	* added separate LLM enums for XB1, PS4 and D3D12 (PS4LLM.cpp/h etc.)
	* lots of changes adding/removing/renaming tags
	* added LLMArray and FLLMObjectAllocator classes
	* disabled asset tag tracking by default because it takes up so much memory even when not used
	* enable LLM in all non-shipping builds. In Test the on screendisplay won't show because it uses the stats system but it till still write out the csv.
	* all the stat macros have been left as they were and can be enabled on the LLM_STAT_TAGS_ENABLED define. These are needed for the asset tagging.
	* disabled LLM_TRACK_PEAK_MEMORY because there is a problem with the way it adds the peaks for multiple threads. This needs to be fixed.
	* added a CVar to control the csv write interval: LLM.LLMWriteInterval
	* added static arrays for the enum tags setup. Easier to manage and removes need for slow switch statements.
	* renamed FLLMThreadStateManager to FLLMTracker to make it consistent with the enum
	* fixed program size stat which was broken recently on PS4. This was due to initialisation order and global platform stats setup

	#jira NONE-01

Change 3622978 by Lukasz.Furman

	changed WeaponStatus BT decorator to be event driven, fixes AI trying to check ranged weapon abilities without valid weapon
	includes copy of CL# 3620700

	#jira FORT-45914
	#review-3622979 John.Abercrombie

Change 3622340 by Josh.Markiewicz

	#UE4 - playerid netconnection variable setup properly on clients and servers for both beacons and game net drivers
	- ipconnection prints uniqueid with lowleveldescribe

	#jira fort-0

Change 3621386 by Tim.Tillotson

	Add the ability to retry HTTP operations by VERB. This allows us to automatically retry cloud save PUT operations.
	#JIRA FORT-53717
	#review-3621317 @Josh.Markiewicz @Ian.Fox @Carlos.Cuello

Change 3620517 by Keith.Judge

	Xbox One - Revert iOS behaviour for the depth bias back to how it was, and make separate XB1 change use its own define to avoid confusion.

	#jira FORT-53928

Change 3620248 by Lukasz.Furman

	changed behavior of UBTTask_MoveTo.bStopOnOverlap flag after recent AcceptanceRadius fix, updated comments to be more detailed
	#jira nojira

Change 3616187 by Bob.Tellez

	#UE4 Throwing an error (for now) if you attempt to use both the malloc profiler and leak detection at the same time since it causes a deadlock.

	#JIRA UE-0

Change 3613935 by Peter.Knepley

	More logging on update launcher launching

	#jira nojira
	#robomerge rp rn

Change 3613537 by Marcus.Wassmer

	Safety asserts around the MarkPendingKill feature for rendering classes.
	#jira FORT-50385

Change 3613399 by Arne.Schober

	Extended ShowMaterialDrawEvents to enable it only in very specific passes and default enabled Depth for Fortnite on PS4 to track down a crash.
	#RB Marcus.Wassmer
	#jira FORT-53610

Change 3610794 by robomerge

	#ROBOMERGE-AUTHOR: marc.audy
	Reduce UMG class memory
	#jira UE-52043

	#ROBOMERGE-SOURCE: CL 3610792 in //Fortnite/Release-Prep/...
	#ROBOMERGE-BOT: FORTNITE (Release-Prep -> Main)

Change 3610144 by Stewart.Lynch

	General LLM improvements

	* added tracking for misc task graph tasks (moves 20MB out of Untagged)
	* renamed EngineTick to EngineMisc
	* added tracking for FName
	* added tracking for GC_ProcessObjectArray potential leak
	* renamed index & vertex buffers stat to Meshes
	* added hooks for MemPro to track allocations from a single category. Currently defined out. I haven't added MemPro.cpp/h.
	* removed AVAILABLE_PHYSICAL stat from LLM csv
	* csv files now include the date in the filename
	* fixed potential threading bug when reading stat values to csv
	* made IsDebugMemoryEnabled() always return false in shipping and if not runnong on a dev-kit (PS4). The reason is that the function is a bit hacky, and should only be used for debug purposes, such as displaying the on screen warning.
	* added lots more scopes
	* started changing Stat scopes to enum scopes. Stat scopes will be phased out.
	* added tracking of FName memory
	* added llmplatform tracking for XBoxSymbols
	* added llm tracking for CPU symbol allocations (20MB)
	* wrote an allocator for XBoxOneStack reading so that it doesn't go through Malloc and get tracked by LLM.
	* added tracking for GC
	* fixed tracking for TransientMemoryAllocator
	* added tracking for networking memory
	* added more audio memory tracking
	* added tracking for blueprints
	* added tracking for static meshes
	* show on screen warning if debug memory is enabled
	* added tracking for particles
	* renamed Phys to PhysX and added more scopes
	* renamed Slate to UI and added more scopes
	* much better coverage of networking memory
	* improved coverage of audio

	#jira FORT-53420

Change 3610136 by robomerge

	#ROBOMERGE-AUTHOR: marc.audy
	Reduce size of UStaticMeshComponent by 224 bytes (cumulative, 64 bytes exclusive)
	Reduce size of UPrimitiveComponent by 176 bytes (cumulative, 64 bytes exclusive).
	Reduce size of USceneComponent by 112 bytes.
	Reduce size of FLightingChannels from 3 bytes to 1.
	Reduce size of FBodyInstance by 16 bytes.

	#jira FORT-52043

	#ROBOMERGE-SOURCE: CL 3610134 in //Fortnite/Release-Prep/...
	#ROBOMERGE-BOT: FORTNITE (Release-Prep -> Main)

Change 3607937 by robomerge

	#ROBOMERGE-AUTHOR: paul.moore
	#jira FORT-53105
	- Fix websocket not providing information when the peer closes the connection.

	#ROBOMERGE-SOURCE: CL 3607933 in //Fortnite/Release-Prep/...
	#ROBOMERGE-BOT: FORTNITE (Release-Prep -> Main)

Change 3607042 by Bart.Hawthorne

	Move replay.Loop functionality into the demo net driver and rename it demo.Loop

	#jira none

Change 3605448 by robomerge

	#ROBOMERGE-AUTHOR: seth.weedin
	#Athena - Pass owner to ActiveSounds created using PlaySoundAtLocation/PlaySound2D to allow "Limit to Owner" concurrency rules to work. Hook up for weapon sounds. #JIRA FORT-53180

	#ROBOMERGE-SOURCE: CL 3605443 in //Fortnite/Release-Prep/...
	#ROBOMERGE-BOT: FORTNITE (Release-Prep -> Main)

Change 3604787 by robomerge

	#ROBOMERGE-AUTHOR: mike.fricker
	Initial support for hotfixing live assets from .ini files
	- This allows clients and server to patch certain assets in memory whenever .ini file hotfixes are downloaded
	- Only CurveTables and DataTables are supported for now
	- The new asset content must be in Json format, the same format the editor uses for importing
	- Assets that are hotfixed will be synchronously loaded if they're not already in memory.  They'll be retained in memory afterwards.
	- IMPORTANT: Json data must be supplied on a single line, and all double quotes must be escaped!

	- The changes must go in the Game.ini file and use the following syntax:

	          [AssetHotfix]
	          +CurveTable=("/Game/Folder/MyCurveTable","[{\"Name\":\"Default\"}]")
	          +DataTable=("/Game/Folder2/MyDataTable","[{\"Name\":\"Foo\"}]")

	#jira FORT-52099
	[CODEREVIEW] frank.gigliotti
	[FYI] peter.knepley,bob.tellez

	#ROBOMERGE-SOURCE: CL 3604784 in //Fortnite/Release-Prep/...
	#ROBOMERGE-BOT: FORTNITE (Release-Prep -> Main)

Change 3602067 by robomerge

	#ROBOMERGE-AUTHOR: mike.fricker
	Loading time improvements
	- This shaves off up to 10 seconds of load time on PS4 in Athena

	Details:
	- Fixed multiple sub-levels not being able to be enqueued for loading in a single client frame.  Athena has ~300 sub-levels, so this ended up wasting up many seconds.
	- Fixed 3D world being rendered while loading (frees up game thread cycles for throttled streaming)
	- UWorld::AllowLevelLoadRequests() was not allowing load requests to go through while an async load was in progress and the match had started.  It now allows this as long as the world isn't being rendered (loading screen.)
	- Eliminated extra 2 second delay before loading screen is dismissed (in Athena only)

	- Note:  A side effect of this change is that the progress bar may not update as smoothly on loading screen.  We'll look at tuning the throttle settings if it ends up being a problem.

	[CODEREVIEW] ori.cohen
	#jira AT-1477

	#ROBOMERGE-SOURCE: CL 3602061 in //Fortnite/Release-Prep/...
	#ROBOMERGE-BOT: FORTNITE (Release-Prep -> Main)

Change 3601951 by Luke.Thatcher

	[FORTNITE] [PS4] [!] Fix memory stats in the old PS4 memory system
	 - Physical memory stat now includes garlic and onion allocations. CPU OOMs will result in "AvailablePhysical" being close to 0.
	 - Added garlic, onion and defrag stats to the platform memory stats struct.
	 - Added fixed pool sizes to platform memory stats.
	 - Modified the Fortnite heartbeat logging to include extra details of PS4 fixed sized pools.

	#jira FORT-52910

Change 3600340 by robomerge

	#ROBOMERGE-AUTHOR: wes.hunt
	All Fort analytics events now contain a GameState attribute indicating the active GameState ClassName when the event is sent.

	Added some new context to crashreporter to help identify Athena matches near and long term.
	* Near Term: GameNameSuffix - set via FCoreDelegates::CrashOverrideParamsChanged
	  * Added bools to the params to indicate WHICH ones are changing
	  * Allows you to set only some values, and clear them out.
	  * Hooked up in FortGameState::PostInitializeComponents.
	  * FortGameState clears it (for returning to main menu).
	  * FortGameStateAthena sets it (for going into an Athena match).
	  * Only does this when it's a true GameMode GameState instance (ie, not PIE) so PIE crashes aren't modified.
	* Long Term: GameStateName - set via FCoreDelegates::GameStateClassChanged.
	  * This works for ANY crash on ANY game.
	  * Hooked up in GameState::HandleMatchIsWaitingToStart.

	#jira AT-1457
	#jira AT-519
	[CODEREVIEW] peter.knepley,josh.markiewicz

	#ROBOMERGE-SOURCE: CL 3600278 in //Fortnite/Release-Prep/...
	#ROBOMERGE-BOT: FORTNITE (Release-Prep -> Main)

Change 3597593 by Ben.Zeigler

	#jira FORT-50722
	Fix issues where AssetBundles weren't being correctly updated during cook, which is blocking both Noland and Abercrombie
	Partial copy of CL #3402335 and #3526538
	#robomerge rp, rn

Change 3597577 by Luke.Thatcher

	[FORTNITE] [PS4] [~] Modified the way memory is allocated on Playstation to make more memory available to the CPU.
	 - Previously the amount of texture memory wasn╞t fixed due to the way the defrag memory is allocated on PS4. This meant we had to have a significant amount of slack.
	 - With the new configuration, we have a guaranteed texture memory pool, so the slack can be significantly smaller, meaning we can give more memory to the CPU, which is where most of our memory pressure is.

	#jira FORT-50825
	#jira FORT-49688
	#jira FORT-49695
	#jira FORT-50054

Change 3596556 by robomerge

	#ROBOMERGE-AUTHOR: mike.fricker
	Enable GC clustering for actors and blueprints in Fortnite
	- This shaves off about 10 ms on GC frames in Athena on PS4 (~52 ms -> 42 ms)
	- Clustering doesn't work on building actors because they're very dynamic, but general Fort static meshes and blueprints are clustered!
	- This gets us into the realm of shippability on console for very large UObject counts

	[FYI] bob.tellez,peter.knepley,michael.noland
	#jira AT-1440

	#ROBOMERGE-SOURCE: CL 3596552 in //Fortnite/Release-Prep/...
	#ROBOMERGE-BOT: FORTNITE (Release-Prep -> Main)

Change 3593994 by robomerge

	#ROBOMERGE-AUTHOR: mike.fricker
	Force largest distance field atlas size in Athena
	- We now force the largest distance field atlas size before preloading Athena content (512x512x1024 = 256 MB).  This helps with load times because it's expensive to re-create this texture on consoles, and typically it gets resized over a dozen times.
	- Added new CVar "r.DistanceFields.ForceMaxAtlasSize" (defaults to zero)
	- Important:  Currently we never "reset" this atlas texture.  This will be a problem when going back to play Campaigns after preloading to play Athena.  I will look into this soon!

	[CODEREVIEW] peter.knepley,marcus.wassmer,michael.noland,daniel.wright
	#jira AT-1477

	#ROBOMERGE-SOURCE: CL 3593992 in //Fortnite/Release-Prep/...
	#ROBOMERGE-BOT: FORTNITE (Release-Prep -> Main)

Change 3592096 by robomerge

	#ROBOMERGE-AUTHOR: ben.salem
	Prototype of gauntlet memory soak test. Not fully fiinished, but want changes in tonight's cook so we can experiment on cooked build tomorrow morning.
	#jira FORT-0

	#ROBOMERGE-SOURCE: CL 3592025 in //Fortnite/Release-Prep/...
	#ROBOMERGE-BOT: FORTNITE (Release-Prep -> Main)

Change 3592085 by robomerge

	#ROBOMERGE-AUTHOR: mike.fricker
	HLOD: Added support for a fixed distance override via CVar
	- Use this to force all HLODs to transition at a specific distance, regardless of their TransitionSize/MinDrawDistance/LODDrawDistance
	- New CVar:  r.HLOD.DistanceOverride (defaults to 0)
	- Fortnite uses 350m for this distance, fornow

	[CODEREVIEW] jurre.debaare
	#jira AT-1462

	#ROBOMERGE-SOURCE: CL 3591929 in //Fortnite/Release-Prep/...
	#ROBOMERGE-BOT: FORTNITE (Release-Prep -> Main)

Change 3587391 by Michael.Noland

	Fortnite: Lots of memory tracking stuff
	- Added memory logging to game state transitions and overall health tracking for the entire session
	- Added support for Gauntlet-based tests to Fortnite
	- Enabled the Gauntlet plugin (and fixed spaces instead of tabs in the .uproject file)
	- Added code to set gauntlet state based on the current subclass of AFortGameState
	- Added a base controller and a memory report controller (WIP, ported from equivalents in Paragon)
	- Updated FortniteClient to use MALLOC_LEAKDETECTION=1, PLATFORM_USES_FIXED_GMalloc_CLASS=0, and AllowASLRInShipping=false in Development builds (may enable them in Test builds in a future CL, to match Paragon)

	#jira FORT-50567

Change 3583307 by Peter.Knepley

	Need non-jittered ViewToClip matrix in order to do "after tonemapper" postprocess blendable material that's positioned in view space

	Modify the AttachScope material function to use "ViewSpaceTransformToClipSpace" instead of going back to world space first. This also means it can utilitize the ViewToClipNoAA matrix.

	#jira AT-733

Change 3582378 by Luke.Thatcher

	[FORTNITE] [~] Unify Xbox and PS4 scalability settings and device profiles.
	 - All Xbox and PS4 r. CVars are overriden in their platform's Scalability.ini file. The device profile only selects sg. groups.
	 - Fixed the Neo 4K profile for Fortnite. Previously players with 4K monitors would choose the Neo_4K profile, which looks worse than Neo, but still renders at 1080p.
	 - Console specific settings have to live in the Base/Default .ini's, as the cooker doesn't load the console specific files. This is fixed in UE4 Main.

	#jira FORT-50206

Change 3580934 by Luke.Thatcher

	[FORTNITE] [PS4] [+] Support different garlic and onion heap sizes in the old memory system for base and neo.
	 - Neo has 512 MB more direct memory than a base kit.
	 - Increased the garlic heap size by 416 MB on Neo, and CPU heap by 96 MB.

	#jira FORT-50206

Change 3576664 by Bart.Hawthorne

	Re-enable Oodle and add Mac implementation. Also includes fixed oodle libraries by MichaelT.

	#jira FORT-49986

	#tests Connected to PC server with editor -game build on Mac in Athena

Change 3575671 by Nick.Darnell

	Athena - The gameplay ability system now supports adding Gameplay Cue's with params.  Now using cues instead of gameplay effects in order to notify when the bandaging/shielding begin and end.  THe new method should properly show and disappear on time, b/c it's all client side.  Added a way in the Athena Context to easily hook gameplay "UI" cues that are rebroadcast from the Athena Pawn.

	#jira AT-644

Change 3575534 by Peter.Knepley

	Ability montage replication optimizations

	#jira AT-955

Change 3573305 by Lukasz.Furman

	disabled path section update when crowd simulated AI is moving through navlink, fixed AI getting stuck in some corners
	#jira FORT-49748

Change 3566775 by John.Abercrombie

	Optimizations from Dev-Athena
	- Tested with PIE & and 2 Player local server game

	#ue4-athena - (merge CLs 3345771 and 3363030 from Framework) - Refactored CharacterMovementComponent determination of net send rate when combining moves into a virtual function GetClientNetSendDeltaTime(). Added configurable values to GameNetworkManager under [/Script/Engine.GameNetworkManager].

	For Fortnite, set unthrottled (<= 10 player) limit to 60Hz (from 90Hz), and trying throttled at 30Hz (from 45Hz).
	#AUTOMERGE using branch //Fortnite/Main-To-//Fortnite/Dev-Athena (reversed) of change#3545535 by Zak.Middleton on 2017/07/19 20:15:17.

	#ue4-athena - (merge CL 3377054 from Framework) -  Fix CharacterMovementComponent updated with very high delta time on server when initially joining. Make sure the ServerTimeStamp is initialized to current world time rather than zero to prevent large delta.
	#AUTOMERGE using branch //Fortnite/Main-To-//Fortnite/Dev-Athena (reversed) of change#3545452 by Zak.Middleton on 2017/07/19 18:57:45.

	#athena - If network smoothing mode is not linear, don't replicate ReplicatedServerLastTransformUpdateTimeStamp. Only AI use linear smoothing in FN.
	#AUTOMERGE using branch //Fortnite/Main-To-//Fortnite/Dev-Athena (reversed) of change#3545559 by Zak.Middleton on 2017/07/19 20:47:18.

	#ue4-athena - Converted all RPCs on UCharacterMovementComponent to be on ACharacter instead, to avoid the bandwidth overhead of calling RPCs on a component.
	Existing overrides of _Implementation and _Validate functions should remain unchanged. If for some reason someone overrode the old RPC virtuals, those are now non-virtual on UCharacterMovementComponent but are still virtual on ACharacter.
	#AUTOMERGE using branch //Fortnite/Main-To-//Fortnite/Dev-Athena (reversed) of change#3557564 by Zak.Middleton on 2017/07/26 20:13:43.

	#ue4-athena - Throttle character movement server corrections and acks to the client based on time since last adjustment. Cuts down on network traffic for character movement.
	Added configurable settings to control this. Set times to zero to disable this.
	- NetworkMinTimeBetweenClientAckGoodMove
	- NetworkMinTimeBetweenClientAdjustments
	- NetworkMinTimeBetweenClientAdjustmentsLargeCorrection
	- NetworkLargeClientCorrectionDistance

	#ue4-athena - Perf: (EditMerge CL 3492200 from Dev-Framework): Always reset the input array in AActor::GetComponents(), but do so without affecting allocated size.
	#AUTOMERGE using branch //Fortnite/Main-To-//Fortnite/Dev-Athena (reversed) of change#3561669 by Zak.Middleton on 2017/07/28 14:16:19.

	#ue4-athena - Perf: (EditMerge CL 3468253 from Dev-AnimPhys): Remove the need for calling constructors for physx PxRaycastHit in the dynamic hit result buffer. Saves 30% of the cost of doing small raycasts.
	#AUTOMERGE using branch //Fortnite/Main-To-//Fortnite/Dev-Athena (reversed) of change#3561672 by Zak.Middleton on 2017/07/28 14:17:12.

	#ue4-athena - Perf: (EditMerge CL 3359553 from Dev-Framework): Optimization in CharacterMovement tick to not extract transform values twice.
	#AUTOMERGE using branch //Fortnite/Main-To-//Fortnite/Dev-Athena (reversed) of change#3561674 by Zak.Middleton on 2017/07/28 14:18:04.

	#ue4-athena - Perf: (EditMerge CL 3426174 from Dev-Framework): Avoid call to virtual getSimulationFilterData() to only use it when needed in PreFilter if we actually have items in the IgnoreComponents list (which is rare). The sim filter data 'word2' stores the component ID.
	#AUTOMERGE using branch //Fortnite/Main-To-//Fortnite/Dev-Athena (reversed) of change#3561709 by Zak.Middleton on 2017/07/28 14:32:11.

	#ue4-athena - Perf: (EditMerge CL 3382054 from Dev-Framework): Optimize CharacterMovementComponent::GetPredictionData_Client_Character() and GetPredictionData_Server_Character() to remove virtual calls.
	#AUTOMERGE using branch //Fortnite/Main-To-//Fortnite/Dev-Athena (reversed) of change#3561856 by Zak.Middleton on 2017/07/28 15:11:57.

	#ue4-athena - Use less bandwidth for CharacterMovement RPCs when the character is not standing on any component (ie during jumps and falling). Added separate "...NoBase()" versions of ServerMove() and ServerMoveDual().
	Undid part of 3557564 and restored the CMC functions to be virtual, and removed virtual keyword from matching Character functions, so that overrides are in one consistent place. Also guarantees backwards compat for licensees.
	#AUTOMERGE using branch //Fortnite/Main-To-//Fortnite/Dev-Athena (reversed) of change#3564858 by Zak.Middleton on 2017/07/31 15:24:39.

	#jira Fort-1

Change 3562825 by Chris.Gagnon

	Added CommonCustomNavigation Widget, this widget can be used to capture navigation requests to handle in custom ways.

	#jira FORT-0

Change 3562098 by Josh.Markiewicz

	#UE4 Encryption token/ack changes
	- moved encryption token request/ack to delegates
	- moved FNetworkNotify to NetworkDelegates.h
	- moved connection logic out of GameInstance and back into networking code
	-- GameInstance sends an enum and the network code does the right thing based on that

	#review-3559694 @ryan.gerleve
	#tests PC dedicated server connections golden path and forced failures
	#jira FORT-0

Change 3559354 by Luke.Thatcher

	[FORTNITE] [PS4] [^] Merging (as edit)  support for setting flip rate on PS4 (CLs 3555687 and 3558843) from //Fortnite/Dev-Athena/... to //Fortnite/Main/...
	 - Allowed rates are now 60Hz, 30Hz and 20Hz.
	 - Exposed by r.PS4FlipRate CVar, set to 60Hz by default. Requires r.Vsync 1.

	#jira FORT-49463

Change 3532644 by Jeff.Campeau

	Fix mapping current culture to movie audio channels using data table to map languages to track indices.
	Don't rewind cinematics (they all start from the begining because we load them and play them once). Seeks cause us to have to redecode video frames at a large perf cost.
	Delay cutscene playback by 0.5 seconds to give us time to build up a buffer of decoded video. (Temporary workaround for audio/video sync).
	Generic implementation for getting current languages in BP.
	Fix more issues with calling into media source functionality when using the source reader (potential hangs).

	#jira FORT-44376,FORT-48209,FORT-48040

	#testedon Preflight from last night combined with Bob's changes from today clear all known issues. This change tested on Xbox and PC multiple times each.

Change 3527761 by Chris.Gagnon

	Fixed various issues in the widget switcher, also added Advanced calls that allow the user to specify if activation/deactivation should occur.

	#jira FORT-47988, FORT-47984

Change 3525390 by Jeff.Campeau

	Remove media player log spam

	#jira FORT-47393

	#testedon compiled client

Change 3518692 by Chris.Gagnon

	Added CleanOperation Adding which will remove unneeded op combinations from the op queue.
	Also added the ability to suspend starting operation queue processing to allow complex operations to accumulate and in turn allow the Clean Op adding code to be effective.

	GameFeedback, and the widget switcer utilize this to avoid unnesacary activations of a screen that is immediately being deactivated.

	Root issue of the mentioned bug is that activation of the quest screen created a latent navigation du to the deferal of scrolling into view.
	This is still an issue in general, there isn't much we can do about it. Other than avoid activating a panel that will deactivated that frame as we did with the code changes in this CL.

	#jira FORT-47395

Change 3514658 by Jeff.Campeau

	Fixed a media player threading issue where the OnMediaOpened event could be called before the media Init script completed.
	Moved the event Cinematic used when setting up and playing media after media file load to use a delayed event from the MovieWidget so that it will always happen after the movie widget processing.
	Fixed an issue that could cause samples to leak in MfMedia plugin and cause ReadSample to lockup.
	Fixed an issue where a default texture is displayed for movies before the movie starts playing (the player may be active before the first frame of the video is decoded). Default is now all black as it is expected that this texture will be displayed for several frames.

	#jira FORT-46801
	#testedon Xbox through rocket launch cinematic including vintertip for stairs, skill tree nodes, and victory result video

Change 3507896 by Ryan.Gerleve

	Changed the net.UseEncryptionToken to be more useful and renamed it to net.AllowEncryption.
	This cvar, if 0, will prevent the PacketHandler from adding the configured encryption component, and prevent UPendingNetGame and AOnlineBeaconClient from filling out the EncryptionToken parameter of NMT_Hello - which prevents the extra encryption handshake connection step.

	#jira FORT-46878
	#review-3507897 @josh.markiewicz

Change 3503928 by Ryan.Gerleve

	Add safety checks around some of the encryption functionality. Fixes a server crash seen during load testing.

	#jira FORT-46772
	#review-3503929 bob.tellez
	#robomerge ReleaseNext

[CL 3673993 by Bob Tellez in Main branch]
2017-09-30 03:42:01 -04:00

1355 lines
41 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 "HAL/PlatformApplicationMisc.h"
#include "Misc/OutputDeviceRedirector.h"
#include "Misc/CoreStats.h"
#include "Misc/TimeGuard.h"
#include "Misc/CoreDelegates.h"
#include "Misc/ScopeLock.h"
#include "RenderCore.h"
#include "RenderCommandFence.h"
#include "RHI.h"
#include "TickableObjectRenderThread.h"
#include "Stats/StatsData.h"
#include "HAL/ThreadHeartBeat.h"
#include "RenderResource.h"
#include "ScopeLock.h"
#include "HAL/LowLevelMemTracker.h"
//
// Globals
//
RENDERCORE_API bool GIsThreadedRendering = false;
RENDERCORE_API bool GUseThreadedRendering = 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 ( 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
{
LLM_SCOPE(ELLMTag::RHIMisc);
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, FPlatformAffinity::GetRHIThreadPriority(),
FPlatformAffinity::GetRHIThreadMask()
);
check(Thread);
}
};
/** The rendering thread main loop */
void RenderingThreadMain( FEvent* TaskGraphBoundSyncEvent )
{
LLM_SCOPE(ELLMTag::RenderingThreadMemory);
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 (!IsRunningRHIInSeparateThread())
{
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_InternalUseOnly 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_InternalUseOnly && !GIsRunningRHIInSeparateThread_InternalUseOnly && !GIsRunningRHIInDedicatedThread_InternalUseOnly && !GIsRunningRHIInTaskThread_InternalUseOnly);
if (GUseRHIThread_InternalUseOnly)
{
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_InternalUseOnly = FRHIThread::Get().Thread;
check(GRHIThread_InternalUseOnly);
GIsRunningRHIInDedicatedThread_InternalUseOnly = true;
GIsRunningRHIInSeparateThread_InternalUseOnly = true;
GRHIThreadId = GRHIThread_InternalUseOnly->GetThreadID();
GRHICommandList.LatchBypass();
}
else if (GUseRHITaskThreads_InternalUseOnly)
{
GIsRunningRHIInSeparateThread_InternalUseOnly = true;
GIsRunningRHIInTaskThread_InternalUseOnly = true;
}
// 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_InternalUseOnly)
{
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_InternalUseOnly = nullptr;
GRHIThreadId = 0;
}
GIsRunningRHIInSeparateThread_InternalUseOnly = false;
GIsRunningRHIInDedicatedThread_InternalUseOnly = false;
GIsRunningRHIInTaskThread_InternalUseOnly = false;
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_InternalUseOnly);
}
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);
FPlatformApplicationMisc::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;
}
TAutoConsoleVariable<int32> CVarGTSyncType(
TEXT("r.GTSyncType"),
0,
TEXT("Determines how the game thread syncs with the render thread, RHI thread and GPU.\n")
TEXT("Syncing to the GPU swap chain flip allows for lower frame latency.\n")
TEXT(" 0 - Sync the game thread with the render thread (default).\n")
TEXT(" 1 - Sync the game thread with the RHI thread.\n")
TEXT(" 2 - Sync the game thread with the GPU swap chain flip (only on supported platforms).\n"),
ECVF_Default);
struct FRHISyncFrameCommand final : public FRHICommand<FRHISyncFrameCommand>
{
FGraphEventRef GraphEvent;
int32 GTSyncType;
FORCEINLINE_DEBUGGABLE FRHISyncFrameCommand(FGraphEventRef InGraphEvent, int32 InGTSyncType)
: GraphEvent(InGraphEvent)
, GTSyncType(InGTSyncType)
{}
void Execute(FRHICommandListBase& CmdList)
{
if (GTSyncType == 1)
{
// Sync the Game Thread with the RHI Thread
// "Complete" the graph event
TArray<FBaseGraphTask*> Subsequents;
GraphEvent->DispatchSubsequents(Subsequents);
}
else
{
// This command runs *after* a present has happened, so the counter has already been incremented.
// Subtracting 1 gives us the index of the frame that has *just* been presented.
RHICompleteGraphEventOnFlip(GRHIPresentCounter - 1, GraphEvent);
}
}
};
void FRenderCommandFence::BeginFence(bool bSyncToRHIAndGPU)
{
if (!GIsThreadedRendering)
{
return;
}
else
{
if (BundledCompletionEvent.GetReference() && IsInGameThread())
{
CompletionEvent = BundledCompletionEvent;
return;
}
// Various places in the engine use FRenderCommandFence to flush render commands,
// and shouldn't be held up on the GPU if it is not necessary...
// Force a GT->RT only sync if bSyncToRHIAndGPU is false (GT sync type 0).
int32 GTSyncType = bSyncToRHIAndGPU ? CVarGTSyncType.GetValueOnAnyThread() : 0;
if (GTSyncType == 0)
{
// Sync Game Thread with Render Thread only
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);
}
else
{
// Create a task graph event which we can pass to the render or RHI threads.
CompletionEvent = FGraphEvent::CreateGraphEvent();
ENQUEUE_UNIQUE_RENDER_COMMAND_TWOPARAMETER(
FSyncFrameCommand,
FGraphEventRef, CompletionEvent, CompletionEvent,
int32, GTSyncType, GTSyncType,
{
if (GRHIThread_InternalUseOnly)
{
new (RHICmdList.AllocCommand<FRHISyncFrameCommand>()) FRHISyncFrameCommand(CompletionEvent, GTSyncType);
RHICmdList.ImmediateFlush(EImmediateFlushType::DispatchToRHIThread);
}
else
{
FRHISyncFrameCommand Command(CompletionEvent, GTSyncType);
Command.Execute(RHICmdList);
}
});
}
}
}
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)
{
if(GIsAutomationTesting)
{
// temp test to log callstacks for this being triggered during automation tests
ensureMsgf(false, TEXT("FlushRenderingCommands called recursively! %d calls on the stack."));
}
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);
#if !WITH_EDITOR
// editor threads can block for quite a while...
if (!bDone && !FPlatformMisc::IsDebuggerPresent())
{
static bool bDisabled = FParse::Param(FCommandLine::Get(), TEXT("nothreadtimeout"));
static bool bGPUDebugging = FParse::Param(FCommandLine::Get(), TEXT("gpucrashdebugging"));
if (bGPUDebugging && FPlatformTime::Seconds() > 2.0f)
// 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)
{
bool IsGpuAlive = true;
if (GDynamicRHI)
{
IsGpuAlive = GDynamicRHI->CheckGpuHeartbeat();
}
UE_CLOG(!IsGpuAlive, LogRendererCore, Fatal, TEXT("CheckGpuHeartbeat returned false after %.02f secs of waiting for the GPU"), FPlatformTime::Seconds() - StartTime);
}
#if !PLATFORM_IOS // @todo MetalMRT: Timeout isn't long enough...
if (FPlatformTime::Seconds() >= EndTime && FThreadHeartBeat::Get().IsBeating() && !bDisabled)
{
bool IsGpuAlive = true;
if (GDynamicRHI)
{
IsGpuAlive = GDynamicRHI->CheckGpuHeartbeat();
}
UE_LOG(LogRendererCore, Fatal, TEXT("GameThread timed out waiting for RenderThread after %.02f secs"), FPlatformTime::Seconds() - StartTime);
}
#endif
}
#endif
}
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 (!IsRunningRHIInSeparateThread())
{
ENQUEUE_UNIQUE_RENDER_COMMAND(
FlushPendingDeleteRHIResources,
{
FlushPendingDeleteRHIResources_RenderThread();
}
);
}
}
void FlushPendingDeleteRHIResources_RenderThread()
{
if (!IsRunningRHIInSeparateThread())
{
FRHIResource::FlushPendingDeletes();
}
}
FRHICommandListImmediate& GetImmediateCommandList_ForRenderCommand()
{
return FRHICommandListExecutor::GetImmediateCommandList();
}
#if WITH_EDITOR || IS_PROGRAM
// mainly concerned about the cooker here, but anyway, the editor can run without a frame for a very long time (hours) and we do not have enough lock free links.
/** The set of deferred cleanup objects which are pending cleanup. */
TArray<FDeferredCleanupInterface*> PendingCleanupObjectsList;
FCriticalSection PendingCleanupObjectsListLock;
FPendingCleanupObjects::FPendingCleanupObjects()
{
check(IsInGameThread());
{
FScopeLock Lock(&PendingCleanupObjectsListLock);
Exchange(CleanupArray, PendingCleanupObjectsList);
}
}
FPendingCleanupObjects::~FPendingCleanupObjects()
{
QUICK_SCOPE_CYCLE_COUNTER(STAT_FPendingCleanupObjects_Destruct);
for (int32 ObjectIndex = 0; ObjectIndex < CleanupArray.Num(); ObjectIndex++)
{
CleanupArray[ObjectIndex]->FinishCleanup();
}
}
void BeginCleanup(FDeferredCleanupInterface* CleanupObject)
{
{
FScopeLock Lock(&PendingCleanupObjectsListLock);
PendingCleanupObjectsList.Add(CleanupObject);
}
}
#else
/** 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);
}
#endif
FPendingCleanupObjects* GetPendingCleanupObjects()
{
return new FPendingCleanupObjects;
}
void SetRHIThreadEnabled(bool bEnableDedicatedThread, bool bEnableRHIOnTaskThreads)
{
if (bEnableDedicatedThread != GUseRHIThread_InternalUseOnly || bEnableRHIOnTaskThreads != GUseRHITaskThreads_InternalUseOnly)
{
if ((bEnableRHIOnTaskThreads || bEnableDedicatedThread) && !GIsThreadedRendering)
{
check(!IsRunningRHIInSeparateThread());
UE_LOG(LogConsoleResponse, Display, TEXT("Can't switch to RHI thread mode when we are not running a multithreaded renderer."));
}
else
{
StopRenderingThread();
if (bEnableRHIOnTaskThreads)
{
GUseRHIThread_InternalUseOnly = false;
GUseRHITaskThreads_InternalUseOnly = true;
}
else if (bEnableDedicatedThread)
{
GUseRHIThread_InternalUseOnly = true;
GUseRHITaskThreads_InternalUseOnly = false;
}
else
{
GUseRHIThread_InternalUseOnly = false;
GUseRHITaskThreads_InternalUseOnly = false;
}
StartRenderingThread();
}
}
if (IsRunningRHIInSeparateThread())
{
if (IsRunningRHIInDedicatedThread())
{
UE_LOG(LogConsoleResponse, Display, TEXT("RHIThread is now running on a dedicated thread."));
}
else
{
check(IsRunningRHIInTaskThread());
UE_LOG(LogConsoleResponse, Display, TEXT("RHIThread is now running on task threads."));
}
}
else
{
check(!IsRunningRHIInTaskThread() && !IsRunningRHIInDedicatedThread());
UE_LOG(LogConsoleResponse, Display, TEXT("RHIThread is disabled."));
}
}
static void HandleRHIThreadEnableChanged(const TArray<FString>& Args)
{
if (Args.Num() > 0)
{
const int32 UseRHIThread = FCString::Atoi(*Args[0]);
SetRHIThreadEnabled(UseRHIThread == 1, UseRHIThread == 2);
}
else
{
UE_LOG(LogConsoleResponse, Display, TEXT("Usage: r.RHIThread.Enable 0=off, 1=dedicated thread, 2=task threads; Currently %d"), IsRunningRHIInSeparateThread() ? (IsRunningRHIInDedicatedThread() ? 1 : 2) : 0);
}
}
static FAutoConsoleCommand CVarRHIThreadEnable(
TEXT("r.RHIThread.Enable"),
TEXT("Enables/disabled the RHI Thread and determine if the RHI work runs on a dedicated thread or not.\n"),
FConsoleCommandWithArgsDelegate::CreateStatic(&HandleRHIThreadEnableChanged)
);