resident.ts
examples/sogs/resident.ts · raw · open the example · all examples · site home
Exports: Resident
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
// GPU arrays shared by splat objects: freed once the owner drops them and
// every object holding them has been released (Radix releases an object
// when no live collection, including a CPU sort's in-flight orders, holds it)
export class Resident {
private holds = 0;
private dropped = false;
private freed = false;
private readonly free: () => void;
constructor(free: () => void) {
this.free = free;
}
// A release callback for one object that draws these arrays
hold(): () => void {
this.holds += 1;
let released = false;
return () => {
if (!released) {
released = true;
this.holds -= 1;
this.check();
}
};
}
drop() {
this.dropped = true;
this.check();
}
private check() {
if (this.dropped && this.holds === 0 && !this.freed) {
this.freed = true;
this.free();
}
}
}