O'Reilly logo

OpenGL Insights by Christophe Riccio, Patrick Cozzi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mixing Graphics and Compute
with Multiple GPUs
Alina Alt
9.1 Introduction
In recent years, GPU computing has evolved to deliver teraflops of floating point
compute power to desktop systems. This trend has necessitated that scientific and
visualization applications require a mix of compute and graphics capabilities in ad-
dition to efficiently processing large amounts of data. Examples of such applications
include physically based simulations (e.g., particle systems) and image/video process-
ing (e.g., special effects, image recognition, augmented reality, etc.).
To maximize this compute and graphics capability, the applications need to be
designed with interoperability in mind, which allows data passing between compute
and graphics contexts.
Current compute APIs include functions dedicated to interoperability with
OpenGL. To illustrate the concept of graphics and compute API interoperability, the
first section of this chapter uses the CUDA C API. The second part of the chapter
focuses on interoperability on a system scale. In particular, what are the challenges
and benefits of dedicating one GPU for compute and another for graphics? And
how does this translate to application design decisions helping to enable efficient,
cross-GPU, compute and graphics interoperability?
133
9
134 IDiscovering
9.2 Graphics and Compute Interoperability on
an API Level
Since they were developed many years after the development of the graphics APIs,
GPU computing languages and APIs, such as the CUDA C API, which is NVIDIAs
platform computing interface, and OpenCL, which is a cross-platform computing
interface, were tasked with providing a way to interact with graphics API objects to
avoid unnecessary data movement through both the GPU and system memory. As
of OpenGL Version 4.2, there is no OpenGL mechanism for interaction with any
of the compute APIs; therefore, it is exclusively up to each of the compute APIs to
provide such a mechanism. The mechanism of interaction with OpenGL is very
similar for each of the compute APIs, and we will illustrate this mechanism with the
usage of the CUDA C Runtime API [NVIDIA 11]. This section of the chapter will
only deal with interoperability on an API level.
9.2.1 Interoperability Preparation
CUDA and OpenGL interoperability requires a current OpenGL context. Also, the
context must remain current in the interoperability execution thread for the duration
of the interoperability.
Before CUDA can begin working with an OpenGL object, a correspondence be-
tween the object and a CUDA graphics resource must be established. Each OpenGL
object must first be registered with the CUDA context as a CUDA graphics resource.
This is a costly operation, as it can allocate resources in the CUDA context and must
be done only once per object after its creation and before CUDA star ts working with
the object.
There are two registering CUDA API calls: one for buffer objects and another for
texture and renderbuffer objects. Listing 9.1 illustrates registering an OpenGL Pixel
Buffer Object (PBO) with CUDA using cudaGraphicsGLRegisterBuffer.
GLuint imagePBO ;
cudaGraphicsResource_t cudaResource;
//OpenGL buffer creation
glGenBuffers(1, &imagePBO );
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, imagePBO );
glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, size, NULL , GL_DYNAMIC_DRAW);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB ,0);
//Registration with CUDA
cudaGraphicsGLRegisterBuffer(&cudaResource , imagePBO ,
cudaGraphicsRegisterFlagsNone);
Listing 9.1. Registering on OpenGL PBO with CUDA.
9. Mixing Graphics and Compute with Multiple GPUs 135
GLuint imageTex ;
cudaGraphicsResource_t cudaResource;
//OpenGL texture creation
glGenTextures(1, &imageTex );
glBindTexture(GL_TEXTURE_2D , imageTex );
//set texture parameters here
glTexImage2D(GL_TEXTURE_2D ,0, GL_RGBA8UI_EXT , width , height , 0,
GL_RGBA_INTEGER_EXT, GL_UNSIGNED_BYTE , NULL );
glBindTexture(GL_TEXTURE_2D ,0);
//Registration with CUDA
cudaGraphicsGLRegisterImage (&cudaResource , imageTex , GL_TEXTURE_2D ,
cudaGraphicsMapFlagsNone);
Listing 9.2. Registering an OpenGL Texture with CUDA.
OpenGL texture and renderbuffer objects are registered u sing cudaGraphics
GLRegisterImage, which currently supports the following image formats:
GL RED, GL RG, GL RGBA, GL LUMINANCE, GL ALPHA, GL LUMINANCE
ALPHA, GL INTENSITY.
{GL R, GL RG, GL RGBA} X {8, 16, 16F, 32F, 8UI, 16UI,
32UI, 8I, 16I, 32I}.
{GL LUMINANCE, GL ALPHA, GL LUMINANCE ALPHA, GL INTENSITY}
X {8, 16, 16F
ARB, 32F ARB, 8UI EXT, 16UI EXT, 32UI EXT,
8I
EXT, 16I EXT, 32I EXT}.
Note that for brevity’s sake, the list is abbreviated. F or example, {GL
R,GL RG}
x{8x16} would expand into {GL
R8, GL R16, GL RG8, GL RG16}.Themost
up-to-date list can be found in [NVIDIA 01].
Applications that require the usage of textures of unsupported formats have two
options: either perform format conversion to and from the supported format before
and after CUDA-GL interoperability, or have CUDA interact with a PBO and then
copy pixels from the PBO to the texture object.
Listing 9.2 illustrates registering an u nnormalized integer texture with CUDA.
After the application is done with the resource, it should unregister it from the
CUDA context using cudaGraphicsUnregisterResource.
9.2.2 OpenGL Object Interaction
Both OpenGL and CUDA logically partition device memory into two kinds of mem-
ory: texture memory and linear memory (Figure 9.1). An OpenGL buffer object,
which is unformatted device memory, will map to a CUDA linear memory object,
which is a CUDA buffer in device memory that can be referenced via pointer. Simi-
larly, OpenGL texture and renderbuffer objects will map to a CUDA array, which is
an opaque memory layout optimized for texture hardware access, and which will be

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required