GLSL ES

数据类型

标量:int, float, bool

向量:

  • vec2, vec3, vec4
  • ivec2, ivec3, ivec4
  • bvec2, bvec3, bvec4

矩阵:mat2, mat3, mat4

矩阵构造方法为列优先

1
2
mat2 m2 = mat2(	1.0, 2.0, 	//		|1.0 3.0|
3.0, 4.0);// |2.0 4.0|

采样器:sampler2D, samplerCube sampler的数量根据硬件有限制

内置函数(常用)

所有的请看官方API:https://www.khronos.org/registry/OpenGL-Refpages/es3/

  • 角度函数

    1
    2
    float radians(float degree);	// 角度转弧度
    float degrees(float radian); // 弧度转角度
  • 三角函数

    1
    2
    3
    4
    5
    6
    float sin	(float angle);
    float cos (float angle);
    float tan (float angle);
    float asin (float x);
    float acos (float x);
    float atan (float x);
  • 指数函数

    1
    2
    3
    4
    5
    6
    7
    float pow		(float x, float y);
    float exp (float x);
    float log (float x);
    float exp2 (float x);
    float log2 (float x);
    float sqrt (float x);
    float inversesqrt(float x);
  • 通用函数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    float	abs		(float x);
    float min (float x, float y);
    float max (float x, float y);
    float mod (float x, float y); //取余数
    float sign (float x); //取正负号(小于1返回-1 大于1返回1 等于0返回0)
    int sign (int x);
    float floor (float x);
    float ceil (float x);
    float clamp (float x, float minVal, float maxVal);
    int clamp (int x, int minVal, int maxVal);
    float mix (float x, float y, float a); // 线性插值
    float step (float edge, float x);
    float smoothstep(float edge0, float edge1, float x);
    float fract (float x); // 取得小数部分
  • 几何函数

    1
    2
    3
    4
    5
    6
    7
    float	length	(vec3 x);
    float distance(vec3 p0, vec3 p1);
    float dot (vec3 v1, vec3 v2);
    vec3 cross (vec3 v1, vec3 v2);
    vec3 normalize(vec3 v);
    vec3 reflect (vec3 I, vec3 N);
    vec3 refract (vec3 I, vec3 N, float IOR);
  • 矩阵函数

    1
    mat matrixCompMult(	mat x, mat y);	// 矩阵逐元素乘法
  • 矢量函数

    1
    2
    bvec3 lessThan(vec3 x, vec3 y);	// 逐元素对比
    ···
  • 纹理采样函数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    // GL ES 1.0的
    vec4 texture2D (sampler2D sampler, vec2 P);
    vec4 texture2DProj (sampler2D sampler, vec2 P);
    vec4 texture2DLod (sampler2D sampler, vec2 P);
    vec4 textureCube (samplerCube sampler, vec2 P);

    // GL ES 3.0后才支持
    vec4 texture (sampler2D sampler,vec2 P,[float bias]);
    vec4 textureLod (sampler2D sampler,vec2 P,[float bias]);
    vec4 textureProj(sampler2D sampler,vec2 P,[float bias]);
    ···

储存限定符

  • const:常量

  • attribute:表示逐顶点的信息

    只能出现在顶点着色器中

    只能被声明为全局变量

  • uniform:公用信息

    顶点着色器和片元着色器都可以用

    必须是全局变量

    只读

    如果顶点和片元中有名字一样的,那么会被共享

  • varying:逐个片元不同的信息

    全局变量

    从顶点着色器向片元着色器传递信息

    会在光栅化阶段被插值

各种变量的数量限制

变量类别 内置变量(表示最大数量) 至少有多少个
attribute gl_MaxVertexAttribs 8
uniform (VS) gl_MaxVertexUniformVectors 128
uniform (FS) gl_MaxFragmentUniformVectors 16
varying gl_MaxVaryingVectors 8

精度限定符

精度限定符 默认 float 数值范围 默认 int 数值范围
highp (-2^62, 2^62) 精度 2^-16 (-2^16, 2^16)
mediump (-2^14, 2^14) 精度 2^-10 (-2^10, 2^10)
lowp (-2, 2) 精度 2^-8 (-2^8, 2^8)
1
2
3
4
5
lowp vec4 color;
mediump float size;
highp vec4 position;
// 使用 precision
precision mediump float; // 所有浮点数默认为中精度

数据类型的默认精度

数据类型 VS 的默认精度 FS 的默认精度
int highp mediump
float highp
sampler2D lowp lowp
samplerCube lowp lowp

所以说在片元着色器 float 是没有默认精度的,需要手动设置。

预处理指令

内置宏

描述
GL_ES 在OpenGL ES 2.0 被定义为1
GL_FRAGMENT_PRECISION_HIGH 片元主色器中 float 是否支持highp
1
2
3
4
5
6
7
#ifdef GL_ES
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif
#endif
1
2
// #version 可以指定 glsl es 的版本
#version 101 // version 1.01