Changeset 313


Ignore:
Timestamp:
08/18/14 23:25:46 (11 years ago)
Author:
epyon
Message:
  • cleanup of context and device interfaces
  • create_vertex_array moved to context (as it's context bound)
  • added partial framebuffer functions to context
Location:
trunk
Files:
13 edited

Legend:

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

    r303 r313  
    2626        {
    2727        public:
    28                 debug_data( device* a_device );
     28                debug_data( context* a_context );
    2929                void update();
    3030                void reset();
     
    3737                ~debug_data();
    3838        private:
    39                 device*                  m_device;
     39                context*                 m_context;
    4040                program                  m_program;
    4141                vertex_array             m_va;
  • trunk/nv/gfx/skeletal_mesh.hh

    r303 r313  
    2727                ~skeletal_mesh()
    2828                {
    29                         m_context->get_device()->release( m_va );
     29                        m_context->release( m_va );
    3030                }
    3131        protected:
  • trunk/nv/gl/gl_context.hh

    r303 r313  
    1717namespace nv
    1818{
     19        struct gl_framebuffer_info : public framebuffer_info
     20        {
     21                unsigned glid;
     22        };
     23
    1924        class gl_context : public context
    2025        {
     
    2328        public:
    2429                ~gl_context();
     30
     31                virtual vertex_array create_vertex_array();
     32                virtual framebuffer create_framebuffer();
     33                virtual void release( vertex_array va );
     34                virtual void release( framebuffer f );
     35                virtual const vertex_array_info* get_vertex_array_info( vertex_array va ) const;
     36                virtual const framebuffer_info* get_framebuffer_info( framebuffer f ) const;
     37
    2538                virtual void bind( texture t, texture_slot slot );
    26                 virtual void bind( program p );
    27                 virtual void bind( buffer b );
    28                 virtual void bind( vertex_array va );
    29                 virtual void unbind( program p );
    30                 virtual void unbind( buffer b );
    31                 virtual void unbind( vertex_array va );
    3239
    3340                virtual void update( texture t, void* data );
     
    4350
    4451        protected:
     52                void bind( program p );
     53//              void bind( buffer b );
     54                void bind( vertex_array va );
     55                void bind( framebuffer va );
     56                void unbind( program p );
     57//              void unbind( buffer b );
     58                void unbind( vertex_array va );
     59                void unbind( framebuffer va );
     60
    4561                void force_apply_render_state( const render_state& state );
    4662                void force_apply_stencil_face( unsigned face, const stencil_test_face& stencil );
     63                // TODO: remove
     64                virtual vertex_array_info* get_vertex_array_info_mutable( vertex_array );
    4765        private:
    4866                void apply_stencil_test( const stencil_test& stencil );
     
    5775                void apply_polygon_mode( const polygon_mode& mode );
    5876                void enable( unsigned int what, bool value );
    59 
    6077        private:
    6178                vec4  m_clear_color;
    6279                float m_clear_depth;
    6380                int   m_clear_stencil;
     81
     82                entity_store< vertex_array_info, vertex_array >  m_vertex_arrays;
     83                entity_store< gl_framebuffer_info, framebuffer > m_framebuffers;
    6484        };
    6585
  • trunk/nv/gl/gl_device.hh

    r303 r313  
    4545
    4646                virtual program create_program( const string& vs_source, const string& fs_source );
    47                 virtual vertex_array create_vertex_array();
    4847                virtual buffer create_buffer( buffer_type type, buffer_hint hint, size_t size, const void* source = nullptr );
    4948                virtual texture create_texture( ivec2 size, image_format aformat, sampler asampler, void* data = nullptr );
     
    5352                virtual void release( buffer b );
    5453                virtual void release( texture t );
    55                 virtual void release( vertex_array va );
    5654                virtual void release( program p );
    5755                virtual const texture_info* get_texture_info( texture t ) const;
    5856                virtual const buffer_info* get_buffer_info( buffer t ) const;
    59                 virtual const vertex_array_info* get_vertex_array_info( vertex_array va ) const;
    6057
    6158                virtual int get_attribute_location( program p, const string& name, bool fatal = true ) const;
     
    6562                uniform_base* get_uniform( program p, const string& name, bool fatal = true ) const;
    6663
    67                 // TODO: remove
    68                 virtual vertex_array_info* get_vertex_array_info_mutable( vertex_array );
    6964       
    7065        private:
     
    7570                bool compile( uint32 sh_type, const std::string& shader_code, unsigned& glid );
    7671
    77 
    78 
    7972                const void* m_info;
    80                 entity_store< gl_texture_info, texture >        m_textures;
    81                 entity_store< gl_buffer_info,  buffer >         m_buffers;
    82                 entity_store< vertex_array_info, vertex_array > m_vertex_arrays;
    83                 entity_store< gl_program_info, program >        m_programs;
     73                entity_store< gl_texture_info, texture >         m_textures;
     74                entity_store< gl_buffer_info,  buffer >          m_buffers;
     75                entity_store< gl_program_info, program >         m_programs;
    8476        };
    8577
  • trunk/nv/interface/context.hh

    r303 r313  
    2222namespace nv
    2323{
     24        struct vertex_array_tag {};
     25        struct framebuffer_tag {};
     26        typedef handle< uint32, 16, 16, vertex_array_tag > vertex_array;
     27        typedef handle< uint32, 16, 16, framebuffer_tag >  framebuffer;
     28
    2429        enum primitive
    2530        {
     
    4550        };
    4651
     52        struct vertex_array_info
     53        {
     54                static const int MAX_ATTRIBUTES = 24;
     55
     56                uint32        count;
     57                buffer        index;
     58                bool          index_owner;
     59                datatype      index_type;
     60                vertex_buffer_attribute attr[MAX_ATTRIBUTES];
     61        };
     62
     63        struct framebuffer_info
     64        {
     65                texture color_attachments[8];
     66                uint32  color_attachment_count;
     67                texture depth_attachment;
     68                texture depth_stencil_attachment;
     69        };
     70
    4771        class context
    4872        {
     
    5276                        m_device = a_device;
    5377                }
     78
     79                virtual vertex_array create_vertex_array() = 0;
     80                virtual framebuffer create_framebuffer() = 0;
     81                virtual void release( vertex_array ) = 0;
     82                virtual void release( framebuffer ) = 0;
     83
     84                virtual const vertex_array_info* get_vertex_array_info( vertex_array ) const = 0;
     85                virtual const framebuffer_info* get_framebuffer_info( framebuffer ) const = 0;
     86
    5487                virtual void bind( texture, texture_slot ) = 0;
    55                 virtual void bind( buffer ) = 0;
    56                 virtual void bind( program ) = 0;
    57                 virtual void bind( vertex_array ) = 0;
    58                 virtual void unbind( buffer ) = 0;
    59                 virtual void unbind( program ) = 0;
    6088                virtual void update( texture, void* ) = 0;
    6189                virtual void update( buffer, const void*, size_t /*offset*/, size_t /*size*/ ) = 0;
     
    88116                {
    89117                }
     118
     119                template < typename VTX, slot SLOT >
     120                void add_vertex_buffer_impl( vertex_array, buffer, const std::false_type& )
     121                {
     122                }
     123
     124                template < typename VTX, slot SLOT >
     125                void add_vertex_buffer_impl( vertex_array va, buffer vb, const std::true_type& )
     126                {
     127                        typedef vertex_slot_info< VTX, SLOT > vinfo;
     128                        typedef datatype_traits< typename vinfo::value_type > dt_traits;
     129                        add_vertex_buffer( va, SLOT, vb, type_to_enum< dt_traits::base_type >::type, dt_traits::size, vinfo::offset, sizeof( VTX ), false );
     130                }
     131
     132                template < typename VTX, slot SLOT >
     133                void add_vertex_buffer( vertex_array va, buffer vb )
     134                {
     135                        add_vertex_buffer_impl< VTX, SLOT >( va, vb, std::integral_constant< bool, vertex_has_slot< VTX, SLOT >::value >() );
     136                }
     137
     138                template < typename VTX >
     139                void add_vertex_buffers( vertex_array va, buffer vb )
     140                {
     141                        add_vertex_buffer< VTX, slot::POSITION >  ( va, vb );
     142                        add_vertex_buffer< VTX, slot::TEXCOORD >  ( va, vb );
     143                        add_vertex_buffer< VTX, slot::NORMAL   >  ( va, vb );
     144                        add_vertex_buffer< VTX, slot::TANGENT >   ( va, vb );
     145                        add_vertex_buffer< VTX, slot::BONEINDEX > ( va, vb );
     146                        add_vertex_buffer< VTX, slot::BONEWEIGHT >( va, vb );
     147                        add_vertex_buffer< VTX, slot::COLOR >     ( va, vb );
     148                }
     149
     150                void add_vertex_buffers( vertex_array va, buffer buf, const mesh_raw_channel* channel )
     151                {
     152                        for ( uint32 s = 0; s < channel->desc.count; ++s )
     153                        {
     154                                const vertex_descriptor_slot& slot = channel->desc.slots[s];
     155                                const datatype_info& info = get_datatype_info(slot.etype);
     156                                add_vertex_buffer( va, slot.vslot, buf, info.base , info.elements, slot.offset, channel->desc.size, false );
     157                        }
     158                }
     159
     160                template < typename VTX >
     161                vertex_array create_vertex_array( const VTX* v, size_t count, buffer_hint hint )
     162                {
     163                        // TODO: vb will not be owned or freed!
     164                        vertex_array va = create_vertex_array();
     165                        buffer       vb = m_device->create_buffer( VERTEX_BUFFER, hint, count * sizeof( VTX ), v );
     166                        add_vertex_buffers< VTX >( va, vb );
     167                        return va;
     168                }
     169
     170                template < typename VTX >
     171                vertex_array create_vertex_array( const std::vector< VTX >& data, buffer_hint hint )
     172                {
     173                        return create_vertex_array( data.data(), data.size(), hint );
     174                }
     175
     176                template < typename VTX, typename IDX >
     177                vertex_array create_vertex_array( const VTX* v, size_t vcount, const IDX* i, size_t icount, buffer_hint hint )
     178                {
     179                        vertex_array va = create_vertex_array( v, vcount, hint );
     180                        buffer       ib = create_buffer( INDEX_BUFFER, hint, icount * sizeof( IDX ), i );
     181                        va->set_index_buffer( ib, type_to_enum< IDX >::type, true );
     182                        return va;
     183                }
     184
     185                template < typename VTX, typename IDX >
     186                vertex_array create_vertex_array( const std::vector< VTX >& data, const std::vector< IDX >& idata, buffer_hint hint )
     187                {
     188                        return create_vertex_array( data.data(), data.size(), idata.data(), idata.size(), hint );
     189                }
     190
     191                void replace_vertex_buffer( vertex_array va, buffer vb, bool owner )
     192                {
     193                        vertex_array_info* info = get_vertex_array_info_mutable( va );
     194                        if ( info )
     195                        {
     196                                for ( uint32 i = 0; i < info->count; ++i )
     197                                {
     198                                        vertex_buffer_attribute& vba = info->attr[i];
     199                                        if ( vba.owner ) m_device->release( vba.vbuffer );
     200                                        vba.vbuffer = vb;
     201                                        vba.owner   = owner;
     202                                }
     203                        }
     204                }
     205
     206                void replace_vertex_buffer( vertex_array va, buffer vb, slot location, bool owner )
     207                {
     208                        vertex_array_info* info = get_vertex_array_info_mutable( va );
     209                        if ( info )
     210                        {
     211                                for ( uint32 i = 0; i < info->count; ++i )
     212                                {
     213                                        if ( info->attr[i].location == location )
     214                                        {
     215                                                vertex_buffer_attribute& vba = info->attr[i];
     216                                                if ( vba.owner ) m_device->release( vba.vbuffer );
     217                                                vba.vbuffer = vb;
     218                                                vba.owner   = owner;
     219                                        }
     220                                }
     221                        }
     222                }
     223
     224                void update_attribute_offset( vertex_array va, slot location, size_t offset )
     225                {
     226                        vertex_array_info* info = get_vertex_array_info_mutable( va );
     227                        if ( info )
     228                        {
     229                                for ( uint32 i = 0; i < info->count; ++i )
     230                                {
     231                                        if ( info->attr[i].location == location )
     232                                        {
     233                                                info->attr[i].offset = offset;
     234                                        }
     235                                }
     236                        }
     237                }
     238
     239                void set_index_buffer( vertex_array va, buffer b, datatype datatype, bool owner )
     240                {
     241                        vertex_array_info* info = get_vertex_array_info_mutable( va );
     242                        if ( info )
     243                        {
     244                                if ( m_device->get_buffer_info( b )->type == INDEX_BUFFER )
     245                                {
     246                                        if (info->index.is_valid() && info->index_owner) m_device->release( info->index );
     247
     248                                        info->index       = b;
     249                                        info->index_owner = owner;
     250                                        info->index_type  = datatype;
     251                                }
     252                        }
     253                }
     254
     255                virtual 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 )
     256                {
     257                        vertex_array_info* info = get_vertex_array_info_mutable( va );
     258                        if ( info )
     259                        {
     260                                NV_ASSERT( info->count < vertex_array_info::MAX_ATTRIBUTES, "MAX_ATTRIBUTES reached!" );
     261                                vertex_buffer_attribute& p = info->attr[ info->count ];
     262                                p.vbuffer    = buf;
     263                                p.dtype      = datatype;
     264                                p.components = components;
     265                                p.offset     = offset;
     266                                p.stride     = stride;
     267                                p.owner      = owner;
     268                                p.location   = location;
     269                                info->count++;
     270                        }
     271                }
     272
     273                buffer find_buffer( vertex_array va, slot location )
     274                {
     275                        const vertex_array_info* info = get_vertex_array_info( va );
     276                        if ( info )
     277                        {
     278                                for ( uint32 i = 0; i < info->count; ++i )
     279                                {
     280                                        if ( info->attr[i].location == location )
     281                                        {
     282                                                return info->attr[i].vbuffer;
     283                                        }
     284                                }
     285                        }
     286                        return buffer();
     287                }
     288
     289                // TODO: HINTS ARE DIFFERENT!
     290                vertex_array create_vertex_array( const mesh_data* data, buffer_hint hint )
     291                {
     292                        vertex_array  va = create_vertex_array();
     293                        const std::vector< mesh_raw_channel* >& channels = data->get_raw_channels();
     294                        for ( uint32 ch = 0; ch < channels.size(); ++ch )
     295                        {
     296                                const mesh_raw_channel* channel = channels[ch];
     297                                if ( channel->count > 0 )
     298                                {
     299                                        buffer_type type = channel->get_buffer_type();
     300                                        buffer b = m_device->create_buffer( type, hint, channel->size(), channel->data );
     301                                        // TODO: no if switch
     302                                        if ( type == INDEX_BUFFER )
     303                                        {
     304                                                set_index_buffer( va, b, channel->desc.slots[0].etype, true );
     305                                        }
     306                                        else
     307                                        {
     308                                                add_vertex_buffers( va, b, channel );
     309                                        }
     310                                }
     311                        }
     312                        return va;
     313                }
     314
    90315        protected:
     316                // TODO: remove
     317                virtual vertex_array_info* get_vertex_array_info_mutable( vertex_array ) = 0;
    91318
    92319                device*      m_device;
  • trunk/nv/interface/device.hh

    r303 r313  
    5252
    5353        struct texture_tag {};
    54         struct vertex_array_tag {};
    5554        struct buffer_tag {};
    5655        struct program_tag {};
    5756        typedef handle< uint32, 16, 16, buffer_tag >       buffer;
    5857        typedef handle< uint32, 16, 16, texture_tag >      texture;
    59         typedef handle< uint32, 16, 16, vertex_array_tag > vertex_array;
    6058        typedef handle< uint32, 16, 16, program_tag >      program;
    6159
     
    118116        };
    119117
    120         struct vertex_array_info
    121         {
    122                 static const int MAX_ATTRIBUTES = 24;
    123 
    124                 uint32        count;
    125                 buffer        index;
    126                 bool          index_owner;
    127                 datatype      index_type;
    128                 vertex_buffer_attribute attr[MAX_ATTRIBUTES];
    129         };
    130 
    131118        struct program_info
    132119        {
     
    150137                virtual image_data* create_image_data( const std::string& filename ) = 0; // temporary
    151138                virtual texture create_texture( ivec2 size, image_format aformat, sampler asampler, void* data = nullptr ) = 0;
    152                 virtual vertex_array create_vertex_array() = 0;
    153139                virtual void release( texture ) = 0;
    154140                virtual void release( buffer ) = 0;
    155                 virtual void release( vertex_array ) = 0;
    156141                virtual void release( program ) = 0;
    157142                virtual const texture_info* get_texture_info( texture ) const = 0;
    158143                virtual const buffer_info* get_buffer_info( buffer ) const = 0;
    159                 virtual const vertex_array_info* get_vertex_array_info( vertex_array ) const = 0;
    160144                virtual uint32 get_ticks() = 0;
    161145                virtual void delay( uint32 ms ) = 0;
     
    164148                {
    165149                        return create_texture( data->get_size(), data->get_format(), asampler, (void*)data->get_data() );
    166                 }
    167 
    168                 template < typename VTX, slot SLOT >
    169                 void add_vertex_buffer_impl( vertex_array, buffer, const std::false_type& )
    170                 {
    171                 }
    172 
    173                 template < typename VTX, slot SLOT >
    174                 void add_vertex_buffer_impl( vertex_array va, buffer vb, const std::true_type& )
    175                 {
    176                         typedef vertex_slot_info< VTX, SLOT > vinfo;
    177                         typedef datatype_traits< typename vinfo::value_type > dt_traits;
    178                         add_vertex_buffer( va, SLOT, vb, type_to_enum< dt_traits::base_type >::type, dt_traits::size, vinfo::offset, sizeof( VTX ), false );
    179                 }
    180 
    181                 template < typename VTX, slot SLOT >
    182                 void add_vertex_buffer( vertex_array va, buffer vb )
    183                 {
    184                         add_vertex_buffer_impl< VTX, SLOT >( va, vb, std::integral_constant< bool, vertex_has_slot< VTX, SLOT >::value >() );
    185                 }
    186 
    187                 template < typename VTX >
    188                 void add_vertex_buffers( vertex_array va, buffer vb )
    189                 {
    190                         add_vertex_buffer< VTX, slot::POSITION >  ( va, vb );
    191                         add_vertex_buffer< VTX, slot::TEXCOORD >  ( va, vb );
    192                         add_vertex_buffer< VTX, slot::NORMAL   >  ( va, vb );
    193                         add_vertex_buffer< VTX, slot::TANGENT >   ( va, vb );
    194                         add_vertex_buffer< VTX, slot::BONEINDEX > ( va, vb );
    195                         add_vertex_buffer< VTX, slot::BONEWEIGHT >( va, vb );
    196                         add_vertex_buffer< VTX, slot::COLOR >     ( va, vb );
    197                 }
    198 
    199                 void add_vertex_buffers( vertex_array va, buffer buf, const mesh_raw_channel* channel )
    200                 {
    201                         for ( uint32 s = 0; s < channel->desc.count; ++s )
    202                         {
    203                                 const vertex_descriptor_slot& slot = channel->desc.slots[s];
    204                                 const datatype_info& info = get_datatype_info(slot.etype);
    205                                 add_vertex_buffer( va, slot.vslot, buf, info.base , info.elements, slot.offset, channel->desc.size, false );
    206                         }
    207                 }
    208 
    209                 template < typename VTX >
    210                 vertex_array create_vertex_array( const VTX* v, size_t count, buffer_hint hint )
    211                 {
    212                         // TODO: vb will not be owned or freed!
    213                         vertex_array va = create_vertex_array();
    214                         buffer       vb = create_buffer( VERTEX_BUFFER, hint, count * sizeof( VTX ), v );
    215                         add_vertex_buffers< VTX >( va, vb );
    216                         return va;
    217                 }
    218 
    219                 template < typename VTX >
    220                 vertex_array create_vertex_array( const std::vector< VTX >& data, buffer_hint hint )
    221                 {
    222                         return create_vertex_array( data.data(), data.size(), hint );
    223                 }
    224 
    225                 template < typename VTX, typename IDX >
    226                 vertex_array create_vertex_array( const VTX* v, size_t vcount, const IDX* i, size_t icount, buffer_hint hint )
    227                 {
    228                         vertex_array va = create_vertex_array( v, vcount, hint );
    229                         buffer       ib = create_buffer( INDEX_BUFFER, hint, icount * sizeof( IDX ), i );
    230                         va->set_index_buffer( ib, type_to_enum< IDX >::type, true );
    231                         return va;
    232                 }
    233 
    234                 template < typename VTX, typename IDX >
    235                 vertex_array create_vertex_array( const std::vector< VTX >& data, const std::vector< IDX >& idata, buffer_hint hint )
    236                 {
    237                         return create_vertex_array( data.data(), data.size(), idata.data(), idata.size(), hint );
    238                 }
    239 
    240                 void replace_vertex_buffer( vertex_array va, buffer vb, bool owner )
    241                 {
    242                         vertex_array_info* info = get_vertex_array_info_mutable( va );
    243                         if ( info )
    244                         {
    245                                 for ( uint32 i = 0; i < info->count; ++i )
    246                                 {
    247                                         vertex_buffer_attribute& vba = info->attr[i];
    248                                         if ( vba.owner ) release( vba.vbuffer );
    249                                         vba.vbuffer = vb;
    250                                         vba.owner   = owner;
    251                                 }
    252                         }
    253                 }
    254 
    255                 void replace_vertex_buffer( vertex_array va, buffer vb, slot location, bool owner )
    256                 {
    257                         vertex_array_info* info = get_vertex_array_info_mutable( va );
    258                         if ( info )
    259                         {
    260                                 for ( uint32 i = 0; i < info->count; ++i )
    261                                 {
    262                                         if ( info->attr[i].location == location )
    263                                         {
    264                                                 vertex_buffer_attribute& vba = info->attr[i];
    265                                                 if ( vba.owner ) release( vba.vbuffer );
    266                                                 vba.vbuffer = vb;
    267                                                 vba.owner   = owner;
    268                                         }
    269                                 }
    270                         }
    271                 }
    272 
    273                 void update_attribute_offset( vertex_array va, slot location, size_t offset )
    274                 {
    275                         vertex_array_info* info = get_vertex_array_info_mutable( va );
    276                         if ( info )
    277                         {
    278                                 for ( uint32 i = 0; i < info->count; ++i )
    279                                 {
    280                                         if ( info->attr[i].location == location )
    281                                         {
    282                                                 info->attr[i].offset = offset;
    283                                         }
    284                                 }
    285                         }
    286                 }
    287 
    288                 void set_index_buffer( vertex_array va, buffer b, datatype datatype, bool owner )
    289                 {
    290                         vertex_array_info* info = get_vertex_array_info_mutable( va );
    291                         if ( info )
    292                         {
    293                                 if ( get_buffer_info( b )->type == INDEX_BUFFER )
    294                                 {
    295                                         if (info->index.is_valid() && info->index_owner) release( info->index );
    296 
    297                                         info->index       = b;
    298                                         info->index_owner = owner;
    299                                         info->index_type  = datatype;
    300                                 }
    301                         }
    302                 }
    303 
    304                 virtual 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 )
    305                 {
    306                         vertex_array_info* info = get_vertex_array_info_mutable( va );
    307                         if ( info )
    308                         {
    309                                 NV_ASSERT( info->count < vertex_array_info::MAX_ATTRIBUTES, "MAX_ATTRIBUTES reached!" );
    310                                 vertex_buffer_attribute& p = info->attr[ info->count ];
    311                                 p.vbuffer    = buf;
    312                                 p.dtype      = datatype;
    313                                 p.components = components;
    314                                 p.offset     = offset;
    315                                 p.stride     = stride;
    316                                 p.owner      = owner;
    317                                 p.location   = location;
    318                                 info->count++;
    319                         }
    320                 }
    321 
    322                 buffer find_buffer( vertex_array va, slot location )
    323                 {
    324                         const vertex_array_info* info = get_vertex_array_info( va );
    325                         if ( info )
    326                         {
    327                                 for ( uint32 i = 0; i < info->count; ++i )
    328                                 {
    329                                         if ( info->attr[i].location == location )
    330                                         {
    331                                                 return info->attr[i].vbuffer;
    332                                         }
    333                                 }
    334                         }
    335                         return buffer();
    336                 }
    337 
    338                 // TODO: HINTS ARE DIFFERENT!
    339                 vertex_array create_vertex_array( const mesh_data* data, buffer_hint hint )
    340                 {
    341                         vertex_array  va = create_vertex_array();
    342                         const std::vector< mesh_raw_channel* >& channels = data->get_raw_channels();
    343                         for ( uint32 ch = 0; ch < channels.size(); ++ch )
    344                         {
    345                                 const mesh_raw_channel* channel = channels[ch];
    346                                 if ( channel->count > 0 )
    347                                 {
    348                                         buffer_type type = channel->get_buffer_type();
    349                                         buffer b = create_buffer( type, hint, channel->size(), channel->data );
    350                                         // TODO: no if switch
    351                                         if ( type == INDEX_BUFFER )
    352                                         {
    353                                                 set_index_buffer( va, b, channel->desc.slots[0].etype, true );
    354                                         }
    355                                         else
    356                                         {
    357                                                 add_vertex_buffers( va, b, channel );
    358                                         }
    359                                 }
    360                         }
    361                         return va;
    362150                }
    363151
     
    445233        protected:
    446234                virtual uniform_base* get_uniform( program p, const string& name, bool fatal = true ) const = 0;
    447 
    448                 // TODO: remove
    449                 virtual vertex_array_info* get_vertex_array_info_mutable( vertex_array ) = 0;
    450235
    451236                void initialize_engine_uniforms()
  • trunk/src/gfx/debug_draw.cc

    r303 r313  
    2828        "}\n";
    2929
    30 nv::debug_data::debug_data( device* a_device )
    31         : m_device( a_device ), m_program(), m_va()
     30nv::debug_data::debug_data( context* a_context )
     31        : m_context( a_context ), m_program(), m_va()
    3232{
    33         m_program = m_device->create_program( nv_debug_draw_vertex_shader, nv_debug_draw_fragment_shader );
     33        m_program = m_context->get_device()->create_program( nv_debug_draw_vertex_shader, nv_debug_draw_fragment_shader );
    3434}
    3535
    3636void nv::debug_data::update()
    3737{
    38         m_device->release( m_va );
    39         m_va = m_device->create_vertex_array( m_data, nv::STATIC_DRAW );
     38        m_context->release( m_va );
     39        m_va = m_context->create_vertex_array( m_data, nv::STATIC_DRAW );
    4040}
    4141
     
    7575nv::debug_data::~debug_data()
    7676{
    77         m_device->release( m_va );
    78         m_device->release( m_program );
     77        m_context->release( m_va );
     78        m_context->get_device()->release( m_program );
    7979}
  • trunk/src/gfx/keyframed_mesh.cc

    r304 r313  
    105105nv::keyframed_mesh::~keyframed_mesh()
    106106{
    107         m_context->get_device()->release( m_va );
     107        m_context->release( m_va );
    108108}
    109109
     
    131131        , m_gpu_next_frame( 0xFFFFFFFF )
    132132{
    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 );
     133        m_va      = a_context->create_vertex_array( a_data, STATIC_DRAW );
     134        m_pbuffer = a_context->find_buffer( m_va, slot::POSITION );
    135135}
    136136
     
    140140        if ( m_loc_next_position == -1 ) return;
    141141        animated_mesh::update( ms );
    142         device* dev = m_context->get_device();
    143142        if ( m_gpu_last_frame != m_last_frame )
    144143        {
    145144                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 ) );
     145                m_context->update_attribute_offset( m_va, slot::POSITION, base_offset );
     146                m_context->update_attribute_offset( m_va, slot::NORMAL,   base_offset + sizeof( vec3 ) );
    148147                if ( m_has_tangent && m_loc_next_tangent != -1 )
    149148                {
    150                         dev->update_attribute_offset( m_va, slot::TANGENT, base_offset + 2*sizeof( vec3 ) );
     149                        m_context->update_attribute_offset( m_va, slot::TANGENT, base_offset + 2*sizeof( vec3 ) );
    151150                }
    152151                m_gpu_last_frame = m_last_frame;
     
    155154        {
    156155                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 ) );
     156                m_context->update_attribute_offset( m_va, (slot)m_loc_next_position, base_offset );
     157                m_context->update_attribute_offset( m_va, (slot)m_loc_next_normal, base_offset + sizeof( vec3 ) );
    159158                if ( m_has_tangent && m_loc_next_tangent != -1 )
    160159                {
    161                         dev->update_attribute_offset( m_va, (slot)m_loc_next_tangent, base_offset + 2*sizeof( vec3 ) );
     160                        m_context->update_attribute_offset( m_va, (slot)m_loc_next_tangent, base_offset + 2*sizeof( vec3 ) );
    162161                }
    163162                m_gpu_next_frame = m_next_frame;
     
    175174                        m_loc_next_tangent  = dev->get_attribute_location( a_program, "nv_next_tangent" );
    176175
    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 );
     176                m_context->add_vertex_buffer( m_va, (slot)m_loc_next_position, m_pbuffer, FLOAT, 3, 0, m_vsize, false );
     177                m_context->add_vertex_buffer( m_va, (slot)m_loc_next_normal,   m_pbuffer, FLOAT, 3, sizeof( vec3 ), m_vsize, false );
    179178                if ( m_has_tangent )
    180                         dev->add_vertex_buffer( m_va, (slot)m_loc_next_tangent, m_pbuffer, FLOAT, 4, 2*sizeof( vec3 ), m_vsize, false );
     179                        m_context->add_vertex_buffer( m_va, (slot)m_loc_next_tangent, m_pbuffer, FLOAT, 4, 2*sizeof( vec3 ), m_vsize, false );
    181180        }
    182181        keyframed_mesh::update( a_program );
     
    186185        : keyframed_mesh( a_context, a_data, a_tag_map )
    187186{
    188         m_va      = m_context->get_device()->create_vertex_array();
     187        m_va      = m_context->create_vertex_array();
    189188        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 );
     189        m_context->add_vertex_buffers( m_va, m_pbuffer, m_vchannel );
    191190
    192191        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>() );
     192        m_context->add_vertex_buffers( m_va, vb, m_mesh_data->get_channel<vertex_t>() );
    194193
    195194        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 );
    196195
    197         m_context->get_device()->set_index_buffer( m_va, ib, m_mesh_data->get_index_channel()->desc.slots[0].etype, true );
     196        m_context->set_index_buffer( m_va, ib, m_mesh_data->get_index_channel()->desc.slots[0].etype, true );
    198197
    199198        m_data = new uint8[ m_vertex_count * m_vsize ];
  • trunk/src/gfx/particle_engine.cc

    r312 r313  
    485485        info->particles = new particle[ data->quota ];
    486486        info->quads     = new particle_quad[ data->quota ];
    487         info->vtx_array = m_device->create_vertex_array<particle_vtx>(
     487        info->vtx_array = m_context->create_vertex_array<particle_vtx>(
    488488                (particle_vtx*)info->quads, data->quota*6, STREAM_DRAW );
    489         info->vtx_buffer = m_device->find_buffer( info->vtx_array, slot::POSITION );
     489        info->vtx_buffer = m_context->find_buffer( info->vtx_array, slot::POSITION );
    490490        info->last_update = 0;
    491491        info->test = false;
     
    519519                delete[] info->quads;
    520520                //if ( system->own_va )
    521                 m_device->release( info->vtx_array );
     521                m_context->release( info->vtx_array );
    522522                m_systems.destroy( system );
    523523        }
  • trunk/src/gfx/skeletal_mesh.cc

    r303 r313  
    2626        m_vtx_data  = a_mesh_data->get_channel_data<md5_vtx_pntiw>();
    2727        m_indices   = a_mesh_data->get_count();
    28         m_va        = a_context->get_device()->create_vertex_array( a_mesh_data,
     28        m_va        = a_context->create_vertex_array( a_mesh_data,
    2929STREAM_DRAW );
    30         m_pbuffer   = a_context->get_device()->find_buffer( m_va, slot::POSITION );
     30        m_pbuffer   = a_context->find_buffer( m_va, slot::POSITION );
    3131}
    3232
     
    195195        : skeletal_mesh( a_context ), m_bone_data( a_bone_data ), m_transform( nullptr )
    196196{
    197         m_va          = a_context->get_device()->create_vertex_array( a_mesh, nv::STATIC_DRAW );
     197        m_va          = a_context->create_vertex_array( a_mesh, nv::STATIC_DRAW );
    198198        m_index_count = a_mesh->get_count();
    199199        if ( m_bone_data )
  • trunk/src/gl/gl_context.cc

    r311 r313  
    1212using namespace nv;
    1313
     14nv::vertex_array nv::gl_context::create_vertex_array()
     15{
     16        vertex_array result = m_vertex_arrays.create();
     17        vertex_array_info* info = m_vertex_arrays.get( result );
     18        info->count       = 0;
     19        info->index       = buffer();
     20        info->index_owner = false;
     21        info->index_type  = USHORT;
     22        return result;
     23}
     24
     25nv::framebuffer nv::gl_context::create_framebuffer()
     26{
     27        if ( is_gl_extension_loaded( GL_EXT_FRAMEBUFFER_OBJECT ) )
     28        {
     29                unsigned glid   = 0;
     30                glGenFramebuffers( 1, &glid );
     31                framebuffer result = m_framebuffers.create();
     32                gl_framebuffer_info* info = m_framebuffers.get( result );
     33                info->glid = glid;
     34                info->color_attachment_count = 0;
     35                return result;
     36        }
     37        else return framebuffer();
     38}
     39
     40void nv::gl_context::release( vertex_array va )
     41{
     42        vertex_array_info* info = m_vertex_arrays.get( va );
     43        if ( info )
     44        {
     45                for ( uint32 i = 0; i < info->count; ++i )
     46                {
     47                        if ( info->attr[i].owner ) m_device->release( info->attr[i].vbuffer );
     48                }
     49                if ( info->index.is_valid() && info->index_owner) m_device->release( info->index );
     50                m_vertex_arrays.destroy( va );
     51        }
     52}
     53
     54void nv::gl_context::release( framebuffer f )
     55{
     56        gl_framebuffer_info* info = m_framebuffers.get( f );
     57        if ( info )
     58        {
     59                // TODO: release textures?
     60                glBindFramebuffer(GL_FRAMEBUFFER, 0);
     61                glDeleteFramebuffers(1, &info->glid);
     62        }
     63}
     64
     65const vertex_array_info* nv::gl_context::get_vertex_array_info( vertex_array va ) const
     66{
     67        return m_vertex_arrays.get( va );
     68}
     69
     70const framebuffer_info* nv::gl_context::get_framebuffer_info( framebuffer f ) const
     71{
     72        return m_framebuffers.get( f );
     73}
     74
     75vertex_array_info* nv::gl_context::get_vertex_array_info_mutable( vertex_array va )
     76{
     77        return m_vertex_arrays.get( va );
     78}
     79
     80
    1481void gl_context::bind( texture t, texture_slot slot )
    1582{
     
    3299}
    33100
    34 void nv::gl_context::bind( buffer b )
    35 {
    36         const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
    37         if ( info )
    38         {
    39                 glBindBuffer( buffer_type_to_enum( info->type ), info->glid );
    40         }
    41 }
     101// void nv::gl_context::bind( buffer b )
     102// {
     103//      const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
     104//      if ( info )
     105//      {
     106//              glBindBuffer( buffer_type_to_enum( info->type ), info->glid );
     107//      }
     108// }
    42109
    43110void nv::gl_context::bind( vertex_array va )
    44111{
    45         const vertex_array_info* info = m_device->get_vertex_array_info( va );
     112        const vertex_array_info* info = m_vertex_arrays.get( va );
    46113        if ( info )
    47114        {
     
    51118                        uint32 location                    = static_cast<uint32>( vba.location );
    52119                        glEnableVertexAttribArray( location );
    53                         bind( vba.vbuffer );
     120                        const gl_buffer_info* vinfo = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( vba.vbuffer ) );
     121                        if ( vinfo && vinfo->type == VERTEX_BUFFER )
     122                                glBindBuffer( GL_ARRAY_BUFFER, vinfo->glid );
     123                        else
     124                        {
     125                                // TODO: report error
     126                        }
     127
    54128                        glVertexAttribPointer(
    55129                                location,
     
    60134                                (void*)vba.offset
    61135                                );
    62                         unbind( vba.vbuffer );
    63                 }
     136                }
     137                glBindBuffer( GL_ARRAY_BUFFER, 0 );
    64138
    65139                if ( info->index.is_valid() )
    66140                {
    67                         bind( info->index );
    68                 }
     141                        const gl_buffer_info* iinfo = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( info->index ) );
     142                        if ( iinfo && iinfo->type == INDEX_BUFFER )
     143                        {
     144                                glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, iinfo->glid );
     145                        }
     146                        else
     147                        {
     148                                // TODO: report error
     149                        }
     150                }
     151        }
     152}
     153
     154void nv::gl_context::bind( framebuffer f )
     155{
     156        const gl_framebuffer_info* info = m_framebuffers.get( f );
     157        if ( info )
     158        {
     159                glBindFramebuffer( GL_FRAMEBUFFER, info->glid );
    69160        }
    70161}
     
    75166}
    76167
    77 void nv::gl_context::unbind( buffer b )
    78 {
    79         const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
    80         if ( info )
    81         {
    82                 glBindBuffer( buffer_type_to_enum( info->type ), 0 );
    83         }
    84 }
     168// void nv::gl_context::unbind( buffer b )
     169// {
     170//      const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
     171//      if ( info )
     172//      {
     173//              glBindBuffer( buffer_type_to_enum( info->type ), 0 );
     174//      }
     175// }
    85176
    86177void nv::gl_context::unbind( vertex_array va )
    87178{
    88         const vertex_array_info* info = m_device->get_vertex_array_info( va );
     179        const vertex_array_info* info = m_vertex_arrays.get( va );
    89180        if ( info )
    90181        {
    91182                if ( info->index.is_valid() )
    92183                {
    93                         unbind( info->index );
     184                        glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
    94185                }
    95186
     
    98189                        glDisableVertexAttribArray( static_cast<uint32>( info->attr[i].location ) );
    99190                }
     191        }
     192}
     193
     194void nv::gl_context::unbind( framebuffer f )
     195{
     196        // this way we are sure that the extension is loaded
     197        const gl_framebuffer_info* info = m_framebuffers.get( f );
     198        if ( info )
     199        {
     200                glBindFramebuffer( GL_FRAMEBUFFER, 0 );
    100201        }
    101202}
     
    119220        if ( info )
    120221        {
    121                 bind( b );
    122                 glBufferSubData( buffer_type_to_enum( info->type ), (GLintptr)offset, (GLsizeiptr)size, data );
     222                GLenum glenum = buffer_type_to_enum( info->type );
     223                glBindBuffer( glenum, info->glid );
     224                glBufferSubData( glenum, (GLintptr)offset, (GLsizeiptr)size, data );
    123225        }
    124226}
     
    483585nv::gl_context::~gl_context()
    484586{
     587        while ( m_framebuffers.size() > 0 )
     588                release( m_framebuffers.get_handle(0) );
     589        while ( m_vertex_arrays.size() > 0 )
     590                release( m_vertex_arrays.get_handle(0) );
    485591}
    486592
     
    500606{
    501607        apply_render_state( rs );
    502         const vertex_array_info* info = m_device->get_vertex_array_info( va );
     608        const vertex_array_info* info = m_vertex_arrays.get( va );
    503609        if ( count > 0 && info )
    504610        {
  • trunk/src/gl/gl_device.cc

    r303 r313  
    9090gl_device::~gl_device()
    9191{
    92         while ( m_vertex_arrays.size() > 0 )
    93                 release( m_vertex_arrays.get_handle(0) );
    9492        while ( m_textures.size() > 0 )
    9593                release( m_textures.get_handle(0) );
     
    9896        while ( m_programs.size() > 0 )
    9997                release( m_programs.get_handle(0) );
    100 
    10198        SDL_Quit();
    10299}
     
    192189}
    193190
    194 nv::vertex_array nv::gl_device::create_vertex_array()
    195 {
    196         vertex_array result = m_vertex_arrays.create();
    197         vertex_array_info* info = m_vertex_arrays.get( result );
    198         info->count       = 0;
    199         info->index       = buffer();
    200         info->index_owner = false;
    201         info->index_type  = USHORT;
    202         return result;
    203 }
    204 
    205 void nv::gl_device::release( vertex_array va )
    206 {
    207         vertex_array_info* info = m_vertex_arrays.get( va );
    208         if ( info )
    209         {
    210                 for ( uint32 i = 0; i < info->count; ++i )
    211                 {
    212                         if ( info->attr[i].owner ) release( info->attr[i].vbuffer );
    213                 }
    214                 if ( info->index.is_valid() && info->index_owner) release( info->index );
    215                 m_vertex_arrays.destroy( va );
    216         }
    217 }
    218 
    219191void nv::gl_device::release( program p )
    220192{
     
    233205                m_programs.destroy( p );
    234206        }
    235 }
    236 
    237 const vertex_array_info* nv::gl_device::get_vertex_array_info( vertex_array va ) const
    238 {
    239         return m_vertex_arrays.get( va );
    240 }
    241 
    242 vertex_array_info* nv::gl_device::get_vertex_array_info_mutable( vertex_array va )
    243 {
    244         return m_vertex_arrays.get( va );
    245207}
    246208
     
    485447}
    486448
    487 
  • trunk/src/gui/gui_renderer.cc

    r303 r313  
    7373        {
    7474                ctx->get_device()->release( shader );
    75                 ctx->get_device()->release( varray );
     75                ctx->release( varray );
    7676        }
    7777
     
    114114        m_scene_state.get_camera().set_ortho( 0.0f, float( m_window->get_width() ), float( m_window->get_height() ), 0.0f );
    115115
    116         sr->varray     = m_window->get_device()->create_vertex_array();
     116        sr->varray     = m_window->get_context()->create_vertex_array();
    117117        buffer vb      = sr->buffer.get_buffer();
    118         m_window->get_device()->add_vertex_buffers< vertex >( sr->varray, vb );
     118        m_window->get_context()->add_vertex_buffers< vertex >( sr->varray, vb );
    119119
    120120        nv::sampler sampler( nv::sampler::LINEAR, nv::sampler::CLAMP_TO_EDGE );
     
    267267        {
    268268                buffer vb = sr->buffer.get_buffer();
    269                 m_context->get_device()->replace_vertex_buffer( sr->varray, vb, false );
     269                m_context->replace_vertex_buffer( sr->varray, vb, false );
    270270        }
    271271        m_context->bind( sr->tex, TEX_DIFFUSE );
Note: See TracChangeset for help on using the changeset viewer.