# shader-slang/slang-rhi architecture

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

- Interactive diagram: https://gitdiagram.com/shader-slang/slang-rhi
- Repository: https://github.com/shader-slang/slang-rhi

## Overview

##### Project Type and Purpose
- This is a cross-platform graphics and rendering hardware abstraction layer (RHI). The project provides a unified API that wraps multiple graphics backends (e.g., Direct3D, Vulkan, Metal, CUDA, WGPU) and possibly CPU simulation.
- Its primary goal is to abstract over various GPU APIs so that applications or higher-level engines can use a consistent interface for graphics operations.

##### Analyzing the File Structure
- Top-level files (e.g., CMakeLists.txt, cmake/, LICENSES/) and directories like “external” (with third-party libraries such as doctest and stb) confirm that the project is built with a robust, multi-platform CMake setup.
- The “include/slang-rhi” directory along with the “src” subdirectories indicate the public API and internal implementations, respectively.
- Within the “src” directory, there are many subdirectories dedicated to different graphics backends (cpu, cuda, d3d, d3d11, d3d12, metal, nvapi, vulkan, wgpu) as well as a “core” directory for common utilities and helper functions.
- The “debug-layer” subdirectory further suggests that the system can operate in both production and debugging/testing modes.

##### Key Architectural Elements to Capture
a. Main Components:
– API Interface: The public header(s) in “include/slang-rhi.h” and the “include/slang-rhi” folder.
– Core/Common Module: Code in “src/core” that implements shared utilities such as memory allocation, assertions, and task pools.
– Backend Drivers: Separate modules under “src” for each backend (CPU, CUDA, Direct3D family, Metal, Vulkan, WGPU) that implement the hardware-specific functionalities.
– Debugging & Testing: “src/debug-layer” and “tests” directories to provide facilities for error-checking and verifying functionality.
– External Libraries: The “external” folder shows usage of libraries like “doctest” (for testing) and “stb” (for image processing), as well as wrappers (e.g., metal-cpp) that support backend-specific APIs.
– Build and Configuration: CMake configuration (CMakeLists.txt, cmake scripts, GitHub workflows) which orchestrates the building and continuous integration of the project.

b. Relationships and Interactions:
– The API layer (public interface) feeds into the Core for common services. Depending on the runtime selection or configuration, Core then hands-off to one of the backend modules that implement specific graphics commands (command buffers, pipelines, shaders, textures, etc.).
– There is layered separation: the generic RHI interface on top, core utilities in the middle, and backend-specific implementations at the bottom.
– The debug-layer interposes or wraps around backend operations to provide additional logging, error-checking, or simulation capabilities.
– Tests and examples use the public API to verify that internal modules (across core and backends) work as expected.

c. Architectural Patterns and Design Principles:
– Modular and Plugin-like: Each graphics API is implemented in its own module, suggesting that switching backends or adding new ones is a matter of plugging in a new module.
– Abstraction Layering: A high-level RHI interface decouples the client from the specifics of the underlying graphics APIs.
– Separation of Concerns: There’s clear division between core functionality, backend implementations, debug utilities, and testing.
– Cross-Platform Build / Configuration: The use of CMake, GitHub workflows, and configuration files enforces consistency across platforms.

d. Relevant Technologies, Frameworks, or Libraries:
– CMake for building and configuration.
– Third-party libraries like doctest and stb.
– Platform-specific APIs including Direct3D (versions 11/12), CUDA, Metal, Vulkan, WGPU.
– Likely custom wrappers around native APIs (as seen in files ending with “-api.cpp” etc.).

##### Specific Elements
– Annotations for build/integration aspects (e.g., “CMake Build System”) might be indicated on the side.

##### Final Recommendations
- Be as detailed as possible—don’t hesitate to segment the project into each available backend.

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% Client Layer
    subgraph "Client Layer"
        A("Client Application"):::client
    end

    %% API Interface Layer
    subgraph "Public API Layer"
        APIH("API Header"):::api
        APIF("API Folder"):::api
    end

    %% Core/Utilities Layer
    subgraph "Core/Utilities"
        CORE("Core/Utilities"):::core
    end

    %% Debug Layer
    subgraph "Debug & Testing"
        DBG("Debug Layer"):::debug
        TST("Tests"):::debug
    end

    %% Backend Drivers Layer
    subgraph "Backend Drivers"
        BCPU("Backend: CPU"):::backend
        BCUDA("Backend: CUDA"):::backend
        BD3D("Backend: D3D"):::backend
        BD3D11("Backend: D3D11"):::backend
        BD3D12("Backend: D3D12"):::backend
        BMETAL("Backend: Metal"):::backend
        BVULKAN("Backend: Vulkan"):::backend
        BWGPU("Backend: WGPU"):::backend
    end

    %% External & Build Layer
    subgraph "External & Build"
        EXT("External Libraries"):::external
        BLD("Build & Config"):::build
    end

    %% Connections from Client to API Interface
    A -->|"calls"| APIH
    A -->|"calls"| APIF

    %% API to Core
    APIH -->|"init"| CORE
    APIF -->|"init"| CORE

    %% Core to Debug (for enhanced monitoring)
    CORE -->|"wraps"| DBG
    DBG -->|"forwards"| CORE

    %% Core to Backend Drivers paths (direct and via Debug)
    CORE -->|"dispatches"| BCPU
    CORE -->|"dispatches"| BCUDA
    CORE -->|"dispatches"| BD3D
    CORE -->|"dispatches"| BD3D11
    CORE -->|"dispatches"| BD3D12
    CORE -->|"dispatches"| BMETAL
    CORE -->|"dispatches"| BVULKAN
    CORE -->|"dispatches"| BWGPU

    DBG -->|"intercepts"| BCPU
    DBG -->|"intercepts"| BCUDA
    DBG -->|"intercepts"| BD3D
    DBG -->|"intercepts"| BD3D11
    DBG -->|"intercepts"| BD3D12
    DBG -->|"intercepts"| BMETAL
    DBG -->|"intercepts"| BVULKAN
    DBG -->|"intercepts"| BWGPU

    %% Testing layer to Core for validation feedback
    TST ---|"verifies"| CORE

    %% External dependencies influencing Core & Backends
    EXT ---|"provides libs to"| CORE
    EXT ---|"provides libs to"| BCPU
    EXT ---|"provides libs to"| BCUDA
    EXT ---|"provides libs to"| BD3D
    EXT ---|"provides libs to"| BD3D11
    EXT ---|"provides libs to"| BD3D12
    EXT ---|"provides libs to"| BMETAL
    EXT ---|"provides libs to"| BVULKAN
    EXT ---|"provides libs to"| BWGPU

    %% Build & Config effects (orchestrating build)
    BLD ---|"builds"| APIH
    BLD ---|"builds"| APIF
    BLD ---|"builds"| CORE
    BLD ---|"builds"| BCPU
    BLD ---|"builds"| BCUDA
    BLD ---|"builds"| BD3D
    BLD ---|"builds"| BD3D11
    BLD ---|"builds"| BD3D12
    BLD ---|"builds"| BMETAL
    BLD ---|"builds"| BVULKAN
    BLD ---|"builds"| BWGPU
    BLD ---|"builds"| DBG
    BLD ---|"builds"| TST

    %% Click Events
    click APIH "https://github.com/shader-slang/slang-rhi/blob/main/include/slang-rhi.h"
    click APIF "https://github.com/shader-slang/slang-rhi/tree/main/include/slang-rhi/"
    click CORE "https://github.com/shader-slang/slang-rhi/tree/main/src/core/"
    click BCPU "https://github.com/shader-slang/slang-rhi/tree/main/src/cpu/"
    click BCUDA "https://github.com/shader-slang/slang-rhi/tree/main/src/cuda/"
    click BD3D "https://github.com/shader-slang/slang-rhi/tree/main/src/d3d/"
    click BD3D11 "https://github.com/shader-slang/slang-rhi/tree/main/src/d3d11/"
    click BD3D12 "https://github.com/shader-slang/slang-rhi/tree/main/src/d3d12/"
    click BMETAL "https://github.com/shader-slang/slang-rhi/tree/main/src/metal/"
    click BVULKAN "https://github.com/shader-slang/slang-rhi/tree/main/src/vulkan/"
    click BWGPU "https://github.com/shader-slang/slang-rhi/tree/main/src/wgpu/"
    click DBG "https://github.com/shader-slang/slang-rhi/tree/main/src/debug-layer/"
    click TST "https://github.com/shader-slang/slang-rhi/tree/main/tests/"
    click EXT "https://github.com/shader-slang/slang-rhi/tree/main/external/"
    click BLD "https://github.com/shader-slang/slang-rhi/blob/main/CMakeLists.txt"

    %% Styles
    classDef client fill:#b3cde0,stroke:#03396c,stroke-width:2px;
    classDef api fill:#ccebc5,stroke:#2b9348,stroke-width:2px;
    classDef core fill:#ffdd94,stroke:#ff9f1c,stroke-width:2px;
    classDef backend fill:#fbb4ae,stroke:#d7191c,stroke-width:2px;
    classDef debug fill:#f6e8c3,stroke:#b58900,stroke-width:2px;
    classDef external fill:#d0e1f9,stroke:#034f84,stroke-width:2px;
    classDef build fill:#e1bee7,stroke:#6a1b9a,stroke-width:2px;
```
