Changeset 301 for trunk/src


Ignore:
Timestamp:
08/07/14 12:11:16 (11 years ago)
Author:
epyon
Message:
  • textures are now handled by lightweight handles
  • textures now need to be manually released via context
  • removed all old texture2d functionality
  • unreleased textures will be auto-released
  • textures are properly tracked via entity system
  • detailed stats and checking now possible
Location:
trunk/src
Files:
1 deleted
3 edited

Legend:

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

    r300 r301  
    88#include "nv/lib/gl.hh"
    99#include "nv/lib/sdl.hh"
    10 #include "nv/gl/gl_texture2d.hh"
     10#include "nv/gl/gl_device.hh"
    1111#include "nv/gl/gl_program.hh"
    1212#include "nv/gl/gl_vertex_buffer.hh"
     
    1414using namespace nv;
    1515
    16 void gl_context::bind( texture2d* texture, texture_slot slot )
    17 {
    18         GLuint id = static_cast< gl_texture2d* >( texture )->glid;
    19         glActiveTexture( GL_TEXTURE0 + static_cast< GLenum >( slot ) );
    20         glBindTexture( GL_TEXTURE_2D, id );
     16void gl_context::bind( texture t, texture_slot slot )
     17{
     18        const gl_texture_info* info = static_cast< const gl_texture_info* >( m_device->get_texture_info( t ) );
     19        if ( info )
     20        {
     21                glActiveTexture( GL_TEXTURE0 + static_cast< GLenum >( slot ) );
     22                glBindTexture( GL_TEXTURE_2D, info->glid );
     23        }
    2124}
    2225
     
    9497}
    9598
    96 void gl_context::update( texture2d* texture, void* data )
    97 {
    98         GLuint id = static_cast< gl_texture2d* >( texture )->glid;
    99         image_format format = texture->get_format();
    100         ivec2        size   = texture->get_size();
    101 
    102         glBindTexture( GL_TEXTURE_2D, id );
    103         glTexImage2D( GL_TEXTURE_2D, 0, (GLint)nv::image_format_to_enum(format.format), size.x, size.y, 0, nv::image_format_to_enum(format.format), nv::datatype_to_gl_enum(format.type), data );
     99void nv::gl_context::update( texture t, void* data )
     100{
     101        const gl_texture_info* info = static_cast< const gl_texture_info* >( m_device->get_texture_info( t ) );
     102        if ( info )
     103        {
     104                image_format format = info->format;
     105                ivec2        size   = info->size;
     106
     107                glBindTexture( GL_TEXTURE_2D, info->glid );
     108                glTexImage2D( GL_TEXTURE_2D, 0, (GLint)nv::image_format_to_enum(format.format), size.x, size.y, 0, nv::image_format_to_enum(format.format), nv::datatype_to_gl_enum(format.type), data );
     109        }
    104110}
    105111
  • trunk/src/gl/gl_device.cc

    r299 r301  
    88#include "nv/gl/gl_program.hh"
    99#include "nv/gl/gl_vertex_buffer.hh"
    10 #include "nv/gl/gl_texture2d.hh"
    1110#include "nv/logging.hh"
    1211#include "nv/lib/sdl.hh"
    1312#include "nv/lib/sdl_image.hh"
     13#include "nv/gl/gl_enum.hh"
     14#include "nv/lib/gl.hh"
    1415
    1516using namespace nv;
     
    8889}
    8990
    90 texture2d* gl_device::create_texture2d( ivec2 size, pixel_format aformat, datatype adatatype, sampler asampler, void* data /*= nullptr */ )
    91 {
    92         return new gl_texture2d( size, aformat, adatatype, asampler, data );
    93 }
    94 
    9591uint32 gl_device::get_ticks()
    9692{
     
    105101gl_device::~gl_device()
    106102{
     103        // TODO: better use release_texture
     104        for ( auto& t : m_textures ) glDeleteTextures( 1, &t.glid );
     105
    107106        SDL_Quit();
    108107}
     108
     109nv::texture nv::gl_device::create_texture( ivec2 size, image_format aformat, sampler asampler, void* data /*= nullptr */ )
     110{
     111        unsigned glid = 0;
     112        glGenTextures( 1, &glid );
     113
     114        glBindTexture( GL_TEXTURE_2D, glid );
     115
     116        // Detect if mipmapping was requested
     117        if (( asampler.filter_min != sampler::LINEAR && asampler.filter_min != sampler::NEAREST ) ||
     118                ( asampler.filter_max != sampler::LINEAR && asampler.filter_max != sampler::NEAREST ))
     119        {
     120                glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
     121        }
     122
     123        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (int)nv::sampler_filter_to_enum( asampler.filter_min ) );
     124        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (int)nv::sampler_filter_to_enum( asampler.filter_max ) );
     125        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (int)nv::sampler_wrap_to_enum( asampler.wrap_s) );
     126        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (int)nv::sampler_wrap_to_enum( asampler.wrap_t) );
     127
     128        if (data)
     129        {
     130                glTexImage2D( GL_TEXTURE_2D, 0, (GLint)nv::image_format_to_enum(aformat.format), size.x, size.y, 0, nv::image_format_to_enum(aformat.format), nv::datatype_to_gl_enum(aformat.type), data );
     131        }
     132
     133        glBindTexture( GL_TEXTURE_2D, 0 );
     134
     135        texture result = m_textures.create();
     136        gl_texture_info* info = m_textures.get( result );
     137        info->format  = aformat;
     138        info->sampler = asampler;
     139        info->size    = size;
     140        info->glid    = glid;
     141        return result;
     142}
     143
     144void nv::gl_device::release_texture( texture t )
     145{
     146        gl_texture_info* info = m_textures.get( t );
     147        if ( info )
     148        {
     149                glDeleteTextures( 1, &(info->glid) );
     150                m_textures.destroy( t );
     151        }
     152}
     153
     154const texture_info* nv::gl_device::get_texture_info( texture t )
     155{
     156        return m_textures.get( t );
     157}
     158
  • trunk/src/gui/gui_renderer.cc

    r299 r301  
    6565{
    6666public:
    67         screen_render_data( context* ctx, size_t initial_size )
    68                 : buffer( ctx, nv::DYNAMIC_DRAW, initial_size ), varray( nullptr ), shader(nullptr), texture(nullptr)
     67        screen_render_data( context* actx, size_t initial_size )
     68                : buffer( actx, nv::DYNAMIC_DRAW, initial_size ), varray( nullptr ), shader(nullptr)
    6969        {
    7070
     
    7474                delete shader;
    7575                delete varray;
    76                 delete texture;
    7776        }
    7877
    7978        nv::sliced_buffer<gui_quad> buffer;
     79        nv::texture       tex;
    8080        nv::vertex_array* varray;
    8181        nv::program*      shader;
    82         nv::texture2d*    texture;
    8382};
    8483
     
    121120
    122121        nv::sampler sampler( nv::sampler::LINEAR, nv::sampler::CLAMP_TO_EDGE );
    123         sr->texture = m_window->get_device()->create_texture2d( m_atlas.get_size(), nv::RGBA, nv::UBYTE, sampler, nullptr );
     122        sr->tex = m_window->get_device()->create_texture( m_atlas.get_size(), image_format( nv::RGBA, nv::UBYTE ), sampler, nullptr );
    124123
    125124        m_render_state.depth_test.enabled = false;
     
    262261        if ( m_reupload )
    263262        {
    264                 m_context->update( sr->texture, (void*)m_atlas.get_data() );
     263                m_context->update( sr->tex, (void*)m_atlas.get_data() );
    265264                m_reupload = false;
    266265        }
     
    273272                sr->varray->update_vertex_buffer( nv::slot::COLOR,    vb, false );
    274273        }
    275         m_context->bind( sr->texture, TEX_DIFFUSE );
     274        m_context->bind( sr->tex, TEX_DIFFUSE );
    276275        m_context->draw( TRIANGLES, m_render_state, m_scene_state, sr->shader, sr->varray, sr->buffer.get_size() * 6 );
    277276}
     
    283282                delete p;
    284283        }
    285         delete m_render_data;
    286 }
     284        if ( m_render_data )
     285        {
     286                m_context->get_device()->release_texture( ((screen_render_data*)m_render_data)->tex );
     287                delete m_render_data;
     288        }
     289}
Note: See TracChangeset for help on using the changeset viewer.