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

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