public unsafe void PutStream(Stream stream) { var unmanagedStream = stream as SharpDX.DataStream; var memory = new SharpDX.DataPointer(unmanagedStream.DataPointer, VVVV.Utils.Streams.StreamUtils.NextHigher((int)unmanagedStream.Length)); unmanagedStream.Dispose(); FUnmanagedMemoryPool.PutMemory(memory); }
private void PlatformGetData <T>(int offsetInBytes, T[] data, int startIndex, int elementCount, int vertexStride) where T : struct { GenerateIfRequired(); if (_isDynamic) { throw new NotImplementedException(); } else { var deviceContext = GraphicsDevice._d3dContext; // Copy the buffer to a staging resource var stagingDesc = _buffer.Description; stagingDesc.BindFlags = SharpDX.Direct3D11.BindFlags.None; stagingDesc.CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.Read | SharpDX.Direct3D11.CpuAccessFlags.Write; stagingDesc.Usage = SharpDX.Direct3D11.ResourceUsage.Staging; stagingDesc.OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None; var stagingBuffer = new SharpDX.Direct3D11.Buffer(GraphicsDevice._d3dDevice, stagingDesc); lock (GraphicsDevice._d3dContext) deviceContext.CopyResource(_buffer, stagingBuffer); int TsizeInBytes = SharpDX.Utilities.SizeOf <T>(); var dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned); var startBytes = startIndex * vertexStride; var dataPtr = (IntPtr)(dataHandle.AddrOfPinnedObject().ToInt64() + startBytes); SharpDX.DataPointer DataPointer = new SharpDX.DataPointer(dataPtr, data.Length * TsizeInBytes); lock (GraphicsDevice._d3dContext) { // Map the staging resource to a CPU accessible memory var box = deviceContext.MapSubresource(stagingBuffer, 0, SharpDX.Direct3D11.MapMode.Read, SharpDX.Direct3D11.MapFlags.None); if (vertexStride == TsizeInBytes) { SharpDX.Utilities.CopyMemory(dataPtr, box.DataPointer + offsetInBytes, vertexStride * elementCount); } else { for (int i = 0; i < data.Length; i++) { SharpDX.Utilities.CopyMemory(dataPtr + i * TsizeInBytes, box.DataPointer + i * vertexStride + offsetInBytes, TsizeInBytes); } } // Make sure that we unmap the resource in case of an exception deviceContext.UnmapSubresource(stagingBuffer, 0); } stagingBuffer.Dispose(); } }
public void GetData <T> (int offsetInBytes, T[] data, int startIndex, int elementCount, int vertexStride) where T : struct { #if GLES // Buffers are write-only on OpenGL ES 1.1 and 2.0. See the GL_OES_mapbuffer extension for more information. // http://www.khronos.org/registry/gles/extensions/OES/OES_mapbuffer.txt throw new NotSupportedException("Vertex buffers are write-only on OpenGL ES platforms"); #else if (data == null) { throw new ArgumentNullException("data", "This method does not accept null for this parameter."); } 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."); } if ((elementCount * vertexStride) > (VertexCount * VertexDeclaration.VertexStride)) { throw new InvalidOperationException("The array is not the correct size for the amount of data requested."); } #if DIRECTX GenerateIfRequired(); if (_isDynamic) { throw new NotImplementedException(); } else { var deviceContext = GraphicsDevice._d3dContext; // Copy the buffer to a staging resource var stagingDesc = _buffer.Description; stagingDesc.BindFlags = SharpDX.Direct3D11.BindFlags.None; stagingDesc.CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.Read | SharpDX.Direct3D11.CpuAccessFlags.Write; stagingDesc.Usage = SharpDX.Direct3D11.ResourceUsage.Staging; stagingDesc.OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None; var stagingBuffer = new SharpDX.Direct3D11.Buffer(GraphicsDevice._d3dDevice, stagingDesc); lock (GraphicsDevice._d3dContext) deviceContext.CopyResource(_buffer, stagingBuffer); int TsizeInBytes = SharpDX.Utilities.SizeOf <T>(); var dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned); var startBytes = startIndex * vertexStride; var dataPtr = (IntPtr)(dataHandle.AddrOfPinnedObject().ToInt64() + startBytes); SharpDX.DataPointer DataPointer = new SharpDX.DataPointer(dataPtr, data.Length * TsizeInBytes); lock (GraphicsDevice._d3dContext) { // Map the staging resource to a CPU accessible memory var box = deviceContext.MapSubresource(stagingBuffer, 0, SharpDX.Direct3D11.MapMode.Read, SharpDX.Direct3D11.MapFlags.None); if (vertexStride == TsizeInBytes) { SharpDX.Utilities.CopyMemory(dataPtr, box.DataPointer + offsetInBytes, vertexStride * elementCount); } else { for (int i = 0; i < data.Length; i++) { SharpDX.Utilities.CopyMemory(dataPtr + i * TsizeInBytes, box.DataPointer + i * vertexStride + offsetInBytes, TsizeInBytes); } } // Make sure that we unmap the resource in case of an exception deviceContext.UnmapSubresource(stagingBuffer, 0); } stagingBuffer.Dispose(); } #elif PSM throw new NotImplementedException(); #else if (Threading.IsOnUIThread()) { GetBufferData(offsetInBytes, data, startIndex, elementCount, vertexStride); } else { Threading.BlockOnUIThread(() => GetBufferData(offsetInBytes, data, startIndex, elementCount, vertexStride)); } #endif #endif }