Mesh: Fix wrong calculations in ensure_capacity.

This commit is contained in:
Alberto Torres 2025-03-18 18:38:45 +01:00
parent f45e1757f6
commit ba70b1addb

View file

@ -420,21 +420,22 @@ proc ensure_capacity*(self: Mesh, extra_elements: int) =
if self.data == nil: if self.data == nil:
let stride = self.layout.stride let stride = self.layout.stride
assert stride != 0 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 self.data.num_indices[0] = 0
return return
let varray = self.data.varrays[0] let varray = self.data.varrays[0]
let bytelen = varray.byteLen let len = varray.len
let current_index = self.data.num_indices[0] let current_index = self.data.num_indices[0]
let stride = self.data.stride let stride = self.data.stride
let index = current_index + extra_elements let index = current_index + extra_elements
let fpos = (stride div 4) * index let fpos = stride * index * 4
if fpos >= bytelen: if fpos >= len:
var cap = bytelen * 2 var cap = len * 2
while fpos >= cap: while fpos >= cap:
cap *= 2 cap *= 2
var va = newArrRef[float32](cap) 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.load_from_va_ia @[va]
self.data.num_indices[0] = current_index self.data.num_indices[0] = current_index