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

Last change on this file since 412 was 412, checked in by epyon, 10 years ago
  • refactoring of raw_data_channel and data_descriptor access
File size: 9.1 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        enum buffer_hint
118        {
119                STATIC_DRAW,
120                STREAM_DRAW,
121                DYNAMIC_DRAW
122        };
123
124        enum buffer_type
125        {
126                VERTEX_BUFFER,
127                INDEX_BUFFER,
128        };
129
130        struct buffer_info
131        {
132                buffer_type type;
133                buffer_hint hint;
134                size_t      size;
135        };
136
137
138        struct texture_info
139        {
140                texture_type type;
141                ivec2        size;
142                image_format format;
143                sampler      tsampler;
144        };
145
146        struct vertex_buffer_attribute
147        {
148                buffer   vbuffer;
149                datatype dtype;
150                size_t   components;
151                size_t   offset;
152                size_t   stride;
153                slot     location;
154                bool     owner;
155        };
156
157        struct program_info
158        {
159                attribute_map*       m_attribute_map;
160                uniform_map*         m_uniform_map;
161                engine_uniform_list* m_engine_uniforms;
162        };
163
164        class device
165        {
166                friend class context;
167        public:
168                device()
169                {
170                        initialize_engine_uniforms();
171                }
172                virtual program create_program( string_view vs_source, string_view fs_source ) = 0;
173                virtual buffer create_buffer( buffer_type type, buffer_hint hint, size_t size, const void* source = nullptr ) = 0;
174                virtual texture create_texture( texture_type type, ivec2 size, image_format aformat, sampler asampler, const void* data = nullptr ) = 0;
175                // TODO: remove?
176                virtual texture create_texture( ivec2 size, image_format aformat, sampler asampler, const void* data = nullptr ) { return create_texture( TEXTURE_2D, size, aformat, asampler, data ); }
177                virtual image_data* create_image_data( string_view filename ) = 0; // temporary
178                virtual image_data* create_image_data( const uint8* data, uint32 size ) = 0; // temporary
179                virtual void release( texture ) = 0;
180                virtual void release( buffer ) = 0;
181                virtual void release( program ) = 0;
182                virtual const texture_info* get_texture_info( texture ) const = 0;
183                virtual const buffer_info* get_buffer_info( buffer ) const = 0;
184                virtual const std::string& get_shader_header() const = 0;
185
186                virtual texture create_texture( image_data* data, sampler asampler )
187                {
188                        return create_texture( data->get_size(), data->get_format(), asampler, data->get_data() );
189                }
190
191                int try_get_attribute_location( program p, const std::string& name ) const
192                {
193                        return get_attribute_location( p, name, false );
194                }
195
196                virtual int get_attribute_location( program p, const std::string& name, bool fatal = true ) const = 0;
197
198                template < typename T >
199                void set_uniform_array( program p, const std::string& name, const T* value, uint32 count, bool fatal = true )
200                {
201                        uniform_base* base = get_uniform( p, name, fatal );
202                        if ( base != nullptr )
203                        {
204                                if ( base->type_check( type_to_enum<T>::type ) )
205                                {
206                                        // TODO: nicer check
207                                        NV_ASSERT( static_cast<int>( count ) <= base->get_length(), "LENGTH CHECK FAIL" );
208                                        static_cast< uniform<T>* >( base )->set_value( value, count );
209                                }
210                        }
211                }
212
213                template < typename T >
214                void set_opt_uniform_array( program p, const std::string& name, const T* value, uint32 count )
215                {
216                        set_uniform_array( p, name, value, count, false );
217                }
218
219                template < typename T >
220                void set_opt_uniform_array( program p, const std::string& name, const array_view<T>& value )
221                {
222                        set_uniform_array( p, name, value.data(), value.size(), false );
223                }
224
225
226                template < typename T >
227                void set_uniform( program p, const std::string& name, const T& value, bool fatal = true )
228                {
229                        uniform_base* base = get_uniform( p, name, fatal );
230                        if ( base != nullptr )
231                        {
232                                if ( base->type_check( type_to_enum<T>::type ) )
233                                {
234                                        static_cast< uniform<T>* >( base )->set_value( value );
235                                }
236                        }
237                }
238
239                template < typename T >
240                void set_opt_uniform( program p, const std::string& name, const T& value )
241                {
242                        set_uniform( p, name, value, false );
243                }
244
245                virtual ~device()
246                {
247                        destroy_engine_uniforms();
248                }
249
250                // This is done this way to avoid compilation unit creation
251                static engine_uniform_factory_map& get_uniform_factory()
252                {
253                        static engine_uniform_factory_map s_engine_uniform_factory_map;
254                        return s_engine_uniform_factory_map;
255                }
256
257                // This is done this way to avoid compilation unit creation
258                static engine_link_uniform_factory_map& get_link_uniform_factory()
259                {
260                        static engine_link_uniform_factory_map s_engine_link_uniform_factory_map;
261                        return s_engine_link_uniform_factory_map;
262                }
263
264                virtual void prepare_program( program p ) = 0;
265
266        protected:
267                virtual uniform_base* get_uniform( program p, const std::string& name, bool fatal = true ) const = 0;
268
269                void initialize_engine_uniforms()
270                {
271                        engine_uniform_factory_map& factory_map = get_uniform_factory();
272                        factory_map[ "nv_m_view" ]       = new engine_uniform_factory< engine_uniform_m_view >();
273                        factory_map[ "nv_m_view_inv" ]   = new engine_uniform_factory< engine_uniform_m_view_inv >();
274                        factory_map[ "nv_m_model" ]      = new engine_uniform_factory< engine_uniform_m_model >();
275                        factory_map[ "nv_m_model_inv" ]  = new engine_uniform_factory< engine_uniform_m_model_inv >();
276                        factory_map[ "nv_m_modelview" ]  = new engine_uniform_factory< engine_uniform_m_modelview >();
277                        factory_map[ "nv_m_projection" ] = new engine_uniform_factory< engine_uniform_m_projection >();
278                        factory_map[ "nv_m_normal" ]     = new engine_uniform_factory< engine_uniform_m_normal >();
279                        factory_map[ "nv_m_mvp" ]        = new engine_uniform_factory< engine_uniform_m_mvp >();
280                        factory_map[ "nv_v_camera_position" ]  = new engine_uniform_factory< engine_uniform_v_camera_position >();
281                        factory_map[ "nv_v_camera_direction" ] = new engine_uniform_factory< engine_uniform_v_camera_direction >();
282                        factory_map[ "nv_v_viewport" ] = new engine_uniform_factory< engine_uniform_v_viewport >();
283                        factory_map[ "nv_v_screen_size" ] = new engine_uniform_factory< engine_uniform_v_screen_size >();
284
285                        engine_link_uniform_factory_map& factory_link_map = get_link_uniform_factory();
286                        factory_link_map[ "nv_texture_0" ] = new engine_link_uniform_int<0>();
287                        factory_link_map[ "nv_texture_1" ] = new engine_link_uniform_int<1>();
288                        factory_link_map[ "nv_texture_2" ] = new engine_link_uniform_int<2>();
289                        factory_link_map[ "nv_texture_3" ] = new engine_link_uniform_int<3>();
290                        factory_link_map[ "nv_texture_4" ] = new engine_link_uniform_int<4>();
291                        factory_link_map[ "nv_texture_5" ] = new engine_link_uniform_int<5>();
292                        factory_link_map[ "nv_texture_6" ] = new engine_link_uniform_int<6>();
293                        factory_link_map[ "nv_texture_7" ] = new engine_link_uniform_int<7>();
294                        factory_link_map[ "nv_t_diffuse" ] = new engine_link_uniform_int<0>();
295                        factory_link_map[ "nv_t_specular"] = new engine_link_uniform_int<1>();
296                        factory_link_map[ "nv_t_normal"  ] = new engine_link_uniform_int<2>();
297                }
298                void destroy_engine_uniforms()
299                {
300                        for ( auto& i : get_uniform_factory() ) delete i.second;
301                        for ( auto& i : get_link_uniform_factory() ) delete i.second;
302                        get_uniform_factory().clear();
303                        get_link_uniform_factory().clear();
304                }
305
306        };
307
308} // namespace nv
309
310
311#endif // NV_INTERFACE_DEVICE_HH
Note: See TracBrowser for help on using the repository browser.