@@ -38,9 +38,7 @@ public class Model {
3838
3939 public String hash ;
4040
41- // The model that owns the texture resources shared by this model and any models derived
42- // from it. For the root (source) model this is itself; for generated models it points to
43- // the ultimate source. refCount is only meaningful on the owner.
41+ // The model where self textures came from, tracked for shared textures dealloc
4442 private Model textureOwner = this ;
4543 private final AtomicInteger refCount = new AtomicInteger (1 );
4644
@@ -228,12 +226,16 @@ public void free() {
228226 }
229227
230228 /**
231- * Decrements the reference count of the shared texture resources, deallocating the texture
229+ * Decrements the reference count of the shared texture resources, and deallocating the texture
232230 * sheets once the last referencing model is released.
233231 */
234232 protected final void tryReleaseTexture () {
235- if (textureOwner .refCount .decrementAndGet () <= 0 ) {
236- textureOwner .deallocTextures ();
233+ Model owner = this ;
234+ while (owner .textureOwner != owner ) {
235+ owner = owner .textureOwner ;
236+ }
237+ if (owner .refCount .decrementAndGet () <= 0 ) {
238+ owner .deallocTextures ();
237239 }
238240 }
239241
@@ -242,8 +244,11 @@ protected final void tryReleaseTexture() {
242244 * models) that reference another model's texture sheets.
243245 */
244246 protected final void shareTexturesWith (Model owner ) {
245- this .textureOwner = owner . textureOwner ;
247+ this .textureOwner = owner ;
246248 this .linkTextures (owner .getTextures (), owner .getSpeculars (), owner .getNormals ());
249+ while (owner .textureOwner != owner ) {
250+ owner = owner .textureOwner ;
251+ }
247252 owner .refCount .getAndIncrement ();
248253 }
249254
0 commit comments