异步数据流
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.数据流是一种优化技术,旨在减少加载图形资源和编译着色器引起的峰值。使用此技术,并非所有数据都立即加载到随机存取存储器(RAM)中。取而代之的是,仅加载所需的数据,其余所有数据根据需要逐步加载。
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.执行资源加载,并在单独的异步线程中将其转移到GPU。之后,资源将同步并添加到CPU一侧的虚拟场景。
In UNIGINE, asynchronous data streaming is enabled by default. You can disable asynchronous data streaming in UnigineEditor or via the console:在 UNIGINE 中,默认启用异步数据流。 您可以在 UnigineEditor 中或通过控制台禁用异步数据流:
- In UnigineEditor, open the Settings window and go to the Streaming section. Here you can switch the streaming mode for textures and/or meshes.在UnigineEditor中,打开Settings窗口并转到Streaming部分。在这里你可以为纹理和/或网格切换流模式。
- 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.).有两种主要的流模式——异步(Async)和强制(Force)。Force模式确保每帧一次加载所需的所有网格和纹理(例如,抓取帧序列,渲染节点预览,预热等)。
For meshes, there is an additional All mode that disables mesh streaming and provides loading of all meshes available in the project on the application start-up. This mode is good for small projects with few meshes.对于网格,有一个额外的All模式,它禁用网格流,并在应用程序启动时提供项目中所有可用网格的加载。这种模式适用于网格较少的小项目。
The streaming system provides asynchronous loading of the following data to RAM:流系统提供以下数据异步加载到RAM:
- All texture runtime files and textures with the Unchanged option enabled, including cubemaps, voxel probe maps, and shadow maps of baked shadows.所有纹理运行时文件和启用了Unchanged选项的纹理,包括cubemaps, 体素探针地图和烘烤阴影的阴影地图。
- Meshes of ObjectMeshStatic, ObjectMeshClutter, ObjectMeshCluster, ObjectGuiMesh objects, and DecalMesh.ObjectMeshStatic, ObjectMeshClutter, ObjectMeshCluster, ObjectGuiMesh对象的网格,以及DecalMesh。
Procedurally generated objects such as ObjectMeshClutter are generated in a separate thread, significantly reducing performance costs.程序生成的对象(如ObjectMeshClutter)生成在一个单独的线程,显著降低性能成本。
You can obtain general information on streamed resources by using the render_streaming_meshes_info and render_streaming_textures_info console commands.可以通过使用render_streaming_meshes_info和render_streaming_textures_info控制台命令获取流资源的一般信息。
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.还可以通过使用render_streaming_meshes_list和render_streaming_textures_list控制台命令打印已加载资源的列表和有关它们的详细信息。
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.还有 2 种模式 - 异步 (Async) 和强制 (Force)。 在Force模式下,当前帧所需的所有着色器都会在当前线程中同时编译并加载到RAM中。 默认情况下,采用异步模式。
The number of compiled and loaded shaders are available in the Performance Profiler tool.Performance Profiler 工具中提供了已编译和加载的着色器数量。
Common Streaming Settings通用流媒体设置#
To make the scene appear immediately when the application starts, there is the First Frames Force parameter, which sets the number of frames that will be loaded right after the first frame. 为了在应用程序启动时立即显示场景,有一个First Frames Force参数,它设置在第一帧之后加载的帧数。
To take advantage of multithreading, set the maximum number of threads used for resource streaming by using the render_streaming_max_threads console parameter. A higher number of threads results in faster streaming but may cause spikes in the case of excessive consumption of GPU resources.利用多线程,设置的最大线程数用于流媒体资源利用(国家统计局) render_streaming_max_threads 控制台参数。更高的线程数量导致更快的流,但可能导致峰值在GPU资源的过度消费。
Memory Limits内存限制#
You can limit the number of loaded and unloaded graphic resources per frame by specifying the corresponding budgets. Use them to find the balance between loading/unloading speed and performance: keep in mind that increasing the budget increases streaming performance, however, memory consumption increases as well.您可以通过指定相应的 预算 来限制每帧加载和卸载的图形资源的数量。 使用它们来找到加载/卸载速度和性能之间的平衡点:请记住,增加预算会提高流媒体性能,但是,内存消耗也会增加。
Adjustable memory limits and life times enable to avoid situations where resources remain loaded in memory or video memory even after they are no longer used. They are defined for meshes loaded to RAM/VRAM, textures, and particles separately in a percentage of the total RAM/VRAM.可调整的内存限制和生命周期可以避免资源在不再使用后仍然加载在内存或视频内存中的情况。它们是为加载到RAM / VRAM的网格、纹理和粒子分别定义的,占总RAM / VRAM的百分比。
The memory limit is associated with the lifetime: resources are deleted from memory or video memory only when both values are exceeded.内存限制与生命周期关联:只有当内存或显存超过这两个值时,资源才会从内存或显存中删除。
Memory limits and RAM / VRAM occupied by streamed resources are available in the Performance Profiler tool.内存限制和RAM / VRAM被流资源可在Performance Profiler工具。
The limits are slash separated: the first value displays the current RAM / VRAM usage, and the second value is the limit.限制用斜杠分隔:第一个值显示当前RAM / VRAM使用情况,第二个值是限制。
Texture Cache纹理缓存#
The streaming system uses the texture cache composed of minimized copies generated for all textures with user-defined resolution stored in the data/.cache_textures folder. These copies are used instead of the originals while they are being loaded.流传输系统使用纹理缓存,该纹理缓存由为具有用户定义的分辨率的所有纹理生成的最小化副本组成,并存储在 data/.cache_textures 文件夹中。这些副本将在加载时代替原始文件使用。
Texture cache is loaded at Engine's startup and always stays in the memory after loading. The following default loading order ensures smooth loading and rendering of resources:纹理缓存在引擎启动时加载,并始终在加载后保留在内存中。为了提供流畅的资源加载和呈现,流式实体具有以下加载优先级:
- Texture cache 纹理缓存
- Geometry 几何形状
- Uncached textures cause spikes as texture cache is generated for them on the fly; materials with uncached and unloaded textures applied are rendered black 未缓存的纹理(由于在运行中为其生成纹理缓存时会产生尖峰;应用了未缓存和未加载纹理的材质会呈现黑色)
- Full-size textures 全尺寸纹理
Using the textures_cache_preload flag in the boot config file, you can choose the texture cache loading priority — preloaded or loaded after geometry data.使用boot配置文件中的 textures_cache_preload标志,您可以选择纹理缓存加载优先级:预加载或在几何数据后加载。
By default, texture cache files are generated with a resolution of 16 x 16 px. Such a low resolution of textures may be noticeable during loading.默认情况下,生成纹理缓存文件的分辨率为16x16,如此低的纹理分辨率会导致加载过程中出现视觉伪像。
Control the resolution of texture cache by setting the Texture Cache Resolution parameter. To apply changes, wipe away existing cache files by using the render_streaming_textures_cache_destroy console command, after which the texture cache will be generated automatically with the new specified resolution.通过设置Texture Cache Resolution参数来控制纹理缓存的分辨率。要应用更改,请使用render_streaming_textures_cache_destroy 控制台命令擦除现有的缓存文件,之后将使用新的指定分辨率自动生成纹理缓存。
The video memory amount currently occupied by the texture cache is available in the Performance Profiler tool.视频内存数量目前占领的纹理缓存可用Performance Profiler工具。
The render_streaming_textures_cache_load and render_streaming_textures_cache_unload console commands enable you to control loading of texture cache. For example, after loading full-size textures, you can unload the texture cache from video memory for better performance. render_streaming_textures_cache_load 和 render_streaming_textures_cache_unload 控制台命令使您能够控制加载纹理缓存。例如,加载后全尺寸的纹理,你可以卸载的纹理缓存视频内存更好的性能。
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.网格可以分别加载到RAM和VRAM中,以便更有效地处理几何图形。这允许消除内存泄漏:参与碰撞和交叉点的网格只能加载到RAM中,如果它们当前没有渲染。
There are 3 modes of mesh streaming to RAM/VRAM:有3种模式的网格流到RAM/VRAM:
- Asynchronous mode that provides asynchronous loading of meshes.异步模式,提供网格的异步加载。
- Forced mode for force-loading of meshes required for the current frame at once.强制模式,用于立即加载当前框架所需的网格。
- Mode for loading all meshes available in the project on the application start-up. Actually, this mode disables mesh streaming at all.模式加载 所有网格应用程序启动的项目。实际上,这种模式禁用网流。
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.异步加载到RAM和VRAM是不同的。即使网格没有及时加载到显存,它也不会影响应用程序的行为(您可能只会注意到一些延迟)。但是,如果网格没有及时加载到内存中,可能会导致场景中对象的物理行为不正确。
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.加载网格并在它们存在时将它们保存在内存中。一些基于网格的对象的API提供了开箱即用的功能。它可以部分解决不正确行为的问题,然而,只有少数网格可以保持加载。
-
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.设置Radius预取模式。
- 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.您还可以预加载所有网格计算碰撞和十字路口(Full预抓取模式),但是它将大大增加内存的使用。
- API of some mesh-based objects, as well as the MeshStatic class API provides also methods which allow implementing a custom prefetch logic for pre-loading meshes.一些基于网格(mesh)对象的API,以及MeshStatic类API也提供了允许实现预加载网格的自定义预取逻辑的方法。
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.异步流网格不应该被修改。改变这种网格的唯一方法是使其程序化。一个过程网格是一个通过代码创建的网格,这样的网格有一个特定的流模式——它们总是在创建后保存在内存中,永远不会卸载,直到对象被代码破坏或网格返回到它的正常模式(从源文件流)。基于网格的对象API允许将网格切换到过程模式并应用更改。
OpenGL SettingsOpenGL设置#
Settings and workflow for OpenGL API are slightly different than for DirectX API. OpenGL API的设置和工作流程与DirectX API略有不同。
Under OpenGL, the Data Streaming System engages two intermediate buffers to provide data transfer between CPU and new resource:在OpenGL,数据流系统使用两个中间缓冲区来提供CPU与新资源之间的数据传输:
- Async Buffer used for mesh and texture streaming Async Buffer用于网格和纹理流处理
- Async Buffer Indices used for streaming of vertex indices of meshes Async Buffer Indices用于流化网格的顶点索引
The size of the Async Buffer buffer must correspond to the size of the largest resource (mesh/texture); otherwise, in the case of a larger resource, the buffer will be resized, causing a spike. Async Buffer缓冲区的 大小 必须与最大资源的大小(网格/纹理)相对应,否则,在资源较大的情况下,将调整缓冲区的大小,从而导致峰值。
The Async Buffer Synchronization parameter stands for the mechanism of buffer synchronization. So, async buffers are created only once and then synchronized, reducing the time on allocating and freeing memory. When the synchronization is disabled, both Async Buffer and Async Buffer Indices are created anew for each new resource. This reduces the number of buffer synchronizations but increases the number of memory allocations.如果将Async Buffer Synchronization参数设置为 1 ,则启用缓冲区同步机制。因此,异步缓冲区仅创建一次,然后进行同步,从而减少了分配和释放内存的时间。禁用同步后,将为每个新资源重新创建Async Buffer和Async Buffer Indices。这减少了缓冲区同步的次数,但增加了内存分配的数量。
Sometimes (depending on the hardware/driver used, e.g., when the main thread is affected by synchronization primitives in other threads), memory allocation may be faster than synchronizations; in such cases, when streaming becomes unacceptably slow, it is recommended to disable buffer synchronization.有时(取决于所使用的硬件/驱动程序,例如,当主线程受到其他线程中的同步原语影响时),内存分配可能比同步要快;在这种情况下,当流变得异常缓慢时,建议禁用缓冲区同步。
There are some known issues and workarounds for some hardware/driver software:一些硬件/驱动程序软件存在一些已知的问题和解决方法:
- The Mesa 3D GL: The buffer synchronization must be disabled (gl_async_buffer_synchronization 0) for better performance. The updated Open Graphics Drivers are required. Mesa 3D GL:必须禁用缓冲区同步(gl_async_buffer_synchronization 0),以获得更好的性能。需要更新的 Open Graphics Drivers 。
- Intel: It is necessary to consider that VRAM is limited by OS to one-half of RAM. Intel:有必要考虑到VRAM受操作系统限制为RAM的一半。