Panda3D
geomMunger.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 geomMunger.h
10  * @author drose
11  * @date 2005-03-10
12  */
13 
14 #ifndef GEOMMUNGER_H
15 #define GEOMMUNGER_H
16 
17 #include "pandabase.h"
18 #include "typedReferenceCount.h"
20 #include "geomVertexFormat.h"
21 #include "geomVertexData.h"
22 #include "geomCacheEntry.h"
23 #include "indirectCompareTo.h"
24 #include "pStatCollector.h"
25 #include "lightMutex.h"
26 #include "lightReMutex.h"
27 #include "pointerTo.h"
28 #include "pmap.h"
29 #include "pset.h"
30 
32 class RenderState;
33 class Geom;
34 
35 /**
36  * Objects of this class are used to convert vertex data from a Geom into a
37  * format suitable for passing to the rendering backend. Typically, the
38  * rendering backend will create a specialization of this class to handle its
39  * particular needs (e.g. DXGeomMunger). This class is necessary because
40  * DirectX and OpenGL have somewhat different requirements for vertex format.
41  *
42  * This also performs runtime application of state changes to the vertex data;
43  * for instance, by scaling all of the color values in response to a
44  * ColorScaleAttrib.
45  *
46  * A GeomMunger must be registered before it can be used, and once registered,
47  * the object is constant and cannot be changed. All registered GeomMungers
48  * that perform the same operation will have the same pointer.
49  */
50 class EXPCL_PANDA_GOBJ GeomMunger : public TypedReferenceCount, public GeomEnums {
51 public:
53  GeomMunger(const GeomMunger &copy);
54  void operator = (const GeomMunger &copy);
55  virtual ~GeomMunger();
56 
57  INLINE GraphicsStateGuardianBase *get_gsg() const;
58 
59  INLINE bool is_registered() const;
60  INLINE static PT(GeomMunger) register_munger(GeomMunger *munger, Thread *current_thread);
61  INLINE static void unregister_mungers_for_gsg(GraphicsStateGuardianBase *gsg);
62 
63  INLINE CPT(GeomVertexFormat) munge_format(const GeomVertexFormat *format,
64  const GeomVertexAnimationSpec &animation) const;
65 
66  INLINE CPT(GeomVertexData) munge_data(const GeomVertexData *data) const;
67  void remove_data(const GeomVertexData *data);
68 
69  bool munge_geom(CPT(Geom) &geom, CPT(GeomVertexData) &data,
70  bool force, Thread *current_thread);
71 
72  INLINE CPT(GeomVertexFormat) premunge_format(const GeomVertexFormat *format) const;
73  INLINE CPT(GeomVertexData) premunge_data(const GeomVertexData *data) const;
74  INLINE void premunge_geom(CPT(Geom) &geom, CPT(GeomVertexData) &data) const;
75 
76 public:
77  INLINE int compare_to(const GeomMunger &other) const;
78  INLINE int geom_compare_to(const GeomMunger &other) const;
79 
80 protected:
81  INLINE void unregister_myself();
82 
83  CPT(GeomVertexFormat) do_munge_format(const GeomVertexFormat *format,
84  const GeomVertexAnimationSpec &animation);
85 
86  virtual CPT(GeomVertexFormat) munge_format_impl(const GeomVertexFormat *orig,
87  const GeomVertexAnimationSpec &animation);
88  virtual CPT(GeomVertexData) munge_data_impl(const GeomVertexData *data);
89  virtual void munge_geom_impl(CPT(Geom) &geom, CPT(GeomVertexData) &data,
90  Thread *current_thread);
91 
92 
93  CPT(GeomVertexFormat) do_premunge_format(const GeomVertexFormat *format);
94  virtual CPT(GeomVertexFormat) premunge_format_impl(const GeomVertexFormat *orig);
95  virtual CPT(GeomVertexData) premunge_data_impl(const GeomVertexData *data);
96  virtual void premunge_geom_impl(CPT(Geom) &geom, CPT(GeomVertexData) &data);
97 
98  virtual int compare_to_impl(const GeomMunger *other) const;
99  virtual int geom_compare_to_impl(const GeomMunger *other) const;
100 
101 private:
102  class Registry;
103  INLINE static Registry *get_registry();
104  static void make_registry();
105 
106  void do_register(Thread *current_thread);
107  void do_unregister();
108 
109 private:
110  class CacheEntry : public GeomCacheEntry {
111  public:
112  virtual void output(std::ostream &out) const;
113 
114  PT(GeomMunger) _munger;
115  };
116 
117  typedef pmap<CPT(GeomVertexFormat), CPT(GeomVertexFormat) > Formats;
119  FormatsByAnimation _formats_by_animation;
120  Formats _premunge_formats;
121 
122  // This mutex protects the above.
123  LightMutex _formats_lock;
124 
126 
127  bool _is_registered;
129  class EXPCL_PANDA_GOBJ Registry {
130  public:
131  Registry();
132  PT(GeomMunger) register_munger(GeomMunger *munger, Thread *current_thread);
133  void unregister_munger(GeomMunger *munger);
134  void unregister_mungers_for_gsg(GraphicsStateGuardianBase *gsg);
135 
136  Mungers _mungers;
137  LightReMutex _registry_lock;
138  };
139 
140  // We store the iterator into the above registry, while we are registered.
141  // This makes it easier to remove our own entry, especially when the
142  // destructor is called. Since it's a virtual destructor, we can't reliably
143  // look up our pointer in the map once we have reached the base class
144  // destructor (since the object has changed types by then, and the sorting
145  // in the map depends partly on type).
146  Mungers::iterator _registered_key;
147 
148  static Registry *_registry;
149 
150  static PStatCollector _munge_pcollector;
151 
152  friend class GeomCacheManager;
153 
154 public:
155  static TypeHandle get_class_type() {
156  return _type_handle;
157  }
158  static void init_type() {
159  TypedReferenceCount::init_type();
160  register_type(_type_handle, "GeomMunger",
161  TypedReferenceCount::get_class_type());
162  }
163  virtual TypeHandle get_type() const {
164  return get_class_type();
165  }
166  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
167 
168 private:
169  static TypeHandle _type_handle;
170 
171  friend class Geom;
172 };
173 
174 #include "geomMunger.I"
175 
176 #endif
Geom
A container for geometry primitives.
Definition: geom.h:54
geomVertexData.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
GeomVertexData
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
Definition: geomVertexData.h:68
pandabase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pmap
This is our own Panda specialization on the default STL map.
Definition: pmap.h:49
register_type
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition: register_type.I:22
compare_to
An STL function object class, this is intended to be used on any ordered collection of class objects ...
Definition: stl_compares.h:73
lightMutex.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
GeomCacheEntry
This object contains a single cache entry in the GeomCacheManager.
Definition: geomCacheEntry.h:31
TypedReferenceCount
A base class for things which need to inherit from both TypedObject and from ReferenceCount.
Definition: typedReferenceCount.h:31
RenderState
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition: renderState.h:47
GeomCacheManager
This is used to keep track of, and limit the size of, the cache of munged vertices,...
Definition: geomCacheManager.h:37
GeomEnums
This class exists just to provide scoping for the various enumerated types used by Geom,...
Definition: geomEnums.h:24
LightMutex
This is a standard, non-reentrant mutex, similar to the Mutex class.
Definition: lightMutex.h:39
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
geomMunger.I
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pmap.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
GeomVertexAnimationSpec
This object describes how the vertex animation, if any, represented in a GeomVertexData is encoded.
Definition: geomVertexAnimationSpec.h:38
geomVertexAnimationSpec.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PStatCollector
A lightweight class that represents a single element that may be timed and/or counted via stats.
Definition: pStatCollector.h:43
GeomVertexFormat
This class defines the physical layout of the vertex data stored within a Geom.
Definition: geomVertexFormat.h:55
geomCacheEntry.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pset.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
GraphicsStateGuardianBase
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
Definition: graphicsStateGuardianBase.h:110
geomVertexFormat.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
typedReferenceCount.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
LightReMutex
A lightweight reentrant mutex.
Definition: lightReMutex.h:30
Thread
A thread; that is, a lightweight process.
Definition: thread.h:46
pointerTo.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pset
This is our own Panda specialization on the default STL set.
Definition: pset.h:49
indirectCompareTo.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
GeomMunger
Objects of this class are used to convert vertex data from a Geom into a format suitable for passing ...
Definition: geomMunger.h:50
lightReMutex.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pStatCollector.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.