How to do it...

Start by loading the shader files into std::string:

std::string vertCode  = loadShaderCode("separable.vert.glsl");
std::string fragCode1 = loadShaderCode("separable1.frag.glsl");
std::string fragCode2 = loadShaderCode("separable2.frag.glsl");

Next, we'll create one shader program for each using glCreateShaderProgramv:

GLuint programs[3];
const GLchar * codePtrs = {vertCode.c_str(), fragCode1.c_str(),
fragCode2.c_str()};
programs[0] = glCreateShaderProgramv(GL_VERTEX_SHADER, 1, codePtrs);
programs[1] = glCreateShaderProgramv(GL_FRAGMENT_SHADER, 1, codePtrs + 1);
programs[2] = glCreateShaderProgramv(GL_FRAGMENT_SHADER, 1, codePtrs + 2);

// Check for errors...

Now, we'll create two program pipelines. The first will use the vertex shader and the first fragment shader, and the second will use the vertex shader and the second fragment shader:

GLuint pipelines[2];
glCreateProgramPipelines(2, pipelines);
// First pipeline
glUseProgramStages(pipelines[0], GL_VERTEX_SHADER_BIT, programs[0]);
glUseProgramStages(pipelines[0], GL_FRAGMENT_SHADER_BIT, programs[1]);
// Second pipeline
glUseProgramStages(pipelines[1], GL_VERTEX_SHADER_BIT, programs[0]);
glUseProgramStages(pipelines[1], GL_FRAGMENT_SHADER_BIT, programs[2]);

To set uniform variables in separable shaders, the recommended technique is to use glProgramUniform rather than glUniform. With separable shaders and program pipelines, it can be a bit tedious and tricky to determine which shader stage is affected by the glUniform functions. The glProgramUniform functions allow us to specify the target program directly. For example, here, we'll set a uniform in the vertex shader program (shared by the two pipelines):

GLint location = glGetUniformLocation(programs[0], uniformName);
glProgramUniform3f(programs[0], location, 0, 1, 0);

To render, we first need to make sure that no programs are currently bound. If there is a program bound via glUseProgram, it will ignore any program pipelines:

glUseProgram(0);

Now, we can use the pipelines that we set up earlier:

glBindProgramPipeline(pipelines[0]);
// Draw...
glBindProgramPipeline(pipelines[1]);
// Draw...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.41.214