00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "renderAttrib.h"
00016 #include "bamReader.h"
00017 #include "indent.h"
00018 #include "config_pgraph.h"
00019 #include "lightReMutexHolder.h"
00020
00021 LightReMutex *RenderAttrib::_attribs_lock = NULL;
00022 RenderAttrib::Attribs *RenderAttrib::_attribs = NULL;
00023 TypeHandle RenderAttrib::_type_handle;
00024
00025
00026
00027
00028
00029
00030 RenderAttrib::
00031 RenderAttrib() {
00032 if (_attribs == (Attribs *)NULL) {
00033 init_attribs();
00034 }
00035 _saved_entry = _attribs->end();
00036
00037 _always_reissue = false;
00038 }
00039
00040
00041
00042
00043
00044
00045 RenderAttrib::
00046 RenderAttrib(const RenderAttrib &) {
00047 nassertv(false);
00048 }
00049
00050
00051
00052
00053
00054
00055 void RenderAttrib::
00056 operator = (const RenderAttrib &) {
00057 nassertv(false);
00058 }
00059
00060
00061
00062
00063
00064
00065
00066 RenderAttrib::
00067 ~RenderAttrib() {
00068 LightReMutexHolder holder(*_attribs_lock);
00069
00070
00071 nassertv(_saved_entry == _attribs->end());
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
00099
00100
00101 bool RenderAttrib::
00102 lower_attrib_can_override() const {
00103 return false;
00104 }
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 bool RenderAttrib::
00116 has_cull_callback() const {
00117 return false;
00118 }
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 bool RenderAttrib::
00134 cull_callback(CullTraverser *, const CullTraverserData &) const {
00135 return true;
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145 bool RenderAttrib::
00146 unref() const {
00147
00148
00149 LightReMutexHolder holder(*_attribs_lock);
00150
00151 if (ReferenceCount::unref()) {
00152
00153 return true;
00154 }
00155
00156
00157
00158
00159 ((RenderAttrib *)this)->release_new();
00160
00161 return false;
00162 }
00163
00164
00165
00166
00167
00168
00169 void RenderAttrib::
00170 output(ostream &out) const {
00171 out << get_type();
00172 }
00173
00174
00175
00176
00177
00178
00179 void RenderAttrib::
00180 write(ostream &out, int indent_level) const {
00181 indent(out, indent_level) << *this << "\n";
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191 int RenderAttrib::
00192 get_num_attribs() {
00193 LightReMutexHolder holder(*_attribs_lock);
00194
00195 if (_attribs == (Attribs *)NULL) {
00196 return 0;
00197 }
00198 return _attribs->size();
00199 }
00200
00201
00202
00203
00204
00205
00206
00207
00208 void RenderAttrib::
00209 list_attribs(ostream &out) {
00210 LightReMutexHolder holder(*_attribs_lock);
00211
00212 out << _attribs->size() << " attribs:\n";
00213 Attribs::const_iterator si;
00214 for (si = _attribs->begin(); si != _attribs->end(); ++si) {
00215 const RenderAttrib *attrib = (*si);
00216 attrib->write(out, 2);
00217 }
00218 }
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228 bool RenderAttrib::
00229 validate_attribs() {
00230 LightReMutexHolder holder(*_attribs_lock);
00231
00232 if (_attribs->empty()) {
00233 return true;
00234 }
00235
00236 Attribs::const_iterator si = _attribs->begin();
00237 Attribs::const_iterator snext = si;
00238 ++snext;
00239 while (snext != _attribs->end()) {
00240 if ((*si)->compare_to(*(*snext)) >= 0) {
00241 pgraph_cat.error()
00242 << "RenderAttribs out of order!\n";
00243 (*si)->write(pgraph_cat.error(false), 2);
00244 (*snext)->write(pgraph_cat.error(false), 2);
00245 return false;
00246 }
00247 si = snext;
00248 ++snext;
00249 }
00250
00251 return true;
00252 }
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265 CPT(RenderAttrib) RenderAttrib::
00266 return_new(RenderAttrib *attrib) {
00267 nassertr(attrib != (RenderAttrib *)NULL, attrib);
00268 if (!uniquify_attribs) {
00269 return attrib;
00270 }
00271
00272 return return_unique(attrib);
00273 }
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 CPT(RenderAttrib) RenderAttrib::
00290 return_unique(RenderAttrib *attrib) {
00291 nassertr(attrib != (RenderAttrib *)NULL, attrib);
00292
00293 #ifndef NDEBUG
00294 if (!state_cache) {
00295 return attrib;
00296 }
00297 #endif
00298
00299 #ifndef NDEBUG
00300 if (paranoid_const) {
00301 nassertr(validate_attribs(), attrib);
00302 }
00303 #endif
00304
00305 LightReMutexHolder holder(*_attribs_lock);
00306
00307 if (attrib->_saved_entry != _attribs->end()) {
00308
00309 nassertr(_attribs->find(attrib) == attrib->_saved_entry, attrib);
00310 return attrib;
00311 }
00312
00313
00314
00315 CPT(RenderAttrib) pt_attrib = attrib;
00316
00317 pair<Attribs::iterator, bool> result = _attribs->insert(attrib);
00318 if (result.second) {
00319
00320
00321 attrib->_saved_entry = result.first;
00322
00323 return pt_attrib;
00324 }
00325
00326
00327
00328 return *(result.first);
00329 }
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346 int RenderAttrib::
00347 compare_to_impl(const RenderAttrib *other) const {
00348 return 0;
00349 }
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368 CPT(RenderAttrib) RenderAttrib::
00369 compose_impl(const RenderAttrib *other) const {
00370 return other;
00371 }
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382 CPT(RenderAttrib) RenderAttrib::
00383 invert_compose_impl(const RenderAttrib *other) const {
00384 return other;
00385 }
00386
00387
00388
00389
00390
00391
00392
00393 void RenderAttrib::
00394 output_comparefunc(ostream &out, PandaCompareFunc fn) const {
00395 switch (fn) {
00396 case M_none:
00397 out << "none";
00398 break;
00399
00400 case M_never:
00401 out << "never";
00402 break;
00403
00404 case M_less:
00405 out << "less";
00406 break;
00407
00408 case M_equal:
00409 out << "equal";
00410 break;
00411
00412 case M_less_equal:
00413 out << "less_equal";
00414 break;
00415
00416 case M_greater:
00417 out << "greater";
00418 break;
00419
00420 case M_not_equal:
00421 out << "not_equal";
00422 break;
00423
00424 case M_greater_equal:
00425 out << "greater_equal";
00426 break;
00427
00428 case M_always:
00429 out << "always";
00430 break;
00431 }
00432 }
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443 void RenderAttrib::
00444 release_new() {
00445 nassertv(_attribs_lock->debug_is_locked());
00446
00447 if (_saved_entry != _attribs->end()) {
00448
00449 #ifndef NDEBUG
00450 nassertd(_attribs->find(this) == _saved_entry) {
00451 cerr << "Tried to release " << *this << " (" << (void *)this << "), not found!\n";
00452 validate_attribs();
00453 Attribs::const_iterator si = _attribs->begin();
00454 if (si == _attribs->end()) {
00455 cerr << " Attribs list is empty.\n";
00456 } else {
00457 cerr << " Attribs list contains " << _attribs->size() << " entries.\n";
00458 const RenderAttrib *attrib = (*si);
00459 cerr << " " << *attrib << " (" << (void *)attrib << ")\n";
00460
00461 Attribs::const_iterator sprev = si;
00462 ++si;
00463 while (si != _attribs->end()) {
00464 const RenderAttrib *attrib = (*si);
00465 cerr << " " << *attrib << " (" << (void *)attrib << ")\n";
00466 if (((*sprev)->compare_to(*attrib)) >= 0) {
00467 cerr << "*** above out of order!\n";
00468 }
00469 sprev = si;
00470 ++si;
00471 }
00472 cerr << " Done.\n";
00473 }
00474 }
00475 #endif // NDEBUG
00476
00477 _attribs->erase(_saved_entry);
00478 _saved_entry = _attribs->end();
00479 }
00480 }
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492 void RenderAttrib::
00493 init_attribs() {
00494 _attribs = new Attribs;
00495
00496
00497
00498
00499
00500
00501 _attribs_lock = new LightReMutex("RenderAttrib::_attribs_lock");
00502 nassertv(Thread::get_current_thread() == Thread::get_main_thread());
00503 }
00504
00505
00506
00507
00508
00509
00510
00511 void RenderAttrib::
00512 write_datagram(BamWriter *manager, Datagram &dg) {
00513 TypedWritable::write_datagram(manager, dg);
00514 }
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527 TypedWritable *RenderAttrib::
00528 change_this(TypedWritable *old_ptr, BamReader *manager) {
00529
00530 RenderAttrib *attrib = DCAST(RenderAttrib, old_ptr);
00531 CPT(RenderAttrib) pointer = return_unique(attrib);
00532
00533
00534
00535
00536
00537
00538 if (pointer == attrib) {
00539 pointer->ref();
00540 manager->register_finalize(attrib);
00541 }
00542
00543
00544
00545 return (RenderAttrib *)pointer.p();
00546 }
00547
00548
00549
00550
00551
00552
00553
00554
00555 void RenderAttrib::
00556 finalize(BamReader *) {
00557
00558 unref();
00559
00560
00561
00562
00563
00564
00565 nassertv(get_ref_count() != 0);
00566 }
00567
00568
00569
00570
00571
00572
00573
00574
00575 void RenderAttrib::
00576 fillin(DatagramIterator &scan, BamReader *manager) {
00577 TypedWritable::fillin(scan, manager);
00578 manager->register_change_this(change_this, this);
00579 }