2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
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.
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.
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/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../../stdafx.h"
26 #include <common/gl/gl_check.h>
30 #include <unordered_map>
32 namespace caspar { namespace accelerator { namespace ogl {
34 struct shader::impl : boost::noncopyable
37 std::unordered_map<std::string, GLint> locations_;
40 impl(const std::string& vertex_source_str, const std::string& fragment_source_str) : program_(0)
44 const char* vertex_source = vertex_source_str.c_str();
46 auto vertex_shader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
48 GL(glShaderSourceARB(vertex_shader, 1, &vertex_source, NULL));
49 GL(glCompileShaderARB(vertex_shader));
51 GL(glGetObjectParameterivARB(vertex_shader, GL_OBJECT_COMPILE_STATUS_ARB, &success));
52 if (success == GL_FALSE)
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()));
62 const char* fragment_source = fragment_source_str.c_str();
64 auto fragmemt_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
66 GL(glShaderSourceARB(fragmemt_shader, 1, &fragment_source, NULL));
67 GL(glCompileShaderARB(fragmemt_shader));
69 GL(glGetObjectParameterivARB(fragmemt_shader, GL_OBJECT_COMPILE_STATUS_ARB, &success));
70 if (success == GL_FALSE)
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()));
80 program_ = glCreateProgramObjectARB();
82 GL(glAttachObjectARB(program_, vertex_shader));
83 GL(glAttachObjectARB(program_, fragmemt_shader));
85 GL(glLinkProgramARB(program_));
87 GL(glDeleteObjectARB(vertex_shader));
88 GL(glDeleteObjectARB(fragmemt_shader));
90 GL(glGetObjectParameterivARB(program_, GL_OBJECT_LINK_STATUS_ARB, &success));
91 if (success == GL_FALSE)
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()));
100 GL(glUseProgramObjectARB(program_));
105 glDeleteProgram(program_);
108 GLint get_location(const char* name)
110 auto it = locations_.find(name);
111 if(it == locations_.end())
112 it = locations_.insert(std::make_pair(name, glGetUniformLocation(program_, name))).first;
116 void set(const std::string& name, bool value)
118 set(name, value ? 1 : 0);
121 void set(const std::string& name, int value)
123 GL(glUniform1i(get_location(name.c_str()), value));
126 void set(const std::string& name, float value)
128 GL(glUniform1f(get_location(name.c_str()), value));
131 void set(const std::string& name, float value0, float value1)
133 GL(glUniform2f(get_location(name.c_str()), value0, value1));
136 void set(const std::string& name, double value)
138 GL(glUniform1f(get_location(name.c_str()), static_cast<float>(value)));
143 GL(glUseProgramObjectARB(program_));
147 shader::shader(const std::string& vertex_source_str, const std::string& fragment_source_str) : impl_(new impl(vertex_source_str, fragment_source_str)){}
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();}