]> git.tdb.fi Git - libs/gl.git/blob - source/core/shader.cpp
Remove some rarely-used export settings
[libs/gl.git] / source / core / shader.cpp
1 #include <msp/gl/extensions/arb_fragment_shader.h>
2 #include <msp/gl/extensions/arb_geometry_shader4.h>
3 #include <msp/gl/extensions/arb_shader_objects.h>
4 #include <msp/gl/extensions/arb_vertex_shader.h>
5 #include <msp/io/print.h>
6 #include "error.h"
7 #include "misc.h"
8 #include "shader.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GL {
14
15 Shader::Shader(GLenum t)
16 {
17         init(t);
18 }
19
20 Shader::Shader(GLenum t, const string &src)
21 {
22         init(t);
23
24         source(src);
25         compile();
26 }
27
28 void Shader::init(GLenum t)
29 {
30         static Require _req_base(ARB_shader_objects);
31         compiled = false;
32
33         if(t==GL_FRAGMENT_SHADER)
34                 static Require _req(ARB_fragment_shader);
35         else if(t==GL_VERTEX_SHADER)
36                 static Require _req(ARB_vertex_shader);
37         else if(t==GL_GEOMETRY_SHADER)
38                 static Require _req(ARB_geometry_shader4);
39
40         id = glCreateShader(t);
41 }
42
43 Shader::~Shader()
44 {
45         if(id)
46                 glDeleteShader(id);
47 }
48
49 void Shader::source(unsigned count, const char **str, const int *len)
50 {
51         glShaderSource(id, count, str, len);
52 }
53
54 void Shader::source(const string &str)
55 {
56         source(str.data(), str.size());
57 }
58
59 void Shader::source(const char *str, int len)
60 {
61         source(1, &str, &len);
62 }
63
64 void Shader::compile()
65 {
66         glCompileShader(id);
67         compiled = get_shader_i(id, GL_COMPILE_STATUS);
68         if(!compiled)
69                 throw compile_error(get_info_log());
70
71 #ifdef DEBUG
72         string info_log = get_info_log();
73         if(!info_log.empty())
74                 IO::print("Shader compile info log:\n%s", info_log);
75 #endif
76 }
77
78 unsigned Shader::steal_id()
79 {
80         unsigned result = id;
81         id = 0;
82         return result;
83 }
84
85 string Shader::get_info_log() const
86 {
87         GLsizei len = get_shader_i(id, GL_INFO_LOG_LENGTH);
88         string log(len+1, 0);
89         glGetShaderInfoLog(id, len+1, &len, &log[0]);
90         log.erase(len);
91         return log;
92 }
93
94
95 VertexShader::VertexShader():
96         Shader(GL_VERTEX_SHADER)
97 { }
98
99 VertexShader::VertexShader(const string &src):
100         Shader(GL_VERTEX_SHADER, src)
101 { }
102
103
104 FragmentShader::FragmentShader():
105         Shader(GL_FRAGMENT_SHADER)
106 { }
107
108 FragmentShader::FragmentShader(const string &src):
109         Shader(GL_FRAGMENT_SHADER, src)
110 { }
111
112
113 GeometryShader::GeometryShader():
114         Shader(GL_GEOMETRY_SHADER)
115 { }
116
117 GeometryShader::GeometryShader(const string &src):
118         Shader(GL_GEOMETRY_SHADER, src)
119 { }
120
121 } // namespace GL
122 } // namespace Msp