1#include "../Rendering/TextureMips11.h"
2#include <d3dx9.h>
3#include <wrl/client.h>
4#include <fstream>
5#include <iostream>
6#include <filesystem>
7#include <stdexcept>
8#include <cstdlib>
9#include <new>
10#include <algorithm>
11#include <dxgiformat.h>
12#include "../ThirdParty/DirectXTK/DDS.h"
13using Microsoft::WRL::ComPtr;
14
15
16namespace AllocationLimit {
17bool enabled=false;
18size_t maximum=SIZE_MAX,largest=0;
19struct Scope {
20 explicit Scope(size_t bytes){maximum=bytes;largest=0;enabled=true;}
21 ~Scope(){enabled=false;}
22};
23}
24void* operator new(size_t bytes)
25{
26 if(AllocationLimit::enabled){AllocationLimit::largest=(std::max)(AllocationLimit::largest,bytes);if(bytes>AllocationLimit::maximum)throw std::bad_alloc();}
27 if(void* memory=std::malloc(bytes ? bytes : 1))return memory;
28 throw std::bad_alloc();
29}
30void* operator new[](size_t bytes){return ::operator new(bytes);}
31void operator delete(void* memory) noexcept {std::free(memory);}
32void operator delete[](void* memory) noexcept {std::free(memory);}
33void operator delete(void* memory,size_t) noexcept {std::free(memory);}
34void operator delete[](void* memory,size_t) noexcept {std::free(memory);}
35namespace {
36unsigned checks=0;
37void Check(bool ok,const char* what){++checks;if(!ok)throw std::runtime_error(what);}
38void HR(
HRESULT hr,
const char* what){
if(FAILED(hr))std::cerr<<what<<
" HRESULT="<<std::hex<<hr<<std::dec<<
'\n';Check(SUCCEEDED(hr),what);}
39uint64_t
Hash(
const std::vector<uint8_t>& bytes)
40{
41 uint64_t value=14695981039346656037ull;for(const auto byte:bytes){value^=byte;value*=1099511628211ull;}return value;
42}
43void CheckMipFixtures()
44{
45
46
48
49
50 const uint64_t expected[]={
51 18057532386634783006ull,2967305515879902019ull,11670791209307012550ull,9756677596094840804ull,7888564466367049946ull,12748029914969980867ull,
52 9393433408961149424ull,4583141836593297764ull,10276516414570034197ull,6781562710592090401ull,3591700840649016626ull,6797037475105842617ull,
53 16507823559443194915ull,5782067726793718476ull,9700757797863837558ull,11313532427937034967ull,659024937391503872ull,9499537163829192474ull,
54 13173730880941257334ull,15567459811594931680ull,15494312992475546152ull,2498051502155388008ull,7695670519520574953ull,13585817076273530888ull,
55 9131142734060870780ull,10234502788895717296ull,4656787087459203153ull,2226497908375969281ull,12408901324881646316ull,3667756814512466891ull,
56 4349421562974609807ull,3429633498095629822ull,3829387834352682761ull,2703808957323399209ull,7515410831938016321ull,6859803032751124903ull};
57 size_t fixture=0;
58 for(UINT format=0;format<6;++format)for(const auto dimensions:{std::pair<UINT,UINT>{13,9},{1,17},{17,1},{2,3},{8,8}})
59 {
60 const auto& pf=formats[format];const UINT block=format<3 ? (format==0 ? 8 : 16) : 0;
63 std::vector<uint8_t> input(128);memcpy(input.data(),"DDS ",4);memcpy(input.data()+4,&header,sizeof(header));
66 {
67 const size_t count=block ? size_t((width+3)/4)*((height+3)/4)*block : size_t(width)*height*(pf.RGBBitCount/8);
68 const size_t offset=input.size();input.resize(offset+count);
69 for(size_t i=0;i<count;++i)input[offset+i]=uint8_t((i*37+i/7+level*53+format*19)&255);
70 width=(std::max)(1u,width/2);height=(std::max)(1u,height/2);
71 }
72 for(bool crop:{false,true})
73 {
74 if(crop && header.
width!=13)
continue;
75 std::vector<uint8_t>
output;
77 std::cout<<
"fixture "<<format<<
' '<<header.
width<<
'x'<<header.
height<<
" crop="<<crop<<
" hash="<<
Hash(output)<<
'\n';
78 Check(
Hash(output)==expected[fixture++],
"encoded mip chain matches original BOX filter");
79 }
80 }
81}
82void CheckConstrainedCockpitTexture()
83{
84 std::ifstream stream("Textures/Cockpits/E-10-saint-cockpit-glass.dds",std::ios::binary);
85 Check(bool(stream),"cockpit glass fixture exists");
86 const std::vector<uint8_t> input{std::istreambuf_iterator<char>(stream),{}};
87 std::vector<uint8_t> expected,limited;
89 std::cout<<
"cockpit glass hash="<<
Hash(expected)<<
'\n';
90 Check(
Hash(expected)==16961660095408999644ull,
"cockpit glass mips match original loader");
93 catch(const std::bad_alloc&){std::cerr<<"Cockpit mip generation threw bad_alloc; allocation bytes="<<AllocationLimit::largest<<'\n';throw;}
94 std::cout<<"Cockpit largest allocation bytes="<<AllocationLimit::largest<<'\n';
95 HR(hr,"cockpit mip generation with 12 MiB allocation limit");Check(limited==expected,"constrained texture is byte-identical");
96
97 for(const size_t cap:{0u,3u*1024*1024})
98 {
99 std::vector<uint8_t> denied;
101 Check(hr==E_OUTOFMEMORY && denied.empty(),"allocation failure returns E_OUTOFMEMORY without a partial DDS");
102 }
103 std::vector<uint8_t> singleLevel;
105 Check(hr==S_FALSE && singleLevel.empty() && AllocationLimit::largest==0,"single-level glass needs no mip workspace");
106}
107std::vector<DWORD> Decode(IDirect3DDevice9* device,IDirect3DTexture9* texture,UINT level)
108{
109 D3DSURFACE_DESC d;HR(texture->GetLevelDesc(level,&d),"read mip dimensions");ComPtr<IDirect3DSurface9> src,out;HR(texture->GetSurfaceLevel(level,&src),"mip source");
110 HR(device->CreateOffscreenPlainSurface(d.Width,d.Height,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&out,nullptr),"mip readback");
111 HR(D3DXLoadSurfaceFromSurface(out.Get(),nullptr,nullptr,src.Get(),nullptr,nullptr,D3DX_FILTER_NONE,0),"decode mip pixels");D3DLOCKED_RECT map;
112 HR(out->LockRect(&map,nullptr,D3DLOCK_READONLY),"map decoded mip");std::vector<DWORD> pixels(size_t(d.Width)*d.Height);
113 for(UINT y=0;y<d.Height;++y)memcpy(pixels.data()+size_t(y)*d.Width,static_cast<BYTE*>(map.pBits)+y*map.Pitch,d.Width*4);out->UnlockRect();return pixels;
114}
115}
117{
118 try {
119 CheckMipFixtures();
120 HWND window=CreateWindowW(L"STATIC",L"Texture mip contract",WS_POPUP,0,0,64,64,nullptr,nullptr,GetModuleHandleW(nullptr),nullptr);Check(window!=nullptr,"test window");
121 ComPtr<IDirect3D9> api;api.Attach(Direct3DCreate9(D3D_SDK_VERSION));ComPtr<IDirect3DDevice9> device;D3DPRESENT_PARAMETERS pp{};
122 pp.Windowed=TRUE;pp.SwapEffect=D3DSWAPEFFECT_DISCARD;pp.hDeviceWindow=window;pp.BackBufferWidth=pp.BackBufferHeight=64;
123 HR(api->CreateDevice(0,D3DDEVTYPE_HAL,window,D3DCREATE_HARDWARE_VERTEXPROCESSING|D3DCREATE_FPU_PRESERVE,&pp,&device),"reference device");
124 for(const wchar_t* file:{L"Textures/RTS/Marston1.dds",L"Textures/RTS/city2playerN.dds",L"Textures/pointsprite.dds"})
125 {
126 std::ifstream stream(std::filesystem::path(file),std::ios::binary);std::vector<uint8_t> input{std::istreambuf_iterator<char>(stream),{}},
output;
127 const bool cropped=wcsstr(file,L"Marston")!=nullptr;const UINT width=cropped ? 57 : 0,height=cropped ? 28 : 0;
130 ComPtr<IDirect3DTexture9> reference,native;
131 HR(D3DXCreateTextureFromFileExW(device.Get(),file,width ? width : D3DX_FROM_FILE,height ? height : D3DX_FROM_FILE,0,0,D3DFMT_FROM_FILE,D3DPOOL_MANAGED,D3DX_FILTER_NONE,D3DX_FILTER_BOX|D3DX_FILTER_MIRROR,0,nullptr,nullptr,&reference),"reference texture creation");
132 HR(D3DXCreateTextureFromFileInMemoryEx(device.Get(),bytes.data(),UINT(bytes.size()),D3DX_FROM_FILE,D3DX_FROM_FILE,D3DX_FROM_FILE,0,D3DFMT_FROM_FILE,D3DPOOL_MANAGED,D3DX_FILTER_NONE,D3DX_FILTER_NONE,0,nullptr,nullptr,&native),"read native DDS chain");
133 Check(reference->GetLevelCount()==native->GetLevelCount(),"complete mip count matches reference");
134 for(UINT level=0;level<native->GetLevelCount();++level)
135 {
136 const auto expected=Decode(device.Get(),reference.Get(),level),actual=Decode(device.Get(),native.Get(),level);Check(expected.size()==actual.size(),"mip sizes match");
137 double difference=0;for(size_t i=0;i<expected.size();++i)for(UINT shift:{0u,8u,16u,24u})difference+=std::abs(int((expected[i]>>shift)&255)-int((actual[i]>>shift)&255));
138 difference/=expected.size()*4;std::wcout<<file<<L" level "<<level<<L" mean channel error "<<difference<<L'\n';
139 Check(difference<=(level ? 5.0 : .1),"native DDS preserves texture filter/compression appearance");
140 }
141 std::vector<uint8_t> repeat;Check(
Rendering::PrepareTextureDDS(bytes.data(),bytes.size(),0,0,0,repeat)==S_FALSE && repeat.empty(),
"authored complete chain remains untouched");
142 }
143 DestroyWindow(window);CheckConstrainedCockpitTexture();std::cout<<"PASS: "<<checks<<" texture mip contract checks\n";return 0;
144 } catch(const std::exception& e){std::cerr<<"FAIL: "<<e.what()<<'\n';return 1;}
145}
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
uint32_t Hash(int x, int y, int z, uint32_t salt)
DDSGLOBALCONST DDS_PIXELFORMAT DDSPF_A8L8
DDSGLOBALCONST DDS_PIXELFORMAT DDSPF_DXT1
DDSGLOBALCONST DDS_PIXELFORMAT DDSPF_A8R8G8B8
DDSGLOBALCONST DDS_PIXELFORMAT DDSPF_DXT5
DDSGLOBALCONST DDS_PIXELFORMAT DDSPF_DXT3
DDSGLOBALCONST DDS_PIXELFORMAT DDSPF_L8
HRESULT PrepareTextureDDS(const uint8_t *data, size_t size, UINT requested, UINT requestedWidth, UINT requestedHeight, std::vector< uint8_t > &output)