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

Last change on this file since 410 was 410, checked in by epyon, 10 years ago
  • merge of vertex_descriptor and key_descriptor concepts - unified raw data description via data_descriptor
  • minor bugfixes
File size: 9.1 KB
RevLine 
[395]1// Copyright (C) 2012-2015 ChaosForge Ltd
[32]2// http://chaosforge.org/
3//
[395]4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
6
[32]7/**
8 * @file device.hh
9 * @author Kornel Kisielewicz epyon@chaosforge.org
10 * @brief Device class
11 */
12
[395]13#ifndef NV_INTERFACE_DEVICE_HH
14#define NV_INTERFACE_DEVICE_HH
[32]15
[395]16#include <nv/common.hh>
[368]17#include <nv/stl/string.hh>
18#include <nv/stl/handle.hh>
[303]19#include <nv/interface/uniform.hh>
[238]20#include <nv/interface/mesh_data.hh>
[90]21#include <nv/interface/image_data.hh>
[32]22
23namespace nv
24{
25        class window;
26
[331]27        enum output_slot
28        {
29                OUTPUT_0 = 0,
30                OUTPUT_1 = 1,
31                OUTPUT_2 = 2,
32                OUTPUT_3 = 3,
33                OUTPUT_4 = 4,
34                OUTPUT_5 = 5,
35                OUTPUT_6 = 6,
36                OUTPUT_7 = 7,
37
38                OUTPUT_NONE  = 16,
39                OUTPUT_FRONT = 17,
40                OUTPUT_BACK  = 18,
41        };
42
43        enum texture_type
44        {
45                TEXTURE_1D,
46                TEXTURE_2D,
47                TEXTURE_RECT,
48                TEXTURE_3D,
49                TEXTURE_CUBE,
50        };
51
[303]52        enum texture_slot
53        {
54                TEX_DIFFUSE  = 0,
55                TEX_SPECULAR = 1,
56                TEX_NORMAL   = 2,
57                TEXTURE_0    = 0,
58                TEXTURE_1    = 1,
59                TEXTURE_2    = 2,
60                TEXTURE_3    = 3,
61                TEXTURE_4    = 4,
62                TEXTURE_5    = 5,
63                TEXTURE_6    = 6,
64                TEXTURE_7    = 7,
65        };
66
67
68        struct attribute
69        {
[379]70                std::string   name;
[303]71                int      location;
72                datatype type;
73                int      length;
74        };
75
[392]76        typedef unordered_map< std::string, attribute >    attribute_map;
[303]77
[302]78        struct texture_tag {};
79        struct buffer_tag {};
[303]80        struct program_tag {};
[302]81        typedef handle< uint32, 16, 16, buffer_tag >       buffer;
82        typedef handle< uint32, 16, 16, texture_tag >      texture;
[303]83        typedef handle< uint32, 16, 16, program_tag >      program;
[302]84
[301]85        struct sampler
86        {
87                enum filter
88                {
89                        LINEAR,
90                        NEAREST,
91                        NEAREST_MIPMAP_NEAREST,
92                        LINEAR_MIPMAP_NEAREST,
93                        NEAREST_MIPMAP_LINEAR,
94                        LINEAR_MIPMAP_LINEAR
95                };
96                enum wrap
97                {
98                        CLAMP_TO_EDGE,
99                        CLAMP_TO_BORDER,
100                        MIRRORED_REPEAT,
101                        REPEAT
102                };
103
104                filter filter_min;
105                filter filter_max;
106                wrap wrap_s;
107                wrap wrap_t;
108
109                sampler() : filter_min( LINEAR ), filter_max( LINEAR ), wrap_s( REPEAT ), wrap_t( REPEAT ) {}
110                sampler( filter min, filter max, wrap s, wrap t )
111                        : filter_min( min ), filter_max( max ), wrap_s( s ), wrap_t( t ) {}
112                sampler( filter f, wrap w )
113                        : filter_min( f ), filter_max( f ), wrap_s( w ), wrap_t( w ) {}
114
115        };
116
[410]117        enum buffer_hint
118        {
119                STATIC_DRAW,
120                STREAM_DRAW,
121                DYNAMIC_DRAW
122        };
123
[302]124        struct buffer_info
125        {
126                buffer_type type;
127                buffer_hint hint;
128                size_t      size;
129        };
130
131
[301]132        struct texture_info
133        {
[331]134                texture_type type;
[301]135                ivec2        size;
136                image_format format;
[323]137                sampler      tsampler;
[301]138        };
139
[302]140        struct vertex_buffer_attribute
141        {
142                buffer   vbuffer;
143                datatype dtype;
144                size_t   components;
145                size_t   offset;
146                size_t   stride;
147                slot     location;
148                bool     owner;
149        };
[301]150
[303]151        struct program_info
152        {
[392]153                attribute_map*       m_attribute_map;
154                uniform_map*         m_uniform_map;
155                engine_uniform_list* m_engine_uniforms;
[303]156        };
157
[32]158        class device
159        {
[302]160                friend class context;
[32]161        public:
[303]162                device()
163                {
164                        initialize_engine_uniforms();
165                }
[399]166                virtual program create_program( string_view vs_source, string_view fs_source ) = 0;
[302]167                virtual buffer create_buffer( buffer_type type, buffer_hint hint, size_t size, const void* source = nullptr ) = 0;
[406]168                virtual texture create_texture( texture_type type, ivec2 size, image_format aformat, sampler asampler, const void* data = nullptr ) = 0;
[331]169                // TODO: remove?
[406]170                virtual texture create_texture( ivec2 size, image_format aformat, sampler asampler, const void* data = nullptr ) { return create_texture( TEXTURE_2D, size, aformat, asampler, data ); }
[399]171                virtual image_data* create_image_data( string_view filename ) = 0; // temporary
[350]172                virtual image_data* create_image_data( const uint8* data, uint32 size ) = 0; // temporary
[302]173                virtual void release( texture ) = 0;
174                virtual void release( buffer ) = 0;
[303]175                virtual void release( program ) = 0;
176                virtual const texture_info* get_texture_info( texture ) const = 0;
177                virtual const buffer_info* get_buffer_info( buffer ) const = 0;
[379]178                virtual const std::string& get_shader_header() const = 0;
[303]179
[301]180                virtual texture create_texture( image_data* data, sampler asampler )
[292]181                {
[406]182                        return create_texture( data->get_size(), data->get_format(), asampler, data->get_data() );
[292]183                }
184
[379]185                int try_get_attribute_location( program p, const std::string& name ) const
[303]186                {
187                        return get_attribute_location( p, name, false );
188                }
[238]189
[379]190                virtual int get_attribute_location( program p, const std::string& name, bool fatal = true ) const = 0;
[303]191
192                template < typename T >
[379]193                void set_uniform_array( program p, const std::string& name, const T* value, uint32 count, bool fatal = true )
[303]194                {
195                        uniform_base* base = get_uniform( p, name, fatal );
196                        if ( base != nullptr )
197                        {
198                                if ( base->type_check( type_to_enum<T>::type ) )
199                                {
200                                        // TODO: nicer check
[406]201                                        NV_ASSERT( static_cast<int>( count ) <= base->get_length(), "LENGTH CHECK FAIL" );
202                                        static_cast< uniform<T>* >( base )->set_value( value, count );
[303]203                                }
204                        }
205                }
206
207                template < typename T >
[379]208                void set_opt_uniform_array( program p, const std::string& name, const T* value, uint32 count )
[303]209                {
210                        set_uniform_array( p, name, value, count, false );
211                }
212
213                template < typename T >
[399]214                void set_opt_uniform_array( program p, const std::string& name, const array_view<T>& value )
[303]215                {
[406]216                        set_uniform_array( p, name, value.data(), value.size(), false );
[303]217                }
218
219
220                template < typename T >
[379]221                void set_uniform( program p, const std::string& name, const T& value, bool fatal = true )
[303]222                {
223                        uniform_base* base = get_uniform( p, name, fatal );
224                        if ( base != nullptr )
225                        {
226                                if ( base->type_check( type_to_enum<T>::type ) )
227                                {
[406]228                                        static_cast< uniform<T>* >( base )->set_value( value );
[303]229                                }
230                        }
231                }
232
233                template < typename T >
[379]234                void set_opt_uniform( program p, const std::string& name, const T& value )
[303]235                {
236                        set_uniform( p, name, value, false );
237                }
238
[302]239                virtual ~device()
240                {
[303]241                        destroy_engine_uniforms();
[302]242                }
[303]243
244                // This is done this way to avoid compilation unit creation
245                static engine_uniform_factory_map& get_uniform_factory()
246                {
247                        static engine_uniform_factory_map s_engine_uniform_factory_map;
248                        return s_engine_uniform_factory_map;
249                }
250
251                // This is done this way to avoid compilation unit creation
252                static engine_link_uniform_factory_map& get_link_uniform_factory()
253                {
254                        static engine_link_uniform_factory_map s_engine_link_uniform_factory_map;
255                        return s_engine_link_uniform_factory_map;
256                }
257
258                virtual void prepare_program( program p ) = 0;
259
[302]260        protected:
[379]261                virtual uniform_base* get_uniform( program p, const std::string& name, bool fatal = true ) const = 0;
[303]262
263                void initialize_engine_uniforms()
264                {
265                        engine_uniform_factory_map& factory_map = get_uniform_factory();
266                        factory_map[ "nv_m_view" ]       = new engine_uniform_factory< engine_uniform_m_view >();
267                        factory_map[ "nv_m_view_inv" ]   = new engine_uniform_factory< engine_uniform_m_view_inv >();
268                        factory_map[ "nv_m_model" ]      = new engine_uniform_factory< engine_uniform_m_model >();
269                        factory_map[ "nv_m_model_inv" ]  = new engine_uniform_factory< engine_uniform_m_model_inv >();
270                        factory_map[ "nv_m_modelview" ]  = new engine_uniform_factory< engine_uniform_m_modelview >();
271                        factory_map[ "nv_m_projection" ] = new engine_uniform_factory< engine_uniform_m_projection >();
272                        factory_map[ "nv_m_normal" ]     = new engine_uniform_factory< engine_uniform_m_normal >();
273                        factory_map[ "nv_m_mvp" ]        = new engine_uniform_factory< engine_uniform_m_mvp >();
274                        factory_map[ "nv_v_camera_position" ]  = new engine_uniform_factory< engine_uniform_v_camera_position >();
275                        factory_map[ "nv_v_camera_direction" ] = new engine_uniform_factory< engine_uniform_v_camera_direction >();
[342]276                        factory_map[ "nv_v_viewport" ] = new engine_uniform_factory< engine_uniform_v_viewport >();
277                        factory_map[ "nv_v_screen_size" ] = new engine_uniform_factory< engine_uniform_v_screen_size >();
[303]278
279                        engine_link_uniform_factory_map& factory_link_map = get_link_uniform_factory();
280                        factory_link_map[ "nv_texture_0" ] = new engine_link_uniform_int<0>();
281                        factory_link_map[ "nv_texture_1" ] = new engine_link_uniform_int<1>();
282                        factory_link_map[ "nv_texture_2" ] = new engine_link_uniform_int<2>();
283                        factory_link_map[ "nv_texture_3" ] = new engine_link_uniform_int<3>();
284                        factory_link_map[ "nv_texture_4" ] = new engine_link_uniform_int<4>();
285                        factory_link_map[ "nv_texture_5" ] = new engine_link_uniform_int<5>();
286                        factory_link_map[ "nv_texture_6" ] = new engine_link_uniform_int<6>();
287                        factory_link_map[ "nv_texture_7" ] = new engine_link_uniform_int<7>();
288                        factory_link_map[ "nv_t_diffuse" ] = new engine_link_uniform_int<0>();
289                        factory_link_map[ "nv_t_specular"] = new engine_link_uniform_int<1>();
290                        factory_link_map[ "nv_t_normal"  ] = new engine_link_uniform_int<2>();
291                }
292                void destroy_engine_uniforms()
293                {
294                        for ( auto& i : get_uniform_factory() ) delete i.second;
295                        for ( auto& i : get_link_uniform_factory() ) delete i.second;
296                        get_uniform_factory().clear();
297                        get_link_uniform_factory().clear();
298                }
299
[32]300        };
301
302} // namespace nv
303
304
[395]305#endif // NV_INTERFACE_DEVICE_HH
Note: See TracBrowser for help on using the repository browser.