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

Last change on this file since 301 was 301, checked in by epyon, 11 years ago
  • textures are now handled by lightweight handles
  • textures now need to be manually released via context
  • removed all old texture2d functionality
  • unreleased textures will be auto-released
  • textures are properly tracked via entity system
  • detailed stats and checking now possible
File size: 5.7 KB
Line 
1// Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
2// http://chaosforge.org/
3//
4// This file is part of NV Libraries.
5// For conditions of distribution and use, see copyright notice in nv.hh
6/**
7 * @file device.hh
8 * @author Kornel Kisielewicz epyon@chaosforge.org
9 * @brief Device class
10 */
11
12#ifndef NV_DEVICE_HH
13#define NV_DEVICE_HH
14
15#include <nv/common.hh>
16#include <nv/string.hh>
17#include <nv/handle.hh>
18#include <nv/interface/mesh_data.hh>
19#include <nv/interface/vertex_buffer.hh>
20#include <nv/interface/image_data.hh>
21
22namespace nv
23{
24        class window;
25        class program;
26
27        struct sampler
28        {
29                enum filter
30                {
31                        LINEAR,
32                        NEAREST,
33                        NEAREST_MIPMAP_NEAREST,
34                        LINEAR_MIPMAP_NEAREST,
35                        NEAREST_MIPMAP_LINEAR,
36                        LINEAR_MIPMAP_LINEAR
37                };
38                enum wrap
39                {
40                        CLAMP_TO_EDGE,
41                        CLAMP_TO_BORDER,
42                        MIRRORED_REPEAT,
43                        REPEAT
44                };
45
46                filter filter_min;
47                filter filter_max;
48                wrap wrap_s;
49                wrap wrap_t;
50
51                sampler() : filter_min( LINEAR ), filter_max( LINEAR ), wrap_s( REPEAT ), wrap_t( REPEAT ) {}
52                sampler( filter min, filter max, wrap s, wrap t )
53                        : filter_min( min ), filter_max( max ), wrap_s( s ), wrap_t( t ) {}
54                sampler( filter f, wrap w )
55                        : filter_min( f ), filter_max( f ), wrap_s( w ), wrap_t( w ) {}
56
57        };
58
59        struct texture_info
60        {
61                ivec2        size;
62                image_format format;
63                sampler      sampler;
64        };
65
66
67        struct texture_tag {};
68        typedef handle< uint32, 16, 16, texture_tag > texture;
69
70        class device
71        {
72        public:
73                virtual window* create_window( uint16 width, uint16 height, bool fullscreen ) = 0;
74                virtual window* adopt_window( void* sys_w_handle, void* sys_dc ) = 0;
75                virtual program* create_program( const string& vs_source, const string& fs_source ) = 0;
76                virtual vertex_buffer* create_vertex_buffer( buffer_hint hint, size_t size, const void* source = nullptr ) = 0;
77                virtual index_buffer* create_index_buffer( buffer_hint hint, size_t size, const void* source = nullptr ) = 0;
78                virtual vertex_array* create_vertex_array() = 0;
79                virtual image_data* create_image_data( const std::string& filename ) = 0; // temporary
80                virtual texture create_texture( ivec2 size, image_format aformat, sampler asampler, void* data = nullptr ) = 0;
81                virtual void release_texture( texture ) = 0;
82                virtual const texture_info* get_texture_info( texture ) = 0;
83                virtual uint32 get_ticks() = 0;
84                virtual void delay( uint32 ms ) = 0;
85
86                virtual texture create_texture( image_data* data, sampler asampler )
87                {
88                        return create_texture( data->get_size(), data->get_format(), asampler, (void*)data->get_data() );
89                }
90
91                template < typename VTX, slot SLOT >
92                void add_vertex_buffer_impl( vertex_array*, vertex_buffer*, const std::false_type& )
93                {
94                }
95
96                template < typename VTX, slot SLOT >
97                void add_vertex_buffer_impl( vertex_array* va, vertex_buffer* vb, const std::true_type& )
98                {
99                        typedef vertex_slot_info< VTX, SLOT > vinfo;
100                        typedef datatype_traits< typename vinfo::value_type > dt_traits;
101                        va->add_vertex_buffer( SLOT, vb, type_to_enum< dt_traits::base_type >::type, dt_traits::size, vinfo::offset, sizeof( VTX ), false );
102                }
103
104                template < typename VTX, slot SLOT >
105                void add_vertex_buffer( vertex_array* va, vertex_buffer* vb )
106                {
107                        add_vertex_buffer_impl< VTX, SLOT >( va, vb, std::integral_constant< bool, vertex_has_slot< VTX, SLOT >::value >() );
108                }
109
110
111                template < typename VTX >
112                vertex_array* create_vertex_array( const VTX* v, size_t count, buffer_hint hint )
113                {
114                        // TODO: vb will not be owned or freed!
115                        vertex_array*  va = create_vertex_array();
116                        vertex_buffer* vb = create_vertex_buffer( hint, count * sizeof( VTX ), v );
117                        add_vertex_buffer< VTX, slot::POSITION >  ( va, vb );
118                        add_vertex_buffer< VTX, slot::TEXCOORD >  ( va, vb );
119                        add_vertex_buffer< VTX, slot::NORMAL   >  ( va, vb );
120                        add_vertex_buffer< VTX, slot::TANGENT >   ( va, vb );
121                        add_vertex_buffer< VTX, slot::BONEINDEX > ( va, vb );
122                        add_vertex_buffer< VTX, slot::BONEWEIGHT >( va, vb );
123                        add_vertex_buffer< VTX, slot::COLOR >     ( va, vb );
124                        return va;
125                }
126
127                template < typename VTX >
128                vertex_array* create_vertex_array( const std::vector< VTX >& data, buffer_hint hint )
129                {
130                        return create_vertex_array( data.data(), data.size(), hint );
131                }
132
133                template < typename VTX, typename IDX >
134                vertex_array* create_vertex_array( const VTX* v, size_t vcount, const IDX* i, size_t icount, buffer_hint hint )
135                {
136                        vertex_array* va = create_vertex_array( v, vcount, hint );
137                        index_buffer* ib = create_index_buffer( hint, icount * sizeof( IDX ), i );
138                        va->set_index_buffer( ib, type_to_enum< IDX >::type, true );
139                        return va;
140                }
141
142                template < typename VTX, typename IDX >
143                vertex_array* create_vertex_array( const std::vector< VTX >& data, const std::vector< IDX >& idata, buffer_hint hint )
144                {
145                        return create_vertex_array( data.data(), data.size(), idata.data(), idata.size(), hint );
146                }
147
148                // TODO: HINTS ARE DIFFERENT!
149                vertex_array* create_vertex_array( const mesh_data* data, buffer_hint hint )
150                {
151                        vertex_array*  va = create_vertex_array();
152                        const std::vector< mesh_raw_channel* >& channels = data->get_raw_channels();
153                        for ( uint32 ch = 0; ch < channels.size(); ++ch )
154                        {
155                                const mesh_raw_channel* channel = channels[ch];
156                                if ( channel->count > 0 )
157                                {
158                                        if ( channel->is_index() )
159                                        {
160                                                index_buffer* ib = create_index_buffer( hint, channel->size(), channel->data );
161                                                va->set_index_buffer( ib, channel->desc.slots[0].etype, true );
162                                        }
163                                        else
164                                        {
165                                                vertex_buffer* vb = create_vertex_buffer( hint, channel->size(), channel->data );
166                                                va->add_vertex_buffers( vb, channel );
167                                        }
168                                }
169                        }
170                        return va;
171                }
172
173
174                virtual ~device() {}
175        };
176
177} // namespace nv
178
179
180#endif // NV_DEVICE_HH
Note: See TracBrowser for help on using the repository browser.