[32] | 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>
|
---|
[238] | 17 | #include <nv/interface/mesh_data.hh>
|
---|
[42] | 18 | #include <nv/interface/vertex_buffer.hh>
|
---|
[45] | 19 | #include <nv/interface/texture2d.hh>
|
---|
[90] | 20 | #include <nv/interface/image_data.hh>
|
---|
[32] | 21 |
|
---|
| 22 | namespace nv
|
---|
| 23 | {
|
---|
| 24 | class window;
|
---|
| 25 | class program;
|
---|
| 26 |
|
---|
| 27 | class device
|
---|
| 28 | {
|
---|
| 29 | public:
|
---|
[228] | 30 | virtual window* create_window( uint16 width, uint16 height, bool fullscreen ) = 0;
|
---|
[245] | 31 | virtual window* adopt_window( void* sys_w_handle, void* sys_dc ) = 0;
|
---|
[39] | 32 | virtual program* create_program( const string& vs_source, const string& fs_source ) = 0;
|
---|
[153] | 33 | virtual vertex_buffer* create_vertex_buffer( buffer_hint hint, size_t size, const void* source = nullptr ) = 0;
|
---|
| 34 | virtual index_buffer* create_index_buffer( buffer_hint hint, size_t size, const void* source = nullptr ) = 0;
|
---|
[44] | 35 | virtual vertex_array* create_vertex_array() = 0;
|
---|
[90] | 36 | virtual image_data* create_image_data( const std::string& filename ) = 0; // temporary
|
---|
[70] | 37 | virtual texture2d* create_texture2d( ivec2 size, image_format aformat, datatype adatatype, sampler asampler, void* data = nullptr ) = 0;
|
---|
[92] | 38 | virtual uint32 get_ticks() = 0;
|
---|
| 39 | virtual void delay( uint32 ms ) = 0;
|
---|
[73] | 40 |
|
---|
[237] | 41 | template < typename VTX, slot SLOT >
|
---|
| 42 | void add_vertex_buffer_impl( vertex_array*, vertex_buffer*, const std::false_type& )
|
---|
| 43 | {
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | template < typename VTX, slot SLOT >
|
---|
| 47 | void add_vertex_buffer_impl( vertex_array* va, vertex_buffer* vb, const std::true_type& )
|
---|
| 48 | {
|
---|
| 49 | typedef vertex_slot_info< VTX, SLOT > vinfo;
|
---|
| 50 | typedef datatype_traits< typename vinfo::value_type > dt_traits;
|
---|
| 51 | va->add_vertex_buffer( SLOT, vb, type_to_enum< dt_traits::base_type >::type, dt_traits::size, vinfo::offset, sizeof( VTX ), false );
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | template < typename VTX, slot SLOT >
|
---|
| 55 | void add_vertex_buffer( vertex_array* va, vertex_buffer* vb )
|
---|
| 56 | {
|
---|
| 57 | add_vertex_buffer_impl< VTX, SLOT >( va, vb, std::integral_constant< bool, vertex_has_slot< VTX, SLOT >::value >() );
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 | template < typename VTX >
|
---|
| 62 | vertex_array* create_vertex_array( const VTX* v, size_t count, buffer_hint hint )
|
---|
| 63 | {
|
---|
| 64 | vertex_array* va = create_vertex_array();
|
---|
| 65 | vertex_buffer* vb = create_vertex_buffer( hint, count * sizeof( VTX ), v );
|
---|
| 66 | add_vertex_buffer< VTX, slot::POSITION > ( va, vb );
|
---|
| 67 | add_vertex_buffer< VTX, slot::TEXCOORD > ( va, vb );
|
---|
| 68 | add_vertex_buffer< VTX, slot::NORMAL > ( va, vb );
|
---|
| 69 | add_vertex_buffer< VTX, slot::TANGENT > ( va, vb );
|
---|
| 70 | add_vertex_buffer< VTX, slot::BONEINDEX > ( va, vb );
|
---|
| 71 | add_vertex_buffer< VTX, slot::BONEWEIGHT >( va, vb );
|
---|
| 72 | add_vertex_buffer< VTX, slot::COLOR > ( va, vb );
|
---|
| 73 | return va;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | template < typename VTX >
|
---|
| 77 | vertex_array* create_vertex_array( const std::vector< VTX >& data, buffer_hint hint )
|
---|
| 78 | {
|
---|
| 79 | return create_vertex_array( data.data(), data.size(), hint );
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | template < typename VTX, typename IDX >
|
---|
| 83 | vertex_array* create_vertex_array( const VTX* v, size_t vcount, const IDX* i, size_t icount, buffer_hint hint )
|
---|
| 84 | {
|
---|
| 85 | vertex_array* va = create_vertex_array( v, vcount, hint );
|
---|
| 86 | index_buffer* ib = create_index_buffer( hint, icount * sizeof( IDX ), i );
|
---|
| 87 | va->set_index_buffer( ib, type_to_enum< IDX >::type, true );
|
---|
| 88 | return va;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | template < typename VTX, typename IDX >
|
---|
| 92 | vertex_array* create_vertex_array( const std::vector< VTX >& data, const std::vector< IDX >& idata, buffer_hint hint )
|
---|
| 93 | {
|
---|
| 94 | return create_vertex_array( data.data(), data.size(), idata.data(), idata.size(), hint );
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[238] | 97 | // TODO: HINTS ARE DIFFERENT!
|
---|
| 98 | vertex_array* create_vertex_array( const mesh_data* data, buffer_hint hint )
|
---|
| 99 | {
|
---|
| 100 | vertex_array* va = create_vertex_array();
|
---|
| 101 | for ( uint32 ch = 0; ch < data->get_channel_data().size(); ++ch )
|
---|
| 102 | {
|
---|
| 103 | const mesh_raw_channel* channel = data->get_channel_data()[ch];
|
---|
| 104 | vertex_buffer* vb = create_vertex_buffer( hint, channel->size, channel->data );
|
---|
[239] | 105 | va->add_vertex_buffers( vb, channel );
|
---|
[238] | 106 | }
|
---|
| 107 | if ( data->get_index_channel() != nullptr )
|
---|
| 108 | {
|
---|
| 109 | const mesh_raw_index_channel* index = data->get_index_channel();
|
---|
| 110 | index_buffer* ib = create_index_buffer( hint, index->size, index->data );
|
---|
| 111 | va->set_index_buffer( ib, index->etype, true );
|
---|
| 112 | }
|
---|
| 113 | return va;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 |
|
---|
[121] | 117 | virtual ~device() {}
|
---|
[32] | 118 | };
|
---|
| 119 |
|
---|
| 120 | } // namespace nv
|
---|
| 121 |
|
---|
| 122 |
|
---|
| 123 | #endif // NV_DEVICE_HH
|
---|