1
0
mirror of https://github.com/microsoft/DirectXTex synced 2024-11-24 05:10:17 +00:00

Add DDS_LOADER_IGNORE_SRGB parameter/functionality to CreateDDSTexture*Ex

This commit is contained in:
Chuck Walbourn 2022-07-28 12:13:09 -07:00
parent f904ecf09b
commit 4333376df1
4 changed files with 134 additions and 41 deletions

View File

@ -207,7 +207,7 @@ namespace
*header = hdr;
auto offset = sizeof(uint32_t)
+ sizeof(DDS_HEADER)
+ (bDXT10Header ? sizeof(DDS_HEADER_DXT10) : 0);
+ (bDXT10Header ? sizeof(DDS_HEADER_DXT10) : 0u);
*bitData = ddsData + offset;
*bitSize = ddsDataSize - offset;
@ -232,18 +232,16 @@ namespace
// open the file
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
ScopedHandle hFile(safe_handle(CreateFile2(fileName,
GENERIC_READ,
FILE_SHARE_READ,
OPEN_EXISTING,
ScopedHandle hFile(safe_handle(CreateFile2(
fileName,
GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING,
nullptr)));
#else
ScopedHandle hFile(safe_handle(CreateFileW(fileName,
GENERIC_READ,
FILE_SHARE_READ,
ScopedHandle hFile(safe_handle(CreateFileW(
fileName,
GENERIC_READ, FILE_SHARE_READ,
nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
nullptr)));
#endif
@ -333,7 +331,7 @@ namespace
// setup the pointers in the process request
*header = hdr;
auto offset = sizeof(uint32_t) + sizeof(DDS_HEADER)
+ (bDXT10Header ? sizeof(DDS_HEADER_DXT10) : 0);
+ (bDXT10Header ? sizeof(DDS_HEADER_DXT10) : 0u);
*bitData = ddsData.get() + offset;
*bitSize = fileInfo.EndOfFile.LowPart - offset;
@ -909,7 +907,7 @@ namespace
#undef ISBITMASK
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
DXGI_FORMAT MakeSRGB(_In_ DXGI_FORMAT format) noexcept
{
switch (format)
@ -941,6 +939,38 @@ namespace
}
//--------------------------------------------------------------------------------------
inline DXGI_FORMAT MakeLinear(_In_ DXGI_FORMAT format) noexcept
{
switch (format)
{
case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
return DXGI_FORMAT_R8G8B8A8_UNORM;
case DXGI_FORMAT_BC1_UNORM_SRGB:
return DXGI_FORMAT_BC1_UNORM;
case DXGI_FORMAT_BC2_UNORM_SRGB:
return DXGI_FORMAT_BC2_UNORM;
case DXGI_FORMAT_BC3_UNORM_SRGB:
return DXGI_FORMAT_BC3_UNORM;
case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
return DXGI_FORMAT_B8G8R8A8_UNORM;
case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
return DXGI_FORMAT_B8G8R8X8_UNORM;
case DXGI_FORMAT_BC7_UNORM_SRGB:
return DXGI_FORMAT_BC7_UNORM;
default:
return format;
}
}
//--------------------------------------------------------------------------------------
HRESULT FillInitData(
_In_ size_t width,
@ -1053,7 +1083,8 @@ namespace
_In_ unsigned int bindFlags,
_In_ unsigned int cpuAccessFlags,
_In_ unsigned int miscFlags,
_In_ bool forceSRGB,
_In_ DDS_LOADER_FLAGS loadFlags,
_In_ bool isCubeMap,
_In_reads_opt_(mipCount*arraySize) const D3D11_SUBRESOURCE_DATA* initData,
_Outptr_opt_ ID3D11Resource** texture,
@ -1064,10 +1095,14 @@ namespace
HRESULT hr = E_FAIL;
if (forceSRGB)
if (loadFlags & DDS_LOADER_FORCE_SRGB)
{
format = MakeSRGB(format);
}
else if (loadFlags & DDS_LOADER_IGNORE_SRGB)
{
format = MakeLinear(format);
}
switch (resDim)
{
@ -1285,7 +1320,7 @@ namespace
_In_ unsigned int bindFlags,
_In_ unsigned int cpuAccessFlags,
_In_ unsigned int miscFlags,
_In_ bool forceSRGB,
_In_ DDS_LOADER_FLAGS loadFlags,
_Outptr_opt_ ID3D11Resource** texture,
_Outptr_opt_ ID3D11ShaderResourceView** textureView) noexcept
{
@ -1485,7 +1520,7 @@ namespace
bindFlags | D3D11_BIND_RENDER_TARGET,
cpuAccessFlags,
miscFlags | D3D11_RESOURCE_MISC_GENERATE_MIPS,
forceSRGB,
loadFlags,
isCubeMap,
nullptr,
&tex, textureView);
@ -1588,7 +1623,7 @@ namespace
resDim, twidth, theight, tdepth, mipCount - skipMip, arraySize,
format,
usage, bindFlags, cpuAccessFlags, miscFlags,
forceSRGB,
loadFlags,
isCubeMap,
initData.get(),
texture, textureView);
@ -1633,7 +1668,7 @@ namespace
resDim, twidth, theight, tdepth, mipCount - skipMip, arraySize,
format,
usage, bindFlags, cpuAccessFlags, miscFlags,
forceSRGB,
loadFlags,
isCubeMap,
initData.get(),
texture, textureView);
@ -1749,7 +1784,7 @@ HRESULT DirectX::CreateDDSTextureFromMemory(
ddsData, ddsDataSize,
maxsize,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0,
false,
DDS_LOADER_DEFAULT,
texture, textureView, alphaMode);
}
@ -1768,7 +1803,7 @@ HRESULT DirectX::CreateDDSTextureFromMemory(
ddsData, ddsDataSize,
maxsize,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0,
false,
DDS_LOADER_DEFAULT,
texture, textureView, alphaMode);
}
@ -1782,7 +1817,7 @@ HRESULT DirectX::CreateDDSTextureFromMemoryEx(
unsigned int bindFlags,
unsigned int cpuAccessFlags,
unsigned int miscFlags,
bool forceSRGB,
DDS_LOADER_FLAGS loadFlags,
ID3D11Resource** texture,
ID3D11ShaderResourceView** textureView,
DDS_ALPHA_MODE* alphaMode) noexcept
@ -1791,7 +1826,7 @@ HRESULT DirectX::CreateDDSTextureFromMemoryEx(
ddsData, ddsDataSize,
maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags,
forceSRGB,
loadFlags,
texture, textureView, alphaMode);
}
@ -1806,7 +1841,7 @@ HRESULT DirectX::CreateDDSTextureFromMemoryEx(
unsigned int bindFlags,
unsigned int cpuAccessFlags,
unsigned int miscFlags,
bool forceSRGB,
DDS_LOADER_FLAGS loadFlags,
ID3D11Resource** texture,
ID3D11ShaderResourceView** textureView,
DDS_ALPHA_MODE* alphaMode) noexcept
@ -1853,7 +1888,7 @@ HRESULT DirectX::CreateDDSTextureFromMemoryEx(
header, bitData, bitSize,
maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags,
forceSRGB,
loadFlags,
texture, textureView);
if (SUCCEEDED(hr))
{
@ -1887,7 +1922,7 @@ HRESULT DirectX::CreateDDSTextureFromFile(
return CreateDDSTextureFromFileEx(d3dDevice, nullptr,
fileName, maxsize,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0,
false,
DDS_LOADER_DEFAULT,
texture, textureView, alphaMode);
}
@ -1905,7 +1940,7 @@ HRESULT DirectX::CreateDDSTextureFromFile(
fileName,
maxsize,
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0,
false,
DDS_LOADER_DEFAULT,
texture, textureView, alphaMode);
}
@ -1918,7 +1953,7 @@ HRESULT DirectX::CreateDDSTextureFromFileEx(
unsigned int bindFlags,
unsigned int cpuAccessFlags,
unsigned int miscFlags,
bool forceSRGB,
DDS_LOADER_FLAGS loadFlags,
ID3D11Resource** texture,
ID3D11ShaderResourceView** textureView,
DDS_ALPHA_MODE* alphaMode) noexcept
@ -1927,7 +1962,7 @@ HRESULT DirectX::CreateDDSTextureFromFileEx(
fileName,
maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags,
forceSRGB,
loadFlags,
texture, textureView, alphaMode);
}
@ -1941,7 +1976,7 @@ HRESULT DirectX::CreateDDSTextureFromFileEx(
unsigned int bindFlags,
unsigned int cpuAccessFlags,
unsigned int miscFlags,
bool forceSRGB,
DDS_LOADER_FLAGS loadFlags,
ID3D11Resource** texture,
ID3D11ShaderResourceView** textureView,
DDS_ALPHA_MODE* alphaMode) noexcept
@ -1989,7 +2024,7 @@ HRESULT DirectX::CreateDDSTextureFromFileEx(
header, bitData, bitSize,
maxsize,
usage, bindFlags, cpuAccessFlags, miscFlags,
forceSRGB,
loadFlags,
texture, textureView);
if (SUCCEEDED(hr))

View File

@ -38,6 +38,28 @@ namespace DirectX
};
#endif
#ifndef DDS_LOADER_FLAGS_DEFINED
#define DDS_LOADER_FLAGS_DEFINED
enum DDS_LOADER_FLAGS : uint32_t
{
DDS_LOADER_DEFAULT = 0,
DDS_LOADER_FORCE_SRGB = 0x1,
DDS_LOADER_IGNORE_SRGB = 0x2,
};
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-dynamic-exception-spec"
#endif
DEFINE_ENUM_FLAG_OPERATORS(DDS_LOADER_FLAGS);
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#endif
// Standard version
HRESULT CreateDDSTextureFromMemory(
_In_ ID3D11Device* d3dDevice,
@ -86,7 +108,7 @@ namespace DirectX
_In_ unsigned int bindFlags,
_In_ unsigned int cpuAccessFlags,
_In_ unsigned int miscFlags,
_In_ bool forceSRGB,
_In_ DDS_LOADER_FLAGS loadFlags,
_Outptr_opt_ ID3D11Resource** texture,
_Outptr_opt_ ID3D11ShaderResourceView** textureView,
_Out_opt_ DDS_ALPHA_MODE* alphaMode = nullptr) noexcept;
@ -99,7 +121,7 @@ namespace DirectX
_In_ unsigned int bindFlags,
_In_ unsigned int cpuAccessFlags,
_In_ unsigned int miscFlags,
_In_ bool forceSRGB,
_In_ DDS_LOADER_FLAGS loadFlags,
_Outptr_opt_ ID3D11Resource** texture,
_Outptr_opt_ ID3D11ShaderResourceView** textureView,
_Out_opt_ DDS_ALPHA_MODE* alphaMode = nullptr) noexcept;
@ -115,7 +137,7 @@ namespace DirectX
_In_ unsigned int bindFlags,
_In_ unsigned int cpuAccessFlags,
_In_ unsigned int miscFlags,
_In_ bool forceSRGB,
_In_ DDS_LOADER_FLAGS loadFlags,
_Outptr_opt_ ID3D11Resource** texture,
_Outptr_opt_ ID3D11ShaderResourceView** textureView,
_Out_opt_ DDS_ALPHA_MODE* alphaMode = nullptr) noexcept;
@ -129,7 +151,7 @@ namespace DirectX
_In_ unsigned int bindFlags,
_In_ unsigned int cpuAccessFlags,
_In_ unsigned int miscFlags,
_In_ bool forceSRGB,
_In_ DDS_LOADER_FLAGS loadFlags,
_Outptr_opt_ ID3D11Resource** texture,
_Outptr_opt_ ID3D11ShaderResourceView** textureView,
_Out_opt_ DDS_ALPHA_MODE* alphaMode = nullptr) noexcept;

View File

@ -253,7 +253,7 @@ namespace
*header = hdr;
auto offset = sizeof(uint32_t)
+ sizeof(DDS_HEADER)
+ (bDXT10Header ? sizeof(DDS_HEADER_DXT10) : 0);
+ (bDXT10Header ? sizeof(DDS_HEADER_DXT10) : 0u);
*bitData = ddsData + offset;
*bitSize = ddsDataSize - offset;
@ -278,10 +278,9 @@ namespace
#ifdef _WIN32
// open the file
ScopedHandle hFile(safe_handle(CreateFile2(fileName,
GENERIC_READ,
FILE_SHARE_READ,
OPEN_EXISTING,
ScopedHandle hFile(safe_handle(CreateFile2(
fileName,
GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING,
nullptr)));
if (!hFile)
@ -408,7 +407,7 @@ namespace
// setup the pointers in the process request
*header = hdr;
auto offset = sizeof(uint32_t) + sizeof(DDS_HEADER)
+ (bDXT10Header ? sizeof(DDS_HEADER_DXT10) : 0);
+ (bDXT10Header ? sizeof(DDS_HEADER_DXT10) : 0u);
*bitData = ddsData.get() + offset;
*bitSize = len - offset;
@ -988,7 +987,7 @@ namespace
#undef ISBITMASK
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
DXGI_FORMAT MakeSRGB(_In_ DXGI_FORMAT format) noexcept
{
switch (format)
@ -1020,6 +1019,38 @@ namespace
}
//--------------------------------------------------------------------------------------
inline DXGI_FORMAT MakeLinear(_In_ DXGI_FORMAT format) noexcept
{
switch (format)
{
case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
return DXGI_FORMAT_R8G8B8A8_UNORM;
case DXGI_FORMAT_BC1_UNORM_SRGB:
return DXGI_FORMAT_BC1_UNORM;
case DXGI_FORMAT_BC2_UNORM_SRGB:
return DXGI_FORMAT_BC2_UNORM;
case DXGI_FORMAT_BC3_UNORM_SRGB:
return DXGI_FORMAT_BC3_UNORM;
case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
return DXGI_FORMAT_B8G8R8A8_UNORM;
case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
return DXGI_FORMAT_B8G8R8X8_UNORM;
case DXGI_FORMAT_BC7_UNORM_SRGB:
return DXGI_FORMAT_BC7_UNORM;
default:
return format;
}
}
//--------------------------------------------------------------------------------------
inline bool IsDepthStencil(DXGI_FORMAT fmt) noexcept
{
@ -1216,6 +1247,10 @@ namespace
{
format = MakeSRGB(format);
}
else if (loadFlags & DDS_LOADER_IGNORE_SRGB)
{
format = MakeLinear(format);
}
D3D12_RESOURCE_DESC desc = {};
desc.Width = static_cast<UINT>(width);

View File

@ -61,6 +61,7 @@ namespace DirectX
{
DDS_LOADER_DEFAULT = 0,
DDS_LOADER_FORCE_SRGB = 0x1,
DDS_LOADER_IGNORE_SRGB = 0x2,
DDS_LOADER_MIP_RESERVE = 0x8,
};