Skip to content

Commit 164768f

Browse files
authored
Extended tracing instrumentation for shader codegen and rendering (#2820)
Building on the Perfetto tracing from #2742, this PR adds CPU trace markers to additional codegen and rendering hot paths. This also required conditionally linking `MaterialXTrace` to `MaterialXGenShader`, which was not needed by #2742 (tracing was only in test code) but is now required for the new codegen instrumentation.
1 parent dc337ca commit 164768f

10 files changed

Lines changed: 62 additions & 3 deletions

File tree

resources/Materials/TestSuite/_options.mtlx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
-->
8080
<input name="outputDirectory" type="string" value="" />
8181

82-
<!-- Enable Perfetto tracing during render tests (requires MATERIALX_BUILD_TRACING).
82+
<!-- Enable Perfetto tracing during render tests (requires MATERIALX_BUILD_PERFETTO_TRACING).
8383
When enabled, generates .perfetto-trace files in outputDirectory.
8484
Default is false to avoid overhead when not profiling.
8585
-->

source/MaterialXGenGlsl/GlslShaderGenerator.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include <MaterialXGenShader/Shader.h>
3232
#include <MaterialXGenShader/Nodes/MaterialNode.h>
3333

34+
#include <MaterialXTrace/Tracing.h>
35+
3436
MATERIALX_NAMESPACE_BEGIN
3537

3638
const string GlslShaderGenerator::TARGET = "genglsl";
@@ -128,6 +130,9 @@ GlslShaderGenerator::GlslShaderGenerator(TypeSystemPtr typeSystem) :
128130

129131
ShaderPtr GlslShaderGenerator::generate(const string& name, ElementPtr element, GenContext& context) const
130132
{
133+
MX_TRACE_FUNCTION(Tracing::Category::ShaderGen);
134+
MX_TRACE_SCOPE(Tracing::Category::ShaderGen, name.c_str());
135+
131136
ShaderPtr shader = createShader(name, element, context);
132137

133138
// Request fixed floating-point notation for consistency across targets.

source/MaterialXGenHw/HwShaderGenerator.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include <MaterialXCore/Definition.h>
1717
#include <MaterialXCore/Document.h>
1818

19+
#include <MaterialXTrace/Tracing.h>
20+
1921
MATERIALX_NAMESPACE_BEGIN
2022

2123
//
@@ -97,6 +99,9 @@ void HwShaderGenerator::applyDefaultOptions(GenOptions& options) const
9799

98100
ShaderPtr HwShaderGenerator::createShader(const string& name, ElementPtr element, GenContext& context) const
99101
{
102+
MX_TRACE_FUNCTION(Tracing::Category::ShaderGen);
103+
MX_TRACE_SCOPE(Tracing::Category::ShaderGen, name.c_str());
104+
100105
// Create the root shader graph
101106
ShaderGraphPtr graph = ShaderGraph::create(nullptr, name, element, context);
102107
ShaderPtr shader = std::make_shared<Shader>(name, graph);

source/MaterialXGenShader/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
file(GLOB_RECURSE materialx_source "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
22
file(GLOB_RECURSE materialx_headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h*")
33

4+
set(GENSHADER_MTLX_MODULES MaterialXFormat MaterialXCore)
5+
if(MATERIALX_BUILD_PERFETTO_TRACING)
6+
list(APPEND GENSHADER_MTLX_MODULES MaterialXTrace)
7+
endif()
8+
49
mx_add_library(MaterialXGenShader
510
SOURCE_FILES
611
${materialx_source}
712
HEADER_FILES
813
${materialx_headers}
914
MTLX_MODULES
10-
MaterialXFormat
11-
MaterialXCore
15+
${GENSHADER_MTLX_MODULES}
1216
EXPORT_DEFINE
1317
MATERIALX_GENSHADER_EXPORTS)
1418

source/MaterialXGenShader/Nodes/CompoundNode.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <MaterialXCore/Document.h>
1313
#include <MaterialXCore/Library.h>
1414

15+
#include <MaterialXTrace/Tracing.h>
16+
1517
MATERIALX_NAMESPACE_BEGIN
1618

1719
ShaderNodeImplPtr CompoundNode::create()
@@ -27,6 +29,9 @@ void CompoundNode::addClassification(ShaderNode& node) const
2729

2830
void CompoundNode::initialize(const InterfaceElement& element, GenContext& context)
2931
{
32+
MX_TRACE_FUNCTION(Tracing::Category::ShaderGen);
33+
MX_TRACE_SCOPE(Tracing::Category::ShaderGen, element.getName().c_str());
34+
3035
ShaderNodeImpl::initialize(element, context);
3136

3237
if (!element.isA<NodeGraph>())
@@ -62,6 +67,9 @@ void CompoundNode::createVariables(const ShaderNode&, GenContext& context, Shade
6267

6368
void CompoundNode::emitFunctionDefinition(const ShaderNode& node, GenContext& context, ShaderStage& stage) const
6469
{
70+
MX_TRACE_FUNCTION(Tracing::Category::ShaderGen);
71+
MX_TRACE_SCOPE(Tracing::Category::ShaderGen, _functionName.c_str());
72+
6573
DEFINE_SHADER_STAGE(stage, Stage::PIXEL)
6674
{
6775
const ShaderGenerator& shadergen = context.getShaderGenerator();
@@ -148,6 +156,9 @@ void CompoundNode::emitFunctionDefinition(const ShaderNode& node, GenContext& co
148156

149157
void CompoundNode::emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const
150158
{
159+
MX_TRACE_FUNCTION(Tracing::Category::ShaderGen);
160+
MX_TRACE_SCOPE(Tracing::Category::ShaderGen, _functionName.c_str());
161+
151162
const ShaderGenerator& shadergen = context.getShaderGenerator();
152163

153164
DEFINE_SHADER_STAGE(stage, Stage::VERTEX)

source/MaterialXGenShader/ShaderGenerator.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <MaterialXCore/Node.h>
1818
#include <MaterialXCore/Value.h>
1919

20+
#include <MaterialXTrace/Tracing.h>
21+
2022
#include <sstream>
2123

2224
MATERIALX_NAMESPACE_BEGIN
@@ -116,6 +118,9 @@ void ShaderGenerator::emitFunctionDefinitionParameter(const ShaderPort* shaderPo
116118

117119
void ShaderGenerator::emitFunctionDefinitions(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const
118120
{
121+
MX_TRACE_FUNCTION(Tracing::Category::ShaderGen);
122+
MX_TRACE_SCOPE(Tracing::Category::ShaderGen, graph.getName().c_str());
123+
119124
// Emit function definitions for all nodes in the graph.
120125
for (ShaderNode* node : graph.getNodes())
121126
{
@@ -140,6 +145,9 @@ void ShaderGenerator::emitFunctionCall(const ShaderNode& node, GenContext& conte
140145

141146
void ShaderGenerator::emitFunctionCalls(const ShaderGraph& graph, GenContext& context, ShaderStage& stage, uint32_t classification) const
142147
{
148+
MX_TRACE_FUNCTION(Tracing::Category::ShaderGen);
149+
MX_TRACE_SCOPE(Tracing::Category::ShaderGen, graph.getName().c_str());
150+
143151
for (ShaderNode* node : graph.getNodes())
144152
{
145153
if (!classification || node->hasClassification(classification))
@@ -307,6 +315,9 @@ ShaderNodeImplPtr ShaderGenerator::createShaderNodeImplForImplementation(const I
307315

308316
ShaderNodeImplPtr ShaderGenerator::getImplementation(const NodeDef& nodedef, GenContext& context) const
309317
{
318+
MX_TRACE_FUNCTION(Tracing::Category::ShaderGen);
319+
MX_TRACE_SCOPE(Tracing::Category::ShaderGen, nodedef.getName().c_str());
320+
310321
InterfaceElementPtr implElement = nodedef.getImplementation(getTarget());
311322
if (!implElement)
312323
{

source/MaterialXGenShader/ShaderGraph.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <MaterialXGenShader/ShaderGraphRefactor.h>
1111
#include <MaterialXGenShader/Util.h>
1212

13+
#include <MaterialXTrace/Tracing.h>
14+
1315
#include <iostream>
1416
#include <queue>
1517

@@ -79,6 +81,8 @@ void ShaderGraph::createConnectedNodes(const ElementPtr& downstreamElement,
7981
ElementPtr connectingElement,
8082
GenContext& context)
8183
{
84+
MX_TRACE_FUNCTION(Tracing::Category::ShaderGen);
85+
8286
// Create the node if it doesn't exist.
8387
NodePtr upstreamNode = upstreamElement->asA<Node>();
8488
if (!upstreamNode)
@@ -170,6 +174,9 @@ void ShaderGraph::createConnectedNodes(const ElementPtr& downstreamElement,
170174

171175
void ShaderGraph::addUpstreamDependencies(const Element& root, GenContext& context)
172176
{
177+
MX_TRACE_FUNCTION(Tracing::Category::ShaderGen);
178+
MX_TRACE_SCOPE(Tracing::Category::ShaderGen, root.getName().c_str());
179+
173180
std::set<ElementPtr> processedOutputs;
174181

175182
for (Edge edge : root.traverseGraph())
@@ -698,6 +705,9 @@ void ShaderGraph::applyInputTransforms(ConstNodePtr node, ShaderNode* shaderNode
698705

699706
ShaderNode* ShaderGraph::createNode(const string& name, const string& uniqueId, ConstNodeDefPtr nodeDef, GenContext& context)
700707
{
708+
MX_TRACE_FUNCTION(Tracing::Category::ShaderGen);
709+
MX_TRACE_SCOPE(Tracing::Category::ShaderGen, name.c_str());
710+
701711
if (!nodeDef)
702712
{
703713
throw ExceptionShaderGenError("Could not find a nodedef for node '" + name + "'");

source/MaterialXGenShader/ShaderNode.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <MaterialXGenShader/GenContext.h>
1010
#include <MaterialXGenShader/Util.h>
1111

12+
#include <MaterialXTrace/Tracing.h>
13+
1214
MATERIALX_NAMESPACE_BEGIN
1315

1416
const string ShaderMetadataRegistry::USER_DATA_NAME = "ShaderMetadataRegistry";
@@ -172,6 +174,9 @@ ShaderNode::ShaderNode(const ShaderGraph* parent, const string& name) :
172174

173175
ShaderNodePtr ShaderNode::create(const ShaderGraph* parent, const string& name, const NodeDef& nodeDef, GenContext& context)
174176
{
177+
MX_TRACE_FUNCTION(Tracing::Category::ShaderGen);
178+
MX_TRACE_SCOPE(Tracing::Category::ShaderGen, name.c_str());
179+
175180
ShaderNodePtr newNode = std::make_shared<ShaderNode>(parent, name);
176181

177182
const ShaderGenerator& shadergen = context.getShaderGenerator();

source/MaterialXRenderGlsl/GlslProgram.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
#include <MaterialXGenHw/HwConstants.h>
1515

16+
#include <MaterialXTrace/Tracing.h>
17+
1618
#include <iostream>
1719

1820
MATERIALX_NAMESPACE_BEGIN
@@ -82,6 +84,8 @@ const string& GlslProgram::getStageSourceCode(const string& stage) const
8284

8385
void GlslProgram::build()
8486
{
87+
MX_TRACE_FUNCTION(Tracing::Category::Render);
88+
8589
clearBuiltData();
8690

8791
GLint glStatus = GL_FALSE;

source/MaterialXTest/MaterialXRenderGlsl/RenderGlsl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ bool GlslShaderRenderTester::runRenderer(const std::string& shaderName,
232232

233233
if (testOptions.dumpGeneratedCode)
234234
{
235+
MX_TRACE_SCOPE(mx::Tracing::Category::Render, "DumpGeneratedCode");
235236
mx::ScopedTimer dumpTimer(&profileTimes.languageTimes.ioTime);
236237
std::ofstream file;
237238
file.open(shaderPath + "_vs.glsl");
@@ -291,6 +292,7 @@ bool GlslShaderRenderTester::runRenderer(const std::string& shaderName,
291292

292293
if (testOptions.dumpUniformsAndAttributes)
293294
{
295+
MX_TRACE_SCOPE(mx::Tracing::Category::Render, "DumpUniformsAndAttributes");
294296
mx::ScopedTimer printTimer(&profileTimes.languageTimes.ioTime);
295297
log << "* Uniform:" << std::endl;
296298
program->printUniforms(log);
@@ -356,10 +358,12 @@ bool GlslShaderRenderTester::runRenderer(const std::string& shaderName,
356358
unsigned int width = (unsigned int) testOptions.renderSize[0] * supersampleFactor;
357359
unsigned int height = (unsigned int) testOptions.renderSize[1] * supersampleFactor;
358360
_renderer->setSize(width, height);
361+
359362
_renderer->render();
360363
}
361364

362365
{
366+
MX_TRACE_SCOPE(mx::Tracing::Category::Render, "CaptureAndSaveImage");
363367
mx::ScopedTimer ioTimer(&profileTimes.languageTimes.imageSaveTime);
364368
std::string fileName = shaderPath + "_glsl.png";
365369
mx::ImagePtr image = _renderer->captureImage();

0 commit comments

Comments
 (0)