Problem with dynamicAlign allocation

Hi, I’ve been playing with dynamic uniform buffer for and everything went smooth until I tried my code on AMD R7 445M.
I tried sasha willems example about dynamicUniformBuffer and it crashed as well.
here is the difference between all adapter and the R7 445M:
AMD R7 : sizeof(Mystruct) = 80, minUniformBufferOffsetAlignment = 4 , alignSize = 80
OTHERs: sizeof(Mystruct) = 80, minUniformBufferOffsetAlignment = 64 , alignSize = 128

here is the code to calculate alignSize:

size_t minUboAlignment = _gpuProperties.limits.minUniformBufferOffsetAlignment;
	size_t alignedSize = originalSize;
	if (minUboAlignment > 0) {
		alignedSize = (alignedSize + minUboAlignment - 1) & ~(minUboAlignment - 1);

The Specifiaction says AlignedSize should be a multiple of minUniformBufferOffsetAlignment , and obviously 80 is a multiple of 4 but poxis_memalign will fail.

int res = posix_memalign(&data, alignment, size);
if (res != 0)
	data = nullptr;

To make it work I changed the alignedSize to

alignedSize = round_up_power_of_2(80, minUniformBufferOffsetAlignment );

this one will return the highest value between minUniformBufferOffsetAlignment and the round_up_powerof2 value. here it will be 128

then both my code and Sashawillems example are working.
My question is: As posix_memalign Only works with power of 2. is there another function to do the allocation or I will have to do a manual padding myself and use those usual new/malloc/calloc to allocate multiple of 4 instead of power of 2?

I didn’t get much answers so I will try to answer it myself in case somebody else have the same issue.
First I’ll correct the problem. they is nothing to do with the fact that the AMD R7 has a minUniformBufferOffsetAlignment set to 4.

I can reproduce the same issue with the new GPU. I just have to set a size still multiple of minUniformBufferOffsetAlignment but not a power of 2 for example:

size = 129 , using sasha Willems formula I will obtain 192 = multiple of 64 (3 x 64) . All examples I saw online are using the same formula to get the alignment. so all example failed to produce a good result if you set a value higher than 128 and lower than 192 or higher than 256 but lower than 512 - 64

A rule will be it doesn’t work if you value is in range:
[powof2 + 1, nexPowof2 - minUniformBufferOffsetAlignment ]

The solutions:
I tried 2 solutions that works, the first one is to round_up_nextPower_of_2 higher or equal to minUniformBufferOffsetAlignment , the second is to keep the formula but use malloc/calloc.

now I have 1 issue and 1 question.
if I use the first solution:
alignment = power_of_2(size);

aligment = alignment > **minUniformBufferOffsetAlignment** ? alignment : **minUniformBufferOffsetAlignment**

the problem is the waste of memory, in my example I will have

allocSize = 50000 * aligment

that is 12,800,000 = 50000 x 256 when using the second method 9,600,000 = 50000 x 192

the question is if I can use 192 and malloc and it works well (at least on 3 different system) why use poxis_memalign with 256?

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.