Rise
The Vieneo Province
Loading...
Searching...
No Matches
DDSTextureLoader.cpp
Go to the documentation of this file.
1//--------------------------------------------------------------------------------------
2// File: DDSTextureLoader.cpp
3//
4// Functions for loading a DDS texture and creating a Direct3D runtime resource for it
5//
6// Note these functions are useful as a light-weight runtime loader for DDS files. For
7// a full-featured DDS file reader, writer, and texture processing pipeline see
8// the 'Texconv' sample and the 'DirectXTex' library.
9//
10// Copyright (c) Microsoft Corporation.
11// Licensed under the MIT License.
12//
13// http://go.microsoft.com/fwlink/?LinkId=248926
14// https://go.microsoft.com/fwlink/?LinkId=248929
15//--------------------------------------------------------------------------------------
16
17#include "pch.h"
18
19#include "DDSTextureLoader.h"
20
21#include "PlatformHelpers.h"
22#include "DDS.h"
23#include "DirectXHelpers.h"
24#include "LoaderHelpers.h"
25
26using namespace DirectX;
27using namespace DirectX::LoaderHelpers;
28
29static_assert(static_cast<int>(DDS_DIMENSION_TEXTURE1D) == static_cast<int>(D3D11_RESOURCE_DIMENSION_TEXTURE1D), "dds mismatch");
30static_assert(static_cast<int>(DDS_DIMENSION_TEXTURE2D) == static_cast<int>(D3D11_RESOURCE_DIMENSION_TEXTURE2D), "dds mismatch");
31static_assert(static_cast<int>(DDS_DIMENSION_TEXTURE3D) == static_cast<int>(D3D11_RESOURCE_DIMENSION_TEXTURE3D), "dds mismatch");
32static_assert(static_cast<int>(DDS_RESOURCE_MISC_TEXTURECUBE) == static_cast<int>(D3D11_RESOURCE_MISC_TEXTURECUBE), "dds mismatch");
33
34namespace
35{
36 //--------------------------------------------------------------------------------------
37 HRESULT FillInitData(_In_ size_t width,
38 _In_ size_t height,
39 _In_ size_t depth,
40 _In_ size_t mipCount,
41 _In_ size_t arraySize,
42 _In_ DXGI_FORMAT format,
43 _In_ size_t maxsize,
44 _In_ size_t bitSize,
45 _In_reads_bytes_(bitSize) const uint8_t* bitData,
46 _Out_ size_t& twidth,
47 _Out_ size_t& theight,
48 _Out_ size_t& tdepth,
49 _Out_ size_t& skipMip,
50 _Out_writes_(mipCount* arraySize) D3D11_SUBRESOURCE_DATA* initData) noexcept
51 {
52 if (!bitData || !initData)
53 {
54 return E_POINTER;
55 }
56
57 skipMip = 0;
58 twidth = 0;
59 theight = 0;
60 tdepth = 0;
61
62 size_t NumBytes = 0;
63 size_t RowBytes = 0;
64 const uint8_t* pSrcBits = bitData;
65 const uint8_t* pEndBits = bitData + bitSize;
66
67 size_t index = 0;
68 for (size_t j = 0; j < arraySize; j++)
69 {
70 size_t w = width;
71 size_t h = height;
72 size_t d = depth;
73 for (size_t i = 0; i < mipCount; i++)
74 {
75 HRESULT hr = GetSurfaceInfo(w, h, format, &NumBytes, &RowBytes, nullptr);
76 if (FAILED(hr))
77 return hr;
78
79 if (NumBytes > UINT32_MAX || RowBytes > UINT32_MAX)
80 return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
81
82 if ((mipCount <= 1) || !maxsize || (w <= maxsize && h <= maxsize && d <= maxsize))
83 {
84 if (!twidth)
85 {
86 twidth = w;
87 theight = h;
88 tdepth = d;
89 }
90
91 assert(index < mipCount * arraySize);
92 _Analysis_assume_(index < mipCount * arraySize);
93 initData[index].pSysMem = pSrcBits;
94 initData[index].SysMemPitch = static_cast<UINT>(RowBytes);
95 initData[index].SysMemSlicePitch = static_cast<UINT>(NumBytes);
96 ++index;
97 }
98 else if (!j)
99 {
100 // Count number of skipped mipmaps (first item only)
101 ++skipMip;
102 }
103
104 if (pSrcBits + (NumBytes * d) > pEndBits)
105 {
106 return HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
107 }
108
109 pSrcBits += NumBytes * d;
110
111 w = w >> 1;
112 h = h >> 1;
113 d = d >> 1;
114 if (w == 0)
115 {
116 w = 1;
117 }
118 if (h == 0)
119 {
120 h = 1;
121 }
122 if (d == 0)
123 {
124 d = 1;
125 }
126 }
127 }
128
129 return (index > 0) ? S_OK : E_FAIL;
130 }
131
132 //--------------------------------------------------------------------------------------
133 HRESULT CreateD3DResources(_In_ ID3D11Device* d3dDevice,
134 _In_ uint32_t resDim,
135 _In_ size_t width,
136 _In_ size_t height,
137 _In_ size_t depth,
138 _In_ size_t mipCount,
139 _In_ size_t arraySize,
140 _In_ DXGI_FORMAT format,
141 _In_ D3D11_USAGE usage,
142 _In_ unsigned int bindFlags,
143 _In_ unsigned int cpuAccessFlags,
144 _In_ unsigned int miscFlags,
145 _In_ DDS_LOADER_FLAGS loadFlags,
146 _In_ bool isCubeMap,
147 _In_reads_opt_(mipCount* arraySize) const D3D11_SUBRESOURCE_DATA* initData,
148 _Outptr_opt_ ID3D11Resource** texture,
149 _Outptr_opt_ ID3D11ShaderResourceView** textureView) noexcept
150 {
151 if (!d3dDevice)
152 return E_POINTER;
153
154 HRESULT hr = E_FAIL;
155
156 if (loadFlags & DDS_LOADER_FORCE_SRGB)
157 {
158 format = MakeSRGB(format);
159 }
160 else if (loadFlags & DDS_LOADER_IGNORE_SRGB)
161 {
162 format = MakeLinear(format);
163 }
164
165 switch (resDim)
166 {
167 case D3D11_RESOURCE_DIMENSION_TEXTURE1D: {
168 D3D11_TEXTURE1D_DESC desc = {};
169 desc.Width = static_cast<UINT>(width);
170 desc.MipLevels = static_cast<UINT>(mipCount);
171 desc.ArraySize = static_cast<UINT>(arraySize);
172 desc.Format = format;
173 desc.Usage = usage;
174 desc.BindFlags = bindFlags;
175 desc.CPUAccessFlags = cpuAccessFlags;
176 desc.MiscFlags = miscFlags & ~static_cast<unsigned int>(D3D11_RESOURCE_MISC_TEXTURECUBE);
177
178 ID3D11Texture1D* tex = nullptr;
179 hr = d3dDevice->CreateTexture1D(&desc, initData, &tex);
180 if (SUCCEEDED(hr) && tex)
181 {
182 if (textureView)
183 {
184 D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc = {};
185 SRVDesc.Format = format;
186
187 if (arraySize > 1)
188 {
189 SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1DARRAY;
190 SRVDesc.Texture1DArray.MipLevels = (!mipCount) ? UINT(-1) : desc.MipLevels;
191 SRVDesc.Texture1DArray.ArraySize = static_cast<UINT>(arraySize);
192 }
193 else
194 {
195 SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1D;
196 SRVDesc.Texture1D.MipLevels = (!mipCount) ? UINT(-1) : desc.MipLevels;
197 }
198
199 hr = d3dDevice->CreateShaderResourceView(tex, &SRVDesc, textureView);
200 if (FAILED(hr))
201 {
202 tex->Release();
203 return hr;
204 }
205 }
206
207 if (texture)
208 {
209 *texture = tex;
210 }
211 else
212 {
213 SetDebugObjectName(tex, "DDSTextureLoader");
214 tex->Release();
215 }
216 }
217 }
218 break;
219
220 case D3D11_RESOURCE_DIMENSION_TEXTURE2D: {
221 D3D11_TEXTURE2D_DESC desc = {};
222 desc.Width = static_cast<UINT>(width);
223 desc.Height = static_cast<UINT>(height);
224 desc.MipLevels = static_cast<UINT>(mipCount);
225 desc.ArraySize = static_cast<UINT>(arraySize);
226 desc.Format = format;
227 desc.SampleDesc.Count = 1;
228 desc.SampleDesc.Quality = 0;
229 desc.Usage = usage;
230 desc.BindFlags = bindFlags;
231 desc.CPUAccessFlags = cpuAccessFlags;
232 if (isCubeMap)
233 {
234 desc.MiscFlags = miscFlags | D3D11_RESOURCE_MISC_TEXTURECUBE;
235 }
236 else
237 {
238 desc.MiscFlags = miscFlags & ~static_cast<unsigned int>(D3D11_RESOURCE_MISC_TEXTURECUBE);
239 }
240
241 ID3D11Texture2D* tex = nullptr;
242 hr = d3dDevice->CreateTexture2D(&desc, initData, &tex);
243 if (SUCCEEDED(hr) && tex)
244 {
245 if (textureView)
246 {
247 D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc = {};
248 SRVDesc.Format = format;
249
250 if (isCubeMap)
251 {
252 if (arraySize > 6)
253 {
254 SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBEARRAY;
255 SRVDesc.TextureCubeArray.MipLevels = (!mipCount) ? UINT(-1) : desc.MipLevels;
256
257 // Earlier we set arraySize to (NumCubes * 6)
258 SRVDesc.TextureCubeArray.NumCubes = static_cast<UINT>(arraySize / 6);
259 }
260 else
261 {
262 SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
263 SRVDesc.TextureCube.MipLevels = (!mipCount) ? UINT(-1) : desc.MipLevels;
264 }
265 }
266 else if (arraySize > 1)
267 {
268 SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY;
269 SRVDesc.Texture2DArray.MipLevels = (!mipCount) ? UINT(-1) : desc.MipLevels;
270 SRVDesc.Texture2DArray.ArraySize = static_cast<UINT>(arraySize);
271 }
272 else
273 {
274 SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
275 SRVDesc.Texture2D.MipLevels = (!mipCount) ? UINT(-1) : desc.MipLevels;
276 }
277
278 hr = d3dDevice->CreateShaderResourceView(tex, &SRVDesc, textureView);
279 if (FAILED(hr))
280 {
281 tex->Release();
282 return hr;
283 }
284 }
285
286 if (texture)
287 {
288 *texture = tex;
289 }
290 else
291 {
292 SetDebugObjectName(tex, "DDSTextureLoader");
293 tex->Release();
294 }
295 }
296 }
297 break;
298
299 case D3D11_RESOURCE_DIMENSION_TEXTURE3D: {
300 D3D11_TEXTURE3D_DESC desc = {};
301 desc.Width = static_cast<UINT>(width);
302 desc.Height = static_cast<UINT>(height);
303 desc.Depth = static_cast<UINT>(depth);
304 desc.MipLevels = static_cast<UINT>(mipCount);
305 desc.Format = format;
306 desc.Usage = usage;
307 desc.BindFlags = bindFlags;
308 desc.CPUAccessFlags = cpuAccessFlags;
309 desc.MiscFlags = miscFlags & ~UINT(D3D11_RESOURCE_MISC_TEXTURECUBE);
310
311 ID3D11Texture3D* tex = nullptr;
312 hr = d3dDevice->CreateTexture3D(&desc, initData, &tex);
313 if (SUCCEEDED(hr) && tex)
314 {
315 if (textureView)
316 {
317 D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc = {};
318 SRVDesc.Format = format;
319
320 SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
321 SRVDesc.Texture3D.MipLevels = (!mipCount) ? UINT(-1) : desc.MipLevels;
322
323 hr = d3dDevice->CreateShaderResourceView(tex, &SRVDesc, textureView);
324 if (FAILED(hr))
325 {
326 tex->Release();
327 return hr;
328 }
329 }
330
331 if (texture)
332 {
333 *texture = tex;
334 }
335 else
336 {
337 SetDebugObjectName(tex, "DDSTextureLoader");
338 tex->Release();
339 }
340 }
341 }
342 break;
343
344 default: return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
345 }
346
347 return hr;
348 }
349
350 //--------------------------------------------------------------------------------------
351 HRESULT CreateTextureFromDDS(_In_ ID3D11Device* d3dDevice,
352 _In_opt_ ID3D11DeviceContext* d3dContext,
353#if defined(_XBOX_ONE) && defined(_TITLE)
354 _In_opt_ ID3D11DeviceX* d3dDeviceX,
355 _In_opt_ ID3D11DeviceContextX* d3dContextX,
356#endif
357 _In_ const DDS_HEADER* header,
358 _In_reads_bytes_(bitSize) const uint8_t* bitData,
359 _In_ size_t bitSize,
360 _In_ size_t maxsize,
361 _In_ D3D11_USAGE usage,
362 _In_ unsigned int bindFlags,
363 _In_ unsigned int cpuAccessFlags,
364 _In_ unsigned int miscFlags,
365 _In_ DDS_LOADER_FLAGS loadFlags,
366 _Outptr_opt_ ID3D11Resource** texture,
367 _Outptr_opt_ ID3D11ShaderResourceView** textureView) noexcept
368 {
369 HRESULT hr = S_OK;
370
371 const UINT width = header->width;
372 UINT height = header->height;
373 UINT depth = header->depth;
374
375 uint32_t resDim = D3D11_RESOURCE_DIMENSION_UNKNOWN;
376 UINT arraySize = 1;
377 DXGI_FORMAT format = DXGI_FORMAT_UNKNOWN;
378 bool isCubeMap = false;
379
380 size_t mipCount = header->mipMapCount;
381 if ((0 == mipCount) || (loadFlags & DDS_LOADER_IGNORE_MIPS))
382 {
383 mipCount = 1;
384 }
385
386 if ((header->ddspf.flags & DDS_FOURCC) && (MAKEFOURCC('D', 'X', '1', '0') == header->ddspf.fourCC))
387 {
388 auto d3d10ext = reinterpret_cast<const DDS_HEADER_DXT10*>(reinterpret_cast<const char*>(header) + sizeof(DDS_HEADER));
389
390 arraySize = d3d10ext->arraySize;
391 if (arraySize == 0)
392 {
393 return HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
394 }
395
396 switch (d3d10ext->dxgiFormat)
397 {
398 case DXGI_FORMAT_NV12:
399 case DXGI_FORMAT_P010:
400 case DXGI_FORMAT_P016:
401 case DXGI_FORMAT_420_OPAQUE:
402 if ((d3d10ext->resourceDimension != D3D11_RESOURCE_DIMENSION_TEXTURE2D) || (width % 2) != 0 || (height % 2) != 0)
403 {
404 DebugTrace("ERROR: Video texture does not meet width/height requirements.\n");
405 return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
406 }
407 break;
408
409 case DXGI_FORMAT_YUY2:
410 case DXGI_FORMAT_Y210:
411 case DXGI_FORMAT_Y216:
412 case DXGI_FORMAT_P208:
413 if ((width % 2) != 0)
414 {
415 DebugTrace("ERROR: Video texture does not meet width requirements.\n");
416 return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
417 }
418 break;
419
420 case DXGI_FORMAT_NV11:
421 if ((width % 4) != 0)
422 {
423 DebugTrace("ERROR: Video texture does not meet width requirements.\n");
424 return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
425 }
426 break;
427
428 case DXGI_FORMAT_AI44:
429 case DXGI_FORMAT_IA44:
430 case DXGI_FORMAT_P8:
431 case DXGI_FORMAT_A8P8:
432 DebugTrace("ERROR: Legacy stream video texture formats are not supported by Direct3D.\n");
433 return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
434
435 default:
436 if (BitsPerPixel(d3d10ext->dxgiFormat) == 0)
437 {
438 DebugTrace("ERROR: Unknown DXGI format (%u)\n", static_cast<uint32_t>(d3d10ext->dxgiFormat));
439 return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
440 }
441 }
442
443 format = d3d10ext->dxgiFormat;
444
445 switch (d3d10ext->resourceDimension)
446 {
447 case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
448 // D3DX writes 1D textures with a fixed Height of 1
449 if ((header->flags & DDS_HEIGHT) && height != 1)
450 {
451 return HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
452 }
453 height = depth = 1;
454 break;
455
456 case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
457 if (d3d10ext->miscFlag & D3D11_RESOURCE_MISC_TEXTURECUBE)
458 {
459 arraySize *= 6;
460 isCubeMap = true;
461 }
462 depth = 1;
463 break;
464
465 case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
466 if (!(header->flags & DDS_HEADER_FLAGS_VOLUME))
467 {
468 return HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
469 }
470
471 if (arraySize > 1)
472 {
473 DebugTrace("ERROR: Volume textures are not texture arrays\n");
474 return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
475 }
476 break;
477
478 case D3D11_RESOURCE_DIMENSION_BUFFER:
479 DebugTrace("ERROR: Resource dimension buffer type not supported for textures\n");
480 return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
481
482 case D3D11_RESOURCE_DIMENSION_UNKNOWN:
483 default:
484 DebugTrace("ERROR: Unknown resource dimension (%u)\n", static_cast<uint32_t>(d3d10ext->resourceDimension));
485 return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
486 }
487
488 resDim = d3d10ext->resourceDimension;
489 }
490 else
491 {
492 format = GetDXGIFormat(header->ddspf);
493
494 if (format == DXGI_FORMAT_UNKNOWN)
495 {
496 DebugTrace("ERROR: DDSTextureLoader does not support all legacy DDS formats. Consider using DirectXTex.\n");
497 return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
498 }
499
500 if (header->flags & DDS_HEADER_FLAGS_VOLUME)
501 {
502 resDim = D3D11_RESOURCE_DIMENSION_TEXTURE3D;
503 }
504 else
505 {
506 if (header->caps2 & DDS_CUBEMAP)
507 {
508 // We require all six faces to be defined
509 if ((header->caps2 & DDS_CUBEMAP_ALLFACES) != DDS_CUBEMAP_ALLFACES)
510 {
511 DebugTrace("ERROR: DirectX 11 does not support partial cubemaps\n");
512 return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
513 }
514
515 arraySize = 6;
516 isCubeMap = true;
517 }
518
519 depth = 1;
520 resDim = D3D11_RESOURCE_DIMENSION_TEXTURE2D;
521
522 // Note there's no way for a legacy Direct3D 9 DDS to express a '1D' texture
523 }
524
525 assert(BitsPerPixel(format) != 0);
526 }
527
528 if ((miscFlags & D3D11_RESOURCE_MISC_TEXTURECUBE) && (resDim == D3D11_RESOURCE_DIMENSION_TEXTURE2D) && ((arraySize % 6) == 0))
529 {
530 isCubeMap = true;
531 }
532
533 // Bound sizes (for security purposes we don't trust DDS file metadata larger than the Direct3D hardware requirements)
534 if (mipCount > D3D11_REQ_MIP_LEVELS)
535 {
536 DebugTrace("ERROR: Too many mipmap levels defined for DirectX 11 (%zu).\n", mipCount);
537 return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
538 }
539
540 switch (resDim)
541 {
542 case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
543 if ((arraySize > D3D11_REQ_TEXTURE1D_ARRAY_AXIS_DIMENSION) || (width > D3D11_REQ_TEXTURE1D_U_DIMENSION))
544 {
545 DebugTrace("ERROR: Resource dimensions too large for DirectX 11 (1D: array %u, size %u)\n", arraySize, width);
546 return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
547 }
548 break;
549
550 case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
551 if (isCubeMap)
552 {
553 // This is the right bound because we set arraySize to (NumCubes*6) above
554 if ((arraySize > D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION) || (width > D3D11_REQ_TEXTURECUBE_DIMENSION)
555 || (height > D3D11_REQ_TEXTURECUBE_DIMENSION))
556 {
557 DebugTrace("ERROR: Resource dimensions too large for DirectX 11 (2D cubemap: array %u, size %u by %u)\n",
558 arraySize,
559 width,
560 height);
561 return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
562 }
563 }
564 else if ((arraySize > D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION) || (width > D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION)
565 || (height > D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION))
566 {
567 DebugTrace("ERROR: Resource dimensions too large for DirectX 11 (2D: array %u, size %u by %u)\n", arraySize, width, height);
568 return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
569 }
570 break;
571
572 case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
573 if ((arraySize > 1) || (width > D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION) || (height > D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION)
574 || (depth > D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION))
575 {
576 DebugTrace("ERROR: Resource dimensions too large for DirectX 11 (3D: array %u, size %u by %u by %u)\n",
577 arraySize,
578 width,
579 height,
580 depth);
581 return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
582 }
583 break;
584
585 case D3D11_RESOURCE_DIMENSION_BUFFER:
586 DebugTrace("ERROR: Resource dimension buffer type not supported for textures\n");
587 return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
588
589 case D3D11_RESOURCE_DIMENSION_UNKNOWN:
590 default:
591 DebugTrace("ERROR: Unknown resource dimension (%u)\n", static_cast<uint32_t>(resDim));
592 return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
593 }
594
595 bool autogen = false;
596 if (mipCount == 1 && d3dContext && textureView) // Must have context and shader-view to auto generate mipmaps
597 {
598 // See if format is supported for auto-gen mipmaps (varies by feature level)
599 UINT fmtSupport = 0;
600 hr = d3dDevice->CheckFormatSupport(format, &fmtSupport);
601 if (SUCCEEDED(hr) && (fmtSupport & D3D11_FORMAT_SUPPORT_MIP_AUTOGEN))
602 {
603 // 10level9 feature levels do not support auto-gen mipgen for volume textures
604 if ((resDim != D3D11_RESOURCE_DIMENSION_TEXTURE3D) || (d3dDevice->GetFeatureLevel() >= D3D_FEATURE_LEVEL_10_0))
605 {
606 autogen = true;
607#if defined(_XBOX_ONE) && defined(_TITLE)
608 if (!d3dDeviceX || !d3dContextX)
609 return E_INVALIDARG;
610#endif
611 }
612 }
613 }
614
615 if (autogen)
616 {
617 // Create texture with auto-generated mipmaps
618 ID3D11Resource* tex = nullptr;
619 hr = CreateD3DResources(d3dDevice,
620 resDim,
621 width,
622 height,
623 depth,
624 0,
625 arraySize,
626 format,
627 usage,
628 bindFlags | D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET,
629 cpuAccessFlags,
630 miscFlags | D3D11_RESOURCE_MISC_GENERATE_MIPS,
631 loadFlags,
632 isCubeMap,
633 nullptr,
634 &tex,
635 textureView);
636 if (SUCCEEDED(hr))
637 {
638 size_t numBytes = 0;
639 size_t rowBytes = 0;
640 hr = GetSurfaceInfo(width, height, format, &numBytes, &rowBytes, nullptr);
641 if (FAILED(hr))
642 return hr;
643
644 if (numBytes > bitSize)
645 {
646 (*textureView)->Release();
647 *textureView = nullptr;
648 tex->Release();
649 return HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
650 }
651
652 if (numBytes > UINT32_MAX || rowBytes > UINT32_MAX)
653 return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
654
655 D3D11_SHADER_RESOURCE_VIEW_DESC desc = {};
656 (*textureView)->GetDesc(&desc);
657
658 UINT mipLevels = 1;
659
660 switch (desc.ViewDimension)
661 {
662 case D3D_SRV_DIMENSION_TEXTURE1D: mipLevels = desc.Texture1D.MipLevels; break;
663 case D3D_SRV_DIMENSION_TEXTURE1DARRAY: mipLevels = desc.Texture1DArray.MipLevels; break;
664 case D3D_SRV_DIMENSION_TEXTURE2D: mipLevels = desc.Texture2D.MipLevels; break;
665 case D3D_SRV_DIMENSION_TEXTURE2DARRAY: mipLevels = desc.Texture2DArray.MipLevels; break;
666 case D3D_SRV_DIMENSION_TEXTURECUBE: mipLevels = desc.TextureCube.MipLevels; break;
667 case D3D_SRV_DIMENSION_TEXTURECUBEARRAY: mipLevels = desc.TextureCubeArray.MipLevels; break;
668 case D3D_SRV_DIMENSION_TEXTURE3D: mipLevels = desc.Texture3D.MipLevels; break;
669 default:
670 (*textureView)->Release();
671 *textureView = nullptr;
672 tex->Release();
673 return E_UNEXPECTED;
674 }
675
676#if defined(_XBOX_ONE) && defined(_TITLE)
677
678 std::unique_ptr<D3D11_SUBRESOURCE_DATA[]> initData(new (std::nothrow) D3D11_SUBRESOURCE_DATA[arraySize]);
679 if (!initData)
680 {
681 return E_OUTOFMEMORY;
682 }
683
684 const uint8_t* pSrcBits = bitData;
685 const uint8_t* pEndBits = bitData + bitSize;
686 for (UINT item = 0; item < arraySize; ++item)
687 {
688 if ((pSrcBits + numBytes) > pEndBits)
689 {
690 (*textureView)->Release();
691 *textureView = nullptr;
692 tex->Release();
693 return HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
694 }
695
696 initData[item].pSysMem = pSrcBits;
697 initData[item].SysMemPitch = static_cast<UINT>(rowBytes);
698 initData[item].SysMemSlicePitch = static_cast<UINT>(numBytes);
699 pSrcBits += numBytes;
700 }
701
702 ID3D11Resource* pStaging = nullptr;
703 switch (resDim)
704 {
705 case D3D11_RESOURCE_DIMENSION_TEXTURE1D: {
706 ID3D11Texture1D* temp = nullptr;
707 CD3D11_TEXTURE1D_DESC stagingDesc(format, width, arraySize, 1, 0, D3D11_USAGE_STAGING, D3D11_CPU_ACCESS_READ);
708 hr = d3dDevice->CreateTexture1D(&stagingDesc, initData.get(), &temp);
709 if (SUCCEEDED(hr))
710 pStaging = temp;
711 }
712 break;
713
714 case D3D11_RESOURCE_DIMENSION_TEXTURE2D: {
715 ID3D11Texture2D* temp = nullptr;
716 CD3D11_TEXTURE2D_DESC stagingDesc(format,
717 width,
718 height,
719 arraySize,
720 1,
721 0,
722 D3D11_USAGE_STAGING,
723 D3D11_CPU_ACCESS_READ,
724 1,
725 0,
726 isCubeMap ? D3D11_RESOURCE_MISC_TEXTURECUBE : 0);
727 hr = d3dDevice->CreateTexture2D(&stagingDesc, initData.get(), &temp);
728 if (SUCCEEDED(hr))
729 pStaging = temp;
730 }
731 break;
732
733 case D3D11_RESOURCE_DIMENSION_TEXTURE3D: {
734 ID3D11Texture3D* temp = nullptr;
735 CD3D11_TEXTURE3D_DESC stagingDesc(format, width, height, depth, 1, 0, D3D11_USAGE_STAGING, D3D11_CPU_ACCESS_READ);
736 hr = d3dDevice->CreateTexture3D(&stagingDesc, initData.get(), &temp);
737 if (SUCCEEDED(hr))
738 pStaging = temp;
739 }
740 break;
741 };
742
743 if (SUCCEEDED(hr))
744 {
745 for (UINT item = 0; item < arraySize; ++item)
746 {
747 UINT res = D3D11CalcSubresource(0, item, mipLevels);
748 d3dContext->CopySubresourceRegion(tex, res, 0, 0, 0, pStaging, item, nullptr);
749 }
750
751 UINT64 copyFence = d3dContextX->InsertFence(0);
752 while (d3dDeviceX->IsFencePending(copyFence))
753 {
754 SwitchToThread();
755 }
756 pStaging->Release();
757 }
758#else
759 if (arraySize > 1)
760 {
761 const uint8_t* pSrcBits = bitData;
762 const uint8_t* pEndBits = bitData + bitSize;
763 for (UINT item = 0; item < arraySize; ++item)
764 {
765 if ((pSrcBits + numBytes) > pEndBits)
766 {
767 (*textureView)->Release();
768 *textureView = nullptr;
769 tex->Release();
770 return HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
771 }
772
773 const UINT res = D3D11CalcSubresource(0, item, mipLevels);
774 d3dContext
775 ->UpdateSubresource(tex, res, nullptr, pSrcBits, static_cast<UINT>(rowBytes), static_cast<UINT>(numBytes));
776 pSrcBits += numBytes;
777 }
778 }
779 else
780 {
781 d3dContext->UpdateSubresource(tex, 0, nullptr, bitData, static_cast<UINT>(rowBytes), static_cast<UINT>(numBytes));
782 }
783#endif
784
785 d3dContext->GenerateMips(*textureView);
786
787 if (texture)
788 {
789 *texture = tex;
790 }
791 else
792 {
793 tex->Release();
794 }
795 }
796 }
797 else
798 {
799 // Create the texture
800 std::unique_ptr<D3D11_SUBRESOURCE_DATA[]> initData(new (std::nothrow) D3D11_SUBRESOURCE_DATA[mipCount * arraySize]);
801 if (!initData)
802 {
803 return E_OUTOFMEMORY;
804 }
805
806 size_t skipMip = 0;
807 size_t twidth = 0;
808 size_t theight = 0;
809 size_t tdepth = 0;
810 hr = FillInitData(width,
811 height,
812 depth,
813 mipCount,
814 arraySize,
815 format,
816 maxsize,
817 bitSize,
818 bitData,
819 twidth,
820 theight,
821 tdepth,
822 skipMip,
823 initData.get());
824
825 if (SUCCEEDED(hr))
826 {
827 hr = CreateD3DResources(d3dDevice,
828 resDim,
829 twidth,
830 theight,
831 tdepth,
832 mipCount - skipMip,
833 arraySize,
834 format,
835 usage,
836 bindFlags,
837 cpuAccessFlags,
838 miscFlags,
839 loadFlags,
840 isCubeMap,
841 initData.get(),
842 texture,
843 textureView);
844
845 if (FAILED(hr) && !maxsize && (mipCount > 1))
846 {
847 // Retry with a maxsize determined by feature level
848 switch (d3dDevice->GetFeatureLevel())
849 {
850 case D3D_FEATURE_LEVEL_9_1:
851 case D3D_FEATURE_LEVEL_9_2:
852 case D3D_FEATURE_LEVEL_9_3: return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
853
854 case D3D_FEATURE_LEVEL_10_0:
855 case D3D_FEATURE_LEVEL_10_1:
856 maxsize = (resDim == D3D11_RESOURCE_DIMENSION_TEXTURE3D) ? 2048u /*D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION*/
857 :
858 8192u /*D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION*/;
859 break;
860
861 default: // D3D_FEATURE_LEVEL_11_0
862 maxsize = (resDim == D3D11_RESOURCE_DIMENSION_TEXTURE3D) ? D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION :
863 D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION;
864 break;
865 }
866
867 hr = FillInitData(width,
868 height,
869 depth,
870 mipCount,
871 arraySize,
872 format,
873 maxsize,
874 bitSize,
875 bitData,
876 twidth,
877 theight,
878 tdepth,
879 skipMip,
880 initData.get());
881 if (SUCCEEDED(hr))
882 {
883 hr = CreateD3DResources(d3dDevice,
884 resDim,
885 twidth,
886 theight,
887 tdepth,
888 mipCount - skipMip,
889 arraySize,
890 format,
891 usage,
892 bindFlags,
893 cpuAccessFlags,
894 miscFlags,
895 loadFlags,
896 isCubeMap,
897 initData.get(),
898 texture,
899 textureView);
900 }
901 }
902 }
903 }
904
905 return hr;
906 }
907
908 //--------------------------------------------------------------------------------------
909 void SetDebugTextureInfo(_In_z_ const wchar_t* fileName,
910 _In_opt_ ID3D11Resource** texture,
911 _In_opt_ ID3D11ShaderResourceView** textureView) noexcept
912 {
913#if !defined(NO_D3D11_DEBUG_NAME) && (defined(_DEBUG) || defined(PROFILE))
914 if (texture || textureView)
915 {
916#if defined(_XBOX_ONE) && defined(_TITLE)
917 if (texture && *texture)
918 {
919 (*texture)->SetName(fileName);
920 }
921 if (textureView && *textureView)
922 {
923 (*textureView)->SetName(fileName);
924 }
925#else
926 CHAR strFileA[MAX_PATH];
927 const int result = WideCharToMultiByte(CP_UTF8, WC_NO_BEST_FIT_CHARS, fileName, -1, strFileA, MAX_PATH, nullptr, nullptr);
928 if (result > 0)
929 {
930 if (texture && *texture)
931 {
932 (*texture)->SetPrivateData(WKPDID_D3DDebugObjectName, static_cast<UINT>(result), strFileA);
933 }
934
935 if (textureView && *textureView)
936 {
937 (*textureView)->SetPrivateData(WKPDID_D3DDebugObjectName, static_cast<UINT>(result), strFileA);
938 }
939 }
940#endif
941 }
942#else
943 UNREFERENCED_PARAMETER(fileName);
944 UNREFERENCED_PARAMETER(texture);
945 UNREFERENCED_PARAMETER(textureView);
946#endif
947 }
948} // anonymous namespace
949
950//--------------------------------------------------------------------------------------
951_Use_decl_annotations_ HRESULT DirectX::CreateDDSTextureFromMemory(ID3D11Device* d3dDevice,
952 const uint8_t* ddsData,
953 size_t ddsDataSize,
954 ID3D11Resource** texture,
955 ID3D11ShaderResourceView** textureView,
956 size_t maxsize,
957 DDS_ALPHA_MODE* alphaMode) noexcept
958{
959 return CreateDDSTextureFromMemoryEx(d3dDevice,
960 ddsData,
961 ddsDataSize,
962 maxsize,
963 D3D11_USAGE_DEFAULT,
964 D3D11_BIND_SHADER_RESOURCE,
965 0,
966 0,
968 texture,
969 textureView,
970 alphaMode);
971}
972
973_Use_decl_annotations_
974#if defined(_XBOX_ONE) && defined(_TITLE)
975 HRESULT DirectX::CreateDDSTextureFromMemory(ID3D11DeviceX* d3dDevice,
976 ID3D11DeviceContextX* d3dContext,
977#else
978 HRESULT DirectX::CreateDDSTextureFromMemory(ID3D11Device* d3dDevice,
979 ID3D11DeviceContext* d3dContext,
980#endif
981 const uint8_t* ddsData,
982 size_t ddsDataSize,
983 ID3D11Resource** texture,
984 ID3D11ShaderResourceView** textureView,
985 size_t maxsize,
986 DDS_ALPHA_MODE* alphaMode) noexcept
987{
988 return CreateDDSTextureFromMemoryEx(d3dDevice,
989 d3dContext,
990 ddsData,
991 ddsDataSize,
992 maxsize,
993 D3D11_USAGE_DEFAULT,
994 D3D11_BIND_SHADER_RESOURCE,
995 0,
996 0,
998 texture,
999 textureView,
1000 alphaMode);
1001}
1002
1003_Use_decl_annotations_ HRESULT DirectX::CreateDDSTextureFromMemoryEx(ID3D11Device* d3dDevice,
1004 const uint8_t* ddsData,
1005 size_t ddsDataSize,
1006 size_t maxsize,
1007 D3D11_USAGE usage,
1008 unsigned int bindFlags,
1009 unsigned int cpuAccessFlags,
1010 unsigned int miscFlags,
1011 DDS_LOADER_FLAGS loadFlags,
1012 ID3D11Resource** texture,
1013 ID3D11ShaderResourceView** textureView,
1014 DDS_ALPHA_MODE* alphaMode) noexcept
1015{
1016 if (texture)
1017 {
1018 *texture = nullptr;
1019 }
1020 if (textureView)
1021 {
1022 *textureView = nullptr;
1023 }
1024 if (alphaMode)
1025 {
1026 *alphaMode = DDS_ALPHA_MODE_UNKNOWN;
1027 }
1028
1029 if (!d3dDevice || !ddsData || (!texture && !textureView))
1030 {
1031 return E_INVALIDARG;
1032 }
1033
1034 if (textureView && !(bindFlags & D3D11_BIND_SHADER_RESOURCE))
1035 {
1036 return E_INVALIDARG;
1037 }
1038
1039 // Validate DDS file in memory
1040 const DDS_HEADER* header = nullptr;
1041 const uint8_t* bitData = nullptr;
1042 size_t bitSize = 0;
1043
1044 HRESULT hr = LoadTextureDataFromMemory(ddsData, ddsDataSize, &header, &bitData, &bitSize);
1045 if (FAILED(hr))
1046 {
1047 return hr;
1048 }
1049
1050 hr = CreateTextureFromDDS(d3dDevice,
1051 nullptr,
1052#if defined(_XBOX_ONE) && defined(_TITLE)
1053 nullptr,
1054 nullptr,
1055#endif
1056 header,
1057 bitData,
1058 bitSize,
1059 maxsize,
1060 usage,
1061 bindFlags,
1062 cpuAccessFlags,
1063 miscFlags,
1064 loadFlags,
1065 texture,
1066 textureView);
1067 if (SUCCEEDED(hr))
1068 {
1069 if (texture && *texture)
1070 {
1071 SetDebugObjectName(*texture, "DDSTextureLoader");
1072 }
1073
1074 if (textureView && *textureView)
1075 {
1076 SetDebugObjectName(*textureView, "DDSTextureLoader");
1077 }
1078
1079 if (alphaMode)
1080 *alphaMode = GetAlphaMode(header);
1081 }
1082
1083 return hr;
1084}
1085
1086_Use_decl_annotations_
1087#if defined(_XBOX_ONE) && defined(_TITLE)
1088 HRESULT DirectX::CreateDDSTextureFromMemoryEx(ID3D11DeviceX* d3dDevice,
1089 ID3D11DeviceContextX* d3dContext,
1090#else
1091 HRESULT DirectX::CreateDDSTextureFromMemoryEx(ID3D11Device* d3dDevice,
1092 ID3D11DeviceContext* d3dContext,
1093#endif
1094 const uint8_t* ddsData,
1095 size_t ddsDataSize,
1096 size_t maxsize,
1097 D3D11_USAGE usage,
1098 unsigned int bindFlags,
1099 unsigned int cpuAccessFlags,
1100 unsigned int miscFlags,
1101 DDS_LOADER_FLAGS loadFlags,
1102 ID3D11Resource** texture,
1103 ID3D11ShaderResourceView** textureView,
1104 DDS_ALPHA_MODE* alphaMode) noexcept
1105{
1106 if (texture)
1107 {
1108 *texture = nullptr;
1109 }
1110 if (textureView)
1111 {
1112 *textureView = nullptr;
1113 }
1114 if (alphaMode)
1115 {
1116 *alphaMode = DDS_ALPHA_MODE_UNKNOWN;
1117 }
1118
1119 if (!d3dDevice || !ddsData || (!texture && !textureView))
1120 {
1121 return E_INVALIDARG;
1122 }
1123
1124 if (textureView && !(bindFlags & D3D11_BIND_SHADER_RESOURCE))
1125 {
1126 return E_INVALIDARG;
1127 }
1128
1129 // Validate DDS file in memory
1130 const DDS_HEADER* header = nullptr;
1131 const uint8_t* bitData = nullptr;
1132 size_t bitSize = 0;
1133
1134 HRESULT hr = LoadTextureDataFromMemory(ddsData, ddsDataSize, &header, &bitData, &bitSize);
1135 if (FAILED(hr))
1136 {
1137 return hr;
1138 }
1139
1140 hr = CreateTextureFromDDS(d3dDevice,
1141 d3dContext,
1142#if defined(_XBOX_ONE) && defined(_TITLE)
1143 d3dDevice,
1144 d3dContext,
1145#endif
1146 header,
1147 bitData,
1148 bitSize,
1149 maxsize,
1150 usage,
1151 bindFlags,
1152 cpuAccessFlags,
1153 miscFlags,
1154 loadFlags,
1155 texture,
1156 textureView);
1157 if (SUCCEEDED(hr))
1158 {
1159 if (texture && *texture)
1160 {
1161 SetDebugObjectName(*texture, "DDSTextureLoader");
1162 }
1163
1164 if (textureView && *textureView)
1165 {
1166 SetDebugObjectName(*textureView, "DDSTextureLoader");
1167 }
1168
1169 if (alphaMode)
1170 *alphaMode = GetAlphaMode(header);
1171 }
1172
1173 return hr;
1174}
1175
1176//--------------------------------------------------------------------------------------
1177_Use_decl_annotations_ HRESULT DirectX::CreateDDSTextureFromFile(ID3D11Device* d3dDevice,
1178 const wchar_t* fileName,
1179 ID3D11Resource** texture,
1180 ID3D11ShaderResourceView** textureView,
1181 size_t maxsize,
1182 DDS_ALPHA_MODE* alphaMode) noexcept
1183{
1184 return CreateDDSTextureFromFileEx(d3dDevice,
1185 fileName,
1186 maxsize,
1187 D3D11_USAGE_DEFAULT,
1188 D3D11_BIND_SHADER_RESOURCE,
1189 0,
1190 0,
1192 texture,
1193 textureView,
1194 alphaMode);
1195}
1196
1197_Use_decl_annotations_
1198#if defined(_XBOX_ONE) && defined(_TITLE)
1199 HRESULT DirectX::CreateDDSTextureFromFile(ID3D11DeviceX* d3dDevice,
1200 ID3D11DeviceContextX* d3dContext,
1201#else
1202 HRESULT DirectX::CreateDDSTextureFromFile(ID3D11Device* d3dDevice,
1203 ID3D11DeviceContext* d3dContext,
1204#endif
1205 const wchar_t* fileName,
1206 ID3D11Resource** texture,
1207 ID3D11ShaderResourceView** textureView,
1208 size_t maxsize,
1209 DDS_ALPHA_MODE* alphaMode) noexcept
1210{
1211 return CreateDDSTextureFromFileEx(d3dDevice,
1212 d3dContext,
1213 fileName,
1214 maxsize,
1215 D3D11_USAGE_DEFAULT,
1216 D3D11_BIND_SHADER_RESOURCE,
1217 0,
1218 0,
1220 texture,
1221 textureView,
1222 alphaMode);
1223}
1224
1225_Use_decl_annotations_ HRESULT DirectX::CreateDDSTextureFromFileEx(ID3D11Device* d3dDevice,
1226 const wchar_t* fileName,
1227 size_t maxsize,
1228 D3D11_USAGE usage,
1229 unsigned int bindFlags,
1230 unsigned int cpuAccessFlags,
1231 unsigned int miscFlags,
1232 DDS_LOADER_FLAGS loadFlags,
1233 ID3D11Resource** texture,
1234 ID3D11ShaderResourceView** textureView,
1235 DDS_ALPHA_MODE* alphaMode) noexcept
1236{
1237 if (texture)
1238 {
1239 *texture = nullptr;
1240 }
1241 if (textureView)
1242 {
1243 *textureView = nullptr;
1244 }
1245 if (alphaMode)
1246 {
1247 *alphaMode = DDS_ALPHA_MODE_UNKNOWN;
1248 }
1249
1250 if (!d3dDevice || !fileName || (!texture && !textureView))
1251 {
1252 return E_INVALIDARG;
1253 }
1254
1255 if (textureView && !(bindFlags & D3D11_BIND_SHADER_RESOURCE))
1256 {
1257 return E_INVALIDARG;
1258 }
1259
1260 const DDS_HEADER* header = nullptr;
1261 const uint8_t* bitData = nullptr;
1262 size_t bitSize = 0;
1263
1264 std::unique_ptr<uint8_t[]> ddsData;
1265 HRESULT hr = LoadTextureDataFromFile(fileName, ddsData, &header, &bitData, &bitSize);
1266 if (FAILED(hr))
1267 {
1268 return hr;
1269 }
1270
1271 hr = CreateTextureFromDDS(d3dDevice,
1272 nullptr,
1273#if defined(_XBOX_ONE) && defined(_TITLE)
1274 nullptr,
1275 nullptr,
1276#endif
1277 header,
1278 bitData,
1279 bitSize,
1280 maxsize,
1281 usage,
1282 bindFlags,
1283 cpuAccessFlags,
1284 miscFlags,
1285 loadFlags,
1286 texture,
1287 textureView);
1288
1289 if (SUCCEEDED(hr))
1290 {
1291 SetDebugTextureInfo(fileName, texture, textureView);
1292
1293 if (alphaMode)
1294 *alphaMode = GetAlphaMode(header);
1295 }
1296
1297 return hr;
1298}
1299
1300_Use_decl_annotations_
1301#if defined(_XBOX_ONE) && defined(_TITLE)
1302 HRESULT DirectX::CreateDDSTextureFromFileEx(ID3D11DeviceX* d3dDevice,
1303 ID3D11DeviceContextX* d3dContext,
1304#else
1305 HRESULT DirectX::CreateDDSTextureFromFileEx(ID3D11Device* d3dDevice,
1306 ID3D11DeviceContext* d3dContext,
1307#endif
1308 const wchar_t* fileName,
1309 size_t maxsize,
1310 D3D11_USAGE usage,
1311 unsigned int bindFlags,
1312 unsigned int cpuAccessFlags,
1313 unsigned int miscFlags,
1314 DDS_LOADER_FLAGS loadFlags,
1315 ID3D11Resource** texture,
1316 ID3D11ShaderResourceView** textureView,
1317 DDS_ALPHA_MODE* alphaMode) noexcept
1318{
1319 if (texture)
1320 {
1321 *texture = nullptr;
1322 }
1323 if (textureView)
1324 {
1325 *textureView = nullptr;
1326 }
1327 if (alphaMode)
1328 {
1329 *alphaMode = DDS_ALPHA_MODE_UNKNOWN;
1330 }
1331
1332 if (!d3dDevice || !fileName || (!texture && !textureView))
1333 {
1334 return E_INVALIDARG;
1335 }
1336
1337 if (textureView && !(bindFlags & D3D11_BIND_SHADER_RESOURCE))
1338 {
1339 return E_INVALIDARG;
1340 }
1341
1342 const DDS_HEADER* header = nullptr;
1343 const uint8_t* bitData = nullptr;
1344 size_t bitSize = 0;
1345
1346 std::unique_ptr<uint8_t[]> ddsData;
1347 HRESULT hr = LoadTextureDataFromFile(fileName, ddsData, &header, &bitData, &bitSize);
1348 if (FAILED(hr))
1349 {
1350 return hr;
1351 }
1352
1353 hr = CreateTextureFromDDS(d3dDevice,
1354 d3dContext,
1355#if defined(_XBOX_ONE) && defined(_TITLE)
1356 d3dDevice,
1357 d3dContext,
1358#endif
1359 header,
1360 bitData,
1361 bitSize,
1362 maxsize,
1363 usage,
1364 bindFlags,
1365 cpuAccessFlags,
1366 miscFlags,
1367 loadFlags,
1368 texture,
1369 textureView);
1370
1371 if (SUCCEEDED(hr))
1372 {
1373 SetDebugTextureInfo(fileName, texture, textureView);
1374
1375 if (alphaMode)
1376 *alphaMode = GetAlphaMode(header);
1377 }
1378
1379 return hr;
1380}
1381
1382//--------------------------------------------------------------------------------------
1383// Adapters for /Zc:wchar_t- clients
1384
1385#if defined(_MSC_VER) && !defined(_NATIVE_WCHAR_T_DEFINED)
1386
1387namespace DirectX
1388{
1389 HRESULT __cdecl CreateDDSTextureFromFile(_In_ ID3D11Device* d3dDevice,
1390 _In_z_ const __wchar_t* szFileName,
1391 _Outptr_opt_ ID3D11Resource** texture,
1392 _Outptr_opt_ ID3D11ShaderResourceView** textureView,
1393 _In_ size_t maxsize,
1394 _Out_opt_ DDS_ALPHA_MODE* alphaMode) noexcept
1395 {
1396 return CreateDDSTextureFromFile(d3dDevice,
1397 reinterpret_cast<const unsigned short*>(szFileName),
1398 texture,
1399 textureView,
1400 maxsize,
1401 alphaMode);
1402 }
1403
1405#if defined(_XBOX_ONE) && defined(_TITLE)
1406 _In_ ID3D11DeviceX* d3dDevice,
1407 _In_opt_ ID3D11DeviceContextX* d3dContext,
1408#else
1409 _In_ ID3D11Device* d3dDevice,
1410 _In_opt_ ID3D11DeviceContext* d3dContext,
1411#endif
1412 _In_z_ const __wchar_t* szFileName,
1413 _Outptr_opt_ ID3D11Resource** texture,
1414 _Outptr_opt_ ID3D11ShaderResourceView** textureView,
1415 _In_ size_t maxsize,
1416 _Out_opt_ DDS_ALPHA_MODE* alphaMode) noexcept
1417 {
1418 return CreateDDSTextureFromFile(d3dDevice,
1419 d3dContext,
1420 reinterpret_cast<const unsigned short*>(szFileName),
1421 texture,
1422 textureView,
1423 maxsize,
1424 alphaMode);
1425 }
1426
1427 HRESULT __cdecl CreateDDSTextureFromFileEx(_In_ ID3D11Device* d3dDevice,
1428 _In_z_ const __wchar_t* szFileName,
1429 _In_ size_t maxsize,
1430 _In_ D3D11_USAGE usage,
1431 _In_ unsigned int bindFlags,
1432 _In_ unsigned int cpuAccessFlags,
1433 _In_ unsigned int miscFlags,
1434 _In_ DDS_LOADER_FLAGS loadFlags,
1435 _Outptr_opt_ ID3D11Resource** texture,
1436 _Outptr_opt_ ID3D11ShaderResourceView** textureView,
1437 _Out_opt_ DDS_ALPHA_MODE* alphaMode) noexcept
1438 {
1439 return CreateDDSTextureFromFileEx(d3dDevice,
1440 reinterpret_cast<const unsigned short*>(szFileName),
1441 maxsize,
1442 usage,
1443 bindFlags,
1444 cpuAccessFlags,
1445 miscFlags,
1446 loadFlags,
1447 texture,
1448 textureView,
1449 alphaMode);
1450 }
1451
1453#if defined(_XBOX_ONE) && defined(_TITLE)
1454 _In_ ID3D11DeviceX* d3dDevice,
1455 _In_opt_ ID3D11DeviceContextX* d3dContext,
1456#else
1457 _In_ ID3D11Device* d3dDevice,
1458 _In_opt_ ID3D11DeviceContext* d3dContext,
1459#endif
1460 _In_z_ const __wchar_t* szFileName,
1461 _In_ size_t maxsize,
1462 _In_ D3D11_USAGE usage,
1463 _In_ unsigned int bindFlags,
1464 _In_ unsigned int cpuAccessFlags,
1465 _In_ unsigned int miscFlags,
1466 _In_ DDS_LOADER_FLAGS loadFlags,
1467 _Outptr_opt_ ID3D11Resource** texture,
1468 _Outptr_opt_ ID3D11ShaderResourceView** textureView,
1469 _Out_opt_ DDS_ALPHA_MODE* alphaMode) noexcept
1470 {
1471 return CreateDDSTextureFromFileEx(d3dDevice,
1472 d3dContext,
1473 reinterpret_cast<const unsigned short*>(szFileName),
1474 maxsize,
1475 usage,
1476 bindFlags,
1477 cpuAccessFlags,
1478 miscFlags,
1479 loadFlags,
1480 texture,
1481 textureView,
1482 alphaMode);
1483 }
1484} // namespace DirectX
1485
1486#endif // !_NATIVE_WCHAR_T_DEFINED
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
#define DDS_HEADER_FLAGS_VOLUME
Definition DDS.h:205
#define DDS_HEIGHT
Definition DDS.h:209
#define DDS_FOURCC
Definition DDS.h:42
#define DDS_CUBEMAP
Definition DDS.h:227
#define MAKEFOURCC(ch0, ch1, ch2, ch3)
Definition DDS.h:56
#define DDS_CUBEMAP_ALLFACES
Definition DDS.h:223
HRESULT GetSurfaceInfo(_In_ size_t width, _In_ size_t height, _In_ DXGI_FORMAT fmt, _Out_opt_ size_t *outNumBytes, _Out_opt_ size_t *outRowBytes, _Out_opt_ size_t *outNumRows) noexcept
HRESULT LoadTextureDataFromMemory(_In_reads_(ddsDataSize) const uint8_t *ddsData, size_t ddsDataSize, const DDS_HEADER **header, const uint8_t **bitData, size_t *bitSize) noexcept
DirectX::DDS_ALPHA_MODE GetAlphaMode(_In_ const DDS_HEADER *header) noexcept
DXGI_FORMAT GetDXGIFormat(const DDS_PIXELFORMAT &ddpf) noexcept
DXGI_FORMAT MakeSRGB(_In_ DXGI_FORMAT format) noexcept
DXGI_FORMAT MakeLinear(_In_ DXGI_FORMAT format) noexcept
HRESULT LoadTextureDataFromFile(_In_z_ const wchar_t *fileName, std::unique_ptr< uint8_t[]> &ddsData, const DDS_HEADER **header, const uint8_t **bitData, size_t *bitSize) noexcept
size_t BitsPerPixel(_In_ DXGI_FORMAT fmt) noexcept
Definition DDS.h:24
DIRECTX_TOOLKIT_API HRESULT __cdecl CreateDDSTextureFromFile(_In_ ID3D11Device *d3dDevice, _In_z_ const wchar_t *szFileName, _Outptr_opt_ ID3D11Resource **texture, _Outptr_opt_ ID3D11ShaderResourceView **textureView, _In_ size_t maxsize=0, _Out_opt_ DDS_ALPHA_MODE *alphaMode=nullptr) noexcept
@ DDS_DIMENSION_TEXTURE3D
Definition DDS.h:236
@ DDS_DIMENSION_TEXTURE1D
Definition DDS.h:234
@ DDS_DIMENSION_TEXTURE2D
Definition DDS.h:235
void DebugTrace(_In_z_ _Printf_format_string_ const char *format,...) noexcept
DIRECTX_TOOLKIT_API HRESULT __cdecl CreateDDSTextureFromMemory(_In_ ID3D11Device *d3dDevice, _In_reads_bytes_(ddsDataSize) const uint8_t *ddsData, _In_ size_t ddsDataSize, _Outptr_opt_ ID3D11Resource **texture, _Outptr_opt_ ID3D11ShaderResourceView **textureView, _In_ size_t maxsize=0, _Out_opt_ DDS_ALPHA_MODE *alphaMode=nullptr) noexcept
@ DDS_RESOURCE_MISC_TEXTURECUBE
Definition DDS.h:242
void SetDebugObjectName(_In_ ID3D11DeviceChild *, _In_z_ const char(&)[TNameLength]) noexcept
DIRECTX_TOOLKIT_API HRESULT __cdecl CreateDDSTextureFromFileEx(_In_ ID3D11Device *d3dDevice, _In_z_ const wchar_t *szFileName, _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
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
DDS_ALPHA_MODE
Definition DDS.h:253
@ DDS_ALPHA_MODE_UNKNOWN
Definition DDS.h:254
#define __cdecl