Panda3D
|
00001 // Filename: geomVertexArrayData.I 00002 // Created by: drose (17Mar05) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 00016 //////////////////////////////////////////////////////////////////// 00017 // Function: GeomVertexArrayData::get_array_format 00018 // Access: Published 00019 // Description: Returns the format object that describes this array. 00020 //////////////////////////////////////////////////////////////////// 00021 INLINE const GeomVertexArrayFormat *GeomVertexArrayData:: 00022 get_array_format() const { 00023 return _array_format; 00024 } 00025 00026 //////////////////////////////////////////////////////////////////// 00027 // Function: GeomVertexArrayData::get_usage_hint 00028 // Access: Published 00029 // Description: Returns the usage hint that describes to the 00030 // rendering backend how often the vertex data will be 00031 // modified and/or rendered. See geomEnums.h. 00032 //////////////////////////////////////////////////////////////////// 00033 INLINE GeomVertexArrayData::UsageHint GeomVertexArrayData:: 00034 get_usage_hint() const { 00035 CDReader cdata(_cycler); 00036 return cdata->_usage_hint; 00037 } 00038 00039 //////////////////////////////////////////////////////////////////// 00040 // Function: GeomVertexArrayData::has_column 00041 // Access: Published 00042 // Description: Returns true if the array has the named column, 00043 // false otherwise. This is really just a shortcut for 00044 // asking the same thing from the format. 00045 //////////////////////////////////////////////////////////////////// 00046 INLINE bool GeomVertexArrayData:: 00047 has_column(const InternalName *name) const { 00048 return _array_format->has_column(name); 00049 } 00050 00051 //////////////////////////////////////////////////////////////////// 00052 // Function: GeomVertexArrayData::get_num_rows 00053 // Access: Published 00054 // Description: Returns the number of rows stored in the array, 00055 // based on the number of bytes and the stride. This 00056 // should be the same for all arrays within a given 00057 // GeomVertexData object. 00058 //////////////////////////////////////////////////////////////////// 00059 INLINE int GeomVertexArrayData:: 00060 get_num_rows() const { 00061 return get_handle()->get_num_rows(); 00062 } 00063 00064 //////////////////////////////////////////////////////////////////// 00065 // Function: GeomVertexArrayData::set_num_rows 00066 // Access: Published 00067 // Description: Sets the length of the array to n rows. 00068 // 00069 // Normally, you would not call this directly, since all 00070 // of the arrays in a particular GeomVertexData must 00071 // have the same number of rows; instead, call 00072 // GeomVertexData::set_num_rows(). 00073 // 00074 // The return value is true if the number of rows 00075 // was changed, false if the object already contained n 00076 // rows (or if there was some error). 00077 // 00078 // The new vertex data is initialized to 0, including 00079 // the "color" column (but see 00080 // GeomVertexData::set_num_rows()). 00081 // 00082 // Don't call this in a downstream thread unless you 00083 // don't mind it blowing away other changes you might 00084 // have recently made in an upstream thread. 00085 //////////////////////////////////////////////////////////////////// 00086 INLINE bool GeomVertexArrayData:: 00087 set_num_rows(int n) { 00088 return modify_handle()->set_num_rows(n); 00089 } 00090 00091 //////////////////////////////////////////////////////////////////// 00092 // Function: GeomVertexArrayData::unclean_set_num_rows 00093 // Access: Published 00094 // Description: This method behaves like set_num_rows(), except the 00095 // new data is not initialized. Furthermore, after this 00096 // call, *any* of the data in the GeomVertexArrayData 00097 // may be uninitialized, including the earlier rows. 00098 // 00099 // Normally, you would not call this directly, since all 00100 // of the arrays in a particular GeomVertexData must 00101 // have the same number of rows; instead, call 00102 // GeomVertexData::unclean_set_num_rows(). 00103 //////////////////////////////////////////////////////////////////// 00104 INLINE bool GeomVertexArrayData:: 00105 unclean_set_num_rows(int n) { 00106 return modify_handle()->unclean_set_num_rows(n); 00107 } 00108 00109 //////////////////////////////////////////////////////////////////// 00110 // Function: GeomVertexArrayData::clear_rows 00111 // Access: Published 00112 // Description: Removes all of the rows in the array. 00113 // Functionally equivalent to set_num_rows(0). 00114 //////////////////////////////////////////////////////////////////// 00115 INLINE void GeomVertexArrayData:: 00116 clear_rows() { 00117 return modify_handle()->clear_rows(); 00118 } 00119 00120 //////////////////////////////////////////////////////////////////// 00121 // Function: GeomVertexArrayData::get_data_size_bytes 00122 // Access: Published 00123 // Description: Returns the number of bytes stored in the array. 00124 //////////////////////////////////////////////////////////////////// 00125 INLINE int GeomVertexArrayData:: 00126 get_data_size_bytes() const { 00127 CDReader cdata(_cycler); 00128 return cdata->_buffer.get_size(); 00129 } 00130 00131 //////////////////////////////////////////////////////////////////// 00132 // Function: GeomVertexArrayData::get_modified 00133 // Access: Published 00134 // Description: Returns a sequence number which is guaranteed to 00135 // change at least every time the array vertex data is 00136 // modified. 00137 //////////////////////////////////////////////////////////////////// 00138 INLINE UpdateSeq GeomVertexArrayData:: 00139 get_modified() const { 00140 CDReader cdata(_cycler); 00141 return cdata->_modified; 00142 } 00143 00144 //////////////////////////////////////////////////////////////////// 00145 // Function: GeomVertexArrayData::request_resident 00146 // Access: Published 00147 // Description: Returns true if the vertex data is currently resident 00148 // in memory. If this returns true, the next call to 00149 // get_handle()->get_read_pointer() will probably not 00150 // block. If this returns false, the vertex data will 00151 // be brought back into memory shortly; try again later. 00152 //////////////////////////////////////////////////////////////////// 00153 INLINE bool GeomVertexArrayData:: 00154 request_resident() const { 00155 CPT(GeomVertexArrayDataHandle) handle = get_handle(); 00156 return handle->request_resident(); 00157 } 00158 00159 //////////////////////////////////////////////////////////////////// 00160 // Function: GeomVertexArrayData::get_handle 00161 // Access: Published 00162 // Description: Returns an object that can be used to read the actual 00163 // data bytes stored in the array. Calling this method 00164 // locks the data, and will block any other threads 00165 // attempting to read or write the data, until the 00166 // returned object destructs. 00167 //////////////////////////////////////////////////////////////////// 00168 INLINE CPT(GeomVertexArrayDataHandle) GeomVertexArrayData:: 00169 get_handle(Thread *current_thread) const { 00170 const CData *cdata = _cycler.read_unlocked(current_thread); 00171 return new GeomVertexArrayDataHandle(this, current_thread, 00172 cdata, false); 00173 } 00174 00175 //////////////////////////////////////////////////////////////////// 00176 // Function: GeomVertexArrayData::modify_handle 00177 // Access: Published 00178 // Description: Returns an object that can be used to read or write 00179 // the actual data bytes stored in the array. Calling 00180 // this method locks the data, and will block any other 00181 // threads attempting to read or write the data, until 00182 // the returned object destructs. 00183 //////////////////////////////////////////////////////////////////// 00184 INLINE PT(GeomVertexArrayDataHandle) GeomVertexArrayData:: 00185 modify_handle(Thread *current_thread) { 00186 CData *cdata = _cycler.write_upstream(true, current_thread); 00187 return new GeomVertexArrayDataHandle(this, current_thread, 00188 cdata, true); 00189 } 00190 00191 //////////////////////////////////////////////////////////////////// 00192 // Function: GeomVertexArrayData::get_independent_lru 00193 // Access: Published, Static 00194 // Description: Returns a pointer to the global LRU object that 00195 // manages the GeomVertexArrayData's that have not (yet) 00196 // been paged out. 00197 //////////////////////////////////////////////////////////////////// 00198 INLINE SimpleLru *GeomVertexArrayData:: 00199 get_independent_lru() { 00200 return &_independent_lru; 00201 } 00202 00203 //////////////////////////////////////////////////////////////////// 00204 // Function: GeomVertexArrayData::get_small_lru 00205 // Access: Published, Static 00206 // Description: Returns a pointer to the global LRU object that 00207 // manages the GeomVertexArrayData's that are deemed too 00208 // small to be paged out. 00209 //////////////////////////////////////////////////////////////////// 00210 INLINE SimpleLru *GeomVertexArrayData:: 00211 get_small_lru() { 00212 return &_small_lru; 00213 } 00214 00215 //////////////////////////////////////////////////////////////////// 00216 // Function: GeomVertexArrayData::get_book 00217 // Access: Published, Static 00218 // Description: Returns the global VertexDataBook that will be 00219 // used to allocate vertex data buffers. 00220 //////////////////////////////////////////////////////////////////// 00221 INLINE VertexDataBook &GeomVertexArrayData:: 00222 get_book() { 00223 return _book; 00224 } 00225 00226 //////////////////////////////////////////////////////////////////// 00227 // Function: GeomVertexArrayData::set_lru_size 00228 // Access: Private 00229 // Description: Should be called when the size of the buffer changes. 00230 //////////////////////////////////////////////////////////////////// 00231 INLINE void GeomVertexArrayData:: 00232 set_lru_size(size_t lru_size) { 00233 SimpleLruPage::set_lru_size(lru_size); 00234 00235 if ((int)lru_size <= vertex_data_small_size) { 00236 SimpleLruPage::mark_used_lru(&_small_lru); 00237 } else { 00238 SimpleLruPage::mark_used_lru(&_independent_lru); 00239 } 00240 } 00241 00242 //////////////////////////////////////////////////////////////////// 00243 // Function: GeomVertexArrayData::CData::Constructor 00244 // Access: Public 00245 // Description: 00246 //////////////////////////////////////////////////////////////////// 00247 INLINE GeomVertexArrayData::CData:: 00248 CData() : 00249 _usage_hint(UH_unspecified), 00250 _rw_lock("GeomVertexArrayData::CData::_rw_lock") 00251 { 00252 } 00253 00254 //////////////////////////////////////////////////////////////////// 00255 // Function: GeomVertexArrayData::CData::Copy Constructor 00256 // Access: Public 00257 // Description: 00258 //////////////////////////////////////////////////////////////////// 00259 INLINE GeomVertexArrayData::CData:: 00260 CData(const GeomVertexArrayData::CData ©) : 00261 _usage_hint(copy._usage_hint), 00262 _buffer(copy._buffer), 00263 _modified(copy._modified), 00264 _rw_lock("GeomVertexArrayData::CData::_rw_lock") 00265 { 00266 } 00267 00268 //////////////////////////////////////////////////////////////////// 00269 // Function: GeomVertexArrayData::CData::Copy Assignment 00270 // Access: Public 00271 // Description: 00272 //////////////////////////////////////////////////////////////////// 00273 INLINE void GeomVertexArrayData::CData:: 00274 operator = (const GeomVertexArrayData::CData ©) { 00275 _usage_hint = copy._usage_hint; 00276 _buffer = copy._buffer; 00277 _modified = copy._modified; 00278 } 00279 00280 //////////////////////////////////////////////////////////////////// 00281 // Function: GeomVertexArrayDataHandle::Constructor 00282 // Access: Private 00283 // Description: 00284 //////////////////////////////////////////////////////////////////// 00285 INLINE GeomVertexArrayDataHandle:: 00286 GeomVertexArrayDataHandle(const GeomVertexArrayData *object, 00287 Thread *current_thread, 00288 const GeomVertexArrayData::CData *cdata, 00289 bool writable) : 00290 _object((GeomVertexArrayData *)object), 00291 _current_thread(current_thread), 00292 _cdata((GeomVertexArrayData::CData *)cdata), 00293 _writable(writable) 00294 { 00295 #ifdef _DEBUG 00296 nassertv(_object->test_ref_count_nonzero()); 00297 #endif // _DEBUG 00298 #ifdef DO_PIPELINING 00299 _cdata->ref(); 00300 #endif // DO_PIPELINING 00301 // We must grab the lock *after* we have incremented the reference 00302 // count, above. 00303 _cdata->_rw_lock.acquire(); 00304 #ifdef DO_MEMORY_USAGE 00305 MemoryUsage::update_type(this, get_class_type()); 00306 #endif 00307 } 00308 00309 //////////////////////////////////////////////////////////////////// 00310 // Function: GeomVertexArrayDataHandle::Copy Constructor 00311 // Access: Private 00312 // Description: Don't attempt to copy these objects. 00313 //////////////////////////////////////////////////////////////////// 00314 INLINE GeomVertexArrayDataHandle:: 00315 GeomVertexArrayDataHandle(const GeomVertexArrayDataHandle ©) { 00316 nassertv(false); 00317 } 00318 00319 //////////////////////////////////////////////////////////////////// 00320 // Function: GeomVertexArrayDataHandle::Copy Assignment Operator 00321 // Access: Private 00322 // Description: Don't attempt to copy these objects. 00323 //////////////////////////////////////////////////////////////////// 00324 INLINE void GeomVertexArrayDataHandle:: 00325 operator = (const GeomVertexArrayDataHandle &) { 00326 nassertv(false); 00327 } 00328 00329 //////////////////////////////////////////////////////////////////// 00330 // Function: GeomVertexArrayDataHandle::Destructor 00331 // Access: Public 00332 // Description: 00333 //////////////////////////////////////////////////////////////////// 00334 INLINE GeomVertexArrayDataHandle:: 00335 ~GeomVertexArrayDataHandle() { 00336 #ifdef _DEBUG 00337 nassertv(_object->test_ref_count_nonzero()); 00338 #endif // _DEBUG 00339 00340 if (_writable) { 00341 _object->_cycler.release_write(_cdata); 00342 } 00343 00344 // We must release the lock *before* we decrement the reference 00345 // count, below. 00346 _cdata->_rw_lock.release(); 00347 00348 #ifdef DO_PIPELINING 00349 unref_delete((CycleData *)_cdata); 00350 #endif // DO_PIPELINING 00351 00352 #ifdef _DEBUG 00353 _object = NULL; 00354 _cdata = NULL; 00355 #endif // _DEBUG 00356 } 00357 00358 //////////////////////////////////////////////////////////////////// 00359 // Function: GeomVertexArrayDataHandle::get_current_thread 00360 // Access: Public 00361 // Description: 00362 //////////////////////////////////////////////////////////////////// 00363 INLINE Thread *GeomVertexArrayDataHandle:: 00364 get_current_thread() const { 00365 return _current_thread; 00366 } 00367 00368 //////////////////////////////////////////////////////////////////// 00369 // Function: GeomVertexArrayDataHandle::get_read_pointer 00370 // Access: Public 00371 // Description: Returns a readable pointer to the beginning of the 00372 // actual data stream, or NULL if the data is not 00373 // currently resident. If the data is not currently 00374 // resident, this will implicitly request it to become 00375 // resident soon. 00376 // 00377 // If force is true, this method will never return NULL, 00378 // but may block until the data is available. 00379 //////////////////////////////////////////////////////////////////// 00380 INLINE const unsigned char *GeomVertexArrayDataHandle:: 00381 get_read_pointer(bool force) const { 00382 mark_used(); 00383 return _cdata->_buffer.get_read_pointer(force); 00384 } 00385 00386 //////////////////////////////////////////////////////////////////// 00387 // Function: GeomVertexArrayDataHandle::get_object 00388 // Access: Published 00389 // Description: 00390 //////////////////////////////////////////////////////////////////// 00391 INLINE const GeomVertexArrayData *GeomVertexArrayDataHandle:: 00392 get_object() const { 00393 return _object; 00394 } 00395 00396 //////////////////////////////////////////////////////////////////// 00397 // Function: GeomVertexArrayDataHandle::get_object 00398 // Access: Published 00399 // Description: 00400 //////////////////////////////////////////////////////////////////// 00401 INLINE GeomVertexArrayData *GeomVertexArrayDataHandle:: 00402 get_object() { 00403 return _object; 00404 } 00405 00406 //////////////////////////////////////////////////////////////////// 00407 // Function: GeomVertexArrayDataHandle::get_array_format 00408 // Access: Published 00409 // Description: 00410 //////////////////////////////////////////////////////////////////// 00411 INLINE const GeomVertexArrayFormat *GeomVertexArrayDataHandle:: 00412 get_array_format() const { 00413 return _object->_array_format; 00414 } 00415 00416 //////////////////////////////////////////////////////////////////// 00417 // Function: GeomVertexArrayDataHandle::get_usage_hint 00418 // Access: Published 00419 // Description: 00420 //////////////////////////////////////////////////////////////////// 00421 INLINE GeomVertexArrayDataHandle::UsageHint GeomVertexArrayDataHandle:: 00422 get_usage_hint() const { 00423 return _cdata->_usage_hint; 00424 } 00425 00426 //////////////////////////////////////////////////////////////////// 00427 // Function: GeomVertexArrayDataHandle::get_num_rows 00428 // Access: Published 00429 // Description: 00430 //////////////////////////////////////////////////////////////////// 00431 INLINE int GeomVertexArrayDataHandle:: 00432 get_num_rows() const { 00433 return get_data_size_bytes() / _object->_array_format->get_stride(); 00434 } 00435 00436 //////////////////////////////////////////////////////////////////// 00437 // Function: GeomVertexArrayDataHandle::clear_rows 00438 // Access: Published 00439 // Description: 00440 //////////////////////////////////////////////////////////////////// 00441 INLINE void GeomVertexArrayDataHandle:: 00442 clear_rows() { 00443 set_num_rows(0); 00444 } 00445 00446 //////////////////////////////////////////////////////////////////// 00447 // Function: GeomVertexArrayDataHandle::get_data_size_bytes 00448 // Access: Published 00449 // Description: 00450 //////////////////////////////////////////////////////////////////// 00451 INLINE int GeomVertexArrayDataHandle:: 00452 get_data_size_bytes() const { 00453 return _cdata->_buffer.get_size(); 00454 } 00455 00456 //////////////////////////////////////////////////////////////////// 00457 // Function: GeomVertexArrayDataHandle::get_modified 00458 // Access: Published 00459 // Description: 00460 //////////////////////////////////////////////////////////////////// 00461 INLINE UpdateSeq GeomVertexArrayDataHandle:: 00462 get_modified() const { 00463 return _cdata->_modified; 00464 } 00465 00466 //////////////////////////////////////////////////////////////////// 00467 // Function: GeomVertexArrayData::request_resident 00468 // Access: Published 00469 // Description: Returns true if the vertex data is currently resident 00470 // in memory. If this returns true, the next call to 00471 // get_handle()->get_read_pointer() will probably not 00472 // block. If this returns false, the vertex data will 00473 // be brought back into memory shortly; try again later. 00474 //////////////////////////////////////////////////////////////////// 00475 INLINE bool GeomVertexArrayDataHandle:: 00476 request_resident() const { 00477 return (get_read_pointer(false) != (const unsigned char *)NULL); 00478 } 00479 00480 //////////////////////////////////////////////////////////////////// 00481 // Function: GeomVertexArrayDataHandle::get_data 00482 // Access: Published 00483 // Description: Returns the entire raw data of the 00484 // GeomVertexArrayData object, formatted as a string. 00485 // This is primarily for the benefit of high-level 00486 // languages such as Python. 00487 //////////////////////////////////////////////////////////////////// 00488 INLINE string GeomVertexArrayDataHandle:: 00489 get_data() const { 00490 mark_used(); 00491 return string((const char *)_cdata->_buffer.get_read_pointer(true), _cdata->_buffer.get_size()); 00492 } 00493 00494 //////////////////////////////////////////////////////////////////// 00495 // Function: GeomVertexArrayDataHandle::get_subdata 00496 // Access: Published 00497 // Description: Returns a subset of the raw data of the 00498 // GeomVertexArrayData object, formatted as a string. 00499 // This is primarily for the benefit of high-level 00500 // languages such as Python. 00501 //////////////////////////////////////////////////////////////////// 00502 INLINE string GeomVertexArrayDataHandle:: 00503 get_subdata(size_t start, size_t size) const { 00504 mark_used(); 00505 start = min(start, _cdata->_buffer.get_size()); 00506 size = min(size, _cdata->_buffer.get_size() - start); 00507 return string((const char *)_cdata->_buffer.get_read_pointer(true) + start, size); 00508 } 00509 00510 //////////////////////////////////////////////////////////////////// 00511 // Function: GeomVertexArrayDataHandle::mark_used 00512 // Access: Published 00513 // Description: Marks the array data recently-used. 00514 //////////////////////////////////////////////////////////////////// 00515 void GeomVertexArrayDataHandle:: 00516 mark_used() const { 00517 _object->set_lru_size(_object->get_lru_size()); 00518 } 00519 00520 INLINE ostream & 00521 operator << (ostream &out, const GeomVertexArrayData &obj) { 00522 obj.output(out); 00523 return out; 00524 }