void GlContext::globalInit() { Lock lock(mutex); if (sharedContext) return; // Create the shared context sharedContext = new ContextType(NULL); sharedContext->initialize(ContextSettings()); // Load our extensions vector extensions.clear(); // Check whether a >= 3.0 context is available int majorVersion = 0; glGetIntegerv(GL_MAJOR_VERSION, &majorVersion); if (glGetError() == GL_INVALID_ENUM) { // Try to load the < 3.0 way const char* extensionString = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS)); do { const char* extension = extensionString; while(*extensionString && (*extensionString != ' ')) extensionString++; extensions.push_back(std::string(extension, extensionString)); } while (*extensionString++); } else { // Try to load the >= 3.0 way glGetStringiFuncType glGetStringiFunc = NULL; glGetStringiFunc = reinterpret_cast<glGetStringiFuncType>(getFunction("glGetStringi")); if (glGetStringiFunc) { int numExtensions = 0; glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions); if (numExtensions) { for (unsigned int i = 0; i < static_cast<unsigned int>(numExtensions); ++i) { const char* extensionString = reinterpret_cast<const char*>(glGetStringiFunc(GL_EXTENSIONS, i)); extensions.push_back(extensionString); } } } } // Deactivate the shared context so that others can activate it when necessary sharedContext->setActive(false); }
bool Context::isExtensionAvailable(const char* name) { static std::vector<std::string> extensions; static bool loaded = false; if (!loaded) { TransientContextLock lock; const char* extensionString = NULL; // Check whether a >= 3.0 context is available int majorVersion = 0; glGetIntegerv(GL_MAJOR_VERSION, &majorVersion); if (glGetError() == GL_INVALID_ENUM) { // Try to load the < 3.0 way extensionString = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS)); do { const char* extension = extensionString; while(*extensionString && (*extensionString != ' ')) extensionString++; extensions.push_back(std::string(extension, extensionString)); } while (*extensionString++); } else { // Try to load the >= 3.0 way glGetStringiFuncType glGetStringiFunc = NULL; glGetStringiFunc = reinterpret_cast<glGetStringiFuncType>(getFunction("glGetStringi")); if (glGetStringiFunc) { int numExtensions = 0; glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions); if (numExtensions) { for (unsigned int i = 0; i < static_cast<unsigned int>(numExtensions); ++i) { extensionString = reinterpret_cast<const char*>(glGetStringiFunc(GL_EXTENSIONS, i)); extensions.push_back(extensionString); } } } } loaded = true; } return std::find(extensions.begin(), extensions.end(), name) != extensions.end(); }
bool Context::isExtensionAvailable(const char* name) { static std::vector<std::string> extensions; static bool loaded = false; if (!loaded) { const Context* context = getActiveContext(); if (!context) return false; const char* extensionString = NULL; if(context->getSettings().majorVersion < 3) { // Try to load the < 3.0 way extensionString = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS)); do { const char* extension = extensionString; while(*extensionString && (*extensionString != ' ')) extensionString++; extensions.push_back(std::string(extension, extensionString)); } while (*extensionString++); } else { // Try to load the >= 3.0 way glGetStringiFuncType glGetStringiFunc = NULL; glGetStringiFunc = reinterpret_cast<glGetStringiFuncType>(getFunction("glGetStringi")); if (glGetStringiFunc) { int numExtensions = 0; glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions); if (numExtensions) { for (unsigned int i = 0; i < static_cast<unsigned int>(numExtensions); ++i) { extensionString = reinterpret_cast<const char*>(glGetStringiFunc(GL_EXTENSIONS, i)); extensions.push_back(extensionString); } } } } loaded = true; } return std::find(extensions.begin(), extensions.end(), name) != extensions.end(); }
int sfogl_LoadFunctions() { int numFailed = 0; ClearExtensionVars(); const char* extensionString = NULL; if (sfogl_GetMajorVersion() < 3) { // Try to load the < 3.0 way glCheck(extensionString = (const char *)glGetString(GL_EXTENSIONS)); ProcExtsFromExtString(extensionString); } else { // Try to load the >= 3.0 way const GLubyte* (CODEGEN_FUNCPTR *glGetStringiFunc)(GLenum, GLuint) = NULL; glGetStringiFunc = (const GLubyte* (CODEGEN_FUNCPTR *)(GLenum, GLuint))IntGetProcAddress("glGetStringi"); if (glGetStringiFunc) { int numExtensions = 0; glCheck(glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions)); if (numExtensions) { for (unsigned int i = 0; i < static_cast<unsigned int>(numExtensions); ++i) { glCheck(extensionString = (const char *)glGetStringiFunc(GL_EXTENSIONS, i)); ProcExtsFromExtString(extensionString); } } } } numFailed = Load_Version_1_1(); if (numFailed == 0) return sfogl_LOAD_SUCCEEDED; else return sfogl_LOAD_SUCCEEDED + numFailed; }
void GlContext::initialize(const ContextSettings& requestedSettings) { // Activate the context setActive(true); // Retrieve the context version number int majorVersion = 0; int minorVersion = 0; // Try the new way first glGetIntegerv(GL_MAJOR_VERSION, &majorVersion); glGetIntegerv(GL_MINOR_VERSION, &minorVersion); if (glGetError() != GL_INVALID_ENUM) { m_settings.majorVersion = static_cast<unsigned int>(majorVersion); m_settings.minorVersion = static_cast<unsigned int>(minorVersion); } else { // Try the old way const GLubyte* version = glGetString(GL_VERSION); if (version) { // The beginning of the returned string is "major.minor" (this is standard) m_settings.majorVersion = version[0] - '0'; m_settings.minorVersion = version[2] - '0'; } else { // Can't get the version number, assume 1.1 m_settings.majorVersion = 1; m_settings.minorVersion = 1; } } // 3.0 contexts only deprecate features, but do not remove them yet // 3.1 contexts remove features if ARB_compatibility is not present // 3.2+ contexts remove features only if a core profile is requested // If the context was created with wglCreateContext, it is guaranteed to be compatibility. // If a 3.0 context was created with wglCreateContextAttribsARB, it is guaranteed to be compatibility. // If a 3.1 context was created with wglCreateContextAttribsARB, the compatibility flag // is set only if ARB_compatibility is present // If a 3.2+ context was created with wglCreateContextAttribsARB, the compatibility flag // would have been set correctly already depending on whether ARB_create_context_profile is supported. // If the user requests a 3.0 context, it will be a compatibility context regardless of the requested profile. // If the user requests a 3.1 context and its creation was successful, the specification // states that it will not be a compatibility profile context regardless of the requested // profile unless ARB_compatibility is present. m_settings.attributeFlags = ContextSettings::Default; if (m_settings.majorVersion >= 3) { // Retrieve the context flags int flags = 0; glGetIntegerv(GL_CONTEXT_FLAGS, &flags); if (flags & GL_CONTEXT_FLAG_DEBUG_BIT) m_settings.attributeFlags |= ContextSettings::Debug; if ((m_settings.majorVersion == 3) && (m_settings.minorVersion == 1)) { m_settings.attributeFlags |= ContextSettings::Core; glGetStringiFuncType glGetStringiFunc = reinterpret_cast<glGetStringiFuncType>(getFunction("glGetStringi")); if (glGetStringiFunc) { int numExtensions = 0; glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions); for (unsigned int i = 0; i < static_cast<unsigned int>(numExtensions); ++i) { const char* extensionString = reinterpret_cast<const char*>(glGetStringiFunc(GL_EXTENSIONS, i)); if (std::strstr(extensionString, "GL_ARB_compatibility")) { m_settings.attributeFlags &= ~static_cast<Uint32>(ContextSettings::Core); break; } } } } else if ((m_settings.majorVersion > 3) || (m_settings.minorVersion >= 2)) { // Retrieve the context profile int profile = 0; glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &profile); if (profile & GL_CONTEXT_CORE_PROFILE_BIT) m_settings.attributeFlags |= ContextSettings::Core; } } // Enable anti-aliasing if requested by the user and supported if ((requestedSettings.antialiasingLevel > 0) && (m_settings.antialiasingLevel > 0)) { glEnable(GL_MULTISAMPLE); } else { m_settings.antialiasingLevel = 0; } // Enable sRGB if requested by the user and supported if (requestedSettings.sRgbCapable && m_settings.sRgbCapable) { glEnable(GL_FRAMEBUFFER_SRGB); // Check to see if the enable was successful if (glIsEnabled(GL_FRAMEBUFFER_SRGB) == GL_FALSE) { err() << "Warning: Failed to enable GL_FRAMEBUFFER_SRGB" << std::endl; m_settings.sRgbCapable = false; } } else { m_settings.sRgbCapable = false; } }