Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / General Programming / Mem alignment and OpenGL Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
Redeemer

January 27, 2005, 07:21 AM

Hi,

Q1: I am using __declspec(align(16)) to align the variable that holds my vertex data. I then use glTexCoordPointer, glVertexPointer and glDrawArrays to draw the vertex buffer, but nothing is drawn. When I remove __declspec(align(16)), recompile and run the code again it works perfectly (No other code has been changed). Any ideas as to why this happens?

Q2: I was wondering how to align memory allocated by a function such as malloc and new. Is it possible to align only variables on the stack?

Thanks,

- Sid

 
Nick

January 27, 2005, 08:17 AM

OpenGL expects your vertex data to be packed together. This is because it has to be able to read the components with the natural offsets. So when you pass it a pointer with data that has an unexpected alignment it will read data from the wrong locations and fail.

Normally you don't have to worry about aligning anything yourself. The only exception I know is variables for SSE assembly instructions. If your application is truely performance critical then I suggest using a professional tool like VTune to locate the bottlenecks. It's quite unlikely that alignment is an issue...

 
Scali

January 27, 2005, 08:47 AM

Q2: I was wondering how to align memory allocated by a function such as malloc and new. Is it possible to align only variables on the stack?


Visual Studio gives you _aligned_malloc();
You could also create your own... It's quite simple...
If you want to align on 4, you can do something like this:
  1. void* aligned = (malloc( size + 3 ) + 3) & ~3;


Basically you allocate some extra slack space, then you increase the pointer up to the next aligned address.

 
Rui Martins

January 27, 2005, 09:12 AM

That code is going to get you into trouble !

If you do exactly that, you can never do

  1. free(aligned);


Because you are freeing the wrong pointer !

You have to keep both pointers, so that you can free the correct one.
Best ideia is to use a data struct for that or a simple pointer object.

Or just use the _align_malloc() if that actually exists, because someone already does the work for you.

 
Scali

January 27, 2005, 11:31 AM

You're right, you can't free an 'aligned' pointer.

With _aligned_malloc() comes _aligned_free() though, so there it's no problem.

 
Redeemer

January 28, 2005, 12:05 AM

Thanks for your help guys. The only reason I was aligning memory was because I was told that in certain cases the mem read and write operations are extremely fast when compared an unaligned memory.

- Sid

 
Nick

January 28, 2005, 05:04 AM

The only reason I was aligning memory was because I was told that in certain cases the mem read and write operations are extremely fast when compared an unaligned memory.

For most data an alignment of 4 is ideal. A 32-bit processor uses 4-byte registers in most situations. An alignment of 8 only becomes useful for MMX and when using lots of double-precision floating-point numbers. And an alignment of 16 is only required for SSE.

Every 'new' and 'malloc' implementation (for 32-bit processors) is supposed to return a pointer which is 4-byte aligned. So in general there is absolutely nothing to worry about.

 
Redeemer

February 07, 2005, 02:57 AM

thanks once again guys

 
This thread contains 8 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.