Advertisement
default_ex

BufferObject.SetData

Jul 25th, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1. public void SetData(DataType[] source, int sourceIndex = 0, int destIndex = 0, int count = -1)
  2. {
  3. #if !RUNS_WITH_SCISSORS
  4.     if (source == null)
  5.         throw new ArgumentNullException("source");
  6.  
  7.     if (sourceIndex < 0 || sourceIndex >= source.Length)
  8.         throw new ArgumentOutOfRangeException("sourceIndex",
  9.             "The source index must be greater-than or equal to 0 and less-than the source length.");
  10.            
  11.     if (destIndex < 0 || destIndex >= m_count)
  12.         throw new ArgumentOutOfRangeException("destIndex",
  13.             "The dest index must be greater-than or equal to 0 and less-than the buffer's count.");
  14. #endif
  15.     if (count == -1)
  16.         count = Math.Min(source.Length - sourceIndex, m_count - destIndex);
  17. #if !RUNS_WITH_SCISSORS
  18.     else if (source.Length - sourceIndex < count)
  19.         throw new ArgumentOutOfRangeException("count",
  20.             "The count specified is larger than the source array.");
  21.     else if (m_count - destIndex < count)
  22.         throw new ArgumentOutOfRangeException("count",
  23.             "The count specified is larger than the buffer's count.");
  24. #endif
  25.     IntPtr offset = new IntPtr(m_stride * destIndex);
  26.     IntPtr size = new IntPtr(m_stride * count);
  27.     int sourceOffset = sourceIndex * m_stride;
  28.     int destOffset = destIndex * m_stride;
  29.  
  30.     GL.BindBuffer(m_target, m_ID);
  31. #if ES20
  32.     GL.BufferSubData(m_target, offset, size, source);
  33. #else
  34.     IntPtr videoMemory = GL.MapBufferRange(m_target, offset, size, BufferAccessMask.MapWriteBit);
  35.     if (videoMemory == IntPtr.Zero)
  36.     {
  37.         videoMemory = GL.MapBuffer(m_target, BufferAccess.WriteOnly);
  38.         if (videoMemory == IntPtr.Zero)
  39.         {
  40.             throw new InvalidOperationException(
  41.                 "Unable to map video memory.", new Exception(GL.GetError().ToString()));
  42.         }
  43.     }
  44.     else
  45.         destOffset = 0; // mapped the range, no need for an offset
  46.  
  47.     GCHandle dataHandle = GCHandle.Alloc(source, GCHandleType.Pinned);
  48.     IntPtr systemMemory = dataHandle.AddrOfPinnedObject();
  49.     unsafe
  50.     {
  51.         byte* sourcePtr = ((byte*)systemMemory) + sourceOffset;
  52.         byte* destPtr = ((byte*)videoMemory) + destOffset;
  53.         for (int i = 0; i < count * m_stride; i++) destPtr[i] = sourcePtr[i];
  54.     }
  55.     dataHandle.Free();
  56.  
  57.     GL.UnmapBuffer(m_target);
  58. #endif
  59.     GL.BindBuffer(m_target, 0);
  60.  
  61.     if (m_shadowCopy)
  62.         Array.Copy(source, sourceIndex, m_data, destIndex, count);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement