Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Feature proposal: Core & compatibility profile context creation & more  (Read 6343 times)

0 Members and 1 Guest are viewing this topic.

Yours3lf

  • Newbie
  • *
  • Posts: 43
    • View Profile
Hi,

I've made a small modification to the latest SFML snapshot that allows me to create a core profile context. It also allows me to create a debug context, or a forward compatible context.
Note that if you use this, then you need to explicitly ignore the OpenGL errors generated by GLEW. (due to depracated functionality usage)

So here's the code:
Code: [Select]
GlxContext.cpp around line 240

        if (glXCreateContextAttribsARB)
        {
            int nbConfigs = 0;
            GLXFBConfig* configs = glXChooseFBConfig(m_display, DefaultScreen(m_display), NULL, &nbConfigs);
            if (configs && nbConfigs)
            {

                /*
                 * CORE OR COMPATIBILITY PROFILE
                 */
                int profile = GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB;
                int flags = 0;
                if(settings.core_profile)
                  profile = GLX_CONTEXT_CORE_PROFILE_BIT_ARB;
                if(settings.debug_context)
                  flags |= GLX_CONTEXT_DEBUG_BIT_ARB;
                if(settings.forward_context)
                  flags |= GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
                //end

                // Create the context
                int attributes[] =
                {
                    GLX_CONTEXT_MAJOR_VERSION_ARB, m_settings.majorVersion,
                    GLX_CONTEXT_MINOR_VERSION_ARB, m_settings.minorVersion,
                    GLX_CONTEXT_PROFILE_MASK_ARB, profile,
                    GLX_CONTEXT_FLAGS_ARB, flags
                };
                m_context = glXCreateContextAttribsARB(m_display, configs[0], toShare, true, attributes);
            }

            if (configs)
                XFree(configs);
        }

Code: [Select]
ContextSettings.hpp around line 35

struct ContextSettings
{
    ////////////////////////////////////////////////////////////
    /// \brief Default constructor
    ///
    /// \param depth        Depth buffer bits
    /// \param stencil      Stencil buffer bits
    /// \param antialiasing Antialiasing level
    /// \param major        Major number of the context version
    /// \param minor        Minor number of the context version
    ///
    ////////////////////////////////////////////////////////////
    explicit ContextSettings(unsigned int depth = 0, unsigned int stencil = 0, unsigned int antialiasing = 0, unsigned int major = 2, unsigned int minor = 0, bool profile = false, bool debug = false, bool forward = false) :
    depthBits        (depth),
    stencilBits      (stencil),
    antialiasingLevel(antialiasing),
    majorVersion     (major),
    minorVersion     (minor),
    core_profile     (profile),
    debug_context    (debug),
    forward_context  (forward)
    {
    }

    ////////////////////////////////////////////////////////////
    // Member data
    ////////////////////////////////////////////////////////////
    unsigned int depthBits;         ///< Bits of the depth buffer
    unsigned int stencilBits;       ///< Bits of the stencil buffer
    unsigned int antialiasingLevel; ///< Level of antialiasing
    unsigned int majorVersion;      ///< Major number of the context version to create
    unsigned int minorVersion;      ///< Minor number of the context version to create
    bool core_profile;
    bool debug_context;
    bool forward_context;
};

With this now you can create a compatibility profile context:
(24 depth bits, 8 stencil bits, OpenGL 4.2 compatibility profile context)
Code: [Select]
sf::Window the_window;
the_window.create( sf::VideoMode( SCREEN_WIDTH,
                                       SCREEN_HEIGHT,
                                       BPP ),
                                       TITLE, MODE,
                                       sf::ContextSettings( 24, 8, 0, 4, 2, false, false, false ) ); //relevant bit, notice the "false" parameters

Querying the video card gives the following result:
Code: [Select]
  std::cout << "Vendor: " << glGetString( GL_VENDOR ) << "\n";
  std::cout << "Renderer: " << glGetString( GL_RENDERER ) << "\n";
  std::cout << "OpenGL version: " << glGetString( GL_VERSION ) << "\n";
  std::cout << "GLSL version: " << glGetString( GL_SHADING_LANGUAGE_VERSION ) << "\n";

result:
Quote
Vendor: ATI Technologies Inc.
Renderer: ATI Radeon HD 5670
OpenGL version: 4.2.11631 Compatibility Profile Context
GLSL version: 4.20

or if you want to create a core profile context:
(24 depth bits, 8 stencil bits, OpenGL 4.2 core profile context, with debug and forward compatible options)
Code: [Select]
sf::Window the_window;
the_window.create( sf::VideoMode( SCREEN_WIDTH,
                                       SCREEN_HEIGHT,
                                       BPP ),
                                       TITLE, MODE,
                                       sf::ContextSettings( 24, 8, 0, 4, 2, true, true, true ) ); //relevant bit, notice the "true" parameters

This gives the following result:
Quote
Vendor: ATI Technologies Inc.
Renderer: ATI Radeon HD 5670
OpenGL version: 4.2.11631 Core Profile Forward-Compatible/Debug Context
GLSL version: 4.20

This modification only works on Linux for now, but you may implement it on Windows and OSX in a similar manner. The reason I didn't implement them, is that I don't have these OSs right now. I hope this will make its way into the core SFML.

Best regards,
Yours3!f

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
I guess only people who understand what you're talking about should be interested in this, right? ;D
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Yours3lf

  • Newbie
  • *
  • Posts: 43
    • View Profile
I guess only people who understand what you're talking about should be interested in this, right? ;D
Well, yes. Anyone who considers OpenGL 3.x+ development.

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Could you explain the difference between the contexts? I am working on a 3.x+  project but I have used the context SFML creates for me without any problems. I want to know why would one need this?

Most of my experience is in DirectX 10 and 11.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Yours3lf

  • Newbie
  • *
  • Posts: 43
    • View Profile
Well, the relevant bit can be found in the OpenGL 4.2 specification appendix E.

So basically if you create a 3.3 context with the core profile set, then you'll only have the 3.3 functionality.
If you call older (removed from 3.3) functions such as glBegin() it will generate a INVALID_OPERATION error. Further errors will be generated if you call a core profile function with old parameters, such as glTexImage2D with GL_LUMINANCE as format.
If you create a 3.3 context with compatibility profile set, then you'll have the functionality of 3.3 and the functionality that is described by the GL_ARB_compatibility extension. This means that you can have the functionality of a 3.3 context AND of older versions. So calling glBegin() will not generate an error.
There is functionality that is only marked as deprecated, however it may still be present in the core profile. The forward compatible option enables you to remove these functions, thus create a context that is free of deprecated functionality.

This is great because the deprecated functionality is often more cumbersome to use, or slower, so this will force you to use the modern features of the api.

as for the debug context creation, here's the relevant specification:
http://www.opengl.org/registry/specs/ARB/glx_create_context.txt
it is meant to be used during application development, and they provide additional runtime checking, validation and logging functionality. It may include possible performance penalties. However the behavior of the debug contexts is implementation defined, so they may act as non-debug contexts.
« Last Edit: May 21, 2012, 12:49:13 pm by Yours3lf »