Jump to content
  • Advertisement
Sign in to follow this  
Finalspace

Understanding OpenGL 3+ profiles

Recommended Posts

Can someone explain what these opengl profiles are, what it does and how i setup wglCreateContextAttribsARB to select a specific profile?

I dont want to guess, i want a clear clarification from one which know this stuff. My opengl knowledge is too old <= GL 2.1...

Share this post


Link to post
Share on other sites
Advertisement

Quite simple.  So when you are creating a core OpenGL profile (3.0 and beyond), you are essentially requesting the OpenGL equivalent of D3D10+ functionality (depending on what profile you are using).

To be more specific, certain core OpenGL extensions were introduced with certain core OpenGL updates.  Usually, the higher the profile you have, the more functionality you have at you disposal.  Of course, some things are vendor specific just as with legacy OpenGL.  You still have to query extension support.  Example, let's say you want to use GL_NV_command_list, you will need core OpenGL 4.5 to use it as NV added support for this extension with OpenGL 4.5 and later.

If you need a better explanation, take a look at this history chart of each OpenGL update as well as what extensions each profile is supposed to support.  https://www.khronos.org/opengl/wiki/History_of_OpenGL#Summary_of_version_changes

Usually, the ARB or Khronos will approve an extension before making it an official part of the spec.  Vendor specific ones do so at their own desire.

Now, as for your question on creating a core OpenGL context, I don't remember off hand on how to do it.  I personally use SDL 2.0 and use it's API to select the core OpenGL profile I want because my engine has to be cross platform (plus I use glew for simplicity with extensions, which you can still use for core OpenGL).  A lot of tutorials use SDL or glfw for simplicity but that doesn't explain how a core context is created though.  But if you want a windows specific that uses the wglCreateContextAttribsARB, take a look at the Khronos example here: https://www.khronos.org/opengl/wiki/Tutorial:_OpenGL_3.1_The_First_Triangle_(C++/Win)

Specifically, let's focus on this part:

bool CGLRenderer::CreateGLContext(CDC* pDC)
{
	PIXELFORMATDESCRIPTOR pfd;
	memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
	pfd.nSize  = sizeof(PIXELFORMATDESCRIPTOR);
	pfd.nVersion   = 1;
	pfd.dwFlags    = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
	pfd.iPixelType = PFD_TYPE_RGBA;
	pfd.cColorBits = 32;
	pfd.cDepthBits = 32;
	pfd.iLayerType = PFD_MAIN_PLANE;
	
	int nPixelFormat = ChoosePixelFormat(pDC->m_hDC, &pfd);
	
	if (nPixelFormat == 0) return false;
	
	BOOL bResult = SetPixelFormat (pDC->m_hDC, nPixelFormat, &pfd);
	
	if (!bResult) return false; 
	
	HGLRC tempContext = wglCreateContext(pDC->m_hDC);
	wglMakeCurrent(pDC->m_hDC, tempContext);
	
	GLenum err = glewInit();
	if (GLEW_OK != err)
	{
		AfxMessageBox(_T("GLEW is not initialized!"));
	}
	
	int attribs[] =
	{
		WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
		WGL_CONTEXT_MINOR_VERSION_ARB, 1,
		WGL_CONTEXT_FLAGS_ARB, 0,
		0
	};
	
        if(wglewIsSupported("WGL_ARB_create_context") == 1)
        {
		m_hrc = wglCreateContextAttribsARB(pDC->m_hDC,0, attribs);
		wglMakeCurrent(NULL,NULL);
		wglDeleteContext(tempContext);
		wglMakeCurrent(pDC->m_hDC, m_hrc);
	}
	else
	{	//It's not possible to make a GL 3.x context. Use the old style context (GL 2.1 and before)
		m_hrc = tempContext;
	}

	//Checking GL version
	const GLubyte *GLVersionString = glGetString(GL_VERSION);

	//Or better yet, use the GL3 way to get the version number
	int OpenGLVersion[2];
	glGetIntegerv(GL_MAJOR_VERSION, &OpenGLVersion[0]);
	glGetIntegerv(GL_MINOR_VERSION, &OpenGLVersion[1]);

	if (!m_hrc) return false;
	
	return true;
}

You'll still want to create your legacy rendering context, but next you want to fill out a structure that tells the driver what core context you want, and pass that into wglCreateContextAttribsARB() and go from there.  That article explains the initialization part well enough I guess, as well as how to render primitives using the proper methods.  Hope that helps.

Shogun

Share this post


Link to post
Share on other sites

I dont use SDL, GLFW or any kind of library, i have my own platform abstraction thing and this already supports a modern rendering context nearly exactly defined like in the example code you have linked in your post, but i wanted to support forcing of core/forward/compatible profile as well.

 

Core seems to be clear, if i understand that correctly:

You get all the features from opengl 3.0 to the major/minor version selected in the context attribs right?

For example, if you setup your context to be 3.3 you will have access to glBindSampler, but not access to glBufferStorage from 4.4, right?

 

But what is that about "Forward" and "Compatible" thing? I am pretty confused about that:

- WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB

- WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB

Edited by Finalspace

Share this post


Link to post
Share on other sites

Never mind, i use the approach unreal engine 4 is using:

 

/**
 * Create a core profile OpenGL context.
 */
static void PlatformCreateOpenGLContextCore(FPlatformOpenGLContext* OutContext, int MajorVersion, int MinorVersion, HGLRC InParentContext)
{
	check(wglCreateContextAttribsARB);
	check(OutContext);
	check(OutContext->DeviceContext);

	OutContext->SyncInterval = -1;	// invalid value to enforce setup on first buffer swap
	OutContext->ViewportFramebuffer = 0;

	int DebugFlag = 0;

	if ( PlatformOpenGLDebugCtx())
	{
		DebugFlag = WGL_CONTEXT_DEBUG_BIT_ARB;
	}

#if !EMULATE_ES31
	int AttribList[] =
	{
		WGL_CONTEXT_MAJOR_VERSION_ARB, MajorVersion,
		WGL_CONTEXT_MINOR_VERSION_ARB, MinorVersion,
		WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB | DebugFlag,
		WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
		0
	};
#else
	int AttribList[] =
	{
		WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
		WGL_CONTEXT_MINOR_VERSION_ARB, 1,
		WGL_CONTEXT_FLAGS_ARB, DebugFlag,
		WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_ES2_PROFILE_BIT_EXT,
		0
	};
#endif

	OutContext->OpenGLContext = wglCreateContextAttribsARB(OutContext->DeviceContext, InParentContext, AttribList);
}

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!