source: trunk/nv/interface/device.hh

Last change on this file was 550, checked in by epyon, 8 years ago
  • ECS and windowing updates
File size: 11.8 KB
Line 
1// Copyright (C) 2012-2015 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
6
7/**
8 * @file device.hh
9 * @author Kornel Kisielewicz epyon@chaosforge.org
10 * @brief Device class
11 */
12
13#ifndef NV_INTERFACE_DEVICE_HH
14#define NV_INTERFACE_DEVICE_HH
15
16#include <nv/common.hh>
17#include <nv/stl/string.hh>
18#include <nv/stl/handle.hh>
19#include <nv/stl/hash_store.hh>
20#include <nv/interface/uniform.hh>
21#include <nv/interface/mesh_data.hh>
22#include <nv/interface/image_data.hh>
23
24namespace nv
25{
26        class window;
27
28        enum shader_type
29        {
30                VERTEX_SHADER,
31                FRAGMENT_SHADER
32        };
33
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,
57                TEXTURE_1D_ARRAY,
58                TEXTURE_2D_ARRAY,
59                TEXTURE_1D_BUFFER,
60                TEXTURE_2D_MULTISAMPLE,
61        };
62
63        enum texture_slot
64        {
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,
81                MAX_TEXTURES  = 16,
82        };
83
84
85        struct attribute
86        {
87                int      location;
88                datatype type;
89                int      length;
90        };
91
92        typedef hash_store< shash64, attribute >    attribute_map;
93
94        struct texture_tag {};
95        struct buffer_tag {};
96        struct shader_tag {};
97        struct program_tag {};
98
99        typedef handle< uint32, 16, 16, buffer_tag >       buffer;
100        typedef handle< uint32, 16, 16, texture_tag >      texture;
101        typedef handle< uint32, 16, 16, shader_tag >       shader;
102        typedef handle< uint32, 16, 16, program_tag >      program;
103
104        NV_RTTI_DECLARE_NAME( buffer,  "buffer" )
105        NV_RTTI_DECLARE_NAME( texture, "texture" )
106        NV_RTTI_DECLARE_NAME( shader,  "shader" )
107        NV_RTTI_DECLARE_NAME( program, "program" )
108
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                wrap wrap_r;
133
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 ) {}
137                sampler( filter f, wrap w )
138                        : filter_min( f ), filter_max( f ), wrap_s( w ), wrap_t( w ), wrap_r( w )
139                {}
140
141        };
142
143        enum buffer_hint
144        {
145                STATIC_DRAW,
146                STREAM_DRAW,
147                DYNAMIC_DRAW
148        };
149
150        enum buffer_type
151        {
152                VERTEX_BUFFER,
153                INDEX_BUFFER,
154                UNIFORM_BUFFER,
155                TEXTURE_BUFFER,
156        };
157
158        enum buffer_access
159        {
160                WRITE_UNSYNCHRONIZED,
161        };
162
163        struct buffer_info
164        {
165                buffer_type type;
166                buffer_hint hint;
167                size_t      size;
168        };
169
170
171        struct texture_info
172        {
173                texture_type type;
174                ivec3        size;
175                pixel_format format;
176                sampler      tsampler;
177        };
178
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     interpolate;
188                bool     owner;
189        };
190
191        struct shader_info
192        {
193                shader_type type;
194        };
195
196        struct program_info
197        {
198                attribute_map*       m_attribute_map;
199                uniform_map*         m_uniform_map;
200                engine_uniform_list* m_engine_uniforms;
201        };
202
203        class device
204        {
205                friend class context;
206        public:
207                device()
208                {
209                        initialize_engine_uniforms();
210                }
211        protected:
212                virtual shader create_shader( shader_type type, string_view sh_source ) = 0;
213//              virtual program create_program( string_view vs_source, string_view fs_source ) = 0;
214                virtual program create_program( shader vs, shader fs ) = 0;
215                virtual buffer  create_buffer( buffer_type type, buffer_hint hint ) = 0;
216                virtual texture create_texture( texture_type type, pixel_format format ) = 0;
217
218                // TODO: remove?
219//              virtual texture create_texture( ivec2 size, image_format aformat, sampler asampler, const void* data = nullptr ) { return create_texture( TEXTURE_2D, size, aformat, asampler, data ); }
220                virtual void attach( program p, shader s ) = 0;
221                virtual void detach( program p, shader s ) = 0;
222
223                virtual void release( texture ) = 0;
224                virtual void release( buffer ) = 0;
225                virtual void release( program ) = 0;
226                virtual void release( shader ) = 0;
227        public:
228
229
230                virtual image_data* create_image_data( string_view filename ) = 0; // temporary
231                virtual image_data* create_image_data( const uint8* data, uint32 size ) = 0; // temporary
232                virtual const texture_info* get_texture_info( texture ) const = 0;
233                virtual const buffer_info* get_buffer_info( buffer ) const = 0;
234                virtual string_view get_shader_header() const = 0;
235
236                virtual int get_attribute_location( program p, const string_view& name, bool fatal = true ) const = 0;
237                virtual int get_block_location( program p, const string_view& name, bool fatal = true ) const = 0;
238                virtual bool bind_block( program p, const string_view& name, uint32 index ) = 0;
239
240                virtual ~device()
241                {
242                        destroy_engine_uniforms();
243                }
244
245                template < typename T >
246                bool set_uniform_array( program p, const string_view& name, const T* value, uint32 count, bool fatal = true )
247                {
248                        uniform_base* base = get_uniform( p, name, fatal );
249                        if ( base != nullptr )
250                        {
251                                if ( base->type_check( type_to_enum<T>::type ) )
252                                {
253                                        // TODO: nicer check
254                                        NV_ASSERT( static_cast<int>( count ) <= base->get_length(), "LENGTH CHECK FAIL" );
255                                        static_cast<uniform<T>*>( base )->set_value( value, count );
256                                        return true;
257                                }
258                        }
259                        return false;
260                }
261
262                template < typename T >
263                bool set_opt_uniform_array( program p, const string_view& name, const T* value, uint32 count )
264                {
265                        return set_uniform_array( p, name, value, count, false );
266                }
267
268                template < typename T >
269                bool set_opt_uniform_array( program p, const string_view& name, const array_view<T>& value )
270                {
271                        return set_uniform_array( p, name, value.data(), value.size(), false );
272                }
273
274
275                template < typename T >
276                bool set_uniform( program p, const string_view& name, const T& value, bool fatal = true )
277                {
278                        uniform_base* base = get_uniform( p, name, fatal );
279                        if ( base != nullptr )
280                        {
281                                if ( base->type_check( type_to_enum<T>::type ) )
282                                {
283                                        static_cast<uniform<T>*>( base )->set_value( value );
284                                        return true;
285                                }
286                        }
287                        return false;
288                }
289
290                template < typename T >
291                bool set_opt_uniform( program p, const string_view& name, const T& value )
292                {
293                        return set_uniform( p, name, value, false );
294                }
295
296                // This is done this way to avoid compilation unit creation
297                static engine_uniform_factory_map& get_uniform_factory()
298                {
299                        static engine_uniform_factory_map s_engine_uniform_factory_map;
300                        return s_engine_uniform_factory_map;
301                }
302
303                // This is done this way to avoid compilation unit creation
304                static engine_link_uniform_factory_map& get_link_uniform_factory()
305                {
306                        static engine_link_uniform_factory_map s_engine_link_uniform_factory_map;
307                        return s_engine_link_uniform_factory_map;
308                }
309
310                virtual void prepare_program( program p ) = 0;
311
312        protected:
313
314                virtual uniform_base* get_uniform( program p, const string_view& name, bool fatal = true ) const = 0;
315
316                void initialize_engine_uniforms()
317                {
318                        engine_uniform_factory_map& factory_map = get_uniform_factory();
319                        factory_map[ "nv_f_near"] = new engine_uniform_factory< engine_uniform_f_near >();
320                        factory_map[ "nv_f_far"]  = new engine_uniform_factory< engine_uniform_f_far >();
321                        factory_map[ "nv_m_view" ]          = new engine_uniform_factory< engine_uniform_m_view >();
322                        factory_map[ "nv_m_view_inv" ]      = new engine_uniform_factory< engine_uniform_m_view_inv >();
323                        factory_map[ "nv_m_model" ]         = new engine_uniform_factory< engine_uniform_m_model >();
324                        factory_map[ "nv_m_model_inv" ]     = new engine_uniform_factory< engine_uniform_m_model_inv >();
325                        factory_map[ "nv_m_modelview" ]     = new engine_uniform_factory< engine_uniform_m_modelview >();
326                        factory_map[ "nv_m_modelview_inv" ] = new engine_uniform_factory< engine_uniform_m_modelview_inv >();
327                        factory_map[ "nv_m_projection" ]    = new engine_uniform_factory< engine_uniform_m_projection >();
328                        factory_map[ "nv_m_projection_inv"] = new engine_uniform_factory< engine_uniform_m_projection_inv >();
329                        factory_map[ "nv_m_viewprojection" ]     = new engine_uniform_factory< engine_uniform_m_viewprojection >();
330                        factory_map[ "nv_m_viewprojection_inv" ] = new engine_uniform_factory< engine_uniform_m_viewprojection_inv >();
331                        factory_map[ "nv_m_normal" ]        = new engine_uniform_factory< engine_uniform_m_normal >();
332                        factory_map[ "nv_m_mvp" ]           = new engine_uniform_factory< engine_uniform_m_mvp >();
333                        factory_map[ "nv_m_mvp_inv"]        = new engine_uniform_factory< engine_uniform_m_mvp_inv >();
334                        factory_map[ "nv_v_camera_position" ]  = new engine_uniform_factory< engine_uniform_v_camera_position >();
335                        factory_map[ "nv_v_camera_direction" ] = new engine_uniform_factory< engine_uniform_v_camera_direction >();
336                        factory_map[ "nv_v_viewport" ] = new engine_uniform_factory< engine_uniform_v_viewport >();
337                        factory_map[ "nv_v_screen_size" ] = new engine_uniform_factory< engine_uniform_v_screen_size >();
338
339                        engine_link_uniform_factory_map& factory_link_map = get_link_uniform_factory();
340                        factory_link_map[ "nv_texture_0"  ] = new engine_link_uniform_int<0>();
341                        factory_link_map[ "nv_texture_1"  ] = new engine_link_uniform_int<1>();
342                        factory_link_map[ "nv_texture_2"  ] = new engine_link_uniform_int<2>();
343                        factory_link_map[ "nv_texture_3"  ] = new engine_link_uniform_int<3>();
344                        factory_link_map[ "nv_texture_4"  ] = new engine_link_uniform_int<4>();
345                        factory_link_map[ "nv_texture_5"  ] = new engine_link_uniform_int<5>();
346                        factory_link_map[ "nv_texture_6"  ] = new engine_link_uniform_int<6>();
347                        factory_link_map[ "nv_texture_7"  ] = new engine_link_uniform_int<7>();
348                        factory_link_map[ "nv_texture_8"  ] = new engine_link_uniform_int<8>();
349                        factory_link_map[ "nv_texture_9"  ] = new engine_link_uniform_int<9>();
350                        factory_link_map[ "nv_t_diffuse"  ] = new engine_link_uniform_int<int( TEX_DIFFUSE )>();
351                        factory_link_map[ "nv_t_normal"   ] = new engine_link_uniform_int<int( TEX_NORMAL )>();
352                        factory_link_map[ "nv_t_metallic" ] = new engine_link_uniform_int<int( TEX_METALLIC )>();
353                        factory_link_map[ "nv_t_roughness"] = new engine_link_uniform_int<int( TEX_ROUGHNESS )>();
354                        factory_link_map[ "nv_t_emissive" ] = new engine_link_uniform_int<int( TEX_EMISSIVE )>();
355                        factory_link_map[ "nv_t_depth"    ] = new engine_link_uniform_int<int( TEX_DEPTH )>();
356                        factory_link_map[ "nv_t_occlusion"] = new engine_link_uniform_int<int( TEX_OCCLUSION )>();
357                        factory_link_map[ "nv_t_final"    ] = new engine_link_uniform_int<int( TEX_FINAL )>();
358                }
359                void destroy_engine_uniforms()
360                {
361                        for ( auto& i : get_uniform_factory() ) delete i.second;
362                        for ( auto& i : get_link_uniform_factory() ) delete i.second;
363                        get_uniform_factory().clear();
364                        get_link_uniform_factory().clear();
365                }
366
367        };
368
369} // namespace nv
370
371
372#endif // NV_INTERFACE_DEVICE_HH
Note: See TracBrowser for help on using the repository browser.