Exemplo n.º 1
0
        public void GetData <T>(
            int offsetInBytes,
            T[] data,
            int startIndex,
            int elementCount,
            int vertexStride
            ) where T : struct
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            if (data.Length < (startIndex + elementCount))
            {
                throw new ArgumentOutOfRangeException(
                          "elementCount",
                          "This parameter must be a valid index within the array."
                          );
            }
            if (BufferUsage == BufferUsage.WriteOnly)
            {
                throw new NotSupportedException("Calling GetData on a resource that was created with BufferUsage.WriteOnly is not supported.");
            }

            int elementSizeInBytes = Marshal.SizeOf(typeof(T));

            if (vertexStride == 0)
            {
                vertexStride = elementSizeInBytes;
            }
            else if (vertexStride < elementSizeInBytes)
            {
                throw new ArgumentOutOfRangeException(
                          "vertexStride",
                          "The vertex stride is too small for the type of data requested. This is not allowed."
                          );
            }
            if (elementCount > 1 &&
                (elementCount * vertexStride) > (VertexCount * VertexDeclaration.VertexStride))
            {
                throw new InvalidOperationException("The array is not the correct size for the amount of data requested.");
            }

            GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);

            FNA3D.FNA3D_GetVertexBufferData(
                GraphicsDevice.GLDevice,
                buffer,
                offsetInBytes,
                handle.AddrOfPinnedObject() + (startIndex * elementSizeInBytes),
                elementCount,
                elementSizeInBytes,
                vertexStride
                );
            handle.Free();
        }