View Full Version : Using same Vertex shader
11-06-2011, 08:44 AM
Well I was wondering if there was a way to use the same vertex shader across multiple programs. For example I need about 20 programs and each of them use the same vertex shader. So rather than storing the uniform location of the same thing for 20 different programs just store it for one? Is there a way to do this? Or better yet is there a way to use only 1 program yet still be able to use multiple fragment shaders?
11-06-2011, 09:23 AM
If your hardware allows it, you can use the separable programs. Each programs owns one stage (so a program holds a vertex shader). Then you combine them in a program pipeline.
11-06-2011, 10:24 AM
Well I'm targeting opengl 2.0 right now. Not sure what you mean by separable programs, could you elaborate on that please.
How efficient are the glAttachShader and glDetachShader functions? That would be one possible solution to attach and detach shaders as needed but it would be called fairly often, is this a viable solution?
11-06-2011, 10:59 AM
Forget them, it's an OpenGL 4.0 feature.
11-06-2011, 12:18 PM
Forget them, it's an OpenGL 4.0 feature.
Technically, it is a 4.1 feature. But it is also an extension. If you're using an NVIDIA GPU of GeForce 6xxx or better, you can use the extension just fine. All other GL 2.1 drivers won't expose it, but that's only because they're no longer supported.
11-06-2011, 03:16 PM
I'm using an ATI but I was hoping to have it be used on pretty much any gpu.
So any other suggestions to how to use many shaders? There isn't really a whole lot of information on it, almost any information I find is usually directed to how to get certain effects.
On a side note I was hoping someone could explain why this is happening. I have an attribute set in my vertex shader and I am using "glVertexAttribPointer" to set the attribute, which is an unsigned short. The only problem is that for some reason it converts it to a type "float" rather than an "int", like so:
uniform mat4 bones;
attribute float node;
gl_Position = gl_ModelViewProjectionMatrix * bones[ int(node) ] * vec4(gl_Vertex.xyz, 1.0);
11-06-2011, 03:25 PM
So any other suggestions to how to use many shaders?
If you can't use ARB_separate_shader_objects, then the only option left is the obvious. Each program is independent and must be treated as such. You must query and store each program's uniform locations independently. And so forth.
This problem is exactly why ARB_separate_shader_objects exists; so you don't have to do this. But if you don't have access to it, you have to accept dealing with what's left.
That's why whenever possible I only concern myself with hardware that's still being supported. Any hardware that doesn't implement ARB_separate_shader_objects is either Intel (where they barely implement OpenGL as is) or is no longer having drivers written for it.
It should be noted that GL 3.x-capable hardware dates back to 2006, so 5+ years now. It can be found embedded in motherboards and even CPUs. It's rather common.
I have an attribute set in my vertex shader and I am using "glVertexAttribPointer" to set the attribute, which is an unsigned short. The only problem is that for some reason it converts it to a type "float" rather than an "int", like so:
Unless you are using GLSL 1.30 or above (or using the EXT_gpu_shader4 extension), you cannot have integer attribute types. And if you do, you have to use `glVertexAttribIPointer` (http://www.opengl.org/sdk/docs/man3/xhtml/glVertexAttribPointer.xml) to set them up.
11-09-2011, 08:41 AM
Thanks for the help, my nvidia card (8600) does have GL_ARB_separate_shader_objects but my ati card (3650 mobility) doesn't.
I am planning on using one program, rather than having thousands, with a switch statement in the main function for the vertex and fragment shader. Along with a uniform int to choose the appropriate functions. What I was wondering is if anyone knows if glsl creates a jump table for a switch statement, much like a C/C++ compiler would?
11-09-2011, 10:46 AM
but my ati card (3650 mobility) doesn't.
Have you tried updating your drivers? Driver updates for mobile GPUs are harder, but still doable.
What I was wondering is if anyone knows if glsl creates a jump table for a switch statement, much like a C/C++ compiler would?
If I recall correctly, switch statements are only available in 1.30 and above. As to exactly what it compiles to, it's compiler-specific.
11-10-2011, 04:45 AM
Using uniform buffer objects will greatly lessen the need to query uniform locations, especially if you use std140 layout. Uniform buffer objects can be shared to any/all programs, so it is easy to set uniform value once and it will affect all or selected programs.
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.