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

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