Rise
The Vieneo Province
Loading...
Searching...
No Matches
Texture11.cpp
Go to the documentation of this file.
1#include "Texture11.h"
5#include <algorithm>
6#include <cstring>
7#include <filesystem>
8#include <fstream>
9#include <vector>
10
11namespace Rendering
12{
13 namespace
14 {
15 // D3D9 luminance sampling replicated L to RGB. DXGI R8/R8G8 sampling
16 // does not. Expand these authored files, including alpha and every mip,
17 // so callers do not need asset-specific shader swizzles. BGRA volumes
18 // also need RGBA because BGRA Texture3D is not universally supported.
19 HRESULT ExpandLegacyDDS(const uint8_t* data, size_t size, std::vector<uint8_t>& expanded)
20 {
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)
39 {
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)
43 {
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)
50 {
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];
55 q[2] = p[0];
56 q[3] = la8 ? p[1] : bgraVolume && pf.ABitMask ? p[3] : 255;
57 }
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);
60 }
61 }
62 header.ddspf = {32, 0x41, 0, 32, 255, 0xff00, 0xff0000, 0xff000000};
63 header.flags = (header.flags & ~0x80000u) | 8; // pitch, not linear size
64 header.pitchOrLinearSize = header.width * 4;
65 memcpy(expanded.data() + 4, &header, sizeof(header));
66 return S_OK;
67 }
68 }
69
70 HRESULT Texture11::Load(ID3D11Device* device, ID3D11DeviceContext* context, const wchar_t* path)
71 {
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());
77 }
78
79 HRESULT Texture11::Load(ID3D11Device* device, ID3D11DeviceContext* context, const uint8_t* data, size_t size)
80 {
81 if (!device || !data || size < 4) return E_INVALIDARG;
82 Microsoft::WRL::ComPtr<ID3D11Resource> next;
83 Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> nextView;
84 HRESULT hr;
85 if (!memcmp(data, "DDS ", 4))
86 {
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(); }
91 hr = DirectX::CreateDDSTextureFromMemoryEx(device, data, size, 0, D3D11_USAGE_DEFAULT,
92 D3D11_BIND_SHADER_RESOURCE, 0, 0, DirectX::DDS_LOADER_IGNORE_SRGB, &next, &nextView);
93 }
94 else
95 {
96 hr = DirectX::CreateWICTextureFromMemoryEx(device, context, data, size, 0, D3D11_USAGE_DEFAULT,
97 D3D11_BIND_SHADER_RESOURCE, 0, 0,
99 }
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";
102 return S_OK;
103 }
104}
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
HRESULT Load(ID3D11Device *device, ID3D11DeviceContext *context, const wchar_t *path)
Definition Texture11.cpp:70
Microsoft::WRL::ComPtr< ID3D11Resource > resource
Definition Texture11.h:23
Microsoft::WRL::ComPtr< ID3D11ShaderResourceView > view
Definition Texture11.h:24
std::string status
Definition Texture11.h:25
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
uint32_t size
Definition DDS.h:264
uint32_t caps2
Definition DDS.h:274
DDS_PIXELFORMAT ddspf
Definition DDS.h:272
uint32_t RGBBitCount
Definition DDS.h:35