Browse Source

Fix ref counting of instanced primitives

trethaller 6 years ago
parent
commit
a89412d3be
1 changed files with 24 additions and 1 deletions
  1. 24 1
      h3d/prim/Instanced.hx

+ 24 - 1
h3d/prim/Instanced.hx

@@ -6,6 +6,7 @@ class Instanced extends MeshPrimitive {
 	public var offset : h3d.col.Sphere;
 	var baseBounds : h3d.col.Bounds;
 	var tmpBounds : h3d.col.Bounds;
+	var primitive : MeshPrimitive;
 
 	public function new() {
 		offset = new h3d.col.Sphere();
@@ -13,8 +14,14 @@ class Instanced extends MeshPrimitive {
 	}
 
 	public function setMesh( m : MeshPrimitive ) {
+		if(refCount > 0) {
+			if(primitive != null)
+				primitive.decref();
+			m.incref();
+		}
+		primitive = m;
 		var engine = h3d.Engine.getCurrent();
-		if( m.buffer == null ) m.alloc(engine);
+		if( m.buffer == null || m.buffer.isDisposed() ) m.alloc(engine);
 		buffer = m.buffer;
 		indexes = m.indexes;
 		baseBounds = m.getBounds();
@@ -25,6 +32,22 @@ class Instanced extends MeshPrimitive {
 		}
 	}
 
+	override function dispose() {
+		// Not owning any resources
+	}
+
+	override function incref() {
+		if(refCount == 0 && primitive != null)
+			primitive.incref();
+		super.incref();
+	}
+
+	override function decref() {
+		super.decref();
+		if(refCount == 0 && primitive != null)
+			primitive.decref();
+	}
+
 	override function getBounds():h3d.col.Bounds {
 		tmpBounds.load(baseBounds);
 		var r = offset.r;