Code refactor and reformat for texture loaders (#137)

This commit is contained in:
Chuck Walbourn 2019-04-17 14:14:01 -07:00 коммит произвёл GitHub
Родитель 86b2bb5194
Коммит 56d86325b6
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 503 добавлений и 302 удалений

Просмотреть файл

@ -134,6 +134,71 @@ namespace
#endif
}
//--------------------------------------------------------------------------------------
HRESULT LoadTextureDataFromMemory(
_In_reads_(ddsDataSize) const uint8_t* ddsData,
size_t ddsDataSize,
const DDS_HEADER** header,
const uint8_t** bitData,
size_t* bitSize)
{
if (!header || !bitData || !bitSize)
{
return E_POINTER;
}
if (ddsDataSize > UINT32_MAX)
{
return E_FAIL;
}
if (ddsDataSize < (sizeof(uint32_t) + sizeof(DDS_HEADER)))
{
return E_FAIL;
}
// DDS files always start with the same magic number ("DDS ")
auto dwMagicNumber = *reinterpret_cast<const uint32_t*>(ddsData);
if (dwMagicNumber != DDS_MAGIC)
{
return E_FAIL;
}
auto hdr = reinterpret_cast<const DDS_HEADER*>(ddsData + sizeof(uint32_t));
// Verify header to validate DDS file
if (hdr->size != sizeof(DDS_HEADER) ||
hdr->ddspf.size != sizeof(DDS_PIXELFORMAT))
{
return E_FAIL;
}
// Check for DX10 extension
bool bDXT10Header = false;
if ((hdr->ddspf.flags & DDS_FOURCC) &&
(MAKEFOURCC('D', 'X', '1', '0') == hdr->ddspf.fourCC))
{
// Must be long enough for both headers and magic value
if (ddsDataSize < (sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10)))
{
return E_FAIL;
}
bDXT10Header = true;
}
// setup the pointers in the process request
*header = hdr;
ptrdiff_t offset = sizeof(uint32_t)
+ sizeof(DDS_HEADER)
+ (bDXT10Header ? sizeof(DDS_HEADER_DXT10) : 0);
*bitData = ddsData + offset;
*bitSize = ddsDataSize - offset;
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT LoadTextureDataFromFile(
_In_z_ const wchar_t* fileName,
@ -183,7 +248,7 @@ namespace
}
// Need at least enough data to fill the header and magic number to be a valid DDS
if (fileInfo.EndOfFile.LowPart < (sizeof(DDS_HEADER) + sizeof(uint32_t)))
if (fileInfo.EndOfFile.LowPart < (sizeof(uint32_t) + sizeof(DDS_HEADER)))
{
return E_FAIL;
}
@ -213,7 +278,7 @@ namespace
}
// DDS files always start with the same magic number ("DDS ")
uint32_t dwMagicNumber = *reinterpret_cast<const uint32_t*>(ddsData.get());
auto dwMagicNumber = *reinterpret_cast<const uint32_t*>(ddsData.get());
if (dwMagicNumber != DDS_MAGIC)
{
return E_FAIL;
@ -1159,7 +1224,6 @@ namespace
return hr;
}
//--------------------------------------------------------------------------------------
HRESULT CreateTextureFromDDS(
_In_ ID3D11Device* d3dDevice,
@ -1365,12 +1429,17 @@ namespace
{
// Create texture with auto-generated mipmaps
ID3D11Resource* tex = nullptr;
hr = CreateD3DResources(d3dDevice, resDim, width, height, depth, 0, arraySize,
format, usage,
hr = CreateD3DResources(d3dDevice,
resDim, width, height, depth, 0, arraySize,
format,
usage,
bindFlags | D3D11_BIND_RENDER_TARGET,
cpuAccessFlags,
miscFlags | D3D11_RESOURCE_MISC_GENERATE_MIPS, forceSRGB,
isCubeMap, nullptr, &tex, textureView);
miscFlags | D3D11_RESOURCE_MISC_GENERATE_MIPS,
forceSRGB,
isCubeMap,
nullptr,
&tex, textureView);
if (SUCCEEDED(hr))
{
size_t numBytes = 0;
@ -1460,14 +1529,20 @@ namespace
size_t twidth = 0;
size_t theight = 0;
size_t tdepth = 0;
hr = FillInitData(width, height, depth, mipCount, arraySize, format, maxsize, bitSize, bitData,
hr = FillInitData(width, height, depth, mipCount, arraySize,
format, maxsize, bitSize, bitData,
twidth, theight, tdepth, skipMip, initData.get());
if (SUCCEEDED(hr))
{
hr = CreateD3DResources(d3dDevice, resDim, twidth, theight, tdepth, mipCount - skipMip, arraySize,
format, usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB,
isCubeMap, initData.get(), texture, textureView);
hr = CreateD3DResources(d3dDevice,
resDim, twidth, theight, tdepth, mipCount - skipMip, arraySize,
format,
usage, bindFlags, cpuAccessFlags, miscFlags,
forceSRGB,
isCubeMap,
initData.get(),
texture, textureView);
if (FAILED(hr) && !maxsize && (mipCount > 1))
{
@ -1505,9 +1580,14 @@ namespace
twidth, theight, tdepth, skipMip, initData.get());
if (SUCCEEDED(hr))
{
hr = CreateD3DResources(d3dDevice, resDim, twidth, theight, tdepth, mipCount - skipMip, arraySize,
format, usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB,
isCubeMap, initData.get(), texture, textureView);
hr = CreateD3DResources(d3dDevice,
resDim, twidth, theight, tdepth, mipCount - skipMip, arraySize,
format,
usage, bindFlags, cpuAccessFlags, miscFlags,
forceSRGB,
isCubeMap,
initData.get(),
texture, textureView);
}
}
}
@ -1544,11 +1624,67 @@ namespace
return DDS_ALPHA_MODE_UNKNOWN;
}
//--------------------------------------------------------------------------------------
void SetDebugTextureInfo(
_In_z_ const wchar_t* fileName,
_In_opt_ ID3D11Resource** texture,
_In_opt_ ID3D11ShaderResourceView** textureView)
{
#if !defined(NO_D3D11_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) )
if (texture || textureView)
{
CHAR strFileA[MAX_PATH];
int result = WideCharToMultiByte(CP_UTF8,
WC_NO_BEST_FIT_CHARS,
fileName,
-1,
strFileA,
MAX_PATH,
nullptr,
nullptr
);
if (result > 0)
{
const char* pstrName = strrchr(strFileA, '\\');
if (!pstrName)
{
pstrName = strFileA;
}
else
{
pstrName++;
}
if (texture && *texture)
{
(*texture)->SetPrivateData(WKPDID_D3DDebugObjectName,
static_cast<UINT>(strnlen_s(pstrName, MAX_PATH)),
pstrName
);
}
if (textureView && *textureView)
{
(*textureView)->SetPrivateData(WKPDID_D3DDebugObjectName,
static_cast<UINT>(strnlen_s(pstrName, MAX_PATH)),
pstrName
);
}
}
}
#else
UNREFERENCED_PARAMETER(fileName);
UNREFERENCED_PARAMETER(texture);
UNREFERENCED_PARAMETER(textureView);
#endif
}
} // anonymous namespace
//--------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT DirectX::CreateDDSTextureFromMemory(ID3D11Device* d3dDevice,
HRESULT DirectX::CreateDDSTextureFromMemory(
ID3D11Device* d3dDevice,
const uint8_t* ddsData,
size_t ddsDataSize,
ID3D11Resource** texture,
@ -1556,13 +1692,17 @@ HRESULT DirectX::CreateDDSTextureFromMemory(ID3D11Device* d3dDevice,
size_t maxsize,
DDS_ALPHA_MODE* alphaMode)
{
return CreateDDSTextureFromMemoryEx(d3dDevice, nullptr, ddsData, ddsDataSize, maxsize,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, false,
return CreateDDSTextureFromMemoryEx(d3dDevice, nullptr,
ddsData, ddsDataSize,
maxsize,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0,
false,
texture, textureView, alphaMode);
}
_Use_decl_annotations_
HRESULT DirectX::CreateDDSTextureFromMemory(ID3D11Device* d3dDevice,
HRESULT DirectX::CreateDDSTextureFromMemory(
ID3D11Device* d3dDevice,
ID3D11DeviceContext* d3dContext,
const uint8_t* ddsData,
size_t ddsDataSize,
@ -1571,13 +1711,17 @@ HRESULT DirectX::CreateDDSTextureFromMemory(ID3D11Device* d3dDevice,
size_t maxsize,
DDS_ALPHA_MODE* alphaMode)
{
return CreateDDSTextureFromMemoryEx(d3dDevice, d3dContext, ddsData, ddsDataSize, maxsize,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, false,
return CreateDDSTextureFromMemoryEx(d3dDevice, d3dContext,
ddsData, ddsDataSize,
maxsize,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0,
false,
texture, textureView, alphaMode);
}
_Use_decl_annotations_
HRESULT DirectX::CreateDDSTextureFromMemoryEx(ID3D11Device* d3dDevice,
HRESULT DirectX::CreateDDSTextureFromMemoryEx(
ID3D11Device* d3dDevice,
const uint8_t* ddsData,
size_t ddsDataSize,
size_t maxsize,
@ -1590,13 +1734,17 @@ HRESULT DirectX::CreateDDSTextureFromMemoryEx(ID3D11Device* d3dDevice,
ID3D11ShaderResourceView** textureView,
DDS_ALPHA_MODE* alphaMode)
{
return CreateDDSTextureFromMemoryEx(d3dDevice, nullptr, ddsData, ddsDataSize, maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB,
return CreateDDSTextureFromMemoryEx(d3dDevice, nullptr,
ddsData, ddsDataSize,
maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags,
forceSRGB,
texture, textureView, alphaMode);
}
_Use_decl_annotations_
HRESULT DirectX::CreateDDSTextureFromMemoryEx(ID3D11Device* d3dDevice,
HRESULT DirectX::CreateDDSTextureFromMemoryEx(
ID3D11Device* d3dDevice,
ID3D11DeviceContext* d3dContext,
const uint8_t* ddsData,
size_t ddsDataSize,
@ -1629,47 +1777,25 @@ HRESULT DirectX::CreateDDSTextureFromMemoryEx(ID3D11Device* d3dDevice,
}
// Validate DDS file in memory
if (ddsDataSize < (sizeof(uint32_t) + sizeof(DDS_HEADER)))
const DDS_HEADER* header = nullptr;
const uint8_t* bitData = nullptr;
size_t bitSize = 0;
HRESULT hr = LoadTextureDataFromMemory(ddsData, ddsDataSize,
&header,
&bitData,
&bitSize
);
if (FAILED(hr))
{
return E_FAIL;
return hr;
}
uint32_t dwMagicNumber = *(const uint32_t*)(ddsData);
if (dwMagicNumber != DDS_MAGIC)
{
return E_FAIL;
}
auto header = reinterpret_cast<const DDS_HEADER*>(ddsData + sizeof(uint32_t));
// Verify header to validate DDS file
if (header->size != sizeof(DDS_HEADER) ||
header->ddspf.size != sizeof(DDS_PIXELFORMAT))
{
return E_FAIL;
}
// Check for DX10 extension
bool bDXT10Header = false;
if ((header->ddspf.flags & DDS_FOURCC) &&
(MAKEFOURCC('D', 'X', '1', '0') == header->ddspf.fourCC))
{
// Must be long enough for both headers and magic value
if (ddsDataSize < (sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10)))
{
return E_FAIL;
}
bDXT10Header = true;
}
ptrdiff_t offset = sizeof(uint32_t)
+ sizeof(DDS_HEADER)
+ (bDXT10Header ? sizeof(DDS_HEADER_DXT10) : 0);
HRESULT hr = CreateTextureFromDDS(d3dDevice, d3dContext, header,
ddsData + offset, ddsDataSize - offset, maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB,
hr = CreateTextureFromDDS(d3dDevice, d3dContext,
header, bitData, bitSize,
maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags,
forceSRGB,
texture, textureView);
if (SUCCEEDED(hr))
{
@ -1692,20 +1818,24 @@ HRESULT DirectX::CreateDDSTextureFromMemoryEx(ID3D11Device* d3dDevice,
//--------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT DirectX::CreateDDSTextureFromFile(ID3D11Device* d3dDevice,
HRESULT DirectX::CreateDDSTextureFromFile(
ID3D11Device* d3dDevice,
const wchar_t* fileName,
ID3D11Resource** texture,
ID3D11ShaderResourceView** textureView,
size_t maxsize,
DDS_ALPHA_MODE* alphaMode)
{
return CreateDDSTextureFromFileEx(d3dDevice, nullptr, fileName, maxsize,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, false,
return CreateDDSTextureFromFileEx(d3dDevice, nullptr,
fileName, maxsize,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0,
false,
texture, textureView, alphaMode);
}
_Use_decl_annotations_
HRESULT DirectX::CreateDDSTextureFromFile(ID3D11Device* d3dDevice,
HRESULT DirectX::CreateDDSTextureFromFile(
ID3D11Device* d3dDevice,
ID3D11DeviceContext* d3dContext,
const wchar_t* fileName,
ID3D11Resource** texture,
@ -1713,13 +1843,17 @@ HRESULT DirectX::CreateDDSTextureFromFile(ID3D11Device* d3dDevice,
size_t maxsize,
DDS_ALPHA_MODE* alphaMode)
{
return CreateDDSTextureFromFileEx(d3dDevice, d3dContext, fileName, maxsize,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, false,
return CreateDDSTextureFromFileEx(d3dDevice, d3dContext,
fileName,
maxsize,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0,
false,
texture, textureView, alphaMode);
}
_Use_decl_annotations_
HRESULT DirectX::CreateDDSTextureFromFileEx(ID3D11Device* d3dDevice,
HRESULT DirectX::CreateDDSTextureFromFileEx(
ID3D11Device* d3dDevice,
const wchar_t* fileName,
size_t maxsize,
D3D11_USAGE usage,
@ -1731,13 +1865,17 @@ HRESULT DirectX::CreateDDSTextureFromFileEx(ID3D11Device* d3dDevice,
ID3D11ShaderResourceView** textureView,
DDS_ALPHA_MODE* alphaMode)
{
return CreateDDSTextureFromFileEx(d3dDevice, nullptr, fileName, maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB,
return CreateDDSTextureFromFileEx(d3dDevice, nullptr,
fileName,
maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags,
forceSRGB,
texture, textureView, alphaMode);
}
_Use_decl_annotations_
HRESULT DirectX::CreateDDSTextureFromFileEx(ID3D11Device* d3dDevice,
HRESULT DirectX::CreateDDSTextureFromFileEx(
ID3D11Device* d3dDevice,
ID3D11DeviceContext* d3dContext,
const wchar_t* fileName,
size_t maxsize,
@ -1784,56 +1922,16 @@ HRESULT DirectX::CreateDDSTextureFromFileEx(ID3D11Device* d3dDevice,
return hr;
}
hr = CreateTextureFromDDS(d3dDevice, d3dContext, header,
bitData, bitSize, maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB,
hr = CreateTextureFromDDS(d3dDevice, d3dContext,
header, bitData, bitSize,
maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags,
forceSRGB,
texture, textureView);
if (SUCCEEDED(hr))
{
#if !defined(NO_D3D11_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) )
if (texture || textureView)
{
CHAR strFileA[MAX_PATH];
int result = WideCharToMultiByte(CP_UTF8,
WC_NO_BEST_FIT_CHARS,
fileName,
-1,
strFileA,
MAX_PATH,
nullptr,
FALSE
);
if (result > 0)
{
const CHAR* pstrName = strrchr(strFileA, '\\');
if (!pstrName)
{
pstrName = strFileA;
}
else
{
pstrName++;
}
if (texture && *texture)
{
(*texture)->SetPrivateData(WKPDID_D3DDebugObjectName,
static_cast<UINT>(strnlen_s(pstrName, MAX_PATH)),
pstrName
);
}
if (textureView && *textureView)
{
(*textureView)->SetPrivateData(WKPDID_D3DDebugObjectName,
static_cast<UINT>(strnlen_s(pstrName, MAX_PATH)),
pstrName
);
}
}
}
#endif
SetDebugTextureInfo(fileName, texture, textureView);
if (alphaMode)
*alphaMode = GetAlphaMode(header);

Просмотреть файл

@ -147,6 +147,72 @@ namespace
return count;
}
//--------------------------------------------------------------------------------------
HRESULT LoadTextureDataFromMemory(
_In_reads_(ddsDataSize) const uint8_t* ddsData,
size_t ddsDataSize,
const DDS_HEADER** header,
const uint8_t** bitData,
size_t* bitSize)
{
if (!header || !bitData || !bitSize)
{
return E_POINTER;
}
if (ddsDataSize > UINT32_MAX)
{
return E_FAIL;
}
if (ddsDataSize < (sizeof(uint32_t) + sizeof(DDS_HEADER)))
{
return E_FAIL;
}
// DDS files always start with the same magic number ("DDS ")
auto dwMagicNumber = *reinterpret_cast<const uint32_t*>(ddsData);
if (dwMagicNumber != DDS_MAGIC)
{
return E_FAIL;
}
auto hdr = reinterpret_cast<const DDS_HEADER*>(ddsData + sizeof(uint32_t));
// Verify header to validate DDS file
if (hdr->size != sizeof(DDS_HEADER) ||
hdr->ddspf.size != sizeof(DDS_PIXELFORMAT))
{
return E_FAIL;
}
// Check for DX10 extension
bool bDXT10Header = false;
if ((hdr->ddspf.flags & DDS_FOURCC) &&
(MAKEFOURCC('D', 'X', '1', '0') == hdr->ddspf.fourCC))
{
// Must be long enough for both headers and magic value
if (ddsDataSize < (sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10)))
{
return E_FAIL;
}
bDXT10Header = true;
}
// setup the pointers in the process request
*header = hdr;
ptrdiff_t offset = sizeof(uint32_t)
+ sizeof(DDS_HEADER)
+ (bDXT10Header ? sizeof(DDS_HEADER_DXT10) : 0);
*bitData = ddsData + offset;
*bitSize = ddsDataSize - offset;
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT LoadTextureDataFromFile(
_In_z_ const wchar_t* fileName,
@ -216,7 +282,7 @@ namespace
}
// DDS files always start with the same magic number ("DDS ")
uint32_t dwMagicNumber = *reinterpret_cast<const uint32_t*>(ddsData.get());
auto dwMagicNumber = *reinterpret_cast<const uint32_t*>(ddsData.get());
if (dwMagicNumber != DDS_MAGIC)
{
return E_FAIL;
@ -1342,6 +1408,35 @@ namespace
return DDS_ALPHA_MODE_UNKNOWN;
}
//--------------------------------------------------------------------------------------
void SetDebugTextureInfo(
_In_z_ const wchar_t* fileName,
_In_ ID3D12Resource** texture)
{
#if !defined(NO_D3D12_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) )
if (texture)
{
const wchar_t* pstrName = wcsrchr(fileName, '\\');
if (!pstrName)
{
pstrName = fileName;
}
else
{
pstrName++;
}
if (texture && *texture)
{
(*texture)->SetName(pstrName);
}
}
#else
UNREFERENCED_PARAMETER(fileName);
UNREFERENCED_PARAMETER(texture);
#endif
}
} // anonymous namespace
@ -1403,46 +1498,22 @@ HRESULT DirectX::LoadDDSTextureFromMemoryEx(
}
// Validate DDS file in memory
if (ddsDataSize < (sizeof(uint32_t) + sizeof(DDS_HEADER)))
const DDS_HEADER* header = nullptr;
const uint8_t* bitData = nullptr;
size_t bitSize = 0;
HRESULT hr = LoadTextureDataFromMemory(ddsData, ddsDataSize,
&header,
&bitData,
&bitSize
);
if (FAILED(hr))
{
return E_FAIL;
return hr;
}
uint32_t dwMagicNumber = *(const uint32_t*)(ddsData);
if (dwMagicNumber != DDS_MAGIC)
{
return E_FAIL;
}
auto header = reinterpret_cast<const DDS_HEADER*>(ddsData + sizeof(uint32_t));
// Verify header to validate DDS file
if (header->size != sizeof(DDS_HEADER) ||
header->ddspf.size != sizeof(DDS_PIXELFORMAT))
{
return E_FAIL;
}
// Check for DX10 extension
bool bDXT10Header = false;
if ((header->ddspf.flags & DDS_FOURCC) &&
(MAKEFOURCC('D', 'X', '1', '0') == header->ddspf.fourCC))
{
// Must be long enough for both headers and magic value
if (ddsDataSize < (sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10)))
{
return E_FAIL;
}
bDXT10Header = true;
}
ptrdiff_t offset = sizeof(uint32_t)
+ sizeof(DDS_HEADER)
+ (bDXT10Header ? sizeof(DDS_HEADER_DXT10) : 0);
HRESULT hr = CreateTextureFromDDS(d3dDevice,
header, ddsData + offset, ddsDataSize - offset, maxsize,
hr = CreateTextureFromDDS(d3dDevice,
header, bitData, bitSize, maxsize,
resFlags, loadFlags,
texture, subresources, isCubeMap);
if (SUCCEEDED(hr))
@ -1538,38 +1609,7 @@ HRESULT DirectX::LoadDDSTextureFromFileEx(
if (SUCCEEDED(hr))
{
#if !defined(NO_D3D12_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) )
if (texture)
{
CHAR strFileA[MAX_PATH];
int result = WideCharToMultiByte(CP_UTF8,
WC_NO_BEST_FIT_CHARS,
fileName,
-1,
strFileA,
MAX_PATH,
nullptr,
FALSE
);
if (result > 0)
{
const wchar_t* pstrName = wcsrchr(fileName, '\\');
if (!pstrName)
{
pstrName = fileName;
}
else
{
pstrName++;
}
if (texture && *texture)
{
(*texture)->SetName(pstrName);
}
}
}
#endif
SetDebugTextureInfo(fileName, texture);
if (alphaMode)
*alphaMode = GetAlphaMode(header);

Просмотреть файл

@ -677,24 +677,85 @@ namespace
return hr;
}
//--------------------------------------------------------------------------------------
void SetDebugTextureInfo(
_In_z_ const wchar_t* fileName,
_In_opt_ ID3D11Resource** texture,
_In_opt_ ID3D11ShaderResourceView** textureView)
{
#if !defined(NO_D3D11_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) )
if (texture || textureView)
{
CHAR strFileA[MAX_PATH];
int result = WideCharToMultiByte(CP_UTF8,
WC_NO_BEST_FIT_CHARS,
fileName,
-1,
strFileA,
MAX_PATH,
nullptr,
nullptr
);
if (result > 0)
{
const char* pstrName = strrchr(strFileA, '\\');
if (!pstrName)
{
pstrName = strFileA;
}
else
{
pstrName++;
}
if (texture && *texture)
{
(*texture)->SetPrivateData(WKPDID_D3DDebugObjectName,
static_cast<UINT>(strnlen_s(pstrName, MAX_PATH)),
pstrName
);
}
if (textureView && *textureView)
{
(*textureView)->SetPrivateData(WKPDID_D3DDebugObjectName,
static_cast<UINT>(strnlen_s(pstrName, MAX_PATH)),
pstrName
);
}
}
}
#else
UNREFERENCED_PARAMETER(fileName);
UNREFERENCED_PARAMETER(texture);
UNREFERENCED_PARAMETER(textureView);
#endif
}
} // anonymous namespace
//--------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT DirectX::CreateWICTextureFromMemory(ID3D11Device* d3dDevice,
HRESULT DirectX::CreateWICTextureFromMemory(
ID3D11Device* d3dDevice,
const uint8_t* wicData,
size_t wicDataSize,
ID3D11Resource** texture,
ID3D11ShaderResourceView** textureView,
size_t maxsize)
{
return CreateWICTextureFromMemoryEx(d3dDevice, nullptr, wicData, wicDataSize, maxsize,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, WIC_LOADER_DEFAULT,
return CreateWICTextureFromMemoryEx(d3dDevice, nullptr,
wicData, wicDataSize,
maxsize,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0,
WIC_LOADER_DEFAULT,
texture, textureView);
}
_Use_decl_annotations_
HRESULT DirectX::CreateWICTextureFromMemory(ID3D11Device* d3dDevice,
HRESULT DirectX::CreateWICTextureFromMemory(
ID3D11Device* d3dDevice,
ID3D11DeviceContext* d3dContext,
const uint8_t* wicData,
size_t wicDataSize,
@ -702,13 +763,17 @@ HRESULT DirectX::CreateWICTextureFromMemory(ID3D11Device* d3dDevice,
ID3D11ShaderResourceView** textureView,
size_t maxsize)
{
return CreateWICTextureFromMemoryEx(d3dDevice, d3dContext, wicData, wicDataSize, maxsize,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, WIC_LOADER_DEFAULT,
return CreateWICTextureFromMemoryEx(d3dDevice, d3dContext,
wicData, wicDataSize,
maxsize,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0,
WIC_LOADER_DEFAULT,
texture, textureView);
}
_Use_decl_annotations_
HRESULT DirectX::CreateWICTextureFromMemoryEx(ID3D11Device* d3dDevice,
HRESULT DirectX::CreateWICTextureFromMemoryEx(
ID3D11Device* d3dDevice,
const uint8_t* wicData,
size_t wicDataSize,
size_t maxsize,
@ -720,13 +785,17 @@ HRESULT DirectX::CreateWICTextureFromMemoryEx(ID3D11Device* d3dDevice,
ID3D11Resource** texture,
ID3D11ShaderResourceView** textureView)
{
return CreateWICTextureFromMemoryEx(d3dDevice, nullptr, wicData, wicDataSize, maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags, loadFlags,
return CreateWICTextureFromMemoryEx(d3dDevice, nullptr,
wicData, wicDataSize,
maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags,
loadFlags,
texture, textureView);
}
_Use_decl_annotations_
HRESULT DirectX::CreateWICTextureFromMemoryEx(ID3D11Device* d3dDevice,
HRESULT DirectX::CreateWICTextureFromMemoryEx(
ID3D11Device* d3dDevice,
ID3D11DeviceContext* d3dContext,
const uint8_t* wicData,
size_t wicDataSize,
@ -782,8 +851,11 @@ HRESULT DirectX::CreateWICTextureFromMemoryEx(ID3D11Device* d3dDevice,
if (FAILED(hr))
return hr;
hr = CreateTextureFromWIC(d3dDevice, d3dContext, frame.Get(), maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags, loadFlags,
hr = CreateTextureFromWIC(d3dDevice, d3dContext,
frame.Get(),
maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags,
loadFlags,
texture, textureView);
if (FAILED(hr))
return hr;
@ -803,32 +875,40 @@ HRESULT DirectX::CreateWICTextureFromMemoryEx(ID3D11Device* d3dDevice,
//--------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT DirectX::CreateWICTextureFromFile(ID3D11Device* d3dDevice,
HRESULT DirectX::CreateWICTextureFromFile(
ID3D11Device* d3dDevice,
const wchar_t* fileName,
ID3D11Resource** texture,
ID3D11ShaderResourceView** textureView,
size_t maxsize)
{
return CreateWICTextureFromFileEx(d3dDevice, nullptr, fileName, maxsize,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, WIC_LOADER_DEFAULT,
return CreateWICTextureFromFileEx(d3dDevice, nullptr,
fileName, maxsize,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0,
WIC_LOADER_DEFAULT,
texture, textureView);
}
_Use_decl_annotations_
HRESULT DirectX::CreateWICTextureFromFile(ID3D11Device* d3dDevice,
HRESULT DirectX::CreateWICTextureFromFile(
ID3D11Device* d3dDevice,
ID3D11DeviceContext* d3dContext,
const wchar_t* fileName,
ID3D11Resource** texture,
ID3D11ShaderResourceView** textureView,
size_t maxsize)
{
return CreateWICTextureFromFileEx(d3dDevice, d3dContext, fileName, maxsize,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, WIC_LOADER_DEFAULT,
return CreateWICTextureFromFileEx(d3dDevice, d3dContext,
fileName,
maxsize,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0,
WIC_LOADER_DEFAULT,
texture, textureView);
}
_Use_decl_annotations_
HRESULT DirectX::CreateWICTextureFromFileEx(ID3D11Device* d3dDevice,
HRESULT DirectX::CreateWICTextureFromFileEx(
ID3D11Device* d3dDevice,
const wchar_t* fileName,
size_t maxsize,
D3D11_USAGE usage,
@ -839,13 +919,17 @@ HRESULT DirectX::CreateWICTextureFromFileEx(ID3D11Device* d3dDevice,
ID3D11Resource** texture,
ID3D11ShaderResourceView** textureView)
{
return CreateWICTextureFromFileEx(d3dDevice, nullptr, fileName, maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags, loadFlags,
return CreateWICTextureFromFileEx(d3dDevice, nullptr,
fileName,
maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags,
loadFlags,
texture, textureView);
}
_Use_decl_annotations_
HRESULT DirectX::CreateWICTextureFromFileEx(ID3D11Device* d3dDevice,
HRESULT DirectX::CreateWICTextureFromFileEx(
ID3D11Device* d3dDevice,
ID3D11DeviceContext* d3dContext,
const wchar_t* fileName,
size_t maxsize,
@ -875,7 +959,11 @@ HRESULT DirectX::CreateWICTextureFromFileEx(ID3D11Device* d3dDevice,
// Initialize WIC
ComPtr<IWICBitmapDecoder> decoder;
HRESULT hr = pWIC->CreateDecoderFromFilename(fileName, nullptr, GENERIC_READ, WICDecodeMetadataCacheOnDemand, decoder.GetAddressOf());
HRESULT hr = pWIC->CreateDecoderFromFilename(fileName,
nullptr,
GENERIC_READ,
WICDecodeMetadataCacheOnDemand,
decoder.GetAddressOf());
if (FAILED(hr))
return hr;
@ -884,56 +972,17 @@ HRESULT DirectX::CreateWICTextureFromFileEx(ID3D11Device* d3dDevice,
if (FAILED(hr))
return hr;
hr = CreateTextureFromWIC(d3dDevice, d3dContext, frame.Get(), maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags, loadFlags,
hr = CreateTextureFromWIC(d3dDevice, d3dContext,
frame.Get(),
maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags,
loadFlags,
texture, textureView);
#if !defined(NO_D3D11_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) )
if (SUCCEEDED(hr))
{
if (texture || textureView)
{
char strFileA[MAX_PATH];
int result = WideCharToMultiByte(CP_UTF8,
WC_NO_BEST_FIT_CHARS,
fileName,
-1,
strFileA,
MAX_PATH,
nullptr,
FALSE
);
if (result > 0)
{
const char* pstrName = strrchr(strFileA, '\\');
if (!pstrName)
{
pstrName = strFileA;
}
else
{
pstrName++;
}
if (texture && *texture)
{
(*texture)->SetPrivateData(WKPDID_D3DDebugObjectName,
static_cast<UINT>(strnlen_s(pstrName, MAX_PATH)),
pstrName
);
}
if (textureView && *textureView)
{
(*textureView)->SetPrivateData(WKPDID_D3DDebugObjectName,
static_cast<UINT>(strnlen_s(pstrName, MAX_PATH)),
pstrName
);
}
}
}
SetDebugTextureInfo(fileName, texture, textureView);
}
#endif
return hr;
}

Просмотреть файл

@ -529,6 +529,35 @@ namespace
*texture = tex;
return hr;
}
//--------------------------------------------------------------------------------------
void SetDebugTextureInfo(
_In_z_ const wchar_t* fileName,
_In_ ID3D12Resource** texture)
{
#if !defined(NO_D3D12_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) )
if (texture)
{
const wchar_t* pstrName = wcsrchr(fileName, '\\');
if (!pstrName)
{
pstrName = fileName;
}
else
{
pstrName++;
}
if (texture && *texture)
{
(*texture)->SetName(pstrName);
}
}
#else
UNREFERENCED_PARAMETER(fileName);
UNREFERENCED_PARAMETER(texture);
#endif
}
} // anonymous namespace
@ -569,50 +598,50 @@ HRESULT DirectX::LoadWICTextureFromMemoryEx(
std::unique_ptr<uint8_t[]>& decodedData,
D3D12_SUBRESOURCE_DATA& subresource)
{
if ( texture )
if (texture)
{
*texture = nullptr;
}
if (!d3dDevice || !wicData || !texture)
if (!d3dDevice || !wicData || !texture)
return E_INVALIDARG;
if ( !wicDataSize )
if (!wicDataSize)
return E_FAIL;
if ( wicDataSize > UINT32_MAX )
return HRESULT_FROM_WIN32( ERROR_FILE_TOO_LARGE );
if (wicDataSize > UINT32_MAX)
return HRESULT_FROM_WIN32(ERROR_FILE_TOO_LARGE);
auto pWIC = _GetWIC();
if ( !pWIC )
if (!pWIC)
return E_NOINTERFACE;
// Create input stream for memory
ComPtr<IWICStream> stream;
HRESULT hr = pWIC->CreateStream( stream.GetAddressOf() );
if ( FAILED(hr) )
HRESULT hr = pWIC->CreateStream(stream.GetAddressOf());
if (FAILED(hr))
return hr;
hr = stream->InitializeFromMemory( const_cast<uint8_t*>( wicData ), static_cast<DWORD>( wicDataSize ) );
if ( FAILED(hr) )
hr = stream->InitializeFromMemory(const_cast<uint8_t*>(wicData), static_cast<DWORD>(wicDataSize));
if (FAILED(hr))
return hr;
// Initialize WIC
ComPtr<IWICBitmapDecoder> decoder;
hr = pWIC->CreateDecoderFromStream( stream.Get(), nullptr, WICDecodeMetadataCacheOnDemand, decoder.GetAddressOf() );
if ( FAILED(hr) )
hr = pWIC->CreateDecoderFromStream(stream.Get(), nullptr, WICDecodeMetadataCacheOnDemand, decoder.GetAddressOf());
if (FAILED(hr))
return hr;
ComPtr<IWICBitmapFrameDecode> frame;
hr = decoder->GetFrame( 0, frame.GetAddressOf() );
if ( FAILED(hr) )
hr = decoder->GetFrame(0, frame.GetAddressOf());
if (FAILED(hr))
return hr;
hr = CreateTextureFromWIC( d3dDevice,
frame.Get(), maxsize,
resFlags, loadFlags,
texture, decodedData, subresource);
if ( FAILED(hr))
hr = CreateTextureFromWIC(d3dDevice,
frame.Get(), maxsize,
resFlags, loadFlags,
texture, decodedData, subresource);
if (FAILED(hr))
return hr;
_Analysis_assume_(*texture != nullptr);
@ -656,52 +685,37 @@ HRESULT DirectX::LoadWICTextureFromFileEx(
std::unique_ptr<uint8_t[]>& decodedData,
D3D12_SUBRESOURCE_DATA& subresource)
{
if ( texture )
if (texture)
{
*texture = nullptr;
}
if (!d3dDevice || !fileName || !texture )
if (!d3dDevice || !fileName || !texture)
return E_INVALIDARG;
auto pWIC = _GetWIC();
if ( !pWIC )
if (!pWIC)
return E_NOINTERFACE;
// Initialize WIC
ComPtr<IWICBitmapDecoder> decoder;
HRESULT hr = pWIC->CreateDecoderFromFilename( fileName, nullptr, GENERIC_READ, WICDecodeMetadataCacheOnDemand, decoder.GetAddressOf() );
if ( FAILED(hr) )
HRESULT hr = pWIC->CreateDecoderFromFilename(fileName, nullptr, GENERIC_READ, WICDecodeMetadataCacheOnDemand, decoder.GetAddressOf());
if (FAILED(hr))
return hr;
ComPtr<IWICBitmapFrameDecode> frame;
hr = decoder->GetFrame( 0, frame.GetAddressOf() );
if ( FAILED(hr) )
hr = decoder->GetFrame(0, frame.GetAddressOf());
if (FAILED(hr))
return hr;
hr = CreateTextureFromWIC( d3dDevice, frame.Get(), maxsize,
resFlags, loadFlags,
texture, decodedData, subresource );
hr = CreateTextureFromWIC(d3dDevice, frame.Get(), maxsize,
resFlags, loadFlags,
texture, decodedData, subresource);
#if !defined(NO_D3D12_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) )
if ( SUCCEEDED(hr) )
if (SUCCEEDED(hr))
{
const wchar_t* pstrName = wcsrchr(fileName, '\\' );
if (!pstrName)
{
pstrName = fileName;
}
else
{
pstrName++;
}
if (texture && *texture)
{
(*texture)->SetName(pstrName);
}
SetDebugTextureInfo(fileName, texture);
}
#endif
return hr;
}