update method

dynamic update(
  1. dynamic object
)

Implementation

update(object) {
  var textures = this.textures;

  var data = this.get(object);

  var bindings = data["bindings"];

  var updateMap = this.updateMap;
  var frame = this.info.render["frame"];

  var needsBindGroupRefresh = false;

  // iterate over all bindings and check if buffer updates or a new binding group is required

  for (var binding in bindings) {
    var isShared = binding.isShared;
    var isUpdated = updateMap.get(binding) == frame;

    if (isShared && isUpdated) continue;
    if (binding is WebGPUUniformBuffer) {
      var buffer = binding.getBuffer();
      var bufferGPU = binding.bufferGPU!;

      var needsBufferWrite = binding.update();

      if (needsBufferWrite == true) {
        if (buffer is Float32Array) {
          this.device.queue.writeBuffer(bufferGPU, 0, buffer.toDartList(), buffer.lengthInBytes);
        } else {
          this.device.queue.writeBuffer(bufferGPU, 0, buffer, buffer.lengthInBytes);
        }
      }
    } else if (binding.isStorageBuffer) {
      var attribute = binding.attribute;
      this.attributes.update(attribute, false, binding.usage);
    } else if (binding.isSampler) {
      var texture = binding.getTexture();

      textures.updateSampler(texture);

      var samplerGPU = textures.getSampler(texture);

      if (binding.samplerGPU != samplerGPU) {
        binding.samplerGPU = samplerGPU;
        needsBindGroupRefresh = true;
      }
    } else if (binding.isSampledTexture) {
      var texture = binding.getTexture();

      var needsTextureRefresh = textures.updateTexture(texture);
      var textureGPU = textures.getTextureGPU(texture);

      if (textureGPU != undefined && binding.textureGPU != textureGPU ||
          needsTextureRefresh == true) {
        binding.textureGPU = textureGPU;
        needsBindGroupRefresh = true;
      }
    }

    updateMap.set(binding, frame);
  }

  if (needsBindGroupRefresh == true) {
    data["group"] = this._createBindGroup(bindings, data["layout"]);
  }
}