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

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