Skip to content

Extended tracing instrumentation for shader codegen and rendering#2820

Open
ppenenko wants to merge 21 commits intoAcademySoftwareFoundation:mainfrom
autodesk-forks:ppenenko/cpp_instrumentation
Open

Extended tracing instrumentation for shader codegen and rendering#2820
ppenenko wants to merge 21 commits intoAcademySoftwareFoundation:mainfrom
autodesk-forks:ppenenko/cpp_instrumentation

Conversation

@ppenenko
Copy link
Contributor

@ppenenko ppenenko commented Mar 12, 2026

Summary

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.

  • Shader codegen: ShaderGraph traversal (createConnectedNodes, addUpstreamDependencies, createNode), ShaderGenerator emit functions (emitFunctionDefinitions, emitFunctionCalls, getImplementation), and CompoundNode operations

    image
  • GLSL build: GlslProgram::build(), DumpGeneratedCode, DumpUniformsAndAttributes, CaptureAndSaveImage

Motivation

This instrumentation was developed to measure and validate shader generation optimizations (early lobe pruning, dead code elimination) in a companion branch. The infrastructure is generally useful for identifying bottlenecks in shader generation and compilation.

Test plan

  • Verified C++ build with MATERIALX_BUILD_PERFETTO_TRACING=ON on Windows (MSVC)
  • Verified C++ build with MATERIALX_BUILD_PERFETTO_TRACING=OFF (no-op macros, no link dependencies)
  • Verified Perfetto traces contain the new CPU slices

Related


Assisted-by: Claude (Anthropic) via Cursor IDE
Signed-off-by: Pavlo Penenko <pavlo.penenko@autodesk.com>

Add configurable environment radiance sample count to test suite options,
allowing game-representative performance testing (1-16 samples) vs reference
quality (1024+ samples). Lower sample counts make shader complexity a larger
fraction of GPU time, enabling meaningful runtime performance comparisons.
Python package (MaterialXTest.diff_test_runs) with three comparison
scripts and shared reporting utilities:

  diff_images.py  -- perceptual image comparison via NVIDIA FLIP,
                     with HTML side-by-side reports
  diff_traces.py  -- Perfetto trace comparison with per-material
                     CPU slice and GPU render time analysis,
                     multiple --slice filters, --warmup-frames
                     burn-in, inline SVG charts
  diff_shaders.py -- offline shader analysis (LOC, SPIR-V size,
                     compile time, spirv-opt time); auto-discovers
                     Vulkan SDK tools in PATH
  _report.py      -- shared comparison tables, SVG chart generation,
                     and HTML report builder
Declare the new test suite options so the C++ test runner can read
them from the options file.  Also reset enableTracing default to
false (opt-in for profiling runs).
Added MX_TRACE markers to key codegen functions for profiling:
- ShaderGraph: createConnectedNodes, addUpstreamDependencies, createNode
- ShaderGenerator: getImplementation, emitFunctionDefinitions, emitFunctionCalls
- CompoundNode: initialize, emitFunctionDefinition, emitFunctionCall
- Add GpuTimerQuery helper class and getCurrentTimeNs() for GPU timing
  via GL_TIME_ELAPSED queries (guarded by MATERIALX_BUILD_TRACING)
- Add multi-frame render loop using framesPerMaterial for statistical
  validity, emitting MX_TRACE_ASYNC events on the GPU track
- Replace stale Cat:: alias with mx::Tracing::Category:: (the alias
  was removed in PR AcademySoftwareFoundation#2742)
Shader codegen source files (ShaderGraph.cpp, ShaderGenerator.cpp, etc.)
include MaterialXTrace/Tracing.h and use MX_TRACE macros. When Perfetto
tracing is enabled, MaterialXGenShader needs to link against MaterialXTrace
for the trace event symbols.
The CMake flag was renamed from MATERIALX_BUILD_TRACING to
MATERIALX_BUILD_PERFETTO_TRACING in PR AcademySoftwareFoundation#2742. Update all #ifdef
guards in RenderGlsl.cpp to match.
Port AsyncTrack enum, Sink::asyncEvent(), and MX_TRACE_ASYNC macro
from the pre-merge proto2 branch. This enables GPU timer query results
to be emitted as events on a dedicated "GPU" track in Perfetto traces.

Use std::numeric_limits<uint64_t>::max() for the GPU track ID to
guarantee no collision with OS thread IDs.
The previous commit accidentally set enableTracing to false, which
would prevent CI from producing Perfetto traces. Restore to true
to match the behavior established in PR AcademySoftwareFoundation#2742.
Without matplotlib, the HTML reports are generated with no charts,
making them essentially empty. Fail early with a clear install
message, consistent with how perfetto and pandas are handled.
Derive display labels from the actual directory names passed on the
command line, matching the convention already used by diff_traces.py.
Affects console output, HTML report title, and per-image labels.

Assisted-by: Claude (Anthropic) via Cursor IDE
Signed-off-by: Pavlo Penenko <pavlo.penenko@autodesk.com>
Use the shared openReport() from _report.py, matching the behavior
already present in diff_traces.py and diff_shaders.py.

Assisted-by: Claude (Anthropic) via Cursor IDE
Signed-off-by: Pavlo Penenko <pavlo.penenko@autodesk.com>
Use the same relative/fallback import pattern as diff_traces.py and
diff_shaders.py so the script works both as a package and standalone.

Assisted-by: Claude (Anthropic) via Cursor IDE
Signed-off-by: Pavlo Penenko <pavlo.penenko@autodesk.com>
Remove the diff_test_runs package to keep this PR focused on
C++ tracing instrumentation only. The Python tooling will be
proposed in a separate follow-up PR.
Remove framesPerMaterial and envSampleCount options to keep this PR
focused on C++ tracing instrumentation. The options will be proposed
in a separate follow-up PR.

Also reset UiNode.h/cpp to main (unrelated Graph Editor fix that
crept into the branch).
Move MX_TRACE_ASYNC macro, AsyncTrack enum, GpuTimerQuery, and
GPU timer query usage to a follow-up PR, keeping this branch
focused on CPU trace markers only.
<input name="outputDirectory" type="string" value="" />

<!-- Enable Perfetto tracing during render tests (requires MATERIALX_BUILD_TRACING).
<!-- Enable Perfetto tracing during render tests (requires MATERIALX_BUILD_PERFETTO_TRACING).
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just fixing an oversight from #2774: we renamed this setting as part of the code review.

Comment on lines +4 to +7
set(GENSHADER_MTLX_MODULES MaterialXFormat MaterialXCore)
if(MATERIALX_BUILD_PERFETTO_TRACING)
list(APPEND GENSHADER_MTLX_MODULES MaterialXTrace)
endif()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The optional dependency on tracing - zero overhead if disabled. If enabled, this wouldn't create any additional linking dependencies for MaterialX's consumers because Perfetto is compiled into MaterialXTrace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant