1. 程式人生 > >GLSL:Shader內建變數與內建函式

GLSL:Shader內建變數與內建函式

shader內建變數:

    gl_Position: 用於vertex shader, 寫頂點位置;被圖元收集、裁剪等固定操作功能所使用;其內部宣告是:highp vec4 gl_Position;

    gl_PointSize: 用於vertex shader, 寫光柵化後的點大小,畫素個數;                                 其內部宣告是:mediump float gl_Position;

    gl_FragColor: 用於Fragment shader,寫fragment color;被後續的固定管線使用;        其內部宣告是:mediump vec4 gl_FragColor;

    gl_FragData: 用於Fragment shader,是個陣列,寫gl_FragData[n] 為data n;被後續的固定管線使用; 其內部宣告是:mediump                      vec4     gl_FragData[gl_MaxDrawBuffers];

    gl_FragColor和gl_FragData是互斥的,不會同時寫入;

    gl_FragCoord: 用於Fragment shader,只讀, Fragment相對於視窗的座標位置 x,y,z,1/w; 這個是固定管線圖元差值後產生的;z 是深度值; 其內部宣告是:mediump vec4 gl_FragCoord;

    gl_FrontFacing: 用於判斷 fragment是否屬於 front-facing primitive;只讀;   其內部宣告是:bool gl_FrontFacing;   

    gl_PointCoord: 僅用於 point primitive;                                                            其內部宣告是:  mediump vec2 gl_PointCoord;


shader內建常量:

    const mediump int gl_MaxVertexAttribs = 8;

    const mediump int gl_MaxVertexUniformVectors = 128;

    const mediump int gl_MaxVaryingVectors = 8;

    const mediump int gl_MaxVertexTextureImageUnits = 0;

    const mediump int gl_MaxCombinedTextureImageUnits = 8;

    const mediump int gl_MaxTextureImageUnits = 8;

    const mediump int gl_MaxFragmentUnitformVectors = 16;

    const mediump int gl_MaxDrawBuffers = 1;

 shader內建函式:

    一般預設都用弧度;

    radians(degree) : 角度變弧度;

    degrees(radian) : 弧度變角度;

    sin(angle), cos(angle), tan(angle)

    asin(x): arc sine, 返回弧度 [-PI/2, PI/2];

    acos(x): arc cosine,返回弧度 [0, PI];

    atan(y, x): arc tangent, 返回弧度 [-PI, PI];

    atan(y/x): arc tangent, 返回弧度 [-PI/2, PI/2];

    pow(x, y): x的y次方;

    exp(x): 指數, log(x):

    exp2(x): 2的x次方, log2(x):

    sqrt(x): x的根號; inversesqrt(x): x根號的倒數

    abs(x): 絕對值

    sign(x): 符號, 1, 0 或 -1

    floor(x): 底部取整

    ceil(x): 頂部取整

    fract(x): 取小數部分

    mod(x, y): 取模, x - y*floor(x/y)

    min(x, y): 取最小值

    max(x, y): 取最大值

    clamp(x, min, max):  min(max(x, min), max);

    mix(x, y, a): x, y的線性混疊, x(1-a) + y*a;

    step(edge, x): 如 x

    smoothstep(edge0, edge1, x): threshod  smooth transition時使用。 edge0<=edge0時為0.0, x>=edge1時為1.

    length(x): 向量長度

    distance(p0, p1): 兩點距離, length(p0-p1);

    dot(x, y): 點積,各分量分別相乘 後 相加

    cross(x, y): 差積,x[1]*y[2]-y[1]*x[2], x[2]*y[0] - y[2]*x[0], x[0]*y[1] - y[0]*x[1]

    normalize(x): 歸一化, length(x)=1;

    faceforward(N, I, Nref): 如 dot(Nref, I)< 0則N, 否則 -N

    reflect(I, N): I的反射方向, I -2*dot(N, I)*N, N必須先歸一化

    refract(I, N, eta): 折射,k=1.0-eta*eta*(1.0 - dot(N, I) * dot(N, I)); 如k<0.0 則0.0,否則 eta*I - (eta*dot(N, I)+sqrt(k))*N

    matrixCompMult(matX, matY): 矩陣相乘, 每個分量 自行相乘, 即 r[i][j] = x[i][j]*y[i][j];   矩陣線性相乘,直接用 * 

    lessThan(vecX, vecY): 向量 每個分量比較 x < y

    lessThanEqual(vecX, vecY): 向量 每個分量比較 x<=y

    greaterThan(vecX, vecY): 向量 每個分量比較 x>y

    greaterThanEqual(vecX, vecY): 向量 每個分量比較 x>=y

    equal(vecX, vecY): 向量 每個分量比較 x==y

    notEqual(vecX, vexY): 向量 每個分量比較 x!=y

    any(bvecX): 只要有一個分量是true, 則true

    all(bvecX): 所有分量是true, 則true

    not(bvecX): 所有分量取反

    texture2D(sampler2D, coord): texture lookup

    texture2D(sampler2D, coord, bias): LOD bias, mip-mapped texture

    texture2DProj(sampler2D, coord):

    texture2DProj(sampler2D, coord, bias):

    texture2DLod(sampler2D, coord, lod):

    texture2DProjLod(sampler2D, coord, lod):

    textureCube(samplerCube, coord):

    textureCube(samplerCube, coord, bias):

    textureCubeLod(samplerCube, coord, lod):