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

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