Functions to exchange data between CUDA and OpenGL.
More...
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;
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;
uniform usampler2D texImg;
void main()
{
float fCol = float(textureLod(texImg, vTexPos, 0).x) / 255.0;
gl_FragColor = vec4(fCol, fCol, fCol, 1.0);
}
static void Rx::Net::ApiLF::RxGlGetContext |
( |
| ) |
|
|
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
-
| eImgID | Identifier for the image. |
[out] | uGlTextureID | Identifier for the gl texture. |
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
-
eImgID | Identifier 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] | iMajor | On return contains the OpenGL major version. |
[out] | iMinor | On 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
-
uIntImgIDs | Internal 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
-
uIntImgIDs | Internal image ID. A number of internal image IDs can be combined with OR operation. |
bCreateMipmaps | the create mipmaps flag. |