Skip to content

Commit

Permalink
Merge branch 'AcademySoftwareFoundation:main' into downstream_traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
kwokcb committed Jul 4, 2023
2 parents b423b74 + cdd53d2 commit 59758e8
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 114 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ jobs:
run: |
python MaterialXTest/main.py
python MaterialXTest/genshader.py
python Scripts/mxformat.py ../resources/Materials/TestSuite/stdlib/upgrade --yes --upgradeVersion True
python Scripts/mxformat.py ../resources/Materials/TestSuite/stdlib/upgrade --yes --upgrade
python Scripts/mxvalidate.py ../resources/Materials/Examples/StandardSurface/standard_surface_marble_solid.mtlx --stdlib --verbose
python Scripts/mxdoc.py --docType md ../libraries/pbrlib/pbrlib_defs.mtlx
python Scripts/mxdoc.py --docType html ../libraries/bxdf/standard_surface.mtlx
Expand Down
2 changes: 1 addition & 1 deletion libraries/lights/genmsl/lights_genmsl_impl.mtlx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<materialx version="1.37">
<materialx version="1.38">

<!-- <point_light> -->
<implementation name="IM_point_light_genmsl" nodedef="ND_point_light" file="mx_point_light.metal" function="mx_point_light" target="genmsl" />
Expand Down
17 changes: 9 additions & 8 deletions libraries/pbrlib/genglsl/lib/mx_environment_fis.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ vec3 mx_environment_radiance(vec3 N, vec3 V, vec3 X, vec2 alpha, int distributio
// Compute derived properties.
float NdotV = clamp(V.z, M_FLOAT_EPS, 1.0);
float avgAlpha = mx_average_alpha(alpha);
float G1V = mx_ggx_smith_G1(NdotV, avgAlpha);

// Integrate outgoing radiance using filtered importance sampling.
// http://cgg.mff.cuni.cz/~jaroslav/papers/2008-egsr-fis/2008-egsr-fis-final-embedded.pdf
Expand All @@ -33,18 +34,16 @@ vec3 mx_environment_radiance(vec3 N, vec3 V, vec3 X, vec2 alpha, int distributio
vec2 Xi = mx_spherical_fibonacci(i, envRadianceSamples);

// Compute the half vector and incoming light direction.
vec3 H = mx_ggx_importance_sample_NDF(Xi, alpha);
vec3 H = mx_ggx_importance_sample_VNDF(Xi, V, alpha);
vec3 L = fd.refraction ? mx_refraction_solid_sphere(-V, H, fd.ior.x) : -reflect(V, H);

// Compute dot products for this sample.
float NdotH = clamp(H.z, M_FLOAT_EPS, 1.0);
float NdotL = clamp(L.z, M_FLOAT_EPS, 1.0);
float VdotH = clamp(dot(V, H), M_FLOAT_EPS, 1.0);
float LdotH = VdotH;

// Sample the environment light from the given direction.
vec3 Lw = tangentToWorld * L;
float pdf = mx_ggx_PDF(H, LdotH, alpha);
float pdf = mx_ggx_NDF(H, alpha) * G1V / (4.0 * NdotV);
float lod = mx_latlong_compute_lod(Lw, pdf, float($envRadianceMips - 1), envRadianceSamples);
vec3 sampleColor = mx_latlong_map_lookup(Lw, $envMatrix, lod, $envRadiance);

Expand All @@ -61,13 +60,15 @@ vec3 mx_environment_radiance(vec3 N, vec3 V, vec3 X, vec2 alpha, int distributio
// From https://cdn2.unrealengine.com/Resources/files/2013SiggraphPresentationsNotes-26915738.pdf
// incidentLight = sampleColor * NdotL
// microfacetSpecular = D * F * G / (4 * NdotL * NdotV)
// pdf = D * NdotH / (4 * VdotH)
// pdf = D * G1V / (4 * NdotV);
// radiance = incidentLight * microfacetSpecular / pdf
radiance += sampleColor * FG * VdotH / (NdotV * NdotH);
radiance += sampleColor * FG;
}

// Normalize and return the final radiance.
radiance /= float(envRadianceSamples);
// Apply the global component of the geometric term and normalize.
radiance /= G1V * float(envRadianceSamples);

// Return the final radiance.
return radiance;
}

Expand Down
46 changes: 10 additions & 36 deletions libraries/pbrlib/genglsl/lib/mx_microfacet_specular.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -61,48 +61,22 @@ float mx_ggx_NDF(vec3 H, vec2 alpha)
return 1.0 / (M_PI * alpha.x * alpha.y * mx_square(denom));
}

// https://media.disneyanimation.com/uploads/production/publication_asset/48/asset/s2012_pbs_disney_brdf_notes_v3.pdf
// Appendix B.1 Equation 3
float mx_ggx_PDF(vec3 H, float LdotH, vec2 alpha)
{
float NdotH = H.z;
return mx_ggx_NDF(H, alpha) * NdotH / (4.0 * LdotH);
}

// https://media.disneyanimation.com/uploads/production/publication_asset/48/asset/s2012_pbs_disney_brdf_notes_v3.pdf
// Appendix B.2 Equation 15
vec3 mx_ggx_importance_sample_NDF(vec2 Xi, vec2 alpha)
{
float phi = 2.0 * M_PI * Xi.x;
float tanTheta = sqrt(Xi.y / (1.0 - Xi.y));
vec3 H = vec3(tanTheta * alpha.x * cos(phi),
tanTheta * alpha.y * sin(phi),
1.0);
return normalize(H);
}

// http://jcgt.org/published/0007/04/01/paper.pdf
// Appendix A Listing 1
// https://ggx-research.github.io/publication/2023/06/09/publication-ggx.html
vec3 mx_ggx_importance_sample_VNDF(vec2 Xi, vec3 V, vec2 alpha)
{
// Transform the view direction to the hemisphere configuration.
V = normalize(vec3(V.xy * alpha, V.z));

// Construct an orthonormal basis from the view direction.
float len = length(V.xy);
vec3 T1 = (len > 0.0) ? vec3(-V.y, V.x, 0.0) / len : vec3(1.0, 0.0, 0.0);
vec3 T2 = cross(V, T1);

// Parameterization of the projected area.
float r = sqrt(Xi.y);
// Sample a spherical cap in (-V.z, 1].
float phi = 2.0 * M_PI * Xi.x;
float t1 = r * cos(phi);
float t2 = r * sin(phi);
float s = 0.5 * (1.0 + V.z);
t2 = (1.0 - s) * sqrt(1.0 - mx_square(t1)) + s * t2;

// Reprojection onto hemisphere.
vec3 H = t1 * T1 + t2 * T2 + sqrt(max(0.0, 1.0 - mx_square(t1) - mx_square(t2))) * V;
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);
vec3 c = vec3(x, y, z);

// Compute the microfacet normal.
vec3 H = c + V;

// Transform the microfacet normal back to the ellipsoid configuration.
H = normalize(vec3(H.xy * alpha, max(H.z, 0.0)));
Expand Down
4 changes: 2 additions & 2 deletions libraries/pbrlib/pbrlib_defs.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@
-->
<nodedef name="ND_conductor_bsdf" node="conductor_bsdf" bsdf="R" nodegroup="pbr" doc="A reflection BSDF node based on a microfacet model and a Fresnel curve for conductors/metals.">
<input name="weight" type="float" value="1.0" uimin="0.0" uimax="1.0" />
<input name="ior" type="color3" value="0.271, 0.677, 1.316" colorspace="none" />
<input name="extinction" type="color3" value="3.609, 2.625, 2.292" colorspace="none" />
<input name="ior" type="color3" value="0.183, 0.421, 1.373" colorspace="none" />
<input name="extinction" type="color3" value="3.424, 2.346, 1.770" colorspace="none" />
<input name="roughness" type="vector2" value="0.05, 0.05" />
<input name="normal" type="vector3" defaultgeomprop="Nworld" />
<input name="tangent" type="vector3" defaultgeomprop="Tworld" />
Expand Down
2 changes: 1 addition & 1 deletion libraries/stdlib/stdlib_defs.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -2280,7 +2280,7 @@
<input name="in2" type="vector4" uiname="in2" value="0.0, 0.0, 0.0, 0.0" />
<output name="out" type="float" />
</nodedef>

<!--
Node: <dotproduct>
Perform a dot product of two 2-4 channel vectors
Expand Down
14 changes: 7 additions & 7 deletions libraries/stdlib/stdlib_ng.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@
<dotproduct name="N_dotBlendedN" type="float">
<input name="in1" type="vector3" nodename="N_blendPower" />
<input name="in2" type="vector3" value="1.0, 1.0, 1.0" />
</dotproduct>
</dotproduct>
<divide name="N_normalizeBlendedWeights" type="vector3">
<input name="in1" type="vector3" nodename="N_blendPower" />
<input name="in2" type="float" nodename="N_dotBlendedN" />
Expand Down Expand Up @@ -411,7 +411,7 @@
<dotproduct name="N_dotBlendedN" type="float">
<input name="in1" type="vector3" nodename="N_blendPower" />
<input name="in2" type="vector3" value="1.0, 1.0, 1.0" />
</dotproduct>
</dotproduct>
<divide name="N_normalizeBlendedWeights" type="vector3">
<input name="in1" type="vector3" nodename="N_blendPower" />
<input name="in2" type="float" nodename="N_dotBlendedN" />
Expand Down Expand Up @@ -531,7 +531,7 @@
<dotproduct name="N_dotBlendedN" type="float">
<input name="in1" type="vector3" nodename="N_blendPower" />
<input name="in2" type="vector3" value="1.0, 1.0, 1.0" />
</dotproduct>
</dotproduct>
<divide name="N_normalizeBlendedWeights" type="vector3">
<input name="in1" type="vector3" nodename="N_blendPower" />
<input name="in2" type="float" nodename="N_dotBlendedN" />
Expand Down Expand Up @@ -651,7 +651,7 @@
<dotproduct name="N_dotBlendedN" type="float">
<input name="in1" type="vector3" nodename="N_blendPower" />
<input name="in2" type="vector3" value="1.0, 1.0, 1.0" />
</dotproduct>
</dotproduct>
<divide name="N_normalizeBlendedWeights" type="vector3">
<input name="in1" type="vector3" nodename="N_blendPower" />
<input name="in2" type="float" nodename="N_dotBlendedN" />
Expand Down Expand Up @@ -771,7 +771,7 @@
<dotproduct name="N_dotBlendedN" type="float">
<input name="in1" type="vector3" nodename="N_blendPower" />
<input name="in2" type="vector3" value="1.0, 1.0, 1.0" />
</dotproduct>
</dotproduct>
<divide name="N_normalizeBlendedWeights" type="vector3">
<input name="in1" type="vector3" nodename="N_blendPower" />
<input name="in2" type="float" nodename="N_dotBlendedN" />
Expand Down Expand Up @@ -891,7 +891,7 @@
<dotproduct name="N_dotBlendedN" type="float">
<input name="in1" type="vector3" nodename="N_blendPower" />
<input name="in2" type="vector3" value="1.0, 1.0, 1.0" />
</dotproduct>
</dotproduct>
<divide name="N_normalizeBlendedWeights" type="vector3">
<input name="in1" type="vector3" nodename="N_blendPower" />
<input name="in2" type="float" nodename="N_dotBlendedN" />
Expand Down Expand Up @@ -1540,7 +1540,7 @@
</magnitude>
<output name="out" type="float" nodename="N_mtlxmagnitude" />
</nodegraph>

<!-- ======================================================================== -->
<!-- Adjustment nodes -->
<!-- ======================================================================== -->
Expand Down
81 changes: 30 additions & 51 deletions python/Scripts/mxformat.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,38 @@
#!/usr/bin/env python
'''
Format MaterialX document content by reanding and writing files. Optionally
upgrade the document version.
Reformat a folder of MaterialX documents in place, optionally upgrading
the documents to the latest version of the standard.
'''

import sys, os, argparse
import MaterialX as mx

def getFiles(rootPath):
filelist = []
for subdir, dirs, files in os.walk(rootPath):
for file in files:
if file.endswith('mtlx'):
filelist.append(os.path.join(subdir, file))
return filelist

def main():
parser = argparse.ArgumentParser(description="Format document by reading the file and writing it back out. Optionally update to the latest version.")
parser = argparse.ArgumentParser(description="Reformat a folder of MaterialX documents in place.")
parser.add_argument("--yes", dest="yes", action="store_true", help="Proceed without asking for confirmation from the user.")
parser.add_argument('--checkForChanges', dest='checkForChanges', type=mx.stringToBoolean, default=True, help='Check if a file has changed. Default is True')
parser.add_argument('--upgradeVersion', dest='upgradeVersion', type=mx.stringToBoolean, default=False, help='Upgrade the document version. Default is False')
parser.add_argument('--upgrade', dest='upgrade', action="store_true", help='Upgrade documents to the latest version of the standard.')
parser.add_argument(dest="inputFolder", help="An input folder to scan for MaterialX documents.")
opts = parser.parse_args()

fileList = []
rootPath = opts.inputFolder
if os.path.isdir(rootPath):
fileList = getFiles(rootPath)
else:
fileList.append(rootPath)

# Preserve version, comments and newlines
readOptions = mx.XmlReadOptions()
readOptions.readComments = True
readOptions.readNewlines = True
readOptions.upgradeVersion = opts.upgradeVersion

validDocs = dict()
for filename in fileList:
doc = mx.createDocument()
try:
mx.readFromXmlFile(doc, filename, mx.FileSearchPath(), readOptions)
validDocs[filename] = doc
except mx.Exception:
pass
for root, dirs, files in os.walk(opts.inputFolder):
for file in files:
if file.endswith('.mtlx'):
filename = os.path.join(root, file)
doc = mx.createDocument()
try:
readOptions = mx.XmlReadOptions()
readOptions.readComments = True
readOptions.readNewlines = True
readOptions.upgradeVersion = opts.upgrade
try:
mx.readFromXmlFile(doc, filename, mx.FileSearchPath(), readOptions)
except Exception as err:
print('Skipping "' + file + '" due to exception: ' + str(err))
continue
validDocs[filename] = doc
except mx.Exception:
pass

if not validDocs:
print('No MaterialX documents were found in "%s"' % (opts.inputFolder))
Expand All @@ -54,31 +43,21 @@ def main():
mxVersion = mx.getVersionIntegers()

if not opts.yes:
question = 'Would you like to update all %i documents in place (y/n)?' % len(validDocs)
if opts.upgradeVersion:
question = 'Would you like to update all %i documents to MaterialX v%i.%i in place (y/n)?' % (len(validDocs), mxVersion[0], mxVersion[1])
if opts.upgrade:
question = 'Would you like to upgrade all %i documents to MaterialX v%i.%i in place (y/n)?' % (len(validDocs), mxVersion[0], mxVersion[1])
else:
question = 'Would you like to reformat all %i documents in place (y/n)?' % len(validDocs)
answer = input(question)
if answer != 'y' and answer != 'Y':
return

writeCount = 0
for (filename, doc) in validDocs.items():
writeFile = True
if opts.checkForChanges:
origString = mx.readFile(filename)
docString = mx.writeToXmlString(doc)
if origString == docString:
writeFile = False

if writeFile:
writeCount = writeCount + 1
print('- Updated file %s.' % filename)
mx.writeToXmlFile(doc, filename)
mx.writeToXmlFile(doc, filename)

if opts.upgradeVersion:
print('Updated %i documents to MaterialX v%i.%i' % (writeCount, mxVersion[0], mxVersion[1]))
if opts.upgrade:
print('Upgraded %i documents to MaterialX v%i.%i' % (len(validDocs), mxVersion[0], mxVersion[1]))
else:
print('Updated %i documents ' % writeCount)
print('Reformatted %i documents ' % len(validDocs))

if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion resources/Lights/goegap_split.mtlx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<materialx version="1.37">
<materialx version="1.38">
<directional_light name="dir_light" type="lightshader">
<input name="direction" type="vector3" value="0.43277, -0.725547, -0.535062" />
<input name="color" type="color3" value="0.993688, 1, 0.993529" />
Expand Down
2 changes: 1 addition & 1 deletion resources/Lights/san_giuseppe_bridge_split.mtlx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<materialx version="1.37">
<materialx version="1.38">
<directional_light name="dir_light" type="lightshader">
<input name="direction" type="vector3" value="0.514434, -0.479014, -0.711269" />
<input name="color" type="color3" value="1, 0.894474, 0.567234" />
Expand Down
2 changes: 1 addition & 1 deletion resources/Lights/table_mountain_split.mtlx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<materialx version="1.37">
<materialx version="1.38">
<directional_light name="dir_light" type="lightshader">
<input name="direction" type="vector3" value="0.757663, -0.126589, -0.640251" />
<input name="color" type="color3" value="1, 0.844753, 0.418417" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0"?>
<!--
<materialx version="1.36">
<!--
Upgrade path test from 1.36 to 1.37
Upgrade path test from 1.36 to 1.37
-->
<materialx version="1.36">
-->

<!-- invert matrix to invertmatrix -->
<invert name="invert1" type="matrix33">
Expand Down
2 changes: 2 additions & 0 deletions source/PyMaterialX/PyMaterialXRender/PyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ void bindPyOiioImageLoader(py::module& mod);
void bindPyTinyObjLoader(py::module& mod);
void bindPyCamera(py::module& mod);
void bindPyShaderRenderer(py::module& mod);
void bindPyCgltfLoader(py::module& mod);

PYBIND11_MODULE(PyMaterialXRender, mod)
{
Expand All @@ -36,4 +37,5 @@ PYBIND11_MODULE(PyMaterialXRender, mod)
bindPyTinyObjLoader(mod);
bindPyCamera(mod);
bindPyShaderRenderer(mod);
bindPyCgltfLoader(mod);
}

0 comments on commit 59758e8

Please sign in to comment.