Panda3D
renderAttribRegistry.cxx
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 renderAttribRegistry.cxx
10  * @author drose
11  * @date 2008-11-13
12  */
13 
14 #include "renderAttribRegistry.h"
15 #include "renderAttrib.h"
16 #include "renderState.h"
17 #include "deletedChain.h"
18 
19 RenderAttribRegistry *RenderAttribRegistry::_global_ptr;
20 
21 /**
22  *
23  */
24 RenderAttribRegistry::
25 RenderAttribRegistry() {
26  _registry.reserve(_max_slots);
27  _sorted_slots.reserve(_max_slots);
28 
29  // Reserve slot 0 for TypeHandle::none(), and for types that exceed
30  // max_slots.
31  _registry.push_back(RegistryNode(TypeHandle::none(), 0, nullptr));
32 }
33 
34 /**
35  *
36  */
37 RenderAttribRegistry::
38 ~RenderAttribRegistry() {
39 }
40 
41 /**
42  * Adds the indicated TypeHandle to the registry, if it is not there already,
43  * and returns a unique slot number in the range 0 < slot < get_max_slots().
44  *
45  * The sort value is an arbitrary integer. In general, the RenderAttribs will
46  * be sorted in order from lowest sort value to highest sort value, when they
47  * are traversed via the get_num_sorted_slots() get_sorted_slot() methods.
48  * This will be used to sort render states, so that heavier RenderAttribs are
49  * changed less frequently. In general, you should choose sort values such
50  * that the heavier RenderAttribs (that is, those which are more expensive to
51  * change) have lower sort values.
52  *
53  * The default_attrib pointer should be a newly created instance of this
54  * attribute that represents the default state for this attribute.
55  *
56  * register_slot() is intended to be called at application start for each
57  * different RenderAttrib type in the system, to assign a different integer
58  * slot number to each one.
59  */
61 register_slot(TypeHandle type_handle, int sort, RenderAttrib *default_attrib) {
62  // Sanity check; if this triggers, you either passed a wrong argument, or
63  // you didn't use the type system correctly.
64  nassertr(default_attrib->get_type() == type_handle, 0);
65 
66  int type_index = type_handle.get_index();
67  while (type_index >= (int)_slots_by_type.size()) {
68  _slots_by_type.push_back(0);
69  }
70 
71  if (_slots_by_type[type_index] != 0) {
72  // This type has already been registered.
73  return _slots_by_type[type_index];
74  }
75 
76  int slot = (int)_registry.size();
77  if (slot >= _max_slots) {
78  pgraph_cat->error()
79  << "Too many registered RenderAttribs; not registering "
80  << type_handle << "\n";
81  nassert_raise("out of RenderAttrib slots");
82  return 0;
83  }
84 
85  // Register the default attribute. We don't use return_unique and register
86  // it even if the state cache is disabled, because we can't read the
87  // state_cache config variable yet at this time. It probably doesn't hurt
88  // to have these 32 entries around in the attrib cache.
89  if (default_attrib != nullptr) {
90  default_attrib->calc_hash();
91 
92  if (default_attrib->_saved_entry == -1) {
93  // If this attribute was already registered, something odd is going on.
94  nassertr(RenderAttrib::_attribs->find(default_attrib) == -1, 0);
95  default_attrib->_saved_entry =
96  RenderAttrib::_attribs->store(default_attrib, nullptr);
97  }
98 
99  // It effectively lives forever. Might as well make it official.
100  default_attrib->local_object();
101  }
102 
103  _slots_by_type[type_index] = slot;
104 
105  _registry.push_back(RegistryNode(type_handle, sort, default_attrib));
106 
107  _sorted_slots.push_back(slot);
108  std::sort(_sorted_slots.begin(), _sorted_slots.end(), SortSlots(this));
109 
110  return slot;
111 }
112 
113 /**
114  * Changes the sort number associated with slot n.
115  */
117 set_slot_sort(int slot, int sort) {
118  nassertv(slot >= 0 && slot < (int)_registry.size());
119  _registry[slot]._sort = sort;
120 
121  // Re-sort the slot list.
122  _sorted_slots.clear();
123  for (int i = 1; i < (int)_registry.size(); ++i) {
124  _sorted_slots.push_back(i);
125  }
126  std::sort(_sorted_slots.begin(), _sorted_slots.end(), SortSlots(this));
127 }
128 
129 /**
130  *
131  */
132 void RenderAttribRegistry::
133 init_global_ptr() {
134  _global_ptr = new RenderAttribRegistry;
135 }
This is the base class for a number of render attributes (other than transform) that may be set on sc...
Definition: renderAttrib.h:51
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int store(const Key &key, const Value &data)
Records the indicated key/data pair in the map.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int register_slot(TypeHandle type_handle, int sort, RenderAttrib *default_attrib)
Adds the indicated TypeHandle to the registry, if it is not there already, and returns a unique slot ...
This class is used to associate each RenderAttrib with a different slot index at runtime,...
get_index
Returns the integer index associated with this TypeHandle.
Definition: typeHandle.h:135
void local_object()
This function should be called, once, immediately after creating a new instance of some ReferenceCoun...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void set_slot_sort(int slot, int sort)
Changes the sort number associated with slot n.