Asynchronous Data Streaming
Data streaming is an optimization technique intended to reduce spikes caused by loading graphic resources and compiling shaders. With this technique, not all the data is loaded into memory at once. Instead, only the required data is loaded, and the rest is loaded progressively on demand.
Resource loading is performed and transferred to the GPU in separate asynchronous threads. After that, resources are synchronized and added to the virtual scene on the CPU side.
In UNIGINE, asynchronous data streaming is enabled by default. You can disable asynchronous data streaming in UnigineEditor or via the console:
- In UnigineEditor, open the Settings window and go to the Streaming section. Here you can switch the streaming mode for textures and/or meshes.
- In the console, run the corresponding commands that switch the streaming mode for textures and/or meshes.
There are two main streaming modes — asynchronous (Async) and forced (Force). The Force mode ensures force-loading of all resources required for each frame simultaneously (e.g., grabbing frame sequences, rendering node previews, warmup, etc.).
The streaming system provides asynchronous loading of the following data to RAM:
- All texture runtime files and textures with the Unchanged option enabled, including cubemaps, voxel probe maps, and shadow maps of baked shadows.
- Meshes of ObjectMeshStatic, ObjectMeshClutter, ObjectMeshCluster, ObjectGuiMesh objects, and DecalMesh.
Procedurally generated objects such as ObjectMeshClutter are generated in a separate thread, significantly reducing performance costs.
You can obtain general information on streamed resources by using the render_streaming_meshes_info and render_streaming_textures_info console commands.
It is also possible to print the list of loaded resources and detailed information on them by using the render_streaming_meshes_list and render_streaming_textures_list console commands.
Asynchronous Shader Compilation#
In addition to the asynchronous loading of meshes and textures, the streaming system provides asynchronous shader compilation and loading.
There are also 2 modes — asynchronous (Async) and forced (Force). In the Force mode, all shaders required for the current frame are compiled and loaded to RAM simultaneously in the current thread. By default, the asynchronous mode is used.
The number of compiled and loaded shaders are available in the Performance Profiler tool.
Memory Usage Limits#
All memory allocations for graphic resources are limited by the committed memory size, which includes both RAM and VRAM.
You can limit the memory available to the application to avoid crashes and find the balance between performance and memory consumption. There are two main parameters for this:
- Usage limits for RAM and VRAM that restrict memory consumption to a specified percentage of the committed memory. However, you should remember that if the streaming system exceeds the VRAM usage limit, it will start using RAM for loading graphic resources. If it exceeds the RAM usage limit, the application will crash.
- Free space for RAM and VRAM that defines how much memory is additionally reserved for allocations per frame. These parameters ensure that there is always enough memory for resource loading in the current frame. The values should be determined empirically, depending on the application.
You can specify the usage limits and free space via UnigineEditor or the console:
- In UnigineEditor, open the Settings window, go to the Streaming section, and specify the required values.
-
In the console, pass the required values to the corresponding commands:
Texture Streaming#
The streaming system manages textures automatically. There are two modes of texture streaming:
- Asynchronous mode that provides asynchronous loading of textures.
- Forced mode for force-loading of textures required for the current frame at once.
For texture streaming optimization, you can enable texture mipmap loading, which significantly improves performance by reducing the memory consumption of texture streaming. This feature allows for the correct mipmap to be loaded at the current moment. When mipmaps loading is enabled, the textures that are not currently in use are unloaded.
To enable and configure mipmaps loading via UnigineEditor, do the following:
- Open the Settings window and go to the Streaming section.
- Toggle on the Mipmaps flag and specify the required Mipmaps Density.
Mipmaps Density sets the density of mipmaps relative to the screen resolution and helps to define which mipmap should be loaded at the current moment. You can specify different values for different quality presets. For example, you can set the density to less than 1 for the low-quality preset. In this case, the engine will load the low-resolution mipmaps, and the textures will look blurry.
Additionally, you may need to configure the Texture Streaming Density Multiplier for each texture in some materials to achieve the desired visual effect.
Forced Streaming for Textures in Post-Effect Materials#
To ensure all textures for your post-effects are available when needed, the forced streaming mode is set for all post materials by default. You can find this setting in Material Editor.
However, if some textures in the post material continuously change (via code, Tracker, etc.), it is recommended to specify the forced streaming mode per texture:
- In the Material Editor, set Setting For Each Texture in the Textures Streaming Mode parameter for your post-effect material.
- Toggle the forced streaming on and off for each texture individually in the Parameters panel of the Material Editor.
These settings override the global texture streaming settings specified in the Settings window.
Mesh Streaming#
Meshes can be loaded to RAM and VRAM separately for more efficient work with geometry. This allows eliminating memory leaks: meshes participating in collisions and intersections can be loaded to RAM only, if they are not currently rendered.
There are two modes of mesh streaming to RAM/VRAM:
- Asynchronous mode that provides asynchronous loading of meshes.
- Forced mode for force-loading of meshes required for the current frame at once.
The asynchronous loading to RAM and VRAM differs. Even if a mesh hasn't been loaded to video memory in time, it doesn't affect the application behavior (you may only notice some lag). However, if a mesh hasn't been loaded to memory in time, it may lead to incorrect physical behavior of objects in the scene.
First of all, we highly recommend you to use shapes for collision and intersection detection as it is faster. If, for some reason, it doesn't suit you, use the following methods:
- Load meshes and hold them in memory while they exist. API of some mesh-based objects provides this functionality out of the box. It may partially solve the problem with incorrect behavior, however, only a few meshes can remain loaded.
-
Use the prefetch system that allows asynchronous pre-loading of meshes participating in collisions and intersections to memory before they are used:
- Set the Radius prefetch mode.
- Specify the physics radius (for collisions) and/or the radius within which intersections are calculated.
- Specify the prefetch radius that should exceed the collison and intersection radius values.
You can also preload all meshes for which collisions and intersections are calculated (the Full prefetch mode), however it will significantly increase RAM usage.
- API of some mesh-based objects, as well as the MeshRender class API provides also methods which allow implementing a custom prefetch logic for pre-loading meshes.
Asynchronously streamed meshes shouldn't be modified. The only way to change such mesh is to make it procedural. A procedural mesh is a mesh created via code, such meshes have a specific streaming mode — they are always kept in memory after creation and never unloaded until the object is destroyed via code or the mesh returns to its normal mode (streaming from a source file). The mesh-based objects API allows switching a mesh to the procedural mode and apply changes.