Ignore:
Timestamp:
08/18/14 23:25:46 (11 years ago)
Author:
epyon
Message:
  • cleanup of context and device interfaces
  • create_vertex_array moved to context (as it's context bound)
  • added partial framebuffer functions to context
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gl/gl_context.cc

    r311 r313  
    1212using namespace nv;
    1313
     14nv::vertex_array nv::gl_context::create_vertex_array()
     15{
     16        vertex_array result = m_vertex_arrays.create();
     17        vertex_array_info* info = m_vertex_arrays.get( result );
     18        info->count       = 0;
     19        info->index       = buffer();
     20        info->index_owner = false;
     21        info->index_type  = USHORT;
     22        return result;
     23}
     24
     25nv::framebuffer nv::gl_context::create_framebuffer()
     26{
     27        if ( is_gl_extension_loaded( GL_EXT_FRAMEBUFFER_OBJECT ) )
     28        {
     29                unsigned glid   = 0;
     30                glGenFramebuffers( 1, &glid );
     31                framebuffer result = m_framebuffers.create();
     32                gl_framebuffer_info* info = m_framebuffers.get( result );
     33                info->glid = glid;
     34                info->color_attachment_count = 0;
     35                return result;
     36        }
     37        else return framebuffer();
     38}
     39
     40void nv::gl_context::release( vertex_array va )
     41{
     42        vertex_array_info* info = m_vertex_arrays.get( va );
     43        if ( info )
     44        {
     45                for ( uint32 i = 0; i < info->count; ++i )
     46                {
     47                        if ( info->attr[i].owner ) m_device->release( info->attr[i].vbuffer );
     48                }
     49                if ( info->index.is_valid() && info->index_owner) m_device->release( info->index );
     50                m_vertex_arrays.destroy( va );
     51        }
     52}
     53
     54void nv::gl_context::release( framebuffer f )
     55{
     56        gl_framebuffer_info* info = m_framebuffers.get( f );
     57        if ( info )
     58        {
     59                // TODO: release textures?
     60                glBindFramebuffer(GL_FRAMEBUFFER, 0);
     61                glDeleteFramebuffers(1, &info->glid);
     62        }
     63}
     64
     65const vertex_array_info* nv::gl_context::get_vertex_array_info( vertex_array va ) const
     66{
     67        return m_vertex_arrays.get( va );
     68}
     69
     70const framebuffer_info* nv::gl_context::get_framebuffer_info( framebuffer f ) const
     71{
     72        return m_framebuffers.get( f );
     73}
     74
     75vertex_array_info* nv::gl_context::get_vertex_array_info_mutable( vertex_array va )
     76{
     77        return m_vertex_arrays.get( va );
     78}
     79
     80
    1481void gl_context::bind( texture t, texture_slot slot )
    1582{
     
    3299}
    33100
    34 void nv::gl_context::bind( buffer b )
    35 {
    36         const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
    37         if ( info )
    38         {
    39                 glBindBuffer( buffer_type_to_enum( info->type ), info->glid );
    40         }
    41 }
     101// void nv::gl_context::bind( buffer b )
     102// {
     103//      const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
     104//      if ( info )
     105//      {
     106//              glBindBuffer( buffer_type_to_enum( info->type ), info->glid );
     107//      }
     108// }
    42109
    43110void nv::gl_context::bind( vertex_array va )
    44111{
    45         const vertex_array_info* info = m_device->get_vertex_array_info( va );
     112        const vertex_array_info* info = m_vertex_arrays.get( va );
    46113        if ( info )
    47114        {
     
    51118                        uint32 location                    = static_cast<uint32>( vba.location );
    52119                        glEnableVertexAttribArray( location );
    53                         bind( vba.vbuffer );
     120                        const gl_buffer_info* vinfo = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( vba.vbuffer ) );
     121                        if ( vinfo && vinfo->type == VERTEX_BUFFER )
     122                                glBindBuffer( GL_ARRAY_BUFFER, vinfo->glid );
     123                        else
     124                        {
     125                                // TODO: report error
     126                        }
     127
    54128                        glVertexAttribPointer(
    55129                                location,
     
    60134                                (void*)vba.offset
    61135                                );
    62                         unbind( vba.vbuffer );
    63                 }
     136                }
     137                glBindBuffer( GL_ARRAY_BUFFER, 0 );
    64138
    65139                if ( info->index.is_valid() )
    66140                {
    67                         bind( info->index );
    68                 }
     141                        const gl_buffer_info* iinfo = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( info->index ) );
     142                        if ( iinfo && iinfo->type == INDEX_BUFFER )
     143                        {
     144                                glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, iinfo->glid );
     145                        }
     146                        else
     147                        {
     148                                // TODO: report error
     149                        }
     150                }
     151        }
     152}
     153
     154void nv::gl_context::bind( framebuffer f )
     155{
     156        const gl_framebuffer_info* info = m_framebuffers.get( f );
     157        if ( info )
     158        {
     159                glBindFramebuffer( GL_FRAMEBUFFER, info->glid );
    69160        }
    70161}
     
    75166}
    76167
    77 void nv::gl_context::unbind( buffer b )
    78 {
    79         const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
    80         if ( info )
    81         {
    82                 glBindBuffer( buffer_type_to_enum( info->type ), 0 );
    83         }
    84 }
     168// void nv::gl_context::unbind( buffer b )
     169// {
     170//      const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
     171//      if ( info )
     172//      {
     173//              glBindBuffer( buffer_type_to_enum( info->type ), 0 );
     174//      }
     175// }
    85176
    86177void nv::gl_context::unbind( vertex_array va )
    87178{
    88         const vertex_array_info* info = m_device->get_vertex_array_info( va );
     179        const vertex_array_info* info = m_vertex_arrays.get( va );
    89180        if ( info )
    90181        {
    91182                if ( info->index.is_valid() )
    92183                {
    93                         unbind( info->index );
     184                        glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
    94185                }
    95186
     
    98189                        glDisableVertexAttribArray( static_cast<uint32>( info->attr[i].location ) );
    99190                }
     191        }
     192}
     193
     194void nv::gl_context::unbind( framebuffer f )
     195{
     196        // this way we are sure that the extension is loaded
     197        const gl_framebuffer_info* info = m_framebuffers.get( f );
     198        if ( info )
     199        {
     200                glBindFramebuffer( GL_FRAMEBUFFER, 0 );
    100201        }
    101202}
     
    119220        if ( info )
    120221        {
    121                 bind( b );
    122                 glBufferSubData( buffer_type_to_enum( info->type ), (GLintptr)offset, (GLsizeiptr)size, data );
     222                GLenum glenum = buffer_type_to_enum( info->type );
     223                glBindBuffer( glenum, info->glid );
     224                glBufferSubData( glenum, (GLintptr)offset, (GLsizeiptr)size, data );
    123225        }
    124226}
     
    483585nv::gl_context::~gl_context()
    484586{
     587        while ( m_framebuffers.size() > 0 )
     588                release( m_framebuffers.get_handle(0) );
     589        while ( m_vertex_arrays.size() > 0 )
     590                release( m_vertex_arrays.get_handle(0) );
    485591}
    486592
     
    500606{
    501607        apply_render_state( rs );
    502         const vertex_array_info* info = m_device->get_vertex_array_info( va );
     608        const vertex_array_info* info = m_vertex_arrays.get( va );
    503609        if ( count > 0 && info )
    504610        {
Note: See TracChangeset for help on using the changeset viewer.