Wrap aligned memory allocation (now also supports Linux and Android)
This commit is contained in:
parent
d89b45eaf2
commit
55cbdbeedf
1 changed files with 27 additions and 4 deletions
|
|
@ -44,6 +44,30 @@ struct Vertex {
|
||||||
float color[3];
|
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
|
class VulkanExample : public VulkanExampleBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
@ -99,8 +123,7 @@ public:
|
||||||
~VulkanExample()
|
~VulkanExample()
|
||||||
{
|
{
|
||||||
if (uboDataDynamic.model) {
|
if (uboDataDynamic.model) {
|
||||||
// todo: linux, android
|
alignedFree(uboDataDynamic.model);
|
||||||
_aligned_free(uboDataDynamic.model);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up used Vulkan resources
|
// Clean up used Vulkan resources
|
||||||
|
|
@ -393,8 +416,8 @@ public:
|
||||||
|
|
||||||
size_t bufferSize = OBJECT_INSTANCES * dynamicAlignment;
|
size_t bufferSize = OBJECT_INSTANCES * dynamicAlignment;
|
||||||
|
|
||||||
//todo: _algined_malloc only windows
|
uboDataDynamic.model = (glm::mat4*)alignedAlloc(bufferSize, dynamicAlignment);
|
||||||
uboDataDynamic.model = (glm::mat4*)_aligned_malloc(bufferSize, dynamicAlignment);
|
assert(uboDataDynamic.model);
|
||||||
|
|
||||||
std::cout << "minUniformBufferOffsetAlignment = " << uboAlignment << std::endl;
|
std::cout << "minUniformBufferOffsetAlignment = " << uboAlignment << std::endl;
|
||||||
std::cout << "dynamicAlignment = " << dynamicAlignment << std::endl;
|
std::cout << "dynamicAlignment = " << dynamicAlignment << std::endl;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue