Changeset 499


Ignore:
Timestamp:
06/06/16 18:54:52 (9 years ago)
Author:
epyon
Message:
  • ecs work
  • particle engine rehaul
  • added map/unmap buffer to ::context
Location:
trunk
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/ecs/ecs.hh

    r498 r499  
    7777                                return dead_eindex;
    7878                        }
     79
     80                        index_type remove_swap( index_type dead_eindex )
     81                        {
     82                                if ( size_t( dead_eindex ) >= m_handles.size() ) return -1;
     83                                handle_type h = m_handles[ dead_eindex ];
     84                                handle_type swap_handle = m_handles.back();
     85                                if ( dead_eindex != static_cast<index_type>( m_handles.size() - 1 ) )
     86                                {
     87                                        m_handles[unsigned( dead_eindex )] = swap_handle;
     88                                        m_indexes[swap_handle.index()] = dead_eindex;
     89                                }
     90                                m_handles.pop_back();
     91                                m_indexes[ h.index() ] = -1;
     92                                return dead_eindex;
     93                        }
     94
    7995
    8096                        void clear()
     
    265281                        }
    266282
     283                        virtual void remove( index_type i )
     284                        {
     285                                index_type dead_eindex = m_index.remove_swap( i );
     286                                if ( dead_eindex == -1 ) return;
     287                                if ( dead_eindex != static_cast<index_type>( m_data.size() - 1 ) )
     288                                {
     289                                        m_data[unsigned( dead_eindex )] = move( m_data.back() );
     290                                }
     291                                m_data.pop_back();
     292                        }
     293
    267294                        virtual bool handle_message( const message_type& )
    268295                        {
  • trunk/nv/engine/particle_engine.hh

    r439 r499  
    137137        };
    138138
     139        struct particle_system_group_tag {};
     140        typedef handle< uint32, 16, 16, particle_system_group_tag > particle_system_group;
     141        struct particle_system_tag {};
     142        typedef handle< uint32, 16, 16, particle_system_tag > particle_system;
    139143
    140144        struct particle_system_info
    141145        {
    142                 bool test;
    143146                uint32                last_update;
    144147                particle*             particles;
    145                 particle_quad*        quads;
    146148                particle_emmiter_info emmiters[MAX_PARTICLE_EMMITERS];
    147149
    148150                uint32                count;
     151                vec2                  texcoords[4];
     152                particle_system_group group;
     153
     154                const particle_system_data*    data;
     155        };
     156
     157        struct particle_system_group_info
     158        {
     159                uint32                count;
     160                uint32                quota;
    149161                vertex_array          vtx_array;
    150162                buffer                vtx_buffer;
    151                 //              bool                           own_va;
    152                 //              uint32                         offset;
    153 
    154                 const particle_system_data*    data;
    155         };
    156 
    157         struct particle_system_tag {};
    158         typedef handle< uint32, 16, 16, particle_system_tag > particle_system;
     163                bool                  local;
     164                particle_quad*        quads;
     165        };
    159166
    160167        class particle_engine
     
    164171                void reset();
    165172                void load( lua::table_guard& table );
    166                 void draw( particle_system system, const render_state& rs, const scene_state& ss );
    167                 particle_system create_system( const string_view& id );
     173                void draw( particle_system_group group, const render_state& rs, const scene_state& ss );
     174                void prepare( particle_system_group group );
     175                particle_system_group create_group( uint32 max_particles );
     176                particle_system create_system( const string_view& id, particle_system_group group );
     177                void release( particle_system_group group );
    168178                void release( particle_system system );
    169                 void update( particle_system system, const scene_state& s, uint32 ms );
     179                void update( const scene_state& s, uint32 ms );
     180                void update( particle_system system );
    170181                void set_texcoords( particle_system system, vec2 a, vec2 b );
    171182                void register_emmiter_type( const string_view& name, particle_emmiter_func func );
     
    193204                vec3     m_camera_pos;
    194205                uint32   m_last_update;
    195 
    196                 handle_store< particle_system_info, particle_system > m_systems;
     206                vec2     m_tc_a;
     207                vec2     m_tc_b;
     208
     209                handle_store< particle_system_info, particle_system >             m_systems;
     210                handle_store< particle_system_group_info, particle_system_group > m_groups;
     211
    197212                hash_store< shash64, uint32 >                         m_names;
    198213                vector< particle_system_data >                        m_data;
  • trunk/nv/gl/gl_context.hh

    r493 r499  
    6060                virtual void update( texture t, const void* data );
    6161                virtual void update( buffer b, const void* data, size_t offset, size_t size );
     62                virtual void* map_buffer( buffer, buffer_access, size_t /*offset*/, size_t /*length*/ );
     63                virtual void unmap_buffer( buffer );
     64
    6265                //              virtual void update( buffer b, uint32 index, const void* data, size_t offset, size_t size );
    6366
  • trunk/nv/gl/gl_enum.hh

    r438 r499  
    4343        unsigned int output_slot_to_enum( output_slot slot );
    4444
     45        unsigned int buffer_access_to_bitfield( buffer_access type );
    4546        unsigned int datatype_to_gl_enum( datatype type );
    4647        datatype gl_enum_to_datatype( unsigned int gl_enum );
  • trunk/nv/interface/context.hh

    r492 r499  
    177177                virtual void update( texture, const void* ) = 0;
    178178                virtual void update( buffer, const void*, size_t /*offset*/, size_t /*size*/ ) = 0;
     179                virtual void* map_buffer( buffer, buffer_access, size_t /*offset*/, size_t /*length*/ ) = 0;
     180                virtual void unmap_buffer( buffer ) = 0;
     181
    179182
    180183//              virtual void update( buffer, uint32 /*index*/, const void* , size_t /*offset*/, size_t /*size*/ ) = 0;
  • trunk/nv/interface/device.hh

    r492 r499  
    152152        };
    153153
     154        enum buffer_access
     155        {
     156                WRITE_UNSYNCHRONIZED,
     157        };
     158
    154159        struct buffer_info
    155160        {
  • trunk/src/engine/particle_engine.cc

    r491 r499  
    470470}
    471471
    472 nv::particle_system nv::particle_engine::create_system( const string_view& id )
     472nv::particle_system nv::particle_engine::create_system( const string_view& id, particle_system_group group )
    473473{
    474474        auto it = m_names.find( id );
     
    480480        particle_system result = m_systems.create();
    481481        particle_system_info* info = m_systems.get( result );
    482 
     482        info->group = group;
    483483        info->data     = data;
    484484        uint32 ecount = data->emmiter_count;
     
    496496        info->count = 0;
    497497        info->particles = new particle[ data->quota ];
    498         info->quads     = new particle_quad[ data->quota ];
    499 
     498        info->last_update = m_last_update;
     499
     500        return result;
     501}
     502
     503void nv::particle_engine::prepare( particle_system_group group )
     504{
     505        particle_system_group_info* info = m_groups.get( group );
     506        if ( info )
     507        {
     508                info->count = 0;
     509        }
     510}
     511
     512void nv::particle_engine::draw( particle_system_group group, const render_state& rs, const scene_state& ss )
     513{
     514        particle_system_group_info* info = m_groups.get( group );
     515        if ( info )
     516        {
     517                m_context->draw( nv::TRIANGLES, rs, ss, info->local ? m_program_local : m_program_world, info->vtx_array, info->count * 6, 0 );
     518        }
     519}
     520
     521nv::particle_system_group nv::particle_engine::create_group( uint32 max_particles )
     522{
     523        particle_system_group result     = m_groups.create();
     524        particle_system_group_info* info = m_groups.get( result );
     525        info->local = false;
     526        info->count = 0;
     527        info->quota = max_particles;
     528        info->vtx_buffer = m_device->create_buffer( VERTEX_BUFFER, STREAM_DRAW, info->quota * sizeof( particle_quad )/*, info->quads_[0].data*/ );
    500529        vertex_array_desc desc;
    501         info->vtx_buffer = m_device->create_buffer( VERTEX_BUFFER, STREAM_DRAW, data->quota * 6 * sizeof( particle_vtx ), info->quads[0].data );
    502530        desc.add_vertex_buffers< particle_vtx >( info->vtx_buffer, true );
    503531        info->vtx_array = m_context->create_vertex_array( desc );
    504         info->last_update = m_last_update;
    505         info->test = false;
    506 //      result->m_own_va      = true;
    507 //      result->m_offset      = 0;
    508 
     532        info->quads     = new particle_quad[info->quota];
    509533        return result;
    510 }
    511 
    512 void nv::particle_engine::draw( particle_system system, const render_state& rs, const scene_state& ss )
    513 {
    514         particle_system_info* info = m_systems.get( system );
    515         if ( info )
    516         {
    517                 m_context->draw( nv::TRIANGLES, rs, ss, info->data->local ?  m_program_local : m_program_world, info->vtx_array, info->count * 6 );
    518         }
    519534}
    520535
     
    537552        while ( m_systems.size() > 0 )
    538553                release( m_systems.get_handle( 0 ) );
     554        while ( m_groups.size() > 0 )
     555                release( m_groups.get_handle( 0 ) );
    539556        m_emmiters.clear();
    540557        m_affectors.clear();
     
    551568        {
    552569                delete[] info->particles;
     570                m_systems.destroy( system );
     571        }
     572}
     573
     574void nv::particle_engine::release( particle_system_group group )
     575{
     576        particle_system_group_info* info = m_groups.get( group );
     577        if ( info )
     578        {
    553579                delete[] info->quads;
    554                 //if ( system->own_va )
    555580                m_context->release( info->vtx_array );
    556                 m_systems.destroy( system );
    557         }
    558 }
    559 
    560 void nv::particle_engine::update( particle_system system, const scene_state& s, uint32 ms )
     581                m_groups.destroy( group );
     582        }
     583}
     584
     585void nv::particle_engine::update( const scene_state& s, uint32 ms )
     586{
     587        m_last_update += ms;
     588        m_view_matrix  = s.get_view();
     589        m_model_matrix = s.get_model();
     590        m_camera_pos   = s.get_camera().get_position();
     591        m_inv_view_dir = math::normalize(-s.get_camera().get_direction());
     592}
     593
     594void nv::particle_engine::update( particle_system system )
    561595{
    562596        particle_system_info* info = m_systems.get( system );
    563         m_last_update += ms;
    564597        if ( info )
    565598        {
    566                 m_view_matrix  = s.get_view();
    567                 m_model_matrix = s.get_model();
    568                 m_camera_pos   = s.get_camera().get_position();
    569                 m_inv_view_dir = math::normalize(-s.get_camera().get_direction());
    570 
    571599                update_emmiters( info, m_last_update );
    572600                destroy_particles( info, m_last_update );
     
    575603
    576604                generate_data( info );
    577                 m_context->update( info->vtx_buffer, info->quads, /*system->m_offset*sizeof(particle_quad)*/ 0, info->count*sizeof(particle_quad) );
    578605        }
    579606}
     
    585612        {
    586613                vec2 texcoords[4] = { a, vec2( b.x, a.y ), vec2( a.x, b.y ), b };
    587 
    588                 for ( uint32 i = 0; i < info->data->quota; ++i )
    589                 {
    590                         particle_quad& rdata   = info->quads[i];
    591                         rdata.data[0].texcoord = texcoords[0];
    592                         rdata.data[1].texcoord = texcoords[1];
    593                         rdata.data[2].texcoord = texcoords[2];
    594                         rdata.data[3].texcoord = texcoords[3];
    595                         rdata.data[4].texcoord = texcoords[2];
    596                         rdata.data[5].texcoord = texcoords[1];
    597                 }
     614                for ( uint32 i = 0; i < 4; ++i )
     615                        info->texcoords[i] = texcoords[i];
    598616        }
    599617}
     
    601619void nv::particle_engine::generate_data( particle_system_info* info )
    602620{
     621//      void* rawptr = m_context->map_buffer( info->vtx_buffer, nv::WRITE_UNSYNCHRONIZED, offset, info->count*sizeof( particle_quad ) );
     622//      particle_quad* quads = reinterpret_cast<particle_quad*>( rawptr );
     623
     624        particle_system_group_info* group = m_groups.get( info->group );
     625        if ( !info )
     626        {
     627                return;
     628        }
     629
    603630        vec2 lb     = vec2( -0.5f, -0.5f );
    604631        vec2 rt     = vec2( 0.5f, 0.5f );
     
    634661        vec3 pdir( 0.0f, 1.0f, 0.0f );
    635662
     663        vec2 texcoords[4] =
     664        {
     665                info->texcoords[0],
     666                info->texcoords[1],
     667                info->texcoords[2],
     668                info->texcoords[3],
     669        };
     670
    636671        for ( uint32 i = 0; i < info->count; ++i )
    637672        {
    638673                const particle& pdata = info->particles[i];
    639                 particle_quad& rdata  = info->quads[i];
     674//              particle_quad& rdata  = quads[i];
     675                particle_quad& rdata  = group->quads[i + group->count];
    640676
    641677                vec3 view_dir( m_inv_view_dir );
     
    676712                rdata.data[0].position = pdata.position + s0;
    677713                rdata.data[0].color    = pdata.color;
     714                rdata.data[0].texcoord = texcoords[0];
    678715
    679716                rdata.data[1].position = pdata.position + s1;
    680717                rdata.data[1].color    = pdata.color;
     718                rdata.data[1].texcoord = texcoords[1];
    681719
    682720                rdata.data[2].position = pdata.position + s2;
    683721                rdata.data[2].color    = pdata.color;
     722                rdata.data[2].texcoord = texcoords[2];
    684723
    685724                rdata.data[3].position = pdata.position + s3;
    686725                rdata.data[3].color    = pdata.color;
     726                rdata.data[3].texcoord = texcoords[3];
    687727
    688728                rdata.data[4] = rdata.data[2];
    689729                rdata.data[5] = rdata.data[1];
    690730        }
     731//      m_context->unmap_buffer( info->vtx_buffer );
     732
     733        m_context->update( group->vtx_buffer, group->quads, group->count*sizeof(particle_quad), info->count*sizeof( particle_quad ) );
     734        group->count += info->count;
    691735}
    692736
  • trunk/src/gfx/debug_draw.cc

    r491 r499  
    4545                if ( m_va.is_valid() ) m_context->release( m_va );
    4646                m_buffer_size = nv::max( m_data.size(), 4096U, m_buffer_size );
    47                 m_vb = m_context->get_device()->create_buffer( VERTEX_BUFFER, nv::STREAM_DRAW, m_buffer_size * sizeof( debug_vtx ), m_data.data() );
     47                m_vb = m_context->get_device()->create_buffer( VERTEX_BUFFER, nv::STREAM_DRAW, m_buffer_size * sizeof( debug_vtx ) );
    4848                vertex_array_desc va_desc;
    4949                va_desc.add_vertex_buffers< debug_vtx >( m_vb, true );
    5050                m_va = m_context->create_vertex_array( va_desc );
    5151        }
    52         else
    53         {
    54                 m_context->update( m_vb, m_data.data(), 0, m_data.size()* sizeof( debug_vtx ) );
    55         }
     52        m_context->update( m_vb, m_data.data(), 0, m_data.size()* sizeof( debug_vtx ) );
    5653}
    5754
  • trunk/src/gl/gl_context.cc

    r493 r499  
    428428                        glTexImage2D( gl_type, 0, static_cast<GLint>( nv::image_format_to_internal_enum(format.format) ), size.x, size.y, 0, nv::image_format_to_enum(format.format), nv::datatype_to_gl_enum(format.type), data );
    429429        }
     430}
     431
     432void* nv::gl_context::map_buffer( buffer b, buffer_access ba, size_t offset, size_t length )
     433{
     434        const gl_buffer_info* info = static_cast<const gl_buffer_info*>( m_device->get_buffer_info( b ) );
     435        unsigned int glenum = buffer_type_to_enum( info->type );
     436        glBindBuffer( glenum, info->glid );
     437        void* result = glMapBufferRange( glenum, GLintptr( offset ), GLsizeiptr( length ), buffer_access_to_bitfield( ba ) );
     438        if ( result == nullptr )
     439        {
     440                while ( GLenum err = glGetError() )
     441                switch ( err )
     442                {
     443                case GL_INVALID_VALUE     : NV_LOG_ERROR( "map_buffer failed : GL_INVALID_VALUE" ) break;
     444                case GL_INVALID_OPERATION : NV_LOG_ERROR( "map_buffer failed : GL_INVALID_OPERATION " ) break;
     445                case GL_OUT_OF_MEMORY     : NV_LOG_ERROR( "map_buffer failed : GL_OUT_OF_MEMORY " ) break;
     446                default:
     447                        break;
     448                }
     449        }
     450        return result;
     451}
     452
     453void nv::gl_context::unmap_buffer( buffer b )
     454{
     455        const gl_buffer_info* info = static_cast<const gl_buffer_info*>( m_device->get_buffer_info( b ) );
     456        glUnmapBuffer( buffer_type_to_enum( info->type ) );
    430457}
    431458
  • trunk/src/gl/gl_enum.cc

    r492 r499  
    287287}
    288288
    289 
    290289unsigned int nv::output_slot_to_enum( output_slot slot )
    291290{
     
    307306}
    308307
     308
     309unsigned int nv::buffer_access_to_bitfield( buffer_access type )
     310{
     311        switch ( type )
     312        {
     313        case WRITE_UNSYNCHRONIZED     : return GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT;
     314        default: return 0; // TODO: throw!
     315        }
     316}
    309317
    310318unsigned int nv::datatype_to_gl_enum( datatype type )
Note: See TracChangeset for help on using the changeset viewer.