Skip to content

Commit

Permalink
Adds Metal Support to MaterialXShaderGen Backend
Browse files Browse the repository at this point in the history
Adds Metal Support to MaterialX Rendering code for testing generated shaders
Adds Metal Support to MaterialXView Rendering Support (switchable between Metal and OpenGL, default is Metal)
  • Loading branch information
Morteeza committed Feb 24, 2023
1 parent d78b4b3 commit 349f7ee
Show file tree
Hide file tree
Showing 130 changed files with 16,476 additions and 2,066 deletions.
11 changes: 10 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ option(MATERIALX_BUILD_DOCS "Create HTML documentation using Doxygen. Requires t
option(MATERIALX_BUILD_GEN_GLSL "Build the GLSL shader generator back-end." ON)
option(MATERIALX_BUILD_GEN_OSL "Build the OSL shader generator back-end." ON)
option(MATERIALX_BUILD_GEN_MDL "Build the MDL shader generator back-end." ON)
option(MATERIALX_BUILD_GEN_MSL "Build the MSL shader generator back-end." ON)
option(MATERIALX_BUILD_RENDER "Build the MaterialX Render modules." ON)
option(MATERIALX_BUILD_OIIO "Build OpenImageIO support for MaterialXRender." OFF)
option(MATERIALX_BUILD_TESTS "Build unit tests." ON)
Expand Down Expand Up @@ -105,6 +106,7 @@ mark_as_advanced(MATERIALX_BUILD_DOCS)
mark_as_advanced(MATERIALX_BUILD_GEN_GLSL)
mark_as_advanced(MATERIALX_BUILD_GEN_OSL)
mark_as_advanced(MATERIALX_BUILD_GEN_MDL)
mark_as_advanced(MATERIALX_BUILD_GEN_MSL)
mark_as_advanced(MATERIALX_BUILD_RENDER)
mark_as_advanced(MATERIALX_BUILD_OIIO)
mark_as_advanced(MATERIALX_BUILD_TESTS)
Expand Down Expand Up @@ -235,7 +237,7 @@ add_subdirectory(source/MaterialXFormat)

# Add shader generation subdirectories
add_subdirectory(source/MaterialXGenShader)
if(MATERIALX_BUILD_GEN_GLSL OR MATERIALX_BUILD_GEN_OSL OR MATERIALX_BUILD_GEN_MDL)
if(MATERIALX_BUILD_GEN_GLSL OR MATERIALX_BUILD_GEN_OSL OR MATERIALX_BUILD_GEN_MDL OR MATERIALX_BUILD_GEN_MSL)
if (MATERIALX_BUILD_GEN_GLSL)
add_definitions(-DMATERIALX_BUILD_GEN_GLSL)
add_subdirectory(source/MaterialXGenGlsl)
Expand All @@ -248,6 +250,10 @@ if(MATERIALX_BUILD_GEN_GLSL OR MATERIALX_BUILD_GEN_OSL OR MATERIALX_BUILD_GEN_MD
add_definitions(-DMATERIALX_BUILD_GEN_MDL)
add_subdirectory(source/MaterialXGenMdl)
endif()
if (MATERIALX_BUILD_GEN_MSL)
add_definitions(-DMATERIALX_BUILD_GEN_MSL)
add_subdirectory(source/MaterialXGenMsl)
endif()
add_subdirectory(libraries)
endif()

Expand All @@ -258,6 +264,9 @@ if(MATERIALX_BUILD_RENDER)
if (MATERIALX_BUILD_GEN_GLSL)
add_subdirectory(source/MaterialXRenderGlsl)
endif()
if (MATERIALX_BUILD_GEN_MSL)
add_subdirectory(source/MaterialXRenderMsl)
endif()
if (MATERIALX_BUILD_GEN_OSL)
add_subdirectory(source/MaterialXRenderOsl)
endif()
Expand Down
13 changes: 13 additions & 0 deletions libraries/lights/genmsl/lights_genmsl_impl.mtlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<materialx version="1.37">

<!-- <point_light> -->
<implementation name="IM_point_light_genmsl" nodedef="ND_point_light" file="mx_point_light.metal" function="mx_point_light" target="genmsl"/>

<!-- <directional_light> -->
<implementation name="IM_directional_light_genmsl" nodedef="ND_directional_light" file="mx_directional_light.metal" function="mx_directional_light" target="genmsl"/>

<!-- <spot_light> -->
<implementation name="IM_spot_light_genmsl" nodedef="ND_spot_light" file="mx_spot_light.metal" function="mx_spot_light" target="genmsl"/>

</materialx>
5 changes: 5 additions & 0 deletions libraries/lights/genmsl/mx_directional_light.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
void mx_directional_light(LightData light, float3 position, thread lightshader& result)
{
result.direction = -light.direction;
result.intensity = light.color * light.intensity;
}
8 changes: 8 additions & 0 deletions libraries/lights/genmsl/mx_point_light.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
void mx_point_light(LightData light, float3 position, thread lightshader& result)
{
result.direction = light.position - position;
float distance = length(result.direction) + M_FLOAT_EPS;
float attenuation = pow(distance + 1.0, light.decay_rate + M_FLOAT_EPS);
result.intensity = light.color * light.intensity / attenuation;
result.direction /= distance;
}
13 changes: 13 additions & 0 deletions libraries/lights/genmsl/mx_spot_light.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
void mx_spot_light(LightData light, float3 position, thread lightshader& result)
{
result.direction = light.position - position;
float distance = length(result.direction) + M_FLOAT_EPS;
float attenuation = pow(distance + 1.0, light.decay_rate + M_FLOAT_EPS);
result.intensity = light.color * light.intensity / attenuation;
result.direction /= distance;
float low = min(light.inner_angle, light.outer_angle);
float high = light.inner_angle;
float cosDir = dot(result.direction, -light.direction);
float spotAttenuation = smoothstep(low, high, cosDir);
result.intensity *= spotAttenuation;
}
29 changes: 25 additions & 4 deletions libraries/pbrlib/genglsl/lib/mx_microfacet_specular.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ struct FresnelData

// Refraction
bool refraction;

#ifdef __METAL__
FresnelData(int _model = 0,
vec3 _ior = vec3(0.0f),
vec3 _extinction = vec3(0.0f),
vec3 _F0 = vec3(0.0f),
vec3 _F90 = vec3(0.0f),
float _exponent = 0.0f,
float _tf_thickness = 0.0f,
float _tf_ior = 0.0f,
bool _refraction = false) :
model(_model),
ior(_ior),
extinction(_extinction),
F0(_F0), F90(_F90), exponent(_exponent),
tf_thickness(_tf_thickness),
tf_ior(_tf_ior),
refraction(_refraction) {}
#endif

};

// https://media.disneyanimation.com/uploads/production/publication_asset/48/asset/s2012_pbs_disney_brdf_notes_v3.pdf
Expand Down Expand Up @@ -333,11 +353,12 @@ void mx_fresnel_dielectric_phase_polarized(float cosTheta, float eta1, float eta
// Phase shift due to a conducting material
void mx_fresnel_conductor_phase_polarized(float cosTheta, float eta1, vec3 eta2, vec3 kappa2, out vec3 phiP, out vec3 phiS)
{
if (kappa2 == vec3(0, 0, 0) && eta2.x == eta2.y && eta2.y == eta2.z) {
if (dot(kappa2, kappa2) == 0 && eta2.x == eta2.y && eta2.y == eta2.z) {
// Use dielectric formula to increase performance
mx_fresnel_dielectric_phase_polarized(cosTheta, eta1, eta2.x, phiP.x, phiS.x);
phiP = phiP.xxx;
phiS = phiS.xxx;
float phiPx, phiSx;
mx_fresnel_dielectric_phase_polarized(cosTheta, eta1, eta2.x, phiPx, phiSx);
phiP = vec3(phiPx, phiPx, phiPx);
phiS = vec3(phiSx, phiSx, phiSx);
return;
}
vec3 k2 = kappa2 / eta2;
Expand Down
74 changes: 74 additions & 0 deletions libraries/pbrlib/genmsl/pbrlib_genglsl_impl.mtlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0"?>
<materialx version="1.38">

<!-- <oren_nayar_diffuse_bsdf> -->
<implementation name="IM_oren_nayar_diffuse_bsdf_genmsl" nodedef="ND_oren_nayar_diffuse_bsdf" file="../genglsl/mx_oren_nayar_diffuse_bsdf.glsl" function="mx_oren_nayar_diffuse_bsdf" target="genmsl" />

<!-- <burley_diffuse_bsdf> -->
<implementation name="IM_burley_diffuse_bsdf_genmsl" nodedef="ND_burley_diffuse_bsdf" file="../genglsl/mx_burley_diffuse_bsdf.glsl" function="mx_burley_diffuse_bsdf" target="genmsl" />

<!-- <translucent_bsdf> -->
<implementation name="IM_translucent_bsdf_genmsl" nodedef="ND_translucent_bsdf" file="../genglsl/mx_translucent_bsdf.glsl" function="mx_translucent_bsdf" target="genmsl" />

<!-- <dielectric_bsdf> -->
<implementation name="IM_dielectric_bsdf_genmsl" nodedef="ND_dielectric_bsdf" file="../genglsl/mx_dielectric_bsdf.glsl" function="mx_dielectric_bsdf" target="genmsl" />

<!-- <conductor_bsdf> -->
<implementation name="IM_conductor_bsdf_genmsl" nodedef="ND_conductor_bsdf" file="../genglsl/mx_conductor_bsdf.glsl" function="mx_conductor_bsdf" target="genmsl" />

<!-- <generalized_schlick_bsdf> -->
<implementation name="IM_generalized_schlick_bsdf_genmsl" nodedef="ND_generalized_schlick_bsdf" file="../genglsl/mx_generalized_schlick_bsdf.glsl" function="mx_generalized_schlick_bsdf" target="genmsl" />

<!-- <subsurface_bsdf> -->
<implementation name="IM_subsurface_bsdf_genmsl" nodedef="ND_subsurface_bsdf" file="../genglsl/mx_subsurface_bsdf.glsl" function="mx_subsurface_bsdf" target="genmsl" />

<!-- <sheen_bsdf> -->
<implementation name="IM_sheen_bsdf_genmsl" nodedef="ND_sheen_bsdf" file="../genglsl/mx_sheen_bsdf.glsl" function="mx_sheen_bsdf" target="genmsl" />

<!-- <anisotropic_vdf> -->
<implementation name="IM_anisotropic_vdf_genmsl" nodedef="ND_anisotropic_vdf" file="../genglsl/mx_anisotropic_vdf.glsl" function="mx_anisotropic_vdf" target="genmsl" />

<!-- <thin_film_bsdf> -->
<implementation name="IM_thin_film_bsdf_genmsl" nodedef="ND_thin_film_bsdf" target="genmsl" />

<!-- <layer> -->
<implementation name="IM_layer_bsdf_genmsl" nodedef="ND_layer_bsdf" target="genmsl" />
<implementation name="IM_layer_vdf_genmsl" nodedef="ND_layer_vdf" target="genmsl" />

<!-- <mix> -->
<implementation name="IM_mix_bsdf_genmsl" nodedef="ND_mix_bsdf" target="genmsl" />
<implementation name="IM_mix_edf_genmsl" nodedef="ND_mix_edf" target="genmsl" />

<!-- <add> -->
<implementation name="IM_add_bsdf_genmsl" nodedef="ND_add_bsdf" target="genmsl" />
<implementation name="IM_add_edf_genmsl" nodedef="ND_add_edf" target="genmsl" />

<!-- <multiply> -->
<implementation name="IM_multiply_bsdfC_genmsl" nodedef="ND_multiply_bsdfC" target="genmsl" />
<implementation name="IM_multiply_bsdfF_genmsl" nodedef="ND_multiply_bsdfF" target="genmsl" />
<implementation name="IM_multiply_edfC_genmsl" nodedef="ND_multiply_edfC" target="genmsl" />
<implementation name="IM_multiply_edfF_genmsl" nodedef="ND_multiply_edfF" target="genmsl" />

<!-- <uniform_edf> -->
<implementation name="IM_uniform_edf_genmsl" nodedef="ND_uniform_edf" file="../genglsl/mx_uniform_edf.glsl" function="mx_uniform_edf" target="genmsl" />

<!-- <surface> -->
<implementation name="IM_surface_genmsl" nodedef="ND_surface" target="genmsl" />

<!-- <displacement> -->
<implementation name="IM_displacement_float_genmsl" nodedef="ND_displacement_float" file="../genglsl/mx_displacement_float.glsl" function="mx_displacement_float" target="genmsl" />
<implementation name="IM_displacement_vector3_genmsl" nodedef="ND_displacement_vector3" file="../genglsl/mx_displacement_vector3.glsl" function="mx_displacement_vector3" target="genmsl" />

<!-- <light> -->
<implementation name="IM_light_genmsl" nodedef="ND_light" target="genmsl" />

<!-- <roughness_anisotropy> -->
<implementation name="IM_roughness_anisotropy_genmsl" nodedef="ND_roughness_anisotropy" file="../genglsl/mx_roughness_anisotropy.glsl" function="mx_roughness_anisotropy" target="genmsl" />

<!-- <roughness_dual> -->
<implementation name="IM_roughness_dual_genmsl" nodedef="ND_roughness_dual" file="../genglsl/mx_roughness_dual.glsl" function="mx_roughness_dual" target="genmsl" />

<!-- <artistic_ior> -->
<implementation name="IM_artistic_ior_genmsl" nodedef="ND_artistic_ior" file="../genglsl/mx_artistic_ior.glsl" function="mx_artistic_ior" target="genmsl" />

</materialx>
2 changes: 1 addition & 1 deletion libraries/stdlib/genglsl/lib/mx_transform_color.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ vec3 mx_srgb_texture_to_lin_rec709(vec3 color)
bvec3 isAbove = greaterThan(color, vec3(0.04045));
vec3 linSeg = color / 12.92;
vec3 powSeg = pow(max(color + vec3(0.055), vec3(0.0)) / 1.055, vec3(2.4));
return mix(linSeg, powSeg, isAbove);
return mix(linSeg, powSeg, vec3(isAbove));
}
91 changes: 91 additions & 0 deletions libraries/stdlib/genmsl/lib/mx_hsv.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
Color transform functions.
These funcions are modified versions of the color operators found in Open Shading Language:
github.com/imageworks/OpenShadingLanguage/blob/master/src/liboslexec/opcolor.cpp
It contains the subset of color operators needed to implement the MaterialX
standard library. The modifications are for conversions from C++ to GLSL.
Original copyright notice:
------------------------------------------------------------------------
Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
All Rights Reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Sony Pictures Imageworks nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
------------------------------------------------------------------------
*/

vec3 mx_hsvtorgb(vec3 hsv)
{
// Reference for this technique: Foley & van Dam
float h = hsv.x; float s = hsv.y; float v = hsv.z;
if (s < 0.0001f) {
return vec3 (v, v, v);
} else {
h = 6.0f * (h - floor(h)); // expand to [0..6)
int hi = int(trunc(h));
float f = h - float(hi);
float p = v * (1.0f-s);
float q = v * (1.0f-s*f);
float t = v * (1.0f-s*(1.0f-f));
if (hi == 0)
return vec3 (v, t, p);
else if (hi == 1)
return vec3 (q, v, p);
else if (hi == 2)
return vec3 (p, v, t);
else if (hi == 3)
return vec3 (p, q, v);
else if (hi == 4)
return vec3 (t, p, v);
return vec3 (v, p, q);
}
}


vec3 mx_rgbtohsv(vec3 c)
{
// See Foley & van Dam
float r = c.x; float g = c.y; float b = c.z;
float mincomp = min (r, min(g, b));
float maxcomp = max (r, max(g, b));
float delta = maxcomp - mincomp; // chroma
float h, s, v;
v = maxcomp;
if (maxcomp > 0.0f)
s = delta / maxcomp;
else s = 0.0f;
if (s <= 0.0f)
h = 0.0f;
else {
if (r >= maxcomp) h = (g-b) / delta;
else if (g >= maxcomp) h = 2.0f + (b-r) / delta;
else h = 4.0f + (r-g) / delta;
h *= (1.0f/6.0f);
if (h < 0.0f)
h += 1.0f;
}
return vec3(h, s, v);
}
Loading

0 comments on commit 349f7ee

Please sign in to comment.