Texture Transcoding Pipeline

Architecture diagram of the texture loading and transcoding system

graph TD
    %% Main Thread / ResourceManager Logic
    subgraph Main_Thread [Main Thread: ResourceManager]
        QueueCall(queue_texture_load) --> PendingQ[Pending Queue]
        PendingQ --> Dispatch[try_dispatch_texture_loads]
        Dispatch -- "Check Limit (MAX_IN_FLIGHT)" --> LimitCheck{Below Limit?}
        LimitCheck -- No --> PendingQ
        LimitCheck -- Yes --> PlatformCheck{Target Arch?}

        %% Polling and Uploading
        UpdateCall(update) --> PollReceiver{Poll Receiver}
        PollReceiver -- "Got Data" --> Upload[upload_decoded_texture]
        Upload -- "Rgba or Compressed" --> GPUCache[GPU Texture Cache]
        PollReceiver -- "Empty" --> Idle
    end

    %% Native Execution Path
    subgraph Native_Execution [Native Implementation]
        PlatformCheck -- "Native (cfg not wasm32)" --> SpawnThread[std::thread::spawn]
        SpawnThread --> N_Load[io::load_binary_file]
        N_Load --> N_Decode[DecodedTexture::from_bytes]
        N_Decode -- "Software Transcode (CPU)" --> N_Send[Send Result]
    end

    %% Web/WASM Execution Path
    subgraph Web_Execution [WASM Implementation]
        PlatformCheck -- "WASM (cfg wasm32)" --> SpawnLocal[wasm_bindgen_futures::spawn_local]
        SpawnLocal --> W_Load[io::load_binary_file]
        W_Load --> FileType{Requires Worker?}

        %% KTX2 Path (Web Worker)
        FileType -- Yes --> JS_Bind[transcodeEmbeddedTexture]

        subgraph Web_Worker [Web Worker / JS]
            JS_Bind -.-> WorkerProcess["Worker Transcode (Off-Main Thread)"]
        end

        WorkerProcess --> JS_Result[JS Promise Resolves]
        JS_Result --> FromWorker[DecodedTexture::from_worker_result]
        FileType -- No --> W_Decode[DecodedTexture::from_bytes]

        FromWorker --> W_Send[Send Result]
        W_Decode --> W_Send
    end

    %% Communication Channel
    N_Send --> Channel(mpsc::Sender / Receiver)
    W_Send --> Channel
    Channel --> PollReceiver

    classDef native fill:#e1f5fe,stroke:#01579b,color:black;
    classDef web fill:#fff3e0,stroke:#e65100,color:black;
    classDef shared fill:#f3e5f5,stroke:#4a148c,color:black;

    class Native_Execution native;
    class Web_Execution,Web_Worker web;
    class Main_Thread shared;