# lunarg/gfxreconstruct architecture

> Architecture diagram of the GitHub repository lunarg/gfxreconstruct, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lunarg/gfxreconstruct
- Repository: https://github.com/lunarg/gfxreconstruct

## Overview

##### Project type and purpose
- GFXReconstruct is an open-source, cross-platform tool suite for capturing and replaying graphics API calls (Vulkan, D3D12, experimental OpenXR).
- It provides a modular framework (“framework/…”) for encoding (capture), formatting (serialize), decoding (replay), and a set of CLI tools (“tools/…”) that drive those modules.

##### File structure
- Top-level:
– CMake build scripts and project configs (CMakeLists.txt, BUILD.md, scripts/).
– external/… — vendored third-party libs (AgilitySDK, SPIRV-Reflect, LZ4, Zstandard, nlohmann-json, Catch2).
– framework/… — core modules subdivided into application (window/OS abstraction), encode (capture), format (file schema), decode (replay), generated code (API bindings).
– layer/… — API injection layers (Vulkan, D3D12, DXGI, AGS) that hook into target applications.
– tools/… — standalone command-line front ends (capture, replay, info, compress, extract, convert, optimize, tocpp, etc.).
– android/… — Android build, framework integration, test apps, and Android variants of tools.
– test/… — unit tests, mock ICDs, known-good traces, test applications for validation.

##### README for additional insights
- Describes each CLI tool’s purpose (gfxrecon-replay, info, compress, extract, convert, optimize).
- Notes the framework design for easy extension of capture/replay tools.
- Specifies supported platforms and APIs.

a. Main components
- "Application Under Test" – any Vulkan/D3D12/OpenXR app.
- API Hooking Layers – Vulkan layer, D3D12 DLL interceptors, DXGI hooks, AGS layer.
- Encode Module – framework/encode: wraps API calls and writes command stream.
- Format Module – framework/format: serializes calls into .gfxr file (binary + JSON metadata).
- Storage – capture file on disk.
- CLI Tools – each tool invokes framework modules:
– capture (wrap + write)
– replay (read + decode + replay)
– info (read + format to console)
– compress/decompress (apply LZ4, Zstd, zlib)
– extract (SPIR-V export)
– convert (JSON Lines output)
– optimize (file rewriting for speed)
- Decode/Replay Module – framework/decode: reads .gfxr, reconstructs API calls, feeds replay consumer.
- Application Framework – framework/application + framework/graphics + framework/util: windowing, context creation, resource management.
- Rendering Backend – actual Vulkan / D3D12 / OpenXR driver invocation at replay time.
- Codegen & Scripts – python and CMake scripts under framework/generated and scripts/ that produce API binding code.
- External Libraries – highlighted as dependencies behind capture/replay and tools.

b. Relationships & data flows
1. App → API Layer hooks → Encode → Format → .gfxr file
2. .gfxr file → (optional) compress/convert/extract/optimize/info tools → new .gfxr or report
3. .gfxr file → Decode → Replay consumer → Application framework → Graphics driver → Display
4. Codegen scripts feed into the build process to generate encode/decode classes.
5. Android variants follow the same flow, but via android/layer & android/tools.

c. Architectural patterns & design principles
- Modular “framework” – clear separation of capture (encode), serialization (format), and replay (decode).
- Plugin/Layer architecture – API interception layers for different graphics APIs.
- Code generation – avoids manual API binding by generating encoders/decoders per API.
- CLI front-ends – minimal glue invoking the shared framework modules.
- Cross-platform abstraction – framework/application hides OS/windowing differences.

d. Key technologies
- CMake + Python scripts (build & codegen)
- C/C++ core (encode/decode/format/application)
- API injection: Vulkan’s layer mechanism, DLL hook (Detours) for D3D12/DXGI
- Serialization: custom binary format + JSON metadata, LZ4/Zstd/zlib compressors
- External libs: AgilitySDK (D3D12), SPIRV-Reflect, nlohmann-json, Catch2

##### Elements
- Clear labels for each component block.
– Blue: capture chain (layers → encode → format → file)
– Green: replay chain (file → decode → replay → driver)
– Orange: CLI tools orbiting the file
– Gray: external libraries/dependencies
– Yellow: Android-specific modules

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Application & Layers
    subgraph "Application & Layers"
        App["Application Under Test"]:::blue
        Layers["API Hooking Layers"]:::blue
    end

    %% Framework Core
    subgraph "Framework Core"
        Encode["Encode Module"]:::blue
        Format["Format Module"]:::blue
        Decode["Decode Module"]:::green
        ReplayConsumer["Replay Consumer"]:::green
        AppFramework["Application Framework"]:::green
        GraphicsBackend["Rendering Backend"]:::green
        Utility["Utility Library"]:::gray
    end

    %% Storage
    Storage[".gfxr File"]:::gray

    %% CLI Tools
    subgraph "CLI Tools"
        CapTool["Capture CLI"]:::orange
        RepTool["Replay CLI"]:::orange
        InfoTool["Info CLI"]:::orange
        CompTool["Compress CLI"]:::orange
        ExtrTool["Extract CLI"]:::orange
        ConvTool["Convert CLI"]:::orange
        OptiTool["Optimize CLI"]:::orange
    end

    %% Codegen & Build
    subgraph "Codegen & Build"
        CodeGen["Code Generation Scripts"]:::gray
        BuildCFG["Build & CI Configurations"]:::gray
    end

    %% Android Modules
    subgraph "Android Modules"
        AndFramework["Android Framework Integration"]:::yellow
        AndLayers["Android API Layers"]:::yellow
        AndTools["Android Tools"]:::yellow
    end

    %% External Dependencies
    External["External Third-Party Libraries"]:::gray

    %% Main Capture Flow
    App -->|"API calls"| Layers
    Layers -->|"hooks"| Encode
    Encode -->|"API calls"| Format
    Format -->|"writes"| Storage

    %% Main Replay Flow
    Storage -->|"reads"| Decode
    Decode -->|"calls"| ReplayConsumer
    ReplayConsumer -->|"uses"| AppFramework
    AppFramework -->|"draws"| GraphicsBackend

    %% Optional Tools
    Storage -.->|optional| CompTool
    Storage -.->|optional| ExtrTool
    Storage -.->|optional| ConvTool
    Storage -.->|optional| OptiTool
    Storage -.->|reads| InfoTool

    %% CLI Invocation
    CapTool -->|"invokes"| Encode
    RepTool -->|"invokes"| Decode

    %% Codegen Relations
    CodeGen -.-|"generates"| Encode
    CodeGen -.-|"generates"| Decode
    BuildCFG -->|"builds"| Encode
    BuildCFG -->|"builds"| Decode
    BuildCFG -->|"builds"| Format
    BuildCFG -->|"builds"| AppFramework

    %% Android Flow
    App --> AndLayers
    AndLayers -->|"hooks"| Encode
    AndTools -->|"invokes"| Decode

    %% External Dependencies usage
    Encode -.-> External
    Format -.-> External
    Decode -.-> External
    Utility -.-> External
    GraphicsBackend -.-> External

    %% Click Events
    click Layers "https://github.com/lunarg/gfxreconstruct/tree/dev/layer/"
    click Encode "https://github.com/lunarg/gfxreconstruct/tree/dev/framework/encode/"
    click Format "https://github.com/lunarg/gfxreconstruct/tree/dev/framework/format/"
    click Decode "https://github.com/lunarg/gfxreconstruct/tree/dev/framework/decode/"
    click AppFramework "https://github.com/lunarg/gfxreconstruct/tree/dev/framework/application/"
    click GraphicsBackend "https://github.com/lunarg/gfxreconstruct/tree/dev/framework/graphics/"
    click Utility "https://github.com/lunarg/gfxreconstruct/tree/dev/framework/util/"
    click CapTool "https://github.com/lunarg/gfxreconstruct/tree/dev/tools/capture/"
    click RepTool "https://github.com/lunarg/gfxreconstruct/tree/dev/tools/replay/"
    click InfoTool "https://github.com/lunarg/gfxreconstruct/tree/dev/tools/info/"
    click CompTool "https://github.com/lunarg/gfxreconstruct/tree/dev/tools/compress/"
    click ExtrTool "https://github.com/lunarg/gfxreconstruct/tree/dev/tools/extract/"
    click ConvTool "https://github.com/lunarg/gfxreconstruct/tree/dev/tools/convert/"
    click OptiTool "https://github.com/lunarg/gfxreconstruct/tree/dev/tools/optimize/"
    click CodeGen "https://github.com/lunarg/gfxreconstruct/tree/dev/framework/generated/"
    click CodeGen "https://github.com/lunarg/gfxreconstruct/tree/dev/scripts/"
    click AndFramework "https://github.com/lunarg/gfxreconstruct/tree/dev/android/framework/"
    click AndLayers "https://github.com/lunarg/gfxreconstruct/tree/dev/android/layer/"
    click AndTools "https://github.com/lunarg/gfxreconstruct/tree/dev/android/tools/"
    click External "https://github.com/lunarg/gfxreconstruct/tree/dev/external/"
    click BuildCFG "https://github.com/lunarg/gfxreconstruct/blob/dev/CMakeLists.txt"
    click BuildCFG "https://github.com/lunarg/gfxreconstruct/blob/dev/BUILD.md"
    click BuildCFG "https://github.com/lunarg/gfxreconstruct/tree/dev/cmake/"
    click BuildCFG "https://github.com/lunarg/gfxreconstruct/tree/dev/.github/workflows/"

    %% Styles
    classDef blue fill:#ADD8E6,stroke:#000000
    classDef green fill:#98FB98,stroke:#000000
    classDef orange fill:#FFA500,stroke:#000000
    classDef gray fill:#D3D3D3,stroke:#000000
    classDef yellow fill:#FFFF99,stroke:#000000
```
