【译】Performance tips when writing shaders

来源:互联网 发布:php如何解决高并发问题 编辑:程序博客网 时间:2024/06/10 16:17

Only compute what you need

The more computations and processing your shader code needs to do, the more it will impact the performance of your game. For example, supporting color per material is nice to make a shader more flexible, but if you always leave that color set to white then useless computations are performed for each vertex or pixel rendered on screen.

The frequency of computations will also impact the performance of your game. Usually there are many more pixels rendered (and subsequently more pixel shader executions) than there are vertices (vertex shader executions), and more vertices than objects being rendered. Where possible, move computations out of the pixel shader code into the the vertex shader code, or move them out of shaders completely and set the values in a script.

 

只运算那些你需要的

你着色器中的计算和任务越多,越会影响你游戏的性能表现。举个例子,为每个材质添加一个单独的混合色能让你的着色器适用性更广,但是如果你经常把它设置成白色那么你在屏幕的顶点或像素的混合计算就没用了。

计算的频率也会影响游戏性能。通常来说,像素渲染(伴随而来会有更多的pixel shader被执行)会高于顶点着色的频率,而顶点着色会高于被渲染的对象。所以尽可能的,把PS搬到VS中去,或完全地搬离shader,而去在单独的脚本中去设置shader的参数。

 

Optimized Surface Shaders

Surface Shaders are great for writing shaders that interact with lighting. However, their default options are tuned to cover a broad number of general cases. Tweak these for specific situations to make shaders run faster or at least be smaller:

 

优化表面着色器

Surface Shaders 是写与光有交互的着色器的利器。他们有很多涵盖了一般情况的默认参数。根据特殊情况去设置这些参数可以让你的shader运行得更快,或者起码会让你的shader更加简洁。


· The approxview directive for shaders that use view direction (i.e. Specular) makes the view direction normalized per vertex instead of per pixel. This is approximate, but often good enough.

 

approxview 着色器会用标准视图的每个顶点方向而不是每个像素方向去计算视角方向。这是一种近似方法,但通常来说可以较好地表现。

 

· The halfasview for Specular shader types is even faster. The half-vector (halfway between lighting direction and view vector) is computed and normalized per vertex, and the lighting function receives the half-vector as a parameter instead of the view vector.

 

halfasview  这个参数让高光类shader更加高效。这个半向量(光方向与视图向量的折中向量)会在每个顶点计算和标准化,而光照函数将传递进来的half-direction代替视图向量。

 

· noforwardadd makes a shader fully support one-directional light in Forward rendering only. The rest of the lights can still have an effect as per-vertex lights or spherical harmonics. This is great to make your shader smaller and make sure it always renders in one pass, even with multiple lights present.

 

noforwardadd 禁用正向渲染添加通道(Forward rendering additive pass) 让着色器仅完整支持一个的方向光正向渲染。其他的光源也会影响per-vertex/SH计算。这个对精简你的着色器很有帮助而且确保了即便有多个光源,所有渲染都在一个通道中进行。

 

· noambient disables ambient lighting and spherical harmonics lights on a shader. This can make performance slightly faster.

 

noambient  禁用了环境光和球谐光。这会使性能有些许提升。

Precision of computations

When writing shaders in Cg/HLSL, there are three basic number types: float, half and fixed (see Data Types and Precision).

For good performance, always use the lowest precision that is possible. This is especially important on mobile platforms like iOS and Android. Good rules of thumb are:

For world space positions and texture coordinates, use float precision.

For everything else (vectors, HDR colors, etc.), start with half precision. Increase only if necessary.

For very simple operations on texture data, use fixed precision.

计算的精度

当用Cg/HLSL编写着色器时。有三种数据类型:float, half fixed 参照 Data Types and Precision

为了高性能,通常尽可能的用低精度数据类型。这个对移动平台尤为重要。

例如:

· 对于世界坐标和纹理坐标,用float精度

· 对于任何其他的(比如向量,HDR颜色),先用half精度,如果需要再提升精度。

· 对于那些非常简单的纹理操作,用fixed精度。


In practice, exactly which number type you should use for depends on the platform and the GPU. Generally speaking:

All modern desktop GPUs will always compute everything in full float precision, so float/half/fixed end up being exactly the same underneath. This can make testing difficult, as it’s harder to see if half/fixed precision is really enough, so always test your shaders on the target device for accurate results.

Mobile GPUs have actual half precision support. This is usually faster, and uses less power to do calculations.

Fixed precision is generally only useful for older mobile GPUs. Most modern GPUs (the ones that can run OpenGL ES 3 or Metal) internally treatfixed and half precision exactly the same.

See Data Types and Precision for more details.


实际上,你应该确切地用那种值类型应该依据平台和GPU。总体来说:

所有的当前的台式机GPU通常会把所有数据都用float精度计算,所以 float/half/fixed 最终没有区别。这会让测试变困难,因为你很难去判断half/fixed精度是否够用,所以通常你需要在真机上测试你的着色器来获得准确的数据。

移动GPU 支持half. 这通常来说更加高效,计算更加低耗。

fixed精度大体来说只对那些旧版的移动GPU有效。大多数的当代GPU(OpenGL ES3 或者Metal支持的)内部对待fixed和half精度完全一样。

See Data Types and Precision for more details.


Alpha Testing

The fixed-function AlphaTest - or its programmable equivalent, clip() - has differentperformance characteristics on different platforms:

Generally you gain a small advantage when using it to remove totally transparent pixels on most platforms.

However, on PowerVR GPUs found in iOS and some Android devices, alpha testing is resource-intensive. Do not try to use it for performance optimization on these platforms, as it causes the game to run slower than usual.


固定功能AlphaTest 或者他等价的函数clip() 在不同平台的性能表现不同。
通常来说,应用它们对那些完全透明的像素剔除在大多平台下会有一些性能提升
但是,在IOS和部分安卓平台下,那些 PowerVR GPU alpha-testing会造成大量的资源占用。别要试图在这些平台通过这种方法优化性能,这通常只会让你的游戏更慢。

Color Mask

On some platforms (mostly mobile GPUs found in iOS and Android devices), using ColorMask to leave out some channels (e.g. ColorMask RGB) can be resource-intensive, so only use it if really necessary.


在一些平台(大多数的IOS或者安卓移动平台的GPU),用通道遮罩去去除一些通道会造成资源密集占用,所以仅仅在需要的时候再用它。



原文地址: https://docs.unity3d.com/Manual/SL-ShaderPerformance.html



0 0
原创粉丝点击