Skip to content

Instantly share code, notes, and snippets.

@haxiomic
Created September 28, 2023 00:45
Show Gist options
  • Save haxiomic/88d41b8dd9d366badda4709f6f2c7ba9 to your computer and use it in GitHub Desktop.
Save haxiomic/88d41b8dd9d366badda4709f6f2c7ba9 to your computer and use it in GitHub Desktop.
Shader Library: frequently used shader functions
/**
First order approximation of acos()
See https://seblagarde.wordpress.com/2014/12/01/inverse-trigonometric-functions-gpu-optimization-for-amd-gcn-architecture/
**/
float acosPoly1(float inX) {
float x = abs(inX);
float res = -0.156583 * x + 1.570796;
res *= sqrt(1.0 - x);
return (inX >= 0.0) ? res : (3.141593 - res);
}
float asinPoly1(float x) {
return 1.570796 - acosPoly1(x);
}
/**
Second order polynomial approximation of atan in the range -1 to 1
**/
float atanPoly2UnitRange(float inX) {
float x = abs(inX);
float p = (-0.269408 * x + 1.05863) * x;
return inX < 0.0 ? -p : p;
}
vec2 atanPoly2UnitRange(vec2 inX) {
vec2 x = abs(inX);
vec2 p = (-0.269408 * x + 1.05863) * x;
return sign(inX) * p;
}
vec3 atanPoly2UnitRange(vec3 inX) {
vec3 x = abs(inX);
vec3 p = (-0.269408 * x + 1.05863) * x;
return sign(inX) * p;
}
/**
Given a transform matrix, the adjugate matrix gives us the equivalent transform matrix for transforming surface normals
The cofactor matrix is equivalent to transpose(adjugate(m))
**/
mat3 adjugate(in mat3 m) {
return mat3(
cross(m[1], m[2]),
cross(m[2], m[0]),
cross(m[0], m[1])
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment