diff --git a/opencl/amdocl/cl_gl.cpp b/opencl/amdocl/cl_gl.cpp index 320edee5e5..2af2d28d26 100644 --- a/opencl/amdocl/cl_gl.cpp +++ b/opencl/amdocl/cl_gl.cpp @@ -1963,11 +1963,18 @@ cl_int clEnqueueAcquireExtObjectsAMD(cl_command_queue command_queue, cl_uint num amd::HostQueue& hostQueue = *queue; if (cmd_type == CL_COMMAND_ACQUIRE_GL_OBJECTS) { + GLFunctions* gl_functions = hostQueue.context().glenv(); // Verify context init'ed for interop - if (!hostQueue.context().glenv() || !hostQueue.context().glenv()->isAssociated()) { + if (!gl_functions || !gl_functions->isAssociated()) { LogWarning("\"amdContext\" is not created from GL context or share list"); return CL_INVALID_CONTEXT; } + // If the cl_khr_gl_event extension is supported, then the OpenCL implementation will ensure + // that any such pending OpenGL operations are complete for an OpenGL context bound + // to the same thread as the OpenCL context. + if (hostQueue.device().settings().checkExtension(ClKhrGlEvent)) { + gl_functions->WaitCurrentGlContext(hostQueue.context().info()); + } } std::vector memObjects; @@ -2082,6 +2089,21 @@ cl_int clEnqueueReleaseExtObjectsAMD(cl_command_queue command_queue, cl_uint num } } #endif //_WIN32 + // If the cl_khr_gl_event extension is supported, then the OpenCL implementation will ensure + // that any pending OpenCL operations are complete for an OpenGL context bound + // to the same thread as the OpenCL context. + if (cmd_type == CL_COMMAND_RELEASE_GL_OBJECTS) { + GLFunctions* gl_functions = hostQueue.context().glenv(); + // Verify context init'ed for interop + if (!gl_functions || !gl_functions->isAssociated()) { + LogWarning("\"amdContext\" is not created from GL context or share list"); + return CL_INVALID_CONTEXT; + } + if (hostQueue.device().settings().checkExtension(ClKhrGlEvent) && + gl_functions->IsCurrentGlContext(hostQueue.context().info())) { + command->awaitCompletion(); + } + } *not_null(event) = as_cl(&command->event()); @@ -2224,6 +2246,8 @@ GLFunctions::GLFunctions(HMODULE h, bool isEGL) if (isEGL_) { GetProcAddress_ = (PFN_xxxGetProcAddress)GETPROCADDRESS(h, "eglGetProcAddress"); + eglGetCurrentContext_ = (PFN_eglGetCurrentContext)GETPROCADDRESS(h, "eglGetCurrentContext"); + VERIFY_POINTER(eglGetCurrentContext_) } else { GetProcAddress_ = (PFN_xxxGetProcAddress)GETPROCADDRESS(h, API_GETPROCADDR); } @@ -2299,6 +2323,12 @@ GLFunctions::~GLFunctions() { #endif //!_WIN32 } +void GLFunctions::WaitCurrentGlContext(const amd::Context::Info& info) const { + if (IsCurrentGlContext(info)) { + glFinish_(); + } +} + bool GLFunctions::init(intptr_t hdc, intptr_t hglrc) { if (isEGL_) { eglDisplay_ = (EGLDisplay)hdc; diff --git a/opencl/amdocl/cl_gl_amd.hpp b/opencl/amdocl/cl_gl_amd.hpp index 36831fa747..75848836c4 100644 --- a/opencl/amdocl/cl_gl_amd.hpp +++ b/opencl/amdocl/cl_gl_amd.hpp @@ -174,7 +174,7 @@ public: GLsizei numSamples, GLenum glCubemapFace = 0) : Image(amdContext, clType, clFlags, format, width, height, depth, - Format(format).getElementSize() * width, + Format(format).getElementSize() * width, Format(format).getElementSize() * width * depth) , GLObject(glTarget, gluiName, gliMipLevel, glInternalFormat, static_cast(width), static_cast(height), @@ -191,6 +191,7 @@ protected: virtual void initDeviceMemory(); }; + typedef EGLContext (*PFN_eglGetCurrentContext) (); #ifdef _WIN32 #define APICALL WINAPI #define GETPROCADDRESS GetProcAddress @@ -263,6 +264,7 @@ private: EGLContext eglInternalContext_; EGLContext eglTempContext_; bool isEGL_; + PFN_eglGetCurrentContext eglGetCurrentContext_; #ifdef _WIN32 HGLRC hOrigGLRC_; @@ -307,6 +309,21 @@ public: GLFunctions(HMODULE h, bool isEGL); ~GLFunctions(); + bool IsCurrentGlContext(const amd::Context::Info& info) const { + if (isEGL_) { + return ((info.hCtx_ != nullptr) && (eglGetCurrentContext_ != nullptr) && + (info.hCtx_ == eglGetCurrentContext_())); + } else { +#ifdef _WIN32 + return ((info.hCtx_ != nullptr) && (info.hCtx_ == wglGetCurrentContext_())); +#else + return ((info.hCtx_ != nullptr) && (info.hCtx_ == glXGetCurrentContext_())); +#endif // _WIN32 + } + } + + void WaitCurrentGlContext(const amd::Context::Info& info) const; + // Query CL-GL context association bool isAssociated() const {