code:
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later

#include <array>
#include <cmath>
#include <span>
#include <unordered_map>

#include "common/assert.h"
#include "common/bit_cast.h"
#include "video_core/engines/sw_blitter/converter.h"
#include "video_core/surface.h"
#include "video_core/textures/decoders.h"

#ifdef _MSC_VER
#define FORCE_INLINE __forceinline
#else
#define FORCE_INLINE inline __attribute__((always_inline))
#endif

namespace Tegra::Engines::Blitter {

enum class Swizzle : size_t {
    R = 0,
    G = 1,
    B = 2,
    A = 3,
    None,
};

enum class ComponentType : u32 {
    SNORM = 1,
    UNORM = 2,
    SINT = 3,
    UINT = 4,
    SNORM_FORCE_FP16 = 5,
    UNORM_FORCE_FP16 = 6,
    FLOAT = 7,
    SRGB = 8,
};

namespace {

/*
 * Note: Use generate_converters.py to generate the structs and searches for new render target
 * formats and copy paste them to this file in order to update. just call "python
 * generate_converters.py" and get the code from the output. modify the file to add new formats.
 */

constexpr std::array<f32, 256> SRGB_TO_RGB_LUT = {
    0.000000e+00f, 3.035270e-04f, 6.070540e-04f, 9.105810e-04f, 1.214108e-03f, 1.517635e-03f,
    1.821162e-03f, 2.124689e-03f, 2.428216e-03f, 2.731743e-03f, 3.035270e-03f, 3.346536e-03f,
    3.676507e-03f, 4.024717e-03f, 4.391442e-03f, 4.776953e-03f, 5.181517e-03f, 5.605392e-03f,
    6.048833e-03f, 6.512091e-03f, 6.995410e-03f, 7.499032e-03f, 8.023193e-03f, 8.568126e-03f,
    9.134059e-03f, 9.721218e-03f, 1.032982e-02f, 1.096009e-02f, 1.161224e-02f, 1.228649e-02f,
    1.298303e-02f, 1.370208e-02f, 1.444384e-02f, 1.520851e-02f, 1.599629e-02f, 1.680738e-02f,
    1.764195e-02f, 1.850022e-02f, 1.938236e-02f, 2.028856e-02f, 2.121901e-02f, 2.217389e-02f,
    2.315337e-02f, 2.415763e-02f, 2.518686e-02f, 2.624122e-02f, 2.732089e-02f, 2.842604e-02f,
    2.955684e-02f, 3.071344e-02f, 3.189603e-02f, 3.310477e-02f, 3.433981e-02f, 3.560131e-02f,
    3.688945e-02f, 3.820437e-02f, 3.954624e-02f, 4.091520e-02f, 4.231141e-02f, 4.373503e-02f,
    4.518620e-02f, 4.666509e-02f, 4.817183e-02f, 4.970657e-02f, 5.126946e-02f, 5.286065e-02f,
    5.448028e-02f, 5.612849e-02f, 5.780543e-02f, 5.951124e-02f, 6.124605e-02f, 6.301001e-02f,
    6.480327e-02f, 6.662594e-02f, 6.847817e-02f, 7.036009e-02f, 7.227185e-02f, 7.421357e-02f,
    7.618538e-02f, 7.818742e-02f, 8.021982e-02f, 8.228271e-02f, 8.437621e-02f, 8.650046e-02f,
    8.865558e-02f, 9.084171e-02f, 9.305897e-02f, 9.530747e-02f, 9.758735e-02f, 9.989873e-02f,
    1.022417e-01f, 1.046165e-01f, 1.070231e-01f, 1.094617e-01f, 1.119324e-01f, 1.144354e-01f,
    1.169707e-01f, 1.195384e-01f, 1.221388e-01f, 1.247718e-01f, 1.274377e-01f, 1.301365e-01f,
    1.328683e-01f, 1.356333e-01f, 1.384316e-01f, 1.412633e-01f, 1.441285e-01f, 1.470273e-01f,
    1.499598e-01f, 1.529261e-01f, 1.559265e-01f, 1.589608e-01f, 1.620294e-01f, 1.651322e-01f,
    1.682694e-01f, 1.714411e-01f, 1.746474e-01f, 1.778884e-01f, 1.811642e-01f, 1.844750e-01f,
    1.878208e-01f, 1.912017e-01f, 1.946178e-01f, 1.980693e-01f, 2.015563e-01f, 2.050787e-01f,
    2.086369e-01f, 2.122308e-01f, 2.158605e-01f, 2.195262e-01f, 2.232280e-01f, 2.269659e-01f,
    2.307401e-01f, 2.345506e-01f, 2.383976e-01f, 2.422811e-01f, 2.462013e-01f, 2.501583e-01f,
    2.541521e-01f, 2.581829e-01f, 2.622507e-01f, 2.663556e-01f, 2.704978e-01f, 2.746773e-01f,
    2.788943e-01f, 2.831487e-01f, 2.874408e-01f, 2.917706e-01f, 2.961383e-01f, 3.005438e-01f,
    3.049873e-01f, 3.094689e-01f, 3.139887e-01f, 3.185468e-01f, 3.231432e-01f, 3.277781e-01f,
    3.324515e-01f, 3.371636e-01f, 3.419144e-01f, 3.467041e-01f, 3.515326e-01f, 3.564001e-01f,
    3.613068e-01f, 3.662526e-01f, 3.712377e-01f, 3.762621e-01f, 3.813260e-01f, 3.864294e-01f,
    3.915725e-01f, 3.967552e-01f, 4.019778e-01f, 4.072402e-01f, 4.125426e-01f, 4.178851e-01f,
    4.232677e-01f, 4.286905e-01f, 4.341536e-01f, 4.396572e-01f, 4.452012e-01f, 4.507858e-01f,
    4.564110e-01f, 4.620770e-01f, 4.677838e-01f, 4.735315e-01f, 4.793202e-01f, 4.851499e-01f,
    4.910209e-01f, 4.969330e-01f, 5.028865e-01f, 5.088813e-01f, 5.149177e-01f, 5.209956e-01f,
    5.271151e-01f, 5.332764e-01f, 5.394795e-01f, 5.457245e-01f, 5.520114e-01f, 5.583404e-01f,
    5.647115e-01f, 5.711249e-01f, 5.775805e-01f, 5.840784e-01f, 5.906188e-01f, 5.972018e-01f,
    6.038274e-01f, 6.104956e-01f, 6.172066e-01f, 6.239604e-01f, 6.307572e-01f, 6.375968e-01f,
    6.444797e-01f, 6.514056e-01f, 6.583748e-01f, 6.653873e-01f, 6.724432e-01f, 6.795425e-01f,
    6.866853e-01f, 6.938717e-01f, 7.011019e-01f, 7.083758e-01f, 7.156935e-01f, 7.230551e-01f,
    7.304608e-01f, 7.379104e-01f, 7.454042e-01f, 7.529422e-01f, 7.605245e-01f, 7.681512e-01f,
    7.758222e-01f, 7.835378e-01f, 7.912979e-01f, 7.991027e-01f, 8.069522e-01f, 8.148466e-01f,
    8.227857e-01f, 8.307699e-01f, 8.387990e-01f, 8.468732e-01f, 8.549926e-01f, 8.631572e-01f,
    8.713671e-01f, 8.796224e-01f, 8.879231e-01f, 8.962694e-01f, 9.046612e-01f, 9.130986e-01f,
    9.215819e-01f, 9.301109e-01f, 9.386857e-01f, 9.473065e-01f, 9.559733e-01f, 9.646863e-01f,
    9.734453e-01f, 9.822506e-01f, 9.911021e-01f, 1.000000e+00f};

constexpr std::array<f32, 256> RGB_TO_SRGB_LUT = {
    0.000000e+00f, 4.984009e-02f, 8.494473e-02f, 1.107021e-01f, 1.318038e-01f, 1.500052e-01f,
    1.661857e-01f, 1.808585e-01f, 1.943532e-01f, 2.068957e-01f, 2.186491e-01f, 2.297351e-01f,
    2.402475e-01f, 2.502604e-01f, 2.598334e-01f, 2.690152e-01f, 2.778465e-01f, 2.863614e-01f,
    2.945889e-01f, 3.025538e-01f, 3.102778e-01f, 3.177796e-01f, 3.250757e-01f, 3.321809e-01f,
    3.391081e-01f, 3.458689e-01f, 3.524737e-01f, 3.589320e-01f, 3.652521e-01f, 3.714419e-01f,
    3.775084e-01f, 3.834581e-01f, 3.892968e-01f, 3.950301e-01f, 4.006628e-01f, 4.061998e-01f,
    4.116451e-01f, 4.170030e-01f, 4.222770e-01f, 4.274707e-01f, 4.325873e-01f, 4.376298e-01f,
    4.426010e-01f, 4.475037e-01f, 4.523403e-01f, 4.571131e-01f, 4.618246e-01f, 4.664766e-01f,
    4.710712e-01f, 4.756104e-01f, 4.800958e-01f, 4.845292e-01f, 4.889122e-01f, 4.932462e-01f,
    4.975329e-01f, 5.017734e-01f, 5.059693e-01f, 5.101216e-01f, 5.142317e-01f, 5.183006e-01f,
    5.223295e-01f, 5.263194e-01f, 5.302714e-01f, 5.341862e-01f, 5.380651e-01f, 5.419087e-01f,
    5.457181e-01f, 5.494938e-01f, 5.532369e-01f, 5.569480e-01f, 5.606278e-01f, 5.642771e-01f,
    5.678965e-01f, 5.714868e-01f, 5.750484e-01f, 5.785821e-01f, 5.820884e-01f, 5.855680e-01f,
    5.890211e-01f, 5.924487e-01f, 5.958509e-01f, 5.992285e-01f, 6.025819e-01f, 6.059114e-01f,
    6.092176e-01f, 6.125010e-01f, 6.157619e-01f, 6.190008e-01f, 6.222180e-01f, 6.254140e-01f,
    6.285890e-01f, 6.317436e-01f, 6.348780e-01f, 6.379926e-01f, 6.410878e-01f, 6.441637e-01f,
    6.472208e-01f, 6.502595e-01f, 6.532799e-01f, 6.562824e-01f, 6.592672e-01f, 6.622347e-01f,
    6.651851e-01f, 6.681187e-01f, 6.710356e-01f, 6.739363e-01f, 6.768209e-01f, 6.796897e-01f,
    6.825429e-01f, 6.853807e-01f, 6.882034e-01f, 6.910111e-01f, 6.938041e-01f, 6.965826e-01f,
    6.993468e-01f, 7.020969e-01f, 7.048331e-01f, 7.075556e-01f, 7.102645e-01f, 7.129600e-01f,
    7.156424e-01f, 7.183118e-01f, 7.209683e-01f, 7.236121e-01f, 7.262435e-01f, 7.288625e-01f,
    7.314693e-01f, 7.340640e-01f, 7.366470e-01f, 7.392181e-01f, 7.417776e-01f, 7.443256e-01f,
    7.468624e-01f, 7.493880e-01f, 7.519025e-01f, 7.544061e-01f, 7.568989e-01f, 7.593810e-01f,
    7.618526e-01f, 7.643137e-01f, 7.667645e-01f, 7.692052e-01f, 7.716358e-01f, 7.740564e-01f,
    7.764671e-01f, 7.788681e-01f, 7.812595e-01f, 7.836413e-01f, 7.860138e-01f, 7.883768e-01f,
    7.907307e-01f, 7.930754e-01f, 7.954110e-01f, 7.977377e-01f, 8.000556e-01f, 8.023647e-01f,
    8.046651e-01f, 8.069569e-01f, 8.092403e-01f, 8.115152e-01f, 8.137818e-01f, 8.160402e-01f,
    8.182903e-01f, 8.205324e-01f, 8.227665e-01f, 8.249926e-01f, 8.272109e-01f, 8.294214e-01f,
    8.316242e-01f, 8.338194e-01f, 8.360070e-01f, 8.381871e-01f, 8.403597e-01f, 8.425251e-01f,
    8.446831e-01f, 8.468339e-01f, 8.489776e-01f, 8.511142e-01f, 8.532437e-01f, 8.553662e-01f,
    8.574819e-01f, 8.595907e-01f, 8.616927e-01f, 8.637881e-01f, 8.658767e-01f, 8.679587e-01f,
    8.700342e-01f, 8.721032e-01f, 8.741657e-01f, 8.762218e-01f, 8.782716e-01f, 8.803151e-01f,
    8.823524e-01f, 8.843835e-01f, 8.864085e-01f, 8.884274e-01f, 8.904402e-01f, 8.924471e-01f,
    8.944480e-01f, 8.964431e-01f, 8.984324e-01f, 9.004158e-01f, 9.023935e-01f, 9.043654e-01f,
    9.063318e-01f, 9.082925e-01f, 9.102476e-01f, 9.121972e-01f, 9.141413e-01f, 9.160800e-01f,
    9.180133e-01f, 9.199412e-01f, 9.218637e-01f, 9.237810e-01f, 9.256931e-01f, 9.276000e-01f,
    9.295017e-01f, 9.313982e-01f, 9.332896e-01f, 9.351761e-01f, 9.370575e-01f, 9.389339e-01f,
    9.408054e-01f, 9.426719e-01f, 9.445336e-01f, 9.463905e-01f, 9.482424e-01f, 9.500897e-01f,
    9.519322e-01f, 9.537700e-01f, 9.556032e-01f, 9.574316e-01f, 9.592555e-01f, 9.610748e-01f,
    9.628896e-01f, 9.646998e-01f, 9.665055e-01f, 9.683068e-01f, 9.701037e-01f, 9.718961e-01f,
    9.736842e-01f, 9.754679e-01f, 9.772474e-01f, 9.790225e-01f, 9.807934e-01f, 9.825601e-01f,
    9.843225e-01f, 9.860808e-01f, 9.878350e-01f, 9.895850e-01f, 9.913309e-01f, 9.930727e-01f,
    9.948106e-01f, 9.965444e-01f, 9.982741e-01f, 1.000000e+00f};

} // namespace

struct R32G32B32A32_FLOATTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::FLOAT, ComponentType::FLOAT, ComponentType::FLOAT, ComponentType::FLOAT};
    static constexpr std::array<size_t, num_components> component_sizes = {32, 32, 32, 32};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::R, Swizzle::G, Swizzle::B, Swizzle::A};
};

struct R32G32B32A32_SINTTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::SINT, ComponentType::SINT, ComponentType::SINT, ComponentType::SINT};
    static constexpr std::array<size_t, num_components> component_sizes = {32, 32, 32, 32};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::R, Swizzle::G, Swizzle::B, Swizzle::A};
};

struct R32G32B32A32_UINTTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UINT, ComponentType::UINT, ComponentType::UINT, ComponentType::UINT};
    static constexpr std::array<size_t, num_components> component_sizes = {32, 32, 32, 32};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::R, Swizzle::G, Swizzle::B, Swizzle::A};
};

struct R32G32B32X32_FLOATTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::FLOAT, ComponentType::FLOAT, ComponentType::FLOAT, ComponentType::FLOAT};
    static constexpr std::array<size_t, num_components> component_sizes = {32, 32, 32, 32};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::R, Swizzle::G, Swizzle::B, Swizzle::None};
};

struct R32G32B32X32_SINTTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::SINT, ComponentType::SINT, ComponentType::SINT, ComponentType::SINT};
    static constexpr std::array<size_t, num_components> component_sizes = {32, 32, 32, 32};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::R, Swizzle::G, Swizzle::B, Swizzle::None};
};

struct R32G32B32X32_UINTTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UINT, ComponentType::UINT, ComponentType::UINT, ComponentType::UINT};
    static constexpr std::array<size_t, num_components> component_sizes = {32, 32, 32, 32};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::R, Swizzle::G, Swizzle::B, Swizzle::None};
};

struct R16G16B16A16_UNORMTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UNORM, ComponentType::UNORM, ComponentType::UNORM, ComponentType::UNORM};
    static constexpr std::array<size_t, num_components> component_sizes = {16, 16, 16, 16};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::R, Swizzle::G, Swizzle::B, Swizzle::A};
};

struct R16G16B16A16_SNORMTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::SNORM, ComponentType::SNORM, ComponentType::SNORM, ComponentType::SNORM};
    static constexpr std::array<size_t, num_components> component_sizes = {16, 16, 16, 16};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::R, Swizzle::G, Swizzle::B, Swizzle::A};
};

struct R16G16B16A16_SINTTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::SINT, ComponentType::SINT, ComponentType::SINT, ComponentType::SINT};
    static constexpr std::array<size_t, num_components> component_sizes = {16, 16, 16, 16};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::R, Swizzle::G, Swizzle::B, Swizzle::A};
};

struct R16G16B16A16_UINTTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UINT, ComponentType::UINT, ComponentType::UINT, ComponentType::UINT};
    static constexpr std::array<size_t, num_components> component_sizes = {16, 16, 16, 16};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::R, Swizzle::G, Swizzle::B, Swizzle::A};
};

struct R16G16B16A16_FLOATTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::FLOAT, ComponentType::FLOAT, ComponentType::FLOAT, ComponentType::FLOAT};
    static constexpr std::array<size_t, num_components> component_sizes = {16, 16, 16, 16};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::R, Swizzle::G, Swizzle::B, Swizzle::A};
};

struct R32G32_FLOATTraits {
    static constexpr size_t num_components = 2;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::FLOAT, ComponentType::FLOAT};
    static constexpr std::array<size_t, num_components> component_sizes = {32, 32};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {Swizzle::R,
                                                                              Swizzle::G};
};

struct R32G32_SINTTraits {
    static constexpr size_t num_components = 2;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::SINT, ComponentType::SINT};
    static constexpr std::array<size_t, num_components> component_sizes = {32, 32};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {Swizzle::R,
                                                                              Swizzle::G};
};

struct R32G32_UINTTraits {
    static constexpr size_t num_components = 2;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UINT, ComponentType::UINT};
    static constexpr std::array<size_t, num_components> component_sizes = {32, 32};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {Swizzle::R,
                                                                              Swizzle::G};
};

struct R16G16B16X16_FLOATTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::FLOAT, ComponentType::FLOAT, ComponentType::FLOAT, ComponentType::FLOAT};
    static constexpr std::array<size_t, num_components> component_sizes = {16, 16, 16, 16};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::R, Swizzle::G, Swizzle::B, Swizzle::None};
};

struct A8R8G8B8_UNORMTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UNORM, ComponentType::UNORM, ComponentType::UNORM, ComponentType::UNORM};
    static constexpr std::array<size_t, num_components> component_sizes = {8, 8, 8, 8};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::A, Swizzle::R, Swizzle::G, Swizzle::B};
};

struct A8R8G8B8_SRGBTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::SRGB, ComponentType::SRGB, ComponentType::SRGB, ComponentType::SRGB};
    static constexpr std::array<size_t, num_components> component_sizes = {8, 8, 8, 8};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::A, Swizzle::R, Swizzle::G, Swizzle::B};
};

struct A2B10G10R10_UNORMTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UNORM, ComponentType::UNORM, ComponentType::UNORM, ComponentType::UNORM};
    static constexpr std::array<size_t, num_components> component_sizes = {2, 10, 10, 10};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::A, Swizzle::B, Swizzle::G, Swizzle::R};
};

struct A2B10G10R10_UINTTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UINT, ComponentType::UINT, ComponentType::UINT, ComponentType::UINT};
    static constexpr std::array<size_t, num_components> component_sizes = {2, 10, 10, 10};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::A, Swizzle::B, Swizzle::G, Swizzle::R};
};

struct A2R10G10B10_UNORMTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UNORM, ComponentType::UNORM, ComponentType::UNORM, ComponentType::UNORM};
    static constexpr std::array<size_t, num_components> component_sizes = {2, 10, 10, 10};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::A, Swizzle::R, Swizzle::G, Swizzle::B};
};

struct A8B8G8R8_UNORMTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UNORM, ComponentType::UNORM, ComponentType::UNORM, ComponentType::UNORM};
    static constexpr std::array<size_t, num_components> component_sizes = {8, 8, 8, 8};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::A, Swizzle::B, Swizzle::G, Swizzle::R};
};

struct A8B8G8R8_SRGBTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::SRGB, ComponentType::SRGB, ComponentType::SRGB, ComponentType::SRGB};
    static constexpr std::array<size_t, num_components> component_sizes = {8, 8, 8, 8};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::A, Swizzle::B, Swizzle::G, Swizzle::R};
};

struct A8B8G8R8_SNORMTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::SNORM, ComponentType::SNORM, ComponentType::SNORM, ComponentType::SNORM};
    static constexpr std::array<size_t, num_components> component_sizes = {8, 8, 8, 8};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::A, Swizzle::B, Swizzle::G, Swizzle::R};
};

struct A8B8G8R8_SINTTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::SINT, ComponentType::SINT, ComponentType::SINT, ComponentType::SINT};
    static constexpr std::array<size_t, num_components> component_sizes = {8, 8, 8, 8};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::A, Swizzle::B, Swizzle::G, Swizzle::R};
};

struct A8B8G8R8_UINTTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UINT, ComponentType::UINT, ComponentType::UINT, ComponentType::UINT};
    static constexpr std::array<size_t, num_components> component_sizes = {8, 8, 8, 8};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::A, Swizzle::B, Swizzle::G, Swizzle::R};
};

struct R16G16_UNORMTraits {
    static constexpr size_t num_components = 2;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UNORM, ComponentType::UNORM};
    static constexpr std::array<size_t, num_components> component_sizes = {16, 16};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {Swizzle::R,
                                                                              Swizzle::G};
};

struct R16G16_SNORMTraits {
    static constexpr size_t num_components = 2;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::SNORM, ComponentType::SNORM};
    static constexpr std::array<size_t, num_components> component_sizes = {16, 16};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {Swizzle::R,
                                                                              Swizzle::G};
};

struct R16G16_SINTTraits {
    static constexpr size_t num_components = 2;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::SINT, ComponentType::SINT};
    static constexpr std::array<size_t, num_components> component_sizes = {16, 16};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {Swizzle::R,
                                                                              Swizzle::G};
};

struct R16G16_UINTTraits {
    static constexpr size_t num_components = 2;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UINT, ComponentType::UINT};
    static constexpr std::array<size_t, num_components> component_sizes = {16, 16};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {Swizzle::R,
                                                                              Swizzle::G};
};

struct R16G16_FLOATTraits {
    static constexpr size_t num_components = 2;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::FLOAT, ComponentType::FLOAT};
    static constexpr std::array<size_t, num_components> component_sizes = {16, 16};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {Swizzle::R,
                                                                              Swizzle::G};
};

struct B10G11R11_FLOATTraits {
    static constexpr size_t num_components = 3;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::FLOAT, ComponentType::FLOAT, ComponentType::FLOAT};
    static constexpr std::array<size_t, num_components> component_sizes = {10, 11, 11};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::B, Swizzle::G, Swizzle::R};
};

struct R32_SINTTraits {
    static constexpr size_t num_components = 1;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::SINT};
    static constexpr std::array<size_t, num_components> component_sizes = {32};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {Swizzle::R};
};

struct R32_UINTTraits {
    static constexpr size_t num_components = 1;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UINT};
    static constexpr std::array<size_t, num_components> component_sizes = {32};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {Swizzle::R};
};

struct R32_FLOATTraits {
    static constexpr size_t num_components = 1;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::FLOAT};
    static constexpr std::array<size_t, num_components> component_sizes = {32};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {Swizzle::R};
};

struct X8R8G8B8_UNORMTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UNORM, ComponentType::UNORM, ComponentType::UNORM, ComponentType::UNORM};
    static constexpr std::array<size_t, num_components> component_sizes = {8, 8, 8, 8};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::None, Swizzle::R, Swizzle::G, Swizzle::B};
};

struct X8R8G8B8_SRGBTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::SRGB, ComponentType::SRGB, ComponentType::SRGB, ComponentType::SRGB};
    static constexpr std::array<size_t, num_components> component_sizes = {8, 8, 8, 8};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::None, Swizzle::R, Swizzle::G, Swizzle::B};
};

struct R5G6B5_UNORMTraits {
    static constexpr size_t num_components = 3;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UNORM, ComponentType::UNORM, ComponentType::UNORM};
    static constexpr std::array<size_t, num_components> component_sizes = {5, 6, 5};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::R, Swizzle::G, Swizzle::B};
};

struct A1R5G5B5_UNORMTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UNORM, ComponentType::UNORM, ComponentType::UNORM, ComponentType::UNORM};
    static constexpr std::array<size_t, num_components> component_sizes = {1, 5, 5, 5};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::A, Swizzle::R, Swizzle::G, Swizzle::B};
};

struct R8G8_UNORMTraits {
    static constexpr size_t num_components = 2;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UNORM, ComponentType::UNORM};
    static constexpr std::array<size_t, num_components> component_sizes = {8, 8};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {Swizzle::R,
                                                                              Swizzle::G};
};

struct R8G8_SNORMTraits {
    static constexpr size_t num_components = 2;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::SNORM, ComponentType::SNORM};
    static constexpr std::array<size_t, num_components> component_sizes = {8, 8};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {Swizzle::R,
                                                                              Swizzle::G};
};

struct R8G8_SINTTraits {
    static constexpr size_t num_components = 2;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::SINT, ComponentType::SINT};
    static constexpr std::array<size_t, num_components> component_sizes = {8, 8};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {Swizzle::R,
                                                                              Swizzle::G};
};

struct R8G8_UINTTraits {
    static constexpr size_t num_components = 2;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UINT, ComponentType::UINT};
    static constexpr std::array<size_t, num_components> component_sizes = {8, 8};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {Swizzle::R,
                                                                              Swizzle::G};
};

struct R16_UNORMTraits {
    static constexpr size_t num_components = 1;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UNORM};
    static constexpr std::array<size_t, num_components> component_sizes = {16};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {Swizzle::R};
};

struct R16_SNORMTraits {
    static constexpr size_t num_components = 1;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::SNORM};
    static constexpr std::array<size_t, num_components> component_sizes = {16};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {Swizzle::R};
};

struct R16_SINTTraits {
    static constexpr size_t num_components = 1;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::SINT};
    static constexpr std::array<size_t, num_components> component_sizes = {16};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {Swizzle::R};
};

struct R16_UINTTraits {
    static constexpr size_t num_components = 1;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UINT};
    static constexpr std::array<size_t, num_components> component_sizes = {16};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {Swizzle::R};
};

struct R16_FLOATTraits {
    static constexpr size_t num_components = 1;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::FLOAT};
    static constexpr std::array<size_t, num_components> component_sizes = {16};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {Swizzle::R};
};

struct R8_UNORMTraits {
    static constexpr size_t num_components = 1;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UNORM};
    static constexpr std::array<size_t, num_components> component_sizes = {8};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {Swizzle::R};
};

struct R8_SNORMTraits {
    static constexpr size_t num_components = 1;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::SNORM};
    static constexpr std::array<size_t, num_components> component_sizes = {8};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {Swizzle::R};
};

struct R8_SINTTraits {
    static constexpr size_t num_components = 1;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::SINT};
    static constexpr std::array<size_t, num_components> component_sizes = {8};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {Swizzle::R};
};

struct R8_UINTTraits {
    static constexpr size_t num_components = 1;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UINT};
    static constexpr std::array<size_t, num_components> component_sizes = {8};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {Swizzle::R};
};

struct X1R5G5B5_UNORMTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UNORM, ComponentType::UNORM, ComponentType::UNORM, ComponentType::UNORM};
    static constexpr std::array<size_t, num_components> component_sizes = {1, 5, 5, 5};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::None, Swizzle::R, Swizzle::G, Swizzle::B};
};

struct X8B8G8R8_UNORMTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::UNORM, ComponentType::UNORM, ComponentType::UNORM, ComponentType::UNORM};
    static constexpr std::array<size_t, num_components> component_sizes = {8, 8, 8, 8};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::None, Swizzle::B, Swizzle::G, Swizzle::R};
};

struct X8B8G8R8_SRGBTraits {
    static constexpr size_t num_components = 4;
    static constexpr std::array<ComponentType, num_components> component_types = {
        ComponentType::SRGB, ComponentType::SRGB, ComponentType::SRGB, ComponentType::SRGB};
    static constexpr std::array<size_t, num_components> component_sizes = {8, 8, 8, 8};
    static constexpr std::array<Swizzle, num_components> component_swizzle = {
        Swizzle::None, Swizzle::B, Swizzle::G, Swizzle::R};
};

template <class ConverterTraits>
class ConverterImpl : public Converter {
private:
    static constexpr size_t num_components = ConverterTraits::num_components;
    static constexpr std::array<ComponentType, num_components> component_types =
        ConverterTraits::component_types;
    static constexpr std::array<size_t, num_components> component_sizes =
        ConverterTraits::component_sizes;
    static constexpr std::array<Swizzle, num_components> component_swizzle =
        ConverterTraits::component_swizzle;

    static constexpr size_t CalculateByteSize() {
        size_t size = 0;
        for (const size_t component_size : component_sizes) {
            size += component_size;
        }
        const size_t power = (sizeof(size_t) * 8) - std::countl_zero(size) - 1ULL;
        const size_t base_size = 1ULL << power;
        const size_t mask = base_size - 1ULL;
        return ((size & mask) != 0 ? base_size << 1ULL : base_size) / 8;
    }

    static constexpr size_t total_bytes_per_pixel = CalculateByteSize();
    static constexpr size_t total_words_per_pixel =
        (total_bytes_per_pixel + sizeof(u32) - 1U) / sizeof(u32);
    static constexpr size_t components_per_ir_rep = 4;

    template <bool get_offsets>
    static constexpr std::array<size_t, num_components> GetBoundWordsOffsets() {
        std::array<size_t, num_components> result;
        result.fill(0);
        constexpr size_t total_bits_per_word = sizeof(u32) * 8;
        size_t accumulated_size = 0;
        size_t count = 0;
        for (size_t i = 0; i < num_components; i++) {
            if constexpr (get_offsets) {
                result[i] = accumulated_size;
            } else {
                result[i] = count;
            }
            accumulated_size += component_sizes[i];
            if (accumulated_size > total_bits_per_word) {
                if constexpr (get_offsets) {
                    result[i] = 0;
                } else {
                    result[i]++;
                }
                count++;
                accumulated_size = component_sizes[i];
            }
        }
        return result;
    }

    static constexpr std::array<size_t, num_components> bound_words = GetBoundWordsOffsets<false>();
    static constexpr std::array<size_t, num_components> bound_offsets =
        GetBoundWordsOffsets<true>();

    static constexpr std::array<u32, num_components> GetComponentsMask() {
        std::array<u32, num_components> result;
        for (size_t i = 0; i < num_components; i++) {
            result[i] = (((u32)~0) >> (8 * sizeof(u32) - component_sizes[i])) << bound_offsets[i];
        }
        return result;
    }

    static constexpr std::array<u32, num_components> component_mask = GetComponentsMask();

    // We are forcing inline so the compiler can SIMD the conversations, since it may do 4 function
    // calls, it may fail to detect the benefit of inlining.
    template <size_t which_component>
    FORCE_INLINE void ConvertToComponent(u32 which_word, f32& out_component) {
        const u32 value = (which_word >> bound_offsets[which_component]) &
                          static_cast<u32>((1ULL << component_sizes[which_component]) - 1ULL);
        const auto sign_extend = [](u32 base_value, size_t bits) {
            const size_t shift_amount = sizeof(u32) * 8 - bits;
            s32 shifted_value = static_cast<s32>(base_value << shift_amount);
            return shifted_value >> shift_amount;
        };
        const auto force_to_fp16 = [](f32 base_value) {
            u32 tmp = Common::BitCast<u32>(base_value);
            constexpr size_t fp32_mantissa_bits = 23;
            constexpr size_t fp16_mantissa_bits = 10;
            constexpr size_t mantissa_mask =
                ~((1ULL << (fp32_mantissa_bits - fp16_mantissa_bits)) - 1ULL);
            tmp = tmp & static_cast<u32>(mantissa_mask);
            // TODO: force the exponent within the range of half float. Not needed in UNORM / SNORM
            return Common::BitCast<f32>(tmp);
        };
        const auto from_fp_n = [&sign_extend](u32 base_value, size_t bits, size_t mantissa) {
            constexpr size_t fp32_mantissa_bits = 23;
            size_t shift_towards = fp32_mantissa_bits - mantissa;
            const u32 new_value =
                static_cast<u32>(sign_extend(base_value, bits) << shift_towards) & (~(1U << 31));
            return Common::BitCast<f32>(new_value);
        };
        const auto calculate_snorm = [&]() {
            return static_cast<f32>(
                static_cast<f32>(sign_extend(value, component_sizes[which_component])) /
                static_cast<f32>((1ULL << (component_sizes[which_component] - 1ULL)) - 1ULL));
        };
        const auto calculate_unorm = [&]() {
            return static_cast<f32>(
                static_cast<f32>(value) /
                static_cast<f32>((1ULL << (component_sizes[which_component])) - 1ULL));
        };
        if constexpr (component_types[which_component] == ComponentType::SNORM) {
            out_component = calculate_snorm();
        } else if constexpr (component_types[which_component] == ComponentType::UNORM) {
            out_component = calculate_unorm();
        } else if constexpr (component_types[which_component] == ComponentType::SINT) {
            out_component = static_cast<f32>(
                static_cast<s32>(sign_extend(value, component_sizes[which_component])));
        } else if constexpr (component_types[which_component] == ComponentType::UINT) {
            out_component = static_cast<f32>(
                static_cast<s32>(sign_extend(value, component_sizes[which_component])));
        } else if constexpr (component_types[which_component] == ComponentType::SNORM_FORCE_FP16) {
            out_component = calculate_snorm();
            out_component = force_to_fp16(out_component);
        } else if constexpr (component_types[which_component] == ComponentType::UNORM_FORCE_FP16) {
            out_component = calculate_unorm();
            out_component = force_to_fp16(out_component);
        } else if constexpr (component_types[which_component] == ComponentType::FLOAT) {
            if constexpr (component_sizes[which_component] == 32) {
                out_component = Common::BitCast<f32>(value);
            } else if constexpr (component_sizes[which_component] == 16) {
                static constexpr u32 sign_mask = 0x8000;
                static constexpr u32 mantissa_mask = 0x8000;
                out_component = Common::BitCast<f32>(((value & sign_mask) << 16) |
                                                     (((value & 0x7c00) + 0x1C000) << 13) |
                                                     ((value & mantissa_mask) << 13));
            } else {
                out_component = from_fp_n(value, component_sizes[which_component],
                                          component_sizes[which_component] - 5);
            }
        } else if constexpr (component_types[which_component] == ComponentType::SRGB) {
            if constexpr (component_swizzle[which_component] == Swizzle::A) {
                out_component = calculate_unorm();
            } else if constexpr (component_sizes[which_component] == 8) {
                out_component = SRGB_TO_RGB_LUT[value];
            } else {
                out_component = calculate_unorm();
                UNIMPLEMENTED_MSG("SRGB Conversion with component sizes of {} is unimplemented",
                                  component_sizes[which_component]);
            }
        }
    }

    // We are forcing inline so the compiler can SIMD the conversations, since it may do 4 function
    // calls, it may fail to detect the benefit of inlining.
    template <size_t which_component>
    FORCE_INLINE void ConvertFromComponent(u32& which_word, f32 in_component) {
        const auto insert_to_word = [&]<typename T>(T new_word) {
            which_word |= (static_cast<u32>(new_word) << bound_offsets[which_component]) &
                          component_mask[which_component];
        };
        const auto to_fp_n = [](f32 base_value, size_t bits, size_t mantissa) {
            constexpr size_t fp32_mantissa_bits = 23;
            u32 tmp_value = Common::BitCast<u32>(std::max(base_value, 0.0f));
            size_t shift_towards = fp32_mantissa_bits - mantissa;
            return tmp_value >> shift_towards;
        };
        const auto calculate_unorm = [&]() {
            return static_cast<u32>(
                static_cast<f32>(in_component) *
                static_cast<f32>((1ULL << (component_sizes[which_component])) - 1ULL));
        };
        if constexpr (component_types[which_component] == ComponentType::SNORM ||
                      component_types[which_component] == ComponentType::SNORM_FORCE_FP16) {
            s32 tmp_word = static_cast<s32>(
                static_cast<f32>(in_component) *
                static_cast<f32>((1ULL << (component_sizes[which_component] - 1ULL)) - 1ULL));
            insert_to_word(tmp_word);

        } else if constexpr (component_types[which_component] == ComponentType::UNORM ||
                             component_types[which_component] == ComponentType::UNORM_FORCE_FP16) {
            u32 tmp_word = calculate_unorm();
            insert_to_word(tmp_word);
        } else if constexpr (component_types[which_component] == ComponentType::SINT) {
            s32 tmp_word = static_cast<s32>(in_component);
            insert_to_word(tmp_word);
        } else if constexpr (component_types[which_component] == ComponentType::UINT) {
            u32 tmp_word = static_cast<u32>(in_component);
            insert_to_word(tmp_word);
        } else if constexpr (component_types[which_component] == ComponentType::FLOAT) {
            if constexpr (component_sizes[which_component] == 32) {
                u32 tmp_word = Common::BitCast<u32>(in_component);
                insert_to_word(tmp_word);
            } else if constexpr (component_sizes[which_component] == 16) {
                static constexpr u32 sign_mask = 0x8000;
                static constexpr u32 mantissa_mask = 0x03ff;
                static constexpr u32 exponent_mask = 0x7c00;
                const u32 tmp_word = Common::BitCast<u32>(in_component);
                const u32 half = ((tmp_word >> 16) & sign_mask) |
                                 ((((tmp_word & 0x7f800000) - 0x38000000) >> 13) & exponent_mask) |
                                 ((tmp_word >> 13) & mantissa_mask);
                insert_to_word(half);
            } else {
                insert_to_word(to_fp_n(in_component, component_sizes[which_component],
                                       component_sizes[which_component] - 5));
            }
        } else if constexpr (component_types[which_component] == ComponentType::SRGB) {
            if constexpr (component_swizzle[which_component] != Swizzle::A) {
                if constexpr (component_sizes[which_component] == 8) {
                    const u32 index = calculate_unorm();
                    in_component = RGB_TO_SRGB_LUT[index];
                } else {
                    UNIMPLEMENTED_MSG("SRGB Conversion with component sizes of {} is unimplemented",
                                      component_sizes[which_component]);
                }
            }
            const u32 tmp_word = calculate_unorm();
            insert_to_word(tmp_word);
        }
    }

public:
    void ConvertTo(std::span<const u8> input, std::span<f32> output) override {
        const size_t num_pixels = output.size() / components_per_ir_rep;
        for (size_t pixel = 0; pixel < num_pixels; pixel++) {
            std::array<u32, total_words_per_pixel> words{};

            std::memcpy(words.data(), &input[pixel * total_bytes_per_pixel], total_bytes_per_pixel);
            std::span<f32> new_components(&output[pixel * components_per_ir_rep],
                                          components_per_ir_rep);
            if constexpr (component_swizzle[0] != Swizzle::None) {
                ConvertToComponent<0>(words[bound_words[0]],
                                      new_components[static_cast<size_t>(component_swizzle[0])]);
            } else {
                new_components[0] = 0.0f;
            }
            if constexpr (num_components >= 2) {
                if constexpr (component_swizzle[1] != Swizzle::None) {
                    ConvertToComponent<1>(
                        words[bound_words[1]],
                        new_components[static_cast<size_t>(component_swizzle[1])]);
                } else {
                    new_components[1] = 0.0f;
                }
            } else {
                new_components[1] = 0.0f;
            }
            if constexpr (num_components >= 3) {
                if constexpr (component_swizzle[2] != Swizzle::None) {
                    ConvertToComponent<2>(
                        words[bound_words[2]],
                        new_components[static_cast<size_t>(component_swizzle[2])]);
                } else {
                    new_components[2] = 0.0f;
                }
            } else {
                new_components[2] = 0.0f;
            }
            if constexpr (num_components >= 4) {
                if constexpr (component_swizzle[3] != Swizzle::None) {
                    ConvertToComponent<3>(
                        words[bound_words[3]],
                        new_components[static_cast<size_t>(component_swizzle[3])]);
                } else {
                    new_components[3] = 0.0f;
                }
            } else {
                new_components[3] = 0.0f;
            }
        }
    }

    void ConvertFrom(std::span<const f32> input, std::span<u8> output) override {
        const size_t num_pixels = output.size() / total_bytes_per_pixel;
        for (size_t pixel = 0; pixel < num_pixels; pixel++) {
            std::span<const f32> old_components(&input[pixel * components_per_ir_rep],
                                                components_per_ir_rep);
            std::array<u32, total_words_per_pixel> words{};
            if constexpr (component_swizzle[0] != Swizzle::None) {
                ConvertFromComponent<0>(words[bound_words[0]],
                                        old_components[static_cast<size_t>(component_swizzle[0])]);
            }
            if constexpr (num_components >= 2) {
                if constexpr (component_swizzle[1] != Swizzle::None) {
                    ConvertFromComponent<1>(
                        words[bound_words[1]],
                        old_components[static_cast<size_t>(component_swizzle[1])]);
                }
            }
            if constexpr (num_components >= 3) {
                if constexpr (component_swizzle[2] != Swizzle::None) {
                    ConvertFromComponent<2>(
                        words[bound_words[2]],
                        old_components[static_cast<size_t>(component_swizzle[2])]);
                }
            }
            if constexpr (num_components >= 4) {
                if constexpr (component_swizzle[3] != Swizzle::None) {
                    ConvertFromComponent<3>(
                        words[bound_words[3]],
                        old_components[static_cast<size_t>(component_swizzle[3])]);
                }
            }
            std::memcpy(&output[pixel * total_bytes_per_pixel], words.data(),
                        total_bytes_per_pixel);
        }
    }

    ConverterImpl() = default;
    ~ConverterImpl() override = default;
};

struct ConverterFactory::ConverterFactoryImpl {
    std::unordered_map<RenderTargetFormat, std::unique_ptr<Converter>> converters_cache;
};

ConverterFactory::ConverterFactory() {
    impl = std::make_unique<ConverterFactoryImpl>();
}

ConverterFactory::~ConverterFactory() = default;

Converter* ConverterFactory::GetFormatConverter(RenderTargetFormat format) {
    auto it = impl->converters_cache.find(format);
    if (it == impl->converters_cache.end()) [[unlikely]] {
        return BuildConverter(format);
    }
    return it->second.get();
}

class NullConverter : public Converter {
public:
    void ConvertTo([[maybe_unused]] std::span<const u8> input, std::span<f32> output) override {
        std::fill(output.begin(), output.end(), 0.0f);
    }
    void ConvertFrom([[maybe_unused]] std::span<const f32> input, std::span<u8> output) override {
        const u8 fill_value = 0U;
        std::fill(output.begin(), output.end(), fill_value);
    }
    NullConverter() = default;
    ~NullConverter() = default;
};

Converter* ConverterFactory::BuildConverter(RenderTargetFormat format) {
    switch (format) {
    case RenderTargetFormat::R32G32B32A32_FLOAT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R32G32B32A32_FLOATTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R32G32B32A32_SINT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R32G32B32A32_SINTTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R32G32B32A32_UINT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R32G32B32A32_UINTTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R32G32B32X32_FLOAT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R32G32B32X32_FLOATTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R32G32B32X32_SINT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R32G32B32X32_SINTTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R32G32B32X32_UINT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R32G32B32X32_UINTTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R16G16B16A16_UNORM:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R16G16B16A16_UNORMTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R16G16B16A16_SNORM:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R16G16B16A16_SNORMTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R16G16B16A16_SINT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R16G16B16A16_SINTTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R16G16B16A16_UINT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R16G16B16A16_UINTTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R16G16B16A16_FLOAT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R16G16B16A16_FLOATTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R32G32_FLOAT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R32G32_FLOATTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R32G32_SINT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R32G32_SINTTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R32G32_UINT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R32G32_UINTTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R16G16B16X16_FLOAT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R16G16B16X16_FLOATTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::A8R8G8B8_UNORM:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<A8R8G8B8_UNORMTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::A8R8G8B8_SRGB:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<A8R8G8B8_SRGBTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::A2B10G10R10_UNORM:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<A2B10G10R10_UNORMTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::A2B10G10R10_UINT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<A2B10G10R10_UINTTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::A2R10G10B10_UNORM:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<A2R10G10B10_UNORMTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::A8B8G8R8_UNORM:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<A8B8G8R8_UNORMTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::A8B8G8R8_SRGB:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<A8B8G8R8_SRGBTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::A8B8G8R8_SNORM:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<A8B8G8R8_SNORMTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::A8B8G8R8_SINT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<A8B8G8R8_SINTTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::A8B8G8R8_UINT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<A8B8G8R8_UINTTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R16G16_UNORM:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R16G16_UNORMTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R16G16_SNORM:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R16G16_SNORMTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R16G16_SINT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R16G16_SINTTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R16G16_UINT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R16G16_UINTTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R16G16_FLOAT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R16G16_FLOATTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::B10G11R11_FLOAT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<B10G11R11_FLOATTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R32_SINT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R32_SINTTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R32_UINT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R32_UINTTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R32_FLOAT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R32_FLOATTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::X8R8G8B8_UNORM:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<X8R8G8B8_UNORMTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::X8R8G8B8_SRGB:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<X8R8G8B8_SRGBTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R5G6B5_UNORM:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R5G6B5_UNORMTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::A1R5G5B5_UNORM:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<A1R5G5B5_UNORMTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R8G8_UNORM:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R8G8_UNORMTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R8G8_SNORM:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R8G8_SNORMTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R8G8_SINT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R8G8_SINTTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R8G8_UINT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R8G8_UINTTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R16_UNORM:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R16_UNORMTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R16_SNORM:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R16_SNORMTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R16_SINT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R16_SINTTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R16_UINT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R16_UINTTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R16_FLOAT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R16_FLOATTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R8_UNORM:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R8_UNORMTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R8_SNORM:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R8_SNORMTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R8_SINT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R8_SINTTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::R8_UINT:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<R8_UINTTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::X1R5G5B5_UNORM:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<X1R5G5B5_UNORMTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::X8B8G8R8_UNORM:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<X8B8G8R8_UNORMTraits>>())
            .first->second.get();
        break;
    case RenderTargetFormat::X8B8G8R8_SRGB:
        return impl->converters_cache
            .emplace(format, std::make_unique<ConverterImpl<X8B8G8R8_SRGBTraits>>())
            .first->second.get();
        break;
    default: {
        UNIMPLEMENTED_MSG("This format {} converter is not implemented", format);
        return impl->converters_cache.emplace(format, std::make_unique<NullConverter>())
            .first->second.get();
    }
    }
}

} // namespace Tegra::Engines::Blitter


code:
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later

#pragma once

#include <memory>
#include <span>

#include "common/common_types.h"

#include "video_core/gpu.h"

namespace Tegra::Engines::Blitter {

class Converter {
public:
    virtual void ConvertTo(std::span<const u8> input, std::span<f32> output) = 0;
    virtual void ConvertFrom(std::span<const f32> input, std::span<u8> output) = 0;
    virtual ~Converter() = default;
};

class ConverterFactory {
public:
    ConverterFactory();
    ~ConverterFactory();

    Converter* GetFormatConverter(RenderTargetFormat format);

private:
    Converter* BuildConverter(RenderTargetFormat format);

    struct ConverterFactoryImpl;
    std::unique_ptr<ConverterFactoryImpl> impl;
};

} // namespace Tegra::Engines::Blitter

repo_name: litucks/torzu

license: mit


code:
# SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
# SPDX-License-Identifier: GPL-3.0-or-later

import re

class Format:
    def __init__(self, string_value):
        self.name = string_value
        tmp = string_value.split('_')
        self.component_type = tmp[1]
        component_data = re.findall(r"\w\d+", tmp[0])
        self.num_components = len(component_data)
        sizes = []
        swizzle = []
        for data in component_data:
            swizzle.append(data[0])
            sizes.append(int(data[1:]))
        self.sizes = sizes
        self.swizzle = swizzle

    def build_component_type_array(self):
        result = "{ "
        b = False
        for i in range(0, self.num_components):
            if b:
                result += ", "
            b = True
            result += "ComponentType::" + self.component_type
        result += " }"
        return result

    def build_component_sizes_array(self):
        result = "{ "
        b = False
        for i in range(0, self.num_components):
            if b:
                result += ", "
            b = True
            result += str(self.sizes[i])
        result += " }"
        return result

    def build_component_swizzle_array(self):
        result = "{ "
        b = False
        for i in range(0, self.num_components):
            if b:
                result += ", "
            b = True
            swizzle = self.swizzle[i]
            if swizzle == "X":
                swizzle = "None"
            result += "Swizzle::" + swizzle
        result += " }"
        return result

    def print_declaration(self):
        print("struct " + self.name + "Traits {")
        print("  static constexpr size_t num_components = " + str(self.num_components) + ";")
        print("  static constexpr std::array<ComponentType, num_components> component_types = " + self.build_component_type_array() + ";")
        print("  static constexpr std::array<size_t, num_components> component_sizes = " + self.build_component_sizes_array() + ";")
        print("  static constexpr std::array<Swizzle, num_components> component_swizzle = " + self.build_component_swizzle_array() + ";")
        print("};\n")

    def print_case(self):
        print("case RenderTargetFormat::" + self.name + ":")
        print("  return impl->converters_cache")
        print("    .emplace(format, std::make_unique<ConverterImpl<" + self.name + "Traits>>())")
        print("    .first->second.get();")
        print("  break;")

txt = """
R32G32B32A32_FLOAT
R32G32B32A32_SINT
R32G32B32A32_UINT
R32G32B32X32_FLOAT
R32G32B32X32_SINT
R32G32B32X32_UINT
R16G16B16A16_UNORM
R16G16B16A16_SNORM
R16G16B16A16_SINT
R16G16B16A16_UINT
R16G16B16A16_FLOAT
R32G32_FLOAT
R32G32_SINT
R32G32_UINT
R16G16B16X16_FLOAT
A8R8G8B8_UNORM
A8R8G8B8_SRGB
A2B10G10R10_UNORM
A2B10G10R10_UINT
A2R10G10B10_UNORM
A8B8G8R8_UNORM
A8B8G8R8_SRGB
A8B8G8R8_SNORM
A8B8G8R8_SINT
A8B8G8R8_UINT
R16G16_UNORM
R16G16_SNORM
R16G16_SINT
R16G16_UINT
R16G16_FLOAT
B10G11R11_FLOAT
R32_SINT
R32_UINT
R32_FLOAT
X8R8G8B8_UNORM
X8R8G8B8_SRGB
R5G6B5_UNORM
A1R5G5B5_UNORM
R8G8_UNORM
R8G8_SNORM
R8G8_SINT
R8G8_UINT
R16_UNORM
R16_SNORM
R16_SINT
R16_UINT
R16_FLOAT
R8_UNORM
R8_SNORM
R8_SINT
R8_UINT
X1R5G5B5_UNORM
X8B8G8R8_UNORM
X8B8G8R8_SRGB
"""

x = txt.split()
y = list(map(lambda a: Format(a), x))
formats = list(y)
for format in formats:
  format.print_declaration()

for format in formats:
  format.print_case()


code:
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <algorithm>
#include <condition_variable>
#include <cstring>
#include <deque>
#include <functional>
#include <memory>
#include <mutex>
#include <thread>
#include <queue>

#include "common/common_types.h"
#include "common/microprofile.h"
#include "common/scope_exit.h"
#include "common/settings.h"
#include "common/thread.h"
#include "video_core/delayed_destruction_ring.h"
#include "video_core/gpu.h"
#include "video_core/host1x/host1x.h"
#include "video_core/host1x/syncpoint_manager.h"
#include "video_core/rasterizer_interface.h"

namespace VideoCommon {

class FenceBase {
public:
    explicit FenceBase(bool is_stubbed_) : is_stubbed{is_stubbed_} {}

    bool IsStubbed() const {
        return is_stubbed;
    }

protected:
    bool is_stubbed;
};

template <typename Traits>
class FenceManager {
    using TFence = typename Traits::FenceType;
    using TTextureCache = typename Traits::TextureCacheType;
    using TBufferCache = typename Traits::BufferCacheType;
    using TQueryCache = typename Traits::QueryCacheType;
    static constexpr bool can_async_check = Traits::HAS_ASYNC_CHECK;

public:
    /// Notify the fence manager about a new frame
    void TickFrame() {
        std::unique_lock lock(ring_guard);
        delayed_destruction_ring.Tick();
    }

    // Unlike other fences, this one doesn't
    void SignalOrdering() {
        if constexpr (!can_async_check) {
            TryReleasePendingFences<false>();
        }
        std::scoped_lock lock{buffer_cache.mutex};
        buffer_cache.AccumulateFlushes();
    }

    void SignalReference() {
        std::function<void()> do_nothing([] {});
        SignalFence(std::move(do_nothing));
    }

    void SyncOperation(std::function<void()>&& func) {
        uncommitted_operations.emplace_back(std::move(func));
    }

    void SignalFence(std::function<void()>&& func) {
        bool delay_fence = Settings::IsGPULevelHigh();
        if constexpr (!can_async_check) {
            TryReleasePendingFences<false>();
        }
        const bool should_flush = ShouldFlush();
        CommitAsyncFlushes();
        TFence new_fence = CreateFence(!should_flush);
        if constexpr (can_async_check) {
            guard.lock();
        }
        if (delay_fence) {
            uncommitted_operations.emplace_back(std::move(func));
        }
        pending_operations.emplace_back(std::move(uncommitted_operations));
        QueueFence(new_fence);
        if (!delay_fence) {
            func();
        }
        fences.push(std::move(new_fence));
        if (should_flush) {
            rasterizer.FlushCommands();
        }
        if constexpr (can_async_check) {
            guard.unlock();
            cv.notify_all();
        }
        rasterizer.InvalidateGPUCache();
    }

    void SignalSyncPoint(u32 value) {
        syncpoint_manager.IncrementGuest(value);
        std::function<void()> func([this, value] { syncpoint_manager.IncrementHost(value); });
        SignalFence(std::move(func));
    }

    void WaitPendingFences([[maybe_unused]] bool force) {
        if constexpr (!can_async_check) {
            TryReleasePendingFences<true>();
        } else {
            if (!force) {
                return;
            }
            std::mutex wait_mutex;
            std::condition_variable wait_cv;
            std::atomic<bool> wait_finished{};
            std::function<void()> func([&] {
                std::scoped_lock lk(wait_mutex);
                wait_finished.store(true, std::memory_order_relaxed);
                wait_cv.notify_all();
            });
            SignalFence(std::move(func));
            std::unique_lock lk(wait_mutex);
            wait_cv.wait(
                lk, [&wait_finished] { return wait_finished.load(std::memory_order_relaxed); });
        }
    }

protected:
    explicit FenceManager(VideoCore::RasterizerInterface& rasterizer_, Tegra::GPU& gpu_,
                          TTextureCache& texture_cache_, TBufferCache& buffer_cache_,
                          TQueryCache& query_cache_)
        : rasterizer{rasterizer_}, gpu{gpu_}, syncpoint_manager{gpu.Host1x().GetSyncpointManager()},
          texture_cache{texture_cache_}, buffer_cache{buffer_cache_}, query_cache{query_cache_} {
        if constexpr (can_async_check) {
            fence_thread =
                std::jthread([this](std::stop_token token) { ReleaseThreadFunc(token); });
        }
    }

    virtual ~FenceManager() {
        if constexpr (can_async_check) {
            fence_thread.request_stop();
            cv.notify_all();
            fence_thread.join();
        }
    }

    /// Creates a Fence Interface, does not create a backend fence if 'is_stubbed' is
    /// true
    virtual TFence CreateFence(bool is_stubbed) = 0;
    /// Queues a fence into the backend if the fence isn't stubbed.
    virtual void QueueFence(TFence& fence) = 0;
    /// Notifies that the backend fence has been signaled/reached in host GPU.
    virtual bool IsFenceSignaled(TFence& fence) const = 0;
    /// Waits until a fence has been signalled by the host GPU.
    virtual void WaitFence(TFence& fence) = 0;

    VideoCore::RasterizerInterface& rasterizer;
    Tegra::GPU& gpu;
    Tegra::Host1x::SyncpointManager& syncpoint_manager;
    TTextureCache& texture_cache;
    TBufferCache& buffer_cache;
    TQueryCache& query_cache;

private:
    template <bool force_wait>
    void TryReleasePendingFences() {
        while (!fences.empty()) {
            TFence& current_fence = fences.front();
            if (ShouldWait() && !IsFenceSignaled(current_fence)) {
                if constexpr (force_wait) {
                    WaitFence(current_fence);
                } else {
                    return;
                }
            }
            PopAsyncFlushes();
            auto operations = std::move(pending_operations.front());
            pending_operations.pop_front();
            for (auto& operation : operations) {
                operation();
            }
            {
                std::unique_lock lock(ring_guard);
                delayed_destruction_ring.Push(std::move(current_fence));
            }
            fences.pop();
        }
    }

    void ReleaseThreadFunc(std::stop_token stop_token) {
        std::string name = "GPUFencingThread";
        MicroProfileOnThreadCreate(name.c_str());

        // Cleanup
        SCOPE_EXIT {
            MicroProfileOnThreadExit();
        };

        Common::SetCurrentThreadName(name.c_str());
        Common::SetCurrentThreadPriority(Common::ThreadPriority::High);

        TFence current_fence;
        std::deque<std::function<void()>> current_operations;
        while (!stop_token.stop_requested()) {
            {
                std::unique_lock lock(guard);
                cv.wait(lock, [&] { return stop_token.stop_requested() || !fences.empty(); });
                if (stop_token.stop_requested()) [[unlikely]] {
                    return;
                }
                current_fence = std::move(fences.front());
                current_operations = std::move(pending_operations.front());
                fences.pop();
                pending_operations.pop_front();
            }
            if (!current_fence->IsStubbed()) {
                WaitFence(current_fence);
            }
            PopAsyncFlushes();
            for (auto& operation : current_operations) {
                operation();
            }
            {
                std::unique_lock lock(ring_guard);
                delayed_destruction_ring.Push(std::move(current_fence));
            }
        }
    }

    bool ShouldWait() const {
        std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
        return texture_cache.ShouldWaitAsyncFlushes() || buffer_cache.ShouldWaitAsyncFlushes() ||
               query_cache.ShouldWaitAsyncFlushes();
    }

    bool ShouldFlush() const {
        std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
        return texture_cache.HasUncommittedFlushes() || buffer_cache.HasUncommittedFlushes() ||
               query_cache.HasUncommittedFlushes();
    }

    void PopAsyncFlushes() {
        {
            std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
            texture_cache.PopAsyncFlushes();
            buffer_cache.PopAsyncFlushes();
        }
        query_cache.PopAsyncFlushes();
    }

    void CommitAsyncFlushes() {
        {
            std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
            texture_cache.CommitAsyncFlushes();
            buffer_cache.CommitAsyncFlushes();
        }
        query_cache.CommitAsyncFlushes();
    }

    std::queue<TFence> fences;
    std::deque<std::function<void()>> uncommitted_operations;
    std::deque<std::deque<std::function<void()>>> pending_operations;

    std::mutex guard;
    std::mutex ring_guard;
    std::condition_variable cv;

    std::jthread fence_thread;

    DelayedDestructionRing<TFence, 8> delayed_destruction_ring;
};

} // namespace VideoCommon


code:
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "common/assert.h"
#include "video_core/framebuffer_config.h"

namespace Tegra {

Common::Rectangle<f32> NormalizeCrop(const FramebufferConfig& framebuffer, u32 texture_width,
                                     u32 texture_height) {
    f32 left, top, right, bottom;

    if (!framebuffer.crop_rect.IsEmpty()) {
        // If crop rectangle is not empty, apply properties from rectangle.
        left = static_cast<f32>(framebuffer.crop_rect.left);
        top = static_cast<f32>(framebuffer.crop_rect.top);
        right = static_cast<f32>(framebuffer.crop_rect.right);
        bottom = static_cast<f32>(framebuffer.crop_rect.bottom);
    } else {
        // Otherwise, fall back to framebuffer dimensions.
        left = 0;
        top = 0;
        right = static_cast<f32>(framebuffer.width);
        bottom = static_cast<f32>(framebuffer.height);
    }

    // Apply transformation flags.
    auto framebuffer_transform_flags = framebuffer.transform_flags;

    if (True(framebuffer_transform_flags & Service::android::BufferTransformFlags::FlipH)) {
        // Switch left and right.
        std::swap(left, right);
    }
    if (True(framebuffer_transform_flags & Service::android::BufferTransformFlags::FlipV)) {
        // Switch top and bottom.
        std::swap(top, bottom);
    }

    framebuffer_transform_flags &= ~Service::android::BufferTransformFlags::FlipH;
    framebuffer_transform_flags &= ~Service::android::BufferTransformFlags::FlipV;
    if (True(framebuffer_transform_flags)) {
        UNIMPLEMENTED_MSG("Unsupported framebuffer_transform_flags={}",
                          static_cast<u32>(framebuffer_transform_flags));
    }

    // Normalize coordinate space.
    left /= static_cast<f32>(texture_width);
    top /= static_cast<f32>(texture_height);
    right /= static_cast<f32>(texture_width);
    bottom /= static_cast<f32>(texture_height);

    return Common::Rectangle<f32>(left, top, right, bottom);
}

} // namespace Tegra


code:
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include "common/common_types.h"
#include "common/math_util.h"
#include "core/hle/service/nvnflinger/buffer_transform_flags.h"
#include "core/hle/service/nvnflinger/pixel_format.h"
#include "core/hle/service/nvnflinger/ui/fence.h"

namespace Tegra {

enum class BlendMode {
    Opaque,
    Premultiplied,
    Coverage,
};

/**
 * Struct describing framebuffer configuration
 */
struct FramebufferConfig {
    DAddr address{};
    u32 offset{};
    u32 width{};
    u32 height{};
    u32 stride{};
    Service::android::PixelFormat pixel_format{};
    Service::android::BufferTransformFlags transform_flags{};
    Common::Rectangle<int> crop_rect{};
    BlendMode blending{};
};

Common::Rectangle<f32> NormalizeCrop(const FramebufferConfig& framebuffer, u32 texture_width,
                                     u32 texture_height);

} // namespace Tegra


code:
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include "common/bit_cast.h"
#include "common/common_types.h"

namespace FSR {
// Reimplementations of the constant generating functions in ffx_fsr1.h
// GCC generated a lot of warnings when using the official header.
void FsrEasuConOffset(u32 con0[4], u32 con1[4], u32 con2[4], u32 con3[4],
                      f32 inputViewportInPixelsX, f32 inputViewportInPixelsY,
                      f32 inputSizeInPixelsX, f32 inputSizeInPixelsY, f32 outputSizeInPixelsX,
                      f32 outputSizeInPixelsY, f32 inputOffsetInPixelsX, f32 inputOffsetInPixelsY);

void FsrRcasCon(u32* con, f32 sharpness);

} // namespace FSR


code:
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <list>
#include <memory>

#include "common/assert.h"
#include "common/microprofile.h"
#include "common/settings.h"
#include "core/core.h"
#include "core/core_timing.h"
#include "core/frontend/emu_window.h"
#include "core/frontend/graphics_context.h"
#include "core/hle/service/nvdrv/nvdata.h"
#include "core/perf_stats.h"
#include "video_core/cdma_pusher.h"
#include "video_core/control/channel_state.h"
#include "video_core/control/scheduler.h"
#include "video_core/dma_pusher.h"
#include "video_core/engines/fermi_2d.h"
#include "video_core/engines/kepler_compute.h"
#include "video_core/engines/kepler_memory.h"
#include "video_core/engines/maxwell_3d.h"
#include "video_core/engines/maxwell_dma.h"
#include "video_core/gpu.h"
#include "video_core/gpu_thread.h"
#include "video_core/host1x/host1x.h"
#include "video_core/host1x/syncpoint_manager.h"
#include "video_core/memory_manager.h"
#include "video_core/renderer_base.h"
#include "video_core/shader_notify.h"

namespace Tegra {

struct GPU::Impl {
    explicit Impl(GPU& gpu_, Core::System& system_, bool is_async_, bool use_nvdec_)
        : gpu{gpu_}, system{system_}, host1x{system.Host1x()}, use_nvdec{use_nvdec_},
          shader_notify{std::make_unique<VideoCore::ShaderNotify>()}, is_async{is_async_},
          gpu_thread{system_, is_async_}, scheduler{std::make_unique<Control::Scheduler>(gpu)} {}

    ~Impl() = default;

    std::shared_ptr<Control::ChannelState> CreateChannel(s32 channel_id) {
        auto channel_state = std::make_shared<Tegra::Control::ChannelState>(channel_id);
        channels.emplace(channel_id, channel_state);
        scheduler->DeclareChannel(channel_state);
        return channel_state;
    }

    void BindChannel(s32 channel_id) {
        if (bound_channel == channel_id) {
            return;
        }
        auto it = channels.find(channel_id);
        ASSERT(it != channels.end());
        bound_channel = channel_id;
        current_channel = it->second.get();

        rasterizer->BindChannel(*current_channel);
    }

    std::shared_ptr<Control::ChannelState> AllocateChannel() {
        return CreateChannel(new_channel_id++);
    }

    void InitChannel(Control::ChannelState& to_init, u64 program_id) {
        to_init.Init(system, gpu, program_id);
        to_init.BindRasterizer(rasterizer);
        rasterizer->InitializeChannel(to_init);
    }

    void InitAddressSpace(Tegra::MemoryManager& memory_manager) {
        memory_manager.BindRasterizer(rasterizer);
    }

    void ReleaseChannel(Control::ChannelState& to_release) {
        UNIMPLEMENTED();
    }

    /// Binds a renderer to the GPU.
    void BindRenderer(std::unique_ptr<VideoCore::RendererBase> renderer_) {
        renderer = std::move(renderer_);
        rasterizer = renderer->ReadRasterizer();
        host1x.MemoryManager().BindInterface(rasterizer);
        host1x.GMMU().BindRasterizer(rasterizer);
    }

    /// Flush all current written commands into the host GPU for execution.
    void FlushCommands() {
        rasterizer->FlushCommands();
    }

    /// Synchronizes CPU writes with Host GPU memory.
    void InvalidateGPUCache() {
        std::function<void(PAddr, size_t)> callback_writes(
            [this](PAddr address, size_t size) { rasterizer->OnCacheInvalidation(address, size); });
        system.GatherGPUDirtyMemory(callback_writes);
    }

    /// Signal the ending of command list.
    void OnCommandListEnd() {
        rasterizer->ReleaseFences(false);
        Settings::UpdateGPUAccuracy();
    }

    /// Request a host GPU memory flush from the CPU.
    template <typename Func>
    [[nodiscard]] u64 RequestSyncOperation(Func&& action) {
        std::unique_lock lck{sync_request_mutex};
        const u64 fence = ++last_sync_fence;
        sync_requests.emplace_back(action);
        return fence;
    }

    /// Obtains current flush request fence id.
    [[nodiscard]] u64 CurrentSyncRequestFence() const {
        return current_sync_fence.load(std::memory_order_relaxed);
    }

    void WaitForSyncOperation(const u64 fence) {
        std::unique_lock lck{sync_request_mutex};
        sync_request_cv.wait(lck, [this, fence] { return CurrentSyncRequestFence() >= fence; });
    }

    /// Tick pending requests within the GPU.
    void TickWork() {
        std::unique_lock lck{sync_request_mutex};
        while (!sync_requests.empty()) {
            auto request = std::move(sync_requests.front());
            sync_requests.pop_front();
            sync_request_mutex.unlock();
            request();
            current_sync_fence.fetch_add(1, std::memory_order_release);
            sync_request_mutex.lock();
            sync_request_cv.notify_all();
        }
    }

    /// Returns a reference to the Maxwell3D GPU engine.
    [[nodiscard]] Engines::Maxwell3D& Maxwell3D() {
        ASSERT(current_channel);
        return *current_channel->maxwell_3d;
    }

    /// Returns a const reference to the Maxwell3D GPU engine.
    [[nodiscard]] const Engines::Maxwell3D& Maxwell3D() const {
        ASSERT(current_channel);
        return *current_channel->maxwell_3d;
    }

    /// Returns a reference to the KeplerCompute GPU engine.
    [[nodiscard]] Engines::KeplerCompute& KeplerCompute() {
        ASSERT(current_channel);
        return *current_channel->kepler_compute;
    }

    /// Returns a reference to the KeplerCompute GPU engine.
    [[nodiscard]] const Engines::KeplerCompute& KeplerCompute() const {
        ASSERT(current_channel);
        return *current_channel->kepler_compute;
    }

    /// Returns a reference to the GPU DMA pusher.
    [[nodiscard]] Tegra::DmaPusher& DmaPusher() {
        ASSERT(current_channel);
        return *current_channel->dma_pusher;
    }

    /// Returns a const reference to the GPU DMA pusher.
    [[nodiscard]] const Tegra::DmaPusher& DmaPusher() const {
        ASSERT(current_channel);
        return *current_channel->dma_pusher;
    }

    /// Returns a reference to the underlying renderer.
    [[nodiscard]] VideoCore::RendererBase& Renderer() {
        return *renderer;
    }

    /// Returns a const reference to the underlying renderer.
    [[nodiscard]] const VideoCore::RendererBase& Renderer() const {
        return *renderer;
    }

    /// Returns a reference to the shader notifier.
    [[nodiscard]] VideoCore::ShaderNotify& ShaderNotify() {
        return *shader_notify;
    }

    /// Returns a const reference to the shader notifier.
    [[nodiscard]] const VideoCore::ShaderNotify& ShaderNotify() const {
        return *shader_notify;
    }

    [[nodiscard]] u64 GetTicks() const {
        u64 gpu_tick = system.CoreTiming().GetGPUTicks();

        if (Settings::values.use_fast_gpu_time.GetValue()) {
            gpu_tick /= 256;
        }

        return gpu_tick;
    }

    [[nodiscard]] bool IsAsync() const {
        return is_async;
    }

    [[nodiscard]] bool UseNvdec() const {
        return use_nvdec;
    }

    void RendererFrameEndNotify() {
        system.GetPerfStats().EndGameFrame();
    }

    /// Performs any additional setup necessary in order to begin GPU emulation.
    /// This can be used to launch any necessary threads and register any necessary
    /// core timing events.
    void Start() {
        Settings::UpdateGPUAccuracy();
        gpu_thread.StartThread(*renderer, renderer->Context(), *scheduler);
    }

    void NotifyShutdown() {
        std::unique_lock lk{sync_mutex};
        shutting_down.store(true, std::memory_order::relaxed);
        sync_cv.notify_all();
    }

    /// Obtain the CPU Context
    void ObtainContext() {
        if (!cpu_context) {
            cpu_context = renderer->GetRenderWindow().CreateSharedContext();
        }
        cpu_context->MakeCurrent();
    }

    /// Release the CPU Context
    void ReleaseContext() {
        cpu_context->DoneCurrent();
    }

    /// Push GPU command entries to be processed
    void PushGPUEntries(s32 channel, Tegra::CommandList&& entries) {
        gpu_thread.SubmitList(channel, std::move(entries));
    }

    /// Push GPU command buffer entries to be processed
    void PushCommandBuffer(u32 id, Tegra::ChCommandHeaderList& entries) {
        if (!use_nvdec) {
            return;
        }

        if (!cdma_pushers.contains(id)) {
            cdma_pushers.insert_or_assign(id, std::make_unique<Tegra::CDmaPusher>(host1x));
        }

        // SubmitCommandBuffer would make the nvdec operations async, this is not currently working
        // TODO(ameerj): RE proper async nvdec operation
        // gpu_thread.SubmitCommandBuffer(std::move(entries));
        cdma_pushers[id]->ProcessEntries(std::move(entries));
    }

    /// Frees the CDMAPusher instance to free up resources
    void ClearCdmaInstance(u32 id) {
        const auto iter = cdma_pushers.find(id);
        if (iter != cdma_pushers.end()) {
            cdma_pushers.erase(iter);
        }
    }

    /// Notify rasterizer that any caches of the specified region should be flushed to Switch memory
    void FlushRegion(DAddr addr, u64 size) {
        gpu_thread.FlushRegion(addr, size);
    }

    VideoCore::RasterizerDownloadArea OnCPURead(DAddr addr, u64 size) {
        auto raster_area = rasterizer->GetFlushArea(addr, size);
        if (raster_area.preemtive) {
            return raster_area;
        }
        raster_area.preemtive = true;
        const u64 fence = RequestSyncOperation([this, &raster_area]() {
            rasterizer->FlushRegion(raster_area.start_address,
                                    raster_area.end_address - raster_area.start_address);
        });
        gpu_thread.TickGPU();
        WaitForSyncOperation(fence);
        return raster_area;
    }

    /// Notify rasterizer that any caches of the specified region should be invalidated
    void InvalidateRegion(DAddr addr, u64 size) {
        gpu_thread.InvalidateRegion(addr, size);
    }

    bool OnCPUWrite(DAddr addr, u64 size) {
        return rasterizer->OnCPUWrite(addr, size);
    }

    /// Notify rasterizer that any caches of the specified region should be flushed and invalidated
    void FlushAndInvalidateRegion(DAddr addr, u64 size) {
        gpu_thread.FlushAndInvalidateRegion(addr, size);
    }

    void RequestComposite(std::vector<Tegra::FramebufferConfig>&& layers,
                          std::vector<Service::Nvidia::NvFence>&& fences) {
        size_t num_fences{fences.size()};
        size_t current_request_counter{};
        {
            std::unique_lock<std::mutex> lk(request_swap_mutex);
            if (free_swap_counters.empty()) {
                current_request_counter = request_swap_counters.size();
                request_swap_counters.emplace_back(num_fences);
            } else {
                current_request_counter = free_swap_counters.front();
                request_swap_counters[current_request_counter] = num_fences;
                free_swap_counters.pop_front();
            }
        }
        const auto wait_fence =
            RequestSyncOperation([this, current_request_counter, &layers, &fences, num_fences] {
                auto& syncpoint_manager = host1x.GetSyncpointManager();
                if (num_fences == 0) {
                    renderer->Composite(layers);
                }
                const auto executer = [this, current_request_counter, layers_copy = layers]() {
                    {
                        std::unique_lock<std::mutex> lk(request_swap_mutex);
                        if (--request_swap_counters[current_request_counter] != 0) {
                            return;
                        }
                        free_swap_counters.push_back(current_request_counter);
                    }
                    renderer->Composite(layers_copy);
                };
                for (size_t i = 0; i < num_fences; i++) {
                    syncpoint_manager.RegisterGuestAction(fences[i].id, fences[i].value, executer);
                }
            });
        gpu_thread.TickGPU();
        WaitForSyncOperation(wait_fence);
    }

    std::vector<u8> GetAppletCaptureBuffer() {
        std::vector<u8> out;

        const auto wait_fence =
            RequestSyncOperation([&] { out = renderer->GetAppletCaptureBuffer(); });
        gpu_thread.TickGPU();
        WaitForSyncOperation(wait_fence);

        return out;
    }

    GPU& gpu;
    Core::System& system;
    Host1x::Host1x& host1x;

    std::map<u32, std::unique_ptr<Tegra::CDmaPusher>> cdma_pushers;
    std::unique_ptr<VideoCore::RendererBase> renderer;
    VideoCore::RasterizerInterface* rasterizer = nullptr;
    const bool use_nvdec;

    s32 new_channel_id{1};
    /// Shader build notifier
    std::unique_ptr<VideoCore::ShaderNotify> shader_notify;
    /// When true, we are about to shut down emulation session, so terminate outstanding tasks
    std::atomic_bool shutting_down{};

    std::array<std::atomic<u32>, Service::Nvidia::MaxSyncPoints> syncpoints{};

    std::array<std::list<u32>, Service::Nvidia::MaxSyncPoints> syncpt_interrupts;

    std::mutex sync_mutex;
    std::mutex device_mutex;

    std::condition_variable sync_cv;

    std::list<std::function<void()>> sync_requests;
    std::atomic<u64> current_sync_fence{};
    u64 last_sync_fence{};
    std::mutex sync_request_mutex;
    std::condition_variable sync_request_cv;

    const bool is_async;

    VideoCommon::GPUThread::ThreadManager gpu_thread;
    std::unique_ptr<Core::Frontend::GraphicsContext> cpu_context;

    std::unique_ptr<Tegra::Control::Scheduler> scheduler;
    std::unordered_map<s32, std::shared_ptr<Tegra::Control::ChannelState>> channels;
    Tegra::Control::ChannelState* current_channel;
    s32 bound_channel{-1};

    std::deque<size_t> free_swap_counters;
    std::deque<size_t> request_swap_counters;
    std::mutex request_swap_mutex;
};

GPU::GPU(Core::System& system, bool is_async, bool use_nvdec)
    : impl{std::make_unique<Impl>(*this, system, is_async, use_nvdec)} {}

GPU::~GPU() = default;

std::shared_ptr<Control::ChannelState> GPU::AllocateChannel() {
    return impl->AllocateChannel();
}

void GPU::InitChannel(Control::ChannelState& to_init, u64 program_id) {
    impl->InitChannel(to_init, program_id);
}

void GPU::BindChannel(s32 channel_id) {
    impl->BindChannel(channel_id);
}

void GPU::ReleaseChannel(Control::ChannelState& to_release) {
    impl->ReleaseChannel(to_release);
}

void GPU::InitAddressSpace(Tegra::MemoryManager& memory_manager) {
    impl->InitAddressSpace(memory_manager);
}

void GPU::BindRenderer(std::unique_ptr<VideoCore::RendererBase> renderer) {
    impl->BindRenderer(std::move(renderer));
}

void GPU::FlushCommands() {
    impl->FlushCommands();
}

void GPU::InvalidateGPUCache() {
    impl->InvalidateGPUCache();
}

void GPU::OnCommandListEnd() {
    impl->OnCommandListEnd();
}

u64 GPU::RequestFlush(DAddr addr, std::size_t size) {
    return impl->RequestSyncOperation(
        [this, addr, size]() { impl->rasterizer->FlushRegion(addr, size); });
}

u64 GPU::CurrentSyncRequestFence() const {
    return impl->CurrentSyncRequestFence();
}

void GPU::WaitForSyncOperation(u64 fence) {
    return impl->WaitForSyncOperation(fence);
}

void GPU::TickWork() {
    impl->TickWork();
}

/// Gets a mutable reference to the Host1x interface
Host1x::Host1x& GPU::Host1x() {
    return impl->host1x;
}

/// Gets an immutable reference to the Host1x interface.
const Host1x::Host1x& GPU::Host1x() const {
    return impl->host1x;
}

Engines::Maxwell3D& GPU::Maxwell3D() {
    return impl->Maxwell3D();
}

const Engines::Maxwell3D& GPU::Maxwell3D() const {
    return impl->Maxwell3D();
}

Engines::KeplerCompute& GPU::KeplerCompute() {
    return impl->KeplerCompute();
}

const Engines::KeplerCompute& GPU::KeplerCompute() const {
    return impl->KeplerCompute();
}

Tegra::DmaPusher& GPU::DmaPusher() {
    return impl->DmaPusher();
}

const Tegra::DmaPusher& GPU::DmaPusher() const {
    return impl->DmaPusher();
}

VideoCore::RendererBase& GPU::Renderer() {
    return impl->Renderer();
}

const VideoCore::RendererBase& GPU::Renderer() const {
    return impl->Renderer();
}

VideoCore::ShaderNotify& GPU::ShaderNotify() {
    return impl->ShaderNotify();
}

const VideoCore::ShaderNotify& GPU::ShaderNotify() const {
    return impl->ShaderNotify();
}

void GPU::RequestComposite(std::vector<Tegra::FramebufferConfig>&& layers,
                           std::vector<Service::Nvidia::NvFence>&& fences) {
    impl->RequestComposite(std::move(layers), std::move(fences));
}

std::vector<u8> GPU::GetAppletCaptureBuffer() {
    return impl->GetAppletCaptureBuffer();
}

u64 GPU::GetTicks() const {
    return impl->GetTicks();
}

bool GPU::IsAsync() const {
    return impl->IsAsync();
}

bool GPU::UseNvdec() const {
    return impl->UseNvdec();
}

void GPU::RendererFrameEndNotify() {
    impl->RendererFrameEndNotify();
}

void GPU::Start() {
    impl->Start();
}

void GPU::NotifyShutdown() {
    impl->NotifyShutdown();
}

void GPU::ObtainContext() {
    impl->ObtainContext();
}

void GPU::ReleaseContext() {
    impl->ReleaseContext();
}

void GPU::PushGPUEntries(s32 channel, Tegra::CommandList&& entries) {
    impl->PushGPUEntries(channel, std::move(entries));
}

void GPU::PushCommandBuffer(u32 id, Tegra::ChCommandHeaderList& entries) {
    impl->PushCommandBuffer(id, entries);
}

void GPU::ClearCdmaInstance(u32 id) {
    impl->ClearCdmaInstance(id);
}

VideoCore::RasterizerDownloadArea GPU::OnCPURead(PAddr addr, u64 size) {
    return impl->OnCPURead(addr, size);
}

void GPU::FlushRegion(DAddr addr, u64 size) {
    impl->FlushRegion(addr, size);
}

void GPU::InvalidateRegion(DAddr addr, u64 size) {
    impl->InvalidateRegion(addr, size);
}

bool GPU::OnCPUWrite(DAddr addr, u64 size) {
    return impl->OnCPUWrite(addr, size);
}

void GPU::FlushAndInvalidateRegion(DAddr addr, u64 size) {
    impl->FlushAndInvalidateRegion(addr, size);
}

} // namespace Tegra