]> git.tdb.fi Git - geometrycompositor.git/commitdiff
Move shader program linking to a separate function
authorMikko Rasa <tdb@tdb.fi>
Sat, 12 Dec 2015 11:16:17 +0000 (13:16 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 12 Dec 2015 11:16:17 +0000 (13:16 +0200)
source/main.c

index 731323fe75afdcd754c7e05e9e50fec6968b72fd..0992542fa960d3bcf1ef400b3685925b1b4cf98e 100644 (file)
@@ -181,12 +181,36 @@ unsigned compile_shader(GLenum type, const char *source)
        return shader;
 }
 
-int create_gl_resources(Compositor *compositor, CompositedScreen *screen)
+unsigned link_program(unsigned *shaders, unsigned nshaders)
 {
+       unsigned program;
+       unsigned i;
        int status;
        char info_log[1024];
        GLsizei length;
 
+       program = glCreateProgram();
+       for(i=0; i<nshaders; ++i)
+               glAttachShader(program, shaders[i]);
+       glBindAttribLocation(program, 0, "vertex");
+       glLinkProgram(program);
+
+       glGetProgramiv(program, GL_LINK_STATUS, &status);
+       glGetProgramInfoLog(program, sizeof(info_log), &length, info_log);
+       if(!status)
+       {
+               fprintf(stderr, "Program link failed:\n%s\n", info_log);
+               glDeleteProgram(program);
+               return 0;
+       }
+       else if(length)
+               printf("Program info log:\n%s\n", info_log);
+
+       return program;
+}
+
+int create_gl_resources(Compositor *compositor, CompositedScreen *screen)
+{
        use_gl(compositor, screen);
 
        screen->shaders[0] = compile_shader(GL_VERTEX_SHADER, vshader);
@@ -194,25 +218,12 @@ int create_gl_resources(Compositor *compositor, CompositedScreen *screen)
        if(!screen->shaders[0] || !screen->shaders[1])
                return 0;
 
-       screen->program = glCreateProgram();
-       glAttachShader(screen->program, screen->shaders[0]);
-       glAttachShader(screen->program, screen->shaders[1]);
-       glBindAttribLocation(screen->program, 0, "vertex");
-       glLinkProgram(screen->program);
+       screen->program = link_program(screen->shaders, 2);
+       if(!screen->program)
+               return 0;
 
        screen->geometry_loc = glGetUniformLocation(screen->program, "geometry");
 
-       glGetProgramiv(screen->program, GL_LINK_STATUS, &status);
-       glGetProgramInfoLog(screen->program, sizeof(info_log), &length, info_log);
-       if(!status)
-       {
-               fprintf(stderr, "Program link failed:\n%s\n", info_log);
-               glDeleteProgram(screen->program);
-               return 0;
-       }
-       else if(length)
-               printf("Program info log:\n%s\n", info_log);
-
        glGenBuffers(1, &screen->vertex_buffer);
        glBindBuffer(GL_ARRAY_BUFFER, screen->vertex_buffer);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);