Line data Source code
1 :
2 : /* Copyright (c) 2015, Stefan Eilemann <eile@equalizergraphics.com>
3 : * Daniel Nachbaur <danielnachbaur@gmail.com>
4 : *
5 : * This library is free software; you can redistribute it and/or modify it under
6 : * the terms of the GNU Lesser General Public License version 2.1 as published
7 : * by the Free Software Foundation.
8 : *
9 : * This library is distributed in the hope that it will be useful, but WITHOUT
10 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 : * details.
13 : *
14 : * You should have received a copy of the GNU Lesser General Public License
15 : * along with this library; if not, write to the Free Software Foundation, Inc.,
16 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 : */
18 :
19 : #include "shader.h"
20 :
21 : #include <eq/gl.h>
22 : #include <lunchbox/log.h>
23 :
24 : #undef glewGetContext
25 : #define glewGetContext() glewContext
26 :
27 : namespace eq
28 : {
29 : namespace util
30 : {
31 : namespace shader
32 : {
33 0 : bool compile(const GLEWContext* glewContext LB_UNUSED, const unsigned shader,
34 : const char* source)
35 : {
36 0 : EQ_GL_CALL(glShaderSource(shader, 1, &source, 0));
37 0 : EQ_GL_CALL(glCompileShader(shader));
38 : GLint status;
39 0 : EQ_GL_CALL(glGetShaderiv(shader, GL_COMPILE_STATUS, &status));
40 0 : if (!status)
41 : {
42 0 : GLchar errorLog[1024] = {0};
43 0 : EQ_GL_CALL(glGetShaderInfoLog(shader, 1024, 0, errorLog));
44 0 : LBWARN << "Failed to compile shader " << shader << ": " << errorLog
45 0 : << std::endl;
46 0 : return false;
47 : }
48 0 : return true;
49 : }
50 :
51 0 : bool linkProgram(const GLEWContext* glewContext LB_UNUSED,
52 : const unsigned program, const char* vertexShaderSource,
53 : const char* fragmentShaderSource)
54 : {
55 0 : if (!program || !vertexShaderSource || !fragmentShaderSource)
56 : {
57 0 : LBWARN << "Failed to link shader program " << program
58 : << ": No valid "
59 0 : "shader program, vertex or fragment source."
60 0 : << std::endl;
61 0 : return false;
62 : }
63 :
64 0 : const GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
65 0 : if (!compile(glewContext, vertexShader, vertexShaderSource))
66 : {
67 0 : EQ_GL_CALL(glDeleteShader(vertexShader));
68 0 : return false;
69 : }
70 :
71 0 : const GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
72 0 : if (!compile(glewContext, fragmentShader, fragmentShaderSource))
73 : {
74 0 : EQ_GL_CALL(glDeleteShader(fragmentShader));
75 0 : return false;
76 : }
77 :
78 0 : EQ_GL_CALL(glAttachShader(program, vertexShader));
79 0 : EQ_GL_CALL(glAttachShader(program, fragmentShader));
80 0 : EQ_GL_CALL(glDeleteShader(vertexShader));
81 0 : EQ_GL_CALL(glDeleteShader(fragmentShader));
82 :
83 0 : EQ_GL_CALL(glLinkProgram(program));
84 : GLint status;
85 0 : EQ_GL_CALL(glGetProgramiv(program, GL_LINK_STATUS, &status));
86 0 : if (!status)
87 : {
88 0 : GLchar errorLog[1024] = {0};
89 0 : EQ_GL_CALL(glGetProgramInfoLog(program, 1024, 0, errorLog));
90 0 : LBWARN << "Failed to link shader program " << program << ": "
91 0 : << errorLog << std::endl;
92 0 : return false;
93 : }
94 0 : return true;
95 : }
96 : }
97 : }
98 30 : }
|