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

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