opengl glsl 语法介绍

以下内容来自于 The OpenGL® Shading Language

基本类型

类型 含义
void 空类型,函数无返回值时必须声明为 void
bool、int、float 值类型
vec2、vec3、vec4 n 维向量,值为 float
bvec2、bvec3、bvec4 n 维向量,值为 bool
ivec2、ivec3、ivec4 n 维向量,值为 int
mat2、mat3、mat4 n*n 矩阵,值为 float
mat2x2、mat2x3、mat2x4 m*n 矩阵,值为 float
mat3x2、mat3x3、mat3x4 m*n 矩阵,值为 float
mat4x2、mat4x3、mat4x4 m*n 矩阵,值为 float
sampler1D、sampler2D、sampler3D n 维纹理的句柄,只能用作函数参数或者定义为 uniform
samplerCube 立方体贴图纹理句柄
sampler1DShadow、sampler2DShadow 带对比的 n 维深度纹理

修饰符

类型 含义
const 常量,必须声明时初始化
attribute 顶点属性,只读,只用于顶点着色器中,只能被声明为全局变量,一般用来表示顶点数据(顶点坐标、顶点法线、顶点颜色等)
uniform 一致变量,类似 const,不能被 shader 修改
varying 易变变量,用于 vertex 和 fragment 之间做数据传递,在 vs 中可读可写,fs 只读
   
in 用于函数参数,参数默认修饰符,表示传入,函数内部修改不会传递到外部
out 用于函数参数,表示传出,函数内部可写不可读
inout 用于函数参数,可读 & 可写 & 可传出
invariant  

内置变量

Vertex Shader Special Variables

类型 含义 含义  
vec4 gl_Position 齐次坐标下的顶点位置 required  
float gl_PointSize contains size of rasterized points, in pixels optional  
vec4 gl_ClipVertex   optional  

Vertex Shader Built-In Attributes

类型 含义 其他
attribute vec4 gl_Color 顶点颜色  
attribute vec4 gl_SecondaryColor 辅助顶点颜色  
attribute vec3 gl_Normal 顶点法线  
attribute vec4 gl_Vertex 顶点物体空间坐标  
attribute vec4 gl_MultiTexCoord[0-7] 顶点的第 n 个纹理坐标  
attribute float gl_FogCoord 顶点的雾坐标  
varying vec4 gl_TexCoord[]    
varying float gl_FogFragCoord    
varying vec2 gl_PointCoord    

Fragment Shader Special Variables

类型 含义 其他
vec4 gl_FragCoord 顶点颜色  
bool gl_FrontFacing 辅助顶点颜色  
vec4 gl_FragColor 顶点法线  
vec4 gl_FragData[gl_MaxDrawBuffers] 顶点物体空间坐标  
float gl_FragDepth 顶点的第 n 个纹理坐标  

内置常量值(Built-In Constants)

ARB:全称 OpenGL Architecture Review Board,简称ARB,中文含义为“OpenGL架构评审委员会”。

类型 含义 备注
const int gl_MaxLights = 8 最多 8 个光源 GL 1.0
const int gl_MaxClipPlanes = 6 最大剪切区域数 GL 1.0
const int gl_MaxTextureUnits = 2   GL 1.3
const int gl_MaxTextureCoords = 2 最大纹理数 ARB_fragment_program
const int gl_MaxVertexAttribs = 16 可以声明的 vertex attribute 的最大数量 ARB_vertex_shader
const int gl_MaxVertexUniformComponents = 512 可以声明的 Uniform 变量的最大数量 ARB_vertex_shader
const int gl_MaxVaryingFloats = 32 Varying 变量的最大个数 ARB_vertex_shader
const int gl_MaxVertexTextureImageUnits = 0   ARB_vertex_shader
const int gl_MaxCombinedTextureImageUnits = 2   ARB_vertex_shader
const int gl_MaxTextureImageUnits = 2   ARB_fragment_shader
const int gl_MaxFragmentUniformComponents = 64   ARB_fragment_shader
const int gl_MaxDrawBuffers = 1 最多可绘制的 buffer 数量 proposed ARB_draw_buffers

Built-In Uniform State

Varying Variables

类型 含义
varying vec4 gl_FrontColor  
varying vec4 gl_BackColor  
varying vec4 gl_FrontSecondaryColor  
varying vec4 gl_BackSecondaryColor  

内置函数

函数 备注
genType radians (genType degrees)  
genType degrees (genType radians)  
genType sin (genType angle)  
genType cos (genType angle)  
genType tan (genType angle)  
genType asin (genType x)  
genType acos (genType x)  
genType atan (genType y, genType x)  
genType atan (genType y_over_x)  
   
genType pow (genType x, genType y)  
genType exp (genType x)  
genType log (genType x)  
genType exp2 (genType x)  
genType log2 (genType x)  
genType sqrt (genType x)  
genType inversesqrt (genType x)  
   
genType abs (genType x)  
genType sign (genType x)  
genType floor (genType x)  
genType ceil (genType x)  
genType fract (genType x)  
genType mod (genType x, float y)  
genType mod (genType x, genType y)  
genType min (genType x, genType y)  
genType min (genType x, float y)  
genType max (genType x, genType y)  
genType max (genType x, float y)  
genType clamp (genType x, genType minVal, genType maxVal)  
genType clamp (genType x, float minVal, float maxVal)  
genType mix (genType x, genType y, genType a)  
genType mix (genType x, genType y, float a)  
genType step (genType edge, genType x)  
genType step (float edge, genType x)  
genType smoothstep (genType edge0, genType edge1, genType x)  
genType smoothstep (float edge0, float edge1, genType x)  
   
float length (genType x)  
float distance (genType p0, genType p1)  
float dot (genType x, genType y)  
vec3 cross (vec3 x, vec3 y)  
genType normalize (genType x)  
vec4 ftransform()  
genType faceforward(genType N, genType I, genType Nref)  
genType reflect (genType I, genType N)  
genType refract(genType I, genType N, float eta)  
   
mat matrixCompMult (mat x, mat y)  
mat2 outerProduct(vec2 c, vec2 r)  
mat3 outerProduct(vec3 c, vec3 r)  
mat4 outerProduct(vec4 c, vec4 r)  
mat2x3 outerProduct(vec3 c, vec2 r)  
mat3x2 outerProduct(vec2 c, vec3 r)  
mat2x4 outerProduct(vec4 c, vec2 r)  
mat4x2 outerProduct(vec2 c, vec4 r)  
mat3x4 outerProduct(vec4 c, vec3 r)  
mat4x3 outerProduct(vec3 c, vec4 r)  
mat2 transpose(mat2 m)  
mat3 transpose(mat3 m)  
mat4 transpose(mat4 m)  
mat2x3 transpose(mat3x2 m)  
mat3x2 transpose(mat2x3 m)  
mat2x4 transpose(mat4x2 m)  
mat4x2 transpose(mat2x4 m)  
mat3x4 transpose(mat4x3 m)  
mat4x3 transpose(mat3x4 m)  
   
bvec lessThan(vec x, vec y)  
bvec lessThan(ivec x, ivec y)  
bvec lessThanEqual(vec x, vec y)  
bvec lessThanEqual(ivec x, ivec y)  
bvec greaterThan(vec x, vec y)  
bvec greaterThan(ivec x, ivec y)  
bvec greaterThanEqual(vec x, vec y)  
bvec greaterThanEqual(ivec x, ivec y)  
bvec equal(vec x, vec y)  
bvec equal(ivec x, ivec y)  
bvec equal(bvec x, bvec y)  
bvec notEqual(vec x, vec y)  
bvec notEqual(ivec x, ivec y)  
bvec notEqual(bvec x, bvec y)  
bool any(bvec x)  
bool all(bvec x)  
bvec not(bvec x)  
   
vec4 texture1D (sampler1D sampler, float coord [, float bias] )  
vec4 texture1DProj (sampler1D sampler, vec2 coord [, float bias] )  
vec4 texture1DProj (sampler1D sampler, vec4 coord [, float bias] )  
vec4 texture1DLod (sampler1D sampler, float coord, float lod)  
vec4 texture1DProjLod (sampler1D sampler, vec2 coord, float lod)  
vec4 texture1DProjLod (sampler1D sampler, vec4 coord, float lod)  
vec4 texture2D (sampler2D sampler, vec2 coord [, float bias] )  
vec4 texture2DProj (sampler2D sampler, vec3 coord [, float bias] )  
vec4 texture2DProj (sampler2D sampler, vec4 coord [, float bias] )  
vec4 texture2DLod (sampler2D sampler, vec2 coord, float lod)  
vec4 texture2DProjLod (sampler2D sampler, vec3 coord, float lod)  
vec4 texture2DProjLod (sampler2D sampler, vec4 coord, float lod)  
vec4 texture3D (sampler3D sampler, vec3 coord [, float bias] )  
vec4 texture3DProj (sampler3D sampler, vec4 coord [, float bias] )  
vec4 texture3DLod (sampler3D sampler, vec3 coord, float lod)  
vec4 texture3DProjLod (sampler3D sampler, vec4 coord, float lod)  
vec4 textureCube (samplerCube sampler, vec3 coord [, float bias] )  
vec4 textureCubeLod (samplerCube sampler, vec3 coord, float lod)  
vec4 shadow1D (sampler1DShadow sampler, vec3 coord [, float bias] )  
vec4 shadow2D (sampler2DShadow sampler, vec3 coord [, float bias] )  
vec4 shadow1DProj (sampler1DShadow sampler, vec4 coord [, float bias] )  
vec4 shadow2DProj (sampler2DShadow sampler, vec4 coord [, float bias] )  
vec4 shadow1DLod (sampler1DShadow sampler, vec3 coord, float lod)  
vec4 shadow2DLod (sampler2DShadow sampler, vec3 coord, float lod)  
vec4 shadow1DProjLod(sampler1DShadow sampler, vec4 coord, float lod)  
vec4 shadow2DProjLod(sampler2DShadow sampler,vec4 coord, float lod)  
   
genType dFdx (genType p)  
genType dFdy (genType p)  
genType fwidth (genType p)  
   
float noise1 (genType x)  
vec2 noise2 (genType x)  
vec3 noise3 (genType x)  
vec4 noise4 (genType x)  

https://blog.csdn.net/jeffasd/article/details/52139262
https://github.com/wshxbqq/GLSL-Card
https://learnopengl-cn.readthedocs.io/zh/latest/04%20Advanced%20OpenGL/08%20Advanced%20GLSL/
https://zhuanlan.zhihu.com/p/349296191
https://docs.cocos.com/creator/manual/zh/shader/glsl.html
https://developer.mozilla.org/zh-CN/docs/Games/Techniques/3D_on_the_web/GLSL_Shaders