0

My Question is based on this answer GLSL reusable/shared functions, shared constants (OpenGL ES 2.0)?

If I can create a common Shader where is the limit ? Can I write in this common shader Uniform variables ? What is the max amount of common shaders that I can attach to on spezific one ? Increase this Performance ?

Is it recommend ? Anyone experience with this ?

EDIT: In my OpenGL Implementation I have some shaders that extends other shaders. Would it be possible without performance loss to get rid of the duplicate code ?

An example

ShaderC extends ShaderB extends ShaderA. Each of them is loading some uniform variables. Would it be good practice if I create a commonShaderB.glsl and a commonShaderA.glsl and attach them to the program from shaderC to avoid duplicate glsl code ?

Meeresgott
  • 431
  • 3
  • 17

1 Answers1

1

Can I write in this common shader Uniform variables ?

Yes.

What is the max amount of common shaders that I can attach to on spezific one ?

There is no limit on the number of shaders you can attach to a shader program. However, there are limits on the number of instructions, uniform variables, and attributes. It is unlikely that you hit any of these limits with a reasonably designed shader.

Increase this Performance ?

No.

If I can create a Comon Shader where is the limit ? Is it recommend ? Anyone experience with this ?

These questions are too broad or opinion-based and out of scope for StackOverflow. The functionality exists for a reason, so use it when appropriate.

Nico Schertler
  • 32,049
  • 4
  • 39
  • 70
  • Then I ask the question wrong. How can I archive this ? When I try to compile the spezific one I get the following message0(31) : error C1008: undefined variable "position" 0(36) : error C1008: undefined variable "viewMatrix" 0(38) : error C1008: undefined variable "projectionMatrix" 0(46) : error C1008: undefined variable "transformationMatrix" – Meeresgott Jul 24 '19 at 21:42
  • Each shader needs to declare the variables that it is going to use. Multiple shaders that are linked to the same program can declare the same variables. If you want to share variable declarations, you need some way of code generation similar to the C++ pre-processor (`#include`). There is no standard way to do this, however. – Nico Schertler Jul 24 '19 at 21:50
  • Now I had created in the common shader the methods vec3 getPosition() usw. But it seems like that only the Uniform Variables from the common shader are loaded - would it be bad practice if I write this pre-processor on my own ? And compile the assembled part ? – Meeresgott Jul 24 '19 at 22:19
  • What do you mean by "*only the uniform variables from the common shader are loaded*"? Loaded where? If you want to use function defined in another shader, you have to declare them in the first shader as well (without their definitions). Writing the pre-processor on your own is not bad practice. In fact, you don't have many options anyway. – Nico Schertler Jul 24 '19 at 23:36
  • I'm using "glGetUniformLocation(int,String):int" to get the location of uniform variables. If I load up two vertex shader this function will return for uniform variables from the second shader -1. Anyway I'm beginning to write the pre-processsor so I will not have this problem anymore. If I understand the concept correct the pre-processor merges multiple files into one file. This one file will be compiled by openGL. – Meeresgott Jul 25 '19 at 08:58
  • This sounds like you are using `glGetUniformLocation()` wrong. The parameters are actually `GLuint` and `const GLchar*`, where the first parameter is the name of the linked program (not a single shader). – Nico Schertler Jul 25 '19 at 15:34
  • I use the java version from OpenGL a library called lwjgl. Yes but if I compile the shaders and attach them to a program. I should be able to use this function to get the uniform location from all attached shaders but I'm only able to get the locations from the first attached vertex and fragment shader. – Meeresgott Jul 25 '19 at 18:49