diff --git a/src/objects/mesh.nim b/src/objects/mesh.nim index 046f3bb..4a7ccf0 100644 --- a/src/objects/mesh.nim +++ b/src/objects/mesh.nim @@ -420,21 +420,22 @@ proc ensure_capacity*(self: Mesh, extra_elements: int) = if self.data == nil: let stride = self.layout.stride assert stride != 0 - self.load_from_va_ia @[newArrRef[float32](4*stride*extra_elements)] + let stride4 = stride div 4 # note: assuming layout.stride is aligned + self.load_from_va_ia @[newArrRef[float32](extra_elements * stride4)] self.data.num_indices[0] = 0 return let varray = self.data.varrays[0] - let bytelen = varray.byteLen + let len = varray.len let current_index = self.data.num_indices[0] let stride = self.data.stride let index = current_index + extra_elements - let fpos = (stride div 4) * index - if fpos >= bytelen: - var cap = bytelen * 2 + let fpos = stride * index * 4 + if fpos >= len: + var cap = len * 2 while fpos >= cap: cap *= 2 var va = newArrRef[float32](cap) - copyMem(va.toPointer, varray.toPointer, bytelen) + copyMem(va.toPointer, varray.toPointer, len * sizeof(float32)) self.load_from_va_ia @[va] self.data.num_indices[0] = current_index