Changeset 280 for trunk


Ignore:
Timestamp:
07/08/14 18:29:24 (11 years ago)
Author:
epyon
Message:
  • unified mesh_raw_channel and mesh_raw_index_channel
  • string_table cleaned up and implementation of creator split into a cc file
Location:
trunk
Files:
1 added
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/formats/assimp_loader.hh

    r277 r280  
    99
    1010#include <nv/common.hh>
     11#include <nv/io/string_table.hh>
    1112#include <nv/interface/mesh_loader.hh>
    1213#include <nv/interface/mesh_data.hh>
     
    7677                key_animation_data* create_direct_keys( const void* vnode );
    7778
     79                string_table_creator m_strings;
    7880                string m_ext;
    7981                mat4   m_rotate_transform;
  • trunk/nv/gfx/keyframed_mesh.hh

    r275 r280  
    5353                        vec3 normal;
    5454                };
     55                struct vertex_t
     56                {
     57                        vec2 texcoord;
     58                };
    5559
    5660                mesh_data* m_mesh_data;
  • trunk/nv/interface/device.hh

    r277 r280  
    100100                {
    101101                        vertex_array*  va = create_vertex_array();
    102                         for ( uint32 ch = 0; ch < data->get_channel_data().size(); ++ch )
     102                        const std::vector< mesh_raw_channel* >& channels = data->get_raw_channels();
     103                        for ( uint32 ch = 0; ch < channels.size(); ++ch )
    103104                        {
    104                                 const mesh_raw_channel* channel = data->get_channel_data()[ch];
    105                                 vertex_buffer* vb = create_vertex_buffer( hint, channel->size, channel->data );
    106                                 va->add_vertex_buffers( vb, channel );
    107                         }
    108                         if ( data->get_index_channel() != nullptr )
    109                         {
    110                                 const mesh_raw_index_channel* index = data->get_index_channel();
    111                                 index_buffer* ib = create_index_buffer( hint, index->size, index->data );
    112                                 va->set_index_buffer( ib, index->etype, true );
     105                                const mesh_raw_channel* channel = channels[ch];
     106                                if ( channel->is_index() )
     107                                {
     108                                        index_buffer* ib = create_index_buffer( hint, channel->size(), channel->data );
     109                                        va->set_index_buffer( ib, channel->desc.slots[0].etype, true );
     110                                }
     111                                else
     112                                {
     113                                        vertex_buffer* vb = create_vertex_buffer( hint, channel->size(), channel->data );
     114                                        va->add_vertex_buffers( vb, channel );
     115                                }
    113116                        }
    114117                        return va;
  • trunk/nv/interface/mesh_data.hh

    r241 r280  
    2424                vertex_descriptor desc;
    2525                uint8*            data;
    26                 uint32            size;
    2726                uint32            count;
    2827
    29                 mesh_raw_channel() : data( nullptr ), size( 0 ) {}
     28                mesh_raw_channel() : data( nullptr ), count( 0 ) {}
    3029                ~mesh_raw_channel()
    3130                {
    3231                        if ( data != nullptr ) delete[] data;
    3332                }
     33
     34                bool is_index() const { return count > 0 && desc.slots[0].vslot == INDEX; }
     35
     36                uint32 size() const { return count * desc.size; }
    3437
    3538                template < typename VTX >
     
    3942                        result->desc.initialize<VTX>();
    4043                        result->count = count;
    41                         result->size  = count * sizeof( VTX );
    42                         result->data  = (count > 0 ? ( new uint8[ result->size ] ) : nullptr );
     44                        result->data  = (count > 0 ? ( new uint8[ result->size() ] ) : nullptr );
    4345                        return result;
    4446                }
     
    4850                        result->desc  = vtxdesc;
    4951                        result->count = count;
    50                         result->size  = count * sizeof( vtxdesc.size );
    51                         result->data  = (count > 0 ? ( new uint8[ result->size ] ) : nullptr );
     52                        result->data  = (count > 0 ? ( new uint8[ result->size() ] ) : nullptr );
    5253                        return result;
    53                 }
    54         };
    55 
    56         struct mesh_raw_index_channel
    57         {
    58                 datatype          etype;
    59                 uint8*            data;
    60                 uint32            size;
    61                 uint32            count;
    62 
    63                 mesh_raw_index_channel() : etype( NONE ), data( nullptr ), size( 0 ) {}
    64                 ~mesh_raw_index_channel()
    65                 {
    66                         if ( data != nullptr ) delete[] data;
    6754                }
    6855
    6956                template < typename ITYPE >
    70                 static mesh_raw_index_channel* create( uint32 count = 0 )
     57                static mesh_raw_channel* create_index( uint32 count = 0 )
    7158                {
    72                         mesh_raw_index_channel* result = new mesh_raw_index_channel();
    73                         result->etype = type_to_enum< ITYPE >::type;
     59                        mesh_raw_channel* result = new mesh_raw_channel();
     60                        result->desc.initialize_index<ITYPE>();
    7461                        result->count = count;
    75                         result->size  = count * sizeof( ITYPE );
    76                         result->data  = (count > 0 ? ( new uint8[ result->size ] ) : nullptr );
     62                        result->data  = (count > 0 ? ( new uint8[ result->size() ] ) : nullptr );
    7763                        return result;
    7864                }
    7965
    80                 static mesh_raw_index_channel* create( datatype etype, uint32 count = 0 )
     66                // TODO: remove this
     67                static mesh_raw_channel* create_index( datatype etype, uint32 count = 0 )
    8168                {
    82                         mesh_raw_index_channel* result = new mesh_raw_index_channel();
    83                         result->etype = etype;
     69                        mesh_raw_channel* result = new mesh_raw_channel();
     70                        result->desc.initialize_index( etype );
    8471                        result->count = count;
    85                         result->size  = count * get_datatype_info( etype ).size;
    86                         result->data  = (count > 0 ? ( new uint8[ result->size ] ) : nullptr );
     72                        result->data  = (count > 0 ? ( new uint8[ result->size() ] ) : nullptr );
    8773                        return result;
    8874                }
     75
    8976        };
    9077
     
    9380        public:
    9481                mesh_data() : m_index_channel( nullptr ) {}
    95                 void add_channel( mesh_raw_channel* channel ) { m_channels.push_back( channel ); }
    96                 void set_index_channel( mesh_raw_index_channel* channel )
     82                void add_channel( mesh_raw_channel* channel )
    9783                {
    98                         if ( m_index_channel ) delete m_index_channel;
    99                         m_index_channel = channel;
     84                        NV_ASSERT( channel, "nullptr passed to add_channel!" );
     85                        m_channels.push_back( channel );
     86                        if ( channel->is_index() )
     87                        {
     88                                NV_ASSERT( !m_index_channel, "second index channel!" );
     89                                m_index_channel = channel;
     90                        }
    10091                }
    10192
    102                 const std::vector< mesh_raw_channel* >& get_channel_data() const { return m_channels; }
    103                 const mesh_raw_index_channel*           get_index_channel() const { return m_index_channel; }
     93                const std::vector< mesh_raw_channel* >& get_raw_channels() const { return m_channels; }
     94                const mesh_raw_channel*           get_index_channel() const { return m_index_channel; }
    10495
    10596                size_t get_count() const
     
    116107                }
    117108
     109                template < typename VTX >
     110                const mesh_raw_channel* get_channel() const
     111                {
     112                        vertex_descriptor compare;
     113                        compare.initialize<VTX>();
     114                        for ( auto ch : m_channels )
     115                        {
     116                                if ( ch->desc == compare )
     117                                {
     118                                        return ch;
     119                                }
     120                        }
     121                        return nullptr;
     122                }
     123
     124                template < typename VTX >
     125                const VTX* get_channel_data() const
     126                {
     127                        vertex_descriptor compare;
     128                        compare.initialize<VTX>();
     129                        for ( auto ch : m_channels )
     130                        {
     131                                if ( ch->desc == compare )
     132                                {
     133                                        return (VTX*)ch->data;
     134                                }
     135                        }
     136                        return nullptr;
     137                }
     138
    118139                virtual ~mesh_data()
    119140                {
    120141                        for ( auto channel : m_channels ) delete channel;
    121                         if ( m_index_channel ) delete m_index_channel;
    122142                }
    123143        private:
    124144                std::vector< mesh_raw_channel* > m_channels;
    125                 mesh_raw_index_channel*          m_index_channel;
     145                mesh_raw_channel*                m_index_channel;
    126146        };
    127147
  • trunk/nv/interface/vertex.hh

    r238 r280  
    2626                COLOR          = 6,
    2727
    28                 SLOT_MAX       = 6,
     28                INDEX          = 7,
     29                SLOT_MAX       = 7,
    2930                SLOT_MAX_STORE = 8,
    3031        };
     
    250251                uint32                 size;
    251252
     253                template < typename IDX >
     254                void initialize_index()
     255                {
     256                        count = 1;
     257                        size  = sizeof( IDX );
     258                        slots[0].etype  = type_to_enum< IDX >::type;
     259                        slots[0].vslot  = slot::INDEX;
     260                        slots[0].offset = 0;
     261                }
     262
     263                void initialize_index( datatype itype )
     264                {
     265                        count = 1;
     266                        size  = get_datatype_info( itype ).size;
     267                        slots[0].etype  = itype;
     268                        slots[0].vslot  = slot::INDEX;
     269                        slots[0].offset = 0;
     270                }
     271
    252272                template < typename VTX >
    253273                void initialize()
     
    263283                        size = sizeof( VTX );
    264284                }
     285
     286                bool operator==( const vertex_descriptor& rhs )
     287                {
     288                        if ( size  != rhs.size )  return false;
     289                        if ( count != rhs.count ) return false;
     290                        for ( uint32 i = 0; i < count; ++i )
     291                        {
     292                                if ( slots[i].etype  != rhs.slots[i].etype )  return false;
     293                                if ( slots[i].offset != rhs.slots[i].offset ) return false;
     294                                if ( slots[i].vslot  != rhs.slots[i].vslot )  return false;
     295                        }
     296                        return true;
     297                }
     298
    265299        private:
    266300                template < typename VTX, slot SLOT >
  • trunk/nv/io/string_table.hh

    r279 r280  
    2020namespace nv
    2121{
    22 
    2322        class string_table : noncopyable
    2423        {
    2524        public:
    26                 string_table( char* data, uint32 size, uint32* indices, uint32 count )
    27                         : m_count( count ), m_size( size ), m_data( data ), m_indices( indices )
     25                typedef uint16 index;
     26                typedef uint32 offset;
     27
     28                string_table( char* data, uint32 size, offset* offsets, index count )
     29                        : m_count( count ), m_size( size ), m_data( data ), m_offsets( offsets )
    2830                {
    2931
     
    3436                }
    3537
    36                 const char* get( uint16 index )
     38                const char* get( index i ) const
    3739                {
    38                         return index < m_count ? m_data + m_indices[index] : nullptr;
     40                        return i < m_count ? m_data + m_offsets[i] : nullptr;
    3941                }
    4042
    41                 void dump( nv::stream* out )
     43                void dump( nv::stream* out ) const
    4244                {
    4345                        out->write( &m_count,  sizeof( m_count ), 1 );
    4446                        out->write( &m_size,   sizeof( m_size ), 1 );
    45                         out->write( m_indices, sizeof( uint32 ), m_count );
     47                        out->write( m_offsets, sizeof( offset ), m_count );
    4648                        out->write( m_data,    sizeof( char ), m_size );
    4749                }
     
    5052                {
    5153                        delete[] m_data;
    52                         delete[] m_indices;
     54                        delete[] m_offsets;
    5355                }
    5456        private:
     
    5759                        in->read( &m_count, sizeof( m_count ), 1 );
    5860                        in->read( &m_size, sizeof( m_size ), 1 );
    59                         m_indices = new uint32[ m_count ];
     61                        m_offsets = new offset[ m_count ];
    6062                        m_data    = new char[ m_count ];
    61                         in->read( m_indices, sizeof( uint32 ), m_count );
     63                        in->read( m_offsets, sizeof( offset ), m_count );
    6264                        in->read( m_data,    sizeof( char ), m_size );
    6365                }
    6466
    65                 uint32  m_count;
     67                char*   m_data;
    6668                uint32  m_size;
    67                 uint32* m_indices;
    68                 char*   m_data;
     69                offset* m_offsets;
     70                index   m_count;
    6971        };
    7072
     
    7274        {
    7375        public:
    74                 string_table_creator() {}
     76                typedef string_table::index  index;
     77                typedef string_table::offset offset;
    7578
    76                 uint32 insert( const char* s )
    77                 {
    78                         return insert( std::string(s) );
    79                 }
    80 
    81                 uint32 insert( const std::string& s )
    82                 {
    83                         auto i = m_map.find( s );
    84                         if ( i != m_map.end() )
    85                         {
    86                                 return i->second;
    87                         }
    88                         const char* cs = s.c_str();
    89                         uint32 cs_size = s.size() + 1;
    90                         uint32 result = m_indexes.size();
    91                         uint32 index  = m_data.size();
    92                         m_indexes.push_back( index );
    93                         std::copy( cs, cs + cs_size, std::back_inserter( m_data ) );
    94                         m_map[ s ] = result;
    95                         return result;
    96                 }
    97 
    98                 string_table* create_table() const
    99                 {
    100                         uint32* indexes = new uint32[m_indexes.size()];
    101                         char*   data    = new char  [m_data.size()];
    102                         std::copy( m_indexes.begin(), m_indexes.end(), indexes );
    103                         std::copy( m_data.begin(),    m_data.end(),    data );
    104                         return new string_table( data, m_data.size(), indexes, m_indexes.size() );
    105                 }
    106 
    107                 void dump( nv::stream* out ) const
    108                 {
    109                         uint32 count = m_indexes.size();
    110                         uint32 size  = m_data.size();
    111                         out->write( &count,  sizeof( count ), 1 );
    112                         out->write( &size,   sizeof( size ), 1 );
    113                         out->write( m_indexes.data(), sizeof( uint16 ), count );
    114                         out->write( m_data.data(),    sizeof( char ), size );
    115                 }
     79                string_table_creator();
     80                index insert( const std::string& s );
     81                string_table* create_table() const;
     82                void dump( nv::stream* out ) const;
     83                const char* get( index i ) const;
     84                index get( const std::string& s ) const;
     85                void clear();
    11686        private:
    117                 std::unordered_map< std::string, uint32 > m_map;
    118                 std::vector< uint32 > m_indexes;
     87                std::unordered_map< std::string, index > m_map;
     88                std::vector< offset > m_offsets;
    11989                std::vector< char >   m_data;
    12090        };
  • trunk/src/formats/assimp_loader.cc

    r279 r280  
    143143        }
    144144
    145         mesh_raw_index_channel* ichannel = mesh_raw_index_channel::create( USHORT, mesh->mNumFaces * 3 );
    146         result->set_index_channel( ichannel );
     145        mesh_raw_channel* ichannel = mesh_raw_channel::create_index( USHORT, mesh->mNumFaces * 3 );
     146        result->add_channel( ichannel );
    147147        uint16* indices = (uint16*)ichannel->data;
    148148        for (unsigned int i=0; i<mesh->mNumFaces; i++)
     
    294294                {
    295295                        mesh_data* mesh = model->meshes[m];
    296                         mesh_raw_channel* channel = mesh->get_channel_data()[0];
     296                        mesh_raw_channel* channel = mesh->get_raw_channels()[0];
     297                        NV_ASSERT( !channel->is_index(), "index channel in release_merged!" );
    297298                        assimp_skinned_vtx* va = (assimp_skinned_vtx*)channel->data;
    298299                        for ( unsigned v = 0; v < channel->count; ++v )
  • trunk/src/formats/md2_loader.cc

    r240 r280  
    359359        }
    360360
    361         mesh_raw_index_channel* ic = mesh_raw_index_channel::create< uint16 >( m_new_indexes.size() );
     361        mesh_raw_channel* ic = mesh_raw_channel::create_index< uint16 >( m_new_indexes.size() );
    362362        if ( m_new_indexes.size() > 0 )
    363363        {
     
    369369        result->add_channel( mc_pn );
    370370        result->add_channel( mc_t );
    371         result->set_index_channel( ic );
     371        result->add_channel( ic );
    372372        return result;
    373373}
  • trunk/src/formats/md3_loader.cc

    r241 r280  
    377377        index = 0;
    378378        sint32 index_base = 0;
    379         mesh_raw_index_channel* ic = mesh_raw_index_channel::create< uint16 >( index_count );
     379        mesh_raw_channel* ic = mesh_raw_channel::create_index< uint16 >( index_count );
    380380        uint16* icp = (uint16*)ic->data;
    381381        for ( sint32 i = 0; i < num_surfaces; ++i )
     
    396396        result->add_channel( mc_pn );
    397397        result->add_channel( mc_t );
    398         result->set_index_channel( ic );
     398        result->add_channel( ic );
    399399        return result;
    400400}
  • trunk/src/formats/md5_loader.cc

    r261 r280  
    145145                                        sstream >> num_tris;
    146146
    147                                         mesh_raw_index_channel* ch_i = mesh_raw_index_channel::create<uint32>( num_tris * 3 );
     147                                        mesh_raw_channel* ch_i = mesh_raw_channel::create_index<uint32>( num_tris * 3 );
    148148                                        uint32* vtx_i                = (uint32*)ch_i->data;
    149149                                        mesh->m_idata                = vtx_i;
    150150                                        uint32 idx = 0;
    151                                         mesh->set_index_channel( ch_i );
     151                                        mesh->add_channel( ch_i );
    152152
    153153                                        next_line( sstream );
  • trunk/src/formats/obj_loader.cc

    r240 r280  
    334334                channel->desc  = m_descriptor;
    335335                channel->count = reader->size * 3;
    336                 channel->size  = reader->raw_size();
    337336
    338337                mesh_data* mesh = new mesh_data();
  • trunk/src/gfx/keyframed_mesh.cc

    r275 r280  
    3232
    3333        m_index_count  = m_mesh_data->get_index_channel()->count;
    34         m_vertex_count = m_mesh_data->get_channel_data()[1]->count;
    35         m_frame_count  = m_mesh_data->get_channel_data()[0]->count / m_vertex_count;
     34        m_vertex_count = m_mesh_data->get_channel<vertex_t>()->count;
     35        m_frame_count  = m_mesh_data->get_channel<vertex_pn>()->count / m_vertex_count;
    3636}
    3737
     
    167167        : keyframed_mesh( a_device, a_data, a_tag_map )
    168168{
    169         m_vb = a_device->create_vertex_buffer( nv::STATIC_DRAW, m_vertex_count * sizeof( vertex_pn ), (void*)m_mesh_data->get_channel_data()[0]->data );
    170         m_va->add_vertex_buffers( m_vb, m_mesh_data->get_channel_data()[0] );
     169        m_vb = a_device->create_vertex_buffer( nv::STATIC_DRAW, m_vertex_count * sizeof( vertex_pn ), (void*)m_mesh_data->get_channel<vertex_pn>()->data );
     170        m_va->add_vertex_buffers( m_vb, m_mesh_data->get_channel<vertex_pn>() );
    171171
    172         nv::vertex_buffer* vb = a_device->create_vertex_buffer( nv::STATIC_DRAW, m_vertex_count * sizeof( nv::vec2 ), (void*)m_mesh_data->get_channel_data()[1]->data );
    173         m_va->add_vertex_buffers( vb, m_mesh_data->get_channel_data()[1] );
     172        nv::vertex_buffer* vb = a_device->create_vertex_buffer( nv::STATIC_DRAW, m_vertex_count * sizeof( nv::vec2 ), (void*)m_mesh_data->get_channel<vertex_t>()->data );
     173        m_va->add_vertex_buffers( vb, m_mesh_data->get_channel<vertex_t>() );
    174174
    175         nv::index_buffer* ib = a_device->create_index_buffer( nv::STATIC_DRAW, m_mesh_data->get_index_channel()->size, (void*)m_mesh_data->get_index_channel()->data );
    176         m_va->set_index_buffer( ib, m_mesh_data->get_index_channel()->etype, true );
     175        nv::index_buffer* ib = a_device->create_index_buffer( nv::STATIC_DRAW, m_mesh_data->get_index_channel()->size(), (void*)m_mesh_data->get_index_channel()->data );
     176        m_va->set_index_buffer( ib, m_mesh_data->get_index_channel()->desc.slots[0].etype, true );
    177177
    178178        m_vertex.resize( m_vertex_count );
     
    183183        keyframed_mesh::update( ms );
    184184
    185         const vertex_pn* data = (const vertex_pn*)(m_mesh_data->get_channel_data()[0]->data);
     185        const vertex_pn* data = m_mesh_data->get_channel_data<vertex_pn>();
    186186        const vertex_pn* prev = data + m_vertex_count * m_last_frame;
    187187        const vertex_pn* next = data + m_vertex_count * m_next_frame;
Note: See TracChangeset for help on using the changeset viewer.