OpenGL extensions, how to use them correctly in C and glsl
Asked Answered
I

2

6

I am working on a game engine and it has evolved greatly. Because the engine needs to work on mac also, I am still using OpenGL 3.2 and GLSL 1.2 :-(. I use GLEW which I assumed would solve extension issues for me.

EDIT: Meanwhile, part of the question is answered - read on at the next EDIT:

I was always able to make shaders work on both windows and mac, sometimes I would have to add a line in the GLSL code like #extension GL_EXT_gpu_shader4 : enable to make it work on Mac. It then seems that my Windows version will give a warning when compiling the same shader, but it will work.

But ever since I started using geometry shaders, my nightmare has begun. For some reason, mac expects me to use #extension GL_EXT_gpu_shader4 : enable while windows expects me to use #extension GL_EXT_geometry_shader4 : enable. This makes it less obvious to create a platform independent shader.

Also, and this is even more annoying: Windows version wants me to use: glTransformFeedbackVaryings and I would think that GLEW would make it available to the mac but there I explicitly need to use glTransformFeedbackVaryingsEXT which will not work on the windows version. So I need to #ifdef for APPLE to use what's needed. Same problem with glBeginTransformFeedback and glBeginTransformFeedbackEXT.

But both accept glProgramParameteriEXT, there I don't need the distiction...

I understand that it's only with the transform feedback that I am having the problem, but... what is this all about?

I thought I understood how OpenGL extensions worked, but I am starting to lose that understanding.

It's getting to a point where I think that when I run the code on another windows system or another mac system or another linux system, I will have new problems because there are different graphics adapters or something.

Can anyone help me understand this? I have read the OpenGL manual about extensions, so I am missing something obvious. How can I make sure that my code will always work?

EDIT: By removing GLEW entirely from the Mac version and fully using the Mac's own OpenGL 3.2 implementation, all the namings are normal and I can entirely remove the #extension GL_EXT_gpu_shader4 : enable from my shaders. The only thing that makes me worry now is that for geometry shaders, I need the function glProgramParameteri which doesn't seem to exist, but glProgramParameteriEXT does. For some reason, this function also works on Windows. Why? Can I be certain that it will work on all systems/hardware?

Isaac answered 27/2, 2013 at 8:40 Comment(5)
Apple usually is a bit behind, so you might just want to check the officialy supported version. this was posted just before your question.Flavoprotein
Hm, I don't think that post helps me. I am developing on OSX Lion 10.7.5 and it's an AMD graphics adapter. But if I understand you correctly, the problems are related to Apple's bad implementation? How do all those games on steam get ported and work?Isaac
Why aren't you using the core OpenGL 3.2 available on MacOSX.Yuonneyup
Ok, that really makes sense! I thought GLEW would work perfectly on Mac as well, but I have now removed GLEW completely from the Mac version and everything works perfectly with the normal namings (except for glProgramParameteriEXT which is also the function I need for windows, why is that?)Isaac
Updated my post for the change from GLEW to native Mac implementation, but also for the question in my message above.Isaac
G
3

EXT means that you are using an extension for the geometry shader. This should work on windows as well. Geometry shader should be core functionality in OpenGL 3.2, but right now you are instead using the extension (Basically you are using functionality of older OpenGL versions not actual 3.2). To use the core geometry shader you have to switch to GLSL 1.5.

I am not that well versed in OpenGL but my assumption is, that glProgramParameteri() is actually not necessary anymore if you start using core functionality, that is why it is missing in the library headers.

Gratis answered 11/4, 2013 at 14:31 Comment(3)
Thanks, that is a very interesting answer. I will absorb it and take some time to find out if it completely answers my question.Isaac
You are right that glProgramParameteri() is no longer needed in core functionality. There those parameters are set inside the shader code itself.Isaac
And I think you have fully answered my question. I will mark it as the correct answer.Isaac
A
3

Your life will be easier if you stick to matching OpenGL/GLSL versions.

http://en.wikipedia.org/wiki/OpenGL_Shading_Language

Pick from:

  • OpenGL 2.1, with GLSL 120 + GL_EXT_gpu_shader4 + GL_EXT_geometry_shader4
  • OpenGL 3.2, with GLSL 150
  • OpenGL 3.3, with GLSL 330

If you are already happy using OpenGL 3.2, then step up to GLSL 150, as it's closer to modern GLSL 330+. If you want the most compatibility, use OpenGL 2.1, GLSL 120, plus geometry shader extensions. (though I'm not sure how much more compatibility it buys you)

ONLY when using GLSL 120 plus extensions, you need to use glProgramParameteriEXT() to specify the geometry shader in/out primitive type, and vertex count. For example:

glUseProgram(prog);
glProgramParameteriEXT(prog, GL_GEOMETRY_INPUT_TYPE_EXT, GL_TRIANGLES);
glProgramParameteriEXT(prog, GL_GEOMETRY_OUTPUT_TYPE_EXT, GL_TRIANGLE_STRIP);
glProgramParameteriEXT(prog, GL_GEOMETRY_VERTICES_OUT_EXT, max_output_verticies);

https://wiki.engr.illinois.edu/display/graphics/Geometry+Shader+Hello+World

In OpenGL 3.2, GLSL 150+, you specify this information in your shader, using a declaration like:

#version 150
layout (triangles) in;
layout (triangle_strip, max_vertices = 3) out;

http://www.lighthouse3d.com/tutorials/glsl-core-tutorial/geometry-shader/

Alcott answered 21/9, 2013 at 2:24 Comment(1)
Well David, thank you, and I would totally agree on this, but here's the thing: when I try to create my shaders to be compatible with Mac, I must use 120 + the extensions. It works fine, but it won't work on my desktop PC where the nvidia driver will fail on the extension line to be non-existant. At this moment I have just ditched the mac support, I can always write extra code for the mac later. It's just holding me back. I'm very happy with 3.3+330 now.Isaac

© 2022 - 2024 — McMap. All rights reserved.