When working with Persistent mapped buffers in C# and OpenTK, you end up needing to use unsafe operations and need to do something like:
WaitSyncStatus waitReturn = WaitSyncStatus.TimeoutExpired;
while (waitReturn != WaitSyncStatus.AlreadySignaled && waitReturn != WaitSyncStatus.ConditionSatisfied)
{
waitReturn = GL.ClientWaitSync(syncObj, ClientWaitSyncFlags.SyncFlushCommandsBit, 1);
}
//Write the data
unsafe
{
fixed (uint* SystemMemory = &data[0])
{
uint* VideoMemory = (uint*)mappedPtr.ToPointer();
for (int i = offset; i < ((length == -1) ? data.Length : length); i++)
VideoMemory[i] = SystemMemory[i - offset]; // simulate what GL.BufferData would do
}
}
// lock the buffer:
GL.DeleteSync(syncObj);
syncObj = GL.FenceSync(SyncCondition.SyncGpuCommandsComplete, WaitSyncFlags.None);
I'm thinking that it might be faster to invoke some native code here to read the data directly into the buffer for instance, by fread-ing to it. Would that perhaps be faster than the way I'm currently forced to do it? It feels like just looping over the array is the worst way to do it.