代码中看到后收集的一些shader学习笔记

来源:互联网 发布:淘宝店铺模版怎么使用 编辑:程序博客网 时间:2024/06/03 21:19


ARB_precision_hint_nicest 的意义,作用:

首先,这2命令最权威解释的地方,肯定就是openGl官网了,以下是链接:
https://www.opengl.org/registry/specs/NV/fragment_program4.txt

这里有这么一段:

+ Precision Hints(ARB_precision_hint_fastest, ARB_precision_hint_nicest)

Fragment program computations are carriedout at an implementation- dependent precision. However, some implementationsmay be able to perform fragment program computations at more than oneprecision, and may be able to trade off computation precision for performance.If a fragment program specifies the "ARB_precision_hint_fastest"program option, implementations should select precision to minimize programexecution time, with possibly reduced precision. If a fragment programspecifies the "ARB_precision_hint_nicest" program option,implementations should maximize the precision, with possibly increasedexecution time. Only one precision control option may be specified by any givenfragment program. A fragment program that specifies both the"ARB_precision_hint_fastest" and "ARB_precision_hint_nicest"program options will fail to load.

看标题也知道了,也就是精度提示。

ARB_precision_hint_fastest最快的,意思就是会用低精度(一般是指fp16),以提升片段着色器的运行速度,减少时间。

ARB_precision_hint_nicest最佳的,意思就是会用高精度(一般是指fp32),可能会降低运行速度,增加时间。

注意:这2个命令不可以同时使用

unity里的写法是#pragma fragmentoption ARB_precision_hint_fastest

具体说fp16fp32是什么的话,就涉及到GPU的知识了,我也不是太懂。
大概说说的话,fp16gpu里一般被称为半精度计算

图形计算中使用的精度是:

FP1616bit,半精度
FX10
10bit,都不是浮点,而是定点数了
FP32
32bit,单精度(最常见的)
FP64
64bit,双精度(不常见的,图像处理一般用不到)

虽然在实际应用中,这2种精度在表现上区别并不大,但是还是建议使用fp16精度的更好。

因为像在 ARM Mali Imagination PowerVR这种移动GPU上,都有独立的fp16单元,也就是说,fp16是从硬件电路级别上被支持的,我们知道,这就意味着一个字,那就是快!,所以你懂的!

不过据说NVIDIAgpu里,都是用fp32 CUDA核心去处理fp16的。(这其实是比单独的fp16单元要慢的)

总结一下就是在写shader时可以顺手把这个命令给加上,但是如果你忘记了,或者懒,其实问题也不是太大

ARB_precision_hint_fastest

如果有UV移动的地方就用不了,Also depends on what platformthe compiler is targetingsome, like iOS 9+ have no performance difference forhalf precision values  // iOS 9+以上没什么性能不同。

fixed and half are the same on iOS, in terms of speed,performance, memory, etc(在性能平台内存方面)

#if UNITY_UV_STARTS_AT_TOP

#if UNITY_UV_STARTS_AT_TOP //判断当前平台是否是DirectX类型
           if ( _MainTex_TexelSize.y < 0 )  //判断是否开启抗锯齿
               uv.y = 1.0-uv.y;
#endif

 

#if UNITY_UV_STARTS_AT_TOP用于判断当前平台是否是DirectX类型,当在该平台下开启了抗锯齿(project Setting->Quality->AntiAliasing)后,主纹理的纹素大小在Y方向上会变负值,以便我们对主纹理正确采样。

SV_POSTION和SV_Target

SV_POSTION描述顶点着色器输出的顶点位置,POSITION无法在PS4平台或使用了细分着色器的情况下工作

SV_Target描述片元着色器的输出颜色,COLOR或者COLOR0无法在PS4或使用了细分着色器的情况下工作

https://docs.unity3d.com/Manual/SL-PlatformDifferences.html不同平台区别

 

fixed(11位) half(16位) float(32位)

·        float32位高精度浮点数。

·        half16位中精度浮点数。范围是[-6, +6],能精确到十进制的小数点后3.3位。它适合存储UV坐标等

·        fixed11位低精度浮点数。范围是[-2, 2],精度是1/256可以用于光照计算存储颜色和单位矢量。

_Time

//_Time是个4维向量,跟Unity3D中的deltaTime(这是个一维的,数值)不同。float4 _Time : Time (t/20, t, t*2, t*3)
       waves += sin(_Time.z * _WaveControl.z * 0.666 + _WaveControl.x *worldPos.z * 1.2);

#pragma target 3.0

 

https://docs.unity3d.com/Manual/SL-ShaderCompileTargets.html

#pragma target 3.0

·        DX9 shader model 3.0: derivativeinstructions, texture LOD sampling, 10 interpolators, more math/textureinstructions allowed.

·        Not supported on DX11 feature level 9.xGPUs (e.g. most Windows Phone devices).

·        Might not be fully supported by someOpenGL ES 2.0 devices, depending on driver extensions present and featuresused.

 


Unity 3D ShaderLab开发实战详解 :适合shader中级开发人员

链接:http://pan.baidu.com/s/1jIhLTFg 密码:dkzo


0 0
原创粉丝点击