ensureCapacity method

void ensureCapacity(
  1. int wanted
)

Grows the buffer to hold at least wanted instances, keeping what is in it.

For a caller that does not know its size in advance — gfx-67n. The automatic batcher is exactly that: it discovers a run of identical draws while encoding a frame, and the run is as long as the scene and the camera make it. Growing doubles rather than fitting, so a batch that creeps up by one a frame reallocates a handful of times rather than every frame.

Implementation

void ensureCapacity(int wanted) {
  if (wanted <= _capacity) return;
  var grown = _capacity;
  while (grown < wanted) {
    grown *= 2;
  }

  final data = Float32List(grown * floatsPerInstance);
  data.setRange(0, _capacity * floatsPerInstance, _data);
  for (var i = _capacity; i < grown; i++) {
    final at = i * floatsPerInstance;
    data[at] = 1.0;
    data[at + 5] = 1.0;
    data[at + 10] = 1.0;
    data[at + 12] = 1.0;
    data[at + 13] = 1.0;
    data[at + 14] = 1.0;
    data[at + 15] = 1.0;
  }
  _data = data;

  final weights = _weights;
  if (weights != null) {
    _weights = Float32List(grown * maxMorphTargets)
      ..setRange(0, _capacity * maxMorphTargets, weights);
    // The texture is one row per slot, so a taller buffer is a different
    // texture and the cached one no longer describes it.
    _weightsVersion++;
  }

  _capacity = grown;
  _touched();
}