Changeset 232 for trunk


Ignore:
Timestamp:
05/08/14 17:28:37 (11 years ago)
Author:
epyon
Message:
  • automatic link-time and draw-time uniforms added
Location:
trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/interface/camera.hh

    r230 r232  
    4141                }
    4242        private:
     43                bool m_dirty;
    4344                mat4 m_projection;
    4445                mat4 m_view;
    4546        };
     47
     48        // TODO - this and camera should have dirty states
     49        class scene_state
     50        {
     51        public:
     52                const camera& get_camera() const   { return m_camera; }
     53                camera& get_camera()               { return m_camera; }
     54                void set_camera( const camera& c ) { m_camera = c; }
     55                void set_model( const mat4& m )    { m_model  = m; }
     56
     57                const mat4& get_model()      const { return m_model; }
     58                const mat4& get_view()       const { return m_camera.get_view(); }
     59                const mat4& get_projection() const { return m_camera.get_projection(); }
     60                mat4 get_modelview()  const { return get_view() * m_model; }
     61                mat4 get_mvp()        const { return m_camera.get_projection() * get_modelview(); }
     62
     63                mat4 get_view_inv()   const { return glm::inverse( get_view() ); }
     64                mat3 get_normal()     const { return glm::transpose(glm::inverse(glm::mat3( get_modelview() ) ) ); }
     65        protected:
     66                mat4   m_model;
     67                camera m_camera;
     68        };
     69
     70
    4671}
    4772
  • trunk/nv/interface/context.hh

    r231 r232  
    1414
    1515#include <nv/common.hh>
     16#include <nv/interface/camera.hh>
    1617#include <nv/interface/program.hh>
    1718#include <nv/interface/vertex_buffer.hh>
     
    5152        {
    5253        public:
    53                 context( device* a_device ) { m_device = a_device; }
     54                context( device* a_device )     
     55                {
     56                        // TODO: this will pose a problem in case of multiple contexts
     57                        program::initialize_engine_uniforms();
     58                        m_device = a_device;
     59                }
    5460                virtual void clear( const clear_state& cs ) = 0;
    5561                // temporary
    5662                virtual void draw( primitive prim, const render_state& rs, program* p, vertex_array* va, size_t count ) = 0;
     63
    5764                virtual void draw( const render_state& rs, program* p, mesh_interface* mesh )
    5865                {
    5966                        draw( mesh->get_primitive_type(), rs, p, mesh->get_vertex_array(), mesh->get_index_count() );
     67                }
     68                virtual void draw( const render_state& rs, const scene_state& s, program* p, mesh_interface* mesh )
     69                {
     70                        p->apply_engine_uniforms( this, &s );
     71                        draw( rs, p, mesh );
     72                }
     73                virtual void draw( primitive prim, const render_state& rs, const scene_state& s, program* p, vertex_array* va, size_t count )
     74                {
     75                        p->apply_engine_uniforms( this, &s );
     76                        draw( prim, rs, p, va, count );
    6077                }
    6178
     
    6481                virtual void set_viewport( const ivec4& viewport ) = 0;
    6582                virtual device* get_device() { return m_device; }
    66                 virtual ~context() {}
     83                virtual ~context()
     84                {
     85                        // TODO: this will pose a problem in case of multiple contexts
     86                        program::destroy_engine_uniforms();
     87                }
    6788        protected:
    6889                device*      m_device;
  • trunk/nv/interface/program.hh

    r214 r232  
    1 // Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
     1// Copyright (C) 2012-2014 ChaosForge / Kornel Kisielewicz
    22// http://chaosforge.org/
    33//
     
    1414
    1515#include <unordered_map>
     16#include <nv/interface/uniform.hh>
    1617#include <nv/logging.hh>
    1718#include <nv/exception.hh>
     
    2223namespace nv
    2324{
     25        class camera;
     26
    2427        enum slot
    2528        {
     
    3033                TANGENT  = 4,
    3134        };
     35
     36        enum texture_slot
     37        {
     38                TEX_DIFFUSE  = 0,
     39                TEX_SPECULAR = 1,
     40                TEX_NORMAL   = 2,
     41                TEXTURE_0    = 0,
     42                TEXTURE_1    = 1,
     43                TEXTURE_2    = 2,
     44                TEXTURE_3    = 3,
     45                TEXTURE_4    = 4,
     46                TEXTURE_5    = 5,
     47                TEXTURE_6    = 6,
     48                TEXTURE_7    = 7,
     49        };
     50
    3251
    3352        class attribute
     
    4867        };
    4968
    50         class uniform_base
    51         {
    52         public:
    53                 uniform_base( const string& name, datatype type, int location, int length )
    54                         : m_name( name ), m_type( type ), m_location(location), m_length( length ), m_dirty( true ) {}
    55                 datatype get_type() const { return m_type; }
    56                 int get_location() const { return m_location; }
    57                 int get_length() const { return m_length; }
    58                 bool is_dirty() const { return m_dirty; }
    59                 void clean() { m_dirty = false; }
    60         protected:
    61                 string   m_name;
    62                 datatype m_type;
    63                 int      m_location;
    64                 int      m_length;
    65                 bool     m_dirty;
    66         };
    67 
    68         template< typename T >
    69         class uniform : public uniform_base
    70         {
    71         public:
    72                 typedef T value_type;
    73 
    74                 uniform( const string& name, int location, int length )
    75                         : uniform_base( name, type_to_enum< T >::type, location, length ), m_value()
    76                 {}
    77 
    78                 void set_value( const T& value )
    79                 {
    80                         if ( value != m_value )
    81                         {
    82                                 m_value = value;
    83                                 m_dirty = true;
    84                         }
    85                 }
    86 
    87                 const T& get_value() { return m_value; }
    88         protected:
    89                 T m_value;
    90         };
    91 
    92         typedef std::unordered_map< string, uniform_base* > uniform_map;
    9369        typedef std::unordered_map< string, attribute* >    attribute_map;
    9470
     
    10278                virtual ~program()
    10379                {
    104                         for ( attribute_map::iterator i = m_attribute_map.begin();
    105                                 i != m_attribute_map.end(); ++i ) delete i->second;
    106                         for ( uniform_map::iterator i = m_uniform_map.begin();
    107                                         i != m_uniform_map.end(); ++i ) delete i->second;
     80                        for ( auto& i : m_attribute_map )   delete i.second;
     81                        for ( auto& i : m_uniform_map )     delete i.second;
    10882                }
    10983
     
    181155                        }
    182156                }
     157
     158                void apply_engine_uniforms( const context* ctx, const scene_state* s )
     159                {
     160                        for ( auto u : m_engine_uniforms )
     161                        {
     162                                u->set( ctx, s );
     163                        }
     164                }
     165
     166                void apply_link_engine_uniforms()
     167                {
     168                        engine_link_uniform_factory_map& factory_map = get_link_uniform_factory();
     169                        for ( auto& i : m_uniform_map )
     170                        {
     171                                auto j = factory_map.find( i.first );
     172                                if ( j != factory_map.end() )
     173                                {
     174                                        j->second->set( i.second );
     175                                }                               
     176                        }
     177                }
     178
     179                void bind_engine_uniforms()
     180                {
     181                        engine_uniform_factory_map& factory_map = get_uniform_factory();
     182                        for ( auto& i : m_uniform_map )
     183                        {
     184                                auto j = factory_map.find( i.first );
     185                                if ( j != factory_map.end() )
     186                                {
     187                                        m_engine_uniforms.push_back( j->second->create( i.second ) );
     188                                }                               
     189                        }
     190                }
     191
     192                // This is done this way to avoid compilation unit creation
     193                static engine_uniform_factory_map& get_uniform_factory()
     194                {
     195                        static engine_uniform_factory_map s_engine_uniform_factory_map;
     196                        return s_engine_uniform_factory_map;
     197                }
     198
     199                // This is done this way to avoid compilation unit creation
     200                static engine_link_uniform_factory_map& get_link_uniform_factory()
     201                {
     202                        static engine_link_uniform_factory_map s_engine_link_uniform_factory_map;
     203                        return s_engine_link_uniform_factory_map;
     204                }
     205
     206                static void initialize_engine_uniforms()
     207                {
     208                        engine_uniform_factory_map& factory_map = get_uniform_factory();
     209                        factory_map[ "nv_m_view" ]       = new engine_uniform_factory< engine_uniform_m_view >();
     210                        factory_map[ "nv_m_view_inv" ]   = new engine_uniform_factory< engine_uniform_m_view_inv >();
     211                        factory_map[ "nv_m_view" ]       = new engine_uniform_factory< engine_uniform_m_view >();
     212                        factory_map[ "nv_m_model" ]      = new engine_uniform_factory< engine_uniform_m_model >();
     213                        factory_map[ "nv_m_modelview" ]  = new engine_uniform_factory< engine_uniform_m_modelview >();
     214                        factory_map[ "nv_m_projection" ] = new engine_uniform_factory< engine_uniform_m_projection >();
     215                        factory_map[ "nv_m_normal" ]     = new engine_uniform_factory< engine_uniform_m_normal >();
     216                        factory_map[ "nv_m_mvp" ]        = new engine_uniform_factory< engine_uniform_m_mvp >();
     217
     218                        engine_link_uniform_factory_map& factory_link_map = get_link_uniform_factory();
     219                        factory_link_map[ "nv_texture_0" ] = new engine_link_uniform_int<0>();
     220                        factory_link_map[ "nv_texture_1" ] = new engine_link_uniform_int<1>();
     221                        factory_link_map[ "nv_texture_2" ] = new engine_link_uniform_int<2>();
     222                        factory_link_map[ "nv_texture_3" ] = new engine_link_uniform_int<3>();
     223                        factory_link_map[ "nv_texture_4" ] = new engine_link_uniform_int<4>();
     224                        factory_link_map[ "nv_texture_5" ] = new engine_link_uniform_int<5>();
     225                        factory_link_map[ "nv_texture_6" ] = new engine_link_uniform_int<6>();
     226                        factory_link_map[ "nv_texture_7" ] = new engine_link_uniform_int<7>();
     227                        factory_link_map[ "nv_t_diffuse" ] = new engine_link_uniform_int<0>();
     228                        factory_link_map[ "nv_t_specular"] = new engine_link_uniform_int<1>();
     229                        factory_link_map[ "nv_t_normal"  ] = new engine_link_uniform_int<2>();
     230                }
     231
     232                static void destroy_engine_uniforms()
     233                {
     234                        for ( auto& i : get_uniform_factory() ) delete i.second;
     235                        for ( auto& i : get_link_uniform_factory() ) delete i.second;
     236                        get_uniform_factory().clear();
     237                        get_link_uniform_factory().clear();
     238                }
     239
    183240        protected:
     241
    184242                uniform_base* create_uniform( datatype utype, const string& name, int location, int length )
    185243                {
     
    201259                }
    202260
    203                 attribute_map m_attribute_map;
    204                 uniform_map   m_uniform_map;
     261                attribute_map                     m_attribute_map;
     262                uniform_map                           m_uniform_map;
     263                engine_uniform_list               m_engine_uniforms;
    205264        };
    206265
  • trunk/src/gl/gl_program.cc

    r161 r232  
    189189                m_uniform_map[ name ] = create_uniform( utype, name, uni_loc, uni_len );
    190190        }
     191
     192        apply_link_engine_uniforms();
     193        bind_engine_uniforms();
    191194}
    192195
Note: See TracChangeset for help on using the changeset viewer.