Web Development

CUDA, Supercomputing for the Masses: Part 18

By Rob Farber, May 27, 2010

Using Vertex Buffer Objects with CUDA and OpenGL

The simpleVBO.cpp File

The Part 15 simplePBO.cpp file was modified in several ways to create the simpleVBO.cpp file shown below. The only significant change is to the display routine to utilize both 3D vertex and color data as discussed below. The remaining changes are straight-forward refactoring to support the CUDA 3.0 and pre-3.0 graphics interoperability APIs as well as two graphics interoperability buffers colorVBO and vertexVBO. Note that colorVBO is created to hold a uchar4 color array and vertexVBO a float4 array of vertex values.

The changes to simplePBO.cpp can be summarized as follows:

The call to launch_kernel now includes parameters to pass both vertex and color arrays.

createVBO was refactored to use a typedef mappedBuffer_t structure. This allows float4 and uchar4 arrays to be created as well as facilitating CUDA 3.0 and pre-3.0 graphics interoperability APIs.

cleanupCuda frees both vertexVBO and colorVBO structures.

runCuda maps and unmaps both vertexVBO and colorVBO objects as well as passing the appropriate pointers to launch_kernel.

As can be seen, the call to renderCuda has been modified to support several drawing modes based on a parameter drawMode. Once inside the renderCuda routine, the following code tells OpenGL to bind the buffer vbo as a float4 vertex array:

As discussed earlier in this article, primitive restart was utilized to render colored surfaces. This example takes the unusual approach of declaring the pointer to the qIndices array as a static pointer within the case statement that only gets allocated and initialized once during the life of the program. This was purposely done to help the reader experiment with alternative drawing methods, index layout, and drawing modes by keeping the allocation, initialization, and usage local to the drawing mode. As a general rule, such use of a static variable should be avoided.

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!