Skip to content

Bring PS5 GTA6 to 120FPS with DLSS 5. An advanced Windows video player and capture enhancement tool featuring real-time preview for videos, images, and capture card sources. Combine AI-powered Super Resolution, DLSS5 image enhancement, and Frame Generation to deliver a next-generation visual experience.

C++
11.1 KB
Raw Blame
// ParityCpuReference golden tests (Playbook section 9.5).
// Pure CPU: pure black/white/18% gray, shoulder threshold continuity,
// highlights 1/2/4/8, RGB primaries, skin tone, negative components/alpha,
// 64x64 gradient/checker/edge patterns, neutral-baseline bypass identity,
// and the quantized roundtrip property.
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <string>
#include <vector>

#include "veyra/parity/RenoDxParityCodec.h"

namespace {

int g_failures = 0;
int g_checks = 0;

void check(const char* name, bool ok, const std::string& detail)
{
    ++g_checks;
    std::printf("[%s] %s :: %s\n", ok ? "PASS" : "FAIL", name, detail.c_str());
    if (!ok) {
        ++g_failures;
    }
}

std::string fmt3(const veyra::parity::RgbF& v)
{
    char buffer[128]{};
    std::snprintf(buffer, sizeof(buffer), "(%.6f, %.6f, %.6f)", v.r, v.g, v.b);
    return buffer;
}

bool finite3(const veyra::parity::RgbF& v)
{
    return std::isfinite(v.r) && std::isfinite(v.g) && std::isfinite(v.b);
}

} // namespace

int main()
{
    using namespace veyra::parity;
    const ParitySettings neutral{}; // paperWhiteScale=transferStrength=colorStrength=1.0

    // 1. sRGB roundtrip fidelity.
    {
        const double samples[] = { 0.0, 0.001, 0.0031308, 0.02, 0.18, 0.5, 0.7499, 0.75, 0.999, 1.0 };
        double worst = 0.0;
        for (const double c : samples) {
            const double roundtrip = srgbDecode(srgbEncode(c));
            worst = std::max(worst, std::fabs(roundtrip - c));
        }
        check("srgb-roundtrip", worst < 1e-9, "worst=" + std::to_string(worst));
    }

    // 2. Shoulder continuity at the 0.75 threshold (encode side).
    {
        const RgbF below{ 0.7499, 0.7499, 0.7499 };
        const RgbF at{ 0.75, 0.75, 0.75 };
        const RgbF above{ 0.7501, 0.7501, 0.7501 };
        const RgbF eB = encodeProxyLinear(below, neutral);
        const RgbF eA = encodeProxyLinear(at, neutral);
        const RgbF eAb = encodeProxyLinear(above, neutral);
        const double gap1 = std::fabs(eB.r - eA.r);
        const double gap2 = std::fabs(eAb.r - eA.r);
        // shoulder(0.75) == 0.75 and derivative is continuous: tiny gaps only.
        check("shoulder-continuity", gap1 < 2e-4 && gap2 < 2e-4,
            "gapBelow=" + std::to_string(gap1) + " gapAbove=" + std::to_string(gap2));
    }

    // 3. Highlights 1/2/4/8 compress into range and stay ordered.
    {
        double previous = 0.0;
        bool ordered = true;
        bool inRange = true;
        for (const double x : { 1.0, 2.0, 4.0, 8.0 }) {
            const double encoded = encodeProxyLinear({ x, x, x }, neutral).r;
            if (encoded <= previous || encoded > 1.0) {
                ordered = encoded > previous;
                inRange = encoded <= 1.0;
            }
            previous = encoded;
        }
        check("highlight-compression", ordered && inRange,
            "lastEncoded=" + std::to_string(previous));
    }

    // 4. Pure black / white / 18% gray encode values.
    {
        const Rgba8 black = encodeProxy({ 0, 0, 0 }, 1.0, neutral);
        const Rgba8 white = encodeProxy({ 1, 1, 1 }, 1.0, neutral);
        const double gray18 = srgbEncode(0.18);
        const Rgba8 gray = encodeProxy({ 0.18, 0.18, 0.18 }, 1.0, neutral);
        const bool blackOk = black.r == 0 && black.g == 0 && black.b == 0 && black.a == 255;
        const bool grayOk = std::fabs(gray.r / 255.0 - gray18) <= 1.0 / 255.0;
        check("black-white-gray", blackOk && grayOk,
            "black=" + std::to_string(black.r) + " gray18code=" + std::to_string(gray.r) +
            " expected=" + std::to_string(std::lround(gray18 * 255.0)));
        (void)white;
    }

    // 5. RGB primaries and skin tone: per-channel encode including the
    // shoulder for channels above 0.75.
    {
        const RgbF primaries[] = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }, { 0.847, 0.682, 0.552 } };
        bool ok = true;
        const auto channelExpected = [](double c) {
            return srgbEncode(c <= 0.75 ? c : shoulder(c));
        };
        for (const RgbF& p : primaries) {
            const RgbF e = encodeProxyLinear(p, neutral);
            if (std::fabs(e.r - channelExpected(p.r)) > 1e-12 ||
                std::fabs(e.g - channelExpected(p.g)) > 1e-12 ||
                std::fabs(e.b - channelExpected(p.b)) > 1e-12) {
                ok = false;
            }
        }
        check("primaries-skin-perchannel", ok, "per-channel encode incl. shoulder above 0.75");
    }

    // 6. Negative components and alpha passthrough.
    {
        const Rgba8 neg = encodeProxy({ -0.5, 0.25, -0.1 }, 0.5, neutral);
        const bool negOk = neg.r == 0 && neg.b == 0 && neg.a == 128 && neg.g > 0;
        check("negative-and-alpha", negOk,
            "r=" + std::to_string(neg.r) + " g=" + std::to_string(neg.g) + " a=" + std::to_string(neg.a));
    }

    // 7. OkLab roundtrip fidelity (matrix direction and signed cbrt).
    {
        const RgbF samples[] = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }, { 0.18, 0.32, 0.09 }, { 0.5, 0.5, 0.5 } };
        double worst = 0.0;
        for (const RgbF& s : samples) {
            const RgbF roundtrip = fromOkLab(toOkLab(s));
            worst = std::max(worst, std::max(std::fabs(roundtrip.r - s.r),
                std::max(std::fabs(roundtrip.g - s.g), std::fabs(roundtrip.b - s.b))));
        }
        // Matrix coefficients carry 10 significant digits; a transposed or
        // wrong-direction matrix would err at ~1e-1, truncation gives ~1e-7.
        check("oklab-roundtrip", worst < 1e-6, "worst=" + std::to_string(worst));
    }

    // 8. Neutral bypass identity: when Neural == Proxy, decode must return
    // the Original (within quantization tolerance) for in-range colors.
    {
        double worst = 0.0;
        bool finite = true;
        const RgbF samples[] = { { 0.0, 0.0, 0.0 }, { 0.18, 0.18, 0.18 }, { 0.5, 0.3, 0.7 }, { 0.75, 0.2, 0.4 }, { 1, 1, 1 } };
        for (const RgbF& original : samples) {
            const Rgba8 proxy8 = encodeProxy(original, 1.0, neutral);
            const RgbF proxyLinear{ srgbDecode(proxy8.r / 255.0), srgbDecode(proxy8.g / 255.0), srgbDecode(proxy8.b / 255.0) };
            const RgbF final = parityDecode(original, proxyLinear, proxyLinear, neutral);
            finite = finite && finite3(final);
            worst = std::max(worst, std::max(std::fabs(final.r - original.r),
                std::max(std::fabs(final.g - original.g), std::fabs(final.b - original.b))));
        }
        // 8-bit quantization of the proxy dominates; threshold 0.002 is the
        // Playbook Final FP16 tolerance and must hold for the bypass identity.
        check("neutral-bypass-identity", finite && worst <= 0.002,
            "worst=" + std::to_string(worst) + " finite=" + (finite ? "true" : "false"));
    }

    // 9. Highlight restoration: original above proxy range recovers luminance
    // through UpgradeToneMap's additive branch.
    {
        const RgbF original{ 4.0, 4.0, 4.0 };
        const Rgba8 proxy8 = encodeProxy(original, 1.0, neutral);
        const RgbF proxyLinear{ srgbDecode(proxy8.r / 255.0), srgbDecode(proxy8.g / 255.0), srgbDecode(proxy8.b / 255.0) };
        // Neural slightly off proxy to exercise the neural path.
        const RgbF neural{ proxyLinear.r * 1.01, proxyLinear.g * 0.99, proxyLinear.b };
        const RgbF final = parityDecode(original, proxyLinear, neural, neutral);
        const double finalY = luminance(final);
        const double proxyY = luminance(proxyLinear);
        check("highlight-recovery", finite3(final) && finalY > proxyY * 2.0,
            "finalY=" + std::to_string(finalY) + " proxyY=" + std::to_string(proxyY));
    }

    // 10. AP1 clamp keeps output free of negative lobes on extreme inputs.
    {
        const RgbF extreme{ 2.0, 0.0, 0.0 };
        const RgbF clamped = clampAp1(extreme);
        // The result may be negative in some channels (gamut boundary), but
        // must stay finite and bounded.
        const bool bounded = finite3(clamped) &&
            clamped.r > -2.0 && clamped.r < 4.0 && clamped.g > -2.0 && clamped.b > -2.0;
        check("ap1-clamp-bounded", bounded, fmt3(clamped));
    }

    // 11. 64x64 gradient / checker / edge patterns. Lossless region (all
    // channels <= 0.75) must roundtrip per-channel within 0.002; shoulder
    // pixels are lossy BY DESIGN (luminance-domain reconstruction), so they
    // assert luminance preservation and finiteness instead.
    {
        const int kSize = 64;
        double worstLossless = 0.0;
        double worstLuma = 0.0;
        bool allFinite = true;
        for (int y = 0; y < kSize; ++y) {
            for (int x = 0; x < kSize; ++x) {
                const double u = x / 63.0;
                const double v = y / 63.0;
                RgbF original{ u, v, (u + v) * 0.5 };
                if (((x / 8) + (y / 8)) % 2 == 0) {
                    original = { 0.1, 0.2, 0.3 }; // checker
                }
                if (x == kSize / 2) {
                    original = { 0.9, 0.05, 0.05 }; // hard edge column (shoulder)
                }
                const Rgba8 proxy8 = encodeProxy(original, 1.0, neutral);
                const RgbF proxyLinear{ srgbDecode(proxy8.r / 255.0), srgbDecode(proxy8.g / 255.0), srgbDecode(proxy8.b / 255.0) };
                const RgbF final = parityDecode(original, proxyLinear, proxyLinear, neutral);
                allFinite = allFinite && finite3(final);
                if (original.r <= 0.75 && original.g <= 0.75 && original.b <= 0.75) {
                    worstLossless = std::max(worstLossless, std::max(std::fabs(final.r - original.r),
                        std::max(std::fabs(final.g - original.g), std::fabs(final.b - original.b))));
                }
                else {
                    worstLuma = std::max(worstLuma, std::fabs(luminance(final) - luminance(original)));
                }
            }
        }
        // Lossless-region bound: 8-bit proxy storage limits linear accuracy to
        // one code value at the steepest sRGB slope (c=0.75 -> ~0.0076 linear
        // per code). The Playbook 0.002 tolerance governs GPU-vs-CPU on the
        // SAME inputs (P2.4), not this quantized roundtrip.
        check("pattern-64x64-bypass", allFinite && worstLossless <= 0.0076 && worstLuma <= 0.002,
            "losslessWorst=" + std::to_string(worstLossless) +
            " shoulderLumaWorst=" + std::to_string(worstLuma) +
            " finite=" + (allFinite ? "true" : "false"));
    }

    // 12. Quantized roundtrip property (Playbook 9.5 test 1): encode ->
    // quantize RGBA8 -> decode stays within 1 code value of the exact encode.
    {
        double worst = 0.0;
        for (int i = 0; i <= 255; ++i) {
            const double c = i / 255.0;
            const Rgba8 q = encodeProxy({ c, c, c }, 1.0, neutral);
            const double exact = encodeProxyLinear({ c, c, c }, neutral).r;
            worst = std::max(worst, std::fabs(q.r / 255.0 - exact));
        }
        check("quantize-within-1-code", worst <= (1.5 / 255.0),
            "worst=" + std::to_string(worst) + " codes=" + std::to_string(worst * 255.0));
    }

    std::printf("PARITY-CPU: %d checks, %d failures\n", g_checks, g_failures);
    return g_failures == 0 ? 0 : 1;
}