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

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

1#include "NativeUiTest11.h"
2#include <algorithm>
3#include <array>
4#include <cmath>
5#include <iostream>
6using namespace Rendering;
7using namespace DirectX;
8using Microsoft::WRL::ComPtr;
9namespace
10{
11 void Check(bool ok, const char* message) { if (!ok) throw std::runtime_error(message); }
12 ComPtr<ID3D11ShaderResourceView> Texture(RenderDevice& device, DWORD rgba)
13 {
14 D3D11_TEXTURE2D_DESC desc{};
15 desc.Width=desc.Height=desc.MipLevels=desc.ArraySize=desc.SampleDesc.Count=1;
16 desc.Format=DXGI_FORMAT_R8G8B8A8_UNORM;desc.BindFlags=D3D11_BIND_SHADER_RESOURCE;
17 D3D11_SUBRESOURCE_DATA initial{&rgba,4,4};
18 ComPtr<ID3D11Texture2D> texture;ComPtr<ID3D11ShaderResourceView> view;
19 NativeUiTest11::HR(device.Native()->CreateTexture2D(&desc,&initial,&texture));
20 NativeUiTest11::HR(device.Native()->CreateShaderResourceView(texture.Get(),nullptr,&view));
21 return view;
22 }
23 struct Vertex { XMFLOAT3 position,normal;XMFLOAT2 uv,logo; };
24}
25int main()
26{
27 try
28 {
29 NativeUiTest11 fixture(512,512);
30 auto& device=fixture.device;auto& draws=device.Draws();auto& values=draws.Materials().Values();
31 XMFLOAT4X4 identity;XMStoreFloat4x4(&identity,XMMatrixIdentity());
32 values.Set("matWorld",identity);values.Set("matWorldViewProj",identity);
33 values.Set("ambient",XMFLOAT4(1,1,1,100000));values.Set("Tangent",XMFLOAT4(1,1,1,0));
34 values.Set("Sunlight",XMFLOAT4(0,0,-1,1));values.Set("Radiosity",XMFLOAT4(0,0,0,0));
35 values.Set("WeapPos",XMFLOAT4(0,0,0,0));values.Set("shiplight",XMFLOAT4(0,0,0,0));
36 values.Set("clouduv",XMFLOAT4(0,0,0,0));values.Set("volumeFog",XMFLOAT4(0,0,0,0));
37 values.Set("LandingLightCount",0);
38 auto window=Texture(device,0xff000000),paint=Texture(device,0x004080c0);
39 auto logo=Texture(device,0xffffffff),halfLogo=Texture(device,0x80ffffff),clearLogo=Texture(device,0x00ffffff);
40 auto redLogo=Texture(device,0xff0000ff),halfRedLogo=Texture(device,0x800000ff);
41 auto blackLogo=Texture(device,0xff000000),blueLogo=Texture(device,0xffff0000);
42 auto halfBlueLogo=Texture(device,0x80ff0000),softLight=Texture(device,0xff339999);
43 ID3D11ShaderResourceView* lightMap=logo.Get();
44 bool logoEnabled=true;
45 Geometry11 mesh;NativeUiTest11::HR(mesh.Create(device.Native(),sizeof(Vertex)*6,false));
46 VertexLayout11 layout;layout.stride=sizeof(Vertex);layout.normal=12;layout.uv[0]=24;layout.uv[1]=32;
47 auto render=[&](unsigned pass,bool passenger,bool paintable,float span,ID3D11ShaderResourceView* pigment,float volume=1.f,float cloak=1.f,bool lit=false)
48 {
49 // Sixteen cells across this patch resolve the round holes at close range.
50 const Vertex vertices[]={
51 {{-1,1,.1f},{0,0,-1},{0,0},{.5f,.5f}},{{1,1,.1f},{0,0,-1},{span,0},{.5f,.5f}},
52 {{-1,-1,.1f},{0,0,-1},{0,span},{.5f,.5f}},{{-1,-1,.1f},{0,0,-1},{0,span},{.5f,.5f}},
53 {{1,1,.1f},{0,0,-1},{span,0},{.5f,.5f}},{{1,-1,.1f},{0,0,-1},{span,span},{.5f,.5f}}};
54 void* destination=nullptr;NativeUiTest11::HR(mesh.Lock(0,sizeof(vertices),&destination));
55 memcpy(destination,vertices,sizeof(vertices));NativeUiTest11::HR(mesh.Unlock());
56 values.Set("Features",XMFLOAT4(cloak,logoEnabled ? 1.f : 0.f,volume,passenger ? 1.f : 0.f));
57 values.Texture("ColorMap",paintable ? paint.Get() : window.Get());values.Texture("OverlayMap",pigment);
58 values.Texture("ColorMap2",lightMap);values.Set("clouduv",XMFLOAT4(0,lit ? 1.f : 0.f,0,0));
59 draws.BindScene();device.Context()->ClearRenderTargetView(fixture.host.Device().SceneTarget(),std::array<float,4>{}.data());
60 draws.Pipeline().raster.CullMode=D3D11_CULL_NONE;
61 draws.Pipeline().depth.DepthEnable=FALSE;draws.Pipeline().blend.RenderTarget[0].BlendEnable=FALSE;
62 draws.Material(pass);NativeUiTest11::HR(draws.Draw(mesh,layout,Primitive11::Triangles,0,2));
63 return fixture.Pixels();
64 };
65 auto mean=[](const std::vector<DWORD>& pixels)
66 {
67 double sum=0;for(DWORD p:pixels)sum+=p&255;return sum/(pixels.size()*255.);
68 };
69 for(unsigned pass:{4u,5u})
70 {
71 const auto close=render(pass,true,false,16.f/512,logo.Get());
72 const double coverage=mean(close);
73 std::cout<<"Pass "<<pass<<" resolved window coverage="<<coverage<<'\n';
74 Check(std::abs(coverage-.5)<.025,"window film must have 50% average coverage");
75 unsigned holes=0,ink=0;
76 for(DWORD p:close){holes+=(p&255)<10;ink+=(p&255)>245;}
77 Check(holes>close.size()*.3 && ink>close.size()*.3,"resolved windows have clear holes and solid ink");
78 Check(mean(render(pass,true,true,16.f/512,logo.Get()))>.99,"paintable panels retain solid logos");
79 Check(mean(render(pass,false,false,16.f/512,logo.Get()))>.99,"other vehicle logos retain their coverage");
80 Check(mean(render(pass,true,false,16.f/512,clearLogo.Get()))<.01,"transparent logo pixels preserve the window");
81 Check(std::abs(mean(render(pass,true,false,16.f/512,halfLogo.Get()))-.25)<.025,"logo alpha multiplies perforation coverage");
82 const auto distant=render(pass,true,false,1.f,logo.Get());
83 Check(std::all_of(distant.begin(),distant.end(),[](DWORD p){return std::abs(int(p&255)-128)<=1;}),
84 "unresolved holes filter to half coverage without shimmer");
85 const auto cloaked=render(pass,true,false,16.f/512,logo.Get(),1,.5f);
86 Check(std::all_of(cloaked.begin(),cloaked.end(),[](DWORD p){return std::abs(int(p>>24)-128)<=1;}),
87 "perforation preserves vehicle cloaking alpha");
88 if(pass==5)
89 Check(std::abs(mean(render(pass,true,false,16.f/512,logo.Get(),0))-.5)<.025,
90 "passenger logo film also works above clouds without volumetric lighting");
91 // Saturated window illumination must be covered by printed ink.
92 // Red ink has zero blue; the clear holes reveal the white window.
93 const auto illuminated=render(pass,true,false,16.f/512,redLogo.Get(),1,1,true);
94 const double openArea=mean(illuminated);
95 std::cout<<"Pass "<<pass<<" illuminated window open area="<<openArea<<'\n';
96 Check(std::abs(openArea-.5)<.025,"window emission must not wash out the perforated logo");
97 Check(std::abs(mean(render(pass,true,false,16.f/512,halfRedLogo.Get(),1,1,true))-.75)<.025,
98 "translucent ink attenuates window emission according to logo alpha");
99 Check(mean(render(pass,true,false,16.f/512,clearLogo.Get(),1,1,true))>.99,
100 "transparent artwork preserves window illumination");
101 }
102 // Painted decals must receive the map's illumination without the old
103 // baked hull color bleaching the ink. Check actual production pixels.
104 values.Set("Sunlight",XMFLOAT4(0,0,-1,0));
105 values.Set("Tangent",XMFLOAT4(.1f,.8f,.3f,0));
106 values.Set("ambient",XMFLOAT4(.2f,.2f,.2f,100000));
107 lightMap=softLight.Get(); // red=.6, green=.6, blue=.2 (before trim tint)
108 auto nearColor=[](const std::vector<DWORD>& pixels,int red,int green,int blue,int alpha=255)
109 {
110 return std::all_of(pixels.begin(),pixels.end(),[&](DWORD p)
111 {return std::abs(int((p>>16)&255)-red)<=2 && std::abs(int((p>>8)&255)-green)<=2 &&
112 std::abs(int(p&255)-blue)<=2 && std::abs(int(p>>24)-alpha)<=1;});
113 };
114 for(unsigned pass:{4u,5u})
115 {
116 const auto painted=[&](ID3D11ShaderResourceView* pigment,bool lit=true,float cloak=1.f,bool passenger=false)
117 {return render(pass,passenger,true,16.f/512,pigment,1,cloak,lit);};
118 Check(nearColor(painted(blackLogo.Get()),0,0,0),"baked hull illumination cannot bleach opaque black paint");
119 Check(nearColor(painted(blueLogo.Get()),0,0,153),"paint uses untinted red-channel illumination like Makeship");
120 Check(nearColor(painted(blueLogo.Get(),true,1,true),0,0,153),"passenger painted panels do not use window-film lighting");
121 Check(nearColor(painted(blueLogo.Get(),false),0,0,51),"lights-off paint uses scene illumination only");
122 Check(nearColor(painted(blueLogo.Get(),true,.5f),0,0,153,128),"painted logos preserve cloaking alpha");
123 const auto clear=painted(clearLogo.Get());
124 logoEnabled=false;
125 Check(clear==painted(blueLogo.Get()),"transparent paint preserves the no-logo illuminated hull exactly");
126 logoEnabled=true;
127 const auto half=painted(halfBlueLogo.Get());
128 const auto solid=painted(blueLogo.Get());
129 for(unsigned shift:{0u,8u,16u})
130 {
131 const double expected=((clear[0]>>shift)&255)*(127./255)+((solid[0]>>shift)&255)*(128./255);
132 Check(std::abs(double((half[0]>>shift)&255)-expected)<=2,"partial paint alpha blends illuminated ink and hull");
133 }
134 values.Set("ambient",XMFLOAT4(.8f,.8f,.8f,100000));
135 Check(nearColor(painted(blueLogo.Get()),0,0,204),"brighter scene lighting takes precedence over baked illumination");
136 values.Set("ambient",XMFLOAT4(.2f,.2f,.2f,100000));
137 std::cout<<"Pass "<<pass<<" painted-logo lighting checks passed.\n";
138 }
139 fixture.DebugClean();
140 std::cout<<"Painted-logo lighting and passenger ARJ window-film GPU checks passed.\n";
141 return 0;
142 }
143 catch(const std::exception& error){std::cerr<<error.what()<<'\n';return 1;}
144}
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
HRESULT Create(ID3D11Device *device, UINT bytes, bool dynamic, bool index=false, bool indices32=false)
HRESULT Lock(UINT offset, UINT size, void **destination, bool readOnly=false, bool discard=false)
close(text, start, opening='(', closing=')')
Definition DDS.h:24