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

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