mgpuReadAsyncDouble function
Future<void>
mgpuReadAsyncDouble(
- MGPUBuffer buffer,
- Float64List outputData, {
- int readElements = 0,
- int elementOffset = 0,
Implementation
Future<void> mgpuReadAsyncDouble(
MGPUBuffer buffer,
Float64List outputData, {
int readElements = 0,
int elementOffset = 0,
}) async {
final int bytesPerElem = Float64List.bytesPerElement;
final int elementsToRead = (readElements > 0)
? readElements
: (outputData.length - elementOffset);
final int sizeToAllocate = elementsToRead * bytesPerElem;
if (elementsToRead <= 0 ||
elementOffset < 0 ||
elementOffset >= outputData.length)
return;
final JSNumber ptr = _malloc(sizeToAllocate.toJS);
final int startIndex = ptr.toDartInt ~/ bytesPerElem;
try {
// Add bounds check
if (startIndex < 0 || startIndex + elementsToRead > _heapF64.length) {
throw StateError(
'Float64 buffer read would exceed heap bounds: '
'trying to read $elementsToRead elements at index $startIndex '
'but heap size is ${_heapF64.length}',
);
}
// Assuming C++ function is named mgpuReadSyncFloat64 now
await ccall(
"mgpuReadSyncFloat64".toJS, // Use specific sync C++ function
"void".toJS,
["number", "number", "number", "number"].toJSDeep,
[buffer, ptr, elementsToRead.toJS, elementOffset.toJS].toJSDeep,
{"async": true}.toJSDeep,
).toDart;
final output = _heapF64.sublist(startIndex, startIndex + elementsToRead);
outputData.setAll(elementOffset, output);
} finally {
_free(ptr);
}
}