More easy optimizations in skeleton

from 23% to 15.5%!
This commit is contained in:
Erimel committed 2026-10-23 17:05:15 -04:00
1 parent 9aa78e8f87
commit 7fb25632d0
9 files changed
+34 -46

No files matched your search

@@ -257,13 +257,15 @@ class ComputedSkeletonBehaviour(
}
// Use single map for the inputs that we'll mutate for performance
val mutableBoneInputs: InputSkeleton = BodyPartMap(if (targetState.pausedProcessedBoneInputs != null) {
// If tracking is paused, we only run the skeleton loop for the always active bones (head and neck)
// and add the pausedProcessedBoneInputs later on.
targetState.boneInputs.filter { it.key in ALWAYS_ACTIVE_PARTS }
} else {
targetState.boneInputs
})
val mutableBoneInputs: InputSkeleton = BodyPartMap(
if (targetState.pausedProcessedBoneInputs != null) {
// If tracking is paused, we only run the skeleton loop for the always active bones (head and neck)
// and add the pausedProcessedBoneInputs later on.
targetState.boneInputs.filter { it.key in ALWAYS_ACTIVE_PARTS }
} else {
targetState.boneInputs
},
)
// Run pre-FK processors (input processors)
for (processor in inputProcessors) processor.process(mutableBoneInputs, targetState.skeletonHeight)
@@ -306,6 +308,7 @@ class ComputedSkeletonBehaviour(
for (processor in targetProcessors) processor.process(ikTargets, mutableBoneInputs, mutableComputedSkeleton, targetState.floorLevel)
// Run IK
// TODO to optimize we could pass previous post-IK mutableBoneInputs as starting point
ccdIk(
mutableBoneInputs,
mutableComputedSkeleton,
@@ -8,8 +8,6 @@ import dev.slimevr.skeleton.SkeletonComputedProcessor
import dev.slimevr.skeleton.Velocity
import dev.slimevr.skeleton.ZERO_VELOCITY
import dev.slimevr.skeleton.bodyPartMap
import dev.slimevr.skeleton.findFirstParent
import dev.slimevr.skeleton.forEachBone
import dev.slimevr.util.inFloatingSeconds
import dev.slimevr.util.millisecondsInSecond
import dev.slimevr.util.timeSource
@@ -58,26 +56,20 @@ class VelocityComputedProcessor(
val deltaTime = (now - lastProcessTime).inFloatingSeconds
lastProcessTime = now
mutableComputedSkeleton.forEachBone { part, bone ->
// The expected TPS for the bone is its input's or its first parent that has one.
// If there's no available expected TPS, that means there's no movement to derive velocity from anyways.
val expectedTps = inputSkeleton[part]?.expectedTps
?: inputSkeleton[part.findFirstParent { inputSkeleton[it]?.expectedTps != null }]?.expectedTps
?: return@forEachBone
for ((bodyPart, bone) in mutableComputedSkeleton) {
// Compute current velocity
val currentVelocityData = VelocityData(bone.rotation, bone.tailPosition)
val currentVelocity = lastVelocityData[part]?.let { computeVelocity(currentVelocityData, it, deltaTime) } ?: ZERO_VELOCITY
val currentVelocity = lastVelocityData[bodyPart]?.let { computeVelocity(currentVelocityData, it, deltaTime) } ?: ZERO_VELOCITY
val newVelocity = if (smooth) {
smoothVelocity(currentVelocity, lastVelocities[part] ?: ZERO_VELOCITY, deltaTime)
smoothVelocity(currentVelocity, lastVelocities[bodyPart] ?: ZERO_VELOCITY, deltaTime)
} else {
currentVelocity
}
mutableComputedSkeleton[part] = bone.copy(velocity = newVelocity)
mutableComputedSkeleton[bodyPart] = bone.copy(velocity = newVelocity)
lastVelocityData[part] = currentVelocityData
lastVelocities[part] = newVelocity
lastVelocityData[bodyPart] = currentVelocityData
lastVelocities[bodyPart] = newVelocity
}
}
@@ -46,7 +46,7 @@ val alwaysActiveBodyParts = arrayOf(
BodyPart.NECK,
BodyPart.HEAD,
)
fun getActiveBodyParts(inputs: InputSkeleton) = inputs.filter { it.value.isRotationActive }.map { it.key } + alwaysActiveBodyParts
fun getActiveBodyParts(inputs: InputSkeleton) = alwaysActiveBodyParts + inputs.filter { it.value.isRotationActive }.map { it.key }
// Follows a bone touching that floor that is considered planted
object FloorLocalizer {
@@ -113,12 +113,10 @@ fun ccdIk(
if (goals.isEmpty()) return
for (i in 0..maxIterations) {
// Iterate chains not meeting the threshold
goals.filter {
chainDistanceFromTarget(mutableComputedSkeleton, it.chain, it.target) > threshold
}.ifEmpty {
break
}.forEach { goal ->
for (goal in goals) {
// Only iterate chains not meeting the threshold
if (chainDistanceFromTarget(mutableComputedSkeleton, goal.chain, goal.target) <= threshold) break
// The chain from the current bone to the end, iterating backwards
val iterationChain = goal.chain.takeLast((i % goal.chain.size) + 1)
ccdIkIteration(
@@ -3,7 +3,6 @@ package dev.slimevr.skeleton.inputprocessors
import dev.slimevr.skeleton.InputSkeleton
import dev.slimevr.skeleton.SkeletonInputProcessor
import dev.slimevr.skeleton.findFirstParent
import dev.slimevr.skeleton.forEachBone
import solarxr_protocol.datatypes.BodyPart
import kotlin.collections.set
@@ -61,8 +60,8 @@ val CHILD_FALLBACKS = mapOf(
*/
class FallbackInputProcessor : SkeletonInputProcessor {
override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) {
mutableInputSkeleton.forEach { (bodyPart, bone) ->
if (bone.isRotationActive && bone.isAccelerationActive) return@forEach
for ((bodyPart, bone) in mutableInputSkeleton) {
if (bone.isRotationActive && bone.isAccelerationActive) continue
val newRotation = if (!bone.isRotationActive) {
val fallbackPart = bodyPart.findFirstParent { mutableInputSkeleton[it]?.isRotationActive == true }
?: CHILD_FALLBACKS[bodyPart]?.firstOrNull { mutableInputSkeleton[it]?.isRotationActive == true }
@@ -6,7 +6,6 @@ import dev.slimevr.skeleton.InputSkeleton
import dev.slimevr.skeleton.ResettableSkeletonProcessor
import dev.slimevr.skeleton.SkeletonInputProcessor
import dev.slimevr.skeleton.bodyPartMap
import dev.slimevr.skeleton.forEachBone
import dev.slimevr.util.MonotonicValueTimeMark
import dev.slimevr.util.timeSource
import io.github.axisangles.ktmath.Quaternion
@@ -56,13 +55,13 @@ class RotationPredictionInputProcessor(val settings: Settings) :
val now = timeSource.markNow()
val newVelocities = bodyPartMap<BoneDelta>()
mutableInputSkeleton.forEachBone { bodyPart, bone ->
if (!bone.isRotationActive) return@forEachBone
for ((bodyPart, bone) in mutableInputSkeleton) {
if (!bone.isRotationActive) continue
val prev = deltas[bodyPart]
if (prev == null) {
newVelocities[bodyPart] = BoneDelta(bone.rotation, Quaternion.IDENTITY, now)
return@forEachBone
continue
}
val bonePredictionAmount = filteringAmount * getMultiplier(bodyPart)
@@ -6,7 +6,6 @@ import dev.slimevr.skeleton.InputSkeleton
import dev.slimevr.skeleton.ResettableSkeletonProcessor
import dev.slimevr.skeleton.SkeletonInputProcessor
import dev.slimevr.skeleton.bodyPartMap
import dev.slimevr.skeleton.forEachBone
import dev.slimevr.util.inFloatingSeconds
import dev.slimevr.util.timeSource
import io.github.axisangles.ktmath.Quaternion
@@ -45,8 +44,8 @@ class RotationSmoothingInputProcessor(val settings: Settings) :
val alpha = ((1 - smoothingAmount) * lastFrameTimeSeconds * SMOOTHING_MULTIPLIER).coerceIn(0f, 1f)
val newSmoothed = bodyPartMap<Quaternion>()
mutableInputSkeleton.forEachBone { bodyPart, bone ->
if (!bone.isRotationActive) return@forEachBone
for ((bodyPart, bone) in mutableInputSkeleton) {
if (!bone.isRotationActive) continue
val prev = smoothed[bodyPart] ?: bone.rotation
val rotation = prev.lerpQ(bone.rotation, alpha).unit()
@@ -2,17 +2,16 @@ package dev.slimevr.skeleton.inputprocessors
import dev.slimevr.skeleton.InputSkeleton
import dev.slimevr.skeleton.SkeletonInputProcessor
import io.github.axisangles.ktmath.Vector3
/**
* Handles tracker offset. For example, an HMD's rotation will be about 10cm in front of the head bone.
*/
class TrackerOffsetInputProcessor : SkeletonInputProcessor {
override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) {
mutableInputSkeleton.forEach { (part, input) ->
if (input.trackerOffset.isZero()) return@forEach
val localOffset = input.rotation.sandwich(input.trackerOffset)
mutableInputSkeleton[part] = input.copy(position = input.position?.plus(localOffset))
for ((bodyPart, bone) in mutableInputSkeleton) {
if (bone.trackerOffset.isZero()) continue
val localOffset = bone.rotation.sandwich(bone.trackerOffset)
mutableInputSkeleton[bodyPart] = bone.copy(position = bone.position?.plus(localOffset))
}
}
}
@@ -5,7 +5,6 @@ import dev.slimevr.skeleton.InputSkeleton
import dev.slimevr.skeleton.ResettableSkeletonProcessor
import dev.slimevr.skeleton.SkeletonInputProcessor
import dev.slimevr.skeleton.bodyPartMap
import dev.slimevr.skeleton.forEachBone
import dev.slimevr.util.inFloatingSeconds
import dev.slimevr.util.timeSource
import io.github.axisangles.ktmath.Quaternion
@@ -42,8 +41,8 @@ class UpsamplingInputProcessor :
val deltaTime = lastProcessTime.elapsedNow().inFloatingSeconds
lastProcessTime = timeSource.markNow()
mutableInputSkeleton.forEachBone { bodyPart, bone ->
if (bone.expectedTps == null) return@forEachBone
for ((bodyPart, bone) in mutableInputSkeleton) {
if (bone.expectedTps == null) continue
// For a 100hz tracker on 500hz skeleton, this will be 0.2f (interpolating 1/5 every frame)
val interpolationRatio = (deltaTime * bone.expectedTps.toFloat()).coerceAtMost(1f)