Changeset 149 for trunk


Ignore:
Timestamp:
07/07/13 17:34:37 (12 years ago)
Author:
epyon
Message:
  • time - choice of uint32/uint64 depending on requested precision in both functions and template util classes
  • md3_loader - severe performance improvements
  • md3_loader - tag transform array loading
Location:
trunk
Files:
4 edited

Legend:

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

    r148 r149  
    3838                virtual const md3_tag* get_tag( const std::string& name ) const;
    3939                virtual mat4 get_tag( sint32 frame, const std::string& name ) const;
     40                sint32 get_max_frames() const;
     41                void load_tags( std::vector<mat4>& t, const std::string& tag );
    4042                void load_positions( std::vector<vec3>& p, sint32 frame =-1 );
    4143                void load_normals( std::vector<vec3>& n, sint32 frame =-1 );
  • trunk/nv/time.hh

    r121 r149  
    4141         * Get microsecond count based on std::clock
    4242         */
    43         uint32 get_cpu_us();
     43        uint64 get_cpu_us();
    4444
    4545        /**
     
    5151         * Get microsecond count based on system counter
    5252         */
    53         uint32 get_system_us();
     53        uint64 get_system_us();
    5454
    55         struct cpu_ms_timer { uint32 operator()() { return get_cpu_ms(); } };
    56         struct cpu_us_timer { uint32 operator()() { return get_cpu_us(); } };
    57         struct system_ms_timer { uint32 operator()() { return get_system_ms(); } };
    58         struct system_us_timer { uint32 operator()() { return get_system_us(); } };
     55        struct cpu_ms_timer
     56        {
     57                typedef uint32 value_type;
     58                static const value_type second = 1000;   
     59                value_type operator()() { return get_cpu_ms(); }
     60        };
     61
     62        struct cpu_us_timer
     63        {
     64                typedef uint64 value_type;
     65                static const value_type second = 1000000;
     66                uint64 operator()() { return get_cpu_us(); }
     67        };
     68
     69        struct system_ms_timer
     70        {
     71                typedef uint32 value_type;
     72                static const value_type second = 1000;   
     73                value_type operator()() { return get_system_ms(); }
     74        };
     75
     76        struct system_us_timer
     77        {
     78                typedef uint64 value_type;
     79                static const value_type second = 1000000;
     80                value_type operator()() { return get_system_us(); }
     81        };
    5982       
    6083        /**
     
    6588        {
    6689        public:
     90                typedef typename Timer::value_type value_type;
     91
    6792                timer_class()   : last( Timer()() ) {}
    6893                void mark()     
    6994                {
    70                         uint32 now = Timer()();
     95                        value_type now = Timer()();
    7196                        stored = now - last;
    7297                        last = now;
    7398                }
    74                 uint32 elapsed() const
     99                value_type elapsed() const
    75100                {
    76101                        return stored;
    77102                }
    78103        private:
    79                 uint32 last;
    80                 uint32 stored;
     104                value_type last;
     105                value_type stored;
    81106        };
    82107
     
    88113        {
    89114        public:
     115                typedef typename Timer::value_type value_type;
     116
    90117                fps_counter_class() : frames(1), last(0) {}
    91118                bool tick()
    92119                {
    93                         uint32 now = Timer()();
    94                         if ( now - last >= 1000 )
     120                        value_type now = Timer()();
     121                        if ( now - last >= Timer::second )
    95122                        {
    96123                                value = (static_cast<float>(frames) /
    97                                         static_cast<float>(now - last))*1000;
     124                                        static_cast<float>(now - last))*Timer::second;
    98125                                frames = 1;
    99126                                last = now;
     
    108135                }
    109136        private:
    110                 uint32 last;
    111                 uint32 frames;
    112                 f32 value;
     137                value_type last;
     138                uint32     frames;
     139                f32        value;
    113140        };
    114141
  • trunk/src/formats/md3_loader.cc

    r148 r149  
    100100        sint16 y;
    101101        sint16 z;
    102         sint16 normal;
     102        uint16 normal;
    103103};
    104104
     
    118118        md3_tag_t*     tags;
    119119        md3_surface_t* surfaces;
     120        // extra information (not in md3 file)
     121        sint32         vertices_per_frame;
    120122};
    121123
     
    208210
    209211        source.seek( md3->header.ofs_surfaces, origin::SET );
     212        md3->vertices_per_frame = 0;
    210213
    211214        for ( sint32 i = 0; i < md3->header.num_surfaces; ++i )
     
    213216                if ( !read_surface( md3->surfaces + i, source ) ) return false;
    214217                if ( md3->header.num_frames != md3->surfaces[i].header.num_frames ) return false;
     218                md3->vertices_per_frame += md3->surfaces[i].header.num_verts;
    215219        }
    216220        return true;
     
    228232}
    229233
    230 static inline vec3 md3_vec3( const md3_vertex_t& v )
    231 {
    232 //      return vec3( v.x * MD3_XYZ_SCALE, v.y * MD3_XYZ_SCALE, v.z * MD3_XYZ_SCALE );
    233         return vec3( v.x * MD3_XYZ_SCALE, v.z * MD3_XYZ_SCALE, v.y * MD3_XYZ_SCALE );
    234 }
    235 
    236 static inline vec3 md3_normal( const md3_vertex_t& v )
    237 {
    238         float pi  = glm::pi<float>();
    239         float lat = (((v.normal >> 8) & 255) * (2 * pi)) / 255.0f;
    240         float lng = ((v.normal & 255) * (2 * pi)) / 255.0f;
    241         return vec3(
    242                 glm::cos( lat ) * glm::sin( lng ),
    243 //              glm::sin( lat ) * glm::sin( lng ),
    244 //              glm::cos( lng )
    245                 glm::cos( lng ),
    246                 glm::sin( lat ) * glm::sin( lng )
    247         );
    248 }
    249 
     234static vec3 s_normal_cache[256*256];
     235static bool s_normal_ready = false;
    250236
    251237md3_loader::md3_loader()
    252238        : m_md3( nullptr ), m_size( 0 )
    253239{
    254        
     240        if ( !s_normal_ready )
     241        {
     242                float pi      = glm::pi<float>();
     243                float convert = (2 * pi) / 255.0f;
     244                int n = 0;
     245                for ( int lat = 0; lat < 256; ++lat )
     246                {
     247                        float flat    = lat * convert;
     248                        float sin_lat = glm::sin( flat );
     249                        float cos_lat = glm::cos( flat );
     250                        for ( int lng = 0; lng < 256; ++lng, ++n )
     251                        {
     252                                float flng    = lng * convert;
     253                                float sin_lng = glm::sin( flng );
     254                                float cos_lng = glm::cos( flng );
     255                                s_normal_cache[n].x = cos_lat * sin_lng;
     256//                              s_normal_cache[n].y = sin_lat * sin_lng;
     257//                              s_normal_cache[n].z = cos_lng;
     258                                s_normal_cache[n].z = sin_lat * sin_lng;
     259                                s_normal_cache[n].y = cos_lng;
     260                        }
     261                }
     262
     263                s_normal_ready = true;
     264        }
    255265}
    256266
     
    318328}
    319329
     330void nv::md3_loader::load_tags( std::vector<mat4>& t, const std::string& tag )
     331{
     332        md3_t* md3 = (md3_t*)m_md3;
     333        t.clear();
     334        for ( sint32 f = 0; f < md3->header.num_frames; ++f )
     335        {
     336                for ( sint32 i = 0; i < md3->header.num_tags; ++i )
     337                {
     338                        const md3_tag_t& rtag = md3->tags[i + md3->header.num_tags * f];
     339                        std::string rname((char*)(rtag.name));
     340                        if (rname == tag)
     341                        {
     342                                vec4 axisx     = vec4( md3_vec3( rtag.axis[0] ), 0.0 );
     343                                vec4 axisz     = vec4( md3_vec3( rtag.axis[1] ), 0.0 );
     344                                vec4 axisy     = vec4( md3_vec3( rtag.axis[2] ), 0.0 );
     345                                vec4 origin    = vec4( md3_vec3( rtag.origin ),  1.0 );
     346                                t.emplace_back( axisx, axisy, axisz, origin );
     347                        }
     348                }
     349
     350        }
     351}
     352
     353sint32 nv::md3_loader::get_max_frames() const
     354{
     355        return ((md3_t*)m_md3)->header.num_frames;
     356}
     357
    320358mat4 md3_loader::get_tag( sint32 frame, const std::string& name ) const
    321359{
     
    352390        sint32 frame_count   = ( frame == -1 ? md3->header.num_frames : 1 );
    353391
     392        p.reserve( md3->vertices_per_frame * frame_count );
     393
    354394        while ( frame_count > 0 )
    355395        {
     
    359399                        sint32         vcount  = surface.header.num_verts;
    360400                        sint32         offset  = vcount * current_frame;
    361                         p.reserve( p.size() + vcount );
    362                         for (sint32 j = 0; j < vcount; ++j )
     401                        sint32         limit   = vcount + offset;
     402                        for (sint32 j = offset; j < limit; ++j )
    363403                        {
    364                                 p.push_back( md3_vec3( surface.vertices[j + offset] ) );
     404                                md3_vertex_t& v = surface.vertices[j];
     405                                p.push_back( vec3( v.x * MD3_XYZ_SCALE, v.z * MD3_XYZ_SCALE, v.y * MD3_XYZ_SCALE ) );
    365406                        }
    366407                }
     
    378419        sint32 frame_count   = ( frame == -1 ? md3->header.num_frames : 1 );
    379420
     421        n.reserve( md3->vertices_per_frame * frame_count );
     422
    380423        while ( frame_count > 0 )
    381424        {
     
    385428                        sint32         vcount  = surface.header.num_verts;
    386429                        sint32         offset  = vcount * current_frame;
    387                         n.reserve( n.size() + vcount );
    388                         for (sint32 j = 0; j < vcount; ++j )
     430                        sint32         limit   = vcount + offset;
     431                        for (sint32 j = offset; j < limit; ++j )
    389432                        {
    390                                 n.push_back( md3_normal( surface.vertices[j + offset] ) );
     433                                n.push_back( s_normal_cache[ surface.vertices[j].normal ] );
    391434                        }
    392435                }
  • trunk/src/time.cc

    r121 r149  
    6767}
    6868
    69 nv::uint32 nv::get_cpu_us()
     69nv::uint64 nv::get_cpu_us()
    7070{
    71         return (uint32)( (f32)( clock() - zero_timer.clock_zero ) / ( (f32)CLOCKS_PER_SEC / 1000000.0 ) ) ;
     71        return (uint64)( (f32)( clock() - zero_timer.clock_zero ) / ( (f32)CLOCKS_PER_SEC / 1000000.0 ) ) ;
    7272}
    7373
     
    7878        QueryPerformanceCounter(&now);
    7979        LONGLONG result = now.QuadPart - zero_timer.query_zero.QuadPart;
    80         return (uint32) (1000.0 * result / zero_timer.frequency.QuadPart);
     80        return (uint32) (1000.0 * result / (double) zero_timer.frequency.QuadPart);
    8181#else
    8282        struct timeval now;
     
    8686}
    8787
    88 nv::uint32 nv::get_system_us()
     88nv::uint64 nv::get_system_us()
    8989{
    9090#if NV_COMPILER == NV_MSVC
     
    9292        QueryPerformanceCounter(&now);
    9393        LONGLONG result = now.QuadPart - zero_timer.query_zero.QuadPart;
    94         return (uint32) (1000000.0 * result / zero_timer.frequency.QuadPart);
     94        return (uint64) (1000000.0 * result / (double) zero_timer.frequency.QuadPart);
    9595#else
    9696        struct timeval now;
Note: See TracChangeset for help on using the changeset viewer.