]> git.sesse.net Git - casparcg/blobdiff - accelerator/ogl/util/shader.cpp
set svn:eol-style native on .h and .cpp files
[casparcg] / accelerator / ogl / util / shader.cpp
index d64d9abe23a0b3b3056a045ffe522d7f609a9c5c..3d211f78623a8b1cb4b595c227e181bebd84208b 100644 (file)
-/*\r
-* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
-*\r
-* This file is part of CasparCG (www.casparcg.com).\r
-*\r
-* CasparCG is free software: you can redistribute it and/or modify\r
-* it under the terms of the GNU General Public License as published by\r
-* the Free Software Foundation, either version 3 of the License, or\r
-* (at your option) any later version.\r
-*\r
-* CasparCG is distributed in the hope that it will be useful,\r
-* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-* GNU General Public License for more details.\r
-*\r
-* You should have received a copy of the GNU General Public License\r
-* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
-*\r
-* Author: Robert Nagy, ronag89@gmail.com\r
-*/\r
-\r
-#include "../../stdafx.h"\r
-\r
-#include "shader.h"\r
-\r
-#include <common/gl/gl_check.h>\r
-\r
-#include <gl/glew.h>\r
-\r
-#include <unordered_map>\r
-\r
-namespace caspar { namespace accelerator { namespace ogl {\r
-\r
-struct shader::impl : boost::noncopyable\r
-{\r
-       GLuint program_;\r
-       std::unordered_map<std::string, GLint> locations_;\r
-public:\r
-\r
-       impl(const std::string& vertex_source_str, const std::string& fragment_source_str) : program_(0)\r
-       {\r
-               GLint success;\r
-       \r
-               const char* vertex_source = vertex_source_str.c_str();\r
-                                               \r
-               auto vertex_shader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);\r
-                                       \r
-               GL(glShaderSourceARB(vertex_shader, 1, &vertex_source, NULL));\r
-               GL(glCompileShaderARB(vertex_shader));\r
-\r
-               GL(glGetObjectParameterivARB(vertex_shader, GL_OBJECT_COMPILE_STATUS_ARB, &success));\r
-               if (success == GL_FALSE)\r
-               {\r
-                       char info[2048];\r
-                       GL(glGetInfoLogARB(vertex_shader, sizeof(info), 0, info));\r
-                       GL(glDeleteObjectARB(vertex_shader));\r
-                       std::stringstream str;\r
-                       str << "Failed to compile vertex shader:" << std::endl << info << std::endl;\r
-                       CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(str.str()));\r
-               }\r
-                       \r
-               const char* fragment_source = fragment_source_str.c_str();\r
-                                               \r
-               auto fragmemt_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);\r
-                                       \r
-               GL(glShaderSourceARB(fragmemt_shader, 1, &fragment_source, NULL));\r
-               GL(glCompileShaderARB(fragmemt_shader));\r
-\r
-               GL(glGetObjectParameterivARB(fragmemt_shader, GL_OBJECT_COMPILE_STATUS_ARB, &success));\r
-               if (success == GL_FALSE)\r
-               {\r
-                       char info[2048];\r
-                       GL(glGetInfoLogARB(fragmemt_shader, sizeof(info), 0, info));\r
-                       GL(glDeleteObjectARB(fragmemt_shader));\r
-                       std::stringstream str;\r
-                       str << "Failed to compile fragment shader:" << std::endl << info << std::endl;\r
-                       CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(str.str()));\r
-               }\r
-                       \r
-               program_ = glCreateProgramObjectARB();\r
-                       \r
-               GL(glAttachObjectARB(program_, vertex_shader));\r
-               GL(glAttachObjectARB(program_, fragmemt_shader));\r
-\r
-               GL(glLinkProgramARB(program_));\r
-                       \r
-               GL(glDeleteObjectARB(vertex_shader));\r
-               GL(glDeleteObjectARB(fragmemt_shader));\r
-\r
-               GL(glGetObjectParameterivARB(program_, GL_OBJECT_LINK_STATUS_ARB, &success));\r
-               if (success == GL_FALSE)\r
-               {\r
-                       char info[2048];\r
-                       GL(glGetInfoLogARB(program_, sizeof(info), 0, info));\r
-                       GL(glDeleteObjectARB(program_));\r
-                       std::stringstream str;\r
-                       str << "Failed to link shader program:" << std::endl << info << std::endl;\r
-                       CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(str.str()));\r
-               }\r
-               GL(glUseProgramObjectARB(program_));\r
-       }\r
-       \r
-       ~impl()\r
-       {\r
-               glDeleteProgram(program_);\r
-       }\r
-\r
-       GLint get_location(const char* name)\r
-       {\r
-               auto it = locations_.find(name);\r
-               if(it == locations_.end())\r
-                       it = locations_.insert(std::make_pair(name, glGetUniformLocation(program_, name))).first;\r
-               return it->second;\r
-       }\r
-       \r
-       void set(const std::string& name, bool value)\r
-       {\r
-               set(name, value ? 1 : 0);\r
-       }\r
-\r
-       void set(const std::string& name, int value)\r
-       {\r
-               GL(glUniform1i(get_location(name.c_str()), value));\r
-       }\r
-       \r
-       void set(const std::string& name, float value)\r
-       {\r
-               GL(glUniform1f(get_location(name.c_str()), value));\r
-       }\r
-       \r
-       void set(const std::string& name, float value0, float value1)\r
-       {\r
-               GL(glUniform2f(get_location(name.c_str()), value0, value1));\r
-       }\r
-\r
-       void set(const std::string& name, double value)\r
-       {\r
-               GL(glUniform1f(get_location(name.c_str()), static_cast<float>(value)));\r
-       }\r
-\r
-       void use()\r
-       {               \r
-               GL(glUseProgramObjectARB(program_));    \r
-       }\r
-};\r
-\r
-shader::shader(const std::string& vertex_source_str, const std::string& fragment_source_str) : impl_(new impl(vertex_source_str, fragment_source_str)){}\r
-shader::~shader(){}\r
-void shader::set(const std::string& name, bool value){impl_->set(name, value);}\r
-void shader::set(const std::string& name, int value){impl_->set(name, value);}\r
-void shader::set(const std::string& name, float value){impl_->set(name, value);}\r
-void shader::set(const std::string& name, float value0, float value1){impl_->set(name, value0, value1);}\r
-void shader::set(const std::string& name, double value){impl_->set(name, value);}\r
-int shader::id() const{return impl_->program_;}\r
-void shader::use()const{impl_->use();}\r
-\r
+/*
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
+*
+* This file is part of CasparCG (www.casparcg.com).
+*
+* CasparCG is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* CasparCG is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
+*
+* Author: Robert Nagy, ronag89@gmail.com
+*/
+
+#include "../../stdafx.h"
+
+#include "shader.h"
+
+#include <common/gl/gl_check.h>
+
+#include <gl/glew.h>
+
+#include <unordered_map>
+
+namespace caspar { namespace accelerator { namespace ogl {
+
+struct shader::impl : boost::noncopyable
+{
+       GLuint program_;
+       std::unordered_map<std::string, GLint> locations_;
+public:
+
+       impl(const std::string& vertex_source_str, const std::string& fragment_source_str) : program_(0)
+       {
+               GLint success;
+       
+               const char* vertex_source = vertex_source_str.c_str();
+                                               
+               auto vertex_shader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
+                                       
+               GL(glShaderSourceARB(vertex_shader, 1, &vertex_source, NULL));
+               GL(glCompileShaderARB(vertex_shader));
+
+               GL(glGetObjectParameterivARB(vertex_shader, GL_OBJECT_COMPILE_STATUS_ARB, &success));
+               if (success == GL_FALSE)
+               {
+                       char info[2048];
+                       GL(glGetInfoLogARB(vertex_shader, sizeof(info), 0, info));
+                       GL(glDeleteObjectARB(vertex_shader));
+                       std::stringstream str;
+                       str << "Failed to compile vertex shader:" << std::endl << info << std::endl;
+                       CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(str.str()));
+               }
+                       
+               const char* fragment_source = fragment_source_str.c_str();
+                                               
+               auto fragmemt_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
+                                       
+               GL(glShaderSourceARB(fragmemt_shader, 1, &fragment_source, NULL));
+               GL(glCompileShaderARB(fragmemt_shader));
+
+               GL(glGetObjectParameterivARB(fragmemt_shader, GL_OBJECT_COMPILE_STATUS_ARB, &success));
+               if (success == GL_FALSE)
+               {
+                       char info[2048];
+                       GL(glGetInfoLogARB(fragmemt_shader, sizeof(info), 0, info));
+                       GL(glDeleteObjectARB(fragmemt_shader));
+                       std::stringstream str;
+                       str << "Failed to compile fragment shader:" << std::endl << info << std::endl;
+                       CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(str.str()));
+               }
+                       
+               program_ = glCreateProgramObjectARB();
+                       
+               GL(glAttachObjectARB(program_, vertex_shader));
+               GL(glAttachObjectARB(program_, fragmemt_shader));
+
+               GL(glLinkProgramARB(program_));
+                       
+               GL(glDeleteObjectARB(vertex_shader));
+               GL(glDeleteObjectARB(fragmemt_shader));
+
+               GL(glGetObjectParameterivARB(program_, GL_OBJECT_LINK_STATUS_ARB, &success));
+               if (success == GL_FALSE)
+               {
+                       char info[2048];
+                       GL(glGetInfoLogARB(program_, sizeof(info), 0, info));
+                       GL(glDeleteObjectARB(program_));
+                       std::stringstream str;
+                       str << "Failed to link shader program:" << std::endl << info << std::endl;
+                       CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(str.str()));
+               }
+               GL(glUseProgramObjectARB(program_));
+       }
+       
+       ~impl()
+       {
+               glDeleteProgram(program_);
+       }
+
+       GLint get_location(const char* name)
+       {
+               auto it = locations_.find(name);
+               if(it == locations_.end())
+                       it = locations_.insert(std::make_pair(name, glGetUniformLocation(program_, name))).first;
+               return it->second;
+       }
+       
+       void set(const std::string& name, bool value)
+       {
+               set(name, value ? 1 : 0);
+       }
+
+       void set(const std::string& name, int value)
+       {
+               GL(glUniform1i(get_location(name.c_str()), value));
+       }
+       
+       void set(const std::string& name, float value)
+       {
+               GL(glUniform1f(get_location(name.c_str()), value));
+       }
+       
+       void set(const std::string& name, float value0, float value1)
+       {
+               GL(glUniform2f(get_location(name.c_str()), value0, value1));
+       }
+
+       void set(const std::string& name, double value)
+       {
+               GL(glUniform1f(get_location(name.c_str()), static_cast<float>(value)));
+       }
+
+       void use()
+       {               
+               GL(glUseProgramObjectARB(program_));    
+       }
+};
+
+shader::shader(const std::string& vertex_source_str, const std::string& fragment_source_str) : impl_(new impl(vertex_source_str, fragment_source_str)){}
+shader::~shader(){}
+void shader::set(const std::string& name, bool value){impl_->set(name, value);}
+void shader::set(const std::string& name, int value){impl_->set(name, value);}
+void shader::set(const std::string& name, float value){impl_->set(name, value);}
+void shader::set(const std::string& name, float value0, float value1){impl_->set(name, value0, value1);}
+void shader::set(const std::string& name, double value){impl_->set(name, value);}
+int shader::id() const{return impl_->program_;}
+void shader::use()const{impl_->use();}
+
 }}}
\ No newline at end of file