Browse Source

gobj: reduce unnecessary use of threading primitives in constructors

rdb 8 years ago
parent
commit
39abc66025

+ 8 - 13
panda/src/gobj/geom.I

@@ -512,22 +512,17 @@ CData() :
  *
  *
  */
  */
 INLINE Geom::CData::
 INLINE Geom::CData::
-CData(const Geom::CData &copy) :
-  _data(copy._data),
-  _primitives(copy._primitives),
-  _primitive_type(copy._primitive_type),
-  _shade_model(copy._shade_model),
-  _geom_rendering(copy._geom_rendering),
-  _modified(copy._modified),
-  _internal_bounds(copy._internal_bounds),
-  _nested_vertices(copy._nested_vertices),
-  _internal_bounds_stale(copy._internal_bounds_stale),
-  _bounds_type(copy._bounds_type),
-  _user_bounds(copy._user_bounds)
+CData(GeomVertexData *data) :
+  _data(data),
+  _primitive_type(PT_none),
+  _shade_model(SM_uniform),
+  _geom_rendering(0),
+  _nested_vertices(0),
+  _internal_bounds_stale(true),
+  _bounds_type(BoundingVolume::BT_default)
 {
 {
 }
 }
 
 
-
 /**
 /**
  *
  *
  */
  */

+ 13 - 21
panda/src/gobj/geom.cxx

@@ -46,13 +46,7 @@ make_cow_copy() {
  *
  *
  */
  */
 Geom::
 Geom::
-Geom(const GeomVertexData *data) {
-  // Let's ensure the vertex data gets set on all stages at once.
-  OPEN_ITERATE_ALL_STAGES(_cycler) {
-    CDStageWriter cdata(_cycler, pipeline_stage);
-    cdata->_data = (GeomVertexData *)data;
-  }
-  CLOSE_ITERATE_ALL_STAGES(_cycler);
+Geom(const GeomVertexData *data) : _cycler(CData((GeomVertexData *)data)) {
 }
 }
 
 
 /**
 /**
@@ -631,7 +625,7 @@ unify_in_place(int max_indices, bool preserve_order) {
     } else {
     } else {
       // We have already encountered another primitive of this type.  Combine
       // We have already encountered another primitive of this type.  Combine
       // them.
       // them.
-      combine_primitives((*npi).second, primitive, current_thread);
+      combine_primitives((*npi).second, move(primitive), current_thread);
     }
     }
   }
   }
 
 
@@ -1491,31 +1485,29 @@ reset_geom_rendering(Geom::CData *cdata) {
  * is modified to append the vertices from b_prim, which is unmodified.
  * is modified to append the vertices from b_prim, which is unmodified.
  */
  */
 void Geom::
 void Geom::
-combine_primitives(GeomPrimitive *a_prim, const GeomPrimitive *b_prim,
+combine_primitives(GeomPrimitive *a_prim, CPT(GeomPrimitive) b_prim,
                    Thread *current_thread) {
                    Thread *current_thread) {
   nassertv(a_prim != b_prim);
   nassertv(a_prim != b_prim);
   nassertv(a_prim->get_type() == b_prim->get_type());
   nassertv(a_prim->get_type() == b_prim->get_type());
 
 
-  CPT(GeomPrimitive) b_prim2 = b_prim;
-
-  if (a_prim->get_index_type() != b_prim2->get_index_type()) {
-    GeomPrimitive::NumericType index_type = max(a_prim->get_index_type(), b_prim2->get_index_type());
+  if (a_prim->get_index_type() != b_prim->get_index_type()) {
+    GeomPrimitive::NumericType index_type = max(a_prim->get_index_type(), b_prim->get_index_type());
     a_prim->set_index_type(index_type);
     a_prim->set_index_type(index_type);
-    if (b_prim2->get_index_type() != index_type) {
-      PT(GeomPrimitive) b_prim_copy = b_prim2->make_copy();
+    if (b_prim->get_index_type() != index_type) {
+      PT(GeomPrimitive) b_prim_copy = b_prim->make_copy();
       b_prim_copy->set_index_type(index_type);
       b_prim_copy->set_index_type(index_type);
-      b_prim2 = b_prim_copy;
+      b_prim = b_prim_copy;
     }
     }
   }
   }
 
 
-  if (!b_prim2->is_indexed()) {
-    PT(GeomPrimitive) b_prim_copy = b_prim2->make_copy();
+  if (!b_prim->is_indexed()) {
+    PT(GeomPrimitive) b_prim_copy = b_prim->make_copy();
     b_prim_copy->make_indexed();
     b_prim_copy->make_indexed();
-    b_prim2 = b_prim_copy;
+    b_prim = b_prim_copy;
   }
   }
 
 
   PT(GeomVertexArrayData) a_vertices = a_prim->modify_vertices();
   PT(GeomVertexArrayData) a_vertices = a_prim->modify_vertices();
-  CPT(GeomVertexArrayData) b_vertices = b_prim2->get_vertices();
+  CPT(GeomVertexArrayData) b_vertices = b_prim->get_vertices();
 
 
   if (a_prim->requires_unused_vertices()) {
   if (a_prim->requires_unused_vertices()) {
     GeomVertexReader index(b_vertices, 0);
     GeomVertexReader index(b_vertices, 0);
@@ -1536,7 +1528,7 @@ combine_primitives(GeomPrimitive *a_prim, const GeomPrimitive *b_prim,
   if (a_prim->is_composite()) {
   if (a_prim->is_composite()) {
     // Also copy the ends array.
     // Also copy the ends array.
     PTA_int a_ends = a_prim->modify_ends();
     PTA_int a_ends = a_prim->modify_ends();
-    CPTA_int b_ends = b_prim2->get_ends();
+    CPTA_int b_ends = b_prim->get_ends();
     for (size_t i = 0; i < b_ends.size(); ++i) {
     for (size_t i = 0; i < b_ends.size(); ++i) {
       a_ends.push_back(b_ends[i] + orig_a_vertices);
       a_ends.push_back(b_ends[i] + orig_a_vertices);
     }
     }

+ 3 - 2
panda/src/gobj/geom.h

@@ -196,7 +196,7 @@ private:
 
 
   void reset_geom_rendering(CData *cdata);
   void reset_geom_rendering(CData *cdata);
 
 
-  void combine_primitives(GeomPrimitive *a_prim, const GeomPrimitive *b_prim,
+  void combine_primitives(GeomPrimitive *a_prim, CPT(GeomPrimitive) b_prim,
                           Thread *current_thread);
                           Thread *current_thread);
 
 
 private:
 private:
@@ -302,7 +302,8 @@ private:
   class EXPCL_PANDA_GOBJ CData : public CycleData {
   class EXPCL_PANDA_GOBJ CData : public CycleData {
   public:
   public:
     INLINE CData();
     INLINE CData();
-    INLINE CData(const CData &copy);
+    INLINE CData(GeomVertexData *data);
+
     ALLOC_DELETED_CHAIN(CData);
     ALLOC_DELETED_CHAIN(CData);
     virtual CycleData *make_copy() const;
     virtual CycleData *make_copy() const;
     virtual void write_datagram(BamWriter *manager, Datagram &dg) const;
     virtual void write_datagram(BamWriter *manager, Datagram &dg) const;

+ 14 - 2
panda/src/gobj/geomVertexArrayData.I

@@ -229,8 +229,20 @@ mark_used() {
  *
  *
  */
  */
 INLINE GeomVertexArrayData::CData::
 INLINE GeomVertexArrayData::CData::
-CData() :
-  _usage_hint(UH_unspecified),
+CData(UsageHint usage_hint) :
+  _usage_hint(usage_hint),
+  _rw_lock("GeomVertexArrayData::CData::_rw_lock")
+{
+}
+
+/**
+ *
+ */
+INLINE GeomVertexArrayData::CData::
+CData(GeomVertexArrayData::CData &&from) NOEXCEPT :
+  _usage_hint(move(from._usage_hint)),
+  _buffer(move(from._buffer)),
+  _modified(move(from._modified)),
   _rw_lock("GeomVertexArrayData::CData::_rw_lock")
   _rw_lock("GeomVertexArrayData::CData::_rw_lock")
 {
 {
 }
 }

+ 5 - 12
panda/src/gobj/geomVertexArrayData.cxx

@@ -77,16 +77,10 @@ GeomVertexArrayData::
 GeomVertexArrayData(const GeomVertexArrayFormat *array_format,
 GeomVertexArrayData(const GeomVertexArrayFormat *array_format,
                     GeomVertexArrayData::UsageHint usage_hint) :
                     GeomVertexArrayData::UsageHint usage_hint) :
   SimpleLruPage(0),
   SimpleLruPage(0),
-  _array_format(array_format)
+  _array_format(array_format),
+  _cycler(CData(usage_hint)),
+  _contexts(nullptr)
 {
 {
-  OPEN_ITERATE_ALL_STAGES(_cycler) {
-    CDStageWriter cdata(_cycler, pipeline_stage);
-    cdata->_usage_hint = usage_hint;
-  }
-  CLOSE_ITERATE_ALL_STAGES(_cycler);
-
-  _contexts = NULL;
-
   set_lru_size(0);
   set_lru_size(0);
   nassertv(_array_format->is_registered());
   nassertv(_array_format->is_registered());
 }
 }
@@ -99,10 +93,9 @@ GeomVertexArrayData(const GeomVertexArrayData &copy) :
   CopyOnWriteObject(copy),
   CopyOnWriteObject(copy),
   SimpleLruPage(copy),
   SimpleLruPage(copy),
   _array_format(copy._array_format),
   _array_format(copy._array_format),
-  _cycler(copy._cycler)
+  _cycler(copy._cycler),
+  _contexts(nullptr)
 {
 {
-  _contexts = NULL;
-
   copy.mark_used_lru();
   copy.mark_used_lru();
 
 
   set_lru_size(get_data_size_bytes());
   set_lru_size(get_data_size_bytes());

+ 2 - 1
panda/src/gobj/geomVertexArrayData.h

@@ -150,7 +150,8 @@ private:
   // This is the data that must be cycled between pipeline stages.
   // This is the data that must be cycled between pipeline stages.
   class EXPCL_PANDA_GOBJ CData : public CycleData {
   class EXPCL_PANDA_GOBJ CData : public CycleData {
   public:
   public:
-    INLINE CData();
+    INLINE CData(UsageHint usage_hint = UH_unspecified);
+    INLINE CData(CData &&from) NOEXCEPT;
     INLINE CData(const CData &copy);
     INLINE CData(const CData &copy);
     INLINE void operator = (const CData &copy);
     INLINE void operator = (const CData &copy);
 
 

+ 7 - 10
panda/src/gobj/geomVertexData.I

@@ -583,17 +583,14 @@ CData() :
  *
  *
  */
  */
 INLINE GeomVertexData::CData::
 INLINE GeomVertexData::CData::
-CData(const GeomVertexData::CData &copy) :
-  _usage_hint(copy._usage_hint),
-  _format(copy._format),
-  _arrays(copy._arrays),
-  _transform_table(copy._transform_table),
-  _transform_blend_table(copy._transform_blend_table),
-  _slider_table(copy._slider_table),
-  _animated_vertices(copy._animated_vertices),
-  _animated_vertices_modified(copy._animated_vertices_modified),
-  _modified(copy._modified)
+CData(const GeomVertexFormat *format, GeomVertexData::UsageHint usage_hint) :
+  _format(format),
+  _usage_hint(usage_hint)
 {
 {
+  size_t num_arrays = format->get_num_arrays();
+  for (size_t i = 0; i < num_arrays; ++i) {
+    _arrays.push_back(new GeomVertexArrayData(format->get_array(i), usage_hint));
+  }
 }
 }
 
 
 /**
 /**

+ 2 - 16
panda/src/gobj/geomVertexData.cxx

@@ -67,24 +67,10 @@ GeomVertexData(const string &name,
   _char_pcollector(PStatCollector(_animation_pcollector, name)),
   _char_pcollector(PStatCollector(_animation_pcollector, name)),
   _skinning_pcollector(_char_pcollector, "Skinning"),
   _skinning_pcollector(_char_pcollector, "Skinning"),
   _morphs_pcollector(_char_pcollector, "Morphs"),
   _morphs_pcollector(_char_pcollector, "Morphs"),
-  _blends_pcollector(_char_pcollector, "Calc blends")
+  _blends_pcollector(_char_pcollector, "Calc blends"),
+  _cycler(GeomVertexData::CData(format, usage_hint))
 {
 {
   nassertv(format->is_registered());
   nassertv(format->is_registered());
-
-  // Create some empty arrays as required by the format.  Let's ensure the
-  // vertex data gets set on all stages at once.
-  OPEN_ITERATE_ALL_STAGES(_cycler) {
-    CDStageWriter cdata(_cycler, pipeline_stage);
-    cdata->_format = format;
-    cdata->_usage_hint = usage_hint;
-    int num_arrays = format->get_num_arrays();
-    for (int i = 0; i < num_arrays; i++) {
-      PT(GeomVertexArrayData) array = new GeomVertexArrayData
-        (format->get_array(i), usage_hint);
-      cdata->_arrays.push_back(array.p());
-    }
-  }
-  CLOSE_ITERATE_ALL_STAGES(_cycler);
 }
 }
 
 
 /**
 /**

+ 2 - 1
panda/src/gobj/geomVertexData.h

@@ -293,7 +293,8 @@ private:
   class EXPCL_PANDA_GOBJ CData : public CycleData {
   class EXPCL_PANDA_GOBJ CData : public CycleData {
   public:
   public:
     INLINE CData();
     INLINE CData();
-    INLINE CData(const CData &copy);
+    INLINE CData(const GeomVertexFormat *format, UsageHint usage_hint);
+
     ALLOC_DELETED_CHAIN(CData);
     ALLOC_DELETED_CHAIN(CData);
     virtual CycleData *make_copy() const;
     virtual CycleData *make_copy() const;
     virtual void write_datagram(BamWriter *manager, Datagram &dg) const;
     virtual void write_datagram(BamWriter *manager, Datagram &dg) const;

+ 0 - 7
panda/src/pipeline/cycleData.I

@@ -10,10 +10,3 @@
  * @author drose
  * @author drose
  * @date 2002-02-21
  * @date 2002-02-21
  */
  */
-
-/**
- *
- */
-INLINE CycleData::
-CycleData() {
-}

+ 3 - 1
panda/src/pipeline/cycleData.h

@@ -49,7 +49,9 @@ class EXPCL_PANDA_PIPELINE CycleData
 #endif  // DO_PIPELINING
 #endif  // DO_PIPELINING
 {
 {
 public:
 public:
-  INLINE CycleData();
+  INLINE CycleData() DEFAULT_CTOR;
+  INLINE CycleData(CycleData &&from) DEFAULT_CTOR;
+  INLINE CycleData(const CycleData &copy) DEFAULT_CTOR;
   virtual ~CycleData();
   virtual ~CycleData();
 
 
   virtual CycleData *make_copy() const=0;
   virtual CycleData *make_copy() const=0;

+ 21 - 0
panda/src/pipeline/pipelineCycler.I

@@ -25,6 +25,16 @@ PipelineCycler(Pipeline *pipeline) :
 {
 {
 }
 }
 
 
+/**
+ *
+ */
+template<class CycleDataType>
+INLINE PipelineCycler<CycleDataType>::
+PipelineCycler(CycleDataType &&initial_data, Pipeline *pipeline) :
+  PipelineCyclerBase(new CycleDataType(move(initial_data)), pipeline)
+{
+}
+
 /**
 /**
  *
  *
  */
  */
@@ -182,6 +192,17 @@ PipelineCycler(Pipeline *pipeline) :
 {
 {
 }
 }
 
 
+/**
+ *
+ */
+template<class CycleDataType>
+INLINE PipelineCycler<CycleDataType>::
+PipelineCycler(CycleDataType &&initial_data, Pipeline *pipeline) :
+  _typed_data(move(initial_data)),
+  PipelineCyclerBase(&_typed_data, pipeline)
+{
+}
+
 /**
 /**
  *
  *
  */
  */

+ 3 - 1
panda/src/pipeline/pipelineCycler.h

@@ -45,7 +45,9 @@
 template<class CycleDataType>
 template<class CycleDataType>
 struct PipelineCycler : public PipelineCyclerBase {
 struct PipelineCycler : public PipelineCyclerBase {
 public:
 public:
-  INLINE PipelineCycler(Pipeline *pipeline = NULL);
+  INLINE PipelineCycler(Pipeline *pipeline = nullptr);
+  INLINE PipelineCycler(CycleDataType &&initial_data, Pipeline *pipeline = nullptr);
+
   INLINE PipelineCycler(const PipelineCycler<CycleDataType> &copy);
   INLINE PipelineCycler(const PipelineCycler<CycleDataType> &copy);
   INLINE void operator = (const PipelineCycler<CycleDataType> &copy);
   INLINE void operator = (const PipelineCycler<CycleDataType> &copy);