Optimizing HLSL Shaders( HLSL Shaders的优化)

翻译自https://msdn.microsoft.com/en-us/library/windows/desktop/cc627119%28v=vs.85%29.aspx


前言:

   学了shader也有3个月了,第一次翻译官网的资料,主要是让自己更深刻的理解和分享好东西给大家,希望对那些有shader编程的伙伴们有所帮助,在翻译的过程中可能有会有诸多地方翻译不准确,希望大家多多批评,共同进步,谢谢大家!大笑

写在最前面:

This section describes general-purpose strategies that you can use to optimize your shaders. You can apply these strategies to shaders that are written in any language, on any platform.

本节描述的通用策略,您可以使用它们来优化你的阴影。可以将这些策略应用到着色器,是用任何语言编写的,在任何平台。


主要的内容如下:

Know Where To Perform Shader Calculations

知道去哪里执行着色器的计算

Vertex shaders perform operations that include fetching vertices and performing matrix transformation of vertex data. Typically, vertex shaders are executed once per vertex.

顶点着色器执行操作,包括抓取顶点和顶点数据的执行矩阵转换。通常,每个顶点顶点着色器执行一次。

Pixel Shaders perform operations that include fetching texture data and performing lighting calculations. Typically, pixel shaders are executed once per pixel for a given piece of geometry.

像素着色器,包括提取纹理数据执行操作和执行照明计算。对于一个给定的几何,像素着色器通常是执行一次每像素。

Typically, pixels outnumber vertices in a scene, so pixel shaders execute more often than vertex shaders.

一般来说,像素比顶点在一个场景,所以像素着色器比顶点着色器执行的更多。


When you design shader algorithms, keep the following in mind:

当您设计着色器算法时,请记住以下几点:

  • Perform calculations on the vertex shader if possible. A calculation that is performed on a pixel shader is much more expensive than a calculation that is performed on a vertex shader.
     在顶点着色器执行计算,如果可能的话。在像素着色器上执行的计算是比在一个顶点着色器上执行的计算昂贵得多
  • Consider using per-vertex calculations to improve performance in situations such as dense meshes. For dense meshes, per-vertex calculations may produce results that are visually indistinguishable from results produced with per-pixel calculations.
   考虑使用逐顶点计算在密集的网格等的情况下去提高性能。对于 密集的网格,逐顶点计算产生的结果在视觉上可能和逐像素计算产生的结果是不能辨别的。

Skip Unnecessary Instructions

跳过不必要的指令

   In HLSL, dynamic branching provides the ability to limit the number of instructions that are executed. Therefore, dynamic branching can help speed up shader execution time. If geometry or pixels are not displayed, use dynamic branching to exit the shader or to limit instructions. For example, if a pixel is not lit, there is no point in executing the lighting algorithm.

   在HLSL、动态分支能够限制指令执行的数量。因此,动态分支可以帮助加快材质执行时间。如果几何或像素不显示,使用动态分支退出材质或限制指令。例如,如果一个像素不是照亮的,是毫无意义的执行照明算法。

   The following table lists some cases where you can test conditions in your shader and use dynamic branching to skip unnecessary instructions. The table not comprehensive. Rather, it is intended to give you ideas for optimizing your code.
   下表列出了一些情况下,你可以在你的材质使用动态分支测试去跳过不必要的指令。表不全面。当然更多的是,它的目的是给你想法优化你的代码。


 Pack Variables and Interpolants
打包变量和插值

   Be mindful of the space required for shader data. Pack as much information into a variable or interpolant as possible. Sometimes, the information from two variables can be packed into the memory space of a single variable.

    要注意材质数据所需的空间。把尽可能多的信息打包到一个变量或插值中去。有时,来自两个变量的信息可以打包到单一变量的内存空间。

Reduce Shader Complexity
减少材质的复杂性

 Keep your shaders small and simple. In general, shaders with fewer instructions execute more quickly than shaders with more instructions. It is also easier to debug and optimize smaller, less complex shaders.

  保持你的着色器小而简单。一般来说,用更少的指令的着色器比用更多指令的着色器执行的更快。也容易调试和优化更小、更不复杂的着色器。

Related Topics

涉及的主题


      




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值