Ignore:
Timestamp:
06/15/16 18:34:37 (9 years ago)
Author:
epyon
Message:
  • particle engine updates
  • device/context redesign
  • caching of GL state - texture bindings and programs
  • camera view_perspective and view_perspective_inv
File:
1 edited

Legend:

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

    r500 r501  
    6666                }
    6767        }
    68         glBindBuffer( GL_ARRAY_BUFFER, 0 );
    6968        glBindVertexArray( 0 );
    7069
     
    104103                {
    105104                        if ( info->attr[i].owner )
    106                                 m_device->release( info->attr[i].vbuffer );
    107                 }
    108                 if ( info->index.is_valid() && info->index_owner) m_device->release( info->index );
     105                                release( info->attr[i].vbuffer );
     106                }
     107                if ( info->index.is_valid() && info->index_owner) release( info->index );
    109108                glDeleteVertexArrays( 1, &info->glid );
    110109                m_vertex_arrays.destroy( va );
     
    223222                glBindFramebuffer( GL_DRAW_FRAMEBUFFER, tinfo ? tinfo->glid : 0 );
    224223                unsigned filter = mask == clear_state::COLOR_BUFFER ? GL_LINEAR : GL_NEAREST;
    225                 int remove_below;
    226                 filter = GL_NEAREST;
    227224                glBlitFramebuffer( src1.x, src1.y, src2.x, src2.y, dst1.x, dst1.y, dst2.x, dst2.y, clear_state_buffers_to_mask( mask ), filter );
    228225                glBindFramebuffer( GL_READ_FRAMEBUFFER, 0 );
     
    270267}
    271268
     269
    272270void nv::gl_context::bind( buffer b, texture t )
    273271{
     
    304302                        set_active_texture( slot );
    305303                        glBindTexture( texture_type_to_enum( info->type ), info->glid );
     304                        m_bound_textures[slot] = t;
    306305                }
    307306        }
     
    314313        if ( info )
    315314        {
    316                 glUseProgram( info->glid );
     315                if ( m_bound_program != p )
     316                        glUseProgram( info->glid );
    317317                gdevice->update_uniforms( info );
    318318                if ( !info->validated )
     
    320320                        validate_program( p );
    321321                }
     322                m_bound_program = p;
    322323        }
    323324}
     
    369370void nv::gl_context::unbind( program )
    370371{
    371         glUseProgram( 0 );
     372        if ( m_bound_program )
     373                glUseProgram( 0 );
     374        m_bound_program = program();
    372375}
    373376
     
    375378{
    376379        if ( slot != m_active_slot )
     380        {
    377381                glActiveTexture( GL_TEXTURE0 + static_cast<GLenum>( slot ) );
     382                m_active_slot = slot;
     383        }
    378384}
    379385
     
    419425                unsigned     gl_type = texture_type_to_enum( info->type );
    420426
    421                 if ( m_bound_textures[ m_active_slot ] != t )
    422                         glBindTexture( gl_type, info->glid );
    423                 int this_should_be_subTexImage;
     427                bind( t, texture_slot::TEXTURE_0 );
    424428                if ( info->type == TEXTURE_3D || info->type == TEXTURE_2D_ARRAY )
    425                         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 );
     429//                      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 );
     430                        glTexSubImage3D( gl_type, 0, 0, 0, 0, size.x, size.y, size.z, nv::image_format_to_enum( format.format ), nv::datatype_to_gl_enum( format.type ), data );
    426431                else
    427                         glTexImage2D( gl_type, 0, static_cast<GLint>( nv::image_format_to_internal_enum(format.format) ), size.x, size.y, 0, nv::image_format_to_enum(format.format), nv::datatype_to_gl_enum(format.type), data );
     432//                      glTexImage2D( gl_type, 0, static_cast<GLint>( nv::image_format_to_internal_enum(format.format) ), size.x, size.y, 0, nv::image_format_to_enum(format.format), nv::datatype_to_gl_enum(format.type), data );
     433                        glTexSubImage2D( gl_type, 0, 0, 0, size.x, size.y, nv::image_format_to_enum( format.format ), nv::datatype_to_gl_enum( format.type ), data );
    428434        }
    429435}
     
    518524void gl_context::set_viewport( const ivec4& viewport )
    519525{
    520         NV_ASSERT_ALWAYS( viewport.z > 0 && viewport.w > 0, "viewport dimensions must be greater than zero!" );
    521         m_viewport = viewport;
    522         glViewport( viewport.x, viewport.y, viewport.z, viewport.w );
     526        if ( m_viewport != viewport )
     527        {
     528                NV_ASSERT_ALWAYS( viewport.z > 0 && viewport.w > 0, "viewport dimensions must be greater than zero!" );
     529                m_viewport = viewport;
     530                glViewport( viewport.x, viewport.y, viewport.z, viewport.w );
     531        }
    523532}
    524533
     
    860869}
    861870
     871nv::texture nv::gl_context::create_texture( texture_type type, ivec2 size, image_format aformat, sampler asampler, const void* data /*= nullptr */ )
     872{
     873        texture result = create_texture( type, aformat.format );
     874        gl_texture_info* info = static_cast<gl_device*>( m_device )->get_full_texture_info( result );
     875        bind( result, texture_slot::TEXTURE_0 );
     876        unsigned glid = info->glid;
     877        unsigned gl_type = texture_type_to_enum( type );
     878        GLenum gl_internal = GLenum( image_format_to_internal_enum( aformat.format ) );
     879        unsigned gl_enum = image_format_to_enum( aformat.format );
     880
     881        bool is_depth = aformat.format == DEPTH16 || aformat.format == DEPTH24 || aformat.format == DEPTH32;
     882
     883        // Detect if mipmapping was requested
     884        //      if ( gl_type == GL_TEXTURE_2D && gl_enum != GL_RED_INTEGER && asampler.filter_min != sampler::LINEAR && asampler.filter_min != sampler::NEAREST )
     885        //      {
     886        //              // TODO: This should not be done if we use framebuffers!
     887        //              glTexParameteri( gl_type, GL_GENERATE_MIPMAP, GL_TRUE);
     888        //      }
     889
     890        if ( asampler.filter_max != sampler::NEAREST )
     891        {
     892                asampler.filter_max = sampler::LINEAR;
     893        }
     894
     895        if ( gl_type != GL_TEXTURE_2D_MULTISAMPLE )
     896        {
     897                glTexParameteri( gl_type, GL_TEXTURE_MIN_FILTER, GLint( nv::sampler_filter_to_enum( asampler.filter_min ) ) );
     898                glTexParameteri( gl_type, GL_TEXTURE_MAG_FILTER, GLint( nv::sampler_filter_to_enum( asampler.filter_max ) ) );
     899        }
     900
     901        if ( gl_type != GL_TEXTURE_2D_MULTISAMPLE && gl_enum != GL_RED_INTEGER )
     902        {
     903                glTexParameteri( gl_type, GL_TEXTURE_WRAP_S, GLint( nv::sampler_wrap_to_enum( asampler.wrap_s ) ) );
     904                glTexParameteri( gl_type, GL_TEXTURE_WRAP_T, GLint( nv::sampler_wrap_to_enum( asampler.wrap_t ) ) );
     905        }
     906
     907        if ( is_depth )
     908        {
     909                //              glTexParameteri( gl_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
     910                //              glTexParameteri( gl_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
     911
     912                // This is to allow usage of shadow2DProj function in the shader
     913                glTexParameteri( gl_type, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE );
     914                glTexParameteri( gl_type, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL );
     915        }
     916
     917        // #define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE
     918        // #define GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF
     919        //
     920        //      float aniso = 0.0f;
     921        //      glGetFloatv( GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &aniso );
     922        //      NV_LOG_INFO( "Anisotropy at ", aniso, " (", int( aniso ), " ) " );
     923        //      glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso );
     924
     925        if ( gl_type != GL_TEXTURE_2D_MULTISAMPLE )
     926                glTexImage2D( gl_type, 0, gl_internal, size.x, size.y, 0, gl_enum, nv::datatype_to_gl_enum( aformat.type ), data );
     927        else
     928                glTexImage2DMultisample( gl_type, 4, gl_internal, size.x, size.y, 1 );
     929
     930        if ( gl_type == GL_TEXTURE_2D && gl_enum != GL_RED_INTEGER && asampler.filter_min != sampler::LINEAR && asampler.filter_min != sampler::NEAREST )
     931        {
     932                // TODO: This should not be done if we use framebuffers!
     933                glGenerateMipmap( gl_type );
     934        }
     935
     936        bind( texture(), texture_slot::TEXTURE_0 );
     937
     938        info->type = type;
     939        info->format = aformat;
     940        info->tsampler = asampler;
     941        info->size = ivec3( size.x, size.y, 1 );
     942        info->glid = glid;
     943
     944        return result;
     945}
     946
     947nv::texture nv::gl_context::create_texture( texture_type type, ivec3 size, image_format aformat, sampler asampler, const void* data /*= nullptr */ )
     948{
     949        texture result = create_texture( type, aformat.format );
     950        gl_texture_info* info = static_cast<gl_device*>( m_device )->get_full_texture_info( result );
     951        bind( result, texture_slot::TEXTURE_0 );
     952        unsigned glid = info->glid;
     953
     954        unsigned gl_type = texture_type_to_enum( type );
     955
     956        bool is_depth = aformat.format == DEPTH16 || aformat.format == DEPTH24 || aformat.format == DEPTH32;
     957
     958        if ( asampler.filter_max != sampler::NEAREST )
     959        {
     960                asampler.filter_max = sampler::LINEAR;
     961        }
     962
     963        glTexParameteri( gl_type, GL_TEXTURE_MIN_FILTER, GLint( nv::sampler_filter_to_enum( asampler.filter_min ) ) );
     964        glTexParameteri( gl_type, GL_TEXTURE_MAG_FILTER, GLint( nv::sampler_filter_to_enum( asampler.filter_max ) ) );
     965        glTexParameteri( gl_type, GL_TEXTURE_WRAP_S, GLint( nv::sampler_wrap_to_enum( asampler.wrap_s ) ) );
     966        glTexParameteri( gl_type, GL_TEXTURE_WRAP_T, GLint( nv::sampler_wrap_to_enum( asampler.wrap_t ) ) );
     967
     968        if ( is_depth )
     969        {
     970                glTexParameteri( gl_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
     971                glTexParameteri( gl_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
     972
     973                // This is to allow usage of shadow2DProj function in the shader
     974                glTexParameteri( gl_type, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE );
     975                glTexParameteri( gl_type, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL );
     976        }
     977
     978        //glTexStorage3D( GL_TEXTURE_2D_ARRAY, mipLevelCount, GL_RGBA8, width, height, layerCount );
     979        glTexImage3D( gl_type, 0, GLint( nv::image_format_to_internal_enum( aformat.format ) ), size.x, size.y, size.z, 0, nv::image_format_to_enum( aformat.format ), nv::datatype_to_gl_enum( aformat.type ), data );
     980
     981        bind( texture(), texture_slot::TEXTURE_0 );
     982
     983        info->type = type;
     984        info->format = aformat;
     985        info->tsampler = asampler;
     986        info->size = size;
     987        info->glid = glid;
     988
     989        return result;
     990}
     991
     992nv::buffer nv::gl_context::create_buffer( buffer_type type, buffer_hint hint, size_t size, const void* source /*= nullptr */ )
     993{
     994        buffer result = static_cast<gl_device*>( m_device )->create_buffer( type, hint );
     995        if ( size > 0 ) create_buffer( result, size, source );
     996        return result;
     997}
     998
     999void nv::gl_context::create_buffer( buffer b, size_t size, const void* source /*= nullptr */ )
     1000{
     1001        gl_buffer_info* info = static_cast<gl_device*>( m_device )->get_full_buffer_info( b );
     1002        if ( info )
     1003        {
     1004                unsigned glenum = buffer_type_to_enum( info->type );
     1005                glBindBuffer( glenum, info->glid );
     1006                glBufferData( glenum, GLsizeiptr( size ), source, buffer_hint_to_enum( info->hint ) );
     1007                glBindBuffer( glenum, 0 );
     1008        }
     1009}
     1010
    8621011void nv::gl_context::set_draw_buffers( uint32 count, const output_slot* slots )
    8631012{
Note: See TracChangeset for help on using the changeset viewer.