internal unsafe void __MarshalTo(ref __Native @ref) { @ref.FontFace = FontFace == null ? IntPtr.Zero : FontFace.NativePointer; @ref.FontEmSize = FontEmSize; @ref.GlyphCount = -1; @ref.GlyphIndices = IntPtr.Zero; @ref.GlyphAdvances = IntPtr.Zero; @ref.GlyphOffsets = IntPtr.Zero; if (Indices != null) { @ref.GlyphCount = Indices.Length; @ref.GlyphIndices = Marshal.AllocHGlobal(Indices.Length * sizeof(ushort)); if (Indices.Length > 0) { UnsafeUtilities.Write(@ref.GlyphIndices, Indices, 0, Indices.Length); } } if (Advances != null) { if (@ref.GlyphCount >= 0 && @ref.GlyphCount != Advances.Length) { throw new InvalidOperationException( $"Invalid length for array Advances [{Advances.Length}] and Indices [{@ref.GlyphCount}]. Indices, Advances and Offsets array must have same size - or may be null" ); } @ref.GlyphCount = Advances.Length; @ref.GlyphAdvances = Marshal.AllocHGlobal(Advances.Length * sizeof(float)); if (Advances.Length > 0) { UnsafeUtilities.Write(@ref.GlyphAdvances, Advances, 0, Advances.Length); } } if (Offsets != null) { if (@ref.GlyphCount >= 0 && @ref.GlyphCount != Offsets.Length) { throw new InvalidOperationException($"Invalid length for array Offsets [{Offsets.Length}]. Indices, Advances and Offsets array must have same size (Current is [{@ref.GlyphCount}]- or may be null"); } @ref.GlyphCount = this.Offsets.Length; @ref.GlyphOffsets = Marshal.AllocHGlobal(this.Offsets.Length * sizeof(GlyphOffset)); if (this.Offsets.Length > 0) { UnsafeUtilities.Write(@ref.GlyphOffsets, Offsets, 0, this.Offsets.Length); } } if (@ref.GlyphCount < 0) { @ref.GlyphCount = 0; } @ref.IsSideways = IsSideways; @ref.BidiLevel = BidiLevel; }
/// <summary> /// Writes an array of values to 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 written to the stream.</typeparam> /// <param name = "data">An array of values to be written to the stream.</param> /// <param name = "offset">The zero-based offset in data at which to begin copying values to the current stream.</param> /// <param name = "count"> /// The number of values to be written to the current stream. If this is zero, all of the contents <paramref name="data" /> will be written. /// </param> /// <exception cref="NotSupportedException">This stream does not support writing.</exception> public void WriteRange <T>(T[] data, int offset, int count) where T : unmanaged { if (!CanWrite) { throw new NotSupportedException(); } _position = (byte *)UnsafeUtilities.Write((IntPtr)(_buffer + _position), data, offset, count) - _buffer; }
private void UpdateBBox(ID3D11Device InDevice, ID3D11DeviceContext InDContext) { // Update vertex buffer VertexLayouts.BasicLayout.Vertex[] BBoxVertices = new VertexLayouts.BasicLayout.Vertex[Objects.Count * 8]; int CurrentBBoxIndex = 0; foreach (RenderBoundingBox BBox in Objects.Values) { VertexLayouts.BasicLayout.Vertex[] Cached = BBox.GetTransformVertices(); System.Array.Copy(Cached, 0, BBoxVertices, CurrentBBoxIndex * Cached.Length, Cached.Length); CurrentBBoxIndex++; } // Update index buffer uint[] BBoxIndices = new uint[Objects.Count * 24]; CurrentBBoxIndex = 0; foreach (RenderBoundingBox BBox in Objects.Values) { uint[] CopiedIndices = new uint[BBox.Indices.Length]; for (int i = 0; i < CopiedIndices.Length; i++) { int BBoxOffset = (CurrentBBoxIndex * 8); CopiedIndices[i] = (ushort)(BBox.Indices[i] + BBoxOffset); } System.Array.Copy(CopiedIndices, 0, BBoxIndices, CurrentBBoxIndex * BBox.Indices.Length, BBox.Indices.Length); CurrentBBoxIndex++; } SizeToRender = Objects.Count * 24; if (VertexBuffer == null && IndexBuffer == null) { VertexBuffer = InDevice.CreateBuffer(BindFlags.VertexBuffer, BBoxVertices, 0, ResourceUsage.Dynamic, CpuAccessFlags.Write); IndexBuffer = InDevice.CreateBuffer(BindFlags.IndexBuffer, BBoxIndices, 0, ResourceUsage.Dynamic, CpuAccessFlags.Write); } else { // TODO: Templatize this MappedSubresource mappedResource = InDContext.Map(VertexBuffer, MapMode.WriteDiscard, MapFlags.None); unsafe { UnsafeUtilities.Write(mappedResource.DataPointer, BBoxVertices); } InDContext.Unmap(VertexBuffer); mappedResource = InDContext.Map(IndexBuffer, MapMode.WriteDiscard, MapFlags.None); unsafe { UnsafeUtilities.Write(mappedResource.DataPointer, BBoxIndices); } InDContext.Unmap(IndexBuffer); } }
public override void UpdateBuffers(ID3D11Device device, ID3D11DeviceContext deviceContext) { if (bIsUpdatedNeeded) { MappedSubresource mappedResource = deviceContext.Map(vertexBuffer, MapMode.WriteDiscard, MapFlags.None); unsafe { UnsafeUtilities.Write(mappedResource.DataPointer, vertices); } deviceContext.Unmap(vertexBuffer, 0); bIsUpdatedNeeded = false; } }
/// <summary> /// Marshals this class to its native/unmanaged counterpart. /// </summary> /// <returns>A pointer to an allocated buffer containing the unmanaged structure.</returns> internal virtual IntPtr MarshalTo() { // By default, copy as-is previous data IntPtr copyData = IntPtr.Zero; if (_bufferSize > 0 && _buffer != null) { copyData = Marshal.AllocHGlobal(_bufferSize); UnsafeUtilities.Write(copyData, _buffer, 0, _bufferSize); } return(copyData); }
private void UpdateLines(ID3D11Device InDevice, ID3D11DeviceContext InDContext) { // Update buffers List <VertexLayouts.BasicLayout.Vertex> TempLineVertices = new List <VertexLayouts.BasicLayout.Vertex>(); List <ushort> TempLineIndices = new List <ushort>(); int CurrentVertexCount = 0; foreach (RenderLine Line in Objects.Values) { TempLineVertices.AddRange(Line.GetVertices); ushort[] CopiedIndices = Line.GetStrippedIndices; for (uint x = 0; x < CopiedIndices.Length; x++) { CopiedIndices[x] += (ushort)(CurrentVertexCount); } TempLineIndices.AddRange(CopiedIndices.ToList()); TempLineIndices.Add(ushort.MaxValue); CurrentVertexCount = TempLineVertices.Count; } SizeToRender = TempLineIndices.Count; if (VertexBuffer == null && IndexBuffer == null) { VertexBuffer = InDevice.CreateBuffer(BindFlags.VertexBuffer, TempLineVertices.ToArray(), 0, ResourceUsage.Dynamic, CpuAccessFlags.Write); IndexBuffer = InDevice.CreateBuffer(BindFlags.IndexBuffer, TempLineIndices.ToArray(), 0, ResourceUsage.Dynamic, CpuAccessFlags.Write); } else { // TODO: Templatize this MappedSubresource mappedResource = InDContext.Map(VertexBuffer, MapMode.WriteDiscard, MapFlags.None); unsafe { UnsafeUtilities.Write(mappedResource.DataPointer, TempLineVertices.ToArray()); } InDContext.Unmap(VertexBuffer); mappedResource = InDContext.Map(IndexBuffer, MapMode.WriteDiscard, MapFlags.None); unsafe { UnsafeUtilities.Write(mappedResource.DataPointer, TempLineIndices.ToArray()); } InDContext.Unmap(IndexBuffer); } }
internal unsafe void __MarshalTo(ref __Native @ref) { @ref.Size = sizeof(__Native); @ref.Flags = Flags; @ref.Duration = Duration; @ref.SamplePeriod = SamplePeriod; @ref.Gain = Gain; @ref.TriggerButton = TriggerButton; @ref.TriggerRepeatInterval = TriggerRepeatInterval; @ref.StartDelay = StartDelay; // Marshal Axes and Directions @ref.AxeCount = 0; @ref.AxePointer = IntPtr.Zero; @ref.DirectionPointer = IntPtr.Zero; if (Axes != null && Axes.Length > 0) { @ref.AxeCount = Axes.Length; @ref.AxePointer = Marshal.AllocHGlobal(Axes.Length * sizeof(int)); UnsafeUtilities.Write(@ref.AxePointer, Axes, 0, Axes.Length); if (Directions != null && Directions.Length == Axes.Length) { @ref.DirectionPointer = Marshal.AllocHGlobal(Directions.Length * sizeof(int)); UnsafeUtilities.Write(@ref.DirectionPointer, Directions, 0, Directions.Length); } } // Marshal Envelope @ref.EnvelopePointer = IntPtr.Zero; if (Envelope != null) { @ref.EnvelopePointer = Marshal.AllocHGlobal(sizeof(Envelope.__Native)); var envelopeNative = Envelope.__NewNative(); Envelope.__MarshalTo(ref envelopeNative); UnsafeUtilities.Write(@ref.EnvelopePointer, ref envelopeNative); } // Marshal TypeSpecificParameters @ref.TypeSpecificParamCount = 0; @ref.TypeSpecificParamPointer = IntPtr.Zero; if (Parameters != null) { @ref.TypeSpecificParamCount = Parameters.Size; @ref.TypeSpecificParamPointer = Parameters.MarshalTo(); } }