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