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:
60 GeomVertexArrayData();
61
62protected:
63 virtual PT(CopyOnWriteObject) make_cow_copy();
64
65PUBLISHED:
66 explicit GeomVertexArrayData(const GeomVertexArrayFormat *array_format,
67 UsageHint usage_hint);
68 GeomVertexArrayData(const GeomVertexArrayData &copy);
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.
140 typedef pmap<PreparedGraphicsObjects *, VertexBufferContext *> Contexts;
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:
252 INLINE GeomVertexArrayDataHandle(CPT(GeomVertexArrayData) object,
253 Thread *current_thread);
254 INLINE GeomVertexArrayDataHandle(const GeomVertexArrayData *object,
255 Thread *current_thread);
256 INLINE GeomVertexArrayDataHandle(PT(GeomVertexArrayData) object,
257 Thread *current_thread);
258 INLINE GeomVertexArrayDataHandle(GeomVertexArrayData *object,
259 Thread *current_thread);
260
261PUBLISHED:
262 INLINE ~GeomVertexArrayDataHandle();
263
264public:
265 GeomVertexArrayDataHandle(const GeomVertexArrayDataHandle &) = delete;
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
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
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
This class exists just to provide scoping for the various enumerated types used by Geom,...
Definition geomEnums.h:24
This data object is returned by GeomVertexArrayData::get_handle() or modify_handle().
void copy_data_from(const GeomVertexArrayDataHandle *other)
Copies the entire data array from the other object.
void set_subdata(size_t start, size_t size, const vector_uchar &data)
Replaces a portion of the data array from the indicated string.
vector_uchar get_subdata(size_t start, size_t size) const
Returns a subset of the raw data of the GeomVertexArrayData object, formatted as a string.
VertexBufferContext * prepare_now(PreparedGraphicsObjects *prepared_objects, GraphicsStateGuardianBase *gsg) const
Creates a context for the data on the particular GSG, if it does not already exist.
const unsigned char * get_read_pointer(bool force) const
Returns a readable pointer to the beginning of the actual data stream, or NULL if the data is not cur...
vector_uchar get_data() const
Returns the entire raw data of the GeomVertexArrayData object, formatted as a string.
void copy_subdata_from(size_t to_start, size_t to_size, const GeomVertexArrayDataHandle *other, size_t from_start, size_t from_size)
Copies a portion of the data array from the other object into a portion of the data array of this obj...
bool request_resident() const
Returns true if the vertex data is currently resident in memory.
unsigned char * get_write_pointer()
Returns a writable pointer to the beginning of the actual data stream.
void mark_used() const
Marks the array data recently-used.
void set_data(const vector_uchar &data)
Replaces the entire raw data array with the contents of the indicated string.
This is the data for one array of a GeomVertexData structure.
get_usage_hint
Returns the usage hint that describes to the rendering backend how often the vertex data will be modi...
get_data_size_bytes
Returns the number of bytes stored in the array.
get_modified
Returns a sequence number which is guaranteed to change at least every time the array vertex data is ...
static VertexDataBook & get_book()
Returns the global VertexDataBook that will be used to allocate vertex data buffers.
bool request_resident(Thread *current_thread=Thread::get_current_thread()) const
Returns true if the vertex data is currently resident in memory.
static SimpleLru * get_small_lru()
Returns a pointer to the global LRU object that manages the GeomVertexArrayData's that are deemed too...
bool release(PreparedGraphicsObjects *prepared_objects)
Frees the data context only on the indicated object, if it exists there.
bool is_prepared(PreparedGraphicsObjects *prepared_objects) const
Returns true if the data has already been prepared or enqueued for preparation on the indicated GSG,...
bool set_num_rows(int n)
Sets the length of the array to n rows.
int compare_to(const GeomVertexArrayData &other) const
Returns 0 if the two arrays are equivalent, even if they are not the same pointer.
void clear_rows()
Removes all of the rows in the array.
get_array_format
Returns the format object that describes this array.
int get_num_rows() const
Returns the number of rows stored in the array, based on the number of bytes and the stride.
static SimpleLru * get_independent_lru()
Returns a pointer to the global LRU object that manages the GeomVertexArrayData's that have not (yet)...
int release_all()
Frees the context allocated on all objects for which the data has been declared.
bool reserve_num_rows(int n)
This ensures that enough memory space for n rows is allocated, so that you may increase the number of...
void prepare(PreparedGraphicsObjects *prepared_objects)
Indicates that the data should be enqueued to be prepared in the indicated prepared_objects at the be...
VertexBufferContext * prepare_now(PreparedGraphicsObjects *prepared_objects, GraphicsStateGuardianBase *gsg)
Creates a context for the data on the particular GSG, if it does not already exist.
bool has_column(const InternalName *name) const
Returns true if the array has the named column, false otherwise.
bool unclean_set_num_rows(int n)
This method behaves like set_num_rows(), except the new data is not initialized.
static void lru_epoch()
Marks that an epoch has passed in each LRU.
set_usage_hint
Changes the UsageHint hint for this array.
This describes the structure of a single array within a Geom data.
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 single block as returned from SimpleAllocator::alloc().
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.
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
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.
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.