Changeset 491 for trunk/src/gl


Ignore:
Timestamp:
04/29/16 12:42:28 (9 years ago)
Author:
epyon
Message:
  • mass update (will try to do atomic from now)
Location:
trunk/src/gl
Files:
3 edited

Legend:

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

    r487 r491  
    1414using namespace nv;
    1515
    16 nv::vertex_array nv::gl_context::create_vertex_array()
     16nv::vertex_array nv::gl_context::create_vertex_array( const vertex_array_desc& desc )
    1717{
    1818        vertex_array result = m_vertex_arrays.create();
    19         vertex_array_info* info = m_vertex_arrays.get( result );
    20         info->count       = 0;
    21         info->index       = buffer();
    22         info->index_owner = false;
    23         info->index_type  = USHORT;
     19        gl_vertex_array_info* info = m_vertex_arrays.get( result );
     20
     21        glGenVertexArrays( 1, &info->glid );
     22        info->count       = desc.count;
     23        info->index       = desc.index;
     24        info->index_owner = desc.index_owner;
     25        info->index_type  = desc.index_type;
     26
     27        for ( uint32 i = 0; i < desc.count; ++i )
     28                info->attr[i] = desc.attr[i];
     29
     30        glBindVertexArray( info->glid );
     31
     32        for ( uint32 i = 0; i < info->count; ++i )
     33        {
     34                const vertex_buffer_attribute& vba = info->attr[i];
     35                uint32 location = static_cast<uint32>( vba.location );
     36                glEnableVertexAttribArray( location );
     37                const gl_buffer_info* vinfo = static_cast<const gl_buffer_info*>( m_device->get_buffer_info( vba.vbuffer ) );
     38                if ( vinfo && vinfo->type == VERTEX_BUFFER )
     39                        glBindBuffer( GL_ARRAY_BUFFER, vinfo->glid );
     40                else
     41                {
     42                        // TODO: report error
     43                }
     44
     45                glVertexAttribPointer(
     46                        location,
     47                        static_cast<GLint>( vba.components ),
     48                        nv::datatype_to_gl_enum( vba.dtype ),
     49                        GL_FALSE,
     50                        static_cast<GLsizei>( vba.stride ),
     51                        reinterpret_cast<void*>( vba.offset )
     52                        );
     53        }
     54        glBindBuffer( GL_ARRAY_BUFFER, 0 );
     55
     56        if ( info->index.is_valid() )
     57        {
     58                const gl_buffer_info* iinfo = static_cast<const gl_buffer_info*>( m_device->get_buffer_info( info->index ) );
     59                if ( iinfo && iinfo->type == INDEX_BUFFER )
     60                {
     61                        glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, iinfo->glid );
     62                }
     63                else
     64                {
     65                        // TODO: report error
     66                }
     67        }
     68        glBindBuffer( GL_ARRAY_BUFFER, 0 );
     69        glBindVertexArray( 0 );
     70
     71        if ( info->index.is_valid() )
     72        {
     73                glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
     74        }
     75
     76        for ( uint32 i = 0; i < info->count; ++i )
     77        {
     78                glDisableVertexAttribArray( static_cast<uint32>( info->attr[i].location ) );
     79        }
     80
     81
    2482        return result;
    2583}
     
    2785nv::framebuffer nv::gl_context::create_framebuffer()
    2886{
    29         if ( is_gl_extension_loaded( GL_EXT_FRAMEBUFFER_OBJECT ) && is_gl_extension_loaded( GL_EXT_FRAMEBUFFER_BLIT ) )
    30         {
    31                 unsigned glid   = 0;
    32                 glGenFramebuffers( 1, &glid );
    33                 framebuffer result = m_framebuffers.create();
    34                 gl_framebuffer_info* info = m_framebuffers.get( result );
    35                 info->glid = glid;
    36                 info->depth_rb_glid = 0;
    37                 info->color_attachment_count = 0;
    38                 return result;
    39         }
    40         else return framebuffer();
     87        unsigned glid   = 0;
     88        glGenFramebuffers( 1, &glid );
     89        framebuffer result = m_framebuffers.create();
     90        gl_framebuffer_info* info = m_framebuffers.get( result );
     91        info->glid = glid;
     92        info->depth_rb_glid = 0;
     93        info->color_attachment_count = 0;
     94        return result;
    4195}
    4296
    4397void nv::gl_context::release( vertex_array va )
    4498{
    45         vertex_array_info* info = m_vertex_arrays.get( va );
     99        gl_vertex_array_info* info = m_vertex_arrays.get( va );
    46100        if ( info )
    47101        {
     
    52106                }
    53107                if ( info->index.is_valid() && info->index_owner) m_device->release( info->index );
     108                glDeleteVertexArrays( 1, &info->glid );
    54109                m_vertex_arrays.destroy( va );
    55110        }
     
    71126}
    72127
    73 const vertex_array_info* nv::gl_context::get_vertex_array_info( vertex_array va ) const
    74 {
    75         return m_vertex_arrays.get( va );
    76 }
    77 
    78128const framebuffer_info* nv::gl_context::get_framebuffer_info( framebuffer f ) const
    79129{
    80130        return m_framebuffers.get( f );
    81 }
    82 
    83 vertex_array_info* nv::gl_context::get_vertex_array_info_mutable( vertex_array va )
    84 {
    85         return m_vertex_arrays.get( va );
    86131}
    87132
     
    170215        glDrawBuffer( 0 );
    171216        glReadBuffer( 0 );
    172         if ( is_gl_extension_loaded( GL_EXT_FRAMEBUFFER_OBJECT ) && is_gl_extension_loaded( GL_EXT_FRAMEBUFFER_BLIT ) )
    173         {
    174                 unsigned result = glCheckFramebufferStatus( framebuffer_slot_to_enum(ft) );
    175                 if ( result == GL_FRAMEBUFFER_COMPLETE ) return true;
    176                 switch ( result )
    177                 {
    178                 case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT         : NV_LOG_ERROR( "gl_context::check : Framebuffer incomplete attachment!" ); break;
    179                 case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT : NV_LOG_ERROR( "gl_context::check : Framebuffer missing attachment!" ); break;
     217
     218        unsigned result = glCheckFramebufferStatus( framebuffer_slot_to_enum(ft) );
     219        if ( result == GL_FRAMEBUFFER_COMPLETE ) return true;
     220        switch ( result )
     221        {
     222        case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT         : NV_LOG_ERROR( "gl_context::check : Framebuffer incomplete attachment!" ); break;
     223        case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT : NV_LOG_ERROR( "gl_context::check : Framebuffer missing attachment!" ); break;
    180224//              case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS         : NV_LOG_ERROR( "gl_context::check : Framebuffer incomplete dimensions!" ); break;
    181225//              case GL_FRAMEBUFFER_INCOMPLETE_FORMATS            : NV_LOG_ERROR( "gl_context::check : Framebuffer incomplete formats!" ); break;
    182                 case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER        : NV_LOG_ERROR( "gl_context::check : Framebuffer incomplete draw buffer!" ); break;
    183                 case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER        : NV_LOG_ERROR( "gl_context::check : Framebuffer incomplete read buffer!" ); break;
    184                 case GL_FRAMEBUFFER_UNSUPPORTED                   : NV_LOG_ERROR( "gl_context::check : Framebuffer format combination unsupported!" ); break;
    185                 default: NV_LOG_ERROR( "gl_context::check : Unknown Framebuffer error! (", result, ")" ); break;
    186                 }
    187         }
    188         else
    189         {
    190                 NV_LOG_ERROR( "gl_context::check : Framebuffer extensions not loaded!" );
     226        case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER        : NV_LOG_ERROR( "gl_context::check : Framebuffer incomplete draw buffer!" ); break;
     227        case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER        : NV_LOG_ERROR( "gl_context::check : Framebuffer incomplete read buffer!" ); break;
     228        case GL_FRAMEBUFFER_UNSUPPORTED                   : NV_LOG_ERROR( "gl_context::check : Framebuffer format combination unsupported!" ); break;
     229        default: NV_LOG_ERROR( "gl_context::check : Unknown Framebuffer error! (", result, ")" ); break;
    191230        }
    192231        glDrawBuffer( GL_BACK );
     
    205244        {
    206245                glBindFramebuffer( framebuffer_slot_to_enum(ft), 0 );
     246        }
     247}
     248
     249void nv::gl_context::bind( buffer b, texture t )
     250{
     251        const gl_buffer_info*  binfo = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
     252        const gl_texture_info* tinfo = static_cast< const gl_texture_info* >( m_device->get_texture_info( t ) );
     253        NV_ASSERT( binfo && tinfo, "Bad buffer or texture passed to create_texture" );
     254        if ( binfo && tinfo )
     255        {
     256                NV_ASSERT_ALWAYS( binfo->type == TEXTURE_BUFFER && tinfo->type == TEXTURE_1D_BUFFER, "bad texture or buffer type!" );
     257                glActiveTexture( GL_TEXTURE0 );
     258                glBindTexture( GL_TEXTURE_BUFFER, tinfo->glid );
     259                glTexBuffer( GL_TEXTURE_BUFFER, image_format_to_internal_enum( tinfo->format.format ), binfo->glid );
     260                glBindTexture( GL_TEXTURE_BUFFER, 0 );
    207261        }
    208262}
     
    252306void nv::gl_context::bind( vertex_array va )
    253307{
    254         const vertex_array_info* info = m_vertex_arrays.get( va );
    255         if ( info )
    256         {
    257                 for ( uint32 i = 0; i < info->count; ++i )
    258                 {
    259                         const vertex_buffer_attribute& vba = info->attr[i];
    260                         uint32 location                    = static_cast<uint32>( vba.location );
    261                         glEnableVertexAttribArray( location );
    262                         const gl_buffer_info* vinfo = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( vba.vbuffer ) );
    263                         if ( vinfo && vinfo->type == VERTEX_BUFFER )
    264                                 glBindBuffer( GL_ARRAY_BUFFER, vinfo->glid );
    265                         else
    266                         {
    267                                 // TODO: report error
    268                         }
    269 
    270                         glVertexAttribPointer(
    271                                 location,
    272                                 static_cast<GLint>( vba.components ),
    273                                 nv::datatype_to_gl_enum( vba.dtype ),
    274                                 GL_FALSE,
    275                                 static_cast<GLsizei>( vba.stride ),
    276                                 reinterpret_cast<void*>( vba.offset )
    277                                 );
    278                 }
    279                 glBindBuffer( GL_ARRAY_BUFFER, 0 );
    280 
    281                 if ( info->index.is_valid() )
    282                 {
    283                         const gl_buffer_info* iinfo = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( info->index ) );
    284                         if ( iinfo && iinfo->type == INDEX_BUFFER )
    285                         {
    286                                 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, iinfo->glid );
    287                         }
    288                         else
    289                         {
    290                                 // TODO: report error
    291                         }
    292                 }
     308        gl_vertex_array_info* info = m_vertex_arrays.get( va );
     309        if ( info )
     310        {
     311                glBindVertexArray( info->glid );
    293312        }
    294313}
     
    313332        if ( info )
    314333        {
    315                 if ( info->index.is_valid() )
    316                 {
    317                         glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
    318                 }
    319 
    320                 for ( uint32 i = 0; i < info->count; ++i )
    321                 {
    322                         glDisableVertexAttribArray( static_cast<uint32>( info->attr[i].location ) );
    323                 }
     334                glBindVertexArray( 0 );
    324335        }
    325336}
     
    340351{
    341352        const gl_texture_info* info = static_cast< const gl_texture_info* >( m_device->get_texture_info( t ) );
     353        NV_ASSERT_ALWAYS( info->type != TEXTURE_1D_BUFFER, "Buffer texture passed to update!" );
    342354        if ( info )
    343355        {
     
    347359
    348360                glBindTexture( gl_type, info->glid );
     361                int this_should_be_subTexImage;
    349362                if ( info->type == TEXTURE_3D || info->type == TEXTURE_2D_ARRAY )
    350363                        glTexImage3D( gl_type, 0, static_cast<GLint>( nv::image_format_to_internal_enum( format.format ) ), size.x, size.y, size.z, 0, nv::image_format_to_enum( format.format ), nv::datatype_to_gl_enum( format.type ), data );
     
    722735{
    723736        // TODO: configurable:
    724         load_gl_extensions( GL_EXT_FRAMEBUFFER_BLIT | GL_EXT_FRAMEBUFFER_OBJECT | GL_EXT_TEXTURE_ARRAY );
     737//      load_gl_extensions( GL_EXT_FRAMEBUFFER_BLIT | GL_EXT_FRAMEBUFFER_OBJECT | GL_EXT_TEXTURE_ARRAY );
    725738        force_apply_render_state( m_render_state );
    726 
    727739}
    728740
  • trunk/src/gl/gl_device.cc

    r487 r491  
    1717gl_device::gl_device()
    1818{
    19         m_shader_header.append( "#version 330\n" );
     19        m_shader_header.append( "#version 330 core\n" );
    2020        for ( auto& i : get_uniform_factory() )
    2121                m_shader_header.append( "uniform "+datatype_to_glsl_type( i.second->get_datatype() )+" "+ i.first +";\n" );
     
    9595nv::texture nv::gl_device::create_texture( texture_type type, ivec2 size, image_format aformat, sampler asampler, const void* data /*= nullptr */ )
    9696{
    97         NV_ASSERT_ALWAYS( type != TEXTURE_3D && type != TEXTURE_2D_ARRAY, "2D texture type expected!" );
     97        NV_ASSERT_ALWAYS( type != TEXTURE_1D_BUFFER && type != TEXTURE_3D && type != TEXTURE_2D_ARRAY, "2D texture type expected!" );
    9898        unsigned glid = 0;
    9999        unsigned gl_type = texture_type_to_enum( type );
     100        GLint gl_internal = GLint( nv::image_format_to_internal_enum( aformat.format ) );
     101        unsigned gl_enum = nv::image_format_to_enum( aformat.format );
    100102
    101103        bool is_depth = aformat.format == DEPTH16 || aformat.format == DEPTH24 || aformat.format == DEPTH32;
     
    106108
    107109        // Detect if mipmapping was requested
    108         if ( gl_type == GL_TEXTURE_2D && asampler.filter_min != sampler::LINEAR && asampler.filter_min != sampler::NEAREST )
     110        if ( gl_type == GL_TEXTURE_2D && gl_enum != GL_RED_INTEGER && asampler.filter_min != sampler::LINEAR && asampler.filter_min != sampler::NEAREST )
    109111        {
    110112                // TODO: This should not be done if we use framebuffers!
     
    119121        glTexParameteri( gl_type, GL_TEXTURE_MIN_FILTER, GLint( nv::sampler_filter_to_enum( asampler.filter_min ) ) );
    120122        glTexParameteri( gl_type, GL_TEXTURE_MAG_FILTER, GLint( nv::sampler_filter_to_enum( asampler.filter_max ) ) );
    121         glTexParameteri( gl_type, GL_TEXTURE_WRAP_S, GLint( nv::sampler_wrap_to_enum( asampler.wrap_s) ) );
    122         glTexParameteri( gl_type, GL_TEXTURE_WRAP_T, GLint( nv::sampler_wrap_to_enum( asampler.wrap_t) ) );
     123       
     124        if ( gl_enum != GL_RED_INTEGER )
     125        {
     126                glTexParameteri( gl_type, GL_TEXTURE_WRAP_S, GLint( nv::sampler_wrap_to_enum( asampler.wrap_s ) ) );
     127                glTexParameteri( gl_type, GL_TEXTURE_WRAP_T, GLint( nv::sampler_wrap_to_enum( asampler.wrap_t ) ) );
     128        }
    123129
    124130        if ( is_depth )
     
    140146//      glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso );
    141147
    142         glTexImage2D( gl_type, 0, GLint( nv::image_format_to_internal_enum(aformat.format) ), size.x, size.y, 0, nv::image_format_to_enum(aformat.format), nv::datatype_to_gl_enum(aformat.type), data );
     148        glTexImage2D( gl_type, 0, gl_internal, size.x, size.y, 0, gl_enum, nv::datatype_to_gl_enum(aformat.type), data );
    143149
    144150        glBindTexture( gl_type, 0 );
     
    154160}
    155161
     162
     163nv::texture nv::gl_device::create_texture( texture_type type, pixel_format format )
     164{
     165        NV_ASSERT_ALWAYS( type == TEXTURE_1D_BUFFER );
     166        unsigned glid = 0;
     167        unsigned gl_type = texture_type_to_enum( type );
     168
     169        glGenTextures( 1, &glid );
     170
     171        texture result = m_textures.create();
     172        gl_texture_info* info = m_textures.get( result );
     173        info->type = type;
     174        info->format = format;
     175        info->tsampler = sampler();
     176        info->size = ivec3( 1, 1, 1 );
     177        info->glid = glid;
     178
     179        return result;
     180}
     181
    156182nv::texture nv::gl_device::create_texture( texture_type type, ivec3 size, image_format aformat, sampler asampler, const void* data /*= nullptr */ )
    157183{
     
    234260nv::buffer nv::gl_device::create_buffer( buffer_type type, buffer_hint hint, size_t size, const void* source /*= nullptr */ )
    235261{
    236         unsigned glid   = 0;
     262        unsigned glid = 0;
    237263        unsigned glenum = buffer_type_to_enum( type );
    238264        glGenBuffers( 1, &glid );
    239265
    240         glBindBuffer( glenum, glid );
    241         glBufferData( glenum, GLsizeiptr( size ), source, buffer_hint_to_enum( hint ) );
    242         glBindBuffer( glenum, 0 );
     266        if ( size > 0 )
     267        {
     268                glBindBuffer( glenum, glid );
     269                glBufferData( glenum, GLsizeiptr( size ), source, buffer_hint_to_enum( hint ) );
     270                glBindBuffer( glenum, 0 );
     271        }
    243272
    244273        buffer result = m_buffers.create();
     
    249278        info->glid = glid;
    250279        return result;
     280}
     281
     282void nv::gl_device::create_buffer( buffer b, size_t size, const void* source )
     283{
     284        gl_buffer_info* info = m_buffers.get( b );
     285        if ( info )
     286        {
     287                unsigned glenum = buffer_type_to_enum( info->type );
     288                glBindBuffer( glenum, info->glid );
     289                glBufferData( glenum, GLsizeiptr( size ), source, buffer_hint_to_enum( info->hint ) );
     290                glBindBuffer( glenum, 0 );
     291        }
    251292}
    252293
  • trunk/src/gl/gl_enum.cc

    r473 r491  
    1515        switch( type )
    1616        {
    17         case TEXTURE_1D      : return GL_TEXTURE_1D;
    18         case TEXTURE_2D      : return GL_TEXTURE_2D;
    19         case TEXTURE_RECT    : return GL_TEXTURE_RECTANGLE;
    20         case TEXTURE_3D      : return GL_TEXTURE_3D;
    21         case TEXTURE_CUBE    : return GL_TEXTURE_CUBE_MAP;
    22         case TEXTURE_1D_ARRAY: return GL_TEXTURE_1D_ARRAY;
    23         case TEXTURE_2D_ARRAY: return GL_TEXTURE_2D_ARRAY;
     17        case TEXTURE_1D       : return GL_TEXTURE_1D;
     18        case TEXTURE_2D       : return GL_TEXTURE_2D;
     19        case TEXTURE_RECT     : return GL_TEXTURE_RECTANGLE;
     20        case TEXTURE_3D       : return GL_TEXTURE_3D;
     21        case TEXTURE_CUBE     : return GL_TEXTURE_CUBE_MAP;
     22        case TEXTURE_1D_ARRAY : return GL_TEXTURE_1D_ARRAY;
     23        case TEXTURE_2D_ARRAY : return GL_TEXTURE_2D_ARRAY;
     24        case TEXTURE_1D_BUFFER: return GL_TEXTURE_BUFFER;
    2425        NV_RETURN_COVERED_DEFAULT( 0 );
    2526        }
     
    171172        case INDEX_BUFFER   : return GL_ELEMENT_ARRAY_BUFFER;
    172173        case UNIFORM_BUFFER : return GL_UNIFORM_BUFFER;
     174        case TEXTURE_BUFFER : return GL_TEXTURE_BUFFER;
    173175                NV_RETURN_COVERED_DEFAULT( 0 );
    174176        }
     
    193195        case DEPTH24 : return GL_DEPTH_COMPONENT;
    194196        case DEPTH32 : return GL_DEPTH_COMPONENT;
     197        case R8I     : return GL_RED_INTEGER;
     198        case R8UI    : return GL_RED_INTEGER;
     199        case R16I    : return GL_RED_INTEGER;
     200        case R16UI   : return GL_RED_INTEGER;
     201        case R32I    : return GL_RED_INTEGER;
     202        case R32UI   : return GL_RED_INTEGER;
    195203        NV_RETURN_COVERED_DEFAULT( 0 );
    196204        }
     
    215223        case DEPTH24 : return GL_DEPTH_COMPONENT24;
    216224        case DEPTH32:  return GL_DEPTH_COMPONENT32;
     225        case R8I     : return GL_R8I;
     226        case R8UI    : return GL_R8UI;
     227        case R16I    : return GL_R16I;
     228        case R16UI   : return GL_R16UI;
     229        case R32I    : return GL_R32I;
     230        case R32UI   : return GL_R32UI;
    217231        NV_RETURN_COVERED_DEFAULT( 0 );
    218232        }
     
    358372        case GL_SAMPLER_1D_ARRAY_SHADOW: return INT;
    359373        case GL_SAMPLER_2D_ARRAY_SHADOW: return INT;
     374        case GL_SAMPLER_BUFFER         : return INT;
     375
     376        case GL_INT_SAMPLER_1D                : return INT;
     377        case GL_INT_SAMPLER_2D                : return INT;
     378        case GL_INT_SAMPLER_3D                : return INT;
     379        case GL_INT_SAMPLER_2D_RECT           : return INT;
     380        case GL_INT_SAMPLER_CUBE              : return INT;
     381        case GL_INT_SAMPLER_1D_ARRAY          : return INT;
     382        case GL_INT_SAMPLER_2D_ARRAY          : return INT;
     383        case GL_INT_SAMPLER_BUFFER            : return INT;
     384        case GL_UNSIGNED_INT_SAMPLER_1D       : return INT;
     385        case GL_UNSIGNED_INT_SAMPLER_2D       : return INT;
     386        case GL_UNSIGNED_INT_SAMPLER_3D       : return INT;
     387        case GL_UNSIGNED_INT_SAMPLER_2D_RECT  : return INT;
     388        case GL_UNSIGNED_INT_SAMPLER_CUBE     : return INT;
     389        case GL_UNSIGNED_INT_SAMPLER_1D_ARRAY : return INT;
     390        case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY : return INT;
     391        case GL_UNSIGNED_INT_SAMPLER_BUFFER   : return INT;
    360392                // TODO: implement?
    361393//      case GL_BOOL   
Note: See TracChangeset for help on using the changeset viewer.