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

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