Unity3D - 图形性能优化:优化着色器加载时间

21 篇文章 1 订阅
18 篇文章 8 订阅

Unity官方文档之“图形性能优化-优化着色器加载时间”翻译,E文链接

Optimizing Shader Load Time 优化着色器加载时间

Shaders are small programs that execute on the GPU, and loading them can take some time. Each individual GPU program typically does not take much time to load, but shaders often have a lot of “variants” internally.

着色器是在GPU上执行的小程序,加载它们需要一些时间。每个独立的GPU程序一般不需要多少时间加载,但是着色器常常有很多内在的“变体”。

For example, the Standard shader, if fully compiled, ends up being many thousands of slightly different GPU programs. This creates two potential problems:

比如,标准着色器如果完全编译,会生成几千个不同的小GPU程序。这会造成两个潜在的问题:

  • Large numbers of these shader variants increase game build time, and game data size.
  • Loading large numbers of shader variants during game is slow and takes up memory.
  • 这些着色器的大量变体会增加游戏打包时间,使游戏包变大。
  • 在游戏中加载大量的着色器变体很慢,并且消耗内存。

Shader build time stripping 着色器生成时去除

While building the game, Unity can detect that some of the internal shader variants are not used by the game, and skip them from build data. Build-time stripping is done for:

生成游戏包时,Unity可以检测到某些内在着色器变体没有被使用,然后忽略它们。生成时去除可用于:

  • Individual shader features, for shaders that use #pragma shader_feature. If none of the used materials use a particular variant, then it is not included into the build. Seeinternal shader variants documentation. Out of built-in shaders, the Standard shader uses this.
  • Shader variants to handle Fog and Lightmapping modes not used by any of the scenes are not included into the game data. See Graphics Settings if you want to override this behavior.
  • 个别着色器特性,使用#pragma着色器特性的着色器。如果一个变体没有被任何用到的材质使用,那么生成时就不把它打进去。参考内在着色器文档,内置着色器中的标准着色器使用这种方式。
  • 没被任何场景使用的处理雾和光照贴图模式的着色器变体,也不会打包到游戏数据中。如果你想改变这个行为,请参考文档图形设置

Combination of the above often substantially cuts down on shader data size. For example, a fully compiled Standard shader would take several hundred megabytes, but in typical projects it often ends up taking just a couple megabytes (and is often compressed further by the application packaging process).

以上方式的结合可以大大减少着色器数据的大小。比如,完全编译的标准着色器有几百兆,但是在一般的工程中常常只有几兆(而且app打包时会被进一步压缩)。

Default Unity shader loading behavior 默认Unity着色器加载行为

Under all default settings, Unity loads the shaderlab Shader object into memory, but does not create the internal shader variants until they are actually needed.

默认设置下,Unity加载shaderlab着色器对象到内存中,但是内部着色器变体直到被真正用到的时候才创建。

This means that shader variants that are included into the game build can still potentially be used, but there’s no memory or load time cost paid until they are needed. For example, shaders always include a variant to handle point lights with shadows, but if you never end up using a point light with shadows in your game, then there’s no point in loading this particular variant.

这意味着打进游戏包中的着色器变体潜在的可能被用到,但是在用到之前没有占用内存或者消耗时间加载。比如,着色器常常包含一个处理点光源阴影的变体,但是如果你的游戏里从来没有用到点光源阴影,那么加载这个变体就没必要。

One downside of this default behavior, however, is a possible hiccup for when some shader variant is needed for the first time - since a new GPU program code has to be loaded into the graphics driver. This is often undesirable during gameplay, so Unity has ShaderVariantCollection assets to help solve that.

然而,这种默认行为的一个弊端是一些着色器变体第一次被用到的时候可能会出现卡顿(hiccup)——因为需要加载一个新的GPU程序代码到图形驱动。在游戏中这是不能接受的,所以Unity有一个叫ShaderVariantCollection资源来帮助解决这个问题。

Shader Variant Collections 着色器变体群

ShaderVariantCollection is an asset that is basically a list of Shaders, and for each of them, a list of Pass types and shader keyword combinations to load.

ShaderVariantCollection基本上是一个着色器列表资源,每次加载一个由通道类型和着色器关键字组合的列表

Shader variant collection inspector
Shader variant collection inspector  着色器变体群检视器

To help with creating these assets based on actually used shaders and their variants, the editor can track which shaders and their variants are actually used. In Graphics Settings, there is a button to create a new ShaderVariantCollection out of currently tracked shaders, or to clear the currently tracked shader list.

为了帮助创建这些基于被真正用到的着色器和它们变体的资源,编辑器可以追踪哪些着色器和它们的变体被真正用到了。当前追踪着色器的图形设置检视器上,有一个按钮可以创建一个新的ShaderVariantCollection,也可以清除当前追踪的着色器列表。

Creating ShaderVariantCollection from shaders used by editor
Creating ShaderVariantCollection from shaders used by editor 从编辑器使用的着色器创建ShaderVariantCollection

Once you have some ShaderVariantCollection assets, you can set for these variants to be automatically preloaded while loading the game (under Preloaded Shaders list in Graphics Settings), or you can preload an individual shader variant collection from a script. See ShaderVariantCollection scripting class.

一旦有了一些ShaderVariantCollection资源,你可以设置这些着色器变体在加载游戏时自动预加载(在图形设置的预加载着色器列表下),或者你也可以用脚本预加载一个独立的着色器变体。参考ShaderVariantCollection脚本类。

See Also 另外可参考


  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Unity3D-XRInput是一个简单易懂的XR输入系统,专为Unity3D开发者设计。XRInput提供了一种集成虚拟现实和增强现实硬件设备的解决方案,帮助开发者更方便地处理虚拟现实设备的输入操作。 XRInput具有以下特点: 1. 简单易用:XRInput提供了一套简化的API接口,开发者可以轻松地获取XR设备的输入信息。无论是手柄、头戴式显示器或其他XR硬件设备,都可以通过XRInput统一管理。 2. 多平台兼容:XRInput支持大部分主流的VR和AR平台,包括Oculus Rift、HTC Vive、Windows Mixed Reality等。开发者可以无需关注具体设备的差异,只需使用XRInput即可适配多个平台。 3. 扩展性强:XRInput提供了可扩展的功能,开发者可以根据自己的需求进行定制。例如,可以添加自定义的手势识别算法,或者扩展新的输入设备。 4. 与Unity集成:XRInput与Unity3D紧密集成,无需额外的配置和插件。开发者可以直接在Unity编辑器中使用XRInput进行虚拟现实应用程序的开发。 5. 支持常见输入操作:XRInput支持常见的输入操作,如位置追踪、手势识别、触摸输入等。开发者可以根据需要处理这些输入操作,以实现更丰富的交互体验。 总之,Unity3D-XRInput是一个简单易懂的XR输入系统,为Unity开发者提供了更便捷的虚拟现实设备输入管理,帮助开发者节省时间和精力,快速开发出高质量的XR应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值