]> git.sesse.net Git - casparcg/blob - accelerator/ogl/util/shader.cpp
set svn:eol-style native on .h and .cpp files
[casparcg] / accelerator / ogl / util / shader.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../../stdafx.h"
23
24 #include "shader.h"
25
26 #include <common/gl/gl_check.h>
27
28 #include <gl/glew.h>
29
30 #include <unordered_map>
31
32 namespace caspar { namespace accelerator { namespace ogl {
33
34 struct shader::impl : boost::noncopyable
35 {
36         GLuint program_;
37         std::unordered_map<std::string, GLint> locations_;
38 public:
39
40         impl(const std::string& vertex_source_str, const std::string& fragment_source_str) : program_(0)
41         {
42                 GLint success;
43         
44                 const char* vertex_source = vertex_source_str.c_str();
45                                                 
46                 auto vertex_shader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
47                                         
48                 GL(glShaderSourceARB(vertex_shader, 1, &vertex_source, NULL));
49                 GL(glCompileShaderARB(vertex_shader));
50
51                 GL(glGetObjectParameterivARB(vertex_shader, GL_OBJECT_COMPILE_STATUS_ARB, &success));
52                 if (success == GL_FALSE)
53                 {
54                         char info[2048];
55                         GL(glGetInfoLogARB(vertex_shader, sizeof(info), 0, info));
56                         GL(glDeleteObjectARB(vertex_shader));
57                         std::stringstream str;
58                         str << "Failed to compile vertex shader:" << std::endl << info << std::endl;
59                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(str.str()));
60                 }
61                         
62                 const char* fragment_source = fragment_source_str.c_str();
63                                                 
64                 auto fragmemt_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
65                                         
66                 GL(glShaderSourceARB(fragmemt_shader, 1, &fragment_source, NULL));
67                 GL(glCompileShaderARB(fragmemt_shader));
68
69                 GL(glGetObjectParameterivARB(fragmemt_shader, GL_OBJECT_COMPILE_STATUS_ARB, &success));
70                 if (success == GL_FALSE)
71                 {
72                         char info[2048];
73                         GL(glGetInfoLogARB(fragmemt_shader, sizeof(info), 0, info));
74                         GL(glDeleteObjectARB(fragmemt_shader));
75                         std::stringstream str;
76                         str << "Failed to compile fragment shader:" << std::endl << info << std::endl;
77                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(str.str()));
78                 }
79                         
80                 program_ = glCreateProgramObjectARB();
81                         
82                 GL(glAttachObjectARB(program_, vertex_shader));
83                 GL(glAttachObjectARB(program_, fragmemt_shader));
84
85                 GL(glLinkProgramARB(program_));
86                         
87                 GL(glDeleteObjectARB(vertex_shader));
88                 GL(glDeleteObjectARB(fragmemt_shader));
89
90                 GL(glGetObjectParameterivARB(program_, GL_OBJECT_LINK_STATUS_ARB, &success));
91                 if (success == GL_FALSE)
92                 {
93                         char info[2048];
94                         GL(glGetInfoLogARB(program_, sizeof(info), 0, info));
95                         GL(glDeleteObjectARB(program_));
96                         std::stringstream str;
97                         str << "Failed to link shader program:" << std::endl << info << std::endl;
98                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(str.str()));
99                 }
100                 GL(glUseProgramObjectARB(program_));
101         }
102         
103         ~impl()
104         {
105                 glDeleteProgram(program_);
106         }
107
108         GLint get_location(const char* name)
109         {
110                 auto it = locations_.find(name);
111                 if(it == locations_.end())
112                         it = locations_.insert(std::make_pair(name, glGetUniformLocation(program_, name))).first;
113                 return it->second;
114         }
115         
116         void set(const std::string& name, bool value)
117         {
118                 set(name, value ? 1 : 0);
119         }
120
121         void set(const std::string& name, int value)
122         {
123                 GL(glUniform1i(get_location(name.c_str()), value));
124         }
125         
126         void set(const std::string& name, float value)
127         {
128                 GL(glUniform1f(get_location(name.c_str()), value));
129         }
130         
131         void set(const std::string& name, float value0, float value1)
132         {
133                 GL(glUniform2f(get_location(name.c_str()), value0, value1));
134         }
135
136         void set(const std::string& name, double value)
137         {
138                 GL(glUniform1f(get_location(name.c_str()), static_cast<float>(value)));
139         }
140
141         void use()
142         {               
143                 GL(glUseProgramObjectARB(program_));    
144         }
145 };
146
147 shader::shader(const std::string& vertex_source_str, const std::string& fragment_source_str) : impl_(new impl(vertex_source_str, fragment_source_str)){}
148 shader::~shader(){}
149 void shader::set(const std::string& name, bool value){impl_->set(name, value);}
150 void shader::set(const std::string& name, int value){impl_->set(name, value);}
151 void shader::set(const std::string& name, float value){impl_->set(name, value);}
152 void shader::set(const std::string& name, float value0, float value1){impl_->set(name, value0, value1);}
153 void shader::set(const std::string& name, double value){impl_->set(name, value);}
154 int shader::id() const{return impl_->program_;}
155 void shader::use()const{impl_->use();}
156
157 }}}