Skip to content

Commit

Permalink
Merge pull request #559 from CartBlanche/develop
Browse files Browse the repository at this point in the history
Develop : back ported changes to DynamicVertexBuffer and VertexBuffer...
  • Loading branch information
dellis1972 committed Jun 28, 2012
2 parents 4952f59 + 47b6621 commit a1ab849
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 38 deletions.
12 changes: 8 additions & 4 deletions MonoGame.Framework/Graphics/Vertices/DynamicVertexBuffer.cs
Expand Up @@ -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)
{
Expand All @@ -14,14 +18,14 @@ public DynamicVertexBuffer (GraphicsDevice graphics, VertexDeclaration vertexDec
{
}

public void SetData<T>(T[] data, int startIndex, int elementCount, SetDataOptions options) where T : struct
public void SetData<T>(int offsetInBytes, T[] data, int startIndex, int elementCount, SetDataOptions options) where T : struct
{
throw new NotImplementedException();
base.SetData<T>(offsetInBytes, data, startIndex, elementCount, VertexDeclaration.VertexStride, options);
}

public void SetData<T>(int offsetInBytes, T[] data, int startIndex, int elementCount, SetDataOptions options) where T : struct
public void SetData<T>(T[] data, int startIndex, int elementCount, SetDataOptions options) where T : struct
{
throw new NotImplementedException();
base.SetData<T>(0, data, startIndex, elementCount, VertexDeclaration.VertexStride, options);
}
}
}
70 changes: 36 additions & 34 deletions 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
Expand All @@ -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<Action> _delayedBufferDelegates = new List<Action>();
Expand Down Expand Up @@ -95,46 +98,45 @@ internal static void CreateFrameBuffers ()
vertices[i] = tbuff[i];
}

public void SetData<T>(T[] vertices) where T : struct
public void SetData<T>(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<T>);

_bufferCount++;
// TODO: Kill buffers in PhoneOSGameView.DestroyFrameBuffer()
SetData<T>(0, data, startIndex, elementCount, VertexDeclaration.VertexStride, SetDataOptions.Discard);
}

public void SetData<T>(T[] data, int startIndex, int elementCount) where T : struct
{
SetData<T>(0, data, startIndex, elementCount, VertexDeclaration.VertexStride, SetDataOptions.Discard);
}

public void SetData<T> (T[] data, int startIndex, int elementCount) where T : struct
public void SetData<T>(T[] data) where T : struct
{
throw new NotImplementedException();
SetData<T>(0, data, 0, data.Length, VertexDeclaration.VertexStride, SetDataOptions.Discard);
}

public void SetData<T> (
int offsetInBytes,
T[] data,
int startIndex,
int elementCount,
int vertexStride
) where T : struct
{
throw new NotImplementedException();
}
protected void SetData<T>(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<T>(BufferTarget.ArrayBuffer, new IntPtr(offsetInBytes), new IntPtr(sizeInBytes), data);
#else
GL11.BindBuffer(All11.ArrayBuffer, vbo);
GL11.BufferSubData<T>(All11.ArrayBuffer, new IntPtr(offsetInBytes), new IntPtr(sizeInBytes), data);
#endif

//});
}

public override void Dispose ()
{
Expand Down

0 comments on commit a1ab849

Please sign in to comment.