diff --git a/CMakeLists.txt b/CMakeLists.txt index 12f2a91..17c7d13 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +28,7 @@ option(ENABLE_CODE_ANALYSIS "Use Static Code Analysis on build" OFF) # NOTE requires adding DirectXTexEXR.h/.cpp source files (vcpkg does this automatically) option(ENABLE_OPENEXR_SUPPORT "Build with OpenEXR support" OFF) -set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) @@ -74,8 +74,7 @@ if(BUILD_DX11) DirectXTex/BCDirectCompute.h DirectXTex/BCDirectCompute.cpp DirectXTex/DirectXTexCompressGPU.cpp - DirectXTex/DirectXTexD3D11.cpp - DirectXTex/Shaders/Compiled/BC6HEncode_EncodeBlockCS.inc) + DirectXTex/DirectXTexD3D11.cpp) endif() if(BUILD_DX12) set(LIBRARY_SOURCES ${LIBRARY_SOURCES} @@ -92,9 +91,10 @@ if(ENABLE_OPENEXR_SUPPORT) DirectXTex/DirectXTexEXR.cpp) endif() -add_library(${PROJECT_NAME} STATIC ${LIBRARY_SOURCES} ${LIBRARY_HEADERS}) - if(BUILD_DX11) + set(LIBRARY_SOURCES ${LIBRARY_SOURCES} + DirectXTex/Shaders/Compiled/BC6HEncode_EncodeBlockCS.inc) + add_custom_command( OUTPUT "${PROJECT_SOURCE_DIR}/DirectXTex/Shaders/Compiled/BC6HEncode_EncodeBlockCS.inc" MAIN_DEPENDENCY "${PROJECT_SOURCE_DIR}/DirectXTex/Shaders/CompileShaders.cmd" @@ -105,6 +105,8 @@ if(BUILD_DX11) USES_TERMINAL) endif() +add_library(${PROJECT_NAME} STATIC ${LIBRARY_SOURCES} ${LIBRARY_HEADERS}) + source_group(${PROJECT_NAME} REGULAR_EXPRESSION DirectXTex/*.*) target_include_directories(${PROJECT_NAME} PUBLIC @@ -160,8 +162,7 @@ install(EXPORT ${PROJECT_NAME}-targets install(FILES ${LIBRARY_HEADERS} DESTINATION include) -install( - FILES +install(FILES ${PROJECT_BINARY_DIR}/cmake/${PACKAGE_NAME}-config.cmake ${PROJECT_BINARY_DIR}/cmake/${PACKAGE_NAME}-config-version.cmake DESTINATION cmake/) diff --git a/DirectXTex/DirectXTexCompressGPU.cpp b/DirectXTex/DirectXTexCompressGPU.cpp index 8769f50..7610025 100644 --- a/DirectXTex/DirectXTexCompressGPU.cpp +++ b/DirectXTex/DirectXTexCompressGPU.cpp @@ -59,7 +59,7 @@ namespace return E_POINTER; } - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc((sizeof(XMVECTOR) * srcImage.width), 16))); + auto scanline = make_AlignedArrayXMVECTOR(srcImage.width); if (!scanline) { image.Release(); diff --git a/DirectXTex/DirectXTexConvert.cpp b/DirectXTex/DirectXTexConvert.cpp index b443cb8..fecb43d 100644 --- a/DirectXTex/DirectXTexConvert.cpp +++ b/DirectXTex/DirectXTexConvert.cpp @@ -2640,7 +2640,7 @@ HRESULT DirectX::_ConvertToR16G16B16A16(const Image& srcImage, ScratchImage& ima if (FAILED(hr)) return hr; - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc((sizeof(XMVECTOR) * srcImage.width), 16))); + auto scanline = make_AlignedArrayXMVECTOR(srcImage.width); if (!scanline) { image.Release(); @@ -2693,7 +2693,7 @@ HRESULT DirectX::_ConvertFromR16G16B16A16(const Image& srcImage, const Image& de if (srcImage.width != destImage.width || srcImage.height != destImage.height) return E_FAIL; - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc((sizeof(XMVECTOR) * srcImage.width), 16))); + auto scanline = make_AlignedArrayXMVECTOR(srcImage.width); if (!scanline) return E_OUTOFMEMORY; @@ -4597,7 +4597,7 @@ namespace if (filter & TEX_FILTER_DITHER_DIFFUSION) { // Error diffusion dithering (aka Floyd-Steinberg dithering) - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc((sizeof(XMVECTOR)*(width * 2 + 2)), 16))); + auto scanline = make_AlignedArrayXMVECTOR(uint64_t(width) * 2 + 2); if (!scanline) return E_OUTOFMEMORY; @@ -4620,7 +4620,7 @@ namespace } else { - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc((sizeof(XMVECTOR)*width), 16))); + auto scanline = make_AlignedArrayXMVECTOR(width); if (!scanline) return E_OUTOFMEMORY; diff --git a/DirectXTex/DirectXTexImage.cpp b/DirectXTex/DirectXTexImage.cpp index 2ff9354..0e12940 100644 --- a/DirectXTex/DirectXTexImage.cpp +++ b/DirectXTex/DirectXTexImage.cpp @@ -786,7 +786,7 @@ bool ScratchImage::IsAlphaAllOpaque() const noexcept } else { - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc((sizeof(XMVECTOR)*m_metadata.width), 16))); + auto scanline = make_AlignedArrayXMVECTOR(m_metadata.width); if (!scanline) return false; diff --git a/DirectXTex/DirectXTexMipmaps.cpp b/DirectXTex/DirectXTexMipmaps.cpp index c496ac4..b6fa404 100644 --- a/DirectXTex/DirectXTexMipmaps.cpp +++ b/DirectXTex/DirectXTexMipmaps.cpp @@ -142,7 +142,7 @@ namespace assert(srcImage.width == destImage.width); assert(srcImage.height == destImage.height); - ScopedAlignedArrayXMVECTOR scanline(reinterpret_cast(_aligned_malloc((sizeof(XMVECTOR)*srcImage.width), 16))); + auto scanline = make_AlignedArrayXMVECTOR(srcImage.width); if (!scanline) { return E_OUTOFMEMORY; @@ -214,13 +214,13 @@ namespace { coverage = 0.0f; - ScopedAlignedArrayXMVECTOR row0(reinterpret_cast(_aligned_malloc((sizeof(XMVECTOR)*srcImage.width), 16))); + auto row0 = make_AlignedArrayXMVECTOR(srcImage.width); if (!row0) { return E_OUTOFMEMORY; } - ScopedAlignedArrayXMVECTOR row1(reinterpret_cast(_aligned_malloc((sizeof(XMVECTOR)*srcImage.width), 16))); + auto row1 = make_AlignedArrayXMVECTOR(srcImage.width); if (!row1) { return E_OUTOFMEMORY; @@ -896,7 +896,7 @@ namespace size_t height = mipChain.GetMetadata().height; // Allocate temporary space (2 scanlines) - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc((sizeof(XMVECTOR)*width * 2), 16))); + auto scanline = make_AlignedArrayXMVECTOR(uint64_t(width) * 2); if (!scanline) return E_OUTOFMEMORY; @@ -983,7 +983,7 @@ namespace return E_FAIL; // Allocate temporary space (3 scanlines) - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc((sizeof(XMVECTOR)*width * 3), 16))); + auto scanline = make_AlignedArrayXMVECTOR(uint64_t(width) * 3); if (!scanline) return E_OUTOFMEMORY; @@ -1074,7 +1074,7 @@ namespace size_t height = mipChain.GetMetadata().height; // Allocate temporary space (3 scanlines, plus X and Y filters) - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc((sizeof(XMVECTOR)*width * 3), 16))); + auto scanline = make_AlignedArrayXMVECTOR(uint64_t(width) * 3); if (!scanline) return E_OUTOFMEMORY; @@ -1185,7 +1185,7 @@ namespace size_t height = mipChain.GetMetadata().height; // Allocate temporary space (5 scanlines, plus X and Y filters) - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc((sizeof(XMVECTOR)*width * 5), 16))); + auto scanline = make_AlignedArrayXMVECTOR(uint64_t(width) * 5); if (!scanline) return E_OUTOFMEMORY; @@ -1373,7 +1373,7 @@ namespace size_t height = mipChain.GetMetadata().height; // Allocate initial temporary space (1 scanline, accumulation rows, plus X and Y filters) - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc(sizeof(XMVECTOR) * width, 16))); + auto scanline = make_AlignedArrayXMVECTOR(width); if (!scanline) return E_OUTOFMEMORY; @@ -1462,9 +1462,10 @@ namespace } else { - rowAcc->scanline.reset(static_cast(_aligned_malloc(sizeof(XMVECTOR) * nwidth, 16))); - if (!rowAcc->scanline) + auto nscanline = make_AlignedArrayXMVECTOR(nwidth); + if (!nscanline) return E_OUTOFMEMORY; + rowAcc->scanline.swap(nscanline); } memset(rowAcc->scanline.get(), 0, sizeof(XMVECTOR) * nwidth); @@ -1641,7 +1642,7 @@ namespace size_t height = mipChain.GetMetadata().height; // Allocate temporary space (2 scanlines) - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc((sizeof(XMVECTOR)*width * 2), 16))); + auto scanline = make_AlignedArrayXMVECTOR(uint64_t(width) * 2); if (!scanline) return E_OUTOFMEMORY; @@ -1790,7 +1791,7 @@ namespace return E_FAIL; // Allocate temporary space (5 scanlines) - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc((sizeof(XMVECTOR)*width * 5), 16))); + auto scanline = make_AlignedArrayXMVECTOR(uint64_t(width) * 5); if (!scanline) return E_OUTOFMEMORY; @@ -1959,7 +1960,7 @@ namespace size_t height = mipChain.GetMetadata().height; // Allocate temporary space (5 scanlines, plus X/Y/Z filters) - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc((sizeof(XMVECTOR)*width * 5), 16))); + auto scanline = make_AlignedArrayXMVECTOR(uint64_t(width) * 5); if (!scanline) return E_OUTOFMEMORY; @@ -2152,7 +2153,7 @@ namespace size_t height = mipChain.GetMetadata().height; // Allocate temporary space (17 scanlines, plus X/Y/Z filters) - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc((sizeof(XMVECTOR)*width * 17), 16))); + auto scanline = make_AlignedArrayXMVECTOR(uint64_t(width) * 17); if (!scanline) return E_OUTOFMEMORY; @@ -2533,7 +2534,7 @@ namespace size_t height = mipChain.GetMetadata().height; // Allocate initial temporary space (1 scanline, accumulation rows, plus X/Y/Z filters) - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc(sizeof(XMVECTOR) * width, 16))); + auto scanline = make_AlignedArrayXMVECTOR(width); if (!scanline) return E_OUTOFMEMORY; @@ -2616,10 +2617,10 @@ namespace } else { - size_t bytes = sizeof(XMVECTOR) * nwidth * nheight; - sliceAcc->scanline.reset(static_cast(_aligned_malloc(bytes, 16))); - if (!sliceAcc->scanline) + auto nscanline = make_AlignedArrayXMVECTOR(uint64_t(nwidth) * uint64_t(nheight)); + if (!nscanline) return E_OUTOFMEMORY; + sliceAcc->scanline.swap(nscanline); } memset(sliceAcc->scanline.get(), 0, sizeof(XMVECTOR) * nwidth * nheight); diff --git a/DirectXTex/DirectXTexMisc.cpp b/DirectXTex/DirectXTexMisc.cpp index 20056ab..b779570 100644 --- a/DirectXTex/DirectXTexMisc.cpp +++ b/DirectXTex/DirectXTexMisc.cpp @@ -33,7 +33,7 @@ namespace const size_t width = image1.width; - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc((sizeof(XMVECTOR)*width) * 2, 16))); + auto scanline = make_AlignedArrayXMVECTOR(uint64_t(width) * 2); if (!scanline) return E_OUTOFMEMORY; @@ -184,7 +184,7 @@ namespace const size_t width = image.width; - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc((sizeof(XMVECTOR)*width), 16))); + auto scanline = make_AlignedArrayXMVECTOR(width); if (!scanline) return E_OUTOFMEMORY; @@ -222,7 +222,7 @@ namespace const size_t width = srcImage.width; - ScopedAlignedArrayXMVECTOR scanlines(static_cast(_aligned_malloc((sizeof(XMVECTOR)*width*2), 16))); + auto scanlines = make_AlignedArrayXMVECTOR(uint64_t(width) * 2); if (!scanlines) return E_OUTOFMEMORY; @@ -347,7 +347,7 @@ HRESULT DirectX::CopyRectangle( uint8_t* pDest = dstImage.pixels + (yOffset * dstImage.rowPitch) + (xOffset * dbpp); - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc((sizeof(XMVECTOR)*srcRect.w), 16))); + auto scanline = make_AlignedArrayXMVECTOR(srcRect.w); if (!scanline) return E_OUTOFMEMORY; diff --git a/DirectXTex/DirectXTexNormalMaps.cpp b/DirectXTex/DirectXTexNormalMaps.cpp index 763f0cd..f5375e8 100644 --- a/DirectXTex/DirectXTexNormalMaps.cpp +++ b/DirectXTex/DirectXTexNormalMaps.cpp @@ -92,11 +92,11 @@ namespace return E_FAIL; // Allocate temporary space (4 scanlines and 3 evaluated rows) - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc((sizeof(XMVECTOR)*width * 4), 16))); + auto scanline = make_AlignedArrayXMVECTOR(uint64_t(width) * 4); if (!scanline) return E_OUTOFMEMORY; - ScopedAlignedArrayFloat buffer(static_cast(_aligned_malloc(((sizeof(float) * (width + 2)) * 3), 16))); + auto buffer = make_AlignedArrayFloat((uint64_t(width) + 2) * 3); if (!buffer) return E_OUTOFMEMORY; diff --git a/DirectXTex/DirectXTexPMAlpha.cpp b/DirectXTex/DirectXTexPMAlpha.cpp index 5674df0..139a4e7 100644 --- a/DirectXTex/DirectXTexPMAlpha.cpp +++ b/DirectXTex/DirectXTexPMAlpha.cpp @@ -31,7 +31,7 @@ namespace assert(srcImage.width == destImage.width); assert(srcImage.height == destImage.height); - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc((sizeof(XMVECTOR)*srcImage.width), 16))); + auto scanline = make_AlignedArrayXMVECTOR(srcImage.width); if (!scanline) return E_OUTOFMEMORY; @@ -74,7 +74,7 @@ namespace static_assert(static_cast(TEX_PMALPHA_SRGB) == static_cast(TEX_FILTER_SRGB), "TEX_PMALHPA_SRGB* should match TEX_FILTER_SRGB*"); flags &= TEX_PMALPHA_SRGB; - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc((sizeof(XMVECTOR)*srcImage.width), 16))); + auto scanline = make_AlignedArrayXMVECTOR(srcImage.width); if (!scanline) return E_OUTOFMEMORY; @@ -116,7 +116,7 @@ namespace assert(srcImage.width == destImage.width); assert(srcImage.height == destImage.height); - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc((sizeof(XMVECTOR)*srcImage.width), 16))); + auto scanline = make_AlignedArrayXMVECTOR(srcImage.width); if (!scanline) return E_OUTOFMEMORY; @@ -162,7 +162,7 @@ namespace static_assert(static_cast(TEX_PMALPHA_SRGB) == static_cast(TEX_FILTER_SRGB), "TEX_PMALPHA_SRGB* should match TEX_FILTER_SRGB*"); flags &= TEX_PMALPHA_SRGB; - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc((sizeof(XMVECTOR)*srcImage.width), 16))); + auto scanline = make_AlignedArrayXMVECTOR(srcImage.width); if (!scanline) return E_OUTOFMEMORY; diff --git a/DirectXTex/DirectXTexResize.cpp b/DirectXTex/DirectXTexResize.cpp index 5e845e7..71635b8 100644 --- a/DirectXTex/DirectXTexResize.cpp +++ b/DirectXTex/DirectXTexResize.cpp @@ -251,8 +251,7 @@ namespace assert(srcImage.format == destImage.format); // Allocate temporary space (2 scanlines) - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc( - (sizeof(XMVECTOR) * (srcImage.width + destImage.width)), 16))); + auto scanline = make_AlignedArrayXMVECTOR(srcImage.width + destImage.width); if (!scanline) return E_OUTOFMEMORY; @@ -312,8 +311,7 @@ namespace return E_FAIL; // Allocate temporary space (3 scanlines) - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc( - (sizeof(XMVECTOR) * (srcImage.width * 2 + destImage.width)), 16))); + auto scanline = make_AlignedArrayXMVECTOR(uint64_t(srcImage.width) * 2 + destImage.width); if (!scanline) return E_OUTOFMEMORY; @@ -371,8 +369,7 @@ namespace assert(srcImage.format == destImage.format); // Allocate temporary space (3 scanlines, plus X and Y filters) - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc( - (sizeof(XMVECTOR) * (srcImage.width * 2 + destImage.width)), 16))); + auto scanline = make_AlignedArrayXMVECTOR(uint64_t(srcImage.width) * 2 + destImage.width); if (!scanline) return E_OUTOFMEMORY; @@ -457,8 +454,7 @@ namespace assert(srcImage.format == destImage.format); // Allocate temporary space (5 scanlines, plus X and Y filters) - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc( - (sizeof(XMVECTOR) * (srcImage.width * 4 + destImage.width)), 16))); + auto scanline = make_AlignedArrayXMVECTOR(uint64_t(srcImage.width) * 4 + destImage.width); if (!scanline) return E_OUTOFMEMORY; @@ -619,7 +615,7 @@ namespace using namespace TriangleFilter; // Allocate initial temporary space (1 scanline, accumulation rows, plus X and Y filters) - ScopedAlignedArrayXMVECTOR scanline(static_cast(_aligned_malloc(sizeof(XMVECTOR) * srcImage.width, 16))); + auto scanline = make_AlignedArrayXMVECTOR(srcImage.width); if (!scanline) return E_OUTOFMEMORY; @@ -688,9 +684,10 @@ namespace } else { - rowAcc->scanline.reset(static_cast(_aligned_malloc(sizeof(XMVECTOR) * destImage.width, 16))); - if (!rowAcc->scanline) + auto nscanline = make_AlignedArrayXMVECTOR(destImage.width); + if (!nscanline) return E_OUTOFMEMORY; + rowAcc->scanline.swap(nscanline); } memset(rowAcc->scanline.get(), 0, sizeof(XMVECTOR) * destImage.width); diff --git a/DirectXTex/scoped.h b/DirectXTex/scoped.h index 5b5cfcf..977c627 100644 --- a/DirectXTex/scoped.h +++ b/DirectXTex/scoped.h @@ -10,6 +10,8 @@ #pragma once #include +#include +#include #include #include @@ -19,8 +21,26 @@ struct aligned_deleter { void operator()(void* p) noexcept { _aligned_free(p); } using ScopedAlignedArrayFloat = std::unique_ptr; +inline ScopedAlignedArrayFloat make_AlignedArrayFloat(uint64_t count) +{ + uint64_t size = sizeof(float) * count; + if (size > static_cast(UINT32_MAX)) + return nullptr; + auto ptr = _aligned_malloc(static_cast(size), 16); + return ScopedAlignedArrayFloat(static_cast(ptr)); +} + using ScopedAlignedArrayXMVECTOR = std::unique_ptr; +inline ScopedAlignedArrayXMVECTOR make_AlignedArrayXMVECTOR(uint64_t count) +{ + uint64_t size = sizeof(DirectX::XMVECTOR) * count; + if (size > static_cast(UINT32_MAX)) + return nullptr; + auto ptr = _aligned_malloc(static_cast(size), 16); + return ScopedAlignedArrayXMVECTOR(static_cast(ptr)); +} + //--------------------------------------------------------------------------------- struct handle_closer { void operator()(HANDLE h) noexcept { assert(h != INVALID_HANDLE_VALUE); if (h) CloseHandle(h); } };