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

Last change on this file since 515 was 515, checked in by epyon, 9 years ago
  • model tag support
  • local transform particle engines
  • fix for 3d textures
  • minor cleanups/fixes
File size: 11.8 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;
[515]132                wrap wrap_r;
[301]133
[515]134                sampler() : filter_min( LINEAR ), filter_max( LINEAR ), wrap_s( REPEAT ), wrap_t( REPEAT ), wrap_r( REPEAT ) {}
135                sampler( filter min, filter max, wrap s, wrap t, wrap r )
136                        : filter_min( min ), filter_max( max ), wrap_s( s ), wrap_t( t ), wrap_r( r ) {}
[301]137                sampler( filter f, wrap w )
[515]138                        : filter_min( f ), filter_max( f ), wrap_s( w ), wrap_t( w ), wrap_r( w )
139                {}
[301]140
141        };
142
[410]143        enum buffer_hint
144        {
145                STATIC_DRAW,
146                STREAM_DRAW,
147                DYNAMIC_DRAW
148        };
149
[412]150        enum buffer_type
151        {
152                VERTEX_BUFFER,
153                INDEX_BUFFER,
[473]154                UNIFORM_BUFFER,
[491]155                TEXTURE_BUFFER,
[412]156        };
157
[499]158        enum buffer_access
159        {
160                WRITE_UNSYNCHRONIZED,
161        };
162
[302]163        struct buffer_info
164        {
165                buffer_type type;
166                buffer_hint hint;
167                size_t      size;
168        };
169
170
[301]171        struct texture_info
172        {
[331]173                texture_type type;
[463]174                ivec3        size;
[301]175                image_format format;
[323]176                sampler      tsampler;
[301]177        };
178
[302]179        struct vertex_buffer_attribute
180        {
181                buffer   vbuffer;
182                datatype dtype;
183                size_t   components;
184                size_t   offset;
185                size_t   stride;
186                slot     location;
187                bool     owner;
188        };
[301]189
[506]190        struct shader_info
191        {
192                shader_type type;
193        };
194
[303]195        struct program_info
196        {
[392]197                attribute_map*       m_attribute_map;
198                uniform_map*         m_uniform_map;
199                engine_uniform_list* m_engine_uniforms;
[303]200        };
201
[32]202        class device
203        {
[302]204                friend class context;
[32]205        public:
[303]206                device()
207                {
208                        initialize_engine_uniforms();
209                }
[501]210        protected:
[506]211                virtual shader create_shader( shader_type type, string_view sh_source ) = 0;
212//              virtual program create_program( string_view vs_source, string_view fs_source ) = 0;
213                virtual program create_program( shader vs, shader fs ) = 0;
[501]214                virtual buffer  create_buffer( buffer_type type, buffer_hint hint ) = 0;
[491]215                virtual texture create_texture( texture_type type, pixel_format format ) = 0;
216
[331]217                // TODO: remove?
[501]218//              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]219                virtual void attach( program p, shader s ) = 0;
220                virtual void detach( program p, shader s ) = 0;
[491]221
[501]222                virtual void release( texture ) = 0;
223                virtual void release( buffer ) = 0;
224                virtual void release( program ) = 0;
[506]225                virtual void release( shader ) = 0;
[501]226        public:
[491]227
228
[399]229                virtual image_data* create_image_data( string_view filename ) = 0; // temporary
[350]230                virtual image_data* create_image_data( const uint8* data, uint32 size ) = 0; // temporary
[303]231                virtual const texture_info* get_texture_info( texture ) const = 0;
232                virtual const buffer_info* get_buffer_info( buffer ) const = 0;
[438]233                virtual string_view get_shader_header() const = 0;
[303]234
[501]235                virtual int get_attribute_location( program p, const string_view& name, bool fatal = true ) const = 0;
236                virtual int get_block_location( program p, const string_view& name, bool fatal = true ) const = 0;
237                virtual bool bind_block( program p, const string_view& name, uint32 index ) = 0;
[292]238
[501]239                virtual ~device()
[303]240                {
[501]241                        destroy_engine_uniforms();
[303]242                }
[238]243
[303]244                template < typename T >
[492]245                bool set_uniform_array( program p, const string_view& name, const T* value, uint32 count, 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                                {
252                                        // TODO: nicer check
[406]253                                        NV_ASSERT( static_cast<int>( count ) <= base->get_length(), "LENGTH CHECK FAIL" );
[501]254                                        static_cast<uniform<T>*>( base )->set_value( value, count );
[492]255                                        return true;
[303]256                                }
257                        }
[492]258                        return false;
[303]259                }
260
261                template < typename T >
[492]262                bool set_opt_uniform_array( program p, const string_view& name, const T* value, uint32 count )
[303]263                {
[492]264                        return set_uniform_array( p, name, value, count, false );
[303]265                }
266
267                template < typename T >
[492]268                bool set_opt_uniform_array( program p, const string_view& name, const array_view<T>& value )
[303]269                {
[492]270                        return set_uniform_array( p, name, value.data(), value.size(), false );
[303]271                }
272
273
274                template < typename T >
[492]275                bool set_uniform( program p, const string_view& name, const T& value, bool fatal = true )
[303]276                {
277                        uniform_base* base = get_uniform( p, name, fatal );
278                        if ( base != nullptr )
279                        {
280                                if ( base->type_check( type_to_enum<T>::type ) )
281                                {
[501]282                                        static_cast<uniform<T>*>( base )->set_value( value );
[492]283                                        return true;
[303]284                                }
285                        }
[492]286                        return false;
[303]287                }
288
289                template < typename T >
[492]290                bool set_opt_uniform( program p, const string_view& name, const T& value )
[303]291                {
[492]292                        return set_uniform( p, name, value, false );
[303]293                }
294
295                // This is done this way to avoid compilation unit creation
296                static engine_uniform_factory_map& get_uniform_factory()
297                {
298                        static engine_uniform_factory_map s_engine_uniform_factory_map;
299                        return s_engine_uniform_factory_map;
300                }
301
302                // This is done this way to avoid compilation unit creation
303                static engine_link_uniform_factory_map& get_link_uniform_factory()
304                {
305                        static engine_link_uniform_factory_map s_engine_link_uniform_factory_map;
306                        return s_engine_link_uniform_factory_map;
307                }
308
309                virtual void prepare_program( program p ) = 0;
310
[302]311        protected:
[501]312
[439]313                virtual uniform_base* get_uniform( program p, const string_view& name, bool fatal = true ) const = 0;
[303]314
315                void initialize_engine_uniforms()
316                {
317                        engine_uniform_factory_map& factory_map = get_uniform_factory();
[486]318                        factory_map[ "nv_f_near"] = new engine_uniform_factory< engine_uniform_f_near >();
319                        factory_map[ "nv_f_far"]  = new engine_uniform_factory< engine_uniform_f_far >();
[462]320                        factory_map[ "nv_m_view" ]          = new engine_uniform_factory< engine_uniform_m_view >();
321                        factory_map[ "nv_m_view_inv" ]      = new engine_uniform_factory< engine_uniform_m_view_inv >();
322                        factory_map[ "nv_m_model" ]         = new engine_uniform_factory< engine_uniform_m_model >();
323                        factory_map[ "nv_m_model_inv" ]     = new engine_uniform_factory< engine_uniform_m_model_inv >();
324                        factory_map[ "nv_m_modelview" ]     = new engine_uniform_factory< engine_uniform_m_modelview >();
325                        factory_map[ "nv_m_modelview_inv" ] = new engine_uniform_factory< engine_uniform_m_modelview_inv >();
326                        factory_map[ "nv_m_projection" ]    = new engine_uniform_factory< engine_uniform_m_projection >();
[472]327                        factory_map[ "nv_m_projection_inv"] = new engine_uniform_factory< engine_uniform_m_projection_inv >();
[501]328                        factory_map[ "nv_m_viewprojection" ]     = new engine_uniform_factory< engine_uniform_m_viewprojection >();
329                        factory_map[ "nv_m_viewprojection_inv" ] = new engine_uniform_factory< engine_uniform_m_viewprojection_inv >();
[462]330                        factory_map[ "nv_m_normal" ]        = new engine_uniform_factory< engine_uniform_m_normal >();
331                        factory_map[ "nv_m_mvp" ]           = new engine_uniform_factory< engine_uniform_m_mvp >();
[472]332                        factory_map[ "nv_m_mvp_inv"]        = new engine_uniform_factory< engine_uniform_m_mvp_inv >();
[303]333                        factory_map[ "nv_v_camera_position" ]  = new engine_uniform_factory< engine_uniform_v_camera_position >();
334                        factory_map[ "nv_v_camera_direction" ] = new engine_uniform_factory< engine_uniform_v_camera_direction >();
[342]335                        factory_map[ "nv_v_viewport" ] = new engine_uniform_factory< engine_uniform_v_viewport >();
336                        factory_map[ "nv_v_screen_size" ] = new engine_uniform_factory< engine_uniform_v_screen_size >();
[303]337
338                        engine_link_uniform_factory_map& factory_link_map = get_link_uniform_factory();
[491]339                        factory_link_map[ "nv_texture_0"  ] = new engine_link_uniform_int<0>();
340                        factory_link_map[ "nv_texture_1"  ] = new engine_link_uniform_int<1>();
341                        factory_link_map[ "nv_texture_2"  ] = new engine_link_uniform_int<2>();
342                        factory_link_map[ "nv_texture_3"  ] = new engine_link_uniform_int<3>();
343                        factory_link_map[ "nv_texture_4"  ] = new engine_link_uniform_int<4>();
344                        factory_link_map[ "nv_texture_5"  ] = new engine_link_uniform_int<5>();
345                        factory_link_map[ "nv_texture_6"  ] = new engine_link_uniform_int<6>();
346                        factory_link_map[ "nv_texture_7"  ] = new engine_link_uniform_int<7>();
[501]347                        factory_link_map[ "nv_texture_8"  ] = new engine_link_uniform_int<8>();
348                        factory_link_map[ "nv_texture_9"  ] = new engine_link_uniform_int<9>();
[491]349                        factory_link_map[ "nv_t_diffuse"  ] = new engine_link_uniform_int<int( TEX_DIFFUSE )>();
350                        factory_link_map[ "nv_t_normal"   ] = new engine_link_uniform_int<int( TEX_NORMAL )>();
351                        factory_link_map[ "nv_t_metallic" ] = new engine_link_uniform_int<int( TEX_METALLIC )>();
352                        factory_link_map[ "nv_t_roughness"] = new engine_link_uniform_int<int( TEX_ROUGHNESS )>();
353                        factory_link_map[ "nv_t_emissive" ] = new engine_link_uniform_int<int( TEX_EMISSIVE )>();
[508]354                        factory_link_map[ "nv_t_depth"    ] = new engine_link_uniform_int<int( TEX_DEPTH )>();
355                        factory_link_map[ "nv_t_occlusion"] = new engine_link_uniform_int<int( TEX_OCCLUSION )>();
356                        factory_link_map[ "nv_t_final"    ] = new engine_link_uniform_int<int( TEX_FINAL )>();
[303]357                }
358                void destroy_engine_uniforms()
359                {
360                        for ( auto& i : get_uniform_factory() ) delete i.second;
361                        for ( auto& i : get_link_uniform_factory() ) delete i.second;
362                        get_uniform_factory().clear();
363                        get_link_uniform_factory().clear();
364                }
365
[32]366        };
367
368} // namespace nv
369
370
[395]371#endif // NV_INTERFACE_DEVICE_HH
Note: See TracBrowser for help on using the repository browser.