<commit_before>part of chronosgl;

WEBGL.Buffer CreateAndInitializeArrayBufferFloat32(
    WEBGL.RenderingContext gl, Float32List data) {
  WEBGL.Buffer b = gl.createBuffer();
  gl.bindBuffer(WEBGL.ARRAY_BUFFER, b);
  gl.bufferDataTyped(WEBGL.ARRAY_BUFFER, data, WEBGL.STATIC_DRAW);
  return b;
}

WEBGL.Buffer CreateAndInitializeArrayElementBufferUint32(
    WEBGL.RenderingContext gl, Uint32List data) {
  WEBGL.Buffer b = gl.createBuffer();
  gl.bindBuffer(WEBGL.ELEMENT_ARRAY_BUFFER, b);
  gl.bufferDataTyped(WEBGL.ELEMENT_ARRAY_BUFFER, data, WEBGL.STATIC_DRAW);
  return b;
}

WEBGL.Buffer CreateAndInitializeArrayElementBufferUint16(
    WEBGL.RenderingContext gl, Uint16List data) {
  WEBGL.Buffer b = gl.createBuffer();
  gl.bindBuffer(WEBGL.ELEMENT_ARRAY_BUFFER, b);
  gl.bufferDataTyped(WEBGL.ELEMENT_ARRAY_BUFFER, data, WEBGL.STATIC_DRAW);
  return b;
}

class Mesh extends Node {
  WEBGL.RenderingContext gl;
  bool debug = false;

  Map<String, WEBGL.Buffer> _buffers = {};
  WEBGL.Buffer _indexBuffer = null;
  int numItems;
  int numInstances = 0;
  Material material;
  MeshData _meshData;

  void AddBuffer(String canonical, Float32List data) {
    if (debug) print ("AddBuffer ${canonical} ${data.length}");
    _buffers[canonical] = CreateAndInitializeArrayBufferFloat32(gl, data);
  }

  void ChangeBuffer(String canonical, Float32List data) {
    if (debug) print ("ChangeBuffer ${canonical} ${data.length}");
    gl.bindBuffer(WEBGL.ARRAY_BUFFER, _buffers[canonical]);
    gl.bufferDataTyped(WEBGL.ARRAY_BUFFER, data, WEBGL.DYNAMIC_DRAW);
  }

  Mesh(this._meshData, this.material, {this.debug: false}) {
    //if (!meshData.isOptimized) meshData.optimize();
    _meshData.SanityCheck();
    gl = ChronosGL.globalGL;
    AddBuffer(
        aVertexPosition, new Float32List.fromList(_meshData.GetVertexData()));

    for (String canonical in _meshData.GetAttributes()) {
      AddBuffer(canonical,
          new Float32List.fromList(_meshData.GetAttributeData(canonical)));
    }

    List<int> indices = _meshData.GetVertexIndices();
    if (indices != null) {
      numItems = indices.length;
      if (ChronosGL.useElementIndexUint) {
        _indexBuffer = CreateAndInitializeArrayElementBufferUint32(
            gl, new Uint32List.fromList(indices));
      } else {
        _indexBuffer = CreateAndInitializeArrayElementBufferUint16(
            gl, new Uint16List.fromList(indices));<commit_msg>add support for updating a mesh's data<commit_after>part of chronosgl;

void ChangeArrayBuffer(
    WEBGL.RenderingContext gl, WEBGL.Buffer buffer, Float32List data) {
  gl.bindBuffer(WEBGL.ARRAY_BUFFER, buffer);
  gl.bufferDataTyped(WEBGL.ARRAY_BUFFER, data, WEBGL.STATIC_DRAW);
}

WEBGL.Buffer CreateAndInitializeArrayBuffer(
    WEBGL.RenderingContext gl, Float32List data) {
  WEBGL.Buffer b = gl.createBuffer();
  ChangeArrayBuffer(gl, b, data);
  return b;
}

void ChangeElementArrayBuffer(
    WEBGL.RenderingContext gl, WEBGL.Buffer buffer, TypedData data) {
  gl.bindBuffer(WEBGL.ELEMENT_ARRAY_BUFFER, buffer);
  gl.bufferDataTyped(WEBGL.ELEMENT_ARRAY_BUFFER, data, WEBGL.STATIC_DRAW);
}

WEBGL.Buffer CreateAndInitializeElementArrayBuffer(
    WEBGL.RenderingContext gl, TypedData data) {
  assert((data is Uint16List) || (data is Uint32List));
  WEBGL.Buffer b = gl.createBuffer();
  ChangeElementArrayBuffer(gl, b, data);
  return b;
}

class Mesh extends Node {
  WEBGL.RenderingContext gl;
  bool debug = false;

  Map<String, WEBGL.Buffer> _buffers = {};
  WEBGL.Buffer _indexBuffer = null;
  int numItems;
  int numInstances = 0;
  Material material;
  MeshData _meshData;

  void AddBuffer(String canonical, Float32List data) {
    if (debug) print("AddBuffer ${canonical} ${data.length}");
    _buffers[canonical] = CreateAndInitializeArrayBuffer(gl, data);
  }

  void ChangeBufferCanonical(String canonical, Float32List data) {
    if (debug) print("ChangeBuffer ${canonical} ${data.length}");
    ChangeArrayBuffer(gl, _buffers[canonical], data);
  }

  Mesh(this._meshData, this.material, {this.debug: false}) {
    //if (!meshData.isOptimized) meshData.optimize();
    _meshData.SanityCheck();
    gl = ChronosGL.globalGL;
    AddBuffer(
        aVertexPosition, new Float32List.fromList(_meshData.GetVertexData()));

    for (String canonical in _meshData.GetAttributes()) {
      AddBuffer(canonical,
          new Float32List.fromList(_meshData.GetAttributeData(canonical)));
    }

    List<int> indices = _meshData.GetVertexIndices();
    if (indices != null) {
      numItems = indices.length;
      if (ChronosGL.useElementIndexUint) {
        _indexBuffer = CreateAndInitializeElementArrayBuffer(
            gl, new Uint32List.fromList(indices));
      } else {
        _indexBuffer = CreateAndInitializeElementArrayBuffer(
            gl, new Uint16List.fromList(indices));
      }
    } else {
      numItems = _meshData.GetVertexData().length ~/ 3;
    }
  }

  void UpdateMeshData(MeshData md) {
    _meshData = md;
    ChangeBufferCanonical(
        aVertexPosition, new Float32List.fromList(_meshData.GetVertexData()));

    for (String canonical in _meshData.GetAttributes()) {
      ChangeBufferCanonical(canonical,
          new Float32List.fromList(_meshData.GetAttributeData(canonical)));
    }
    List<int> indices = _meshData.GetVertexIndices();
    if (indices != null) {
      numItems = indices.length;
      if (ChronosGL.useElementIndexUint) {
        ChangeElementArrayBuffer(
            gl, _indexBuffer, new Uint32List.fromList(indices));
      } else {
        ChangeElementArrayBuffer(
            gl, _indexBuffer, new Uint16List.fromList(indices));
{'fraction_non_alphanumeric': 0.10746003552397869, 'fraction_numerical': 0.0108348134991119, 'mean_word_length': 4.142465753424657, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2834191', 'n_tokens_mistral': 1856, 'n_tokens_neox': 1813, 'n_words': 351}