# OpenGL Program Objects Usage

This topic is 4236 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello all, I used to code with OpenGL a while back, but never touched shaders. I am trying to get myself up to date with it again and have a few questions regarding program objects. a) Does a PO require both pixel and vertex shaders to be set? b) When you bind a null PO, do you revert to FFP ? c) What is the most optimal way to utilise program objects? I thought I would create a single PO and assign my multiple shaders to it before drawing. I am not sure if this would be best solution so please let me know what you think. Something tells me the assigning and linking when drawing objects that use different shaders hurts performance. Thanks!

##### Share on other sites
a) no, you can use both or one of the other and the driver will supply you with the one you dont' create. That said, myself and others have noticed pretty large performance loss when you don't supply both shaders, this could be a driver thing and it might be fixed however it's probably best just to provide both and be done with it

b) yes

c) GLSL PO and shaders should be thought of the same way as textures; setup at the beginning. Compiling and linking a shader is very heavy work, so do it at start up.

##### Share on other sites
Thank you for the reply phantom.

With regards to my third question. I understand the compiling of shaders would not be suitable every frame, but what about linking while drawing? I am not sure if this is possible but as an example:

glAttachObjectARB( program_object, vertex_shader0 );glAttachObjectARB( program_object, fragment_shader1 );glLinkProgramARB( program_object);glUseProgramObjectARB( program_object );// Now render all objects that use vs0 and fs1glAttachObjectARB( program_object, vertex_shader0 );glAttachObjectARB( program_object, fragment_shader4 );glLinkProgramARB( program_object);glUseProgramObjectARB( program_object );// Now render all objects that use vs0 and fs4

Could this work and would it be wise to do it like this?

The only other choice ( as I can see ) is having a PO for every shader configuration.

i.e FS0 and VS0 has PO0, FS1 and VS0 has PO1, FS0 and VS1 has PO2, etc...

where FS = fragment shader, VS = vertex shader, PO = program object

Thanks

##### Share on other sites
linking is also out as it's a heavy operation.
For starters you'd have to deattach the shaders you weren't going to use (FS in this case) and attach a new one. When linking the driver has to perform various validations and match up slots and assign slots for data; this is all as I say heavy work and doing it mid-frame would kill your frame rate.

So, yes, the best way is to produce your combinations up front

##### Share on other sites
As phantom pointed out, neither compiling nor linking shaders in a program should be done in real-time. Also, I'd like to point out that you can reuse your shaders and link them to several programs (and detach/destroy them thereafter). I remember having some problems with (old) ATI cards in the past, but I can't remember if it had anything to do with this. It did have something to do with not following the specs though, so be careful.

##### Share on other sites
Quote:
 Original post by phantoma) no, you can use both or one of the other and the driver will supply you with the one you dont' create. That said, myself and others have noticed pretty large performance loss when you don't supply both shaders, this could be a driver thing and it might be fixed however it's probably best just to provide both and be done with it

Interesting, I've not heard about that before. I usually just use a fragment shader and leave the vertex shader as the fixed function.

I'll have to try supplying a simple vertex shader and see if that makes any effect on my framerate (it could explain the surprisingly poor performance I got a while ago).

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
17
5. 5

• 9
• 14
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
631756
• Total Posts
3002113
×