//////////////////////////////////////////////////////////// /// /see WindowImpl::SetActive //////////////////////////////////////////////////////////// void WindowImplX11::SetActive(bool Active) const { if (Active) { if (myWindow && myGLContext && (glXGetCurrentContext() != myGLContext)) glXMakeCurrent(ourDisplay, myWindow, myGLContext); } else { if (glXGetCurrentContext() == myGLContext) glXMakeCurrent(ourDisplay, None, NULL); } }
void Renderer::Init() { #ifdef _WIN32 m_CurrentContext = wglGetCurrentContext(); m_CurrentDC = wglGetCurrentDC(); // release current context wglMakeCurrent( nullptr, nullptr ); #endif #ifdef __linux__ // Rendering works fine under X in a separate thread, but quitting breaks some SDL internals. Haven't figured it out yet. if (!XInitThreads()) { THROW( "XLib is not thread safe." ); } SDL_SysWMinfo wm_info; SDL_VERSION( &wm_info.version ); if ( SDL_GetWMInfo( &wm_info ) ) { // TODO: drag-n-drop for non win32 Display *display = wm_info.info.x11.gfxdisplay; m_CurrentContext = glXGetCurrentContext(); ASSERT( m_CurrentContext, "Error! No current GL context!" ); glXMakeCurrent( display, None, nullptr ); XSync( display, false ); } #endif }
static inline void fill_gl_drawable(RedDrawable_p* dest, const SpiceRect& area, rgb32_t color, const SpicePoint& offset) { int vertex_x1, vertex_x2; int vertex_y1, vertex_y2; GLXContext context; context = glXGetCurrentContext(); if (!context) { return; } vertex_x1 = area.left + offset.x; vertex_y1 = dest->source.x_drawable.height - (area.top + offset.y) - (area.bottom - area.top); vertex_x2 = vertex_x1 + (area.right - area.left); vertex_y2 = vertex_y1 + (area.bottom - area.top); glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0); glColor3f(rgb32_get_red(color), rgb32_get_green(color), rgb32_get_blue(color)); glBegin(GL_QUADS); glVertex2i(vertex_x1, vertex_y1); glVertex2i(vertex_x1, vertex_y2); glVertex2i(vertex_x2, vertex_y2); glVertex2i(vertex_x2, vertex_y1); glEnd(); glFlush(); glColor3f(1, 1, 1); }
void cl_select_context(cl_platform_id* platform, cl_device_id* device, cl_context* context) { cl_int err; #if defined (__APPLE__) CGLContextObj kCGLContext = CGLGetCurrentContext(); CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(kCGLContext); cl_context_properties props[] = { CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)kCGLShareGroup, 0 }; *context = clCreateContext(props, 0,0, NULL, NULL, &err); #else #ifdef UNIX cl_context_properties props[] = { CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(), CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(), CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 }; *context = clCreateContext(props, 1, &cdDevices[uiDeviceUsed], NULL, NULL, &err); #else // Win32 cl_context_properties props[] = { CL_GL_CONTEXT_KHR, (cl_context_properties)wglGetCurrentContext(), CL_WGL_HDC_KHR, (cl_context_properties)wglGetCurrentDC(), CL_CONTEXT_PLATFORM, (cl_context_properties)*platform, 0 }; *context = clCreateContext(props, 1, device, NULL, NULL, &err); CHECK_ERR(err); #endif #endif }
static int gl_thread_create(void* unused) { int err = glXMakeCurrent(gl_thread_display, gl_thread_drawable, gl_thread_context); if (err == 0) { // Could not make context current return -1; } int status = gl_thread_func(gl_thread_data); Display* display = glXGetCurrentDisplay(); if (display == NULL) { // Could not get current display return -2; } GLXContext context = glXGetCurrentContext(); if (context == NULL) { // Could not get current context return -3; } err = glXMakeCurrent(display, None, NULL); if (err == 0) { // Could not make context current return -4; } glXDestroyContext(display, context); return status; }
//-------------------------------------------------------------------------------------------------// void GLXWindow::setVSyncEnabled(bool vsync) { mVSync = vsync; // we need to make our context current to set vsync // store previous context to restore when finished. ::GLXDrawable oldDrawable = glXGetCurrentDrawable(); ::GLXContext oldContext = glXGetCurrentContext(); mContext->setCurrent(); if (! mIsExternalGLControl) { if (GLXEW_MESA_swap_control) glXSwapIntervalMESA (vsync ? mVSyncInterval : 0); else if (GLXEW_EXT_swap_control) glXSwapIntervalEXT (mGLSupport->getGLDisplay(), glXGetCurrentDrawable(), vsync ? mVSyncInterval : 0); else if (GLXEW_SGI_swap_control) if (vsync && mVSyncInterval) glXSwapIntervalSGI (mVSyncInterval); } mContext->endCurrent(); glXMakeCurrent (mGLSupport->getGLDisplay(), oldDrawable, oldContext); }
const GrGLInterface* GrGLCreateNativeInterface() { if (nullptr == glXGetCurrentContext()) { return nullptr; } return GrGLAssembleInterface(nullptr, glx_get); }
GlxContext::~GlxContext() { // Destroy the context if (m_context) { #if defined(GLX_DEBUGGING) GlxErrorHandler handler(m_display); #endif if (glXGetCurrentContext() == m_context) glXMakeCurrent(m_display, None, NULL); glXDestroyContext(m_display, m_context); #if defined(GLX_DEBUGGING) if (glxErrorOccurred) err() << "GLX error in GlxContext::~GlxContext()" << std::endl; #endif } if (m_pbuffer) { glXDestroyPbuffer(m_display, m_pbuffer); } // Destroy the window if we own it if (m_window && m_ownsWindow) { xcb_destroy_window(m_connection, m_window); xcb_flush(m_connection); } // Close the connection with the X server CloseDisplay(m_display); }
glc_bool_t glc_is_current( glc_t * context ) { assert( context != 0 && "Calls glc_is_current() on a null glc context."); // @todo glc_status() assert( context->drawable != 0 && "Calls glc_is_current() with a context associated with a null drawable." ); // @todo drawable_status() #ifdef __SDL2__ GLC_GLRC_HANDLE glrc = SDL_GL_GetCurrentContext(); #elif defined(GLC_USE_WGL) GLC_GLRC_HANDLE glrc = wglGetCurrentContext(); /*assert( ( glrc == 0 ) || // no current context => is not current ( glrc != context->context ) || // another context is current => is not current ( (glrc == context->context) && // is current (wglGetCurrentDC() == context->drawable->dc) ) // and for the good drawable/dc && "The context is current, but not for its associated drawable/graphical context !" );*/ #elif defined(GLC_USE_GLX) GLC_GLRC_HANDLE glrc = glXGetCurrentContext(); #else #error "Platform not yet supported." #endif return glrc == context->context ? 1 : 0; }
GlfQGLPlatformDebugContextPrivate::GlfQGLPlatformDebugContextPrivate( int majorVersion, int minorVersion, bool coreProfile, bool directRendering) : _dpy(NULL) , _ctx(NULL) { Display *shareDisplay = glXGetCurrentDisplay(); GLXContext shareContext = glXGetCurrentContext(); int fbConfigId = 0; glXQueryContext(shareDisplay, shareContext, GLX_FBCONFIG_ID, &fbConfigId); int screen = 0; glXQueryContext(shareDisplay, shareContext, GLX_SCREEN, &screen); int configSpec[] = { GLX_FBCONFIG_ID, fbConfigId, None, }; GLXFBConfig *configs = NULL; int configCount = 0; configs = glXChooseFBConfig(shareDisplay, screen, configSpec, &configCount); if (not TF_VERIFY(configCount > 0)) { return; } const int profile = coreProfile ? GLX_CONTEXT_CORE_PROFILE_BIT_ARB : GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB; int attribs[] = { GLX_CONTEXT_MAJOR_VERSION_ARB, majorVersion, GLX_CONTEXT_MINOR_VERSION_ARB, minorVersion, GLX_CONTEXT_PROFILE_MASK_ARB, profile, GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB, 0, }; // Extension entry points must be resolved at run-time. PFNGLXCREATECONTEXTATTRIBSARBPROC createContextAttribs = (PFNGLXCREATECONTEXTATTRIBSARBPROC) glXGetProcAddressARB((const GLubyte*)"glXCreateContextAttribsARB"); // Create a GL context with the requested capabilities. if (createContextAttribs) { _ctx = (*createContextAttribs)(shareDisplay, configs[0], shareContext, directRendering, attribs); } else { TF_WARN("Unable to create GL debug context."); XVisualInfo *vis = glXGetVisualFromFBConfig(shareDisplay, configs[0]); _ctx = glXCreateContext(shareDisplay, vis, shareContext, directRendering); } if (not TF_VERIFY(_ctx)) { return; } _dpy = shareDisplay; }
ocl_constructs * newOclConstructs(cl_uint workerSize, cl_uint groupSize, cl_bool clGlSharing){ ocl_constructs * occs = (ocl_constructs*) malloc(sizeof(ocl_constructs)); occs->platformId = NULL; occs->deviceId = NULL; occs->context = NULL; occs->queue = NULL; occs->program = NULL; occs->buffers = NULL; occs->kernel = NULL; if(clGlSharing && 0){ occs->contextProperties = (cl_context_properties*) malloc(sizeof(cl_context_properties) * 7); occs->contextProperties[0] = CL_GL_CONTEXT_KHR; occs->contextProperties[1] = (cl_context_properties)glXGetCurrentContext(); occs->contextProperties[2] = CL_GLX_DISPLAY_KHR; occs->contextProperties[3] = (cl_context_properties)glXGetCurrentDisplay(); occs->contextProperties[4] = CL_CONTEXT_PLATFORM; //occs->contextProperties[5] = (cl_context_properties)cpPlatform; occs->contextProperties[6] = 0; } else occs->contextProperties = NULL; //occs->refreshCount = 30; return occs; }
void InitCL() { void* glCtx=0; void* glDC = 0; #ifdef _WIN32 glCtx = wglGetCurrentContext(); #else //!_WIN32 GLXContext glCtx = glXGetCurrentContext(); #endif //!_WIN32 glDC = wglGetCurrentDC(); int ciErrNum = 0; cl_device_type deviceType = CL_DEVICE_TYPE_ALL;//GPU; g_cxMainContext = btOpenCLUtils::createContextFromType(deviceType, &ciErrNum, glCtx, glDC); oclCHECKERROR(ciErrNum, CL_SUCCESS); int numDev = btOpenCLUtils::getNumDevices(g_cxMainContext); if (numDev>0) { g_device= btOpenCLUtils::getDevice(g_cxMainContext,0); btOpenCLUtils::printDeviceInfo(g_device); // create a command-queue g_cqCommandQue = clCreateCommandQueue(g_cxMainContext, g_device, 0, &ciErrNum); oclCHECKERROR(ciErrNum, CL_SUCCESS); //normally you would create and execute kernels using this command queue } }
void glx_ctx_push_thread_local(VdpDeviceData *deviceData) { glx_ctx_lock(); Display *dpy = deviceData->display; const Window wnd = deviceData->root; thread_id_t thread_id = get_current_thread_id(); ctx_stack.dpy = glXGetCurrentDisplay(); if (!ctx_stack.dpy) ctx_stack.dpy = dpy; ctx_stack.wnd = glXGetCurrentDrawable(); ctx_stack.glc = glXGetCurrentContext(); ctx_stack.element_count ++; struct val_s *val = g_hash_table_lookup(glc_hash_table, GSIZE_TO_POINTER(thread_id)); if (!val) { GLXContext glc = glXCreateContext(dpy, root_vi, root_glc, GL_TRUE); assert(glc); val = make_val(dpy, glc); g_hash_table_insert(glc_hash_table, GSIZE_TO_POINTER(thread_id), val); // try cleanup expired entries g_hash_table_foreach_remove(glc_hash_table, is_thread_expired, NULL); } assert(val->dpy == dpy); glXMakeCurrent(dpy, wnd, val->glc); }
iface::iface(gui::window& window) : iface_base(window) { context_ = glXCreateContext(gui::g_display, &window_.current_visual(), glXGetCurrentContext(), true); if (!context_) { throw std::runtime_error("Failed to create an OpenGL context for this window"); } set_active(true); set_vsync_interval(1); GLenum const glew_err = glewInit(); if(glew_err != GLEW_OK) { std::string const err_str = (char const*)glewGetErrorString(glew_err); throw std::runtime_error("glew error: " + err_str); } #if 0 trace("glew initialized\n"); const char* extensions = (char*)glGetString(GL_EXTENSIONS); const char* version = (char*)glGetString(GL_VERSION); trace("OpenGL version: %s\n",version); trace("OpenGL extensions: %s\n",extensions); extensions = glXQueryExtensionsString(gui::g_display, gui::g_screen); if (extensions) trace("GLX extensions: %s\n",extensions); #endif }
void GLEngine::checkContextExternallyChanged() { if(glXGetCurrentContext() != mCurrentContext) { GPGPU_EXCEPT(ERR_RENDERSYSTEM, "GL context changed unexpectedly"); } }
void OCLContextProperties::AddOpenGLContextProperties() { #ifdef __linux__ cl_context_properties props[7] = { CL_GL_CONTEXT_KHR, (cl_context_properties) glXGetCurrentContext(), CL_GLX_DISPLAY_KHR, (cl_context_properties) glXGetCurrentDisplay(), 0 }; #elif __APPLE__ CGLContextObj kGLContext = CGLGetCurrentContext(); CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(kGLContext); cl_context_properties props[5] = { CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties) kCGLShareGroup, NULL }; //foundCtx = clCreateContext( //props, 0, 0, NULL, NULL, clerr); #elif WIN32 cl_context_properties props[5] = { CL_GL_CONTEXT_KHR, (cl_context_properties) wglGetCurrentContext(), CL_WGL_HDC_KHR, (cl_context_properties) wglGetCurrentDC() }; #else #error "Platform Not avalible for GL intergration" #endif AddProperties(props); }
/* * These are special functions for stereoscopic support * differences in MacOS X. */ void glDrawBuffer(GLenum mode) { GLXContext gc = glXGetCurrentContext(); if (gc && apple_glx_context_uses_stereo(gc->apple)) { GLenum buf[2]; GLsizei n = 0; switch (mode) { case GL_BACK: buf[0] = GL_BACK_LEFT; buf[1] = GL_BACK_RIGHT; n = 2; break; case GL_FRONT: buf[0] = GL_FRONT_LEFT; buf[1] = GL_FRONT_RIGHT; n = 2; break; default: buf[0] = mode; n = 1; break; } __gl_api.DrawBuffers(n, buf); } else { __gl_api.DrawBuffer(mode); } }
void makeCurrent() { m_detachedContext = glXGetCurrentContext(); m_detachedSurface = glXGetCurrentDrawable(); if (m_surface && m_glContext) glXMakeCurrent(m_display, m_surface, m_glContext); }
inline static ContextKey getCurrentContextKey() { ContextKey key = glXGetCurrentContext(); detail::errorCheck(); if (!key) M_throw() << "Not in a valid GLX context"; return key; }
const GrGLInterface* GrGLCreateNativeInterface() { if (NULL == glXGetCurrentContext()) { return NULL; } return GrGLAssembleInterface(NULL, glx_get); }
// XXX: initCL should be removed from libosd void OsdClKernelDispatcher::initCL() { cl_int ciErrNum; cl_platform_id cpPlatform = 0; cl_uint num_platforms; ciErrNum = clGetPlatformIDs(0, NULL, &num_platforms); if (ciErrNum != CL_SUCCESS) { OSD_ERROR("Error %i in clGetPlatformIDs call.\n", ciErrNum); exit(1); } if (num_platforms == 0) { OSD_ERROR("No OpenCL platform found.\n"); exit(1); } cl_platform_id *clPlatformIDs; clPlatformIDs = new cl_platform_id[num_platforms]; ciErrNum = clGetPlatformIDs(num_platforms, clPlatformIDs, NULL); char chBuffer[1024]; for (cl_uint i = 0; i < num_platforms; ++i) { ciErrNum = clGetPlatformInfo(clPlatformIDs[i], CL_PLATFORM_NAME, 1024, chBuffer,NULL); if (ciErrNum == CL_SUCCESS) { cpPlatform = clPlatformIDs[i]; } } // ------------- clGetDeviceIDs(cpPlatform, CL_DEVICE_TYPE_GPU, 1, &_clDevice, NULL); #if defined(_WIN32) cl_context_properties props[] = { CL_GL_CONTEXT_KHR, (cl_context_properties)wglGetCurrentContext(), CL_WGL_HDC_KHR, (cl_context_properties)wglGetCurrentDC(), CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform, 0 }; #elif defined(__APPLE__) CGLContextObj kCGLContext = CGLGetCurrentContext(); CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(kCGLContext); cl_context_properties props[] = { CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)kCGLShareGroup, 0 }; #else cl_context_properties props[] = { CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(), CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(), CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform, 0 }; #endif // XXX context creation should be moved to client code _clContext = clCreateContext(props, 1, &_clDevice, NULL, NULL, &ciErrNum); CL_CHECK_ERROR(ciErrNum, "clCreateContext\n"); _clQueue = clCreateCommandQueue(_clContext, _clDevice, 0, &ciErrNum); CL_CHECK_ERROR(ciErrNum, "clCreateCommandQueue\n"); }
GarchGLXContextState::GarchGLXContextState() : display(glXGetCurrentDisplay()), drawable(glXGetCurrentDrawable()), context(glXGetCurrentContext()), _defaultCtor(true) { // Do nothing }
void PBuffer::enable() { data->old_pbuffer = glXGetCurrentDrawable(); data->old_context = glXGetCurrentContext(); if(!glXMakeCurrent(data->display,data->pbuffer,data->context)) { error("PBuffer::enable(): glXMakeCurrent() failed"); } }
Display PUBLIC * glXGetCurrentDisplay(void) { /* Same code as in libGL's glxext.c */ __GLXcontext *gc = (__GLXcontext *) glXGetCurrentContext(); if (NULL == gc) return NULL; return gc->currentDpy; }
bool Context::createContext(bool glInterop) { if(_initialized) return false; QMutexLocker locker(&_lock); _glInterop= glInterop; QVector<cl_context_properties> props; if(_glInterop) { // Add OpenGL properties #ifdef __MACOSX // Apple (untested) props << CL_CGL_SHAREGROUP_KHR; props << reinterpret_cast<cl_context_properties>(CGLGetShareGroup(CGLGetCurrentContext())); props << CL_CONTEXT_PLATFORM; props << reinterpret_cast<cl_context_properties>(devMgr().platform()); #elif _WIN32 // Windows (untested) props << CL_GL_CONTEXT_KHR props << reinterpret_cast<cl_context_properties>(wglGetCurrentContext()); props << CL_WGL_HDC_KHR props << reinterpret_cast<cl_context_properties>(wglGetCurrentDC()); #else // Linux/GLX props << CL_GL_CONTEXT_KHR; props << (cl_context_properties)glXGetCurrentContext(); props << CL_GLX_DISPLAY_KHR; props << (cl_context_properties)glXGetCurrentDisplay(); props << (cl_context_properties)(devMgr().platform()); #endif props << 0; // Can't use nullptr here } // Create OpenCL context cl_int err; const auto propsPtr= props.count() ? props.data() : nullptr; const auto devs= devMgr().devices(); // Get selected devices from the dev manager _context= clCreateContext(propsPtr, devs.count(), devs.data(), nullptr, nullptr, &err); if(checkCLError(err, "clCreateContext")) return false; // Get list of supported formats cl_uint formatCount; err= clGetSupportedImageFormats(_context, CL_MEM_READ_WRITE, CL_MEM_OBJECT_IMAGE2D, 0, nullptr, &formatCount); if(checkCLError(err, "clGetSupportedImageFormats") or formatCount<=0) return false; _imgFormats.resize(formatCount); err= clGetSupportedImageFormats(_context, CL_MEM_READ_WRITE, CL_MEM_OBJECT_IMAGE2D, formatCount, _imgFormats.data(), nullptr); if(checkCLError(err, "clGetSupportedImageFormats")) return false; return true; }
void PBuffer::makeCurrent() { if(glXGetCurrentContext() == mContext) // Already active return; Display *dpy = GLEngine::getInstance().getDisplay(); glXMakeCurrent(dpy, mPBuffer, mContext); GLEngine::getInstance().setCurrentContext(mContext); }
cl_context QHoneycombWidget::CreateContext() { cl_int errNum; cl_uint numPlatforms; cl_platform_id firstPlatformId; cl_context context = NULL; // First, select an OpenCL platform to run on. For this example, we // simply choose the first available platform. Normally, you would // query for all available platforms and select the most appropriate one. errNum = clGetPlatformIDs(1, &firstPlatformId, &numPlatforms); if (errNum != CL_SUCCESS || numPlatforms <= 0) { std::cerr << "Failed to find any OpenCL platforms." << std::endl; return NULL; } // Next, create an OpenCL context on the platform. Attempt to // create a GPU-based context, and if that fails, try to create // a CPU-based context. cl_context_properties contextProperties[] = { #ifdef _WIN32 CL_CONTEXT_PLATFORM, (cl_context_properties)firstPlatformId, CL_GL_CONTEXT_KHR, (cl_context_properties)wglGetCurrentContext(), CL_WGL_HDC_KHR, (cl_context_properties)wglGetCurrentDC(), #elif defined( __GNUC__) CL_CONTEXT_PLATFORM, (cl_context_properties)clSelectedPlatformID, CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(), CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(), #elif defined(__APPLE__) //todo #endif 0 }; context = clCreateContextFromType(contextProperties, CL_DEVICE_TYPE_GPU, NULL, NULL, &errNum); if (errNum != CL_SUCCESS) { std::cout << "Could not create GPU context, trying CPU..." << std::endl; context = clCreateContextFromType(contextProperties, CL_DEVICE_TYPE_CPU, NULL, NULL, &errNum); if (errNum != CL_SUCCESS) { std::cerr << "Failed to create an OpenCL GPU or CPU context." << std::endl; return NULL; } } return context; }
//----------------------------------------------------------------------------// void OpenGLGLXPBTextureTarget::createContext() { d_context = glXCreateNewContext(d_dpy, d_fbconfig, GLX_RGBA_TYPE, glXGetCurrentContext(), true); if (!d_context) throw RendererException("OpenGLGLXPBTextureTarget::createContext - " "Failed to create GLX context for pbuffer."); }
JNIEXPORT jlong JNICALL GLX_NATIVE(_1glXGetCurrentContext) (JNIEnv *env, jclass that) { jlong rc = 0; GLX_NATIVE_ENTER(env, that, _1glXGetCurrentContext_FUNC); rc = (jlong)glXGetCurrentContext(); GLX_NATIVE_EXIT(env, that, _1glXGetCurrentContext_FUNC); return rc; }
JNIEXPORT jint JNICALL GLX_NATIVE(glXGetCurrentContext) (JNIEnv *env, jclass that) { jint rc = 0; GLX_NATIVE_ENTER(env, that, glXGetCurrentContext_FUNC); rc = (jint)glXGetCurrentContext(); GLX_NATIVE_EXIT(env, that, glXGetCurrentContext_FUNC); return rc; }