着色器语言

创建日期:2024-06-21
更新日期:2025-01-12

1、着色器语言字符集

a-z, A-Z, _

0-9

.+-/*%<>[](){}^|&=!:;,?

space, tab, carriage-return, form feed, line feed

注意:着色器语言不支持字符串类型,所以没有引号。

2、预处理指令

#define

#undef

#if

#ifdef

#ifndef

#else

#elif

#endif

#error

#pragma

#extension

#version

#line

defined

__LINE__

__FILE__

__VERSION__

3、优先级

  1. ()
  2. defined, +, -, , !
  3. *, /, %
  4. +, -
  5. <<, >> (左移、右移)
  6. <, >, <+, >=
  7. ==, !=
  8. & (按位与)
  9. ^ (按位异或)

10. | (按位或)

11. &&

12. ||

2的优先级从右到左,3-12为从左到右。

4、pragma指令

#pragma optimize(on)

#pragma optimize(off)

#pragma debug(on)

#pragma debug(off)

5、注释

/* */

或者

6、关键字

attribute const uniform varying

break continue do for while

if else

in out inout

float int void bool true false

discard reture

mat2 mat3 mat4

vec2 vec3 vec4 ivec2 ivec3 ivec4 bvec2 bvec3 bvec4

sampler1D sampler2D sampler3D samplerCube sampler1DShadow sampler2DShadow struct

7、数据类型

void bool int float

vec2 vec3 vec4 bvec2 bvec3 bvec4 ivec2 ivec3 ivec4

mat2 mat3 mat4

sampler1D sampler2D sampler3D samplerCube sampler1DShadow sampler2DShadow

结构体

声明:

struct light {

float intensity;

vec3 position;

} lightVar;

使用:

light lightVar2;

数组

float frequencies[3];

uniform vec4 lightPosition[4];

light lights[];

const int numLights = 2;

light lights[numLights];

8、类型限定符

const

attribute

uniform

varying

in

out

inout

9、数组下标

diffuseColor += lightIntensity[3] * NdotL;

10、类型转换和构造函数

float转int,小数点舍弃。

bool转int,true为1,false为0。

int转bool,非0为true,0为false。

初始化向量:

vec3(float),每个分量都是float的值。

vec2(float, float),两个浮点数初始化vec2.

vec2(vec3),舍弃第三个分量。

vec3(vec4),舍弃第四个分量。

vec3(vec2,float),第三个分量为float。

vec3(float, vec2),第一个分量为float。

vec4(vec2, vec2),两个vec2初始化vec4。

初始化矩阵(列主序,先填满一列):

mat2(vec2, vec2);

mat3(vec3, vec3, vec3);

mat4(vec4, vec4, vec4);

mat2(float, float, float, float);

mat3(float, float, float, float, float, float, float, float, float);

结构体初始化:

struct light {

float intensity;

vec3 position;

};

light lightVar = light(3.0, vec3(1.0, 2.0, 3.0));

11、向量组成部分

可以使用字母方便获取vec2,vec3,vec4的分量。

坐标和法线:x, y, z, w

颜色:r, g, b, a

uv坐标:s, t, p, q

例如:

vec4 v4 = vec4(1.0, 2.0, 3.0, 4.0);

v4.rgba,返回vec4

v4.rgb,返回vec3

v4.b,返回float

v4.xy,返回vec2。

12、矩阵组成部分

mat4 m;

m[1] = vec2(2.0); // 设置矩阵第二列都是2.0

m[0][0] = 1.0; // 设置左上角元素为1.0

13、赋值

相同类型结构体变量,可以使用.取其中一部分,使用=给整个结构体赋值,使用==和!=比较两个结构体。

14、向量和矩阵运算

vec3 v, u;

float f;

v = u + f; // 向量v的每个分量加f//

vec3 v, u, w;

w = v + u; // 向量v和u的每个分量相加。//

vec3 v, u;

mat3 m;

u = v * m; // v点乘m的每一列,不满足交换律

相当于:

u.x = dot(v, m[0]);

u.y = dot(v, m[1]);

u.z = dot(v, m[2]);//

u = m * v; // 矩阵m的每一行点乘向量v

相当于

u.x = m[0].x * v.x + m[1].x * v.y + m[2].x * v.z;

u.y = m[0].y * v.x + m[1].y * v.y + m[2].y * v.z;

u.z = m[0].z * v.x + m[1].z * v.y + m[2].z * v.z;//

mat m, n, r;

r = m * n;

相当于:

r[0].x = m[0].x * n[0].x + m[1].x * n[0].y + m[2].x * n[0].z;

r[1].x = m[0].x * n[1].x + m[1].x * n[1].y + m[2].x * n[1].z;

r[2].x = m[0].x * n[2].x + m[1].x * n[2].y + m[2].x * n[2].z;

r[0].y = m[0].y * n[0].x + m[1].y * n[0].y + m[2].y * n[0].z;

r[1].y = m[0].y * n[1].x + m[1].y * n[1].y + m[2].y * n[1].z;

r[2].y = m[0].y * n[2].x + m[1].y * n[2].y + m[2].y * n[2].z;

r[0].z = m[0].z * n[0].x + m[1].z * n[0].y + m[2].z * n[0].z;

r[1].z = m[0].z * n[1].x + m[1].z * n[1].y + m[2].z * n[1].z;

r[2].z = m[0].z * n[2].x + m[1].z * n[2].y + m[2].z * n[2].z;

15、函数定义

// 声明

returnType functionName(type0 arg0, type1 arg1, ..., typen argn);//

// 定义

returnType functionName(type0 arg0, type1 arg1, ..., typen argn) {

return returnValue;

}//

// 参数可以用in、out、inout和const修饰。//

16、条件语句

if(bool-expression)

true-statement

else

false-statement

17、循环语句

for(init-expression; condition-expression; loop-expression)

sub-statement

\\while(condition-expression)

sub-statement

\\do

statement

while(condition-expression)

18、跳转语句

continue;

break;

return;

return expression;

discard;

19、内置变量

顶点着色器:

gl_Position, type: vec4

gl_PointSize, type: float

gl_ClipVertex, type: vec4

片源着色器:

gl_FragColor, type: vec4

gl_FragData, type: vec4[gl_MaxDrawBuffers]

gl_FragDepth, type: float

gl_FragCoord, type: vec4

20、内置属性

attribute vec4 gl_Color;

attribute vec4 gl_SecondaryColor;

attribute vec3 gl_Normal;

attribute vec4 gl_Vertex;

attribute vec4 gl_MultiTexCoord0;

attribute vec4 gl_MultiTexCoord1;

attribute vec4 gl_MultiTexCoord2;

attribute vec4 gl_MultiTexCoord3;

attribute vec4 gl_MultiTexCoord4;

attribute vec4 gl_MultiTexCoord5;

attribute vec4 gl_MultiTexCoord6;

attribute vec4 gl_MultiTexCoord7;

attribute float gl_FogCoord;

21、内置常量

// 后面的数字是这些最大量允许的最小值。//

const int gl_MaxLights = 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; // ARB_vertex_shader

const int gl_MaxVertexUniformComponents = 512; // ARB_vertex_shader

const int gl_MaxVaryingFloats = 32; // 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; // proposed ARB_draw_buffers

```

22、内置Uniform状态

23、Varying变量

24、内置函数

// 角度和三角函数//

genType radians(genType degrees); // 角度转弧度

genType degrees(genType radians); // 弧度转角度

sin // 参数角度

cos // 参数角度

tan // 参数角度

asin

acos

atan//

// 指数函数//

pow

exp

log

exp2

log2

sqrt

inversesqrt // 正平方根倒数//

// 常用函数//

abs

sign

floor

ceil

fract // x-floor(x)

genType mod(genType x, genType y); // x - y * floor(x / y)

min

max

genType clamp(genType x, genType minVal, genType maxVal); // min(max(x, minVal), maxVal)

genType mix(genType x, genType y, genType a); // x * (1 - a) + y * a

genType step(genType edge, genType x); // x

genType smoothstep(genType edge0, genType edge1, genType x); // xedge1返回1,否则插值

// 几何函数//

length // 线段长度

distance // 两点距离

dot // 向量点乘

cross // 向量叉乘

normalize // 归一化

ftransform

faceforward

reflect

refract//

// 矩阵函数//

mat matrixCompMult(max x, max y)

// 向量关系函数//

lessThan

lessThanEqual

greaterThan

greaterThanEqual

equal

notEqual

any

all

not

// 纹理查询函数//

vec4 texture1D (sampler1D sampler, float coord [, float bias] )

vec4 texture1DProj (sampler1D sampler, vec2 coord [, float bias] )

vec4 texture1DLod (sampler1D sampler, float coord, float lod)

vec4 texture1DProjLod (sampler1D sampler, vec2 coord, float lod)

vec4 texture2D (sampler2D sampler, vec2 coord [, float bias] )

vec4 texture2DProj (sampler2D sampler, vec3 coord [, float bias] )

vec4 texture2DLod (sampler2D sampler, vec2 coord, float lod)

vec4 texture2DProjLod (sampler2D sampler, vec3 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 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)

// 片源处理函数//

dFdx

dFdy

fwidth

// 噪声函数//

noise1

noise2

noise3

noise4

25. 着色器语言语法