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

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