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

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