其他分享
首页 > 其他分享> > CG 标准函数库

CG 标准函数库

作者:互联网

数学函数(Mathematical Functions)

函数 功能
abs(x)返回输入参数的绝对值
acos(x)反余切函数,输入参数范围为[-1,1], 返回[0,π ]区间的角度值
all(x)如果输入参数均不为 0,则返回 ture; 否则返回 flase.&&运算
any(x)输入参数只要有其中一个不为 0,则返回 true.||运算
asin(x)    反正弦函数,输入参数取值区间为[−1,1],返回角度值范围为[-π/2,π/2]
atan(x)    反正切函数,返回角度值范围为[-π/2,π/2]
atan2(y,x)计算 y/x 的反正切值.实际上和 atan(x)函数 功能完全一样,至少输入参数不同.atan(x) = atan2(x, float(1)).
ceil(x)对输入参数向上取整.例如:ceil(float(1.3)) ,其返回值为 2.0
clamp(x,a,b)如果 x 值小于 a,则返回 a;如果 x 值大于 b, 返回 b;否则,返回 x
cos(x)返回弧度 x 的余弦值.返回值范围为[−1,1]
cosh(x)双曲余弦(hyperbolic cosine)函数,计算 x 的双曲余弦值
cross(A,B)返回两个三元向量的叉积(cross product).注 意,输入参数必须是三元向量
degrees(x)输入参数为弧度值(radians),函数将其转换为 角度值(degrees)
determinant(m)计算矩阵的行列式因子
dot(A,B)返回 A 和 B 的点积(dot product).参数 A 和 B 可以是标量,也可以是向量(输入参数方面, 点积和叉积函数有很大不同)
exp(x)计算 e^x的值,e= 2.71828182845904523536
exp2(x)计算2^x 的值
floor(x)对输入参数向下取整.例如 floor(float(1.3)) 返回的值为 1.0;但是 floor(float(-1.3))返回的 值为-2.0.该函数与 ceil(x)函数相对应
fmod(x,y)返回 x/y 的余数.如果 y 为 0,结果不可预料
frac(x) 返回标量或每个向量分量的小数部分
frexp(x, out exp)将浮点数 x 分解为尾数和指数,即 x = m* 2^exp,返回 m,并将指数存入 exp 中; 如果 x 为 0,则尾数和指数都返回 0
isfinite(x)判断标量或者向量中的每个数据是否是有限数,如果是返回 true;否则返回 false;无限的 或者非数据(not-a-number NaN)
isinf(x)判断标量或者向量中的每个数据是否是无限,如果是返回 true;否则返回 false
isnan(x)判断标量或者向量中的每个数据是否是非数 据(not-a-number NaN),如果是返回 true;否 则返回 false
ldexp(x, n)计算 x * 2^n 的值
lerp(a, b, f)计算(1− f ) ∗a+b*f 或者a +f*( b-a)的 值.即在下限 a 和上限 b 之间进行插值,f 表 示权值.注意,如果 a 和 b 是向量,则权值 f 必须是标量或者等长的向量
lit(NdotL, NdotH, m)N 表示法向量
L 表示入射光向量
H 表示 半角向量
m 表示高光系数. 
函数计算环境光、散射光、镜面光的贡献,返回的 4 元向量: 
  X 位表示环境光的贡献,总是 1.0; 
  Y 位代表散射光的贡献,如果 N• L < 0,则 为 0;否则为 N •L 
  Z 位代表镜面光的贡献,如果 N •L < 0或者 N• H< 0 ,则位 0;否则为〖(N •H)〗^m ; 
  W 位始终位 1.0 
log(x)计算ln(x) 的值,x 必须大于 0 
log2(x)    计算 log2(x) 的值,x 必须大于 0
log10(x)计算 log10(x)的值,x 必须大于 0
max(a, b)比较两个标量或等长向量元素,返回最大值
min(a,b)比较两个标量或等长向量元素,返回最小值
modf(x, out ip)将 x 拆分为整数部分和小数部分,每个部分都有与 x 相同的符号.将整数部分存储在 ip 中并返回小数部分
mul(M, N)计算两个矩阵相乘,如果 M 为 AxB 阶矩阵, N 为 BxC 阶矩阵,则返回 AxC 阶矩阵.下面 两个函数为其重载函数
mul(M, v)计算矩阵和向量相乘
mul(v, M)计算向量和矩阵相乘
noise(x)噪声函数,返回值始终在 0,1 之间;对于同 样的输入,始终返回相同的值(也就是说, 并不是真正意义上的随机噪声)
pow(x, y)x^y
radians(x)函数将角度值转换为弧度值
round(x) Round-to-nearest,或 closest integer to x 即四舍五入
rsqrt(x)X 的反平方根,x 必须大于 0
saturate(x)如果 x 小于 0,返回 0;如果 x 大于 1,返回 1;否则,返回 x
sign(x)如果 x 大于 0,返回 1;如果 x 小于 0,返回 01;否则返回 0
sin(x)输入参数为弧度,计算正弦值,返回值范围 为[−1,1]
sincos(float x, out s, out c)该函数是同时计算 x 的 sin 值和 cos 值,其中 s=sin(x),c=cos(x).该函数用于"同时需要 计算 sin 值和 cos 值的情况",比分别运算要 快很多
sinh(x)计算双曲正弦(hyperbolic sine)值
smoothstep(min, max, x)值 x 位于 min、max 区间中.如果 x=min,返 回 0;如果 x=max,返回 1;如果 x 在两者之 间,按照下列公式返回数据:-2*(((x-min)/(max-min)))^3-3*(((x-min)/(max-min)))^2
step(a, x)如果 x <a,返回 0;否则,返回 1
sqrt(x)求 x 的平方根 √x,x 必须大于 0
tan(x)输入参数为弧度,计算正切值
tanh(x)计算双曲正切值
transpose(M)M 为矩阵,计算其转置矩阵

几何函数(Geometric Functions)

函数功能
distance( pt1, pt2)两点之间的欧几里德距离(Euclidean distance) 
faceforward(N,I,Ng)如果 Ng• I | < 0 ,返回 N;否则返回-N
length(v)返回一个向量的模,即 sqrt(dot(v,v))
normalize(v)归一化向量
reflect(I, N)根据入射光方向向量 I,和顶点法向量 N,计算反射光方向向量.其中 I 和 N 必须被归一化,需要非常注意的是,这 个 I 是指向顶点的;函数只对三元向量有效
refract(I,N,eta)计算折射向量,I 为入射光线,N 为法 向量,eta 为折射系数;其中 I 和 N 必 须被归一化,如果 I 和 N 之间的夹角太 大,则返回(0,0,0),也就是没有折 射光线;I 是指向顶点的;函数只对三元向量有效

        着色程序中的向量最好进行归一化之后再使用,否则会出现难以预料的错误
        reflect 函数和 refract 函数都存在以"入射光方向向量"作为输入参数,注意这两个函数中使用的入射光方向向量,是从外指向几何顶点的;平时我们在着色程序中或者在课本上都是将入射光方向向量作为从顶点出发

纹理映射函数(Texture Map Functions)

函数 功能
tex1D(sampler1D tex, float s)1D nonprojective
tex1D(sampler1D tex, float s, float dsdx, float dsdy) 1D nonprojective with derivatives
tex1D(sampler1D tex, float2 sz) 1D nonprojective depth compare
tex1D(sampler1D tex, float2 sz, float dsdx,float dsdy) 1D nonprojective depth compare
tex1Dproj(sampler1D tex, float2 sq) 1D projective
tex1Dproj(sampler1D tex, float3 szq)1D projective depth compare
tex2D(sampler2D tex, float2 s) 2D nonprojective
tex2D(sampler2D tex, float2 s, float2 dsdx, float2 dsdy) 2D nonprojective with derivatives
tex2D(sampler2D tex, float3 sz)2D nonprojective depth compare
tex2D(sampler2D tex, float3 sz, float2 dsdx,float2 dsdy)2D nonprojective depth compare with derivatives
tex2Dproj(sampler2D tex, float3 sq) 2D projective
tex2Dproj(sampler2D tex, float4 szq)2D projective depth compare
texRECT(samplerRECT tex, float2 s)2D RECT nonprojective
texRECT (samplerRECT tex, float2 s, float2 dsdx, float2 dsdy)2D RECT nonprojective with derivatives
texRECT (samplerRECT tex, float3 sz)2D RECT nonprojective depth compare
texRECT (samplerRECT tex, float3 sz, float2 dsdx,float2 dsdy) 2D RECT nonprojective depth compare with derivatives
texRECT proj(samplerRECT tex, float3 sq)2D RECT projective
texRECT proj(samplerRECT tex, float3 szq) 2D RECT projective depth compare
tex3D(sampler3D tex, float s) 3D nonprojective
tex3D(sampler3D tex, float3 s, float3 dsdx, float3 dsdy) 3D nonprojective with derivatives
tex3Dproj(sampler3D tex, float4 szq)3D projective depth compare
texCUBE(samplerCUBE tex, float3 s)Cubemap nonprojective
texCUBE (samplerCUBE tex, float3 s, float3 dsdx, float3 dsdy)Cubemap nonprojective with derivatives
texCUBEproj (samplerCUBE tex, float4 sq) Cubemap projective

        In the table, the name of the second argument to each function indicates how its values are used when performing the texture lookup: s indicates a 1‐, 2‐, or 3‐component texture coordinate; z indicates a depth comparison value for shadowmap lookups; q indicates a perspective value and is used to divide the texture coordinate, s, before the texture lookup is performed. For convenience, the standard library also defines versions of the texture functions prefixed with h4, such as h4tex2D(), that return half4 values and prefixed with x4, such as x4tex2D(), that return fixed4 values. When the texture functions that allow specifying a depth comparison value are used, the associated texture unit must be configured for depth compare texturing. Otherwise, no depth comparison is actually performed.
        s象征一元、二元、三元纹理坐标;z 代表使用"深度比较(depth comparison)"的值;q表示一个透视值(perspective value,其实就是透视投影后所得到的齐次坐标的最后一位),这个值被用来除以纹理坐标(S),得到新的纹理坐标(已归一化到0和1之间)然后用于纹理查询
还有一类较为特殊的纹理查询函数以proj结尾,主要是针对投影纹理进行查询.所谓投影纹理是指:将纹理当做一张幻灯片投影到场景中,使用投影纹理技术需要计算投影纹理坐标,然后使用投影纹理坐标进行查询.使用投影纹理坐标进行查询的函数就是投影纹理查询函数
        tex2Dproj(texture,uvproj); <=> float4 uvproj = uvproj/uvproj.q;  tex2D(texture,uvproj);

偏导函数(Derivative Functions)

函数功能
ddx(a)Approximate partial derivative of a with respect to screen-space x coordinate.(参数 a 对应一个像素位置,返回该像素 值在 X 轴上的偏导数)
ddy(a)Approximate partial derivative of a with respect to screen-space y coordinate.(参数 a 对应一个像素位置,返回该像素 值在 Y 轴上的偏导数)
  1. 函数 ddx 和 ddy 用于求取相邻像素间某属性的差值; 
  2. 函数 ddx 和 ddy 的输入参数通常是纹理坐标; 
  3. 函数 ddx 和 ddy 返回相邻像素键的属性差值;

        If you evaluate ddx (myVar), the GPU will give you the difference between the value of myVar at the current pixel and its value at the pixel next door. It's a straight linear difference, made efficient by the nature of GPU SIMD architectures (neighboring pixels will be calculated simultaneously).The derivative of any uniform value will always be zero.Because these derivatives are always linear, the second derivatives—for example, ddx(ddx(myVar))—will always be zero.
        上面这句话的意思是,如果函数ddx的参数为myVar,该参数对应的像素点记为pij ( ) , 则 ddx(myVar)的值为"像素点pij ( +1, ) 的值减去myVar".同理, ddy(myVar)的值为"像素点 pij ( ) , 1+的值减去myVar".如果函数ddx和ddy的输入参数为常数,则函数返回值永远为0

        这里面存在像素点所对应数据的类型问题.从前面的知识可知,传入片段程 序的顶点属性一般有:屏幕坐标空间的顶点齐次坐标、纹理坐标、法向量、光照 颜色等.假设传递给 ddx\ddy 函数的参数myVar是纹理坐标,则ddx(myVar) 的值为,纹理上像素点pij ( ) +1, 的纹理颜色值减去myVar对应的纹理颜色值

        Complex filtering depends on knowing just how much of the texture (or shading) we need to filter. Modern GPUs such as the GeForce FX provide partial derivative functions to help us. For any value used in shading, we can ask the GPU: "How much does this value change from pixel to pixel, in either the screen-x or the screen-y direction?" These functions are ddx() and ddy(). Although they are little used, they can be very helpful for filtering and antialiasing operations.
        中文含义是:现代的GPU中提供了计算partial derivative的指令(通常被称为梯度指令或者偏导数指令,DDX或DDY),Cg中所对应的函数为ddx()和 ddy()(注:glsl和hlsl中也有该函数).按照屏幕相关空间x或y计算偏导数,对纹理滤镜以及抗锯齿等非常有用(注:也可以用于TXD纹理查找的参数计算).
        偏导数的物理含义是:在某一个方向上的变化快慢.所以ddx求的是X方向上,相邻两个像素的某属性值的变化量;ddy球的是Y方向上,相邻两个像素的某属性值的变化量. 
        正是由于ddx和ddy指令是作用于像素级的,所以 ddx和ddy函数只被片段程序所支持. 
        当在纹理查询函数中使用ddx和ddy是可以进行图形过滤.所谓图像过滤,一个比较简单的定义是:对于给定的输入图像 A,要创建新的图像 B,把源图像A变换到目标图像 B 的操作就是图像滤波.最一般的变换是调整图像大小、锐 化、变化颜色,以及模糊图像等.复杂的过滤有赖于知道究竟需要过滤多少纹理. 向GeForce那样的现代GPU提供的偏导函数可以帮助我们

调试函数(Debugging Function)

函数功能
void debug(float4 x)如果在编译时设置了DEBUG,片段着色程序中调用该函数可以将值x作为COLOR语义的最终输出;否则该函数 什么也不做.顶点 profiles 不支持调试函数

标签:返回,ddx,函数库,CG,纹理,tex,标准,向量,函数
来源: https://blog.csdn.net/liyan_1992/article/details/121253558