glVertexAttribPointer and double precision buffers


This post will hopefully help people avoid a mistake that cost me several hours of debugging.

In one of my projects I have been working with OpenGL 4 double precision vertex buffers. As the buffer data was passed to the shader as a generic attribute, I have been using glVertexAttribPointer to specify the attribute location in the data stream. This worked fine with floating point data.

When I needed to switch to double precision, I checked the glVertexAttribPointer specification: this function accepts GL_DOUBLE as an input type: therefore, I assumed that change was all I needed to pass double precision data to my shader. I noticed two other version of the attrib pointer function existed (glVertexAttribIPointer and glVertexAttribLPointer) but I thought they were just legacy functions or alternatives to using glVertexAttribPointer with GL_DOUBLE or GL_INT type specifiers.

Things did not work as I expected. trying to pass double precision data to the shader caused a crash deep into the OpenGL driver. I double-checked the buffer structure and size, and everything looked correct. Switching back to single precision made my program happy again.

Turns out the problem is that GL_DOUBLE in glVertexAttribPointer does not mean what I thought it meant. glVertexAttribPointer can only specify attribute locations for single precision vertex buffers. The input data can be of other types (double, int etc), hence the type specifier, but it will always be internally converted to a single-precision float. Honestly, I think the OpenGL docs should underscore this difference: using the functions incorrectly won't cause an error with the functions, but your program will explode without a lot of feedback on where the problem is.

To create a true double precision vertex attribute, you need to use glVertexAttribLPointer (same thing with glVertexAttribIPointer and integer attributes). Switching from glVertexAttribPointer to glVertexAttribLPointer when my input data was double precision fixed the crash.

Comments

haseeb said…
birthday wishes for your long distance husband or boyfriend. Guaranteed to be a hit, whether he's into romance, humor, poems or quotes.https://wishesquotz.com/long-distance-birthday-wishes-for-wife/

Popular posts from this blog

Parallel Beam Tracing and Visualization of 200 Million Sonar Points

Parallel Gaussian Elimination Using MPI

Flickering 2D transform elements in Chrome: a simple fix