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

Last change on this file since 342 was 342, checked in by epyon, 11 years ago
  • gl_context - set_draw_buffers accepts const
  • gl_context - set_draw_buffers with 1 buffer behaves like set_draw_buffer
  • camera - holds viewport info (does not set it, should it?)
  • clear_state - NONE added
  • device/uniform - nv_v_viewport & nv_v_screen_size built-ins added
File size: 8.8 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/core/string.hh>
17#include <nv/core/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                string   name;
70                int      location;
71                datatype type;
72                int      length;
73        };
74
75        typedef std::unordered_map< 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( const string& vs_source, const string& 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( const std::string& filename ) = 0; // temporary
164                virtual void release( texture ) = 0;
165                virtual void release( buffer ) = 0;
166                virtual void release( program ) = 0;
167                virtual const texture_info* get_texture_info( texture ) const = 0;
168                virtual const buffer_info* get_buffer_info( buffer ) const = 0;
169                virtual const string& get_shader_header() const = 0;
170
171                virtual texture create_texture( image_data* data, sampler asampler )
172                {
173                        return create_texture( data->get_size(), data->get_format(), asampler, (void*)data->get_data() );
174                }
175
176                int try_get_attribute_location( program p, const string& name ) const
177                {
178                        return get_attribute_location( p, name, false );
179                }
180
181                virtual int get_attribute_location( program p, const string& name, bool fatal = true ) const = 0;
182
183                template < typename T >
184                void set_uniform_array( program p, const string& name, const T* value, uint32 count, bool fatal = true )
185                {
186                        uniform_base* base = get_uniform( p, name, fatal );
187                        if ( base != nullptr )
188                        {
189                                if ( base->type_check( type_to_enum<T>::type ) )
190                                {
191                                        // TODO: nicer check
192                                        NV_ASSERT( (int)count <= base->get_length(), "LENGTH CHECK FAIL" );
193                                        ((uniform<T>*)( base ))->set_value( value, count );
194                                }
195                        }
196                }
197
198                template < typename T >
199                void set_opt_uniform_array( program p, const string& name, const T* value, uint32 count )
200                {
201                        set_uniform_array( p, name, value, count, false );
202                }
203
204                template < typename T >
205                void set_opt_uniform_array( program p, const string& name, const std::vector<T>& value )
206                {
207                        set_uniform_array( p, name, (const T*)value.data(), value.size(), false );
208                }
209
210
211                template < typename T >
212                void set_uniform( program p, const string& name, const T& value, bool fatal = true )
213                {
214                        uniform_base* base = get_uniform( p, name, fatal );
215                        if ( base != nullptr )
216                        {
217                                if ( base->type_check( type_to_enum<T>::type ) )
218                                {
219                                        ((uniform<T>*)( base ))->set_value( value );
220                                }
221                        }
222                }
223
224                template < typename T >
225                void set_opt_uniform( program p, const string& name, const T& value )
226                {
227                        set_uniform( p, name, value, false );
228                }
229
230                virtual ~device()
231                {
232                        destroy_engine_uniforms();
233                }
234
235                // This is done this way to avoid compilation unit creation
236                static engine_uniform_factory_map& get_uniform_factory()
237                {
238                        static engine_uniform_factory_map s_engine_uniform_factory_map;
239                        return s_engine_uniform_factory_map;
240                }
241
242                // This is done this way to avoid compilation unit creation
243                static engine_link_uniform_factory_map& get_link_uniform_factory()
244                {
245                        static engine_link_uniform_factory_map s_engine_link_uniform_factory_map;
246                        return s_engine_link_uniform_factory_map;
247                }
248
249                virtual void prepare_program( program p ) = 0;
250
251        protected:
252                virtual uniform_base* get_uniform( program p, const string& name, bool fatal = true ) const = 0;
253
254                void initialize_engine_uniforms()
255                {
256                        engine_uniform_factory_map& factory_map = get_uniform_factory();
257                        factory_map[ "nv_m_view" ]       = new engine_uniform_factory< engine_uniform_m_view >();
258                        factory_map[ "nv_m_view_inv" ]   = new engine_uniform_factory< engine_uniform_m_view_inv >();
259                        factory_map[ "nv_m_model" ]      = new engine_uniform_factory< engine_uniform_m_model >();
260                        factory_map[ "nv_m_model_inv" ]  = new engine_uniform_factory< engine_uniform_m_model_inv >();
261                        factory_map[ "nv_m_modelview" ]  = new engine_uniform_factory< engine_uniform_m_modelview >();
262                        factory_map[ "nv_m_projection" ] = new engine_uniform_factory< engine_uniform_m_projection >();
263                        factory_map[ "nv_m_normal" ]     = new engine_uniform_factory< engine_uniform_m_normal >();
264                        factory_map[ "nv_m_mvp" ]        = new engine_uniform_factory< engine_uniform_m_mvp >();
265                        factory_map[ "nv_v_camera_position" ]  = new engine_uniform_factory< engine_uniform_v_camera_position >();
266                        factory_map[ "nv_v_camera_direction" ] = new engine_uniform_factory< engine_uniform_v_camera_direction >();
267                        factory_map[ "nv_v_viewport" ] = new engine_uniform_factory< engine_uniform_v_viewport >();
268                        factory_map[ "nv_v_screen_size" ] = new engine_uniform_factory< engine_uniform_v_screen_size >();
269
270                        engine_link_uniform_factory_map& factory_link_map = get_link_uniform_factory();
271                        factory_link_map[ "nv_texture_0" ] = new engine_link_uniform_int<0>();
272                        factory_link_map[ "nv_texture_1" ] = new engine_link_uniform_int<1>();
273                        factory_link_map[ "nv_texture_2" ] = new engine_link_uniform_int<2>();
274                        factory_link_map[ "nv_texture_3" ] = new engine_link_uniform_int<3>();
275                        factory_link_map[ "nv_texture_4" ] = new engine_link_uniform_int<4>();
276                        factory_link_map[ "nv_texture_5" ] = new engine_link_uniform_int<5>();
277                        factory_link_map[ "nv_texture_6" ] = new engine_link_uniform_int<6>();
278                        factory_link_map[ "nv_texture_7" ] = new engine_link_uniform_int<7>();
279                        factory_link_map[ "nv_t_diffuse" ] = new engine_link_uniform_int<0>();
280                        factory_link_map[ "nv_t_specular"] = new engine_link_uniform_int<1>();
281                        factory_link_map[ "nv_t_normal"  ] = new engine_link_uniform_int<2>();
282                }
283                void destroy_engine_uniforms()
284                {
285                        for ( auto& i : get_uniform_factory() ) delete i.second;
286                        for ( auto& i : get_link_uniform_factory() ) delete i.second;
287                        get_uniform_factory().clear();
288                        get_link_uniform_factory().clear();
289                }
290
291        };
292
293} // namespace nv
294
295
296#endif // NV_DEVICE_HH
Note: See TracBrowser for help on using the repository browser.