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

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