1#include "../Rendering/Device11.h"
2#include "../Rendering/ShaderBytecode11.h"
3#include "../Rendering/Shaders/Catalog11.generated.h"
4#include "../Rendering/FixedFunction11.h"
5#include "../Rendering/CloudPass11.h"
6#include "../Rendering/WindshieldRain11.h"
7#include "../Rendering/ReentryEnvelope11.h"
8#include <d3dcompiler.h>
9#include <d3d11sdklayers.h>
10#include <iostream>
11#include <stdexcept>
12#include <cstring>
14using Microsoft::WRL::ComPtr;
15namespace
16{
17 void Check(bool value,const char* reason){if(!value)throw std::runtime_error(reason);}
18 void HR(
HRESULT hr,
const char* reason){
if(FAILED(hr))
throw std::runtime_error(std::string(reason)+
": "+std::to_string(hr));}
19}
20int main(int argc,char** argv)
21{
22 try
23 {
26 const char excessive[]={'R','Z','1','1',0,0,0,127,0};
27 Check(
DecodeShaderBytecode11(excessive,
sizeof(excessive)).empty(),
"oversized packed shader rejected before allocation");
28 const char damaged[]={'R','Z','1','1',4,0,0,0,1,2,3,4};
30 bool warp=false,debug=true;std::filesystem::path resourceFile,referenceDirectory;
31 for(int i=1;i<argc;++i)
32 {
33 if(!strcmp(argv[i],"--warp"))warp=true;
34 else if(!strcmp(argv[i],"--no-debug-layer"))debug=false;
35 else if(!strcmp(argv[i],"--module") && i+1<argc)resourceFile=argv[++i];
36 else if(!strcmp(argv[i],"--compare-dir") && i+1<argc)referenceDirectory=argv[++i];
37 else throw std::runtime_error("unknown argument");
38 }
39 Device11 gpu;HR(gpu.
Create({warp ? D3D_DRIVER_TYPE_WARP : D3D_DRIVER_TYPE_HARDWARE,debug}),
"device");
40 std::cout<<gpu.
Status()<<std::endl;
41 HMODULE resourceModule=nullptr;
42 if(!resourceFile.empty())
43 {
44 resourceModule=LoadLibraryExW(resourceFile.c_str(),nullptr,LOAD_LIBRARY_AS_DATAFILE|LOAD_LIBRARY_AS_IMAGE_RESOURCE);
45 Check(resourceModule!=nullptr,"module resource load");
46 }
48 {
50 if(code.empty())throw std::runtime_error("missing packaged shader: "+std::filesystem::path(artifact.name).string());
51 Check(
DecodeShaderBytecode11(code.data(),code.size())==code,
"historical uncompressed shader remains supported");
52 if(!referenceDirectory.empty())
53 Check(code==
ReadShaderBytecode11(referenceDirectory/artifact.name),
"packaged bytecode differs from the parity-test shader");
54 ComPtr<ID3D11ShaderReflection> reflection;
55 HR(D3DReflect(code.data(),code.size(),IID_PPV_ARGS(&reflection)),"reflection");
56 D3D11_SHADER_DESC desc{};HR(reflection->GetDesc(&desc),"reflection description");
57 Check(D3D11_SHVER_GET_MAJOR(desc.Version)==5,"shader model");
58 if(artifact.profile[0]=='v')
59 {
60 ComPtr<ID3D11VertexShader> shader;
61 HR(gpu.
Device()->CreateVertexShader(code.data(),code.size(),
nullptr,&shader),
"packaged vertex");
62 }
63 else if(artifact.profile[0]=='p')
64 {
65 ComPtr<ID3D11PixelShader> shader;
66 HR(gpu.
Device()->CreatePixelShader(code.data(),code.size(),
nullptr,&shader),
"packaged pixel");
67 }
68 else if(artifact.profile[0]=='g')
69 {
70 ComPtr<ID3D11GeometryShader> shader;
71 HR(gpu.
Device()->CreateGeometryShader(code.data(),code.size(),
nullptr,&shader),
"packaged geometry");
72 }
73 else if(artifact.profile[0]=='c')
74 {
75 ComPtr<ID3D11ComputeShader> shader;
76 HR(gpu.
Device()->CreateComputeShader(code.data(),code.size(),
nullptr,&shader),
"packaged compute");
77 }
78 else throw std::runtime_error("unknown profile");
79 }
81 Check(
ReadShaderBytecode11(L
"missing-directory/FixedFunction-FixedVertex-1.cso").empty(),
"explicit loose path does not fall back");
82 if(resourceModule)FreeLibrary(resourceModule);
87 Check(reentry.
Ready(),
"packaged reentry material is not ready");
89 if(SUCCEEDED(gpu.
Device()->QueryInterface(IID_PPV_ARGS(&messages))))
90 {
91 for(UINT64 i=0;i<
messages->GetNumStoredMessages();++i)
92 {
93 SIZE_T size=0;
messages->GetMessage(i,
nullptr,&size);std::vector<char> data(size);
94 auto* message=
reinterpret_cast<D3D11_MESSAGE*
>(data.data());HR(
messages->GetMessage(i,message,&size),
"debug message");
95 if(message->Severity<=D3D11_MESSAGE_SEVERITY_WARNING)throw std::runtime_error(message->pDescription);
96 }
97 }
98 std::cout<<std::size(
NativeShaderArtifacts)<<
" packaged shaders created; native passes initialized without loose files\n";
99 if(!referenceDirectory.empty())std::cout<<"PASS: every packaged shader matches the parity-test bytecode\n";
100 return 0;
101 }
102 catch(const std::exception& error){std::cerr<<"FAIL: "<<error.what()<<'\n';return 1;}
103}
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
HRESULT Initialize(ID3D11Device *device, const std::filesystem::path &shaderDirectory={})
ID3D11Device * Device() const
const std::string & Status() const
HRESULT Create(const DeviceOptions11 &options={})
HRESULT Initialize(ID3D11Device *device, const std::filesystem::path &shaderDirectory={})
HRESULT Initialize(ID3D11Device *device, const std::filesystem::path &directory={})
HRESULT Initialize(ID3D11Device *device, const std::filesystem::path &shaderDirectory={})
std::vector< char > DecodeShaderBytecode11(const char *data, size_t size)
constexpr ShaderArtifact11 NativeShaderArtifacts[]
std::vector< char > ReadShaderBytecode11(const std::filesystem::path &path, HMODULE resourceModule=nullptr)