Skip to content

Commit

Permalink
Simplify mx_math files (#2058)
Browse files Browse the repository at this point in the history
This changelist simplifies mx_math.glsl and mx_math.metal to reduce code repetition and clarify the differences between dedicated helper functions and simple keyword replacement.

Additionally it addresses an omitted update from atan to mx_atan in the implementation of chiang_hair_bsdf, and adds an update step to Linux CI for robustness.
  • Loading branch information
jstone-lucasfilm authored Oct 12, 2024
1 parent cb04ef7 commit 4f380f3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 210 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ jobs:
- name: Install Dependencies (Linux)
if: runner.os == 'Linux'
run: |
sudo apt-get update
sudo apt-get install xorg-dev
if [ "${{ matrix.compiler_version }}" != 'None' ]; then
if [ "${{ matrix.compiler }}" = "gcc" ]; then
Expand Down
2 changes: 1 addition & 1 deletion libraries/pbrlib/genglsl/mx_hair_bsdf.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ vec3 mx_chiang_hair_bsdf(
float x1 = dot(L, Y);
float y2 = dot(V, N);
float x2 = dot(V, Y);
float phi = atan(y1 * x2 - y2 * x1, x1 * x2 + y1 * y2);
float phi = mx_atan(y1 * x2 - y2 * x1, x1 * x2 + y1 * y2);

vec3 k1_p = normalize(V - X * dot(V, X));
float cosGammaO = dot(N, k1_p);
Expand Down
127 changes: 11 additions & 116 deletions libraries/stdlib/genglsl/lib/mx_math.glsl
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
#define M_FLOAT_EPS 1e-8

#define mx_inversesqrt inversesqrt
#define mx_sin sin
#define mx_cos cos
#define mx_tan tan
#define mx_asin asin
#define mx_acos acos
#define mx_atan atan
#define mx_radians radians

float mx_square(float x)
{
return x*x;
}

vec2 mx_square(vec2 x)
{
return x*x;
}

vec3 mx_square(vec3 x)
{
return x*x;
Expand All @@ -20,119 +31,3 @@ vec3 mx_srgb_encode(vec3 color)
vec3 powSeg = 1.055 * pow(max(color, vec3(0.0)), vec3(1.0 / 2.4)) - 0.055;
return mix(linSeg, powSeg, isAbove);
}

float mx_inversesqrt(float x)
{
return inversesqrt(x);
}

float mx_radians(float degree)
{
return radians(degree);
}

float mx_sin(float x)
{
return sin(x);
}
vec2 mx_sin(vec2 x)
{
return sin(x);
}
vec3 mx_sin(vec3 x)
{
return sin(x);
}
vec4 mx_sin(vec4 x)
{
return sin(x);
}

float mx_cos(float x)
{
return cos(x);
}
vec2 mx_cos(vec2 x)
{
return cos(x);
}
vec3 mx_cos(vec3 x)
{
return cos(x);
}
vec4 mx_cos(vec4 x)
{
return cos(x);
}

float mx_tan(float x)
{
return tan(x);
}
vec2 mx_tan(vec2 x)
{
return tan(x);
}
vec3 mx_tan(vec3 x)
{
return tan(x);
}
vec4 mx_tan(vec4 x)
{
return tan(x);
}

float mx_asin(float x)
{
return asin(x);
}
vec2 mx_asin(vec2 x)
{
return asin(x);
}
vec3 mx_asin(vec3 x)
{
return asin(x);
}
vec4 mx_asin(vec4 x)
{
return asin(x);
}

float mx_acos(float x)
{
return acos(x);
}
vec2 mx_acos(vec2 x)
{
return acos(x);
}
vec3 mx_acos(vec3 x)
{
return acos(x);
}
vec4 mx_acos(vec4 x)
{
return acos(x);
}

float mx_atan(float y_over_x)
{
return atan(y_over_x);
}
float mx_atan(float y, float x)
{
return atan(y, x);
}
vec2 mx_atan(vec2 y, vec2 x)
{
return atan(y, x);
}
vec3 mx_atan(vec3 y, vec3 x)
{
return atan(y, x);
}
vec4 mx_atan(vec4 y, vec4 x)
{
return atan(y, x);
}
113 changes: 20 additions & 93 deletions libraries/stdlib/genmsl/lib/mx_math.metal
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
#define M_FLOAT_EPS 1e-8

#define mx_sin sin
#define mx_cos cos
#define mx_tan tan
#define mx_asin asin
#define mx_acos acos

float mx_square(float x)
{
return x*x;
}

vec2 mx_square(vec2 x)
{
return x*x;
}

vec3 mx_square(vec3 x)
{
return x*x;
}

template<class T1, class T2>
T1 mx_mod(T1 x, T2 y)
{
return x - y * floor(x/y);
}

float mx_inversesqrt(float x)
{
return ::rsqrt(x);
}

float mx_radians(float degree)
template<class T1, class T2>
T1 mx_mod(T1 x, T2 y)
{
return (degree * M_PI_F / 180.0f);
return x - y * floor(x/y);
}

float3x3 mx_inverse(float3x3 m)
Expand Down Expand Up @@ -95,108 +98,32 @@ float4x4 mx_inverse(float4x4 m)
return ret;
}

float mx_sin(float x)
{
return sin(x);
}
vec2 mx_sin(vec2 x)
{
return sin(x);
}
vec3 mx_sin(vec3 x)
{
return sin(x);
}
vec4 mx_sin(vec4 x)
{
return sin(x);
}

float mx_cos(float x)
{
return cos(x);
}
vec2 mx_cos(vec2 x)
{
return cos(x);
}
vec3 mx_cos(vec3 x)
{
return cos(x);
}
vec4 mx_cos(vec4 x)
{
return cos(x);
}

float mx_tan(float x)
{
return tan(x);
}
vec2 mx_tan(vec2 x)
{
return tan(x);
}
vec3 mx_tan(vec3 x)
{
return tan(x);
}
vec4 mx_tan(vec4 x)
{
return tan(x);
}

float mx_asin(float x)
{
return asin(x);
}
vec2 mx_asin(vec2 x)
{
return asin(x);
}
vec3 mx_asin(vec3 x)
{
return asin(x);
}
vec4 mx_asin(vec4 x)
{
return asin(x);
}

float mx_acos(float x)
{
return acos(x);
}
vec2 mx_acos(vec2 x)
{
return acos(x);
}
vec3 mx_acos(vec3 x)
{
return acos(x);
}
vec4 mx_acos(vec4 x)
{
return acos(x);
}

float mx_atan(float y_over_x)
{
return ::atan(y_over_x);
}

float mx_atan(float y, float x)
{
return ::atan2(y, x);
}

vec2 mx_atan(vec2 y, vec2 x)
{
return ::atan2(y, x);
}

vec3 mx_atan(vec3 y, vec3 x)
{
return ::atan2(y, x);
}

vec4 mx_atan(vec4 y, vec4 x)
{
return ::atan2(y, x);
}

float mx_radians(float degree)
{
return (degree * M_PI_F / 180.0f);
}

0 comments on commit 4f380f3

Please sign in to comment.