Changeset 508 for trunk


Ignore:
Timestamp:
07/19/16 13:43:10 (9 years ago)
Author:
epyon
Message:
  • nv::engine upgrades
  • default_resource_manager implementation
Location:
trunk
Files:
6 added
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/engine/animation.hh

    r505 r508  
    354354        };
    355355
    356         class animator_manager : public nv::lua_resource_manager< animator_data >
     356        class animator_manager : public file_resource_manager< animator_data, true, lua_resource_manager_base >
    357357        {
    358358        public:
  • trunk/nv/engine/mesh_manager.hh

    r506 r508  
    2626        enum shader_geo
    2727        {
    28                 NORMAL = 0,
     28                DIRECT = 0,
    2929                BONE = 1,
    3030        };
  • trunk/nv/engine/model_manager.hh

    r507 r508  
    9696                }
    9797        protected:
    98                 static void flatten( flat_model& result, const model_node* m, random_base& rng, const transform& ptr, sint32 parent_id )
     98                static void flatten( flat_model& result, const model_node* m, random_base& rng, const transform& ptr, sint16 parent_id )
    9999                {
    100100                        if ( m->chance < 1.0f )
     
    114114                                re.parent_id = parent_id;
    115115                                re.attach_id = m->attach_id;
    116                                 parent_id = id;
     116                                parent_id = sint16(id);
    117117                        }
    118118                        for ( auto c : m->children )
  • trunk/nv/engine/renderer.hh

    r507 r508  
    1818#include <nv/interface/device.hh>
    1919#include <nv/interface/context.hh>
     20#include <nv/engine/program_manager.hh>
     21#include <nv/engine/material_manager.hh>
     22#include <nv/engine/mesh_manager.hh>
     23#include <nv/engine/render_pass.hh>
    2024
    2125namespace nv
    2226{
    23         struct render_pass
     27
     28        struct renderer_element
    2429        {
    25                 render_state rstate;
    26                 clear_state  cstate;
    27                 framebuffer  fbuffer;
    28                 output_slot  output[8];
    29                 uint32       output_count;
     30                resource< gpu_mesh >     mesh;
     31                resource< gpu_material > mat;
     32                uint32                   layer;
     33                sint32                   bone_offset;
     34                transform                model;
     35                flags< 32 >              flags;
     36        };
    3037
     38        struct renderer_direct_element
     39        {
     40                vertex_array             va;
     41                uint32                   count;
     42                uint32                   instances;
     43                uint32                   first;
     44                resource< gpu_material > material;
     45                resource< program >      programs[4];
     46                transform                model;
     47                flags< 32 >              flags;
     48        };
     49
     50        struct renderer_element_compare
     51        {
     52                bool operator()( const renderer_element& l, const renderer_element& r )
     53                {
     54                        if ( l.layer < r.layer ) return true;
     55                        if ( l.layer > r.layer ) return false;
     56                        if ( *l.flags.data() < *r.flags.data() ) return true;
     57                        if ( *l.flags.data() > *r.flags.data() ) return false;
     58                        if ( l.mat < r.mat ) return true;
     59                        if ( l.mat > r.mat ) return false;
     60                        return false;
     61                }
    3162        };
    3263
    3364        class renderer
    3465        {
     66        public:
     67                enum stats
     68                {
     69                        DRAW_CALL,
     70                        TRIANGLE_COUNT,
     71                        MAX
     72                };
     73
     74                explicit renderer( context* ctx );
     75                void start_frame()
     76                {
     77                        for ( uint32& s : m_statistics ) s = 0;
     78                }
     79
     80                void push_element( const nv::renderer_element& element )
     81                {
     82                        m_elements.push_back( element );
     83                }
     84
     85                void sort();
     86                void render( const scene_state& ss, const render_pass& pass );
     87                void render( const scene_state& ss, const render_pass& pass, vertex_array va, uint32 va_count, uint32 program_idx = 0 );
     88                void render_quad( const scene_state& ss, const render_pass& pass, uint32 program_idx = 0 )
     89                {
     90                        render( ss, pass, m_quad, 6, program_idx );
     91                }
     92                void render_direct( const scene_state& ss, const render_pass& pass, const nv::array_view< renderer_direct_element >& elements, uint32 program_idx = 0 );
     93
     94                uint32 get_stat( stats s ) const { return m_statistics[s]; }
     95
     96                void end_frame()
     97                {
     98                        m_elements.clear();
     99                }
     100                void reset()
     101                {
     102                        m_elements.clear();
     103                }
     104
     105        protected:
     106                bool match( flags<32> f, const render_pass& pass )
     107                {
     108                        if ( ( f & pass.any ).empty() ) return false;
     109                        if ( ( f & pass.exclude ).any() ) return false;
     110                        return true;
     111                }
     112
     113                context*                    m_context;
     114                vertex_array                m_quad;
     115                vector< renderer_element >  m_elements;
     116                uint32                      m_statistics[MAX];
    35117
    36118        };
  • trunk/nv/interface/device.hh

    r506 r508  
    6363        enum texture_slot
    6464        {
    65                 TEX_DIFFUSE   = 0,
    66                 TEX_NORMAL    = 1,
    67                 TEX_METALLIC  = 2,
    68                 TEX_SPECULAR  = 2, // obsolete
    69                 TEX_ROUGHNESS = 3,
    70                 TEX_GLOSS     = 3, // obsolete
    71                 TEX_EMISSIVE  = 4,
    72                 TEXTURE_0     = 0,
    73                 TEXTURE_1     = 1,
    74                 TEXTURE_2     = 2,
    75                 TEXTURE_3     = 3,
    76                 TEXTURE_4     = 4,
    77                 TEXTURE_5     = 5,
    78                 TEXTURE_6     = 6,
    79                 TEXTURE_7     = 7,
    80                 TEXTURE_8     = 8,
    81                 TEXTURE_9     = 9,
    82                 TEXTURE_10    = 10,
    83                 TEXTURE_11    = 11,
    84                 TEXTURE_12    = 12,
    85                 TEXTURE_13    = 13,
    86                 TEXTURE_14    = 14,
    87                 TEXTURE_15    = 15,
     65                TEXTURE_0     = 0,  TEX_DIFFUSE   = 0,
     66                TEXTURE_1     = 1,  TEX_NORMAL    = 1,
     67                TEXTURE_2     = 2,  TEX_METALLIC  = 2,
     68                TEXTURE_3     = 3,  TEX_ROUGHNESS = 3,
     69                TEXTURE_4     = 4,  TEX_EMISSIVE  = 4,
     70                TEXTURE_5     = 5,  TEX_DEPTH     = 5,
     71                TEXTURE_6     = 6,  TEX_OCCLUSION = 6,
     72                TEXTURE_7     = 7,  TEX_FINAL     = 7,
     73                TEXTURE_8     = 8,  TEX_CUSTOM_0  = 8,
     74                TEXTURE_9     = 9,  TEX_CUSTOM_1  = 9,
     75                TEXTURE_10    = 10, TEX_CUSTOM_2  = 10,
     76                TEXTURE_11    = 11, TEX_CUSTOM_3  = 11,
     77                TEXTURE_12    = 12, TEX_CUSTOM_4  = 12,
     78                TEXTURE_13    = 13, TEX_CUSTOM_5  = 13,
     79                TEXTURE_14    = 14, TEX_CUSTOM_6  = 14,
     80                TEXTURE_15    = 15, TEX_CUSTOM_7  = 15,
    8881                MAX_TEXTURES  = 16,
    8982        };
     
    355348                        factory_link_map[ "nv_t_normal"   ] = new engine_link_uniform_int<int( TEX_NORMAL )>();
    356349                        factory_link_map[ "nv_t_metallic" ] = new engine_link_uniform_int<int( TEX_METALLIC )>();
    357                         factory_link_map[ "nv_t_specular" ] = new engine_link_uniform_int<int( TEX_METALLIC )>();
    358350                        factory_link_map[ "nv_t_roughness"] = new engine_link_uniform_int<int( TEX_ROUGHNESS )>();
    359                         factory_link_map[ "nv_t_gloss"    ] = new engine_link_uniform_int<int( TEX_ROUGHNESS )>();
    360351                        factory_link_map[ "nv_t_emissive" ] = new engine_link_uniform_int<int( TEX_EMISSIVE )>();
     352                        factory_link_map[ "nv_t_depth"    ] = new engine_link_uniform_int<int( TEX_DEPTH )>();
     353                        factory_link_map[ "nv_t_occlusion"] = new engine_link_uniform_int<int( TEX_OCCLUSION )>();
     354                        factory_link_map[ "nv_t_final"    ] = new engine_link_uniform_int<int( TEX_FINAL )>();
    361355                }
    362356                void destroy_engine_uniforms()
  • trunk/nv/io/c_stream.hh

    r484 r508  
    1616#include <nv/common.hh>
    1717#include <nv/stl/stream.hh>
     18#include <nv/stl/string.hh>
    1819
    1920namespace nv
     
    3940                virtual void flush();
    4041        private:
    41                 void*       m_file;      //!< FILE* structure, masked
    42                 const char* m_file_name; //!< File name
    43                 size_t      m_file_size; //!< Cached file size
     42                void*        m_file;      //!< FILE* structure, masked
     43                const_string m_file_name; //!< File name
     44                size_t       m_file_size; //!< Cached file size
    4445        };
    4546
  • trunk/nv/stl/flags.hh

    r406 r508  
    183183                }
    184184
     185                bool any() const
     186                {
     187                        for ( auto c : m_data )
     188                                if ( c != 0 ) return true;
     189                        return false;
     190                }
     191
     192                bool empty() const
     193                {
     194                        for ( auto c : m_data )
     195                                if ( c != 0 ) return false;
     196                        return true;
     197                }
     198
    185199                const data_type* data() const
    186200                {
     
    212226                        return reference( this, idx );
    213227                }
     228
     229                template < uint32 SIZE, typename T >
     230                inline friend flags< SIZE, T > operator &( const flags< SIZE, T >& lhs, const flags< SIZE, T >& rhs );
     231
     232                template < uint32 SIZE, typename T >
     233                inline friend flags< SIZE, T > operator |( const flags< SIZE, T >& lhs, const flags< SIZE, T >& rhs );
    214234
    215235        private:
     
    217237        };
    218238
     239        template < uint32 SIZE, typename T >
     240        inline flags< SIZE, T > operator &( const flags< SIZE, T >& lhs, const flags< SIZE, T >& rhs )
     241        {
     242                flags< SIZE, T > result( lhs );
     243                for ( uint32 i = 0; i < result.data_size; ++i )
     244                        result.m_data[i] &= rhs.m_data[i];
     245                return result;
     246        }
     247
     248        template < uint32 SIZE, typename T >
     249        inline flags< SIZE, T > operator |( const flags< SIZE, T >& lhs, const flags< SIZE, T >& rhs )
     250        {
     251                flags< SIZE, T > result( lhs );
     252                for ( uint32 i = 0; i < result.data_size; ++i )
     253                        result.m_data[i] |= rhs.m_data[i];
     254                return result;
     255        }
     256
     257
    219258} // namespace nv
    220259
  • trunk/src/engine/animation.cc

    r487 r508  
    4141                {
    4242                        nv::c_file_system fs;
    43                         nv::stream* poses_file = fs.open( poses_path );
     43                        nv::stream* poses_file = open_stream( fs, poses_path );
     44                        NV_ASSERT_ALWAYS( poses_file, "CANT FIND POSES FILE!" );
    4445                        nv::nmd_loader* ploader = new nv::nmd_loader( nullptr );
    4546                        ploader->load( *poses_file );
  • trunk/src/engine/material_manager.cc

    r505 r508  
    3939
    4040                // HACK
    41                 for ( uint32 i = 0; i < 8; ++i )
    42                         if ( result->textures[i].is_nil() )
    43                                 result->textures[i] = m_default;
     41                for ( uint32 i = 0; i < 5; ++i )
     42                        if ( result->textures[i].is_nil() )
     43                                result->textures[i] = m_default;
    4444
    4545                return add( id, result );
     
    7777                                if ( i != TEX_EMISSIVE )
    7878                                        NV_LOG_ERROR( "Texture file not found! : ", m->paths[i] );
    79                                 m->paths[i].clear();
     79                                //m->paths[i].clear();
    8080                        }
    8181                }
  • trunk/src/engine/mesh_manager.cc

    r505 r508  
    1818                gm->va = m_context->create_vertex_array( &*lmesh, STATIC_DRAW );
    1919                gm->count = lmesh->get_channel_size( slot::INDEX );
    20                 gm->shader = lmesh->get_channel( slot::BONEINDEX ) != nullptr ? BONE : NORMAL;
     20                gm->shader = lmesh->get_channel( slot::BONEINDEX ) != nullptr ? BONE : DIRECT;
    2121                return add( mesh.id(), gm );
    2222        }
  • trunk/src/engine/model_manager.cc

    r507 r508  
    6464                if ( table.is_number( "attach" ) )
    6565                {
    66                         attach_id = table.get_integer( "attach", -1 );
     66                        attach_id = sint16( table.get_integer( "attach", -1 ) );
    6767                        //                              parent_id = 0;
    6868                }
     
    7171                        auto it = m->node_names.find( table.get_string_hash_64( "attach" ) );
    7272                        if ( it != m->node_names.end() )
    73                                 attach_id = it->second + 1;
     73                                attach_id = sint16( it->second + 1 );
    7474                        int error; int hack;
    7575                }
  • trunk/src/engine/renderer.cc

    r507 r508  
    66
    77#include "nv/engine/renderer.hh"
     8
     9#include "nv/core/profiler.hh"
     10
     11using namespace nv;
     12
     13renderer::renderer( context* ctx )
     14        : m_context( ctx )
     15{
     16        for ( auto& i : m_statistics ) i = 0;
     17
     18        struct qvtx
     19        {
     20                nv::vec2 position;
     21                nv::vec2 texcoord;
     22        };
     23
     24        qvtx quad[] = {
     25                qvtx{ vec2( -1.0f,-1.0f ), vec2( 0.0f, 0.0f ) },
     26                qvtx{ vec2( 1.0f,-1.0f ),  vec2( 1.0f, 0.0f ) },
     27                qvtx{ vec2( 1.0f,1.0f ),   vec2( 1.0f, 1.0f ) },
     28                qvtx{ vec2( 1.0f,1.0f ),   vec2( 1.0f, 1.0f ) },
     29                qvtx{ vec2( -1.0f,1.0f ),  vec2( 0.0f, 1.0f ) },
     30                qvtx{ vec2( -1.0f,-1.0f ), vec2( 0.0f, 0.0f ) },
     31        };
     32        m_quad = m_context->create_vertex_array( quad, 6, STATIC_DRAW );
     33}
     34
     35void renderer::sort()
     36{
     37        stable_sort( m_elements.begin(), m_elements.end(), renderer_element_compare() );
     38}
     39
     40
     41void renderer::render( const scene_state& s, const render_pass& pass )
     42{
     43        scene_state ss( s );
     44        m_context->bind( pass.fbuffer, FRAMEBUFFER );
     45        m_context->set_draw_buffers( pass.output_count, pass.output );
     46        m_context->set_viewport( ss.get_viewport() );
     47
     48        if ( pass.cstate.buffers != buffer_mask::NO_BUFFER )
     49                m_context->clear( pass.cstate );
     50
     51        for ( uint32 i = 0; i < size( pass.binds ); ++i )
     52                if ( pass.binds[i] )
     53                        m_context->bind( pass.binds[i], texture_slot( i ) );
     54
     55        resource_id current_mat;
     56        mat4 model = ss.get_model();
     57        for ( const renderer_element& element : m_elements )
     58                if ( match( element.flags, pass ) )
     59                {
     60                        ss.set_model( model * element.model.extract() );
     61                        if ( auto mesh = element.mesh.lock() )
     62                                if ( mesh->count > 0 )
     63                                        if ( auto program = pass.programs[mesh->shader].lock() )
     64                                        {
     65                                                if ( element.mat.id() != current_mat )
     66                                                {
     67                                                        if ( auto mat = element.mat.lock() )
     68                                                        {
     69                                                                for ( uint32 i = 0; i < 8; ++i )
     70                                                                        if ( mat->textures[i] && !pass.binds[i] )
     71                                                                                m_context->bind( mat->textures[i], nv::texture_slot( i ) );
     72                                                                current_mat = element.mat.id();
     73                                                        }
     74                                                }
     75
     76                                                m_context->get_device()->set_opt_uniform( *program, "nv_bone_offset", element.bone_offset );
     77                                                m_context->get_device()->set_opt_uniform( *program, "nv_flags", unsigned( *element.flags.data() ) );
     78
     79                                                m_context->apply_engine_uniforms( *program, ss );
     80                                                m_context->draw( TRIANGLES, pass.rstate, *program, mesh->va, mesh->count );
     81
     82                                                m_statistics[TRIANGLE_COUNT] += mesh->count / 3;
     83                                                m_statistics[DRAW_CALL]++;
     84                                        }
     85                }
     86
     87        m_context->bind( pass.fbuffer, FRAMEBUFFER );
     88}
     89
     90void renderer::render( const scene_state& ss, const render_pass& pass, vertex_array va, uint32 va_count, uint32 program_idx )
     91{
     92        m_context->bind( pass.fbuffer, FRAMEBUFFER );
     93        m_context->set_draw_buffers( pass.output_count, pass.output );
     94        m_context->set_viewport( ss.get_viewport() );
     95
     96        if ( pass.cstate.buffers != buffer_mask::NO_BUFFER )
     97                m_context->clear( pass.cstate );
     98
     99        for ( uint32 i = 0; i < size( pass.binds ); ++i )
     100                if ( pass.binds[i] )
     101                        m_context->bind( pass.binds[i], texture_slot(i) );
     102
     103        if ( auto program = pass.programs[program_idx].lock() )
     104        {
     105                m_context->apply_engine_uniforms( *program, ss );
     106                m_context->draw( TRIANGLES, pass.rstate, *program, va, va_count );
     107                m_statistics[ TRIANGLE_COUNT ] += 2;
     108                m_statistics[ DRAW_CALL ]++;
     109        }
     110
     111        m_context->bind( pass.fbuffer, FRAMEBUFFER );
     112}
     113
     114void nv::renderer::render_direct( const scene_state& s, const render_pass& pass, const nv::array_view< renderer_direct_element >& elements, uint32 program_idx )
     115{
     116        if ( m_elements.size() == 0 ) return;
     117
     118        scene_state ss( s );
     119        m_context->bind( pass.fbuffer, FRAMEBUFFER );
     120        m_context->set_draw_buffers( pass.output_count, pass.output );
     121        m_context->set_viewport( ss.get_viewport() );
     122
     123        if ( pass.cstate.buffers != buffer_mask::NO_BUFFER )
     124                m_context->clear( pass.cstate );
     125
     126        for ( uint32 i = 0; i < size( pass.binds ); ++i )
     127                if ( pass.binds[i] )
     128                        m_context->bind( pass.binds[i], texture_slot( i ) );
     129
     130        resource_id current_mat;
     131        resource_id current_prog;
     132
     133        mat4 model = ss.get_model();
     134        for ( uint32 index = 0; index < elements.size(); ++index )
     135        {
     136                const renderer_direct_element& element = elements[index];
     137                if ( match( element.flags, pass ) )
     138                {
     139                        ss.set_model( model * element.model.extract() );
     140                        if ( element.count > 0 )
     141                        {
     142                                if ( auto program = element.programs[program_idx].lock() )
     143                                {
     144                                        if ( element.material.id() != current_mat )
     145                                        {
     146                                                if ( auto mat = element.material.lock() )
     147                                                {
     148                                                        for ( uint32 i = 0; i < 8; ++i )
     149                                                                if ( mat->textures[i] && !pass.binds[i] )
     150                                                                        m_context->bind( mat->textures[i], nv::texture_slot( i ) );
     151                                                        current_mat = element.material.id();
     152                                                }
     153                                        }
     154
     155                                        m_context->get_device()->set_opt_uniform( *program, "nv_flags", unsigned( *element.flags.data() ) );
     156                                        m_context->get_device()->set_opt_uniform( *program, "nv_index", unsigned( index ) );
     157                                        m_context->apply_engine_uniforms( *program, ss );
     158
     159                                        if ( element.instances > 0 )
     160                                                m_context->draw_instanced( TRIANGLES, pass.rstate, *program, element.instances, element.va, element.count, element.first );
     161                                        else
     162                                                m_context->draw( TRIANGLES, pass.rstate, *program, element.va, element.count, element.first );
     163
     164                                        m_statistics[TRIANGLE_COUNT] += ( element.count / 3 ) * max< uint32 >( element.instances, 1 );
     165                                        m_statistics[DRAW_CALL]++;
     166                                }
     167                        }
     168                }
     169        }
     170        m_context->bind( pass.fbuffer, FRAMEBUFFER );
     171}
  • trunk/src/io/c_stream.cc

    r487 r508  
    7575        {
    7676                struct stat fstat;
    77                 int result = stat(m_file_name, &fstat );
     77                int result = stat(m_file_name.data(), &fstat );
    7878                if ( result != 0 )
    7979                {
Note: See TracChangeset for help on using the changeset viewer.