Skip to content

Commit b33ebd4

Browse files
committed
Move backend specific shadergen features behind ifdefs so that each backend is isolated from the others, and removes the need for MaterialX patches.
1 parent 269f9ec commit b33ebd4

5 files changed

Lines changed: 72 additions & 34 deletions

File tree

build_scripts/build_usd.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,19 +1538,8 @@ def InstallMaterialX(context, force, buildArgs):
15381538
# but MaterialX intertwines GLSL shadergen support with also requiring rendering support.
15391539
cmakeOptions.extend([
15401540
'-DMATERIALX_BUILD_GEN_MSL=ON',
1541-
'-DMATERIALX_BUILD_GEN_GLSL=ON',
1541+
'-DMATERIALX_BUILD_GEN_GLSL=OFF',
15421542
'-DMATERIALX_BUILD_IOS=ON'])
1543-
PatchFile("CMakeLists.txt",
1544-
[(' set(MATERIALX_BUILD_GEN_GLSL OFF)',
1545-
' set(MATERIALX_BUILD_GEN_GLSL ON)'),
1546-
(' if (MATERIALX_BUILD_GEN_GLSL)\n' +
1547-
' add_subdirectory(source/MaterialXRenderGlsl)\n' +
1548-
' endif()',
1549-
' if (MATERIALX_BUILD_GEN_GLSL AND NOT MATERIALX_BUILD_IOS)\n' +
1550-
' add_subdirectory(source/MaterialXRenderGlsl)\n' +
1551-
' endif()')
1552-
], multiLineMatches=True)
1553-
15541543
cmakeOptions += buildArgs
15551544
RunCMake(context, force, cmakeOptions)
15561545

pxr/imaging/hdSt/CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,16 @@ if (${PXR_ENABLE_MATERIALX_SUPPORT})
2424
MaterialXRender
2525
MaterialXCore
2626
MaterialXFormat
27-
MaterialXGenGlsl
28-
MaterialXGenMsl
2927
hdMtlx
3028
)
29+
if (${PXR_ENABLE_METAL_SUPPORT})
30+
list(APPEND optionalLibs MaterialXGenMsl)
31+
endif()
32+
33+
if (${PXR_ENABLE_GL_SUPPORT})
34+
list(APPEND optionalLibs MaterialXGenGlsl)
35+
endif()
36+
3137
list(APPEND optionalPrivateClasses
3238
materialXFilter
3339
materialXShaderGen

pxr/imaging/hdSt/materialXFilter.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,21 +166,27 @@ _CreateHdStMaterialXContext(
166166
HdSt_MxShaderGenInfo const& mxHdInfo,
167167
TfToken const& apiName)
168168
{
169+
#if defined(PXR_METAL_SUPPORT_ENABLED)
169170
if (apiName == HgiTokens->Metal) {
170171
return HdStMaterialXShaderGenMsl::create(mxHdInfo);
171172
}
173+
#endif
174+
#if defined(PXR_VULKAN_SUPPORT_ENABLED)
172175
if (apiName == HgiTokens->Vulkan) {
173176
return HdStMaterialXShaderGenVkGlsl::create(mxHdInfo);
174177
}
178+
#endif
179+
#if defined(PXR_OPENGL_SUPPORT_ENABLED)
175180
if (apiName == HgiTokens->OpenGL) {
176181
return HdStMaterialXShaderGenGlsl::create(mxHdInfo);
177182
}
178-
else {
179-
TF_CODING_ERROR(
180-
"MaterialX Shader Generator doesn't support %s API.",
181-
apiName.GetText());
182-
return mx::ShaderGeneratorPtr();
183-
}
183+
#endif
184+
185+
TF_CODING_ERROR(
186+
"MaterialX Shader Generator doesn't support %s API.",
187+
apiName.GetText());
188+
return mx::ShaderGeneratorPtr();
189+
184190
}
185191

186192
// Use the given mxDocument to generate the corresponding glsl shader

pxr/imaging/hdSt/materialXShaderGen.cpp

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
#include <MaterialXGenShader/Shader.h>
1313
#include <MaterialXGenShader/ShaderGenerator.h>
1414
#include <MaterialXGenShader/Syntax.h>
15+
#if defined(PXR_OPENGL_SUPPORT_ENABLED)
1516
#include <MaterialXGenGlsl/Nodes/SurfaceNodeGlsl.h>
17+
#endif
18+
#if defined(PXR_METAL_SUPPORT_ENABLED)
1619
#include <MaterialXGenMsl/Nodes/SurfaceNodeMsl.h>
20+
#endif
1721
#include <MaterialXGenMsl/MslResourceBindingContext.h>
1822
#include <MaterialXGenMsl/MslShaderGenerator.h>
1923

@@ -482,10 +486,14 @@ HdStMaterialXShaderGen<Base>::_EmitMxVertexDataDeclarations(
482486
const std::string &targetShadingLanguage = Base::getTarget();
483487

484488
// add beginning ( or {
489+
#if defined(PXR_GL_SUPPORT_ENABLED)
485490
if (targetShadingLanguage == mx::GlslShaderGenerator::TARGET) {
486491
line += "(";
487492
}
488493
else if (targetShadingLanguage == mx::MslShaderGenerator::TARGET) {
494+
#elif defined(PXR_METAL_SUPPORT_ENABLED)
495+
if (targetShadingLanguage == mx::MslShaderGenerator::TARGET) {
496+
#endif
489497
line += "{";
490498
}
491499
else {
@@ -500,13 +508,16 @@ HdStMaterialXShaderGen<Base>::_EmitMxVertexDataDeclarations(
500508
}
501509

502510
// add ending ) or }
511+
#if defined(PXR_OPENGL_SUPPORT_ENABLED)
503512
if (targetShadingLanguage == mx::GlslShaderGenerator::TARGET) {
504513
line += ")";
505514
}
506515
else if (targetShadingLanguage == mx::MslShaderGenerator::TARGET) {
516+
#elif defined(PXR_METAL_SUPPORT_ENABLED)
517+
if (targetShadingLanguage == mx::MslShaderGenerator::TARGET) {
507518
line += "}";
508519
}
509-
520+
#endif
510521
emitLine(line, mxStage);
511522
}
512523

@@ -776,17 +787,23 @@ HdStMaterialXShaderGen<Base>::_EmitDataStructsAndFunctionDefinitions(
776787
}
777788
if (shadowing) {
778789
mx::ShaderGenerator::emitLibraryInclude(
779-
"pbrlib/" + mx::GlslShaderGenerator::TARGET
780-
+ "/lib/mx_shadow.glsl", mxContext, mxStage);
790+
"pbrlib/"
791+
#if defined(PXR_OPENGL_SUPPORT_ENABLED)
792+
+ mx::GlslShaderGenerator::TARGET + "/" +
793+
#endif
794+
"lib/mx_shadow.glsl", mxContext, mxStage);
781795
}
782796

783797
// Emit directional albedo table code.
784798
if (mxContext.getOptions().hwDirectionalAlbedoMethod ==
785799
mx::HwDirectionalAlbedoMethod::DIRECTIONAL_ALBEDO_TABLE ||
786800
mxContext.getOptions().hwWriteAlbedoTable) {
787801
mx::ShaderGenerator::emitLibraryInclude(
788-
"pbrlib/" + mx::GlslShaderGenerator::TARGET
789-
+ "/lib/mx_table.glsl", mxContext, mxStage);
802+
"pbrlib/"
803+
#if defined(PXR_OPENGL_SUPPORT_ENABLED)
804+
+ mx::GlslShaderGenerator::TARGET + "/" +
805+
#endif
806+
"lib/mx_table.glsl", mxContext, mxStage);
790807
Base::emitLineBreak(mxStage);
791808
}
792809

@@ -832,7 +849,7 @@ HdStMaterialXShaderGen<Base>::_EmitDataStructsAndFunctionDefinitions(
832849
// ----------------------------------------------------------------------------
833850
// HdSt MaterialX ShaderGen OpenGL GLSL
834851
// ----------------------------------------------------------------------------
835-
852+
#if defined(PXR_OPENGL_SUPPORT_ENABLED)
836853
namespace {
837854
// Create a customized version of the class mx::SurfaceNodeGlsl
838855
// to be able to notify the shader generator when we start/end
@@ -982,7 +999,9 @@ HdStMaterialXShaderGenGlsl::_EmitMxFunctions(
982999
_EmitDataStructsAndFunctionDefinitions(
9831000
mxGraph, mxContext, mxStage, &_tokenSubstitutions);
9841001
}
1002+
#endif
9851003

1004+
#if defined(PXR_VULKAN_SUPPORT_ENABLED)
9861005
// ----------------------------------------------------------------------------
9871006
// HdSt MaterialX ShaderGen Vulkan GLSL
9881007
// ----------------------------------------------------------------------------
@@ -1134,7 +1153,9 @@ HdStMaterialXShaderGenVkGlsl::_EmitMxFunctions(
11341153
_EmitDataStructsAndFunctionDefinitions(
11351154
mxGraph, mxContext, mxStage, &_tokenSubstitutions);
11361155
}
1156+
#endif
11371157

1158+
#if defined(PXR_METAL_SUPPORT_ENABLED)
11381159
// ----------------------------------------------------------------------------
11391160
// HdSt MaterialX ShaderGen Metal
11401161
// ----------------------------------------------------------------------------
@@ -1295,11 +1316,17 @@ HdStMaterialXShaderGenMsl::_EmitMxFunctions(
12951316
mx::ShaderStage& mxStage) const
12961317
{
12971318
mx::ShaderGenerator::emitLibraryInclude(
1298-
"pbrlib/" + mx::GlslShaderGenerator::TARGET
1299-
+ "/lib/mx_microfacet.glsl", mxContext, mxStage);
1319+
"pbrlib/"
1320+
#if defined(PXR_OPENGL_SUPPORT_ENABLED)
1321+
+ mx::GlslShaderGenerator::TARGET + "/" +
1322+
#endif
1323+
"lib/mx_microfacet.glsl", mxContext, mxStage);
13001324
mx::ShaderGenerator::emitLibraryInclude(
1301-
"stdlib/" + mx::MslShaderGenerator::TARGET
1302-
+ "/lib/mx_math.metal", mxContext, mxStage);
1325+
"stdlib/"
1326+
#if defined(PXR_OPENGL_SUPPORT_ENABLED)
1327+
+ mx::GlslShaderGenerator::TARGET + "/" +
1328+
#endif
1329+
"lib/mx_math.metal", mxContext, mxStage);
13031330
_EmitConstantsUniformsAndTypeDefs(
13041331
mxContext, mxStage,_syntax->getConstantQualifier());
13051332

@@ -1347,6 +1374,6 @@ HdStMaterialXShaderGenMsl::_EmitMxFunctions(
13471374
_EmitDataStructsAndFunctionDefinitions(
13481375
mxGraph, mxContext, mxStage, &_tokenSubstitutions);
13491376
}
1350-
1377+
#endif
13511378

13521379
PXR_NAMESPACE_CLOSE_SCOPE

pxr/imaging/hdSt/materialXShaderGen.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@
88
#define PXR_IMAGING_HD_ST_MATERIALX_SHADER_GEN_H
99

1010
#include "pxr/pxr.h"
11-
11+
#include <MaterialXCore/Library.h>
12+
#if defined(PXR_OPENGL_SUPPORT_ENABLED)
1213
#include <MaterialXGenGlsl/GlslShaderGenerator.h>
14+
#endif
15+
#if defined(PXR_METAL_SUPPORT_ENABLED)
1316
#include <MaterialXGenMsl/MslShaderGenerator.h>
17+
#endif
18+
#if defined(PXR_VULKAN_SUPPORT_ENABLED)
1419
#include <MaterialXGenGlsl/VkShaderGenerator.h>
20+
#endif
1521

1622
PXR_NAMESPACE_OPEN_SCOPE
1723

@@ -98,7 +104,7 @@ class HdStMaterialXShaderGen : public Base
98104
bool _emittingSurfaceNode;
99105
};
100106

101-
107+
#if defined(PXR_OPENGL_SUPPORT_ENABLED)
102108
/// \class HdStMaterialXShaderGenGlsl
103109
///
104110
/// Generates a glslfx shader with a surfaceShader function for a MaterialX
@@ -128,7 +134,9 @@ class HdStMaterialXShaderGenGlsl
128134
MaterialX::GenContext& mxContext,
129135
MaterialX::ShaderStage& mxStage) const;
130136
};
137+
#endif
131138

139+
#if defined(PXR_VULKAN_SUPPORT_ENABLED)
132140
/// \class HdStMaterialXShaderGenVkGlsl
133141
///
134142
/// Generates a glslfx shader with a surfaceShader function for a MaterialX
@@ -158,7 +166,9 @@ class HdStMaterialXShaderGenVkGlsl
158166
MaterialX::GenContext& mxContext,
159167
MaterialX::ShaderStage& mxStage) const;
160168
};
169+
#endif
161170

171+
#if defined(PXR_METAL_SUPPORT_ENABLED)
162172
/// \class HdStMaterialXShaderGenMsl
163173
///
164174
/// Generates a glslfx shader with a surfaceShader function for a MaterialX
@@ -191,7 +201,7 @@ class HdStMaterialXShaderGenMsl
191201
MaterialX::GenContext& mxContext,
192202
MaterialX::ShaderStage& mxStage) const;
193203
};
194-
204+
#endif
195205

196206
PXR_NAMESPACE_CLOSE_SCOPE
197207

0 commit comments

Comments
 (0)