Skip to content

Anthony/ixsb 1096 virtual mouse #2175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,8 @@ public void ResetActionState(int actionIndex, InputActionPhase toPhase = InputAc
// Wipe state.
actionState->phase = toPhase;
actionState->controlIndex = kInvalidIndex;
actionState->bindingIndex = memory.actionBindingIndices[memory.actionBindingIndicesAndCounts[actionIndex]];
var idx = memory.actionBindingIndicesAndCounts[actionIndex];
actionState->bindingIndex = memory.actionBindingIndices != null ? memory.actionBindingIndices[idx] : 0;
actionState->interactionIndex = kInvalidIndex;
actionState->startTime = 0;
actionState->time = 0;
Expand Down Expand Up @@ -2879,6 +2880,9 @@ internal TValue ReadValue<TValue>(int bindingIndex, int controlIndex, bool ignor
internal TValue ApplyProcessors<TValue>(int bindingIndex, TValue value, InputControl<TValue> controlOfType = null)
where TValue : struct
{
if (totalBindingCount == 0)
return value;

var processorCount = bindingStates[bindingIndex].processorCount;
if (processorCount > 0)
{
Expand Down Expand Up @@ -4139,6 +4143,19 @@ public struct UnmanagedMemory : IDisposable

public ActionMapIndices* mapIndices;

// This is Anthony's version of block allocation from a blob
private byte* AllocFromBlob(ref byte* top, int size)
{
if (size == 0)
return null;
Copy link
Collaborator Author

@K-Tone K-Tone Apr 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The largest diff here is this size check, which would guarantee that a zero-size array would be null by design. Priorly zero-sized data would cause invalid non-null pointers. So code that wasn't accurately written to check for bounds would access random memory. This way we might get some crashes more apparent, which is good as otherwise it's super tricky to debug code that uses garbage.


var allocation = top;

top += size;

return allocation;
}

public void Allocate(int mapCount, int actionCount, int bindingCount, int controlCount, int interactionCount, int compositeCount)
{
Debug.Assert(basePtr == null, "Memory already allocated! Free first!");
Expand All @@ -4164,17 +4181,17 @@ public void Allocate(int mapCount, int actionCount, int bindingCount, int contro
// NOTE: This depends on the individual structs being sufficiently aligned in order to not
// cause any misalignment here. TriggerState, InteractionState, and BindingState all
// contain doubles so put them first in memory to make sure they get proper alignment.
actionStates = (TriggerState*)ptr; ptr += actionCount * sizeof(TriggerState);
interactionStates = (InteractionState*)ptr; ptr += interactionCount * sizeof(InteractionState);
bindingStates = (BindingState*)ptr; ptr += bindingCount * sizeof(BindingState);
mapIndices = (ActionMapIndices*)ptr; ptr += mapCount * sizeof(ActionMapIndices);
controlMagnitudes = (float*)ptr; ptr += controlCount * sizeof(float);
compositeMagnitudes = (float*)ptr; ptr += compositeCount * sizeof(float);
controlIndexToBindingIndex = (int*)ptr; ptr += controlCount * sizeof(int);
controlGroupingAndComplexity = (ushort*)ptr; ptr += controlCount * sizeof(ushort) * 2;
actionBindingIndicesAndCounts = (ushort*)ptr; ptr += actionCount * sizeof(ushort) * 2;
actionBindingIndices = (ushort*)ptr; ptr += bindingCount * sizeof(ushort);
enabledControls = (int*)ptr; ptr += (controlCount + 31) / 32 * sizeof(int);
actionStates = (TriggerState*)AllocFromBlob(ref ptr, actionCount * sizeof(TriggerState));
interactionStates = (InteractionState*)AllocFromBlob(ref ptr, interactionCount * sizeof(InteractionState));
bindingStates = (BindingState*)AllocFromBlob(ref ptr, bindingCount * sizeof(BindingState));
mapIndices = (ActionMapIndices*)AllocFromBlob(ref ptr, mapCount * sizeof(ActionMapIndices));
controlMagnitudes = (float*)AllocFromBlob(ref ptr, controlCount * sizeof(float));
compositeMagnitudes = (float*)AllocFromBlob(ref ptr, compositeCount * sizeof(float));
controlIndexToBindingIndex = (int*)AllocFromBlob(ref ptr, controlCount * sizeof(int));
controlGroupingAndComplexity = (ushort*)AllocFromBlob(ref ptr, controlCount * sizeof(ushort) * 2);
actionBindingIndicesAndCounts = (ushort*)AllocFromBlob(ref ptr, actionCount * sizeof(ushort) * 2);
actionBindingIndices = (ushort*)AllocFromBlob(ref ptr, bindingCount * sizeof(ushort));
enabledControls = (int*)AllocFromBlob(ref ptr, (controlCount + 31) / 32 * sizeof(int));
}

public void Dispose()
Expand Down
Loading