Explorar o código

wip: slot-based-RenderStates

David Rose %!s(int64=17) %!d(string=hai) anos
pai
achega
386b250585

+ 0 - 92
panda/src/pgraph/drawMaskAttrib.I

@@ -1,92 +0,0 @@
-// Filename: drawMaskAttrib.I
-// Created by:  drose (28Sep05)
-//
-////////////////////////////////////////////////////////////////////
-//
-// PANDA 3D SOFTWARE
-// Copyright (c) Carnegie Mellon University.  All rights reserved.
-//
-// All use of this software is subject to the terms of the revised BSD
-// license.  You should have received a copy of this license along
-// with this source code in a file named "LICENSE."
-//
-////////////////////////////////////////////////////////////////////
-
-
-////////////////////////////////////////////////////////////////////
-//     Function: DrawMaskAttrib::Constructor
-//       Access: Protected
-//  Description: Use DrawMaskAttrib::make() to construct a new
-//               DrawMaskAttrib object.
-////////////////////////////////////////////////////////////////////
-INLINE DrawMaskAttrib::
-DrawMaskAttrib(DrawMask new_mask, DrawMask bits_to_change) :
-  _new_mask(new_mask & bits_to_change),
-  _bits_to_change(bits_to_change)
-{
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: DrawMaskAttrib::Copy Constructor
-//       Access: Protected
-//  Description: Use DrawMaskAttrib::make() to construct a new
-//               DrawMaskAttrib object.
-////////////////////////////////////////////////////////////////////
-INLINE DrawMaskAttrib::
-DrawMaskAttrib(const DrawMaskAttrib &copy) :
-  _new_mask(copy._new_mask),
-  _bits_to_change(copy._bits_to_change)
-{
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: DrawMaskAttrib::make_hide
-//       Access: Published, Static
-//  Description: Constructs a new DrawMaskAttrib that removes the
-//               indicated draw bits from the visibility mask.  That
-//               is, it makes any nodes invisible to cameras that have
-//               any bits in common with draw_mask.  This is similar
-//               to (but not quite identical to)
-//               NodePath.hide(draw_mask).
-////////////////////////////////////////////////////////////////////
-INLINE CPT(RenderAttrib) DrawMaskAttrib::
-make_hide(DrawMask draw_mask) {
-  return make(DrawMask::all_off(), draw_mask);
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: DrawMaskAttrib::make_show
-//       Access: Published, Static
-//  Description: Constructs a new DrawMaskAttrib that adds the
-//               indicated draw bits to the visibility mask.  That
-//               is, it makes any nodes visible to cameras that have
-//               any bits in common with draw_mask.  This is similar
-//               to (but not quite identical to)
-//               NodePath.show(draw_mask).
-////////////////////////////////////////////////////////////////////
-INLINE CPT(RenderAttrib) DrawMaskAttrib::
-make_show(DrawMask draw_mask) {
-  return make(draw_mask, draw_mask);
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: DrawMaskAttrib::get_new_mask
-//       Access: Published
-//  Description: Returns the new DrawMask that will be set after the
-//               attrib has been applied.
-////////////////////////////////////////////////////////////////////
-INLINE DrawMask DrawMaskAttrib::
-get_new_mask() const {
-  return _new_mask;
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: DrawMaskAttrib::get_bits_to_change
-//       Access: Published
-//  Description: Returns the set of bits that will be allowed to be
-//               changed by this DrawMaskAttrib.
-////////////////////////////////////////////////////////////////////
-INLINE DrawMask DrawMaskAttrib::
-get_bits_to_change() const {
-  return _bits_to_change;
-}

+ 0 - 226
panda/src/pgraph/drawMaskAttrib.cxx

@@ -1,226 +0,0 @@
-// Filename: drawMaskAttrib.cxx
-// Created by:  drose (28Sep05)
-//
-////////////////////////////////////////////////////////////////////
-//
-// PANDA 3D SOFTWARE
-// Copyright (c) Carnegie Mellon University.  All rights reserved.
-//
-// All use of this software is subject to the terms of the revised BSD
-// license.  You should have received a copy of this license along
-// with this source code in a file named "LICENSE."
-//
-////////////////////////////////////////////////////////////////////
-
-#include "drawMaskAttrib.h"
-#include "dcast.h"
-#include "bamReader.h"
-#include "bamWriter.h"
-#include "datagram.h"
-#include "datagramIterator.h"
-#include "cullTraverser.h"
-#include "config_pgraph.h"
-
-TypeHandle DrawMaskAttrib::_type_handle;
-
-////////////////////////////////////////////////////////////////////
-//     Function: DrawMaskAttrib::make
-//       Access: Published, Static
-//  Description: Constructs a new DrawMaskAttrib that changes the
-//               bits_to_change bits in the current DrawMask to the
-//               values of the corresponding bits in new_mask.  Only
-//               those bits in common with bits_to_change are
-//               affected.
-////////////////////////////////////////////////////////////////////
-CPT(RenderAttrib) DrawMaskAttrib::
-make(DrawMask new_mask, DrawMask bits_to_change) {
-  DrawMaskAttrib *attrib = new DrawMaskAttrib(new_mask, bits_to_change);
-  return return_new(attrib);
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: DrawMaskAttrib::output
-//       Access: Public, Virtual
-//  Description: 
-////////////////////////////////////////////////////////////////////
-void DrawMaskAttrib::
-output(ostream &out) const {
-  out << get_type() << ":" << get_new_mask() << "/" << get_bits_to_change();
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: DrawMaskAttrib::compare_to_impl
-//       Access: Protected, Virtual
-//  Description: Intended to be overridden by derived DrawMaskAttrib
-//               types to return a unique number indicating whether
-//               this DrawMaskAttrib is equivalent to the other one.
-//
-//               This should return 0 if the two DrawMaskAttrib objects
-//               are equivalent, a number less than zero if this one
-//               should be sorted before the other one, and a number
-//               greater than zero otherwise.
-//
-//               This will only be called with two DrawMaskAttrib
-//               objects whose get_type() functions return the same.
-////////////////////////////////////////////////////////////////////
-int DrawMaskAttrib::
-compare_to_impl(const RenderAttrib *other) const {
-  const DrawMaskAttrib *ta;
-  DCAST_INTO_R(ta, other, 0);
-
-  int compare = get_new_mask().compare_to(ta->get_new_mask());
-  if (compare != 0) {
-    return compare;
-  }
-  compare = get_bits_to_change().compare_to(ta->get_bits_to_change());
-  return compare;
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: DrawMaskAttrib::compose_impl
-//       Access: Protected, Virtual
-//  Description: Intended to be overridden by derived RenderAttrib
-//               types to specify how two consecutive RenderAttrib
-//               objects of the same type interact.
-//
-//               This should return the result of applying the other
-//               RenderAttrib to a node in the scene graph below this
-//               RenderAttrib, which was already applied.  In most
-//               cases, the result is the same as the other
-//               RenderAttrib (that is, a subsequent RenderAttrib
-//               completely replaces the preceding one).  On the other
-//               hand, some kinds of RenderAttrib (for instance,
-//               ColorTransformAttrib) might combine in meaningful
-//               ways.
-////////////////////////////////////////////////////////////////////
-CPT(RenderAttrib) DrawMaskAttrib::
-compose_impl(const RenderAttrib *other) const {
-  const DrawMaskAttrib *ta;
-  DCAST_INTO_R(ta, other, 0);
-  
-  DrawMask mask = get_new_mask();
-  mask = (mask & ~ta->get_bits_to_change()) | ta->get_new_mask();
-
-  DrawMaskAttrib *attrib = new DrawMaskAttrib(mask, DrawMask::all_on());
-  return return_new(attrib);
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: DrawMaskAttrib::make_default_impl
-//       Access: Protected, Virtual
-//  Description: Intended to be overridden by derived DrawMaskAttrib
-//               types to specify what the default property for a
-//               DrawMaskAttrib of this type should be.
-//
-//               This should return a newly-allocated DrawMaskAttrib of
-//               the same type that corresponds to whatever the
-//               standard default for this kind of DrawMaskAttrib is.
-////////////////////////////////////////////////////////////////////
-RenderAttrib *DrawMaskAttrib::
-make_default_impl() const {
-  return new DrawMaskAttrib(DrawMask::all_on(), DrawMask::all_on());
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: DrawMaskAttrib::store_into_slot
-//       Access: Public, Virtual
-//  Description: Stores this attrib into the appropriate slot of
-//               an object of class AttribSlots.
-////////////////////////////////////////////////////////////////////
-void DrawMaskAttrib::
-store_into_slot(AttribSlots *) const {
-  // There's no need to store a DrawMaskAttrib at the moment, since it
-  // doesn't actually change any rendering state.
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: DrawMaskAttrib::has_cull_callback
-//       Access: Public, Virtual
-//  Description: Should be overridden by derived classes to return
-//               true if cull_callback() has been defined.  Otherwise,
-//               returns false to indicate cull_callback() does not
-//               need to be called for this node during the cull
-//               traversal.
-////////////////////////////////////////////////////////////////////
-bool DrawMaskAttrib::
-has_cull_callback() const {
-  return (_new_mask != DrawMask::all_on());
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: DrawMaskAttrib::cull_callback
-//       Access: Public, Virtual
-//  Description: If has_cull_callback() returns true, this function
-//               will be called during the cull traversal to perform
-//               any additional operations that should be performed at
-//               cull time.
-//
-//               This is called each time the RenderAttrib is
-//               discovered applied to a Geom in the traversal.  It
-//               should return true if the Geom is visible, false if
-//               it should be omitted.
-////////////////////////////////////////////////////////////////////
-bool DrawMaskAttrib::
-cull_callback(CullTraverser *trav, const CullTraverserData &) const {
-  return (trav->get_camera_mask() & _new_mask) != 0;
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: DrawMaskAttrib::register_with_read_factory
-//       Access: Public, Static
-//  Description: Tells the BamReader how to create objects of type
-//               DrawMaskAttrib.
-////////////////////////////////////////////////////////////////////
-void DrawMaskAttrib::
-register_with_read_factory() {
-  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: DrawMaskAttrib::write_datagram
-//       Access: Public, Virtual
-//  Description: Writes the contents of this object to the datagram
-//               for shipping out to a Bam file.
-////////////////////////////////////////////////////////////////////
-void DrawMaskAttrib::
-write_datagram(BamWriter *manager, Datagram &dg) {
-  RenderAttrib::write_datagram(manager, dg);
-
-  dg.add_uint32(_new_mask.get_word());
-  dg.add_uint32(_bits_to_change.get_word());
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: DrawMaskAttrib::make_from_bam
-//       Access: Protected, Static
-//  Description: This function is called by the BamReader's factory
-//               when a new object of type DrawMaskAttrib is encountered
-//               in the Bam file.  It should create the DrawMaskAttrib
-//               and extract its information from the file.
-////////////////////////////////////////////////////////////////////
-TypedWritable *DrawMaskAttrib::
-make_from_bam(const FactoryParams &params) {
-  DrawMaskAttrib *attrib = new DrawMaskAttrib(0, 0);
-  DatagramIterator scan;
-  BamReader *manager;
-
-  parse_params(params, scan, manager);
-  attrib->fillin(scan, manager);
-
-  return attrib;
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: DrawMaskAttrib::fillin
-//       Access: Protected
-//  Description: This internal function is called by make_from_bam to
-//               read in all of the relevant data from the BamFile for
-//               the new DrawMaskAttrib.
-////////////////////////////////////////////////////////////////////
-void DrawMaskAttrib::
-fillin(DatagramIterator &scan, BamReader *manager) {
-  RenderAttrib::fillin(scan, manager);
-
-  _new_mask.set_word(scan.get_uint32());
-  _bits_to_change.set_word(scan.get_uint32());
-}

+ 0 - 98
panda/src/pgraph/drawMaskAttrib.h

@@ -1,98 +0,0 @@
-// Filename: drawMaskAttrib.h
-// Created by:  drose (28Sep05)
-//
-////////////////////////////////////////////////////////////////////
-//
-// PANDA 3D SOFTWARE
-// Copyright (c) Carnegie Mellon University.  All rights reserved.
-//
-// All use of this software is subject to the terms of the revised BSD
-// license.  You should have received a copy of this license along
-// with this source code in a file named "LICENSE."
-//
-////////////////////////////////////////////////////////////////////
-
-#ifndef DRAWMASKATTRIB_H
-#define DRAWMASKATTRIB_H
-
-#include "pandabase.h"
-
-#include "renderAttrib.h"
-#include "drawMask.h"
-
-class FactoryParams;
-
-////////////////////////////////////////////////////////////////////
-//       Class : DrawMaskAttrib
-// Description : This attrib can be used to control the visibility of
-//               certain Geoms from certain cameras.  It is similar in
-//               principle to the PandaNode::set_draw_mask()
-//               interface, except it does not cause an early prune in
-//               the cull traversal; thus, it can be used to show a
-//               node even though its parent has been hidden (if the
-//               parent was hidden using the same interface).
-//
-//               It is mainly useful for unusual circumstances in
-//               which the visibility of a node is not easy to
-//               determine from examining the static hierarchy of the
-//               graph.
-////////////////////////////////////////////////////////////////////
-class EXPCL_PANDA_PGRAPH DrawMaskAttrib : public RenderAttrib {
-protected:
-  INLINE DrawMaskAttrib(DrawMask new_mask, DrawMask bits_to_change);
-  INLINE DrawMaskAttrib(const DrawMaskAttrib &copy);
-
-PUBLISHED:
-  INLINE static CPT(RenderAttrib) make_hide(DrawMask draw_mask = DrawMask::all_on());
-  INLINE static CPT(RenderAttrib) make_show(DrawMask draw_mask = DrawMask::all_on());
-  static CPT(RenderAttrib) make(DrawMask new_mask, DrawMask bits_to_change);
-
-  INLINE DrawMask get_new_mask() const;
-  INLINE DrawMask get_bits_to_change() const;
-
-public:
-  virtual void output(ostream &out) const;
-  virtual void store_into_slot(AttribSlots *slots) const;
-
-  virtual bool has_cull_callback() const;
-  virtual bool cull_callback(CullTraverser *trav, const CullTraverserData &data) const;
-
-protected:
-  virtual int compare_to_impl(const RenderAttrib *other) const;
-  virtual CPT(RenderAttrib) compose_impl(const RenderAttrib *other) const;
-  virtual RenderAttrib *make_default_impl() const;
-
-private:
-  DrawMask _new_mask;
-  DrawMask _bits_to_change;
-
-public:
-  static void register_with_read_factory();
-  virtual void write_datagram(BamWriter *manager, Datagram &dg);
-
-protected:
-  static TypedWritable *make_from_bam(const FactoryParams &params);
-  void fillin(DatagramIterator &scan, BamReader *manager);
-  
-public:
-  static TypeHandle get_class_type() {
-    return _type_handle;
-  }
-  static void init_type() {
-    RenderAttrib::init_type();
-    register_type(_type_handle, "DrawMaskAttrib",
-                  RenderAttrib::get_class_type());
-  }
-  virtual TypeHandle get_type() const {
-    return get_class_type();
-  }
-  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
-
-private:
-  static TypeHandle _type_handle;
-};
-
-#include "drawMaskAttrib.I"
-
-#endif
-

+ 150 - 0
panda/src/pgraph/renderAttribRegistry.I

@@ -0,0 +1,150 @@
+// Filename: renderAttribRegistry.I
+// Created by:  drose (13Nov08)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) Carnegie Mellon University.  All rights reserved.
+//
+// All use of this software is subject to the terms of the revised BSD
+// license.  You should have received a copy of this license along
+// with this source code in a file named "LICENSE."
+//
+////////////////////////////////////////////////////////////////////
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: RenderAttribRegistry::get_slot
+//       Access: Published
+//  Description: Returns the slot number assigned to the indicated
+//               TypeHandle, or 0 if no slot number has been assigned.
+////////////////////////////////////////////////////////////////////
+INLINE int RenderAttribRegistry::
+get_slot(TypeHandle type_handle) const {
+  int type_index = type_handle.get_index();
+  if (type_index >= (int)_slots_by_type.size()) {
+    return 0;
+  }
+  return _slots_by_type[type_index];
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: RenderAttribRegistry::get_max_slots
+//       Access: Published
+//  Description: Returns the maximum number that any slot number is
+//               allowed to grow.  Actually, this number will be one
+//               higher than the highest possible slot number.  This
+//               puts an upper bound on the number of RenderAttrib
+//               slots that may be allocated, and allows other code to
+//               define an array of slots.
+//
+//               This number will not change during the lifetime of
+//               the application.
+////////////////////////////////////////////////////////////////////
+INLINE int RenderAttribRegistry::
+get_max_slots() const {
+  return _max_slots;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: RenderAttribRegistry::get_num_slots
+//       Access: Published
+//  Description: Returns the number of RenderAttrib slots that have
+//               been allocated.  This is one more than the highest
+//               slot number in use.
+////////////////////////////////////////////////////////////////////
+INLINE int RenderAttribRegistry::
+get_num_slots() const {
+  return _registry.size();
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: RenderAttribRegistry::get_slot_type
+//       Access: Published
+//  Description: Returns the TypeHandle associated with slot n.
+////////////////////////////////////////////////////////////////////
+INLINE TypeHandle RenderAttribRegistry::
+get_slot_type(int slot) const {
+  nassertr(slot >= 0 && slot < (int)_registry.size(), TypeHandle::none());
+  return _registry[slot]._type;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: RenderAttribRegistry::get_slot_sort
+//       Access: Published
+//  Description: Returns the sort number associated with slot n.
+////////////////////////////////////////////////////////////////////
+INLINE int RenderAttribRegistry::
+get_slot_sort(int slot) const {
+  nassertr(slot >= 0 && slot < (int)_registry.size(), 0);
+  return _registry[slot]._sort;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: RenderAttribRegistry::get_num_sorted_slots
+//       Access: Published
+//  Description: Returns the number of entries in the sorted_slots
+//               list.
+////////////////////////////////////////////////////////////////////
+INLINE int RenderAttribRegistry::
+get_num_sorted_slots() const {
+  return _sorted_slots.size();
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: RenderAttribRegistry::get_sorted_slot
+//       Access: Published
+//  Description: Returns the nth slot in sorted order.  By traversing
+//               this list, you will retrieve all the slot numbers in
+//               order according to their registered sort value.
+////////////////////////////////////////////////////////////////////
+INLINE int RenderAttribRegistry::
+get_sorted_slot(int n) const {
+  nassertr(n >= 0 && n < (int)_sorted_slots.size(), 0);
+  return _sorted_slots[n];
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: RenderAttribRegistry::get_global_ptr
+//       Access: Published, Static
+//  Description:
+////////////////////////////////////////////////////////////////////
+INLINE RenderAttribRegistry *RenderAttribRegistry::
+get_global_ptr() {
+  if (_global_ptr == (RenderAttribRegistry *)NULL) {
+    init_global_ptr();
+  }
+  return _global_ptr;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: RenderAttribRegistry::quick_get_global_ptr
+//       Access: Public, Static
+//  Description: Returns the global_ptr without first ensuring it has
+//               been initialized.  Only safe for code that knows it
+//               has already been initialized.
+////////////////////////////////////////////////////////////////////
+INLINE RenderAttribRegistry *RenderAttribRegistry::
+quick_get_global_ptr() {
+  return _global_ptr;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: RenderAttribRegistry::SortSlots::Constructor
+//       Access: Public
+//  Description: This is an STL function object for sorting the
+//               _sorted_slots list into order by slot sort number.
+////////////////////////////////////////////////////////////////////
+INLINE RenderAttribRegistry::SortSlots::
+SortSlots(RenderAttribRegistry *reg) : _reg(reg) {
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: RenderAttribRegistry::SortSlots::operator ()
+//       Access: Public
+//  Description: 
+////////////////////////////////////////////////////////////////////
+INLINE bool RenderAttribRegistry::SortSlots::
+operator () (int a, int b) const {
+  return _reg->get_slot_sort(a) < _reg->get_slot_sort(b);
+}

+ 168 - 0
panda/src/pgraph/renderAttribRegistry.cxx

@@ -0,0 +1,168 @@
+// Filename: renderAttribRegistry.cxx
+// Created by:  drose (13Nov08)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) Carnegie Mellon University.  All rights reserved.
+//
+// All use of this software is subject to the terms of the revised BSD
+// license.  You should have received a copy of this license along
+// with this source code in a file named "LICENSE."
+//
+////////////////////////////////////////////////////////////////////
+
+#include "renderAttribRegistry.h"
+#include "renderAttrib.h"
+
+RenderAttribRegistry *RenderAttribRegistry::_global_ptr;
+
+////////////////////////////////////////////////////////////////////
+//     Function: RenderAttribRegistry::Constructor
+//       Access: Private
+//  Description:
+////////////////////////////////////////////////////////////////////
+RenderAttribRegistry::
+RenderAttribRegistry() {
+  ConfigVariableInt max_attribs
+    ("max-attribs", SlotMask::get_max_num_bits(),
+     PRC_DESC("This specifies the maximum number of different RenderAttrib "
+              "types that may be defined at runtime.  Normally you should "
+              "never need to change this, but if the default value is too "
+              "low for the number of attribs that Panda actually defines, "
+              "you may need to raise this number."));
+
+  // Assign this number once, at startup, and never change it again.
+  _max_slots = max((int)max_attribs, 1);
+  if (_max_slots > SlotMask::get_max_num_bits()) {
+    pgraph_cat->warning()
+      << "Value for max-attribs too large: cannot exceed " 
+      << SlotMask::get_max_num_bits()
+      << " in this build.  To raise this limit, change the typedef "
+      << "for SlotMask in renderAttribRegistry.h and recompile.\n";
+      
+    _max_slots = SlotMask::get_max_num_bits();
+  }
+
+  // Reserve slot 0 for TypeHandle::none(), and for types that exceed
+  // max_slots.
+  RegistryNode node;
+  node._sort = 0;
+  node._make_default_func = NULL;
+  _registry.push_back(node);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: RenderAttribRegistry::Destructor
+//       Access: Private
+//  Description:
+////////////////////////////////////////////////////////////////////
+RenderAttribRegistry::
+~RenderAttribRegistry() {
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: RenderAttribRegistry::register_slot
+//       Access: Public
+//  Description: Adds the indicated TypeHandle to the registry, if it
+//               is not there already, and returns a unique slot
+//               number in the range 0 < slot < get_max_slots().
+//
+//               The sort value is an arbitrary integer.  In general,
+//               the RenderAttribs will be sorted in order from lowest
+//               sort value to highest sort value, when they are
+//               traversed via the get_num_sorted_slots() /
+//               get_sorted_slot() methods.  This will be used to sort
+//               render states, so that heavier RenderAttribs are
+//               changed less frequently.  In general, you should
+//               choose sort values such that the heavier
+//               RenderAttribs (that is, those which are more
+//               expensive to change) have lower sort values.
+//
+//               The make_default_func pointer is a function that may
+//               be called to generate a default RenderAttrib to apply
+//               in the absence of any other attrib of this type.
+//
+//               register_slot() is intended to be called at
+//               application start for each different RenderAttrib
+//               type in the system, to assign a different integer
+//               slot number to each one.
+////////////////////////////////////////////////////////////////////
+int RenderAttribRegistry::
+register_slot(TypeHandle type_handle, int sort,
+              RenderAttribRegistry::MakeDefaultFunc *make_default_func) {
+  int type_index = type_handle.get_index();
+  while (type_index >= (int)_slots_by_type.size()) {
+    _slots_by_type.push_back(0);
+  }
+
+  if (_slots_by_type[type_index] != 0) {
+    // This type has already been registered.
+    return _slots_by_type[type_index];
+  }
+
+  int slot = (int)_registry.size();
+  if (slot >= _max_slots) {
+    pgraph_cat->error()
+      << "Too many registered RenderAttribs; not registering " 
+      << type_handle << "\n";
+    nassertr(false, 0);
+    return 0;
+  }
+
+  _slots_by_type[type_index] = slot;
+
+  RegistryNode node;
+  node._type = type_handle;
+  node._sort = sort;
+  node._make_default_func = make_default_func;
+  _registry.push_back(node);
+
+  _sorted_slots.push_back(slot);
+  ::sort(_sorted_slots.begin(), _sorted_slots.end(), SortSlots(this));
+
+  return slot;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: RenderAttribRegistry::set_slot_sort
+//       Access: Published
+//  Description: Changes the sort number associated with slot n.
+////////////////////////////////////////////////////////////////////
+void RenderAttribRegistry::
+set_slot_sort(int slot, int sort) {
+  nassertv(slot >= 0 && slot < (int)_registry.size());
+  _registry[slot]._sort = sort;
+
+  // Re-sort the slot list.
+  _sorted_slots.clear();
+  _sorted_slots.reserve(_registry.size() - 1);
+  for (int i = 1; i < (int)_registry.size(); ++i) {
+    _sorted_slots.push_back(i);
+  }
+  ::sort(_sorted_slots.begin(), _sorted_slots.end(), SortSlots(this));
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: RenderAttribRegistry::get_slot_default
+//       Access: Published
+//  Description: Returns the default RenderAttrib object associated
+//               with slot n.  This is the attrib that should be
+//               applied in the absence of any other attrib of this
+//               type.
+////////////////////////////////////////////////////////////////////
+CPT(RenderAttrib) RenderAttribRegistry::
+get_slot_default(int slot) const {
+  nassertr(slot >= 0 && slot < (int)_registry.size(), 0);
+  return (*_registry[slot]._make_default_func)();
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: RenderAttribRegistry::init_global_ptr
+//       Access: Private, Static
+//  Description:
+////////////////////////////////////////////////////////////////////
+void RenderAttribRegistry::
+init_global_ptr() {
+  _global_ptr = new RenderAttribRegistry;
+}

+ 102 - 0
panda/src/pgraph/renderAttribRegistry.h

@@ -0,0 +1,102 @@
+// Filename: renderAttribRegistry.h
+// Created by:  drose (13Nov08)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) Carnegie Mellon University.  All rights reserved.
+//
+// All use of this software is subject to the terms of the revised BSD
+// license.  You should have received a copy of this license along
+// with this source code in a file named "LICENSE."
+//
+////////////////////////////////////////////////////////////////////
+
+#ifndef RENDERATTRIBREGISTRY_H
+#define RENDERATTRIBREGISTRY_H
+
+#include "pandabase.h"
+#include "typeHandle.h"
+#include "vector_int.h"
+#include "pointerTo.h"
+#include "bitMask.h"
+
+class RenderAttrib;
+
+////////////////////////////////////////////////////////////////////
+//       Class : RenderAttribRegistry
+// Description : This class is used to associate each RenderAttrib
+//               with a different slot index at runtime, so we can
+//               store a list of RenderAttribs in the RenderState
+//               object, and very quickly look them up by type.
+////////////////////////////////////////////////////////////////////
+class EXPCL_PANDA_PGRAPH RenderAttribRegistry {
+private:
+  RenderAttribRegistry();
+  ~RenderAttribRegistry();
+
+public:
+  typedef CPT(RenderAttrib) MakeDefaultFunc();
+
+  // This typedef defines the native bitmask type for indicating which
+  // slots are present in a RenderState.  It must be wide enough to
+  // allow room for all of the possible RenderAttribs that might
+  // register themselves.  Presently, 32 bits is wide enough, but only
+  // barely; when we exceed this limit, we will need to go to a 64-bit
+  // type instead.  It will be interesting to see whether a BitMask64
+  // or a DoubleBitMask<BitMask32> will be faster on a 32-bit machine.
+  typedef BitMask32 SlotMask;
+
+  int register_slot(TypeHandle type_handle, int sort,
+                    MakeDefaultFunc *make_default_func);
+
+PUBLISHED:
+  INLINE int get_slot(TypeHandle type_handle) const;
+  INLINE int get_max_slots() const;
+
+  INLINE int get_num_slots() const;
+  INLINE TypeHandle get_slot_type(int slot) const;
+  INLINE int get_slot_sort(int slot) const;
+  void set_slot_sort(int slot, int sort);
+  CPT(RenderAttrib) get_slot_default(int slot) const;
+
+  INLINE int get_num_sorted_slots() const;
+  INLINE int get_sorted_slot(int n) const;
+
+  INLINE static RenderAttribRegistry *get_global_ptr();
+
+public:
+  INLINE static RenderAttribRegistry *quick_get_global_ptr();
+
+private:
+  static void init_global_ptr();
+
+private:
+  int _max_slots;
+
+  class SortSlots {
+  public:
+    INLINE SortSlots(RenderAttribRegistry *reg);
+    INLINE bool operator () (int a, int b) const;
+    RenderAttribRegistry *_reg;
+  };
+
+  class RegistryNode {
+  public:
+    TypeHandle _type;
+    int _sort;
+    MakeDefaultFunc *_make_default_func;
+  };
+  typedef pvector<RegistryNode> Registry;
+  Registry _registry;
+
+  vector_int _slots_by_type;
+  vector_int _sorted_slots;
+
+  static RenderAttribRegistry *_global_ptr;
+};
+
+#include "renderAttribRegistry.I"
+
+#endif
+