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