如果用GLSL ES编写的着色器,浮点精确度规定如下:
- highp – 32位浮点格式,适合用于顶点变换,但性能最慢。
- mediump – 16位浮点格式,适用于纹理UV坐标和比highp 大约快两倍
- lowp – 10位的顶点格式,适合对颜色,照明计算和其它高性能操作,速度大约是highp 的4倍
如果是用CG编写的着色器或是一个表面着色器,指定精度如下:
- float – 类似于在GLSL ES 的highp ,最慢
- half – 类似于在GLSL ES 的mediump ,比float大约快两倍
- fixed – 类似于在GLSL ES的lowp,速度大约是float 的4倍使用技巧
(1)指定变量精度(放在数据类型之前):
- highp vec4 position;
- varying lowp vec4 color;
- mediump float specularExp;
(2)指定默认精度(放在Vertex和Fragment shader源码的开始处):
- precision highp float;
- precision mediump int;
在Vertex Shader中,如果没有默认的精度,则float和int精度都为highp;在Fragment Shader中,float没有默认的精度,所以必须在Fragment Shader中为float指定一个默认精度或为每个float变量指定精度。
着色语言定了三种级别的精度:lowp, mediump, highp。
highp
has a floating point magnitude range of 2^-62 to 2^62, and a floating point precision (relative) of 2^-16.
Whereas mediump
has a floating point magnitude range of 2^-14 to 2^14, and floating point precision (relative) of 2^-10.