【TA 100】2.3 Introduction to HLSL common functions

1. Basic mathematical operations

● max(a, b)
○ Return the larger
● min(a, b)
○ Return the smaller
● mul(a, b)
○ Multiply two vectors, often used in matrix operations
● abs(a)
○ Return the value of a Absolute value
● round(x)
○ Returns the nearest integer to x
● sqrt(x)
○ Returns the square root of x
● rsqrt(x)
○ Returns the reciprocal of the square root of x
● degrees(x)
○ Convert radians to angles
● redians ( x)
○ Convert angles to radians
● noise (x)
○ Noise function
○ Pass in the coordinates of uv as x and randomly return a value of [0,1], and you will get a noise map
Insert image description here
Insert image description here

2. Power pair functions and partial derivatives

● pow (x, y)
○ x raised to the y power (both x and y can be independent variables or specific numbers) → xyx^yxy
● exp(x)
○ Return the exponential function with e as the base →exe^xex
● exp2 (value x)
○ Returns the power with 2 as the base and x as the exponent →2 x 2^x2x
● ldexp(x, exp)
○ Returns the product of 2 raised to the power of exp →x ∗ 2 expx*2^expx2e xp
● log(x)
○ Returns the base e logarithm of the specified value →lnx lnxl n x
● log10(x)
○ Find the base 10 logarithm →log 10 x log_{10}xlog10x
● log2(x)
○ Find the logarithm with base 2 →log 2 x log_{2}xlog2x
● frexp (x, out exp)
○ Decompose floating point numbers into mantissa and exponent. The return value
of Decomposed
■ eg: The number 1.2 is divided into two parts, the mantissa is 12, and the exponent is 0.1
○ out keyword: exp will also be passed out as a return value, which means that this function has two return values.
○ If the x parameter is 0, both the mantissa and exponent of this function return 0

3. Trigonometric and hyperbolic functions

● sin(x), cos(x), tan(x)
○ Trigonometric functions (radians: 1°=π/180 rad)
● asin(x), acos(x), atan(x)
○ Inverse trigonometric functions
● sincos(x, out s, out c)
○ Returns the sine and cosine of x
● tan(y,x)
○ Returns the tangent of y/x
● atan2(y,x)
○ Returns the arc tangent of y/x
● Hyperbolic tangent Cosine
○ sinh(x) returns the hyperbolic sine of x → ( ex − e − x ) / 2 (e^{x}-e^{-x})/2exex )/2
○ cosh(x) returns the hyperbolic cosine of x →( ex + e − x ) / 2 (e^{x}+e^{-x})/2ex+ex )/2
○ tanh(x) returns the hyperbolic sine of x →( ex − e − x ) / ( ex + e − x ) (e^{x}-e^{-x})/(e ^{x}+e^{-x})exex/ex+ex

4. Data range class

● ceil(x)
○ Return the smallest integer >=x (rounded up)
● floor(x)
○ Return the largest integer <=x (rounded down)
● step(x, y)
○ x<=y returns 1 (true), otherwise 0 is returned
● saturate(x)
○ Returns a value that clamps x to the range [0,1]
● clamp(x, min, max)
○ Clamps x to the range [min, max] If the value is smaller than min, min will be returned. If it is larger than max, max will be returned.
● fmod(x, y)
○ Return the remainder of x to y
● frac(x)
○ Take the decimal part of x
● modf(x, out ip)
○ Will x is divided into decimal and integer parts (the output ip is the integer part, and the return value is the decimal part)
● lerp (x, y, s)
○ Interpolate between x and y according to s and return x ∗ (1 − s) + y ∗ sx*(1-s)+y*sx1s+ys
● smoothstep(min, max, x)
○ If x is in the range [min, max], return a smooth Hermite interpolation between [0, 1], use smoothstep to directly create a smooth transition between the two values
​​○ Smooth mix two colors

5. Type Judgment Class

● all(x)
○ Determine whether all components of the specified quantity are non-zero, return true if they are non-zero, otherwise return false (processing scalars, vectors or matrices defined by floating point, integer, Boolean data)
● clip(x)
○ If the input value is less than zero, discard the current pixel. Often used to determine the range (not just for 0, the return value is void)
○ Often used in Alpha Test, if each component represents the distance to the plane, it can also be used Simulate the shear plane
● sign(x) Returns the sign of x
○ Returns -1 if x is less than zero
○ Returns 0 if x is equal to zero
○ Returns 1 if x is greater than zero
● isinf(x)
○ If the x argument is +INF or - INF (infinity + infinity is still infinite, 0x3f3f3f3f), returns true, otherwise returns False
● isfinite (x)
○ Judge the x parameter to be finite, that is, bounded, which is the opposite of isinf (x)
● isnan (x)
○ If the x parameter is NAN (not a number), returns true, otherwise returns false

6. Vector and matrix classes

● length(v)
○ Returns the length of the vector
● normalize(v)
○ Vector normalization, x/length(x) direction vector normalization
● distance(a,b)
○ Returns the distance between two vectors, not Two parallel vectors should be 0, which is expressed here as the sum of the squares of the differences between the components under the root sign
● dot(a,b)
○ Dot product
● cross(a,b)
○ Cross product
●determinant(m)
○ Return Specifies the determinant-wise value of a floating-point matrix
● transpose(m)
○ Returns the transposed matrix of matrix m

7. Light operation class

● reflect(i,n)
○ The reflected light with i as the incident vector and n as the normal direction
● refract(i,n,ri)
○ The refracted light with i as the incident vector n as the normal direction and ri as the refractive index
● lit(n_dot_l,n_dot_h,m)
○ Input scalar (normal, light, half-angle vector h, specular reflection coefficient m)
○ Return lighting vector (ambient light, diffuse light, specular highlight reflection, 1)

8. Texture search

1. Some points to note:
Classification of texture search
● All texture search functions can be divided into three categories: ordinary, differential, and projected

—1.1 Partial derivative function ddx ddy (micro classification)
● If the parameter of function ddx is myVar, and the pixel corresponding to this parameter is recorded as p(i,j), then the value of ddx(myVar) is “pixel p(i+ 1, j) minus myVar" (same for ddy)
● If the input parameters of functions ddx and ddy are constants, the function return value will always be 0.
● 1. Functions ddx and ddy are used to find the difference of a certain attribute between adjacent pixels;
● 2. The input parameters of functions ddx and ddy are usually texture coordinates;
● 3. Functions ddx and ddy return the attributes of adjacent pixel keys Difference; the physical meaning of partial derivative is: the speed of change in a certain direction.
○ 4 So what ddx seeks is the change in the value of a certain attribute of two adjacent pixels
in
the The ddy instruction acts on the pixel level, so the ddx and ddy functions are only supported by fragment programs.
● When mipmap chooses which level of mipmap to use, it relies on partial derivatives.

1.2 Projection class
● Projection texture refers to projecting the texture into the scene as a slide. Using projected texture technology requires calculating the projected texture coordinates, and then using the projected texture coordinates for query. The function that uses projected texture coordinates
to query is the projected texture query function.

1.3 Mipmap class
● lod samples a mipmap
● bias samples after bias (determined by t, w)
● grad uses differentiation to select the mip layer for sampling

1D texture lookup (almost never used)
● tex1D(s, t) Ordinary 1D texture lookup returns color4 of texture sampler s at position scalar t
● tex1D(s,t,ddx,ddy) Query a 1D texture using differentials, t and Both ddxy are vectors
● tex1Dlod(s, t) Use LOD to find the color4 of texture s at the tw position
● tex1Dbias(s, t) Find the one-dimensional texture after biasing a certain MIP layer determined by tw
● tex1Dgrad(s,t ,ddx,ddy) Use differentiation and specify the one-dimensional texture search of the MIP layer
● ex1Dproj(s, t) Project the texture into the scene as a slide. First use the projected texture technology to calculate the projected texture coordinates t (coordinate tw divided by the perspective value) and then query using the projected texture coordinates

2D texture search
● tex2D(s, t) Ordinary 2D texture search returns the color of texture sampler s at vector t position
● tex2D(s,t,ddx,ddy) Use differential query to query 2D texture, t and ddxy are both vectors
● tex2Dlod(s, t) Use LOD to find the color4 of texture s at the tw position
● tex2Dbias(s, t) Find the two-dimensional texture after biasing a certain MIP layer determined by tw
● tex2Dgrad(s,t,ddx,ddy ) Use differentiation and specify the 2D texture lookup of the MIP layer
● tex2Dproj(s, t) To project the texture into the scene as a slide, first use the projected texture technology to calculate the projected texture coordinate t (coordinate tw divided by the perspective value ) and then query using projected texture coordinates

3D texture search
● tex3D(s, t) Ordinary 3D texture search returns the color of texture sampler s at the position of vector t
● tex3D(s,t,ddx,ddy) Use differential to query 3D texture, t and ddxy are both vectors
● tex3Dlod (s, t) Use LOD to find the color4 of texture s at the tw position
● tex3Dbias(s, t) Find the three-dimensional texture after biasing a certain MIP layer determined by tw
● tex3Dgrad(s,t,ddx,ddy) Use differential And specify the three-dimensional texture search of the MIP layer
● tex3Dproj(s, t) To project the texture into the scene as a slide, first use the projected texture technology to calculate the projected texture coordinate t (coordinate tw divided by the perspective value), and then use Projected texture coordinates for query

Cube texture search
Cube texture records information in three directions, from inside to outside
● texCUBE(s,t) Returns the color of texture sampler s at the vector t position
● texCUBE(s,t,ddx,ddy) Use differential to query the cube dimensional texture, both t and ddxy are vectors
● texCUBEDload(s,t) Use LOD to find the color4 of texture s at the tw position
● texCUBEbias(s,t) Find the cube texture after biasing a certain MIP layer determined by tw
● texCUBEgrad (s,t,ddx,ddy) Cube texture lookup using differentiation and specifying the MIP layer
● texCUBEproj(s,t) Cube texture lookup using projection

2. Actual usage test of ddx ddy

● As mentioned above, ddx and ddy seek the change of a certain attribute value of two adjacent pixels in the x and y directions. ● This is what
"GAMES101" said when talking about the mipmap part, adjacent pixels (pixels) The distance (difference) between them corresponds to the distance in the texture (texel).
● Randomly find a shader that samples the texture. The left side is the result of normal sampling. As a comparison
● On the right side, a coefficient Range that modifies the difference is declared in the shader, and a ddx ddy part is added to the fragment shader.

   fixed4 frag (v2f i) : SV_Target
            {
    
    
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                col += (ddx(col), ddy(col)) * _Float;
                // apply fog
                // UNITY_APPLY_FOG(i.fogCoord, col);
                return fixed4(col.rgb,1.0);
            }

Please add image description

Guess you like

Origin blog.csdn.net/weixin_45810196/article/details/131067530