Added make_AlignedArray helpers to scoped.h

This commit is contained in:
Chuck Walbourn 2021-01-04 17:54:00 -08:00
Родитель 122bcdf0b6
Коммит 6dd6455de4
6 изменённых файлов: 68 добавлений и 13 удалений

Просмотреть файл

@ -13,12 +13,12 @@ project(DirectXMesh
option(BUILD_TOOLS "Build meshconvert" ON)
# Includes the support for DX 12 input layouts
# Includes the support for DirectX 12 input layouts
option(BUILD_DX12 "Build with DirectX12 Runtime support" ON)
option(ENABLE_CODE_ANALYSIS "Use Static Code Analysis on build" OFF)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
@ -130,8 +130,7 @@ install(FILES ${LIBRARY_HEADERS}
install(FILES ${UTILS_HEADERS}
DESTINATION utils)
install(
FILES
install(FILES
${PROJECT_BINARY_DIR}/cmake/${PACKAGE_NAME}-config.cmake
${PROJECT_BINARY_DIR}/cmake/${PACKAGE_NAME}-config-version.cmake
DESTINATION cmake/)

Просмотреть файл

@ -24,7 +24,7 @@ namespace
_In_reads_(nVerts) const XMFLOAT3* positions, size_t nVerts,
bool cw, _Out_writes_(nVerts) XMFLOAT3* normals) noexcept
{
ScopedAlignedArrayXMVECTOR temp(static_cast<XMVECTOR*>(_aligned_malloc(sizeof(XMVECTOR) * nVerts, 16)));
auto temp = make_AlignedArrayXMVECTOR(nVerts);
if (!temp)
return E_OUTOFMEMORY;
@ -93,7 +93,7 @@ namespace
_In_reads_(nVerts) const XMFLOAT3* positions, size_t nVerts,
bool cw, _Out_writes_(nVerts) XMFLOAT3* normals) noexcept
{
ScopedAlignedArrayXMVECTOR temp(static_cast<XMVECTOR*>(_aligned_malloc(sizeof(XMVECTOR) * nVerts, 16)));
auto temp = make_AlignedArrayXMVECTOR(nVerts);
if (!temp)
return E_OUTOFMEMORY;
@ -183,7 +183,7 @@ namespace
_In_reads_(nVerts) const XMFLOAT3* positions, size_t nVerts,
bool cw, _Out_writes_(nVerts) XMFLOAT3* normals) noexcept
{
ScopedAlignedArrayXMVECTOR temp(static_cast<XMVECTOR*>(_aligned_malloc(sizeof(XMVECTOR) * nVerts, 16)));
auto temp = make_AlignedArrayXMVECTOR(nVerts);
if (!temp)
return E_OUTOFMEMORY;

Просмотреть файл

@ -41,7 +41,7 @@ namespace
static constexpr float EPSILON = 0.0001f;
static const XMVECTORF32 s_flips = { { { 1.f, -1.f, -1.f, 1.f } } };
ScopedAlignedArrayXMVECTOR temp(static_cast<XMVECTOR*>(_aligned_malloc(sizeof(XMVECTOR) * nVerts * 2, 16)));
auto temp = make_AlignedArrayXMVECTOR(uint64_t(nVerts) * 2);
if (!temp)
return E_OUTOFMEMORY;

Просмотреть файл

@ -107,9 +107,11 @@ public:
mTempSize = mVerts[j];
}
mTempBuffer.reset(static_cast<XMVECTOR*>(_aligned_malloc(sizeof(XMVECTOR) * mTempSize, 16)));
if (!mTempBuffer)
auto temp = make_AlignedArrayXMVECTOR(mTempSize);
if (!temp)
mTempSize = 0;
mTempBuffer.swap(temp);
}
return mTempBuffer.get();

Просмотреть файл

@ -107,9 +107,11 @@ public:
mTempSize = mVerts[j];
}
mTempBuffer.reset(static_cast<XMVECTOR*>(_aligned_malloc(sizeof(XMVECTOR) * mTempSize, 16)));
if (!mTempBuffer)
auto temp = make_AlignedArrayXMVECTOR(mTempSize);
if (!temp)
mTempSize = 0;
mTempBuffer.swap(temp);
}
return mTempBuffer.get();

Просмотреть файл

@ -9,8 +9,11 @@
#pragma once
#include <assert.h>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <malloc.h>
//---------------------------------------------------------------------------------
@ -18,10 +21,59 @@ struct aligned_deleter { void operator()(void* p) noexcept { _aligned_free(p); }
using ScopedAlignedArrayFloat = std::unique_ptr<float[], aligned_deleter>;
inline ScopedAlignedArrayFloat make_AlignedArrayFloat(uint64_t count)
{
uint64_t size = sizeof(float) * count;
if (size > static_cast<uint64_t>(UINT32_MAX))
return nullptr;
auto ptr = _aligned_malloc(static_cast<size_t>(size), 16);
return ScopedAlignedArrayFloat(static_cast<float*>(ptr));
}
using ScopedAlignedArrayXMVECTOR = std::unique_ptr<DirectX::XMVECTOR[], aligned_deleter>;
inline ScopedAlignedArrayXMVECTOR make_AlignedArrayXMVECTOR(uint64_t count)
{
uint64_t size = sizeof(DirectX::XMVECTOR) * count;
if (size > static_cast<uint64_t>(UINT32_MAX))
return nullptr;
auto ptr = _aligned_malloc(static_cast<size_t>(size), 16);
return ScopedAlignedArrayXMVECTOR(static_cast<DirectX::XMVECTOR*>(ptr));
}
//---------------------------------------------------------------------------------
struct handle_closer { void operator()(HANDLE h) noexcept { assert(h != INVALID_HANDLE_VALUE); if (h) CloseHandle(h); } };
using ScopedHandle = std::unique_ptr<void, handle_closer>;
inline HANDLE safe_handle(HANDLE h) noexcept { return (h == INVALID_HANDLE_VALUE) ? nullptr : h; }
//---------------------------------------------------------------------------------
struct find_closer { void operator()(HANDLE h) noexcept { assert(h != INVALID_HANDLE_VALUE); if (h) FindClose(h); } };
using ScopedFindHandle = std::unique_ptr<void, find_closer>;
//---------------------------------------------------------------------------------
class auto_delete_file
{
public:
auto_delete_file(HANDLE hFile) noexcept : m_handle(hFile) {}
auto_delete_file(const auto_delete_file&) = delete;
auto_delete_file& operator=(const auto_delete_file&) = delete;
~auto_delete_file()
{
if (m_handle)
{
FILE_DISPOSITION_INFO info = {};
info.DeleteFile = TRUE;
(void)SetFileInformationByHandle(m_handle, FileDispositionInfo, &info, sizeof(info));
}
}
void clear() noexcept { m_handle = nullptr; }
private:
HANDLE m_handle;
};