Rise
The Vieneo Province
Loading...
Searching...
No Matches
ShaderBytecode11.h
Go to the documentation of this file.
1#pragma once
2#include <Windows.h>
3#include <compressapi.h>
4#include <cstring>
5#include <cstdint>
6#include <filesystem>
7#include <fstream>
8#include <vector>
9#pragma comment(lib,"cabinet.lib")
10
11namespace Rendering
12{
13 inline HRSRC FindShaderResource11(const std::filesystem::path& path,HMODULE module)
14 {
15 auto resourceName=L"R11_"+path.stem().native();
16 for(auto& character:resourceName)
17 {
18 if(character==L'-')character=L'_';
19 if(character>=L'a' && character<=L'z')character-=L'a'-L'A';
20 }
21 return FindResourceW(module,resourceName.c_str(),L"RISE_D3D11_SHADER");
22 }
23 inline bool HasShaderBytecode11(const std::filesystem::path& path)
24 {
25 if(path.has_parent_path()){std::error_code error;return std::filesystem::is_regular_file(path,error);}
26 return FindShaderResource11(path,GetModuleHandleW(nullptr))!=nullptr;
27 }
28 inline std::vector<char> DecodeShaderBytecode11(const char* data,size_t size)
29 {
30 if(!data || size<4)return {};
31 // Historical/loose packages remain ordinary DXBC. Packed resources use
32 // lossless Windows XPRESS-Huffman, expanded only for the requested shader.
33 if(std::memcmp(data,"DXBC",4)==0)return {data,data+size};
34 if(size<=8 || std::memcmp(data,"RZ11",4)!=0)return {};
35 uint32_t bytes=0;std::memcpy(&bytes,data+4,4);
36 if(bytes<4 || bytes>16u*1024*1024)return {};
37 std::vector<char> code(bytes);DECOMPRESSOR_HANDLE decompressor=nullptr;
38 if(!CreateDecompressor(COMPRESS_ALGORITHM_XPRESS_HUFF,nullptr,&decompressor))return {};
39 SIZE_T written=0;
40 const BOOL ok=Decompress(decompressor,data+8,size-8,code.data(),code.size(),&written);
41 CloseDecompressor(decompressor);
42 if(!ok || written!=bytes || std::memcmp(code.data(),"DXBC",4)!=0)return {};
43 return code;
44 }
45 // Bare names resolve only from this executable's build-time resources.
46 // A directory explicitly opts into loose bytecode for parity experiments.
47 // Never compile shaders on the render thread or silently use stale files.
48 inline std::vector<char> ReadShaderBytecode11(const std::filesystem::path& path,HMODULE resourceModule=nullptr)
49 {
50 if(path.has_parent_path())
51 {
52 std::ifstream file(path,std::ios::binary);
53 return {std::istreambuf_iterator<char>(file),std::istreambuf_iterator<char>()};
54 }
55 const HMODULE module=resourceModule ? resourceModule : GetModuleHandleW(nullptr);
56 const HRSRC resource=FindShaderResource11(path,module);
57 if(!resource)return {};
58 const DWORD size=SizeofResource(module,resource);
59 const auto* data=static_cast<const char*>(LockResource(LoadResource(module,resource)));
60 if(!data || !size)return {};
61 return DecodeShaderBytecode11(data,size);
62 }
63}
SCRIPT_CONTROL SCRIPT_STATE const void SCRIPT_CONTROL SCRIPT_STATE const int SCRIPT_TABDEF const BYTE SCRIPT_STRING_ANALYSIS BOOL
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
HRSRC FindShaderResource11(const std::filesystem::path &path, HMODULE module)
bool HasShaderBytecode11(const std::filesystem::path &path)
std::vector< char > DecodeShaderBytecode11(const char *data, size_t size)
std::vector< char > ReadShaderBytecode11(const std::filesystem::path &path, HMODULE resourceModule=nullptr)