Skip to content

Commit

Permalink
Refactor mx_math files to remove GLSL guard (#2014)
Browse files Browse the repository at this point in the history
Refactor mx_math.glsl and mx_math.metal to remove the `__DECL_GL_MATH_FUNCTIONS__` guard, and prefix any necessary functions to ensure isolation from other shader generators, such as HdStorm.
  • Loading branch information
ld-kerley authored Oct 10, 2024
1 parent 0f2438e commit cb04ef7
Show file tree
Hide file tree
Showing 10 changed files with 296 additions and 97 deletions.
6 changes: 3 additions & 3 deletions libraries/pbrlib/genglsl/lib/mx_generate_prefilter_env.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ vec3 mx_latlong_map_projection_inverse(vec2 uv)
float latitude = (uv.y - 0.5) * M_PI;
float longitude = (uv.x - 0.5) * M_PI * 2.0;

float x = -cos(latitude) * sin(longitude);
float y = -sin(latitude);
float z = cos(latitude) * cos(longitude);
float x = -mx_cos(latitude) * mx_sin(longitude);
float y = -mx_sin(latitude);
float z = mx_cos(latitude) * mx_cos(longitude);

return vec3(x, y, z);
}
Expand Down
8 changes: 4 additions & 4 deletions libraries/pbrlib/genglsl/lib/mx_microfacet.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ vec3 mx_uniform_sample_hemisphere(vec2 Xi)
float phi = 2.0 * M_PI * Xi.x;
float cosTheta = 1.0 - Xi.y;
float sinTheta = sqrt(1.0 - mx_square(cosTheta));
return vec3(cos(phi) * sinTheta,
sin(phi) * sinTheta,
return vec3(mx_cos(phi) * sinTheta,
mx_sin(phi) * sinTheta,
cosTheta);
}

Expand All @@ -88,8 +88,8 @@ vec3 mx_cosine_sample_hemisphere(vec2 Xi)
float phi = 2.0 * M_PI * Xi.x;
float cosTheta = sqrt(Xi.y);
float sinTheta = sqrt(1.0 - Xi.y);
return vec3(cos(phi) * sinTheta,
sin(phi) * sinTheta,
return vec3(mx_cos(phi) * sinTheta,
mx_sin(phi) * sinTheta,
cosTheta);
}

Expand Down
8 changes: 4 additions & 4 deletions libraries/pbrlib/genglsl/lib/mx_microfacet_diffuse.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ float mx_oren_nayar_fujii_diffuse_dir_albedo(float cosTheta, float roughness)
float A = 1.0 / (1.0 + FUJII_CONSTANT_1 * roughness);
float B = roughness * A;
float Si = sqrt(max(0.0, 1.0 - mx_square(cosTheta)));
float G = Si * (acos(clamp(cosTheta, -1.0, 1.0)) - Si * cosTheta) +
float G = Si * (mx_acos(clamp(cosTheta, -1.0, 1.0)) - Si * cosTheta) +
2.0 * ((Si / cosTheta) * (1.0 - Si * Si * Si) - Si) / 3.0;
return A + (B * G * M_PI_INV);
}
Expand Down Expand Up @@ -169,7 +169,7 @@ vec3 mx_burley_diffusion_profile(float dist, vec3 shape)
// Inspired by Eric Penner's presentation in http://advances.realtimerendering.com/s2011/
vec3 mx_integrate_burley_diffusion(vec3 N, vec3 L, float radius, vec3 mfp)
{
float theta = acos(dot(N, L));
float theta = mx_acos(dot(N, L));

// Estimate the Burley diffusion shape from mean free path.
vec3 shape = vec3(1.0) / max(mfp, 0.1);
Expand All @@ -182,9 +182,9 @@ vec3 mx_integrate_burley_diffusion(vec3 N, vec3 L, float radius, vec3 mfp)
for (int i = 0; i < SAMPLE_COUNT; i++)
{
float x = -M_PI + (float(i) + 0.5) * SAMPLE_WIDTH;
float dist = radius * abs(2.0 * sin(x * 0.5));
float dist = radius * abs(2.0 * mx_sin(x * 0.5));
vec3 R = mx_burley_diffusion_profile(dist, shape);
sumD += R * max(cos(theta + x), 0.0);
sumD += R * max(mx_cos(theta + x), 0.0);
sumR += R;
}

Expand Down
20 changes: 10 additions & 10 deletions libraries/pbrlib/genglsl/lib/mx_microfacet_specular.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ vec3 mx_ggx_importance_sample_VNDF(vec2 Xi, vec3 V, vec2 alpha)
float phi = 2.0 * M_PI * Xi.x;
float z = (1.0 - Xi.y) * (1.0 + V.z) - V.z;
float sinTheta = sqrt(clamp(1.0 - z * z, 0.0, 1.0));
float x = sinTheta * cos(phi);
float y = sinTheta * sin(phi);
float x = sinTheta * mx_cos(phi);
float y = sinTheta * mx_sin(phi);
vec3 c = vec3(x, y, z);

// Compute the microfacet normal.
Expand Down Expand Up @@ -286,9 +286,9 @@ void mx_fresnel_conductor_phase_polarized(float cosTheta, float eta1, vec3 eta2,
vec3 U = sqrt((A+B)/2.0);
vec3 V = max(vec3(0.0), sqrt((B-A)/2.0));

phiS = atan(2.0*eta1*V*cosTheta, U*U + V*V - mx_square(eta1*cosTheta));
phiP = atan(2.0*eta1*eta2*eta2*cosTheta * (2.0*k2*U - (vec3(1.0)-k2*k2) * V),
mx_square(eta2*eta2*(vec3(1.0)+k2*k2)*cosTheta) - eta1*eta1*(U*U+V*V));
phiS = mx_atan(2.0*eta1*V*cosTheta, U*U + V*V - mx_square(eta1*cosTheta));
phiP = mx_atan(2.0*eta1*eta2*eta2*cosTheta * (2.0*k2*U - (vec3(1.0)-k2*k2) * V),
mx_square(eta2*eta2*(vec3(1.0)+k2*k2)*cosTheta) - eta1*eta1*(U*U+V*V));
}

// https://belcour.github.io/blog/research/publication/2017/05/01/brdf-thin-film.html
Expand All @@ -299,8 +299,8 @@ vec3 mx_eval_sensitivity(float opd, vec3 shift)
vec3 val = vec3(5.4856e-13, 4.4201e-13, 5.2481e-13);
vec3 pos = vec3(1.6810e+06, 1.7953e+06, 2.2084e+06);
vec3 var = vec3(4.3278e+09, 9.3046e+09, 6.6121e+09);
vec3 xyz = val * sqrt(2.0*M_PI * var) * cos(pos * phase + shift) * exp(- var * phase*phase);
xyz.x += 9.7470e-14 * sqrt(2.0*M_PI * 4.5282e+09) * cos(2.2399e+06 * phase + shift[0]) * exp(- 4.5282e+09 * phase*phase);
vec3 xyz = val * sqrt(2.0*M_PI * var) * mx_cos(pos * phase + shift) * exp(- var * phase*phase);
xyz.x += 9.7470e-14 * sqrt(2.0*M_PI * 4.5282e+09) * mx_cos(2.2399e+06 * phase + shift[0]) * exp(- 4.5282e+09 * phase*phase);
return xyz / 1.0685e-7;
}

Expand Down Expand Up @@ -341,7 +341,7 @@ vec3 mx_fresnel_airy(float cosTheta, FresnelData fd)
}

// Phase shift
float cosB = cos(atan(eta2 / eta1));
float cosB = mx_cos(mx_atan(eta2 / eta1));
vec2 phi21 = vec2(cosTheta < cosB ? 0.0 : M_PI, M_PI);
vec3 phi23p, phi23s;
if (fd.model == FRESNEL_MODEL_SCHLICK)
Expand Down Expand Up @@ -486,8 +486,8 @@ vec3 mx_refraction_solid_sphere(vec3 R, vec3 N, float ior)

vec2 mx_latlong_projection(vec3 dir)
{
float latitude = -asin(dir.y) * M_PI_INV + 0.5;
float longitude = atan(dir.x, -dir.z) * M_PI_INV * 0.5 + 0.5;
float latitude = -mx_asin(dir.y) * M_PI_INV + 0.5;
float longitude = mx_atan(dir.x, -dir.z) * M_PI_INV * 0.5 + 0.5;
return vec2(longitude, latitude);
}

Expand Down
113 changes: 111 additions & 2 deletions libraries/stdlib/genglsl/lib/mx_math.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ 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 @@ -27,3 +25,114 @@ 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);
}
6 changes: 3 additions & 3 deletions libraries/stdlib/genglsl/mx_rotate_vector2.glsl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
void mx_rotate_vector2(vec2 _in, float amount, out vec2 result)
{
float rotationRadians = radians(amount);
float sa = sin(rotationRadians);
float ca = cos(rotationRadians);
float rotationRadians = mx_radians(amount);
float sa = mx_sin(rotationRadians);
float ca = mx_cos(rotationRadians);
result = vec2(ca*_in.x + sa*_in.y, -sa*_in.x + ca*_in.y);
}
6 changes: 3 additions & 3 deletions libraries/stdlib/genglsl/mx_rotate_vector3.glsl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
mat4 mx_rotationMatrix(vec3 axis, float angle)
{
axis = normalize(axis);
float s = sin(angle);
float c = cos(angle);
float s = mx_sin(angle);
float c = mx_cos(angle);
float oc = 1.0 - c;

return mat4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0,
Expand All @@ -13,7 +13,7 @@ mat4 mx_rotationMatrix(vec3 axis, float angle)

void mx_rotate_vector3(vec3 _in, float amount, vec3 axis, out vec3 result)
{
float rotationRadians = radians(amount);
float rotationRadians = mx_radians(amount);
mat4 m = mx_rotationMatrix(axis, rotationRadians);
result = (m * vec4(_in, 1.0)).xyz;
}
48 changes: 24 additions & 24 deletions libraries/stdlib/genglsl/stdlib_genglsl_impl.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -326,30 +326,30 @@
<implementation name="IM_power_vector4FA_genglsl" nodedef="ND_power_vector4FA" target="genglsl" sourcecode="pow({{in1}}, vec4({{in2}}))" />

<!-- <sin>, <cos>, <tan>, <asin>, <acos>, <atan2> -->
<implementation name="IM_sin_float_genglsl" nodedef="ND_sin_float" target="genglsl" sourcecode="sin({{in}})" />
<implementation name="IM_cos_float_genglsl" nodedef="ND_cos_float" target="genglsl" sourcecode="cos({{in}})" />
<implementation name="IM_tan_float_genglsl" nodedef="ND_tan_float" target="genglsl" sourcecode="tan({{in}})" />
<implementation name="IM_asin_float_genglsl" nodedef="ND_asin_float" target="genglsl" sourcecode="asin({{in}})" />
<implementation name="IM_acos_float_genglsl" nodedef="ND_acos_float" target="genglsl" sourcecode="acos({{in}})" />
<implementation name="IM_atan2_float_genglsl" nodedef="ND_atan2_float" target="genglsl" sourcecode="atan({{iny}}, {{inx}})" />
<implementation name="IM_sin_vector2_genglsl" nodedef="ND_sin_vector2" target="genglsl" sourcecode="sin({{in}})" />
<implementation name="IM_cos_vector2_genglsl" nodedef="ND_cos_vector2" target="genglsl" sourcecode="cos({{in}})" />
<implementation name="IM_tan_vector2_genglsl" nodedef="ND_tan_vector2" target="genglsl" sourcecode="tan({{in}})" />
<implementation name="IM_asin_vector2_genglsl" nodedef="ND_asin_vector2" target="genglsl" sourcecode="asin({{in}})" />
<implementation name="IM_acos_vector2_genglsl" nodedef="ND_acos_vector2" target="genglsl" sourcecode="acos({{in}})" />
<implementation name="IM_atan2_vector2_genglsl" nodedef="ND_atan2_vector2" target="genglsl" sourcecode="atan({{iny}}, {{inx}})" />
<implementation name="IM_sin_vector3_genglsl" nodedef="ND_sin_vector3" target="genglsl" sourcecode="sin({{in}})" />
<implementation name="IM_cos_vector3_genglsl" nodedef="ND_cos_vector3" target="genglsl" sourcecode="cos({{in}})" />
<implementation name="IM_tan_vector3_genglsl" nodedef="ND_tan_vector3" target="genglsl" sourcecode="tan({{in}})" />
<implementation name="IM_asin_vector3_genglsl" nodedef="ND_asin_vector3" target="genglsl" sourcecode="asin({{in}})" />
<implementation name="IM_acos_vector3_genglsl" nodedef="ND_acos_vector3" target="genglsl" sourcecode="acos({{in}})" />
<implementation name="IM_atan2_vector3_genglsl" nodedef="ND_atan2_vector3" target="genglsl" sourcecode="atan({{iny}}, {{inx}})" />
<implementation name="IM_sin_vector4_genglsl" nodedef="ND_sin_vector4" target="genglsl" sourcecode="sin({{in}})" />
<implementation name="IM_cos_vector4_genglsl" nodedef="ND_cos_vector4" target="genglsl" sourcecode="cos({{in}})" />
<implementation name="IM_tan_vector4_genglsl" nodedef="ND_tan_vector4" target="genglsl" sourcecode="tan({{in}})" />
<implementation name="IM_asin_vector4_genglsl" nodedef="ND_asin_vector4" target="genglsl" sourcecode="asin({{in}})" />
<implementation name="IM_acos_vector4_genglsl" nodedef="ND_acos_vector4" target="genglsl" sourcecode="acos({{in}})" />
<implementation name="IM_atan2_vector4_genglsl" nodedef="ND_atan2_vector4" target="genglsl" sourcecode="atan({{iny}}, {{inx}})" />
<implementation name="IM_sin_float_genglsl" nodedef="ND_sin_float" target="genglsl" sourcecode="mx_sin({{in}})" />
<implementation name="IM_cos_float_genglsl" nodedef="ND_cos_float" target="genglsl" sourcecode="mx_cos({{in}})" />
<implementation name="IM_tan_float_genglsl" nodedef="ND_tan_float" target="genglsl" sourcecode="mx_tan({{in}})" />
<implementation name="IM_asin_float_genglsl" nodedef="ND_asin_float" target="genglsl" sourcecode="mx_asin({{in}})" />
<implementation name="IM_acos_float_genglsl" nodedef="ND_acos_float" target="genglsl" sourcecode="mx_acos({{in}})" />
<implementation name="IM_atan2_float_genglsl" nodedef="ND_atan2_float" target="genglsl" sourcecode="mx_atan({{iny}}, {{inx}})" />
<implementation name="IM_sin_vector2_genglsl" nodedef="ND_sin_vector2" target="genglsl" sourcecode="mx_sin({{in}})" />
<implementation name="IM_cos_vector2_genglsl" nodedef="ND_cos_vector2" target="genglsl" sourcecode="mx_cos({{in}})" />
<implementation name="IM_tan_vector2_genglsl" nodedef="ND_tan_vector2" target="genglsl" sourcecode="mx_tan({{in}})" />
<implementation name="IM_asin_vector2_genglsl" nodedef="ND_asin_vector2" target="genglsl" sourcecode="mx_asin({{in}})" />
<implementation name="IM_acos_vector2_genglsl" nodedef="ND_acos_vector2" target="genglsl" sourcecode="mx_acos({{in}})" />
<implementation name="IM_atan2_vector2_genglsl" nodedef="ND_atan2_vector2" target="genglsl" sourcecode="mx_atan({{iny}}, {{inx}})" />
<implementation name="IM_sin_vector3_genglsl" nodedef="ND_sin_vector3" target="genglsl" sourcecode="mx_sin({{in}})" />
<implementation name="IM_cos_vector3_genglsl" nodedef="ND_cos_vector3" target="genglsl" sourcecode="mx_cos({{in}})" />
<implementation name="IM_tan_vector3_genglsl" nodedef="ND_tan_vector3" target="genglsl" sourcecode="mx_tan({{in}})" />
<implementation name="IM_asin_vector3_genglsl" nodedef="ND_asin_vector3" target="genglsl" sourcecode="mx_asin({{in}})" />
<implementation name="IM_acos_vector3_genglsl" nodedef="ND_acos_vector3" target="genglsl" sourcecode="mx_acos({{in}})" />
<implementation name="IM_atan2_vector3_genglsl" nodedef="ND_atan2_vector3" target="genglsl" sourcecode="mx_atan({{iny}}, {{inx}})" />
<implementation name="IM_sin_vector4_genglsl" nodedef="ND_sin_vector4" target="genglsl" sourcecode="mx_sin({{in}})" />
<implementation name="IM_cos_vector4_genglsl" nodedef="ND_cos_vector4" target="genglsl" sourcecode="mx_cos({{in}})" />
<implementation name="IM_tan_vector4_genglsl" nodedef="ND_tan_vector4" target="genglsl" sourcecode="mx_tan({{in}})" />
<implementation name="IM_asin_vector4_genglsl" nodedef="ND_asin_vector4" target="genglsl" sourcecode="mx_asin({{in}})" />
<implementation name="IM_acos_vector4_genglsl" nodedef="ND_acos_vector4" target="genglsl" sourcecode="mx_acos({{in}})" />
<implementation name="IM_atan2_vector4_genglsl" nodedef="ND_atan2_vector4" target="genglsl" sourcecode="mx_atan({{iny}}, {{inx}})" />

<!-- <sqrt> -->
<implementation name="IM_sqrt_float_genglsl" nodedef="ND_sqrt_float" target="genglsl" sourcecode="sqrt({{in}})" />
Expand Down
Loading

0 comments on commit cb04ef7

Please sign in to comment.