-
Notifications
You must be signed in to change notification settings - Fork 450
/
DirectXTexEXR.cpp
647 lines (550 loc) · 17.5 KB
/
DirectXTexEXR.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
//--------------------------------------------------------------------------------------
// File: DirectXTexEXR.cpp
//
// DirectXTex Auxilary functions for using the OpenEXR library
//
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//--------------------------------------------------------------------------------------
#include "DirectXTexP.h"
#include "DirectXTexEXR.h"
#include <DirectXPackedVector.h>
#include <cassert>
#include <cstdint>
#include <exception>
#include <memory>
#include <stdexcept>
#include <string>
#include <tuple>
//
// Requires the OpenEXR library <http://www.openexr.com/> and its dependencies.
//
#ifdef __clang__
#pragma clang diagnostic ignored "-Wswitch-enum"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#pragma clang diagnostic ignored "-Wdeprecated-dynamic-exception-spec"
#pragma clang diagnostic ignored "-Wfloat-equal"
#pragma clang diagnostic ignored "-Wimplicit-int-conversion"
#pragma clang diagnostic ignored "-Wlanguage-extension-token"
#pragma clang diagnostic ignored "-Wold-style-cast"
#pragma clang diagnostic ignored "-Wreserved-id-macro"
#pragma clang diagnostic ignored "-Wshadow-field-in-constructor"
#pragma clang diagnostic ignored "-Wsign-conversion"
#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
#endif
#pragma warning(push)
#pragma warning(disable : 4244 4996)
#include <ImfRgbaFile.h>
#include <ImfIO.h>
// https://openexr.com/en/latest/PortingGuide.html
#include <OpenEXRConfig.h>
#pragma warning(pop)
#define COMBINED_OPENEXR_VERSION ((10000*OPENEXR_VERSION_MAJOR) + (100*OPENEXR_VERSION_MINOR) + OPENEXR_VERSION_PATCH)
#ifdef __clang__
#pragma clang diagnostic pop
#endif
static_assert(sizeof(Imf::Rgba) == 8, "Mismatch size");
using namespace DirectX;
using PackedVector::XMHALF4;
#ifdef _WIN32
namespace
{
class com_exception : public std::exception
{
public:
com_exception(HRESULT hr) noexcept : result(hr) {}
const char* what() const noexcept override
{
static char s_str[64] = {};
sprintf_s(s_str, "Failure with HRESULT of %08X", static_cast<unsigned int>(result));
return s_str;
}
HRESULT get_result() const noexcept { return result; }
private:
HRESULT result;
};
class InputStream : public Imf::IStream
{
public:
InputStream(HANDLE hFile, const char fileName[]) :
IStream(fileName), m_hFile(hFile)
{
const LARGE_INTEGER dist = {};
LARGE_INTEGER result;
if (!SetFilePointerEx(m_hFile, dist, &result, FILE_END))
{
throw com_exception(HRESULT_FROM_WIN32(GetLastError()));
}
m_EOF = result.QuadPart;
if (!SetFilePointerEx(m_hFile, dist, nullptr, FILE_BEGIN))
{
throw com_exception(HRESULT_FROM_WIN32(GetLastError()));
}
}
InputStream(const InputStream&) = delete;
InputStream& operator = (const InputStream&) = delete;
InputStream(InputStream&&) = delete;
InputStream& operator=(InputStream&&) = delete;
bool read(char c[], int n) override
{
DWORD bytesRead;
if (!ReadFile(m_hFile, c, static_cast<DWORD>(n), &bytesRead, nullptr))
{
throw com_exception(HRESULT_FROM_WIN32(GetLastError()));
}
const LARGE_INTEGER dist = {};
LARGE_INTEGER result;
if (!SetFilePointerEx(m_hFile, dist, &result, FILE_CURRENT))
{
throw com_exception(HRESULT_FROM_WIN32(GetLastError()));
}
return result.QuadPart >= m_EOF;
}
uint64_t tellg() override
{
const LARGE_INTEGER dist = {};
LARGE_INTEGER result;
if (!SetFilePointerEx(m_hFile, dist, &result, FILE_CURRENT))
{
throw com_exception(HRESULT_FROM_WIN32(GetLastError()));
}
return static_cast<uint64_t>(result.QuadPart);
}
void seekg(uint64_t pos) override
{
LARGE_INTEGER dist;
dist.QuadPart = static_cast<LONGLONG>(pos);
if (!SetFilePointerEx(m_hFile, dist, nullptr, FILE_BEGIN))
{
throw com_exception(HRESULT_FROM_WIN32(GetLastError()));
}
}
void clear() override
{
SetLastError(0);
}
#if COMBINED_OPENEXR_VERSION >= 30300
int64_t read(void *buf, uint64_t sz, uint64_t offset) override
{
return Imf::IStream::read(buf, sz, offset);
}
#endif
private:
HANDLE m_hFile;
LONGLONG m_EOF;
};
class OutputStream : public Imf::OStream
{
public:
OutputStream(HANDLE hFile, const char fileName[]) :
OStream(fileName), m_hFile(hFile) {}
OutputStream(const OutputStream&) = delete;
OutputStream& operator = (const OutputStream&) = delete;
OutputStream(OutputStream&&) = delete;
OutputStream& operator=(OutputStream&&) = delete;
void write(const char c[], int n) override
{
DWORD bytesWritten;
if (!WriteFile(m_hFile, c, static_cast<DWORD>(n), &bytesWritten, nullptr))
{
throw com_exception(HRESULT_FROM_WIN32(GetLastError()));
}
}
uint64_t tellp() override
{
const LARGE_INTEGER dist = {};
LARGE_INTEGER result;
if (!SetFilePointerEx(m_hFile, dist, &result, FILE_CURRENT))
{
throw com_exception(HRESULT_FROM_WIN32(GetLastError()));
}
return static_cast<uint64_t>(result.QuadPart);
}
void seekp(uint64_t pos) override
{
LARGE_INTEGER dist;
dist.QuadPart = static_cast<LONGLONG>(pos);
if (!SetFilePointerEx(m_hFile, dist, nullptr, FILE_BEGIN))
{
throw com_exception(HRESULT_FROM_WIN32(GetLastError()));
}
}
private:
HANDLE m_hFile;
};
}
#endif // _WIN32
//=====================================================================================
// Entry-points
//=====================================================================================
//-------------------------------------------------------------------------------------
// Obtain metadata from EXR file on disk
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT DirectX::GetMetadataFromEXRFile(const wchar_t* szFile, TexMetadata& metadata)
{
if (!szFile)
return E_INVALIDARG;
#ifdef _WIN32
std::string fileName;
const int nameLength = WideCharToMultiByte(CP_UTF8, 0, szFile, -1, nullptr, 0, nullptr, nullptr);
if (nameLength > 0)
{
fileName.resize(static_cast<size_t>(nameLength));
const int result = WideCharToMultiByte(CP_UTF8, 0, szFile, -1, fileName.data(), nameLength, nullptr, nullptr);
if (result <= 0)
{
fileName.clear();
}
}
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
ScopedHandle hFile(safe_handle(CreateFile2(
szFile,
GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING,
nullptr)));
#else
ScopedHandle hFile(safe_handle(CreateFileW(
szFile,
GENERIC_READ, FILE_SHARE_READ,
nullptr,
OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN,
nullptr)));
#endif
if (!hFile)
{
return HRESULT_FROM_WIN32(GetLastError());
}
InputStream stream(hFile.get(), fileName.c_str());
#else
std::wstring wFileName(szFile);
std::string fileName(wFileName.cbegin(), wFileName.cend());
#endif
HRESULT hr = S_OK;
try
{
#ifdef _WIN32
Imf::RgbaInputFile file(stream);
#else
Imf::RgbaInputFile file(fileName.c_str());
#endif
const auto dw = file.dataWindow();
const int width = dw.max.x - dw.min.x + 1;
int height = dw.max.y - dw.min.y + 1;
size_t arraySize = 1;
if (width < 1 || height < 1)
return E_FAIL;
if (file.header().find("envmap") != file.header().end())
{
if (width == height / 6)
{
height = width;
arraySize = 6;
}
}
metadata.width = static_cast<size_t>(width);
metadata.height = static_cast<size_t>(height);
metadata.depth = metadata.mipLevels = 1;
metadata.arraySize = arraySize;
metadata.format = DXGI_FORMAT_R16G16B16A16_FLOAT;
metadata.dimension = TEX_DIMENSION_TEXTURE2D;
}
#ifdef _WIN32
catch (const com_exception& exc)
{
#ifdef _DEBUG
OutputDebugStringA(exc.what());
#endif
hr = exc.get_result();
}
#endif
#if defined(_WIN32) && defined(_DEBUG)
catch (const std::exception& exc)
{
OutputDebugStringA(exc.what());
hr = E_FAIL;
}
#else
catch (const std::exception&)
{
hr = E_FAIL;
}
#endif
catch (...)
{
hr = E_UNEXPECTED;
}
return hr;
}
//-------------------------------------------------------------------------------------
// Load a EXR file from disk
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT DirectX::LoadFromEXRFile(const wchar_t* szFile, TexMetadata* metadata, ScratchImage& image)
{
if (!szFile)
return E_INVALIDARG;
image.Release();
if (metadata)
{
memset(metadata, 0, sizeof(TexMetadata));
}
#ifdef _WIN32
std::string fileName;
const int nameLength = WideCharToMultiByte(CP_UTF8, 0, szFile, -1, nullptr, 0, nullptr, nullptr);
if (nameLength > 0)
{
fileName.resize(static_cast<size_t>(nameLength));
const int result = WideCharToMultiByte(CP_UTF8, 0, szFile, -1, fileName.data(), nameLength, nullptr, nullptr);
if (result <= 0)
{
fileName.clear();
}
}
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
ScopedHandle hFile(safe_handle(CreateFile2(
szFile,
GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING,
nullptr)));
#else
ScopedHandle hFile(safe_handle(CreateFileW(
szFile,
GENERIC_READ, FILE_SHARE_READ,
nullptr,
OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN,
nullptr)));
#endif
if (!hFile)
{
return HRESULT_FROM_WIN32(GetLastError());
}
InputStream stream(hFile.get(), fileName.c_str());
#else
std::wstring wFileName(szFile);
std::string fileName(wFileName.cbegin(), wFileName.cend());
#endif
HRESULT hr = S_OK;
try
{
#ifdef _WIN32
Imf::RgbaInputFile file(stream);
#else
Imf::RgbaInputFile file(fileName.c_str());
#endif
auto const dw = file.dataWindow();
const int width = dw.max.x - dw.min.x + 1;
int height = dw.max.y - dw.min.y + 1;
size_t arraySize = 1;
if (width < 1 || height < 1)
return E_FAIL;
if (file.header().find("envmap") != file.header().end())
{
if (width == height / 6)
{
height = width;
arraySize = 6;
}
}
if (metadata)
{
metadata->width = static_cast<size_t>(width);
metadata->height = static_cast<size_t>(height);
metadata->depth = metadata->mipLevels = 1;
metadata->arraySize = arraySize;
metadata->format = DXGI_FORMAT_R16G16B16A16_FLOAT;
metadata->dimension = TEX_DIMENSION_TEXTURE2D;
}
hr = image.Initialize2D(DXGI_FORMAT_R16G16B16A16_FLOAT,
static_cast<size_t>(width), static_cast<size_t>(height), arraySize, 1u);
if (FAILED(hr))
return hr;
file.setFrameBuffer(reinterpret_cast<Imf::Rgba*>(image.GetPixels()) - dw.min.x - dw.min.y * width, 1, static_cast<size_t>(width));
file.readPixels(dw.min.y, dw.max.y);
}
#ifdef _WIN32
catch (const com_exception& exc)
{
#ifdef _DEBUG
OutputDebugStringA(exc.what());
#endif
hr = exc.get_result();
}
#endif
#if defined(_WIN32) && defined(_DEBUG)
catch (const std::exception& exc)
{
OutputDebugStringA(exc.what());
hr = E_FAIL;
}
#else
catch (const std::exception&)
{
hr = E_FAIL;
}
#endif
catch (...)
{
hr = E_UNEXPECTED;
}
if (FAILED(hr))
{
image.Release();
}
return hr;
}
//-------------------------------------------------------------------------------------
// Save a EXR file to disk
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT DirectX::SaveToEXRFile(const Image& image, const wchar_t* szFile)
{
if (!szFile)
return E_INVALIDARG;
if (!image.pixels)
return E_POINTER;
if (image.width > INT32_MAX || image.height > INT32_MAX)
return /* HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED) */ static_cast<HRESULT>(0x80070032L);
switch (image.format)
{
case DXGI_FORMAT_R16G16B16A16_FLOAT:
if ((image.rowPitch % 8) > 0)
return E_FAIL;
break;
case DXGI_FORMAT_R32G32B32A32_FLOAT:
case DXGI_FORMAT_R32G32B32_FLOAT:
break;
default:
return /* HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED) */ static_cast<HRESULT>(0x80070032L);
}
#ifdef _WIN32
std::string fileName;
const int nameLength = WideCharToMultiByte(CP_UTF8, 0, szFile, -1, nullptr, 0, nullptr, nullptr);
if (nameLength > 0)
{
fileName.resize(static_cast<size_t>(nameLength));
const int result = WideCharToMultiByte(CP_UTF8, 0, szFile, -1, fileName.data(), nameLength, nullptr, nullptr);
if (result <= 0)
{
fileName.clear();
}
}
// Create file and write header
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
ScopedHandle hFile(safe_handle(CreateFile2(
szFile,
GENERIC_WRITE, 0, CREATE_ALWAYS,
nullptr)));
#else
ScopedHandle hFile(safe_handle(CreateFileW(
szFile,
GENERIC_WRITE, 0,
nullptr,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
nullptr)));
#endif
if (!hFile)
{
return HRESULT_FROM_WIN32(GetLastError());
}
auto_delete_file delonfail(hFile.get());
OutputStream stream(hFile.get(), fileName.c_str());
#else
std::wstring wFileName(szFile);
std::string fileName(wFileName.cbegin(), wFileName.cend());
#endif
HRESULT hr = S_OK;
try
{
const int width = static_cast<int>(image.width);
const int height = static_cast<int>(image.height);
#ifdef _WIN32
Imf::RgbaOutputFile file(stream, Imf::Header(width, height), Imf::WRITE_RGBA);
#else
Imf::RgbaOutputFile file(fileName.c_str(), Imf::Header(width, height), Imf::WRITE_RGBA);
#endif
if (image.format == DXGI_FORMAT_R16G16B16A16_FLOAT)
{
file.setFrameBuffer(reinterpret_cast<const Imf::Rgba*>(image.pixels), 1, image.rowPitch / 8);
file.writePixels(height);
}
else
{
const uint64_t bytes = image.width * image.height;
if (bytes > static_cast<uint64_t>(UINT32_MAX))
{
return /* HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW) */ static_cast<HRESULT>(0x80070216L);
}
std::unique_ptr<XMHALF4> temp(new (std::nothrow) XMHALF4[static_cast<size_t>(bytes)]);
if (!temp)
return E_OUTOFMEMORY;
file.setFrameBuffer(reinterpret_cast<const Imf::Rgba*>(temp.get()), 1, image.width);
auto sPtr = image.pixels;
auto dPtr = temp.get();
if (image.format == DXGI_FORMAT_R32G32B32A32_FLOAT)
{
for (int j = 0; j < height; ++j)
{
auto srcPtr = reinterpret_cast<const XMFLOAT4*>(sPtr);
auto destPtr = dPtr;
for (int k = 0; k < width; ++k, ++srcPtr, ++destPtr)
{
const XMVECTOR v = XMLoadFloat4(srcPtr);
PackedVector::XMStoreHalf4(destPtr, v);
}
sPtr += image.rowPitch;
dPtr += width;
file.writePixels(1);
}
}
else
{
assert(image.format == DXGI_FORMAT_R32G32B32_FLOAT);
for (int j = 0; j < height; ++j)
{
auto srcPtr = reinterpret_cast<const XMFLOAT3*>(sPtr);
auto destPtr = dPtr;
for (int k = 0; k < width; ++k, ++srcPtr, ++destPtr)
{
XMVECTOR v = XMLoadFloat3(srcPtr);
v = XMVectorSelect(g_XMIdentityR3, v, g_XMSelect1110);
PackedVector::XMStoreHalf4(destPtr, v);
}
sPtr += image.rowPitch;
dPtr += width;
file.writePixels(1);
}
}
}
}
#ifdef _WIN32
catch (const com_exception& exc)
{
#ifdef _DEBUG
OutputDebugStringA(exc.what());
#endif
hr = exc.get_result();
}
#endif
#if defined(_WIN32) && defined(_DEBUG)
catch (const std::exception& exc)
{
OutputDebugStringA(exc.what());
hr = E_FAIL;
}
#else
catch (const std::exception&)
{
hr = E_FAIL;
}
#endif
catch (...)
{
hr = E_UNEXPECTED;
}
if (FAILED(hr))
return hr;
#ifdef _WIN32
delonfail.clear();
#endif
return S_OK;
}