From db7d84a71678028038297a5347c1b1d363827e8b Mon Sep 17 00:00:00 2001 From: Chuck Walbourn Date: Thu, 31 Oct 2024 11:56:05 -0700 Subject: [PATCH 1/6] Support using C++17s std::byte for FromMemory functions --- Auxiliary/DirectXTexXbox.h | 42 ++++++++++++++++++++ DirectXTex/DirectXTex.h | 63 ++++++++++++++++++++++++++++++ DirectXTex/DirectXTex.inl | 76 ++++++++++++++++++++++++++++++++++++ DirectXTex/DirectXTexDDS.cpp | 2 +- 4 files changed, 182 insertions(+), 1 deletion(-) diff --git a/Auxiliary/DirectXTexXbox.h b/Auxiliary/DirectXTexXbox.h index 0829c8f9..5fba4639 100644 --- a/Auxiliary/DirectXTexXbox.h +++ b/Auxiliary/DirectXTexXbox.h @@ -132,6 +132,39 @@ namespace Xbox HRESULT __cdecl SaveToDDSMemory(_In_ const XboxImage& xbox, _Out_ DirectX::Blob& blob); HRESULT __cdecl SaveToDDSFile(_In_ const XboxImage& xbox, _In_z_ const wchar_t* szFile); +#ifdef __cpp_lib_byte + inline HRESULT __cdecl GetMetadataFromDDSMemory( + _In_reads_bytes_(size) const std::byte* pSource, _In_ size_t size, + _Out_ DirectX::TexMetadata& metadata, _Out_ bool& isXbox) + { + return GetMetadataFromDDSMemory(reinterpret_cast(pSource), size, metadata, isXbox); + } + + inline HRESULT __cdecl GetMetadataFromDDSMemoryEx( + _In_reads_bytes_(size) const std::byte* pSource, _In_ size_t size, + _Out_ DirectX::TexMetadata& metadata, _Out_ bool& isXbox, + _Out_opt_ DirectX::DDSMetaData* ddPixelFormat) + { + return GetMetadataFromDDSMemoryEx(reinterpret_cast(pSource), size, metadata, isXbox, ddPixelFormat); + } + + inline HRESULT __cdecl LoadFromDDSMemory( + _In_reads_bytes_(size) const std::byte* pSource, _In_ size_t size, + _Out_opt_ DirectX::TexMetadata* metadata, _Out_ XboxImage& image) + { + return LoadFromDDSMemory(reinterpret_cast(pSource), size, metadta, image); + } + + inline HRESULT __cdecl LoadFromDDSMemoryEx( + _In_reads_bytes_(size) const std::byte* pSource, _In_ size_t size, + _Out_opt_ DirectX::TexMetadata* metadata, + _Out_opt_ DirectX::DDSMetaData* ddPixelFormat, + _Out_ XboxImage& image) + { + return LoadFromDDSMemoryEx(reinterpret_cast(pSource), size, metadata, ddPixelFormat); + } +#endif // __cpp_lib_byte + //--------------------------------------------------------------------------------- // Xbox Texture Tiling / Detiling (requires XG DLL to be present at runtime) @@ -178,4 +211,13 @@ namespace Xbox const XboxImage& xbox, _Out_writes_bytes_(maxsize) uint8_t* pDestination, _In_ size_t maxsize) noexcept; +#ifdef __cpp_lib_byte + inline HRESULT __cdecl EncodeDDSHeader( + const XboxImage& xbox, + _Out_writes_bytes_(maxsize) std::byte* pDestination, _In_ size_t maxsize) noexcept + { + return EncodeDDSHeader(xbox, reinterpret_cast(pDestination), maxsize); + } +#endif + } // namespace diff --git a/DirectXTex/DirectXTex.h b/DirectXTex/DirectXTex.h index e24142cb..90db52a4 100644 --- a/DirectXTex/DirectXTex.h +++ b/DirectXTex/DirectXTex.h @@ -382,6 +382,34 @@ namespace DirectX _In_z_ const wchar_t* szFile, _Out_ TexMetadata& metadata) noexcept; +#ifdef __cpp_lib_byte + HRESULT __cdecl GetMetadataFromDDSMemory( + _In_reads_bytes_(size) const std::byte* pSource, _In_ size_t size, + _In_ DDS_FLAGS flags, + _Out_ TexMetadata& metadata) noexcept; + HRESULT __cdecl GetMetadataFromDDSMemoryEx( + _In_reads_bytes_(size) const std::byte* pSource, _In_ size_t size, + _In_ DDS_FLAGS flags, + _Out_ TexMetadata& metadata, + _Out_opt_ DDSMetaData* ddPixelFormat) noexcept; + HRESULT __cdecl GetMetadataFromHDRMemory( + _In_reads_bytes_(size) const std::byte* pSource, _In_ size_t size, + _Out_ TexMetadata& metadata) noexcept; + HRESULT __cdecl GetMetadataFromTGAMemory( + _In_reads_bytes_(size) const std::byte* pSource, _In_ size_t size, + _In_ TGA_FLAGS flags, + _Out_ TexMetadata& metadata) noexcept; + +#ifdef _WIN32 + HRESULT __cdecl GetMetadataFromWICMemory( + _In_reads_bytes_(size) const std::byte* pSource, _In_ size_t size, + _In_ WIC_FLAGS flags, + _Out_ TexMetadata& metadata, + _In_ std::function getMQR = nullptr); +#endif +#endif // __cpp_lib_byte + + //--------------------------------------------------------------------------------- // Bitmap image container struct Image @@ -590,6 +618,34 @@ namespace DirectX HRESULT __cdecl SaveToTGAMemory(_In_ const Image& image, _Out_ Blob& blob, _In_opt_ const TexMetadata* metadata = nullptr) noexcept; HRESULT __cdecl SaveToTGAFile(_In_ const Image& image, _In_z_ const wchar_t* szFile, _In_opt_ const TexMetadata* metadata = nullptr) noexcept; +#ifdef __cpp_lib_byte + HRESULT __cdecl LoadFromDDSMemory( + _In_reads_bytes_(size) const std::byte* pSource, _In_ size_t size, + _In_ DDS_FLAGS flags, + _Out_opt_ TexMetadata* metadata, _Out_ ScratchImage& image) noexcept; + HRESULT __cdecl LoadFromDDSMemoryEx( + _In_reads_bytes_(size) const std::byte* pSource, _In_ size_t size, + _In_ DDS_FLAGS flags, + _Out_opt_ TexMetadata* metadata, + _Out_opt_ DDSMetaData* ddPixelFormat, + _Out_ ScratchImage& image) noexcept; + HRESULT __cdecl LoadFromHDRMemory( + _In_reads_bytes_(size) const std::byte* pSource, _In_ size_t size, + _Out_opt_ TexMetadata* metadata, _Out_ ScratchImage& image) noexcept; + HRESULT __cdecl LoadFromTGAMemory( + _In_reads_bytes_(size) const std::byte* pSource, _In_ size_t size, + _In_ TGA_FLAGS flags, + _Out_opt_ TexMetadata* metadata, _Out_ ScratchImage& image) noexcept; + +#ifdef _WIN32 + HRESULT __cdecl LoadFromWICMemory( + _In_reads_bytes_(size) const std::byte* pSource, _In_ size_t size, + _In_ WIC_FLAGS flags, + _Out_opt_ TexMetadata* metadata, _Out_ ScratchImage& image, + _In_ std::function getMQR = nullptr); +#endif +#endif // __cpp_lib_byte + //--------------------------------------------------------------------------------- // Texture conversion, resizing, mipmap generation, and block compression @@ -959,6 +1015,13 @@ namespace DirectX _Out_writes_bytes_to_opt_(maxsize, required) uint8_t* pDestination, _In_ size_t maxsize, _Out_ size_t& required) noexcept; +#ifdef __cpp_lib_byte + HRESULT __cdecl EncodeDDSHeader( + _In_ const TexMetadata& metadata, DDS_FLAGS flags, + _Out_writes_bytes_to_opt_(maxsize, required) std::byte* pDestination, _In_ size_t maxsize, + _Out_ size_t& required) noexcept; +#endif + //--------------------------------------------------------------------------------- // Direct3D interop diff --git a/DirectXTex/DirectXTex.inl b/DirectXTex/DirectXTex.inl index 49745aea..44eecdd9 100644 --- a/DirectXTex/DirectXTex.inl +++ b/DirectXTex/DirectXTex.inl @@ -190,3 +190,79 @@ inline HRESULT __cdecl SaveToTGAFile(const Image& image, const wchar_t* szFile, { return SaveToTGAFile(image, TGA_FLAGS_NONE, szFile, metadata); } + + +//===================================================================================== +// C++17 helpers +//===================================================================================== +#ifdef __cpp_lib_byte + +_Use_decl_annotations_ +inline HRESULT __cdecl GetMetadataFromDDSMemory(const std::byte* pSource, size_t size, DDS_FLAGS flags, TexMetadata& metadata) noexcept +{ + return GetMetadataFromDDSMemory(reinterpret_cast(pSource), size, flags, metadata); +} + +_Use_decl_annotations_ +inline HRESULT __cdecl LoadFromDDSMemory(const std::byte* pSource, size_t size, DDS_FLAGS flags, TexMetadata* metadata, ScratchImage& image) noexcept +{ + return LoadFromDDSMemory(reinterpret_cast(pSource), size, flags, metadata, image); +} + +_Use_decl_annotations_ +inline HRESULT __cdecl GetMetadataFromDDSMemoryEx(const std::byte* pSource, size_t size, DDS_FLAGS flags, TexMetadata& metadata, DDSMetaData* ddPixelFormat) noexcept +{ + return GetMetadataFromDDSMemoryEx(reinterpret_cast(pSource), size, flags, metadata, ddPixelFormat); +} + +_Use_decl_annotations_ +inline HRESULT __cdecl LoadFromDDSMemoryEx(const std::byte* pSource, size_t size, DDS_FLAGS flags, TexMetadata* metadata, DDSMetaData* ddPixelFormat, ScratchImage& image) noexcept +{ + return LoadFromDDSMemoryEx(reinterpret_cast(pSource), size, flags, metadata, ddPixelFormat, image); +} + +_Use_decl_annotations_ +inline HRESULT __cdecl GetMetadataFromHDRMemory(const std::byte* pSource, size_t size, TexMetadata& metadata) noexcept +{ + return GetMetadataFromHDRMemory(reinterpret_cast(pSource), size, metadata); +} + +_Use_decl_annotations_ +inline HRESULT __cdecl LoadFromHDRMemory(const std::byte* pSource, size_t size, TexMetadata* metadata, ScratchImage& image) noexcept +{ + return LoadFromHDRMemory(reinterpret_cast(pSource), size, metadata, image); +} + +_Use_decl_annotations_ +inline HRESULT __cdecl GetMetadataFromTGAMemory(const std::byte* pSource, size_t size, TGA_FLAGS flags, TexMetadata& metadata) noexcept +{ + return GetMetadataFromTGAMemory(reinterpret_cast(pSource), size, flags, metadata); +} + +_Use_decl_annotations_ +inline HRESULT __cdecl LoadFromTGAMemory(const std::byte* pSource, size_t size, TGA_FLAGS flags, TexMetadata* metadata, ScratchImage& image) noexcept +{ + return LoadFromTGAMemory(reinterpret_cast(pSource), size, flags, metadata, image); +} + +_Use_decl_annotations_ +inline HRESULT __cdecl EncodeDDSHeader(const TexMetadata& metadata, DDS_FLAGS flags, std::byte* pDestination, size_t maxsize, size_t& required) noexcept +{ + return EncodeDDSHeader(metadata, flags, reinterpret_cast(pDestination), maxsize, required); +} + +#ifdef _WIN32 +_Use_decl_annotations_ +inline HRESULT __cdecl GetMetadataFromWICMemory(const std::byte* pSource, size_t size, WIC_FLAGS flags, TexMetadata& metadata, std::function getMQR) +{ + return GetMetadataFromWICMemory(reinterpret_cast(pSource), size, flags, metadata, getMQR); +} + +_Use_decl_annotations_ +inline HRESULT __cdecl LoadFromWICMemory(const std::byte* pSource, size_t size, WIC_FLAGS flags, TexMetadata* metadata, ScratchImage& image, std::function getMQR) +{ + return LoadFromWICMemory(reinterpret_cast(pSource), size, flags, metadata, image, getMQR); +} +#endif // _WIN32 + +#endif // __cpp_lib_byte diff --git a/DirectXTex/DirectXTexDDS.cpp b/DirectXTex/DirectXTexDDS.cpp index c471da3c..49d5710a 100644 --- a/DirectXTex/DirectXTexDDS.cpp +++ b/DirectXTex/DirectXTexDDS.cpp @@ -2317,7 +2317,7 @@ HRESULT DirectX::SaveToDDSMemory( // Determine memory required size_t required = 0; - HRESULT hr = EncodeDDSHeader(metadata, flags, nullptr, 0, required); + HRESULT hr = EncodeDDSHeader(metadata, flags, static_cast(nullptr), 0, required); if (FAILED(hr)) return hr; From 22ff3e70e444ea1aae5ef1d80854da0a6d593dc2 Mon Sep 17 00:00:00 2001 From: Chuck Walbourn Date: Thu, 31 Oct 2024 14:06:12 -0700 Subject: [PATCH 2/6] Fixed up nullptr issue --- Auxiliary/DirectXTexXbox.h | 7 +++++++ DirectXTex/DirectXTex.h | 5 +++++ DirectXTex/DirectXTex.inl | 6 ++++++ DirectXTex/DirectXTexDDS.cpp | 2 +- ScreenGrab/ScreenGrab12.cpp | 8 ++++++++ 5 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Auxiliary/DirectXTexXbox.h b/Auxiliary/DirectXTexXbox.h index 5fba4639..a098cf4b 100644 --- a/Auxiliary/DirectXTexXbox.h +++ b/Auxiliary/DirectXTexXbox.h @@ -218,6 +218,13 @@ namespace Xbox { return EncodeDDSHeader(xbox, reinterpret_cast(pDestination), maxsize); } + + inline HRESULT __cdecl EncodeDDSHeader( + const XboxImage& xbox, + _Reserved_ std::nullptr_t, _In_ size_t maxsize) noexcept + { + return EncodeDDSHeader(xbox, reinterpret_cast(nullptr), maxsize); + } #endif } // namespace diff --git a/DirectXTex/DirectXTex.h b/DirectXTex/DirectXTex.h index 90db52a4..4dc36c86 100644 --- a/DirectXTex/DirectXTex.h +++ b/DirectXTex/DirectXTex.h @@ -1020,6 +1020,11 @@ namespace DirectX _In_ const TexMetadata& metadata, DDS_FLAGS flags, _Out_writes_bytes_to_opt_(maxsize, required) std::byte* pDestination, _In_ size_t maxsize, _Out_ size_t& required) noexcept; + + HRESULT __cdecl EncodeDDSHeader( + _In_ const TexMetadata& metadata, DDS_FLAGS flags, + _Reserved_ std::nullptr_t, _In_ size_t maxsize, + _Out_ size_t& required) noexcept; #endif //--------------------------------------------------------------------------------- diff --git a/DirectXTex/DirectXTex.inl b/DirectXTex/DirectXTex.inl index 44eecdd9..95ffd5ea 100644 --- a/DirectXTex/DirectXTex.inl +++ b/DirectXTex/DirectXTex.inl @@ -251,6 +251,12 @@ inline HRESULT __cdecl EncodeDDSHeader(const TexMetadata& metadata, DDS_FLAGS fl return EncodeDDSHeader(metadata, flags, reinterpret_cast(pDestination), maxsize, required); } +_Use_decl_annotations_ +inline HRESULT __cdecl EncodeDDSHeader(const TexMetadata& metadata, DDS_FLAGS flags, std::nullptr_t, size_t maxsize, size_t& required) noexcept +{ + return EncodeDDSHeader(metadata, flags, reinterpret_cast(nullptr), maxsize, required); +} + #ifdef _WIN32 _Use_decl_annotations_ inline HRESULT __cdecl GetMetadataFromWICMemory(const std::byte* pSource, size_t size, WIC_FLAGS flags, TexMetadata& metadata, std::function getMQR) diff --git a/DirectXTex/DirectXTexDDS.cpp b/DirectXTex/DirectXTexDDS.cpp index 49d5710a..c471da3c 100644 --- a/DirectXTex/DirectXTexDDS.cpp +++ b/DirectXTex/DirectXTexDDS.cpp @@ -2317,7 +2317,7 @@ HRESULT DirectX::SaveToDDSMemory( // Determine memory required size_t required = 0; - HRESULT hr = EncodeDDSHeader(metadata, flags, static_cast(nullptr), 0, required); + HRESULT hr = EncodeDDSHeader(metadata, flags, nullptr, 0, required); if (FAILED(hr)) return hr; diff --git a/ScreenGrab/ScreenGrab12.cpp b/ScreenGrab/ScreenGrab12.cpp index 7c196309..b8cd697f 100644 --- a/ScreenGrab/ScreenGrab12.cpp +++ b/ScreenGrab/ScreenGrab12.cpp @@ -853,7 +853,11 @@ namespace hr = commandList->Close(); if (FAILED(hr)) + { + (*pStaging)->Release(); + *pStaging = nullptr; return hr; + } // Execute the command list pCommandQ->ExecuteCommandLists(1, CommandListCast(commandList.GetAddressOf())); @@ -861,7 +865,11 @@ namespace // Signal the fence hr = pCommandQ->Signal(fence.Get(), 1); if (FAILED(hr)) + { + (*pStaging)->Release(); + *pStaging = nullptr; return hr; + } // Block until the copy is complete while (fence->GetCompletedValue() < 1) From 7b63946fef677a365465fb8d439bd75977663b1b Mon Sep 17 00:00:00 2001 From: Chuck Walbourn Date: Thu, 31 Oct 2024 14:07:13 -0700 Subject: [PATCH 3/6] Minor cmake tweak --- CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cffe5cae..ec26d0b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -319,13 +319,13 @@ endif() if(directxmath_FOUND) message(STATUS "Using DirectXMath package") - target_link_libraries(${PROJECT_NAME} PUBLIC Microsoft::DirectXMath) + target_link_libraries(${PROJECT_NAME} PRIVATE Microsoft::DirectXMath) endif() if(directx-headers_FOUND) message(STATUS "Using DirectX-Headers package") - target_link_libraries(${PROJECT_NAME} PUBLIC Microsoft::DirectX-Headers) - target_compile_definitions(${PROJECT_NAME} PUBLIC USING_DIRECTX_HEADERS) + target_link_libraries(${PROJECT_NAME} PRIVATE Microsoft::DirectX-Headers) + target_compile_definitions(${PROJECT_NAME} PRIVATE USING_DIRECTX_HEADERS) target_compile_options(${PROJECT_NAME} PRIVATE $<$:/wd4062> $<$:-Wno-switch-enum>) endif() From f621033eb0588bcce246f5c559dc9b2946059831 Mon Sep 17 00:00:00 2001 From: Chuck Walbourn Date: Thu, 31 Oct 2024 14:12:58 -0700 Subject: [PATCH 4/6] Try to fix linux --- Auxiliary/DirectXTexXbox.h | 2 +- DirectXTex/DirectXTex.inl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Auxiliary/DirectXTexXbox.h b/Auxiliary/DirectXTexXbox.h index a098cf4b..b5a0ba95 100644 --- a/Auxiliary/DirectXTexXbox.h +++ b/Auxiliary/DirectXTexXbox.h @@ -223,7 +223,7 @@ namespace Xbox const XboxImage& xbox, _Reserved_ std::nullptr_t, _In_ size_t maxsize) noexcept { - return EncodeDDSHeader(xbox, reinterpret_cast(nullptr), maxsize); + return EncodeDDSHeader(xbox, static_cast(nullptr), maxsize); } #endif diff --git a/DirectXTex/DirectXTex.inl b/DirectXTex/DirectXTex.inl index 95ffd5ea..d20f41ef 100644 --- a/DirectXTex/DirectXTex.inl +++ b/DirectXTex/DirectXTex.inl @@ -254,7 +254,7 @@ inline HRESULT __cdecl EncodeDDSHeader(const TexMetadata& metadata, DDS_FLAGS fl _Use_decl_annotations_ inline HRESULT __cdecl EncodeDDSHeader(const TexMetadata& metadata, DDS_FLAGS flags, std::nullptr_t, size_t maxsize, size_t& required) noexcept { - return EncodeDDSHeader(metadata, flags, reinterpret_cast(nullptr), maxsize, required); + return EncodeDDSHeader(metadata, flags, static_cast(nullptr), maxsize, required); } #ifdef _WIN32 From 047b20068c664392973480fb7edfb5f1cf176dc8 Mon Sep 17 00:00:00 2001 From: Chuck Walbourn Date: Thu, 31 Oct 2024 14:34:44 -0700 Subject: [PATCH 5/6] Fix typo --- Auxiliary/DirectXTexXbox.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Auxiliary/DirectXTexXbox.h b/Auxiliary/DirectXTexXbox.h index b5a0ba95..3473f3b4 100644 --- a/Auxiliary/DirectXTexXbox.h +++ b/Auxiliary/DirectXTexXbox.h @@ -152,7 +152,7 @@ namespace Xbox _In_reads_bytes_(size) const std::byte* pSource, _In_ size_t size, _Out_opt_ DirectX::TexMetadata* metadata, _Out_ XboxImage& image) { - return LoadFromDDSMemory(reinterpret_cast(pSource), size, metadta, image); + return LoadFromDDSMemory(reinterpret_cast(pSource), size, metadata, image); } inline HRESULT __cdecl LoadFromDDSMemoryEx( From 3724a23339affc22edfc9591397a0ada69cdbd02 Mon Sep 17 00:00:00 2001 From: Chuck Walbourn Date: Thu, 31 Oct 2024 15:07:57 -0700 Subject: [PATCH 6/6] Fix Xbox build break --- Auxiliary/DirectXTexXbox.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Auxiliary/DirectXTexXbox.h b/Auxiliary/DirectXTexXbox.h index 3473f3b4..9f75866d 100644 --- a/Auxiliary/DirectXTexXbox.h +++ b/Auxiliary/DirectXTexXbox.h @@ -161,7 +161,7 @@ namespace Xbox _Out_opt_ DirectX::DDSMetaData* ddPixelFormat, _Out_ XboxImage& image) { - return LoadFromDDSMemoryEx(reinterpret_cast(pSource), size, metadata, ddPixelFormat); + return LoadFromDDSMemoryEx(reinterpret_cast(pSource), size, metadata, ddPixelFormat, image); } #endif // __cpp_lib_byte