Skip to content

Commit

Permalink
Fix Metal shader errors with MaterialX 1.39
Browse files Browse the repository at this point in the history
* mx_microfacet.glsl uses functions from mx_math.metal, so the
  latter must be included first.
* Defining atan(y, x) for GLSL compatibility conflicts with
  mx_math.metal using ::atan(y_over_x). Work around this by tweaking
  the definition.
* textureQueryLevels(u_envRadiance) does not take into account that
  this texture is wrapped in a MetalTexture class.

Signed-off-by: Brecht Van Lommel <[email protected]>
  • Loading branch information
brechtvl committed Feb 5, 2025
1 parent 0fff6d9 commit 84c1a9f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
17 changes: 13 additions & 4 deletions pxr/imaging/hdSt/materialXShaderGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,17 @@ HdStMaterialXShaderGen<Base>::_EmitMxInitFunction(
emitLine("u_envIrradiance = HdGetSampler_domeLightFallback()", mxStage);
emitLine("u_envRadiance = HdGetSampler_domeLightFallback()", mxStage);
emitLine("#endif", mxStage, false);
emitLine("u_envRadianceMips = textureQueryLevels(u_envRadiance)", mxStage);
}
else {
if (std::is_same_v<Base, MaterialX::MslShaderGenerator>) {
// Msl has this wrapped in a MetalTexture class
emitLine("u_envRadianceMips = textureQueryLevels(u_envRadiance.tex)", mxStage);
}
else {
emitLine("u_envRadianceMips = textureQueryLevels(u_envRadiance)", mxStage);
}
}
emitLine("u_envRadianceMips = textureQueryLevels(u_envRadiance)", mxStage);
Base::emitLineBreak(mxStage);

// Initialize MaterialX Texture samplers with HdGetSampler equivalents
Expand Down Expand Up @@ -1309,12 +1318,12 @@ HdStMaterialXShaderGenMsl::_EmitMxFunctions(
mx::GenContext& mxContext,
mx::ShaderStage& mxStage) const
{
mx::ShaderGenerator::emitLibraryInclude(
"pbrlib/" + mx::GlslShaderGenerator::TARGET
+ "/lib/mx_microfacet.glsl", mxContext, mxStage);
mx::ShaderGenerator::emitLibraryInclude(
"stdlib/" + mx::MslShaderGenerator::TARGET
+ "/lib/mx_math.metal", mxContext, mxStage);
mx::ShaderGenerator::emitLibraryInclude(
"pbrlib/" + mx::GlslShaderGenerator::TARGET
+ "/lib/mx_microfacet.glsl", mxContext, mxStage);
_EmitConstantsUniformsAndTypeDefs(
mxContext, mxStage,_syntax->getConstantQualifier());

Expand Down
4 changes: 3 additions & 1 deletion pxr/imaging/hgiMetal/shaderGenerator.mm
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,9 @@ void _Init(
"template <typename T>\n"
"T mod(T y, T x) { return fmod(y, x); }\n\n"
"template <typename T>\n"
"T atan(T y, T x) { return atan2(y, x); }\n\n"
"T atan(T y_over_x) { return ::atan(y_over_x); }\n\n"
"template <typename T>\n"
"T atan(T y, T x) { return ::atan2(y, x); }\n\n"
"template <typename T>\n"
"T bitfieldReverse(T x) { return reverse_bits(x); }\n\n"
"template <typename T>\n"
Expand Down

0 comments on commit 84c1a9f

Please sign in to comment.