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

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