diff --git a/CMakeLists.txt b/CMakeLists.txt index 00753e2..f183487 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,16 +67,17 @@ if (WIN32) set(LIBRARY_SOURCES ${LIBRARY_SOURCES} DirectXTex/DirectXTexCompressGPU.cpp) set(LIBRARY_SOURCES ${LIBRARY_SOURCES} DirectXTex/BCDirectCompute.cpp) set(LIBRARY_SOURCES ${LIBRARY_SOURCES} DirectXTex/DirectXTexWIC.cpp) -endif() -if(BUILD_DX11) - set(LIBRARY_SOURCES ${LIBRARY_SOURCES} DirectXTex/DirectXTexD3D11.cpp) -endif() -if(BUILD_DX12) - set(LIBRARY_SOURCES ${LIBRARY_SOURCES} DirectXTex/DirectXTexD3D12.cpp) + if(BUILD_DX11) + set(LIBRARY_SOURCES ${LIBRARY_SOURCES} DirectXTex/DirectXTexD3D11.cpp) + endif() + if(BUILD_DX12) + set(LIBRARY_SOURCES ${LIBRARY_SOURCES} DirectXTex/DirectXTexD3D12.cpp) + endif() endif() + set(SHADER_SOURCES DirectXTex/Shaders/BC6HEncode.hlsl DirectXTex/Shaders/BC7Encode.hlsl) diff --git a/DirectXTex/DirectXTexTGA.cpp b/DirectXTex/DirectXTexTGA.cpp index 8d6d73a..f02a830 100644 --- a/DirectXTex/DirectXTexTGA.cpp +++ b/DirectXTex/DirectXTexTGA.cpp @@ -939,15 +939,14 @@ namespace time(&now); tm info; - if (!gmtime_s(&info, &now)) - { - ext->wStampMonth = static_cast(info.tm_mon + 1); - ext->wStampDay = static_cast(info.tm_mday); - ext->wStampYear = static_cast(info.tm_year + 1900); - ext->wStampHour = static_cast(info.tm_hour); - ext->wStampMinute = static_cast(info.tm_min); - ext->wStampSecond = static_cast(info.tm_sec); - } + info = *gmtime(&now); + + ext->wStampMonth = static_cast(info.tm_mon + 1); + ext->wStampDay = static_cast(info.tm_mday); + ext->wStampYear = static_cast(info.tm_year + 1900); + ext->wStampHour = static_cast(info.tm_hour); + ext->wStampMinute = static_cast(info.tm_min); + ext->wStampSecond = static_cast(info.tm_sec); } } @@ -995,32 +994,10 @@ HRESULT DirectX::GetMetadataFromTGAFile(const wchar_t* szFile, TexMetadata& meta if (!szFile) return E_INVALIDARG; -#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) - ScopedHandle hFile(safe_handle(CreateFile2(szFile, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, nullptr))); -#else - ScopedHandle hFile(safe_handle(CreateFileW(szFile, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, - FILE_FLAG_SEQUENTIAL_SCAN, nullptr))); -#endif - if (!hFile) - { - return HRESULT_FROM_WIN32(GetLastError()); - } - - // Get the file size - FILE_STANDARD_INFO fileInfo; - if (!GetFileInformationByHandleEx(hFile.get(), FileStandardInfo, &fileInfo, sizeof(fileInfo))) - { - return HRESULT_FROM_WIN32(GetLastError()); - } - - // File is too big for 32-bit allocation, so reject read (4 GB should be plenty large enough for a valid TGA file) - if (fileInfo.EndOfFile.HighPart > 0) - { - return HRESULT_FROM_WIN32(ERROR_FILE_TOO_LARGE); - } + ScopedReadFile hFile(szFile); // Need at least enough data to fill the standard header to be a valid TGA - if (fileInfo.EndOfFile.LowPart < (sizeof(TGA_HEADER))) + if (hFile.GetLength() < (sizeof(TGA_HEADER))) { return E_FAIL; } @@ -1028,9 +1005,9 @@ HRESULT DirectX::GetMetadataFromTGAFile(const wchar_t* szFile, TexMetadata& meta // Read the standard header (we don't need the file footer to parse the file) uint8_t header[sizeof(TGA_HEADER)] = {}; DWORD bytesRead = 0; - if (!ReadFile(hFile.get(), header, sizeof(TGA_HEADER), &bytesRead, nullptr)) + if (!(bytesRead = hFile.Read(header, sizeof(TGA_HEADER)))) { - return HRESULT_FROM_WIN32(GetLastError()); + return E_FAIL; } size_t offset; @@ -1130,32 +1107,12 @@ HRESULT DirectX::LoadFromTGAFile( image.Release(); -#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) - ScopedHandle hFile(safe_handle(CreateFile2(szFile, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, nullptr))); -#else - ScopedHandle hFile(safe_handle(CreateFileW(szFile, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, - FILE_FLAG_SEQUENTIAL_SCAN, nullptr))); -#endif - if (!hFile) - { - return HRESULT_FROM_WIN32(GetLastError()); - } + ScopedReadFile hFile(szFile); - // Get the file size - FILE_STANDARD_INFO fileInfo; - if (!GetFileInformationByHandleEx(hFile.get(), FileStandardInfo, &fileInfo, sizeof(fileInfo))) - { - return HRESULT_FROM_WIN32(GetLastError()); - } + auto length = hFile.GetLength(); - // File is too big for 32-bit allocation, so reject read (4 GB should be plenty large enough for a valid TGA file) - if (fileInfo.EndOfFile.HighPart > 0) - { - return HRESULT_FROM_WIN32(ERROR_FILE_TOO_LARGE); - } - - // Need at least enough data to fill the header to be a valid TGA - if (fileInfo.EndOfFile.LowPart < sizeof(TGA_HEADER)) + // Need at least enough data to fill the standard header to be a valid TGA + if (length < (sizeof(TGA_HEADER))) { return E_FAIL; } @@ -1163,9 +1120,9 @@ HRESULT DirectX::LoadFromTGAFile( // Read the header uint8_t header[sizeof(TGA_HEADER)] = {}; DWORD bytesRead = 0; - if (!ReadFile(hFile.get(), header, sizeof(TGA_HEADER), &bytesRead, nullptr)) + if (!(bytesRead = hFile.Read(header, sizeof(TGA_HEADER)))) { - return HRESULT_FROM_WIN32(GetLastError()); + return E_FAIL; } size_t offset; @@ -1175,19 +1132,15 @@ HRESULT DirectX::LoadFromTGAFile( if (FAILED(hr)) return hr; + // Read the pixels - auto remaining = static_cast(fileInfo.EndOfFile.LowPart - offset); + auto remaining = static_cast(length - offset); if (remaining == 0) return E_FAIL; if (offset > sizeof(TGA_HEADER)) { - // Skip past the id string - LARGE_INTEGER filePos = { { static_cast(offset), 0 } }; - if (!SetFilePointerEx(hFile.get(), filePos, nullptr, FILE_BEGIN)) - { - return HRESULT_FROM_WIN32(GetLastError()); - } + hFile.SeekBegin(offset); } hr = image.Initialize2D(mdata.format, mdata.width, mdata.height, 1, 1); @@ -1213,13 +1166,7 @@ HRESULT DirectX::LoadFromTGAFile( return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW); } - if (!ReadFile(hFile.get(), image.GetPixels(), static_cast(image.GetPixelsSize()), &bytesRead, nullptr)) - { - image.Release(); - return HRESULT_FROM_WIN32(GetLastError()); - } - - if (bytesRead != image.GetPixelsSize()) + if (image.GetPixelsSize() != hFile.Read(image.GetPixels(), static_cast(image.GetPixelsSize()))) { image.Release(); return E_FAIL; @@ -1363,10 +1310,10 @@ HRESULT DirectX::LoadFromTGAFile( return E_OUTOFMEMORY; } - if (!ReadFile(hFile.get(), temp.get(), remaining, &bytesRead, nullptr)) + if (!(bytesRead = hFile.Read(temp.get(), remaining))) { image.Release(); - return HRESULT_FROM_WIN32(GetLastError()); + return E_FAIL; } if (bytesRead != remaining) @@ -1406,37 +1353,26 @@ HRESULT DirectX::LoadFromTGAFile( // Handle optional TGA 2.0 footer TGA_FOOTER footer = {}; - if (SetFilePointer(hFile.get(), -static_cast(sizeof(TGA_FOOTER)), nullptr, FILE_END) == INVALID_SET_FILE_POINTER) - { - return HRESULT_FROM_WIN32(GetLastError()); - } + hFile.SeekEnd(-static_cast(sizeof(TGA_FOOTER))); - if (!ReadFile(hFile.get(), &footer, sizeof(TGA_FOOTER), &bytesRead, nullptr)) - { - image.Release(); - return HRESULT_FROM_WIN32(GetLastError()); - } - - if (bytesRead != sizeof(TGA_FOOTER)) + if (sizeof(TGA_FOOTER) != hFile.Read(&footer, sizeof(TGA_FOOTER))) { image.Release(); return E_FAIL; } + if (memcmp(footer.Signature, g_Signature, sizeof(g_Signature)) == 0) { if (footer.dwExtensionOffset != 0 - && ((footer.dwExtensionOffset + sizeof(TGA_EXTENSION)) <= fileInfo.EndOfFile.LowPart)) + && ((footer.dwExtensionOffset + sizeof(TGA_EXTENSION)) <= length)) { - LARGE_INTEGER filePos = { { static_cast(footer.dwExtensionOffset), 0 } }; - if (SetFilePointerEx(hFile.get(), filePos, nullptr, FILE_BEGIN)) + hFile.SeekBegin(footer.dwExtensionOffset); + TGA_EXTENSION ext = {}; + + if ((sizeof(TGA_EXTENSION) == hFile.Read(&ext, sizeof(TGA_EXTENSION)))) { - TGA_EXTENSION ext = {}; - if (ReadFile(hFile.get(), &ext, sizeof(TGA_EXTENSION), &bytesRead, nullptr) - && bytesRead == sizeof(TGA_EXTENSION)) - { metadata->SetAlphaMode(GetAlphaModeFromExtension(&ext)); - } } } } @@ -1549,18 +1485,7 @@ HRESULT DirectX::SaveToTGAFile(const Image& image, const wchar_t* szFile, const if (FAILED(hr)) return hr; - // Create file and write header -#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) - ScopedHandle hFile(safe_handle(CreateFile2(szFile, GENERIC_WRITE, 0, CREATE_ALWAYS, nullptr))); -#else - ScopedHandle hFile(safe_handle(CreateFileW(szFile, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr))); -#endif - if (!hFile) - { - return HRESULT_FROM_WIN32(GetLastError()); - } - - auto_delete_file delonfail(hFile.get()); + ScopedWriteFile hFile(szFile); // Determine size for TGA pixel data size_t rowPitch, slicePitch; @@ -1580,13 +1505,7 @@ HRESULT DirectX::SaveToTGAFile(const Image& image, const wchar_t* szFile, const // Write blob const DWORD bytesToWrite = static_cast(blob.GetBufferSize()); - DWORD bytesWritten; - if (!WriteFile(hFile.get(), blob.GetBufferPointer(), bytesToWrite, &bytesWritten, nullptr)) - { - return HRESULT_FROM_WIN32(GetLastError()); - } - - if (bytesWritten != bytesToWrite) + if (! hFile.Write(blob.GetBufferPointer(), bytesToWrite)) { return E_FAIL; } @@ -1599,15 +1518,11 @@ HRESULT DirectX::SaveToTGAFile(const Image& image, const wchar_t* szFile, const return E_OUTOFMEMORY; // Write header - DWORD bytesWritten; - if (!WriteFile(hFile.get(), &tga_header, sizeof(TGA_HEADER), &bytesWritten, nullptr)) + if (!hFile.Write(&tga_header, sizeof(TGA_HEADER))) { - return HRESULT_FROM_WIN32(GetLastError()); - } - - if (bytesWritten != sizeof(TGA_HEADER)) return E_FAIL; - + } + if (rowPitch > UINT32_MAX) return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW); @@ -1632,13 +1547,10 @@ HRESULT DirectX::SaveToTGAFile(const Image& image, const wchar_t* szFile, const pPixels += image.rowPitch; - if (!WriteFile(hFile.get(), temp.get(), static_cast(rowPitch), &bytesWritten, nullptr)) + if (!hFile.Write(temp.get(), rowPitch)) { - return HRESULT_FROM_WIN32(GetLastError()); - } - - if (bytesWritten != rowPitch) return E_FAIL; + } } uint32_t extOffset = 0; @@ -1648,19 +1560,12 @@ HRESULT DirectX::SaveToTGAFile(const Image& image, const wchar_t* szFile, const TGA_EXTENSION ext = {}; SetExtension(&ext, *metadata); - extOffset = SetFilePointer(hFile.get(), 0, nullptr, FILE_CURRENT); - if (extOffset == INVALID_SET_FILE_POINTER) - { - return HRESULT_FROM_WIN32(GetLastError()); - } + extOffset = hFile.GetOffset(); - if (!WriteFile(hFile.get(), &ext, sizeof(TGA_EXTENSION), &bytesWritten, nullptr)) + if (!hFile.Write(&ext, sizeof(TGA_EXTENSION))) { - return HRESULT_FROM_WIN32(GetLastError()); - } - - if (bytesWritten != sizeof(TGA_EXTENSION)) return E_FAIL; + } } // Write TGA 2.0 footer @@ -1668,16 +1573,12 @@ HRESULT DirectX::SaveToTGAFile(const Image& image, const wchar_t* szFile, const footer.dwExtensionOffset = extOffset; memcpy(footer.Signature, g_Signature, sizeof(g_Signature)); - if (!WriteFile(hFile.get(), &footer, sizeof(footer), &bytesWritten, nullptr)) + + if (!hFile.Write(&footer, sizeof(footer))) { - return HRESULT_FROM_WIN32(GetLastError()); + return E_FAIL; } - - if (bytesWritten != sizeof(footer)) - return E_FAIL; } - delonfail.clear(); - return S_OK; } diff --git a/DirectXTex/DirectXTexUtil.cpp b/DirectXTex/DirectXTexUtil.cpp index 795d9f2..78bb398 100644 --- a/DirectXTex/DirectXTexUtil.cpp +++ b/DirectXTex/DirectXTexUtil.cpp @@ -11,6 +11,10 @@ #include "DirectXTexP.h" +using Blob = DirectX::Blob; + +#if !defined(_DXTX_NOWIN) + #if (defined(_XBOX_ONE) && defined(_TITLE)) || defined(_GAMING_XBOX) static_assert(XBOX_DXGI_FORMAT_R10G10B10_7E3_A2_FLOAT == DXGI_FORMAT_R10G10B10_7E3_A2_FLOAT, "Xbox mismatch detected"); static_assert(XBOX_DXGI_FORMAT_R10G10B10_6E4_A2_FLOAT == DXGI_FORMAT_R10G10B10_6E4_A2_FLOAT, "Xbox mismatch detected"); @@ -32,6 +36,7 @@ using Microsoft::WRL::ComPtr; namespace { + //------------------------------------------------------------------------------------- // WIC Pixel Format Translation Data //------------------------------------------------------------------------------------- @@ -145,16 +150,8 @@ bool DirectX::_DXGIToWIC(DXGI_FORMAT format, GUID& guid, bool ignoreRGBvsBGR) no { switch (format) { - case DXGI_FORMAT_R8G8B8A8_UNORM: - case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB: - if (ignoreRGBvsBGR) - { - // If we are not doing conversion so don't really care about BGR vs RGB color-order, - // we can use the canonical WIC 32bppBGRA format which avoids an extra format conversion when using the WIC scaler - memcpy(&guid, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID)); - } - else - { + case DXGI_FORMA#endif + memcpy(&guid, &GUID_WICPixelFormat32bppRGBA, sizeof(GUID)); } return true; @@ -318,6 +315,7 @@ void DirectX::SetWICFactory(_In_opt_ IWICImagingFactory* pWIC) noexcept pWIC->Release(); } +#endif //===================================================================================== @@ -1384,7 +1382,7 @@ DXGI_FORMAT DirectX::MakeTypelessFLOAT(DXGI_FORMAT fmt) noexcept //===================================================================================== _Use_decl_annotations_ -size_t TexMetadata::ComputeIndex(size_t mip, size_t item, size_t slice) const noexcept +size_t DirectX::TexMetadata::ComputeIndex(size_t mip, size_t item, size_t slice) const noexcept { if (mip >= mipLevels) return size_t(-1); @@ -1456,7 +1454,7 @@ void Blob::Release() noexcept { if (m_buffer) { - _aligned_free(m_buffer); + std::free(m_buffer); m_buffer = nullptr; } @@ -1471,7 +1469,7 @@ HRESULT Blob::Initialize(size_t size) noexcept Release(); - m_buffer = _aligned_malloc(size, 16); + m_buffer = std::aligned_alloc(size, 16); if (!m_buffer) { Release(); diff --git a/Makefile b/Makefile index fd91289..b609dd5 100644 --- a/Makefile +++ b/Makefile @@ -289,60 +289,6 @@ DirectXTex/DirectXTexConvert.cpp.s: $(MAKE) $(MAKESILENT) -f CMakeFiles/DirectXTex.dir/build.make CMakeFiles/DirectXTex.dir/DirectXTex/DirectXTexConvert.cpp.s .PHONY : DirectXTex/DirectXTexConvert.cpp.s -DirectXTex/DirectXTexD3D11.o: DirectXTex/DirectXTexD3D11.cpp.o - -.PHONY : DirectXTex/DirectXTexD3D11.o - -# target to build an object file -DirectXTex/DirectXTexD3D11.cpp.o: - $(MAKE) $(MAKESILENT) -f CMakeFiles/DirectXTex.dir/build.make CMakeFiles/DirectXTex.dir/DirectXTex/DirectXTexD3D11.cpp.o -.PHONY : DirectXTex/DirectXTexD3D11.cpp.o - -DirectXTex/DirectXTexD3D11.i: DirectXTex/DirectXTexD3D11.cpp.i - -.PHONY : DirectXTex/DirectXTexD3D11.i - -# target to preprocess a source file -DirectXTex/DirectXTexD3D11.cpp.i: - $(MAKE) $(MAKESILENT) -f CMakeFiles/DirectXTex.dir/build.make CMakeFiles/DirectXTex.dir/DirectXTex/DirectXTexD3D11.cpp.i -.PHONY : DirectXTex/DirectXTexD3D11.cpp.i - -DirectXTex/DirectXTexD3D11.s: DirectXTex/DirectXTexD3D11.cpp.s - -.PHONY : DirectXTex/DirectXTexD3D11.s - -# target to generate assembly for a file -DirectXTex/DirectXTexD3D11.cpp.s: - $(MAKE) $(MAKESILENT) -f CMakeFiles/DirectXTex.dir/build.make CMakeFiles/DirectXTex.dir/DirectXTex/DirectXTexD3D11.cpp.s -.PHONY : DirectXTex/DirectXTexD3D11.cpp.s - -DirectXTex/DirectXTexD3D12.o: DirectXTex/DirectXTexD3D12.cpp.o - -.PHONY : DirectXTex/DirectXTexD3D12.o - -# target to build an object file -DirectXTex/DirectXTexD3D12.cpp.o: - $(MAKE) $(MAKESILENT) -f CMakeFiles/DirectXTex.dir/build.make CMakeFiles/DirectXTex.dir/DirectXTex/DirectXTexD3D12.cpp.o -.PHONY : DirectXTex/DirectXTexD3D12.cpp.o - -DirectXTex/DirectXTexD3D12.i: DirectXTex/DirectXTexD3D12.cpp.i - -.PHONY : DirectXTex/DirectXTexD3D12.i - -# target to preprocess a source file -DirectXTex/DirectXTexD3D12.cpp.i: - $(MAKE) $(MAKESILENT) -f CMakeFiles/DirectXTex.dir/build.make CMakeFiles/DirectXTex.dir/DirectXTex/DirectXTexD3D12.cpp.i -.PHONY : DirectXTex/DirectXTexD3D12.cpp.i - -DirectXTex/DirectXTexD3D12.s: DirectXTex/DirectXTexD3D12.cpp.s - -.PHONY : DirectXTex/DirectXTexD3D12.s - -# target to generate assembly for a file -DirectXTex/DirectXTexD3D12.cpp.s: - $(MAKE) $(MAKESILENT) -f CMakeFiles/DirectXTex.dir/build.make CMakeFiles/DirectXTex.dir/DirectXTex/DirectXTexD3D12.cpp.s -.PHONY : DirectXTex/DirectXTexD3D12.cpp.s - DirectXTex/DirectXTexDDS.o: DirectXTex/DirectXTexDDS.cpp.o .PHONY : DirectXTex/DirectXTexDDS.o @@ -841,12 +787,6 @@ help: @echo "... DirectXTex/DirectXTexConvert.o" @echo "... DirectXTex/DirectXTexConvert.i" @echo "... DirectXTex/DirectXTexConvert.s" - @echo "... DirectXTex/DirectXTexD3D11.o" - @echo "... DirectXTex/DirectXTexD3D11.i" - @echo "... DirectXTex/DirectXTexD3D11.s" - @echo "... DirectXTex/DirectXTexD3D12.o" - @echo "... DirectXTex/DirectXTexD3D12.i" - @echo "... DirectXTex/DirectXTexD3D12.s" @echo "... DirectXTex/DirectXTexDDS.o" @echo "... DirectXTex/DirectXTexDDS.i" @echo "... DirectXTex/DirectXTexDDS.s" diff --git a/PlatformSupport/StdFS.hpp b/PlatformSupport/StdFS.hpp index 193e7bf..512287b 100644 --- a/PlatformSupport/StdFS.hpp +++ b/PlatformSupport/StdFS.hpp @@ -80,6 +80,11 @@ public: _stream.seekp(offset, std::ios_base::beg); } + size_t GetOffset() + { + return static_cast(_stream.tellp());; + } + size_t GetLength() { if (!_stream.is_open()) @@ -88,7 +93,7 @@ public: } auto old = _stream.tellp(); _stream.seekp(0, std::ios_base::end); - auto ret = static_cast(_stream.tellp()); + auto ret = GetOffset(); _stream.seekp(old, std::ios_base::beg); return ret; } @@ -137,6 +142,12 @@ public: _stream.seekg(offset, std::ios_base::end); } + + size_t GetOffset() + { + return static_cast(_stream.tellg());; + } + size_t GetLength() { if (!_stream.is_open()) @@ -145,7 +156,7 @@ public: } auto old = _stream.tellg(); _stream.seekg(0, std::ios_base::end); - auto ret = static_cast(_stream.tellg()); + auto ret = GetOffset(); _stream.seekg(old, std::ios_base::beg); return ret; } diff --git a/bin/CMake/libDirectXTex.a b/bin/CMake/libDirectXTex.a new file mode 100644 index 0000000..aa5ed3f Binary files /dev/null and b/bin/CMake/libDirectXTex.a differ