Raytrix Light Field SDK  v3.1
Logo
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Properties Events Groups Pages
CUDA/OpenGL interop. functions

Functions to exchange data between CUDA and OpenGL. More...

Functions

static void Rx::Net::ApiLF::RxGlGetContext ()
 Assigns the OpenGL context created by Rx::Net::ApiLF::RxCudaSelectDevice to the calling thread. More...
 
static void Rx::Net::ApiLF::RxGlGetTextureID (Rx::Net::ApiLF::EImgID eImgID, [Out] unsigned% uGlTextureID)
 Get the OpenGl texture ID corresponding to given parameter eImgID. The content of this texture can be updated with the Rx::Net::ApiLF::RxGlUpdateTex function. More...
 
static unsigned Rx::Net::ApiLF::RxGlGetTextureID (Rx::Net::ApiLF::EImgID eImgID)
 Get the OpenGl texture ID corresponding to given parameter eImgID. The content of this texture can be updated with the Rx::Net::ApiLF::RxGlUpdateTex function. More...
 
static void Rx::Net::ApiLF::RxGlGetVersion (int% iMajor, int% iMinor)
 Get OpenGL version of current OpenGL rendering context. More...
 
static void Rx::Net::ApiLF::RxGlReleaseContext ()
 Releases the OpenGL context owned by the calling thread. More...
 
static void Rx::Net::ApiLF::RxGlUpdateTex (unsigned uIntImgIDs)
 Copy CUDA result images to OpenGL textures. More...
 
static void Rx::Net::ApiLF::RxGlUpdateTex (unsigned uIntImgIDs, bool bCreateMipmaps)
 Copy CUDA result images to OpenGL textures. More...
 

Detailed Description

Functions to exchange data between CUDA and OpenGL.

CUDA/OpenGL interop. works as follows. After calling Rx::Net::ApiLF::RxCudaSelectDevice with bUseGL set to true, create an OpenGL rendering context. It is then possible for the Raytrix API to write directly to textures defined by OpenGL, which can in turn be displayed through OpenGL. This avoids having to copy CUDA result images from the CUDA device to host memory and then back again to display them. To work with OpenGL textures you can use Rx::Net::ApiLF::RxGlGetTextureID with the image id (Rx::Net::ApiLF::EImgID) of your choice, to receive the corresponding OpenGL texture id. For example if you want to work with the Rx::Net::ApiLF::EImgID::RefocusBasic_VirtualSpace image you can generate it, using Rx::Net::ApiLF::RxRefocusBasic. The result of this operation is stored on the device.

It is copied to the OpenGL texture by calling Rx::Net::ApiLF::RxGlUpdateTex(Rx::Net::ApiLF::EImgID::RefocusBasic_VirtualSpace).

Call the Rx::Net::ApiLF::RxGlUpdateTex function each time the internal refocused image has changed.

On the OpenGL side you have to display the texture by mapping it to a plane or some other geometric object. However, note that CUDA currently only supports writing to unnormalized textures, which means that you have to define a shader to display a texture written to by the Raytrix API. The texture is initialized by the Raytrix API with the appropriate size and type, the first time Rx::Net::ApiLF::RxGlUpdateTex is called, or if the internal result image size of the Raytrix API has changed. For example, for an RGBA image with 8bit per color channel, and dimensions iWidth by iHeight, the texture is created internally as

glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8UI, iWidth, iHeight, 0, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE, pImageData);

The most basic shader to display such a texture on the OpenGL side is shown below. First of all, the vertex shader:

varying out vec2 vTexPos;
void main()
{
vec2 texpos = gl_MultiTexCoord0.st;
vTexPos = vec4( texpos, 0.0, 0.0 );
gl_Position = ftransform();
}

The fragment shader for color images:

#version 130
in vec2 vTexPos;
out vec4 gl_FragColor;
// The image texture
uniform usampler2D texImg;
void main()
{
vec3 vCol = vec3(textureLod(texImg, vTexPos, 0).xyz) / 255.0;
gl_FragColor = vec4(vCol, 1.0);
}

The fragment shader for luminance images:

#version 130
in vec2 vTexPos;
out vec4 gl_FragColor;
// The image texture
uniform usampler2D texImg;
void main()
{
float fCol = float(textureLod(texImg, vTexPos, 0).x) / 255.0;
gl_FragColor = vec4(fCol, fCol, fCol, 1.0);
}

Function Documentation

static void Rx::Net::ApiLF::RxGlGetContext ( )
static

Assigns the OpenGL context created by Rx::Net::ApiLF::RxCudaSelectDevice to the calling thread.

Precondition
Before assigning a context, the thread that has the context currently must release it via Rx::Net::ApiLF::RxGlReleaseContext.
static void Rx::Net::ApiLF::RxGlGetTextureID ( Rx::Net::ApiLF::EImgID  eImgID,
[Out] unsigned%  uGlTextureID 
)
static

Get the OpenGl texture ID corresponding to given parameter eImgID. The content of this texture can be updated with the Rx::Net::ApiLF::RxGlUpdateTex function.

Parameters
eImgIDIdentifier for the image.
[out]uGlTextureIDIdentifier for the gl texture.
static unsigned Rx::Net::ApiLF::RxGlGetTextureID ( Rx::Net::ApiLF::EImgID  eImgID)
static

Get the OpenGl texture ID corresponding to given parameter eImgID. The content of this texture can be updated with the Rx::Net::ApiLF::RxGlUpdateTex function.

Parameters
eImgIDIdentifier for the image.
Returns
The identifier for the gl texture.
static void Rx::Net::ApiLF::RxGlGetVersion ( int%  iMajor,
int%  iMinor 
)
static

Get OpenGL version of current OpenGL rendering context.

Warning
This function only return a meaningful result if it is called within an active OpenGL rendering context.
Parameters
[out]iMajorOn return contains the OpenGL major version.
[out]iMinorOn return contains the OpenGL minor version.
static void Rx::Net::ApiLF::RxGlReleaseContext ( )
static

Releases the OpenGL context owned by the calling thread.

Warning
Another thread can get the released context via Rx::Net::ApiLF::RxGlGetContext. If the calling thread hasn't a valid context this function will do nothing and only returns.
static void Rx::Net::ApiLF::RxGlUpdateTex ( unsigned  uIntImgIDs)
static

Copy CUDA result images to OpenGL textures.

The internal image IDs that can be used with this function are those declared in Rx::Net::ApiLF::EImgID. Combining a number of image IDs by OR, updates all the related textures.

Parameters
uIntImgIDsInternal image ID. A number of internal image IDs can be combined with OR operation.
static void Rx::Net::ApiLF::RxGlUpdateTex ( unsigned  uIntImgIDs,
bool  bCreateMipmaps 
)
static

Copy CUDA result images to OpenGL textures.

The internal image IDs that can be used with this function are those declared in Rx::Net::ApiLF::EImgID. Combining a number of image IDs by OR, updates all the related textures.

Parameters
uIntImgIDsInternal image ID. A number of internal image IDs can be combined with OR operation.
bCreateMipmapsthe create mipmaps flag.