Changeset 302 for trunk/src/gl


Ignore:
Timestamp:
08/07/14 19:06:34 (11 years ago)
Author:
epyon
Message:
  • buffers and vertex_arrays are now handle based
Location:
trunk/src/gl
Files:
1 deleted
3 edited

Legend:

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

    r301 r302  
    1010#include "nv/gl/gl_device.hh"
    1111#include "nv/gl/gl_program.hh"
    12 #include "nv/gl/gl_vertex_buffer.hh"
    1312
    1413using namespace nv;
     
    3130}
    3231
    33 void nv::gl_context::bind( vertex_buffer* b )
    34 {
    35         GLuint id = static_cast< gl_vertex_buffer* >( b )->glid;
    36         glBindBuffer( GL_ARRAY_BUFFER, id );
    37 }
    38 
    39 void nv::gl_context::bind( index_buffer* b )
    40 {
    41         GLuint id = static_cast< gl_index_buffer* >( b )->glid;
    42         glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, id );
    43 }
    44 
    45 void nv::gl_context::bind( vertex_array* va )
    46 {
    47         for ( vertex_buffer_attribute_map::iterator i = va->m_map.begin();      i != va->m_map.end(); ++i )
    48         {
    49                 uint32 location             = static_cast<uint32>( i->first );
    50                 vertex_buffer_attribute* va = i->second;
    51                 vertex_buffer*           vb = va->get_buffer();
    52                 glEnableVertexAttribArray( location );
    53                 bind( vb );
    54                 glVertexAttribPointer(
    55                         location,
    56                         static_cast<GLint>( va->get_components() ),
    57                         nv::datatype_to_gl_enum( va->get_datatype() ),
    58                         GL_FALSE,
    59                         static_cast<GLsizei>( va->get_stride() ),
    60                         (void*)va->get_offset()
    61                         );
    62                 unbind( vb );
    63         }
    64 
    65         if ( va->m_index )
    66         {
    67                 bind( va->m_index );
     32void nv::gl_context::bind( buffer b )
     33{
     34        const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
     35        if ( info )
     36        {
     37                glBindBuffer( buffer_type_to_enum( info->type ), info->glid );
     38        }
     39}
     40
     41void nv::gl_context::bind( vertex_array va )
     42{
     43        vertex_array_info* info = get_vertex_array_info( va );
     44        if ( info )
     45        {
     46                for ( uint32 i = 0; i < info->count; ++i )
     47                {
     48                        const vertex_buffer_attribute& vba = info->attr[i];
     49                        uint32 location                    = static_cast<uint32>( vba.location );
     50                        glEnableVertexAttribArray( location );
     51                        bind( vba.vbuffer );
     52                        glVertexAttribPointer(
     53                                location,
     54                                static_cast<GLint>( vba.components ),
     55                                nv::datatype_to_gl_enum( vba.dtype ),
     56                                GL_FALSE,
     57                                static_cast<GLsizei>( vba.stride ),
     58                                (void*)vba.offset
     59                                );
     60                        unbind( vba.vbuffer );
     61                }
     62
     63                if ( info->index.is_valid() )
     64                {
     65                        bind( info->index );
     66                }
    6867        }
    6968}
     
    7473}
    7574
    76 void nv::gl_context::unbind( vertex_buffer* )
    77 {
    78         glBindBuffer( GL_ARRAY_BUFFER, 0 );
    79 }
    80 
    81 void nv::gl_context::unbind( index_buffer* )
    82 {
    83         glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
    84 }
    85 
    86 void nv::gl_context::unbind( vertex_array* va )
    87 {
    88         if ( va->m_index )
    89         {
    90                 unbind( va->m_index );
    91         }
    92 
    93         for ( vertex_buffer_attribute_map::iterator i = va->m_map.begin();      i != va->m_map.end(); ++i )
    94         {
    95                 glDisableVertexAttribArray( static_cast<uint32>( i->first ) );
     75void nv::gl_context::unbind( buffer b )
     76{
     77        const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
     78        if ( info )
     79        {
     80                glBindBuffer( buffer_type_to_enum( info->type ), 0 );
     81        }
     82}
     83
     84void nv::gl_context::unbind( vertex_array va )
     85{
     86        vertex_array_info* info = get_vertex_array_info( va );
     87        if ( info )
     88        {
     89                if ( info->index.is_valid() )
     90                {
     91                        unbind( info->index );
     92                }
     93
     94                for ( uint32 i = 0; i < info->count; ++i )
     95                {
     96                        glDisableVertexAttribArray( static_cast<uint32>( info->attr[i].location ) );
     97                }
    9698        }
    9799}
     
    110112}
    111113
    112 void gl_context::update( vertex_buffer* b, const void* data, size_t offset, size_t size )
    113 {
    114         bind( b );
    115         glBufferSubData( GL_ARRAY_BUFFER, (GLintptr)offset, (GLsizeiptr)size, data );
    116 }
    117 
    118 void gl_context::update( index_buffer* b, const void* data, size_t offset, size_t size )
    119 {
    120         bind( b );
    121         glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, (GLintptr)offset, (GLsizeiptr)size, data );
     114void gl_context::update( buffer b, const void* data, size_t offset, size_t size )
     115{
     116        const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
     117        if ( info )
     118        {
     119                bind( b );
     120                glBufferSubData( buffer_type_to_enum( info->type ), (GLintptr)offset, (GLsizeiptr)size, data );
     121        }
    122122}
    123123
     
    483483}
    484484
    485 void gl_context::draw( primitive prim, const render_state& rs, program* p, vertex_array* va, size_t count )
     485void gl_context::draw( primitive prim, const render_state& rs, program* p, vertex_array va, size_t count )
    486486{
    487487        apply_render_state( rs );
    488         if ( count > 0 )
     488        vertex_array_info* info = get_vertex_array_info( va );
     489        if ( count > 0 && info )
    489490        {
    490491                bind( p );
    491492                bind( va );
    492                 if ( va->has_index_buffer() )
    493                 {
    494                         glDrawElements( primitive_to_enum(prim), static_cast<GLsizei>( count ), datatype_to_gl_enum( va->get_index_buffer_type() ), 0 );
     493                if ( info->index.is_valid() )
     494                {
     495                        glDrawElements( primitive_to_enum(prim), static_cast<GLsizei>( count ), datatype_to_gl_enum( info->index_type ), 0 );
    495496                }
    496497                else
  • trunk/src/gl/gl_device.cc

    r301 r302  
    77#include "nv/gl/gl_window.hh"
    88#include "nv/gl/gl_program.hh"
    9 #include "nv/gl/gl_vertex_buffer.hh"
    109#include "nv/logging.hh"
    1110#include "nv/lib/sdl.hh"
     
    5655}
    5756
    58 vertex_buffer* gl_device::create_vertex_buffer( buffer_hint hint, size_t size, const void* source /*= nullptr */ )
    59 {
    60         return new gl_vertex_buffer( hint, size, source );
    61 }
    62 
    63 index_buffer* gl_device::create_index_buffer( buffer_hint hint, size_t size, const void* source /*= nullptr */ )
    64 {
    65         return new gl_index_buffer( hint, size, source );
    66 }
    67 
    68 vertex_array* gl_device::create_vertex_array()
    69 {
    70         return new vertex_array();
    71 }
    72 
    7357// this is a temporary function that will be removed once we find a way to
    7458// pass binary file data around
     
    10185gl_device::~gl_device()
    10286{
    103         // TODO: better use release_texture
    104         for ( auto& t : m_textures ) glDeleteTextures( 1, &t.glid );
     87        while ( m_textures.size() > 0 )
     88                release( m_textures.get_handle(0) );
     89        while ( m_buffers.size() > 0 )
     90                release( m_buffers.get_handle(0) );
    10591
    10692        SDL_Quit();
     
    142128}
    143129
    144 void nv::gl_device::release_texture( texture t )
     130void nv::gl_device::release( texture t )
    145131{
    146132        gl_texture_info* info = m_textures.get( t );
    147133        if ( info )
    148134        {
    149                 glDeleteTextures( 1, &(info->glid) );
     135                if ( info->glid != 0 )
     136                {
     137                        glDeleteTextures( 1, &(info->glid) );
     138                }
    150139                m_textures.destroy( t );
     140        }
     141}
     142
     143void nv::gl_device::release( buffer b )
     144{
     145        gl_buffer_info* info = m_buffers.get( b );
     146        if ( info )
     147        {
     148                if ( info->glid != 0 )
     149                {
     150                        glDeleteBuffers( 1, &(info->glid) );
     151                }
     152                m_buffers.destroy( b );
    151153        }
    152154}
     
    157159}
    158160
     161nv::buffer nv::gl_device::create_buffer( buffer_type type, buffer_hint hint, size_t size, const void* source /*= nullptr */ )
     162{
     163        unsigned glid   = 0;
     164        unsigned glenum = buffer_type_to_enum( type );
     165        glGenBuffers( 1, &glid );
     166
     167        glBindBuffer( glenum, glid );
     168        glBufferData( glenum, (GLsizeiptr)size, source, buffer_hint_to_enum( hint ) );
     169        glBindBuffer( glenum, 0 );
     170
     171        buffer result = m_buffers.create();
     172        gl_buffer_info* info = m_buffers.get( result );
     173        info->type = type;
     174        info->hint = hint;
     175        info->size = size;
     176        info->glid = glid;
     177        return result;
     178}
     179
     180const buffer_info* nv::gl_device::get_buffer_info( buffer t )
     181{
     182        return m_buffers.get( t );
     183}
  • trunk/src/gl/gl_enum.cc

    r292 r302  
    144144        case DYNAMIC_DRAW  : return GL_DYNAMIC_DRAW;
    145145        NV_RETURN_COVERED_DEFAULT( 0 );
     146        }
     147}
     148
     149unsigned int nv::buffer_type_to_enum( buffer_type type )
     150{
     151        switch( type )
     152        {
     153        case VERTEX_BUFFER : return GL_ARRAY_BUFFER;
     154        case INDEX_BUFFER  : return GL_ELEMENT_ARRAY_BUFFER;
     155                NV_RETURN_COVERED_DEFAULT( 0 );
    146156        }
    147157}
Note: See TracChangeset for help on using the changeset viewer.