Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
eglimage cuda
// Create a texture and register it for sharing with CUDA.
int setup_external_texture_oes(nengl_ws* ws, void* window)
{
int isSupported = -1;
isSupported = ws->check_extension("EGL_KHR_gl_texture_2D_image");
isSupported = ws->check_extension("GL_OES_EGL_image_external");
constexpr GLsizei texSize = 32;
GLubyte data[texSize * texSize * 4];
GLuint mSourceTexture, mExternalTexture;
glGenTextures(1, &mSourceTexture);
glBindTexture(GL_TEXTURE_2D, mSourceTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texSize, texSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glGenTextures(1, &mExternalTexture);
PFNEGLCREATEIMAGEKHRPROC pfnEglCreateImageKHR =
(PFNEGLCREATEIMAGEKHRPROC)glfwGetProcAddress("eglCreateImageKHR");
PFNGLEGLIMAGETARGETTEXTURE2DOESPROC pfnGlEglImageTargetTexture2dOes =
(PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)glfwGetProcAddress("glEGLImageTargetTexture2DOES");
if (!pfnEglCreateImageKHR)
{
return -1;
}
if (!pfnGlEglImageTargetTexture2dOes)
{
return -1;
}
EGLint attribs[] = {
EGL_IMAGE_PRESERVED,
EGL_TRUE,
EGL_NONE,
};
EGLImageKHR image =
pfnEglCreateImageKHR(ws->get_egl_display(), ws->get_egl_context(window), EGL_GL_TEXTURE_2D_KHR,
reinterpret_cast<EGLClientBuffer>(mSourceTexture), attribs);
GL_CHECK(pfnEglCreateImageKHR);
glBindTexture(GL_TEXTURE_EXTERNAL_OES, mExternalTexture);
pfnGlEglImageTargetTexture2dOes(GL_TEXTURE_EXTERNAL_OES, image);
GL_CHECK(pfnGlEglImageTargetTexture2dOes);
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Register buffer with CUDA
struct cudaGraphicsResource* pResource = NULL;
cudaError_t err = cudaSuccess;
err = cudaGraphicsEGLRegisterImage(&pResource, image, CU_GRAPHICS_REGISTER_FLAGS_NONE);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment