source: trunk/src/gl/gl_device.cc @ 535

Last change on this file since 535 was 535, checked in by epyon, 8 years ago
  • unified pixel_format instead of image_format
File size: 14.9 KB
RevLine 
[534]1// Copyright (C) 2012-2017 ChaosForge Ltd
[395]2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
[38]6
7#include "nv/gl/gl_device.hh"
8
9#include "nv/gl/gl_window.hh"
[319]10#include "nv/core/logging.hh"
[90]11#include "nv/lib/sdl_image.hh"
[301]12#include "nv/gl/gl_enum.hh"
13#include "nv/lib/gl.hh"
[38]14
15using namespace nv;
16
17gl_device::gl_device()
18{
[491]19        m_shader_header.append( "#version 330 core\n" );
[316]20        for ( auto& i : get_uniform_factory() )
[438]21                m_shader_header.append( "uniform "+datatype_to_glsl_type( i.second->get_datatype() )+" "+ i.first +";\n" );
[316]22        for ( auto& i : get_link_uniform_factory() )
[438]23                m_shader_header.append( "uniform sampler2D "+i.first +";\n" );
[38]24}
25
[90]26// this is a temporary function that will be removed once we find a way to
27// pass binary file data around
[399]28image_data* gl_device::create_image_data( string_view filename )
[90]29{
30        load_sdl_image_library();
[361]31        SDL_Surface* image = IMG_Load( filename.data() );
[120]32        if (!image)
33        {
[365]34                NV_LOG_ERROR( "Image file ", filename, " not found!" );
[120]35                return nullptr;
36        }
[292]37        // TODO: BGR vs RGB, single channel
[535]38        pixel_format pformat = RGBA8;
[461]39        switch ( image->format->BytesPerPixel )
40        {
[535]41        case 4: pformat = RGBA8; break;
42        case 3: pformat = RGB8; break;
43        case 1: pformat = R8; break;
[461]44        default: NV_ASSERT( false, "BytesPerPixel != 4,3 or 1!" );
45        }
[535]46        image_data* data = new image_data( pformat, ivec2( image->w, image->h ), static_cast<nv::uint8*>( image->pixels ) );
[90]47        return data;
48}
49
[350]50// this is a temporary function that will be removed once we find a way to
51// pass binary file data around
52image_data* gl_device::create_image_data( const uint8* data, uint32 size )
53{
54        load_sdl_image_library();
[406]55        SDL_Surface* image = IMG_LoadTyped_RW( SDL_RWFromMem( const_cast<uint8*>( data ), int( size ) ), 1, "png" );
[350]56        if ( !image )
57        {
[365]58                NV_LOG_ERROR( "Image binary data cannot be loaded found!" );
[350]59                return nullptr;
60        }
61        // TODO: BGR vs RGB, single channel
[471]62        NV_ASSERT( image->format->BytesPerPixel > 2, "bytes per pixel > 2!" );
[535]63        pixel_format format( image->format->BytesPerPixel == 3 ? RGB8 : RGBA8 );
[406]64        image_data* idata = new image_data( format, ivec2( image->w, image->h ), static_cast<nv::uint8*>( image->pixels ) );
[350]65        return idata;
66}
[92]67
[350]68
[38]69gl_device::~gl_device()
70{
[302]71        while ( m_textures.size() > 0 )
72                release( m_textures.get_handle(0) );
73        while ( m_buffers.size() > 0 )
74                release( m_buffers.get_handle(0) );
[303]75        while ( m_programs.size() > 0 )
76                release( m_programs.get_handle(0) );
[506]77        while ( m_shaders.size() > 0 )
78                release( m_shaders.get_handle( 0 ) );
[38]79}
[301]80
[506]81nv::shader nv::gl_device::create_shader( shader_type type, string_view sh_source )
82{
83        uint32 glid = 0;
84        if ( !compile( shader_type_to_enum( type ), sh_source, glid ) )
85                return shader();
86
87        shader result = m_shaders.create();
88        gl_shader_info* info = m_shaders.get( result );
89        info->type = type;
90        info->glid = glid;
91        info->ref  = 0;
92
93        return result;
94}
95
96nv::program nv::gl_device::create_program( shader vs, shader fs )
97{
98        program result = m_programs.create();
99        gl_program_info* info = m_programs.get( result );
100
101        info->m_attribute_map = new attribute_map;
102        info->m_engine_uniforms = new engine_uniform_list;
103        info->m_uniform_map = new uniform_map;
104
105        info->glid = glCreateProgram();
106        info->validated = false;
107
108        compile( result, vs, fs );
109        prepare_program( result );
110        return result;
111
112}
113
[491]114nv::texture nv::gl_device::create_texture( texture_type type, pixel_format format )
115{
116        unsigned glid = 0;
117
118        glGenTextures( 1, &glid );
119
120        texture result = m_textures.create();
121        gl_texture_info* info = m_textures.get( result );
122        info->type = type;
123        info->format = format;
124        info->tsampler = sampler();
125        info->size = ivec3( 1, 1, 1 );
126        info->glid = glid;
127
128        return result;
129}
130
[302]131void nv::gl_device::release( texture t )
[301]132{
133        gl_texture_info* info = m_textures.get( t );
134        if ( info )
135        {
[302]136                if ( info->glid != 0 )
137                {
138                        glDeleteTextures( 1, &(info->glid) );
139                }
[301]140                m_textures.destroy( t );
141        }
142}
143
[302]144void nv::gl_device::release( buffer b )
145{
146        gl_buffer_info* info = m_buffers.get( b );
147        if ( info )
148        {
149                if ( info->glid != 0 )
150                {
151                        glDeleteBuffers( 1, &(info->glid) );
152                }
153                m_buffers.destroy( b );
154        }
155}
156
[303]157const texture_info* nv::gl_device::get_texture_info( texture t ) const
[301]158{
159        return m_textures.get( t );
160}
161
[501]162nv::buffer nv::gl_device::create_buffer( buffer_type type, buffer_hint hint )
[302]163{
[491]164        unsigned glid = 0;
[302]165        glGenBuffers( 1, &glid );
166
167        buffer result = m_buffers.create();
168        gl_buffer_info* info = m_buffers.get( result );
169        info->type = type;
170        info->hint = hint;
[501]171        info->size = 0;
[302]172        info->glid = glid;
173        return result;
174}
175
[506]176void nv::gl_device::attach( program p, shader s )
177{
178        gl_program_info* pinfo = m_programs.get( p );
179        gl_shader_info*  sinfo = m_shaders.get( s );
180
181        if ( sinfo && pinfo )
182        {
183                glAttachShader( pinfo->glid, sinfo->glid );
184                sinfo->ref++;
185        }
186
187}
188
189void nv::gl_device::detach( program p, shader s )
190{
191        gl_program_info* pinfo = m_programs.get( p );
192        gl_shader_info*  sinfo = m_shaders.get( s );
193        if ( sinfo && pinfo )
194        {
195                glDetachShader( pinfo->glid, sinfo->glid );
196                sinfo->ref--;
197        }
198}
[303]199const buffer_info* nv::gl_device::get_buffer_info( buffer t ) const
[302]200{
201        return m_buffers.get( t );
202}
[303]203
204void nv::gl_device::release( program p )
205{
206        gl_program_info* info = m_programs.get( p );
207        if ( info )
208        {
[392]209                for ( auto& i : *info->m_uniform_map )
[303]210                        delete i.second;
211
[506]212                detach( p, info->vertex );
213                detach( p, info->fragment );
[303]214                glDeleteProgram( info->glid );
215
[392]216                delete info->m_attribute_map;
217                delete info->m_engine_uniforms;
218                delete info->m_uniform_map;
219
[303]220                m_programs.destroy( p );
221        }
222}
223
[506]224void nv::gl_device::release( shader s )
225{
226        gl_shader_info* info = m_shaders.get( s );
227        if ( info && info->ref == 0 )
228        {
229                glDeleteShader( info->glid );
230                m_shaders.destroy( s );
231        }
232}
233
234
[501]235nv::gl_texture_info* nv::gl_device::get_full_texture_info( texture t )
236{
237        return m_textures.get( t );
238
239}
240
241nv::gl_buffer_info* nv::gl_device::get_full_buffer_info( buffer t )
242{
243        return m_buffers.get( t );
244}
245
[303]246void nv::gl_device::prepare_program( program p )
247{
248        gl_program_info* info = m_programs.get( p );
249        if ( info )
250        {
251                auto& map  = get_uniform_factory();
252                auto& lmap = get_link_uniform_factory();
253
[392]254                for ( auto& i : *info->m_uniform_map )
[303]255                {
[439]256                        auto j = lmap.find( i.first );
[303]257                        if ( j != lmap.end() )
258                        {
259                                j->second->set( i.second );
260                        }                       
261
[439]262                        auto k = map.find( i.first );
[303]263                        if ( k != map.end() )
264                        {
[392]265                                info->m_engine_uniforms->push_back( k->second->create( i.second ) );
[303]266                        }                               
267                }
268        }
269}
270
[439]271uniform_base* nv::gl_device::get_uniform( program p, const string_view& name, bool fatal /*= true */ ) const
[303]272{
273        const gl_program_info* info = m_programs.get( p );
274        {
[392]275                nv::uniform_map::const_iterator i = info->m_uniform_map->find( name );
276                if ( i != info->m_uniform_map->end() )
[303]277                {
278                        return i->second;
279                }
280                if ( fatal )
281                {
[439]282                        NV_LOG_CRITICAL( "gl_device : uniform '", name, "' not found in program!" );
[403]283                        NV_ABORT( "gl_device : uniform not found!" );
[303]284                }
285        }
286        return nullptr;
287}
288
[439]289int nv::gl_device::get_attribute_location( program p, const string_view& name, bool fatal /*= true */ ) const
[303]290{
291        const gl_program_info* info = m_programs.get( p );
292        if ( info )
293        {
[392]294                attribute_map::const_iterator i = info->m_attribute_map->find( name );
295                if ( i != info->m_attribute_map->end() )
[303]296                {
297                        return i->second.location;
298                }
299                if ( fatal )
300                {
[439]301                        NV_LOG_CRITICAL( "gl_device : attribute '", name, "' not found in program!" );
[403]302                        NV_ABORT( "gl_device : attribute not found!" );
[303]303                }
304        }
305        return -1;
306}
307
[485]308bool nv::gl_device::bind_block( program p, const string_view& name, uint32 index )
309{
310        const gl_program_info* info = m_programs.get( p );
311        if ( info )
312        {
313                int id = get_block_location( p, name, false );
314                if ( id < 0 ) return false;
315                glUniformBlockBinding( info->glid, unsigned( id ), index );
316                return true;
317        }
318        return false;
319}
320
[473]321int nv::gl_device::get_block_location( program p, const string_view& name, bool fatal /*= true */ ) const
322{
[485]323        const gl_program_info* info = m_programs.get( p );
324        if ( info )
325        {
[487]326                GLuint result = glGetUniformBlockIndex( info->glid, name.data() );
327                if ( result != GL_INVALID_INDEX ) return static_cast<int>( result );
[485]328                if ( fatal )
329                {
330                        NV_LOG_CRITICAL( "gl_device : block '", name, "' not found in program!" );
331                        NV_ABORT( "gl_device : block not found!" );
332                }
333        }
[473]334        return -1;
335}
336
[506]337bool nv::gl_device::compile( program pp, shader vp, shader fp )
[303]338{
[506]339        gl_program_info* p = m_programs.get( pp );
340        p->vertex = vp;
341        p->fragment = fp;
[303]342
343        glBindAttribLocation( p->glid, static_cast<GLuint>( slot::POSITION   ), "nv_position"  );
344        glBindAttribLocation( p->glid, static_cast<GLuint>( slot::TEXCOORD   ), "nv_texcoord"  );
345        glBindAttribLocation( p->glid, static_cast<GLuint>( slot::NORMAL     ), "nv_normal"    );
346        glBindAttribLocation( p->glid, static_cast<GLuint>( slot::COLOR      ), "nv_color"     );
347        glBindAttribLocation( p->glid, static_cast<GLuint>( slot::TANGENT    ), "nv_tangent"   );
348        glBindAttribLocation( p->glid, static_cast<GLuint>( slot::BONEINDEX  ), "nv_boneindex" );
349        glBindAttribLocation( p->glid, static_cast<GLuint>( slot::BONEWEIGHT ), "nv_boneweight");
350
[506]351        attach( pp, p->vertex );
352        attach( pp, p->fragment );
[303]353        glLinkProgram( p->glid );
354
355        const uint32 buffer_size = 2048;
356        char buffer[ buffer_size ] = { 0 };
357        int length;
358        int status;
359
360        glGetProgramiv( p->glid, GL_LINK_STATUS, &status );
361        glGetProgramInfoLog( p->glid, buffer_size, &length, buffer );
362
[365]363        NV_LOG_INFO( "Program #", p->glid, (status == GL_FALSE ? " failed to compile!" : " compiled successfully.") );
[303]364
365        if ( length > 0 )
366        {
[466]367                NV_LOG_INFO( "Program #", p->glid, " log: ", string_view( buffer, size_t( length ) ) );
[303]368        }
369
370        if ( status == GL_FALSE )
371        {
372                return false;
373        }
374
375        load_attributes( p );
376        load_uniforms( p );
377        return true;
378}
379
380void nv::gl_device::update_uniforms( gl_program_info* p )
381{
[392]382        for ( auto& i : *p->m_uniform_map )
[303]383        {
[392]384                uniform_base* ubase = i.second;
[303]385                if ( ubase->is_dirty() )
386                {
[487]387                        GLint   uloc = ubase->get_location();
388                        GLsizei size = static_cast<GLsizei>( ubase->get_length() );
[303]389                        switch( ubase->get_type() )
390                        {
[487]391                        case FLOAT          : glUniform1fv( uloc, size, static_cast< uniform< enum_to_type< FLOAT >::type >*>( ubase )->get_value() ); break;
392                        case INT            : glUniform1iv( uloc, size, static_cast< uniform< enum_to_type< INT >::type >*>( ubase )->get_value() ); break;
393                        case FLOAT_VECTOR_2 : glUniform2fv( uloc, size, reinterpret_cast<const GLfloat*>( static_cast< uniform< enum_to_type< FLOAT_VECTOR_2 >::type >*>( ubase )->get_value())); break;
394                        case FLOAT_VECTOR_3 : glUniform3fv( uloc, size, reinterpret_cast<const GLfloat*>( static_cast< uniform< enum_to_type< FLOAT_VECTOR_3 >::type >*>( ubase )->get_value())); break;
395                        case FLOAT_VECTOR_4 : glUniform4fv( uloc, size, reinterpret_cast<const GLfloat*>( static_cast< uniform< enum_to_type< FLOAT_VECTOR_4 >::type >*>( ubase )->get_value())); break;
396                        case INT_VECTOR_2   : glUniform2iv( uloc, size, reinterpret_cast<const GLint*>( static_cast< uniform< enum_to_type< INT_VECTOR_2 >::type >*>( ubase )->get_value())); break;
397                        case INT_VECTOR_3   : glUniform3iv( uloc, size, reinterpret_cast<const GLint*>( static_cast< uniform< enum_to_type< INT_VECTOR_3 >::type >*>( ubase )->get_value())); break;
398                        case INT_VECTOR_4   : glUniform4iv( uloc, size, reinterpret_cast<const GLint*>( static_cast< uniform< enum_to_type< INT_VECTOR_4 >::type >*>( ubase )->get_value())); break;
399                        case FLOAT_MATRIX_2 : glUniformMatrix2fv( uloc, size, GL_FALSE, reinterpret_cast<const GLfloat*>( static_cast< uniform< enum_to_type< FLOAT_MATRIX_2 >::type >*>( ubase )->get_value())); break;
400                        case FLOAT_MATRIX_3 : glUniformMatrix3fv( uloc, size, GL_FALSE, reinterpret_cast<const GLfloat*>( static_cast< uniform< enum_to_type< FLOAT_MATRIX_3 >::type >*>( ubase )->get_value())); break;
401                        case FLOAT_MATRIX_4 : glUniformMatrix4fv( uloc, size, GL_FALSE, reinterpret_cast<const GLfloat*>( static_cast< uniform< enum_to_type< FLOAT_MATRIX_4 >::type >*>( ubase )->get_value())); break;
[303]402                        default : break; // error?
403                        }
404                        ubase->clean();
405                }
406        }
407}
408
409void nv::gl_device::load_attributes( gl_program_info* p )
410{
411        int params;
412        glGetProgramiv( p->glid, GL_ACTIVE_ATTRIBUTES, &params );
413
[406]414        for ( unsigned i = 0; i < unsigned( params ); ++i )
[303]415        {
416                int attr_nlen;
417                int attr_len;
418                unsigned attr_type;
419                char name_buffer[128];
420
421                glGetActiveAttrib( p->glid, i, 128, &attr_nlen, &attr_len, &attr_type, name_buffer );
422
[439]423                string_view name( name_buffer, size_t( attr_nlen ) );
[303]424
425                // skip built-ins
426                if ( name.substr(0,3) == "gl_" ) continue;
427
[439]428                int attr_loc = glGetAttribLocation( p->glid, name.data() );
[303]429
[392]430                attribute& attr = (*p->m_attribute_map)[ name ];
[303]431                attr.location = attr_loc;
432                attr.type     = gl_enum_to_datatype( attr_type );
433                attr.length   = attr_len;
434        }
435}
436
437void nv::gl_device::load_uniforms( gl_program_info* p )
438{
439        int params;
[473]440        int bparams;
[303]441        glGetProgramiv( p->glid, GL_ACTIVE_UNIFORMS, &params );
[473]442        glGetProgramiv( p->glid, GL_ACTIVE_UNIFORM_BLOCKS, &bparams );
[303]443
[406]444        for ( unsigned i = 0; i < unsigned( params ); ++i )
[303]445        {
446                int uni_nlen;
447                int uni_len;
448                unsigned uni_type;
449                char name_buffer[128];
450
451                glGetActiveUniform( p->glid, i, 128, &uni_nlen, &uni_len, &uni_type, name_buffer );
452
[533]453                string_view name( name_buffer, uint32( uni_nlen ) );
[303]454
455                // skip built-ins
456                if ( name.substr(0,3) == "gl_" ) continue;
457
[439]458                int uni_loc = glGetUniformLocation( p->glid, name.data() );
[303]459                datatype utype = gl_enum_to_datatype( uni_type );
460
461                // check for array
[533]462                uint32 arrchar = name.find( '[' );
[439]463                if ( arrchar != string_view::npos )
[303]464                {
465                        name = name.substr( 0, arrchar );
466                }
467
[487]468                uniform_base* u = uniform_base::create( utype, uni_loc, size_t( uni_len ) );
[303]469                NV_ASSERT( u, "Unknown uniform type!" );
[392]470                (*p->m_uniform_map)[ name ] = u;
[492]471                //NV_LOG_DEBUG( "Uniform : ", name, " - ", utype, "/", uni_len );
472
[303]473        }
[473]474
475        for ( unsigned i = 0; i < unsigned( bparams ); ++i )
476        {
477                int uni_len;
478                char name_buffer[128];
479
480                glGetActiveUniformBlockName( p->glid, i, 128, &uni_len, name_buffer );
481                NV_LOG_INFO( string_view( name_buffer, size_t( uni_len ) ) );
482        }
[303]483}
484
[399]485bool nv::gl_device::compile( uint32 sh_type, string_view shader_code, unsigned& glid )
[303]486{
487        glid = glCreateShader( sh_type );
488
[361]489        const char* pc = shader_code.data();
[406]490        int l = int( shader_code.length() );
[303]491
[361]492        glShaderSource( glid, 1, &pc, &l );
[303]493        glCompileShader( glid );
494
495        const uint32 buffer_size = 1024;
496        char buffer[ buffer_size ] = { 0 };
497        int length;
498        int compile_ok = GL_FALSE;
499        glGetShaderiv(glid, GL_COMPILE_STATUS, &compile_ok);
500        glGetShaderInfoLog( glid, buffer_size, &length, buffer );
501
502        if ( length > 0 )
503        {
504                if ( compile_ok == 0 )
505                {
[406]506                        NV_LOG_ERROR( "Shader #", glid, " error: ", string_view( buffer, size_t( length ) ) );
[303]507                }
508                else
509                {
[406]510                        NV_LOG_INFO( "Shader #", glid, " compiled successfully: ", string_view( buffer, size_t( length ) ) );
[303]511                }
512        }
513        else
514        {
[365]515                NV_LOG_INFO( "Shader #", glid, " compiled successfully." );
[303]516        }
517        return compile_ok != 0;
518
519}
520
Note: See TracBrowser for help on using the repository browser.