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

Last change on this file since 319 was 319, checked in by epyon, 11 years ago
  • created core module and moved all free source files there
  • took the opportunity to update all copyright lines and minor cleanup
  • minor fixes
  • not all examples are up to date
File size: 8.4 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      sampler;
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_uniform_array( const string& name, const std::vector<T>& value )
176                {
177                        set_uniform_array( program p, name, (const T*)value.data(), value.size() );
178                }
179
180                template < typename T >
181                void set_opt_uniform_array( program p, const string& name, const T* value, uint32 count )
182                {
183                        set_uniform_array( p, name, value, count, false );
184                }
185
186                template < typename T >
187                void set_opt_uniform_array( program p, const string& name, const std::vector<T>& value )
188                {
189                        set_uniform_array( p, name, (const T*)value.data(), value.size(), false );
190                }
191
192
193                template < typename T >
194                void set_uniform( program p, const string& name, const T& value, bool fatal = true )
195                {
196                        uniform_base* base = get_uniform( p, name, fatal );
197                        if ( base != nullptr )
198                        {
199                                if ( base->type_check( type_to_enum<T>::type ) )
200                                {
201                                        ((uniform<T>*)( base ))->set_value( value );
202                                }
203                        }
204                }
205
206                template < typename T >
207                void set_opt_uniform( program p, const string& name, const T& value )
208                {
209                        set_uniform( p, name, value, false );
210                }
211
212                virtual ~device()
213                {
214                        destroy_engine_uniforms();
215                }
216
217                // This is done this way to avoid compilation unit creation
218                static engine_uniform_factory_map& get_uniform_factory()
219                {
220                        static engine_uniform_factory_map s_engine_uniform_factory_map;
221                        return s_engine_uniform_factory_map;
222                }
223
224                // This is done this way to avoid compilation unit creation
225                static engine_link_uniform_factory_map& get_link_uniform_factory()
226                {
227                        static engine_link_uniform_factory_map s_engine_link_uniform_factory_map;
228                        return s_engine_link_uniform_factory_map;
229                }
230
231                virtual void prepare_program( program p ) = 0;
232
233        protected:
234                virtual uniform_base* get_uniform( program p, const string& name, bool fatal = true ) const = 0;
235
236                void initialize_engine_uniforms()
237                {
238                        engine_uniform_factory_map& factory_map = get_uniform_factory();
239                        factory_map[ "nv_m_view" ]       = new engine_uniform_factory< engine_uniform_m_view >();
240                        factory_map[ "nv_m_view_inv" ]   = new engine_uniform_factory< engine_uniform_m_view_inv >();
241                        factory_map[ "nv_m_model" ]      = new engine_uniform_factory< engine_uniform_m_model >();
242                        factory_map[ "nv_m_model_inv" ]  = new engine_uniform_factory< engine_uniform_m_model_inv >();
243                        factory_map[ "nv_m_modelview" ]  = new engine_uniform_factory< engine_uniform_m_modelview >();
244                        factory_map[ "nv_m_projection" ] = new engine_uniform_factory< engine_uniform_m_projection >();
245                        factory_map[ "nv_m_normal" ]     = new engine_uniform_factory< engine_uniform_m_normal >();
246                        factory_map[ "nv_m_mvp" ]        = new engine_uniform_factory< engine_uniform_m_mvp >();
247                        factory_map[ "nv_v_camera_position" ]  = new engine_uniform_factory< engine_uniform_v_camera_position >();
248                        factory_map[ "nv_v_camera_direction" ] = new engine_uniform_factory< engine_uniform_v_camera_direction >();
249
250                        engine_link_uniform_factory_map& factory_link_map = get_link_uniform_factory();
251                        factory_link_map[ "nv_texture_0" ] = new engine_link_uniform_int<0>();
252                        factory_link_map[ "nv_texture_1" ] = new engine_link_uniform_int<1>();
253                        factory_link_map[ "nv_texture_2" ] = new engine_link_uniform_int<2>();
254                        factory_link_map[ "nv_texture_3" ] = new engine_link_uniform_int<3>();
255                        factory_link_map[ "nv_texture_4" ] = new engine_link_uniform_int<4>();
256                        factory_link_map[ "nv_texture_5" ] = new engine_link_uniform_int<5>();
257                        factory_link_map[ "nv_texture_6" ] = new engine_link_uniform_int<6>();
258                        factory_link_map[ "nv_texture_7" ] = new engine_link_uniform_int<7>();
259                        factory_link_map[ "nv_t_diffuse" ] = new engine_link_uniform_int<0>();
260                        factory_link_map[ "nv_t_specular"] = new engine_link_uniform_int<1>();
261                        factory_link_map[ "nv_t_normal"  ] = new engine_link_uniform_int<2>();
262                }
263                void destroy_engine_uniforms()
264                {
265                        for ( auto& i : get_uniform_factory() ) delete i.second;
266                        for ( auto& i : get_link_uniform_factory() ) delete i.second;
267                        get_uniform_factory().clear();
268                        get_link_uniform_factory().clear();
269                }
270
271        };
272
273} // namespace nv
274
275
276#endif // NV_DEVICE_HH
Note: See TracBrowser for help on using the repository browser.