From 4f380f3b47041b1b2768acf484564906547a8d1c Mon Sep 17 00:00:00 2001 From: Jonathan Stone Date: Sat, 12 Oct 2024 09:09:43 -0700 Subject: [PATCH] Simplify mx_math files (#2058) 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. --- .github/workflows/main.yml | 1 + libraries/pbrlib/genglsl/mx_hair_bsdf.glsl | 2 +- libraries/stdlib/genglsl/lib/mx_math.glsl | 127 ++------------------- libraries/stdlib/genmsl/lib/mx_math.metal | 113 ++++-------------- 4 files changed, 33 insertions(+), 210 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0967a6f9ab..95516bd4e5 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/libraries/pbrlib/genglsl/mx_hair_bsdf.glsl b/libraries/pbrlib/genglsl/mx_hair_bsdf.glsl index ec1acf91b3..8ac6222714 100644 --- a/libraries/pbrlib/genglsl/mx_hair_bsdf.glsl +++ b/libraries/pbrlib/genglsl/mx_hair_bsdf.glsl @@ -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); diff --git a/libraries/stdlib/genglsl/lib/mx_math.glsl b/libraries/stdlib/genglsl/lib/mx_math.glsl index 838fe1f3dd..f87e5653da 100644 --- a/libraries/stdlib/genglsl/lib/mx_math.glsl +++ b/libraries/stdlib/genglsl/lib/mx_math.glsl @@ -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; @@ -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); -} diff --git a/libraries/stdlib/genmsl/lib/mx_math.metal b/libraries/stdlib/genmsl/lib/mx_math.metal index cda4e867e4..afd8fb4a7e 100644 --- a/libraries/stdlib/genmsl/lib/mx_math.metal +++ b/libraries/stdlib/genmsl/lib/mx_math.metal @@ -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 -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 +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) @@ -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); +}