6.7着色器内部函数大全
2024-06-26
7
0
HLSL(High Level Shading Language)是一种用于编写着色器程序的语言,它包含许多内置函数可以用于实现各种图形效果。以下是一些常见的内置函数类型:
数学函数:
- abs(): 返回输入值的绝对值。
- sin()/cos(): 计算输入值的正弦值/余弦值。
- sqrt(): 返回输入值的平方根。
- pow(): 计算指定底数和指数的幂值。
- ceil()/floor(): 返回不小于/不大于输入值的最小整数值。
向量操作函数:
- normalize(): 将输入向量归一化为单位长度。
- dot(): 计算两个向量的点积。
- cross(): 计算两个三维向量的叉积。
颜色操作函数:
- saturate(): 将输入值限制在[0, 1]范围内。
- lerp(): 在两个值之间进行线性插值。
- rgba(): 返回包含指定RGB颜色和alpha值的颜色向量。
纹理采样函数:
- tex2D()/tex2Dlod(): 根据指定纹理坐标采样2D纹理纹理数据。
- texCUBE()/texCUBElod(): 根据指定立方体纹理坐标采样立方体纹理数据。
矩阵操作函数:
- mul(): 执行矩阵乘法操作。
- transpose(): 计算输入矩阵的转置矩阵。
这些函数提供了丰富的功能,可以帮助开发人员实现各种复杂的着色器效果。在编写HLSL着色器时,可以根据需求选择合适的内置函数来实现所需的功能。
- abs(x) – Returns |x|.
- ceil(x) – Returns the smallest integer ≥ x.
- cos(x) – Returns the cosine of x, where x is in radians.
- clamp(x, a, b) – Clamps x to the range [a, b] and returns the result.
- clip(x) – This function can only be called in a pixel shader, and it discards the current pixel from further processing if x < 0.
- cross(u, v) – Returns u × v.
- ddx(p) – Estimates screen space partial derivative ∂p/∂x. This allows you to determine how per pixel quantities p vary from pixel to pixel in the screen space x-direction.
- ddy(p) – Estimates screen space partial derivative ∂p/∂y. This allows you to determine how per pixel quantities p vary from pixel to pixel in the screen space y-direction.
- degrees(x) – Converts x from radians to degrees.
- determinant(M) – Returns the determinant of a matrix.
- distance(u, v) – Returns the distance || v - u || between the points u and v.
- dot(u, v) – Returns u v.
- floor(x) – Returns the greatest integer ≤ x.
- frac(x) – This function returns the fractional part of a floating-point number (i.e., the mantissa). For example, if x = (235.52,
696.32) and frac(x) = (0.52, 0.32). - length(v) – Returns || v ||.
- lerp(u, v, t) – Linearly interpolates between u and v based on the parameter t ∈ [0,1].
- log(x) – Returns ln(x).
- log10(x) – Returns log10(x).
- log2(x) – Returns log2(x).
- max(x, y) – Returns x if x ≥ y, else returns y.
- min(x, y) – Returns x if x ≥ y, else returns y.
- mul(M, N) – Returns the matrix product MN. Note that the matrix product MN must be defined. If M is a vector, it is treated as a row vector so that the vector-matrix product is defined. Likewise, if N is a vector, it is treated as a column vector so that the
matrix-vector product is defined. - normalize(v) – Returns v/|| v ||.
- pow(b, n) – Returns bn.
- radians(x) – Converts x from degrees to radians.
- saturate(x) – Returns clamp(x, 0.0, 1.0).
- sin(x) – Returns the sine of x, where x is in radians.
- sincos(in x, out s, out c) – Returns the sine and cosine of x, where x is in radians.
- sqrt(x) – Returns .
- reflect(v, n) – Computes the reflection vector given the incident vector v and the surface normal n.
- refract(v, n, eta) – Computes the refraction vector given the incident vector v, the surface normal n, and the ratio of the two in dices of refraction of the two materials eta.
- rsqrt(x) – Returns .
- tan(x) – Returns the tangent of x, where x is in radians
- transpose(M) – Returns the transpose MT.
- Texture2D::Sample(S, texC) – Returns a color from a 2D texture map based on the SamplerState object S, and 2D texturecoordinates texC.
- Texture2D::SampleLevel(S, texC, mipLevel) – Returns a color from a 2D texture map based on the SamplerState object S, 2D texture coordinates texC, and mipmap level mipLevel. This function differs from Texture2D::Sample in that the third parameter manually specifies the mipmap level to use. For example, we would specify 0 to access the topmost mipmap LOD.
- TextureCube::Sample(S, v) – Returns a color from a cube map based on the SamplerState object S and 3D lookup vector v.
- Texture2DArray::Sample(S, texC) – Returns a color from a 2D texture array based on the SamplerState object S (recall a sampler state specifies texture filters and texture address modes) and 3D texture coordinates texC, where the first two coordinates are the usual 2D texture coordinates and the third coordinate specifies the array index.