Jump to content

  • Log In with Google      Sign In   
  • 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.

  • You cannot reply to this topic
15 replies to this topic

#1 Rhiakath   Members   -  Reputation: 122

Like
0Likes
Like

Posted 27 April 2009 - 11:11 AM

Hi there. I'm trying to keep my GL framework simple, and i'm currently facing a design doubt. How to initialize the extensions? Like, the framework is made up of several cpp files, so when i try to declare the extensions in an .h file, it just gives me linker errors, saying that those functions are already declared on the previous cpp file. All those files should have access to the extensions. For example, I have a couple of files for shaders, one file for general opengl operations, among other things. the shader files should have access to the function pointers, just as the general opengl file. How to do this? I tried using "external " keyword on the declaration, and then redeclaring them on the main cpp, and initialize them there. I'd assume all the cpp files would have access to the now initialized func pointers. Didn't work. Also tried creating a singleton class, only for extension pointers, with static members. Compiler borked on me. This is g++/linux. Any ideas?

Sponsor:

#2 Kwizatz   GDNet+   -  Reputation: 1206

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




#3 V-man   Members   -  Reputation: 805

Like
0Likes
Like

Posted 27 April 2009 - 12:53 PM

Quote:
Original post by Rhiakath
Hi there. I'm trying to keep my GL framework simple, and i'm currently facing a design doubt. How to initialize the extensions? Like, the framework is made up of several cpp files, so when i try to declare the extensions in an .h file, it just gives me linker errors, saying that those functions are already declared on the previous cpp file. All those files should have access to the extensions. For example, I have a couple of files for shaders, one file for general opengl operations, among other things. the shader files should have access to the function pointers, just as the general opengl file. How to do this? I tried using "external " keyword on the declaration, and then redeclaring them on the main cpp, and initialize them there. I'd assume all the cpp files would have access to the now initialized func pointers.


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.

#4 Rhiakath   Members   -  Reputation: 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?

#5 HuntsMan   Members   -  Reputation: 368

Like
0Likes
Like

Posted 28 April 2009 - 01:18 AM

Quote:
Original post by Rhiakath
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?


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

I prefer core versions, but is just me :)

#6 Rhiakath   Members   -  Reputation: 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.

#7 samoth   Crossbones+   -  Reputation: 5034

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).

#8 Brother Bob   Moderators   -  Reputation: 8605

Like
0Likes
Like

Posted 28 April 2009 - 03:00 AM

Quote:
Original post by Rhiakath
rhiakath@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 Rhiakath
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.

... 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.

#9 AndyEsser   GDNet+   -  Reputation: 386

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.

#10 Rhiakath   Members   -  Reputation: 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?

#11 Rhiakath   Members   -  Reputation: 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();
.
.
.
OpenGL::CGLShaderProgram Program;
OpenGL::CGLShader VS, PS;
glCreateShader(GL_VERTEX_SHADER);
Context.InitializeExtensions();
glCreateShader(GL_VERTEX_SHADER);
VS.LoadFromFile ( OpenGL::CGLShader::VERTEX_SHADER, "default.vertex_shader" );

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?

#12 MARS_999   Members   -  Reputation: 1297

Like
0Likes
Like

Posted 28 April 2009 - 04:47 AM

OpenGL::CGLShaderProgram Program;
OpenGL::CGLShader VS, PS;
glCreateShader(GL_VERTEX_SHADER);
Context.InitializeExtensions();

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


#13 Brother Bob   Moderators   -  Reputation: 8605

Like
0Likes
Like

Posted 28 April 2009 - 04:51 AM

Quote:
Original post by Rhiakath
And, 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 Rhiakath
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?

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.


#14 Rhiakath   Members   -  Reputation: 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.

#15 Brother Bob   Moderators   -  Reputation: 8605

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.

#16 Rhiakath   Members   -  Reputation: 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.



PARTNERS