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

Last change on this file since 323 was 323, checked in by epyon, 11 years ago
  • nova now compiles again under all three compilers with -Winsane and no warnings
File size: 8.2 KB
Line 
1// Copyright (C) 2012-2014 ChaosForge Ltd
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
15#include <nv/core/common.hh>
16#include <nv/core/string.hh>
17#include <nv/core/handle.hh>
18#include <nv/interface/uniform.hh>
19#include <nv/interface/mesh_data.hh>
20#include <nv/interface/image_data.hh>
21
22namespace nv
23{
24        class window;
25
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
52        struct texture_tag {};
53        struct buffer_tag {};
54        struct program_tag {};
55        typedef handle< uint32, 16, 16, buffer_tag >       buffer;
56        typedef handle< uint32, 16, 16, texture_tag >      texture;
57        typedef handle< uint32, 16, 16, program_tag >      program;
58
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
91        struct buffer_info
92        {
93                buffer_type type;
94                buffer_hint hint;
95                size_t      size;
96        };
97
98
99        struct texture_info
100        {
101                ivec2        size;
102                image_format format;
103                sampler      tsampler;
104        };
105
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        };
116
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
124        class device
125        {
126                friend class context;
127        public:
128                device()
129                {
130                        initialize_engine_uniforms();
131                }
132                virtual window* create_window( uint16 width, uint16 height, bool fullscreen ) = 0;
133                virtual window* adopt_window( void* sys_w_handle, void* sys_dc ) = 0;
134                virtual program create_program( const string& vs_source, const string& fs_source ) = 0;
135                virtual buffer create_buffer( buffer_type type, buffer_hint hint, size_t size, const void* source = nullptr ) = 0;
136                virtual image_data* create_image_data( const std::string& filename ) = 0; // temporary
137                virtual texture create_texture( ivec2 size, image_format aformat, sampler asampler, void* data = nullptr ) = 0;
138                virtual void release( texture ) = 0;
139                virtual void release( buffer ) = 0;
140                virtual void release( program ) = 0;
141                virtual const texture_info* get_texture_info( texture ) const = 0;
142                virtual const buffer_info* get_buffer_info( buffer ) const = 0;
143                virtual uint32 get_ticks() = 0;
144                virtual void delay( uint32 ms ) = 0;
145                virtual const string& get_shader_header() const = 0;
146
147                virtual texture create_texture( image_data* data, sampler asampler )
148                {
149                        return create_texture( data->get_size(), data->get_format(), asampler, (void*)data->get_data() );
150                }
151
152                int try_get_attribute_location( program p, const string& name ) const
153                {
154                        return get_attribute_location( p, name, false );
155                }
156
157                virtual int get_attribute_location( program p, const string& name, bool fatal = true ) const = 0;
158
159                template < typename T >
160                void set_uniform_array( program p, const string& name, const T* value, uint32 count, bool fatal = true )
161                {
162                        uniform_base* base = get_uniform( p, name, fatal );
163                        if ( base != nullptr )
164                        {
165                                if ( base->type_check( type_to_enum<T>::type ) )
166                                {
167                                        // TODO: nicer check
168                                        NV_ASSERT( (int)count <= base->get_length(), "LENGTH CHECK FAIL" );
169                                        ((uniform<T>*)( base ))->set_value( value, count );
170                                }
171                        }
172                }
173
174                template < typename T >
175                void set_opt_uniform_array( program p, const string& name, const T* value, uint32 count )
176                {
177                        set_uniform_array( p, name, value, count, false );
178                }
179
180                template < typename T >
181                void set_opt_uniform_array( program p, const string& name, const std::vector<T>& value )
182                {
183                        set_uniform_array( p, name, (const T*)value.data(), value.size(), false );
184                }
185
186
187                template < typename T >
188                void set_uniform( program p, const string& name, const T& value, bool fatal = true )
189                {
190                        uniform_base* base = get_uniform( p, name, fatal );
191                        if ( base != nullptr )
192                        {
193                                if ( base->type_check( type_to_enum<T>::type ) )
194                                {
195                                        ((uniform<T>*)( base ))->set_value( value );
196                                }
197                        }
198                }
199
200                template < typename T >
201                void set_opt_uniform( program p, const string& name, const T& value )
202                {
203                        set_uniform( p, name, value, false );
204                }
205
206                virtual ~device()
207                {
208                        destroy_engine_uniforms();
209                }
210
211                // This is done this way to avoid compilation unit creation
212                static engine_uniform_factory_map& get_uniform_factory()
213                {
214                        static engine_uniform_factory_map s_engine_uniform_factory_map;
215                        return s_engine_uniform_factory_map;
216                }
217
218                // This is done this way to avoid compilation unit creation
219                static engine_link_uniform_factory_map& get_link_uniform_factory()
220                {
221                        static engine_link_uniform_factory_map s_engine_link_uniform_factory_map;
222                        return s_engine_link_uniform_factory_map;
223                }
224
225                virtual void prepare_program( program p ) = 0;
226
227        protected:
228                virtual uniform_base* get_uniform( program p, const string& name, bool fatal = true ) const = 0;
229
230                void initialize_engine_uniforms()
231                {
232                        engine_uniform_factory_map& factory_map = get_uniform_factory();
233                        factory_map[ "nv_m_view" ]       = new engine_uniform_factory< engine_uniform_m_view >();
234                        factory_map[ "nv_m_view_inv" ]   = new engine_uniform_factory< engine_uniform_m_view_inv >();
235                        factory_map[ "nv_m_model" ]      = new engine_uniform_factory< engine_uniform_m_model >();
236                        factory_map[ "nv_m_model_inv" ]  = new engine_uniform_factory< engine_uniform_m_model_inv >();
237                        factory_map[ "nv_m_modelview" ]  = new engine_uniform_factory< engine_uniform_m_modelview >();
238                        factory_map[ "nv_m_projection" ] = new engine_uniform_factory< engine_uniform_m_projection >();
239                        factory_map[ "nv_m_normal" ]     = new engine_uniform_factory< engine_uniform_m_normal >();
240                        factory_map[ "nv_m_mvp" ]        = new engine_uniform_factory< engine_uniform_m_mvp >();
241                        factory_map[ "nv_v_camera_position" ]  = new engine_uniform_factory< engine_uniform_v_camera_position >();
242                        factory_map[ "nv_v_camera_direction" ] = new engine_uniform_factory< engine_uniform_v_camera_direction >();
243
244                        engine_link_uniform_factory_map& factory_link_map = get_link_uniform_factory();
245                        factory_link_map[ "nv_texture_0" ] = new engine_link_uniform_int<0>();
246                        factory_link_map[ "nv_texture_1" ] = new engine_link_uniform_int<1>();
247                        factory_link_map[ "nv_texture_2" ] = new engine_link_uniform_int<2>();
248                        factory_link_map[ "nv_texture_3" ] = new engine_link_uniform_int<3>();
249                        factory_link_map[ "nv_texture_4" ] = new engine_link_uniform_int<4>();
250                        factory_link_map[ "nv_texture_5" ] = new engine_link_uniform_int<5>();
251                        factory_link_map[ "nv_texture_6" ] = new engine_link_uniform_int<6>();
252                        factory_link_map[ "nv_texture_7" ] = new engine_link_uniform_int<7>();
253                        factory_link_map[ "nv_t_diffuse" ] = new engine_link_uniform_int<0>();
254                        factory_link_map[ "nv_t_specular"] = new engine_link_uniform_int<1>();
255                        factory_link_map[ "nv_t_normal"  ] = new engine_link_uniform_int<2>();
256                }
257                void destroy_engine_uniforms()
258                {
259                        for ( auto& i : get_uniform_factory() ) delete i.second;
260                        for ( auto& i : get_link_uniform_factory() ) delete i.second;
261                        get_uniform_factory().clear();
262                        get_link_uniform_factory().clear();
263                }
264
265        };
266
267} // namespace nv
268
269
270#endif // NV_DEVICE_HH
Note: See TracBrowser for help on using the repository browser.