Skip to content

Commit 2be4fb4

Browse files
ref: use ref count for texture freeing
1 parent 07ce017 commit 2be4fb4

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

src/main/java/cam72cam/mod/model/common/mesh/GeneratedModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import java.util.function.Supplier;
99

1010
/**
11-
* Internal, don't use directly
11+
* Generated model with no group data
1212
*/
1313
public final class GeneratedModel extends Model {
1414
public GeneratedModel(Model base, Identifier loc, Supplier<float[]> vboSupplier) {

src/main/java/cam72cam/mod/model/common/mesh/Model.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)