• Create Account

Eigen with modern OpenGL

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

#1Ender1618  Members

Posted 25 April 2012 - 10:28 AM

So I'm getting back into OpenGL after a many years of work with D3D (the project I'm working on needs to be cross platform). Wanted to avoid fixed function (and the rest of the deprecated functionality) and stick with modern OpenGL (3.0,4.0).

So then comes the immediate question as to what vector/linear algebra library to use. The code base I am working with is already using Eigen for linear algebra. So i though i should use Eigen for my OpenGL work as well (since many people are saying they are doing this), and it would be a bit of a head ache to use multiple math libraries that were suppose to be doing many of the same things. Then I hit the alignment issue in Eigen, where all fixed sized types are 16 byte aligned.

The typical way i define a vertex in D3D is with a structure e.g.
struct MyVertex
{
Vec3 position;
Vec3 nornal;
Vec2 texcoord;
};


I know I can do something like this in OGL with interleaved VBOs, or i could split the vertices up in to component arrays. The issue is that if I replace Vec3 with Eigen::Vector3f (or the 2 float variants), Vector3f is 16 byte aligned. In the Eigen documentation, it states that it is recommended when declaring a struct containing Eigen fixed size types (such as Vector3f) that you should use the macro EIGEN_MAKE_ALIGNED_OPERATOR_NEW which redefines new to deal with alignment issues: http://eigen.tuxfami...genMembers.html

Would this not wreak havoc with an array of MyVertex, and specifying component offsets (or even component arrays)? Should I not be using Eigen types in my vertex structures or component arrays? Anyone have any experience with using OGL with Eigen as their math library and has dealt with these issues?

There isnt much documentation on the caveats of using OGL with Eigen (or maybe I'm just looking in the wrong place), the support module provided by Eigen seems to focus more on fixed function calls: http://eigen.tuxfami...rt__Module.html

Any insight would be greatly appreciated.

#2tanzanite7  Members

Posted 25 April 2012 - 06:13 PM

Never heard of Eigen before, so can not be of any help there, sadly. Although you are not searching for a new math library i think it would not hurt to drop an alternative lib name anyway. GLM - it is quite convenient, mostly because it mimics glsl and gl conventions in general [including extendability + many extensions. ex: quaternions, simplex noise, etc] (it is also a header-only library - which is also quite convenient). Only complaint i have is that its half-float conversion is terribly inefficient (unless the latest release fixed it).

#3Hodgman  Moderators

Posted 25 April 2012 - 08:24 PM

Would this not wreak havoc with an array of MyVertex? Should I not be using Eigen types in my vertex structures or component arrays?

Yes, you probably shouldn't be using eigen types inside your vertex structs.
Your GL vertex structs are usually packed to be optimal for consumption by the GPU (which usually means: as small as possible), whereas your eigen structs are going to be padded/aligned to be optimal for SIMD processing by a CPU (which usually means allocated on 16-byte aligned boundaries). These two goals are (usually) contradictory, so you should use different types for each goal (CPU-optimised types for CPU-processing, GPU-optimised types for GPU-processing).

#4ggael  Members

Posted 26 April 2012 - 03:16 AM

Only vectorizable fixed vector and matrix types require 16bytes alignment. In other word, this concerns only types which sizes are a multiple of 16 bytes. So Eigen's types will never introduce any padding in your structs.

#5RobinsonUK  Members

Posted 26 April 2012 - 11:40 AM

I had no end of problems when I tried to use a SIMD aware math library in my project (slmath). First I had to allocate on a boundary and second there's a problem with containers in VC++ such that putting such aligned objects into them resulted in seg faults and all kinds of issues (I'm a heavy user of std:. In the end I decided it was too much trouble for the potential gain in speed, so I switched to glm::, which so far has been a joy to use (and program that doesn't seg fault runs much faster than one that does!). I wouldn't rule out using SIMD for very tight, focused algorithms though, and I'm waiting with anticipation for a future where compilers intelligently make use of SIMD themselves, so I don't have to.

#6V-man  Members

Posted 01 May 2012 - 06:27 AM

I had no end of problems when I tried to use a SIMD aware math library in my project (slmath). First I had to allocate on a boundary ...

There is a function in VC++ that allocates on a boundary. I imagine you are using new/delete and searching for a memory location that starts on a boundary by yourself.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];