Michael I Gold Michael I Gold NVIDIA Corporation NVIDIA Corporation OpenGL Interoperability OpenGL Interoperability
Jan 18, 2015
Michael I GoldMichael I GoldNVIDIA CorporationNVIDIA Corporation
OpenGL InteroperabilityOpenGL Interoperability
What is Interoperability?
•Share memory objects between APIs or contexts within an API
•Share within a device, or between devices
• Implicit or explicit synchronization
Who can play?
•CUDA•OpenCL•OpenGL•Direct3D
CUDA Interoperability
• Import OpenGL buffer objects as CUDA device pointers–Read or write buffer memory directly
from a kernel
– Implicit synchronization within a CPU thread
•Texture support coming soon
CUDA Interop: ExampleApplication
OpenGLdriver
CUDAdriver
Quadro or GeForce
display
Tesla or
GeForceInterop
fast
•Multi-card interop fastest with GL on Quadro. Transfer between cards w/o CPU intervention
•GeForce performs indirect copy, functional but slower
Interop
faster
CUDA example: Initalization// Create a GL buffer object
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer);
glBufferData(GL_PIXEL_UNPACK_BUFFER, buffer_size, NULL, GL_STREAM_COPY);
// Register the buffer object for CUDA interop
cuGLRegisterBufferObject(buffer);
cuGLSetBufferObjectMapFlags(buffer, CU_GL_MAP_RESOURCE_FLAGS_WRITE_DISCARD);
CUDA example: Per-frame usage// map the buffer, invoke a kernel to populate it, and release it back to OpenGL
cuGLMapBufferObjectAsync(&dptr, &size, buffer, stream);
call_kernel(dptr, size);
cuGLUnmapBufferObjectAsync(&dptr, &size, buffer, stream);
// Use the buffer as a pixel buffer object to populate a texture
glTexSubImage2D( … , NULL);
CUDA resources
•Drivers, Toolkit and SDKhttp://www.nvidia.com/cuda
•Forumshttp://forums.nvidia.com
OpenCL Interoperability
•Analogous to CUDA–Create cl_mem from textures and
buffers
•Requires explicit synchronization
OpenCL Shared Objects
OpenCL 3D image object
OpenGL renderbuffer
OpenGL buffer OpenCL buffer object
OpenGL texture 2D(or cubemap face)
OpenGL texture 3D
OpenCL 2D image object
OpenCL 2D image object
clCreateFromGLBuffer
clCreateFromGLTexture2D
clCreateFromGLTexture3D
clCreateFromGLRenderbuffer
OpenGL OpenCL
OpenGL Interoperability• Copy between image objects
– 3D memcpy
– No format conversions
• Copy between dimensionalities– E.g. A slice of a 3D texture and a face of a cubemap
are treated equally
• Copy between GPUs– Complements WGL_NV_gpu_affinity
OpenGL Interop: ExampleApplication
OpenGLdriver
OpenGLdriver
Quadro
display
Quadro
Interop
fast
•Quadro only
•Transfer between cards, no host copy
•Rendering window constrained to affinity GPU
GPU affinity
Offscreen buffer
OpenGL Interop: UsagewglCopyImageSubDataNV(
hSrcRC, srcName, srcTarget, srcLevel, srcX, srcY, srcZ,
hDstRC, dstName, dstTarget, dstLevel, dstX, dstY, dstZ,
width, height, depth);
OpenGL Interop: Resources• NV_copy_image extension specification
http://developer.download.nvidia.com/ opengl/specs/GL_NV_copy_image.txt
• NV_gpu_affinity extension specificationhttp://developer.download.nvidia.com/
opengl/specs/WGL_NV_gpu_affinity.txt
Direct3D Interoperability
• Import Direct3D buffer and image resources into OpenGL
– Vertex and Index buffers -> buffer objects
– Textures and surfaces -> textures and renderbuffers
• Currently WinXP / Direct3D 9 only
• Experimental extension– WGL_NVX_DX_interop
Direct3D Interop: ExampleApplication
OpenGLdriver
DXdriver
Quadroor
GeForce
display
Interop
fast
•OGL can map DX buffers, not the other way
•Single GPU only (at this time)