00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "renderAttribRegistry.h"
00016 #include "renderAttrib.h"
00017 #include "renderState.h"
00018 #include "deletedChain.h"
00019
00020 RenderAttribRegistry *RenderAttribRegistry::_global_ptr;
00021
00022
00023
00024
00025
00026
00027 RenderAttribRegistry::
00028 RenderAttribRegistry() {
00029 ConfigVariableInt max_attribs
00030 ("max-attribs", SlotMask::get_max_num_bits(),
00031 PRC_DESC("This specifies the maximum number of different RenderAttrib "
00032 "types that may be defined at runtime. Normally you should "
00033 "never need to change this, but if the default value is too "
00034 "low for the number of attribs that Panda actually defines, "
00035 "you may need to raise this number."));
00036
00037
00038 _max_slots = max((int)max_attribs, 1);
00039 if (_max_slots > SlotMask::get_max_num_bits()) {
00040 pgraph_cat->warning()
00041 << "Value for max-attribs too large: cannot exceed "
00042 << SlotMask::get_max_num_bits()
00043 << " in this build. To raise this limit, change the typedef "
00044 << "for SlotMask in renderAttribRegistry.h and recompile.\n";
00045
00046 _max_slots = SlotMask::get_max_num_bits();
00047 }
00048
00049
00050
00051 init_memory_hook();
00052 _array_chain = memory_hook->get_deleted_chain(_max_slots * sizeof(RenderState::Attribute));
00053
00054
00055
00056 RegistryNode node;
00057 node._sort = 0;
00058 node._make_default_func = NULL;
00059 _registry.push_back(node);
00060 }
00061
00062
00063
00064
00065
00066
00067 RenderAttribRegistry::
00068 ~RenderAttribRegistry() {
00069 }
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 int RenderAttribRegistry::
00099 register_slot(TypeHandle type_handle, int sort,
00100 RenderAttribRegistry::MakeDefaultFunc *make_default_func) {
00101 int type_index = type_handle.get_index();
00102 while (type_index >= (int)_slots_by_type.size()) {
00103 _slots_by_type.push_back(0);
00104 }
00105
00106 if (_slots_by_type[type_index] != 0) {
00107
00108 return _slots_by_type[type_index];
00109 }
00110
00111 int slot = (int)_registry.size();
00112 if (slot >= _max_slots) {
00113 pgraph_cat->error()
00114 << "Too many registered RenderAttribs; not registering "
00115 << type_handle << "\n";
00116 nassertr(false, 0);
00117 return 0;
00118 }
00119
00120 _slots_by_type[type_index] = slot;
00121
00122 RegistryNode node;
00123 node._type = type_handle;
00124 node._sort = sort;
00125 node._make_default_func = make_default_func;
00126 _registry.push_back(node);
00127
00128 _sorted_slots.push_back(slot);
00129 ::sort(_sorted_slots.begin(), _sorted_slots.end(), SortSlots(this));
00130
00131 return slot;
00132 }
00133
00134
00135
00136
00137
00138
00139 void RenderAttribRegistry::
00140 set_slot_sort(int slot, int sort) {
00141 nassertv(slot >= 0 && slot < (int)_registry.size());
00142 _registry[slot]._sort = sort;
00143
00144
00145 _sorted_slots.clear();
00146 _sorted_slots.reserve(_registry.size() - 1);
00147 for (int i = 1; i < (int)_registry.size(); ++i) {
00148 _sorted_slots.push_back(i);
00149 }
00150 ::sort(_sorted_slots.begin(), _sorted_slots.end(), SortSlots(this));
00151 }
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161 CPT(RenderAttrib) RenderAttribRegistry::
00162 get_slot_default(int slot) const {
00163 nassertr(slot >= 0 && slot < (int)_registry.size(), 0);
00164 return (*_registry[slot]._make_default_func)();
00165 }
00166
00167
00168
00169
00170
00171
00172 void RenderAttribRegistry::
00173 init_global_ptr() {
00174 _global_ptr = new RenderAttribRegistry;
00175 }