diff --git a/DDSTextureLoader/DDSTextureLoader.cpp b/DDSTextureLoader/DDSTextureLoader.cpp index 218a27e..5511680 100644 --- a/DDSTextureLoader/DDSTextureLoader.cpp +++ b/DDSTextureLoader/DDSTextureLoader.cpp @@ -1,7 +1,7 @@ //-------------------------------------------------------------------------------------- // File: DDSTextureLoader.cpp // -// Functions for loading a DDS texture and creating a Direct3D 11 runtime resource for it +// Functions for loading a DDS texture and creating a Direct3D runtime resource for it // // Note these functions are useful as a light-weight runtime loader for DDS files. For // a full-featured DDS file reader, writer, and texture processing pipeline see @@ -142,8 +142,8 @@ namespace HRESULT LoadTextureDataFromFile( _In_z_ const wchar_t* fileName, std::unique_ptr& ddsData, - DDS_HEADER** header, - uint8_t** bitData, + const DDS_HEADER** header, + const uint8_t** bitData, size_t* bitSize) { if (!header || !bitData || !bitSize) @@ -153,31 +153,31 @@ namespace // open the file #if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) - ScopedHandle hFile( safe_handle( CreateFile2( fileName, - GENERIC_READ, - FILE_SHARE_READ, - OPEN_EXISTING, - nullptr ) ) ); + 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, - nullptr, - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, - nullptr ) ) ); + ScopedHandle hFile(safe_handle(CreateFileW(fileName, + GENERIC_READ, + FILE_SHARE_READ, + nullptr, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + nullptr))); #endif - if ( !hFile ) + if (!hFile) { - return HRESULT_FROM_WIN32( GetLastError() ); + return HRESULT_FROM_WIN32(GetLastError()); } // Get the file size FILE_STANDARD_INFO fileInfo; - if ( !GetFileInformationByHandleEx( hFile.get(), FileStandardInfo, &fileInfo, sizeof(fileInfo) ) ) + if (!GetFileInformationByHandleEx(hFile.get(), FileStandardInfo, &fileInfo, sizeof(fileInfo))) { - return HRESULT_FROM_WIN32( GetLastError() ); + return HRESULT_FROM_WIN32(GetLastError()); } // File is too big for 32-bit allocation, so reject read @@ -187,13 +187,13 @@ 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(DDS_HEADER) + sizeof(uint32_t))) { return E_FAIL; } // create enough space for the file data - ddsData.reset( new (std::nothrow) uint8_t[fileInfo.EndOfFile.LowPart ] ); + ddsData.reset(new (std::nothrow) uint8_t[fileInfo.EndOfFile.LowPart]); if (!ddsData) { return E_OUTOFMEMORY; @@ -201,14 +201,14 @@ namespace // read the data in DWORD BytesRead = 0; - if (!ReadFile( hFile.get(), - ddsData.get(), - fileInfo.EndOfFile.LowPart, - &BytesRead, - nullptr - )) + if (!ReadFile(hFile.get(), + ddsData.get(), + fileInfo.EndOfFile.LowPart, + &BytesRead, + nullptr + )) { - return HRESULT_FROM_WIN32( GetLastError() ); + return HRESULT_FROM_WIN32(GetLastError()); } if (BytesRead < fileInfo.EndOfFile.LowPart) @@ -217,13 +217,13 @@ namespace } // DDS files always start with the same magic number ("DDS ") - uint32_t dwMagicNumber = *( const uint32_t* )( ddsData.get() ); + uint32_t dwMagicNumber = *reinterpret_cast(ddsData.get()); if (dwMagicNumber != DDS_MAGIC) { return E_FAIL; } - auto hdr = reinterpret_cast( ddsData.get() + sizeof( uint32_t ) ); + auto hdr = reinterpret_cast(ddsData.get() + sizeof(uint32_t)); // Verify header to validate DDS file if (hdr->size != sizeof(DDS_HEADER) || @@ -235,10 +235,10 @@ namespace // Check for DX10 extension bool bDXT10Header = false; if ((hdr->ddspf.flags & DDS_FOURCC) && - (MAKEFOURCC( 'D', 'X', '1', '0' ) == hdr->ddspf.fourCC)) + (MAKEFOURCC('D', 'X', '1', '0') == hdr->ddspf.fourCC)) { // Must be long enough for both headers and magic value - if (fileInfo.EndOfFile.LowPart < ( sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10) ) ) + if (fileInfo.EndOfFile.LowPart < (sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10))) { return E_FAIL; } @@ -248,8 +248,8 @@ namespace // setup the pointers in the process request *header = hdr; - ptrdiff_t offset = sizeof( uint32_t ) + sizeof( DDS_HEADER ) - + (bDXT10Header ? sizeof( DDS_HEADER_DXT10 ) : 0); + ptrdiff_t offset = sizeof(uint32_t) + sizeof(DDS_HEADER) + + (bDXT10Header ? sizeof(DDS_HEADER_DXT10) : 0); *bitData = ddsData.get() + offset; *bitSize = fileInfo.EndOfFile.LowPart - offset; @@ -260,9 +260,9 @@ namespace //-------------------------------------------------------------------------------------- // Return the BPP for a particular format //-------------------------------------------------------------------------------------- - size_t BitsPerPixel( _In_ DXGI_FORMAT fmt ) + size_t BitsPerPixel(_In_ DXGI_FORMAT fmt) { - switch( fmt ) + switch (fmt) { case DXGI_FORMAT_R32G32B32A32_TYPELESS: case DXGI_FORMAT_R32G32B32A32_FLOAT: @@ -416,7 +416,7 @@ namespace _In_ DXGI_FORMAT fmt, size_t* outNumBytes, _Out_opt_ size_t* outRowBytes, - _Out_opt_ size_t* outNumRows ) + _Out_opt_ size_t* outNumRows) { size_t numBytes = 0; size_t rowBytes = 0; @@ -434,7 +434,7 @@ namespace case DXGI_FORMAT_BC4_TYPELESS: case DXGI_FORMAT_BC4_UNORM: case DXGI_FORMAT_BC4_SNORM: - bc=true; + bc = true; bpe = 8; break; @@ -488,12 +488,12 @@ namespace size_t numBlocksWide = 0; if (width > 0) { - numBlocksWide = std::max( 1, (width + 3) / 4 ); + numBlocksWide = std::max(1, (width + 3) / 4); } size_t numBlocksHigh = 0; if (height > 0) { - numBlocksHigh = std::max( 1, (height + 3) / 4 ); + numBlocksHigh = std::max(1, (height + 3) / 4); } rowBytes = numBlocksWide * bpe; numRows = numBlocksHigh; @@ -501,26 +501,26 @@ namespace } else if (packed) { - rowBytes = ( ( width + 1 ) >> 1 ) * bpe; + rowBytes = ((width + 1) >> 1) * bpe; numRows = height; numBytes = rowBytes * height; } - else if ( fmt == DXGI_FORMAT_NV11 ) + else if (fmt == DXGI_FORMAT_NV11) { - rowBytes = ( ( width + 3 ) >> 2 ) * 4; + rowBytes = ((width + 3) >> 2) * 4; numRows = height * 2; // Direct3D makes this simplifying assumption, although it is larger than the 4:1:1 data numBytes = rowBytes * numRows; } else if (planar) { - rowBytes = ( ( width + 1 ) >> 1 ) * bpe; - numBytes = ( rowBytes * height ) + ( ( rowBytes * height + 1 ) >> 1 ); - numRows = height + ( ( height + 1 ) >> 1 ); + rowBytes = ((width + 1) >> 1) * bpe; + numBytes = (rowBytes * height) + ((rowBytes * height + 1) >> 1); + numRows = height + ((height + 1) >> 1); } else { - size_t bpp = BitsPerPixel( fmt ); - rowBytes = ( width * bpp + 7 ) / 8; // round up to nearest byte + size_t bpp = BitsPerPixel(fmt); + rowBytes = (width * bpp + 7) / 8; // round up to nearest byte numRows = height; numBytes = rowBytes * height; } @@ -543,7 +543,7 @@ namespace //-------------------------------------------------------------------------------------- #define ISBITMASK( r,g,b,a ) ( ddpf.RBitMask == r && ddpf.GBitMask == g && ddpf.BBitMask == b && ddpf.ABitMask == a ) - DXGI_FORMAT GetDXGIFormat( const DDS_PIXELFORMAT& ddpf ) + DXGI_FORMAT GetDXGIFormat(const DDS_PIXELFORMAT& ddpf) { if (ddpf.flags & DDS_RGB) { @@ -552,17 +552,17 @@ namespace switch (ddpf.RGBBitCount) { case 32: - if (ISBITMASK(0x000000ff,0x0000ff00,0x00ff0000,0xff000000)) + if (ISBITMASK(0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000)) { return DXGI_FORMAT_R8G8B8A8_UNORM; } - if (ISBITMASK(0x00ff0000,0x0000ff00,0x000000ff,0xff000000)) + if (ISBITMASK(0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000)) { return DXGI_FORMAT_B8G8R8A8_UNORM; } - if (ISBITMASK(0x00ff0000,0x0000ff00,0x000000ff,0x00000000)) + if (ISBITMASK(0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000)) { return DXGI_FORMAT_B8G8R8X8_UNORM; } @@ -576,19 +576,19 @@ namespace // header extension and specify the DXGI_FORMAT_R10G10B10A2_UNORM format directly // For 'correct' writers, this should be 0x000003ff,0x000ffc00,0x3ff00000 for RGB data - if (ISBITMASK(0x3ff00000,0x000ffc00,0x000003ff,0xc0000000)) + if (ISBITMASK(0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000)) { return DXGI_FORMAT_R10G10B10A2_UNORM; } // No DXGI format maps to ISBITMASK(0x000003ff,0x000ffc00,0x3ff00000,0xc0000000) aka D3DFMT_A2R10G10B10 - if (ISBITMASK(0x0000ffff,0xffff0000,0x00000000,0x00000000)) + if (ISBITMASK(0x0000ffff, 0xffff0000, 0x00000000, 0x00000000)) { return DXGI_FORMAT_R16G16_UNORM; } - if (ISBITMASK(0xffffffff,0x00000000,0x00000000,0x00000000)) + if (ISBITMASK(0xffffffff, 0x00000000, 0x00000000, 0x00000000)) { // Only 32-bit color channel format in D3D9 was R32F return DXGI_FORMAT_R32_FLOAT; // D3DX writes this out as a FourCC of 114 @@ -600,18 +600,18 @@ namespace break; case 16: - if (ISBITMASK(0x7c00,0x03e0,0x001f,0x8000)) + if (ISBITMASK(0x7c00, 0x03e0, 0x001f, 0x8000)) { return DXGI_FORMAT_B5G5R5A1_UNORM; } - if (ISBITMASK(0xf800,0x07e0,0x001f,0x0000)) + if (ISBITMASK(0xf800, 0x07e0, 0x001f, 0x0000)) { return DXGI_FORMAT_B5G6R5_UNORM; } // No DXGI format maps to ISBITMASK(0x7c00,0x03e0,0x001f,0x0000) aka D3DFMT_X1R5G5B5 - if (ISBITMASK(0x0f00,0x00f0,0x000f,0xf000)) + if (ISBITMASK(0x0f00, 0x00f0, 0x000f, 0xf000)) { return DXGI_FORMAT_B4G4R4A4_UNORM; } @@ -626,7 +626,7 @@ namespace { if (8 == ddpf.RGBBitCount) { - if (ISBITMASK(0x000000ff,0x00000000,0x00000000,0x00000000)) + if (ISBITMASK(0x000000ff, 0x00000000, 0x00000000, 0x00000000)) { return DXGI_FORMAT_R8_UNORM; // D3DX10/11 writes this out as DX10 extension } @@ -636,11 +636,11 @@ namespace if (16 == ddpf.RGBBitCount) { - if (ISBITMASK(0x0000ffff,0x00000000,0x00000000,0x00000000)) + if (ISBITMASK(0x0000ffff, 0x00000000, 0x00000000, 0x00000000)) { return DXGI_FORMAT_R16_UNORM; // D3DX10/11 writes this out as DX10 extension } - if (ISBITMASK(0x000000ff,0x00000000,0x00000000,0x0000ff00)) + if (ISBITMASK(0x000000ff, 0x00000000, 0x00000000, 0x0000ff00)) { return DXGI_FORMAT_R8G8_UNORM; // D3DX10/11 writes this out as DX10 extension } @@ -679,74 +679,74 @@ namespace } else if (ddpf.flags & DDS_FOURCC) { - if (MAKEFOURCC( 'D', 'X', 'T', '1' ) == ddpf.fourCC) + if (MAKEFOURCC('D', 'X', 'T', '1') == ddpf.fourCC) { return DXGI_FORMAT_BC1_UNORM; } - if (MAKEFOURCC( 'D', 'X', 'T', '3' ) == ddpf.fourCC) + if (MAKEFOURCC('D', 'X', 'T', '3') == ddpf.fourCC) { return DXGI_FORMAT_BC2_UNORM; } - if (MAKEFOURCC( 'D', 'X', 'T', '5' ) == ddpf.fourCC) + if (MAKEFOURCC('D', 'X', 'T', '5') == ddpf.fourCC) { return DXGI_FORMAT_BC3_UNORM; } // While pre-multiplied alpha isn't directly supported by the DXGI formats, // they are basically the same as these BC formats so they can be mapped - if (MAKEFOURCC( 'D', 'X', 'T', '2' ) == ddpf.fourCC) + if (MAKEFOURCC('D', 'X', 'T', '2') == ddpf.fourCC) { return DXGI_FORMAT_BC2_UNORM; } - if (MAKEFOURCC( 'D', 'X', 'T', '4' ) == ddpf.fourCC) + if (MAKEFOURCC('D', 'X', 'T', '4') == ddpf.fourCC) { return DXGI_FORMAT_BC3_UNORM; } - if (MAKEFOURCC( 'A', 'T', 'I', '1' ) == ddpf.fourCC) + if (MAKEFOURCC('A', 'T', 'I', '1') == ddpf.fourCC) { return DXGI_FORMAT_BC4_UNORM; } - if (MAKEFOURCC( 'B', 'C', '4', 'U' ) == ddpf.fourCC) + if (MAKEFOURCC('B', 'C', '4', 'U') == ddpf.fourCC) { return DXGI_FORMAT_BC4_UNORM; } - if (MAKEFOURCC( 'B', 'C', '4', 'S' ) == ddpf.fourCC) + if (MAKEFOURCC('B', 'C', '4', 'S') == ddpf.fourCC) { return DXGI_FORMAT_BC4_SNORM; } - if (MAKEFOURCC( 'A', 'T', 'I', '2' ) == ddpf.fourCC) + if (MAKEFOURCC('A', 'T', 'I', '2') == ddpf.fourCC) { return DXGI_FORMAT_BC5_UNORM; } - if (MAKEFOURCC( 'B', 'C', '5', 'U' ) == ddpf.fourCC) + if (MAKEFOURCC('B', 'C', '5', 'U') == ddpf.fourCC) { return DXGI_FORMAT_BC5_UNORM; } - if (MAKEFOURCC( 'B', 'C', '5', 'S' ) == ddpf.fourCC) + if (MAKEFOURCC('B', 'C', '5', 'S') == ddpf.fourCC) { return DXGI_FORMAT_BC5_SNORM; } // BC6H and BC7 are written using the "DX10" extended header - if (MAKEFOURCC( 'R', 'G', 'B', 'G' ) == ddpf.fourCC) + if (MAKEFOURCC('R', 'G', 'B', 'G') == ddpf.fourCC) { return DXGI_FORMAT_R8G8_B8G8_UNORM; } - if (MAKEFOURCC( 'G', 'R', 'G', 'B' ) == ddpf.fourCC) + if (MAKEFOURCC('G', 'R', 'G', 'B') == ddpf.fourCC) { return DXGI_FORMAT_G8R8_G8B8_UNORM; } - if (MAKEFOURCC('Y','U','Y','2') == ddpf.fourCC) + if (MAKEFOURCC('Y', 'U', 'Y', '2') == ddpf.fourCC) { return DXGI_FORMAT_YUY2; } // Check for D3DFORMAT enums being set here - switch( ddpf.fourCC ) + switch (ddpf.fourCC) { case 36: // D3DFMT_A16B16G16R16 return DXGI_FORMAT_R16G16B16A16_UNORM; @@ -779,9 +779,9 @@ namespace //-------------------------------------------------------------------------------------- - DXGI_FORMAT MakeSRGB( _In_ DXGI_FORMAT format ) + DXGI_FORMAT MakeSRGB(_In_ DXGI_FORMAT format) { - switch( format ) + switch (format) { case DXGI_FORMAT_R8G8B8A8_UNORM: return DXGI_FORMAT_R8G8B8A8_UNORM_SRGB; @@ -825,9 +825,9 @@ namespace _Out_ size_t& theight, _Out_ size_t& tdepth, _Out_ size_t& skipMip, - _Out_writes_(mipCount*arraySize) D3D11_SUBRESOURCE_DATA* initData ) + _Out_writes_(mipCount*arraySize) D3D11_SUBRESOURCE_DATA* initData) { - if ( !bitData || !initData ) + if (!bitData || !initData) { return E_POINTER; } @@ -843,24 +843,24 @@ namespace const uint8_t* pEndBits = bitData + bitSize; size_t index = 0; - for( size_t j = 0; j < arraySize; j++ ) + for (size_t j = 0; j < arraySize; j++) { size_t w = width; size_t h = height; size_t d = depth; - for( size_t i = 0; i < mipCount; i++ ) + for (size_t i = 0; i < mipCount; i++) { - GetSurfaceInfo( w, - h, - format, - &NumBytes, - &RowBytes, - nullptr - ); + GetSurfaceInfo(w, + h, + format, + &NumBytes, + &RowBytes, + nullptr + ); - if ( (mipCount <= 1) || !maxsize || (w <= maxsize && h <= maxsize && d <= maxsize) ) + if ((mipCount <= 1) || !maxsize || (w <= maxsize && h <= maxsize && d <= maxsize)) { - if ( !twidth ) + if (!twidth) { twidth = w; theight = h; @@ -869,12 +869,12 @@ namespace assert(index < mipCount * arraySize); _Analysis_assume_(index < mipCount * arraySize); - initData[index].pSysMem = ( const void* )pSrcBits; - initData[index].SysMemPitch = static_cast( RowBytes ); - initData[index].SysMemSlicePitch = static_cast( NumBytes ); + initData[index].pSysMem = (const void*)pSrcBits; + initData[index].SysMemPitch = static_cast(RowBytes); + initData[index].SysMemSlicePitch = static_cast(NumBytes); ++index; } - else if ( !j ) + else if (!j) { // Count number of skipped mipmaps (first item only) ++skipMip; @@ -882,9 +882,9 @@ namespace if (pSrcBits + (NumBytes*d) > pEndBits) { - return HRESULT_FROM_WIN32( ERROR_HANDLE_EOF ); + return HRESULT_FROM_WIN32(ERROR_HANDLE_EOF); } - + pSrcBits += NumBytes * d; w = w >> 1; @@ -927,217 +927,217 @@ namespace _In_ bool isCubeMap, _In_reads_opt_(mipCount*arraySize) D3D11_SUBRESOURCE_DATA* initData, _Outptr_opt_ ID3D11Resource** texture, - _Outptr_opt_ ID3D11ShaderResourceView** textureView ) + _Outptr_opt_ ID3D11ShaderResourceView** textureView) { - if ( !d3dDevice ) + if (!d3dDevice) return E_POINTER; HRESULT hr = E_FAIL; - if ( forceSRGB ) + if (forceSRGB) { - format = MakeSRGB( format ); + format = MakeSRGB(format); } - switch ( resDim ) + switch (resDim) { - case D3D11_RESOURCE_DIMENSION_TEXTURE1D: + case D3D11_RESOURCE_DIMENSION_TEXTURE1D: + { + D3D11_TEXTURE1D_DESC desc; + desc.Width = static_cast(width); + desc.MipLevels = static_cast(mipCount); + desc.ArraySize = static_cast(arraySize); + desc.Format = format; + desc.Usage = usage; + desc.BindFlags = bindFlags; + desc.CPUAccessFlags = cpuAccessFlags; + desc.MiscFlags = miscFlags & ~D3D11_RESOURCE_MISC_TEXTURECUBE; + + ID3D11Texture1D* tex = nullptr; + hr = d3dDevice->CreateTexture1D(&desc, + initData, + &tex + ); + if (SUCCEEDED(hr) && tex != 0) + { + if (textureView != 0) { - D3D11_TEXTURE1D_DESC desc; - desc.Width = static_cast( width ); - desc.MipLevels = static_cast( mipCount ); - desc.ArraySize = static_cast( arraySize ); - desc.Format = format; - desc.Usage = usage; - desc.BindFlags = bindFlags; - desc.CPUAccessFlags = cpuAccessFlags; - desc.MiscFlags = miscFlags & ~D3D11_RESOURCE_MISC_TEXTURECUBE; + D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc = {}; + SRVDesc.Format = format; - ID3D11Texture1D* tex = nullptr; - hr = d3dDevice->CreateTexture1D( &desc, - initData, - &tex - ); - if (SUCCEEDED( hr ) && tex != 0) + if (arraySize > 1) { - if (textureView != 0) - { - D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc = {}; - SRVDesc.Format = format; - - if (arraySize > 1) - { - SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1DARRAY; - SRVDesc.Texture1DArray.MipLevels = (!mipCount) ? -1 : desc.MipLevels; - SRVDesc.Texture1DArray.ArraySize = static_cast( arraySize ); - } - else - { - SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1D; - SRVDesc.Texture1D.MipLevels = (!mipCount) ? -1 : desc.MipLevels; - } - - hr = d3dDevice->CreateShaderResourceView( tex, - &SRVDesc, - textureView - ); - if ( FAILED(hr) ) - { - tex->Release(); - return hr; - } - } - - if (texture != 0) - { - *texture = tex; - } - else - { - SetDebugObjectName(tex, "DDSTextureLoader"); - tex->Release(); - } - } - } - break; - - case D3D11_RESOURCE_DIMENSION_TEXTURE2D: - { - D3D11_TEXTURE2D_DESC desc; - desc.Width = static_cast( width ); - desc.Height = static_cast( height ); - desc.MipLevels = static_cast( mipCount ); - desc.ArraySize = static_cast( arraySize ); - desc.Format = format; - desc.SampleDesc.Count = 1; - desc.SampleDesc.Quality = 0; - desc.Usage = usage; - desc.BindFlags = bindFlags; - desc.CPUAccessFlags = cpuAccessFlags; - if ( isCubeMap ) - { - desc.MiscFlags = miscFlags | D3D11_RESOURCE_MISC_TEXTURECUBE; + SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1DARRAY; + SRVDesc.Texture1DArray.MipLevels = (!mipCount) ? -1 : desc.MipLevels; + SRVDesc.Texture1DArray.ArraySize = static_cast(arraySize); } else { - desc.MiscFlags = miscFlags & ~D3D11_RESOURCE_MISC_TEXTURECUBE; + SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1D; + SRVDesc.Texture1D.MipLevels = (!mipCount) ? -1 : desc.MipLevels; } - ID3D11Texture2D* tex = nullptr; - hr = d3dDevice->CreateTexture2D( &desc, - initData, - &tex - ); - if (SUCCEEDED( hr ) && tex != 0) + hr = d3dDevice->CreateShaderResourceView(tex, + &SRVDesc, + textureView + ); + if (FAILED(hr)) { - if (textureView != 0) - { - D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc = {}; - SRVDesc.Format = format; - - if ( isCubeMap ) - { - if (arraySize > 6) - { - SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBEARRAY; - SRVDesc.TextureCubeArray.MipLevels = (!mipCount) ? -1 : desc.MipLevels; - - // Earlier we set arraySize to (NumCubes * 6) - SRVDesc.TextureCubeArray.NumCubes = static_cast( arraySize / 6 ); - } - else - { - SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE; - SRVDesc.TextureCube.MipLevels = (!mipCount) ? -1 : desc.MipLevels; - } - } - else if (arraySize > 1) - { - SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY; - SRVDesc.Texture2DArray.MipLevels = (!mipCount) ? -1 : desc.MipLevels; - SRVDesc.Texture2DArray.ArraySize = static_cast( arraySize ); - } - else - { - SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; - SRVDesc.Texture2D.MipLevels = (!mipCount) ? -1 : desc.MipLevels; - } - - hr = d3dDevice->CreateShaderResourceView( tex, - &SRVDesc, - textureView - ); - if ( FAILED(hr) ) - { - tex->Release(); - return hr; - } - } - - if (texture != 0) - { - *texture = tex; - } - else - { - SetDebugObjectName(tex, "DDSTextureLoader"); - tex->Release(); - } + tex->Release(); + return hr; } } - break; - case D3D11_RESOURCE_DIMENSION_TEXTURE3D: + if (texture != 0) { - D3D11_TEXTURE3D_DESC desc; - desc.Width = static_cast( width ); - desc.Height = static_cast( height ); - desc.Depth = static_cast( depth ); - desc.MipLevels = static_cast( mipCount ); - desc.Format = format; - desc.Usage = usage; - desc.BindFlags = bindFlags; - desc.CPUAccessFlags = cpuAccessFlags; - desc.MiscFlags = miscFlags & ~D3D11_RESOURCE_MISC_TEXTURECUBE; + *texture = tex; + } + else + { + SetDebugObjectName(tex, "DDSTextureLoader"); + tex->Release(); + } + } + } + break; - ID3D11Texture3D* tex = nullptr; - hr = d3dDevice->CreateTexture3D( &desc, - initData, - &tex - ); - if (SUCCEEDED( hr ) && tex != 0) + case D3D11_RESOURCE_DIMENSION_TEXTURE2D: + { + D3D11_TEXTURE2D_DESC desc; + desc.Width = static_cast(width); + desc.Height = static_cast(height); + desc.MipLevels = static_cast(mipCount); + desc.ArraySize = static_cast(arraySize); + desc.Format = format; + desc.SampleDesc.Count = 1; + desc.SampleDesc.Quality = 0; + desc.Usage = usage; + desc.BindFlags = bindFlags; + desc.CPUAccessFlags = cpuAccessFlags; + if (isCubeMap) + { + desc.MiscFlags = miscFlags | D3D11_RESOURCE_MISC_TEXTURECUBE; + } + else + { + desc.MiscFlags = miscFlags & ~D3D11_RESOURCE_MISC_TEXTURECUBE; + } + + ID3D11Texture2D* tex = nullptr; + hr = d3dDevice->CreateTexture2D(&desc, + initData, + &tex + ); + if (SUCCEEDED(hr) && tex != 0) + { + if (textureView != 0) + { + D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc = {}; + SRVDesc.Format = format; + + if (isCubeMap) { - if (textureView != 0) + if (arraySize > 6) { - D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc = {}; - SRVDesc.Format = format; + SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBEARRAY; + SRVDesc.TextureCubeArray.MipLevels = (!mipCount) ? -1 : desc.MipLevels; - SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D; - SRVDesc.Texture3D.MipLevels = (!mipCount) ? -1 : desc.MipLevels; - - hr = d3dDevice->CreateShaderResourceView( tex, - &SRVDesc, - textureView - ); - if ( FAILED(hr) ) - { - tex->Release(); - return hr; - } - } - - if (texture != 0) - { - *texture = tex; + // Earlier we set arraySize to (NumCubes * 6) + SRVDesc.TextureCubeArray.NumCubes = static_cast(arraySize / 6); } else { - SetDebugObjectName(tex, "DDSTextureLoader"); - tex->Release(); + SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE; + SRVDesc.TextureCube.MipLevels = (!mipCount) ? -1 : desc.MipLevels; } } + else if (arraySize > 1) + { + SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY; + SRVDesc.Texture2DArray.MipLevels = (!mipCount) ? -1 : desc.MipLevels; + SRVDesc.Texture2DArray.ArraySize = static_cast(arraySize); + } + else + { + SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; + SRVDesc.Texture2D.MipLevels = (!mipCount) ? -1 : desc.MipLevels; + } + + hr = d3dDevice->CreateShaderResourceView(tex, + &SRVDesc, + textureView + ); + if (FAILED(hr)) + { + tex->Release(); + return hr; + } } - break; + + if (texture != 0) + { + *texture = tex; + } + else + { + SetDebugObjectName(tex, "DDSTextureLoader"); + tex->Release(); + } + } + } + break; + + case D3D11_RESOURCE_DIMENSION_TEXTURE3D: + { + D3D11_TEXTURE3D_DESC desc; + desc.Width = static_cast(width); + desc.Height = static_cast(height); + desc.Depth = static_cast(depth); + desc.MipLevels = static_cast(mipCount); + desc.Format = format; + desc.Usage = usage; + desc.BindFlags = bindFlags; + desc.CPUAccessFlags = cpuAccessFlags; + desc.MiscFlags = miscFlags & ~D3D11_RESOURCE_MISC_TEXTURECUBE; + + ID3D11Texture3D* tex = nullptr; + hr = d3dDevice->CreateTexture3D(&desc, + initData, + &tex + ); + if (SUCCEEDED(hr) && tex != 0) + { + if (textureView != 0) + { + D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc = {}; + SRVDesc.Format = format; + + SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D; + SRVDesc.Texture3D.MipLevels = (!mipCount) ? -1 : desc.MipLevels; + + hr = d3dDevice->CreateShaderResourceView(tex, + &SRVDesc, + textureView + ); + if (FAILED(hr)) + { + tex->Release(); + return hr; + } + } + + if (texture != 0) + { + *texture = tex; + } + else + { + SetDebugObjectName(tex, "DDSTextureLoader"); + tex->Release(); + } + } + } + break; } return hr; @@ -1158,7 +1158,7 @@ namespace _In_ unsigned int miscFlags, _In_ bool forceSRGB, _Outptr_opt_ ID3D11Resource** texture, - _Outptr_opt_ ID3D11ShaderResourceView** textureView ) + _Outptr_opt_ ID3D11ShaderResourceView** textureView) { HRESULT hr = S_OK; @@ -1178,40 +1178,40 @@ namespace } if ((header->ddspf.flags & DDS_FOURCC) && - (MAKEFOURCC( 'D', 'X', '1', '0' ) == header->ddspf.fourCC )) + (MAKEFOURCC('D', 'X', '1', '0') == header->ddspf.fourCC)) { - auto d3d10ext = reinterpret_cast( (const char*)header + sizeof(DDS_HEADER) ); + auto d3d10ext = reinterpret_cast((const char*)header + sizeof(DDS_HEADER)); arraySize = d3d10ext->arraySize; if (arraySize == 0) { - return HRESULT_FROM_WIN32( ERROR_INVALID_DATA ); + return HRESULT_FROM_WIN32(ERROR_INVALID_DATA); } - switch( d3d10ext->dxgiFormat ) + switch (d3d10ext->dxgiFormat) { case DXGI_FORMAT_AI44: case DXGI_FORMAT_IA44: case DXGI_FORMAT_P8: case DXGI_FORMAT_A8P8: - return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED ); + return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED); default: - if ( BitsPerPixel( d3d10ext->dxgiFormat ) == 0 ) + if (BitsPerPixel(d3d10ext->dxgiFormat) == 0) { - return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED ); + return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED); } } - + format = d3d10ext->dxgiFormat; - switch ( d3d10ext->resourceDimension ) + switch (d3d10ext->resourceDimension) { case D3D11_RESOURCE_DIMENSION_TEXTURE1D: // D3DX writes 1D textures with a fixed Height of 1 if ((header->flags & DDS_HEIGHT) && height != 1) { - return HRESULT_FROM_WIN32( ERROR_INVALID_DATA ); + return HRESULT_FROM_WIN32(ERROR_INVALID_DATA); } height = depth = 1; break; @@ -1228,42 +1228,42 @@ namespace case D3D11_RESOURCE_DIMENSION_TEXTURE3D: if (!(header->flags & DDS_HEADER_FLAGS_VOLUME)) { - return HRESULT_FROM_WIN32( ERROR_INVALID_DATA ); + return HRESULT_FROM_WIN32(ERROR_INVALID_DATA); } if (arraySize > 1) { - return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED ); + return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED); } break; default: - return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED ); + return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED); } resDim = d3d10ext->resourceDimension; } else { - format = GetDXGIFormat( header->ddspf ); + format = GetDXGIFormat(header->ddspf); if (format == DXGI_FORMAT_UNKNOWN) { - return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED ); + return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED); } if (header->flags & DDS_HEADER_FLAGS_VOLUME) { resDim = D3D11_RESOURCE_DIMENSION_TEXTURE3D; } - else + else { if (header->caps2 & DDS_CUBEMAP) { // We require all six faces to be defined - if ((header->caps2 & DDS_CUBEMAP_ALLFACES ) != DDS_CUBEMAP_ALLFACES) + if ((header->caps2 & DDS_CUBEMAP_ALLFACES) != DDS_CUBEMAP_ALLFACES) { - return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED ); + return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED); } arraySize = 6; @@ -1276,41 +1276,41 @@ namespace // Note there's no way for a legacy Direct3D 9 DDS to express a '1D' texture } - assert( BitsPerPixel( format ) != 0 ); + assert(BitsPerPixel(format) != 0); } // Bound sizes (for security purposes we don't trust DDS file metadata larger than the D3D 11.x hardware requirements) if (mipCount > D3D11_REQ_MIP_LEVELS) { - return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED ); + return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED); } - switch ( resDim ) + switch (resDim) { case D3D11_RESOURCE_DIMENSION_TEXTURE1D: if ((arraySize > D3D11_REQ_TEXTURE1D_ARRAY_AXIS_DIMENSION) || - (width > D3D11_REQ_TEXTURE1D_U_DIMENSION) ) + (width > D3D11_REQ_TEXTURE1D_U_DIMENSION)) { - return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED ); + return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED); } break; case D3D11_RESOURCE_DIMENSION_TEXTURE2D: - if ( isCubeMap ) + if (isCubeMap) { // This is the right bound because we set arraySize to (NumCubes*6) above if ((arraySize > D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION) || (width > D3D11_REQ_TEXTURECUBE_DIMENSION) || (height > D3D11_REQ_TEXTURECUBE_DIMENSION)) { - return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED ); + return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED); } } else if ((arraySize > D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION) || - (width > D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION) || - (height > D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION)) + (width > D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION) || + (height > D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION)) { - return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED ); + return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED); } break; @@ -1318,63 +1318,63 @@ namespace if ((arraySize > 1) || (width > D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION) || (height > D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION) || - (depth > D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION) ) + (depth > D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION)) { - return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED ); + return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED); } break; default: - return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED ); + return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED); } bool autogen = false; - if ( mipCount == 1 && d3dContext != 0 && textureView != 0 ) // Must have context and shader-view to auto generate mipmaps + if (mipCount == 1 && d3dContext != 0 && textureView != 0) // Must have context and shader-view to auto generate mipmaps { // See if format is supported for auto-gen mipmaps (varies by feature level) UINT fmtSupport = 0; - hr = d3dDevice->CheckFormatSupport( format, &fmtSupport ); - if ( SUCCEEDED(hr) && ( fmtSupport & D3D11_FORMAT_SUPPORT_MIP_AUTOGEN ) ) + hr = d3dDevice->CheckFormatSupport(format, &fmtSupport); + if (SUCCEEDED(hr) && (fmtSupport & D3D11_FORMAT_SUPPORT_MIP_AUTOGEN)) { // 10level9 feature levels do not support auto-gen mipgen for volume textures - if ( ( resDim != D3D11_RESOURCE_DIMENSION_TEXTURE3D ) - || ( d3dDevice->GetFeatureLevel() >= D3D_FEATURE_LEVEL_10_0 ) ) + if ((resDim != D3D11_RESOURCE_DIMENSION_TEXTURE3D) + || (d3dDevice->GetFeatureLevel() >= D3D_FEATURE_LEVEL_10_0)) { autogen = true; } } } - if ( autogen ) + if (autogen) { // Create texture with auto-generated mipmaps ID3D11Resource* tex = nullptr; - 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 ); - if ( SUCCEEDED(hr) ) + 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); + if (SUCCEEDED(hr)) { size_t numBytes = 0; size_t rowBytes = 0; - GetSurfaceInfo( width, height, format, &numBytes, &rowBytes, nullptr ); + GetSurfaceInfo(width, height, format, &numBytes, &rowBytes, nullptr); - if ( numBytes > bitSize ) + if (numBytes > bitSize) { (*textureView)->Release(); *textureView = nullptr; tex->Release(); - return HRESULT_FROM_WIN32( ERROR_HANDLE_EOF ); + return HRESULT_FROM_WIN32(ERROR_HANDLE_EOF); } D3D11_SHADER_RESOURCE_VIEW_DESC desc; - (*textureView)->GetDesc( &desc ); + (*textureView)->GetDesc(&desc); UINT mipLevels = 1; - switch( desc.ViewDimension ) + switch (desc.ViewDimension) { case D3D_SRV_DIMENSION_TEXTURE1D: mipLevels = desc.Texture1D.MipLevels; break; case D3D_SRV_DIMENSION_TEXTURE1DARRAY: mipLevels = desc.Texture1DArray.MipLevels; break; @@ -1390,33 +1390,33 @@ namespace return E_UNEXPECTED; } - if ( arraySize > 1 ) + if (arraySize > 1) { const uint8_t* pSrcBits = bitData; const uint8_t* pEndBits = bitData + bitSize; - for( UINT item = 0; item < arraySize; ++item ) + for (UINT item = 0; item < arraySize; ++item) { - if ( (pSrcBits + numBytes) > pEndBits ) + if ((pSrcBits + numBytes) > pEndBits) { (*textureView)->Release(); *textureView = nullptr; tex->Release(); - return HRESULT_FROM_WIN32( ERROR_HANDLE_EOF ); + return HRESULT_FROM_WIN32(ERROR_HANDLE_EOF); } - UINT res = D3D11CalcSubresource( 0, item, mipLevels ); - d3dContext->UpdateSubresource( tex, res, nullptr, pSrcBits, static_cast(rowBytes), static_cast(numBytes) ); + UINT res = D3D11CalcSubresource(0, item, mipLevels); + d3dContext->UpdateSubresource(tex, res, nullptr, pSrcBits, static_cast(rowBytes), static_cast(numBytes)); pSrcBits += numBytes; } } else { - d3dContext->UpdateSubresource( tex, 0, nullptr, bitData, static_cast(rowBytes), static_cast(numBytes) ); + d3dContext->UpdateSubresource(tex, 0, nullptr, bitData, static_cast(rowBytes), static_cast(numBytes)); } - d3dContext->GenerateMips( *textureView ); + d3dContext->GenerateMips(*textureView); - if ( texture ) + if (texture) { *texture = tex; } @@ -1429,8 +1429,8 @@ namespace else { // Create the texture - std::unique_ptr initData( new (std::nothrow) D3D11_SUBRESOURCE_DATA[ mipCount * arraySize ] ); - if ( !initData ) + std::unique_ptr initData(new (std::nothrow) D3D11_SUBRESOURCE_DATA[mipCount * arraySize]); + if (!initData) { return E_OUTOFMEMORY; } @@ -1439,54 +1439,54 @@ namespace size_t twidth = 0; size_t theight = 0; size_t tdepth = 0; - hr = FillInitData( width, height, depth, mipCount, arraySize, format, maxsize, bitSize, bitData, - twidth, theight, tdepth, skipMip, initData.get() ); + hr = FillInitData(width, height, depth, mipCount, arraySize, format, maxsize, bitSize, bitData, + twidth, theight, tdepth, skipMip, initData.get()); - if ( SUCCEEDED(hr) ) + 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) ) + if (FAILED(hr) && !maxsize && (mipCount > 1)) { // Retry with a maxsize determined by feature level - switch( d3dDevice->GetFeatureLevel() ) + switch (d3dDevice->GetFeatureLevel()) { case D3D_FEATURE_LEVEL_9_1: case D3D_FEATURE_LEVEL_9_2: - if ( isCubeMap ) + if (isCubeMap) { maxsize = 512 /*D3D_FL9_1_REQ_TEXTURECUBE_DIMENSION*/; } else { maxsize = (resDim == D3D11_RESOURCE_DIMENSION_TEXTURE3D) - ? 256 /*D3D_FL9_1_REQ_TEXTURE3D_U_V_OR_W_DIMENSION*/ - : 2048 /*D3D_FL9_1_REQ_TEXTURE2D_U_OR_V_DIMENSION*/; + ? 256 /*D3D_FL9_1_REQ_TEXTURE3D_U_V_OR_W_DIMENSION*/ + : 2048 /*D3D_FL9_1_REQ_TEXTURE2D_U_OR_V_DIMENSION*/; } break; case D3D_FEATURE_LEVEL_9_3: maxsize = (resDim == D3D11_RESOURCE_DIMENSION_TEXTURE3D) - ? 256 /*D3D_FL9_1_REQ_TEXTURE3D_U_V_OR_W_DIMENSION*/ - : 4096 /*D3D_FL9_3_REQ_TEXTURE2D_U_OR_V_DIMENSION*/; + ? 256 /*D3D_FL9_1_REQ_TEXTURE3D_U_V_OR_W_DIMENSION*/ + : 4096 /*D3D_FL9_3_REQ_TEXTURE2D_U_OR_V_DIMENSION*/; break; default: // D3D_FEATURE_LEVEL_10_0 & D3D_FEATURE_LEVEL_10_1 maxsize = (resDim == D3D11_RESOURCE_DIMENSION_TEXTURE3D) - ? 2048 /*D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION*/ - : 8192 /*D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION*/; + ? 2048 /*D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION*/ + : 8192 /*D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION*/; break; } - hr = FillInitData( width, height, depth, mipCount, arraySize, format, maxsize, bitSize, bitData, - twidth, theight, tdepth, skipMip, initData.get() ); - if ( SUCCEEDED(hr) ) + 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); } } } @@ -1497,15 +1497,15 @@ namespace //-------------------------------------------------------------------------------------- - DDS_ALPHA_MODE GetAlphaMode( _In_ const DDS_HEADER* header ) + DDS_ALPHA_MODE GetAlphaMode(_In_ const DDS_HEADER* header) { - if ( header->ddspf.flags & DDS_FOURCC ) + if (header->ddspf.flags & DDS_FOURCC) { - if ( MAKEFOURCC( 'D', 'X', '1', '0' ) == header->ddspf.fourCC ) + if (MAKEFOURCC('D', 'X', '1', '0') == header->ddspf.fourCC) { - auto d3d10ext = reinterpret_cast( (const char*)header + sizeof(DDS_HEADER) ); - auto mode = static_cast( d3d10ext->miscFlags2 & DDS_MISC_FLAGS2_ALPHA_MODE_MASK ); - switch( mode ) + auto d3d10ext = reinterpret_cast((const char*)header + sizeof(DDS_HEADER)); + auto mode = static_cast(d3d10ext->miscFlags2 & DDS_MISC_FLAGS2_ALPHA_MODE_MASK); + switch (mode) { case DDS_ALPHA_MODE_STRAIGHT: case DDS_ALPHA_MODE_PREMULTIPLIED: @@ -1514,8 +1514,8 @@ namespace return mode; } } - else if ( ( MAKEFOURCC( 'D', 'X', 'T', '2' ) == header->ddspf.fourCC ) - || ( MAKEFOURCC( 'D', 'X', 'T', '4' ) == header->ddspf.fourCC ) ) + else if ((MAKEFOURCC('D', 'X', 'T', '2') == header->ddspf.fourCC) + || (MAKEFOURCC('D', 'X', 'T', '4') == header->ddspf.fourCC)) { return DDS_ALPHA_MODE_PREMULTIPLIED; } @@ -1527,77 +1527,77 @@ namespace //-------------------------------------------------------------------------------------- _Use_decl_annotations_ -HRESULT DirectX::CreateDDSTextureFromMemory( ID3D11Device* d3dDevice, - const uint8_t* ddsData, - size_t ddsDataSize, - ID3D11Resource** texture, - ID3D11ShaderResourceView** textureView, - size_t maxsize, - DDS_ALPHA_MODE* alphaMode ) +HRESULT DirectX::CreateDDSTextureFromMemory(ID3D11Device* d3dDevice, + const uint8_t* ddsData, + size_t ddsDataSize, + ID3D11Resource** texture, + ID3D11ShaderResourceView** textureView, + size_t maxsize, + DDS_ALPHA_MODE* alphaMode) { - return CreateDDSTextureFromMemoryEx( d3dDevice, nullptr, ddsData, ddsDataSize, maxsize, - D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, false, - texture, textureView, alphaMode ); + 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, - ID3D11DeviceContext* d3dContext, - const uint8_t* ddsData, - size_t ddsDataSize, - ID3D11Resource** texture, - ID3D11ShaderResourceView** textureView, - size_t maxsize, - DDS_ALPHA_MODE* alphaMode ) +HRESULT DirectX::CreateDDSTextureFromMemory(ID3D11Device* d3dDevice, + ID3D11DeviceContext* d3dContext, + const uint8_t* ddsData, + size_t ddsDataSize, + ID3D11Resource** texture, + ID3D11ShaderResourceView** textureView, + size_t maxsize, + DDS_ALPHA_MODE* alphaMode) { - return CreateDDSTextureFromMemoryEx( d3dDevice, d3dContext, ddsData, ddsDataSize, maxsize, - D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, false, - texture, textureView, alphaMode ); + 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, - const uint8_t* ddsData, - size_t ddsDataSize, - size_t maxsize, - D3D11_USAGE usage, - unsigned int bindFlags, - unsigned int cpuAccessFlags, - unsigned int miscFlags, - bool forceSRGB, - ID3D11Resource** texture, - ID3D11ShaderResourceView** textureView, - DDS_ALPHA_MODE* alphaMode ) +HRESULT DirectX::CreateDDSTextureFromMemoryEx(ID3D11Device* d3dDevice, + const uint8_t* ddsData, + size_t ddsDataSize, + size_t maxsize, + D3D11_USAGE usage, + unsigned int bindFlags, + unsigned int cpuAccessFlags, + unsigned int miscFlags, + bool forceSRGB, + ID3D11Resource** texture, + ID3D11ShaderResourceView** textureView, + DDS_ALPHA_MODE* alphaMode) { - return CreateDDSTextureFromMemoryEx( d3dDevice, nullptr, ddsData, ddsDataSize, maxsize, - usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB, - texture, textureView, alphaMode ); + return CreateDDSTextureFromMemoryEx(d3dDevice, nullptr, ddsData, ddsDataSize, maxsize, + usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB, + texture, textureView, alphaMode); } _Use_decl_annotations_ -HRESULT DirectX::CreateDDSTextureFromMemoryEx( ID3D11Device* d3dDevice, - ID3D11DeviceContext* d3dContext, - const uint8_t* ddsData, - size_t ddsDataSize, - size_t maxsize, - D3D11_USAGE usage, - unsigned int bindFlags, - unsigned int cpuAccessFlags, - unsigned int miscFlags, - bool forceSRGB, - ID3D11Resource** texture, - ID3D11ShaderResourceView** textureView, - DDS_ALPHA_MODE* alphaMode ) +HRESULT DirectX::CreateDDSTextureFromMemoryEx(ID3D11Device* d3dDevice, + ID3D11DeviceContext* d3dContext, + const uint8_t* ddsData, + size_t ddsDataSize, + size_t maxsize, + D3D11_USAGE usage, + unsigned int bindFlags, + unsigned int cpuAccessFlags, + unsigned int miscFlags, + bool forceSRGB, + ID3D11Resource** texture, + ID3D11ShaderResourceView** textureView, + DDS_ALPHA_MODE* alphaMode) { - if ( texture ) + if (texture) { *texture = nullptr; } - if ( textureView ) + if (textureView) { *textureView = nullptr; } - if ( alphaMode ) + if (alphaMode) { *alphaMode = DDS_ALPHA_MODE_UNKNOWN; } @@ -1613,13 +1613,13 @@ HRESULT DirectX::CreateDDSTextureFromMemoryEx( ID3D11Device* d3dDevice, return E_FAIL; } - uint32_t dwMagicNumber = *( const uint32_t* )( ddsData ); + uint32_t dwMagicNumber = *(const uint32_t*)(ddsData); if (dwMagicNumber != DDS_MAGIC) { return E_FAIL; } - auto header = reinterpret_cast( ddsData + sizeof( uint32_t ) ); + auto header = reinterpret_cast(ddsData + sizeof(uint32_t)); // Verify header to validate DDS file if (header->size != sizeof(DDS_HEADER) || @@ -1631,7 +1631,7 @@ HRESULT DirectX::CreateDDSTextureFromMemoryEx( ID3D11Device* d3dDevice, // Check for DX10 extension bool bDXT10Header = false; if ((header->ddspf.flags & DDS_FOURCC) && - (MAKEFOURCC( 'D', 'X', '1', '0' ) == header->ddspf.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))) @@ -1642,15 +1642,15 @@ HRESULT DirectX::CreateDDSTextureFromMemoryEx( ID3D11Device* d3dDevice, bDXT10Header = true; } - ptrdiff_t offset = sizeof( uint32_t ) - + sizeof( DDS_HEADER ) - + (bDXT10Header ? sizeof( DDS_HEADER_DXT10 ) : 0); + 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, - texture, textureView ); - if ( SUCCEEDED(hr) ) + HRESULT hr = CreateTextureFromDDS(d3dDevice, d3dContext, header, + ddsData + offset, ddsDataSize - offset, maxsize, + usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB, + texture, textureView); + if (SUCCEEDED(hr)) { if (texture != 0 && *texture != 0) { @@ -1662,8 +1662,8 @@ HRESULT DirectX::CreateDDSTextureFromMemoryEx( ID3D11Device* d3dDevice, SetDebugObjectName(*textureView, "DDSTextureLoader"); } - if ( alphaMode ) - *alphaMode = GetAlphaMode( header ); + if (alphaMode) + *alphaMode = GetAlphaMode(header); } return hr; @@ -1671,73 +1671,73 @@ HRESULT DirectX::CreateDDSTextureFromMemoryEx( ID3D11Device* d3dDevice, //-------------------------------------------------------------------------------------- _Use_decl_annotations_ -HRESULT DirectX::CreateDDSTextureFromFile( ID3D11Device* d3dDevice, - const wchar_t* fileName, - ID3D11Resource** texture, - ID3D11ShaderResourceView** textureView, - size_t maxsize, - DDS_ALPHA_MODE* alphaMode ) +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, - texture, textureView, alphaMode ); + 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, - ID3D11DeviceContext* d3dContext, - const wchar_t* fileName, - ID3D11Resource** texture, - ID3D11ShaderResourceView** textureView, - size_t maxsize, - DDS_ALPHA_MODE* alphaMode ) +HRESULT DirectX::CreateDDSTextureFromFile(ID3D11Device* d3dDevice, + ID3D11DeviceContext* d3dContext, + const wchar_t* fileName, + ID3D11Resource** texture, + ID3D11ShaderResourceView** textureView, + size_t maxsize, + DDS_ALPHA_MODE* alphaMode) { - return CreateDDSTextureFromFileEx( d3dDevice, d3dContext, fileName, maxsize, - D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, false, - texture, textureView, alphaMode ); + 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, - const wchar_t* fileName, - size_t maxsize, - D3D11_USAGE usage, - unsigned int bindFlags, - unsigned int cpuAccessFlags, - unsigned int miscFlags, - bool forceSRGB, - ID3D11Resource** texture, - ID3D11ShaderResourceView** textureView, - DDS_ALPHA_MODE* alphaMode ) +HRESULT DirectX::CreateDDSTextureFromFileEx(ID3D11Device* d3dDevice, + const wchar_t* fileName, + size_t maxsize, + D3D11_USAGE usage, + unsigned int bindFlags, + unsigned int cpuAccessFlags, + unsigned int miscFlags, + bool forceSRGB, + ID3D11Resource** texture, + ID3D11ShaderResourceView** textureView, + DDS_ALPHA_MODE* alphaMode) { - return CreateDDSTextureFromFileEx( d3dDevice, nullptr, fileName, maxsize, - usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB, - texture, textureView, alphaMode ); + return CreateDDSTextureFromFileEx(d3dDevice, nullptr, fileName, maxsize, + usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB, + texture, textureView, alphaMode); } _Use_decl_annotations_ -HRESULT DirectX::CreateDDSTextureFromFileEx( ID3D11Device* d3dDevice, - ID3D11DeviceContext* d3dContext, - const wchar_t* fileName, - size_t maxsize, - D3D11_USAGE usage, - unsigned int bindFlags, - unsigned int cpuAccessFlags, - unsigned int miscFlags, - bool forceSRGB, - ID3D11Resource** texture, - ID3D11ShaderResourceView** textureView, - DDS_ALPHA_MODE* alphaMode ) +HRESULT DirectX::CreateDDSTextureFromFileEx(ID3D11Device* d3dDevice, + ID3D11DeviceContext* d3dContext, + const wchar_t* fileName, + size_t maxsize, + D3D11_USAGE usage, + unsigned int bindFlags, + unsigned int cpuAccessFlags, + unsigned int miscFlags, + bool forceSRGB, + ID3D11Resource** texture, + ID3D11ShaderResourceView** textureView, + DDS_ALPHA_MODE* alphaMode) { - if ( texture ) + if (texture) { *texture = nullptr; } - if ( textureView ) + if (textureView) { *textureView = nullptr; } - if ( alphaMode ) + if (alphaMode) { *alphaMode = DDS_ALPHA_MODE_UNKNOWN; } @@ -1747,45 +1747,45 @@ HRESULT DirectX::CreateDDSTextureFromFileEx( ID3D11Device* d3dDevice, return E_INVALIDARG; } - DDS_HEADER* header = nullptr; - uint8_t* bitData = nullptr; + const DDS_HEADER* header = nullptr; + const uint8_t* bitData = nullptr; size_t bitSize = 0; std::unique_ptr ddsData; - HRESULT hr = LoadTextureDataFromFile( fileName, - ddsData, - &header, - &bitData, - &bitSize - ); + HRESULT hr = LoadTextureDataFromFile(fileName, + ddsData, + &header, + &bitData, + &bitSize + ); if (FAILED(hr)) { return hr; } - hr = CreateTextureFromDDS( d3dDevice, d3dContext, header, - bitData, bitSize, maxsize, - usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB, - texture, textureView ); + hr = CreateTextureFromDDS(d3dDevice, d3dContext, header, + bitData, bitSize, maxsize, + usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB, + texture, textureView); - if ( SUCCEEDED(hr) ) + if (SUCCEEDED(hr)) { #if !defined(NO_D3D11_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) ) if (texture != 0 || textureView != 0) { CHAR strFileA[MAX_PATH]; - int result = WideCharToMultiByte( CP_ACP, - WC_NO_BEST_FIT_CHARS, - fileName, - -1, - strFileA, - MAX_PATH, - nullptr, - FALSE - ); - if ( result > 0 ) + int result = WideCharToMultiByte(CP_ACP, + WC_NO_BEST_FIT_CHARS, + fileName, + -1, + strFileA, + MAX_PATH, + nullptr, + FALSE + ); + if (result > 0) { - const CHAR* pstrName = strrchr( strFileA, '\\' ); + const CHAR* pstrName = strrchr(strFileA, '\\'); if (!pstrName) { pstrName = strFileA; @@ -1797,25 +1797,25 @@ HRESULT DirectX::CreateDDSTextureFromFileEx( ID3D11Device* d3dDevice, if (texture != 0 && *texture != 0) { - (*texture)->SetPrivateData( WKPDID_D3DDebugObjectName, - static_cast( strnlen_s(pstrName, MAX_PATH) ), - pstrName - ); + (*texture)->SetPrivateData(WKPDID_D3DDebugObjectName, + static_cast(strnlen_s(pstrName, MAX_PATH)), + pstrName + ); } - if (textureView != 0 && *textureView != 0 ) + if (textureView != 0 && *textureView != 0) { - (*textureView)->SetPrivateData( WKPDID_D3DDebugObjectName, - static_cast( strnlen_s(pstrName, MAX_PATH) ), - pstrName - ); + (*textureView)->SetPrivateData(WKPDID_D3DDebugObjectName, + static_cast(strnlen_s(pstrName, MAX_PATH)), + pstrName + ); } } } #endif - if ( alphaMode ) - *alphaMode = GetAlphaMode( header ); + if (alphaMode) + *alphaMode = GetAlphaMode(header); } return hr; diff --git a/DDSTextureLoader/DDSTextureLoader.h b/DDSTextureLoader/DDSTextureLoader.h index dca85be..b170226 100644 --- a/DDSTextureLoader/DDSTextureLoader.h +++ b/DDSTextureLoader/DDSTextureLoader.h @@ -1,7 +1,7 @@ //-------------------------------------------------------------------------------------- // File: DDSTextureLoader.h // -// Functions for loading a DDS texture and creating a Direct3D 11 runtime resource for it +// Functions for loading a DDS texture and creating a Direct3D runtime resource for it // // Note these functions are useful as a light-weight runtime loader for DDS files. For // a full-featured DDS file reader, writer, and texture processing pipeline see diff --git a/DDSTextureLoader/DDSTextureLoader12.cpp b/DDSTextureLoader/DDSTextureLoader12.cpp index 62b8ac6..3869740 100644 --- a/DDSTextureLoader/DDSTextureLoader12.cpp +++ b/DDSTextureLoader/DDSTextureLoader12.cpp @@ -1,7 +1,7 @@ //-------------------------------------------------------------------------------------- // File: DDSTextureLoader12.cpp // -// Functions for loading a DDS texture and creating a Direct3D 12 runtime resource for it +// Functions for loading a DDS texture and creating a Direct3D runtime resource for it // // Note these functions are useful as a light-weight runtime loader for DDS files. For // a full-featured DDS file reader, writer, and texture processing pipeline see @@ -155,8 +155,8 @@ namespace HRESULT LoadTextureDataFromFile( _In_z_ const wchar_t* fileName, std::unique_ptr& ddsData, - DDS_HEADER** header, - uint8_t** bitData, + const DDS_HEADER** header, + const uint8_t** bitData, size_t* bitSize) { if (!header || !bitData || !bitSize) @@ -165,22 +165,22 @@ namespace } // open the file - ScopedHandle hFile( safe_handle( CreateFile2( fileName, - GENERIC_READ, - FILE_SHARE_READ, - OPEN_EXISTING, - nullptr ) ) ); + ScopedHandle hFile(safe_handle(CreateFile2(fileName, + GENERIC_READ, + FILE_SHARE_READ, + OPEN_EXISTING, + nullptr))); - if ( !hFile ) + if (!hFile) { - return HRESULT_FROM_WIN32( GetLastError() ); + return HRESULT_FROM_WIN32(GetLastError()); } // Get the file size FILE_STANDARD_INFO fileInfo; - if ( !GetFileInformationByHandleEx( hFile.get(), FileStandardInfo, &fileInfo, sizeof(fileInfo) ) ) + if (!GetFileInformationByHandleEx(hFile.get(), FileStandardInfo, &fileInfo, sizeof(fileInfo))) { - return HRESULT_FROM_WIN32( GetLastError() ); + return HRESULT_FROM_WIN32(GetLastError()); } // File is too big for 32-bit allocation, so reject read @@ -190,13 +190,13 @@ 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(DDS_HEADER) + sizeof(uint32_t))) { return E_FAIL; } // create enough space for the file data - ddsData.reset( new (std::nothrow) uint8_t[fileInfo.EndOfFile.LowPart ] ); + ddsData.reset(new (std::nothrow) uint8_t[fileInfo.EndOfFile.LowPart]); if (!ddsData) { return E_OUTOFMEMORY; @@ -204,14 +204,14 @@ namespace // read the data in DWORD BytesRead = 0; - if (!ReadFile( hFile.get(), - ddsData.get(), - fileInfo.EndOfFile.LowPart, - &BytesRead, - nullptr - )) + if (!ReadFile(hFile.get(), + ddsData.get(), + fileInfo.EndOfFile.LowPart, + &BytesRead, + nullptr + )) { - return HRESULT_FROM_WIN32( GetLastError() ); + return HRESULT_FROM_WIN32(GetLastError()); } if (BytesRead < fileInfo.EndOfFile.LowPart) @@ -220,13 +220,13 @@ namespace } // DDS files always start with the same magic number ("DDS ") - uint32_t dwMagicNumber = *( const uint32_t* )( ddsData.get() ); + uint32_t dwMagicNumber = *reinterpret_cast(ddsData.get()); if (dwMagicNumber != DDS_MAGIC) { return E_FAIL; } - auto hdr = reinterpret_cast( ddsData.get() + sizeof( uint32_t ) ); + auto hdr = reinterpret_cast(ddsData.get() + sizeof(uint32_t)); // Verify header to validate DDS file if (hdr->size != sizeof(DDS_HEADER) || @@ -238,10 +238,10 @@ namespace // Check for DX10 extension bool bDXT10Header = false; if ((hdr->ddspf.flags & DDS_FOURCC) && - (MAKEFOURCC( 'D', 'X', '1', '0' ) == hdr->ddspf.fourCC)) + (MAKEFOURCC('D', 'X', '1', '0') == hdr->ddspf.fourCC)) { // Must be long enough for both headers and magic value - if (fileInfo.EndOfFile.LowPart < ( sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10) ) ) + if (fileInfo.EndOfFile.LowPart < (sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10))) { return E_FAIL; } @@ -251,8 +251,8 @@ namespace // setup the pointers in the process request *header = hdr; - ptrdiff_t offset = sizeof( uint32_t ) + sizeof( DDS_HEADER ) - + (bDXT10Header ? sizeof( DDS_HEADER_DXT10 ) : 0); + ptrdiff_t offset = sizeof(uint32_t) + sizeof(DDS_HEADER) + + (bDXT10Header ? sizeof(DDS_HEADER_DXT10) : 0); *bitData = ddsData.get() + offset; *bitSize = fileInfo.EndOfFile.LowPart - offset; @@ -914,15 +914,15 @@ namespace //-------------------------------------------------------------------------------------- HRESULT CreateTextureResource( _In_ ID3D12Device* d3dDevice, - _In_ D3D12_RESOURCE_DIMENSION resDim, - _In_ size_t width, - _In_ size_t height, - _In_ size_t depth, - _In_ size_t mipCount, - _In_ size_t arraySize, - _In_ DXGI_FORMAT format, - _In_ D3D12_RESOURCE_FLAGS flags, - _In_ bool forceSRGB, + D3D12_RESOURCE_DIMENSION resDim, + size_t width, + size_t height, + size_t depth, + size_t mipCount, + size_t arraySize, + DXGI_FORMAT format, + D3D12_RESOURCE_FLAGS resFlags, + unsigned int loadFlags, _Outptr_ ID3D12Resource** texture) { if (!d3dDevice) @@ -930,7 +930,7 @@ namespace HRESULT hr = E_FAIL; - if (forceSRGB) + if (loadFlags & DDS_LOADER_FORCE_SRGB) { format = MakeSRGB(format); } @@ -941,7 +941,7 @@ namespace desc.MipLevels = static_cast(mipCount); desc.DepthOrArraySize = (resDim == D3D12_RESOURCE_DIMENSION_TEXTURE3D) ? static_cast(depth) : static_cast(arraySize); desc.Format = format; - desc.Flags = flags; + desc.Flags = resFlags; desc.SampleDesc.Count = 1; desc.SampleDesc.Quality = 0; desc.Dimension = resDim; @@ -969,13 +969,12 @@ namespace HRESULT CreateTextureFromDDS(_In_ ID3D12Device* d3dDevice, _In_ const DDS_HEADER* header, _In_reads_bytes_(bitSize) const uint8_t* bitData, - _In_ size_t bitSize, - _In_ size_t maxsize, - _In_ D3D12_RESOURCE_FLAGS flags, - _In_ bool forceSRGB, - _In_ bool reserveFullMipChain, + size_t bitSize, + size_t maxsize, + D3D12_RESOURCE_FLAGS resFlags, + unsigned int loadFlags, _Outptr_ ID3D12Resource** texture, - _Out_ std::vector& subresources, + std::vector& subresources, _Out_opt_ bool* outIsCubeMap) { HRESULT hr = S_OK; @@ -1165,13 +1164,13 @@ namespace if (SUCCEEDED(hr)) { size_t reservedMips = mipCount; - if (reserveFullMipChain) + if (loadFlags & DDS_LOADER_MIP_RESERVE) { reservedMips = std::min(D3D12_REQ_MIP_LEVELS, CountMips(width, height)); } hr = CreateTextureResource(d3dDevice, resDim, twidth, theight, tdepth, reservedMips - skipMip, arraySize, - format, flags, forceSRGB, texture); + format, resFlags, loadFlags, texture); if (FAILED(hr) && !maxsize && (mipCount > 1)) { @@ -1184,7 +1183,7 @@ namespace if (SUCCEEDED(hr)) { hr = CreateTextureResource(d3dDevice, resDim, twidth, theight, tdepth, mipCount - skipMip, arraySize, - format, flags, forceSRGB, texture); + format, resFlags, loadFlags, texture); } } } @@ -1224,7 +1223,6 @@ namespace return DDS_ALPHA_MODE_UNKNOWN; } - } // anonymous namespace @@ -1241,43 +1239,41 @@ HRESULT DirectX::LoadDDSTextureFromMemory( bool* isCubeMap) { return LoadDDSTextureFromMemoryEx( - d3dDevice, - ddsData, - ddsDataSize, - maxsize, - D3D12_RESOURCE_FLAG_NONE, - false, - false, - texture, - subresources, + d3dDevice, + ddsData, + ddsDataSize, + maxsize, + D3D12_RESOURCE_FLAG_NONE, + DDS_LOADER_DEFAULT, + texture, + subresources, alphaMode, isCubeMap); } _Use_decl_annotations_ -HRESULT DirectX::LoadDDSTextureFromMemoryEx( +HRESULT DirectX::LoadDDSTextureFromMemoryEx( ID3D12Device* d3dDevice, const uint8_t* ddsData, size_t ddsDataSize, size_t maxsize, - D3D12_RESOURCE_FLAGS flags, - bool forceSRGB, - bool reserveFullMipChain, + D3D12_RESOURCE_FLAGS resFlags, + unsigned int loadFlags, ID3D12Resource** texture, std::vector& subresources, DDS_ALPHA_MODE* alphaMode, - bool* isCubeMap ) + bool* isCubeMap) { - if ( texture ) + if (texture) { *texture = nullptr; } - if ( alphaMode ) + if (alphaMode) { *alphaMode = DDS_ALPHA_MODE_UNKNOWN; } - if ( isCubeMap ) + if (isCubeMap) { *isCubeMap = false; } @@ -1293,13 +1289,13 @@ HRESULT DirectX::LoadDDSTextureFromMemoryEx( return E_FAIL; } - uint32_t dwMagicNumber = *( const uint32_t* )( ddsData ); + uint32_t dwMagicNumber = *(const uint32_t*)(ddsData); if (dwMagicNumber != DDS_MAGIC) { return E_FAIL; } - auto header = reinterpret_cast( ddsData + sizeof( uint32_t ) ); + auto header = reinterpret_cast(ddsData + sizeof(uint32_t)); // Verify header to validate DDS file if (header->size != sizeof(DDS_HEADER) || @@ -1311,7 +1307,7 @@ HRESULT DirectX::LoadDDSTextureFromMemoryEx( // Check for DX10 extension bool bDXT10Header = false; if ((header->ddspf.flags & DDS_FOURCC) && - (MAKEFOURCC( 'D', 'X', '1', '0' ) == header->ddspf.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))) @@ -1322,23 +1318,23 @@ HRESULT DirectX::LoadDDSTextureFromMemoryEx( bDXT10Header = true; } - ptrdiff_t offset = sizeof( uint32_t ) - + sizeof( DDS_HEADER ) - + (bDXT10Header ? sizeof( DDS_HEADER_DXT10 ) : 0); + 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, - flags, forceSRGB, reserveFullMipChain, - texture, subresources, isCubeMap ); - if ( SUCCEEDED(hr) ) + HRESULT hr = CreateTextureFromDDS(d3dDevice, + header, ddsData + offset, ddsDataSize - offset, maxsize, + resFlags, loadFlags, + texture, subresources, isCubeMap); + if (SUCCEEDED(hr)) { if (texture != 0 && *texture != 0) { SetDebugObjectName(*texture, L"DDSTextureLoader"); } - if ( alphaMode ) - *alphaMode = GetAlphaMode( header ); + if (alphaMode) + *alphaMode = GetAlphaMode(header); } return hr; @@ -1347,7 +1343,7 @@ HRESULT DirectX::LoadDDSTextureFromMemoryEx( //-------------------------------------------------------------------------------------- _Use_decl_annotations_ -HRESULT DirectX::LoadDDSTextureFromFile( +HRESULT DirectX::LoadDDSTextureFromFile( ID3D12Device* d3dDevice, const wchar_t* fileName, ID3D12Resource** texture, @@ -1355,20 +1351,19 @@ HRESULT DirectX::LoadDDSTextureFromFile( std::vector& subresources, size_t maxsize, DDS_ALPHA_MODE* alphaMode, - bool* isCubeMap ) + bool* isCubeMap) { - return LoadDDSTextureFromFileEx( - d3dDevice, - fileName, - maxsize, - D3D12_RESOURCE_FLAG_NONE, - false, - false, + return LoadDDSTextureFromFileEx( + d3dDevice, + fileName, + maxsize, + D3D12_RESOURCE_FLAG_NONE, + DDS_LOADER_DEFAULT, texture, ddsData, - subresources, + subresources, alphaMode, - isCubeMap ); + isCubeMap); } _Use_decl_annotations_ @@ -1376,24 +1371,23 @@ HRESULT DirectX::LoadDDSTextureFromFileEx( ID3D12Device* d3dDevice, const wchar_t* fileName, size_t maxsize, - D3D12_RESOURCE_FLAGS flags, - bool forceSRGB, - bool reserveFullMipChain, + D3D12_RESOURCE_FLAGS resFlags, + unsigned int loadFlags, ID3D12Resource** texture, std::unique_ptr& ddsData, std::vector& subresources, DDS_ALPHA_MODE* alphaMode, - bool* isCubeMap ) + bool* isCubeMap) { - if ( texture ) + if (texture) { *texture = nullptr; } - if ( alphaMode ) + if (alphaMode) { *alphaMode = DDS_ALPHA_MODE_UNKNOWN; } - if ( isCubeMap ) + if (isCubeMap) { *isCubeMap = false; } @@ -1403,42 +1397,42 @@ HRESULT DirectX::LoadDDSTextureFromFileEx( return E_INVALIDARG; } - DDS_HEADER* header = nullptr; - uint8_t* bitData = nullptr; + const DDS_HEADER* header = nullptr; + const uint8_t* bitData = nullptr; size_t bitSize = 0; - HRESULT hr = LoadTextureDataFromFile( fileName, - ddsData, - &header, - &bitData, - &bitSize - ); + HRESULT hr = LoadTextureDataFromFile(fileName, + ddsData, + &header, + &bitData, + &bitSize + ); if (FAILED(hr)) { return hr; } - hr = CreateTextureFromDDS( d3dDevice, - header, bitData, bitSize, maxsize, - flags, forceSRGB, reserveFullMipChain, - texture, subresources, isCubeMap ); + hr = CreateTextureFromDDS(d3dDevice, + header, bitData, bitSize, maxsize, + resFlags, loadFlags, + texture, subresources, isCubeMap); - if ( SUCCEEDED(hr) ) + if (SUCCEEDED(hr)) { #if !defined(NO_D3D12_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) ) if (texture != 0) { CHAR strFileA[MAX_PATH]; - int result = WideCharToMultiByte( CP_ACP, - WC_NO_BEST_FIT_CHARS, - fileName, - -1, - strFileA, - MAX_PATH, - nullptr, - FALSE - ); - if ( result > 0 ) + int result = WideCharToMultiByte(CP_ACP, + WC_NO_BEST_FIT_CHARS, + fileName, + -1, + strFileA, + MAX_PATH, + nullptr, + FALSE + ); + if (result > 0) { const wchar_t* pstrName = wcsrchr(fileName, '\\'); if (!pstrName) @@ -1458,8 +1452,8 @@ HRESULT DirectX::LoadDDSTextureFromFileEx( } #endif - if ( alphaMode ) - *alphaMode = GetAlphaMode( header ); + if (alphaMode) + *alphaMode = GetAlphaMode(header); } return hr; diff --git a/DDSTextureLoader/DDSTextureLoader12.h b/DDSTextureLoader/DDSTextureLoader12.h index 31f99ce..30d95f9 100644 --- a/DDSTextureLoader/DDSTextureLoader12.h +++ b/DDSTextureLoader/DDSTextureLoader12.h @@ -1,7 +1,7 @@ //-------------------------------------------------------------------------------------- // File: DDSTextureLoader12.h // -// Functions for loading a DDS texture and creating a Direct3D 12 runtime resource for it +// Functions for loading a DDS texture and creating a Direct3D runtime resource for it // // Note these functions are useful as a light-weight runtime loader for DDS files. For // a full-featured DDS file reader, writer, and texture processing pipeline see @@ -38,6 +38,13 @@ namespace DirectX DDS_ALPHA_MODE_CUSTOM = 4, }; + enum DDS_LOADER_FLAGS + { + DDS_LOADER_DEFAULT = 0, + DDS_LOADER_FORCE_SRGB = 0x1, + DDS_LOADER_MIP_RESERVE = 0x8, + }; + // Standard version HRESULT __cdecl LoadDDSTextureFromMemory( _In_ ID3D12Device* d3dDevice, @@ -65,9 +72,8 @@ namespace DirectX _In_reads_bytes_(ddsDataSize) const uint8_t* ddsData, size_t ddsDataSize, size_t maxsize, - D3D12_RESOURCE_FLAGS flags, - bool forceSRGB, - bool reserveFullMipChain, + D3D12_RESOURCE_FLAGS resFlags, + unsigned int loadFlags, _Outptr_ ID3D12Resource** texture, std::vector& subresources, _Out_opt_ DDS_ALPHA_MODE* alphaMode = nullptr, @@ -77,9 +83,8 @@ namespace DirectX _In_ ID3D12Device* d3dDevice, _In_z_ const wchar_t* szFileName, size_t maxsize, - D3D12_RESOURCE_FLAGS flags, - bool forceSRGB, - bool reserveFullMipChain, + D3D12_RESOURCE_FLAGS resFlags, + unsigned int loadFlags, _Outptr_ ID3D12Resource** texture, std::unique_ptr& ddsData, std::vector& subresources, diff --git a/WICTextureLoader/WICTextureLoader.cpp b/WICTextureLoader/WICTextureLoader.cpp index 7f51cf4..9038b23 100644 --- a/WICTextureLoader/WICTextureLoader.cpp +++ b/WICTextureLoader/WICTextureLoader.cpp @@ -1,7 +1,7 @@ //-------------------------------------------------------------------------------------- // File: WICTextureLoader.cpp // -// Function for loading a WIC image and creating a Direct3D 11 runtime texture for it +// Function for loading a WIC image and creating a Direct3D runtime texture for it // (auto-generating mipmaps if possible) // // Note: Assumes application has already called CoInitializeEx @@ -43,20 +43,21 @@ #pragma comment(lib,"dxguid.lib") #endif +using namespace DirectX; using Microsoft::WRL::ComPtr; namespace { //-------------------------------------------------------------------------------------- template - inline void SetDebugObjectName(_In_ ID3D11DeviceChild* resource, _In_ const char (&name)[TNameLength]) + inline void SetDebugObjectName(_In_ ID3D11DeviceChild* resource, _In_ const char(&name)[TNameLength]) { - #if !defined(NO_D3D11_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) ) +#if !defined(NO_D3D11_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) ) resource->SetPrivateData(WKPDID_D3DDebugObjectName, TNameLength - 1, name); - #else +#else UNREFERENCED_PARAMETER(resource); UNREFERENCED_PARAMETER(name); - #endif +#endif } //------------------------------------------------------------------------------------- @@ -173,90 +174,90 @@ namespace IWICImagingFactory* factory = nullptr; InitOnceExecuteOnce(&s_initOnce, [](PINIT_ONCE, PVOID, PVOID *factory) -> BOOL - { - #if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) || defined(_WIN7_PLATFORM_UPDATE) - HRESULT hr = CoCreateInstance( - CLSID_WICImagingFactory2, - nullptr, - CLSCTX_INPROC_SERVER, - __uuidof(IWICImagingFactory2), - factory - ); + { +#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) || defined(_WIN7_PLATFORM_UPDATE) + HRESULT hr = CoCreateInstance( + CLSID_WICImagingFactory2, + nullptr, + CLSCTX_INPROC_SERVER, + __uuidof(IWICImagingFactory2), + factory + ); - if ( SUCCEEDED(hr) ) - { - // WIC2 is available on Windows 10, Windows 8.x, and Windows 7 SP1 with KB 2670838 installed - g_WIC2 = true; - return TRUE; - } - else - { - hr = CoCreateInstance( - CLSID_WICImagingFactory1, - nullptr, - CLSCTX_INPROC_SERVER, - __uuidof(IWICImagingFactory), - factory - ); - return SUCCEEDED(hr) ? TRUE : FALSE; - } - #else - return SUCCEEDED( CoCreateInstance( - CLSID_WICImagingFactory, + if (SUCCEEDED(hr)) + { + // WIC2 is available on Windows 10, Windows 8.x, and Windows 7 SP1 with KB 2670838 installed + g_WIC2 = true; + return TRUE; + } + else + { + hr = CoCreateInstance( + CLSID_WICImagingFactory1, nullptr, CLSCTX_INPROC_SERVER, __uuidof(IWICImagingFactory), - factory) ) ? TRUE : FALSE; - #endif - }, nullptr, reinterpret_cast(&factory)); + factory + ); + return SUCCEEDED(hr) ? TRUE : FALSE; + } +#else + return SUCCEEDED(CoCreateInstance( + CLSID_WICImagingFactory, + nullptr, + CLSCTX_INPROC_SERVER, + __uuidof(IWICImagingFactory), + factory)) ? TRUE : FALSE; +#endif + }, nullptr, reinterpret_cast(&factory)); return factory; } //--------------------------------------------------------------------------------- - DXGI_FORMAT _WICToDXGI( const GUID& guid ) + DXGI_FORMAT _WICToDXGI(const GUID& guid) { - for( size_t i=0; i < _countof(g_WICFormats); ++i ) + for (size_t i = 0; i < _countof(g_WICFormats); ++i) { - if ( memcmp( &g_WICFormats[i].wic, &guid, sizeof(GUID) ) == 0 ) + if (memcmp(&g_WICFormats[i].wic, &guid, sizeof(GUID)) == 0) return g_WICFormats[i].format; } - #if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) || defined(_WIN7_PLATFORM_UPDATE) - if ( g_WIC2 ) +#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) || defined(_WIN7_PLATFORM_UPDATE) + if (g_WIC2) { - if ( memcmp( &GUID_WICPixelFormat96bppRGBFloat, &guid, sizeof(GUID) ) == 0 ) + if (memcmp(&GUID_WICPixelFormat96bppRGBFloat, &guid, sizeof(GUID)) == 0) return DXGI_FORMAT_R32G32B32_FLOAT; } - #endif +#endif return DXGI_FORMAT_UNKNOWN; } //--------------------------------------------------------------------------------- - size_t _WICBitsPerPixel( REFGUID targetGuid ) + size_t _WICBitsPerPixel(REFGUID targetGuid) { - IWICImagingFactory* pWIC = _GetWIC(); - if ( !pWIC ) + auto pWIC = _GetWIC(); + if (!pWIC) return 0; - + ComPtr cinfo; - if ( FAILED( pWIC->CreateComponentInfo( targetGuid, cinfo.GetAddressOf() ) ) ) + if (FAILED(pWIC->CreateComponentInfo(targetGuid, cinfo.GetAddressOf()))) return 0; WICComponentType type; - if ( FAILED( cinfo->GetComponentType( &type ) ) ) + if (FAILED(cinfo->GetComponentType(&type))) return 0; - if ( type != WICPixelFormat ) + if (type != WICPixelFormat) return 0; ComPtr pfinfo; - if ( FAILED( cinfo.As( &pfinfo ) ) ) + if (FAILED(cinfo.As(&pfinfo))) return 0; UINT bpp; - if ( FAILED( pfinfo->GetBitsPerPixel( &bpp ) ) ) + if (FAILED(pfinfo->GetBitsPerPixel(&bpp))) return 0; return bpp; @@ -264,9 +265,9 @@ namespace //-------------------------------------------------------------------------------------- - DXGI_FORMAT MakeSRGB( _In_ DXGI_FORMAT format ) + DXGI_FORMAT MakeSRGB(_In_ DXGI_FORMAT format) { - switch( format ) + switch (format) { case DXGI_FORMAT_R8G8B8A8_UNORM: return DXGI_FORMAT_R8G8B8A8_UNORM_SRGB; @@ -296,8 +297,7 @@ namespace //--------------------------------------------------------------------------------- - HRESULT CreateTextureFromWIC( - _In_ ID3D11Device* d3dDevice, + HRESULT CreateTextureFromWIC(_In_ ID3D11Device* d3dDevice, _In_opt_ ID3D11DeviceContext* d3dContext, _In_ IWICBitmapFrameDecode *frame, _In_ size_t maxsize, @@ -305,62 +305,62 @@ namespace _In_ unsigned int bindFlags, _In_ unsigned int cpuAccessFlags, _In_ unsigned int miscFlags, - _In_ bool forceSRGB, - _Out_opt_ ID3D11Resource** texture, - _Out_opt_ ID3D11ShaderResourceView** textureView ) + _In_ unsigned int loadFlags, + _Outptr_opt_ ID3D11Resource** texture, + _Outptr_opt_ ID3D11ShaderResourceView** textureView) { UINT width, height; - HRESULT hr = frame->GetSize( &width, &height ); - if ( FAILED(hr) ) + HRESULT hr = frame->GetSize(&width, &height); + if (FAILED(hr)) return hr; - assert( width > 0 && height > 0 ); + assert(width > 0 && height > 0); - if ( !maxsize ) + if (!maxsize) { // This is a bit conservative because the hardware could support larger textures than // the Feature Level defined minimums, but doing it this way is much easier and more // performant for WIC than the 'fail and retry' model used by DDSTextureLoader - switch( d3dDevice->GetFeatureLevel() ) + switch (d3dDevice->GetFeatureLevel()) { - case D3D_FEATURE_LEVEL_9_1: - case D3D_FEATURE_LEVEL_9_2: - maxsize = 2048 /*D3D_FL9_1_REQ_TEXTURE2D_U_OR_V_DIMENSION*/; - break; + case D3D_FEATURE_LEVEL_9_1: + case D3D_FEATURE_LEVEL_9_2: + maxsize = 2048 /*D3D_FL9_1_REQ_TEXTURE2D_U_OR_V_DIMENSION*/; + break; - case D3D_FEATURE_LEVEL_9_3: - maxsize = 4096 /*D3D_FL9_3_REQ_TEXTURE2D_U_OR_V_DIMENSION*/; - break; + case D3D_FEATURE_LEVEL_9_3: + maxsize = 4096 /*D3D_FL9_3_REQ_TEXTURE2D_U_OR_V_DIMENSION*/; + break; - case D3D_FEATURE_LEVEL_10_0: - case D3D_FEATURE_LEVEL_10_1: - maxsize = 8192 /*D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION*/; - break; + case D3D_FEATURE_LEVEL_10_0: + case D3D_FEATURE_LEVEL_10_1: + maxsize = 8192 /*D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION*/; + break; - default: - maxsize = D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; - break; + default: + maxsize = D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; + break; } } - assert( maxsize > 0 ); + assert(maxsize > 0); UINT twidth, theight; - if ( width > maxsize || height > maxsize ) + if (width > maxsize || height > maxsize) { float ar = static_cast(height) / static_cast(width); - if ( width > height ) + if (width > height) { - twidth = static_cast( maxsize ); - theight = static_cast( static_cast(maxsize) * ar ); + twidth = static_cast(maxsize); + theight = static_cast(static_cast(maxsize) * ar); } else { - theight = static_cast( maxsize ); - twidth = static_cast( static_cast(maxsize) / ar ); + theight = static_cast(maxsize); + twidth = static_cast(static_cast(maxsize) / ar); } - assert( twidth <= maxsize && theight <= maxsize ); + assert(twidth <= maxsize && theight <= maxsize); } else { @@ -370,112 +370,112 @@ namespace // Determine format WICPixelFormatGUID pixelFormat; - hr = frame->GetPixelFormat( &pixelFormat ); - if ( FAILED(hr) ) + hr = frame->GetPixelFormat(&pixelFormat); + if (FAILED(hr)) return hr; WICPixelFormatGUID convertGUID; - memcpy( &convertGUID, &pixelFormat, sizeof(WICPixelFormatGUID) ); + memcpy(&convertGUID, &pixelFormat, sizeof(WICPixelFormatGUID)); size_t bpp = 0; - DXGI_FORMAT format = _WICToDXGI( pixelFormat ); - if ( format == DXGI_FORMAT_UNKNOWN ) + DXGI_FORMAT format = _WICToDXGI(pixelFormat); + if (format == DXGI_FORMAT_UNKNOWN) { - if ( memcmp( &GUID_WICPixelFormat96bppRGBFixedPoint, &pixelFormat, sizeof(WICPixelFormatGUID) ) == 0 ) + if (memcmp(&GUID_WICPixelFormat96bppRGBFixedPoint, &pixelFormat, sizeof(WICPixelFormatGUID)) == 0) { - #if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) || defined(_WIN7_PLATFORM_UPDATE) - if ( g_WIC2 ) +#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) || defined(_WIN7_PLATFORM_UPDATE) + if (g_WIC2) { - memcpy( &convertGUID, &GUID_WICPixelFormat96bppRGBFloat, sizeof(WICPixelFormatGUID) ); + memcpy(&convertGUID, &GUID_WICPixelFormat96bppRGBFloat, sizeof(WICPixelFormatGUID)); format = DXGI_FORMAT_R32G32B32_FLOAT; } else - #endif +#endif { - memcpy( &convertGUID, &GUID_WICPixelFormat128bppRGBAFloat, sizeof(WICPixelFormatGUID) ); + memcpy(&convertGUID, &GUID_WICPixelFormat128bppRGBAFloat, sizeof(WICPixelFormatGUID)); format = DXGI_FORMAT_R32G32B32A32_FLOAT; } } else { - for( size_t i=0; i < _countof(g_WICConvert); ++i ) + for (size_t i = 0; i < _countof(g_WICConvert); ++i) { - if ( memcmp( &g_WICConvert[i].source, &pixelFormat, sizeof(WICPixelFormatGUID) ) == 0 ) + if (memcmp(&g_WICConvert[i].source, &pixelFormat, sizeof(WICPixelFormatGUID)) == 0) { - memcpy( &convertGUID, &g_WICConvert[i].target, sizeof(WICPixelFormatGUID) ); + memcpy(&convertGUID, &g_WICConvert[i].target, sizeof(WICPixelFormatGUID)); - format = _WICToDXGI( g_WICConvert[i].target ); - assert( format != DXGI_FORMAT_UNKNOWN ); - bpp = _WICBitsPerPixel( convertGUID ); + format = _WICToDXGI(g_WICConvert[i].target); + assert(format != DXGI_FORMAT_UNKNOWN); + bpp = _WICBitsPerPixel(convertGUID); break; } } } - if ( format == DXGI_FORMAT_UNKNOWN ) - return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED ); + if (format == DXGI_FORMAT_UNKNOWN) + return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED); } else { - bpp = _WICBitsPerPixel( pixelFormat ); + bpp = _WICBitsPerPixel(pixelFormat); } - #if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) || defined(_WIN7_PLATFORM_UPDATE) - if ( (format == DXGI_FORMAT_R32G32B32_FLOAT) && d3dContext != 0 && textureView != 0 ) +#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) || defined(_WIN7_PLATFORM_UPDATE) + if ((format == DXGI_FORMAT_R32G32B32_FLOAT) && d3dContext != 0 && textureView != 0) { // Special case test for optional device support for autogen mipchains for R32G32B32_FLOAT UINT fmtSupport = 0; - hr = d3dDevice->CheckFormatSupport( DXGI_FORMAT_R32G32B32_FLOAT, &fmtSupport ); - if ( FAILED(hr) || !( fmtSupport & D3D11_FORMAT_SUPPORT_MIP_AUTOGEN ) ) + hr = d3dDevice->CheckFormatSupport(DXGI_FORMAT_R32G32B32_FLOAT, &fmtSupport); + if (FAILED(hr) || !(fmtSupport & D3D11_FORMAT_SUPPORT_MIP_AUTOGEN)) { // Use R32G32B32A32_FLOAT instead which is required for Feature Level 10.0 and up - memcpy( &convertGUID, &GUID_WICPixelFormat128bppRGBAFloat, sizeof(WICPixelFormatGUID) ); + memcpy(&convertGUID, &GUID_WICPixelFormat128bppRGBAFloat, sizeof(WICPixelFormatGUID)); format = DXGI_FORMAT_R32G32B32A32_FLOAT; bpp = 128; } } - #endif +#endif - if ( !bpp ) + if (!bpp) return E_FAIL; // Handle sRGB formats - if ( forceSRGB ) + if (loadFlags & WIC_LOADER_FORCE_SRGB) { - format = MakeSRGB( format ); + format = MakeSRGB(format); } - else + else if (!(loadFlags & WIC_LOADER_IGNORE_SRGB)) { ComPtr metareader; - if ( SUCCEEDED( frame->GetMetadataQueryReader( metareader.GetAddressOf() ) ) ) + if (SUCCEEDED(frame->GetMetadataQueryReader(metareader.GetAddressOf()))) { GUID containerFormat; - if ( SUCCEEDED( metareader->GetContainerFormat( &containerFormat ) ) ) + if (SUCCEEDED(metareader->GetContainerFormat(&containerFormat))) { // Check for sRGB colorspace metadata bool sRGB = false; PROPVARIANT value; - PropVariantInit( &value ); + PropVariantInit(&value); - if ( memcmp( &containerFormat, &GUID_ContainerFormatPng, sizeof(GUID) ) == 0 ) + if (memcmp(&containerFormat, &GUID_ContainerFormatPng, sizeof(GUID)) == 0) { // Check for sRGB chunk - if ( SUCCEEDED( metareader->GetMetadataByName( L"/sRGB/RenderingIntent", &value ) ) && value.vt == VT_UI1 ) + if (SUCCEEDED(metareader->GetMetadataByName(L"/sRGB/RenderingIntent", &value)) && value.vt == VT_UI1) { sRGB = true; } } - else if ( SUCCEEDED( metareader->GetMetadataByName( L"System.Image.ColorSpace", &value ) ) && value.vt == VT_UI2 && value.uiVal == 1 ) + else if (SUCCEEDED(metareader->GetMetadataByName(L"System.Image.ColorSpace", &value)) && value.vt == VT_UI2 && value.uiVal == 1) { sRGB = true; } - PropVariantClear( &value ); + PropVariantClear(&value); - if ( sRGB ) - format = MakeSRGB( format ); + if (sRGB) + format = MakeSRGB(format); } } } @@ -483,119 +483,119 @@ namespace // Verify our target format is supported by the current device // (handles WDDM 1.0 or WDDM 1.1 device driver cases as well as DirectX 11.0 Runtime without 16bpp format support) UINT support = 0; - hr = d3dDevice->CheckFormatSupport( format, &support ); - if ( FAILED(hr) || !(support & D3D11_FORMAT_SUPPORT_TEXTURE2D) ) + hr = d3dDevice->CheckFormatSupport(format, &support); + if (FAILED(hr) || !(support & D3D11_FORMAT_SUPPORT_TEXTURE2D)) { // Fallback to RGBA 32-bit format which is supported by all devices - memcpy( &convertGUID, &GUID_WICPixelFormat32bppRGBA, sizeof(WICPixelFormatGUID) ); + memcpy(&convertGUID, &GUID_WICPixelFormat32bppRGBA, sizeof(WICPixelFormatGUID)); format = DXGI_FORMAT_R8G8B8A8_UNORM; bpp = 32; } // Allocate temporary memory for image - size_t rowPitch = ( twidth * bpp + 7 ) / 8; + size_t rowPitch = (twidth * bpp + 7) / 8; size_t imageSize = rowPitch * theight; - std::unique_ptr temp( new (std::nothrow) uint8_t[ imageSize ] ); + std::unique_ptr temp(new (std::nothrow) uint8_t[imageSize]); if (!temp) return E_OUTOFMEMORY; // Load image data - if ( memcmp( &convertGUID, &pixelFormat, sizeof(GUID) ) == 0 - && twidth == width - && theight == height ) + if (memcmp(&convertGUID, &pixelFormat, sizeof(GUID)) == 0 + && twidth == width + && theight == height) { // No format conversion or resize needed - hr = frame->CopyPixels( 0, static_cast( rowPitch ), static_cast( imageSize ), temp.get() ); - if ( FAILED(hr) ) + hr = frame->CopyPixels(0, static_cast(rowPitch), static_cast(imageSize), temp.get()); + if (FAILED(hr)) return hr; } - else if ( twidth != width || theight != height ) + else if (twidth != width || theight != height) { // Resize - IWICImagingFactory* pWIC = _GetWIC(); - if ( !pWIC ) + auto pWIC = _GetWIC(); + if (!pWIC) return E_NOINTERFACE; ComPtr scaler; - hr = pWIC->CreateBitmapScaler( scaler.GetAddressOf() ); - if ( FAILED(hr) ) + hr = pWIC->CreateBitmapScaler(scaler.GetAddressOf()); + if (FAILED(hr)) return hr; - hr = scaler->Initialize( frame, twidth, theight, WICBitmapInterpolationModeFant ); - if ( FAILED(hr) ) + hr = scaler->Initialize(frame, twidth, theight, WICBitmapInterpolationModeFant); + if (FAILED(hr)) return hr; WICPixelFormatGUID pfScaler; - hr = scaler->GetPixelFormat( &pfScaler ); - if ( FAILED(hr) ) + hr = scaler->GetPixelFormat(&pfScaler); + if (FAILED(hr)) return hr; - if ( memcmp( &convertGUID, &pfScaler, sizeof(GUID) ) == 0 ) + if (memcmp(&convertGUID, &pfScaler, sizeof(GUID)) == 0) { // No format conversion needed - hr = scaler->CopyPixels( 0, static_cast( rowPitch ), static_cast( imageSize ), temp.get() ); - if ( FAILED(hr) ) + hr = scaler->CopyPixels(0, static_cast(rowPitch), static_cast(imageSize), temp.get()); + if (FAILED(hr)) return hr; } else { ComPtr FC; - hr = pWIC->CreateFormatConverter( FC.GetAddressOf() ); - if ( FAILED(hr) ) + hr = pWIC->CreateFormatConverter(FC.GetAddressOf()); + if (FAILED(hr)) return hr; BOOL canConvert = FALSE; - hr = FC->CanConvert( pfScaler, convertGUID, &canConvert ); - if ( FAILED(hr) || !canConvert ) + hr = FC->CanConvert(pfScaler, convertGUID, &canConvert); + if (FAILED(hr) || !canConvert) { return E_UNEXPECTED; } - hr = FC->Initialize( scaler.Get(), convertGUID, WICBitmapDitherTypeErrorDiffusion, 0, 0, WICBitmapPaletteTypeCustom ); - if ( FAILED(hr) ) + hr = FC->Initialize(scaler.Get(), convertGUID, WICBitmapDitherTypeErrorDiffusion, 0, 0, WICBitmapPaletteTypeCustom); + if (FAILED(hr)) return hr; - hr = FC->CopyPixels( 0, static_cast( rowPitch ), static_cast( imageSize ), temp.get() ); - if ( FAILED(hr) ) + hr = FC->CopyPixels(0, static_cast(rowPitch), static_cast(imageSize), temp.get()); + if (FAILED(hr)) return hr; } } else { // Format conversion but no resize - IWICImagingFactory* pWIC = _GetWIC(); - if ( !pWIC ) + auto pWIC = _GetWIC(); + if (!pWIC) return E_NOINTERFACE; ComPtr FC; - hr = pWIC->CreateFormatConverter( FC.GetAddressOf() ); - if ( FAILED(hr) ) + hr = pWIC->CreateFormatConverter(FC.GetAddressOf()); + if (FAILED(hr)) return hr; BOOL canConvert = FALSE; - hr = FC->CanConvert( pixelFormat, convertGUID, &canConvert ); - if ( FAILED(hr) || !canConvert ) + hr = FC->CanConvert(pixelFormat, convertGUID, &canConvert); + if (FAILED(hr) || !canConvert) { return E_UNEXPECTED; } - hr = FC->Initialize( frame, convertGUID, WICBitmapDitherTypeErrorDiffusion, 0, 0, WICBitmapPaletteTypeCustom ); - if ( FAILED(hr) ) + hr = FC->Initialize(frame, convertGUID, WICBitmapDitherTypeErrorDiffusion, 0, 0, WICBitmapPaletteTypeCustom); + if (FAILED(hr)) return hr; - hr = FC->CopyPixels( 0, static_cast( rowPitch ), static_cast( imageSize ), temp.get() ); - if ( FAILED(hr) ) + hr = FC->CopyPixels(0, static_cast(rowPitch), static_cast(imageSize), temp.get()); + if (FAILED(hr)) return hr; } // See if format is supported for auto-gen mipmaps (varies by feature level) bool autogen = false; - if ( d3dContext != 0 && textureView != 0 ) // Must have context and shader-view to auto generate mipmaps + if (d3dContext != 0 && textureView != 0) // Must have context and shader-view to auto generate mipmaps { UINT fmtSupport = 0; - hr = d3dDevice->CheckFormatSupport( format, &fmtSupport ); - if ( SUCCEEDED(hr) && ( fmtSupport & D3D11_FORMAT_SUPPORT_MIP_AUTOGEN ) ) + hr = d3dDevice->CheckFormatSupport(format, &fmtSupport); + if (SUCCEEDED(hr) && (fmtSupport & D3D11_FORMAT_SUPPORT_MIP_AUTOGEN)) { autogen = true; } @@ -613,7 +613,7 @@ namespace desc.Usage = usage; desc.CPUAccessFlags = cpuAccessFlags; - if ( autogen ) + if (autogen) { desc.BindFlags = bindFlags | D3D11_BIND_RENDER_TARGET; desc.MiscFlags = miscFlags | D3D11_RESOURCE_MISC_GENERATE_MIPS; @@ -626,12 +626,12 @@ namespace D3D11_SUBRESOURCE_DATA initData; initData.pSysMem = temp.get(); - initData.SysMemPitch = static_cast( rowPitch ); - initData.SysMemSlicePitch = static_cast( imageSize ); + initData.SysMemPitch = static_cast(rowPitch); + initData.SysMemSlicePitch = static_cast(imageSize); ID3D11Texture2D* tex = nullptr; - hr = d3dDevice->CreateTexture2D( &desc, (autogen) ? nullptr : &initData, &tex ); - if ( SUCCEEDED(hr) && tex != 0 ) + hr = d3dDevice->CreateTexture2D(&desc, (autogen) ? nullptr : &initData, &tex); + if (SUCCEEDED(hr) && tex != 0) { if (textureView != 0) { @@ -641,18 +641,18 @@ namespace SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; SRVDesc.Texture2D.MipLevels = (autogen) ? -1 : 1; - hr = d3dDevice->CreateShaderResourceView( tex, &SRVDesc, textureView ); - if ( FAILED(hr) ) + hr = d3dDevice->CreateShaderResourceView(tex, &SRVDesc, textureView); + if (FAILED(hr)) { tex->Release(); return hr; } - if ( autogen ) + if (autogen) { - assert( d3dContext != 0 ); - d3dContext->UpdateSubresource( tex, 0, nullptr, temp.get(), static_cast(rowPitch), static_cast(imageSize) ); - d3dContext->GenerateMips( *textureView ); + assert(d3dContext != 0); + d3dContext->UpdateSubresource(tex, 0, nullptr, temp.get(), static_cast(rowPitch), static_cast(imageSize)); + d3dContext->GenerateMips(*textureView); } } @@ -673,69 +673,69 @@ namespace //-------------------------------------------------------------------------------------- _Use_decl_annotations_ -HRESULT DirectX::CreateWICTextureFromMemory( ID3D11Device* d3dDevice, - const uint8_t* wicData, - size_t wicDataSize, - ID3D11Resource** texture, - ID3D11ShaderResourceView** textureView, - size_t maxsize ) +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, false, - texture, textureView ); + 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, - ID3D11DeviceContext* d3dContext, - const uint8_t* wicData, - size_t wicDataSize, - ID3D11Resource** texture, - ID3D11ShaderResourceView** textureView, - size_t maxsize ) +HRESULT DirectX::CreateWICTextureFromMemory(ID3D11Device* d3dDevice, + ID3D11DeviceContext* d3dContext, + const uint8_t* wicData, + size_t wicDataSize, + ID3D11Resource** texture, + ID3D11ShaderResourceView** textureView, + size_t maxsize) { - return CreateWICTextureFromMemoryEx( d3dDevice, d3dContext, wicData, wicDataSize, maxsize, - D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, false, - texture, textureView ); + 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, - const uint8_t* wicData, - size_t wicDataSize, - size_t maxsize, - D3D11_USAGE usage, - unsigned int bindFlags, - unsigned int cpuAccessFlags, - unsigned int miscFlags, - bool forceSRGB, - ID3D11Resource** texture, - ID3D11ShaderResourceView** textureView ) +HRESULT DirectX::CreateWICTextureFromMemoryEx(ID3D11Device* d3dDevice, + const uint8_t* wicData, + size_t wicDataSize, + size_t maxsize, + D3D11_USAGE usage, + unsigned int bindFlags, + unsigned int cpuAccessFlags, + unsigned int miscFlags, + unsigned int loadFlags, + ID3D11Resource** texture, + ID3D11ShaderResourceView** textureView) { - return CreateWICTextureFromMemoryEx( d3dDevice, nullptr, wicData, wicDataSize, maxsize, - usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB, - texture, textureView ); + return CreateWICTextureFromMemoryEx(d3dDevice, nullptr, wicData, wicDataSize, maxsize, + usage, bindFlags, cpuAccessFlags, miscFlags, loadFlags, + texture, textureView); } _Use_decl_annotations_ -HRESULT DirectX::CreateWICTextureFromMemoryEx( ID3D11Device* d3dDevice, - ID3D11DeviceContext* d3dContext, - const uint8_t* wicData, - size_t wicDataSize, - size_t maxsize, - D3D11_USAGE usage, - unsigned int bindFlags, - unsigned int cpuAccessFlags, - unsigned int miscFlags, - bool forceSRGB, - ID3D11Resource** texture, - ID3D11ShaderResourceView** textureView ) +HRESULT DirectX::CreateWICTextureFromMemoryEx(ID3D11Device* d3dDevice, + ID3D11DeviceContext* d3dContext, + const uint8_t* wicData, + size_t wicDataSize, + size_t maxsize, + D3D11_USAGE usage, + unsigned int bindFlags, + unsigned int cpuAccessFlags, + unsigned int miscFlags, + unsigned int loadFlags, + ID3D11Resource** texture, + ID3D11ShaderResourceView** textureView) { - if ( texture ) + if (texture) { *texture = nullptr; } - if ( textureView ) + if (textureView) { *textureView = nullptr; } @@ -743,41 +743,41 @@ HRESULT DirectX::CreateWICTextureFromMemoryEx( ID3D11Device* d3dDevice, if (!d3dDevice || !wicData || (!texture && !textureView)) 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); - IWICImagingFactory* pWIC = _GetWIC(); - if ( !pWIC ) + auto pWIC = _GetWIC(); + if (!pWIC) return E_NOINTERFACE; // Create input stream for memory ComPtr 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( wicData ), static_cast( wicDataSize ) ); - if ( FAILED(hr) ) + hr = stream->InitializeFromMemory(const_cast(wicData), static_cast(wicDataSize)); + if (FAILED(hr)) return hr; // Initialize WIC ComPtr decoder; - hr = pWIC->CreateDecoderFromStream( stream.Get(), 0, WICDecodeMetadataCacheOnDemand, decoder.GetAddressOf() ); - if ( FAILED(hr) ) + hr = pWIC->CreateDecoderFromStream(stream.Get(), 0, WICDecodeMetadataCacheOnDemand, decoder.GetAddressOf()); + if (FAILED(hr)) return hr; ComPtr frame; - hr = decoder->GetFrame( 0, frame.GetAddressOf() ); - if ( FAILED(hr) ) + hr = decoder->GetFrame(0, frame.GetAddressOf()); + if (FAILED(hr)) return hr; - hr = CreateTextureFromWIC( d3dDevice, d3dContext, frame.Get(), maxsize, - usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB, - texture, textureView ); - if ( FAILED(hr)) + hr = CreateTextureFromWIC(d3dDevice, d3dContext, frame.Get(), maxsize, + usage, bindFlags, cpuAccessFlags, miscFlags, loadFlags, + texture, textureView); + if (FAILED(hr)) return hr; if (texture != 0 && *texture != 0) @@ -795,65 +795,65 @@ HRESULT DirectX::CreateWICTextureFromMemoryEx( ID3D11Device* d3dDevice, //-------------------------------------------------------------------------------------- _Use_decl_annotations_ -HRESULT DirectX::CreateWICTextureFromFile( ID3D11Device* d3dDevice, - const wchar_t* fileName, - ID3D11Resource** texture, - ID3D11ShaderResourceView** textureView, - size_t maxsize ) +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, false, - texture, textureView ); + 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, - ID3D11DeviceContext* d3dContext, - const wchar_t* fileName, - ID3D11Resource** texture, - ID3D11ShaderResourceView** textureView, - size_t maxsize ) +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, false, - texture, textureView ); + 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, - const wchar_t* fileName, - size_t maxsize, - D3D11_USAGE usage, - unsigned int bindFlags, - unsigned int cpuAccessFlags, - unsigned int miscFlags, - bool forceSRGB, - ID3D11Resource** texture, - ID3D11ShaderResourceView** textureView ) +HRESULT DirectX::CreateWICTextureFromFileEx(ID3D11Device* d3dDevice, + const wchar_t* fileName, + size_t maxsize, + D3D11_USAGE usage, + unsigned int bindFlags, + unsigned int cpuAccessFlags, + unsigned int miscFlags, + unsigned int loadFlags, + ID3D11Resource** texture, + ID3D11ShaderResourceView** textureView) { - return CreateWICTextureFromFileEx( d3dDevice, nullptr, fileName, maxsize, - usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB, - texture, textureView ); + return CreateWICTextureFromFileEx(d3dDevice, nullptr, fileName, maxsize, + usage, bindFlags, cpuAccessFlags, miscFlags, loadFlags, + texture, textureView); } _Use_decl_annotations_ -HRESULT DirectX::CreateWICTextureFromFileEx( ID3D11Device* d3dDevice, - ID3D11DeviceContext* d3dContext, - const wchar_t* fileName, - size_t maxsize, - D3D11_USAGE usage, - unsigned int bindFlags, - unsigned int cpuAccessFlags, - unsigned int miscFlags, - bool forceSRGB, - ID3D11Resource** texture, - ID3D11ShaderResourceView** textureView ) +HRESULT DirectX::CreateWICTextureFromFileEx(ID3D11Device* d3dDevice, + ID3D11DeviceContext* d3dContext, + const wchar_t* fileName, + size_t maxsize, + D3D11_USAGE usage, + unsigned int bindFlags, + unsigned int cpuAccessFlags, + unsigned int miscFlags, + unsigned int loadFlags, + ID3D11Resource** texture, + ID3D11ShaderResourceView** textureView) { - if ( texture ) + if (texture) { *texture = nullptr; } - if ( textureView ) + if (textureView) { *textureView = nullptr; } @@ -861,43 +861,43 @@ HRESULT DirectX::CreateWICTextureFromFileEx( ID3D11Device* d3dDevice, if (!d3dDevice || !fileName || (!texture && !textureView)) return E_INVALIDARG; - IWICImagingFactory* pWIC = _GetWIC(); - if ( !pWIC ) + auto pWIC = _GetWIC(); + if (!pWIC) return E_NOINTERFACE; // Initialize WIC ComPtr decoder; - HRESULT hr = pWIC->CreateDecoderFromFilename( fileName, 0, GENERIC_READ, WICDecodeMetadataCacheOnDemand, decoder.GetAddressOf() ); - if ( FAILED(hr) ) + HRESULT hr = pWIC->CreateDecoderFromFilename(fileName, 0, GENERIC_READ, WICDecodeMetadataCacheOnDemand, decoder.GetAddressOf()); + if (FAILED(hr)) return hr; ComPtr frame; - hr = decoder->GetFrame( 0, frame.GetAddressOf() ); - if ( FAILED(hr) ) + hr = decoder->GetFrame(0, frame.GetAddressOf()); + if (FAILED(hr)) return hr; - hr = CreateTextureFromWIC( d3dDevice, d3dContext, frame.Get(), maxsize, - usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB, - texture, textureView ); + 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 (SUCCEEDED(hr)) { if (texture != 0 || textureView != 0) { CHAR strFileA[MAX_PATH]; - int result = WideCharToMultiByte( CP_ACP, - WC_NO_BEST_FIT_CHARS, - fileName, - -1, - strFileA, - MAX_PATH, - nullptr, - FALSE - ); - if ( result > 0 ) + int result = WideCharToMultiByte(CP_ACP, + WC_NO_BEST_FIT_CHARS, + fileName, + -1, + strFileA, + MAX_PATH, + nullptr, + FALSE + ); + if (result > 0) { - const CHAR* pstrName = strrchr( strFileA, '\\' ); + const CHAR* pstrName = strrchr(strFileA, '\\'); if (!pstrName) { pstrName = strFileA; @@ -909,18 +909,18 @@ HRESULT DirectX::CreateWICTextureFromFileEx( ID3D11Device* d3dDevice, if (texture != 0 && *texture != 0) { - (*texture)->SetPrivateData( WKPDID_D3DDebugObjectName, - static_cast( strnlen_s(pstrName, MAX_PATH) ), - pstrName - ); + (*texture)->SetPrivateData(WKPDID_D3DDebugObjectName, + static_cast(strnlen_s(pstrName, MAX_PATH)), + pstrName + ); } - if (textureView != 0 && *textureView != 0 ) + if (textureView != 0 && *textureView != 0) { - (*textureView)->SetPrivateData( WKPDID_D3DDebugObjectName, - static_cast( strnlen_s(pstrName, MAX_PATH) ), - pstrName - ); + (*textureView)->SetPrivateData(WKPDID_D3DDebugObjectName, + static_cast(strnlen_s(pstrName, MAX_PATH)), + pstrName + ); } } } diff --git a/WICTextureLoader/WICTextureLoader.h b/WICTextureLoader/WICTextureLoader.h index 9b5ac4f..736bf5d 100644 --- a/WICTextureLoader/WICTextureLoader.h +++ b/WICTextureLoader/WICTextureLoader.h @@ -1,7 +1,7 @@ //-------------------------------------------------------------------------------------- // File: WICTextureLoader.h // -// Function for loading a WIC image and creating a Direct3D 11 runtime texture for it +// Function for loading a WIC image and creating a Direct3D runtime texture for it // (auto-generating mipmaps if possible) // // Note: Assumes application has already called CoInitializeEx @@ -33,23 +33,28 @@ namespace DirectX { + enum WIC_LOADER_FLAGS + { + WIC_LOADER_DEFAULT = 0, + WIC_LOADER_FORCE_SRGB = 0x1, + WIC_LOADER_IGNORE_SRGB = 0x2, + }; + // Standard version HRESULT CreateWICTextureFromMemory( _In_ ID3D11Device* d3dDevice, _In_reads_bytes_(wicDataSize) const uint8_t* wicData, _In_ size_t wicDataSize, - _Out_opt_ ID3D11Resource** texture, - _Out_opt_ ID3D11ShaderResourceView** textureView, - _In_ size_t maxsize = 0 - ); + _Outptr_opt_ ID3D11Resource** texture, + _Outptr_opt_ ID3D11ShaderResourceView** textureView, + _In_ size_t maxsize = 0); HRESULT CreateWICTextureFromFile( _In_ ID3D11Device* d3dDevice, _In_z_ const wchar_t* szFileName, - _Out_opt_ ID3D11Resource** texture, - _Out_opt_ ID3D11ShaderResourceView** textureView, - _In_ size_t maxsize = 0 - ); + _Outptr_opt_ ID3D11Resource** texture, + _Outptr_opt_ ID3D11ShaderResourceView** textureView, + _In_ size_t maxsize = 0); // Standard version with optional auto-gen mipmap support HRESULT CreateWICTextureFromMemory( @@ -57,19 +62,17 @@ namespace DirectX _In_opt_ ID3D11DeviceContext* d3dContext, _In_reads_bytes_(wicDataSize) const uint8_t* wicData, _In_ size_t wicDataSize, - _Out_opt_ ID3D11Resource** texture, - _Out_opt_ ID3D11ShaderResourceView** textureView, - _In_ size_t maxsize = 0 - ); + _Outptr_opt_ ID3D11Resource** texture, + _Outptr_opt_ ID3D11ShaderResourceView** textureView, + _In_ size_t maxsize = 0); HRESULT CreateWICTextureFromFile( _In_ ID3D11Device* d3dDevice, _In_opt_ ID3D11DeviceContext* d3dContext, _In_z_ const wchar_t* szFileName, - _Out_opt_ ID3D11Resource** texture, - _Out_opt_ ID3D11ShaderResourceView** textureView, - _In_ size_t maxsize = 0 - ); + _Outptr_opt_ ID3D11Resource** texture, + _Outptr_opt_ ID3D11ShaderResourceView** textureView, + _In_ size_t maxsize = 0); // Extended version HRESULT CreateWICTextureFromMemoryEx( @@ -77,14 +80,13 @@ namespace DirectX _In_reads_bytes_(wicDataSize) const uint8_t* wicData, _In_ size_t wicDataSize, _In_ size_t maxsize, - D3D11_USAGE usage, + _In_ D3D11_USAGE usage, _In_ unsigned int bindFlags, _In_ unsigned int cpuAccessFlags, _In_ unsigned int miscFlags, - _In_ bool forceSRGB, - _Out_opt_ ID3D11Resource** texture, - _Out_opt_ ID3D11ShaderResourceView** textureView - ); + _In_ unsigned int loadFlags, + _Outptr_opt_ ID3D11Resource** texture, + _Outptr_opt_ ID3D11ShaderResourceView** textureView); HRESULT CreateWICTextureFromFileEx( _In_ ID3D11Device* d3dDevice, @@ -94,10 +96,9 @@ namespace DirectX _In_ unsigned int bindFlags, _In_ unsigned int cpuAccessFlags, _In_ unsigned int miscFlags, - _In_ bool forceSRGB, - _Out_opt_ ID3D11Resource** texture, - _Out_opt_ ID3D11ShaderResourceView** textureView - ); + _In_ unsigned int loadFlags, + _Outptr_opt_ ID3D11Resource** texture, + _Outptr_opt_ ID3D11ShaderResourceView** textureView); // Extended version with optional auto-gen mipmap support HRESULT CreateWICTextureFromMemoryEx( @@ -110,10 +111,9 @@ namespace DirectX _In_ unsigned int bindFlags, _In_ unsigned int cpuAccessFlags, _In_ unsigned int miscFlags, - _In_ bool forceSRGB, - _Out_opt_ ID3D11Resource** texture, - _Out_opt_ ID3D11ShaderResourceView** textureView - ); + _In_ unsigned int loadFlags, + _Outptr_opt_ ID3D11Resource** texture, + _Outptr_opt_ ID3D11ShaderResourceView** textureView); HRESULT CreateWICTextureFromFileEx( _In_ ID3D11Device* d3dDevice, @@ -124,8 +124,7 @@ namespace DirectX _In_ unsigned int bindFlags, _In_ unsigned int cpuAccessFlags, _In_ unsigned int miscFlags, - _In_ bool forceSRGB, - _Out_opt_ ID3D11Resource** texture, - _Out_opt_ ID3D11ShaderResourceView** textureView - ); + _In_ unsigned int loadFlags, + _Outptr_opt_ ID3D11Resource** texture, + _Outptr_opt_ ID3D11ShaderResourceView** textureView); } \ No newline at end of file diff --git a/WICTextureLoader/WICTextureLoader12.cpp b/WICTextureLoader/WICTextureLoader12.cpp index f0dc9a7..4ab9926 100644 --- a/WICTextureLoader/WICTextureLoader12.cpp +++ b/WICTextureLoader/WICTextureLoader12.cpp @@ -269,9 +269,8 @@ namespace HRESULT CreateTextureFromWIC(_In_ ID3D12Device* d3dDevice, _In_ IWICBitmapFrameDecode *frame, size_t maxsize, - D3D12_RESOURCE_FLAGS flags, - bool forceSRGB, - bool reserveFullMipChain, + D3D12_RESOURCE_FLAGS resFlags, + unsigned int loadFlags, _Outptr_ ID3D12Resource** texture, std::unique_ptr& decodedData, D3D12_SUBRESOURCE_DATA& subresource) @@ -351,11 +350,11 @@ namespace return E_FAIL; // Handle sRGB formats - if (forceSRGB) + if (loadFlags & WIC_LOADER_FORCE_SRGB) { format = MakeSRGB(format); } - else + else if (!(loadFlags & WIC_LOADER_IGNORE_SRGB)) { ComPtr metareader; if (SUCCEEDED(frame->GetMetadataQueryReader(metareader.GetAddressOf()))) @@ -488,7 +487,7 @@ namespace } // Count the number of mips - uint32_t mipCount = (reserveFullMipChain) ? CountMips(twidth, theight) : 1; + uint32_t mipCount = (loadFlags & (WIC_LOADER_MIP_AUTOGEN | WIC_LOADER_MIP_RESERVE)) ? CountMips(twidth, theight) : 1; // Create texture D3D12_RESOURCE_DESC desc = {}; @@ -499,7 +498,7 @@ namespace desc.Format = format; desc.SampleDesc.Count = 1; desc.SampleDesc.Quality = 0; - desc.Flags = flags; + desc.Flags = resFlags; desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; CD3DX12_HEAP_PROPERTIES defaultHeapProperties(D3D12_HEAP_TYPE_DEFAULT); @@ -547,8 +546,7 @@ HRESULT DirectX::LoadWICTextureFromMemory( wicDataSize, maxsize, D3D12_RESOURCE_FLAG_NONE, - false, - false, + WIC_LOADER_DEFAULT, texture, decodedData, subresource); @@ -562,9 +560,8 @@ HRESULT DirectX::LoadWICTextureFromMemoryEx( const uint8_t* wicData, size_t wicDataSize, size_t maxsize, - D3D12_RESOURCE_FLAGS flags, - bool forceSRGB, - bool reserveFullMipChain, + D3D12_RESOURCE_FLAGS resFlags, + unsigned int loadFlags, ID3D12Resource** texture, std::unique_ptr& decodedData, D3D12_SUBRESOURCE_DATA& subresource) @@ -610,7 +607,7 @@ HRESULT DirectX::LoadWICTextureFromMemoryEx( hr = CreateTextureFromWIC( d3dDevice, frame.Get(), maxsize, - flags, forceSRGB, reserveFullMipChain, + resFlags, loadFlags, texture, decodedData, subresource); if ( FAILED(hr)) return hr; @@ -637,8 +634,7 @@ HRESULT DirectX::LoadWICTextureFromFile( fileName, maxsize, D3D12_RESOURCE_FLAG_NONE, - false, - false, + WIC_LOADER_DEFAULT, texture, wicData, subresource); @@ -651,9 +647,8 @@ HRESULT DirectX::LoadWICTextureFromFileEx( ID3D12Device* d3dDevice, const wchar_t* fileName, size_t maxsize, - D3D12_RESOURCE_FLAGS flags, - bool forceSRGB, - bool reserveFullMipChain, + D3D12_RESOURCE_FLAGS resFlags, + unsigned int loadFlags, ID3D12Resource** texture, std::unique_ptr& decodedData, D3D12_SUBRESOURCE_DATA& subresource) @@ -682,7 +677,7 @@ HRESULT DirectX::LoadWICTextureFromFileEx( return hr; hr = CreateTextureFromWIC( d3dDevice, frame.Get(), maxsize, - flags, forceSRGB, reserveFullMipChain, + resFlags, loadFlags, texture, decodedData, subresource ); #if !defined(NO_D3D12_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) ) diff --git a/WICTextureLoader/WICTextureLoader12.h b/WICTextureLoader/WICTextureLoader12.h index 1d0defd..00a0212 100644 --- a/WICTextureLoader/WICTextureLoader12.h +++ b/WICTextureLoader/WICTextureLoader12.h @@ -1,7 +1,7 @@ //-------------------------------------------------------------------------------------- // File: WICTextureLoader12.h // -// Function for loading a WIC image and creating a Direct3D 12 runtime texture for it +// Function for loading a WIC image and creating a Direct3D runtime texture for it // (auto-generating mipmaps if possible) // // Note: Assumes application has already called CoInitializeEx @@ -31,6 +31,15 @@ namespace DirectX { + enum WIC_LOADER_FLAGS + { + WIC_LOADER_DEFAULT = 0, + WIC_LOADER_FORCE_SRGB = 0x1, + WIC_LOADER_IGNORE_SRGB = 0x2, + WIC_LOADER_MIP_AUTOGEN = 0x4, + WIC_LOADER_MIP_RESERVE = 0x8, + }; + // Standard version HRESULT __cdecl LoadWICTextureFromMemory( _In_ ID3D12Device* d3dDevice, @@ -55,9 +64,8 @@ namespace DirectX _In_reads_bytes_(wicDataSize) const uint8_t* wicData, size_t wicDataSize, size_t maxsize, - D3D12_RESOURCE_FLAGS flags, - bool forceSRGB, - bool reserveFullMipChain, + D3D12_RESOURCE_FLAGS resFlags, + unsigned int loadFlags, _Outptr_ ID3D12Resource** texture, std::unique_ptr& decodedData, D3D12_SUBRESOURCE_DATA& subresource); @@ -66,9 +74,8 @@ namespace DirectX _In_ ID3D12Device* d3dDevice, _In_z_ const wchar_t* szFileName, size_t maxsize, - D3D12_RESOURCE_FLAGS flags, - bool forceSRGB, - bool reserveFullMipChain, + D3D12_RESOURCE_FLAGS resFlags, + unsigned int loadFlags, _Outptr_ ID3D12Resource** texture, std::unique_ptr& decodedData, D3D12_SUBRESOURCE_DATA& subresource);