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++
// veyra_nvof_probe - NVOF data-contract proof harness (user directive
// 2026-09-04 s6). Runs the FULL displacement matrix on textured 2D patterns:
// dx in {+4,-4,+8,-8}, dy in {+4,-4,+8,-8} (8 single-axis cases + 2D cases)
// and validates:
// - capability list via two-call nvOFGetCaps (element count, then array)
// - B8G8R8A8 inputs (NV_OF_BUFFER_FORMAT_ABGR8 per NvOFD3DCommon.cpp)
// - raw SHORT2 output at ceil(w/grid) x ceil(h/grid), R16G16_SINT
// - cost buffer actually written (R8_UINT at grid extent)
// - raw/32.0 conversion, current->previous direction (sign matrix)
// - interior median endpoint error <= 1px
// - confidence INVERSELY related to cost (low-cost quartile has smaller
// endpoint error than the high-cost quartile)
// Debug layer + GBV + DRED + InfoQueue are enabled; 0 ERROR / 0 CORRUPTION
// required. JSON carries every case's stats; exit 0 only if all pass.
#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 <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <format>
#include <string>
#include <vector>
// NVOF SDK headers
#include <nvOpticalFlowCommon.h>
#include <nvOpticalFlowD3D12.h>
#include "veyra/Log.h"
#include "veyra/Result.h"
#include "veyra/gfx/D3D12DeviceContext.h"
#pragma comment(lib, "d3d12.lib")
#pragma comment(lib, "dxgi.lib")
using veyra::gfx::ComPtr;
namespace {
struct Json {
std::string body;
bool first = true;
void field(const std::string& k, const std::string& v) {
if (!first) body += ",";
first = false;
body += "\"" + k + "\":" + v;
}
void num(const std::string& k, double v) {
char b[64];
std::snprintf(b, sizeof(b), "%.4f", v);
field(k, b);
}
void integer(const std::string& k, long long v) { field(k, std::to_string(v)); }
void boolean(const std::string& k, bool v) { field(k, v ? "true" : "false"); }
void str(const std::string& k, const std::string& v) {
std::string esc;
for (char c : v) { if (c == '"' || c == '\\') esc += '\\'; esc += c; }
field(k, "\"" + esc + "\"");
}
};
struct CaseResult {
std::string name;
double dxTrue = 0, dyTrue = 0;
double dxMedian = 0, dyMedian = 0; // raw/32 interior median
double endpointErrMedian = 0; // |median - true| in px
double rawNonZeroFrac = 0;
double costP50 = 0, costP95 = 0, costMax = 0;
double gatedFrac = 0;
bool signCorrect = false;
bool errorOk = false;
};
uint32_t lcg(uint32_t& s) { s = s * 1664525u + 1013904223u; return s >> 16; }
} // namespace
int main(int argc, char** argv)
{
std::string jsonPath = "nvof-proof.json";
std::string logPath;
for (int i = 1; i < argc; ++i) {
if (std::strcmp(argv[i], "--json-file") == 0 && i + 1 < argc) jsonPath = argv[++i];
else if (std::strcmp(argv[i], "--log-file") == 0 && i + 1 < argc) logPath = argv[++i];
}
if (!logPath.empty()) (void)veyra::Logger::instance().openFile(std::wstring(logPath.begin(), logPath.end()));
// ---- Diagnostics BEFORE device ----------------------------------------
ComPtr<ID3D12Debug1> debug1;
if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debug1)))) {
debug1->EnableDebugLayer();
debug1->SetEnableGPUBasedValidation(TRUE);
debug1->SetEnableSynchronizedCommandQueueValidation(TRUE);
veyra::log::info("nvof-proof", "diag: debug layer + GBV + sync queue validation ON");
}
ComPtr<ID3D12DeviceRemovedExtendedDataSettings1> dred;
if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&dred)))) {
dred->SetAutoBreadcrumbsEnablement(D3D12_DRED_ENABLEMENT_FORCED_ON);
dred->SetPageFaultEnablement(D3D12_DRED_ENABLEMENT_FORCED_ON);
}
veyra::gfx::D3D12DeviceContext ctx;
veyra::gfx::DeviceContextDesc dcd{};
dcd.enableDebugLayer = true;
dcd.commandSlotCount = 4;
veyra::Status st = veyra::Status::Ok;
if (!ctx.initialize(dcd, st)) return 3;
ComPtr<ID3D12InfoQueue> iq;
ctx.device()->QueryInterface(IID_PPV_ARGS(&iq));
const uint32_t W = 1920, H = 1080;
// ---- NVOF API ---------------------------------------------------------
HMODULE dll = LoadLibraryExW(L"nvofapi64.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
if (dll == nullptr) { veyra::log::error("nvof-proof", "nvofapi64 load failed"); return 3; }
auto getMaxVer = reinterpret_cast<NV_OF_STATUS(NVOFAPI*)(uint32_t*)>(
GetProcAddress(dll, "NvOFGetMaxSupportedApiVersion"));
auto createInstance = reinterpret_cast<NV_OF_STATUS(NVOFAPI*)(uint32_t, NV_OF_D3D12_API_FUNCTION_LIST*)>(
GetProcAddress(dll, "NvOFAPICreateInstanceD3D12"));
if (getMaxVer == nullptr || createInstance == nullptr) return 3;
uint32_t maxVer = 0;
if (getMaxVer(&maxVer) != NV_OF_SUCCESS) return 3;
NV_OF_D3D12_API_FUNCTION_LIST fn{};
if (createInstance(maxVer, &fn) != NV_OF_SUCCESS) return 3;
NvOFHandle hOF = nullptr;
if (fn.nvCreateOpticalFlowD3D12(ctx.device(), &hOF) != NV_OF_SUCCESS) return 3;
// ---- Capability list: two-call protocol --------------------------------
std::vector<uint32_t> grids;
{
uint32_t count = 0;
NV_OF_STATUS s1 = fn.nvOFGetCaps(hOF, NV_OF_CAPS_SUPPORTED_OUTPUT_GRID_SIZES, nullptr, &count);
veyra::log::info("nvof-proof", std::format("caps grids: first call st={} elemCount={}", (int)s1, count));
if (s1 == NV_OF_SUCCESS && count > 0 && count < 32) {
grids.resize(count, 0);
uint32_t filled = count;
NV_OF_STATUS s2 = fn.nvOFGetCaps(hOF, NV_OF_CAPS_SUPPORTED_OUTPUT_GRID_SIZES, grids.data(), &filled);
veyra::log::info("nvof-proof", std::format("caps grids: second call st={} filled={}", (int)s2, filled));
if (s2 != NV_OF_SUCCESS) grids.clear();
else grids.resize(filled);
}
std::string list;
for (auto g : grids) list += std::to_string(g) + " ";
veyra::log::info("nvof-proof", std::format("capability grid list: [{}]", list));
}
const uint32_t GRID = 4;
bool gridSupported = false;
for (auto g : grids) if (g == GRID) { gridSupported = true; break; }
if (!gridSupported) {
veyra::log::error("nvof-proof", std::format("grid {} not in capability list (fail closed)", GRID));
return 4;
}
const uint32_t GW = (W + GRID - 1) / GRID;
const uint32_t GH = (H + GRID - 1) / GRID;
// ---- Session init (grid-4, cost on, ABGR8) -----------------------------
NV_OF_INIT_PARAMS init{};
init.width = W; init.height = H;
init.outGridSize = NV_OF_OUTPUT_VECTOR_GRID_SIZE_4;
init.mode = NV_OF_MODE_OPTICALFLOW;
init.perfLevel = NV_OF_PERF_LEVEL_MEDIUM;
init.enableExternalHints = NV_OF_FALSE;
init.enableOutputCost = NV_OF_TRUE;
init.predDirection = NV_OF_PRED_DIRECTION_FORWARD;
init.enableGlobalFlow = NV_OF_FALSE;
init.inputBufferFormat = NV_OF_BUFFER_FORMAT_ABGR8;
NV_OF_STATUS ist = fn.nvOFInit(hOF, &init);
veyra::log::info("nvof-proof", std::format("nvOFInit st={} inputExtent={}x{} grid={} gridExtent={}x{} ABGR8(B8G8R8A8)",
(int)ist, W, H, GRID, GW, GH));
if (ist != NV_OF_SUCCESS) return 4;
// ---- Resources ---------------------------------------------------------
auto makeTex = [&](uint32_t w, uint32_t h, DXGI_FORMAT f, D3D12_RESOURCE_STATES initial) {
D3D12_HEAP_PROPERTIES hp{}; hp.Type = D3D12_HEAP_TYPE_DEFAULT;
D3D12_RESOURCE_DESC d{};
d.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
d.Width = w; d.Height = h; d.DepthOrArraySize = 1; d.MipLevels = 1;
d.Format = f; d.SampleDesc.Count = 1;
d.Flags = D3D12_RESOURCE_FLAG_NONE;
ComPtr<ID3D12Resource> r;
if (FAILED(ctx.device()->CreateCommittedResource(&hp, D3D12_HEAP_FLAG_NONE, &d, initial, nullptr, IID_PPV_ARGS(&r))))
return ComPtr<ID3D12Resource>();
return r;
};
ComPtr<ID3D12Resource> texA = makeTex(W, H, DXGI_FORMAT_B8G8R8A8_UNORM, D3D12_RESOURCE_STATE_COMMON);
ComPtr<ID3D12Resource> texB = makeTex(W, H, DXGI_FORMAT_B8G8R8A8_UNORM, D3D12_RESOURCE_STATE_COMMON);
ComPtr<ID3D12Resource> texFlow = makeTex(GW, GH, DXGI_FORMAT_R16G16_SINT, D3D12_RESOURCE_STATE_COMMON);
ComPtr<ID3D12Resource> texCost = makeTex(GW, GH, DXGI_FORMAT_R8_UINT, D3D12_RESOURCE_STATE_COMMON);
if (!texA || !texB || !texFlow || !texCost) {
veyra::log::error("nvof-proof", "resource allocation failed (fail closed)");
return 4;
}
veyra::log::info("nvof-proof", std::format("resources: A/B {}x{} B8G8R8A8 | flow {}x{} R16G16_SINT | cost {}x{} R8_UINT",
W, H, GW, GH, GW, GH));
NvOFGPUBufferHandle hA = nullptr, hB = nullptr, hFlow = nullptr, hCost = nullptr;
auto reg = [&](ID3D12Resource* res, NvOFGPUBufferHandle& h) {
NV_OF_REGISTER_RESOURCE_PARAMS_D3D12 p{};
p.resource = res;
p.hOFGpuBuffer = &h;
return fn.nvOFRegisterResourceD3D12(hOF, &p);
};
if (reg(texA.Get(), hA) != NV_OF_SUCCESS || reg(texB.Get(), hB) != NV_OF_SUCCESS ||
reg(texFlow.Get(), hFlow) != NV_OF_SUCCESS || reg(texCost.Get(), hCost) != NV_OF_SUCCESS) {
veyra::log::error("nvof-proof", "registration failed");
return 4;
}
veyra::log::info("nvof-proof", "registered A/B/flow/cost OK");
ComPtr<ID3D12Fence> fence;
ctx.device()->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence));
HANDLE ev = CreateEventW(nullptr, FALSE, FALSE, nullptr);
uint64_t fenceVal = 0;
const size_t rowPitch = (static_cast<size_t>(W) * 4 + 255) & ~size_t(255);
auto makeBuffer = [&](D3D12_HEAP_TYPE type, size_t sz, D3D12_RESOURCE_STATES initSt) {
D3D12_HEAP_PROPERTIES hp{}; hp.Type = type;
D3D12_RESOURCE_DESC d{};
d.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
d.Width = sz; d.Height = 1; d.DepthOrArraySize = 1; d.MipLevels = 1;
d.SampleDesc.Count = 1; d.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
ComPtr<ID3D12Resource> r;
ctx.device()->CreateCommittedResource(&hp, D3D12_HEAP_FLAG_NONE, &d, initSt, nullptr, IID_PPV_ARGS(&r));
return r;
};
ComPtr<ID3D12Resource> upA = makeBuffer(D3D12_HEAP_TYPE_UPLOAD, rowPitch * H, D3D12_RESOURCE_STATE_GENERIC_READ);
ComPtr<ID3D12Resource> upB = makeBuffer(D3D12_HEAP_TYPE_UPLOAD, rowPitch * H, D3D12_RESOURCE_STATE_GENERIC_READ);
const size_t flowPitch = (static_cast<size_t>(GW) * 4 + 255) & ~size_t(255);
const size_t costPitch = (static_cast<size_t>(GW) + 255) & ~size_t(255);
ComPtr<ID3D12Resource> rbFlow = makeBuffer(D3D12_HEAP_TYPE_READBACK, flowPitch * GH, D3D12_RESOURCE_STATE_COPY_DEST);
ComPtr<ID3D12Resource> rbCost = makeBuffer(D3D12_HEAP_TYPE_READBACK, costPitch * GH, D3D12_RESOURCE_STATE_COPY_DEST);
ComPtr<ID3D12CommandAllocator> alloc;
ctx.device()->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&alloc));
ComPtr<ID3D12GraphicsCommandList> list;
ctx.device()->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, alloc.Get(), nullptr, IID_PPV_ARGS(&list));
list->Close();
auto drain = [&]() {
++fenceVal;
ctx.directQueue()->Signal(fence.Get(), fenceVal);
fence->SetEventOnCompletion(fenceVal, ev);
WaitForSingleObject(ev, 5000);
};
// s8-6 base: three content regions so the cost distribution is
// NON-degenerate:
// region T (top 60%): noise + colored blocks (strong texture);
// region P (60-80%): flat gray (textureless - OF must be uncertain);
// region N (80-100%): high-frequency color noise (ambiguity).
std::vector<uint8_t> base(static_cast<size_t>(W) * H * 4);
{
uint32_t rng = 0xC0FFEE;
const uint32_t texH = H * 3 / 5;
const uint32_t flatY1 = H * 4 / 5;
for (uint32_t y = 0; y < H; ++y) {
for (uint32_t x = 0; x < W; ++x) {
const size_t i = (static_cast<size_t>(y) * W + x) * 4;
uint8_t r, g, b;
if (y < texH) {
const uint8_t v = static_cast<uint8_t>(lcg(rng) & 0xFF);
r = g = b = v;
} else if (y < flatY1) {
r = g = b = 128; // textureless
} else {
r = static_cast<uint8_t>(lcg(rng) & 0xFF);
g = static_cast<uint8_t>(lcg(rng) & 0xFF);
b = static_cast<uint8_t>(lcg(rng) & 0xFF);
}
base[i] = r; base[i + 1] = g; base[i + 2] = b; base[i + 3] = 255;
}
}
for (int by = 0; by < 7; ++by) {
for (int bx = 0; bx < 20; ++bx) {
const uint32_t x0 = bx * 96 + (lcg(rng) % 32), y0 = by * 90 + (lcg(rng) % 32);
const uint8_t col[3] = { static_cast<uint8_t>(lcg(rng)), static_cast<uint8_t>(lcg(rng)), static_cast<uint8_t>(lcg(rng)) };
for (uint32_t y = y0; y < std::min<uint32_t>(y0 + 40, texH); ++y)
for (uint32_t x = x0; x < std::min<uint32_t>(x0 + 40, W); ++x) {
const size_t i = (static_cast<size_t>(y) * W + x) * 4;
base[i] = col[0]; base[i + 1] = col[1]; base[i + 2] = col[2]; base[i + 3] = 255;
}
}
}
}
auto regionOfRow = [&](uint32_t cellY) -> int {
const uint32_t py = cellY * GRID;
if (py < H * 3 / 5) return 0;
if (py < H * 4 / 5) return 1;
return 2;
};
auto renderShifted = [&](std::vector<uint8_t>& out, int dx, int dy) {
out.assign(static_cast<size_t>(W) * H * 4, 0);
for (uint32_t y = 0; y < H; ++y) {
const int sy = static_cast<int>(y) - dy;
if (sy < 0 || sy >= static_cast<int>(H)) continue;
for (uint32_t x = 0; x < W; ++x) {
const int sx = static_cast<int>(x) - dx;
if (sx < 0 || sx >= static_cast<int>(W)) continue;
std::memcpy(&out[(static_cast<size_t>(y) * W + x) * 4], &base[(static_cast<size_t>(sy) * W + sx) * 4], 4);
}
}
};
struct Case { const char* name; int dx, dy; };
const Case cases[] = {
{"dx+4", 4, 0}, {"dx-4", -4, 0}, {"dx+8", 8, 0}, {"dx-8", -8, 0},
{"dy+4", 0, 4}, {"dy-4", 0, -4}, {"dy+8", 0, 8}, {"dy-8", 0, -8},
{"2d+6+3", 6, 3}, {"2d-5+7", -5, 7},
};
auto uploadAB = [&](const std::vector<uint8_t>& imgA, const std::vector<uint8_t>& imgB) {
uint8_t* mA = nullptr; uint8_t* mB = nullptr;
upA->Map(0, nullptr, reinterpret_cast<void**>(&mA));
upB->Map(0, nullptr, reinterpret_cast<void**>(&mB));
for (uint32_t y = 0; y < H; ++y) {
std::memcpy(mA + y * rowPitch, &imgA[static_cast<size_t>(y) * W * 4], W * 4);
std::memcpy(mB + y * rowPitch, &imgB[static_cast<size_t>(y) * W * 4], W * 4);
}
upA->Unmap(0, nullptr);
upB->Unmap(0, nullptr);
alloc->Reset();
list->Reset(alloc.Get(), nullptr);
for (int k = 0; k < 2; ++k) {
ID3D12Resource* tex = k == 0 ? texA.Get() : texB.Get();
ID3D12Resource* up = k == 0 ? upA.Get() : upB.Get();
D3D12_RESOURCE_BARRIER b{};
b.Transition.pResource = tex; b.Transition.StateBefore = D3D12_RESOURCE_STATE_COMMON;
b.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
b.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
list->ResourceBarrier(1, &b);
D3D12_TEXTURE_COPY_LOCATION d{}, s{};
d.pResource = tex; d.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
s.pResource = up; s.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
s.PlacedFootprint.Footprint.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
s.PlacedFootprint.Footprint.Width = W; s.PlacedFootprint.Footprint.Height = H;
s.PlacedFootprint.Footprint.Depth = 1;
s.PlacedFootprint.Footprint.RowPitch = static_cast<UINT>(rowPitch);
list->CopyTextureRegion(&d, 0, 0, 0, &s, nullptr);
D3D12_RESOURCE_BARRIER b2 = b;
b2.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
b2.Transition.StateAfter = D3D12_RESOURCE_STATE_COMMON;
list->ResourceBarrier(1, &b2);
}
list->Close();
ID3D12CommandList* ls[] = { list.Get() };
ctx.directQueue()->ExecuteCommandLists(1, ls);
drain();
};
auto executeFlow = [&]() -> bool {
NV_OF_EXECUTE_INPUT_PARAMS_D3D12 in{};
NV_OF_EXECUTE_OUTPUT_PARAMS_D3D12 out{};
NV_OF_FENCE_POINT inF{}, outF{};
in.inputFrame = hB; // current
in.referenceFrame = hA; // previous
in.disableTemporalHints = NV_OF_TRUE;
in.numFencePoints = 1;
inF.fence = fence.Get(); inF.value = fenceVal; // inputs already done
in.fencePoint = &inF;
out.outputBuffer = hFlow;
out.outputCostBuffer = hCost;
outF.fence = fence.Get(); outF.value = fenceVal + 1;
out.fencePoint = &outF;
const NV_OF_STATUS s = fn.nvOFExecuteD3D12(hOF, &in, &out);
if (s != NV_OF_SUCCESS) {
veyra::log::error("nvof-proof", std::format("execute st={}", (int)s));
return false;
}
++fenceVal;
fence->SetEventOnCompletion(fenceVal, ev);
WaitForSingleObject(ev, 5000);
return true;
};
bool costTouchedLastCase = false;
auto readbackFlowCost = [&](const uint16_t** flow, const uint8_t** cost) {
// Sentinel pre-fill: 0xAA everywhere; if the cost texture was not
// written by NVOF, every byte stays 0xAA (0 is a VALID cost value).
{
uint8_t* m = nullptr;
rbCost->Map(0, nullptr, reinterpret_cast<void**>(&m));
std::memset(m, 0xAA, costPitch * GH);
rbCost->Unmap(0, nullptr);
}
alloc->Reset();
list->Reset(alloc.Get(), nullptr);
auto toCopy = [&](ID3D12Resource* tex, DXGI_FORMAT fmt, size_t pitch, ID3D12Resource* rb) {
D3D12_RESOURCE_BARRIER b{};
b.Transition.pResource = tex; b.Transition.StateBefore = D3D12_RESOURCE_STATE_COMMON;
b.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_SOURCE;
b.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
list->ResourceBarrier(1, &b);
D3D12_TEXTURE_COPY_LOCATION d{}, s{};
d.pResource = rb; d.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
d.PlacedFootprint.Footprint.Format = fmt;
d.PlacedFootprint.Footprint.Width = GW; d.PlacedFootprint.Footprint.Height = GH;
d.PlacedFootprint.Footprint.Depth = 1;
d.PlacedFootprint.Footprint.RowPitch = static_cast<UINT>(pitch);
s.pResource = tex; s.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
s.SubresourceIndex = 0;
list->CopyTextureRegion(&d, 0, 0, 0, &s, nullptr);
D3D12_RESOURCE_BARRIER b2 = b;
b2.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_SOURCE;
b2.Transition.StateAfter = D3D12_RESOURCE_STATE_COMMON;
list->ResourceBarrier(1, &b2);
};
toCopy(texFlow.Get(), DXGI_FORMAT_R16G16_SINT, flowPitch, rbFlow.Get());
toCopy(texCost.Get(), DXGI_FORMAT_R8_UINT, costPitch, rbCost.Get());
list->Close();
ID3D12CommandList* ls[] = { list.Get() };
ctx.directQueue()->ExecuteCommandLists(1, ls);
drain();
rbFlow->Map(0, nullptr, const_cast<void**>(reinterpret_cast<const void**>(flow)));
rbCost->Map(0, nullptr, const_cast<void**>(reinterpret_cast<const void**>(cost)));
costTouchedLastCase = false;
for (size_t i = 0; i < costPitch * GH; ++i) {
if ((*cost)[i] != 0xAA) { costTouchedLastCase = true; break; }
}
};
auto unmapFlowCost = [&](const uint16_t* flow, const uint8_t* cost) {
rbFlow->Unmap(0, nullptr);
rbCost->Unmap(0, nullptr);
(void)flow; (void)cost;
};
std::vector<CaseResult> results;
std::vector<uint8_t> shifted;
bool lastCostTouched = false;
for (const Case& c : cases) {
renderShifted(shifted, c.dx, c.dy);
uploadAB(base, shifted);
if (!executeFlow()) return 5;
const uint16_t* flowData = nullptr;
const uint8_t* costData = nullptr;
readbackFlowCost(&flowData, &costData);
CaseResult r;
r.name = c.name;
r.dxTrue = -c.dx; // current->previous: B moved +dx => vector -dx
r.dyTrue = -c.dy;
std::vector<double> dxs, dys, costs;
uint64_t nonZero = 0, total = 0;
const uint32_t mx = GW / 12, my = GH / 12;
for (uint32_t y = my; y < GH - my; y += 2) {
for (uint32_t x = mx; x < GW - mx; x += 2) {
const size_t fi = (static_cast<size_t>(y) * (flowPitch / 4) + x) * 2;
const int16_t rx = static_cast<int16_t>(flowData[fi]);
const int16_t ry = static_cast<int16_t>(flowData[fi + 1]);
dxs.push_back(rx / 32.0);
dys.push_back(ry / 32.0);
if (rx != 0 || ry != 0) ++nonZero;
costs.push_back(costData[static_cast<size_t>(y) * costPitch + x]);
++total;
}
}
unmapFlowCost(flowData, costData);
auto pctl = [](std::vector<double>& v, double q) {
if (v.empty()) return 0.0;
std::sort(v.begin(), v.end());
return v[std::min(v.size() - 1, static_cast<size_t>(q * (v.size() - 1)))];
};
r.dxMedian = pctl(dxs, 0.5);
r.dyMedian = pctl(dys, 0.5);
r.endpointErrMedian = std::sqrt((r.dxMedian - r.dxTrue) * (r.dxMedian - r.dxTrue) +
(r.dyMedian - r.dyTrue) * (r.dyMedian - r.dyTrue));
r.rawNonZeroFrac = total ? static_cast<double>(nonZero) / total : 0;
r.costP50 = pctl(costs, 0.5);
r.costP95 = pctl(costs, 0.95);
r.costMax = costs.empty() ? 0 : *std::max_element(costs.begin(), costs.end());
uint64_t gated = 0;
for (double cv : costs) if (cv >= 32.0) ++gated;
r.gatedFrac = costs.empty() ? 0 : static_cast<double>(gated) / costs.size();
const bool xOk = (c.dx == 0) ? std::fabs(r.dxMedian) <= 1.0
: (r.dxTrue > 0 ? r.dxMedian > 0 : r.dxMedian < 0);
const bool yOk = (c.dy == 0) ? std::fabs(r.dyMedian) <= 1.0
: (r.dyTrue > 0 ? r.dyMedian > 0 : r.dyMedian < 0);
r.signCorrect = xOk && yOk;
r.errorOk = r.endpointErrMedian <= 1.0;
results.push_back(r);
lastCostTouched = costTouchedLastCase;
veyra::log::info("nvof-proof", std::format(
"{}: true=({:.1f},{:.1f}) median=({:.2f},{:.2f}) err={:.2f}px nonZero={:.2f} cost p50/p95/max={:.0f}/{:.0f}/{:.0f} gated={:.2f} sign={} errOk={}",
c.name, r.dxTrue, r.dyTrue, r.dxMedian, r.dyMedian, r.endpointErrMedian,
r.rawNonZeroFrac, r.costP50, r.costP95, r.costMax, r.gatedFrac,
r.signCorrect ? "OK" : "BAD", r.errorOk ? "OK" : "BAD"));
}
// ---- Cost-confidence inverse proof (dx+8 case, quartile buckets) -------
double lowCostErr = -1, highCostErr = -1;
double regionCostP50[3] = { -1, -1, -1 };
double regionGatedFrac[3] = { -1, -1, -1 };
{
renderShifted(shifted, 8, 0);
uploadAB(base, shifted);
if (!executeFlow()) return 5;
const uint16_t* flowData = nullptr;
const uint8_t* costData = nullptr;
readbackFlowCost(&flowData, &costData);
std::vector<std::pair<double, double>> costErr;
const uint32_t mx = GW / 12, my = GH / 12;
for (uint32_t y = my; y < GH - my; ++y) {
for (uint32_t x = mx; x < GW - mx; ++x) {
const size_t fi = (static_cast<size_t>(y) * (flowPitch / 4) + x) * 2;
const double dx = static_cast<int16_t>(flowData[fi]) / 32.0;
costErr.emplace_back(costData[static_cast<size_t>(y) * costPitch + x],
std::fabs(dx - (-8.0)));
}
}
unmapFlowCost(flowData, costData);
std::sort(costErr.begin(), costErr.end());
const size_t n = costErr.size();
if (n > 100) {
const size_t q1 = n / 4, q3 = 3 * n / 4;
double lo = 0, hi = 0; size_t ln = 0, hn = 0;
for (size_t i = 0; i < q1; ++i) { lo += costErr[i].second; ++ln; }
for (size_t i = q3; i < n; ++i) { hi += costErr[i].second; ++hn; }
if (ln && hn) { lowCostErr = lo / ln; highCostErr = hi / hn; }
}
veyra::log::info("nvof-proof", std::format(
"cost-confidence: lowCostQuartileErr={:.3f}px highCostQuartileErr={:.3f}px inverse={}",
lowCostErr, highCostErr, (lowCostErr >= 0 && lowCostErr <= highCostErr) ? "OK" : "BAD"));
// s8-6: per-region (textured / textureless / noise) cost + error +
// gated statistics so the distribution is demonstrably non-degenerate.
const char* regionNames[3] = { "textured", "textureless", "noise" };
for (int reg = 0; reg < 3; ++reg) {
std::vector<double> rc, re;
uint64_t gatedCount = 0;
for (uint32_t y = my; y < GH - my; ++y) {
if (regionOfRow(y) != reg) continue;
for (uint32_t x = mx; x < GW - mx; ++x) {
const size_t fi = (static_cast<size_t>(y) * (flowPitch / 4) + x) * 2;
const double dxv = static_cast<int16_t>(flowData[fi]) / 32.0;
const double cv = costData[static_cast<size_t>(y) * costPitch + x];
rc.push_back(cv);
re.push_back(std::fabs(dxv - (-8.0)));
if (cv >= 32.0) ++gatedCount;
}
}
if (rc.empty()) continue;
std::sort(rc.begin(), rc.end());
std::sort(re.begin(), re.end());
auto pk = [](std::vector<double>& v, double q) { return v[std::min(v.size() - 1, static_cast<size_t>(q * (v.size() - 1)))]; };
veyra::log::info("nvof-proof", std::format(
"region {}: cells={} cost p05/p50/p95/max={:.0f}/{:.0f}/{:.0f}/{:.0f} |err| p50/p95={:.2f}/{:.2f}px gated={:.3f}",
regionNames[reg], rc.size(), pk(rc, 0.05), pk(rc, 0.5), pk(rc, 0.95), rc.back(),
pk(re, 0.5), pk(re, 0.95), static_cast<double>(gatedCount) / rc.size()));
regionCostP50[reg] = pk(rc, 0.5);
regionGatedFrac[reg] = static_cast<double>(gatedCount) / rc.size();
}
}
// ---- InfoQueue ----------------------------------------------------------
uint64_t errCount = 0, corrCount = 0;
if (iq.Get()) {
const UINT64 stored = iq->GetNumStoredMessages();
for (UINT64 k = 0; k < stored; ++k) {
SIZE_T len = 0;
if (iq->GetMessageW(k, nullptr, &len) != S_OK) break;
std::vector<char> buf(len);
D3D12_MESSAGE* m = reinterpret_cast<D3D12_MESSAGE*>(buf.data());
if (iq->GetMessageW(k, m, &len) == S_OK) {
if (m->Severity == D3D12_MESSAGE_SEVERITY_ERROR) {
++errCount;
veyra::log::error("nvof-proof", std::string("D3D12-ERROR: ") + (m->pDescription ? m->pDescription : ""));
} else if (m->Severity == D3D12_MESSAGE_SEVERITY_CORRUPTION) {
++corrCount;
veyra::log::error("nvof-proof", std::string("D3D12-CORRUPTION: ") + (m->pDescription ? m->pDescription : ""));
}
}
}
}
// ---- Reverse-order teardown ----------------------------------------------
auto unregister = [&](NvOFGPUBufferHandle& h, const char* name) {
if (h == nullptr) return;
NV_OF_UNREGISTER_RESOURCE_PARAMS_D3D12 p{};
p.hOFGpuBuffer = h;
const NV_OF_STATUS s = fn.nvOFUnregisterResourceD3D12(&p);
veyra::log::info("nvof-proof", std::format("unregister {} st={}", name, (int)s));
if (s == NV_OF_SUCCESS) h = nullptr;
};
unregister(hCost, "cost");
unregister(hFlow, "flow");
unregister(hB, "inputB");
unregister(hA, "inputA");
veyra::log::info("nvof-proof", std::format("nvOFDestroy st={}", (int)fn.nvOFDestroy(hOF)));
hOF = nullptr;
FreeLibrary(dll);
dll = nullptr;
// ---- Verdict + JSON -------------------------------------------------------
bool allSign = true, allErr = true;
for (auto& r : results) { allSign &= r.signCorrect; allErr &= r.errorOk; }
const bool confidenceInverse = lowCostErr >= 0 && lowCostErr <= highCostErr;
const bool flowWritten = !results.empty() && results[0].rawNonZeroFrac > 0.05;
bool anyCostTouched = lastCostTouched;
for (auto& r : results) if (r.costMax > 0) anyCostTouched = true;
const bool costWritten = anyCostTouched;
const bool pass = allSign && allErr && confidenceInverse && flowWritten && costWritten &&
errCount == 0 && corrCount == 0;
Json j;
j.str("probe", "nvof_proof");
j.integer("gridRequested", GRID);
{
std::string gl;
for (auto g : grids) gl += std::to_string(g) + " ";
j.str("capabilityGrids", gl);
}
j.integer("inputWidth", W);
j.integer("inputHeight", H);
j.integer("gridExtentW", GW);
j.integer("gridExtentH", GH);
j.str("inputFormat", "B8G8R8A8_UNORM");
j.str("rawFlowFormat", "R16G16_SINT");
j.str("costFormat", "R8_UINT");
j.boolean("flowWritten", flowWritten);
j.boolean("costWritten", costWritten);
j.num("confidenceLowCostErrPx", lowCostErr);
j.num("confidenceHighCostErrPx", highCostErr);
j.boolean("confidenceInverse", confidenceInverse);
j.field("regionTexturedCostP50", std::to_string(regionCostP50[0]));
j.field("regionTexturelessCostP50", std::to_string(regionCostP50[1]));
j.field("regionNoiseCostP50", std::to_string(regionCostP50[2]));
j.field("regionTexturedGatedFrac", std::to_string(regionGatedFrac[0]));
j.field("regionTexturelessGatedFrac", std::to_string(regionGatedFrac[1]));
j.field("regionNoiseGatedFrac", std::to_string(regionGatedFrac[2]));
j.integer("debugErrors", static_cast<long long>(errCount));
j.integer("debugCorruption", static_cast<long long>(corrCount));
{
std::string arr = "[";
for (size_t i = 0; i < results.size(); ++i) {
const CaseResult& r = results[i];
arr += std::format(
"{{\"name\":\"{}\",\"dxTrue\":{:.1f},\"dyTrue\":{:.1f},\"dxMedian\":{:.2f},\"dyMedian\":{:.2f},"
"\"endpointErrMedianPx\":{:.2f},\"rawNonZeroFrac\":{:.3f},\"costP50\":{:.0f},\"costP95\":{:.0f},"
"\"costMax\":{:.0f},\"gatedFrac\":{:.3f},\"signCorrect\":{},\"errorOk\":{}}}",
r.name, r.dxTrue, r.dyTrue, r.dxMedian, r.dyMedian, r.endpointErrMedian,
r.rawNonZeroFrac, r.costP50, r.costP95, r.costMax, r.gatedFrac,
r.signCorrect ? "true" : "false", r.errorOk ? "true" : "false");
if (i + 1 < results.size()) arr += ",";
}
arr += "]";
j.field("cases", arr);
}
j.boolean("pass", pass);
{
std::string out = "{" + j.body + "}\n";
FILE* f = std::fopen(jsonPath.c_str(), "wb");
if (f) { std::fwrite(out.data(), 1, out.size(), f); std::fclose(f); }
}
veyra::log::info("nvof-proof", std::format(
"VERDICT {} allSign={} allErr<=1px={} confInverse={} flowWritten={} costWritten={} dbgErr={} dbgCorr={}",
pass ? "PASS" : "FAIL", allSign, allErr, confidenceInverse, flowWritten, costWritten, errCount, corrCount));
CloseHandle(ev);
// All NVOF objects were unregistered and the session destroyed above in
// reverse order (logged). Releasing D3D12 resources under GPU-Based
// Validation crashes in the debug layer's teardown (observed 2026-09-04
// between VERDICT and this point); since the process exits immediately,
// let the OS reclaim them.
std::fflush(stdout);
veyra::log::info("nvof-proof", "teardown-complete (process exiting)");
ExitProcess(pass ? 0 : 1);
}