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++
16.0 KB
Raw Blame
// PR19 integration: real graph -> VideoPresenter -> native DXGI readback.
// Readback/waits are diagnostic-only. This measures pixels, not panel light.
#include "veyra/pipeline/EnhanceGraph.h"
#include "veyra/engine/VideoPresenter.h"
#include "veyra/engine/EnhancementSettings.h"
#include "veyra/gfx/D3D12DeviceContext.h"
#include "veyra/gfx/CommandSlotRing.h"
#include "veyra/RuntimePaths.h"
#include <DirectXPackedVector.h>
#include <iostream>
#include <array>
#include <thread>
#include <d3d12sdklayers.h>
#include <cmath>
#include <string_view>
extern "C" {
#include <libavutil/frame.h>
}
#include "CaptureFormatGpuCases.h"
#include "HdrNativeRoundTripCases.h"

using namespace veyra;
// Optional runtime-backed regression: an SDR source converted by TrueHDR must
// keep the display curve, even though graph.hdrInput() is false. The regular
// native HDR suite below remains independent of proprietary runtimes.
static int videoHdrPreview(gfx::D3D12DeviceContext& ctx,gfx::CommandSlotRing& ring){
    pipeline::EnhanceGraph graph(ctx,ring);pipeline::EnhanceGraphDesc gd;
    gd.sourceWidth=gd.workWidth=gd.nrWidth=1280;gd.sourceHeight=gd.workHeight=gd.nrHeight=720;
    gd.rgbInput=true;gd.enableNr=gd.enableSr=gd.enableFg=gd.enableNvofStandalone=false;
    gd.videoHdr.enabled=true;gd.hdrOutput=true;gd.hdrOutputMode=engine::HdrOutputMode::ScRgb;
    gd.runtimeAbsPath=runtime::localRuntimeDirectory().wstring();
    AVFrame* frame=av_frame_alloc();if(!frame)return 2;
    frame->format=AV_PIX_FMT_BGRA;frame->width=1280;frame->height=720;
    frame->color_range=AVCOL_RANGE_JPEG;frame->color_primaries=AVCOL_PRI_BT709;frame->color_trc=AVCOL_TRC_IEC61966_2_1;
    bool ok=av_frame_get_buffer(frame,32)>=0;
    if(ok)for(int y=0;y<720;++y)for(int x=0;x<1280;++x){auto* p=frame->data[0]+y*frame->linesize[0]+x*4;p[0]=p[1]=p[2]=uint8_t(x*255/1279);p[3]=255;}
    pipeline::EnhanceGraph::FrameOutputs out;std::vector<float> raw,shown;
    ok=ok&&graph.initialize(gd)&&graph.createViews()&&graph.videoHdrActive()&&!graph.hdrInput()&&
        graph.process(frame,0,true,out,1)&&hdrRoundTrip::read(ctx,ring,graph.videoFrameResource(out.videoSlot),raw);
    HWND window=CreateWindowExW(0,L"STATIC",L"SDR to HDR display curve regression",WS_POPUP,0,0,1280,720,nullptr,nullptr,GetModuleHandleW(nullptr),nullptr);
    engine::VideoPresenter presenter;
    const auto present=[&](){
        bool pass=presenter.present(ctx,ring,graph,out.videoSlot,false,true,0,false,.5f,{1,1,1});
        auto buffer=presenter.presentedResourceForTest();return pass&&hdrRoundTrip::read(ctx,ring,buffer.Get(),shown);
    };
    ok=ok&&window&&presenter.open(ctx,window,graph)&&present()&&shown==raw;
    engine::EnhancementSettings settings;settings.nr=settings.sr=false;settings.multiplier=1;
    settings.videoHdr=gd.videoHdr;settings.hdrOutputMode=gd.hdrOutputMode;settings.hdrCurve.enabled=true;
    settings.hdrCurve.highlightStartNits=200;settings.hdrCurve.peakCapNits=400;settings.hdrCurve.displayPeakNits=1000;
    ok=ok&&graph.applySettings(settings)&&present()&&shown.size()==raw.size();
    double maxError=0,meanChange=0;
    if(ok)for(size_t i=0;i<raw.size();++i){
        const double before=raw[i]*80,after=shown[i]*80;
        const double expected=before<=200?before:200+200*(before-200)/(200+before-200);
        if(expected>=1)maxError=std::max(maxError,std::abs(after-expected)/expected);
        meanChange+=std::abs(after-before);
    }
    if(ok){meanChange/=raw.size();ok=maxError<.015&&meanChange>1;}
    presenter.observeHdrCoverage(true);ok=ok&&present()&&present()&&presenter.liveCurveCoverage().valid;
    settings.hdrCurve.strengthPercent=0;
    ok=ok&&graph.applySettings(settings)&&present()&&shown==raw;
    std::cout<<"VIDEO_HDR_DISPLAY_CURVE pass="<<ok<<" maxRelative="<<maxError<<" meanChangeNits="<<meanChange
        <<" sourceHdr="<<graph.hdrInput()<<" videoHdrActive="<<graph.videoHdrActive()<<std::endl;
    ring.drainQueue();presenter.close();out={};graph.shutdown();av_frame_free(&frame);if(window)DestroyWindow(window);
    return ok?0:1;
}
int wmain(int argc,wchar_t** argv){
    CoInitializeEx(nullptr,COINIT_MULTITHREADED);
    gfx::D3D12DeviceContext ctx;gfx::CommandSlotRing ring;Status status;
    gfx::DeviceContextDesc dd;dd.enableDebugLayer=true;
    if(!ctx.initialize(dd,status)||!ring.initialize(ctx.device(),ctx.directQueue(),ctx.fence(),ctx.fenceEvent(),4,status))return 2;
    if(argc==2&&std::wstring_view(argv[1])==L"--video-hdr")return videoHdrPreview(ctx,ring);
    int failures=0;
    const auto check=[&](bool ok,const char* label){std::cout<<(ok?"PASS ":"FAIL ")<<label<<std::endl;failures+=!ok;};
    constexpr std::array<double,16> patches={0,.005,.05,.5,1,2,5,10,20,50,100,203,400,1000,4000,10000};
    AVFrame* frame=av_frame_alloc();frame->width=64;frame->height=16;frame->format=AV_PIX_FMT_YUV420P10LE;
    frame->color_range=AVCOL_RANGE_MPEG;frame->colorspace=AVCOL_SPC_BT2020_NCL;
    frame->color_primaries=AVCOL_PRI_BT2020;frame->color_trc=AVCOL_TRC_SMPTE2084;
    if(av_frame_get_buffer(frame,32)<0)return 2;
    for(unsigned y=0;y<16;++y)for(unsigned x=0;x<64;++x){
        reinterpret_cast<uint16_t*>(frame->data[0]+y*frame->linesize[0])[x]=uint16_t(std::lround(64+876*hdrRoundTrip::pq(patches[x/4])));
        if(!(y%2)&&!(x%2))for(unsigned c=1;c<3;++c)reinterpret_cast<uint16_t*>(frame->data[c]+y/2*frame->linesize[c])[x/2]=512;
    }
    for(bool packed:{false,true}){
        pipeline::EnhanceGraph graph(ctx,ring);pipeline::EnhanceGraphDesc gd;
        gd.sourceWidth=gd.workWidth=64;gd.sourceHeight=gd.workHeight=16;
        gd.hdrInput=gd.hdrOutput=true;gd.noFeatures=gd.noNgx=true;gd.enableNr=gd.enableSr=gd.enableFg=false;
        gd.hdrOutputMode=packed?engine::HdrOutputMode::Hdr10:engine::HdrOutputMode::ScRgb;
        gd.hdrCurve.displayPeakNits=1000;
        pipeline::EnhanceGraph::FrameOutputs output;std::vector<float> raw,shown;
        bool ready=graph.initialize(gd)&&graph.createViews()&&graph.process(frame,0,true,output,1)&&hdrRoundTrip::read(ctx,ring,graph.videoFrameResource(output.videoSlot),raw);
        HWND window=CreateWindowExW(0,L"STATIC",L"PR19 HDR output diagnostic",WS_POPUP,0,0,64,16,nullptr,nullptr,GetModuleHandleW(nullptr),nullptr);
        engine::VideoPresenter presenter;
        ready=ready&&window&&presenter.open(ctx,window,graph);
        uint64_t epoch=1;
        const auto present=[&](int comparison=0,bool retain=false){
            bool ok=presenter.present(ctx,ring,graph,output.videoSlot,false,true,comparison,false,.5f,{epoch,1,1},{},-1,retain);
            auto buffer=presenter.presentedResourceForTest();
            ok=ok&&hdrRoundTrip::read(ctx,ring,buffer.Get(),shown);buffer.Reset();return ok;
        };
        check(ready&&present(0,true)&&shown==raw,packed?"HDR10 disabled curve: original PQ pixels exact":"scRGB disabled curve: original signed pixels exact");
        const auto idleCount=presenter.submittedCount();
        check(present(0,true)&&presenter.submittedCount()==idleCount,"unchanged paused view reuses its buffer");
        engine::EnhancementSettings settings;settings.hdrOutputMode=gd.hdrOutputMode;
        settings.hdrCurve=gd.hdrCurve;settings.hdrCurve.enabled=true;
        settings.hdrCurve.highlightStartNits=200;settings.hdrCurve.peakCapNits=400;
        ready=ready&&graph.applySettings(settings)&&present(0,true);
        check(ready&&presenter.submittedCount()>idleCount,"HDR uniform edits invalidate the paused display cache");
        check(!presenter.liveCurveCoverage().valid,"HDR coverage is silent before a panel requests it");
        presenter.observeHdrCoverage(true);
        check(present(),"visible HDR panel records a statistics probe");
        check(present(),"statistics collect without a CPU fence wait");
        const auto coverage=presenter.liveCurveCoverage();
        check(coverage.valid&&coverage.samples==2304&&coverage.inRollOff>0,"HDR10/scRGB statistics report a completed sparse sample");
        check(coverage.aboveCeiling==0,"soft highlight knee is not misreported as hard clipping");
        // A true black frame must be a valid zero reading, not a copy error.
        auto savedOutput=std::move(output);AVFrame* black=av_frame_clone(frame);
        if(black&&av_frame_make_writable(black)>=0){
            for(int y=0;y<black->height;++y)for(int x=0;x<black->width;++x)reinterpret_cast<uint16_t*>(black->data[0]+y*black->linesize[0])[x]=64;
            check(graph.process(black,10,false,output,2),"black HDR source enters the real graph");
            std::this_thread::sleep_for(std::chrono::milliseconds(260));
            check(present()&&present(),"black HDR probe completes");
            const auto zero=presenter.liveCurveCoverage();
            check(zero.valid&&zero.samples==2304&&zero.aboveCeiling==0&&zero.inRollOff==0&&zero.inLift==0,"black HDR image reports valid zero statistics");
            ++epoch;check(present()&&!presenter.liveCurveCoverage().valid,"source epoch invalidates old HDR statistics");
        }else check(false,"allocate black diagnostic frame");
        av_frame_free(&black);output=std::move(savedOutput);epoch=1;
        presenter.observeHdrCoverage(false);check(!presenter.liveCurveCoverage().valid,"hiding HDR panel clears its statistics");
        check(present(),"restore grayscale presentation after black-frame fixture");
        double maxRelative=0;bool monotonic=true;double previous=0;
        if(ready)for(unsigned i=0;i<patches.size();++i){
            const auto at=size_t(i*4+2)*3;
            const double before=packed?hdrRoundTrip::nits(raw[at]):raw[at]*80;
            const double after=packed?hdrRoundTrip::nits(shown[at]):shown[at]*80;
            // Independent expected knee, expressed in absolute nits from the
            // actual graph output (including its input/output quantization).
            const double expected=before<=200?before:200+200*(before-200)/(200+(before-200));
            if(expected>=1)maxRelative=std::max(maxRelative,std::abs(after-expected)/expected);
            monotonic=monotonic&&after+.001>=previous&&std::isfinite(after)&&after<=402;previous=after;
        }
        std::cout<<"CURVE_EVIDENCE packed="<<packed<<" maxRelative="<<maxRelative<<" monotonic="<<monotonic<<std::endl;
        check(ready&&maxRelative<.018&&monotonic,packed?"HDR10 curve: PQ decoded to nits, knee and re-encode":"scRGB curve: linear nits knee and peak bound");
        std::vector<float> untouched;
        check(hdrRoundTrip::read(ctx,ring,graph.videoFrameResource(output.videoSlot),untouched)&&untouched==raw,"preview tuning leaves graph/export surface byte-equivalent");
        settings.hdrCurve.strengthPercent=0;
        check(graph.applySettings(settings)&&present()&&shown==raw,"live zero strength restores exact identity without rebuilding");
        settings.hdrCurve.strengthPercent=100;
        check(graph.applySettings(settings)&&present(1),"linear comparison reference presents through the same curve (no PQ double decode)");
        // Reopening the same presenter instance must reset metadata and monitor
        // caches; a new graph/source never inherits the previous clip's state.
        ring.drainQueue();presenter.close();
        check(presenter.open(ctx,window,graph)&&present(),"same presenter reopen retains working HDR output");
        if(packed){
            // Exercise the live DXGI hint through ResizeBuffers, then disable
            // before another enabled render. The runner checks accepted hints
            // are followed by a clear call; unsupported hints remain optional.
            std::cout<<"METADATA_RESIZE_BEGIN"<<std::endl;
            settings.hdrMetadata.enabled=true;
            check(graph.applySettings(settings)&&present(),"optional HDR metadata does not block presentation");
            SetWindowPos(window,nullptr,0,0,96,16,SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE);
            Sleep(120); // Native resize coalescing; diagnostic-only.
            check(present(),"HDR window resizes with metadata active");
            {const auto buffer=presenter.presentedResourceForTest();check(buffer&&buffer->GetDesc().Width==96,"resize exercised real DXGI buffers");}
            settings.hdrMetadata.enabled=false;
            check(graph.applySettings(settings)&&present(),"disable metadata immediately after resize");
            std::cout<<"METADATA_RESIZE_END"<<std::endl;
            SetWindowPos(window,nullptr,0,0,64,16,SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE);
            Sleep(120);
            check(present(),"restore diagnostic window extent");
        }
        // BT.2020 primaries decoded into scRGB contain negative components.
        // A curve must scale signed RGB uniformly instead of changing hue.
        constexpr std::array<std::array<double,3>,4> colors={{{1000,0,0},{0,1000,0},{0,0,1000},{400,100,20}}};
        for(unsigned y=0;y<16;++y)for(unsigned x=0;x<64;++x){
            const auto& c=colors[x/16];
            const double r=hdrRoundTrip::pq(c[0]),g=hdrRoundTrip::pq(c[1]),b=hdrRoundTrip::pq(c[2]);
            const double yy=.2627*r+.678*g+.0593*b;
            reinterpret_cast<uint16_t*>(frame->data[0]+y*frame->linesize[0])[x]=uint16_t(std::lround(64+876*yy));
            if(!(y%2)&&!(x%2)){
                reinterpret_cast<uint16_t*>(frame->data[1]+y/2*frame->linesize[1])[x/2]=uint16_t(std::lround(512+896*(b-yy)/1.8814));
                reinterpret_cast<uint16_t*>(frame->data[2]+y/2*frame->linesize[2])[x/2]=uint16_t(std::lround(512+896*(r-yy)/1.4746));
            }
        }
        output={};ring.drainQueue();
        ready=graph.process(frame,16,true,output,2)&&hdrRoundTrip::read(ctx,ring,graph.videoFrameResource(output.videoSlot),raw);
        settings.hdrCurve=engine::findHdrTuningPreset("darkLift")->curve;settings.hdrCurve.strengthPercent=200;settings.hdrCurve.displayPeakNits=1000;
        ready=ready&&graph.applySettings(settings)&&present();
        const auto linear=[packed](const std::vector<float>& pixels,size_t at){
            std::array<double,3> c{pixels[at],pixels[at+1],pixels[at+2]};
            if(!packed)return c;
            for(auto& v:c)v=hdrRoundTrip::nits(v)/80;
            return std::array<double,3>{1.660491*c[0]-.587641*c[1]-.072850*c[2],-.124550*c[0]+1.132900*c[1]-.008349*c[2],-.018151*c[0]-.100579*c[1]+1.118730*c[2]};
        };
        double maxChromaError=0;bool signs=true;
        if(ready)for(unsigned i=0;i<4;++i){
            const auto at=size_t(i*16+8)*3;const auto a=linear(raw,at),b=linear(shown,at);
            const double aNorm=std::sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]);
            const double bNorm=std::sqrt(b[0]*b[0]+b[1]*b[1]+b[2]*b[2]);
            if(aNorm<=0||bNorm<=0){signs=false;continue;}
            for(unsigned c=0;c<3;++c){maxChromaError=std::max(maxChromaError,std::abs(a[c]/aNorm-b[c]/bNorm));if(a[c]<-.05)signs=signs&&b[c]<0;}
        }
        std::cout<<"COLOR_EVIDENCE packed="<<packed<<" normalizedRgbError="<<maxChromaError<<" negativeSigns="<<signs<<std::endl;
        check(ready&&signs&&maxChromaError<(packed?.015:.002),"enabled HDR curve preserves wide-gamut RGB direction and negative scRGB");
        ring.drainQueue();presenter.close();DestroyWindow(window);output={};graph.shutdown();
        // Restore grayscale input for the second output contract.
        for(unsigned y=0;y<16;++y)for(unsigned x=0;x<64;++x){
            reinterpret_cast<uint16_t*>(frame->data[0]+y*frame->linesize[0])[x]=uint16_t(std::lround(64+876*hdrRoundTrip::pq(patches[x/4])));
            if(!(y%2)&&!(x%2))for(unsigned c=1;c<3;++c)reinterpret_cast<uint16_t*>(frame->data[c]+y/2*frame->linesize[c])[x/2]=512;
        }
    }
    av_frame_free(&frame);ring.drainQueue();
    Microsoft::WRL::ComPtr<ID3D12InfoQueue> messages;
    if(SUCCEEDED(ctx.device()->QueryInterface(IID_PPV_ARGS(&messages)))){
        for(UINT64 i=0;i<messages->GetNumStoredMessages();++i){SIZE_T bytes=0;messages->GetMessage(i,nullptr,&bytes);std::vector<uint8_t> storage(bytes);auto* message=reinterpret_cast<D3D12_MESSAGE*>(storage.data());if(SUCCEEDED(messages->GetMessage(i,message,&bytes))&&message->Severity<=D3D12_MESSAGE_SEVERITY_ERROR){++failures;std::cout<<"GPU_VALIDATION "<<message->pDescription<<std::endl;}}
    }
    std::cout<<"HDR output integration failures="<<failures<<" (GPU/DXGI pixels only; physical monitor unmeasured)"<<std::endl;
    return failures?1:0;
}