From 55cbdbeedf45e321c63bad8b72b822d0a7af8579 Mon Sep 17 00:00:00 2001 From: saschawillems Date: Fri, 23 Dec 2016 11:21:01 +0100 Subject: [PATCH] Wrap aligned memory allocation (now also supports Linux and Android) --- dynamicuniformbuffer/dynamicuniformbuffer.cpp | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/dynamicuniformbuffer/dynamicuniformbuffer.cpp b/dynamicuniformbuffer/dynamicuniformbuffer.cpp index daa04ab4..e454094c 100644 --- a/dynamicuniformbuffer/dynamicuniformbuffer.cpp +++ b/dynamicuniformbuffer/dynamicuniformbuffer.cpp @@ -44,6 +44,30 @@ struct Vertex { float color[3]; }; +// Wrapper functions for aligned memory allocation +// There is currently no standard for this in C++ that works across all platforms and vendors, so we abstract this +void* alignedAlloc(size_t size, size_t alignment) +{ + void *data = nullptr; +#ifdef _MSC_VER + data = _aligned_malloc(size, alignment); +#else + int res = posix_memalign(&data, alignment, size); + if (res != 0) + data = nullptr; +#endif + return data; +} + +void alignedFree(void* data) +{ +#ifdef _MSC_VER + _aligned_free(data); +#else + free(data); +#endif +} + class VulkanExample : public VulkanExampleBase { public: @@ -99,8 +123,7 @@ public: ~VulkanExample() { if (uboDataDynamic.model) { - // todo: linux, android - _aligned_free(uboDataDynamic.model); + alignedFree(uboDataDynamic.model); } // Clean up used Vulkan resources @@ -393,8 +416,8 @@ public: size_t bufferSize = OBJECT_INSTANCES * dynamicAlignment; - //todo: _algined_malloc only windows - uboDataDynamic.model = (glm::mat4*)_aligned_malloc(bufferSize, dynamicAlignment); + uboDataDynamic.model = (glm::mat4*)alignedAlloc(bufferSize, dynamicAlignment); + assert(uboDataDynamic.model); std::cout << "minUniformBufferOffsetAlignment = " << uboAlignment << std::endl; std::cout << "dynamicAlignment = " << dynamicAlignment << std::endl;