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

Last change on this file since 313 was 313, checked in by epyon, 11 years ago
  • cleanup of context and device interfaces
  • create_vertex_array moved to context (as it's context bound)
  • added partial framebuffer functions to context
File size: 8.4 KB
Line 
1// Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
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/common.hh>
16#include <nv/string.hh>
17#include <nv/handle.hh>
18#include <nv/interface/uniform.hh>
19#include <nv/interface/mesh_data.hh>
20#include <nv/interface/vertex_buffer.hh>
21#include <nv/interface/image_data.hh>
22
23namespace nv
24{
25        class window;
26
27        enum texture_slot
28        {
29                TEX_DIFFUSE  = 0,
30                TEX_SPECULAR = 1,
31                TEX_NORMAL   = 2,
32                TEXTURE_0    = 0,
33                TEXTURE_1    = 1,
34                TEXTURE_2    = 2,
35                TEXTURE_3    = 3,
36                TEXTURE_4    = 4,
37                TEXTURE_5    = 5,
38                TEXTURE_6    = 6,
39                TEXTURE_7    = 7,
40        };
41
42
43        struct attribute
44        {
45                string   name;
46                int      location;
47                datatype type;
48                int      length;
49        };
50
51        typedef std::unordered_map< string, attribute >    attribute_map;
52
53        struct texture_tag {};
54        struct buffer_tag {};
55        struct program_tag {};
56        typedef handle< uint32, 16, 16, buffer_tag >       buffer;
57        typedef handle< uint32, 16, 16, texture_tag >      texture;
58        typedef handle< uint32, 16, 16, program_tag >      program;
59
60        struct sampler
61        {
62                enum filter
63                {
64                        LINEAR,
65                        NEAREST,
66                        NEAREST_MIPMAP_NEAREST,
67                        LINEAR_MIPMAP_NEAREST,
68                        NEAREST_MIPMAP_LINEAR,
69                        LINEAR_MIPMAP_LINEAR
70                };
71                enum wrap
72                {
73                        CLAMP_TO_EDGE,
74                        CLAMP_TO_BORDER,
75                        MIRRORED_REPEAT,
76                        REPEAT
77                };
78
79                filter filter_min;
80                filter filter_max;
81                wrap wrap_s;
82                wrap wrap_t;
83
84                sampler() : filter_min( LINEAR ), filter_max( LINEAR ), wrap_s( REPEAT ), wrap_t( REPEAT ) {}
85                sampler( filter min, filter max, wrap s, wrap t )
86                        : filter_min( min ), filter_max( max ), wrap_s( s ), wrap_t( t ) {}
87                sampler( filter f, wrap w )
88                        : filter_min( f ), filter_max( f ), wrap_s( w ), wrap_t( w ) {}
89
90        };
91
92        struct buffer_info
93        {
94                buffer_type type;
95                buffer_hint hint;
96                size_t      size;
97        };
98
99
100        struct texture_info
101        {
102                ivec2        size;
103                image_format format;
104                sampler      sampler;
105        };
106
107        struct vertex_buffer_attribute
108        {
109                buffer   vbuffer;
110                datatype dtype;
111                size_t   components;
112                size_t   offset;
113                size_t   stride;
114                slot     location;
115                bool     owner;
116        };
117
118        struct program_info
119        {
120                attribute_map       m_attribute_map;
121                uniform_map             m_uniform_map;
122                engine_uniform_list m_engine_uniforms;
123        };
124
125        class device
126        {
127                friend class context;
128        public:
129                device()
130                {
131                        initialize_engine_uniforms();
132                }
133                virtual window* create_window( uint16 width, uint16 height, bool fullscreen ) = 0;
134                virtual window* adopt_window( void* sys_w_handle, void* sys_dc ) = 0;
135                virtual program create_program( const string& vs_source, const string& fs_source ) = 0;
136                virtual buffer create_buffer( buffer_type type, buffer_hint hint, size_t size, const void* source = nullptr ) = 0;
137                virtual image_data* create_image_data( const std::string& filename ) = 0; // temporary
138                virtual texture create_texture( ivec2 size, image_format aformat, sampler asampler, void* data = nullptr ) = 0;
139                virtual void release( texture ) = 0;
140                virtual void release( buffer ) = 0;
141                virtual void release( program ) = 0;
142                virtual const texture_info* get_texture_info( texture ) const = 0;
143                virtual const buffer_info* get_buffer_info( buffer ) const = 0;
144                virtual uint32 get_ticks() = 0;
145                virtual void delay( uint32 ms ) = 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.