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

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