source: trunk/nv/interface/device.hh @ 326

Last change on this file since 326 was 326, checked in by epyon, 11 years ago
  • window_manager interface added
  • input interface added
  • sdl::window_manager and sdl::input added
  • gl_device/gl_window/gl_context made oblivious to sdl
  • currently some setup is required, application class needed
File size: 8.0 KB
RevLine 
[319]1// Copyright (C) 2012-2014 ChaosForge Ltd
[32]2// http://chaosforge.org/
3//
4// This file is part of NV Libraries.
5// For conditions of distribution and use, see copyright notice in nv.hh
6/**
7 * @file device.hh
8 * @author Kornel Kisielewicz epyon@chaosforge.org
9 * @brief Device class
10 */
11
12#ifndef NV_DEVICE_HH
13#define NV_DEVICE_HH
14
[319]15#include <nv/core/common.hh>
16#include <nv/core/string.hh>
17#include <nv/core/handle.hh>
[303]18#include <nv/interface/uniform.hh>
[238]19#include <nv/interface/mesh_data.hh>
[90]20#include <nv/interface/image_data.hh>
[32]21
22namespace nv
23{
24        class window;
25
[303]26        enum texture_slot
27        {
28                TEX_DIFFUSE  = 0,
29                TEX_SPECULAR = 1,
30                TEX_NORMAL   = 2,
31                TEXTURE_0    = 0,
32                TEXTURE_1    = 1,
33                TEXTURE_2    = 2,
34                TEXTURE_3    = 3,
35                TEXTURE_4    = 4,
36                TEXTURE_5    = 5,
37                TEXTURE_6    = 6,
38                TEXTURE_7    = 7,
39        };
40
41
42        struct attribute
43        {
44                string   name;
45                int      location;
46                datatype type;
47                int      length;
48        };
49
50        typedef std::unordered_map< string, attribute >    attribute_map;
51
[302]52        struct texture_tag {};
53        struct buffer_tag {};
[303]54        struct program_tag {};
[302]55        typedef handle< uint32, 16, 16, buffer_tag >       buffer;
56        typedef handle< uint32, 16, 16, texture_tag >      texture;
[303]57        typedef handle< uint32, 16, 16, program_tag >      program;
[302]58
[301]59        struct sampler
60        {
61                enum filter
62                {
63                        LINEAR,
64                        NEAREST,
65                        NEAREST_MIPMAP_NEAREST,
66                        LINEAR_MIPMAP_NEAREST,
67                        NEAREST_MIPMAP_LINEAR,
68                        LINEAR_MIPMAP_LINEAR
69                };
70                enum wrap
71                {
72                        CLAMP_TO_EDGE,
73                        CLAMP_TO_BORDER,
74                        MIRRORED_REPEAT,
75                        REPEAT
76                };
77
78                filter filter_min;
79                filter filter_max;
80                wrap wrap_s;
81                wrap wrap_t;
82
83                sampler() : filter_min( LINEAR ), filter_max( LINEAR ), wrap_s( REPEAT ), wrap_t( REPEAT ) {}
84                sampler( filter min, filter max, wrap s, wrap t )
85                        : filter_min( min ), filter_max( max ), wrap_s( s ), wrap_t( t ) {}
86                sampler( filter f, wrap w )
87                        : filter_min( f ), filter_max( f ), wrap_s( w ), wrap_t( w ) {}
88
89        };
90
[302]91        struct buffer_info
92        {
93                buffer_type type;
94                buffer_hint hint;
95                size_t      size;
96        };
97
98
[301]99        struct texture_info
100        {
101                ivec2        size;
102                image_format format;
[323]103                sampler      tsampler;
[301]104        };
105
[302]106        struct vertex_buffer_attribute
107        {
108                buffer   vbuffer;
109                datatype dtype;
110                size_t   components;
111                size_t   offset;
112                size_t   stride;
113                slot     location;
114                bool     owner;
115        };
[301]116
[303]117        struct program_info
118        {
119                attribute_map       m_attribute_map;
120                uniform_map             m_uniform_map;
121                engine_uniform_list m_engine_uniforms;
122        };
123
[32]124        class device
125        {
[302]126                friend class context;
[32]127        public:
[303]128                device()
129                {
130                        initialize_engine_uniforms();
131                }
132                virtual program create_program( const string& vs_source, const string& fs_source ) = 0;
[302]133                virtual buffer create_buffer( buffer_type type, buffer_hint hint, size_t size, const void* source = nullptr ) = 0;
[326]134                virtual texture create_texture( ivec2 size, image_format aformat, sampler asampler, void* data = nullptr ) = 0;
[90]135                virtual image_data* create_image_data( const std::string& filename ) = 0; // temporary
[302]136                virtual void release( texture ) = 0;
137                virtual void release( buffer ) = 0;
[303]138                virtual void release( program ) = 0;
139                virtual const texture_info* get_texture_info( texture ) const = 0;
140                virtual const buffer_info* get_buffer_info( buffer ) const = 0;
[316]141                virtual const string& get_shader_header() const = 0;
[303]142
[301]143                virtual texture create_texture( image_data* data, sampler asampler )
[292]144                {
[301]145                        return create_texture( data->get_size(), data->get_format(), asampler, (void*)data->get_data() );
[292]146                }
147
[303]148                int try_get_attribute_location( program p, const string& name ) const
149                {
150                        return get_attribute_location( p, name, false );
151                }
[238]152
[303]153                virtual int get_attribute_location( program p, const string& name, bool fatal = true ) const = 0;
154
155                template < typename T >
156                void set_uniform_array( program p, const string& name, const T* value, uint32 count, bool fatal = true )
157                {
158                        uniform_base* base = get_uniform( p, name, fatal );
159                        if ( base != nullptr )
160                        {
161                                if ( base->type_check( type_to_enum<T>::type ) )
162                                {
163                                        // TODO: nicer check
164                                        NV_ASSERT( (int)count <= base->get_length(), "LENGTH CHECK FAIL" );
165                                        ((uniform<T>*)( base ))->set_value( value, count );
166                                }
167                        }
168                }
169
170                template < typename T >
171                void set_opt_uniform_array( program p, const string& name, const T* value, uint32 count )
172                {
173                        set_uniform_array( p, name, value, count, false );
174                }
175
176                template < typename T >
177                void set_opt_uniform_array( program p, const string& name, const std::vector<T>& value )
178                {
179                        set_uniform_array( p, name, (const T*)value.data(), value.size(), false );
180                }
181
182
183                template < typename T >
184                void set_uniform( program p, const string& name, const T& value, bool fatal = true )
185                {
186                        uniform_base* base = get_uniform( p, name, fatal );
187                        if ( base != nullptr )
188                        {
189                                if ( base->type_check( type_to_enum<T>::type ) )
190                                {
191                                        ((uniform<T>*)( base ))->set_value( value );
192                                }
193                        }
194                }
195
196                template < typename T >
197                void set_opt_uniform( program p, const string& name, const T& value )
198                {
199                        set_uniform( p, name, value, false );
200                }
201
[302]202                virtual ~device()
203                {
[303]204                        destroy_engine_uniforms();
[302]205                }
[303]206
207                // This is done this way to avoid compilation unit creation
208                static engine_uniform_factory_map& get_uniform_factory()
209                {
210                        static engine_uniform_factory_map s_engine_uniform_factory_map;
211                        return s_engine_uniform_factory_map;
212                }
213
214                // This is done this way to avoid compilation unit creation
215                static engine_link_uniform_factory_map& get_link_uniform_factory()
216                {
217                        static engine_link_uniform_factory_map s_engine_link_uniform_factory_map;
218                        return s_engine_link_uniform_factory_map;
219                }
220
221                virtual void prepare_program( program p ) = 0;
222
[302]223        protected:
[303]224                virtual uniform_base* get_uniform( program p, const string& name, bool fatal = true ) const = 0;
225
226                void initialize_engine_uniforms()
227                {
228                        engine_uniform_factory_map& factory_map = get_uniform_factory();
229                        factory_map[ "nv_m_view" ]       = new engine_uniform_factory< engine_uniform_m_view >();
230                        factory_map[ "nv_m_view_inv" ]   = new engine_uniform_factory< engine_uniform_m_view_inv >();
231                        factory_map[ "nv_m_model" ]      = new engine_uniform_factory< engine_uniform_m_model >();
232                        factory_map[ "nv_m_model_inv" ]  = new engine_uniform_factory< engine_uniform_m_model_inv >();
233                        factory_map[ "nv_m_modelview" ]  = new engine_uniform_factory< engine_uniform_m_modelview >();
234                        factory_map[ "nv_m_projection" ] = new engine_uniform_factory< engine_uniform_m_projection >();
235                        factory_map[ "nv_m_normal" ]     = new engine_uniform_factory< engine_uniform_m_normal >();
236                        factory_map[ "nv_m_mvp" ]        = new engine_uniform_factory< engine_uniform_m_mvp >();
237                        factory_map[ "nv_v_camera_position" ]  = new engine_uniform_factory< engine_uniform_v_camera_position >();
238                        factory_map[ "nv_v_camera_direction" ] = new engine_uniform_factory< engine_uniform_v_camera_direction >();
239
240                        engine_link_uniform_factory_map& factory_link_map = get_link_uniform_factory();
241                        factory_link_map[ "nv_texture_0" ] = new engine_link_uniform_int<0>();
242                        factory_link_map[ "nv_texture_1" ] = new engine_link_uniform_int<1>();
243                        factory_link_map[ "nv_texture_2" ] = new engine_link_uniform_int<2>();
244                        factory_link_map[ "nv_texture_3" ] = new engine_link_uniform_int<3>();
245                        factory_link_map[ "nv_texture_4" ] = new engine_link_uniform_int<4>();
246                        factory_link_map[ "nv_texture_5" ] = new engine_link_uniform_int<5>();
247                        factory_link_map[ "nv_texture_6" ] = new engine_link_uniform_int<6>();
248                        factory_link_map[ "nv_texture_7" ] = new engine_link_uniform_int<7>();
249                        factory_link_map[ "nv_t_diffuse" ] = new engine_link_uniform_int<0>();
250                        factory_link_map[ "nv_t_specular"] = new engine_link_uniform_int<1>();
251                        factory_link_map[ "nv_t_normal"  ] = new engine_link_uniform_int<2>();
252                }
253                void destroy_engine_uniforms()
254                {
255                        for ( auto& i : get_uniform_factory() ) delete i.second;
256                        for ( auto& i : get_link_uniform_factory() ) delete i.second;
257                        get_uniform_factory().clear();
258                        get_link_uniform_factory().clear();
259                }
260
[32]261        };
262
263} // namespace nv
264
265
266#endif // NV_DEVICE_HH
Note: See TracBrowser for help on using the repository browser.