15 #include "renderAttrib.h"
16 #include "bamReader.h"
18 #include "config_pgraph.h"
19 #include "lightReMutexHolder.h"
20 #include "pStatTimer.h"
26 int RenderAttrib::_garbage_index = 0;
28 PStatCollector RenderAttrib::_garbage_collect_pcollector(
"*:State Cache:Garbage Collect");
37 if (_attribs == (Attribs *)NULL) {
42 _always_reissue =
false;
76 nassertv(_saved_entry == -1);
149 get_auto_shader_attrib_impl(const
RenderState *state)
const {
162 if (!state_cache || garbage_collect_states) {
197 output(ostream &out)
const {
207 write(ostream &out,
int indent_level)
const {
208 indent(out, indent_level) << *
this <<
"\n";
222 if (_attribs == (
Attribs *)NULL) {
241 for (
int si = 0; si < size; ++si) {
246 attrib->write(out, 2);
259 if (_attribs == (
Attribs *)NULL || !garbage_collect_states) {
264 PStatTimer timer(_garbage_collect_pcollector);
271 int num_this_pass = int(size * garbage_collect_states_rate);
272 if (num_this_pass <= 0) {
275 num_this_pass = min(num_this_pass, size);
276 int stop_at_element = (_garbage_index + num_this_pass) % size;
278 int num_elements = 0;
279 int si = _garbage_index;
291 attrib->release_new();
292 unref_delete(attrib);
296 si = (si + 1) % size;
297 }
while (si != stop_at_element);
302 return orig_size - new_size;
322 <<
"RenderAttrib::_attribs cache is invalid!\n";
325 for (
int si = 0; si < size; ++si) {
330 cerr << si <<
": " << attrib <<
"\n";
331 attrib->write(cerr, 2);
339 while (si < size && !_attribs->has_element(si)) {
342 nassertr(si < size,
false);
343 nassertr(_attribs->
get_key(si)->get_ref_count() >= 0,
false);
346 while (snext < size && !_attribs->has_element(snext)) {
349 while (snext < size) {
350 nassertr(_attribs->
get_key(snext)->get_ref_count() >= 0,
false);
353 int c = ssi->compare_to(*ssnext);
355 if ((ci < 0) != (c > 0) ||
356 (ci > 0) != (c < 0) ||
357 (ci == 0) != (c == 0)) {
359 <<
"RenderAttrib::compare_to() not defined properly!\n";
360 pgraph_cat.error(
false)
361 <<
"(a, b): " << c <<
"\n";
362 pgraph_cat.error(
false)
363 <<
"(b, a): " << ci <<
"\n";
364 ssi->write(pgraph_cat.error(
false), 2);
365 ssnext->write(pgraph_cat.error(
false), 2);
370 while (snext < size && !_attribs->has_element(snext)) {
392 if (!uniquify_attribs) {
397 return return_unique(attrib);
425 if (paranoid_const) {
426 nassertr(validate_attribs(), attrib);
432 if (attrib->_saved_entry != -1) {
442 int si = _attribs->find(attrib);
445 return _attribs->get_key(si);
449 if (garbage_collect_states) {
455 si = _attribs->store(attrib, Empty());
458 attrib->_saved_entry = si;
492 size_t RenderAttrib::
493 get_hash_impl()
const {
540 output_comparefunc(ostream &out, PandaCompareFunc fn)
const {
570 case M_greater_equal:
571 out <<
"greater_equal";
593 if (_saved_entry != -1) {
595 _saved_entry = _attribs->
find(
this);
620 _attribs_lock =
new LightReMutex(
"RenderAttrib::_attribs_lock");
657 if (pointer == attrib) {
int find(const Key &key) const
Searches for the indicated key in the table.
static void list_attribs(ostream &out)
Lists all of the RenderAttribs in the cache to the output stream, one per line.
This is the base class for a number of render attributes (other than transform) that may be set on sc...
static int get_num_attribs()
Returns the total number of unique RenderAttrib objects allocated in the world.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
bool debug_is_locked() const
Returns true if the current thread has locked the LightReMutex, false otherwise.
virtual bool unref() const FINAL
Explicitly decrements the reference count.
int get_num_entries() const
Returns the number of active entries in the table.
Base class for objects that can be written to and read from Bam files.
A lightweight reentrant mutex.
This collects together the pieces of data that are accumulated for each node while walking the scene ...
static void init_attribs()
Make sure the global _attribs map is allocated.
A lightweight class that can be used to automatically start and stop a PStatCollector around a sectio...
This template class implements an unordered map of keys to data, implemented as a hashtable...
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
static int garbage_collect()
Performs a garbage-collection cycle.
virtual bool lower_attrib_can_override() const
Intended to be overridden by derived RenderAttrib types to specify how two consecutive RenderAttrib o...
virtual ~RenderAttrib()
The destructor is responsible for removing the RenderAttrib from the global set if it is there...
virtual void fillin(DatagramIterator &scan, BamReader *manager)
This internal function is intended to be called by each class's make_from_bam() method to read in all...
void register_change_this(ChangeThisFunc func, TypedWritable *whom)
Called by an object reading itself from the bam file to indicate that the object pointer that will be...
static Thread * get_current_thread()
Returns a pointer to the currently-executing Thread object.
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
static Thread * get_main_thread()
Returns a pointer to the "main" Thread object–this is the Thread that started the whole process...
A lightweight class that represents a single element that may be timed and/or counted via stats...
bool is_empty() const
Returns true if the table is empty; i.e.
int get_size() const
Returns the total number of slots in the table.
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
virtual bool unref() const
Explicitly decrements the reference count.
void register_finalize(TypedWritable *whom)
Should be called by an object reading itself from the Bam file to indicate that this particular objec...
Similar to MutexHolder, but for a light reentrant mutex.
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
static TypedWritable * change_this(TypedWritable *old_ptr, BamReader *manager)
Called immediately after complete_pointers(), this gives the object a chance to adjust its own pointe...
static bool validate_attribs()
Ensures that the cache is still stored in sorted order.
int compare_to(const RenderAttrib &other) const
Provides an arbitrary ordering among all unique RenderAttribs, so we can store the essentially differ...
virtual bool has_cull_callback() const
Should be overridden by derived classes to return true if cull_callback() has been defined...
virtual void finalize(BamReader *manager)
Called by the BamReader to perform any final actions needed for setting up the object after all objec...
bool validate() const
Returns true if the internal table appears to be consistent, false if there are some internal errors...
A class to retrieve the individual data elements previously stored in a Datagram. ...
TypeHandle is the identifier used to differentiate C++ class types.
int get_ref_count() const
Returns the current reference count.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
void remove_element(int n)
Removes the nth slot from the table.
virtual bool cull_callback(CullTraverser *trav, const CullTraverserData &data) const
If has_cull_callback() returns true, this function will be called during the cull traversal to perfor...
This object performs a depth-first traversal of the scene graph, with optional view-frustum culling...
const Key & get_key(int n) const
Returns the key in the nth slot of the table.
bool has_element(int n) const
Returns true if there is an element stored in the nth slot, false otherwise.