Forráskód Böngészése

better cache invalidation

David Rose 21 éve
szülő
commit
4915b8f216
2 módosított fájl, 46 hozzáadás és 35 törlés
  1. 24 22
      panda/src/glstuff/glGraphicsStateGuardian_src.cxx
  2. 22 13
      panda/src/gobj/qpgeom.cxx

+ 24 - 22
panda/src/glstuff/glGraphicsStateGuardian_src.cxx

@@ -2537,14 +2537,16 @@ record_deleted_display_list(GLuint index) {
 ////////////////////////////////////////////////////////////////////
 VertexBufferContext *CLP(GraphicsStateGuardian)::
 prepare_vertex_buffer(qpGeomVertexArrayData *data) {
-  if (GLCAT.is_debug()) {
-    GLCAT.debug()
-      << "prepare_vertex_buffer(" << (void *)data << ")\n";
-  }
-
   if (_supports_buffers) {
     CLP(VertexBufferContext) *gvbc = new CLP(VertexBufferContext)(data);
     _glGenBuffers(1, &gvbc->_index);
+
+    if (GLCAT.is_debug()) {
+      GLCAT.debug()
+        << "creating vertex buffer " << gvbc->_index << ": "
+        << data->get_num_vertices() << " vertices " 
+        << *data->get_array_format() << "\n";
+    }
     
     report_my_gl_errors();
     return gvbc;
@@ -2571,7 +2573,8 @@ apply_vertex_buffer(VertexBufferContext *vbc) {
   if (gvbc->was_modified()) {
     if (GLCAT.is_debug()) {
       GLCAT.debug()
-        << "apply_vertex_buffer(" << (void *)vbc->get_data() << ")\n";
+        << "copying " << gvbc->get_data()->get_data_size_bytes()
+        << " bytes into vertex buffer " << gvbc->_index << "\n";
     }
     if (gvbc->changed_size()) {
       _glBufferData(GL_ARRAY_BUFFER, gvbc->get_data()->get_data_size_bytes(),
@@ -2599,15 +2602,14 @@ apply_vertex_buffer(VertexBufferContext *vbc) {
 ////////////////////////////////////////////////////////////////////
 void CLP(GraphicsStateGuardian)::
 release_vertex_buffer(VertexBufferContext *vbc) {
-  if (GLCAT.is_debug()) {
-    GLCAT.debug()
-      << "release_vertex_buffer(" << (void *)vbc->get_data() << ")\n";
-  }
-
   nassertv(_supports_buffers);
   
   CLP(VertexBufferContext) *gvbc = DCAST(CLP(VertexBufferContext), vbc);
 
+  if (GLCAT.is_debug()) {
+    GLCAT.debug()
+      << "deleting vertex buffer " << gvbc->_index << "\n";
+  }
   _glDeleteBuffers(1, &gvbc->_index);
   report_my_gl_errors();
 
@@ -2668,14 +2670,14 @@ setup_array_data(const qpGeomVertexArrayData *data) {
 ////////////////////////////////////////////////////////////////////
 IndexBufferContext *CLP(GraphicsStateGuardian)::
 prepare_index_buffer(qpGeomPrimitive *data) {
-  if (GLCAT.is_debug()) {
-    GLCAT.debug()
-      << "prepare_index_buffer(" << (void *)data << ")\n";
-  }
-
   if (_supports_buffers) {
     CLP(IndexBufferContext) *gibc = new CLP(IndexBufferContext)(data);
     _glGenBuffers(1, &gibc->_index);
+
+    if (GLCAT.is_debug()) {
+      GLCAT.debug()
+        << "creating vertex buffer " << gibc->_index << "\n";
+    }
     
     report_my_gl_errors();
     return gibc;
@@ -2702,7 +2704,8 @@ apply_index_buffer(IndexBufferContext *ibc) {
   if (gibc->was_modified()) {
     if (GLCAT.is_debug()) {
       GLCAT.debug()
-        << "apply_index_buffer(" << (void *)ibc->get_data() << ")\n";
+        << "copying " << gibc->get_data()->get_data_size_bytes()
+        << " bytes into index buffer " << gibc->_index << "\n";
     }
     if (gibc->changed_size()) {
       _glBufferData(GL_ELEMENT_ARRAY_BUFFER, gibc->get_data()->get_data_size_bytes(),
@@ -2730,15 +2733,14 @@ apply_index_buffer(IndexBufferContext *ibc) {
 ////////////////////////////////////////////////////////////////////
 void CLP(GraphicsStateGuardian)::
 release_index_buffer(IndexBufferContext *ibc) {
-  if (GLCAT.is_debug()) {
-    GLCAT.debug()
-      << "release_index_buffer(" << (void *)ibc->get_data() << ")\n";
-  }
-
   nassertv(_supports_buffers);
   
   CLP(IndexBufferContext) *gibc = DCAST(CLP(IndexBufferContext), ibc);
 
+  if (GLCAT.is_debug()) {
+    GLCAT.debug()
+      << "deleting index buffer " << gibc->_index << "\n";
+  }
   _glDeleteBuffers(1, &gibc->_index);
   report_my_gl_errors();
 

+ 22 - 13
panda/src/gobj/qpgeom.cxx

@@ -239,27 +239,36 @@ munge_geom(const qpGeomMunger *munger,
            CPT(qpGeom) &result, CPT(qpGeomVertexData) &data) const {
   // Look up the munger in our cache--maybe we've recently applied it.
   {
-    // Use read() and release_read() instead of CDReader, because the
-    // call to record_geom() might recursively call back into this
-    // object, and require a write.
-    const CData *cdata = _cycler.read();
+    CDReader cdata(_cycler);
     CacheEntry temp_entry(munger);
     temp_entry.ref();  // big ugly hack to allow a stack-allocated ReferenceCount object.
     Cache::const_iterator ci = cdata->_cache.find(&temp_entry);
     if (ci != cdata->_cache.end()) {
-      _cycler.release_read(cdata);
       CacheEntry *entry = (*ci);
 
-      // Record a cache hit, so this element will stay in the cache a
-      // while longer.
-      entry->refresh();
-      result = entry->_geom_result;
-      data = entry->_data_result;
-      temp_entry.unref();
-      return;
+      if (get_modified() <= entry->_geom_result->get_modified() &&
+          get_vertex_data()->get_modified() <= entry->_data_result->get_modified()) {
+        // The cache entry is still good; use it.
+
+        // Record a cache hit, so this element will stay in the cache a
+        // while longer.
+        entry->refresh();
+        result = entry->_geom_result;
+        data = entry->_data_result;
+        temp_entry.unref();
+        return;
+      }
+
+      // The cache entry is stale, remove it.
+      if (gobj_cat.is_debug()) {
+        gobj_cat.debug()
+          << "Cache entry " << *entry << " is stale, removing.\n";
+      }
+      entry->erase();
+      CDWriter cdataw(((qpGeom *)this)->_cycler, cdata);
+      cdataw->_cache.erase(entry);
     }
     temp_entry.unref();
-    _cycler.release_read(cdata);
   }
 
   // Ok, invoke the munger.