1#pragma once
2
3namespace NvidiaDepthCopyTest
4{
5 static std::vector<IDirect3DResource9*> registered;
6 static int registrations=0,copies=0,failRegistration=0,copyStatus=0;
7 static int __cdecl Register(IDirect3DResource9* resource)
8 {
9 if (++registrations==failRegistration) return -1;
10 Check(std::find(registered.begin(),registered.end(),resource)==registered.end(),"NVAPI resource registered only once");
11 registered.push_back(resource);
12 return 0;
13 }
14 static int __cdecl Unregister(IDirect3DResource9* resource)
15 {
16 const auto found=std::find(registered.begin(),registered.end(),resource);
17 Check(found!=registered.end(),"NVAPI unregister requires a successful registration");
18 registered.erase(found);
19 return 0;
20 }
21 static int __cdecl Copy(IDirect3DDevice9* device,IDirect3DResource9* source,
const RECT* sourceRect,
22 IDirect3DResource9* destination,const RECT* destinationRect,D3DTEXTUREFILTERTYPE filter)
23 {
24 Check(device && !sourceRect && !destinationRect && filter==D3DTEXF_NONE,"depth copied whole without filtering");
25 Check(registered.size()==2 && registered[0]==source && registered[1]==destination,"both depth resources registered before copying");
26 ++copies;
27 return copyStatus;
28 }
29 static void Run(IDirect3DDevice9* device)
30 {
31 ComPtr<IDirect3DSurface9> source;
32 ComPtr<IDirect3DTexture9> destination,replacement;
33 HR(device->CreateDepthStencilSurface(16,16,D3DFMT_D24S8,D3DMULTISAMPLE_NONE,0,TRUE,&source,nullptr),"NVAPI lifetime test source");
34 const auto format=D3DFORMAT(
MAKEFOURCC(
'I',
'N',
'T',
'Z'));
35 HR(device->CreateTexture(16,16,1,D3DUSAGE_DEPTHSTENCIL,format,D3DPOOL_DEFAULT,&destination,nullptr),"NVAPI lifetime test destination");
36 HR(device->CreateTexture(32,32,1,D3DUSAGE_DEPTHSTENCIL,format,D3DPOOL_DEFAULT,&replacement,nullptr),"NVAPI lifetime test replacement");
37 {
38 NvidiaDepthCopy copy({Register,Unregister,Copy});
39 HR(copy.Copy(device,source.Get(),destination.Get()),"register and copy NVIDIA depth");
40 HR(copy.Copy(device,source.Get(),destination.Get()),"reuse registered NVIDIA depth");
41 Check(registrations==2 && copies==2,"steady frames do not register depth repeatedly");
42 HR(copy.Copy(device,source.Get(),replacement.Get()),"replace depth resources after resize");
43 Check(registrations==4 && registered.size()==2,"resize releases old NVIDIA registrations");
44 copy.ResetResources();
45 Check(registered.empty(),"device loss unregisters all NVIDIA resources");
46 failRegistration=registrations+2;
47 Check(FAILED(copy.Copy(device,source.Get(),destination.Get())) && registered.empty(),"failed destination registration rolls back source");
48 Check(copies==3 && copy.LastStatus()==-1,"registration failure does not attempt a depth copy");
49 failRegistration=0; copyStatus=-1;
50 Check(FAILED(copy.Copy(device,source.Get(),destination.Get())),"driver copy failure is propagated");
51 copyStatus=0;
52 HR(copy.Copy(device,source.Get(),destination.Get()),"retry after driver failure");
53 }
54 Check(registered.empty(),"destruction unregisters resources before releasing them");
55 std::cout<<"PASS: NVIDIA depth resource lifecycle and failure handling (injected API, not GPU validation)\n";
56 }
57}
#define MAKEFOURCC(ch0, ch1, ch2, ch3)