Overview
This document provides a detailed analysis of the asynchronous asset loading flow of FStreamableManager in UE5, covering the complete process from RequestAsyncLoad to callback execution. By examining the source code in depth, we will understand the interaction mechanism between the main thread and the loading thread, as well as how asset dependencies are handled.
Core Concepts
Key Classes and Structures
- FStreamableManager: The core class in UE5 responsible for managing asynchronous asset loading
- FStreamableHandle: Tracks the status and callbacks of a single asynchronous load request
- FStreamable: Used internally to track the loading status of a single asset
- FSoftObjectPath: A soft reference path used to point to assets that have not yet been loaded
Main Flow Components
- Request initiation: Initiate an asynchronous load request via
RequestAsyncLoador related methods - Request processing:
StartHandleRequestsprocesses load requests - Asset loading:
StreamInternalperforms the actual asset loading logic - Callback handling: Notify the main thread via callback after loading completes
- Dependency handling: Automatically handles dependencies between assets
Complete Flow Analysis
Request Initiation Stage
Starting with the RequestAsyncLoadInternal function:
| |
Main steps:
- Create a load handle: Create an
FStreamableHandleobject to track the load request - Set the callback: Store the user-provided callback function in the handle
- Validate asset paths: Check and clean up invalid asset paths
- Manage the handle: Add the handle to the active handle list if needed
- Start loading: Call
StartHandleRequeststo begin processing the load request
Load Processing Stage
The StartHandleRequests function processes the actual load requests:
| |
Main steps:
- Iterate over assets: Call
StreamInternalfor each requested asset - Track existing streams: Add the returned
FStreamableobjects to the list - Check already-loaded assets: For assets already in memory, mark them as complete directly
Asset Loading Core
StreamInternal is the core implementation of asset loading:
| |
Main steps:
- Check whether it exists in memory: If the asset is already in memory, return directly
- Create a stream object: If it is a new asset, create an
FStreamableobject - Synchronous load check: Perform synchronous loading in certain cases (such as initialization loading or inside constructors)
- Asynchronous loading: Call
LoadPackageAsyncto initiate an asynchronous load request - Set the callback: Configure the callback function for when loading completes
Callback Handling Stage
After loading completes, the AsyncLoadCallback function is called:
| |
Main steps:
- Get the stream object: Find the corresponding
FStreamableobject based on the asset path - Mark loading complete: Set
bAsyncLoadRequestOutstanding = false - Find the asset in memory: Call
FindInMemoryto find the loaded asset - Check completed requests: Call
CheckCompletedRequeststo check whether all requests have completed
Completion Check and Callback Execution
The CheckCompletedRequests function checks whether load requests are complete and executes the corresponding callbacks:
Main steps:
- Iterate over related handles: Check all
FStreamableHandleobjects that reference the asset - Mark completion status: For completed assets, update the handle’s completion status
- Execute callbacks: When all assets have finished loading, execute the user-provided callback function
Interaction Between the Main Thread and the Loading Thread
Thread Responsibility Division
| Thread Type | Responsibility | Key Operations |
|---|---|---|
| Main thread | Initiate load requests and handle callbacks | Create load requests and execute completion callbacks |
| Loading thread | Perform the actual asset loading | Read from disk, parse package files, and load assets |
Interaction Mechanism
- Request initiation: The main thread calls
LoadPackageAsyncto initiate a load request - Task scheduling:
LoadPackageAsyncsubmits the load task to the asynchronous loading system - Background loading: The loading thread performs the actual loading operations in the background
- Completion notification: After loading completes, notify the main thread via the
FLoadPackageAsyncDelegatecallback - Callback execution: The main thread handles the loaded assets in
AsyncLoadCallback
Synchronization Points
Although the loading process is asynchronous, synchronization is forced in certain cases:
- Initialization stage: During game initialization, certain assets need to be loaded synchronously
- Inside constructors: Load requests initiated inside object constructors are executed synchronously
- Forced synchronization: When the
bForceSynchronousLoadsflag is set
Asset Dependency Handling
Dependency Loading Mechanism
UE5’s package loading system automatically handles asset dependencies:
- Package dependency analysis: When loading an asset, the system analyzes the other assets it depends on
- Recursive loading: Automatically load all dependent assets
- Dependency graph management: Build and manage the dependency graph between assets
Specific Flow
- Main asset loading: Initiate a load request for the main asset
- Dependency analysis: The loading system analyzes the dependencies of the main asset
- Dependency loading: Automatically create load requests for all dependencies
- Loading order: Determine the loading order based on dependency relationships
- Completion notification: The main asset is notified as loaded only after all dependencies have finished loading
Dependency Handling Example
Scenario: Loading a static mesh that contains a material and a texture
- Initiate loading: Request to load the static mesh asset
StaticMesh'/Game/Meshes/Sphere.Sphere' - Dependency analysis: The loading system discovers that the mesh uses the material
Material'/Game/Materials/Metal.Metal' - Material loading: Automatically load the material asset
- Texture loading: Discover that the material depends on the texture
Texture2D'/Game/Textures/Metal_Albedo.Metal_Albedo', and automatically load the texture - Completion order: Texture → Material → Static Mesh
- Callback execution: When all assets have finished loading, execute the user-provided callback
Code Flow Details
Core Flowchart
flowchart TD
A[RequestAsyncLoadInternal] --> B[创建FStreamableHandle]
B --> C[StartHandleRequests]
C --> D[StreamInternal]
D --> E{资源是否在内存中?}
E -->|是| F[直接返回]
E -->|否| G{是否需要同步加载?}
G -->|是| H[StaticLoadObject]
G -->|否| I[LoadPackageAsync]
I --> J[加载线程执行]
J --> K[AsyncLoadCallback]
H --> L[CheckCompletedRequests]
K --> L
L --> M{所有资源加载完成?}
M -->|是| N[执行用户回调]
M -->|否| O[继续等待]
Key Code Analysis
Request Creation and Initialization
| |
This code creates a new FStreamableHandle object and sets the callback function, asset paths, and other information.
Asset Loading Core
| |
This code calls LoadPackageAsync to initiate an asynchronous load request and sets the callback function.
Callback Handling
| |
This code handles the load completion callback, marks the loading status, and checks whether all requests have completed.
Practical Application Examples
Basic Asynchronous Loading
| |
Asynchronous Loading with a Handle
| |
Dependency Loading Example
| |
Performance Optimization Suggestions
Loading Strategy Optimization
- Batch loading: Merge multiple related assets into a single load request to reduce loading overhead
- Priority setting: Set appropriate load priorities based on the importance of the assets
- Preloading: Preload assets that will be used soon at appropriate times
- Asynchronous loading: Use asynchronous loading whenever possible to avoid blocking the main thread
Memory Management
- Release promptly: Promptly release assets that are no longer used via
ReleaseHandle - Dependency analysis: Analyze and optimize asset dependency relationships to reduce unnecessary loading
Error Handling
- Load failure handling: Add callback handling for load failures
- Timeout handling: For critical assets, add load timeout detection
- Error logs: Add detailed error logs to facilitate troubleshooting loading issues
Common Issues and Solutions
Load Failure
Problem: Asset loading fails
Solution:
- Check whether the asset path is correct
- Ensure the asset file exists at the specified location
- Check whether asset dependencies are complete
- Check the logs for detailed error information
Slow Loading
Problem: Asset loading is too slow
Solution:
- Optimize asset size and complexity
- Use appropriate compression formats
- Implement asset preloading
High Memory Usage
Problem: Loading too many assets causes high memory usage
Solution:
- Release assets that are no longer used promptly
- Implement asset pool management
- Consider using dynamic asset loading strategies
- Monitor memory usage and adjust loading strategies promptly
Summary
UE5’s FStreamableManager provides a powerful and flexible asynchronous asset loading system, enabling efficient asset management through the following mechanisms:
- Separation of main thread and loading thread: The main thread initiates requests, the loading thread performs the actual loading, and completion is notified through a callback mechanism
- Automatic dependency handling: The loading system automatically handles dependencies between assets, ensuring all dependencies are loaded correctly
- Flexible loading strategies: Supports synchronous and asynchronous loading, allowing the appropriate loading method to be chosen for different scenarios
- Detailed status tracking: Provides detailed loading status information through
FStreamableHandle
By using FStreamableManager appropriately, developers can achieve efficient asset management and improve the game’s loading speed and runtime performance.
References
- Asynchronous Asset Loading in Unreal Engine | Unreal Engine 5.7 Documentation | Epic Developer Community
- UE5 source code:
Engine/Source/Runtime/Engine/Private/StreamableManager.cpp - UE5 source code:
Engine/Source/Runtime/CoreUObject/Private/UObject/AsyncLoad.cpp
This document is based on analysis of the UE5.5 source code; minor differences may exist between versions.