Panda3D
|
00001 // Filename: vertexDataBuffer.I 00002 // Created by: drose (14May07) 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: VertexDataBuffer::Constructor 00018 // Access: Public 00019 // Description: 00020 //////////////////////////////////////////////////////////////////// 00021 INLINE VertexDataBuffer:: 00022 VertexDataBuffer() : 00023 _resident_data(NULL), 00024 _size(0), 00025 _reserved_size(0) 00026 { 00027 } 00028 00029 //////////////////////////////////////////////////////////////////// 00030 // Function: VertexDataBuffer::Constructor 00031 // Access: Public 00032 // Description: 00033 //////////////////////////////////////////////////////////////////// 00034 INLINE VertexDataBuffer:: 00035 VertexDataBuffer(size_t size) : 00036 _resident_data(NULL), 00037 _size(0), 00038 _reserved_size(0) 00039 { 00040 do_unclean_realloc(size); 00041 _size = size; 00042 } 00043 00044 //////////////////////////////////////////////////////////////////// 00045 // Function: VertexDataBuffer::Copy Constructor 00046 // Access: Public 00047 // Description: 00048 //////////////////////////////////////////////////////////////////// 00049 INLINE VertexDataBuffer:: 00050 VertexDataBuffer(const VertexDataBuffer ©) : 00051 _resident_data(NULL), 00052 _size(0), 00053 _reserved_size(0) 00054 { 00055 (*this) = copy; 00056 } 00057 00058 //////////////////////////////////////////////////////////////////// 00059 // Function: VertexDataBuffer::Destructor 00060 // Access: Public 00061 // Description: 00062 //////////////////////////////////////////////////////////////////// 00063 INLINE VertexDataBuffer:: 00064 ~VertexDataBuffer() { 00065 clear(); 00066 } 00067 00068 //////////////////////////////////////////////////////////////////// 00069 // Function: VertexDataBuffer::get_read_pointer 00070 // Access: Public 00071 // Description: Returns a read-only pointer to the raw data, or NULL 00072 // if the data is not currently resident. If the data 00073 // is not currently resident, this will implicitly 00074 // request it to become resident soon. 00075 // 00076 // If force is true, this method will never return NULL 00077 // (unless the data is actually empty), but may block 00078 // until the data is available. 00079 //////////////////////////////////////////////////////////////////// 00080 INLINE const unsigned char *VertexDataBuffer:: 00081 get_read_pointer(bool force) const { 00082 LightMutexHolder holder(_lock); 00083 00084 if (_resident_data != (unsigned char *)NULL || _size == 0) { 00085 return _resident_data; 00086 } 00087 00088 nassertr(_block != (VertexDataBlock *)NULL, NULL); 00089 nassertr(_reserved_size >= _size, NULL); 00090 00091 // We don't necessarily need to page the buffer all the way into 00092 // independent status; it's sufficient just to return the block's 00093 // pointer, which will force its page to resident status. 00094 return _block->get_pointer(force); 00095 } 00096 00097 //////////////////////////////////////////////////////////////////// 00098 // Function: VertexDataBuffer::get_write_pointer 00099 // Access: Public 00100 // Description: Returns a writable pointer to the raw data. 00101 //////////////////////////////////////////////////////////////////// 00102 INLINE unsigned char *VertexDataBuffer:: 00103 get_write_pointer() { 00104 LightMutexHolder holder(_lock); 00105 00106 if (_resident_data == (unsigned char *)NULL && _reserved_size != 0) { 00107 do_page_in(); 00108 } 00109 nassertr(_reserved_size >= _size, NULL); 00110 return _resident_data; 00111 } 00112 00113 //////////////////////////////////////////////////////////////////// 00114 // Function: VertexDataBuffer::get_size 00115 // Access: Public 00116 // Description: Returns the number of bytes in the buffer. 00117 //////////////////////////////////////////////////////////////////// 00118 INLINE size_t VertexDataBuffer:: 00119 get_size() const { 00120 return _size; 00121 } 00122 00123 //////////////////////////////////////////////////////////////////// 00124 // Function: VertexDataBuffer::get_reserved_size 00125 // Access: Public 00126 // Description: Returns the total number of bytes "reserved" in the 00127 // buffer. This may be greater than or equal to 00128 // get_size(). If it is greater, the additional bytes 00129 // are extra unused bytes in the buffer, and this 00130 // indicates the maximum value that may be passed to 00131 // set_size() without first calling one of the realloc 00132 // methods. 00133 //////////////////////////////////////////////////////////////////// 00134 INLINE size_t VertexDataBuffer:: 00135 get_reserved_size() const { 00136 return _reserved_size; 00137 } 00138 00139 //////////////////////////////////////////////////////////////////// 00140 // Function: VertexDataBuffer::set_size 00141 // Access: Public 00142 // Description: Changes the size of the buffer. The new size must be 00143 // less than or equal to the "reserved" size, which can 00144 // only be changed via clean_realloc() or 00145 // unclean_realloc(). 00146 //////////////////////////////////////////////////////////////////// 00147 INLINE void VertexDataBuffer:: 00148 set_size(size_t size) { 00149 LightMutexHolder holder(_lock); 00150 nassertv(size <= _reserved_size); 00151 00152 if (size != _size) { 00153 if (_resident_data == (unsigned char *)NULL && _reserved_size != 0) { 00154 do_page_in(); 00155 } 00156 00157 _size = size; 00158 } 00159 } 00160 00161 //////////////////////////////////////////////////////////////////// 00162 // Function: VertexDataBuffer::clean_realloc 00163 // Access: Public 00164 // Description: Changes the "reserved" size of the buffer, preserving 00165 // its data (except for any data beyond the new end of 00166 // the buffer, if the buffer is being reduced). If the 00167 // buffer is expanded, the new data is uninitialized. 00168 // 00169 // It is an error to set the reserved size smaller than 00170 // the size specified with set_size(). 00171 //////////////////////////////////////////////////////////////////// 00172 INLINE void VertexDataBuffer:: 00173 clean_realloc(size_t reserved_size) { 00174 LightMutexHolder holder(_lock); 00175 do_clean_realloc(reserved_size); 00176 } 00177 00178 //////////////////////////////////////////////////////////////////// 00179 // Function: VertexDataBuffer::unclean_realloc 00180 // Access: Public 00181 // Description: Changes the size of the buffer, without regard to 00182 // preserving its data. The buffer may contain random 00183 // data after this call. 00184 // 00185 // It is an error to set the reserved size smaller than 00186 // the size specified with set_size(). 00187 //////////////////////////////////////////////////////////////////// 00188 INLINE void VertexDataBuffer:: 00189 unclean_realloc(size_t reserved_size) { 00190 LightMutexHolder holder(_lock); 00191 do_unclean_realloc(reserved_size); 00192 } 00193 00194 //////////////////////////////////////////////////////////////////// 00195 // Function: VertexDataBuffer::clear 00196 // Access: Public 00197 // Description: Empties the buffer and sets its size to 0. 00198 //////////////////////////////////////////////////////////////////// 00199 INLINE void VertexDataBuffer:: 00200 clear() { 00201 LightMutexHolder holder(_lock); 00202 _size = 0; 00203 do_unclean_realloc(0); 00204 } 00205 00206 //////////////////////////////////////////////////////////////////// 00207 // Function: VertexDataBuffer::page_out 00208 // Access: Public 00209 // Description: Moves the buffer out of independent memory and puts 00210 // it on a page in the indicated book. The buffer may 00211 // still be directly accessible as long as its page 00212 // remains resident. Any subsequent attempt to rewrite 00213 // the buffer will implicitly move it off of the page 00214 // and back into independent memory. 00215 //////////////////////////////////////////////////////////////////// 00216 INLINE void VertexDataBuffer:: 00217 page_out(VertexDataBook &book) { 00218 LightMutexHolder holder(_lock); 00219 do_page_out(book); 00220 }