1 | // Copyright (C) 2012-2013 Kornel Kisielewicz
|
---|
2 | // This file is part of NV Libraries.
|
---|
3 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
4 |
|
---|
5 | #include "nv/gl/gl_program.hh"
|
---|
6 |
|
---|
7 | #include "nv/gl/gl_enum.hh"
|
---|
8 | #include "nv/logging.hh"
|
---|
9 | #include "nv/lib/gl.hh"
|
---|
10 |
|
---|
11 | using namespace nv;
|
---|
12 |
|
---|
13 | gl_shader::gl_shader( uint32 shader_type )
|
---|
14 | : object_id(0), shader_type( shader_type )
|
---|
15 | {
|
---|
16 | // no op
|
---|
17 | }
|
---|
18 |
|
---|
19 | gl_shader::gl_shader( uint32 shader_type, const string& shader_code )
|
---|
20 | : object_id(0), shader_type( shader_type )
|
---|
21 | {
|
---|
22 | compile( shader_code );
|
---|
23 | }
|
---|
24 |
|
---|
25 | gl_shader::~gl_shader()
|
---|
26 | {
|
---|
27 | free();
|
---|
28 | }
|
---|
29 |
|
---|
30 | bool gl_shader::compile( const string& shader_code )
|
---|
31 | {
|
---|
32 | generate();
|
---|
33 |
|
---|
34 | const char* pc = shader_code.c_str();
|
---|
35 |
|
---|
36 | glShaderSource( object_id, 1, &pc, 0 );
|
---|
37 | glCompileShader( object_id );
|
---|
38 |
|
---|
39 | return validate();
|
---|
40 | }
|
---|
41 |
|
---|
42 | void gl_shader::generate()
|
---|
43 | {
|
---|
44 | if ( is_valid() )
|
---|
45 | {
|
---|
46 | free();
|
---|
47 | }
|
---|
48 | object_id = glCreateShader( shader_type );
|
---|
49 | }
|
---|
50 |
|
---|
51 | void gl_shader::free()
|
---|
52 | {
|
---|
53 | glDeleteShader( object_id );
|
---|
54 | object_id = 0;
|
---|
55 | }
|
---|
56 |
|
---|
57 | bool gl_shader::validate()
|
---|
58 | {
|
---|
59 | const uint32 buffer_size = 1024;
|
---|
60 | char buffer[ buffer_size ] = { 0 };
|
---|
61 | int length;
|
---|
62 | int compile_ok = GL_FALSE;
|
---|
63 | glGetShaderiv(object_id, GL_COMPILE_STATUS, &compile_ok);
|
---|
64 | glGetShaderInfoLog( object_id, buffer_size, &length, buffer );
|
---|
65 |
|
---|
66 | if ( length > 0 )
|
---|
67 | {
|
---|
68 | if ( compile_ok == 0 )
|
---|
69 | {
|
---|
70 | NV_LOG( LOG_ERROR, "Shader #" << object_id << " error: " << buffer );
|
---|
71 | }
|
---|
72 | else
|
---|
73 | {
|
---|
74 | NV_LOG( LOG_INFO, "Shader #" << object_id << " compiled successfully: " << buffer );
|
---|
75 | }
|
---|
76 | }
|
---|
77 | else
|
---|
78 | {
|
---|
79 | NV_LOG( LOG_INFO, "Shader #" << object_id << " compiled successfully." );
|
---|
80 | }
|
---|
81 | return compile_ok != 0;
|
---|
82 | }
|
---|
83 |
|
---|
84 | gl_program::gl_program( const string& vertex_program, const string& fragment_program )
|
---|
85 | : vertex_shader( GL_VERTEX_SHADER ), fragment_shader( GL_FRAGMENT_SHADER )
|
---|
86 | {
|
---|
87 | compile( vertex_program, fragment_program );
|
---|
88 | }
|
---|
89 |
|
---|
90 | gl_program::~gl_program()
|
---|
91 | {
|
---|
92 | if ( is_valid() )
|
---|
93 | {
|
---|
94 | // Detach the shaders from the program
|
---|
95 | glDetachShader( m_name.get_value(), vertex_shader.get_id() );
|
---|
96 | glDetachShader( m_name.get_value(), fragment_shader.get_id() );
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | bool gl_program::compile( const string& vertex_program, const string& fragment_program )
|
---|
101 | {
|
---|
102 | if (!vertex_shader.compile( vertex_program )) { return false; }
|
---|
103 | if (!fragment_shader.compile( fragment_program )) { return false; }
|
---|
104 |
|
---|
105 | glAttachShader( m_name.get_value(), fragment_shader.get_id() );
|
---|
106 | glAttachShader( m_name.get_value(), vertex_shader.get_id() );
|
---|
107 | glLinkProgram( m_name.get_value() );
|
---|
108 |
|
---|
109 | if (!validate())
|
---|
110 | {
|
---|
111 | return false;
|
---|
112 | }
|
---|
113 | load_attributes();
|
---|
114 | load_uniforms();
|
---|
115 | return true;
|
---|
116 | }
|
---|
117 |
|
---|
118 | void gl_program::bind()
|
---|
119 | {
|
---|
120 | glUseProgram( m_name.get_value() );
|
---|
121 | }
|
---|
122 |
|
---|
123 | void gl_program::unbind()
|
---|
124 | {
|
---|
125 | glUseProgram( 0 );
|
---|
126 | }
|
---|
127 |
|
---|
128 | bool gl_program::is_valid() const
|
---|
129 | {
|
---|
130 | return m_name.is_valid();
|
---|
131 | }
|
---|
132 |
|
---|
133 | void gl_program::load_attributes()
|
---|
134 | {
|
---|
135 | int params;
|
---|
136 | glGetProgramiv( m_name.get_value(), GL_ACTIVE_ATTRIBUTES, ¶ms );
|
---|
137 |
|
---|
138 | for ( int i = 0; i < params; ++i )
|
---|
139 | {
|
---|
140 | int attr_nlen;
|
---|
141 | int attr_len;
|
---|
142 | unsigned attr_type;
|
---|
143 | char name_buffer[128];
|
---|
144 |
|
---|
145 | glGetActiveAttrib( m_name.get_value(), i, 128, &attr_nlen, &attr_len, &attr_type, name_buffer );
|
---|
146 |
|
---|
147 | string name( name_buffer, attr_nlen );
|
---|
148 |
|
---|
149 | // skip built-ins
|
---|
150 | if ( name.substr(0,3) == "gl_" ) continue;
|
---|
151 |
|
---|
152 | int attr_loc = glGetAttribLocation( m_name.get_value(), name.c_str() );
|
---|
153 |
|
---|
154 | m_attribute_map[ name ] = new attribute( name, attr_loc, gl_enum_to_type( attr_type ), attr_len );
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | void gl_program::load_uniforms()
|
---|
159 | {
|
---|
160 | int params;
|
---|
161 | glGetProgramiv( m_name.get_value(), GL_ACTIVE_UNIFORMS, ¶ms );
|
---|
162 |
|
---|
163 | for ( int i = 0; i < params; ++i )
|
---|
164 | {
|
---|
165 | int uni_nlen;
|
---|
166 | int uni_len;
|
---|
167 | unsigned uni_type;
|
---|
168 | char name_buffer[128];
|
---|
169 |
|
---|
170 | glGetActiveUniform( m_name.get_value(), i, 128, &uni_nlen, &uni_len, &uni_type, name_buffer );
|
---|
171 |
|
---|
172 | string name( name_buffer, uni_nlen );
|
---|
173 |
|
---|
174 | // skip built-ins
|
---|
175 | if ( name.substr(0,3) == "gl_" ) continue;
|
---|
176 |
|
---|
177 | int uni_loc = glGetUniformLocation( m_name.get_value(), name.c_str() );
|
---|
178 | type utype = gl_enum_to_type( uni_type );
|
---|
179 | m_uniform_map[ name ] = create_uniform( utype, name, uni_loc, uni_len );
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | bool gl_program::validate()
|
---|
184 | {
|
---|
185 | const uint32 buffer_size = 2048;
|
---|
186 | char buffer[ buffer_size ] = { 0 };
|
---|
187 | int length;
|
---|
188 | int status;
|
---|
189 |
|
---|
190 | glGetProgramiv( m_name.get_value(), GL_LINK_STATUS, &status );
|
---|
191 | glGetProgramInfoLog( m_name.get_value(), buffer_size, &length, buffer );
|
---|
192 |
|
---|
193 | NV_LOG( LOG_INFO, "Program #" << m_name.get_value() << (status == GL_FALSE ? " failed to compile!" : " compiled successfully.") );
|
---|
194 |
|
---|
195 | if ( length > 0 )
|
---|
196 | {
|
---|
197 | NV_LOG( LOG_INFO, "Program #" << m_name.get_value() << " log: " << buffer );
|
---|
198 | }
|
---|
199 |
|
---|
200 | if ( status == GL_FALSE )
|
---|
201 | {
|
---|
202 | return false;
|
---|
203 | }
|
---|
204 |
|
---|
205 | glValidateProgram( m_name.get_value() );
|
---|
206 | glGetProgramiv( m_name.get_value(), GL_VALIDATE_STATUS, &status );
|
---|
207 |
|
---|
208 | if ( status == GL_FALSE )
|
---|
209 | {
|
---|
210 | glGetProgramInfoLog( m_name.get_value(), buffer_size, &length, buffer );
|
---|
211 | NV_LOG( LOG_ERROR, "Program #" << m_name.get_value() << " validation error : " << buffer );
|
---|
212 | return false;
|
---|
213 | }
|
---|
214 | return true;
|
---|
215 | }
|
---|
216 |
|
---|
217 |
|
---|