Changeset 302


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

Legend:

Unmodified
Added
Removed
  • trunk/nv/gfx/debug_draw.hh

    r237 r302  
    3131                void reset();
    3232                program*      get_program()      { return m_program; }
    33                 vertex_array* get_vertex_array() { return m_va; }
     33                vertex_array  get_vertex_array() { return m_va; }
    3434                uint32        get_count()        { return m_data.size(); }
    3535
     
    4040                device*                  m_device;
    4141                program*                 m_program;
    42                 vertex_array*            m_va;
     42                vertex_array             m_va;
    4343                std::vector< debug_vtx > m_data;
    4444        };
  • trunk/nv/gfx/keyframed_mesh.hh

    r299 r302  
    1919        {
    2020        public:
    21                 keyframed_mesh( const mesh_data* a_data, const mesh_nodes_data* a_tag_map );
     21                keyframed_mesh( context* a_context, const mesh_data* a_data, const mesh_nodes_data* a_tag_map );
     22                virtual vertex_array get_vertex_array() const { return m_va; }
    2223                virtual size_t get_index_count() const { return m_index_count; }
    2324                virtual void run_animation( animation_entry* a_anim );
     
    5051                const mesh_nodes_data*  m_tag_map;
    5152                const mesh_raw_channel* m_vchannel;
     53
     54                context*     m_context;
     55                buffer       m_pbuffer;
     56                vertex_array m_va;
    5257
    5358                uint32 m_last_frame;
     
    8691                ~keyframed_mesh_cpu();
    8792        private:
    88                 context* m_context;
    89 
    90                 uint8*         m_data;
    91                 vertex_buffer* m_vb;
     93                uint8*  m_data;
    9294        };
    9395
  • trunk/nv/gfx/skeletal_mesh.hh

    r299 r302  
    1919        {
    2020        public:
    21                 skeletal_mesh() : animated_mesh() {}
     21                skeletal_mesh( context* a_context ) : animated_mesh(), m_context( a_context ) {}
     22                virtual vertex_array get_vertex_array() const { return m_va; }
    2223                virtual void run_animation( animation_entry* a_anim )
    2324                {
    2425                        update_animation( a_anim, 0 );
    2526                }
     27                ~skeletal_mesh()
     28                {
     29                        m_context->get_device()->release( m_va );
     30                }
     31        protected:
     32                vertex_array m_va;
     33                context*     m_context;
    2634        };
    2735
     
    4452                virtual size_t get_index_count() const { return m_indices; }
    4553                virtual void update_animation( animation_entry* a_anim, uint32 a_anim_time );
    46                 virtual ~skeletal_mesh_cpu();
    4754        protected:
    48                 context*                     m_context;
     55                buffer                       m_pbuffer;
    4956                uint32                       m_indices;
    5057                dynamic_array< md5_vtx_pnt > m_pntdata;
  • trunk/nv/gfx/sliced_buffer.hh

    r299 r302  
    9393                static const size_t value_type_size = sizeof(T);
    9494
    95                 sliced_buffer( context* ctx, buffer_hint hint, size_t initial_size, bool is_vertex = true )
     95                sliced_buffer( context* ctx, buffer_type type, buffer_hint hint, size_t initial_size )
    9696                        : m_context( ctx )
    97                         , m_buffer( nullptr )
     97                        , m_buffer()
     98                        , m_type( type )
    9899                        , m_hint( hint )
    99100                        , m_full_update( true )
    100                         , m_is_vertex( is_vertex )
    101101                        , m_data()
     102                        , m_capacity(0)
    102103                        , m_min(0)
    103104                        , m_max(0)
     
    168169                                size_t offset = m_min * value_type_size;
    169170                                size_t size   = (m_max-m_min) * value_type_size;
    170                                 if ( m_is_vertex )
    171                                         m_context->update( (vertex_buffer*)m_buffer, m_data.data() + m_min, offset, size );
    172                                 else
    173                                         m_context->update( (index_buffer*)m_buffer, m_data.data() + m_min, offset, size );
     171                                m_context->update( m_buffer, m_data.data() + m_min, offset, size );
    174172                        }
    175173                        m_full_update = false;
     
    181179                size_t get_max_size() const
    182180                {
    183                         return m_buffer->get_size() / value_type_size;
     181                        return m_capacity;
    184182                }
    185183
     
    189187                }
    190188
    191                 buffer* get_buffer()
     189                buffer get_buffer()
    192190                {
    193191                        return m_buffer;
     
    196194                virtual ~sliced_buffer()
    197195                {
    198                         delete m_buffer;
     196                        m_context->get_device()->release( m_buffer );
    199197                }
    200198        private:
    201199                void create_buffer( size_t size )
    202200                {
    203                         delete m_buffer;
    204                         if ( m_is_vertex )
    205                                 m_buffer = m_context->get_device()->create_vertex_buffer( m_hint, size * value_type_size, nullptr );
    206                         else
    207                                 m_buffer = m_context->get_device()->create_index_buffer( m_hint, size * value_type_size, nullptr );
     201                        m_context->get_device()->release( m_buffer );
     202                        m_capacity = size;
     203                        m_buffer   = m_context->get_device()->create_buffer( m_type, m_hint, size * value_type_size, nullptr );
    208204                }
    209205        private:
    210206                context*    m_context;
    211                 buffer*     m_buffer;
     207                buffer      m_buffer;
    212208                buffer_hint m_hint;
     209                buffer_type m_type;
    213210                bool        m_full_update;
    214                 bool        m_is_vertex;
    215211                vector      m_data;
     212                uint32      m_capacity;
    216213
    217214                size_t      m_min;
     
    275272
    276273                indexed_sliced_buffer( device* dev, buffer_hint hint, int initial_size, int initial_index_size )
    277                         : m_vertex_buffer( dev, hint, initial_size, true )
    278                         , m_index_buffer( dev, hint, initial_index_size, false )
     274                        : m_vertex_buffer( dev, VERTEX_BUFFER, hint, initial_size )
     275                        , m_index_buffer( dev, INDEX_BUFFER, hint, initial_index_size )
    279276                {
    280277
     
    298295                int get_vertex_size()     const { return m_vertex_buffer.get_size(); }
    299296                int get_index_size()      const { return m_index_buffer.get_size(); }
    300                 vertex_buffer* get_vertex_buffer() { return static_cast<vertex_buffer*>( m_vertex_buffer.get_buffer() ); }
    301                 index_buffer*  get_index_buffer()  { return static_cast<index_buffer*>( m_index_buffer.get_buffer() ); }
     297                buffer* get_buffer() { return m_vertex_buffer.get_buffer(); }
    302298                sliced_vertex_buffer& get_vertex_cache() { return m_vertex_buffer; }
    303299                sliced_index_buffer&  get_index_cache()  { return m_index_buffer; }
  • trunk/nv/gl/gl_context.hh

    r301 r302  
    2525                virtual void bind( texture t, texture_slot slot );
    2626                virtual void bind( program* p );
    27                 virtual void bind( vertex_buffer* b );
    28                 virtual void bind( index_buffer* b );
    29                 virtual void bind( vertex_array* va );
     27                virtual void bind( buffer b );
     28                virtual void bind( vertex_array va );
    3029                virtual void unbind( program* p );
    31                 virtual void unbind( index_buffer* b );
    32                 virtual void unbind( vertex_buffer* b );
    33                 virtual void unbind( vertex_array* va );
     30                virtual void unbind( buffer b );
     31                virtual void unbind( vertex_array va );
    3432
    3533                virtual void update( texture t, void* data );
    36                 virtual void update( index_buffer* b, const void* data, size_t offset, size_t size );
    37                 virtual void update( vertex_buffer* b, const void* data, size_t offset, size_t size );
     34                virtual void update( buffer b, const void* data, size_t offset, size_t size );
    3835
    3936                virtual void clear( const clear_state& cs );
    4037                // temporary
    41                 virtual void draw( primitive prim, const render_state& rs, program* p, vertex_array* va, size_t count );
     38                virtual void draw( primitive prim, const render_state& rs, program* p, vertex_array va, size_t count );
    4239                virtual const ivec4& get_viewport();
    4340                virtual void set_viewport( const ivec4& viewport );
  • trunk/nv/gl/gl_device.hh

    r301 r302  
    2222        };
    2323
     24        struct gl_buffer_info : public buffer_info
     25        {
     26                unsigned glid;
     27        };
     28
    2429        class gl_device : public device
    2530        {
     
    2833                virtual window* create_window( uint16 width, uint16 height, bool fullscreen );
    2934                virtual window* adopt_window( void* sys_w_handle, void* sys_dc );
     35                virtual image_data* create_image_data( const std::string& filename ); // temporary
     36
    3037                virtual program* create_program( const string& vs_source, const string& fs_source );
    31                 virtual vertex_buffer* create_vertex_buffer( buffer_hint hint, size_t size, const void* source = nullptr );
    32                 virtual index_buffer* create_index_buffer( buffer_hint hint, size_t size, const void* source = nullptr );
    33                 virtual vertex_array* create_vertex_array();
    34                 virtual image_data* create_image_data( const std::string& filename ); // temporary
     38                virtual buffer create_buffer( buffer_type type, buffer_hint hint, size_t size, const void* source = nullptr );
     39                virtual texture create_texture( ivec2 size, image_format aformat, sampler asampler, void* data = nullptr );
    3540                virtual uint32 get_ticks();
    3641                virtual void delay( uint32 ms );
    3742
    38                 virtual texture create_texture( ivec2 size, image_format aformat, sampler asampler, void* data = nullptr );
    39                 virtual void release_texture( texture t );
     43                virtual void release( buffer b );
     44                virtual void release( texture t );
    4045                virtual const texture_info* get_texture_info( texture t );
     46                virtual const buffer_info* get_buffer_info( buffer t );
    4147
    4248                virtual ~gl_device();
     
    4450                const void* m_info;
    4551                entity_store< gl_texture_info, texture > m_textures;
     52                entity_store< gl_buffer_info,  buffer >  m_buffers;
    4653        };
    4754
  • trunk/nv/gl/gl_enum.hh

    r301 r302  
    3333        unsigned int stencil_operation_to_enum( stencil_test_face::operation type );
    3434        unsigned int buffer_hint_to_enum( buffer_hint hint );
     35        unsigned int buffer_type_to_enum( buffer_type type );
    3536        unsigned int image_format_to_enum( pixel_format format );
    3637        unsigned int sampler_filter_to_enum( sampler::filter filter );
  • trunk/nv/gui/gui_renderer.hh

    r299 r302  
    3333                struct vertex
    3434                {
    35                         ivec2 coord;
    36                         vec2  tcoord;
     35                        ivec2 position;
     36                        vec2  texcoord;
    3737                        vec4  color;
    3838                        vertex() {}
    3939                        vertex( const nv::ivec2& cr, const nv::vec2& tc, const nv::vec4& c )
    40                                 : coord( cr ), tcoord( tc ), color( c ) {}
     40                                : position( cr ), texcoord( tc ), color( c ) {}
    4141                };
    4242
  • trunk/nv/interface/context.hh

    r301 r302  
    3737        {
    3838        public:
    39                 mesh_interface() : m_va( nullptr ) {}
    40                 explicit mesh_interface( vertex_array* array ) : m_va( array ) {}
     39                mesh_interface() {}
    4140                virtual void update( uint32 ) {}
    4241                virtual void update( program* ) {}
    43                 virtual nv::vertex_array* get_vertex_array() const { return m_va; }
    44                 virtual size_t get_index_count() const { return 0; }
     42                virtual nv::vertex_array get_vertex_array() const = 0;
     43                virtual size_t get_index_count() const = 0;
    4544                virtual nv::primitive get_primitive_type() const { return nv::TRIANGLES; }
    4645                virtual ~mesh_interface() {}
    47         protected:
    48                 vertex_array* m_va;
    4946        };
    5047
    51         class device;
    52         class texture2d;
    5348        class context
    5449        {
     
    6156                }
    6257                virtual void bind( texture, texture_slot ) = 0;
    63                 virtual void bind( vertex_buffer* ) = 0;
    64                 virtual void bind( index_buffer* ) = 0;
     58                virtual void bind( buffer ) = 0;
    6559                virtual void bind( program* ) = 0;
    66                 virtual void bind( vertex_array* ) = 0;
    67                 virtual void unbind( vertex_buffer* ) = 0;
    68                 virtual void unbind( index_buffer* ) = 0;
     60                virtual void bind( vertex_array ) = 0;
     61                virtual void unbind( buffer ) = 0;
    6962                virtual void unbind( program* ) = 0;
    70                 virtual void unbind( vertex_array* ) = 0;
     63                virtual void unbind( vertex_array ) = 0;
    7164                virtual void update( texture, void* ) = 0;
    72                 virtual void update( index_buffer*, const void*, size_t /*offset*/, size_t /*size*/ ) = 0;
    73                 virtual void update( vertex_buffer*, const void*, size_t /*offset*/, size_t /*size*/ ) = 0;
     65                virtual void update( buffer, const void*, size_t /*offset*/, size_t /*size*/ ) = 0;
    7466
    7567                virtual void clear( const clear_state& cs ) = 0;
    7668                // temporary
    77                 virtual void draw( primitive prim, const render_state& rs, program* p, vertex_array* va, size_t count ) = 0;
     69                virtual void draw( primitive prim, const render_state& rs, program* p, vertex_array va, size_t count ) = 0;
    7870
    7971                virtual void draw( const render_state& rs, program* p, const mesh_interface* mesh )
     
    8678                        draw( rs, p, mesh );
    8779                }
    88                 virtual void draw( primitive prim, const render_state& rs, const scene_state& s, program* p, vertex_array* va, size_t count )
     80                virtual void draw( primitive prim, const render_state& rs, const scene_state& s, program* p, vertex_array va, size_t count )
    8981                {
    9082                        p->apply_engine_uniforms( this, &s );
     
    10294                }
    10395        protected:
     96                vertex_array_info* get_vertex_array_info( vertex_array va )
     97                {
     98                        return m_device->m_vertex_arrays.get( va );
     99                }
     100
    104101                device*      m_device;
    105102                clear_state  m_clear_state;
  • trunk/nv/interface/device.hh

    r301 r302  
    2525        class program;
    2626
     27        struct texture_tag {};
     28        struct vertex_array_tag {};
     29        struct buffer_tag {};
     30        typedef handle< uint32, 16, 16, buffer_tag >       buffer;
     31        typedef handle< uint32, 16, 16, texture_tag >      texture;
     32        typedef handle< uint32, 16, 16, vertex_array_tag > vertex_array;
     33
     34
    2735        struct sampler
    2836        {
     
    5765        };
    5866
     67        struct buffer_info
     68        {
     69                buffer_type type;
     70                buffer_hint hint;
     71                size_t      size;
     72        };
     73
     74
    5975        struct texture_info
    6076        {
     
    6480        };
    6581
    66 
    67         struct texture_tag {};
    68         typedef handle< uint32, 16, 16, texture_tag > texture;
     82        struct vertex_buffer_attribute
     83        {
     84                buffer   vbuffer;
     85                datatype dtype;
     86                size_t   components;
     87                size_t   offset;
     88                size_t   stride;
     89                slot     location;
     90                bool     owner;
     91        };
     92
     93        struct vertex_array_info
     94        {
     95                static const int MAX_ATTRIBUTES = 24;
     96
     97                uint32        count;
     98                buffer        index;
     99                bool          index_owner;
     100                datatype      index_type;
     101                vertex_buffer_attribute attr[MAX_ATTRIBUTES];
     102        };
    69103
    70104        class device
    71105        {
     106                friend class context;
    72107        public:
    73108                virtual window* create_window( uint16 width, uint16 height, bool fullscreen ) = 0;
    74109                virtual window* adopt_window( void* sys_w_handle, void* sys_dc ) = 0;
    75110                virtual program* create_program( const string& vs_source, const string& fs_source ) = 0;
    76                 virtual vertex_buffer* create_vertex_buffer( buffer_hint hint, size_t size, const void* source = nullptr ) = 0;
    77                 virtual index_buffer* create_index_buffer( buffer_hint hint, size_t size, const void* source = nullptr ) = 0;
    78                 virtual vertex_array* create_vertex_array() = 0;
     111                virtual buffer create_buffer( buffer_type type, buffer_hint hint, size_t size, const void* source = nullptr ) = 0;
    79112                virtual image_data* create_image_data( const std::string& filename ) = 0; // temporary
    80113                virtual texture create_texture( ivec2 size, image_format aformat, sampler asampler, void* data = nullptr ) = 0;
    81                 virtual void release_texture( texture ) = 0;
     114                virtual void release( texture ) = 0;
     115                virtual void release( buffer ) = 0;
    82116                virtual const texture_info* get_texture_info( texture ) = 0;
     117                virtual const buffer_info* get_buffer_info( buffer ) = 0;
    83118                virtual uint32 get_ticks() = 0;
    84119                virtual void delay( uint32 ms ) = 0;
    85 
    86120                virtual texture create_texture( image_data* data, sampler asampler )
    87121                {
     
    89123                }
    90124
     125                virtual vertex_array create_vertex_array()
     126                {
     127                        vertex_array result = m_vertex_arrays.create();
     128                        vertex_array_info* info = m_vertex_arrays.get( result );
     129                        info->count       = 0;
     130                        info->index       = buffer();
     131                        info->index_owner = false;
     132                        info->index_type  = USHORT;
     133                        return result;
     134                }
     135
     136                virtual void release( vertex_array va )
     137                {
     138                        vertex_array_info* info = m_vertex_arrays.get( va );
     139                        if ( info )
     140                        {
     141                                for ( uint32 i = 0; i < info->count; ++i )
     142                                {
     143                                        if ( info->attr[i].owner ) release( info->attr[i].vbuffer );
     144                                }
     145                                if ( info->index.is_valid() && info->index_owner) release( info->index );
     146                                m_vertex_arrays.destroy( va );
     147                        }
     148                };
     149
    91150                template < typename VTX, slot SLOT >
    92                 void add_vertex_buffer_impl( vertex_array*, vertex_buffer*, const std::false_type& )
     151                void add_vertex_buffer_impl( vertex_array, buffer, const std::false_type& )
    93152                {
    94153                }
    95154
    96155                template < typename VTX, slot SLOT >
    97                 void add_vertex_buffer_impl( vertex_array* va, vertex_buffer* vb, const std::true_type& )
     156                void add_vertex_buffer_impl( vertex_array va, buffer vb, const std::true_type& )
    98157                {
    99158                        typedef vertex_slot_info< VTX, SLOT > vinfo;
    100159                        typedef datatype_traits< typename vinfo::value_type > dt_traits;
    101                         va->add_vertex_buffer( SLOT, vb, type_to_enum< dt_traits::base_type >::type, dt_traits::size, vinfo::offset, sizeof( VTX ), false );
     160                        add_vertex_buffer( va, SLOT, vb, type_to_enum< dt_traits::base_type >::type, dt_traits::size, vinfo::offset, sizeof( VTX ), false );
    102161                }
    103162
    104163                template < typename VTX, slot SLOT >
    105                 void add_vertex_buffer( vertex_array* va, vertex_buffer* vb )
     164                void add_vertex_buffer( vertex_array va, buffer vb )
    106165                {
    107166                        add_vertex_buffer_impl< VTX, SLOT >( va, vb, std::integral_constant< bool, vertex_has_slot< VTX, SLOT >::value >() );
    108167                }
    109168
    110 
    111169                template < typename VTX >
    112                 vertex_array* create_vertex_array( const VTX* v, size_t count, buffer_hint hint )
    113                 {
    114                         // TODO: vb will not be owned or freed!
    115                         vertex_array*  va = create_vertex_array();
    116                         vertex_buffer* vb = create_vertex_buffer( hint, count * sizeof( VTX ), v );
     170                void add_vertex_buffers( vertex_array va, buffer vb )
     171                {
    117172                        add_vertex_buffer< VTX, slot::POSITION >  ( va, vb );
    118173                        add_vertex_buffer< VTX, slot::TEXCOORD >  ( va, vb );
     
    122177                        add_vertex_buffer< VTX, slot::BONEWEIGHT >( va, vb );
    123178                        add_vertex_buffer< VTX, slot::COLOR >     ( va, vb );
     179                }
     180
     181                void add_vertex_buffers( vertex_array va, buffer buf, const mesh_raw_channel* channel )
     182                {
     183                        for ( uint32 s = 0; s < channel->desc.count; ++s )
     184                        {
     185                                const vertex_descriptor_slot& slot = channel->desc.slots[s];
     186                                const datatype_info& info = get_datatype_info(slot.etype);
     187                                add_vertex_buffer( va, slot.vslot, buf, info.base , info.elements, slot.offset, channel->desc.size, false );
     188                        }
     189                }
     190
     191                template < typename VTX >
     192                vertex_array create_vertex_array( const VTX* v, size_t count, buffer_hint hint )
     193                {
     194                        // TODO: vb will not be owned or freed!
     195                        vertex_array va = create_vertex_array();
     196                        buffer       vb = create_buffer( VERTEX_BUFFER, hint, count * sizeof( VTX ), v );
     197                        add_vertex_buffers< VTX >( va, vb );
    124198                        return va;
    125199                }
    126200
    127201                template < typename VTX >
    128                 vertex_array* create_vertex_array( const std::vector< VTX >& data, buffer_hint hint )
     202                vertex_array create_vertex_array( const std::vector< VTX >& data, buffer_hint hint )
    129203                {
    130204                        return create_vertex_array( data.data(), data.size(), hint );
     
    132206
    133207                template < typename VTX, typename IDX >
    134                 vertex_array* create_vertex_array( const VTX* v, size_t vcount, const IDX* i, size_t icount, buffer_hint hint )
    135                 {
    136                         vertex_array* va = create_vertex_array( v, vcount, hint );
    137                         index_buffer* ib = create_index_buffer( hint, icount * sizeof( IDX ), i );
     208                vertex_array create_vertex_array( const VTX* v, size_t vcount, const IDX* i, size_t icount, buffer_hint hint )
     209                {
     210                        vertex_array va = create_vertex_array( v, vcount, hint );
     211                        buffer       ib = create_buffer( INDEX_BUFFER, hint, icount * sizeof( IDX ), i );
    138212                        va->set_index_buffer( ib, type_to_enum< IDX >::type, true );
    139213                        return va;
     
    141215
    142216                template < typename VTX, typename IDX >
    143                 vertex_array* create_vertex_array( const std::vector< VTX >& data, const std::vector< IDX >& idata, buffer_hint hint )
     217                vertex_array create_vertex_array( const std::vector< VTX >& data, const std::vector< IDX >& idata, buffer_hint hint )
    144218                {
    145219                        return create_vertex_array( data.data(), data.size(), idata.data(), idata.size(), hint );
    146220                }
    147221
     222                void replace_vertex_buffer( vertex_array va, buffer vb, bool owner )
     223                {
     224                        vertex_array_info* info = m_vertex_arrays.get( va );
     225                        if ( info )
     226                        {
     227                                for ( uint32 i = 0; i < info->count; ++i )
     228                                {
     229                                        vertex_buffer_attribute& vba = info->attr[i];
     230                                        if ( vba.owner ) release( vba.vbuffer );
     231                                        vba.vbuffer = vb;
     232                                        vba.owner   = owner;
     233                                }
     234                        }
     235                }
     236
     237                void replace_vertex_buffer( vertex_array va, buffer vb, slot location, bool owner )
     238                {
     239                        vertex_array_info* info = m_vertex_arrays.get( va );
     240                        if ( info )
     241                        {
     242                                for ( uint32 i = 0; i < info->count; ++i )
     243                                {
     244                                        if ( info->attr[i].location == location )
     245                                        {
     246                                                vertex_buffer_attribute& vba = info->attr[i];
     247                                                if ( vba.owner ) release( vba.vbuffer );
     248                                                vba.vbuffer = vb;
     249                                                vba.owner   = owner;
     250                                        }
     251                                }
     252                        }
     253                }
     254
     255                void update_attribute_offset( vertex_array va, slot location, size_t offset )
     256                {
     257                        vertex_array_info* info = m_vertex_arrays.get( va );
     258                        if ( info )
     259                        {
     260                                for ( uint32 i = 0; i < info->count; ++i )
     261                                {
     262                                        if ( info->attr[i].location == location )
     263                                        {
     264                                                info->attr[i].offset = offset;
     265                                        }
     266                                }
     267                        }
     268                }
     269
     270                void set_index_buffer( vertex_array va, buffer b, datatype datatype, bool owner )
     271                {
     272                        vertex_array_info* info = m_vertex_arrays.get( va );
     273                        if ( info )
     274                        {
     275                                if ( get_buffer_info( b )->type == INDEX_BUFFER )
     276                                {
     277                                        if (info->index.is_valid() && info->index_owner) release( info->index );
     278
     279                                        info->index       = b;
     280                                        info->index_owner = owner;
     281                                        info->index_type  = datatype;
     282                                }
     283                        }
     284                }
     285
     286                void add_vertex_buffer( vertex_array va, slot location, buffer buf, datatype datatype, size_t components, size_t offset = 0, size_t stride = 0, bool owner = true )
     287                {
     288                        vertex_array_info* info = m_vertex_arrays.get( va );
     289                        if ( info )
     290                        {
     291                                NV_ASSERT( info->count < vertex_array_info::MAX_ATTRIBUTES, "MAX_ATTRIBUTES reached!" );
     292                                vertex_buffer_attribute& p = info->attr[ info->count ];
     293                                p.vbuffer    = buf;
     294                                p.dtype      = datatype;
     295                                p.components = components;
     296                                p.offset     = offset;
     297                                p.stride     = stride;
     298                                p.owner      = owner;
     299                                p.location   = location;
     300                                info->count++;
     301                        }
     302                }
     303
     304                buffer find_buffer( vertex_array va, slot location )
     305                {
     306                        vertex_array_info* info = m_vertex_arrays.get( va );
     307                        if ( info )
     308                        {
     309                                for ( uint32 i = 0; i < info->count; ++i )
     310                                {
     311                                        if ( info->attr[i].location == location )
     312                                        {
     313                                                return info->attr[i].vbuffer;
     314                                        }
     315                                }
     316                        }
     317                        return buffer();
     318                }
     319
    148320                // TODO: HINTS ARE DIFFERENT!
    149                 vertex_array* create_vertex_array( const mesh_data* data, buffer_hint hint )
    150                 {
    151                         vertex_array*  va = create_vertex_array();
     321                vertex_array create_vertex_array( const mesh_data* data, buffer_hint hint )
     322                {
     323                        vertex_array  va = create_vertex_array();
    152324                        const std::vector< mesh_raw_channel* >& channels = data->get_raw_channels();
    153325                        for ( uint32 ch = 0; ch < channels.size(); ++ch )
     
    156328                                if ( channel->count > 0 )
    157329                                {
    158                                         if ( channel->is_index() )
    159                                         {
    160                                                 index_buffer* ib = create_index_buffer( hint, channel->size(), channel->data );
    161                                                 va->set_index_buffer( ib, channel->desc.slots[0].etype, true );
     330                                        buffer_type type = channel->get_buffer_type();
     331                                        buffer b = create_buffer( type, hint, channel->size(), channel->data );
     332                                        // TODO: no if switch
     333                                        if ( type == INDEX_BUFFER )
     334                                        {
     335                                                set_index_buffer( va, b, channel->desc.slots[0].etype, true );
    162336                                        }
    163337                                        else
    164338                                        {
    165                                                 vertex_buffer* vb = create_vertex_buffer( hint, channel->size(), channel->data );
    166                                                 va->add_vertex_buffers( vb, channel );
     339                                                add_vertex_buffers( va, b, channel );
    167340                                        }
    168341                                }
     
    172345
    173346
    174                 virtual ~device() {}
     347                virtual ~device()
     348                {
     349                        // TODO: releases buffers via gl_context which is destoryed!
     350                        while ( m_vertex_arrays.size() > 0 )
     351                                release( m_vertex_arrays.get_handle(0) );
     352                }
     353        protected:
     354                entity_store< vertex_array_info, vertex_array > m_vertex_arrays;
    175355        };
    176356
  • trunk/nv/interface/mesh_data.hh

    r297 r302  
    3636                }
    3737
    38                 bool is_index() const { return count > 0 && desc.slots[0].vslot == slot::INDEX; }
     38                // TODO: this could have more options if stored!
     39                buffer_type get_buffer_type() const
     40                {
     41                        if ( count > 0 && desc.slots[0].vslot == slot::INDEX )
     42                        {
     43                                return INDEX_BUFFER;
     44                        }
     45                        return VERTEX_BUFFER;
     46                }
    3947
    4048                uint32 size() const { return count * desc.size; }
     
    92100                        NV_ASSERT( channel, "nullptr passed to add_channel!" );
    93101                        m_channels.push_back( channel );
    94                         if ( channel->is_index() )
     102                        if ( channel->get_buffer_type() == INDEX_BUFFER )
    95103                        {
    96104                                NV_ASSERT( !m_index_channel, "second index channel!" );
  • trunk/nv/interface/vertex.hh

    r295 r302  
    3131                SLOT_MAX_STORE = 8,
    3232        };
     33
     34        enum buffer_hint
     35        {
     36                STATIC_DRAW,
     37                STREAM_DRAW,
     38                DYNAMIC_DRAW
     39        };
     40
     41        enum buffer_type
     42        {
     43                VERTEX_BUFFER,
     44                INDEX_BUFFER,
     45        };
     46
    3347
    3448        namespace detail
  • trunk/nv/interface/vertex_buffer.hh

    r299 r302  
    2121namespace nv
    2222{
    23         class vertex_array;
    24 
    25         enum buffer_hint
    26         {
    27                 STATIC_DRAW,
    28                 STREAM_DRAW,
    29                 DYNAMIC_DRAW
    30         };
    31 
    32         class buffer
    33         {
    34         public:
    35                 buffer( buffer_hint hint, size_t size ) { m_size = size; m_hint = hint; }
    36                 size_t get_size() const { return m_size; }
    37                 buffer_hint get_hint() const { return m_hint; }
    38         virtual ~buffer() {}
    39         protected:
    40                 size_t      m_size;
    41                 buffer_hint m_hint;
    42         };
    43 
    44         class vertex_buffer : public buffer
    45         {
    46         public:
    47                 vertex_buffer( buffer_hint hint, size_t size ) : buffer( hint, size ) {}
    48         };
    49 
    50         class index_buffer  : public buffer
    51         {
    52         public:
    53                 index_buffer( buffer_hint hint, size_t size ) : buffer( hint, size ) {}
    54         };
    55 
    56         class vertex_buffer_attribute
    57         {
    58         public:
    59                 vertex_buffer_attribute( vertex_buffer* buffer, datatype datatype, size_t components, size_t offset = 0, size_t stride = 0, bool owner = true )
    60                         : m_buffer( buffer ), m_datatype( datatype ), m_components( components ), m_offset( offset ), m_stride( stride ), m_owner( owner ) {}
    61 
    62                 vertex_buffer* get_buffer() const { return m_buffer; }
    63                 void set_buffer( vertex_buffer* b, bool owner ) { if (m_owner) delete m_buffer; m_buffer = b; m_owner = owner; }
    64                 datatype get_datatype() const { return m_datatype; }
    65                 size_t get_components() const { return m_components; }
    66                 size_t get_offset() const { return m_offset; }
    67                 size_t get_stride() const { return m_stride; }
    68 
    69                 ~vertex_buffer_attribute()
    70                 {
    71                         if (m_owner)
    72                         {
    73                                 delete m_buffer;
    74                         }
    75                 }
    76         protected:
    77                 friend class vertex_array;
    78 
    79                 vertex_buffer* m_buffer;
    80                 datatype       m_datatype;
    81                 size_t  m_components;
    82                 size_t  m_offset;
    83                 size_t  m_stride;
    84                 bool    m_owner;
    85         };
    86 
    87         typedef std::unordered_map< int, vertex_buffer_attribute* > vertex_buffer_attribute_map;
    88 
    89         class vertex_array
    90         {
    91         public:
    92                 friend class context;
    93                 friend class gl_context; // TODO: HACK, remove
    94 
    95                 vertex_array() : m_map(), m_index( nullptr ), m_index_owner( false ), m_index_type(USHORT) {}
    96                 void add_vertex_buffer( int location, vertex_buffer* buffer, datatype datatype, size_t components, size_t offset = 0, size_t stride = 0, bool owner = true )
    97                 {
    98                         auto p = new vertex_buffer_attribute( buffer, datatype, components, offset, stride, owner );
    99                         m_map[ location ] = p;
    100                 }
    101                 void add_vertex_buffer( slot location, vertex_buffer* buffer, datatype datatype, size_t components, size_t offset = 0, size_t stride = 0, bool owner = true )
    102                 {
    103                         add_vertex_buffer( (int)location, buffer, datatype, components, offset, stride, owner );
    104                 }
    105 
    106                 void add_vertex_buffers( vertex_buffer* buffer, const mesh_raw_channel* channel )
    107                 {
    108                         for ( uint32 s = 0; s < channel->desc.count; ++s )
    109                         {
    110                                 add_vertex_buffer( buffer, channel->desc.slots[s], channel->desc.size );
    111                         }
    112                 }
    113 
    114                 void add_vertex_buffer( vertex_buffer* buffer, const vertex_descriptor_slot& slot, size_t vtx_size )
    115                 {
    116                         const datatype_info& info = get_datatype_info(slot.etype);
    117                         add_vertex_buffer( slot.vslot, buffer, info.base , info.elements, slot.offset, vtx_size, false );
    118                 }
    119 
    120                 void update_vertex_buffer( int location, vertex_buffer* b, bool owner )
    121                 {
    122                         auto i = m_map.find( location );
    123                         if ( i != m_map.end() )
    124                         {
    125                                 i->second->set_buffer( b, owner );
    126                         }
    127                 }
    128                 void update_vertex_buffer( int location, size_t offset )
    129                 {
    130                         auto i = m_map.find( location );
    131                         if ( i != m_map.end() )
    132                         {
    133                                 i->second->m_offset = offset;
    134                         }
    135                 }
    136                 void update_vertex_buffer( slot location, vertex_buffer* b, bool owner )
    137                 {update_vertex_buffer( (int)location, b, owner ); }
    138                 void update_vertex_buffer( slot location, size_t offset ) {update_vertex_buffer( (int)location, offset );       }
    139                 void set_index_buffer( index_buffer* buffer, datatype datatype, bool owner )
    140                 {
    141                         if (m_index && m_index_owner) delete m_index;
    142                         m_index       = buffer;
    143                         m_index_owner = owner;
    144                         m_index_type  = datatype;
    145                 }
    146                 void set_index_buffer( index_buffer* buffer )
    147                 {
    148                         if (m_index && m_index_owner) delete m_index;
    149                         m_index       = buffer;
    150                 }
    151                 vertex_buffer* find_buffer( int location )
    152                 {
    153                         auto i = m_map.find( location );
    154                         if ( i != m_map.end() )
    155                         {
    156                                 return i->second->get_buffer();
    157                         }
    158                         return nullptr;
    159                 }
    160                 vertex_buffer* find_buffer( slot location ) { return find_buffer((int)location); }
    161                 bool has_index_buffer() const { return m_index != nullptr; }
    162                 datatype get_index_buffer_type() const { return m_index_type; }
    163                 virtual ~vertex_array() {
    164                         for ( vertex_buffer_attribute_map::iterator i = m_map.begin();  i != m_map.end(); ++i )
    165                         {
    166                                 delete i->second;
    167                         }
    168                         if (m_index && m_index_owner) delete m_index;
    169                 }
    170         protected:
    171                 vertex_buffer_attribute_map m_map;
    172                 index_buffer* m_index;
    173                 bool          m_index_owner;
    174                 datatype      m_index_type;
    175         };
    176 
    17723
    17824} // namespace nv
  • trunk/src/formats/assimp_loader.cc

    r294 r302  
    317317                        {
    318318                                mesh_raw_channel* channel = meshes[m].get_raw_channels()[0];
    319                                 NV_ASSERT( !channel->is_index(), "index channel in release_merged!" );
    320319                                assimp_skinned_vtx* va = (assimp_skinned_vtx*)channel->data;
    321320                                for ( unsigned v = 0; v < channel->count; ++v )
  • trunk/src/gfx/debug_draw.cc

    r237 r302  
    2929
    3030nv::debug_data::debug_data( device* a_device )
    31         : m_device( a_device ), m_program( nullptr ), m_va( nullptr )
     31        : m_device( a_device ), m_program( nullptr ), m_va()
    3232{
    3333        m_program = m_device->create_program( nv_debug_draw_vertex_shader, nv_debug_draw_fragment_shader );
     
    3636void nv::debug_data::update()
    3737{
    38         delete m_va;
     38        m_device->release( m_va );
    3939        m_va = m_device->create_vertex_array( m_data, nv::STATIC_DRAW );
    4040}
     
    7575nv::debug_data::~debug_data()
    7676{
    77         delete m_va;
     77        m_device->release( m_va );
    7878        delete m_program;
    7979}
  • trunk/src/gfx/keyframed_mesh.cc

    r299 r302  
    1515using namespace nv;
    1616
    17 nv::keyframed_mesh::keyframed_mesh( const mesh_data* a_data, const mesh_nodes_data* a_tag_map )
     17nv::keyframed_mesh::keyframed_mesh( context* a_context, const mesh_data* a_data, const mesh_nodes_data* a_tag_map )
    1818        : animated_mesh()
     19        , m_context( a_context )
    1920        , m_mesh_data( a_data )
    2021        , m_tag_map( a_tag_map )
     
    3637        }
    3738        m_frame_count  = m_vchannel->count / m_vertex_count;
     39        m_pbuffer      = buffer();
    3840}
    3941
     
    103105nv::keyframed_mesh::~keyframed_mesh()
    104106{
    105         delete m_va;
     107        m_context->get_device()->release( m_va );
    106108}
    107109
     
    122124
    123125nv::keyframed_mesh_gpu::keyframed_mesh_gpu( context* a_context, const mesh_data* a_data, const mesh_nodes_data* a_tag_map )
    124         : keyframed_mesh( a_data, a_tag_map )
     126        : keyframed_mesh( a_context, a_data, a_tag_map )
    125127        , m_loc_next_position( -1 )
    126128        , m_loc_next_normal( -1 )
     
    129131        , m_gpu_next_frame( 0xFFFFFFFF )
    130132{
    131         m_va = a_context->get_device()->create_vertex_array( a_data, STATIC_DRAW );
     133        m_va      = a_context->get_device()->create_vertex_array( a_data, STATIC_DRAW );
     134        m_pbuffer = a_context->get_device()->find_buffer( m_va, slot::POSITION );
    132135}
    133136
     
    137140        if ( m_loc_next_position == -1 ) return;
    138141        animated_mesh::update( ms );
    139 
     142        device* dev = m_context->get_device();
    140143        if ( m_gpu_last_frame != m_last_frame )
    141144        {
    142                 m_va->update_vertex_buffer( slot::POSITION, m_last_frame * m_vertex_count * m_vsize );
    143                 m_va->update_vertex_buffer( slot::NORMAL,   m_last_frame * m_vertex_count * m_vsize + sizeof( vec3 ) );
     145                uint32 base_offset = m_last_frame * m_vertex_count * m_vsize;
     146                dev->update_attribute_offset( m_va, slot::POSITION, base_offset );
     147                dev->update_attribute_offset( m_va, slot::NORMAL,   base_offset + sizeof( vec3 ) );
    144148                if ( m_has_tangent && m_loc_next_tangent != -1 )
    145149                {
    146                         m_va->update_vertex_buffer( slot::TANGENT,   m_last_frame * m_vertex_count * m_vsize + 2*sizeof( vec3 ) );
     150                        dev->update_attribute_offset( m_va, slot::TANGENT, base_offset + 2*sizeof( vec3 ) );
    147151                }
    148152                m_gpu_last_frame = m_last_frame;
     
    150154        if ( m_loc_next_position != -1 && m_gpu_next_frame != m_next_frame )
    151155        {
    152                 m_va->update_vertex_buffer( m_loc_next_position, m_next_frame * m_vertex_count * m_vsize );
    153                 m_va->update_vertex_buffer( m_loc_next_normal,   m_next_frame * m_vertex_count * m_vsize + sizeof( vec3 ) );
    154                 m_va->update_vertex_buffer( m_loc_next_tangent,   m_next_frame * m_vertex_count * m_vsize + 2*sizeof( vec3 ) );
     156                uint32 base_offset = m_next_frame * m_vertex_count * m_vsize;
     157                dev->update_attribute_offset( m_va, (slot)m_loc_next_position, base_offset );
     158                dev->update_attribute_offset( m_va, (slot)m_loc_next_normal, base_offset + sizeof( vec3 ) );
     159                if ( m_has_tangent && m_loc_next_tangent != -1 )
     160                {
     161                        dev->update_attribute_offset( m_va, (slot)m_loc_next_tangent, base_offset + 2*sizeof( vec3 ) );
     162                }
    155163                m_gpu_next_frame = m_next_frame;
    156164        }
     
    166174                        m_loc_next_tangent  = a_program->get_attribute( "nv_next_tangent" )->get_location();
    167175
    168                 vertex_buffer* vb = m_va->find_buffer( slot::POSITION );
    169                 m_va->add_vertex_buffer( m_loc_next_position, vb, FLOAT, 3, 0, m_vsize, false );
    170                 m_va->add_vertex_buffer( m_loc_next_normal,   vb, FLOAT, 3, sizeof( vec3 ), m_vsize, false );
     176                device* dev = m_context->get_device();
     177                dev->add_vertex_buffer( m_va, (slot)m_loc_next_position, m_pbuffer, FLOAT, 3, 0, m_vsize, false );
     178                dev->add_vertex_buffer( m_va, (slot)m_loc_next_normal,   m_pbuffer, FLOAT, 3, sizeof( vec3 ), m_vsize, false );
    171179                if ( m_has_tangent )
    172                         m_va->add_vertex_buffer( m_loc_next_tangent, vb, FLOAT, 4, 2*sizeof( vec3 ), m_vsize, false );
     180                        dev->add_vertex_buffer( m_va, (slot)m_loc_next_tangent, m_pbuffer, FLOAT, 4, 2*sizeof( vec3 ), m_vsize, false );
    173181        }
    174182        keyframed_mesh::update( a_program );
     
    176184
    177185nv::keyframed_mesh_cpu::keyframed_mesh_cpu( context* a_context, const mesh_data* a_data, const mesh_nodes_data* a_tag_map )
    178         : keyframed_mesh( a_data, a_tag_map )
    179         , m_context( a_context )
    180 {
    181         m_va = m_context->get_device()->create_vertex_array();
    182         m_vb = m_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_vertex_count * m_vsize, (void*)m_vchannel->data );
    183         m_va->add_vertex_buffers( m_vb, m_vchannel );
    184 
    185         nv::vertex_buffer* vb = m_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_vertex_count * sizeof( nv::vec2 ), (void*)m_mesh_data->get_channel<vertex_t>()->data );
    186         m_va->add_vertex_buffers( vb, m_mesh_data->get_channel<vertex_t>() );
    187 
    188         nv::index_buffer* ib = m_context->get_device()->create_index_buffer( nv::STATIC_DRAW, m_mesh_data->get_index_channel()->size(), (void*)m_mesh_data->get_index_channel()->data );
    189         m_va->set_index_buffer( ib, m_mesh_data->get_index_channel()->desc.slots[0].etype, true );
     186        : keyframed_mesh( a_context, a_data, a_tag_map )
     187{
     188        m_va      = m_context->get_device()->create_vertex_array();
     189        m_pbuffer = m_context->get_device()->create_buffer( VERTEX_BUFFER, STATIC_DRAW, m_vertex_count * m_vsize, (void*)m_vchannel->data );
     190        m_context->get_device()->add_vertex_buffers( m_va, m_pbuffer, m_vchannel );
     191
     192        buffer  vb = m_context->get_device()->create_buffer( VERTEX_BUFFER, STATIC_DRAW, m_vertex_count * sizeof( vec2 ), (void*)m_mesh_data->get_channel<vertex_t>()->data );
     193        m_context->get_device()->add_vertex_buffers( m_va, vb, m_mesh_data->get_channel<vertex_t>() );
     194
     195        buffer  ib = m_context->get_device()->create_buffer( INDEX_BUFFER, STATIC_DRAW, m_mesh_data->get_index_channel()->size(), (void*)m_mesh_data->get_index_channel()->data );
     196
     197        m_context->get_device()->set_index_buffer( m_va, ib, m_mesh_data->get_index_channel()->desc.slots[0].etype, true );
    190198
    191199        m_data = new uint8[ m_vertex_count * m_vsize ];
     
    224232        }
    225233
    226         m_context->update( m_vb, m_data, 0, m_vertex_count * m_vsize );
     234        m_context->update( m_pbuffer, m_data, 0, m_vertex_count * m_vsize );
    227235}
    228236
  • trunk/src/gfx/mesh_creator.cc

    r295 r302  
    430430        {
    431431                mesh_raw_channel* old = m_data->m_channels[c];
    432                 size_t frame_count = ( old->is_index() ? 1 : old->count / size );
     432                size_t frame_count = ( old->get_buffer_type() == INDEX_BUFFER ? 1 : old->count / size );
    433433                m_data->m_channels[c] = append_channels( old, other->m_channels[c], frame_count );
    434434                NV_ASSERT( m_data->m_channels[c], "Merge problem!" );
    435                 if ( old->is_index() )
     435                if ( old->get_buffer_type() == INDEX_BUFFER )
    436436                {
    437437                        switch ( old->desc.slots[0].etype )
  • trunk/src/gfx/skeletal_mesh.cc

    r299 r302  
    1212
    1313nv::skeletal_mesh_cpu::skeletal_mesh_cpu( context* a_context, const mesh_data* a_mesh_data, const mesh_nodes_data* bones )
    14         : skeletal_mesh()
    15         , m_context( a_context )
     14        : skeletal_mesh( a_context )
    1615        , m_data( a_mesh_data )
    1716{
     
    2726        m_vtx_data  = a_mesh_data->get_channel_data<md5_vtx_pntiw>();
    2827        m_indices   = a_mesh_data->get_count();
    29         m_va        = a_context->get_device()->create_vertex_array( a_mesh_data, nv::STREAM_DRAW );
     28        m_va        = a_context->get_device()->create_vertex_array( a_mesh_data,
     29STREAM_DRAW );
     30        m_pbuffer   = a_context->get_device()->find_buffer( m_va, slot::POSITION );
    3031}
    3132
     
    6364                }
    6465
    65                 vertex_buffer* vb = m_va->find_buffer( nv::slot::POSITION );
    66                 m_context->update( vb, m_pntdata.data(), 0, m_pntdata.raw_size() );
     66                m_context->update( m_pbuffer, m_pntdata.data(), 0, m_pntdata.raw_size() );
    6767        }
    6868}
     
    8080                skeleton[i] = m_node_data->get_node(i)->data->get_transform( frame_num );
    8181        }
    82 }
    83 
    84 
    85 
    86 nv::skeletal_mesh_cpu::~skeletal_mesh_cpu()
    87 {
    88         delete m_va;
    8982}
    9083
     
    200193
    201194nv::skeletal_mesh_gpu::skeletal_mesh_gpu( context* a_context, const mesh_data* a_mesh, const mesh_nodes_data* a_bone_data )
    202         : skeletal_mesh(), m_bone_data( a_bone_data ), m_transform( nullptr )
     195        : skeletal_mesh( a_context ), m_bone_data( a_bone_data ), m_transform( nullptr )
    203196{
    204197        m_va          = a_context->get_device()->create_vertex_array( a_mesh, nv::STATIC_DRAW );
  • 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}
  • trunk/src/gui/gui_renderer.cc

    r301 r302  
    2121        {
    2222                set_color( color );
    23                 vtx[0].coord = coorda;
    24                 vtx[1].coord = nv::ivec2( coorda.x, coordb.y );
    25                 vtx[2].coord = coordb;
    26                 vtx[3].coord = coordb;
    27                 vtx[4].coord = nv::ivec2( coordb.x, coorda.y );
    28                 vtx[5].coord = coorda;
     23                vtx[0].position = coorda;
     24                vtx[1].position = nv::ivec2( coorda.x, coordb.y );
     25                vtx[2].position = coordb;
     26                vtx[3].position = coordb;
     27                vtx[4].position = nv::ivec2( coordb.x, coorda.y );
     28                vtx[5].position = coorda;
    2929        }
    3030        gui_quad( const nv::ivec2& coorda, const nv::ivec2& coordb, const nv::vec4& color, const nv::vec2& tcoorda, const nv::vec2& tcoordb )
    3131        {
    3232                set_color( color );
    33                 vtx[0].coord = coorda;
    34                 vtx[1].coord = nv::ivec2( coorda.x, coordb.y );
    35                 vtx[2].coord = coordb;
    36                 vtx[3].coord = coordb;
    37                 vtx[4].coord = nv::ivec2( coordb.x, coorda.y );
    38                 vtx[5].coord = coorda;
    39                 vtx[0].tcoord = tcoorda;
    40                 vtx[1].tcoord = nv::vec2( tcoorda.x, tcoordb.y );
    41                 vtx[2].tcoord = tcoordb;
    42                 vtx[3].tcoord = tcoordb;
    43                 vtx[4].tcoord = nv::vec2( tcoordb.x, tcoorda.y );
    44                 vtx[5].tcoord = tcoorda;
     33                vtx[0].position = coorda;
     34                vtx[1].position = nv::ivec2( coorda.x, coordb.y );
     35                vtx[2].position = coordb;
     36                vtx[3].position = coordb;
     37                vtx[4].position = nv::ivec2( coordb.x, coorda.y );
     38                vtx[5].position = coorda;
     39                vtx[0].texcoord = tcoorda;
     40                vtx[1].texcoord = nv::vec2( tcoorda.x, tcoordb.y );
     41                vtx[2].texcoord = tcoordb;
     42                vtx[3].texcoord = tcoordb;
     43                vtx[4].texcoord = nv::vec2( tcoordb.x, tcoorda.y );
     44                vtx[5].texcoord = tcoorda;
    4545        }
    4646        gui_quad( const nv::ivec2& coorda, const nv::ivec2& coordb, const nv::ivec2& coordc, const nv::ivec2& coordd, const nv::vec4& color )
    4747        {
    4848                set_color( color );
    49                 vtx[0].coord = coorda;
    50                 vtx[1].coord = coordb;
    51                 vtx[2].coord = coordc;
    52                 vtx[3].coord = coordc;
    53                 vtx[4].coord = coordd;
    54                 vtx[5].coord = coorda;
     49                vtx[0].position = coorda;
     50                vtx[1].position = coordb;
     51                vtx[2].position = coordc;
     52                vtx[3].position = coordc;
     53                vtx[4].position = coordd;
     54                vtx[5].position = coorda;
    5555        }
    5656        inline void set_color( const nv::vec4& color )
     
    6666public:
    6767        screen_render_data( context* actx, size_t initial_size )
    68                 : buffer( actx, nv::DYNAMIC_DRAW, initial_size ), varray( nullptr ), shader(nullptr)
     68                : buffer( actx, VERTEX_BUFFER, DYNAMIC_DRAW, initial_size ), ctx( actx ), varray(), shader(nullptr)
    6969        {
    7070
     
    7373        {
    7474                delete shader;
    75                 delete varray;
     75                ctx->get_device()->release( varray );
    7676        }
    7777
    7878        nv::sliced_buffer<gui_quad> buffer;
     79        nv::context*      ctx;
    7980        nv::texture       tex;
    80         nv::vertex_array* varray;
     81        nv::vertex_array  varray;
    8182        nv::program*      shader;
    8283};
     
    110111        m_render_data = sr;
    111112        // ** EXTREMELY TEMPORARY!
    112         sr->varray     = m_window->get_device()->create_vertex_array();
    113113        sr->shader     = m_window->get_device()->create_program( nv::slurp( shader_path + ".vert" ), nv::slurp( shader_path + ".frag" ) );
    114114        m_scene_state.get_camera().set_ortho( 0.0f, float( m_window->get_width() ), float( m_window->get_height() ), 0.0f );
    115115
    116         vertex_buffer* vb = (vertex_buffer*)sr->buffer.get_buffer();
    117         sr->varray->add_vertex_buffer( slot::POSITION, vb, nv::INT,   2, 0, sizeof( vertex ), false );
    118         sr->varray->add_vertex_buffer( slot::TEXCOORD, vb, nv::FLOAT, 2, offset_of( &vertex::tcoord ), sizeof( vertex ), false );
    119         sr->varray->add_vertex_buffer( slot::COLOR,    vb, nv::FLOAT, 4, offset_of( &vertex::color ), sizeof( vertex ), false );
     116        sr->varray     = m_window->get_device()->create_vertex_array();
     117        buffer vb      = sr->buffer.get_buffer();
     118        m_window->get_device()->add_vertex_buffers< vertex >( sr->varray, vb );
    120119
    121120        nv::sampler sampler( nv::sampler::LINEAR, nv::sampler::CLAMP_TO_EDGE );
     
    267266        if ( sr->buffer.commit() )
    268267        {
    269                 nv::vertex_buffer* vb = (nv::vertex_buffer*)sr->buffer.get_buffer();
    270                 sr->varray->update_vertex_buffer( nv::slot::POSITION, vb, false );
    271                 sr->varray->update_vertex_buffer( nv::slot::TEXCOORD, vb, false );
    272                 sr->varray->update_vertex_buffer( nv::slot::COLOR,    vb, false );
     268                buffer vb = sr->buffer.get_buffer();
     269                m_context->get_device()->replace_vertex_buffer( sr->varray, vb, false );
    273270        }
    274271        m_context->bind( sr->tex, TEX_DIFFUSE );
     
    284281        if ( m_render_data )
    285282        {
    286                 m_context->get_device()->release_texture( ((screen_render_data*)m_render_data)->tex );
     283                m_context->get_device()->release( ((screen_render_data*)m_render_data)->tex );
    287284                delete m_render_data;
    288285        }
Note: See TracChangeset for help on using the changeset viewer.