-#include <msp/gl/extensions/arb_fragment_shader.h>
-#include <msp/gl/extensions/arb_geometry_shader4.h>
-#include <msp/gl/extensions/arb_shader_objects.h>
-#include <msp/gl/extensions/arb_vertex_shader.h>
-#include <msp/io/print.h>
-#include "error.h"
-#include "misc.h"
-#include "shader.h"
-
-using namespace std;
-
-namespace Msp {
-namespace GL {
-
-Shader::Shader(GLenum t)
-{
- init(t);
-}
-
-Shader::Shader(GLenum t, const string &src)
-{
- init(t);
-
- source(src);
- compile();
-}
-
-void Shader::init(GLenum t)
-{
- static Require _req_base(ARB_shader_objects);
- compiled = false;
-
- if(t==GL_FRAGMENT_SHADER)
- static Require _req(ARB_fragment_shader);
- else if(t==GL_VERTEX_SHADER)
- static Require _req(ARB_vertex_shader);
- else if(t==GL_GEOMETRY_SHADER)
- static Require _req(ARB_geometry_shader4);
-
- id = glCreateShader(t);
-}
-
-Shader::~Shader()
-{
- if(id)
- glDeleteShader(id);
-}
-
-void Shader::source(unsigned count, const char **str, const int *len)
-{
- glShaderSource(id, count, str, len);
-}
-
-void Shader::source(const string &str)
-{
- source(str.data(), str.size());
-}
-
-void Shader::source(const char *str, int len)
-{
- source(1, &str, &len);
-}
-
-void Shader::compile()
-{
- glCompileShader(id);
- compiled = get_shader_i(id, GL_COMPILE_STATUS);
- if(!compiled)
- throw compile_error(get_info_log());
-
-#ifdef DEBUG
- string info_log = get_info_log();
- if(!info_log.empty())
- IO::print("Shader compile info log:\n%s", info_log);
-#endif
-}
-
-unsigned Shader::steal_id()
-{
- unsigned result = id;
- id = 0;
- return result;
-}
-
-string Shader::get_info_log() const
-{
- GLsizei len = get_shader_i(id, GL_INFO_LOG_LENGTH);
- string log(len+1, 0);
- glGetShaderInfoLog(id, len+1, &len, &log[0]);
- log.erase(len);
- return log;
-}
-
-
-VertexShader::VertexShader():
- Shader(GL_VERTEX_SHADER)
-{ }
-
-VertexShader::VertexShader(const string &src):
- Shader(GL_VERTEX_SHADER, src)
-{ }
-
-
-FragmentShader::FragmentShader():
- Shader(GL_FRAGMENT_SHADER)
-{ }
-
-FragmentShader::FragmentShader(const string &src):
- Shader(GL_FRAGMENT_SHADER, src)
-{ }
-
-
-GeometryShader::GeometryShader():
- Shader(GL_GEOMETRY_SHADER)
-{ }
-
-GeometryShader::GeometryShader(const string &src):
- Shader(GL_GEOMETRY_SHADER, src)
-{ }
-
-} // namespace GL
-} // namespace Msp