Panda3D
 All Classes Functions Variables Enumerations
vertexDataBuffer.I
1 // Filename: vertexDataBuffer.I
2 // Created by: drose (14May07)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: VertexDataBuffer::Constructor
18 // Access: Public
19 // Description:
20 ////////////////////////////////////////////////////////////////////
21 INLINE VertexDataBuffer::
22 VertexDataBuffer() :
23  _resident_data(NULL),
24  _size(0),
25  _reserved_size(0)
26 {
27 }
28 
29 ////////////////////////////////////////////////////////////////////
30 // Function: VertexDataBuffer::Constructor
31 // Access: Public
32 // Description:
33 ////////////////////////////////////////////////////////////////////
34 INLINE VertexDataBuffer::
35 VertexDataBuffer(size_t size) :
36  _resident_data(NULL),
37  _size(0),
38  _reserved_size(0)
39 {
40  do_unclean_realloc(size);
41  _size = size;
42 }
43 
44 ////////////////////////////////////////////////////////////////////
45 // Function: VertexDataBuffer::Copy Constructor
46 // Access: Public
47 // Description:
48 ////////////////////////////////////////////////////////////////////
49 INLINE VertexDataBuffer::
50 VertexDataBuffer(const VertexDataBuffer &copy) :
51  _resident_data(NULL),
52  _size(0),
53  _reserved_size(0)
54 {
55  (*this) = copy;
56 }
57 
58 ////////////////////////////////////////////////////////////////////
59 // Function: VertexDataBuffer::Destructor
60 // Access: Public
61 // Description:
62 ////////////////////////////////////////////////////////////////////
63 INLINE VertexDataBuffer::
64 ~VertexDataBuffer() {
65  clear();
66 }
67 
68 ////////////////////////////////////////////////////////////////////
69 // Function: VertexDataBuffer::get_read_pointer
70 // Access: Public
71 // Description: Returns a read-only pointer to the raw data, or NULL
72 // if the data is not currently resident. If the data
73 // is not currently resident, this will implicitly
74 // request it to become resident soon.
75 //
76 // If force is true, this method will never return NULL
77 // (unless the data is actually empty), but may block
78 // until the data is available.
79 ////////////////////////////////////////////////////////////////////
80 INLINE const unsigned char *VertexDataBuffer::
81 get_read_pointer(bool force) const {
82  LightMutexHolder holder(_lock);
83 
84  if (_resident_data != (unsigned char *)NULL || _size == 0) {
85  return _resident_data;
86  }
87 
88  nassertr(_block != (VertexDataBlock *)NULL, NULL);
89  nassertr(_reserved_size >= _size, NULL);
90 
91  // We don't necessarily need to page the buffer all the way into
92  // independent status; it's sufficient just to return the block's
93  // pointer, which will force its page to resident status.
94  return _block->get_pointer(force);
95 }
96 
97 ////////////////////////////////////////////////////////////////////
98 // Function: VertexDataBuffer::get_write_pointer
99 // Access: Public
100 // Description: Returns a writable pointer to the raw data.
101 ////////////////////////////////////////////////////////////////////
102 INLINE unsigned char *VertexDataBuffer::
104  LightMutexHolder holder(_lock);
105 
106  if (_resident_data == (unsigned char *)NULL && _reserved_size != 0) {
107  do_page_in();
108  }
109  nassertr(_reserved_size >= _size, NULL);
110  return _resident_data;
111 }
112 
113 ////////////////////////////////////////////////////////////////////
114 // Function: VertexDataBuffer::get_size
115 // Access: Public
116 // Description: Returns the number of bytes in the buffer.
117 ////////////////////////////////////////////////////////////////////
118 INLINE size_t VertexDataBuffer::
119 get_size() const {
120  return _size;
121 }
122 
123 ////////////////////////////////////////////////////////////////////
124 // Function: VertexDataBuffer::get_reserved_size
125 // Access: Public
126 // Description: Returns the total number of bytes "reserved" in the
127 // buffer. This may be greater than or equal to
128 // get_size(). If it is greater, the additional bytes
129 // are extra unused bytes in the buffer, and this
130 // indicates the maximum value that may be passed to
131 // set_size() without first calling one of the realloc
132 // methods.
133 ////////////////////////////////////////////////////////////////////
134 INLINE size_t VertexDataBuffer::
136  return _reserved_size;
137 }
138 
139 ////////////////////////////////////////////////////////////////////
140 // Function: VertexDataBuffer::set_size
141 // Access: Public
142 // Description: Changes the size of the buffer. The new size must be
143 // less than or equal to the "reserved" size, which can
144 // only be changed via clean_realloc() or
145 // unclean_realloc().
146 ////////////////////////////////////////////////////////////////////
147 INLINE void VertexDataBuffer::
148 set_size(size_t size) {
149  LightMutexHolder holder(_lock);
150  nassertv(size <= _reserved_size);
151 
152  if (size != _size) {
153  if (_resident_data == (unsigned char *)NULL && _reserved_size != 0) {
154  do_page_in();
155  }
156 
157  _size = size;
158  }
159 }
160 
161 ////////////////////////////////////////////////////////////////////
162 // Function: VertexDataBuffer::clean_realloc
163 // Access: Public
164 // Description: Changes the "reserved" size of the buffer, preserving
165 // its data (except for any data beyond the new end of
166 // the buffer, if the buffer is being reduced). If the
167 // buffer is expanded, the new data is uninitialized.
168 //
169 // It is an error to set the reserved size smaller than
170 // the size specified with set_size().
171 ////////////////////////////////////////////////////////////////////
172 INLINE void VertexDataBuffer::
173 clean_realloc(size_t reserved_size) {
174  LightMutexHolder holder(_lock);
175  do_clean_realloc(reserved_size);
176 }
177 
178 ////////////////////////////////////////////////////////////////////
179 // Function: VertexDataBuffer::unclean_realloc
180 // Access: Public
181 // Description: Changes the size of the buffer, without regard to
182 // preserving its data. The buffer may contain random
183 // data after this call.
184 //
185 // It is an error to set the reserved size smaller than
186 // the size specified with set_size().
187 ////////////////////////////////////////////////////////////////////
188 INLINE void VertexDataBuffer::
189 unclean_realloc(size_t reserved_size) {
190  LightMutexHolder holder(_lock);
191  do_unclean_realloc(reserved_size);
192 }
193 
194 ////////////////////////////////////////////////////////////////////
195 // Function: VertexDataBuffer::clear
196 // Access: Public
197 // Description: Empties the buffer and sets its size to 0.
198 ////////////////////////////////////////////////////////////////////
199 INLINE void VertexDataBuffer::
200 clear() {
201  LightMutexHolder holder(_lock);
202  _size = 0;
203  do_unclean_realloc(0);
204 }
205 
206 ////////////////////////////////////////////////////////////////////
207 // Function: VertexDataBuffer::page_out
208 // Access: Public
209 // Description: Moves the buffer out of independent memory and puts
210 // it on a page in the indicated book. The buffer may
211 // still be directly accessible as long as its page
212 // remains resident. Any subsequent attempt to rewrite
213 // the buffer will implicitly move it off of the page
214 // and back into independent memory.
215 ////////////////////////////////////////////////////////////////////
216 INLINE void VertexDataBuffer::
218  LightMutexHolder holder(_lock);
219  do_page_out(book);
220 }
unsigned char * get_write_pointer()
Returns a writable pointer to the raw data.
void clear()
Empties the buffer and sets its size to 0.
size_t get_reserved_size() const
Returns the total number of bytes &quot;reserved&quot; in the buffer.
A block of bytes that stores the actual raw vertex data referenced by a GeomVertexArrayData object...
void clean_realloc(size_t reserved_size)
Changes the &quot;reserved&quot; size of the buffer, preserving its data (except for any data beyond the new en...
Similar to MutexHolder, but for a light mutex.
A block of bytes that stores the actual raw vertex data referenced by a GeomVertexArrayData object...
void set_size(size_t size)
Changes the size of the buffer.
A collection of VertexDataPages, which can be used to allocate new VertexDataBlock objects...
void unclean_realloc(size_t reserved_size)
Changes the size of the buffer, without regard to preserving its data.
size_t get_size() const
Returns the number of bytes in the buffer.
const unsigned char * get_read_pointer(bool force) const
Returns a read-only pointer to the raw data, or NULL if the data is not currently resident...
void page_out(VertexDataBook &book)
Moves the buffer out of independent memory and puts it on a page in the indicated book...