private static void AddBeziersImpl(IntPtr thisPtr, IntPtr beziers, int beziersCount)
            {
                var shadow         = ToShadow <ID2D1SimplifiedGeometrySinkShadow>(thisPtr);
                var callback       = (ID2D1SimplifiedGeometrySink)shadow.Callback;
                var managedBeziers = new BezierSegment[beziersCount];

                MemoryHelpers.Read(beziers, managedBeziers, 0, beziersCount);
                callback.AddBeziers(managedBeziers);
            }
            private static void AddLinesImpl(IntPtr thisPtr, IntPtr points, int pointsCount)
            {
                var shadow        = ToShadow <ID2D1SimplifiedGeometrySinkShadow>(thisPtr);
                var callback      = (ID2D1SimplifiedGeometrySink)shadow.Callback;
                var managedPoints = new PointF[pointsCount];

                MemoryHelpers.Read(points, managedPoints, 0, pointsCount);
                callback.AddLines(managedPoints);
            }
Example #3
0
    /// <summary>
    /// Gets structured data contained in this chunk.
    /// </summary>
    /// <typeparam name="T">The type of the data to return</typeparam>
    /// <returns>
    /// A structure filled with the chunk data
    /// </returns>
    public unsafe T GetDataAs <T>() where T : unmanaged
    {
        T value = new();

        byte[] data = GetData();
        fixed(void *ptr = data)
        {
            MemoryHelpers.Read((IntPtr)ptr, ref value);
        }

        return(value);
    }
    /// <summary>
    ///   Reads a sequence of elements from the current stream into a target buffer and
    ///   advances the position within the stream by the number of bytes read.
    /// </summary>
    /// <remarks>
    /// In order to provide faster read/write, this operation doesn't check stream bound.
    /// A client must carefully not read/write above the size of this datastream.
    /// </remarks>
    /// <param name = "buffer">An array of values to be read from the stream.</param>
    /// <param name = "offset">The zero-based byte offset in buffer at which to begin storing
    ///   the data read from the current stream.</param>
    /// <param name = "count">The number of values to be read from the current stream.</param>
    /// <returns>The number of bytes read from the stream.</returns>
    /// <exception cref="NotSupportedException">This stream does not support reading.</exception>
    public int ReadRange <T>(T[] buffer, int offset, int count) where T : unmanaged
    {
        if (!CanRead)
        {
            throw new NotSupportedException();
        }

        long oldPosition = _position;

        _position = (byte *)MemoryHelpers.Read((IntPtr)(_buffer + _position), buffer, offset, count) - _buffer;
        return((int)(_position - oldPosition));
    }
    /// <summary>
    /// Reads an array of values from the current stream, and advances the current position within this stream by the number of bytes written.
    /// </summary>
    /// <remarks>
    /// In order to provide faster read/write, this operation doesn't check stream bound.
    /// A client must carefully not read/write above the size of this datastream.
    /// </remarks>
    /// <typeparam name="T">The type of the values to be read from the stream.</typeparam>
    /// <returns>An array of values that was read from the current stream.</returns>
    public T[] ReadRange <T>(int count) where T : unmanaged
    {
        if (!CanRead)
        {
            throw new NotSupportedException();
        }

        byte *from = _buffer + _position;

        T[] result = new T[count];
        _position = (byte *)MemoryHelpers.Read((IntPtr)from, result, 0, count) - _buffer;
        return(result);
    }
Example #6
0
        public unsafe uint Write(IntPtr buffer, uint numberOfBytesToWrite)
        {
            uint totalWrite = 0;

            while (numberOfBytesToWrite > 0)
            {
                uint countWrite = (uint)Math.Min(numberOfBytesToWrite, tempBuffer.Length);
                MemoryHelpers.Read(new IntPtr(totalWrite + (byte *)buffer), new ReadOnlySpan <byte>(tempBuffer), (int)countWrite);
                sourceStream.Write(tempBuffer, 0, (int)countWrite);
                numberOfBytesToWrite -= countWrite;
                totalWrite           += countWrite;
            }
            return(totalWrite);
        }
 public void GetCurrentState <T, TRaw, TUpdate>(ref T data)
     where T : class, IDeviceState <TRaw, TUpdate>, new()
     where TRaw : unmanaged
     where TUpdate : unmanaged, IStateUpdate
 {
     unsafe
     {
         int   size  = sizeof(TRaw);
         byte *pTemp = stackalloc byte[size * 2];
         TRaw  temp  = default;
         GetDeviceState(size, (IntPtr)pTemp);
         MemoryHelpers.Read((IntPtr)pTemp, ref temp);
         data.MarshalFrom(ref temp);
     }
 }
        /// <summary>
        /// Marshal this class from an unmanaged buffer.
        /// </summary>
        /// <param name="bufferSize">The size of the unmanaged buffer.</param>
        /// <param name="bufferPointer">The pointer to the unmanaged buffer.</param>
        /// <returns>An instance of TypeSpecificParameters or null</returns>
        protected override TypeSpecificParameters?MarshalFrom(int bufferSize, IntPtr bufferPointer)
        {
            unsafe
            {
                // BufferSize must be a multiple of the size of Condition structure
                if (bufferSize <= 0 || (bufferSize % sizeof(Condition)) != 0)
                {
                    return(null);
                }

                // Allocate a set of Condition and marshal from unmanaged memory
                int numberOfConditions = bufferSize / sizeof(Condition);
                Conditions = new Condition[numberOfConditions];
                MemoryHelpers.Read(bufferPointer, Conditions, 0, Conditions.Length);
                return(this);
            }
        }
Example #9
0
        /// <summary>
        /// Marshal this class from an unmanaged buffer.
        /// </summary>
        /// <param name="bufferSize">The size of the unmanaged buffer.</param>
        /// <param name="bufferPointer">The pointer to the unmanaged buffer.</param>
        /// <returns>An instance of TypeSpecificParameters or null</returns>
        protected override TypeSpecificParameters?MarshalFrom(int bufferSize, IntPtr bufferPointer)
        {
            unsafe
            {
                if (bufferSize != sizeof(RawCustomForce))
                {
                    return(null);
                }

                ChannelCount = ((RawCustomForce *)bufferPointer)->Channels;
                SamplePeriod = ((RawCustomForce *)bufferPointer)->SamplePeriod;
                SampleCount  = ((RawCustomForce *)bufferPointer)->Samples;
                ForceData    = new int[SampleCount];
                MemoryHelpers.Read(((RawCustomForce *)bufferPointer)->ForceDataPointer, ForceData, 0, ForceData.Length);
                return(this);
            }
        }
Example #10
0
    /// <summary>
    /// Gets structured data contained in this chunk.
    /// </summary>
    /// <typeparam name="T">The type of the data to return</typeparam>
    /// <returns>A structure filled with the chunk data</returns>
    public unsafe T[] GetDataAsArray <T>() where T : unmanaged
    {
        int sizeOfT = sizeof(T);

        if ((Size % sizeOfT) != 0)
        {
            throw new ArgumentException("Size of T is incompatible with size of chunk");
        }

        T[]    values = new T[Size / sizeOfT];
        byte[] data   = GetData();
        fixed(byte *dataPtr = data)
        {
            MemoryHelpers.Read((IntPtr)dataPtr, values, 0, values.Length);
        }

        return(values);
    }
    internal unsafe void __MarshalFrom(ref __Native @ref)
    {
        Categories = new InfoQueueMessageCategory[@ref.NumCategories];
        if (@ref.NumCategories > 0)
        {
            MemoryHelpers.Read(@ref.PCategoryList, Categories, 0, @ref.NumCategories);
        }

        Severities = new InfoQueueMessageSeverity[@ref.NumSeverities];
        if (@ref.NumSeverities > 0)
        {
            MemoryHelpers.Read(@ref.PSeverityList, Severities, 0, @ref.NumSeverities);
        }

        Ids = new int[@ref.NumIDs];
        if (@ref.NumIDs > 0)
        {
            MemoryHelpers.Read(@ref.PIDList, Ids, 0, @ref.NumIDs);
        }
    }
Example #12
0
 public ShaderBytecode(Blob blob)
 {
     Data = new byte[blob.BufferSize];
     MemoryHelpers.Read(blob.BufferPointer, Data, 0, Data.Length);
 }
Example #13
0
 public ShaderBytecode(IntPtr bytecode, PointerSize length)
 {
     Data = new byte[length];
     MemoryHelpers.Read(bytecode, Data, 0, Data.Length);
 }