# lisyarus/webgpu-raytracer architecture

> Architecture diagram of the GitHub repository lisyarus/webgpu-raytracer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lisyarus/webgpu-raytracer
- Repository: https://github.com/lisyarus/webgpu-raytracer

## Overview

##### Core Components (Main Layer)

a) Window Management & Platform Integration
- SDL2 Window System
- WebGPU Surface Creation (via SDL_WGPU bridge)
- Platform-specific adaptations (Windows/Linux/MacOS)

b) Rendering Pipeline
- Preview Pipeline (Fast preview rendering)
- Raytracing Pipeline
- First Hit Pipeline
- Monte Carlo Pipeline (Main raytracing implementation)
- Compose Pipeline (Final image composition)

c) Scene Management
- GLTF Asset Loading
- BVH (Bounding Volume Hierarchy) System
- Scene Data Management
- Camera System

##### Resource Management Layer

a) Bind Groups (WebGPU Resource Organizations)
- Accumulation Bind Group
- Camera Bind Group
- Geometry Bind Group
- Material Bind Group
- Compose Uniforms Bind Group

b) Shader Management
- Shader Registry
- WGSL Shader Collection
- BRDF
- BVH Traverse
- Camera
- Material
- Environment Map
- etc.

##### Support Systems
- Profiler
- Timer
- Scene Data Structures (AABB, etc.)

Key Relationships to Highlight:

##### Data Flow
**SDL Window → WebGPU Surface → Renderer → Pipelines → Final Output**

##### Resource Management
**Bind Groups ←→ Pipelines**
**Shader Registry → Pipelines**
**Scene Data → Geometry Bind Group**

##### Raytracing Pipeline Detail
**Scene BVH → Ray Intersection → Material Evaluation → Monte Carlo Integration**

Special Considerations:

1. Multiple importance sampling system in the Monte Carlo pipeline, showing connections to:
- BRDF sampling
- Light sampling
- VNDF sampling
- Transmission sampling

##### BVH construction and traversal system, highlighting
- Initial construction at program start
- Runtime traversal during raytracing
- Surface area heuristic optimization

##### Material System
- Cook-Torrance GGX implementation
- Texture management (albedo, normal, material maps)
- VNDF normal distribution

##### Environment Integration
- HDRI environment maps
- Background system
- Environment sampling

- Environment map importance sampling
- Refraction support
- Wavefront path-tracing
- GLB support

This structure captures both the current implementation and future expansion possibilities while maintaining clarity about the system's core architecture.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Style Definitions
    classDef core fill:#a8d1f0,stroke:#333
    classDef pipeline fill:#b8e0d2,stroke:#333,stroke-width:2px
    classDef bindGroup fill:#ffd1a1,stroke:#333
    classDef shader fill:#d7b7d8,stroke:#333
    classDef support fill:#ffb6b6,stroke:#333
    classDef external fill:#e6e6e6,stroke:#333
    classDef future fill:none,stroke:#666,stroke-dasharray: 5 5

    %% Main Application
    APP["Application"]:::core
    click APP "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/include/webgpu-raytracer/application.hpp"

    %% Window Management & Platform Integration
    subgraph WindowSystem["Window Management"]
        SDL["SDL2 Window System"]:::external
        WGPU["WebGPU Surface"]:::external
        click SDL "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/include/webgpu-raytracer/sdl_wgpu.h"
    end

    %% Rendering Pipeline
    subgraph RenderSystem["Rendering System"]
        REND["Renderer"]:::core
        click REND "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/include/webgpu-raytracer/renderer.hpp"

        subgraph Pipelines["Pipeline Components"]
            PREV["Preview Pipeline"]:::pipeline
            FH["First Hit Pipeline"]:::pipeline
            MC["Monte Carlo Pipeline"]:::pipeline
            COMP["Compose Pipeline"]:::pipeline

            click PREV "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/include/webgpu-raytracer/preview_pipeline.hpp"
            click FH "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/include/webgpu-raytracer/raytrace_first_hit_pipeline.hpp"
            click MC "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/include/webgpu-raytracer/raytrace_monte_carlo_pipeline.hpp"
            click COMP "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/include/webgpu-raytracer/compose_pipeline.hpp"
        end
    end

    %% Scene Management
    subgraph SceneSystem["Scene Management"]
        GLTF["GLTF Asset Loader"]:::core
        BVH["BVH System"]:::core
        SCENE["Scene Data"]:::core
        CAM["Camera System"]:::core

        click GLTF "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/include/webgpu-raytracer/gltf_loader.hpp"
        click BVH "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/include/webgpu-raytracer/bvh.hpp"
        click SCENE "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/include/webgpu-raytracer/scene_data.hpp"
        click CAM "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/include/webgpu-raytracer/camera.hpp"
    end

    %% Resource Management
    subgraph Resources["Resource Management"]
        subgraph BindGroups["Bind Groups"]
            ACC["Accumulation"]:::bindGroup
            CAMBG["Camera"]:::bindGroup
            GEO["Geometry"]:::bindGroup
            MAT["Material"]:::bindGroup
            COMPBG["Compose Uniforms"]:::bindGroup

            click ACC "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/include/webgpu-raytracer/accumulation_bind_group.hpp"
            click CAMBG "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/include/webgpu-raytracer/camera_bind_group.hpp"
            click GEO "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/include/webgpu-raytracer/geometry_bind_group.hpp"
            click MAT "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/include/webgpu-raytracer/material_bind_group.hpp"
            click COMPBG "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/include/webgpu-raytracer/compose_uniforms_bind_group.hpp"
        end

        subgraph Shaders["Shader System"]
            SREG["Shader Registry"]:::shader
            BRDF["BRDF Shader"]:::shader
            BVHS["BVH Traverse Shader"]:::shader
            CAMS["Camera Shader"]:::shader
            MATS["Material Shader"]:::shader
            ENV["Environment Map Shader"]:::shader

            click SREG "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/include/webgpu-raytracer/shader_registry.hpp"
            click BRDF "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/shaders/brdf.wgsl"
            click BVHS "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/shaders/bvh_traverse.wgsl"
            click CAMS "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/shaders/camera.wgsl"
            click MATS "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/shaders/material.wgsl"
            click ENV "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/shaders/env_map.wgsl"
        end
    end

    %% Support Systems
    subgraph Support["Support Systems"]
        PROF["Profiler"]:::support
        TIME["Timer"]:::support
        AABB["AABB"]:::support

        click PROF "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/include/webgpu-raytracer/profiler.hpp"
        click TIME "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/include/webgpu-raytracer/timer.hpp"
        click AABB "https://github.com/lisyarus/webgpu-raytracer/blob/trunk/include/webgpu-raytracer/aabb.hpp"
    end

    %% Future Extensions
    subgraph Future["Future Extensions"]
        direction TB
        EIS["Environment Map\nImportance Sampling"]:::future
        REF["Refraction Support"]:::future
        WPT["Wavefront Path-Tracing"]:::future
        GLB["GLB Support"]:::future
    end

    %% Relationships
    APP --> SDL
    SDL --> WGPU
    WGPU --> REND
    REND --> Pipelines
    SCENE --> GEO
    BVH --> MC
    CAM --> CAMBG
    BindGroups --> Pipelines
    Shaders --> Pipelines
    PROF -.-> REND
    TIME -.-> REND
    AABB --> BVH
```
