diff --git a/MonoGame.Framework/Graphics/Vertices/DynamicVertexBuffer.cs b/MonoGame.Framework/Graphics/Vertices/DynamicVertexBuffer.cs index dd171cde143..5672c08406c 100644 --- a/MonoGame.Framework/Graphics/Vertices/DynamicVertexBuffer.cs +++ b/MonoGame.Framework/Graphics/Vertices/DynamicVertexBuffer.cs @@ -4,6 +4,10 @@ namespace Microsoft.Xna.Framework.Graphics { public class DynamicVertexBuffer : VertexBuffer { + internal int UserOffset; + + public bool IsContentLost { get { return false; } } + public DynamicVertexBuffer(GraphicsDevice graphics, Type type, int vertexCount, BufferUsage bufferUsage) : base(graphics, type, vertexCount, bufferUsage) { @@ -14,14 +18,14 @@ public DynamicVertexBuffer (GraphicsDevice graphics, VertexDeclaration vertexDec { } - public void SetData(T[] data, int startIndex, int elementCount, SetDataOptions options) where T : struct + public void SetData(int offsetInBytes, T[] data, int startIndex, int elementCount, SetDataOptions options) where T : struct { - throw new NotImplementedException(); + base.SetData(offsetInBytes, data, startIndex, elementCount, VertexDeclaration.VertexStride, options); } - public void SetData(int offsetInBytes, T[] data, int startIndex, int elementCount, SetDataOptions options) where T : struct + public void SetData(T[] data, int startIndex, int elementCount, SetDataOptions options) where T : struct { - throw new NotImplementedException(); + base.SetData(0, data, startIndex, elementCount, VertexDeclaration.VertexStride, options); } } } \ No newline at end of file diff --git a/MonoGame.Framework/Graphics/Vertices/VertexBuffer.cs b/MonoGame.Framework/Graphics/Vertices/VertexBuffer.cs index 0bf88dca751..6d4d9ec63ed 100644 --- a/MonoGame.Framework/Graphics/Vertices/VertexBuffer.cs +++ b/MonoGame.Framework/Graphics/Vertices/VertexBuffer.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Linq; +using System.Text; using System.Runtime.InteropServices; #if MONOMAC @@ -24,6 +26,7 @@ public class VertexBuffer : GraphicsResource internal uint _bufferStore; // TODO: Remove this VB limit! + internal uint vbo; internal static int _bufferCount; internal static VertexBuffer[] _allBuffers = new VertexBuffer[50]; internal static List _delayedBufferDelegates = new List(); @@ -95,46 +98,45 @@ internal static void CreateFrameBuffers () vertices[i] = tbuff[i]; } - public void SetData(T[] vertices) where T : struct + public void SetData(int offsetInBytes, T[] data, int startIndex, int elementCount, int vertexStride) where T : struct { - // TODO: This is fundimentally broken in that it is not - // assured that the incoming vertex array will exist unmodified - // long enough for the delayed buffer creation to occur. - // - // We either need to remove the concept of delayed buffer - // creation or copy the data here for safe keeping. - - //the creation of the buffer should mb be moved to the constructor and then glMapBuffer and Unmap should be used to update it - //glMapBuffer - sets data - //glUnmapBuffer - finished setting data - - _buffer = vertices; - _bufferPtr = GCHandle.Alloc(_buffer, GCHandleType.Pinned).AddrOfPinnedObject(); - - _bufferIndex = _bufferCount + 1; - _allBuffers[_bufferIndex] = this; - - _delayedBufferDelegates.Add(GenerateBuffer); - - _bufferCount++; - // TODO: Kill buffers in PhoneOSGameView.DestroyFrameBuffer() + SetData(0, data, startIndex, elementCount, VertexDeclaration.VertexStride, SetDataOptions.Discard); } + + public void SetData(T[] data, int startIndex, int elementCount) where T : struct + { + SetData(0, data, startIndex, elementCount, VertexDeclaration.VertexStride, SetDataOptions.Discard); + } - public void SetData (T[] data, int startIndex, int elementCount) where T : struct + public void SetData(T[] data) where T : struct { - throw new NotImplementedException(); + SetData(0, data, 0, data.Length, VertexDeclaration.VertexStride, SetDataOptions.Discard); } - public void SetData ( - int offsetInBytes, - T[] data, - int startIndex, - int elementCount, - int vertexStride - ) where T : struct - { - throw new NotImplementedException(); - } + protected void SetData(int offsetInBytes, T[] data, int startIndex, int elementCount, int vertexStride, SetDataOptions options) where T : struct + { + if (data == null) + throw new ArgumentNullException("data is null"); + if (data.Length < (startIndex + elementCount)) + throw new InvalidOperationException("The array specified in the data parameter is not the correct size for the amount of data requested."); + if ((vertexStride > (VertexCount * VertexDeclaration.VertexStride)) || (vertexStride < VertexDeclaration.VertexStride)) + throw new ArgumentOutOfRangeException("One of the following conditions is true:\nThe vertex stride is larger than the vertex buffer.\nThe vertex stride is too small for the type of data requested."); + + var elementSizeInBytes = Marshal.SizeOf(typeof(T)); + + //Threading.BlockOnUIThread(() => + //{ + var sizeInBytes = elementSizeInBytes * elementCount; +#if MONOMAC + GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); + GL.BufferSubData(BufferTarget.ArrayBuffer, new IntPtr(offsetInBytes), new IntPtr(sizeInBytes), data); +#else + GL11.BindBuffer(All11.ArrayBuffer, vbo); + GL11.BufferSubData(All11.ArrayBuffer, new IntPtr(offsetInBytes), new IntPtr(sizeInBytes), data); +#endif + + //}); + } public override void Dispose () {