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

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