I'm starting with the programmable pipeline and the shaders in C++ for OpenGL 3.0+, i would love to be able to change some settings on the fly, for example replacing a function with another function, supposing that i have a shader with an operation like

var = A + B

my goal is replacing + with any other operation on the fly with my C++ program, maybe with a GUI, but in general terms with C++, the problem is that until now i'm able to run a shader only after compiling it, so i have something like a static approach.

The same goal applies to the values of the vars that i would also like to change through C++.

It's possible with a programmable pipeline? I'm not interested in performance, just if is possible for the C++ to dynamically communicate and exchange data with the OpenGL pipeline.


I'm not sure exactly what you mean.

You can certainly create a new shader string, compile it into a new shader object, and link it into a new program object. But you can't modify an existing shader's source code.

If you just want to change what gets called when, you can use an if statement in your shader based on uniform variables. Though this can get out of hand if you do it a lot.

GLSL 4.00 adds the ability to define multiple subroutines in a shader and dynamically (meaning, post-linking) pick which one to execute. But that's about it.

  • \$\begingroup\$ I would like to write my small utility with a GUI a find out what changes when changing a particular operation in a shader, like adding vs subtracting, or change a particular value with the help of a slider for example, everything in real time and dynamically. A very basic shader designer using widget from a GUI. \$\endgroup\$
    – user827992
    Aug 20 '12 at 3:55
  • \$\begingroup\$ @user827992: It doesn't matter whether the changes come from a GUI or what have you. If you want to change your shader, you must compile a new shader. You can change uniform variables with the API, but the shader itself is immutable. \$\endgroup\$ Aug 20 '12 at 3:58
  • \$\begingroup\$ to what API are you referring to? \$\endgroup\$
    – user827992
    Aug 20 '12 at 4:07
  • \$\begingroup\$ @user827992: OpenGL. That's what we're talking about, isn't it? \$\endgroup\$ Aug 20 '12 at 4:13
  • \$\begingroup\$ yes, but i was thinking about a specific subset only for changing variables. \$\endgroup\$
    – user827992
    Aug 20 '12 at 4:15

Assumption: You have shader code in the variable.

You actually can do something that accomplishes this. Change the source code of the given shader, recompile it and run the new one.

My use of this :

char s1[] = "#version 430 core\n\
                #define SIZE ";
char *s2 = new char[sizeof(itoa(256, s2, 10))];
char computeShaderSource[] = {"rest of the shader code here"}
char *finalSource = new char[sizeof(s1) + sizeof(s2)+ sizeof(computeShaderSource)];

I really dont guarantee that this is the best practices.

From the performance point of view, it will depend on how many times you will change this.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.