Skip to content

Commit 0a5b2b9

Browse files
committed
front-load timestamp calculations and perform no sorting on inputs
1 parent 81bc3a5 commit 0a5b2b9

23 files changed

Lines changed: 319 additions & 482 deletions

‎sources/Input/Input/Implementations/EnumInfo.cs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44

55
using System.Collections.Concurrent;
6-
using System.Diagnostics;
76
using System.Runtime.CompilerServices;
87

98
namespace Silk.NET.Input;

‎sources/Input/Input/Implementations/KeyHandling/TextRecorder.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Diagnostics;
55
using System.Diagnostics.CodeAnalysis;
6+
using System.Runtime.CompilerServices;
67
using System.Text;
78
using Silk.NET.SDL;
89

@@ -256,6 +257,7 @@ public void InsertTextAt(Ptr<sbyte> textPtr, int cursorStart)
256257
/// <param name="textPtr"></param>
257258
/// <param name="cursorStart">The cursor position in the buffer to inject</param>
258259
/// <param name="textLength"></param>
260+
[SkipLocalsInit]
259261
public void InsertTextAt(Ptr<sbyte> textPtr, int cursorStart, int textLength)
260262
{
261263
Span<char> textSpan = stackalloc char[textLength];
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Runtime.CompilerServices;
5+
6+
namespace Silk.NET.Input.SDL3.DataStructures;
7+
8+
internal readonly record struct GenericEvent(object Queue, int Index, SdlEventDiscriminator Type)
9+
{
10+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
11+
public ref readonly T Value<T>() where T : struct => ref ((SdlInputEventQueue<T>)Queue).UnsafeGetRef(Index);
12+
}
Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Diagnostics;
54
using System.Runtime.CompilerServices;
65

76
namespace Silk.NET.Input.SDL3.DataStructures;
87

98
internal sealed class SdlInputEventQueue<T> where T : struct
109
{
11-
public static readonly SdlEventDiscriminator TypeDiscriminator = InputEventKinds.Get(typeof(T));
10+
private static readonly SdlEventDiscriminator _typeDiscriminator = InputEventKinds.Get(typeof(T));
1211

13-
public int Count => _events.Count;
12+
/// <summary>
13+
/// The "master" list of events we append each of our events to
14+
/// </summary>
15+
private readonly List<GenericEvent> _genericEvents;
16+
17+
public SdlInputEventQueue(List<GenericEvent> genericEventQueue) => _genericEvents = genericEventQueue;
1418

15-
public void Enqueue(in T item, ulong sdlTimestamp)
19+
public void Enqueue(in T item)
1620
{
1721
if (_disposed)
1822
{
1923
GC.ReRegisterForFinalize(this);
2024
_disposed = false;
2125
}
2226

27+
_genericEvents.Add(new GenericEvent(this, _events.Count, _typeDiscriminator));
2328
_events.Add(item);
24-
_sdlTimestamps.Add(sdlTimestamp);
25-
}
26-
27-
public ReadOnlySpan<ulong> SdlTimestamps
28-
{
29-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
30-
get => _sdlTimestamps.AsSpan();
3129
}
3230

3331
/// <inheritdoc cref="PinnedGcMemory{T}.UnsafeGetRef"/>
@@ -43,7 +41,6 @@ public void Dispose()
4341
}
4442

4543
_disposed = true;
46-
_sdlTimestamps.Dispose();
4744
_events.Dispose();
4845
GC.SuppressFinalize(this);
4946
}
@@ -52,48 +49,16 @@ public void Dispose()
5249
{
5350
if (!_disposed)
5451
{
55-
_sdlTimestamps.Dispose();
5652
_events.Dispose();
5753
}
5854
}
5955

60-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
61-
internal SdlTimestampedValues<T> AsSpanPair() => new(_events.AsSpan(), _sdlTimestamps.AsSpan());
62-
63-
64-
internal readonly ref struct SdlTimestampedValues<TValue> where TValue : struct
65-
{
66-
public readonly Span<TValue> Values;
67-
public readonly Span<ulong> SdlTimestamps;
68-
69-
public SdlTimestampedValues(Span<TValue> values,
70-
Span<ulong> sdlTimestamps)
71-
{
72-
Values = values;
73-
SdlTimestamps = sdlTimestamps;
74-
}
75-
76-
public int Length
77-
{
78-
get
79-
{
80-
Debug.Assert(Values.Length == SdlTimestamps.Length);
81-
return Values.Length;
82-
}
83-
}
84-
}
85-
8656
private bool _disposed;
8757
private PinnedGcMemory<T> _events;
88-
private PinnedGcMemory<ulong> _sdlTimestamps;
8958

9059
/// <summary>
9160
/// Resets the count of our events without explicitly clearing the memory
9261
/// </summary>
9362
[MethodImpl(MethodImplOptions.AggressiveInlining)]
94-
public void ResetCount()
95-
{
96-
_events.ResetCount();
97-
_sdlTimestamps.ResetCount();
98-
}
63+
public void ResetCount() => _events.ResetCount();
9964
}

‎sources/Input/Input/Implementations/SDL3/Devices/ISdlDevice.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Silk.NET.Input.SDL3;
99
/// <typeparam name="T"></typeparam>
1010
internal interface ISdlDevice<out T> : IInputDevice where T : SdlDevice
1111
{
12-
public static abstract T? CreateDevice(ulong sdlDeviceId, long timestamp, ulong sdlTimestamp, bool isSimulated, SdlInputBackend backend, SdlInputEventContext context);
12+
public static abstract T? CreateDevice(ulong sdlDeviceId, long timestamp, bool isSimulated, SdlInputBackend backend, SdlInputEventContext context);
1313
}
1414

1515
internal interface INeedCompletionEachFrame

‎sources/Input/Input/Implementations/SDL3/Devices/Joysticks/ISdlJoystick.cs‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,22 @@ internal interface ISdlJoystick : IOrderedDevice
2020
/// </summary>
2121
/// <param name="axis">Input axis (which axis)</param>
2222
/// <param name="joystickInput">Input axis value</param>
23-
/// <param name="sdlTimestamp"></param>
2423
/// <param name="timestamp"></param>
25-
public void UpdateFromJoyAxis(int axis, short joystickInput, ulong sdlTimestamp, long timestamp);
24+
public void UpdateFromJoyAxis(int axis, short joystickInput, long timestamp);
2625

2726
/// <summary>
2827
/// Raw joystick hat input events are forwarded here
2928
/// </summary>
3029
/// <param name="hatIdx">Input hat (which hat)</param>
3130
/// <param name="hatState">Input hat value</param>
32-
/// <param name="sdlTimestamp"></param>
3331
/// <param name="timestamp"></param>
34-
public void UpdateFromJoyHat(int hatIdx, SdlJoystick.HatState hatState, ulong sdlTimestamp, long timestamp);
32+
public void UpdateFromJoyHat(int hatIdx, SdlJoystick.HatState hatState, long timestamp);
3533

3634
/// <summary>
3735
/// Raw joystick button input events are forwarded here
3836
/// </summary>
3937
/// <param name="buttonIdx">Input button (which button)</param>
4038
/// <param name="down">Button state</param>
41-
/// <param name="sdlTimestamp"></param>
4239
/// <param name="timestamp"></param>
43-
public void UpdateFromJoyButton(int buttonIdx, bool down, ulong sdlTimestamp, long timestamp);
40+
public void UpdateFromJoyButton(int buttonIdx, bool down, long timestamp);
4441
}

0 commit comments

Comments
 (0)