Difference between revisions of "Example/GLSL Separate Program Multi Stage"

From OpenGL Wiki
Jump to: navigation, search
(Adding header.)
 
Line 2: Line 2:
  
 
<source lang="cpp">
 
<source lang="cpp">
//Create two programs. One with just the vertex shader, and  
+
// Create two programs. One with just the vertex shader, and  
//one with both geometry and fragment stages.
+
// one with both geometry and fragment stages.
 
GLuint vertexProgram  = glCreateProgram();
 
GLuint vertexProgram  = glCreateProgram();
 
GLuint geomFragProgram = glCreateProgram();
 
GLuint geomFragProgram = glCreateProgram();
  
//Declare that programs are separable - this is crucial!
+
// Declare that programs are separable - this is crucial!
 
glProgramParameteri(vertexProgram  , GL_PROGRAM_SEPARABLE, GL_TRUE);
 
glProgramParameteri(vertexProgram  , GL_PROGRAM_SEPARABLE, GL_TRUE);
 
glProgramParameteri(geomFragProgram, GL_PROGRAM_SEPARABLE, GL_TRUE);
 
glProgramParameteri(geomFragProgram, GL_PROGRAM_SEPARABLE, GL_TRUE);
  
//Generate and compile shader objects, as normal.
+
// Generate and compile shader objects, as normal.
 
GLuint vertShader  = glCreateShader(GL_VERTEX_SHADER);
 
GLuint vertShader  = glCreateShader(GL_VERTEX_SHADER);
 
GLuint geomShader  = glCreateShader(GL_GEOMETRY_SHADER);
 
GLuint geomShader  = glCreateShader(GL_GEOMETRY_SHADER);
Line 24: Line 24:
 
glCompileShader(fragShader);
 
glCompileShader(fragShader);
  
//Attach the shaders to their respective programs
+
// Attach the shaders to their respective programs
 
glAttachShader(vertexProgram  , vertShader);
 
glAttachShader(vertexProgram  , vertShader);
 
glAttachShader(geomFragProgram, geomShader);
 
glAttachShader(geomFragProgram, geomShader);
 
glAttachShader(geomFragProgram, fragShader);
 
glAttachShader(geomFragProgram, fragShader);
  
//Perform any pre-linking steps.
+
// Perform any pre-linking steps.
 
glBindAttribLocation(vertexProgram    , 0, "Position");
 
glBindAttribLocation(vertexProgram    , 0, "Position");
 
glBindFragDataLocation(geomFragProgram, 0, "FragColor");
 
glBindFragDataLocation(geomFragProgram, 0, "FragColor");
  
//Link the programs
+
// Link the programs
 
glLinkProgram(vertexProgram);
 
glLinkProgram(vertexProgram);
 
glLinkProgram(geomFragProgram);
 
glLinkProgram(geomFragProgram);
  
//Detach and delete the shader objects
+
// Detach and delete the shader objects
 
glDetachShader(vertexProgram, vertShader);
 
glDetachShader(vertexProgram, vertShader);
 
glDeleteShader(vertShader);
 
glDeleteShader(vertShader);
Line 46: Line 46:
 
glDeleteShader(fragShader);
 
glDeleteShader(fragShader);
  
//Generate a program pipeline
+
// Generate a program pipeline
 
glGenProgramPipelines(1, &pipeline);
 
glGenProgramPipelines(1, &pipeline);
  
//Attach the first program to the vertex stage, and the second program
+
// Attach the first program to the vertex stage, and the second program
//to the geometry and fragment stages
+
// to the geometry and fragment stages
 
glUseProgramStages(pipeline, GL_VERTEX_SHADER_BIT, vertexProgram);
 
glUseProgramStages(pipeline, GL_VERTEX_SHADER_BIT, vertexProgram);
 
glUseProgramStages(pipeline, GL_GEOMETRY_SHADER_BIT | GL_FRAGMENT_SHADER_BIT, geomFragProgram);
 
glUseProgramStages(pipeline, GL_GEOMETRY_SHADER_BIT | GL_FRAGMENT_SHADER_BIT, geomFragProgram);
 
</source>
 
</source>
 
<noinclude>[[Category:Example Code]]</noinclude>
 
<noinclude>[[Category:Example Code]]</noinclude>

Latest revision as of 11:13, 3 January 2018

Creates a separate program, where some of the stages are directly linked together.

// Create two programs. One with just the vertex shader, and 
// one with both geometry and fragment stages.
GLuint vertexProgram   = glCreateProgram();
GLuint geomFragProgram = glCreateProgram();

// Declare that programs are separable - this is crucial!
glProgramParameteri(vertexProgram  , GL_PROGRAM_SEPARABLE, GL_TRUE);
glProgramParameteri(geomFragProgram, GL_PROGRAM_SEPARABLE, GL_TRUE);

// Generate and compile shader objects, as normal.
GLuint vertShader  = glCreateShader(GL_VERTEX_SHADER);
GLuint geomShader  = glCreateShader(GL_GEOMETRY_SHADER);
GLuint fragShader  = glCreateShader(GL_FRAGMENT_SHADER);

glShaderSource(vertShader, 1, &vertSrc, NULL);
glShaderSource(geomShader, 1, &geomSrc, NULL);
glShaderSource(fragShader, 1, &fragSrc, NULL);

glCompileShader(vertShader);
glCompileShader(geomShader);
glCompileShader(fragShader);

// Attach the shaders to their respective programs
glAttachShader(vertexProgram  , vertShader);
glAttachShader(geomFragProgram, geomShader);
glAttachShader(geomFragProgram, fragShader);

// Perform any pre-linking steps.
glBindAttribLocation(vertexProgram    , 0, "Position");
glBindFragDataLocation(geomFragProgram, 0, "FragColor");

// Link the programs
glLinkProgram(vertexProgram);
glLinkProgram(geomFragProgram);

// Detach and delete the shader objects
glDetachShader(vertexProgram, vertShader);
glDeleteShader(vertShader);

glDetachShader(geomFragProgram, geomShader);
glDetachShader(geomFragProgram, fragShader);
glDeleteShader(geomShader);
glDeleteShader(fragShader);

// Generate a program pipeline
glGenProgramPipelines(1, &pipeline);

// Attach the first program to the vertex stage, and the second program
// to the geometry and fragment stages
glUseProgramStages(pipeline, GL_VERTEX_SHADER_BIT, vertexProgram);
glUseProgramStages(pipeline, GL_GEOMETRY_SHADER_BIT | GL_FRAGMENT_SHADER_BIT, geomFragProgram);