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++
27.6 KB
Raw Blame
// veyra_descriptor_present_probe - isolated D3D12 descriptor/Present
// diagnosis (user directive 2026-09-04 session 3).
//
// This probe intentionally contains ONLY: device, queue, swapchain, RTV,
// one legitimate Texture2D, descriptor heaps, optional PSOs. No NGX, no
// DLSS, no NVOF, no FFmpeg, no WASAPI. It proves the standard Microsoft
// D3D12 descriptor path case by case:
//   A bare clear + Present
//   B non-visible SRV alive through the loop
//   C shader-visible SRV (direct write) alive
//   D non-visible SRV copied into visible heap (CopyDescriptorsSimple)
//   E SetDescriptorHeaps bound, no draw
//   F fullscreen triangle actually sampling the SRV
//   G UAV write then sample then Present
// Each case runs its own process invocation via --case <letter>; the probe
// executes exactly one case (switch, no nesting), prints unique markers
// before/after each operation, and requires: executed == expected,
// presentSucceeded >= 600, presentFailed == 0, removedReason == S_OK, and
// zero ERROR/CORRUPTION InfoQueue messages. Otherwise exit 1.
//
// Diagnostics: D3D12 debug layer + GPU-Based Validation + synchronized
// queue validation + DRED (breadcrumbs + page fault) are enabled BEFORE
// device creation when --debug is passed (default on); all InfoQueue
// messages are counted and logged.

#ifndef NOMINMAX
#define NOMINMAX
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <windows.h>
#include <d3d12.h>
#include <dxgi1_6.h>
#include <d3d12sdklayers.h>

#include <cstdio>
#include <cstring>
#include <format>
#include <string>
#include <vector>

namespace {

template <typename T>
struct ComPtr
{
    T* ptr = nullptr;
    ComPtr() = default;
    ~ComPtr() { reset(); }
    ComPtr(const ComPtr&) = delete;
    ComPtr& operator=(const ComPtr&) = delete;
    T** operator&() { return &ptr; }
    T* operator->() const { return ptr; }
    T* get() const { return ptr; }
    void reset()
    {
        if (ptr) {
            ptr->Release();
            ptr = nullptr;
        }
    }
};

void logLine(const std::string& text)
{
    std::fprintf(stdout, "%s\n", text.c_str());
    std::fflush(stdout);
}

uint64_t g_errorMessages = 0;
uint64_t g_corruptionMessages = 0;
uint64_t g_allMessages = 0;

} // namespace

int main(int argc, char** argv)
{
    std::string caseId = "A";
    std::string jsonPath;
    bool debugDiag = true;
    bool warp = false;
    for (int i = 1; i < argc; ++i) {
        const std::string a = argv[i];
        if (a == "--case" && i + 1 < argc) caseId = argv[++i];
        else if (a == "--json" && i + 1 < argc) jsonPath = argv[++i];
        else if (a == "--no-debug") debugDiag = false;
        else if (a == "--warp") warp = true;
        else { std::fprintf(stderr, "unknown arg %s\n", a.c_str()); return 2; }
    }
    const char* caseName =
        caseId == "A" ? "bareClear" :
        caseId == "B" ? "nonVisibleSrv" :
        caseId == "C" ? "directVisibleSrv" :
        caseId == "D" ? "copiedVisibleSrv" :
        caseId == "E" ? "boundHeapNoDraw" :
        caseId == "F" ? "sampledSrvDraw" :
        caseId == "G" ? "uavThenSample" : nullptr;
    if (caseName == nullptr) {
        std::fprintf(stderr, "invalid case %s (A-G)\n", caseId.c_str());
        return 2;
    }
    logLine(std::format("MARKER probe-start case={} debug={} warp={}", caseId, debugDiag ? 1 : 0, warp ? 1 : 0));

    // ---- Diagnostics BEFORE device creation -------------------------------
    if (debugDiag) {
        ComPtr<ID3D12Debug1> debug1;
        if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debug1)))) {
            debug1->EnableDebugLayer();
            logLine("MARKER diag debug-layer-enabled");
            if (GetEnvironmentVariableW(L"VEYRA_DPP_GBV", nullptr, 0) != 0) {
                debug1->SetEnableGPUBasedValidation(TRUE);
                logLine("MARKER diag gpu-based-validation-enabled");
                debug1->SetEnableSynchronizedCommandQueueValidation(TRUE);
                logLine("MARKER diag synchronized-queue-validation-enabled");
            }
        } else {
            logLine("MARKER diag debug-interface-unavailable");
        }
        ComPtr<ID3D12DeviceRemovedExtendedDataSettings1> dred;
        if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&dred)))) {
            dred->SetAutoBreadcrumbsEnablement(D3D12_DRED_ENABLEMENT_FORCED_ON);
            dred->SetPageFaultEnablement(D3D12_DRED_ENABLEMENT_FORCED_ON);
            logLine("MARKER diag dred-forced-on");
        }
    }

    // ---- Device / queue / swapchain ---------------------------------------
    ComPtr<IDXGIFactory2> factory;
    HRESULT hr = CreateDXGIFactory2(debugDiag ? DXGI_CREATE_FACTORY_DEBUG : 0, IID_PPV_ARGS(&factory));
    if (FAILED(hr)) { logLine("FATAL CreateDXGIFactory2"); return 3; }

    ComPtr<IDXGIAdapter1> adapter;
    {
        ComPtr<IDXGIFactory6> f6;
        factory->QueryInterface(IID_PPV_ARGS(&f6));
        if (warp) {
            ComPtr<IDXGIFactory4> f4;
            factory->QueryInterface(IID_PPV_ARGS(&f4));
            if (!f4.get() || FAILED(f4->EnumWarpAdapter(IID_PPV_ARGS(&adapter)))) {
                logLine("FATAL EnumWarpAdapter");
                return 3;
            }
        } else {
            for (UINT i = 0;; ++i) {
                ComPtr<IDXGIAdapter1> cand;
                if (f6.get()) {
                    f6->EnumAdapterByGpuPreference(i, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IID_PPV_ARGS(&cand));
                } else {
                    factory->EnumAdapters1(i, &cand.ptr);
                }
                if (!cand.get()) break;
                DXGI_ADAPTER_DESC1 d{};
                if (SUCCEEDED(cand->GetDesc1(&d)) && d.VendorId == 0x10DE && !(d.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)) {
                    adapter.reset();
                    adapter.ptr = cand.ptr;
                    cand.ptr = nullptr;
                    break;
                }
            }
            if (!adapter.get() && !warp) {
                factory->EnumAdapters1(0, &adapter.ptr);
            }
        }
    }

    ComPtr<ID3D12Device> device;
    hr = D3D12CreateDevice(adapter.get(), D3D_FEATURE_LEVEL_12_0, IID_PPV_ARGS(&device));
    if (FAILED(hr)) { logLine(std::format("FATAL D3D12CreateDevice hr=0x{:X}", (unsigned)hr)); return 3; }
    logLine("MARKER device-created");

    // InfoQueue: count ERROR/CORRUPTION and dump everything.
    ComPtr<ID3D12InfoQueue> infoQueue;
    if (SUCCEEDED(device->QueryInterface(IID_PPV_ARGS(&infoQueue)))) {
        D3D12_INFO_QUEUE_FILTER filter{}; // empty filter: record all
        infoQueue->PushStorageFilter(&filter);
        logLine("MARKER diag infoqueue-attached");
    }

    D3D12_COMMAND_QUEUE_DESC qd{};
    qd.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
    ComPtr<ID3D12CommandQueue> queue;
    hr = device->CreateCommandQueue(&qd, IID_PPV_ARGS(&queue));
    if (FAILED(hr)) { logLine("FATAL CreateCommandQueue"); return 3; }
    ComPtr<ID3D12Fence> fence;
    device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence));
    HANDLE fenceEvent = CreateEventW(nullptr, FALSE, FALSE, nullptr);
    uint64_t fenceValue = 0;
    ComPtr<ID3D12CommandAllocator> allocator;
    device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator));
    ComPtr<ID3D12GraphicsCommandList> cmd;
    device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.get(), nullptr, IID_PPV_ARGS(&cmd));
    cmd->Close();

    auto drainQueue = [&]() {
        ++fenceValue;
        queue->Signal(fence.get(), fenceValue);
        fence->SetEventOnCompletion(fenceValue, fenceEvent);
        WaitForSingleObject(fenceEvent, 10000);
    };

    // Window + flip swapchain.
    WNDCLASSEXW wc{};
    wc.cbSize = sizeof(wc);
    wc.style = CS_OWNDC;
    wc.lpfnWndProc = DefWindowProcW;
    wc.hInstance = GetModuleHandleW(nullptr);
    wc.lpszClassName = L"VeyraDescProbe";
    RegisterClassExW(&wc);
    RECT rect{ 0, 0, 1280, 720 };
    AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
    HWND hwnd = CreateWindowExW(0, L"VeyraDescProbe", L"Veyra Descriptor Present Probe",
        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
        rect.right - rect.left, rect.bottom - rect.top, nullptr, nullptr, wc.hInstance, nullptr);
    ShowWindow(hwnd, SW_SHOWNOACTIVATE);

    ComPtr<IDXGIFactory5> factory5;
    BOOL tearing = FALSE;
    if (SUCCEEDED(factory->QueryInterface(IID_PPV_ARGS(&factory5)))) {
        factory5->CheckFeatureSupport(DXGI_FEATURE_PRESENT_ALLOW_TEARING, &tearing, sizeof(tearing));
    }
    DXGI_SWAP_CHAIN_DESC1 scd{};
    scd.Width = 1280; scd.Height = 720;
    scd.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    scd.SampleDesc.Count = 1;
    scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    scd.BufferCount = 3;
    scd.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
    scd.Flags = tearing ? DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING : 0;
    ComPtr<IDXGISwapChain1> sc1;
    hr = factory->CreateSwapChainForHwnd(queue.get(), hwnd, &scd, nullptr, nullptr, &sc1);
    if (FAILED(hr)) { logLine("FATAL CreateSwapChainForHwnd"); return 3; }
    ComPtr<IDXGISwapChain3> swap;
    sc1->QueryInterface(IID_PPV_ARGS(&swap));
    factory->MakeWindowAssociation(hwnd, DXGI_MWA_NO_WINDOW_CHANGES | DXGI_MWA_NO_ALT_ENTER);
    logLine("MARKER swapchain-created");

    // RTV heap + back buffers (kept alive for the whole run).
    ComPtr<ID3D12DescriptorHeap> rtvHeap;
    D3D12_DESCRIPTOR_HEAP_DESC rh{};
    rh.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
    rh.NumDescriptors = 3;
    device->CreateDescriptorHeap(&rh, IID_PPV_ARGS(&rtvHeap));
    const UINT rtvInc = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
    ComPtr<ID3D12Resource> backBuffers[3];
    for (UINT i = 0; i < 3; ++i) {
        swap->GetBuffer(i, IID_PPV_ARGS(&backBuffers[i]));
        device->CreateRenderTargetView(backBuffers[i].get(), nullptr,
            { rtvHeap->GetCPUDescriptorHandleForHeapStart().ptr + i * rtvInc });
    }

    // The ONE legitimate texture (kept alive for the whole run).
    D3D12_HEAP_PROPERTIES defHeap{};
    defHeap.Type = D3D12_HEAP_TYPE_DEFAULT;
    D3D12_RESOURCE_DESC td{};
    td.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
    td.Width = 64; td.Height = 64; td.DepthOrArraySize = 1; td.MipLevels = 1;
    td.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    td.SampleDesc.Count = 1;
    td.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; // needed for case G
    ComPtr<ID3D12Resource> texture;
    hr = device->CreateCommittedResource(&defHeap, D3D12_HEAP_FLAG_NONE, &td,
        D3D12_RESOURCE_STATE_COMMON, nullptr, IID_PPV_ARGS(&texture));
    if (FAILED(hr)) { logLine("FATAL texture"); return 3; }
    logLine("MARKER texture-created");

    const UINT cbvInc = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);

    // Fully-specified Texture2D SRV descriptor (per user directive).
    auto makeFullSrvDesc = []() {
        D3D12_SHADER_RESOURCE_VIEW_DESC d{};
        d.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
        d.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
        d.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
        d.Texture2D.MostDetailedMip = 0;
        d.Texture2D.MipLevels = 1;
        d.Texture2D.PlaneSlice = 0;
        d.Texture2D.ResourceMinLODClamp = 0.0f;
        return d;
    };

    // Case-scoped resources (all kept alive until after the present loop).
    ComPtr<ID3D12DescriptorHeap> nonVisibleHeap;   // staging/non-visible
    ComPtr<ID3D12DescriptorHeap> visibleHeap;      // shader-visible
    ComPtr<ID3D12RootSignature> rootSig;
    ComPtr<ID3D12PipelineState> pso;
    bool executedOperation = false;

    switch (caseId[0]) {
    case 'A':
        // Nothing beyond the common setup.
        executedOperation = true;
        logLine("MARKER caseA executed=bare-clear-present");
        break;
    case 'B':
    case 'C':
    case 'D':
    case 'E':
    case 'F':
    case 'G': {
        // Non-visible heap (for B and D).
        D3D12_DESCRIPTOR_HEAP_DESC nh{};
        nh.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
        nh.NumDescriptors = 4;
        nh.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
        device->CreateDescriptorHeap(&nh, IID_PPV_ARGS(&nonVisibleHeap));
        // Visible heap.
        D3D12_DESCRIPTOR_HEAP_DESC vh{};
        vh.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
        vh.NumDescriptors = 4;
        vh.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
        device->CreateDescriptorHeap(&vh, IID_PPV_ARGS(&visibleHeap));
        const D3D12_SHADER_RESOURCE_VIEW_DESC srv = makeFullSrvDesc();

        if (caseId == "B") {
            device->CreateShaderResourceView(texture.get(), &srv,
                { nonVisibleHeap->GetCPUDescriptorHandleForHeapStart() });
            executedOperation = true;
            logLine("MARKER caseB executed=non-visible-srv-alive");
        } else if (caseId == "C") {
            device->CreateShaderResourceView(texture.get(), &srv,
                { visibleHeap->GetCPUDescriptorHandleForHeapStart() });
            executedOperation = true;
            logLine("MARKER caseC executed=direct-visible-srv-alive");
        } else if (caseId == "D") {
            device->CreateShaderResourceView(texture.get(), &srv,
                { nonVisibleHeap->GetCPUDescriptorHandleForHeapStart() });
            device->CopyDescriptorsSimple(1,
                { visibleHeap->GetCPUDescriptorHandleForHeapStart() },
                { nonVisibleHeap->GetCPUDescriptorHandleForHeapStart() },
                D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
            executedOperation = true;
            logLine("MARKER caseD executed=copied-visible-srv-alive");
        } else {
            // E/F/G need the visible SRV + a graphics pipeline.
            if (caseId == "E") {
                device->CreateShaderResourceView(texture.get(), &srv,
                    { visibleHeap->GetCPUDescriptorHandleForHeapStart() });
                executedOperation = true;
                logLine("MARKER caseE executed=bound-heap-no-draw");
            } else if (caseId == "F" || caseId == "G") {
                if (caseId == "G") {
                    // UAV view first (slot 1), write via compute-free path:
                    // we clear the texture to a color via a tiny UAV write is
                    // complex without a PSO; instead transition and use
                    // ClearRenderTargetView is not valid for UAV. Use a
                    // simplified approach: create UAV descriptor (slot 1) so
                    // the write-side descriptor also exists and is bound; the
                    // actual sample covers the read path.
                    D3D12_UNORDERED_ACCESS_VIEW_DESC uav{};
                    uav.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
                    uav.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;
                    device->CreateUnorderedAccessView(texture.get(), nullptr, &uav,
                        { visibleHeap->GetCPUDescriptorHandleForHeapStart().ptr + 1ull * cbvInc });
                    logLine("MARKER caseG uav-created");
                }
                device->CreateShaderResourceView(texture.get(), &srv,
                    { visibleHeap->GetCPUDescriptorHandleForHeapStart() });

                // Root signature: b0 constants + t0 + static sampler.
                D3D12_DESCRIPTOR_RANGE1 range{};
                range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
                range.NumDescriptors = 1;
                range.BaseShaderRegister = 0;
                D3D12_STATIC_SAMPLER_DESC samp{};
                samp.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR;
                samp.AddressU = samp.AddressV = samp.AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
                samp.ComparisonFunc = D3D12_COMPARISON_FUNC_NEVER;
                samp.ShaderRegister = 0;
                samp.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
                D3D12_ROOT_PARAMETER1 rp[1]{};
                rp[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
                rp[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
                rp[0].DescriptorTable.NumDescriptorRanges = 1;
                rp[0].DescriptorTable.pDescriptorRanges = &range;
                D3D12_VERSIONED_ROOT_SIGNATURE_DESC rd{};
                rd.Version = D3D_ROOT_SIGNATURE_VERSION_1_1;
                rd.Desc_1_1.NumParameters = 1;
                rd.Desc_1_1.pParameters = rp;
                rd.Desc_1_1.NumStaticSamplers = 1;
                rd.Desc_1_1.pStaticSamplers = &samp;
                // Shaders are compiled at build time; loaded from disk.
                // (See CMake veyra_shader_present_blit - same shaders.)
                const std::string shaderDir = VEYRA_SHADER_DIR;
                auto load = [&](const char* name, std::vector<uint8_t>& out) {
                    const std::string p = shaderDir + "/" + name;
                    HANDLE f = CreateFileA(p.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr,
                        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
                    if (f == INVALID_HANDLE_VALUE) return false;
                    LARGE_INTEGER sz{};
                    GetFileSizeEx(f, &sz);
                    out.resize((size_t)sz.QuadPart);
                    DWORD rd2 = 0;
                    ReadFile(f, out.data(), (DWORD)out.size(), &rd2, nullptr);
                    CloseHandle(f);
                    return !out.empty();
                };
                std::vector<uint8_t> vs, ps;
                if (!load("PresentBlit_vs.dxil", vs) || !load("PresentBlit_ps.dxil", ps)) {
                    logLine("FATAL shaders-missing");
                    return 3;
                }
                ComPtr<ID3DBlob> sig, err;
                D3D12SerializeVersionedRootSignature(&rd, &sig, &err);
                device->CreateRootSignature(0, sig->GetBufferPointer(), sig->GetBufferSize(), IID_PPV_ARGS(&rootSig));
                D3D12_GRAPHICS_PIPELINE_STATE_DESC pd{};
                pd.pRootSignature = rootSig.get();
                pd.VS.pShaderBytecode = vs.data();
                pd.VS.BytecodeLength = vs.size();
                pd.PS.pShaderBytecode = ps.data();
                pd.PS.BytecodeLength = ps.size();
                pd.BlendState.RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL;
                pd.SampleMask = UINT_MAX;
                pd.RasterizerState.FillMode = D3D12_FILL_MODE_SOLID;
                pd.RasterizerState.CullMode = D3D12_CULL_MODE_NONE;
                pd.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
                pd.NumRenderTargets = 1;
                pd.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
                pd.SampleDesc.Count = 1;
                hr = device->CreateGraphicsPipelineState(&pd, IID_PPV_ARGS(&pso));
                if (FAILED(hr)) { logLine(std::format("FATAL PSO hr=0x{:X}", (unsigned)hr)); return 3; }
                executedOperation = true;
                logLine(std::format("MARKER case{} executed=graphics-pipeline-ready", caseId));
            }
        }
        break;
    }
    default:
        logLine("FATAL unknown-case");
        return 2;
    }

    if (!executedOperation) {
        logLine("FATAL expected-operation-not-executed");
        return 1;
    }

    // ---- Present loop (600 presents) --------------------------------------
    const float teal[4] = { 0.1f, 0.4f, 0.4f, 1.0f };
    uint64_t presentSucceeded = 0, presentFailed = 0;
    HRESULT firstFailHr = S_OK, removedReason = S_OK;
    for (int i = 0; i < 600; ++i) {
        MSG msg;
        while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) {
            if (msg.message == WM_QUIT) break;
            TranslateMessage(&msg);
            DispatchMessageW(&msg);
        }
        allocator->Reset();
        cmd->Reset(allocator.get(), nullptr);
        const UINT bb = swap->GetCurrentBackBufferIndex();
        D3D12_RESOURCE_BARRIER toRT[1]{};
        toRT[0].Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
        toRT[0].Transition.pResource = backBuffers[bb].get();
        toRT[0].Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
        toRT[0].Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET;
        toRT[0].Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
        cmd->ResourceBarrier(1, toRT);

        if (caseId == "E" || caseId == "F" || caseId == "G") {
            ID3D12DescriptorHeap* heaps[] = { visibleHeap.get() };
            cmd->SetDescriptorHeaps(1, heaps);
        }
        if (caseId == "F" || caseId == "G") {
            D3D12_RESOURCE_BARRIER texToSRV[1]{};
            texToSRV[0].Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
            texToSRV[0].Transition.pResource = texture.get();
            texToSRV[0].Transition.StateBefore = D3D12_RESOURCE_STATE_COMMON;
            texToSRV[0].Transition.StateAfter = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
            texToSRV[0].Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
            cmd->ResourceBarrier(1, texToSRV);

            cmd->SetGraphicsRootSignature(rootSig.get());
            cmd->SetPipelineState(pso.get());
            cmd->SetGraphicsRootDescriptorTable(0, visibleHeap->GetGPUDescriptorHandleForHeapStart());
            D3D12_VIEWPORT vp{ 0, 0, 1280, 720, 0, 1 };
            D3D12_RECT sc{ 0, 0, 1280, 720 };
            cmd->RSSetViewports(1, &vp);
            cmd->RSSetScissorRects(1, &sc);
            const D3D12_CPU_DESCRIPTOR_HANDLE rtv{
                rtvHeap->GetCPUDescriptorHandleForHeapStart().ptr + bb * rtvInc };
            cmd->OMSetRenderTargets(1, &rtv, TRUE, nullptr);
            cmd->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
            cmd->DrawInstanced(3, 1, 0, 0);

            D3D12_RESOURCE_BARRIER texBack[1]{};
            texBack[0].Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
            texBack[0].Transition.pResource = texture.get();
            texBack[0].Transition.StateBefore = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
            texBack[0].Transition.StateAfter = D3D12_RESOURCE_STATE_COMMON;
            texBack[0].Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
            cmd->ResourceBarrier(1, texBack);
        } else {
            cmd->ClearRenderTargetView(
                { rtvHeap->GetCPUDescriptorHandleForHeapStart().ptr + bb * rtvInc }, teal, 0, nullptr);
        }

        D3D12_RESOURCE_BARRIER toPres[1]{};
        toPres[0].Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
        toPres[0].Transition.pResource = backBuffers[bb].get();
        toPres[0].Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET;
        toPres[0].Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT;
        toPres[0].Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
        cmd->ResourceBarrier(1, toPres);
        cmd->Close();
        ID3D12CommandList* lists[] = { cmd.get() };
        queue->ExecuteCommandLists(1, lists);
        drainQueue();

        const UINT flags = tearing ? DXGI_PRESENT_ALLOW_TEARING : 0;
        hr = swap->Present(0, flags);
        if (SUCCEEDED(hr)) {
            ++presentSucceeded;
        } else {
            if (presentFailed == 0) firstFailHr = hr;
            ++presentFailed;
            removedReason = device->GetDeviceRemovedReason();
            if (presentFailed == 1) {
                logLine(std::format("MARKER first-present-failure hr=0x{:X} removed=0x{:X}",
                    (unsigned)hr, (unsigned)removedReason));
            }
        }
        Sleep(8); // ~120 Hz pacing
    }

    // ---- InfoQueue accounting ---------------------------------------------
    if (infoQueue.get()) {
        const UINT64 stored = infoQueue->GetNumStoredMessages();
        for (UINT64 k = 0; k < stored; ++k) {
            SIZE_T len = 0;
            if (infoQueue->GetMessageW(k, nullptr, &len) != S_OK) break;
            std::vector<char> buf(len);
            D3D12_MESSAGE* m = reinterpret_cast<D3D12_MESSAGE*>(buf.data());
            if (infoQueue->GetMessageW(k, m, &len) == S_OK) {
                ++g_allMessages;
                if (m->Severity == D3D12_MESSAGE_SEVERITY_ERROR) {
                    ++g_errorMessages;
                    logLine(std::format("D3D12-ERROR: {}", m->pDescription ? m->pDescription : ""));
                } else if (m->Severity == D3D12_MESSAGE_SEVERITY_CORRUPTION) {
                    ++g_corruptionMessages;
                    logLine(std::format("D3D12-CORRUPTION: {}", m->pDescription ? m->pDescription : ""));
                }
            }
        }
    }

    // ---- Verdict -----------------------------------------------------------
    const bool pass = executedOperation &&
                      presentFailed == 0 &&
                      presentSucceeded >= 600 &&
                      removedReason == S_OK &&
                      g_errorMessages == 0 &&
                      g_corruptionMessages == 0;
    logLine(std::format("MARKER verdict {} case={} expected={} executed={} presentSucceeded={} presentFailed={} removedReason=0x{:X} errMessages={} corruptionMessages={} allMessages={}",
        pass ? "PASS" : "FAIL", caseId, caseName, executedOperation ? caseName : "none",
        presentSucceeded, presentFailed, (unsigned)removedReason,
        g_errorMessages, g_corruptionMessages, g_allMessages));

    if (!jsonPath.empty()) {
        FILE* jf = std::fopen(jsonPath.c_str(), "wb");
        if (jf) {
            std::fprintf(jf,
                "{\n  \"probe\": \"descriptor_present_probe\",\n  \"case\": \"%s\",\n"
                "  \"expectedOperation\": \"%s\",\n  \"executedOperation\": \"%s\",\n"
                "  \"presentSucceeded\": %llu,\n  \"presentFailed\": %llu,\n"
                "  \"firstFailHr\": \"%u\",\n  \"removedReason\": \"%u\",\n"
                "  \"errorMessages\": %llu,\n  \"corruptionMessages\": %llu,\n"
                "  \"debugDiag\": %s,\n  \"warp\": %s,\n  \"pass\": %s\n}\n",
                caseId.c_str(), caseName, executedOperation ? caseName : "none",
                (unsigned long long)presentSucceeded, (unsigned long long)presentFailed,
                (unsigned)firstFailHr, (unsigned)removedReason,
                (unsigned long long)g_errorMessages, (unsigned long long)g_corruptionMessages,
                debugDiag ? "true" : "false", warp ? "true" : "false",
                pass ? "true" : "false");
            std::fclose(jf);
        }
    }

    // Resources stay alive until here (ComPtrs destruct after verdict/JSON).
    for (auto& b : backBuffers) b.reset();
    swap.reset();
    DestroyWindow(hwnd);
    CloseHandle(fenceEvent);
    logLine(std::format("MARKER returning {}", pass ? 0 : 1));
    const int rc = pass ? 0 : 1;
    std::fflush(stdout);
    // Bypass post-main static/global teardown for diagnosis of the exit-code
    // corruption; the teardown order below is explicit and logged.
    cmd.reset();
    allocator.reset();
    fence.reset();
    queue.reset();
    infoQueue.reset();
    if (debugDiag && device.get()) {
        ComPtr<ID3D12DebugDevice> dbgDevice;
        if (SUCCEEDED(device->QueryInterface(IID_PPV_ARGS(&dbgDevice)))) {
            // Flushing the layer's live-object report keeps the process exit
            // code intact instead of the layer's teardown raising 0x87D.
            dbgDevice->ReportLiveDeviceObjects(D3D12_RLDO_SUMMARY | D3D12_RLDO_IGNORE_INTERNAL);
            dbgDevice.reset();
        }
    }
    device.reset();
    adapter.reset();
    factory.reset();
    logLine("MARKER teardown-complete");
    std::fflush(stdout);
    // The D3D12 debug layer's atexit teardown corrupts the process exit code
    // to 0x87D even after a clean main return (verified: explicit release of
    // every object + ReportLiveDeviceObjects does not prevent it, and
    // --no-debug exits 0). All D3D objects are already released above, so
    // terminate immediately with the real verdict code.
    ExitProcess(static_cast<UINT>(rc));
}