XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFE.SpaceEngineers.AgentBridge

【SpaceEngineer】AI调试插件

公开
关注 0 Fork 0 Star 0
UTF-8
#include <windows.h>
#include <d3d11_4.h>
#include <d3d12.h>
#include <DirectXMath.h>
#include <wrl/client.h>
#include <vector>
#include <cstdio>
#include <cmath>
#include <chrono>
#include <thread>
#include "../../src/XFE.SeRayTracing.Native/Native.h"
using Microsoft::WRL::ComPtr;
static void Check(HRESULT hr,const char* name) { if(FAILED(hr)) { printf("FAIL %s %08X\n",name,(unsigned)hr); exit(1); } }
struct Texture { ComPtr<ID3D11Texture2D> texture; ComPtr<ID3D11ShaderResourceView> srv; ComPtr<ID3D11RenderTargetView> rtv; };
static Texture Make(ID3D11Device* device,DXGI_FORMAT format,const void* values,int stride,UINT binds=D3D11_BIND_SHADER_RESOURCE) {
    Texture result; D3D11_TEXTURE2D_DESC desc{}; desc.Width=64; desc.Height=64; desc.MipLevels=1; desc.ArraySize=1; desc.Format=format; desc.SampleDesc.Count=1; desc.Usage=D3D11_USAGE_DEFAULT; desc.BindFlags=binds;
    D3D11_SUBRESOURCE_DATA data{}; data.pSysMem=values; data.SysMemPitch=64*stride;
    Check(device->CreateTexture2D(&desc,&data,&result.texture),"Test texture");
    Check(device->CreateShaderResourceView(result.texture.Get(),nullptr,&result.srv),"Test SRV");
    if(binds&D3D11_BIND_RENDER_TARGET) Check(device->CreateRenderTargetView(result.texture.Get(),nullptr,&result.rtv),"Test RTV");
    return result;
}
int main() {
    ComPtr<ID3D11Device> device; ComPtr<ID3D11DeviceContext> context;
    Check(D3D11CreateDevice(nullptr,D3D_DRIVER_TYPE_HARDWARE,nullptr,0,nullptr,0,D3D11_SDK_VERSION,&device,nullptr,&context),"Test D3D11 device");
    void* ray=nullptr; int hr=RtCreate(device.Get(),&ray); RtStatus status{}; RtGetStatus(ray,&status);
    if(hr<0) { printf("FAIL init 0x%08X: %s\n",hr,status.message); RtDestroy(ray); return 1; }
    printf("DXR tier %u\n",status.tier);
    std::vector<float> depth(64*64,.5f),normal(64*64*4,.5f),white(64*64*4,1),material(64*64*4,0);
    for(int y=0;y<8;y++) for(int x=0;x<8;x++) depth[y*64+x]=0;
    auto d=Make(device.Get(),DXGI_FORMAT_R32_FLOAT,depth.data(),4);
    auto n=Make(device.Get(),DXGI_FORMAT_R32G32B32A32_FLOAT,normal.data(),16);
    auto l=Make(device.Get(),DXGI_FORMAT_R32G32B32A32_FLOAT,white.data(),16,D3D11_BIND_SHADER_RESOURCE|D3D11_BIND_RENDER_TARGET);
    auto m=Make(device.Get(),DXGI_FORMAT_R32G32B32A32_FLOAT,material.data(),16);
    float vertices[]={-10,-10,1,0,0,0,0,0, 10,-10,1,1,0,0,0,0, 10,10,1,1,1,0,0,0, -10,10,1,0,1,0,0,0}; uint32_t indices[]={0,1,2,0,2,3},materials[]={1,1};
    std::vector<uint8_t> dds(132,0); auto set=[&](int offset,uint32_t value){memcpy(dds.data()+offset,&value,4);};
    set(0,0x20534444); set(4,124); set(12,1); set(16,1); set(28,1); set(76,32);set(80,0x41);set(88,32);set(92,0xff);set(96,0xff00);set(100,0xff0000);set(104,0xff000000);dds[129]=255;
    Check(RtMaterial(ray,1,dds.data(),(uint32_t)dds.size()),"Material DDS");
    Check(RtMesh(ray,1,vertices,4,indices,6,materials),"Test model");
    uint32_t bad[]={0,1,20}; if(RtMesh(ray,2,vertices,4,bad,3,nullptr)>=0) { puts("FAIL invalid indices accepted"); return 1; }
    float saved=vertices[0]; vertices[0]=NAN;
    if(RtMesh(ray,2,vertices,4,indices,6,nullptr)>=0) { puts("FAIL NaN vertices accepted"); return 1; } vertices[0]=saved;
    RtInstance instance{}; instance.transform[0]=instance.transform[5]=instance.transform[10]=1; instance.mesh=1;
    RtParameters p{};p.pathSettings[3]=1; for(int i=0;i<4;i++) { p.inverseViewProjection[i*4+i]=1; p.inverseView[i*4+i]=1; }
    p.sunDirection[2]=1; p.settings[0]=3; p.settings[1]=.3f; p.settings[2]=.15f;
    p.sizes[0]=p.sizes[1]=64; p.sizes[2]=p.sizes[3]=32;
    for(int i=0;i<3;i++) {
        // Restore input to avoid repeated darkening across synthetic test frames.
        context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
        hr=RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),&instance,1,&p);
        if(hr<0) { RtGetStatus(ray,&status); printf("FAIL frame %08X: %s\n",hr,status.message); RtDestroy(ray); return 1; }
    }
    ComPtr<ID3D11Texture2D> readback; D3D11_TEXTURE2D_DESC desc{}; l.texture->GetDesc(&desc); desc.Usage=D3D11_USAGE_STAGING; desc.BindFlags=0; desc.CPUAccessFlags=D3D11_CPU_ACCESS_READ;
    Check(device->CreateTexture2D(&desc,nullptr,&readback),"Readback"); context->CopyResource(readback.Get(),l.texture.Get());
    D3D11_MAPPED_SUBRESOURCE mapped{}; Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Read pixels");
    float center=*((float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4), sky=*(float*)mapped.pData;
    context->Unmap(readback.Get(),0); RtGetStatus(ray,&status);
    printf("frames=%llu meshes=%u triangles=%llu center=%.6f sky=%.6f cpu=%.3fms\n",status.frames,status.meshes,status.triangles,center,sky,status.cpuMilliseconds);
    bool valid=center>.55f && center<.65f && std::abs(sky-1)<.00001 && status.frames==3 && status.triangles==2;
    p.settings[3]=4; p.effects[1]=1; p.sunColor[0]=p.sunColor[1]=p.sunColor[2]=1;
    Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),&instance,1,&p),"Indirect frame");
    context->CopyResource(readback.Get(),l.texture.Get()); Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Indirect readback");
    float* color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4; float bounce=color[1]; valid=valid && color[0]<.001 && bounce>.01 && bounce<.025; context->Unmap(readback.Get(),0);
    // Move the green mesh behind the camera: it is absent from raster inputs but
    // must remain visible to an actual reflection ray through the TLAS.
    instance.transform[11]=-2; p.settings[3]=3; p.effects[0]=1;
    for(size_t i=0;i<normal.size();i+=4) normal[i]=1; // receiver normal faces the camera (-Z)
    context->UpdateSubresource(n.texture.Get(),0,nullptr,normal.data(),64*16,0);
    context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP);
    Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),&instance,1,&p),"Off-screen reflection frame");
    D3D11_PRIMITIVE_TOPOLOGY topology;context->IAGetPrimitiveTopology(&topology);valid=valid && topology==D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP;
    context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Reflection readback");
    color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4; float reflection=color[1];
    // A dielectric now has a neutral GGX sun highlight as well as its green
    // diffuse lobe. The previous Lambert-only expectation of zero red is wrong.
    printf("GGX reflection RGB=%.6f,%.6f,%.6f\n",color[0],color[1],color[2]);
    valid=valid && color[0]>.01 && color[0]<.1 && std::abs(color[0]-color[2])<.001 && reflection-color[0]>.3 && reflection-color[0]<.34;
    context->Unmap(readback.Get(),0);
    printf("green indirect=%.6f offscreen reflection=%.6f preserved state=%u\n",bounce,reflection,topology);
    // Reproject a stationary receiver across jittered GI samples and check that
    // the converged radiance remains stable. The geometry exists only in DXR.
    instance.transform[11]=0; p.settings[3]=4;
    for(size_t i=0;i<normal.size();i+=4) normal[i]=.5f;
    context->UpdateSubresource(n.texture.Get(),0,nullptr,normal.data(),64*16,0);
    for(int i=0;i<4;i++) p.previousViewProjection[i*4+i]=1;
    float low=1,high=0;
    for(int frame=0;frame<32;frame++) {
        p.history[0]=frame>0?1.0f:0.0f;
        Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),&instance,1,&p),"Temporal GI frame");
        context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Temporal readback");
        color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;
        if(frame>=16) { if(color[1]<low) low=color[1]; if(color[1]>high) high=color[1]; }
        context->Unmap(readback.Get(),0);
    }
    valid=valid && low>.016 && high<.019 && high-low<.001;
    printf("converged GI range=%.6f..%.6f\n",low,high);
    RtGetStatus(ray,&status); auto beforeRetire=status.frames;
    uint32_t keep=3; Check(RtRetainMeshes(ray,&keep,1),"Retire old model");
    Check(RtMesh(ray,3,vertices,4,indices,6,materials),"Replacement model");instance.mesh=3;p.history[0]=0;
    Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),&instance,1,&p),"Incremental geometry upload");
    // A large unused vertex range forces compaction when retired. The surviving
    // BLAS and its textured shading must still match after index rebasing.
    std::vector<float> padding(100004*8,0);memcpy(padding.data(),vertices,sizeof(vertices));
    Check(RtMesh(ray,4,padding.data(),100004,indices,6,materials),"Temporary large range");
    Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),&instance,1,&p),"Build temporary range");
    Check(RtRetainMeshes(ray,&keep,1),"Compact retired ranges");
    Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),&instance,1,&p),"Compacted scene frame");
    RtGetStatus(ray,&status);valid=valid && status.frames==beforeRetire+3 && status.meshes==1 && status.triangles==2;
    context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Compacted scene readback");
    color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;valid=valid && color[0]<.001 && color[1]>.01 && color[1]<.025;
    printf("retired/compacted meshes=%u triangles=%llu continuous frames=%llu green=%.6f\n",status.meshes,status.triangles,status.frames-beforeRetire,color[1]);
    context->Unmap(readback.Get(),0);
    // A remote planet must remain byte-for-byte unchanged. This exercises FP16
    // overflow avoidance, not just a sky pixel with depth zero.
    p.settings[3]=0; p.inverseViewProjection[14]=100000;
    context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
    Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),&instance,1,&p),"Far planet frame");
    context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Far planet readback");
    float planet=*((float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4);
    valid=valid && std::isfinite(planet) && std::abs(planet-1)<.00001;context->Unmap(readback.Get(),0);
    printf("far planet unchanged=%.6f\n",planet);
    // Material-aware transmission: the same geometry must cease behaving as
    // an opaque occluder, and tinted glass must also work against a sky pixel
    // where no opaque G-buffer surface exists.
    p.inverseViewProjection[14]=0;p.transport[0]=3;p.transport[1]=64;p.transport[2]=1;
    RtGlassMaterial glass{{.7f,1,1,.04f},{1.52f,.01f,0,0}};
    Check(RtGlassMaterials(ray,&glass,1),"Dielectric parameters");
    uint32_t glassIds[]={0x40000000,0x40000000};keep=5;
    Check(RtRetainMeshes(ray,&keep,1),"Isolated glass scene");
    Check(RtMesh(ray,5,vertices,4,indices,6,glassIds),"Glass geometry");instance.mesh=5;
    context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
    Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),&instance,1,&p),"Transmission shadow frame");
    context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Transmission readback");
    color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;float transparentShadow=color[0];
    valid=valid && transparentShadow>.88 && transparentShadow<1;context->Unmap(readback.Get(),0);
    p.settings[3]=5;
    std::fill(depth.begin(),depth.end(),0.0f);context->UpdateSubresource(d.texture.Get(),0,nullptr,depth.data(),64*4,0);
    Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),&instance,1,&p),"Sky-backed glass frame");
    context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Sky-backed glass readback");
    color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;
    printf("glass shadow=%.6f (opaque %.6f); sky-backed glass transmission=%.6f,%.6f,%.6f\n",transparentShadow,center,color[0],color[1],color[2]);
    valid=valid && color[0]>.65 && color[0]<.7 && color[1]>.95 && color[1]<.97 && std::abs(color[1]-color[2])<.001;
    context->Unmap(readback.Get(),0);
    // Triplanar voxel shading uses each vertex's material indices; it must not
    // interpret those indices as an HSV paint tint.
    uint32_t voxelIds[]={0x80000000,0x80000000};keep=6;
    Check(RtRetainMeshes(ray,&keep,1),"Isolated voxel scene");
    for(int v=0;v<4;v++){vertices[v*8+5]=vertices[v*8+6]=1;vertices[v*8+7]=.1f;}
    Check(RtMesh(ray,6,vertices,4,indices,6,voxelIds),"Voxel triplanar geometry");instance.mesh=6;
    std::fill(depth.begin(),depth.end(),.5f);context->UpdateSubresource(d.texture.Get(),0,nullptr,depth.data(),64*4,0);
    p.settings[3]=4;p.history[0]=0;
    Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),&instance,1,&p),"Voxel material frame");
    context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Voxel material readback");
    color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;
    printf("voxel triplanar RGB=%.6f,%.6f,%.6f\n",color[0],color[1],color[2]);
    valid=valid && color[0]<.001 && color[1]>.015 && color[2]<.001;context->Unmap(readback.Get(),0);
    RtLight lamp{{0,0,0,8},{0,1,0,3},{0,0,0,-1},{1,0,0,0}};
    Check(RtLights(ray,&lamp,1),"Local fixture snapshot");p.transport[3]=1;
    Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),&instance,1,&p),"RIS local lighting frame");
    context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Local lighting readback");
    color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;
    printf("local fixture green indirect=%.6f\n",color[1]);valid=valid && color[0]<.001 && color[1]>.1 && color[2]<.001;
    context->Unmap(readback.Get(),0);Check(RtLights(ray,nullptr,0),"Disable local fixture");p.transport[3]=0;
    // A second diffuse wall must contribute only when the path budget permits
    // another bounce. Use actual scene geometry, not an injected history value.
    uint32_t keepBoth[]={6,7};Check(RtRetainMeshes(ray,keepBoth,2),"Retain two-wall scene");
    uint32_t neutral[]={0,0};Check(RtMesh(ray,7,vertices,4,indices,6,neutral),"Second wall");
    RtInstance room[]={instance,instance};room[1].mesh=7;room[1].transform[11]=-3;
    float pathValues[2]{};
    for(int mode=0;mode<2;mode++) {
        p.transport[0]=mode?3.0f:1.0f;
        for(int frame=0;frame<32;frame++) {
            p.history[0]=frame?1.0f:0.0f;
            Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),room,2,&p),"Multi-bounce room frame");
        }
        context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Multi-bounce readback");
        color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;pathValues[mode]=color[1];context->Unmap(readback.Get(),0);
    }
    printf("one bounce=%.6f three bounces=%.6f\n",pathValues[0],pathValues[1]);
    valid=valid && pathValues[0]>.015 && pathValues[1]>pathValues[0]*1.1 && pathValues[1]<.3;
    // Primary refraction: a slanted camera ray through a known parallel slab
    // must shift a visible background gradient. This exercises geometry, screen
    // reprojection, depth validation and subpixel interpolation together.
    uint32_t refractKeep[]={8,9};Check(RtRetainMeshes(ray,refractKeep,2),"Refraction scene");
    glass.optics[1]=.3f;Check(RtGlassMaterials(ray,&glass,1),"Reference slab thickness");
    Check(RtMesh(ray,8,vertices,4,indices,6,glassIds),"Reference slab");
    std::vector<float> background(vertices,vertices+32);for(int v=0;v<4;v++) background[v*8+2]=2;
    Check(RtMesh(ray,9,background.data(),4,indices,6,neutral),"Background plane");
    room[0]=instance;room[0].mesh=8;room[1]=instance;room[1].mesh=9;
    p.settings[1]=p.settings[2]=p.settings[3]=0;p.effects[0]=p.effects[1]=0;p.history[0]=0;
    p.inverseViewProjection[12]=1;p.inverseViewProjection[10]=4;
    for(int i=0;i<4;i++) p.viewProjection[i*4+i]=1;p.viewProjection[12]=-1;p.viewProjection[10]=.25f;
    for(int y=0;y<64;y++) for(int x=0;x<64;x++) {white[(y*64+x)*4]=x/63.0f;white[(y*64+x)*4+1]=.25f;white[(y*64+x)*4+2]=.25f;}
    context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
    Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),room,2,&p),"Primary refraction frame");
    context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Primary refraction readback");
    color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;
    printf("primary slab background red=%.6f (unrefracted %.6f)\n",color[0],32/63.0f);
    valid=valid && color[0]>.46 && color[0]<.5 && std::abs(color[1]-.25)<.0001;
    context->Unmap(readback.Get(),0);
    // A pair of fixtures should produce a steady reflected signal even
    // without history. The brighter blue fixture in the next case keeps
    // red/green in the stochastic remainder for the denoising tests.
    uint32_t temporalKeep[]={8,10};Check(RtRetainMeshes(ray,temporalKeep,2),"Glass temporal scene");
    for(int v=0;v<4;v++) background[v*8+2]=-2;
    Check(RtMesh(ray,10,background.data(),4,indices,6,neutral),"Reflected wall");room[1].mesh=10;
    RtLight lamps[]={
        {{0,0,0,8},{1,0,0,3},{0,0,0,-1},{1,0,0,0}},
        {{0,0,0,8},{0,1,0,3},{0,0,0,-1},{1,0,0,0}},
        {{0,0,0,8},{0,0,1,40},{0,0,0,-1},{1,0,0,0}}};
    Check(RtLights(ray,lamps,2),"Exact lamp pair");
    p.inverseViewProjection[12]=0;p.inverseViewProjection[10]=1;
    for(int i=0;i<16;i++) p.previousViewProjection[i]=(i%5==0)?1.0f:0.0f;
    // Camera rays use normalize(x,y,1). Reflected virtual points are farther
    // than the pane, so history must use the matching perspective division.
    p.previousViewProjection[11]=1;p.previousViewProjection[15]=0;
    p.transport[0]=1;p.transport[3]=2;p.effects[0]=1;p.effects[1]=0;
    std::fill(depth.begin(),depth.end(),0.0f);context->UpdateSubresource(d.texture.Get(),0,nullptr,depth.data(),64*4,0);
    std::fill(white.begin(),white.end(),0.0f);
    p.history[0]=0;p.pathSettings[3]=0;
    float minGreen=1,maxGreen=0,minRed=1;
    for(int frame=0;frame<16;frame++) {
        context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
        Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),room,2,&p),"Exact fixture pair reflection");
        context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Fixture pair variance");
        color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;
        minGreen=fmin(minGreen,color[1]);maxGreen=fmax(maxGreen,color[1]);minRed=fmin(minRed,color[0]);
        context->Unmap(readback.Get(),0);
    }
    printf("raw fixture pair green range %.6f..%.6f red >= %.6f\n",minGreen,maxGreen,minRed);
    valid=valid && minGreen>.002 && minRed>.002 && maxGreen-minGreen<.00002;
    p.pathSettings[3]=1;
    Check(RtLights(ray,lamps,3),"Noisy fixture remainder");
    double deviations[2]{},means[2]{};
    for(int mode=0;mode<2;mode++) {
        double sum=0,squares=0;
        for(int frame=0;frame<96;frame++) {
            p.history[0]=(mode && frame)?1.0f:0.0f;
            context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
            Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),room,2,&p),"Glass accumulation frame");
            if(frame<64) continue;
            context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Glass variance readback");
            color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;
            sum+=color[1];squares+=color[1]*color[1];context->Unmap(readback.Get(),0);
        }
        means[mode]=sum/32;deviations[mode]=sqrt(fmax(0,squares/32-means[mode]*means[mode]));
    }
    printf("glass temporal green mean %.6f -> %.6f; standard deviation %.6f -> %.6f\n",means[0],means[1],deviations[0],deviations[1]);
    // Changing the illumination is not a pane/hit disocclusion. Converged red
    // energy must disappear on the first green-only frame, including in hybrid mode.
    lamps[0].colorIntensity[0]=0;lamps[0].colorIntensity[1]=1;
    Check(RtLights(ray,lamps,3),"Reflected illumination changes to green");
    context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
    Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),room,2,&p),"First changed glass illumination");
    context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Glass illumination response");
    color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;
    float changedGlassRed=color[0];context->Unmap(readback.Get(),0);
    p.history[0]=0;context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
    Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),room,2,&p),"Fresh glass illumination reference");
    context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Fresh glass illumination readback");
    color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;
    printf("changed glass old-red excess=%.6f green=%.6f\n",changedGlassRed-color[0],color[1]);
    valid=valid && color[1]>.002 && std::abs(changedGlassRed-color[0])<.0001;context->Unmap(readback.Get(),0);
    valid=valid && means[0]>.001 && means[1]>means[0]*.6 && means[1]<means[0]*1.4 && deviations[0]>.00001 && deviations[1]<deviations[0]*.5;
    // Motion/disocclusion may legitimately reject all history. The current-frame
    // reflected-surface filter must still suppress stochastic lamp noise.
    lamps[0].colorIntensity[0]=1;lamps[0].colorIntensity[1]=0;Check(RtLights(ray,lamps,3),"Fresh-frame noisy lamps");
    // A 64px test views a small patch: preserve the angular footprint of a
    // native-4K local denoiser instead of blurring the entire GGX highlight.
    p.inverseViewProjection[0]=p.inverseViewProjection[5]=.1f;
    for(int mode=0;mode<2;mode++) {
        p.history[0]=0;p.pathSettings[3]=(float)mode;double sum=0,squares=0;
        for(int frame=0;frame<64;frame++) {
            p.inverseViewProjection[12]=.012f*std::sin(frame*.31f);
            context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
            Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),room,2,&p),"Fresh moving glass filtering");
            context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Fresh glass variance");
            color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;sum+=color[1];squares+=color[1]*color[1];context->Unmap(readback.Get(),0);
        }
        means[mode]=sum/64;deviations[mode]=sqrt(fmax(0,squares/64-means[mode]*means[mode]));
    }
    printf("history-free moving glass noise %.6f -> %.6f, mean %.6f -> %.6f\n",deviations[0],deviations[1],means[0],means[1]);
    valid=valid && means[0]>.001 && means[1]>means[0]*.8 && means[1]<means[0]*1.2 && deviations[1]<deviations[0]*.15;
    // The dominant second-pane transmission escapes to sky, while its minor
    // reflected branch hits a wall behind the camera-side pane. Previously this
    // returned a zero-roughness guide and left the residual branch unfiltered.
    auto escapePane=background,escapeWall=background;
    for(int v=0;v<4;v++){escapePane[v*8+2]=-1;escapeWall[v*8+2]=2;}
    Check(RtMesh(ray,27,escapePane.data(),4,indices,6,glassIds),"Escape branch pane");
    Check(RtMesh(ray,28,escapeWall.data(),4,indices,6,neutral),"Escape residual wall");
    RtInstance escape[]={room[0],room[0],room[1]};escape[1].mesh=27;escape[2].mesh=28;
    for(int mode=0;mode<2;mode++) {
        p.history[0]=0;p.pathSettings[3]=(float)mode;double sum=0,squares=0;
        for(int frame=0;frame<64;frame++) {
            p.inverseViewProjection[12]=.012f*std::sin(frame*.31f);
            context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
            Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),escape,3,&p),"Escaping glass residual");
            context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Escape residual variance");
            color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;sum+=color[1];squares+=color[1]*color[1];context->Unmap(readback.Get(),0);
        }
        means[mode]=sum/64;deviations[mode]=sqrt(fmax(0,squares/64-means[mode]*means[mode]));
    }
    printf("escaping glass residual noise %.6f -> %.6f, mean %.6f -> %.6f\n",deviations[0],deviations[1],means[0],means[1]);
    valid=valid && means[0]>.00001 && means[1]>means[0]*.8 && means[1]<means[0]*1.2 && deviations[1]<deviations[0]*.2;
    Check(RtRetainMeshes(ray,temporalKeep,2),"Restore reflected wall scene");
    p.inverseViewProjection[0]=p.inverseViewProjection[5]=1;p.inverseViewProjection[12]=0;p.pathSettings[3]=1;
    for(int i=0;i<16;i++) p.previousViewProjection[i]=(i%5==0)?1.0f:0.0f;
    // The same noisy lighting on an opaque receiver must accumulate too. A
    // stochastic miss/hit must not reset its temporal history every other frame.
    keep=10;Check(RtRetainMeshes(ray,&keep,1),"Opaque reflection scene");
    p.settings[3]=3;p.transport[2]=0;
    std::fill(depth.begin(),depth.end(),.5f);context->UpdateSubresource(d.texture.Get(),0,nullptr,depth.data(),64*4,0);
    for(size_t i=0;i<normal.size();i+=4) normal[i]=1;
    context->UpdateSubresource(n.texture.Get(),0,nullptr,normal.data(),64*16,0);
    for(int mode=0;mode<2;mode++) {
        double sum=0,squares=0;
        for(int frame=0;frame<96;frame++) {
            p.history[0]=(mode && frame)?1.0f:0.0f;
            Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),room+1,1,&p),"Opaque accumulation frame");
            if(frame<64) continue;
            context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Opaque variance readback");
            color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;
            sum+=color[1];squares+=color[1]*color[1];context->Unmap(readback.Get(),0);
        }
        means[mode]=sum/32;deviations[mode]=sqrt(fmax(0,squares/32-means[mode]*means[mode]));
    }
    printf("opaque temporal green mean %.6f -> %.6f; standard deviation %.6f -> %.6f\n",means[0],means[1],deviations[0],deviations[1]);
    valid=valid && means[0]>.01 && means[1]>means[0]*.8 && means[1]<means[0]*1.2 && deviations[1]<deviations[0]*.5;
    // Ray and raster voxel LODs have different depths. Coverage may not be
    // clipped to the opaque receiver + 10 cm (this plane is 50 cm behind it).
    keep=11;Check(RtRetainMeshes(ray,&keep,1),"Coverage scene");
    Check(RtMesh(ray,11,vertices,4,indices,6,voxelIds),"Coverage plane");instance.mesh=11;
    p.settings[3]=6;p.history[0]=0;
    Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),&instance,1,&p),"Depth mismatch coverage frame");
    context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Coverage readback");
    color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;
    printf("voxel coverage behind raster receiver=%.6f\n",color[0]);valid=valid && color[0]>.999;context->Unmap(readback.Get(),0);
    keep=12;Check(RtRetainMeshes(ray,&keep,1),"Foreground guard scene");
    Check(RtMesh(ray,12,vertices,4,indices,6,glassIds),"Background glass");instance.mesh=12;
    p.settings[3]=5;p.transport[2]=1;
    Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),&instance,1,&p),"Opaque foreground frame");
    context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Foreground readback");
    color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;
    printf("glass behind opaque foreground=%.6f\n",color[0]);valid=valid && color[0]<.001;context->Unmap(readback.Get(),0);
    // Authored extension emission is linear data and radiates without a sun/lamp.
    auto ext=dds;ext[128]=0;ext[129]=128;ext[130]=ext[131]=0;
    Check(RtMaterial(ray,2,ext.data(),(uint32_t)ext.size(),1),"Linear emission map");
    keep=13;Check(RtRetainMeshes(ray,&keep,1),"Emissive-only scene");
    uint32_t emissiveIds[]={1|(2<<10),1|(2<<10)};
    Check(RtMesh(ray,13,background.data(),4,indices,6,emissiveIds),"Emissive text plane");instance.mesh=13;
    Check(RtLights(ray,nullptr,0),"No lamps");p.sunColor[0]=p.sunColor[1]=p.sunColor[2]=0;
    p.settings[3]=3;p.transport[2]=0;p.effects[0]=1;p.history[0]=0;
    for(int frame=0;frame<3;frame++)Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),&instance,1,&p),"Emission reflection frame");
    context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Emission readback");
    color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;
    printf("unlit reflected emission green=%.6f (linear mask expected 0.516)\n",color[1]);
    valid=valid && color[1]>.51 && color[1]<.525 && color[0]<.001;context->Unmap(readback.Get(),0);
    valid=(RtMaterial(ray,3,nullptr,0,1)==1) && valid;
    keep=16;Check(RtRetainMeshes(ray,&keep,1),"Missing extension scene");
    uint32_t missingIds[]={1|(3<<10),1|(3<<10)};
    Check(RtMesh(ray,16,background.data(),4,indices,6,missingIds),"Missing extension plane");instance.mesh=16;
    for(int frame=0;frame<3;frame++)Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),&instance,1,&p),"Missing emission reflection");
    context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Missing emission readback");
    color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;
    printf("missing extension green=%.6f (ambient only)\n",color[1]);
    valid=valid && color[1]>.017 && color[1]<.019;context->Unmap(readback.Get(),0);
    // Real D3D11 render-target content, behind the camera, reflected by real DXR glass.
    uint32_t liveKeep[]={14,15};Check(RtRetainMeshes(ray,liveKeep,2),"Live LCD scene");
    Check(RtMesh(ray,14,vertices,4,indices,6,glassIds),"LCD receiver glass");
    uint32_t screenIds[]={1u<<20,1u<<20};Check(RtMesh(ray,15,background.data(),4,indices,6,screenIds),"LCD geometry");
    room[0]=instance;room[0].mesh=14;room[1]=instance;room[1].mesh=15;
    RtSurfaceMaterial surfaces[2]{};for(auto& s:surfaces)for(float& v:s.colorEmission)v=1;
    surfaces[1].texture=992;surfaces[1].flags=7;Check(RtSurfaceMaterials(ray,surfaces,2),"Live LCD material");
    std::vector<float> screen(64*64*4,0);auto live=Make(device.Get(),DXGI_FORMAT_R32G32B32A32_FLOAT,screen.data(),16,D3D11_BIND_SHADER_RESOURCE|D3D11_BIND_RENDER_TARGET);
    if(RtDynamicMaterial(ray,1,live.srv.Get())>=0){puts("FAIL live material accepted static slot");return 1;}
    std::fill(depth.begin(),depth.end(),0.0f);context->UpdateSubresource(d.texture.Get(),0,nullptr,depth.data(),64*4,0);
    p.settings[3]=0;p.transport[2]=1;p.history[0]=0;
    for(int mode=0;mode<3;mode++) {
        std::fill(screen.begin(),screen.end(),0.0f);for(size_t i=0;i<screen.size();i+=4)screen[i+(mode?2:0)]=1;
        context->UpdateSubresource(live.texture.Get(),0,nullptr,screen.data(),64*16,0);
        Check(RtDynamicMaterial(ray,992,live.srv.Get()),"Copy current LCD pixels");
        if(mode==2){surfaces[1].flags|=8;Check(RtSurfaceMaterials(ray,surfaces,2),"Hide LCD");}
        for(int frame=0;frame<3;frame++) {
            context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
            Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),room,2,&p),"Live glass reflection");
        }
        context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Live LCD readback");
        color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;
        printf("live LCD mode %d glass RGB=%.6f,%.6f,%.6f\n",mode,color[0],color[1],color[2]);
        valid=valid && (mode==0?(color[0]>.02 && color[2]<.001):mode==1?(color[2]>.02 && color[0]<.001):(color[0]<.001 && color[2]<.001));
        context->Unmap(readback.Get(),0);
    }
    // A live LCD change must be visible in the very first reflected frame even
    // with a fully converged history. Fine text edges must not enter a blur.
    surfaces[1].flags=7;Check(RtSurfaceMaterials(ray,surfaces,2),"Restore LCD");
    room[0].key=914;room[1].key=915;p.sizes[2]=p.sizes[3]=64;
    p.pathSettings[0]=1;p.pathSettings[1]=2;p.pathSettings[3]=1;p.transport[0]=1;
    std::fill(white.begin(),white.end(),0.0f);
    for(int y=0;y<64;y++)for(int x=0;x<64;x++) {
        int i=(y*64+x)*4;screen[i]=((x/2)%2)?1.0f:0.0f;screen[i+1]=screen[i+2]=0;screen[i+3]=1;
    }
    context->UpdateSubresource(live.texture.Get(),0,nullptr,screen.data(),64*16,0);
    Check(RtDynamicMaterial(ray,992,live.srv.Get()),"Fine LCD stripe texture");
    for(int frame=0;frame<40;frame++) {
        p.history[0]=frame?1.0f:0.0f;context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
        Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),room,2,&p),"Converged fine LCD reflection");
    }
    context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"LCD detail readback");
    double lcdError=0,lcdSignal=0;
    for(int y=16;y<48;y++)for(int x=16;x<48;x++) {
        double px=(x+.5)/32-1,py=1-(y+.5)/32;
        // z=1 pane reflects the z=-2 plane to virtual z=4. The ray origin
        // offset is .012 m toward the camera; texture filtering is bilinear.
        double texel=(.5+px*(4-.012)/20)*64-.5,fraction=texel-floor(texel);
        int a=(int)floor(texel);double value=((a/2)%2)*(1-fraction)+(((a+1)/2)%2)*fraction;
        double c=1/sqrt(1+px*px+py*py),ior=1.52,ct=sqrt(1-(1-c*c)/(ior*ior));
        double rs=(c-ior*ct)/(c+ior*ct),rp=(ior*c-ct)/(ior*c+ct),expected=value*(rs*rs+rp*rp)*.5*p.effects[0];
        double actual=((float*)((char*)mapped.pData+y*mapped.RowPitch))[x*4];
        lcdError=(std::max)(lcdError,std::abs(actual-expected));lcdSignal=(std::max)(lcdSignal,expected);
    }
    context->Unmap(readback.Get(),0);
    printf("sharp glass LCD stripe max error=%.8f signal=%.6f\n",lcdError,lcdSignal);valid=valid && lcdSignal>.04 && lcdError<.001;
    double movingLcdError=0;
    for(float offset : { .12f, .25f, -.1f, 0.0f }) {
        p.inverseViewProjection[12]=offset;context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
        Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),room,2,&p),"LCD camera movement and reversal");
        context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Moving LCD detail readback");
        for(int y=16;y<48;y++)for(int x=16;x<48;x++) {
            double px=(x+.5)/32-1+offset,py=1-(y+.5)/32;
            double texel=(.5+px*(4-.012)/20)*64-.5,fraction=texel-floor(texel);int a=(int)floor(texel);
            double value=((a/2)%2)*(1-fraction)+(((a+1)/2)%2)*fraction;
            double c=1/sqrt(1+px*px+py*py),ior=1.52,ct=sqrt(1-(1-c*c)/(ior*ior));
            double rs=(c-ior*ct)/(c+ior*ct),rp=(ior*c-ct)/(ior*c+ct),expected=value*(rs*rs+rp*rp)*.5*p.effects[0];
            double actual=((float*)((char*)mapped.pData+y*mapped.RowPitch))[x*4];
            movingLcdError=(std::max)(movingLcdError,std::abs(actual-expected));
        }
        context->Unmap(readback.Get(),0);
        // The off-center perspective ray x/z = screenX + offset.
        memset(p.previousViewProjection,0,64);p.previousViewProjection[0]=p.previousViewProjection[5]=1;p.previousViewProjection[8]=-offset;p.previousViewProjection[11]=1;
    }
    printf("moving/reversing glass LCD max stale/detail error=%.8f\n",movingLcdError);valid=valid && movingLcdError<.001;
    for(int i=0;i<16;i++)p.previousViewProjection[i]=(i%5==0)?1.0f:0;
    std::fill(screen.begin(),screen.end(),0.0f);for(size_t i=0;i<screen.size();i+=4)screen[i+2]=1;
    context->UpdateSubresource(live.texture.Get(),0,nullptr,screen.data(),64*16,0);Check(RtDynamicMaterial(ray,992,live.srv.Get()),"Changed LCD blue");
    context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
    Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),room,2,&p),"First changed LCD frame");
    context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"LCD response readback");
    color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;
    printf("first changed LCD RGB=%.6f,%.6f,%.6f\n",color[0],color[1],color[2]);valid=valid && color[0]<.0001 && color[2]>.04;
    context->Unmap(readback.Get(),0);
    // Real LCDs render new pixels into the same SRV; they do not re-register
    // that resource with the bridge on every content update.
    std::fill(screen.begin(),screen.end(),0.0f);for(size_t i=0;i<screen.size();i+=4)screen[i]=1;
    context->UpdateSubresource(live.texture.Get(),0,nullptr,screen.data(),64*16,0);
    context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
    Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),room,2,&p),"Same-SRV LCD content update");
    context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Same-SRV LCD response");
    color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;
    printf("same-SRV changed LCD RGB=%.6f,%.6f,%.6f\n",color[0],color[1],color[2]);valid=valid && color[0]>.04 && color[2]<.0001;
    context->Unmap(readback.Get(),0);

    // Cockpit reflections often cross another pane before the real LCD. This
    // must not randomly drop text or classify all its radiance as noisy light.
    auto secondPane=background;for(int v=0;v<4;v++)secondPane[v*8+2]=-1;
    Check(RtMesh(ray,24,secondPane.data(),4,indices,6,glassIds),"Secondary cockpit pane");
    RtInstance layered[]={room[0],room[1],room[0]};layered[2].mesh=24;layered[2].key=924;
    double layeredMin=1,layeredMax=0;p.history[0]=0;p.pathSettings[3]=0;
    for(int frame=0;frame<48;frame++) {
        context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
        Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),layered,3,&p),"Layered cockpit reflection");
        context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Layered LCD readback");
        color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;
        layeredMin=(std::min)(layeredMin,double(color[0]));layeredMax=(std::max)(layeredMax,double(color[0]));context->Unmap(readback.Get(),0);
    }
    printf("two-pane LCD with no history/filter minimum=%.6f maximum=%.6f\n",layeredMin,layeredMax);
    valid=valid && layeredMin>.025 && layeredMax-layeredMin<.002;

    // PT must replace raster lighting using the primary surface, preserve a
    // missing primary surface, and work at native resolution as well as half.
    keep=17;Check(RtRetainMeshes(ray,&keep,1),"Primary PT scene");
    Check(RtMesh(ray,17,vertices,4,indices,6,emissiveIds),"Primary emissive surface");
    instance.mesh=17;instance.transform[11]=0;
    p.transport[0]=1;p.transport[2]=0;p.settings[3]=0;p.history[0]=0;
    p.pathSettings[0]=1;p.pathSettings[1]=1;p.pathSettings[3]=0;
    std::fill(white.begin(),white.end(),.2f);
    for(int mode=0;mode<3;mode++) {
        p.sizes[2]=p.sizes[3]=mode==1?64:32;
        std::fill(depth.begin(),depth.end(),mode==2?.5f:1.0f);
        context->UpdateSubresource(d.texture.Get(),0,nullptr,depth.data(),64*4,0);
        for(int frame=0;frame<3;frame++) {
            context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
            Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),&instance,1,&p),"Primary PT frame");
        }
        context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Primary PT readback");
        color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;
        printf("primary PT mode %d RGB=%.6f,%.6f,%.6f\n",mode,color[0],color[1],color[2]);
        valid=valid && (mode==2?(std::abs(color[0]-.2)<.001 && std::abs(color[1]-.2)<.001):(color[0]<.001 && color[1]>.49 && color[1]<.505 && color[2]<.001));
        context->Unmap(readback.Get(),0);
    }
    uint32_t pathKeep[]={18,19};Check(RtRetainMeshes(ray,pathKeep,2),"PT bounce scene");
    Check(RtMesh(ray,18,vertices,4,indices,6,neutral),"Unlit primary receiver");
    Check(RtMesh(ray,19,background.data(),4,indices,6,emissiveIds),"Offscreen emissive bounce wall");
    room[0]=instance;room[0].mesh=18;room[1]=instance;room[1].mesh=19;
    std::fill(depth.begin(),depth.end(),1.0f);context->UpdateSubresource(d.texture.Get(),0,nullptr,depth.data(),64*4,0);
    p.pathSettings[1]=8;p.pathSettings[3]=1;
    float pathGreen[2]{};
    for(int mode=0;mode<2;mode++) {
        p.transport[0]=mode?3:1;
        for(int frame=0;frame<64;frame++) {
            p.history[0]=frame?1.0f:0;
            context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
            Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),m.srv.Get(),l.rtv.Get(),room,2,&p),"PT indirect integration");
        }
        context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"PT bounce readback");
        color=(float*)((char*)mapped.pData+32*mapped.RowPitch)+32*4;pathGreen[mode]=color[1];context->Unmap(readback.Get(),0);
    }
    printf("primary PT one bounce=%.6f three bounces=%.6f (8 spp, denoised)\n",pathGreen[0],pathGreen[1]);
    valid=valid && pathGreen[0]<.001 && pathGreen[1]>.025 && pathGreen[1]<.5;

    // A real HDR engine-style cubemap supplies radiance on path misses. Exercise
    // the GPU-only distribution builder, MIS, material pack, and new stage split.
    D3D11_TEXTURE2D_DESC cubeDesc{};cubeDesc.Width=cubeDesc.Height=16;cubeDesc.MipLevels=1;cubeDesc.ArraySize=6;
    cubeDesc.Format=DXGI_FORMAT_R32G32B32A32_FLOAT;cubeDesc.SampleDesc.Count=1;cubeDesc.BindFlags=D3D11_BIND_SHADER_RESOURCE;cubeDesc.MiscFlags=D3D11_RESOURCE_MISC_TEXTURECUBE;
    std::vector<float> cubePixels(16*16*4);for(int i=0;i<16*16;i++){cubePixels[i*4]=.25f;cubePixels[i*4+1]=.5f;cubePixels[i*4+2]=.75f;cubePixels[i*4+3]=1;}
    D3D11_SUBRESOURCE_DATA faces[6]{};for(auto& f:faces){f.pSysMem=cubePixels.data();f.SysMemPitch=16*16;}
    ComPtr<ID3D11Texture2D> cube;ComPtr<ID3D11ShaderResourceView> cubeSrv;
    Check(device->CreateTexture2D(&cubeDesc,faces,&cube),"Synthetic HDR environment");Check(device->CreateShaderResourceView(cube.Get(),nullptr,&cubeSrv),"Environment cube SRV");
    Check(RtEnvironment(ray,cubeSrv.Get()),"Set HDR environment");Check(RtLights(ray,nullptr,0),"No local lamps");
    uint32_t onlyReceiver=18;Check(RtRetainMeshes(ray,&onlyReceiver,1),"Environment receiver only");instance.mesh=18;instance.key=123;
    std::fill(material.begin(),material.end(),0.0f);std::vector<float> baseColors(64*64*4,.6f);
    for(int i=0;i<64*64;i++){material[i*4+1]=.45f;normal[i*4]=1;normal[i*4+1]=.5f;}
    context->UpdateSubresource(m.texture.Get(),0,nullptr,material.data(),64*16,0);context->UpdateSubresource(n.texture.Get(),0,nullptr,normal.data(),64*16,0);
    auto base=Make(device.Get(),DXGI_FORMAT_R32G32B32A32_FLOAT,baseColors.data(),16);
    p.environment[1]=p.environment[2]=p.environment[3]=1;p.sunColor[0]=p.sunColor[1]=p.sunColor[2]=0;
    p.pathSettings[0]=1;p.pathSettings[1]=2;p.pathSettings[2]=1;p.transport[0]=4;p.sizes[2]=p.sizes[3]=64;
    auto statistics=[&](int component,float& mean,float& deviation) {
        context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Environment statistics");
        double sum=0,square=0;int count=0;for(int y=16;y<48;y++)for(int x=16;x<48;x++){float v=((float*)((char*)mapped.pData+y*mapped.RowPitch))[x*4+component];sum+=v;square+=v*v;count++;}
        mean=(float)(sum/count);deviation=(float)sqrt((std::max)(0.0,square/count-double(mean)*mean));context->Unmap(readback.Get(),0);
    };
    // A polished conductor under a uniform HDR environment has a nearly
    // constant reflected signal. No history/filter may hide wasted diffuse
    // lobe samples; raw 2-spp output itself must have low variance.
    for(int i=0;i<64*64;i++){material[i*4]=1;material[i*4+1]=.95f;}
    context->UpdateSubresource(m.texture.Get(),0,nullptr,material.data(),64*16,0);
    p.pathSettings[3]=0;p.history[0]=0;p.settings[3]=0;
    float metalMean=0,metalNoise=0;
    for(int frame=0;frame<16;frame++) {
        context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
        Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),base.srv.Get(),l.rtv.Get(),&instance,1,&p),"Raw polished conductor sampling");
        float mean,deviation;statistics(1,mean,deviation);metalMean+=mean/16;metalNoise+=deviation/16;
    }
    printf("raw 2-spp conductor mean=%.6f noise=%.6f\n",metalMean,metalNoise);
    valid=valid && metalMean>.28f && metalMean<.32f && metalNoise<.01f;
    for(int i=0;i<64*64;i++){material[i*4]=0;material[i*4+1]=.45f;}
    context->UpdateSubresource(m.texture.Get(),0,nullptr,material.data(),64*16,0);
    float envMean[2]{},envNoise[2]{};
    for(int phase=0;phase<2;phase++) {
        p.pathSettings[3]=(float)phase;p.settings[3]=0;
        for(int frame=0;frame<(phase?48:1);frame++) {
            p.history[0]=phase && frame?1.0f:0;instance.transform[3]=phase?frame*.02f:0;
            context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
            Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),base.srv.Get(),l.rtv.Get(),&instance,1,&p),"Moving HDR environment path frame");
        }
        statistics(1,envMean[phase],envNoise[phase]);
    }
    printf("HDR environment fresh/moving green %.6f/%.6f noise %.6f/%.6f\n",envMean[0],envMean[1],envNoise[0],envNoise[1]);
    valid=valid && envMean[0]>.24 && envMean[0]<.39 && envMean[1]>.24 && envMean[1]<.39 && envNoise[1]<envNoise[0]*.4;
    p.settings[3]=7;p.history[0]=1;instance.transform[3]+=.02f;
    context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
    Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),base.srv.Get(),l.rtv.Get(),&instance,1,&p),"Rigid motion history diagnostic");
    float historyMean,historyNoise;statistics(1,historyMean,historyNoise);printf("moving history fraction=%.6f\n",historyMean);valid=valid && historyMean>.5f;
    // Perspective camera rotation must reuse the same world surface, including
    // view-space normal decoding. Identity projections cannot detect this bug.
    using namespace DirectX;
    auto projection=XMMatrixPerspectiveFovLH(XM_PIDIV2,1,50,.1f);
    XMMATRIX lastViewProjection=XMMatrixIdentity();instance.transform[3]=0;
    for(int frame=0;frame<64;frame++) {
        float angle=-.1f+frame*.003f;
        auto view=XMMatrixLookToLH(XMVectorZero(),XMVectorSet(sin(angle),0,cos(angle),0),XMVectorSet(0,1,0,0));
        auto vp=view*projection;auto inv=XMMatrixInverse(nullptr,vp);XMFLOAT4X4 packed;
        XMStoreFloat4x4(&packed,inv);memcpy(p.inverseViewProjection,&packed,64);
        XMStoreFloat4x4(&packed,XMMatrixInverse(nullptr,view));memcpy(p.inverseView,&packed,64);
        XMStoreFloat4x4(&packed,lastViewProjection);memcpy(p.previousViewProjection,&packed,64);
        XMStoreFloat4x4(&packed,vp);memcpy(p.viewProjection,&packed,64);lastViewProjection=vp;
        XMFLOAT3 nv;XMStoreFloat3(&nv,XMVector3TransformNormal(XMVectorSet(0,0,-1,0),view));
        float factor=sqrt(2.0f/(1+nv.z));
        for(int y=0;y<64;y++)for(int x=0;x<64;x++) {
            int i=y*64+x;auto rayPoint=XMVector3TransformCoord(XMVectorSet((x+.5f)/32-1,1-(y+.5f)/32,1,1),inv);
            rayPoint=XMVectorScale(rayPoint,1/XMVectorGetZ(rayPoint));
            depth[i]=XMVectorGetZ(XMVector3TransformCoord(rayPoint,vp));
            normal[i*4]=.5f+.25f*nv.x*factor;normal[i*4+1]=.5f+.25f*nv.y*factor;
        }
        context->UpdateSubresource(d.texture.Get(),0,nullptr,depth.data(),64*4,0);context->UpdateSubresource(n.texture.Get(),0,nullptr,normal.data(),64*16,0);
        context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);p.history[0]=frame?1.0f:0.0f;
        Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),base.srv.Get(),l.rtv.Get(),&instance,1,&p),"Rotating perspective camera history");
    }
    statistics(1,historyMean,historyNoise);printf("perspective rotation history fraction=%.6f\n",historyMean);valid=valid && historyMean>.7f;
    // Range culling may insert/remove an unrelated object before the receiver.
    // A surviving stable identity must keep history across reordered TLAS IDs.
    memcpy(p.previousViewProjection,p.viewProjection,64);p.history[0]=1;
    RtInstance offscreen=instance;offscreen.key=456;offscreen.transform[3]=100;
    for(int frame=0;frame<32;frame++) {
        room[0]=frame%2?instance:offscreen;room[1]=instance;
        context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
        Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),base.srv.Get(),l.rtv.Get(),room,frame%2?1:2,&p),"Range-culling identity history");
    }
    statistics(1,historyMean,historyNoise);printf("reordered survivor history fraction=%.6f\n",historyMean);valid=valid && historyMean>.9f;
    // A new identity at exactly the old position must not inherit old lighting.
    instance.key=789;
    context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
    Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),base.srv.Get(),l.rtv.Get(),&instance,1,&p),"New identity rejects history");
    statistics(1,historyMean,historyNoise);printf("new identity history fraction=%.6f\n",historyMean);valid=valid && std::abs(historyMean-1.0f/32)<.001f;
    // The post-transparent resolve must preserve opaque native/particle content:
    // it consumes only glass, and it cannot repeat the opaque path replacement.
    p.settings[3]=0;Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),base.srv.Get(),l.rtv.Get(),&instance,1,&p),"Opaque lighting stage");
    std::fill(white.begin(),white.end(),.7f);context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
    Check(RtFinishTransparency(ray,d.srv.Get(),l.srv.Get(),l.rtv.Get()),"Post-transparent glass-only resolve");statistics(1,historyMean,historyNoise);
    printf("transparent-stage opaque preservation=%.6f\n",historyMean);valid=valid && std::abs(historyMean-.7f)<.001;
    // An unfinished DX12 frame must be ordered on the GPU, not block the CPU
    // when it appends the transparent resolve. Hold the shared input queue with
    // a test fence; a watchdog releases it even if this regression returns.
    ComPtr<IDXGIDevice> testDxgi;Check(device.As(&testDxgi),"Fence test adapter query");
    ComPtr<IDXGIAdapter> testAdapter;Check(testDxgi->GetAdapter(&testAdapter),"Fence test adapter");
    ComPtr<ID3D12Device> gateDevice;Check(D3D12CreateDevice(testAdapter.Get(),D3D_FEATURE_LEVEL_11_0,IID_PPV_ARGS(&gateDevice)),"Fence test D3D12 device");
    ComPtr<ID3D12Fence> gate;Check(gateDevice->CreateFence(0,D3D12_FENCE_FLAG_SHARED,IID_PPV_ARGS(&gate)),"Fence test gate");
    HANDLE gateHandle=nullptr;Check(gateDevice->CreateSharedHandle(gate.Get(),nullptr,GENERIC_ALL,nullptr,&gateHandle),"Fence test shared handle");
    ComPtr<ID3D11Device5> device5;Check(device.As(&device5),"Fence test D3D11 device");
    ComPtr<ID3D11Fence> gate11;Check(device5->OpenSharedFence(gateHandle,IID_PPV_ARGS(&gate11)),"Fence test import");CloseHandle(gateHandle);
    ComPtr<ID3D11DeviceContext4> context4;Check(context.As(&context4),"Fence test context");
    Check(context4->Wait(gate11.Get(),1),"Fence test queue hold");
    std::thread releaseGate([&] {Sleep(600);gate->Signal(1);});
    Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),base.srv.Get(),l.rtv.Get(),&instance,1,&p),"Queued opaque frame");
    bool pending=gate->GetCompletedValue()==0;auto queuedAt=std::chrono::steady_clock::now();
    int finishResult=RtFinishTransparency(ray,d.srv.Get(),l.srv.Get(),l.rtv.Get());
    double finishMs=std::chrono::duration<double,std::milli>(std::chrono::steady_clock::now()-queuedAt).count();
    gate->Signal(1);releaseGate.join();Check(finishResult,"Nonblocking transparent submission");
    printf("pending GPU gate=%d transparent CPU submit=%.3fms\n",pending?1:0,finishMs);valid=valid && pending && finishMs<300;
    statistics(1,historyMean,historyNoise);valid=valid && std::isfinite(historyMean);
    // Stochastic filtering must not erase deterministic high-frequency normal
    // detail in a directly sunlit surface. No environment energy in this scene.
    p.environment[1]=0;p.transport[0]=1;p.settings[3]=0;p.history[0]=0;
    memset(p.inverseViewProjection,0,64);memset(p.inverseView,0,64);
    for(int i=0;i<4;i++){p.inverseViewProjection[i*4+i]=1;p.inverseView[i*4+i]=1;}
    p.sunDirection[0]=.5f;p.sunDirection[2]=-.8660254f;p.sunColor[0]=p.sunColor[1]=p.sunColor[2]=1;
    std::fill(depth.begin(),depth.end(),1.0f);
    for(int y=0;y<64;y++)for(int x=0;x<64;x++) {
        int i=y*64+x;float nx=((x/2+y/2)%2)?-.3f:.3f,nz=-sqrt(1-nx*nx);
        normal[i*4]=.5f+.25f*nx*sqrt(2/(1+nz));normal[i*4+1]=.5f;
    }
    context->UpdateSubresource(d.texture.Get(),0,nullptr,depth.data(),64*4,0);context->UpdateSubresource(n.texture.Get(),0,nullptr,normal.data(),64*16,0);
    std::vector<float> directPixels;double directError=0;
    for(int filtered=0;filtered<2;filtered++) {
        p.pathSettings[3]=(float)filtered;context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
        Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),base.srv.Get(),l.rtv.Get(),&instance,1,&p),"Sun normal detail preservation");
        context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Direct lighting detail readback");
        int i=0;for(int y=16;y<48;y++)for(int x=16;x<48;x++,i++) {
            float v=((float*)((char*)mapped.pData+y*mapped.RowPitch))[x*4+1];
            if(!filtered)directPixels.push_back(v);else directError=(std::max)(directError,double(std::abs(v-directPixels[i])));
        }
        context->Unmap(readback.Get(),0);
    }
    printf("direct sun normal detail max filtering error=%.8f\n",directError);valid=valid && directError<.0001;
    // Receiver depth/material/identity stay fixed while illumination disappears.
    // Reprojection alone accepts the stale lighting: current samples must bound
    // history immediately instead of leaving a 32-frame afterimage.
    p.sunColor[0]=p.sunColor[1]=p.sunColor[2]=0;p.environment[1]=1;p.transport[0]=4;p.settings[3]=0;p.history[0]=0;
    memcpy(p.previousViewProjection,p.inverseView,64);memcpy(p.viewProjection,p.inverseView,64);
    for(int frame=0;frame<48;frame++) {
        p.history[0]=frame?1.0f:0.0f;context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
        Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),base.srv.Get(),l.rtv.Get(),&instance,1,&p),"Stable receiver illumination");
    }
    float litMean,litNoise,darkMean,darkNoise;statistics(1,litMean,litNoise);
    p.settings[3]=7;Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),base.srv.Get(),l.rtv.Get(),&instance,1,&p),"Lighting change history precondition");statistics(1,historyMean,historyNoise);
    printf("lighting change history fraction=%.6f\n",historyMean);valid=valid && historyMean>.8;p.settings[3]=0;
    p.environment[1]=0;context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
    Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),base.srv.Get(),l.rtv.Get(),&instance,1,&p),"First extinguished illumination frame");statistics(1,darkMean,darkNoise);
    printf("lighting discontinuity green=%.6f -> %.6f in one frame\n",litMean,darkMean);valid=valid && litMean>.2 && darkMean<.015 && darkMean<litMean*.05;
    // A polished receiver remains in place while the camera translates. Its
    // reflection has different motion from the receiver itself. Compare first
    // moving-frame dark pixels against a fresh estimate at the identical pose.
    uint32_t motionKeep[]={18,23};Check(RtRetainMeshes(ray,motionKeep,2),"Moving mirror scene");
    auto strip=background;for(int v=0;v<4;v++){strip[v*8]=v==0||v==3?-.5f:.5f;strip[v*8+2]=-2;}
    Check(RtMesh(ray,23,strip.data(),4,indices,6,emissiveIds),"Reflected emissive strip");
    room[0]=instance;room[0].mesh=18;room[0].key=231;room[0].transform[3]=0;
    room[1]=room[0];room[1].mesh=23;room[1].key=232;
    p={};p.sizes[0]=p.sizes[1]=p.sizes[2]=p.sizes[3]=64;
    for(int i=0;i<4;i++){p.inverseViewProjection[i*4+i]=1;p.inverseView[i*4+i]=1;}
    p.previousViewProjection[0]=p.previousViewProjection[5]=1;p.previousViewProjection[11]=1;
    memcpy(p.viewProjection,p.previousViewProjection,64);
    p.pathSettings[0]=1;p.pathSettings[1]=8;p.pathSettings[3]=1;p.environment[2]=p.environment[3]=1;p.transport[0]=2;
    std::fill(material.begin(),material.end(),0.0f);std::fill(baseColors.begin(),baseColors.end(),1.0f);std::fill(depth.begin(),depth.end(),1.0f);std::fill(white.begin(),white.end(),0.0f);
    for(int i=0;i<64*64;i++){material[i*4]=1;material[i*4+1]=.98f;normal[i*4]=1;normal[i*4+1]=.5f;}
    context->UpdateSubresource(m.texture.Get(),0,nullptr,material.data(),64*16,0);context->UpdateSubresource(n.texture.Get(),0,nullptr,normal.data(),64*16,0);
    context->UpdateSubresource(base.texture.Get(),0,nullptr,baseColors.data(),64*16,0);context->UpdateSubresource(d.texture.Get(),0,nullptr,depth.data(),64*4,0);
    for(int pathMode=0;pathMode<2;pathMode++) {
        p.pathSettings[0]=(float)pathMode;p.settings[3]=pathMode?0.0f:3.0f;p.effects[0]=1;p.originDelta[0]=0;
        for(int frame=0;frame<48;frame++){
            p.history[0]=frame?1.0f:0;context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
            Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),base.srv.Get(),l.rtv.Get(),room,2,&p),"Converge mirror strip");
        }
        p.originDelta[0]=.08f;std::vector<float> movingMirror;double mirrorGhost=0,mirrorSignal=0;int darkPixels=0;
        for(int fresh=0;fresh<2;fresh++){
            p.history[0]=fresh?0.0f:1.0f;context->UpdateSubresource(l.texture.Get(),0,nullptr,white.data(),64*16,0);
            Check(RtFrame(ray,d.srv.Get(),n.srv.Get(),l.srv.Get(),m.srv.Get(),base.srv.Get(),l.rtv.Get(),room,2,&p),"Translated mirror comparison");
            context->CopyResource(readback.Get(),l.texture.Get());Check(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"Mirror afterimage readback");
            int i=0;for(int y=16;y<48;y++)for(int x=16;x<48;x++,i++){
                float value=((float*)((char*)mapped.pData+y*mapped.RowPitch))[x*4+1];
                if(!fresh)movingMirror.push_back(value);else {mirrorSignal=(std::max)(mirrorSignal,double(value));if(value<.005){darkPixels++;mirrorGhost=(std::max)(mirrorGhost,double(movingMirror[i]-value));}}
            }
            context->Unmap(readback.Get(),0);
        }
        printf("translated polished reflection mode=%d old-light excess=%.6f signal=%.6f darkPixels=%d\n",pathMode,mirrorGhost,mirrorSignal,darkPixels);
        valid=valid && mirrorSignal>.4 && darkPixels>100 && mirrorGhost<.015;
    }
    Check(RtEnvironment(ray,nullptr),"Unset environment");
    RtDestroy(ray); puts(valid?"PASS GPU occlusion, textured offscreen reflection, indirect bounce, live emissive LCD, primary path tracing, native resolution, interop, state restoration, validation":"FAIL ray output"); return valid?0:1;
}