source: trunk/src/gl/gl_program.cc @ 157

Last change on this file since 157 was 121, checked in by epyon, 12 years ago
  • Nova builds with -Weverything/-Wall/-pedantic/etc on: on MSVC 2012 on GCC 4.6.3 on clang 3.2
  • ... without a single fucking warning.
File size: 7.0 KB
Line 
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#include <glm/glm.hpp>
12#include <glm/gtc/type_ptr.hpp>
13
14using namespace nv;
15
16gl_shader::gl_shader( uint32 sh_type )
17        : shader_type( sh_type ), object_id(0)
18{
19        // no op
20}
21
22gl_shader::gl_shader( uint32 sh_type, const string& shader_code )
23        : shader_type( sh_type ), object_id(0)
24{
25        compile( shader_code );
26}
27
28gl_shader::~gl_shader()
29{
30        free();
31}
32
33bool gl_shader::compile( const string& shader_code )
34{
35        generate();
36
37        const char* pc = shader_code.c_str();
38
39        glShaderSource( object_id,   1, &pc, 0 );
40        glCompileShader( object_id );
41
42        return validate();
43}
44
45void gl_shader::generate()
46{
47        if ( is_valid() )
48        {
49                free();
50        }
51        object_id = glCreateShader( shader_type );
52}
53
54void gl_shader::free()
55{
56        glDeleteShader( object_id );
57        object_id = 0;
58}
59
60bool gl_shader::validate()
61{
62        const uint32 buffer_size = 1024;
63        char buffer[ buffer_size ] = { 0 };
64        int length;
65        int compile_ok = GL_FALSE;
66        glGetShaderiv(object_id, GL_COMPILE_STATUS, &compile_ok);
67        glGetShaderInfoLog( object_id, buffer_size, &length, buffer );
68
69        if ( length > 0 )
70        {
71                if ( compile_ok == 0 )
72                {
73                        NV_LOG( LOG_ERROR, "Shader #" << object_id << " error: " << buffer );
74                }
75                else
76                {
77                        NV_LOG( LOG_INFO, "Shader #" << object_id << " compiled successfully: " << buffer );
78                }
79        }
80        else
81        {
82                NV_LOG( LOG_INFO, "Shader #" << object_id << " compiled successfully." );
83        }
84        return compile_ok != 0;
85}
86
87gl_program::gl_program( const string& vertex_program, const string& fragment_program )
88        : vertex_shader( GL_VERTEX_SHADER ), fragment_shader( GL_FRAGMENT_SHADER )
89{
90        compile( vertex_program, fragment_program );
91}
92
93gl_program::~gl_program()
94{
95        if ( is_valid() )
96        {
97                // Detach the shaders from the program
98                glDetachShader( m_name.get_value(), vertex_shader.get_id() );
99                glDetachShader( m_name.get_value(), fragment_shader.get_id() );
100        }
101}
102
103bool gl_program::compile( const string& vertex_program, const string& fragment_program )
104{
105        if (!vertex_shader.compile( vertex_program )) { return false; }
106        if (!fragment_shader.compile( fragment_program )) { return false; }
107
108        glAttachShader( m_name.get_value(), fragment_shader.get_id() );
109        glAttachShader( m_name.get_value(), vertex_shader.get_id() );
110        glLinkProgram( m_name.get_value() );
111
112        if (!validate())
113        {
114                return false;
115        }
116        load_attributes();
117        load_uniforms();
118        return true;
119}
120
121void gl_program::bind()
122{
123        glUseProgram( m_name.get_value() );
124        update_uniforms();
125}
126
127void gl_program::unbind()
128{
129        glUseProgram( 0 );
130}
131
132bool gl_program::is_valid() const
133{
134        return m_name.is_valid();
135}
136
137void gl_program::load_attributes()
138{
139        int params;
140        glGetProgramiv( m_name.get_value(), GL_ACTIVE_ATTRIBUTES, &params );
141
142        for ( unsigned i = 0; i < (unsigned)params; ++i )
143        {
144                int attr_nlen;
145                int attr_len;
146                unsigned attr_type;
147                char name_buffer[128];
148
149                glGetActiveAttrib( m_name.get_value(), i, 128, &attr_nlen, &attr_len, &attr_type, name_buffer );
150
151                string name( name_buffer, size_t(attr_nlen) );
152
153                // skip built-ins
154                if ( name.substr(0,3) == "gl_" ) continue;
155
156                int attr_loc = glGetAttribLocation( m_name.get_value(), name.c_str() );
157
158                m_attribute_map[ name ] = new attribute( name, attr_loc, gl_enum_to_datatype( attr_type ), attr_len );
159        }
160}
161
162void gl_program::load_uniforms()
163{
164        int params;
165        glGetProgramiv( m_name.get_value(), GL_ACTIVE_UNIFORMS, &params );
166
167        for ( unsigned i = 0; i < size_t(params); ++i )
168        {
169                int uni_nlen;
170                int uni_len;
171                unsigned uni_type;
172                char name_buffer[128];
173
174                glGetActiveUniform( m_name.get_value(), i, 128, &uni_nlen, &uni_len, &uni_type, name_buffer );
175
176                string name( name_buffer, size_t(uni_nlen) );
177
178                // skip built-ins
179                if ( name.substr(0,3) == "gl_" ) continue;
180
181                int uni_loc = glGetUniformLocation( m_name.get_value(), name.c_str() );
182                datatype utype = gl_enum_to_datatype( uni_type );
183                m_uniform_map[ name ] = create_uniform( utype, name, uni_loc, uni_len );
184        }
185}
186
187void gl_program::update_uniforms()
188{
189        for ( uniform_map::iterator i = m_uniform_map.begin();  i != m_uniform_map.end(); ++i )
190        {
191                uniform_base* ubase = i->second;
192                if ( ubase->is_dirty() )
193                {
194                        int uloc = ubase->get_location();
195                        switch( ubase->get_type() )
196                        {
197                        case FLOAT          : glUniform1f( uloc, ((uniform< enum_to_type< FLOAT >::type >*)( ubase ))->get_value() ); break;
198                        case INT            : glUniform1i( uloc, ((uniform< enum_to_type< INT >::type >*)( ubase ))->get_value() ); break;
199                        case FLOAT_VECTOR_2 : glUniform2fv( uloc, 1, glm::value_ptr(((uniform< enum_to_type< FLOAT_VECTOR_2 >::type >*)( ubase ))->get_value()) ); break;
200                        case FLOAT_VECTOR_3 : glUniform3fv( uloc, 1, glm::value_ptr(((uniform< enum_to_type< FLOAT_VECTOR_3 >::type >*)( ubase ))->get_value()) ); break;
201                        case FLOAT_VECTOR_4 : glUniform4fv( uloc, 1, glm::value_ptr(((uniform< enum_to_type< FLOAT_VECTOR_4 >::type >*)( ubase ))->get_value()) ); break;
202                        case INT_VECTOR_2   : glUniform2iv( uloc, 1, glm::value_ptr(((uniform< enum_to_type< INT_VECTOR_2 >::type >*)( ubase ))->get_value()) ); break;
203                        case INT_VECTOR_3   : glUniform3iv( uloc, 1, glm::value_ptr(((uniform< enum_to_type< INT_VECTOR_3 >::type >*)( ubase ))->get_value()) ); break;
204                        case INT_VECTOR_4   : glUniform4iv( uloc, 1, glm::value_ptr(((uniform< enum_to_type< INT_VECTOR_4 >::type >*)( ubase ))->get_value()) ); break;
205                        case FLOAT_MATRIX_2 : glUniformMatrix2fv( uloc, 1, GL_FALSE, glm::value_ptr(((uniform< enum_to_type< FLOAT_MATRIX_2 >::type >*)( ubase ))->get_value()) ); break;
206                        case FLOAT_MATRIX_3 : glUniformMatrix3fv( uloc, 1, GL_FALSE, glm::value_ptr(((uniform< enum_to_type< FLOAT_MATRIX_3 >::type >*)( ubase ))->get_value()) ); break;
207                        case FLOAT_MATRIX_4 : glUniformMatrix4fv( uloc, 1, GL_FALSE, glm::value_ptr(((uniform< enum_to_type< FLOAT_MATRIX_4 >::type >*)( ubase ))->get_value()) ); break;
208                        default : break; // error?
209                        }
210                        ubase->clean();
211                }
212        }
213
214}
215
216
217bool gl_program::validate()
218{
219        const uint32 buffer_size = 2048;
220        char buffer[ buffer_size ] = { 0 };
221        int length;
222        int status;
223
224        glGetProgramiv( m_name.get_value(), GL_LINK_STATUS, &status );
225        glGetProgramInfoLog( m_name.get_value(), buffer_size, &length, buffer );
226
227        NV_LOG( LOG_INFO, "Program #" << m_name.get_value() << (status == GL_FALSE ? " failed to compile!" : " compiled successfully.") );
228
229        if ( length > 0 )
230        {
231                NV_LOG( LOG_INFO, "Program #" << m_name.get_value() << " log: " << buffer );
232        }
233
234        if ( status == GL_FALSE )
235        {
236                return false;
237        }
238
239        glValidateProgram( m_name.get_value() );
240        glGetProgramiv( m_name.get_value(), GL_VALIDATE_STATUS, &status );
241
242        if ( status == GL_FALSE )
243        {
244                glGetProgramInfoLog( m_name.get_value(), buffer_size, &length, buffer );
245                NV_LOG( LOG_ERROR, "Program #" << m_name.get_value() << " validation error : " << buffer );
246                return false;
247        }
248        return true;
249}
250
251
Note: See TracBrowser for help on using the repository browser.