XNA::AxisAlignedBox

Started by
1 comment, last by ivarboms 12 years, 5 months ago
I have a class that has data member:

std::vector<XNA::AxisAlignedBox> boxes;

I am getting error C2719: '_Val': formal parameter with __declspec(align('16')) won't be aligned

Is there a way to have a vector of XNA::AxisAlignedBox?
-----Quat
Advertisement
some of the xna data types can't be stored into arrays or vectors, data types like xmvector and xmmatrix. I had a really good link that explained exactly why, but i can't seem to find it now. but the idea is that when doing calculations with these, each of the components get their own thread to do the processing. I can't remember the details, but I'm afraid you might be out of luck, probably because AxisAlignedBox uses one of the xna data types like xmvector.


just found the link:
http://www.asawicki.info/news.php5?x=tag&tag=directx
The problem is that XNA::AxisAlignedBox (and XMVECTOR) has alignment requirements, which VC's std::vector does not support by default.
The reason you're getting an error is because vector's resize (I think) is taking a parameter by value, which VC does not support for aligned types (or something along those lines).

There are a few solutions to this, such as editing that one function in <vector> to take its parameter by const reference instead of value (double click on your error to find the exact line it's on), or using a different dynamic array, such as btAlignedObjectArray.

If you go with the header editing solution, you're going to need an STL compatible allocator which supports aligned allocations. The easiest way to make this is making a class which derives from std::allocator, and simply override allocate and deallocate functions with _aligned_malloc and _aligned free.
You can use aligned objects in a vector with the default STL allocator (it will compile just fine). However, the elements will not be guaranteed to be aligned properly, so your program will likely end up crashing about 75% of the time if you do this (the last 25% is you getting lucky and the elements just happened to get aligned to 16-byte boundaries). Not a good plan.

This topic is closed to new replies.

Advertisement