// Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz // http://chaosforge.org/ // // This file is part of NV Libraries. // For conditions of distribution and use, see copyright notice in nv.hh /** * @file device.hh * @author Kornel Kisielewicz epyon@chaosforge.org * @brief Device class */ #ifndef NV_DEVICE_HH #define NV_DEVICE_HH #include #include #include #include #include #include namespace nv { class window; class program; class device { public: virtual window* create_window( uint16 width, uint16 height, bool fullscreen ) = 0; virtual program* create_program( const string& vs_source, const string& fs_source ) = 0; virtual vertex_buffer* create_vertex_buffer( buffer_hint hint, size_t size, const void* source = nullptr ) = 0; virtual index_buffer* create_index_buffer( buffer_hint hint, size_t size, const void* source = nullptr ) = 0; virtual vertex_array* create_vertex_array() = 0; virtual image_data* create_image_data( const std::string& filename ) = 0; // temporary virtual texture2d* create_texture2d( ivec2 size, image_format aformat, datatype adatatype, sampler asampler, void* data = nullptr ) = 0; virtual uint32 get_ticks() = 0; virtual void delay( uint32 ms ) = 0; virtual vertex_array* create_vertex_array( const mesh_pack* m, const attribute_map* am, buffer_hint hint ) { vertex_array* result = create_vertex_array(); for ( auto& attr : m->get_attributes() ) { // TODO : error checking vertex_buffer* vb = create_vertex_buffer( hint, attr.second->get_size(), attr.second->get_data() ); result->add_vertex_buffer( am->at( attr.first )->get_location(), vb, attr.second->get_base_type(), attr.second->get_components() ); } if ( m->has_indices() ) { const vertex_attribute_base* i = m->get_indices(); index_buffer* vb = create_index_buffer( hint, i->get_size(), i->get_data() ); result->set_index_buffer( vb, i->get_base_type(), true ); } return result; } template < typename VTX, slot SLOT > void add_vertex_buffer_impl( vertex_array*, vertex_buffer*, const std::false_type& ) { } template < typename VTX, slot SLOT > void add_vertex_buffer_impl( vertex_array* va, vertex_buffer* vb, const std::true_type& ) { typedef vertex_slot_info< VTX, SLOT > vinfo; typedef datatype_traits< typename vinfo::value_type > dt_traits; va->add_vertex_buffer( SLOT, vb, type_to_enum< dt_traits::base_type >::type, dt_traits::size, vinfo::offset, sizeof( VTX ), false ); } template < typename VTX, slot SLOT > void add_vertex_buffer( vertex_array* va, vertex_buffer* vb ) { add_vertex_buffer_impl< VTX, SLOT >( va, vb, std::integral_constant< bool, vertex_has_slot< VTX, SLOT >::value >() ); } template < typename VTX > vertex_array* create_vertex_array( const VTX* v, size_t count, buffer_hint hint ) { vertex_array* va = create_vertex_array(); vertex_buffer* vb = create_vertex_buffer( hint, count * sizeof( VTX ), v ); add_vertex_buffer< VTX, slot::POSITION > ( va, vb ); add_vertex_buffer< VTX, slot::TEXCOORD > ( va, vb ); add_vertex_buffer< VTX, slot::NORMAL > ( va, vb ); add_vertex_buffer< VTX, slot::TANGENT > ( va, vb ); add_vertex_buffer< VTX, slot::BONEINDEX > ( va, vb ); add_vertex_buffer< VTX, slot::BONEWEIGHT >( va, vb ); add_vertex_buffer< VTX, slot::COLOR > ( va, vb ); return va; } template < typename VTX > vertex_array* create_vertex_array( const std::vector< VTX >& data, buffer_hint hint ) { return create_vertex_array( data.data(), data.size(), hint ); } template < typename VTX, typename IDX > vertex_array* create_vertex_array( const VTX* v, size_t vcount, const IDX* i, size_t icount, buffer_hint hint ) { vertex_array* va = create_vertex_array( v, vcount, hint ); index_buffer* ib = create_index_buffer( hint, icount * sizeof( IDX ), i ); va->set_index_buffer( ib, type_to_enum< IDX >::type, true ); return va; } template < typename VTX, typename IDX > vertex_array* create_vertex_array( const std::vector< VTX >& data, const std::vector< IDX >& idata, buffer_hint hint ) { return create_vertex_array( data.data(), data.size(), idata.data(), idata.size(), hint ); } virtual ~device() {} }; } // namespace nv #endif // NV_DEVICE_HH