[418] | 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 | #ifndef NV_INTERFACE_DATA_CHANNEL_HH
|
---|
| 8 | #define NV_INTERFACE_DATA_CHANNEL_HH
|
---|
| 9 |
|
---|
| 10 | #include <nv/common.hh>
|
---|
| 11 | #include <nv/stl/vector.hh>
|
---|
| 12 | #include <nv/interface/data_descriptor.hh>
|
---|
| 13 |
|
---|
| 14 | namespace nv
|
---|
| 15 | {
|
---|
[482] | 16 | struct data_node_info
|
---|
| 17 | {
|
---|
| 18 | shash64 name;
|
---|
| 19 | sint16 parent_id;
|
---|
| 20 | mat4 transform;
|
---|
| 21 | };
|
---|
[418] | 22 |
|
---|
| 23 | struct raw_data_channel
|
---|
| 24 | {
|
---|
| 25 | raw_data_channel( raw_data_channel&& other )
|
---|
| 26 | {
|
---|
| 27 | m_data = other.m_data;
|
---|
| 28 | m_size = other.m_size;
|
---|
| 29 | m_desc = other.m_desc;
|
---|
| 30 | other.m_data = nullptr;
|
---|
| 31 | other.m_size = 0;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | raw_data_channel& operator=( raw_data_channel&& other )
|
---|
| 35 | {
|
---|
| 36 | if ( this != &other )
|
---|
| 37 | {
|
---|
| 38 | if ( m_data != nullptr ) delete[] m_data;
|
---|
| 39 | m_data = other.m_data;
|
---|
| 40 | m_size = other.m_size;
|
---|
| 41 | m_desc = other.m_desc;
|
---|
| 42 | other.m_data = nullptr;
|
---|
| 43 | other.m_size = 0;
|
---|
| 44 | }
|
---|
| 45 | return *this;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | const data_descriptor& descriptor() const { return m_desc; }
|
---|
| 49 | uint32 element_size() const { return m_desc.element_size(); }
|
---|
| 50 | uint32 size() const { return m_size; }
|
---|
| 51 | uint32 raw_size() const { return m_size * m_desc.element_size(); }
|
---|
| 52 | const uint8* raw_data() const { return m_data; }
|
---|
| 53 |
|
---|
| 54 | // TODO: constexpr compile-time cast check?
|
---|
| 55 | template < typename Struct >
|
---|
| 56 | const Struct* data_cast() const
|
---|
| 57 | {
|
---|
| 58 | return reinterpret_cast<const Struct*>( m_data );
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[456] | 61 | // TODO: constexpr compile-time cast check?
|
---|
| 62 | template < typename T >
|
---|
| 63 | const T& extract( uint16 slot_index, uint32 index ) const
|
---|
| 64 | {
|
---|
[487] | 65 | NV_ASSERT( slot_index < m_desc.size(), "Bas slot passed to extract!" );
|
---|
[456] | 66 | return *reinterpret_cast<const T*>( m_data + m_desc.element_size()*index + m_desc[slot_index].offset );
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[418] | 69 | bool has_slot( slot vslot ) const { return m_desc.has_slot( vslot ); }
|
---|
| 70 |
|
---|
| 71 | ~raw_data_channel()
|
---|
| 72 | {
|
---|
| 73 | if ( m_data != nullptr ) delete[] m_data;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | protected:
|
---|
| 77 |
|
---|
| 78 | template < typename Struct >
|
---|
| 79 | friend class data_channel_access;
|
---|
| 80 | friend class raw_data_channel_access;
|
---|
| 81 | friend class data_channel_creator;
|
---|
| 82 | friend class data_channel_set;
|
---|
| 83 |
|
---|
| 84 | raw_data_channel() : m_data( nullptr ), m_size( 0 ) {}
|
---|
| 85 | raw_data_channel( const raw_data_channel& ) = delete;
|
---|
| 86 | raw_data_channel& operator=( const raw_data_channel& ) = delete;
|
---|
| 87 |
|
---|
| 88 | uint8* m_data;
|
---|
| 89 | uint32 m_size;
|
---|
| 90 | data_descriptor m_desc;
|
---|
| 91 | };
|
---|
| 92 |
|
---|
| 93 | class data_channel_set
|
---|
| 94 | {
|
---|
| 95 | public:
|
---|
| 96 | friend class data_channel_set_creator;
|
---|
| 97 | friend class raw_data_channel_access;
|
---|
[424] | 98 | friend class mesh_nodes_creator;
|
---|
[418] | 99 |
|
---|
| 100 | typedef const raw_data_channel* const_iterator;
|
---|
| 101 |
|
---|
[421] | 102 | data_channel_set( data_channel_set&& other )
|
---|
| 103 | {
|
---|
| 104 | for ( uint32 c = 0; c < other.m_size; ++c )
|
---|
| 105 | m_channels[c] = move( other.m_channels[c] );
|
---|
[431] | 106 | m_size = other.m_size;
|
---|
[421] | 107 | other.m_size = 0;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | data_channel_set& operator=( data_channel_set&& other )
|
---|
| 111 | {
|
---|
| 112 | if ( this != &other )
|
---|
| 113 | {
|
---|
| 114 | for ( uint32 c = 0; c < other.m_size; ++c )
|
---|
| 115 | m_channels[c] = move( other.m_channels[c] );
|
---|
[424] | 116 | m_size = other.m_size;
|
---|
[421] | 117 | other.m_size = 0;
|
---|
| 118 | }
|
---|
| 119 | return *this;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[418] | 122 | size_t size() const { return m_size; }
|
---|
| 123 |
|
---|
| 124 | const raw_data_channel* get_channel( size_t index ) const
|
---|
| 125 | {
|
---|
| 126 | if ( m_size > index ) return &m_channels[index];
|
---|
| 127 | return nullptr;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | const raw_data_channel* get_channel( slot s ) const
|
---|
| 131 | {
|
---|
| 132 | for ( uint32 c = 0; c < m_size; ++c )
|
---|
| 133 | if ( m_channels[c].has_slot( s ) )
|
---|
| 134 | return &m_channels[c];
|
---|
| 135 | return nullptr;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | template < typename Struct >
|
---|
| 139 | const raw_data_channel* get_channel() const
|
---|
| 140 | {
|
---|
| 141 | data_descriptor compare;
|
---|
| 142 | compare.initialize<Struct>();
|
---|
| 143 | for ( uint32 c = 0; c < m_size; ++c )
|
---|
| 144 | {
|
---|
| 145 | if ( m_channels[c].descriptor() == compare )
|
---|
| 146 | {
|
---|
| 147 | return &m_channels[c];
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 | return nullptr;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | size_t get_channel_size( slot s ) const
|
---|
| 154 | {
|
---|
| 155 | const raw_data_channel* channel = get_channel( s );
|
---|
| 156 | return channel ? channel->size() : 0;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | size_t get_channel_size( size_t channel ) const
|
---|
| 160 | {
|
---|
| 161 | if ( m_size > channel ) return m_channels[channel].size();
|
---|
| 162 | return 0;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | template < typename Struct >
|
---|
| 166 | size_t get_channel_size() const
|
---|
| 167 | {
|
---|
| 168 | data_descriptor compare;
|
---|
| 169 | compare.initialize<Struct>();
|
---|
| 170 | for ( uint32 c = 0; c < m_size; ++c )
|
---|
| 171 | {
|
---|
| 172 | if ( m_channels[c].descriptor() == compare )
|
---|
| 173 | {
|
---|
| 174 | return m_channels[c].size();
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 | return 0;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | template < typename VTX >
|
---|
| 181 | const VTX* get_channel_data() const
|
---|
| 182 | {
|
---|
| 183 | data_descriptor compare;
|
---|
| 184 | compare.initialize<VTX>();
|
---|
| 185 | for ( uint32 c = 0; c < m_size; ++c )
|
---|
| 186 | {
|
---|
| 187 | if ( m_channels[c].descriptor() == compare )
|
---|
| 188 | {
|
---|
| 189 | return m_channels[c].data_cast<VTX>( );
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 | return nullptr;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | int get_channel_index( slot s ) const
|
---|
| 196 | {
|
---|
| 197 | for ( uint32 c = 0; c < m_size; ++c )
|
---|
| 198 | if ( m_channels[c].has_slot( s ) )
|
---|
| 199 | return int( c );
|
---|
| 200 | return -1;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[419] | 203 | data_descriptor get_interpolation_key() const
|
---|
| 204 | {
|
---|
| 205 | data_descriptor result;
|
---|
| 206 | for ( uint32 c = 0; c < m_size; ++c )
|
---|
| 207 | {
|
---|
| 208 | for ( const auto& cslot : m_channels[c].descriptor() )
|
---|
| 209 | if ( cslot.vslot != slot::TIME )
|
---|
| 210 | result.push_slot( cslot.etype, cslot.vslot );
|
---|
| 211 | }
|
---|
| 212 | return result;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[418] | 215 | const_iterator begin() const { return &m_channels[0]; }
|
---|
| 216 | const_iterator end() const { return &m_channels[ m_size ]; }
|
---|
[424] | 217 |
|
---|
[418] | 218 | protected:
|
---|
| 219 |
|
---|
| 220 | data_channel_set()
|
---|
| 221 | {
|
---|
[482] | 222 | m_size = 0;
|
---|
[418] | 223 | }
|
---|
[424] | 224 |
|
---|
[418] | 225 | raw_data_channel m_channels[4];
|
---|
[482] | 226 | uint32 m_size;
|
---|
[418] | 227 | };
|
---|
| 228 |
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | #endif // NV_INTERFACE_DATA_CHANNEL_HH
|
---|