Panda3D
 All Classes Functions Variables Enumerations
geomVertexArrayData.h
1 // Filename: geomVertexArrayData.h
2 // Created by: drose (17Mar05)
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 #ifndef GEOMVERTEXARRAYDATA_H
16 #define GEOMVERTEXARRAYDATA_H
17 
18 #include "pandabase.h"
19 #include "copyOnWriteObject.h"
20 #include "geomVertexArrayFormat.h"
21 #include "geomEnums.h"
22 #include "pta_uchar.h"
23 #include "updateSeq.h"
24 #include "cycleData.h"
25 #include "cycleDataReader.h"
26 #include "cycleDataWriter.h"
27 #include "cycleDataStageReader.h"
28 #include "cycleDataStageWriter.h"
29 #include "pipelineCycler.h"
30 #include "pmap.h"
31 #include "reMutex.h"
32 #include "simpleLru.h"
33 #include "vertexDataBuffer.h"
34 #include "config_gobj.h"
35 #include "bamReader.h"
36 
41 class VertexDataBook;
43 
44 ////////////////////////////////////////////////////////////////////
45 // Class : GeomVertexArrayData
46 // Description : This is the data for one array of a GeomVertexData
47 // structure. Many GeomVertexData structures will only
48 // define one array, with all data elements interleaved
49 // (DirectX 8.0 and before insisted on this format);
50 // some will define multiple arrays.
51 //
52 // DirectX calls this concept of one array a "stream".
53 // It also closely correlates with the concept of a
54 // vertex buffer.
55 //
56 // This object is just a block of data. In general, you
57 // should not be directly messing with this object from
58 // application code. See GeomVertexData for the
59 // organizing structure, and see
60 // GeomVertexReader/Writer/Rewriter for high-level tools
61 // to manipulate the actual vertex data.
62 ////////////////////////////////////////////////////////////////////
63 class EXPCL_PANDA_GOBJ GeomVertexArrayData : public CopyOnWriteObject, public SimpleLruPage, public GeomEnums {
64 private:
66 
67 protected:
68  virtual PT(CopyOnWriteObject) make_cow_copy();
69 
70 PUBLISHED:
71  GeomVertexArrayData(const GeomVertexArrayFormat *array_format,
72  UsageHint usage_hint);
74  void operator = (const GeomVertexArrayData &copy);
75  virtual ~GeomVertexArrayData();
76  ALLOC_DELETED_CHAIN(GeomVertexArrayData);
77 
78  int compare_to(const GeomVertexArrayData &other) const;
79 
80  INLINE const GeomVertexArrayFormat *get_array_format() const;
81 
82  INLINE UsageHint get_usage_hint() const;
83  void set_usage_hint(UsageHint usage_hint);
84 
85  INLINE bool has_column(const InternalName *name) const;
86 
87  INLINE int get_num_rows() const;
88  INLINE bool set_num_rows(int n);
89  INLINE bool unclean_set_num_rows(int n);
90  INLINE bool reserve_num_rows(int n);
91  INLINE void clear_rows();
92 
93  INLINE int get_data_size_bytes() const;
94  INLINE UpdateSeq get_modified() const;
95 
96  void output(ostream &out) const;
97  void write(ostream &out, int indent_level = 0) const;
98 
99  INLINE bool request_resident() const;
100 
101  INLINE CPT(GeomVertexArrayDataHandle) get_handle(Thread *current_thread = Thread::get_current_thread()) const;
102  INLINE PT(GeomVertexArrayDataHandle) modify_handle(Thread *current_thread = Thread::get_current_thread());
103 
104  void prepare(PreparedGraphicsObjects *prepared_objects);
105  bool is_prepared(PreparedGraphicsObjects *prepared_objects) const;
106 
107  VertexBufferContext *prepare_now(PreparedGraphicsObjects *prepared_objects,
109  bool release(PreparedGraphicsObjects *prepared_objects);
110  int release_all();
111 
112  INLINE static SimpleLru *get_independent_lru();
113  INLINE static SimpleLru *get_small_lru();
114  static void lru_epoch();
115  INLINE static VertexDataBook &get_book();
116 
117 #if PY_VERSION_HEX >= 0x02060000
118  EXTENSION(int __getbuffer__(PyObject *self, Py_buffer *view, int flags));
119  EXTENSION(int __getbuffer__(PyObject *self, Py_buffer *view, int flags) const);
120  EXTENSION(void __releasebuffer__(PyObject *self, Py_buffer *view) const);
121 #endif
122 
123 public:
124  virtual void evict_lru();
125 
126 private:
127  INLINE void set_lru_size(size_t lru_size);
128 
129  void clear_prepared(PreparedGraphicsObjects *prepared_objects);
130  void reverse_data_endianness(unsigned char *dest,
131  const unsigned char *source, size_t size);
132 
133 
134  CPT(GeomVertexArrayFormat) _array_format;
135 
136  // A GeomVertexArrayData keeps a list (actually, a map) of all the
137  // PreparedGraphicsObjects tables that it has been prepared into.
138  // Each PGO conversely keeps a list (a set) of all the Geoms that
139  // have been prepared there. When either destructs, it removes
140  // itself from the other's list.
142  Contexts *_contexts;
143 
144  // This data is only needed when reading from a bam file.
145  class BamAuxData : public BamReader::AuxData {
146  public:
147  // set true to indicate the data must be endian-reversed in
148  // finalize().
149  bool _endian_reversed;
150  };
151 
152  // This is the data that must be cycled between pipeline stages.
153  class EXPCL_PANDA_GOBJ CData : public CycleData {
154  public:
155  INLINE CData();
156  INLINE CData(const CData &copy);
157  INLINE void operator = (const CData &copy);
158 
159  virtual ~CData();
160  ALLOC_DELETED_CHAIN(CData);
161  virtual CycleData *make_copy() const;
162  virtual void write_datagram(BamWriter *manager, Datagram &dg,
163  void *extra_data) const;
164  virtual void fillin(DatagramIterator &scan, BamReader *manager,
165  void *extra_data);
166  virtual TypeHandle get_parent_type() const {
167  return GeomVertexArrayData::get_class_type();
168  }
169 
170  UsageHint _usage_hint;
171  VertexDataBuffer _buffer;
172  UpdateSeq _modified;
173 
174  // This implements read-write locking. Anyone who gets the data for
175  // reading or writing will hold this mutex during the lock.
176  ReMutex _rw_lock;
177 
178  public:
179  static TypeHandle get_class_type() {
180  return _type_handle;
181  }
182  static void init_type() {
183  register_type(_type_handle, "GeomVertexArrayData::CData");
184  }
185 
186  private:
187  static TypeHandle _type_handle;
188 
189  friend class GeomVertexArrayData;
190  };
191 
192  PipelineCycler<CData> _cycler;
197 
198  static SimpleLru _independent_lru;
199  static SimpleLru _small_lru;
200  static VertexDataBook _book;
201 
202 public:
203  static void register_with_read_factory();
204  virtual void write_datagram(BamWriter *manager, Datagram &dg);
205  PTA_uchar read_raw_data(BamReader *manager, DatagramIterator &source);
206  virtual int complete_pointers(TypedWritable **plist, BamReader *manager);
207 
208  virtual void finalize(BamReader *manager);
209 
210 protected:
211  static TypedWritable *make_from_bam(const FactoryParams &params);
212  void fillin(DatagramIterator &scan, BamReader *manager);
213 
214 public:
215  static TypeHandle get_class_type() {
216  return _type_handle;
217  }
218  static void init_type() {
219  CopyOnWriteObject::init_type();
220  register_type(_type_handle, "GeomVertexArrayData",
221  CopyOnWriteObject::get_class_type());
222  CData::init_type();
223  }
224  virtual TypeHandle get_type() const {
225  return get_class_type();
226  }
227  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
228 
229 private:
230  static TypeHandle _type_handle;
231 
232  friend class GeomCacheManager;
233  friend class GeomVertexData;
234  friend class PreparedGraphicsObjects;
235  friend class GeomVertexArrayDataHandle;
236 };
237 
238 ////////////////////////////////////////////////////////////////////
239 // Class : GeomVertexArrayDataHandle
240 // Description : This data object is returned by
241 // GeomVertexArrayData::get_handle() or modify_handle().
242 // As long as it exists, the data is locked; when the
243 // last of these destructs, the data is unlocked.
244 //
245 // Only one thread at a time may lock the data; other
246 // threads attempting to lock the data will block. A
247 // given thread may simultaneously lock the data
248 // multiple times.
249 //
250 // This class serves in lieu of a pair of
251 // GeomVertexArrayDataPipelineReader and
252 // GeomVertexArrayDataPipelineWriter classes
253 ////////////////////////////////////////////////////////////////////
254 class EXPCL_PANDA_GOBJ GeomVertexArrayDataHandle : public ReferenceCount, public GeomEnums {
255 private:
256  INLINE GeomVertexArrayDataHandle(const GeomVertexArrayData *object,
257  Thread *current_thread,
258  const GeomVertexArrayData::CData *_cdata,
259  bool writable);
260  INLINE GeomVertexArrayDataHandle(const GeomVertexArrayDataHandle &);
261  INLINE void operator = (const GeomVertexArrayDataHandle &);
262 
263 PUBLISHED:
264  INLINE ~GeomVertexArrayDataHandle();
265 
266 public:
267  ALLOC_DELETED_CHAIN_DECL(GeomVertexArrayDataHandle);
268 
269  INLINE Thread *get_current_thread() const;
270 
271  INLINE const unsigned char *get_read_pointer(bool force) const;
272  unsigned char *get_write_pointer();
273 
274 PUBLISHED:
275  INLINE const GeomVertexArrayData *get_object() const;
276  INLINE GeomVertexArrayData *get_object();
277 
278  INLINE const GeomVertexArrayFormat *get_array_format() const;
279  INLINE UsageHint get_usage_hint() const;
280 
281  INLINE int get_num_rows() const;
282  bool set_num_rows(int n);
283  bool unclean_set_num_rows(int n);
284  bool reserve_num_rows(int n);
285  INLINE void clear_rows();
286 
287  INLINE int get_data_size_bytes() const;
288  INLINE UpdateSeq get_modified() const;
289 
290  INLINE bool request_resident() const;
291 
292  INLINE VertexBufferContext *prepare_now(PreparedGraphicsObjects *prepared_objects,
293  GraphicsStateGuardianBase *gsg) const;
294 
295  void copy_data_from(const GeomVertexArrayDataHandle *other);
296  void copy_subdata_from(size_t to_start, size_t to_size,
297  const GeomVertexArrayDataHandle *other,
298  size_t from_start, size_t from_size);
299 
300  void copy_data_from(const unsigned char *source, size_t size);
301  void copy_subdata_from(size_t to_start, size_t to_size,
302  const unsigned char *source,
303  size_t from_start, size_t from_size);
304 
305  EXTENSION(void copy_data_from(PyObject *buffer));
306  EXTENSION(void copy_subdata_from(size_t to_start, size_t to_size,
307  PyObject *buffer));
308  EXTENSION(void copy_subdata_from(size_t to_start, size_t to_size,
309  PyObject *buffer,
310  size_t from_start, size_t from_size));
311 
312  INLINE string get_data() const;
313  void set_data(const string &data);
314  INLINE string get_subdata(size_t start, size_t size) const;
315  void set_subdata(size_t start, size_t size, const string &data);
316 
317  INLINE void mark_used() const;
318 
319 private:
320  PT(GeomVertexArrayData) _object;
321  Thread *_current_thread;
322  GeomVertexArrayData::CData *_cdata;
323  bool _writable;
324 
325 public:
326  static TypeHandle get_class_type() {
327  return _type_handle;
328  }
329  static void init_type() {
330  ReferenceCount::init_type();
331  register_type(_type_handle, "GeomVertexArrayDataHandle",
332  ReferenceCount::get_class_type());
333  }
334 
335 private:
336  static TypeHandle _type_handle;
337 
338  friend class GeomVertexArrayData;
339 };
340 
341 INLINE ostream &operator << (ostream &out, const GeomVertexArrayData &obj);
342 
343 #include "geomVertexArrayData.I"
344 
345 #endif
An implementation of a very simple LRU algorithm.
Definition: simpleLru.h:31
This is our own Panda specialization on the default STL map.
Definition: pmap.h:52
PTA_uchar read_raw_data(BamReader *manager, DatagramIterator &source)
Called by CData::fillin to read the raw data of the array from the indicated datagram.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
This class is similar to CycleDataWriter, except it allows writing to a particular stage of the pipel...
A single page of data maintained by a PipelineCycler.
Definition: cycleData.h:50
This class exists just to provide scoping for the various enumerated types used by Geom...
Definition: geomEnums.h:27
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
This is our own Panda specialization on the default STL list.
Definition: plist.h:38
This data object is returned by GeomVertexArrayData::get_handle() or modify_handle().
A table of objects that are saved within the graphics context for reference by handle later...
void set_lru_size(size_t lru_size)
Specifies the size of this page, presumably in bytes, although any unit is possible.
Definition: simpleLru.I:219
static Thread * get_current_thread()
Returns a pointer to the currently-executing Thread object.
Definition: thread.I:145
This template class calls PipelineCycler::read_unlocked(), and then provides a transparent read-only ...
A single block as returned from SimpleAllocator::alloc().
virtual void evict_lru()
Evicts the page from the LRU.
Definition: simpleLru.cxx:255
One atomic piece that may be managed by a SimpleLru chain.
Definition: simpleLru.h:70
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
This is used to keep track of, and limit the size of, the cache of munged vertices, which would otherwise be distributed through all of the GeomVertexData objects in the system.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
A block of bytes that stores the actual raw vertex data referenced by a GeomVertexArrayData object...
virtual int complete_pointers(TypedWritable **p_list, BamReader *manager)
Receives an array of pointers, one for each time manager-&gt;read_pointer() was called in fillin()...
Definition: cycleData.cxx:55
This template class calls PipelineCycler::write() in the constructor and PipelineCycler::release_writ...
A collection of VertexDataPages, which can be used to allocate new VertexDataBlock objects...
virtual void fillin(DatagramIterator &scan, BamReader *manager)
This internal function is intended to be called by each class&#39;s make_from_bam() method to read in all...
Definition: cycleData.cxx:68
This class is similar to CycleDataReader, except it allows reading from a particular stage of the pip...
A base class for all things that want to be reference-counted.
An STL function object class, this is intended to be used on any ordered collection of class objects ...
Definition: stl_compares.h:79
This base class provides basic reference counting, but also can be used with a CopyOnWritePointer to ...
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
A thread; that is, a lightweight process.
Definition: thread.h:51
This is a special class object that holds all the information returned by a particular GSG to indicat...
static void register_with_read_factory()
Tells the BamReader how to create objects of type GeomVertexArrayData.
A class to retrieve the individual data elements previously stored in a Datagram. ...
virtual void write_datagram(BamWriter *, Datagram &) const
Writes the contents of this object to the datagram for shipping out to a Bam file.
Definition: cycleData.cxx:34
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
This is a sequence number that increments monotonically.
Definition: updateSeq.h:43
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
A reentrant mutex.
Definition: reMutex.h:36
This is the data for one array of a GeomVertexData structure.
virtual void finalize(BamReader *manager)
Called by the BamReader to perform any final actions needed for setting up the object after all objec...