• Create Account

## OpenGL extension initialization

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

15 replies to this topic

### #1Rhiakath  Members

122
Like
0Likes
Like

Posted 27 April 2009 - 11:11 AM

### #2Kwizatz  Members

1343
Like
0Likes
Like

Posted 27 April 2009 - 11:26 AM

Why don't you just use GLee or GLEW?

Otherwise, check for the GL_VERSION_[mayor]_[minor] macro before declaring your functions, for example:

#ifndef GL_VERSION_2_0// define GL 2.0 extensions here#endif

### #3V-man  Members

813
Like
0Likes
Like

Posted 27 April 2009 - 12:53 PM

Quote:

Yes, it will work. Declare as extern in a single .h file. include that .h in all your .cpp files. Keep the definition in a single .cpp file.
That's what I use to do. Then I moved to glew.

### #4Rhiakath  Members

122
Like
0Likes
Like

Posted 28 April 2009 - 12:15 AM

Yes, I was using glew too, but i wanted to do the initialization myself, because i don't understand some minor stuff about glew.
Also, if I have the glCreateShaderARB extension, and also the glCreateShader core function, should I use the core, or the extension? Why are the pointers different? Since it's now part of core, shouldn't the pointers to the functions point to the same place?

### #5HuntsMan  Members

368
Like
0Likes
Like

Posted 28 April 2009 - 01:18 AM

Quote:
 Original post by RhiakathYes, I was using glew too, but i wanted to do the initialization myself, because i don't understand some minor stuff about glew. Also, if I have the glCreateShaderARB extension, and also the glCreateShader core function, should I use the core, or the extension? Why are the pointers different? Since it's now part of core, shouldn't the pointers to the functions point to the same place?

Because one returns a GLhandleARB, and the core version uses GLuint's as handles.

I prefer core versions, but is just me :)

### #6Rhiakath  Members

122
Like
0Likes
Like

Posted 28 April 2009 - 02:25 AM

rhiakath@Gentoo /usr/include/GL $grep GLhandleARB * glext.h:typedef unsigned int GLhandleARB; /* shader object handle */ So, they're both the same. So, the only difference is really the function pointer. Supposedly they both do the same. Do you guys know any reason for the pointer not be the same? Also, the reason because I wanted to do my own initializing was just because I was planning to do something like, if both funcs are available, then both pointers point to the core func. That way, I would always use the call to the core func, and let the pointers do the rest durint init. I would not have to have a gazillion if's spread across the code to see if the function is available as core, instead of extension. ### #7samoth Members 8966 Like 0Likes Like Posted 28 April 2009 - 03:00 AM If you really want to do your own initialisation (I'm doing that because I find Glee's mere code size and parse overhead unacceptable), you're probably off best writing a spec parser and automatising the process. You can grab all the extensions in 3-4 minutes off opengl.org with wget. When a new extension that you want to use 4comes out, download that additional spec and re-run your tool for a new header/source pair. What you need for the extension loading are the names of functions. You don't have to care about constants or everything else, all that is already in glext.h. In every spec file, you find the names of the functions in the section labelled New\s+(Functions|Procedures)+ (it may be "functions and procedures" or "procedures and functions", sometimes it's "functions, procedures, and something else" too). The typedefs for the function pointers in glext.h are named PFNGL(NAME-OF-FUNCTION)PROC, which is easy enough to automate. You'll want to declare extern global variables for the function pointers and have a source file that defines each of them and that has a "init" function of some sort which retrieves the addresses. From your point of view, they'll work like plain ordinary functions later. I'm doing the spec parsing using a PHP script, as PHP nicely supports reading files into an array, running a regex over them, and building the output header/source, eleminating duplicate elements etc. You'd probably take half a year coding that in C whereas it's a mere afternoon and under 500 lines of code (including formatting) in PHP. Other people may prefer perl instead (which I'm uncomfortable with), it sure does the job just fine, too -- use what you like. Two things to watch for: 1. Some functions may be defined identically in two (or possibly more) extension specs, so you must make sure you don't break the one-definition-rule. PHP's array_unique() is your friend here. 2. glext.h may be erroneous. This is currently (revision of march 27, 2009) the case with ARB_instanced_arrays, which lacks the ARB suffix in its function pointer names. Unluckily, there is no easy way to deal with this in an automated manner. GLee implements this extension wrong (in accordance with glext.h) which of course works fine. You might do the same or you could edit glext.h, or you could wait and hope that they fix it eventually (and you might not need that particular extension at all). ### #8Brother Bob Moderators 10108 Like 0Likes Like Posted 28 April 2009 - 03:00 AM Quote:  Original post by Rhiakathrhiakath@Gentoo /usr/include/GL$ grep GLhandleARB * glext.h:typedef unsigned int GLhandleARB; /* shader object handle */So, they're both the same.

Purely an implementation detail. As far as OpenGL and the interface is concerned, the types are different; one is GLuint and one is GLhandleARB.

Quote:
 Original post by Rhiakath So, the only difference is really the function pointer. Supposedly they both do the same. Do you guys know any reason for the pointer not be the same?

Supposedly they both do the same? If you cannot guarantee that they are, in fact, doing exactly the same thing, then...

Quote:
 Original post by RhiakathAlso, the reason because I wanted to do my own initializing was just because I was planning to do something like, if both funcs are available, then both pointers point to the core func. That way, I would always use the call to the core func, and let the pointers do the rest durint init.I would not have to have a gazillion if's spread across the code to see if the function is available as core, instead of extension.

... really is a bad idea. Given that you say the function pointers returned are different suggest that you should be very careful.

Even though the documentation suggests they behave the same, there could be subtle differences as allocating names from different pools, accepting slightly different parameters (maybe the ARB-variant accept some additional parameter for some other extension, like geometry shaders which is not a part of the core), or otherwise behaving different internally. Subtle differences like that may not be obvious from the documentation. The API and interface makes a difference between the function (the name), the driver exposes different entry points, so you better treat them as different.

### #9AndyEsser  GDNet+

394
Like
0Likes
Like

Posted 28 April 2009 - 03:35 AM

I use GLEW in my engine, found it extremely easy to install and use and grants me instant access to the extensions I need. However, even if it says that a core function is supported as core, I still use the ARB one, simply because 1) it's easy than programming two different ways for different users based on whether it's supported or not 2) I had issues with FBO's not working when using the Core functions exposed through GLEW.

### #10Rhiakath  Members

122
Like
0Likes
Like

Posted 28 April 2009 - 04:18 AM

I assumed they do the same thing, not that they do it the same way.
And, of course, I'd only do this with functions that had similar signatures, right?

Now, is it really needed for me to have a thousand if's, just checking if the core function is available, and if not use the extension?
Is there no other way?

### #11Rhiakath  Members

122
Like
0Likes
Like

Posted 28 April 2009 - 04:41 AM

Ok, maybe you guys can help me out if I show you a little snippet.

Tihs is a part of the main() function.

int main ( int argc, char *argv[] )
{
OpenGL::CGLContext Context;

Context.Initialize();
Context.CreateWindow();
.
.
.
Context.InitializeExtensions();

So, inside Initialize, all it does is create a context, then create a window to that context. Get extensions, destroy window (not the context).

The problem is, glCreateShader resolves just fine, when i get the pointer. the debugger shows me an address, and successfully resolves it into the func name, so I check it's correct.
after it leaves Initialize(), it comes to glCreateShader.
It segfaults here.
If I comment out that line, it just segfaults on the next glCreateShader, even after getting the func pointers again inside InitializeExtensions.
The only way it does not segfault, is if I call glCreateShader inside InitializeExtensions.

Nevertheless, the pointer remains with the same value all across the whole session.

What's happening here?

### #12MARS_999  Members

1589
Like
0Likes
Like

Posted 28 April 2009 - 04:47 AM

Context.InitializeExtensions();

If I am reading this right you have glCreateShader() called before you initialize the extensions?

### #13Brother Bob  Moderators

10108
Like
0Likes
Like

Posted 28 April 2009 - 04:51 AM

Quote:
 Original post by RhiakathAnd, of course, I'd only do this with functions that had similar signatures, right?

I assume you mean identical signature, not similar. Similar signature implies there is a difference in the signature, and then they definitely are different. But still, identical signature does in no way imply same functionality.

Quote:
 Original post by RhiakathNow, is it really needed for me to have a thousand if's, just checking if the core function is available, and if not use the extension?Is there no other way?

No, it is not needed to make thousands of ifs to get around this. A few perhaps if you wrap it. For example, wrap shader functions around some unified interface, and then let the implementation of the interface deal with different extension and core support.
GLuint loadprogram(GLuint vshader, GLuint fshader){	GLuint program = glCreateProgram();	glAttachShader(program, vshader);	glAttachShader(program, fshader);	glLinkProgram(program);	return program;}

A quick example using the core variants to create a program with a vertex and a fragment shader. A single if is needed to make this use the ARB-variants when needed and I can still load all my shaders from thousands of places, which would need thousands of ifs without the wrapper.

### #14Rhiakath  Members

122
Like
0Likes
Like

Posted 28 April 2009 - 04:51 AM

Reading only that part, it would seem so. But as I said, it is calling Initialize Extensions inside Initialize, right at the beginning.
I'm just trying to figure out where it breaks, so I'm calling it twice, and enveloping it with glCreateShader.

### #15Brother Bob  Moderators

10108
Like
0Likes
Like

Posted 28 April 2009 - 04:55 AM

Some more replies there before my post. Looks like there are some wrapper classes for shaders there, which is what I'm talking about. Don't use OpenGL-code directly everywhere; wrap it and use the interface instead.

### #16Rhiakath  Members

122
Like
0Likes
Like

Posted 28 April 2009 - 05:08 AM

That is what I'm doing. unfortunately, the opengl calls inside the shader class all segfault. I just added the opengl calls here to be able to see where it screws up. What bothers me, is that between the initialize call, and the shader loading call, there is nothing. and the pointer remains the same. why does it segfault?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.