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

Last change on this file since 506 was 506, checked in by epyon, 9 years ago
  • split shader objects from programs to allow separate compilation
  • minor additions
File size: 11.5 KB
Line 
1// Copyright (C) 2012-2015 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
6
7/**
8 * @file device.hh
9 * @author Kornel Kisielewicz epyon@chaosforge.org
10 * @brief Device class
11 */
12
13#ifndef NV_INTERFACE_DEVICE_HH
14#define NV_INTERFACE_DEVICE_HH
15
16#include <nv/common.hh>
17#include <nv/stl/string.hh>
18#include <nv/stl/handle.hh>
19#include <nv/stl/hash_store.hh>
20#include <nv/interface/uniform.hh>
21#include <nv/interface/mesh_data.hh>
22#include <nv/interface/image_data.hh>
23
24namespace nv
25{
26        class window;
27
28        enum shader_type
29        {
30                VERTEX_SHADER,
31                FRAGMENT_SHADER
32        };
33
34        enum output_slot
35        {
36                OUTPUT_0 = 0,
37                OUTPUT_1 = 1,
38                OUTPUT_2 = 2,
39                OUTPUT_3 = 3,
40                OUTPUT_4 = 4,
41                OUTPUT_5 = 5,
42                OUTPUT_6 = 6,
43                OUTPUT_7 = 7,
44
45                OUTPUT_NONE  = 16,
46                OUTPUT_FRONT = 17,
47                OUTPUT_BACK  = 18,
48        };
49
50        enum texture_type
51        {
52                TEXTURE_1D,
53                TEXTURE_2D,
54                TEXTURE_RECT,
55                TEXTURE_3D,
56                TEXTURE_CUBE,
57                TEXTURE_1D_ARRAY,
58                TEXTURE_2D_ARRAY,
59                TEXTURE_1D_BUFFER,
60                TEXTURE_2D_MULTISAMPLE,
61        };
62
63        enum texture_slot
64        {
65                TEX_DIFFUSE   = 0,
66                TEX_NORMAL    = 1,
67                TEX_METALLIC  = 2,
68                TEX_SPECULAR  = 2, // obsolete
69                TEX_ROUGHNESS = 3,
70                TEX_GLOSS     = 3, // obsolete
71                TEX_EMISSIVE  = 4,
72                TEXTURE_0     = 0,
73                TEXTURE_1     = 1,
74                TEXTURE_2     = 2,
75                TEXTURE_3     = 3,
76                TEXTURE_4     = 4,
77                TEXTURE_5     = 5,
78                TEXTURE_6     = 6,
79                TEXTURE_7     = 7,
80                TEXTURE_8     = 8,
81                TEXTURE_9     = 9,
82                TEXTURE_10    = 10,
83                TEXTURE_11    = 11,
84                TEXTURE_12    = 12,
85                TEXTURE_13    = 13,
86                TEXTURE_14    = 14,
87                TEXTURE_15    = 15,
88                MAX_TEXTURES  = 16,
89        };
90
91
92        struct attribute
93        {
94                int      location;
95                datatype type;
96                int      length;
97        };
98
99        typedef hash_store< shash64, attribute >    attribute_map;
100
101        struct texture_tag {};
102        struct buffer_tag {};
103        struct shader_tag {};
104        struct program_tag {};
105
106        typedef handle< uint32, 16, 16, buffer_tag >       buffer;
107        typedef handle< uint32, 16, 16, texture_tag >      texture;
108        typedef handle< uint32, 16, 16, shader_tag >       shader;
109        typedef handle< uint32, 16, 16, program_tag >      program;
110
111        NV_RTTI_DECLARE_NAME( buffer,  "buffer" )
112        NV_RTTI_DECLARE_NAME( texture, "texture" )
113        NV_RTTI_DECLARE_NAME( shader,  "shader" )
114        NV_RTTI_DECLARE_NAME( program, "program" )
115
116        struct sampler
117        {
118                enum filter
119                {
120                        LINEAR,
121                        NEAREST,
122                        NEAREST_MIPMAP_NEAREST,
123                        LINEAR_MIPMAP_NEAREST,
124                        NEAREST_MIPMAP_LINEAR,
125                        LINEAR_MIPMAP_LINEAR
126                };
127                enum wrap
128                {
129                        CLAMP_TO_EDGE,
130                        CLAMP_TO_BORDER,
131                        MIRRORED_REPEAT,
132                        REPEAT
133                };
134
135                filter filter_min;
136                filter filter_max;
137                wrap wrap_s;
138                wrap wrap_t;
139
140                sampler() : filter_min( LINEAR ), filter_max( LINEAR ), wrap_s( REPEAT ), wrap_t( REPEAT ) {}
141                sampler( filter min, filter max, wrap s, wrap t )
142                        : filter_min( min ), filter_max( max ), wrap_s( s ), wrap_t( t ) {}
143                sampler( filter f, wrap w )
144                        : filter_min( f ), filter_max( f ), wrap_s( w ), wrap_t( w ) {}
145
146        };
147
148        enum buffer_hint
149        {
150                STATIC_DRAW,
151                STREAM_DRAW,
152                DYNAMIC_DRAW
153        };
154
155        enum buffer_type
156        {
157                VERTEX_BUFFER,
158                INDEX_BUFFER,
159                UNIFORM_BUFFER,
160                TEXTURE_BUFFER,
161        };
162
163        enum buffer_access
164        {
165                WRITE_UNSYNCHRONIZED,
166        };
167
168        struct buffer_info
169        {
170                buffer_type type;
171                buffer_hint hint;
172                size_t      size;
173        };
174
175
176        struct texture_info
177        {
178                texture_type type;
179                ivec3        size;
180                image_format format;
181                sampler      tsampler;
182        };
183
184        struct vertex_buffer_attribute
185        {
186                buffer   vbuffer;
187                datatype dtype;
188                size_t   components;
189                size_t   offset;
190                size_t   stride;
191                slot     location;
192                bool     owner;
193        };
194
195        struct shader_info
196        {
197                shader_type type;
198        };
199
200        struct program_info
201        {
202                attribute_map*       m_attribute_map;
203                uniform_map*         m_uniform_map;
204                engine_uniform_list* m_engine_uniforms;
205        };
206
207        class device
208        {
209                friend class context;
210        public:
211                device()
212                {
213                        initialize_engine_uniforms();
214                }
215        protected:
216                virtual shader create_shader( shader_type type, string_view sh_source ) = 0;
217//              virtual program create_program( string_view vs_source, string_view fs_source ) = 0;
218                virtual program create_program( shader vs, shader fs ) = 0;
219                virtual buffer  create_buffer( buffer_type type, buffer_hint hint ) = 0;
220                virtual texture create_texture( texture_type type, pixel_format format ) = 0;
221
222                // TODO: remove?
223//              virtual texture create_texture( ivec2 size, image_format aformat, sampler asampler, const void* data = nullptr ) { return create_texture( TEXTURE_2D, size, aformat, asampler, data ); }
224                virtual void attach( program p, shader s ) = 0;
225                virtual void detach( program p, shader s ) = 0;
226
227                virtual void release( texture ) = 0;
228                virtual void release( buffer ) = 0;
229                virtual void release( program ) = 0;
230                virtual void release( shader ) = 0;
231        public:
232
233
234                virtual image_data* create_image_data( string_view filename ) = 0; // temporary
235                virtual image_data* create_image_data( const uint8* data, uint32 size ) = 0; // temporary
236                virtual const texture_info* get_texture_info( texture ) const = 0;
237                virtual const buffer_info* get_buffer_info( buffer ) const = 0;
238                virtual string_view get_shader_header() const = 0;
239
240                virtual int get_attribute_location( program p, const string_view& name, bool fatal = true ) const = 0;
241                virtual int get_block_location( program p, const string_view& name, bool fatal = true ) const = 0;
242                virtual bool bind_block( program p, const string_view& name, uint32 index ) = 0;
243
244                virtual ~device()
245                {
246                        destroy_engine_uniforms();
247                }
248
249                template < typename T >
250                bool set_uniform_array( program p, const string_view& name, const T* value, uint32 count, bool fatal = true )
251                {
252                        uniform_base* base = get_uniform( p, name, fatal );
253                        if ( base != nullptr )
254                        {
255                                if ( base->type_check( type_to_enum<T>::type ) )
256                                {
257                                        // TODO: nicer check
258                                        NV_ASSERT( static_cast<int>( count ) <= base->get_length(), "LENGTH CHECK FAIL" );
259                                        static_cast<uniform<T>*>( base )->set_value( value, count );
260                                        return true;
261                                }
262                        }
263                        return false;
264                }
265
266                template < typename T >
267                bool set_opt_uniform_array( program p, const string_view& name, const T* value, uint32 count )
268                {
269                        return set_uniform_array( p, name, value, count, false );
270                }
271
272                template < typename T >
273                bool set_opt_uniform_array( program p, const string_view& name, const array_view<T>& value )
274                {
275                        return set_uniform_array( p, name, value.data(), value.size(), false );
276                }
277
278
279                template < typename T >
280                bool set_uniform( program p, const string_view& name, const T& value, bool fatal = true )
281                {
282                        uniform_base* base = get_uniform( p, name, fatal );
283                        if ( base != nullptr )
284                        {
285                                if ( base->type_check( type_to_enum<T>::type ) )
286                                {
287                                        static_cast<uniform<T>*>( base )->set_value( value );
288                                        return true;
289                                }
290                        }
291                        return false;
292                }
293
294                template < typename T >
295                bool set_opt_uniform( program p, const string_view& name, const T& value )
296                {
297                        return set_uniform( p, name, value, false );
298                }
299
300                // This is done this way to avoid compilation unit creation
301                static engine_uniform_factory_map& get_uniform_factory()
302                {
303                        static engine_uniform_factory_map s_engine_uniform_factory_map;
304                        return s_engine_uniform_factory_map;
305                }
306
307                // This is done this way to avoid compilation unit creation
308                static engine_link_uniform_factory_map& get_link_uniform_factory()
309                {
310                        static engine_link_uniform_factory_map s_engine_link_uniform_factory_map;
311                        return s_engine_link_uniform_factory_map;
312                }
313
314                virtual void prepare_program( program p ) = 0;
315
316        protected:
317
318                virtual uniform_base* get_uniform( program p, const string_view& name, bool fatal = true ) const = 0;
319
320                void initialize_engine_uniforms()
321                {
322                        engine_uniform_factory_map& factory_map = get_uniform_factory();
323                        factory_map[ "nv_f_near"] = new engine_uniform_factory< engine_uniform_f_near >();
324                        factory_map[ "nv_f_far"]  = new engine_uniform_factory< engine_uniform_f_far >();
325                        factory_map[ "nv_m_view" ]          = new engine_uniform_factory< engine_uniform_m_view >();
326                        factory_map[ "nv_m_view_inv" ]      = new engine_uniform_factory< engine_uniform_m_view_inv >();
327                        factory_map[ "nv_m_model" ]         = new engine_uniform_factory< engine_uniform_m_model >();
328                        factory_map[ "nv_m_model_inv" ]     = new engine_uniform_factory< engine_uniform_m_model_inv >();
329                        factory_map[ "nv_m_modelview" ]     = new engine_uniform_factory< engine_uniform_m_modelview >();
330                        factory_map[ "nv_m_modelview_inv" ] = new engine_uniform_factory< engine_uniform_m_modelview_inv >();
331                        factory_map[ "nv_m_projection" ]    = new engine_uniform_factory< engine_uniform_m_projection >();
332                        factory_map[ "nv_m_projection_inv"] = new engine_uniform_factory< engine_uniform_m_projection_inv >();
333                        factory_map[ "nv_m_viewprojection" ]     = new engine_uniform_factory< engine_uniform_m_viewprojection >();
334                        factory_map[ "nv_m_viewprojection_inv" ] = new engine_uniform_factory< engine_uniform_m_viewprojection_inv >();
335                        factory_map[ "nv_m_normal" ]        = new engine_uniform_factory< engine_uniform_m_normal >();
336                        factory_map[ "nv_m_mvp" ]           = new engine_uniform_factory< engine_uniform_m_mvp >();
337                        factory_map[ "nv_m_mvp_inv"]        = new engine_uniform_factory< engine_uniform_m_mvp_inv >();
338                        factory_map[ "nv_v_camera_position" ]  = new engine_uniform_factory< engine_uniform_v_camera_position >();
339                        factory_map[ "nv_v_camera_direction" ] = new engine_uniform_factory< engine_uniform_v_camera_direction >();
340                        factory_map[ "nv_v_viewport" ] = new engine_uniform_factory< engine_uniform_v_viewport >();
341                        factory_map[ "nv_v_screen_size" ] = new engine_uniform_factory< engine_uniform_v_screen_size >();
342
343                        engine_link_uniform_factory_map& factory_link_map = get_link_uniform_factory();
344                        factory_link_map[ "nv_texture_0"  ] = new engine_link_uniform_int<0>();
345                        factory_link_map[ "nv_texture_1"  ] = new engine_link_uniform_int<1>();
346                        factory_link_map[ "nv_texture_2"  ] = new engine_link_uniform_int<2>();
347                        factory_link_map[ "nv_texture_3"  ] = new engine_link_uniform_int<3>();
348                        factory_link_map[ "nv_texture_4"  ] = new engine_link_uniform_int<4>();
349                        factory_link_map[ "nv_texture_5"  ] = new engine_link_uniform_int<5>();
350                        factory_link_map[ "nv_texture_6"  ] = new engine_link_uniform_int<6>();
351                        factory_link_map[ "nv_texture_7"  ] = new engine_link_uniform_int<7>();
352                        factory_link_map[ "nv_texture_8"  ] = new engine_link_uniform_int<8>();
353                        factory_link_map[ "nv_texture_9"  ] = new engine_link_uniform_int<9>();
354                        factory_link_map[ "nv_t_diffuse"  ] = new engine_link_uniform_int<int( TEX_DIFFUSE )>();
355                        factory_link_map[ "nv_t_normal"   ] = new engine_link_uniform_int<int( TEX_NORMAL )>();
356                        factory_link_map[ "nv_t_metallic" ] = new engine_link_uniform_int<int( TEX_METALLIC )>();
357                        factory_link_map[ "nv_t_specular" ] = new engine_link_uniform_int<int( TEX_METALLIC )>();
358                        factory_link_map[ "nv_t_roughness"] = new engine_link_uniform_int<int( TEX_ROUGHNESS )>();
359                        factory_link_map[ "nv_t_gloss"    ] = new engine_link_uniform_int<int( TEX_ROUGHNESS )>();
360                        factory_link_map[ "nv_t_emissive" ] = new engine_link_uniform_int<int( TEX_EMISSIVE )>();
361                }
362                void destroy_engine_uniforms()
363                {
364                        for ( auto& i : get_uniform_factory() ) delete i.second;
365                        for ( auto& i : get_link_uniform_factory() ) delete i.second;
366                        get_uniform_factory().clear();
367                        get_link_uniform_factory().clear();
368                }
369
370        };
371
372} // namespace nv
373
374
375#endif // NV_INTERFACE_DEVICE_HH
Note: See TracBrowser for help on using the repository browser.