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

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

1// Reuse the existing D3D9/D3D11 parity harness without changing its scenarios.
2#define main ExistingShaderParityMain
3#include "ShaderParity11Tests.cpp"
4#undef main
5#include <d3dcompiler.h>
6
7namespace
8{
9 void Image(Fixture& f,const char* name,UINT width,UINT height,const std::vector<DWORD>& pixels)
10 {
11 ComPtr<IDirect3DTexture9> old;
12 HR(f.device9->CreateTexture(width,height,1,0,D3DFMT_A8R8G8B8,D3DPOOL_MANAGED,&old,nullptr),"fixture texture9");
13 D3DLOCKED_RECT r{};HR(old->LockRect(0,&r,nullptr,0),"texture9 lock");
14 for(UINT y=0;y<height;++y)memcpy(static_cast<char*>(r.pBits)+y*r.Pitch,pixels.data()+y*width,width*4);
15 old->UnlockRect(0);HR(f.effect->SetTexture(name,old.Get()),"texture9 bind");f.textures9.push_back(old);
16 D3D11_TEXTURE2D_DESC desc{};desc.Width=width;desc.Height=height;desc.MipLevels=desc.ArraySize=1;
17 desc.Format=DXGI_FORMAT_B8G8R8A8_UNORM;desc.SampleDesc.Count=1;desc.Usage=D3D11_USAGE_IMMUTABLE;desc.BindFlags=D3D11_BIND_SHADER_RESOURCE;
18 D3D11_SUBRESOURCE_DATA initial{pixels.data(),width*4,0};ComPtr<ID3D11Texture2D> t;ComPtr<ID3D11ShaderResourceView> view;
19 HR(f.gpu.Device()->CreateTexture2D(&desc,&initial,&t),"fixture texture11");HR(f.gpu.Device()->CreateShaderResourceView(t.Get(),nullptr,&view),"fixture view");
20 f.parameters.Texture(name,view.Get());
21 }
22 void Enabled(Fixture& f,bool enabled,int type=26)
23 {
24 const int flag=enabled;f.parameters.Set("CityOverlayEnabled",flag);f.effect->SetBool("CityOverlayEnabled",flag);
25 f.Vector("Features",float(type),0,0,0);
26 }
27 std::array<float,4> Color(DWORD p){return {float((p>>16)&255)/255,float((p>>8)&255)/255,float(p&255)/255,float(p>>24)/255};}
28 std::array<float,4> Sample(const std::vector<DWORD>& pixels,int width,int height,float u,float v,bool wrap)
29 {
30 const float fx=u*width-.5f,fy=v*height-.5f;const int x=int(std::floor(fx)),y=int(std::floor(fy));
31 auto coordinate=[&](int c,int n){return wrap ? (c%n+n)%n : (std::max)(0,(std::min)(n-1,c));};
32 std::array<float,4> result{};
33 for(int j=0;j<2;++j)for(int i=0;i<2;++i)
34 {
35 const auto c=Color(pixels[coordinate(y+j,height)*width+coordinate(x+i,width)]);
36 const float weight=(i?fx-x:1-(fx-x))*(j?fy-y:1-(fy-y));
37 for(int k=0;k<4;++k)result[k]+=weight*c[k];
38 }
39 return result;
40 }
41 void Expected(DWORD pixel,std::array<float,4> base,std::array<float,4> overlay,float light,bool enabled)
42 {
43 const auto actual=Color(pixel);float emission[3];
44 for(int i=0;i<3;++i)emission[i]=overlay[i]*overlay[3];
45 const float coverage=(std::max)({emission[0],emission[1],emission[2]});
46 for(int i=0;i<3;++i)
47 {
48 const float expected=enabled ? base[i]*light*(1-coverage)+emission[i]*(std::min)(1.f,base[i]*2)*2 : base[i]*light;
49 Check(std::abs(actual[i]-(std::min)(1.f,expected))<2.1f/255,"emissive lattice RGB equation");
50 }
51 if (std::abs(actual[3]-(enabled?base[3]:1))>=1.1f/255)
52 std::cerr << "alpha actual=" << actual[3] << " expected=" << (enabled?base[3]:1) << " enabled=" << enabled << '\n';
53 Check(std::abs(actual[3]-(enabled?base[3]:1))<1.1f/255,"surface alpha preserved for overlay; legacy alpha otherwise");
54 }
55}
56int main()
57{
58 try
59 {
60 Fixture f(true,false,true);
61 unsigned scenarios=0;
62 for(const auto& pass:CustomUIPasses)
63 {
64 if(strcmp(pass.technique,"TShader") || (pass.index!=2 && pass.index!=3))continue;
65 ComPtr<ID3DBlob> vs,ps,errors;
66 HR(D3DCompileFromFile(L"Rendering/Shaders/CustomUI.hlsl",nullptr,D3D_COMPILE_STANDARD_FILE_INCLUDE,
67 pass.vertex,"vs_5_0",D3DCOMPILE_PACK_MATRIX_ROW_MAJOR|D3DCOMPILE_OPTIMIZATION_LEVEL3,0,&vs,&errors),"compile city VS");
68 HR(D3DCompileFromFile(L"Rendering/Shaders/CustomUI.hlsl",nullptr,D3D_COMPILE_STANDARD_FILE_INCLUDE,
69 pass.pixel,"ps_5_0",D3DCOMPILE_PACK_MATRIX_ROW_MAJOR|D3DCOMPILE_OPTIMIZATION_LEVEL3,0,&ps,&errors),"compile city PS");
70 Program11 program;HR(program.Create(f.gpu.Device(),vs->GetBufferPointer(),vs->GetBufferSize(),ps->GetBufferPointer(),ps->GetBufferSize(),elements,ARRAYSIZE(elements)),"city shader program");
71 f.Configure(pass,0);f.Vector("shiplight",0,0,0,0);f.Vector("WeapPos",0,0,0,0);f.Vector("clouduv",0,0,0,0);
72 Image(f,"ColorMap",1,1,{0x99333333});Image(f,"ColorMap2",1,1,{0});
73 for(float light:{0.01f,0.8f})
74 for(DWORD logo:{0x00ffffffu,0xff000000u,0xff400000u,0x80408020u})
75 {
76 f.Vector("ambient",light,light,light,1000000);Image(f,"CityOverlayMap",1,1,{logo});Enabled(f,true);
77 const auto native=f.Native(program),reference=f.Reference(pass);
78 Expected(native[Height/2*Width+Width/2],Color(0x99333333),Color(logo),light,true);
79 Expected(reference[Height/2*Width+Width/2],Color(0x99333333),Color(logo),light,true);
80 ++scenarios;
81 }
82 // Explicit enable/type changes in consecutive draws catch stale material state.
83 for(const auto state:{std::pair<bool,int>{false,26},{true,25},{true,26},{false,26}})
84 {
85 Enabled(f,state.first,state.second);const auto native=f.Native(program);
86 Expected(native[Height/2*Width+Width/2],Color(0x99333333),Color(0x80408020),.8f,state.first && state.second==26);++scenarios;
87 }
88 // Different image dimensions, repeating base UV, clamped full-image UV.
89 const std::vector<DWORD> base={0x99191919,0x99333333};
90 const std::vector<DWORD> logo={0xff800000,0xff008000,0xff000080,0x00ffffff,0xff000000,0x80808080};
91 Image(f,"ColorMap",2,1,base);Image(f,"CityOverlayMap",3,2,logo);Enabled(f,true);
92 Vertex mesh[4];memcpy(mesh,vertices,sizeof mesh);
93 for(auto& v:mesh){v.uv[0]=v.uv[0]*3;v.uv[1]*=2;v.uv2[0]=v.uv2[0]*1.5f-.25f;}
94 D3D11_BUFFER_DESC bd{};bd.ByteWidth=sizeof mesh;bd.Usage=D3D11_USAGE_IMMUTABLE;bd.BindFlags=D3D11_BIND_VERTEX_BUFFER;
95 D3D11_SUBRESOURCE_DATA data{mesh,0,0};f.geometry.Reset();HR(f.gpu.Device()->CreateBuffer(&bd,&data,&f.geometry),"independent UV mesh");
96 memcpy(f.vertexBytes.data(),mesh,sizeof mesh);
97 const auto native=f.Native(program),reference=f.Reference(pass);
98 for(UINT y:{24u,80u,150u})for(UINT x:{4u,40u,95u,170u,245u})
99 {
100 const float u=(x+.5f)/Width,v=(y+.5f)/Height;
101 Expected(native[y*Width+x],Sample(base,2,1,u*3,v*2,true),Sample(logo,3,2,u*1.5f-.25f,v,false),.8f,true);
102 const auto a=Color(native[y*Width+x]),b=Color(reference[y*Width+x]);
103 for(int k=0;k<4;++k)Check(std::abs(a[k]-b[k])<2.1f/255,"SM3/SM5 city image parity");
104 }
105 ++scenarios;
106 // Restore vertex data before the next pass.
107 data.pSysMem=vertices;f.geometry.Reset();HR(f.gpu.Device()->CreateBuffer(&bd,&data,&f.geometry),"restore UV mesh");
108 memcpy(f.vertexBytes.data(),vertices,sizeof vertices);
109 }
110 f.DebugCheck();std::cout<<"PASS: "<<scenarios<<" near/far GPU city logo scenarios (WARP + D3D9 reference)\n";return 0;
111 }
112 catch(const std::exception& e){std::cerr<<"FAIL: "<<e.what()<<'\n';return 1;}
113}
SCRIPT_CONTROL SCRIPT_STATE const void int
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
constexpr float Height
SRenderWeather Sample(const unsigned char *p)
uint32_t Color(Echo echo)