强制着色器编译
A shader is a small program executed on the GPU, and of course it takes some time to get compiled and loaded. Usually, compiling and loading a single GPU program does not take much time, but shaders often have a lot of "variants". Complex shaders have multiple preprocessor directives (defines) for various cases, each combination of these directives defines a shader variant. So we end up having to compile many thousands of slightly different GPU programs. Compiling shaders on demand at runtime causes freezes and requires extra memory. In order to reduce overhead, all required shader combinations are parsed, compiled, and stored in the shader cache. Loading precompiled shaders from cache is much faster. 着色器是在GPU上执行的小程序,当然需要花费一些时间来进行编译和加载。 通常,编译和加载单个GPU程序不需要花费很多时间,但是着色器通常具有很多“变体”。 复杂的着色器在各种情况下都有多个预处理器指令(定义),这些指令的每种组合都定义了一个着色器变体。 因此,我们最终不得不编译成千上万个稍有不同的GPU程序。 在运行时按需编译着色器会导致冻结,并需要额外的内存。 为了减少这种开销,所有必需的着色器组合都会被解析,编译并存储在着色器缓存中。 从缓存加载预编译的着色器要快得多。
There are no special requirements for shaders caching such as installing or setting up any additional software.对着色器缓存没有特殊要求,例如安装或设置任何其他软件。
Every time when the Engine fails to retrieve a shader from the cache, the required shader is compiled and added to cache automatically. However, this may cause your application to "hiccup" (freeze for a while). Which is totally unacceptable for many applications (e.g. real-time simulators). So, the point is that you should have the most complete shader cache containing all variants that are used in your application.每次引擎未能从缓存中检索着色器时,所需的着色器都会被编译并自动添加到缓存中。但是,这可能会导致您的应用程序“打h”(冻结一会儿)。对于许多应用程序(例如实时模拟器),这是完全不可接受的。因此,关键是您应该拥有最完整的着色器缓存,其中包含应用程序中使用的所有变体。
Precompilation of all shaders is an important feature guaranteeing, that at the time your project is completed and final builds are ready to be handed over to customers, you will have a complete shader cache. This significantly reduces the number of freezes in the final release build and smooths your application's framerate. Shaders are precompiled for all materials in the project. 所有着色器的预编译是一项重要的功能,可确保在您的项目完成并且最终版本可以交付给客户时,您将拥有完整的着色器缓存。这显着减少了最终版本构建中的冻结次数,并平滑了应用程序的帧速率。已为项目中的所有材质预先编译了着色器。
This feature is available via the Precompile All Shaders button on the toolbar near the Play button or via the Editor section of the Settings window when a world is loaded.通过加载工具栏上Play按钮附近的工具栏上的Precompile All Shaders按钮或通过加载世界时通过Settings窗口的Editor部分可以使用此功能。
You can use it in one of the following ways:您可以通过以下方式之一使用它:
-
Constant background precompilation 恒定的后台预编译
When this button is enabled, each time you create a new material, you'll see the following message on shader compilation in the viewport:启用此按钮后,每次创建新材质时,您将在视口中的着色器编译中看到以下消息:
You can stop the process of shaders cache generation in case the UnigineEditor's UI response has become too slow via the corresponding button right on the compilation message.如果通过编译消息上的相应按钮使UnigineEditor的UI响应变得太慢,则可以停止着色器缓存的生成过程。Materials, for which the shaders were not yet compiled, will be temporarily replaced with base ones colored in grey and having no textures. This may take a couple of seconds depending on your computer's performance. This option guarantees, that you always have a complete shader cache for the selected graphics API.尚未为其编译着色器的材质将临时替换为灰色且没有纹理的基础材质。根据您计算机的性能,这可能需要花费几秒钟的时间。此选项可确保您始终具有所选图形API的完整着色器缓存。
Precompilation on demand 按需预编译
Sometimes constant background compilation of shaders may disturb and slow down your work, this is typical for some engines. UNIGINE lets you disable shaders precompilation. You can enable it just before measuring performance of your final build. In this case shader cache generation will take much more time (up to half an hour) depending on your project's complexity and selected graphics API. But it will be performed only once instead of slowing down your work each time you make changes to your materials.有时,不断进行的着色器后台编译可能会干扰并减慢您的工作速度,这对于某些引擎而言是典型的。 UNIGINE使您可以禁用着色器预编译。您可以在评估最终版本的性能之前启用它。在这种情况下,着色器缓存的生成将花费更多的时间(最多半小时),具体取决于项目的复杂性和所选的图形API。但是它只会执行一次,而不是每次更改材质时都会减慢您的工作。
This mode is recommended for graphics programmers to save time on recompilation when changing states of materials.建议图形程序员使用此模式,以在更改材质状态时节省重新编译的时间。
Forced shader precompilation ensures, that you have the most complete world-independent shaders cache for all materials in your project. However, changing some local settings, such as the ones for geodetic pivot, terrain, water, or clouds (e.g. changing some material state via the Tracker or your application logic) still leads to recompilation of shaders, but just a local one performed only for the corresponding type of objects. 强制着色器预编译可确保为项目中的所有材质提供最完整的独立于世界的着色器缓存。 但是,更改某些本地设置,例如用于大地坐标,地形,水或云的设置(例如,通过Tracker或您的应用程序逻辑更改某些材质状态)仍会导致重新编译着色器,但仅仅是重新编译一种仅针对相应类型的对象执行。
The following actions should be performed to generate the most complete shader cache for your project:应该执行以下操作来为您的项目生成最完整的着色器缓存:
- Open all worlds, that contain geodetic pivot, terrain, water, or clouds.打开所有包含大地坐标,地形,水或云的世界。
- For each of these worlds perform the following:
- water - put a scene camera over water, under water and at the waterline between the two media.water - put a scene camera over water, under water and at the waterline between the two media.
- terrain - put a scene camera high above the terrain and move it to the terrain surface.terrain - put a scene camera high above the terrain and move it to the terrain surface.
- clouds - put a scene camera over above the clouds, under the clouds and inside the clouds.clouds - put a scene camera over above the clouds, under the clouds and inside the clouds.
- water - put a scene camera over water, under water and at the waterline between the two media. 水-将场景摄影机置于水上,水下和两种介质之间的水线处。
- terrain - put a scene camera high above the terrain and move it to the terrain surface. 地形-将场景摄像机放在地形上方,然后将其移至地形表面。
- clouds - put a scene camera over above the clouds, under the clouds and inside the clouds. 云-将场景摄像机放在云之上,云之下和云内部。
- Create and launch tracks for all material states, that you plan to switch via your application's logic.为所有计划通过应用程序逻辑进行切换的材质状态创建并启动轨道。
Output shader cache for DirectX / Vulkan is generated inside the data folder:在data文件夹内生成DirectX / Vulkan的输出着色器缓存:
- DirectX 11 — shader_d3d11.cache
- DirectX 12 — shader_d3d12_dxc.cache, pso_log_d3d12_dxc.cache
- Vulkan — shader_vk.cache, pso_log_vk.cache
As your project is completed and final builds are ready to be handed over to customers, you should copy all shader cache files to the data directory of the final build.当您的项目完成并且最终版本准备好交付给客户端时,您应该将所有缓存文件复制到最终版本的 data 目录中。
If you pass a project name via the command line or on engine initialization: -project_name "YourProject"
-project_name "YourProject"
- Windows - C:/Users/<username>/YourProject/
- Linux - /home/<username>/.YourProject/