static bool TryWritePrimitives(Span<byte> output, Sha256 hash, string keyType, string verb, string resourceId, string resourceType, string tokenVersion, DateTime utc, out int bytesWritten)
    {
        int totalWritten = 0;
        bytesWritten = 0;

        Span<byte> buffer = stackalloc byte[AuthenticationHeaderBufferSize];

        s_type.CopyTo(buffer);
        totalWritten += s_type.Length;

        if (Encodings.Utf16.ToUtf8(MemoryMarshal.AsBytes(keyType.AsSpan()), buffer.Slice(totalWritten), out int consumed, out int written) != OperationStatus.Done)
        {
            throw new NotImplementedException("need to resize buffer");
        }
        totalWritten += written;

        s_ver.CopyTo(buffer.Slice(totalWritten));
        totalWritten += s_ver.Length;

        if (Encodings.Utf16.ToUtf8(MemoryMarshal.AsBytes(tokenVersion.AsSpan()), buffer.Slice(totalWritten), out consumed, out written) != OperationStatus.Done)
        {
            throw new NotImplementedException("need to resize buffer");
        }
        totalWritten += written;

        s_sig.CopyTo(buffer.Slice(totalWritten));
        totalWritten += s_sig.Length;

        var front = buffer.Slice(0, totalWritten);

        var payload = buffer.Slice(totalWritten);
        totalWritten = 0;

        if (verb.Equals("GET", StringComparison.Ordinal) || verb.Equals("get", StringComparison.Ordinal))
        {
            s_get.CopyTo(payload);
            totalWritten += s_get.Length;
        }
        else if (verb.Equals("POST", StringComparison.Ordinal) || verb.Equals("post", StringComparison.Ordinal))
        {
            s_post.CopyTo(payload);
            totalWritten += s_post.Length;
        }
        else if (verb.Equals("DELETE", StringComparison.Ordinal) || verb.Equals("delete", StringComparison.Ordinal))
        {
            s_delete.CopyTo(payload);
            totalWritten += s_delete.Length;
        }
        else
        {
            if (Encodings.Utf16.ToUtf8(MemoryMarshal.AsBytes(verb.AsSpan()), payload, out consumed, out written) != OperationStatus.Done)
            {
                throw new NotImplementedException("need to resize buffer");
            }
            if (Encodings.Ascii.ToLowerInPlace(payload.Slice(0, written), out written) != OperationStatus.Done)
            {
                throw new NotImplementedException("need to resize buffer");
            }

            payload[written] = (byte)'\n';
            totalWritten += written + 1;
        }

        var bufferSlice = payload.Slice(totalWritten);

        if (Encodings.Utf16.ToUtf8(MemoryMarshal.AsBytes(resourceType.AsSpan()), bufferSlice, out consumed, out written) != OperationStatus.Done)
        {
            throw new NotImplementedException("need to resize buffer");
        }
        if (Encodings.Ascii.ToLowerInPlace(bufferSlice.Slice(0, written), out written) != OperationStatus.Done)
        {
            throw new NotImplementedException("need to resize buffer");
        }
        bufferSlice[written] = (byte)'\n';
        totalWritten += written + 1;
        bufferSlice = payload.Slice(totalWritten);

        if (Encodings.Utf16.ToUtf8(MemoryMarshal.AsBytes(resourceId.AsSpan()), bufferSlice, out consumed, out written) != OperationStatus.Done)
        {
            throw new NotImplementedException("need to resize buffer");
        }
        bufferSlice[written] = (byte)'\n';
        totalWritten += written + 1;
        bufferSlice = payload.Slice(totalWritten);

        if (!Utf8Formatter.TryFormat(utc, bufferSlice, out written, 'l'))
        {
            throw new NotImplementedException("need to resize buffer");
        }
        bufferSlice[written] = (byte)'\n';
        totalWritten += written + 1;
        bufferSlice = payload.Slice(totalWritten);

        bufferSlice[0] = (byte)'\n';
        totalWritten += 1;

        hash.Append(buffer.Slice(front.Length, totalWritten));
        if (!hash.TryWrite(buffer.Slice(front.Length), out written))
        {
            throw new NotImplementedException("need to resize buffer");
        }
        if (Base64.EncodeToUtf8InPlace(buffer.Slice(front.Length), written, out written) != OperationStatus.Done)
        {
            throw new NotImplementedException("need to resize buffer");
        }

        var len = front.Length + written;
        if (UrlEncoder.Utf8.Encode(buffer.Slice(0, len), output, out consumed, out bytesWritten) != OperationStatus.Done)
        {
            bytesWritten = 0;
            return false;
        }
        return true;
    }
Beispiel #2
0
 static ref T head <M, N, T>(BlockMatrix <M, N, T> src)
     where N : ITypeNat, new()
     where M : ITypeNat, new()
     where T : struct
 => ref MemoryMarshal.GetReference <T>(src.Unsized);
Beispiel #3
0
 static ref T head <N, T>(Span <N, T> src)
     where T : struct
     where N : ITypeNat, new()
 => ref MemoryMarshal.GetReference <T>(src.Unsized);
 public override Span<TTo> GetSpan()
     => MemoryMarshal.Cast<TFrom, TTo>(_from.Span);
Beispiel #5
0
 public static ref T head <T>(BlockVector <T> src)
     where T : struct
 => ref MemoryMarshal.GetReference <T>(src.Unblocked);
Beispiel #6
0
        private static unsafe int ReadSyncUsingAsyncHandle(SafeFileHandle handle, Span <byte> buffer, long fileOffset)
        {
            handle.EnsureThreadPoolBindingInitialized();

            CallbackResetEvent resetEvent = new CallbackResetEvent(handle.ThreadPoolBinding !);
            NativeOverlapped * overlapped = null;

            try
            {
                overlapped = GetNativeOverlappedForAsyncHandle(handle, fileOffset, resetEvent);

                fixed(byte *pinned = &MemoryMarshal.GetReference(buffer))
                {
                    Interop.Kernel32.ReadFile(handle, pinned, buffer.Length, IntPtr.Zero, overlapped);

                    int errorCode = FileStreamHelpers.GetLastWin32ErrorAndDisposeHandleIfInvalid(handle);

                    if (errorCode == Interop.Errors.ERROR_IO_PENDING)
                    {
                        resetEvent.WaitOne();
                        errorCode = Interop.Errors.ERROR_SUCCESS;
                    }

                    if (errorCode == Interop.Errors.ERROR_SUCCESS)
                    {
                        int result = 0;
                        if (Interop.Kernel32.GetOverlappedResult(handle, overlapped, ref result, bWait: false))
                        {
                            Debug.Assert(result >= 0 && result <= buffer.Length, $"GetOverlappedResult returned {result} for {buffer.Length} bytes request");
                            return(result);
                        }

                        errorCode = FileStreamHelpers.GetLastWin32ErrorAndDisposeHandleIfInvalid(handle);
                    }

                    switch (errorCode)
                    {
                    case Interop.Errors.ERROR_HANDLE_EOF:     // logically success with 0 bytes read (read at end of file)
                    case Interop.Errors.ERROR_BROKEN_PIPE:
                    case Interop.Errors.ERROR_INVALID_PARAMETER when IsEndOfFileForNoBuffering(handle, fileOffset):
                        // EOF on a pipe. Callback will not be called.
                        // We clear the overlapped status bit for this special case (failure
                        // to do so looks like we are freeing a pending overlapped later).
                        overlapped->InternalLow = IntPtr.Zero;

                        return(0);

                    default:
                        throw Win32Marshal.GetExceptionForWin32Error(errorCode, handle.Path);
                    }
                }
            }
            finally
            {
                if (overlapped != null)
                {
                    resetEvent.FreeNativeOverlapped(overlapped);
                }

                resetEvent.Dispose();
            }
        }
Beispiel #7
0
 private static void WriteSectionHeader(ScenarioSectionHeader header, BinaryWriter output)
 {
     output.Write(MemoryMarshal.Cast <ScenarioSectionHeader, byte>(
                      MemoryMarshal.CreateSpan(ref header, 1)
                      ));
 }
Beispiel #8
0
 private static ByteArrayContent ToByteArrayContent(ReadOnlyMemory <byte> content) =>
 MemoryMarshal.TryGetArray(content, out var segment)
     ? new ByteArrayContent(segment.Array, segment.Offset, segment.Count)
 // TODO: Just throw?
     : new ByteArrayContent(content.ToArray());
Beispiel #9
0
        static bool TryWritePrimitive(Span <byte> output, Sha256 hash, string verb, string canonicalizedResource, DateTime utc, out int bytesWritten)
        {
            int written, consumed;

            bytesWritten = 0;

            if (verb.Equals("GET", StringComparison.Ordinal))
            {
                if (output.Length < 3)
                {
                    bytesWritten = 0;
                    return(false);
                }
                s_GET.CopyTo(output);
                bytesWritten += s_GET.Length;
            }
            else
            {
                if (TextEncodings.Utf16.ToUtf8(MemoryMarshal.AsBytes(verb.AsSpan()), output, out consumed, out written) != OperationStatus.Done)
                {
                    bytesWritten = 0;
                    return(false);
                }

                output[written] = (byte)'\n';
                bytesWritten   += written + 1;
            }

            var free = output.Slice(bytesWritten);

            s_emptyHeaders.CopyTo(free);
            bytesWritten += s_emptyHeaders.Length;

            free = output.Slice(bytesWritten);
            if (!Utf8Formatter.TryFormat(utc, free, out written, 'R'))
            {
                bytesWritten = 0;
                return(false);
            }
            free[written] = (byte)'\n';
            bytesWritten += written + 1;
            free          = output.Slice(bytesWritten);

            if (TextEncodings.Utf16.ToUtf8(MemoryMarshal.AsBytes(canonicalizedResource.AsSpan()), free, out consumed, out written) != OperationStatus.Done)
            {
                bytesWritten = 0;
                return(false);
            }
            bytesWritten += written;

            var formatted = output.Slice(0, bytesWritten);

            hash.Append(formatted);
            if (!hash.TryWrite(output, out written))
            {
                throw new NotImplementedException("need to resize buffer");
            }

            if (Base64.EncodeToUtf8InPlace(output, written, out written) != OperationStatus.Done)
            {
                bytesWritten = 0;
                return(false);
            }

            bytesWritten = written;
            return(true);
        }
Beispiel #10
0
 private void WritePrimitiveList <T>(List <T> values) where T : unmanaged
 {
     Write(MemoryMarshal.AsBytes(CollectionsMarshal.AsSpan(values)));
 }
 public Span <RGBQUAD> AsSpan() => MemoryMarshal.CreateSpan(ref e0, 256);
Beispiel #12
0
 private void WritePrimitiveArray <T>(T[] values) where T : unmanaged
 {
     Write(MemoryMarshal.AsBytes(new ReadOnlySpan <T>(values)));
 }
Beispiel #13
0
        private void StartLoop(int messageSize, int delayMicros, int burst)
        {
            _cts = new CancellationTokenSource();
            var histogram = new LongHistogram(TimeSpan.FromSeconds(10).Ticks, 2);

            void FeedClientOnMessageReceived(ReadOnlySpan <byte> bytes)
            {
                var now = Stopwatch.GetTimestamp();

                var count = Interlocked.Increment(ref _messageCounter);

                if (count <= _warmupSkipCount)
                {
                    return;
                }

                var start  = Unsafe.ReadUnaligned <long>(ref MemoryMarshal.GetReference(bytes));
                var rrt    = now - start;
                var micros = (long)(rrt * 1_000_000.0 / Stopwatch.Frequency);

                if (!_cts.IsCancellationRequested)
                {
                    histogram.RecordValue(micros);
                }
            }

            _feedClientManual.MessageReceived += FeedClientOnMessageReceived;

            histogram.Reset();
            _messageCounter = 0;

            var buffer = new byte[messageSize];

            for (var i = 0; i < buffer.Length; i++)
            {
                buffer[i] = 42;
            }

            var rateReporter = Task.Run(async() =>
            {
                var previousCount = 0L;

                while (!_cts.IsCancellationRequested)
                {
                    var count       = Volatile.Read(ref _messageCounter);
                    var countPerSec = count - previousCount;
                    previousCount   = count;

                    Console.WriteLine($"Processed messages: {countPerSec:N0}, total: {count:N0} [GC 0:{GC.CollectionCount(0)} 1:{GC.CollectionCount(1)}]");
                    await Task.Delay(1000);
                }
            });

            while (!_cts.IsCancellationRequested)
            {
                _sw.Restart();

                for (int i = 0; i < burst; i++)
                {
                    Unsafe.WriteUnaligned(ref buffer[0], Stopwatch.GetTimestamp());
                    _feedClientManual.Send(buffer);
                }

                NOP(delayMicros / 1000_000.0);
            }

            _feedClientManual.MessageReceived -= FeedClientOnMessageReceived;

            rateReporter.Wait();
            histogram.OutputPercentileDistribution(Console.Out, 1);
            using var writer = new StreamWriter(Path.Combine(_outFolder, $"Latency_{messageSize}_{delayMicros}.hgrm"));
            histogram.OutputPercentileDistribution(writer);
        }
 public Span <D3D11_RENDER_TARGET_BLEND_DESC1> AsSpan() => MemoryMarshal.CreateSpan(ref e0, 8);
 protected override bool TryGetArray(out ArraySegment <byte> segment)
 {
     _pool.CheckDisposed();
     return(MemoryMarshal.TryGetArray(_owner.Memory, out segment));
 }
Beispiel #16
0
        private static unsafe string JoinInternal(ReadOnlySpan <char> first, ReadOnlySpan <char> second, ReadOnlySpan <char> third, ReadOnlySpan <char> fourth)
        {
            Debug.Assert(first.Length > 0 && second.Length > 0 && third.Length > 0 && fourth.Length > 0, "should have dealt with empty paths");

            bool firstHasSeparator = PathInternal.IsDirectorySeparator(first[first.Length - 1]) ||
                                     PathInternal.IsDirectorySeparator(second[0]);
            bool thirdHasSeparator = PathInternal.IsDirectorySeparator(second[second.Length - 1]) ||
                                     PathInternal.IsDirectorySeparator(third[0]);
            bool fourthHasSeparator = PathInternal.IsDirectorySeparator(third[third.Length - 1]) ||
                                      PathInternal.IsDirectorySeparator(fourth[0]);

            fixed(char *f = &MemoryMarshal.GetReference(first), s = &MemoryMarshal.GetReference(second), t = &MemoryMarshal.GetReference(third), u = &MemoryMarshal.GetReference(fourth))
            {
#if MS_IO_REDIST
                return(StringExtensions.Create(
#else
                return string.Create(
#endif
                           first.Length + second.Length + third.Length + fourth.Length + (firstHasSeparator ? 0 : 1) + (thirdHasSeparator ? 0 : 1) + (fourthHasSeparator ? 0 : 1),
                           (First: (IntPtr)f, FirstLength: first.Length, Second: (IntPtr)s, SecondLength: second.Length,
                            Third: (IntPtr)t, ThirdLength: third.Length, Fourth: (IntPtr)u, FourthLength: fourth.Length,
                            FirstHasSeparator: firstHasSeparator, ThirdHasSeparator: thirdHasSeparator, FourthHasSeparator: fourthHasSeparator),
                           (destination, state) =>
                {
                    new Span <char>((char *)state.First, state.FirstLength).CopyTo(destination);
                    if (!state.FirstHasSeparator)
                    {
                        destination[state.FirstLength] = PathInternal.DirectorySeparatorChar;
                    }
                    new Span <char>((char *)state.Second, state.SecondLength).CopyTo(destination.Slice(state.FirstLength + (state.FirstHasSeparator ? 0 : 1)));
                    if (!state.ThirdHasSeparator)
                    {
                        destination[state.FirstLength + state.SecondLength + (state.FirstHasSeparator ? 0 : 1)] = PathInternal.DirectorySeparatorChar;
                    }
                    new Span <char>((char *)state.Third, state.ThirdLength).CopyTo(destination.Slice(state.FirstLength + state.SecondLength + (state.FirstHasSeparator ? 0 : 1) + (state.ThirdHasSeparator ? 0 : 1)));
                    if (!state.FourthHasSeparator)
                    {
                        destination[destination.Length - state.FourthLength - 1] = PathInternal.DirectorySeparatorChar;
                    }
                    new Span <char>((char *)state.Fourth, state.FourthLength).CopyTo(destination.Slice(destination.Length - state.FourthLength));
                }));
            }
        }
Beispiel #17
0
        private static unsafe void WriteSyncUsingAsyncHandle(SafeFileHandle handle, ReadOnlySpan <byte> buffer, long fileOffset)
        {
            if (buffer.IsEmpty)
            {
                return;
            }

            handle.EnsureThreadPoolBindingInitialized();

            CallbackResetEvent resetEvent = new CallbackResetEvent(handle.ThreadPoolBinding !);
            NativeOverlapped * overlapped = null;

            try
            {
                overlapped = GetNativeOverlappedForAsyncHandle(handle, fileOffset, resetEvent);

                fixed(byte *pinned = &MemoryMarshal.GetReference(buffer))
                {
                    Interop.Kernel32.WriteFile(handle, pinned, buffer.Length, IntPtr.Zero, overlapped);

                    int errorCode = FileStreamHelpers.GetLastWin32ErrorAndDisposeHandleIfInvalid(handle);

                    if (errorCode == Interop.Errors.ERROR_IO_PENDING)
                    {
                        resetEvent.WaitOne();
                        errorCode = Interop.Errors.ERROR_SUCCESS;
                    }

                    if (errorCode == Interop.Errors.ERROR_SUCCESS)
                    {
                        int result = 0;
                        if (Interop.Kernel32.GetOverlappedResult(handle, overlapped, ref result, bWait: false))
                        {
                            Debug.Assert(result == buffer.Length, $"GetOverlappedResult returned {result} for {buffer.Length} bytes request");
                            return;
                        }

                        errorCode = FileStreamHelpers.GetLastWin32ErrorAndDisposeHandleIfInvalid(handle);
                    }

                    switch (errorCode)
                    {
                    case Interop.Errors.ERROR_NO_DATA:
                        // For pipes, ERROR_NO_DATA is not an error, but the pipe is closing.
                        return;

                    case Interop.Errors.ERROR_INVALID_PARAMETER:
                        // ERROR_INVALID_PARAMETER may be returned for writes
                        // where the position is too large or for synchronous writes
                        // to a handle opened asynchronously.
                        throw new IOException(SR.IO_FileTooLong);

                    default:
                        throw Win32Marshal.GetExceptionForWin32Error(errorCode, handle.Path);
                    }
                }
            }
            finally
            {
                if (overlapped != null)
                {
                    resetEvent.FreeNativeOverlapped(overlapped);
                }

                resetEvent.Dispose();
            }
        }
Beispiel #18
0
 public static Span <bool> DecodeBools(Span <byte> span)
 {
     return(MemoryMarshal.Cast <byte, bool>(span));
 }
Beispiel #19
0
        public static unsafe IDictionary GetEnvironmentVariables()
        {
            // Format for GetEnvironmentStrings is:
            //     [=HiddenVar=value\0]* [Variable=value\0]* \0
            // See the description of Environment Blocks in MSDN's CreateProcess
            // page (null-terminated array of null-terminated strings). Note
            // the =HiddenVar's aren't always at the beginning.

            // Copy strings out, parsing into pairs and inserting into the table.
            // The first few environment variable entries start with an '='.
            // The current working directory of every drive (except for those drives
            // you haven't cd'ed into in your DOS window) are stored in the
            // environment block (as =C:=pwd) and the program's exit code is
            // as well (=ExitCode=00000000).

            char *stringPtr = Interop.Kernel32.GetEnvironmentStringsW();

            if (stringPtr == null)
            {
                throw new OutOfMemoryException();
            }

            try
            {
                var results = new Hashtable();

                char *currentPtr = stringPtr;
                while (true)
                {
                    ReadOnlySpan <char> variable = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(currentPtr);
                    if (variable.IsEmpty)
                    {
                        break;
                    }

                    // Find the = separating the key and value. We skip entries that begin with =.  We also skip entries that don't
                    // have =, which can happen on some older OSes when the environment block gets corrupted.
                    int i = variable.IndexOf('=');
                    if (i > 0)
                    {
                        // Add the key and value.
                        string key   = new string(variable.Slice(0, i));
                        string value = new string(variable.Slice(i + 1));
                        try
                        {
                            // Add may throw if the environment block was corrupted leading to duplicate entries.
                            // We allow such throws and eat them (rather than proactively checking for duplication)
                            // to provide a non-fatal notification about the corruption.
                            results.Add(key, value);
                        }
                        catch (ArgumentException) { }
                    }

                    // Move to the end of this variable, after its terminator.
                    currentPtr += variable.Length + 1;
                }

                return(results);
            }
            finally
            {
                Interop.BOOL success = Interop.Kernel32.FreeEnvironmentStringsW(stringPtr);
                Debug.Assert(success != Interop.BOOL.FALSE);
            }
        }
 public Span <WCM_PROFILE_INFO> AsSpan(int length) => MemoryMarshal.CreateSpan(ref e0, length);
Beispiel #21
0
		//TODO: some buffer data are reused between primitives, and I duplicate the datas
		//buffers must be constructed without duplications
		public Mesh[] LoadMeshes<TVertex> (VkIndexType indexType, Buffer vbo, ulong vboOffset, Buffer ibo, ulong iboOffset) {
			ulong vCount, iCount;
			VkIndexType idxType;

			GetVertexCount (out vCount, out iCount, out idxType);

			int vertexByteSize = Marshal.SizeOf<TVertex> ();
			ulong vertSize = vCount * (ulong)vertexByteSize;
			ulong idxSize = iCount * (indexType == VkIndexType.Uint16 ? 2ul : 4ul);
			ulong size = vertSize + idxSize;

			int vertexCount = 0, indexCount = 0;
			int autoNamedMesh = 1;

			meshes = new List<Mesh> ();

			using (HostBuffer stagging = new HostBuffer (dev, VkBufferUsageFlags.TransferSrc, size)) {
				stagging.Map ();

				unsafe {

					Span<byte> stagVertPtrInit = new Span<byte>(stagging.MappedData.ToPointer (), (int)vertSize);
					Span<byte> stagIdxPtrInit = new Span<byte>((byte*)stagging.MappedData.ToPointer() + vertSize, (int)idxSize);
					Span<byte> stagVertPtr = stagVertPtrInit, stagIdxPtr = stagIdxPtrInit;

					foreach (GL.Mesh mesh in gltf.Meshes) {

						string meshName = mesh.Name;
						if (string.IsNullOrEmpty (meshName)) {
							meshName = "mesh_" + autoNamedMesh.ToString ();
							autoNamedMesh++;
						}
						Mesh m = new Mesh { Name = meshName };

						foreach (GL.MeshPrimitive p in mesh.Primitives) {
							GL.Accessor AccPos = null, AccNorm = null, AccUv = null, AccUv1 = null;

							int accessorIdx;
							if (p.Attributes.TryGetValue ("POSITION", out accessorIdx)) {
								AccPos = gltf.Accessors[accessorIdx];
								ensureBufferIsLoaded (gltf.BufferViews[(int)AccPos.BufferView].Buffer);
							}
							if (p.Attributes.TryGetValue ("NORMAL", out accessorIdx)) {
								AccNorm = gltf.Accessors[accessorIdx];
								ensureBufferIsLoaded (gltf.BufferViews[(int)AccNorm.BufferView].Buffer);
							}
							if (p.Attributes.TryGetValue ("TEXCOORD_0", out accessorIdx)) {
								AccUv = gltf.Accessors[accessorIdx];
								ensureBufferIsLoaded (gltf.BufferViews[(int)AccUv.BufferView].Buffer);
							}
							if (p.Attributes.TryGetValue ("TEXCOORD_1", out accessorIdx)) {
								AccUv1 = gltf.Accessors[accessorIdx];
								ensureBufferIsLoaded (gltf.BufferViews[(int)AccUv1.BufferView].Buffer);
							}

							Primitive prim = new Primitive {
								indexBase = (uint)indexCount,
								vertexBase = vertexCount,
								vertexCount = (uint)AccPos.Count,
								material = (uint)(p.Material ?? 0)
							};

							prim.bb.min.ImportFloatArray (AccPos.Min);
							prim.bb.max.ImportFloatArray (AccPos.Max);
							prim.bb.isValid = true;

							//Interleaving vertices
							Span<byte> inPosPtr = Span<byte>.Empty, inNormPtr = Span<byte>.Empty, inUvPtr = Span<byte>.Empty, inUv1Ptr = Span<byte>.Empty;

							GL.BufferView bv = gltf.BufferViews[(int)AccPos.BufferView];
							inPosPtr = loadedBuffers[bv.Buffer].Span.Slice (AccPos.ByteOffset + bv.ByteOffset);

							if (AccNorm != null) {
								bv = gltf.BufferViews[(int)AccNorm.BufferView];
								inNormPtr = loadedBuffers[bv.Buffer].Span.Slice (AccNorm.ByteOffset + bv.ByteOffset);
							}
							if (AccUv != null) {
								bv = gltf.BufferViews[(int)AccUv.BufferView];
								inUvPtr = loadedBuffers[bv.Buffer].Span.Slice (AccUv.ByteOffset + bv.ByteOffset);
							}
							if (AccUv1 != null) {
								bv = gltf.BufferViews[(int)AccUv1.BufferView];
								inUv1Ptr = loadedBuffers[bv.Buffer].Span.Slice (AccUv1.ByteOffset + bv.ByteOffset);
							}

							//TODO: use vertex attributes scan for copying data if they exists
							for (int j = 0; j < prim.vertexCount; j++) {
								inPosPtr.Slice (0, 12).CopyTo (stagVertPtr);
								inPosPtr = inPosPtr.Slice(12);
								if (!inNormPtr.IsEmpty) {
									inNormPtr.Slice (0, 12).CopyTo (stagVertPtr.Slice (12));
									inNormPtr = inNormPtr.Slice (12);
								}
								if (inUvPtr != null) {
									inUvPtr.Slice (0, 8).CopyTo (stagVertPtr.Slice (24));
									inUvPtr = inUvPtr.Slice (8);
								}
								if (inUv1Ptr != null) {
									inUv1Ptr.Slice (0, 8).CopyTo (stagVertPtr.Slice (32));
									inUv1Ptr = inUvPtr.Slice (8);
								}
								stagVertPtr = stagVertPtr.Slice (vertexByteSize);
							}

							/*Span<byte> s = stagVertPtrInit;
							for (int i = 0; i < s.Length; i++)
								Console.Write (s[i].ToString ("X2") + (i % 32 == 0 ? "\n" : " "));*/


							//indices loading
							if (p.Indices != null) {
								GL.Accessor acc = gltf.Accessors[(int)p.Indices];
								bv = gltf.BufferViews[(int)acc.BufferView];

								Span<byte> inIdxPtr = loadedBuffers[bv.Buffer].Span.Slice (acc.ByteOffset + bv.ByteOffset);

								//TODO:double check this, I dont seems to increment stag pointer
								if (acc.ComponentType == GL.Accessor.ComponentTypeEnum.UNSIGNED_SHORT) {
									if (indexType == VkIndexType.Uint16) {
										inIdxPtr.Slice (0, acc.Count * 2).CopyTo (stagIdxPtr);
										stagIdxPtr = stagIdxPtr.Slice (acc.Count * 2);
									} else {

										Span<uint> usPtr = MemoryMarshal.Cast<byte, uint> (stagIdxPtr);
										Span<ushort> inPtr = MemoryMarshal.Cast < byte, ushort> (inIdxPtr);
										for (int i = 0; i < acc.Count; i++)
											usPtr[i] = inPtr[i];
										stagIdxPtr = stagIdxPtr.Slice (acc.Count * 4);
									}
								} else if (acc.ComponentType == GL.Accessor.ComponentTypeEnum.UNSIGNED_INT) {
									if (indexType == VkIndexType.Uint32) {
										inIdxPtr.Slice (0, acc.Count * 4).CopyTo (stagIdxPtr);
										stagIdxPtr = stagIdxPtr.Slice (acc.Count * 4);
									} else {
										Span<ushort> usPtr = MemoryMarshal.Cast<byte, ushort> (stagIdxPtr);
										Span<uint> inPtr = MemoryMarshal.Cast<byte, uint> (inIdxPtr);

										for (int i = 0; i < acc.Count; i++) 
											usPtr[i] = (ushort)inPtr[i];
										stagIdxPtr = stagIdxPtr.Slice (acc.Count * 2);
									}
								} else if (acc.ComponentType == GL.Accessor.ComponentTypeEnum.UNSIGNED_BYTE) {
									//convert
									if (indexType == VkIndexType.Uint16) {
										Span<ushort> usPtr = MemoryMarshal.Cast<byte, ushort> (stagIdxPtr);
										for (int i = 0; i < acc.Count; i++)
											usPtr[i] = (ushort)inIdxPtr[i];
										stagIdxPtr = stagIdxPtr.Slice (acc.Count * 2);
									} else {
										Span<uint> usPtr = MemoryMarshal.Cast<byte, uint> (stagIdxPtr);
										for (int i = 0; i < acc.Count; i++)
											usPtr[i] = (uint)inIdxPtr[i];
										stagIdxPtr = stagIdxPtr.Slice (acc.Count * 4);
									}
								} else
									throw new NotImplementedException ();

								prim.indexCount = (uint)acc.Count;
								indexCount += acc.Count;
							}

							m.AddPrimitive (prim);

							vertexCount += AccPos.Count;
						}
						meshes.Add (m);
					}

					/*ReadOnlySpan<byte> tmp = new ReadOnlySpan<byte> (stagging.MappedData.ToPointer (), (int)size);
					Memory<byte> mtmp = new Memory<byte> (tmp.ToArray());
					mtmp.Dump();*/
				}

				stagging.Unmap ();

				PrimaryCommandBuffer cmd = cmdPool.AllocateCommandBuffer ();
				cmd.Start (VkCommandBufferUsageFlags.OneTimeSubmit);

				stagging.CopyTo (cmd, vbo, vertSize, 0, vboOffset);
				if (iCount>0)
					stagging.CopyTo (cmd, ibo, idxSize, vertSize, iboOffset);

				cmd.End ();

				transferQ.Submit (cmd);

				dev.WaitIdle ();
				cmd.Free ();

			}

			return meshes.ToArray ();
		}
Beispiel #22
0
 public T ParseColumnData <T>()
     where T : struct
 {
     return(MemoryMarshal.Read <T>(RowData.Span));
 }
 internal static unsafe ErrorCode NCryptVerifySignature(SafeNCryptKeyHandle hKey, void *pPaddingInfo, ReadOnlySpan <byte> pbHashValue, int cbHashValue, ReadOnlySpan <byte> pbSignature, int cbSignature, AsymmetricPaddingMode dwFlags) =>
 NCryptVerifySignature(hKey, pPaddingInfo, ref MemoryMarshal.GetReference(pbHashValue), cbHashValue, ref MemoryMarshal.GetReference(pbSignature), cbSignature, dwFlags);
Beispiel #24
0
        /// <inheritdoc/>
        public override void TransferFullDuplex(ReadOnlySpan <byte> writeBuffer, Span <byte> readBuffer)
        {
            ushort readBytes;
            var    ftStatus = FtFunction.FT4222_SPIMaster_SingleReadWrite(_ftHandle,
                                                                          in MemoryMarshal.GetReference(readBuffer), in MemoryMarshal.GetReference(writeBuffer),
                                                                          (ushort)writeBuffer.Length, out readBytes, true);

            if (ftStatus != FtStatus.Ok)
            {
                throw new IOException($"{nameof(TransferFullDuplex)} failed to do a full duplex transfer, error: {ftStatus}");
            }
        }
Beispiel #25
0
 static ref T head <N, T>(BlockVector <N, T> src)
     where N : ITypeNat, new()
     where T : struct
 => ref MemoryMarshal.GetReference <T>(src.Unsized);
Beispiel #26
0
        /// <summary>
        /// Updates host shaders based on the guest GPU state.
        /// </summary>
        /// <param name="state">Current GPU state</param>
        private void UpdateShaderState(GpuState state)
        {
            ShaderAddresses addresses = new ShaderAddresses();

            Span <ShaderAddresses> addressesSpan = MemoryMarshal.CreateSpan(ref addresses, 1);

            Span <ulong> addressesArray = MemoryMarshal.Cast <ShaderAddresses, ulong>(addressesSpan);

            ulong baseAddress = state.Get <GpuVa>(MethodOffset.ShaderBaseAddress).Pack();

            for (int index = 0; index < 6; index++)
            {
                var shader = state.Get <ShaderState>(MethodOffset.ShaderState, index);

                if (!shader.UnpackEnable() && index != 1)
                {
                    continue;
                }

                addressesArray[index] = baseAddress + shader.Offset;
            }

            ShaderBundle gs = ShaderCache.GetGraphicsShader(state, addresses);

            byte oldVsClipDistancesWritten = _vsClipDistancesWritten;

            _vsUsesInstanceId       = gs.Shaders[0]?.Info.UsesInstanceId ?? false;
            _vsClipDistancesWritten = gs.Shaders[0]?.Info.ClipDistancesWritten ?? 0;

            if (oldVsClipDistancesWritten != _vsClipDistancesWritten)
            {
                UpdateUserClipState(state);
            }

            int storageBufferBindingsCount = 0;
            int uniformBufferBindingsCount = 0;

            for (int stage = 0; stage < Constants.ShaderStages; stage++)
            {
                ShaderProgramInfo info = gs.Shaders[stage]?.Info;

                _currentProgramInfo[stage] = info;

                if (info == null)
                {
                    TextureManager.SetGraphicsTextures(stage, Array.Empty <TextureBindingInfo>());
                    TextureManager.SetGraphicsImages(stage, Array.Empty <TextureBindingInfo>());
                    BufferManager.SetGraphicsStorageBufferBindings(stage, null);
                    BufferManager.SetGraphicsUniformBufferBindings(stage, null);
                    continue;
                }

                var textureBindings = new TextureBindingInfo[info.Textures.Count];

                for (int index = 0; index < info.Textures.Count; index++)
                {
                    var descriptor = info.Textures[index];

                    Target target = ShaderTexture.GetTarget(descriptor.Type);

                    textureBindings[index] = new TextureBindingInfo(
                        target,
                        descriptor.Binding,
                        descriptor.CbufSlot,
                        descriptor.HandleIndex,
                        descriptor.Flags);
                }

                TextureManager.SetGraphicsTextures(stage, textureBindings);

                var imageBindings = new TextureBindingInfo[info.Images.Count];

                for (int index = 0; index < info.Images.Count; index++)
                {
                    var descriptor = info.Images[index];

                    Target target = ShaderTexture.GetTarget(descriptor.Type);
                    Format format = ShaderTexture.GetFormat(descriptor.Format);

                    imageBindings[index] = new TextureBindingInfo(
                        target,
                        format,
                        descriptor.Binding,
                        descriptor.CbufSlot,
                        descriptor.HandleIndex,
                        descriptor.Flags);
                }

                TextureManager.SetGraphicsImages(stage, imageBindings);

                BufferManager.SetGraphicsStorageBufferBindings(stage, info.SBuffers);
                BufferManager.SetGraphicsUniformBufferBindings(stage, info.CBuffers);

                if (info.SBuffers.Count != 0)
                {
                    storageBufferBindingsCount = Math.Max(storageBufferBindingsCount, info.SBuffers.Max(x => x.Binding) + 1);
                }

                if (info.CBuffers.Count != 0)
                {
                    uniformBufferBindingsCount = Math.Max(uniformBufferBindingsCount, info.CBuffers.Max(x => x.Binding) + 1);
                }
            }

            BufferManager.SetGraphicsStorageBufferBindingsCount(storageBufferBindingsCount);
            BufferManager.SetGraphicsUniformBufferBindingsCount(uniformBufferBindingsCount);

            _context.Renderer.Pipeline.SetProgram(gs.HostProgram);
        }
Beispiel #27
0
 static ref T head <T>(Span <T> src)
 => ref MemoryMarshal.GetReference <T>(src);
Beispiel #28
0
 private static void Write(ref Span <byte> span, ushort value)
 {
     MemoryMarshal.Write(span, ref value);
     span = span.Slice(2);
 }
Beispiel #29
0
 internal static T ParseBySpanCast <T>(ReadOnlySpan <byte> buffer) where T : struct
 {
     return(MemoryMarshal.Cast <byte, T>(buffer)[0]);
 }
Beispiel #30
0
        /// <summary>
        /// Compute CRC32C for data
        /// </summary>
        /// <param name="input">input data</param>
        /// <param name="offset">offset</param>
        /// <param name="length">length</param>
        /// <returns>CRC32C checksum</returns>
        public static uint Compute(byte[] input, int offset, int length)
        {
            uint crcLocal = uint.MaxValue;

#if NETCOREAPP3_0_OR_GREATER || NETCOREAPP3_1 || NET5_0
            if (Sse42.IsSupported)
            {
                var data      = new ReadOnlySpan <byte>(input, offset, length);
                int processed = 0;
                if (Sse42.X64.IsSupported && data.Length > sizeof(ulong))
                {
                    processed = data.Length / sizeof(ulong) * sizeof(ulong);
                    var   ulongs  = MemoryMarshal.Cast <byte, ulong>(data.Slice(0, processed));
                    ulong crclong = crcLocal;
                    for (int i = 0; i < ulongs.Length; i++)
                    {
                        crclong = Sse42.X64.Crc32(crclong, ulongs[i]);
                    }

                    crcLocal = (uint)crclong;
                }
                else if (data.Length > sizeof(uint))
                {
                    processed = data.Length / sizeof(uint) * sizeof(uint);
                    var uints = MemoryMarshal.Cast <byte, uint>(data.Slice(0, processed));
                    for (int i = 0; i < uints.Length; i++)
                    {
                        crcLocal = Sse42.Crc32(crcLocal, uints[i]);
                    }
                }

                for (int i = processed; i < data.Length; i++)
                {
                    crcLocal = Sse42.Crc32(crcLocal, data[i]);
                }

                return(crcLocal ^ uint.MaxValue);
            }
#endif
#if NET5_0_OR_GREATER || NET5_0
            if (Crc32.IsSupported)
            {
                var data      = new ReadOnlySpan <byte>(input, offset, length);
                int processed = 0;
                if (Crc32.Arm64.IsSupported && data.Length > sizeof(ulong))
                {
                    processed = data.Length / sizeof(ulong) * sizeof(ulong);
                    var ulongs = MemoryMarshal.Cast <byte, ulong>(data.Slice(0, processed));
                    for (int i = 0; i < ulongs.Length; i++)
                    {
                        crcLocal = Crc32.Arm64.ComputeCrc32C(crcLocal, ulongs[i]);
                    }
                }
                else if (data.Length > sizeof(uint))
                {
                    processed = data.Length / sizeof(uint) * sizeof(uint);
                    var uints = MemoryMarshal.Cast <byte, uint>(data.Slice(0, processed));
                    for (int i = 0; i < uints.Length; i++)
                    {
                        crcLocal = Crc32.ComputeCrc32C(crcLocal, uints[i]);
                    }
                }

                for (int i = processed; i < data.Length; i++)
                {
                    crcLocal = Crc32.ComputeCrc32C(crcLocal, data[i]);
                }

                return(crcLocal ^ uint.MaxValue);
            }
#endif
            while (length >= 16)
            {
                var a = Table[(3 * 256) + input[offset + 12]]
                        ^ Table[(2 * 256) + input[offset + 13]]
                        ^ Table[(1 * 256) + input[offset + 14]]
                        ^ Table[(0 * 256) + input[offset + 15]];

                var b = Table[(7 * 256) + input[offset + 8]]
                        ^ Table[(6 * 256) + input[offset + 9]]
                        ^ Table[(5 * 256) + input[offset + 10]]
                        ^ Table[(4 * 256) + input[offset + 11]];

                var c = Table[(11 * 256) + input[offset + 4]]
                        ^ Table[(10 * 256) + input[offset + 5]]
                        ^ Table[(9 * 256) + input[offset + 6]]
                        ^ Table[(8 * 256) + input[offset + 7]];

                var d = Table[(15 * 256) + ((byte)crcLocal ^ input[offset])]
                        ^ Table[(14 * 256) + ((byte)(crcLocal >> 8) ^ input[offset + 1])]
                        ^ Table[(13 * 256) + ((byte)(crcLocal >> 16) ^ input[offset + 2])]
                        ^ Table[(12 * 256) + ((crcLocal >> 24) ^ input[offset + 3])];

                crcLocal = d ^ c ^ b ^ a;
                offset  += 16;
                length  -= 16;
            }
            while (--length >= 0)
            {
                crcLocal = Table[(byte)(crcLocal ^ input[offset++])] ^ crcLocal >> 8;
            }
            return(crcLocal ^ uint.MaxValue);
        }