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 :
34 0 : bool compile( const GLEWContext* glewContext LB_UNUSED, const unsigned shader,
35 : const char* source )
36 : {
37 0 : EQ_GL_CALL( glShaderSource( shader, 1, &source, 0 ));
38 0 : EQ_GL_CALL( glCompileShader( shader ));
39 : GLint status;
40 0 : EQ_GL_CALL( glGetShaderiv( shader, GL_COMPILE_STATUS, &status ));
41 0 : if( !status )
42 : {
43 0 : GLchar errorLog[1024] = {0};
44 0 : EQ_GL_CALL( glGetShaderInfoLog( shader, 1024, 0, errorLog ));
45 0 : LBWARN << "Failed to compile shader " << shader << ": " << errorLog
46 0 : << std::endl;
47 0 : return false;
48 : }
49 0 : return true;
50 : }
51 :
52 0 : bool linkProgram( const GLEWContext* glewContext LB_UNUSED,
53 : const unsigned program, const char* vertexShaderSource,
54 : const char* fragmentShaderSource )
55 : {
56 0 : if( !program || !vertexShaderSource || !fragmentShaderSource )
57 : {
58 0 : LBWARN << "Failed to link shader program " << program << ": No valid "
59 0 : "shader program, vertex or fragment source." << std::endl;
60 0 : return false;
61 : }
62 :
63 0 : const GLuint vertexShader = glCreateShader( GL_VERTEX_SHADER );
64 0 : if( !compile( glewContext, vertexShader, vertexShaderSource ))
65 : {
66 0 : EQ_GL_CALL( glDeleteShader( vertexShader ));
67 0 : return false;
68 : }
69 :
70 0 : const GLuint fragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
71 0 : if( !compile( glewContext, fragmentShader, fragmentShaderSource ))
72 : {
73 0 : EQ_GL_CALL( glDeleteShader( fragmentShader ));
74 0 : return false;
75 : }
76 :
77 0 : EQ_GL_CALL( glAttachShader( program, vertexShader ));
78 0 : EQ_GL_CALL( glAttachShader( program, fragmentShader ));
79 0 : EQ_GL_CALL( glDeleteShader( vertexShader ));
80 0 : EQ_GL_CALL( glDeleteShader( fragmentShader ));
81 :
82 0 : EQ_GL_CALL( glLinkProgram( program ));
83 : GLint status;
84 0 : EQ_GL_CALL( glGetProgramiv( program, GL_LINK_STATUS, &status ));
85 0 : if( !status )
86 : {
87 0 : GLchar errorLog[1024] = {0};
88 0 : EQ_GL_CALL( glGetProgramInfoLog( program, 1024, 0, errorLog ));
89 0 : LBWARN << "Failed to link shader program " << program << ": "
90 0 : << errorLog << std::endl;
91 0 : return false;
92 : }
93 0 : return true;
94 : }
95 :
96 : }
97 : }
98 42 : }
|