Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/FixedFunction11Tests.cpp

Source at documentation-generation time. See Test catalog for fixture associations.

1#include "../Rendering/Device11.h"
2#include "../Rendering/FixedFunction11.h"
3#include "../Viewscreen/LandingLights.h"
4#include <d3dx9.h>
5#include <d3d11sdklayers.h>
6#include <cmath>
7#include <cstring>
8#include <fstream>
9#include <iostream>
10#include <stdexcept>
11using namespace Rendering;
12using namespace DirectX;
13using Microsoft::WRL::ComPtr;
14namespace
15{
16 constexpr UINT Width=192,Height=144;
17 void HR(HRESULT hr,const char* what){if(FAILED(hr))throw std::runtime_error(std::string(what)+": "+std::to_string(hr));}
18 void Check(bool value,const char* what){if(!value)throw std::runtime_error(what);}
19 DWORD Bits(float value){DWORD result;memcpy(&result,&value,4);return result;}
20 DWORD Color(const XMFLOAT4& c){return D3DCOLOR_ARGB(UINT(std::lround(c.w*255)),UINT(std::lround(c.x*255)),UINT(std::lround(c.y*255)),UINT(std::lround(c.z*255)));}
21 D3DCOLORVALUE ColorValue(const XMFLOAT4& c){return {c.x,c.y,c.z,c.w};}
22 D3DVECTOR Vector(const XMFLOAT3& c){return {c.x,c.y,c.z};}
23 struct Fixture
24 {
25 HWND window=nullptr;
26 Device11 gpu;
27 FixedFunction11 fixed;
28 ComPtr<IDirect3D9> api;
29 ComPtr<IDirect3DDevice9> old;
30 ComPtr<IDirect3DSurface9> oldTarget,oldStaging;
31 ComPtr<IDirect3DTexture9> oldTexture;
32 ComPtr<IDirect3DVolumeTexture9> oldVolume;
33 ComPtr<ID3D11Texture2D> target,staging;
34 ComPtr<ID3D11RenderTargetView> rtv;
35 ComPtr<ID3D11ShaderResourceView> texture;
36 ComPtr<ID3D11ShaderResourceView> volume;
37 std::filesystem::path folder;
38 std::ofstream metrics;
39 unsigned count=0,failures=0;
40 Fixture(bool warp,bool debug)
41 {
42 folder=std::string("outputs/direct3d11/fixed-")+(warp ? "warp" : "hardware");std::filesystem::create_directories(folder);
43 metrics.open(folder/"metrics.csv");metrics<<"case,mean_rgb,mean_alpha,fraction_over_3,max_rgb\n";
44 window=CreateWindowW(L"STATIC",L"Fixed-function parity",WS_POPUP,0,0,Width,Height,nullptr,nullptr,GetModuleHandleW(nullptr),nullptr);
45 Check(window!=nullptr,"fixture window");HR(gpu.Create({warp ? D3D_DRIVER_TYPE_WARP : D3D_DRIVER_TYPE_HARDWARE,debug}),"native device");
46 std::cout<<gpu.Status()<<std::endl;HR(fixed.Initialize(gpu.Device(),"outputs/direct3d11/shaders"),fixed.Status().c_str());
47 api.Attach(Direct3DCreate9(D3D_SDK_VERSION));Check(bool(api),"reference D3D9");
48 D3DPRESENT_PARAMETERS present{};present.Windowed=TRUE;present.SwapEffect=D3DSWAPEFFECT_DISCARD;present.hDeviceWindow=window;
49 present.BackBufferWidth=Width;present.BackBufferHeight=Height;
50 HR(api->CreateDevice(0,D3DDEVTYPE_HAL,window,D3DCREATE_HARDWARE_VERTEXPROCESSING|D3DCREATE_FPU_PRESERVE,&present,&old),"reference device");
51 HR(old->CreateRenderTarget(Width,Height,D3DFMT_A8R8G8B8,D3DMULTISAMPLE_NONE,0,FALSE,&oldTarget,nullptr),"reference target");
52 HR(old->CreateOffscreenPlainSurface(Width,Height,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&oldStaging,nullptr),"reference staging");
53 D3D11_TEXTURE2D_DESC desc{};desc.Width=Width;desc.Height=Height;desc.ArraySize=desc.MipLevels=1;
54 desc.Format=DXGI_FORMAT_B8G8R8A8_UNORM;desc.SampleDesc.Count=1;desc.BindFlags=D3D11_BIND_RENDER_TARGET;
55 HR(gpu.Device()->CreateTexture2D(&desc,nullptr,&target),"native target");HR(gpu.Device()->CreateRenderTargetView(target.Get(),nullptr,&rtv),"native RTV");
56 desc.BindFlags=0;desc.CPUAccessFlags=D3D11_CPU_ACCESS_READ;desc.Usage=D3D11_USAGE_STAGING;
57 HR(gpu.Device()->CreateTexture2D(&desc,nullptr,&staging),"native staging");
58 std::array<DWORD,64> pixels{};
59 for(UINT y=0;y<8;++y)for(UINT x=0;x<8;++x)pixels[y*8+x]=D3DCOLOR_ARGB(128+x*12,48+x*24,64+y*20,128+(x+y)*4);
60 HR(old->CreateTexture(8,8,1,0,D3DFMT_A8R8G8B8,D3DPOOL_MANAGED,&oldTexture,nullptr),"reference texture");
61 D3DLOCKED_RECT lock{};HR(oldTexture->LockRect(0,&lock,nullptr,0),"texture map");
62 for(UINT y=0;y<8;++y)memcpy(static_cast<char*>(lock.pBits)+y*lock.Pitch,pixels.data()+y*8,32);oldTexture->UnlockRect(0);
63 desc.Width=desc.Height=8;desc.BindFlags=D3D11_BIND_SHADER_RESOURCE;desc.CPUAccessFlags=0;desc.Usage=D3D11_USAGE_IMMUTABLE;
64 D3D11_SUBRESOURCE_DATA data{pixels.data(),32,0};ComPtr<ID3D11Texture2D> native;
65 HR(gpu.Device()->CreateTexture2D(&desc,&data,&native),"native texture");HR(gpu.Device()->CreateShaderResourceView(native.Get(),nullptr,&texture),"native texture view");
66 std::array<DWORD,64> voxels{};
67 for(UINT z=0;z<4;++z)for(UINT y=0;y<4;++y)for(UINT x=0;x<4;++x)voxels[(z*4+y)*4+x]=D3DCOLOR_ARGB(160,32+x*48,48+y*40,64+z*40);
68 HR(old->CreateVolumeTexture(4,4,4,1,0,D3DFMT_A8R8G8B8,D3DPOOL_MANAGED,&oldVolume,nullptr),"reference volume");
69 D3DLOCKED_BOX box{};HR(oldVolume->LockBox(0,&box,nullptr,0),"reference volume map");
70 for(UINT z=0;z<4;++z)for(UINT y=0;y<4;++y)memcpy(static_cast<char*>(box.pBits)+z*box.SlicePitch+y*box.RowPitch,voxels.data()+z*16+y*4,16);
71 oldVolume->UnlockBox(0);
72 for(auto& voxel:voxels)voxel=(voxel&0xff00ff00)|((voxel&255)<<16)|((voxel>>16)&255);
73 D3D11_TEXTURE3D_DESC vd{};vd.Width=vd.Height=vd.Depth=4;vd.MipLevels=1;vd.Format=DXGI_FORMAT_R8G8B8A8_UNORM;vd.Usage=D3D11_USAGE_IMMUTABLE;vd.BindFlags=D3D11_BIND_SHADER_RESOURCE;
74 D3D11_SUBRESOURCE_DATA volumeData{voxels.data(),16,64};ComPtr<ID3D11Texture3D> nativeVolume;
75 HR(gpu.Device()->CreateTexture3D(&vd,&volumeData,&nativeVolume),"native volume");HR(gpu.Device()->CreateShaderResourceView(nativeVolume.Get(),nullptr,&volume),"native volume SRV");
76 }
77 ~Fixture(){gpu.Context()->ClearState();fixed.Reset();old.Reset();api.Reset();if(window)DestroyWindow(window);}
78 void BindReference(const FixedState11& s)
79 {
80 old->SetVertexShader(nullptr);old->SetPixelShader(nullptr);
81 old->SetTransform(D3DTS_WORLD,reinterpret_cast<const D3DMATRIX*>(&s.world));
82 old->SetTransform(D3DTS_VIEW,reinterpret_cast<const D3DMATRIX*>(&s.view));
83 old->SetTransform(D3DTS_PROJECTION,reinterpret_cast<const D3DMATRIX*>(&s.projection));
84 old->SetRenderState(D3DRS_LIGHTING,s.lighting);old->SetRenderState(D3DRS_COLORVERTEX,TRUE);
85 old->SetRenderState(D3DRS_NORMALIZENORMALS,s.normalizeNormals);old->SetRenderState(D3DRS_LOCALVIEWER,s.localViewer);
86 old->SetRenderState(D3DRS_SPECULARENABLE,s.specularEnabled);
87 old->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE,DWORD(s.diffuseSource));old->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE,DWORD(s.ambientSource));
88 old->SetRenderState(D3DRS_SPECULARMATERIALSOURCE,DWORD(s.specularSource));old->SetRenderState(D3DRS_EMISSIVEMATERIALSOURCE,DWORD(s.emissiveSource));
90 old->SetMaterial(&material);old->SetRenderState(D3DRS_AMBIENT,Color(s.ambient));
91 for(unsigned i=0;i<8;++i)
92 {
93 const auto& l=s.lights[i];D3DLIGHT9 light{};light.Type=l.kind==1 ? D3DLIGHT_DIRECTIONAL : l.kind==3 ? D3DLIGHT_SPOT : D3DLIGHT_POINT;
94 light.Position=Vector(l.position);light.Direction=Vector(l.direction);light.Diffuse=ColorValue(l.diffuse);light.Specular=ColorValue(l.specular);light.Ambient=ColorValue(l.ambient);
95 light.Range=l.range;light.Attenuation0=l.attenuation0;light.Attenuation1=l.attenuation1;light.Attenuation2=l.attenuation2;
96 light.Theta=l.theta;light.Phi=l.phi;light.Falloff=l.falloff;HR(old->SetLight(i,&light),"reference light");old->LightEnable(i,l.kind!=0);
97 }
98 const DWORD fogModes[]={D3DFOG_NONE,D3DFOG_LINEAR,D3DFOG_EXP,D3DFOG_EXP2};
99 old->SetRenderState(D3DRS_FOGENABLE,s.fogEnabled);old->SetRenderState(D3DRS_FOGVERTEXMODE,fogModes[unsigned(s.fog)]);old->SetRenderState(D3DRS_FOGTABLEMODE,D3DFOG_NONE);
100 old->SetRenderState(D3DRS_RANGEFOGENABLE,s.rangeFog);old->SetRenderState(D3DRS_FOGCOLOR,Color(s.fogColor));
101 old->SetRenderState(D3DRS_FOGSTART,Bits(s.fogStart));old->SetRenderState(D3DRS_FOGEND,Bits(s.fogEnd));old->SetRenderState(D3DRS_FOGDENSITY,Bits(s.fogDensity));
102 old->SetRenderState(D3DRS_ALPHATESTENABLE,s.alphaTest);old->SetRenderState(D3DRS_ALPHAFUNC,s.alphaFunction);old->SetRenderState(D3DRS_ALPHAREF,s.alphaReference);
103 old->SetRenderState(D3DRS_POINTSIZE,Bits(s.pointSize));old->SetRenderState(D3DRS_POINTSIZE_MIN,Bits(s.pointMin));old->SetRenderState(D3DRS_POINTSIZE_MAX,Bits(s.pointMax));
104 old->SetRenderState(D3DRS_POINTSPRITEENABLE,s.pointSprite);old->SetRenderState(D3DRS_POINTSCALEENABLE,FALSE);
105 old->SetRenderState(D3DRS_CLIPPLANEENABLE,s.clipEnabled ? 1 : 0);old->SetClipPlane(0,&s.clipPlane.x);
106 old->SetRenderState(D3DRS_TEXTUREFACTOR,Color(s.textureFactor));
107 const DWORD ops[]={D3DTOP_DISABLE,D3DTOP_SELECTARG1,D3DTOP_SELECTARG2,D3DTOP_MODULATE,D3DTOP_MODULATE2X,D3DTOP_ADDSMOOTH,D3DTOP_BLENDFACTORALPHA};
108 const DWORD args[]={D3DTA_CURRENT,D3DTA_DIFFUSE,D3DTA_SPECULAR,D3DTA_TEXTURE,D3DTA_TFACTOR};
109 for(unsigned i=0;i<8;++i)
110 {
111 const auto& stage=s.stages[i];
112 old->SetTexture(i,stage.texture.Get()==volume.Get() ? static_cast<IDirect3DBaseTexture9*>(oldVolume.Get()) : stage.texture ? oldTexture.Get() : nullptr);
113 old->SetTextureStageState(i,D3DTSS_COLOROP,ops[int(stage.color)]);old->SetTextureStageState(i,D3DTSS_ALPHAOP,ops[int(stage.alpha)]);
114 old->SetTextureStageState(i,D3DTSS_COLORARG1,args[int(stage.color1)]);old->SetTextureStageState(i,D3DTSS_COLORARG2,args[int(stage.color2)]);
115 old->SetTextureStageState(i,D3DTSS_ALPHAARG1,args[int(stage.alpha1)]);old->SetTextureStageState(i,D3DTSS_ALPHAARG2,args[int(stage.alpha2)]);
116 old->SetTextureStageState(i,D3DTSS_TEXCOORDINDEX,stage.coordinate);old->SetTextureStageState(i,D3DTSS_TEXTURETRANSFORMFLAGS,stage.transformCount|(stage.projected ? D3DTTFF_PROJECTED : 0));
117 old->SetTransform(D3DTRANSFORMSTATETYPE(D3DTS_TEXTURE0+i),reinterpret_cast<const D3DMATRIX*>(&stage.transform));
118 old->SetSamplerState(i,D3DSAMP_MINFILTER,D3DTEXF_POINT);old->SetSamplerState(i,D3DSAMP_MAGFILTER,D3DTEXF_POINT);old->SetSamplerState(i,D3DSAMP_MIPFILTER,D3DTEXF_NONE);
119 old->SetSamplerState(i,D3DSAMP_ADDRESSU,D3DTADDRESS_WRAP);old->SetSamplerState(i,D3DSAMP_ADDRESSV,D3DTADDRESS_WRAP);old->SetSamplerState(i,D3DSAMP_SRGBTEXTURE,FALSE);
120 }
121 }
122 void Save(const std::vector<uint32_t>& pixels,const std::string& name)
123 {
124 D3DLOCKED_RECT lock{};HR(oldStaging->LockRect(&lock,nullptr,0),"export map");
125 for(UINT y=0;y<Height;++y)memcpy(static_cast<char*>(lock.pBits)+y*lock.Pitch,pixels.data()+y*Width,Width*4);oldStaging->UnlockRect();
126 HR(D3DXSaveSurfaceToFileW((folder/name).c_str(),D3DXIFF_PNG,oldStaging.Get(),nullptr,nullptr),"export image");
127 }
128 void Compare(const std::string& name,FixedState11 s,bool pointList=false,const LandingLights::Profile* beamProfile=nullptr,bool expectSpill=false)
129 {
130 s.viewport={0,0,float(Width),float(Height)};
131 FixedVertex11 vertices[4];
132 for(unsigned i=0;i<4;++i)
133 {
134 vertices[i].position={(i&1) ? .8f : -.8f,(i&2) ? -.8f : .8f,.3f,1};
135 vertices[i].normal={0,0,-1};vertices[i].pointSize=float(3+i*4);
136 vertices[i].diffuse=0xb0a06030;vertices[i].specular=0xcc0c2233;
137 vertices[i].uv[0]={float(i&1),float((i>>1)&1),.2f+float(i&1)*.6f};
138 vertices[i].uv[1]={.125f+float(i&1)*.5f,.125f+float((i>>1)&1)*.5f,0};
139 if(s.transformed)vertices[i].position={(i&1) ? float(Width-15) : 13.f,(i&2) ? float(Height-17) : 11.f,.3f,1};
140 }
141 std::vector<D3DVERTEXELEMENT9> layout;
142 layout.push_back({0,0,BYTE(s.transformed ? D3DDECLTYPE_FLOAT4 : D3DDECLTYPE_FLOAT3),D3DDECLMETHOD_DEFAULT,BYTE(s.transformed ? D3DDECLUSAGE_POSITIONT : D3DDECLUSAGE_POSITION),0});
143 if(!s.transformed)layout.push_back({0,16,D3DDECLTYPE_FLOAT3,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_NORMAL,0});
144 if(s.vertexPointSize)layout.push_back({0,28,D3DDECLTYPE_FLOAT1,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_PSIZE,0});
145 if((s.vertexColorMask&1) || beamProfile)layout.push_back({0,32,D3DDECLTYPE_D3DCOLOR,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_COLOR,0});
146 if(s.vertexColorMask&2)layout.push_back({0,36,D3DDECLTYPE_D3DCOLOR,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_COLOR,1});
147 layout.push_back({0,40,BYTE(s.uvDimensions[0]==3 ? D3DDECLTYPE_FLOAT3 : D3DDECLTYPE_FLOAT2),D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TEXCOORD,0});
148 layout.push_back({0,52,D3DDECLTYPE_FLOAT2,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TEXCOORD,1});layout.push_back(D3DDECL_END());
149 ComPtr<IDirect3DVertexDeclaration9> declaration;HR(old->CreateVertexDeclaration(layout.data(),&declaration),"reference declaration");
150 HR(old->SetRenderTarget(0,oldTarget.Get()),"reference target");old->SetDepthStencilSurface(nullptr);
151 const D3DVIEWPORT9 viewport9{0,0,Width,Height,0,1};old->SetViewport(&viewport9);
152 old->SetRenderState(D3DRS_ZENABLE,FALSE);old->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);old->SetRenderState(D3DRS_ALPHABLENDENABLE,FALSE);
153 auto referenceState=s;
154 FixedVertex11 referenceVertices[4];std::copy(std::begin(vertices),std::end(vertices),referenceVertices);
155 if(beamProfile)
156 {
157 // D3D9 cannot express elliptical/cutoff lamps. Compare native
158 // vertex lighting against CPU photometry baked into colors.
159 referenceState.lighting=false;referenceState.vertexColorMask=1;
160 LandingLights::Light lamp;lamp.profile=*beamProfile;
161 const auto& source=s.lights[0];
162 lamp.origin={source.position.x,source.position.y,source.position.z};
163 lamp.direction={source.direction.x,source.direction.y,source.direction.z};
164 lamp.up={source.beamUp.x,source.beamUp.y,source.beamUp.z};
165 for(auto& vertex:referenceVertices)
166 {
167 XMFLOAT3 point;XMStoreFloat3(&point,XMVector3TransformCoord(XMLoadFloat4(&vertex.position),XMLoadFloat4x4(&s.world)));
168 const auto delta=RiseMath::Vector3(point.x,point.y,point.z)-lamp.origin;
169 const float lambert=delta.z/RiseMath::Vec3Length(&delta);
170 const float light=(std::clamp)(LandingLights::Intensity(lamp,delta)*lambert,0.f,1.f);
171 vertex.diffuse=Color({light,light,light,1});
172 }
173 }
174 BindReference(referenceState);HR(old->SetVertexDeclaration(declaration.Get()),"reference layout");
175 old->Clear(0,nullptr,D3DCLEAR_TARGET,0xff203040,1,0);old->BeginScene();
176 HR(old->DrawPrimitiveUP(pointList ? D3DPT_POINTLIST : D3DPT_TRIANGLESTRIP,pointList ? 4 : 2,referenceVertices,sizeof(FixedVertex11)),"reference draw");old->EndScene();
177 HR(old->GetRenderTargetData(oldTarget.Get(),oldStaging.Get()),"reference read");
178 D3DLOCKED_RECT lock{};HR(oldStaging->LockRect(&lock,nullptr,D3DLOCK_READONLY),"reference map");std::vector<uint32_t> expected(Width*Height);
179 for(UINT y=0;y<Height;++y)memcpy(expected.data()+y*Width,static_cast<char*>(lock.pBits)+y*lock.Pitch,Width*4);oldStaging->UnlockRect();
180 auto* context=gpu.Context();context->ClearState();auto* renderTarget=rtv.Get();context->OMSetRenderTargets(1,&renderTarget,nullptr);
181 D3D11_RASTERIZER_DESC rs{};rs.FillMode=D3D11_FILL_SOLID;rs.CullMode=D3D11_CULL_NONE;rs.DepthClipEnable=TRUE;
182 ComPtr<ID3D11RasterizerState> raster;HR(gpu.Device()->CreateRasterizerState(&rs,&raster),"native rasterizer");context->RSSetState(raster.Get());
183 const D3D11_VIEWPORT viewport{0,0,float(Width),float(Height),0,1};context->RSSetViewports(1,&viewport);
184 D3D11_BUFFER_DESC desc{};desc.ByteWidth=sizeof(vertices);desc.BindFlags=D3D11_BIND_VERTEX_BUFFER;desc.Usage=D3D11_USAGE_IMMUTABLE;
185 D3D11_SUBRESOURCE_DATA data{vertices,0,0};ComPtr<ID3D11Buffer> buffer;HR(gpu.Device()->CreateBuffer(&desc,&data,&buffer),"native vertices");
186 auto* nativeVertices=buffer.Get();const UINT stride=sizeof(FixedVertex11),offset=0;context->IASetVertexBuffers(0,1,&nativeVertices,&stride,&offset);
187 context->IASetPrimitiveTopology(pointList ? D3D11_PRIMITIVE_TOPOLOGY_POINTLIST : D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
188 const float clear[]={32.f/255,48.f/255,64.f/255,1};context->ClearRenderTargetView(renderTarget,clear);
189 const HRESULT bound=fixed.Bind(context,s,pointList);if(FAILED(bound))std::cerr<<fixed.Status()<<std::endl;HR(bound,"native fixed-function bind");context->Draw(4,0);
190 context->OMSetRenderTargets(0,nullptr,nullptr);context->CopyResource(staging.Get(),target.Get());
191 D3D11_MAPPED_SUBRESOURCE mapped{};HR(context->Map(staging.Get(),0,D3D11_MAP_READ,0,&mapped),"native read");
192 std::vector<uint32_t> actual(Width*Height),difference(Width*Height);double sum=0,alpha=0;unsigned over=0,maximum=0;
193 for(UINT y=0;y<Height;++y)for(UINT x=0;x<Width;++x)
194 {
195 const auto a=expected[y*Width+x],b=reinterpret_cast<uint32_t*>(static_cast<char*>(mapped.pData)+y*mapped.RowPitch)[x];actual[y*Width+x]=b;
196 unsigned worst=0;for(int shift:{0,8,16}){const auto error=unsigned(std::abs(int((a>>shift)&255)-int((b>>shift)&255)));sum+=error;worst=(std::max)(worst,error);}
197 maximum=(std::max)(maximum,worst);alpha+=std::abs(int(a>>24)-int(b>>24));if(worst>3)++over;difference[y*Width+x]=0xff000000|((std::min)(255u,worst*8)*0x10101);
198 }
199 context->Unmap(staging.Get(),0);const double mean=sum/(Width*Height*3.),alphaMean=alpha/(Width*Height),fraction=double(over)/(Width*Height);
200 if(expectSpill)
201 {
202 const auto level=actual[(Height/2)*Width+Width/2]&255;
203 Check(level>8 && level<64,"foreground GPU probe receives dim spill, not darkness or full beam");
204 }
205 const bool passed=mean<=1 && alphaMean<=1 && fraction<=.01;
206 std::cout<<(passed ? "PASS: " : "FAIL: ")<<name<<" mean="<<mean<<" alpha="<<alphaMean<<" over3="<<fraction<<" max="<<maximum<<std::endl;
207 metrics<<name<<','<<mean<<','<<alphaMean<<','<<fraction<<','<<maximum<<'\n';metrics.flush();
208 Save(expected,name+"-d3d9.png");Save(actual,name+"-d3d11.png");Save(difference,name+"-diff8x.png");++count;if(!passed)++failures;
209 }
210 void DebugCheck()
211 {
212 ComPtr<ID3D11InfoQueue> queue;if(FAILED(gpu.Device()->QueryInterface(IID_PPV_ARGS(&queue))))return;
213 for(UINT64 i=0;i<queue->GetNumStoredMessages();++i)
214 {
215 SIZE_T size=0;queue->GetMessage(i,nullptr,&size);std::vector<char> bytes(size);auto* message=reinterpret_cast<D3D11_MESSAGE*>(bytes.data());
216 HR(queue->GetMessage(i,message,&size),"debug message");if(message->Severity<=D3D11_MESSAGE_SEVERITY_WARNING)throw std::runtime_error(message->pDescription);
217 }
218 }
219 };
220}
221int main(int argc,char** argv)
222{
223 try
224 {
225 bool warp=false,debug=true;for(int i=1;i<argc;++i){if(!strcmp(argv[i],"--warp"))warp=true;else if(!strcmp(argv[i],"--no-debug-layer"))debug=false;else throw std::runtime_error("unknown argument");}
226 Fixture test(warp,debug);FixedState11 base;base.lighting=false;base.vertexColorMask=3;
227 test.Compare("untextured",base);
228 auto missing=base;missing.vertexColorMask=0;test.Compare("missing-vertex-colors",missing);
229 auto s=base;s.transformed=true;test.Compare("screen",s);
230 for(int op=1;op<=6;++op)
231 {
232 s=base;s.stages[0].texture=test.texture;s.stages[0].color=TextureOp11(op);s.textureFactor={.2f,.4f,.6f,.4f};
233 test.Compare("texture-op-"+std::to_string(op),s);
234 }
235 s=base;s.stages[0].texture=test.texture;s.stages[0].transformCount=2;s.stages[0].transform._31=.125f;s.stages[0].transform._32=-.25f;
236 test.Compare("uv-translation-row3",s);
237 s.stages[1].texture=test.texture;s.stages[1].color=TextureOp11::AddSmooth;s.stages[1].color1=TextureArg11::Texture;s.stages[1].color2=TextureArg11::Current;s.stages[1].coordinate=1;
238 test.Compare("two-texture-stages",s);
239 s=base;s.stages[0].texture=test.volume;s.uvDimensions[0]=3;s.stages[0].transformCount=3;s.stages[0].transform._33=.5f;test.Compare("volume-coordinates",s);
240 s=base;s.stages[0].color=TextureOp11::Select1;s.stages[0].color1=TextureArg11::Factor;s.stages[0].alpha1=TextureArg11::Factor;
241 s.textureFactor={.2f,.4f,.6f,.8f};test.Compare("texture-factor-no-texture",s);
243 s.stages[0].alpha1=TextureArg11::Diffuse;test.Compare("diffuse-specular-arguments",s);
244 s=base;s.lighting=true;s.vertexColorMask=0;s.materialDiffuse={.6f,.5f,.4f,.7f};s.materialAmbient={.3f,.4f,.5f,1};s.ambient={.2f,.2f,.2f,1};
245 s.lights[0].kind=1;s.lights[0].direction={0,0,1};s.lights[0].diffuse={.7f,.8f,.9f,1};s.lights[0].ambient={.1f,.2f,.3f,1};
246 test.Compare("directional-material",s);
247 auto scaled=s;XMStoreFloat4x4(&scaled.view,XMMatrixScaling(.7f,.7f,.7f));test.Compare("scaled-view-directional",scaled);
248 scaled.normalizeNormals=true;test.Compare("scaled-view-normalized",scaled);
249 s.lights[0].kind=2;s.lights[0].position={0,0,-1};s.lights[0].range=10;s.lights[0].attenuation1=.2f;test.Compare("point-light",s);
250 scaled=s;XMStoreFloat4x4(&scaled.view,XMMatrixScaling(.7f,.7f,.7f));test.Compare("scaled-view-point",scaled);
251 scaled=s;XMStoreFloat4x4(&scaled.world,XMMatrixScaling(.8f,.9f,1.2f)*XMMatrixRotationY(.2f));test.Compare("scaled-world-point",scaled);
252 scaled.normalizeNormals=true;test.Compare("scaled-world-normalized",scaled);
253 s.lights[0].kind=3;s.lights[0].theta=.4f;s.lights[0].phi=1.8f;s.lights[0].falloff=2;test.Compare("spot-light",s);
254 s.lights[0].kind=1;s.specularEnabled=true;s.power=8;s.materialSpecular={.4f,.6f,.8f,1};s.lights[0].specular={1,1,1,1};test.Compare("specular",s);
255 s.localViewer=false;test.Compare("infinite-viewer",s);
257 for(int fog=1;fog<=3;++fog)
258 {
259 s=base;s.fogEnabled=true;s.fog=Fog11(fog);s.rangeFog=true;s.fogStart=0;s.fogEnd=2;s.fogDensity=.5f;s.fogColor={.2f,.3f,.4f,1};
260 test.Compare("range-fog-"+std::to_string(fog),s);
261 }
262 s=base;s.clipEnabled=true;s.clipPlane={1,0,0,0};test.Compare("world-clip-plane",s);
263 XMStoreFloat4x4(&s.world,XMMatrixRotationY(.25f)*XMMatrixTranslation(.2f,0,0));test.Compare("transformed-world-clip-plane",s);
264 s=base;s.transformed=true;s.fogEnabled=true;s.fogColor={.2f,.3f,.4f,1};test.Compare("screen-vertex-fog",s);
265 s=base;s.alphaTest=true;s.alphaFunction=D3D11_COMPARISON_GREATER;s.alphaReference=180;s.stages[0].texture=test.texture;test.Compare("alpha-test",s);
266 s=base;s.pointSize=9;test.Compare("fixed-points",s,true);
267 s.vertexPointSize=true;test.Compare("vertex-point-sizes",s,true);
268 s.pointSprite=true;s.stages[0].texture=test.texture;test.Compare("point-sprites",s,true);
269 s=base;s.lighting=true;s.normalizeNormals=true;s.vertexColorMask=0;
270 s.materialDiffuse={1,1,1,1};s.materialAmbient={0,0,0,1};s.ambient={0,0,0,1};
271 XMStoreFloat4x4(&s.world,XMMatrixScaling(.001f,.001f,.001f));
272 XMStoreFloat4x4(&s.projection,XMMatrixScaling(1000,1000,1000));
273 auto& lamp=s.lights[0];lamp.kind=3;lamp.position={0,0,-.03f};lamp.direction={0,0,1};lamp.diffuse={1,1,1,1};
274 lamp.profiledBeam=true;lamp.range=LandingLights::RoadLow.rangeKm;
275 lamp.beamAngles=LandingLights::RoadLow.Angles<XMFLOAT4>();lamp.beamParameters=LandingLights::RoadLow.Parameters<XMFLOAT4>();
276 test.Compare("profiled-road-cutoff",s,false,&LandingLights::RoadLow);
277 XMStoreFloat4x4(&s.view,XMMatrixScaling(.7f,.7f,.7f));
278 XMStoreFloat4x4(&s.projection,XMMatrixScaling(1000/.7f,1000/.7f,1000/.7f));
279 test.Compare("profiled-road-scaled-view",s,false,&LandingLights::RoadLow);
280 // Sample below the bright core, 2 m ahead and 0.49 m below the lamp.
281 // The original 10-degree outer cone left this entire patch black.
282 XMStoreFloat4x4(&s.world,XMMatrixScaling(.0001f,.0001f,.0001f)*XMMatrixTranslation(0,-.00049f,0));
283 XMStoreFloat4x4(&s.view,XMMatrixIdentity());
284 XMStoreFloat4x4(&s.projection,XMMatrixInverse(nullptr,XMLoadFloat4x4(&s.world)));
285 lamp.position={0,0,-.002f};
286 test.Compare("profiled-road-foreground-spill",s,false,&LandingLights::RoadLow,true);
287 // Vertices inside the upper feather band exercise the shared shader
288 // against CPU photometry at both near-wall and road distances.
289 for(float distance : {.002f,.02f})
290 {
291 XMStoreFloat4x4(&s.world,XMMatrixScaling(.0001f,.000005f,.001f)*
292 XMMatrixTranslation(0,LandingLights::RoadLow.cutoffSlope*distance-.000012f,0));
293 XMStoreFloat4x4(&s.projection,XMMatrixInverse(nullptr,XMLoadFloat4x4(&s.world)));
294 lamp.position={0,0,.0003f-distance};
295 test.Compare(distance<.01f ? "profiled-road-near-feather" : "profiled-road-far-feather",
296 s,false,&LandingLights::RoadLow);
297 }
298 test.DebugCheck();std::cout<<test.count<<" fixed-function fixtures; "<<test.failures<<" failures\n";return test.failures ? 1 : 0;
299 }
300 catch(const std::exception& error){std::cerr<<"FAIL: "<<error.what()<<std::endl;return 1;}
301}
RiseMath::Color ColorValue(Rendering::PackedColor c)
Definition Controls.cpp:39
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
ID3D11DeviceContext * Context() const
Definition Device11.h:66
ID3D11Device * Device() const
Definition Device11.h:65
const std::string & Status() const
Definition Device11.h:75
HRESULT Create(const DeviceOptions11 &options={})
Definition Device11.cpp:84
HRESULT Initialize(ID3D11Device *device, const std::filesystem::path &shaderDirectory={})
const std::string & Status() const
HRESULT Bind(ID3D11DeviceContext *context, const FixedState11 &state, bool pointList=false)
Definition DDS.h:24
constexpr float Width
constexpr float Height
float Intensity(const Light &light, const RiseMath::Vector3 &delta)
constexpr Profile RoadLow
Definition BeamProfile.h:35
DirectX::XMFLOAT3 Vector
Definition Math11.h:7
float Vec3Length(const Vector3 *a)
Definition ClientMath.h:95
uint32_t Color(Echo echo)
args
Definition erin.py:146
RiseMath::Vector3 origin
RiseMath::Vector3 up
RiseMath::Vector3 direction
PackedVector Parameters(float extinctionPerKm=0) const
Definition BeamProfile.h:24
PackedVector Angles() const
Definition BeamProfile.h:18
MaterialSource11 specularSource
DirectX::XMFLOAT4X4 world
std::array< TextureStage11, 8 > stages
DirectX::XMFLOAT4X4 view
MaterialSource11 emissiveSource
DirectX::XMFLOAT4X4 projection
MaterialSource11 diffuseSource
DirectX::XMFLOAT4 materialSpecular
DirectX::XMFLOAT4 ambient
DirectX::XMFLOAT4 fogColor
MaterialSource11 ambientSource
DirectX::XMFLOAT4 materialAmbient
DirectX::XMFLOAT4 textureFactor
D3D11_COMPARISON_FUNC alphaFunction
DirectX::XMFLOAT4 materialDiffuse
std::array< FixedLight11, 8 > lights
DirectX::XMFLOAT4 materialEmissive
DirectX::XMFLOAT4 viewport
DirectX::XMFLOAT4 clipPlane
DirectX::XMFLOAT3 uv[2]
DirectX::XMFLOAT3 normal
DirectX::XMFLOAT4 position