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

Last change on this file since 406 was 406, checked in by epyon, 10 years ago
  • code compiles cleanly on maximum warning level
File size: 9.0 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
[302]117        struct buffer_info
118        {
119                buffer_type type;
120                buffer_hint hint;
121                size_t      size;
122        };
123
124
[301]125        struct texture_info
126        {
[331]127                texture_type type;
[301]128                ivec2        size;
129                image_format format;
[323]130                sampler      tsampler;
[301]131        };
132
[302]133        struct vertex_buffer_attribute
134        {
135                buffer   vbuffer;
136                datatype dtype;
137                size_t   components;
138                size_t   offset;
139                size_t   stride;
140                slot     location;
141                bool     owner;
142        };
[301]143
[303]144        struct program_info
145        {
[392]146                attribute_map*       m_attribute_map;
147                uniform_map*         m_uniform_map;
148                engine_uniform_list* m_engine_uniforms;
[303]149        };
150
[32]151        class device
152        {
[302]153                friend class context;
[32]154        public:
[303]155                device()
156                {
157                        initialize_engine_uniforms();
158                }
[399]159                virtual program create_program( string_view vs_source, string_view fs_source ) = 0;
[302]160                virtual buffer create_buffer( buffer_type type, buffer_hint hint, size_t size, const void* source = nullptr ) = 0;
[406]161                virtual texture create_texture( texture_type type, ivec2 size, image_format aformat, sampler asampler, const void* data = nullptr ) = 0;
[331]162                // TODO: remove?
[406]163                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]164                virtual image_data* create_image_data( string_view filename ) = 0; // temporary
[350]165                virtual image_data* create_image_data( const uint8* data, uint32 size ) = 0; // temporary
[302]166                virtual void release( texture ) = 0;
167                virtual void release( buffer ) = 0;
[303]168                virtual void release( program ) = 0;
169                virtual const texture_info* get_texture_info( texture ) const = 0;
170                virtual const buffer_info* get_buffer_info( buffer ) const = 0;
[379]171                virtual const std::string& get_shader_header() const = 0;
[303]172
[301]173                virtual texture create_texture( image_data* data, sampler asampler )
[292]174                {
[406]175                        return create_texture( data->get_size(), data->get_format(), asampler, data->get_data() );
[292]176                }
177
[379]178                int try_get_attribute_location( program p, const std::string& name ) const
[303]179                {
180                        return get_attribute_location( p, name, false );
181                }
[238]182
[379]183                virtual int get_attribute_location( program p, const std::string& name, bool fatal = true ) const = 0;
[303]184
185                template < typename T >
[379]186                void set_uniform_array( program p, const std::string& name, const T* value, uint32 count, bool fatal = true )
[303]187                {
188                        uniform_base* base = get_uniform( p, name, fatal );
189                        if ( base != nullptr )
190                        {
191                                if ( base->type_check( type_to_enum<T>::type ) )
192                                {
193                                        // TODO: nicer check
[406]194                                        NV_ASSERT( static_cast<int>( count ) <= base->get_length(), "LENGTH CHECK FAIL" );
195                                        static_cast< uniform<T>* >( base )->set_value( value, count );
[303]196                                }
197                        }
198                }
199
200                template < typename T >
[379]201                void set_opt_uniform_array( program p, const std::string& name, const T* value, uint32 count )
[303]202                {
203                        set_uniform_array( p, name, value, count, false );
204                }
205
206                template < typename T >
[399]207                void set_opt_uniform_array( program p, const std::string& name, const array_view<T>& value )
[303]208                {
[406]209                        set_uniform_array( p, name, value.data(), value.size(), false );
[303]210                }
211
212
213                template < typename T >
[379]214                void set_uniform( program p, const std::string& name, const T& value, bool fatal = true )
[303]215                {
216                        uniform_base* base = get_uniform( p, name, fatal );
217                        if ( base != nullptr )
218                        {
219                                if ( base->type_check( type_to_enum<T>::type ) )
220                                {
[406]221                                        static_cast< uniform<T>* >( base )->set_value( value );
[303]222                                }
223                        }
224                }
225
226                template < typename T >
[379]227                void set_opt_uniform( program p, const std::string& name, const T& value )
[303]228                {
229                        set_uniform( p, name, value, false );
230                }
231
[302]232                virtual ~device()
233                {
[303]234                        destroy_engine_uniforms();
[302]235                }
[303]236
237                // This is done this way to avoid compilation unit creation
238                static engine_uniform_factory_map& get_uniform_factory()
239                {
240                        static engine_uniform_factory_map s_engine_uniform_factory_map;
241                        return s_engine_uniform_factory_map;
242                }
243
244                // This is done this way to avoid compilation unit creation
245                static engine_link_uniform_factory_map& get_link_uniform_factory()
246                {
247                        static engine_link_uniform_factory_map s_engine_link_uniform_factory_map;
248                        return s_engine_link_uniform_factory_map;
249                }
250
251                virtual void prepare_program( program p ) = 0;
252
[302]253        protected:
[379]254                virtual uniform_base* get_uniform( program p, const std::string& name, bool fatal = true ) const = 0;
[303]255
256                void initialize_engine_uniforms()
257                {
258                        engine_uniform_factory_map& factory_map = get_uniform_factory();
259                        factory_map[ "nv_m_view" ]       = new engine_uniform_factory< engine_uniform_m_view >();
260                        factory_map[ "nv_m_view_inv" ]   = new engine_uniform_factory< engine_uniform_m_view_inv >();
261                        factory_map[ "nv_m_model" ]      = new engine_uniform_factory< engine_uniform_m_model >();
262                        factory_map[ "nv_m_model_inv" ]  = new engine_uniform_factory< engine_uniform_m_model_inv >();
263                        factory_map[ "nv_m_modelview" ]  = new engine_uniform_factory< engine_uniform_m_modelview >();
264                        factory_map[ "nv_m_projection" ] = new engine_uniform_factory< engine_uniform_m_projection >();
265                        factory_map[ "nv_m_normal" ]     = new engine_uniform_factory< engine_uniform_m_normal >();
266                        factory_map[ "nv_m_mvp" ]        = new engine_uniform_factory< engine_uniform_m_mvp >();
267                        factory_map[ "nv_v_camera_position" ]  = new engine_uniform_factory< engine_uniform_v_camera_position >();
268                        factory_map[ "nv_v_camera_direction" ] = new engine_uniform_factory< engine_uniform_v_camera_direction >();
[342]269                        factory_map[ "nv_v_viewport" ] = new engine_uniform_factory< engine_uniform_v_viewport >();
270                        factory_map[ "nv_v_screen_size" ] = new engine_uniform_factory< engine_uniform_v_screen_size >();
[303]271
272                        engine_link_uniform_factory_map& factory_link_map = get_link_uniform_factory();
273                        factory_link_map[ "nv_texture_0" ] = new engine_link_uniform_int<0>();
274                        factory_link_map[ "nv_texture_1" ] = new engine_link_uniform_int<1>();
275                        factory_link_map[ "nv_texture_2" ] = new engine_link_uniform_int<2>();
276                        factory_link_map[ "nv_texture_3" ] = new engine_link_uniform_int<3>();
277                        factory_link_map[ "nv_texture_4" ] = new engine_link_uniform_int<4>();
278                        factory_link_map[ "nv_texture_5" ] = new engine_link_uniform_int<5>();
279                        factory_link_map[ "nv_texture_6" ] = new engine_link_uniform_int<6>();
280                        factory_link_map[ "nv_texture_7" ] = new engine_link_uniform_int<7>();
281                        factory_link_map[ "nv_t_diffuse" ] = new engine_link_uniform_int<0>();
282                        factory_link_map[ "nv_t_specular"] = new engine_link_uniform_int<1>();
283                        factory_link_map[ "nv_t_normal"  ] = new engine_link_uniform_int<2>();
284                }
285                void destroy_engine_uniforms()
286                {
287                        for ( auto& i : get_uniform_factory() ) delete i.second;
288                        for ( auto& i : get_link_uniform_factory() ) delete i.second;
289                        get_uniform_factory().clear();
290                        get_link_uniform_factory().clear();
291                }
292
[32]293        };
294
295} // namespace nv
296
297
[395]298#endif // NV_INTERFACE_DEVICE_HH
Note: See TracBrowser for help on using the repository browser.