Reference Counting

I frequently run into the situation that I want to share WebGL resources - especially textures - between multiple objects. For example, in the terain and imagery system, a single imagery texture is frequently shared among multiple terrain tiles. WebGL resources, as you probably know, should be freed explicitly rather than left to be managed by the garbage collector. So the best way I know to deal with this is by reference counting. Every time we add another reference to the object, we increment its reference count. When we remove a reference, we decrement the reference count. When the reference count goes to zero, we free the resource. Simple enough.

So my question is: should we add reference counting capability to all of our types that represent WebGL resources? It adds a little bit of cost in terms of object size and runtime cost (for managing the reference count), but I think it’s worth it to save the (common?) client that needs to count references the trouble of doing ad-hoc reference counting on these types. I’ve found myself doing that multiple times now.

Kevin

I wouldn’t address this now. We are going to look at system-wide texture caching and URI-style access as part of models and screen-space rendering.

Chances are reference counting will be in the cache, which most folks use, but not in the texture itself, like the current shader cache. Actually, you’re welcome to do all this if you are up for handling the model and screen-space use cases.

Patrick

I have no choice but to address it now, because I’m sharing textures between tiles in the terrain and imagery system, and there’s no other way to determine when the last tile is done with a texture. The choice is just whether I add reference counting monkey-patch style as I need it (what I’m doing now), add it just to textures, or add it to all WebGL resource objects.

It sounds like you’re saying I should just do the ad-hoc thing for now. That’s fine, and that’s what I thought, too. But I’ve had to do it enough now that it exceeded my DRY threshold awhile ago. Imagery textures? Ok. Terrain index buffers? Alright, fine, let’s keep moving. Water mask? Now maybe we’ve gone too far.

I don’t think texture caching and URI access would help me. But maybe I’m missing something.

Kevin

This sounds OK for now. Let’s revisit when we look at textures again for models. It shouldn’t be more than a month or so.

Patrick