This page has been translated automatically.
视频教程
界面
要领
高级
实用建议
基础
专业(SIM)
UnigineEditor
界面概述
资源工作流程
Version Control
设置和首选项
项目开发
调整节点参数
Setting Up Materials
设置属性
照明
Sandworm
使用编辑器工具执行特定任务
如何擴展編輯器功能
嵌入式节点类型
Nodes
Objects
Effects
Decals
光源
Geodetics
World Nodes
Sound Objects
Pathfinding Objects
Players
编程
基本原理
搭建开发环境
使用范例
C++
C#
UnigineScript
统一的Unigine着色器语言 UUSL (Unified UNIGINE Shader Language)
Plugins
File Formats
材质和着色器
Rebuilding the Engine Tools
GUI
双精度坐标
应用程序接口
Animations-Related Classes
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Objects-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
IG Plugin
CIGIConnector Plugin
Rendering-Related Classes
VR-Related Classes
创建内容
内容优化
材质
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
Art Samples
Tutorials

强制着色器编译

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程序。 在运行时按需编译着色器会导致冻结,并需要额外的内存。 为了减少这种开销,所有必需的着色器组合都会被解析,编译并存储在着色器缓存中。 从缓存加载预编译的着色器要快得多。

注意
DirectX shaders cache generated for debug builds should not be used for release builds, as shader compilation is performed without optimizations. Re-generate DirectX shaders cache for your release build for maximum performance (ensured by optimized compilation).为调试版本生成的DirectX着色器缓存不应用于发行版本,因为着色器编译是在没有优化的情况下执行的。重新生成DirectX着色器缓存以用于您的发行版,以实现最佳性能(通过优化编译确保)。

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”(冻结一会儿)。对于许多应用程序(例如实时模拟器),这是完全不可接受的。因此,关键是您应该拥有最完整的着色器缓存,其中包含应用程序中使用的所有变体。

注意
The shaders cache should be generated for float- and double-precision builds separately, as each of them uses its own shader cache (i.e. cache generated for float precision is not suitable for a double-precision build). In case your application is released with both builds float and double, you'll have to generate shaders cache twice.应该分别为浮点精度和双精度生成生成着色器缓存,因为它们每个都使用自己的着色器缓存(即,为浮点精度生成的缓存不适合于双精度生成)。如果您的应用程序同时使用float和double版本发布,则必须两次生成着色器缓存。

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部分可以使用此功能。

Precompile All Shaders option on the toolbar and in the Editor section of the Settings window工具栏上和Settings窗口的Editor部分中的Precompile All Shaders选项

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:应该执行以下操作来为您的项目生成最完整的着色器缓存:

  1. Open all worlds, that contain geodetic pivot, terrain, water, or clouds.打开所有包含大地坐标,地形,水或云的世界。
  2. 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.
    对于这些世界,请执行以下操作:
    • 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. -将场景摄像机放在云之上,云之下和云内部。
  3. Create and launch tracks for all material states, that you plan to switch via your application's logic.为所有计划通过应用程序逻辑进行切换的材质状态创建并启动轨道
注意
To minimize run-time recompilation of shaders for materials, whose states are to be changed via the application logic, you can also create separate materials having the required state values and switch between these materials instead of changing states.为了最大程度地减少材质的着色器的运行时重新编译,材质的状态将通过应用程序逻辑进行更改,您还可以创建具有所需状态值的单独材质,并在这些材质之间切换,而不用更改状态。

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
警告
Do not put shader cache files to UNG archives! You will not have a substantial file size reduction, as cache files already use LZ4 compression. Moreover, in this case new shaders, that can be generated on your end user's side, won't be added to cache.请勿将着色器缓存文件放入UNG存档!由于缓存文件已经使用LZ4压缩,因此文件大小不会显着减少。此外,在这种情况下,可以在最终用户方面生成的新着色器将不会添加到缓存中。

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"
you should copy shader cache files to the following directory:如果您通过命令行或引擎初始化传递了项目名称

命令行
-project_name "YourProject"
您应该将着色器缓存文件复制到以下目录:
  • Windows - C:/Users/<username>/YourProject/
  • Linux - /home/<username>/.YourProject/
最新更新: 2024-08-16
Build: ()