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

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