19 HRESULT ExpandLegacyDDS(
const uint8_t* data,
size_t size, std::vector<uint8_t>& expanded)
21 if (size < 4 +
sizeof(DirectX::DDS_HEADER))
return E_INVALIDARG;
22 DirectX::DDS_HEADER header{};
23 memcpy(&header, data + 4,
sizeof(header));
24 if (header.
size !=
sizeof(header) || header.
ddspf.
size !=
sizeof(header.
ddspf))
return E_INVALIDARG;
25 const auto& pf = header.
ddspf;
26 const bool l8 = (pf.flags & 0x20000) && pf.
RGBBitCount == 8 && pf.RBitMask == 255;
27 const bool la8 = (pf.flags & 0x20000) && pf.RGBBitCount == 16 && pf.RBitMask == 255 && pf.ABitMask == 0xff00;
28 const bool bgraVolume = (header.
caps2 & 0x200000) && pf.RGBBitCount == 32
29 && pf.RBitMask == 0xff0000 && pf.GBitMask == 0xff00 && pf.BBitMask == 255;
30 if (!l8 && !la8 && !bgraVolume)
return S_FALSE;
31 if (!header.width || !header.height || header.width > D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION
32 || header.height > D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION || header.depth > D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION
33 || header.mipMapCount > D3D11_REQ_MIP_LEVELS)
return E_INVALIDARG;
34 const UINT sourceBytes = l8 ? 1 : la8 ? 2 : 4;
35 expanded.assign(data, data + 4 +
sizeof(header));
36 size_t offset = expanded.size();
37 const UINT faces = (header.caps2 & 0x200) ? 6 : 1;
38 for (UINT face = 0; face < faces; ++face)
40 UINT w = header.width, h = header.height;
41 UINT d = (header.caps2 & 0x200000) ? (std::max)(1u, header.depth) : 1;
42 for (UINT level = 0; level < (std::max)(1u, header.mipMapCount); ++level)
44 const uint64_t pixels = uint64_t(w) * h * d;
45 if (pixels > (size - offset) / sourceBytes || pixels > (UINT32_MAX - expanded.size()) / 4)
46 return HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
47 const size_t start = expanded.size();
48 expanded.resize(start +
size_t(pixels) * 4);
49 for (
size_t i = 0; i < size_t(pixels); ++i)
51 const auto* p = data + offset + i * sourceBytes;
52 auto* q = expanded.data() + start + i * 4;
53 q[0] = bgraVolume ? p[2] : p[0];
54 q[1] = bgraVolume ? p[1] : p[0];
56 q[3] = la8 ? p[1] : bgraVolume && pf.ABitMask ? p[3] : 255;
58 offset += size_t(pixels) * sourceBytes;
59 w = (std::max)(1u, w / 2); h = (std::max)(1u, h / 2); d = (std::max)(1u, d / 2);
62 header.ddspf = {32, 0x41, 0, 32, 255, 0xff00, 0xff0000, 0xff000000};
63 header.flags = (header.flags & ~0x80000u) | 8;
64 header.pitchOrLinearSize = header.width * 4;
65 memcpy(expanded.data() + 4, &header,
sizeof(header));
72 if (!path)
return E_INVALIDARG;
73 std::ifstream file(std::filesystem::path(path), std::ios::binary);
74 if (!file) {
status =
"open texture failed";
return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND); }
75 const std::vector<uint8_t> bytes{std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()};
76 return Load(device, context, bytes.data(), bytes.size());
81 if (!device || !data || size < 4)
return E_INVALIDARG;
82 Microsoft::WRL::ComPtr<ID3D11Resource> next;
83 Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> nextView;
85 if (!memcmp(data,
"DDS ", 4))
87 std::vector<uint8_t> expanded;
88 hr = ExpandLegacyDDS(data, size, expanded);
89 if (FAILED(hr)) {
status =
"invalid legacy DDS";
return hr; }
90 if (!expanded.empty()) { data = expanded.data(); size = expanded.size(); }
97 D3D11_BIND_SHADER_RESOURCE, 0, 0,
100 if (FAILED(hr)) {
status =
"native texture decode/create failed: HRESULT=" + std::to_string(hr);
return hr; }
101 resource = std::move(next);
view = std::move(nextView);
status =
"native texture ready";
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
HRESULT Load(ID3D11Device *device, ID3D11DeviceContext *context, const wchar_t *path)
Microsoft::WRL::ComPtr< ID3D11Resource > resource
Microsoft::WRL::ComPtr< ID3D11ShaderResourceView > view
@ WIC_LOADER_FORCE_RGBA32
DIRECTX_TOOLKIT_API HRESULT __cdecl CreateWICTextureFromMemoryEx(_In_ ID3D11Device *d3dDevice, _In_reads_bytes_(wicDataSize) const uint8_t *wicData, _In_ size_t wicDataSize, _In_ size_t maxsize, _In_ D3D11_USAGE usage, _In_ unsigned int bindFlags, _In_ unsigned int cpuAccessFlags, _In_ unsigned int miscFlags, _In_ WIC_LOADER_FLAGS loadFlags, _Outptr_opt_ ID3D11Resource **texture, _Outptr_opt_ ID3D11ShaderResourceView **textureView) noexcept
DIRECTX_TOOLKIT_API HRESULT __cdecl CreateDDSTextureFromMemoryEx(_In_ ID3D11Device *d3dDevice, _In_reads_bytes_(ddsDataSize) const uint8_t *ddsData, _In_ size_t ddsDataSize, _In_ size_t maxsize, _In_ D3D11_USAGE usage, _In_ unsigned int bindFlags, _In_ unsigned int cpuAccessFlags, _In_ unsigned int miscFlags, _In_ DDS_LOADER_FLAGS loadFlags, _Outptr_opt_ ID3D11Resource **texture, _Outptr_opt_ ID3D11ShaderResourceView **textureView, _Out_opt_ DDS_ALPHA_MODE *alphaMode=nullptr) noexcept