opengl glsl 语法介绍

252 阅读4分钟

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

table, th, td { border-collapse: separate; border-spacing:2px 1px; border-width: 1px; } tr{background: #f6f6f6;}

基本类型

类型含义
void空类型,函数无返回值时必须声明为 void
bool、int、float值类型
vec2、vec3、vec4n 维向量,值为 float
bvec2、bvec3、bvec4n 维向量,值为 bool
ivec2、ivec3、ivec4n 维向量,值为 int
mat2、mat3、mat4n*n 矩阵,值为 float
mat2x2、mat2x3、mat2x4m*n 矩阵,值为 float
mat3x2、mat3x3、mat3x4m*n 矩阵,值为 float
mat4x2、mat4x3、mat4x4m*n 矩阵,值为 float
sampler1D、sampler2D、sampler3Dn 维纹理的句柄,只能用作函数参数或者定义为 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_PointSizecontains size of rasterized points, in pixelsoptional
vec4 gl_ClipVertexoptional

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 = 2GL 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 = 32Varying 变量的最大个数ARB_vertex_shader
const int gl_MaxVertexTextureImageUnits = 0ARB_vertex_shader
const int gl_MaxCombinedTextureImageUnits = 2ARB_vertex_shader
const int gl_MaxTextureImageUnits = 2ARB_fragment_shader
const int gl_MaxFragmentUniformComponents = 64ARB_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)

blog.csdn.net/jeffasd/art…
github.com/wshxbqq/GLS…
learnopengl-cn.readthedocs.io/zh/latest/0…
zhuanlan.zhihu.com/p/349296191
docs.cocos.com/creator/man…
developer.mozilla.org/zh-CN/docs/…