Add VITURE XR controls, head tracking, and SteamVR (#732)

* Add VITURE glasses support: hardware control, 3D, head tracking

Glasses get their own controller over the MCU (permission, 120 Hz default, brightness, volume, sunblock, 2D/3D) and the game moves onto them when plugged in. The IMU drives Cardboard head tracking and the SteamVR driver. 3D falls back to one eye's view on phones that can't send the 3840x1080 signal. Options open from a glasses button in game and on the Steam page.

* Drop the glasses settings like Steam's: Play-style button, Settings lip, tabs

* Hang the glasses Settings lip over the top of the button

* Space glasses MCU commands so the IMU rate sticks, fit fixed resolutions to the glasses

* Keep Steam alive with SteamVR registered, run SteamVR at the glasses' rate, keep the glasses lip on top

* Fit VR games on the glasses: full view, seated origin at the head, pad hands reach with the d-pad

* Let the pad hands reach a metre out so buttons with deep colliders press

* Show every VR frame in order at 120 fps: wait for SteamVR's GPU work, present only finished copies, skip the grace on idle shared counters

* Glasses: SteamVR starts and stops with them, closer view with a field of view setting, Recenter on the pad

* Glasses: SteamVR field of view defaults to 90°

* VR pad hands: sticks move and aim the hands, left stick click switches the sticks to the game, d-pad pulls a held hand

* Glasses: Play SteamVR beside the glasses button instead of starting SteamVR with them, pad hands drawn as Index controllers and off while other VR controllers are connected, glasses sheet tabs sized like Steam's

* Track glasses mode, refresh-aware glasses window, align Play SteamVR with Play Steam

* Fix 3D on the glasses: full-res SteamVR presenter, revert VR 3D at session end, coalesce vsync ticks, no session restart after end

* Address review: glasses output after installs and session end, VR revert retry, display fallback, presenter swapchain retry, agent hex checks, drawer bumpers, glasses translations, docs

* Harden VITURE stereo negotiation and output refresh tracking

* Localize stereo recovery feedback and record Astra reconnect tests

* Improve test failure diagnostics and refresh Astra firmware findings

* Handle VITURE docks separately and reject truncated USB frames

* Glasses: remember a 3D fallback per model, docks as display only, VR timing fixes

- A 3D attempt that ends at the 640x480 fallback is remembered for that glasses
  model, so Play SteamVR no longer blacks out the glasses after every restart;
  Retry clears it and "3D for VR games" stays reachable
- A VITURE Mobile Dock on its own no longer counts as glasses; through a dock
  Play SteamVR is hidden and the sheet shows the output and why there is no control
- HDR is probed on the glasses when the session starts there
- Glasses frames with a mismatched checksum are marked in the status frame log
- SteamVR layer and driver vblank counters stay monotonic across refresh changes;
  one-eye render scale applies from activation and on mono/3D switches
- Compositor binds announce the refresh clients were last told; the presenter
  retries a failed first swapchain
- Dead strings and helpers removed; docs updated

* Glasses: drop USB frames with a bad checksum

* Glasses: count frames dropped for a bad checksum

* Feat/viture xr (#907)

* Add RayNeo Air and GT support to the XR glasses pipeline

* Add RayNeo Air and GT support to the XR glasses pipeline

* Checkpoint glasses tracking, calibration, and SteamVR launch integration

* Checkpoint glasses tracking, calibration, and SteamVR launch integration

* Add XREAL 1S support and correct GT and Pro 2 tracking

* Add XREAL 1S support and correct GT and Pro 2 tracking

* Fix mirrored glasses tracking and recover SteamVR after device loss

* Fix mirrored glasses tracking and recover SteamVR after device loss

* Document confirmed XREAL 1S brightness control

* Document confirmed XREAL 1S brightness control

* Fix SteamVR glasses defaults and rendering controls

* Fix SteamVR glasses defaults and rendering controls

* chore: keep only the 3 newest session log folders (#852)

* chore: add agent comment rules (#748)

Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>

* Fix SteamVR polling, VITURE checksums, and fence tracking

Keep manual SteamVR launches polled, discard corrupt VITURE packets, and retire Vulkan fences out of order without blocking submissions at a fixed capacity. Preserve the contiguous completion watermark used by the presenter.

Fix optional Windows launcher configuration and audited helper staging. Add Android, Windows, and native tracker regression coverage.

* Fix SteamVR polling, VITURE checksums, and fence tracking

Keep manual SteamVR launches polled, discard corrupt VITURE packets, and retire Vulkan fences out of order without blocking submissions at a fixed capacity. Preserve the contiguous completion watermark used by the presenter.

Fix optional Windows launcher configuration and audited helper staging. Add Android, Windows, and native tracker regression coverage.

* fix: wireless ADB page names the child-process switch the way Android shows it (#848)

* l10n: refresh stale and untranslated strings in es, fr, ko, ru (#850)

* l10n: refresh stale and untranslated strings in es, fr, ko, ru

- mode_added_art_hint: now mentions Steam then SteamGridDB (all four locales).
- wincomp_needs_installer / wincomp_installers_note: es and ko still described
  the old "coming soon" behaviour; align with the current English text.
- summary_remove_game: was still English in all four locales.

Machine-assisted translation; native-speaker review is welcome.

* l10n(es): say registro, not grabación, for the missing installer record

---------

Co-authored-by: ovurrsl <ovurrsl@users.noreply.github.com>

* fix: a failed cloud save recovery no longer ends the app (#846)

The cloud-recovery thread started at every app start had nothing to
catch an error, so one went to CrashHandler, which halts the process
with status 2. In unit tests the thread can outlive the test that
started it, fail on the torn-down preferences, and take the whole test
run down. The error is now logged and the thread ends.

Co-authored-by: Kirk <12691869+KirkPatrickJunsay@users.noreply.github.com>

* fix: an interrupted Linux runtime download resumes instead of starting again (#847)

* fix: an interrupted Linux runtime download resumes instead of starting again

installOnce() asked the downloader to resume but always deleted the archive in its finally block, so the next attempt began at zero. The archive is now named after the release version and checksum, a failed download keeps what it fetched, and archives of other releases (and the old fixed name) are removed. A checksum or extraction failure still deletes the file.

* test: Downloader resumes a cut-off download from the bytes it kept

Starts a socket server that promises the whole file, hangs up part way and then answers a Range request, and checks that the second downloadFile call resumes with the right offset and ends with identical bytes.

* fix: a resumed download discards a 206 that does not start where the kept bytes end

Downloader appended every 206 response to the partial file without
looking at Content-Range. A server or proxy that answers a Range request
with a different offset would leave a corrupt file. Check that the range
starts at the kept length; otherwise delete the partial file and start
again without a Range header.

Adds a test with a server that answers the Range request from offset 0.

---------

Co-authored-by: ovurrsl <ovurrsl@users.noreply.github.com>

* l10n: German, French, Spanish, Italian, Portuguese and Russian for the zRAM, reset-settings and store free-space strings (#849)

* l10n: German, French, Spanish, Italian, Portuguese and Russian for the zRAM, reset-settings and store free-space strings

Adds the strings values-de, values-it and values-pt were missing (14 each) and the six that values-fr, values-es and values-ru were missing. Machine-assisted drafts that follow each file's existing register and terms; native review is welcome.

* l10n: correct missing active swap translations

---------

Co-authored-by: ovurrsl <ovurrsl@users.noreply.github.com>
Co-authored-by: xXJsonDeruloXx <danielhimebauch@gmail.com>

* fix: droiddeckctl reaches variant-package builds (recover #842) (#854)

fix: droiddeckctl reaches builds shipped under a variant package

The provider authority and the start Activity's component were fixed to
com.droiddeck.launcher, so droiddeckctl could not talk to the antutu,
ludashi or pubg builds from tools/release/variants.txt. DROIDDECK_PACKAGE
now picks the package; the Activity's class name stays the same in every
variant.

Co-authored-by: angelrenefm <17342133+angelrenefm@users.noreply.github.com>

* feat: droiddeckctl reports on-screen pacing (recover #843) (#855)

feat: droiddeckctl pacing reports the frames that reached the screen

Reads SurfaceFlinger's timestats for the game's layer (or the session's
SurfaceView) over a capture: frames on screen, fps on screen, the
present-to-present histogram and how many intervals were off the usual
cadence. Records split by a refresh-rate change are summed. A game's own fps
counter counts frames handed over, so it can't show dropped or doubled frames.

Co-authored-by: angelrenefm <17342133+angelrenefm@users.noreply.github.com>

* fix: Steam can restart more than twice in a session (#754)

* fix: Steam can restart more than twice in a session

Every exit 42 counted toward the cap that stops an update which will
not install, and the count was never cleared, so the third restart of
a session ended it however far apart they were. A client that ran for
a minute before asking is not that loop: the count starts again.

* fix: measure the client's run time as it exits

The time spent collecting logs and checking FEX after the client exits
counted toward its run, so a slow cleanup could make a quick exit 42
look like a long one and clear the count.

---------

Co-authored-by: Kirk <12691869+KirkPatrickJunsay@users.noreply.github.com>
Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>

* fix: the front end keeps its page when the Fold is folded or unfolded (#832)

* fix: the front end keeps its page when the Fold is folded or unfolded

MainActivity, FileManagerActivity, FilePickerActivity and StoreLoginActivity did not declare the screen-size configuration changes, so folding or unfolding a foldable recreated them and dropped their in-memory state. They now handle orientation, screenSize, screenLayout, smallestScreenSize and density themselves, like SessionActivity already does. uiMode is left out on purpose.

* fix: store login keeps rebuilding its toolbar when the display density changes

StoreLoginActivity builds its toolbar in code with dp-to-pixel values taken at creation time and has no onConfigurationChanged. It no longer declares density in configChanges, so a density change recreates it as before this PR. Size and layout changes are still handled in place.

---------

Co-authored-by: ovurrsl <ovurrsl@users.noreply.github.com>

* Release pointer buttons without replaying stale motion (#803)

* Fix cursor warping on native touch down

* Preserve input cleanup without replaying stale pointer motion

---------

Co-authored-by: siyuan.qsy <siyuan.qsy@alibaba-inc.com>

* l10n: translate the strings added since #284 into zh-CN, zh-HK, zh-TW and ja (#539)

* l10n: translate the 184 strings added since #284 (zh-CN/HK/TW, ja)

The stores, cloud saves, Windows components, Steam touch, desktop
removal and background-downloads strings landed after #284's
translation snapshot, so every one of them fell back to English.
Translate all 184 into Simplified Chinese, Traditional Chinese
(Taiwan and Hong Kong) and Japanese, keeping each locale file's own
vocabulary (工作階段/會話, 解除安裝/卸載, 元件/組件, 設定).

Also fix four Hong Kong strings that kept the simplified 着色器.

* l10n(zh): fix stale and ambiguous strings found in review

Severe: summary_remove_game was untranslated English in all three
files; mode_added_art_hint, wincomp_installers_note and
wincomp_needs_installer still described the pre-#480/#492 English.

Also: distinguish the Setup wizard from Android system settings in
zh-HK/zh-TW (初始設定 / 系統設定 / Android 設定), unify library as
收藏庫, component as 組件 in zh-HK, session as 會話 in zh-HK, account
as 帳戶 in zh-HK, Trending as 趋势/趨勢, Forget as 清除, reword the
re-applied-at-launch, action-failed and already-in-Games strings, and
play as 執行/開啟遊戲 in zh-TW/zh-HK.

* l10n(ja): fix stale and ambiguous strings found in review

---------

Co-authored-by: Dantalian <Dantalian-dev@users.noreply.github.com>
Co-authored-by: xXJsonDeruloXx <danielhimebauch@gmail.com>

* Rebuild and verify Windows VR runtime components

* Rebuild and verify Windows VR runtime components

* Add top, bottom, and centered picture positions for Steam sessions (#871)

* fix: dependency scan ignores folders above the game folder (#859)

* DroidDeck-Proton: get releases from the Droid-Deck org repo

DroidDeck-Proton is now built and released on Droid-Deck/DroidDeck-Proton
(11-9.1 is Latest there), so the Components page, the update check and the
guest installer read releases from the org repo instead of The412Banner's.

* feat: low-latency controller and touch toggles in the session drawer (#808)

* fix: SteamGridDB lookups no longer stick as "no match" after a failure; add a search to the art chooser (#876)

* feat: Steam home wall fills from the signed-in library instead of store picks (#878)

* test: cover GPU family and support classification (#880)

* Fix local builds on Linux failing after the first Comet build (#866)

* feat: sort the store library and installed tabs, and switch between card and list views (#798)

* feat: three-finger tap toggles Steam's on-screen keyboard in game mode (#782)

* fix: keep Quest-patched Turnip as the auto default (#853)

* Fix deploy_local.sh and droiddeckctl not finding a phone connected over wireless debugging (#864)

* fix: keep proot sessions alive when the kernel loses fork events (#457)

* fix(quest): correct desktop and Steam display corruption across sessions (#733)

fix(quest): stabilize desktop and Steam layer presentation

Keep Quest SDR scenes and the black backdrop off the Android Vulkan swapchain regardless of session order or HDR preferences. Only report the first frame after client content is submitted.

Add backdrop lifecycle and first-frame regression coverage. Record the user's successful Quest 3 Test 2 result.

Co-authored-by: xXJsonDeruloXx <danielhimebauch@gmail.com>

* fix: keep SteamVR syscall hooks and report Steam crashes (#905)

* fix: preserve SteamVR syscall hooks

* fix: report Steam crashes through gamescope

* fix: honor glasses pacing and resolution in SteamVR launches

---------

Co-authored-by: connor g <connorsspam1992@gmail.com>
Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>
Co-authored-by: Yassine Gherbi <y.gherbi.dev@gmail.com>
Co-authored-by: Resul Övür <92828257+ovurrsl@users.noreply.github.com>
Co-authored-by: ovurrsl <ovurrsl@users.noreply.github.com>
Co-authored-by: Kirk <kirtoy.junsay@gmail.com>
Co-authored-by: Kirk <12691869+KirkPatrickJunsay@users.noreply.github.com>
Co-authored-by: xXJsonDeruloXx <danielhimebauch@gmail.com>
Co-authored-by: angelrenefm <17342133+angelrenefm@users.noreply.github.com>
Co-authored-by: asdf17128 <asdf17128@gmail.com>
Co-authored-by: siyuan.qsy <siyuan.qsy@alibaba-inc.com>
Co-authored-by: Dantalian <wsu091227@qq.com>
Co-authored-by: Dantalian <Dantalian-dev@users.noreply.github.com>
Co-authored-by: The412Banner <the412banner@users.noreply.github.com>
Co-authored-by: The412Banner <205237651+The412Banner@users.noreply.github.com>
Co-authored-by: FX1922 <120828496+FX192@users.noreply.github.com>
Co-authored-by: Muaz Kadan <32595066+muazkadan@users.noreply.github.com>
Co-authored-by: kingokksa <47888451+kingokksa@users.noreply.github.com>
Co-authored-by: Jamie Schenker <jam1esch3nk3r@gmail.com>
Co-authored-by: Jagger15 <jagger2333@live.com>

* Glasses: keep a pending 2D restore and the SteamVR recovery offer, show Play SteamVR only with a picture

* Translate the glasses calibration, XREAL and SteamVR recovery strings

* SteamVR recovery: give a finished teardown time to collect logs before restarting

* Credit the projects the XR glasses support builds on

* Joy-Cons: SteamVR hands from their gyro while SteamVR runs, one combined pad otherwise

* Headsets: settings badge on Play SteamVR instead of a top-right button, both shown only once connected

* Hand tracking: camera-tracked Joy-Cons and bare hands in SteamVR, drawn as white gloves

- Joy-Con movement from the accelerometer (report reordering, per-Joy-Con calibration, zero-velocity updates)
- MediaPipe pose and hand landmarks from the device camera, placement check, per-hand Kalman filter with the Joy-Con
- No controller connected: bare camera hands with Quest-style gestures, 1€ filtered, rendered as a white glove model built from a distance field
- SteamVR safe mode cleared before each start so one crash no longer blocks the driver

---------

Co-authored-by: MaxsTechReview <6025299+maxjivi05@users.noreply.github.com>
Co-authored-by: informalTechCode <informaltechyt@gmail.com>
Co-authored-by: connor g <connorsspam1992@gmail.com>
Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>
Co-authored-by: Yassine Gherbi <y.gherbi.dev@gmail.com>
Co-authored-by: Resul Övür <92828257+ovurrsl@users.noreply.github.com>
Co-authored-by: ovurrsl <ovurrsl@users.noreply.github.com>
Co-authored-by: Kirk <kirtoy.junsay@gmail.com>
Co-authored-by: Kirk <12691869+KirkPatrickJunsay@users.noreply.github.com>
Co-authored-by: xXJsonDeruloXx <danielhimebauch@gmail.com>
Co-authored-by: angelrenefm <17342133+angelrenefm@users.noreply.github.com>
Co-authored-by: asdf17128 <asdf17128@gmail.com>
Co-authored-by: siyuan.qsy <siyuan.qsy@alibaba-inc.com>
Co-authored-by: Dantalian <wsu091227@qq.com>
Co-authored-by: Dantalian <Dantalian-dev@users.noreply.github.com>
Co-authored-by: The412Banner <the412banner@users.noreply.github.com>
Co-authored-by: The412Banner <205237651+The412Banner@users.noreply.github.com>
Co-authored-by: FX1922 <120828496+FX192@users.noreply.github.com>
Co-authored-by: Muaz Kadan <32595066+muazkadan@users.noreply.github.com>
Co-authored-by: kingokksa <47888451+kingokksa@users.noreply.github.com>
Co-authored-by: Jamie Schenker <jam1esch3nk3r@gmail.com>
Co-authored-by: Jagger15 <jagger2333@live.com>
This commit is contained in:
221 files changed
+24369 -491

No files matched your search

+20
View File
@@ -35,9 +35,29 @@ int main(void) {
q.source = CARDBOARD_SOURCE_SBS;
cardboard_push_constants(&q, pose, 3840, 1080, 2400, 1080, pc);
CHECK(near(pc[17], 1920.0f / 1080.0f) && near(pc[18], 1.0f));
q.mono = 1;
cardboard_push_constants(&q, pose, 3840, 1080, 1920, 1080, pc);
CHECK(near(pc[3], 0.0f) && near(pc[19], 1920.0f / 1080.0f) && near(pc[20], 1.0f));
q.mono = 0;
cardboard_push_constants(&q, pose, 3840, 1080, 1920, 1080, pc);
CHECK(near(pc[3], 0.032f) && near(pc[19], 960.0f / 1080.0f) && near(pc[20], 0.0f));
cardboard_push_constants(&q, pose, 0, 0, 0, 0, pc);
CHECK(isfinite(pc[17]) && isfinite(pc[19]));
// Narrow optical profiles and the lower SteamVR slider values must reach
// the projection unchanged, rather than all becoming a 30-degree view.
const float glasses_fovs[] = {22.0f, 23.3f, 24.0f, 25.8f, 29.0f};
for (unsigned i = 0; i < sizeof(glasses_fovs) / sizeof(glasses_fovs[0]); i++) {
q.fov_deg = glasses_fovs[i];
cardboard_clamp(&q);
CHECK(near(q.fov_deg, glasses_fovs[i]));
cardboard_push_constants(&q, pose, 3840, 1080, 3840, 1080, pc);
CHECK(near(pc[14], tanf(glasses_fovs[i] * 0.5f * (float)M_PI / 180.0f)));
}
q.fov_deg = 0.0f;
cardboard_clamp(&q);
CHECK(q.fov_deg > 0.0f && q.fov_deg < glasses_fovs[0]);
if (failures) return 1;
printf("PASS: cardboard params\n");
return 0;
+42
View File
@@ -517,6 +517,8 @@ def execute(args: argparse.Namespace, adb: str, serial: str) -> dict:
if not args.url.startswith("steam://"):
raise ControlError("Steam URL must start with steam://", "INVALID_STEAM_URL")
request["steamUrl"] = args.url
if args.steamvr:
request["steamVr"] = True
if args.reuse:
current = get_state(adb, serial)["session"]
wanted = {"steam": "steam", "desktop": "lxqt"}[args.mode]
@@ -660,6 +662,24 @@ def execute(args: argparse.Namespace, adb: str, serial: str) -> dict:
if command == "access":
return add_serial(checked(provider_call(adb, serial, "access"), "ACCESS_FAILED"), serial)
if command == "glasses":
request = {"op": args.glasses_op or "status"}
if args.glasses_op == "set":
request.update(key=args.key, value=args.value)
elif args.glasses_op == "simulate":
request["seconds"] = args.seconds
if args.yaw is not None:
request["yaw"] = args.yaw
if args.pitch is not None:
request["pitch"] = args.pitch
elif args.glasses_op == "mode":
request["mode"] = args.mode
elif args.glasses_op == "imu":
request["ms"] = args.ms
elif args.glasses_op == "raw":
request.update(msgId=args.msg_id, payload=args.payload or "")
return add_serial(checked(provider_call(adb, serial, "glasses", request=request), "GLASSES_FAILED"), serial)
if command == "logs":
state = get_state(adb, serial)
session = state.get("session", {})
@@ -710,6 +730,7 @@ def build_parser() -> argparse.ArgumentParser:
start = commands.add_parser("start", help="start Steam or the desktop")
start.add_argument("mode", choices=["steam", "desktop"])
start.add_argument("--ui", choices=["bigpicture", "desktop"], default="bigpicture")
start.add_argument("--steamvr", action="store_true", help="start SteamVR with the session (VITURE glasses)")
start.add_argument("--url", help="steam:// URL to pass to the client")
start.add_argument("--wait", action="store_true", help="wait for READY after starting")
start.add_argument("--reuse", action="store_true", help="keep a running session of this mode instead of failing")
@@ -828,6 +849,27 @@ def build_parser() -> argparse.ArgumentParser:
commands.add_parser("access", help="whether debugging tools are enabled on the device")
glasses = commands.add_parser("glasses", help="VITURE glasses: status, settings, head tracking test")
glasses_ops = glasses.add_subparsers(dest="glasses_op")
glasses_ops.add_parser("status")
glasses_ops.add_parser("allow", help="ask for USB control of the glasses again")
glasses_set = glasses_ops.add_parser("set", help="refreshHz, sbsHz, brightness, volume, sunblock, 3d, gameOnGlasses, headTracking, auto3d, picture3d, screenWidthDm, screenDistanceDm, vrFovDeg")
glasses_set.add_argument("key")
glasses_set.add_argument("value")
glasses_sim = glasses_ops.add_parser("simulate", help="feed a moving head pose (yaw, pitch and roll sweeps), or hold one")
glasses_sim.add_argument("--seconds", type=int, default=20)
glasses_sim.add_argument("--yaw", type=float, help="hold this yaw (degrees, left positive) instead of sweeping")
glasses_sim.add_argument("--pitch", type=float, help="hold this pitch (degrees, down positive) instead of sweeping")
glasses_ops.add_parser("stop-simulate")
glasses_ops.add_parser("query", help="ask the glasses for their display mode, brightness, volume and IMU state")
glasses_mode = glasses_ops.add_parser("mode", help="send a display mode byte, e.g. 0x31 (1080p60), 0x34 (1080p120), 0x32 (3D)")
glasses_mode.add_argument("mode")
glasses_imu = glasses_ops.add_parser("imu", help="stream the glasses' IMU for a moment and show what it reports")
glasses_imu.add_argument("--ms", type=int, default=2000)
glasses_raw = glasses_ops.add_parser("raw", help="send one MCU command (debugging tools)")
glasses_raw.add_argument("msg_id", help="hex message id, e.g. 0x0007")
glasses_raw.add_argument("payload", nargs="?", help="hex bytes")
logs = commands.add_parser("logs", help="pull the latest session artifact directory")
logs.add_argument("which", choices=["latest"])
logs.add_argument("destination", nargs="?")
@@ -636,6 +636,10 @@ BUSCONF
exit 69
fi
# The UI bridge launches SteamVR, which needs the guest agent's library environment.
_bl_compat_env=(env -u LD_LIBRARY_PATH -u STEAM_RUNTIME)
[ "${LD_LIBRARY_PATH+x}" ] && _bl_compat_env+=("LD_LIBRARY_PATH=$LD_LIBRARY_PATH")
[ "${STEAM_RUNTIME+x}" ] && _bl_compat_env+=("STEAM_RUNTIME=$STEAM_RUNTIME")
export STEAM_RUNTIME=1
export LD_LIBRARY_PATH=$steam_root/steamrtarm64:$steam_root/steamrtarm64/panorama${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
export SDL_VIDEO_X11_DGAMOUSE=0
@@ -686,7 +690,7 @@ BUSCONF
echo "== steam: devtools port $BL_CDP_PORT, guest processes only"
fi
[ -n "${BL_LAUNCH_DIR:-}" ] && mkdir -p "$BL_LAUNCH_DIR/agent" && echo "$BL_CDP_PORT" > "$BL_LAUNCH_DIR/agent/cdp-port"
droiddeck-steam-compat "$steam_root" 2>&1 &
"${_bl_compat_env[@]}" droiddeck-steam-compat "$steam_root" 2>&1 &
_bl_ui=(-gamepadui)
[ "${BL_STEAM_UI:-}" = desktop ] && _bl_ui=()
_bl_cef=(-cef-force-gpu -cef-use-gl=angle -cef-use-angle=vulkan)
@@ -153,6 +153,82 @@ SWITCH_JS = r"""
})()
""".replace("__BINDING__", SWITCH_BINDING)
SESSION_SELECT = "/usr/bin/steamos-session-select"
VR_BINDING = "droiddeckLaunchSteamVr"
VR_HELPER = "/usr/local/bin/droiddeck-steamvr"
VR_JS = r"""
(() => {
if (window.__droiddeckSteamVr) return "present";
const apps = SteamClient.Apps;
if (!apps || typeof apps.RunGame !== "function") throw new Error("Steam RunGame is not ready");
const run = apps.RunGame;
const self = { running: false, pending: new Map(), lastError: null };
const flush = () => {
if (!self.running) return;
const queued = Array.from(self.pending.values());
self.pending.clear();
for (const item of queued) {
clearTimeout(item.timer);
run.call(item.receiver, item.id, ...item.args);
}
};
if (typeof SteamClient.OpenVR?.RegisterForVRModeChange === "function") {
self.subscription = SteamClient.OpenVR.RegisterForVRModeChange(running => {
self.running = !!running;
flush();
});
}
const wrapped = function (id, ...args) {
const gameid = String(id);
if (typeof window.__BINDING__ !== "function") return run.call(this, id, ...args);
if (gameid === "250820") {
window.__BINDING__(gameid);
return;
}
const overview = window.appStore?.GetAppOverviewByGameID?.(gameid);
const vr = overview?.vr_only || overview?.vr_supported || overview?.BSupportsVR?.();
if (vr && self.subscription && /^[1-9][0-9]{0,19}$/.test(gameid)) {
if (self.pending.has(gameid)) return;
self.lastError = null;
const item = { receiver: this, id, args, timer: null };
item.timer = setTimeout(() => {
self.pending.delete(gameid);
self.lastError = "SteamVR did not become ready. Start SteamVR from DroidDeck and try the game again.";
console.error("DroidDeck: " + self.lastError);
}, 600000);
self.pending.set(gameid, item);
try { window.__BINDING__(gameid); }
catch (e) { clearTimeout(item.timer); self.pending.delete(gameid); throw e; }
flush();
return;
}
return run.call(this, id, ...args);
};
apps.RunGame = wrapped;
if (apps.RunGame !== wrapped) throw new Error("Steam RunGame could not be redirected");
if (typeof SteamClient.OpenVR?.StartVR === "function") {
const start = SteamClient.OpenVR.StartVR;
SteamClient.OpenVR.StartVR = function (...args) {
if (typeof window.__BINDING__ === "function") return window.__BINDING__("250820");
return start.apply(this, args);
};
}
window.__droiddeckSteamVr = self;
return "installed";
})()
""".replace("__BINDING__", VR_BINDING)
def launch_steamvr():
"""Keep downloads/startup off the Steam UI bridge's event loop."""
say("SteamVR library launch: using the DroidDeck ARM64 helper")
state = os.path.join(os.environ.get("XDG_RUNTIME_DIR", "/tmp"), "droiddeck-steamvr")
try:
os.makedirs(state, exist_ok=True)
with open(os.path.join(state, "library-launch.log"), "a") as log:
return subprocess.Popen([VR_HELPER, "launch"], stdin=subprocess.DEVNULL,
stdout=log, stderr=subprocess.STDOUT, start_new_session=True)
except OSError as e:
say("SteamVR library launch failed: %s" % e)
class Closed(Exception):
@@ -405,6 +481,7 @@ class Helper:
self.known = set()
self.installed = None
self.fex_asked = set()
self.vr_launch = None
def attach(self):
path = shared_context(self.port)
@@ -415,6 +492,8 @@ class Helper:
if page.evaluate("typeof SteamClient === 'object' && typeof SteamClient.WebUITransport === 'object'") is not True:
raise LookupError("SteamClient is not ready")
page.call("Runtime.addBinding", {"name": BINDING})
page.call("Runtime.addBinding", {"name": VR_BINDING})
page.evaluate(VR_JS)
if not self.steamos:
page.call("Runtime.addBinding", {"name": SWITCH_BINDING})
page.evaluate(SWITCH_JS)
@@ -498,6 +577,14 @@ class Helper:
events = self.page.drain()
if any(called(e, SWITCH_BINDING) for e in events):
switch_to_desktop()
vr_games = {e.get("params", {}).get("payload") for e in events if called(e, VR_BINDING) and
isinstance(e.get("params", {}).get("payload"), str)}
vr_games = {game for game in vr_games if isinstance(game, str) and game.isascii() and
game.isdecimal() and not game.startswith("0") and len(game) <= 20 and
int(game) <= 0xffffffffffffffff}
if vr_games and (self.vr_launch is None or self.vr_launch.poll() is not None):
say("preparing SteamVR for library app(s): " + ", ".join(sorted(vr_games)))
self.vr_launch = launch_steamvr()
return events
def serve(self, watcher):
@@ -42,6 +42,16 @@ stop() {
}
case "${1:-status}" in
launch)
# Library/URL entry point: use the same installer and start path as the app.
exec 8>"$state/launch.lock"
flock -n 8 || exit 0
if ! installed; then
"$0" install || exit $?
while [ -f "$state/installing" ]; do sleep 1; done
fi
"$0" start
;;
status)
i=0; installed && i=1
r=0; running && r=1
@@ -51,8 +61,9 @@ status)
echo "installed=$i running=$r installing=$n driver=$d"
;;
install)
exec 7>"$state/install.lock"
flock -n 7 || { echo "already installing"; exit 0; }
installed && { echo "SteamVR is already in $steamvr"; exit 0; }
[ -f "$state/installing" ] && { echo "already installing"; exit 0; }
: > "$state/installing"
trap 'rm -f "$state/installing"' EXIT
cache=$HOME/.cache/droiddeck-vr
@@ -72,11 +83,63 @@ stop)
stop || exit 1
;;
start)
exec 9>"$state/start.lock"
flock -n 9 || { echo "SteamVR start already in progress"; exit 0; }
installed || { echo "SteamVR is not installed (droiddeck-steamvr install)"; exit 1; }
[ -f "$driver/bin/linuxarm64/driver_droiddeck.so" ] || { echo "the Cardboard driver is missing from $driver"; exit 1; }
if [ -f "$state/active" ] && { running || pgrep -x vrstartup >/dev/null 2>&1; }; then
echo "DroidDeck SteamVR is already running"
exit 0
fi
stop || exit 1
sleep 1
mkdir -p "$HOME/.config/openvr" "$HOME/.config/openxr/1" "$logs" "$HOME/.local/share/Steam/config"
if [ -S "/run/droiddeck/droiddeck-xr-frames" ] && [ -z "${DROIDDECK_VR_PRESENT:-}" ]; then
export DROIDDECK_VR_PRESENT=questwsi
fi
python3 - "$HOME/.local/share/Steam/config/steamvr.vrsettings" <<'PY'
import json
import os
from pathlib import Path
import sys
import tempfile
path = Path(sys.argv[1])
try:
settings = json.loads(path.read_text()) if path.exists() else {}
section = settings.setdefault("steamvr", {})
defaults = {} if os.environ.get("DROIDDECK_VR_PRESENT", "").startswith("quest") else {"supersampleManualOverride": True, "supersampleScale": 1.0}
changed = any(key not in section for key in defaults)
for key, value in defaults.items():
section.setdefault(key, value)
driver = settings.get("driver_droiddeck")
if isinstance(driver, dict) and driver.get("blocked_by_safe_mode") is True:
driver["blocked_by_safe_mode"] = False
changed = True
print("SteamVR had blocked the DroidDeck driver after a crash; unblocked it")
if changed:
with tempfile.NamedTemporaryFile(mode="w", dir=path.parent, delete=False) as output:
temporary = Path(output.name)
try:
json.dump(settings, output, indent=3)
output.write("\n")
except BaseException:
temporary.unlink(missing_ok=True)
raise
try:
os.replace(temporary, path)
finally:
temporary.unlink(missing_ok=True)
except (OSError, ValueError, AttributeError, TypeError) as error:
print("Could not set SteamVR resolution defaults: " + str(error), file=sys.stderr)
try:
(path.parent / "vrserver_crash_timestamp.txt").unlink()
print("Removed SteamVR's crash marker so it starts without safe mode")
except FileNotFoundError:
pass
except OSError as error:
print("Could not remove SteamVR's crash marker: " + str(error), file=sys.stderr)
PY
cat > "$HOME/.config/openvr/openvrpaths.vrpath" <<JSON
{
"config": ["$HOME/.local/share/Steam/config"],
@@ -88,13 +151,31 @@ start)
}
JSON
ln -sf "$steamvr/steamxr_linuxarm64.json" "$HOME/.config/openxr/1/active_runtime.json"
chaperone=$HOME/.local/share/Steam/config/chaperone_info.vrchap
[ -s "$chaperone" ] || cat > "$chaperone" <<'JSON'
{
"jsonid" : "chaperone_info",
"universes" : [
{
"collision_bounds" : [
[ [ -1.0, 0.0, -1.0 ], [ -1.0, 2.43, -1.0 ], [ -1.0, 2.43, 1.0 ], [ -1.0, 0.0, 1.0 ] ],
[ [ -1.0, 0.0, 1.0 ], [ -1.0, 2.43, 1.0 ], [ 1.0, 2.43, 1.0 ], [ 1.0, 0.0, 1.0 ] ],
[ [ 1.0, 0.0, 1.0 ], [ 1.0, 2.43, 1.0 ], [ 1.0, 2.43, -1.0 ], [ 1.0, 0.0, -1.0 ] ],
[ [ 1.0, 0.0, -1.0 ], [ 1.0, 2.43, -1.0 ], [ -1.0, 2.43, -1.0 ], [ -1.0, 0.0, -1.0 ] ]
],
"play_area" : [ 2.0, 2.0 ],
"seated" : { "translation" : [ 0.0, -1.6, 0.0 ], "yaw" : 0.0 },
"standing" : { "translation" : [ 0.0, 0.0, 0.0 ], "yaw" : 0.0 },
"universeID" : "2"
}
],
"version" : 5
}
JSON
export VK_LAYER_PATH="$layer${VK_LAYER_PATH:+:$VK_LAYER_PATH}"
export VK_INSTANCE_LAYERS="VK_LAYER_DROIDDECK_vr${VK_INSTANCE_LAYERS:+:$VK_INSTANCE_LAYERS}"
export VK_LOADER_LAYERS_DISABLE="${VK_LOADER_LAYERS_DISABLE:-~implicit~}"
if [ -S "/run/droiddeck/droiddeck-xr-frames" ] && [ -z "${DROIDDECK_VR_PRESENT:-}" ]; then
export DROIDDECK_VR_PRESENT=questwsi
fi
if [ "${DROIDDECK_VR_PRESENT:-}" = questwsi ]; then
quest_settings apply || exit 1
startup_handed_off=0
@@ -128,12 +209,12 @@ JSON
cd "$steamvr/bin/linuxarm64" || exit 1
startup_log=$logs/droiddeck-vrstartup.log
ln -sf "$startup_log" "$state/vrstartup.log"
setsid ./vrstartup > "$startup_log" 2>&1 < /dev/null &
setsid ./vrstartup 8>&- 9>&- > "$startup_log" 2>&1 < /dev/null &
startup_handed_off=1
echo "SteamVR starting; logs in $logs and $state/vrstartup.log"
;;
*)
echo "usage: droiddeck-steamvr status|install|start|stop" >&2
echo "usage: droiddeck-steamvr status|install|start|stop|launch" >&2
exit 2
;;
esac
+340
View File
@@ -0,0 +1,340 @@
"""Prepare the audited Windows runtime from a pinned APK and rebuilt replacements.
The donor was built from a CI merge, not just the PR head. Only unchanged
components are reused. Session helpers come from this checkout, libblsession
must be rebuilt for all three architectures, Gamescope comes from the current
checksum-pinned release, and the VR components are rebuilt in Docker (or reused
from a verified build manifest). Store engines are omitted (build with skipRust).
"""
import argparse
import hashlib
import io
import json
from pathlib import Path, PurePosixPath
import shutil
import struct
import subprocess
import sys
import tarfile
import tempfile
from zipfile import ZipFile
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'tools/release'))
import runtime_inputs as runtime
from check_session_assets import source_assets
sys.path.insert(0, str(ROOT / 'tools/vr'))
import build_windows as vr_build
DONOR_SHA256 = 'b7efef6442f384446bdd1750cec21f0eda61b370bc21d9f55187448e5c256760'
DONOR_PR_SOURCE = 'bdf876d47cc9bb6fa810d014370da5bbb3c28039'
DONOR_MAIN_SOURCE = '46fac69438ed428f2ca43ea935cd47790264f380'
DONOR_TREE = 'ddbca4d572b809fb969be0cf80ef83d6ef6580ce'
DONOR_REVISION = 'cf24c5f2c771633ea426046006c2b977c7bd7d32'
DONOR_CERT_SHA256 = 'b241ea7d77822d39b89d3227e06f3ecf62adec6cbfd38ad06196787991b5d3f0'
DONOR_URL = 'https://github.com/Droid-Deck/DroidDeck-CI/releases/download/pr-732/DroidDeck-PR.732-bdf876d.apk'
AUDITED_SOURCE = '1dc66bf9be64a404c80400049c0803614814461b'
LOCAL_BUILD_PLUMBING = {'app/build.gradle', 'tools/release/runtime_inputs.py'}
# These Android-only inputs are compiled by Gradle, never taken from the donor.
# Keep this list explicit so unrelated native or Linux runtime edits still fail.
LOCAL_ANDROID_SOURCES = {'app/src/main/cpp/CMakeLists.txt',
'app/src/main/cpp/viture/sdk_bridge.cpp',
'app/src/main/cpp/viture/display_modes.h',
'app/src/main/cpp/waylandcomp/src/cardboard.c'}
# These audited helper updates are copied from this checkout by runtime.record().
# Permit them only while source_assets() still guarantees that staging path.
LOCAL_SESSION_HELPERS = {'tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-compat',
'tools/linuxfs/overlay/usr/local/bin/droiddeck-steamvr'}
VR_PREFIX = 'assets/linuxfs/usr/local/lib/droiddeck-vr/'
CHECK_PATHS = list(dict.fromkeys(runtime.SOURCE_PATHS + [
'app/src/main/cpp', 'app/src/main/jniLibs', 'app/src/main/rust/stores', 'tools/vr']))
REBUILT = {'libblsession.so': 183, 'x86_64/libblsession.so': 62, 'i386/libblsession.so': 3}
REBUILT_SHA256 = {
'libblsession.so': '1093ce33ca6d0a17a2eec7b3a3cc53969b01ffc459aef16b320cd3857825b738',
'x86_64/libblsession.so': 'd0a558fee1ac4d5b898170a96b03e15610d9835e7097425027b8c31728da870a',
'i386/libblsession.so': '785d8d9bf1fa20d083a7fed65d064c7580684d7065530d95a648e2d32901d2a2',
}
JNI = ('libproot.so', 'libproot-loader.so', 'libdirectaudiorelay.so')
PREPARED = ('pulseaudio.tzst',
'graphics_driver/adrenotools-turnip25.1.0-quest1.tzst',
'graphics_driver/adrenotools-turnip26.3.0-a6xx.tzst',
'graphics_driver/adrenotools-turnip26.3.0-quest2.tzst')
def git(*args):
return subprocess.check_output(['git', '-C', str(ROOT), *args])
def changed(base, target=None):
args = ['diff', '--no-ext-diff', '--ignore-cr-at-eol', '--name-only', '-z', base]
if target:
args.append(target)
return set(filter(None, git(*args, '--', *CHECK_PATHS).decode().split('\0')))
def check_sources():
# A branch label alone is insufficient: CI merged this main commit into it.
if git('merge-tree', '--write-tree', DONOR_PR_SOURCE, DONOR_MAIN_SOURCE).decode().splitlines()[0] != DONOR_TREE:
raise ValueError('The donor CI merge does not match the audited source tree')
helper_sources = {path.relative_to(ROOT).as_posix() for path in source_assets()}
reviewed = LOCAL_BUILD_PLUMBING | LOCAL_ANDROID_SOURCES | (LOCAL_SESSION_HELPERS & helper_sources)
unexpected = {p for p in changed(AUDITED_SOURCE, 'HEAD') - reviewed if not vr_build.is_source(p)}
if unexpected:
raise ValueError('Sources changed since the component audit:\n' + '\n'.join(sorted(unexpected)))
local = changed('HEAD')
untracked = set(filter(None, git('ls-files', '--others', '--exclude-standard', '-z',
'--', *CHECK_PATHS).decode().split('\0')))
unexpected = {p for p in (local - reviewed) | (untracked - LOCAL_ANDROID_SOURCES)
if not vr_build.is_source(p)}
if unexpected:
raise ValueError('Unreviewed runtime/native/Rust changes:\n' + '\n'.join(sorted(unexpected)))
classifications = {}
for path in sorted(changed(DONOR_TREE, 'HEAD') | local | untracked):
if vr_build.is_source(path):
action = 'VR component rebuilt from current source and verified; never imported from donor'
elif path in LOCAL_ANDROID_SOURCES:
action = 'Android native source compiled by Gradle; not imported from donor'
elif path.startswith('app/src/main/rust/stores/'):
action = 'store engines omitted; Gradle must use skipRust=true'
elif path in helper_sources:
action = 'restaged from current source with LF line endings'
elif path in {'tools/linuxfs/preload/net.c', 'tools/linuxfs/preload/webhelper.c'}:
action = 'all three libblsession libraries rebuilt from current source'
elif path == 'tools/gamescope/release.env':
action = 'current checksum-pinned Gamescope archive replaces donor'
elif path in {'tools/build_local.sh', 'tools/directaudio/fetch.sh'}:
action = 'audited download/build cache plumbing; component inputs unchanged'
elif path == 'app/src/main/cpp/waylandcomp/src/compositor.c':
action = 'Android native library rebuilt by Gradle; not imported'
elif path in LOCAL_BUILD_PLUMBING:
action = 'local Windows build plumbing; changes recorded below'
else:
raise ValueError('Unclassified donor source difference: ' + path)
classifications[path] = action
return classifications
def validate_relative(name):
path = PurePosixPath(name)
if path.is_absolute() or not path.parts or any(part in {'.', '..'} or ':' in part for part in path.parts):
raise ValueError('Unsafe archive member: ' + name)
if '\\' in name:
raise ValueError('Backslash in archive member: ' + name)
return path
def checked_output(relative):
validate_relative(relative)
target = (ROOT / relative).resolve()
if not target.is_relative_to(ROOT.resolve()):
raise ValueError('Output escapes the checkout: ' + relative)
if git('ls-files', '--', relative).strip():
raise ValueError('Refusing to overwrite tracked file: ' + relative)
return target
def read_gamescope(path):
pins = dict(line.split('=', 1) for line in (ROOT / 'tools/gamescope/release.env').read_text().splitlines()
if line and not line.startswith('#'))
if runtime.digest(path) != pins['GAMESCOPE_SHA256']:
raise ValueError('Gamescope archive checksum does not match tools/gamescope/release.env')
data = subprocess.run(['zstd', '-dc', str(path)], capture_output=True, check=True).stdout
files = {}
with tarfile.open(fileobj=io.BytesIO(data)) as archive:
for member in archive:
name = member.name.removeprefix('./')
validate_relative(name)
if member.isdir():
continue
if not member.isfile():
raise ValueError('Gamescope archive contains a non-regular member: ' + name)
if name in files:
raise ValueError('Duplicate Gamescope archive member: ' + name)
files[name] = archive.extractfile(member).read()
if set(files) != {'usr/local/bin/gamescope'}:
raise ValueError('Unexpected Gamescope archive payload: ' + ', '.join(files))
return files, pins
def validate_rebuilt(path, machine):
data = path.read_bytes()
if len(data) < 20 or data[:4] != b'\x7fELF' or data[5] != 1 or struct.unpack('<H', data[18:20])[0] != machine:
raise ValueError('Replacement has the wrong ELF architecture: ' + str(path))
return data
def validate_tracked_assets(package, llvm_strip):
checked = {}
for prefix, apk_prefix in [('app/src/main/jniLibs/', 'lib/'),
('app/src/main/assets/desktop-removal/', 'assets/desktop-removal/')]:
tracked = filter(None, git('ls-files', '-z', '--', prefix).decode().split('\0'))
for name in tracked:
archive_name = apk_prefix + name.removeprefix(prefix)
content = (ROOT / name).read_bytes()
donor_content = package.read(archive_name)
verification = 'identical bytes'
if content != donor_content:
if prefix != 'app/src/main/jniLibs/':
raise ValueError('Tracked bytes differ from the donor: ' + name +
' (text inputs must be checked out with LF)')
# AGP strips prebuilt JNI libraries during packaging. Verify that
# deterministic transformation on a temporary copy, never the source.
with tempfile.TemporaryDirectory(prefix='droiddeck-strip-check-') as temp:
stripped = Path(temp) / 'library.so'
subprocess.run([str(llvm_strip), '--strip-unneeded', '-o', str(stripped),
str(ROOT / name)], check=True, capture_output=True)
if stripped.read_bytes() != donor_content:
raise ValueError('Stripped tracked binary differs from donor: ' + name)
verification = 'identical after NDK llvm-strip --strip-unneeded'
checked[name] = {'source_sha256': hashlib.sha256(content).hexdigest(),
'packaged_sha256': hashlib.sha256(donor_content).hexdigest(),
'verification': verification}
return checked
def prepare(apk, rebuilt_dir, gamescope, llvm_strip=None, rebuilt_vr=None, vk_include=None, vr_image=vr_build.IMAGE):
classifications = check_sources()
if runtime.digest(apk) != DONOR_SHA256:
raise ValueError('Donor APK checksum mismatch')
gamescope_files, pins = read_gamescope(gamescope)
replacements = {name: validate_rebuilt(rebuilt_dir / name, machine) for name, machine in REBUILT.items()}
for name, data in replacements.items():
if hashlib.sha256(data).hexdigest() != REBUILT_SHA256[name]:
raise ValueError('Replacement does not match the audited local rebuild: ' + name)
if vk_include is None:
if llvm_strip is None:
raise ValueError('Set --vk-include or --llvm-strip to locate the NDK Vulkan headers')
vk_include = llvm_strip.parent.parent / 'sysroot/usr/include'
if rebuilt_vr is None:
vr_files, vr_manifest = vr_build.build(ROOT / 'build/windows-setup/vr', vk_include, root=ROOT, image=vr_image)
else:
vr_files, vr_manifest = vr_build.validate(rebuilt_vr, vk_include, root=ROOT)
helpers = source_assets()
helper_assets = set(helpers.values())
plan = {}
origins = {}
with ZipFile(apk) as package:
names = package.namelist()
if len(set(names)) != len(names):
raise ValueError('Donor APK has duplicate ZIP entries')
tracked = validate_tracked_assets(package, llvm_strip)
excluded = {'assets/linuxfs/' + name for name in REBUILT}
excluded.update('assets/linuxfs/' + name for name in gamescope_files)
excluded.update(helper_assets)
for name in names:
if name.endswith('/') or name in excluded or name.startswith(VR_PREFIX):
continue
if name.startswith(('assets/linuxfs/', 'assets/directaudio/', 'assets/droiddeck-esync/')):
target = 'app/src/main/' + name
checked_output(target)
plan[target] = package.read(name)
origins[target] = 'verified donor APK'
for name in JNI:
target = 'app/src/main/jniLibs/arm64-v8a/' + name
checked_output(target)
plan[target] = package.read('lib/arm64-v8a/' + name)
origins[target] = 'verified donor APK'
for name in PREPARED:
target = 'app/build/prepared-assets/' + name
checked_output(target)
plan[target] = package.read('assets/' + name)
origins[target] = 'verified donor APK'
for name, data in replacements.items():
target = 'app/src/main/assets/linuxfs/' + name
checked_output(target)
plan[target] = data
origins[target] = 'locally rebuilt libblsession'
for name, data in gamescope_files.items():
target = 'app/src/main/assets/linuxfs/' + name
checked_output(target)
plan[target] = data
origins[target] = 'checksum-pinned Gamescope release'
for name, data in vr_files.items():
target = 'app/src/main/' + VR_PREFIX + name
checked_output(target)
plan[target] = data
origins[target] = 'VR rebuilt from current source; source and output checksums verified'
# Do not silently retain unrelated files from a prior preparation.
expected = set(plan) | {'app/src/main/' + name for name in helper_assets}
for tree in ('linuxfs', 'directaudio', 'droiddeck-esync'):
base = ROOT / 'app/src/main/assets' / tree
for path in base.rglob('*'):
if path.is_file() and not any(part.startswith('.') for part in path.relative_to(base).parts):
if path.relative_to(ROOT).as_posix() not in expected:
raise ValueError('Unexpected existing generated asset: ' + str(path))
for source in helpers:
data = source.read_bytes()
normalized = data.replace(b'\r\n', b'\n')
if data != normalized:
source.write_bytes(normalized)
for target, data in plan.items():
path = checked_output(target)
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(data)
runtime.record()
runtime.verify()
provenance = {
'version': 1,
'donor': {'url': DONOR_URL, 'sha256': DONOR_SHA256, 'signer_sha256': DONOR_CERT_SHA256,
'pr_source': DONOR_PR_SOURCE, 'main_source': DONOR_MAIN_SOURCE,
'source_tree': DONOR_TREE, 'embedded_revision': DONOR_REVISION},
'source_commit': git('rev-parse', 'HEAD').decode().strip(),
'classified_differences': classifications,
'build_plumbing': {name: {'sha256': runtime.digest(ROOT / name),
'diff': git('diff', 'HEAD', '--', name).decode()}
for name in sorted(LOCAL_BUILD_PLUMBING)},
'android_sources_built_by_gradle': {name: runtime.digest(ROOT / name)
for name in sorted(LOCAL_ANDROID_SOURCES)
if (ROOT / name).is_file()},
'stores': 'omitted; use -PskipRust=true',
'gamescope': {'repo': pins['GAMESCOPE_REPO'], 'tag': pins['GAMESCOPE_TAG'],
'archive_sha256': pins['GAMESCOPE_SHA256']},
'tracked_files_verified': tracked,
'vr_build': vr_manifest,
'outputs': {name: {'origin': origins[name], 'sha256': hashlib.sha256(data).hexdigest()}
for name, data in sorted(plan.items())},
'preload_sources': {path.relative_to(ROOT).as_posix(): runtime.digest(path)
for path in sorted((ROOT / 'tools/linuxfs/preload').glob('*.c'))},
'preload_build_scripts': {path.relative_to(ROOT).as_posix(): {
'sha256': runtime.digest(path), 'script': path.read_text()}
for path in sorted((ROOT / 'build/windows-setup').glob('build-preloads-*.sh'))},
'runtime_manifest_sha256': runtime.digest(ROOT / runtime.MANIFEST),
}
output = ROOT / 'app/build/windows-runtime-provenance.json'
output.write_text(json.dumps(provenance, indent=2, sort_keys=True) + '\n', encoding='utf-8')
print('Runtime inputs verified; donor components, rebuilt preloads, rebuilt VR and pinned Gamescope recorded.')
print('Store engines omitted: build with -PskipRust=true.')
print(output)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--apk', type=Path, default=ROOT / 'build/windows-setup/DroidDeck-PR.732-bdf876d.apk')
parser.add_argument('--rebuilt-linuxfs', type=Path, default=ROOT / 'build/windows-setup/rebuilt-linuxfs')
parser.add_argument('--gamescope', type=Path, default=ROOT / 'build/windows-setup/gamescope.tzst')
parser.add_argument('--llvm-strip', type=Path, default=Path.home() /
'AppData/Local/Android/Sdk/ndk/27.3.13750724/toolchains/llvm/prebuilt/windows-x86_64/bin/llvm-strip.exe')
parser.add_argument('--rebuilt-vr', type=Path, help='Reuse a VR build with vr-build.json; default rebuilds in Docker')
parser.add_argument('--vk-include', type=Path, help='NDK Vulkan headers; defaults to the sysroot beside --llvm-strip')
parser.add_argument('--vr-image', default=vr_build.IMAGE, help='Docker compiler image for a fresh VR build')
parser.add_argument('--check-sources', action='store_true', help='Audit source differences without extracting files')
args = parser.parse_args()
zstd = ROOT / 'build/windows-setup/tools/zstd/zstd.exe'
if not shutil.which('zstd') and zstd.is_file():
import os
os.environ['PATH'] = str(zstd.parent) + os.pathsep + os.environ.get('PATH', '')
try:
if args.check_sources:
print(json.dumps(check_sources(), indent=2, sort_keys=True))
else:
prepare(args.apk.resolve(), args.rebuilt_linuxfs.resolve(), args.gamescope.resolve(), args.llvm_strip.resolve(),
args.rebuilt_vr.resolve() if args.rebuilt_vr else None,
args.vk_include.resolve() if args.vk_include else None, args.vr_image)
except (ValueError, OSError, subprocess.CalledProcessError, KeyError) as error:
parser.exit(1, str(error) + '\n')
if __name__ == '__main__':
main()
+6
View File
@@ -88,6 +88,12 @@ Added by DroidDeck:
`mnt_id` from the pin's fdinfo. A later close/reuse of the tracee fd cannot redirect that read;
a concurrent reuse for the same inode through a different bind mount is indistinguishable at
the exit stop. It does not synthesize `STATX_MNT_ID_UNIQUE`.
- `0016-robust-list-from-seccomp.patch` - Android's app policy refuses `set_robust_list` and
`get_robust_list`. proot still answers the first with `ENOSYS` but keeps the head each thread
offered (cleared on `execve`, none in a new task), and answers the second for the calling thread
with it. SteamVR's `vrclient.so` asks for the head through libc's `syscall()`, which
`libblsession.so`'s robust.c never sees: `libopenvr_api.so` opens it `RTLD_DEEPBIND`. Steam
aborted on "futex robust_list not initialized by pthreads" as soon as SteamVR was registered.
Prototype (inert unless `PROOT_FASTPATH` is set; see `docs/development/proot-performance.md`):
@@ -0,0 +1,61 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: DroidDeck <droiddeck@users.noreply.github.com>
Date: Tue, 13 Oct 2026 00:00:00 +0000
Subject: [PATCH] seccomp: answer get_robust_list with the head set_robust_list
was refused for
diff --git a/src/execve/exit.c b/src/execve/exit.c
index f32d3ec..b5475b3 100644
--- a/src/execve/exit.c
+++ b/src/execve/exit.c
@@ -473,6 +473,8 @@ void translate_execve_exit(Tracee *tracee)
talloc_set_name_const(tracee->exe, "$exe");
}
+ tracee->robust_list = 0;
+
/* New processes have no heap. */
if (talloc_reference_count(tracee->heap) >= 1) {
talloc_unlink(tracee, tracee->heap);
diff --git a/src/tracee/seccomp.c b/src/tracee/seccomp.c
index 77d5b27..0af232b 100644
--- a/src/tracee/seccomp.c
+++ b/src/tracee/seccomp.c
@@ -559,6 +559,24 @@ static int handle_seccomp_event_common(Tracee *tracee)
}
case PR_set_robust_list:
+ tracee->robust_list = peek_reg(tracee, CURRENT, SYSARG_1);
+ set_result_after_seccomp(tracee, -ENOSYS);
+ break;
+
+ case PR_get_robust_list:
+ {
+ pid_t pid = (pid_t) peek_reg(tracee, CURRENT, SYSARG_1);
+ if (tracee->robust_list == 0 || (pid != 0 && pid != tracee->pid)) {
+ set_result_after_seccomp(tracee, -ENOSYS);
+ break;
+ }
+ poke_word(tracee, peek_reg(tracee, CURRENT, SYSARG_2), tracee->robust_list);
+ if (errno == 0)
+ poke_word(tracee, peek_reg(tracee, CURRENT, SYSARG_3), 3 * sizeof_word(tracee));
+ set_result_after_seccomp(tracee, errno != 0 ? -EFAULT : 0);
+ break;
+ }
+
default:
/* Set errno to -ENOSYS */
set_result_after_seccomp(tracee, -ENOSYS);
diff --git a/src/tracee/tracee.h b/src/tracee/tracee.h
index 1fca85f..496ca75 100644
--- a/src/tracee/tracee.h
+++ b/src/tracee/tracee.h
@@ -248,6 +248,8 @@ typedef struct tracee {
* has to be ignored as it's same syscall entry */
bool seccomp_already_handled_enter;
+ word_t robust_list;
+
/**********************************************************************
* Shared or private resources, depending on the CLONE_FS/VM flags. *
**********************************************************************/
+5
View File
@@ -18,6 +18,7 @@ SOURCE_PATHS = ['app/build.gradle', 'app/src/main/cpp/fakeinput_steam.cpp', 'app
'tools/mangoapp', 'tools/msitools', 'tools/comet', 'tools/build_local.sh',
'tools/release/runtime_inputs.py', 'app/src/main/assets/desktop-removal',
'tools/quest', 'tools/vr/src', 'tools/vr/driver_droiddeck', 'tools/vr/layer', 'tools/vr/build.sh',
'app/src/main/cpp/waylandcomp/src/headpose_shm.h',
'app/src/main/cpp/adrenotools/src/hook/kgsl.h',
'app/src/main/assets/graphics_driver/adrenotools-turnip25.1.0.tzst']
@@ -77,6 +78,10 @@ def required():
['droiddeck/bin/linuxarm64/driver_droiddeck.so', 'droiddeck/driver.vrdrivermanifest',
'droiddeck/resources/settings/default.vrsettings', 'layer/libVkLayer_droiddeck_vr.so',
'layer/VK_LAYER_DROIDDECK_vr.json', 'layer/VK_LAYER_DROIDDECK_vr_implicit.json']]
for side in ['left', 'right']:
paths += [f'assets/linuxfs/usr/local/lib/droiddeck-vr/droiddeck/resources/rendermodels/droiddeck_hand_{side}/{name}'
for name in [f'droiddeck_hand_{side}.json', 'hand.mtl', 'hand.png', 'hand_open.obj', 'hand_fist.obj',
'thumb_index_open.obj', 'thumb_index_pinch.obj']]
paths += ['assets/linuxfs/usr/local/lib/mangoapp/' + name for name in
['mangoapp', 'libfmt.so.10', 'libspdlog.so.1.13', 'libglfw.so.3', 'libtraceevent.so.1', 'libtracefs.so.1']]
paths += ['assets/linuxfs/usr/local/lib/droiddeck-comet/' + name for name in
+24
View File
@@ -0,0 +1,24 @@
param([string]$Serial)
$ErrorActionPreference = 'Stop'
$repoRoot = Split-Path -Parent $PSScriptRoot
Push-Location $repoRoot
try {
$javaConfig = if (Test-Path -LiteralPath '.gradle/config.properties' -PathType Leaf) {
Get-Content -LiteralPath '.gradle/config.properties' | Where-Object { $_ -match '^java.home=' } | Select-Object -First 1
}
if ($javaConfig) { $env:JAVA_HOME = $javaConfig.Substring('java.home='.Length).Replace('\\', '\').Replace('\:', ':') }
& .\gradlew.bat :app:assembleDebug --console=plain
if ($LASTEXITCODE -ne 0) { throw 'Android build failed.' }
$adbArgs = @()
if ($Serial) { $adbArgs = @('-s', $Serial) }
& adb @adbArgs install -r 'app/build/outputs/apk/debug/app-debug.apk'
if ($LASTEXITCODE -ne 0) { throw 'APK installation failed; existing app data was not removed.' }
$suffixLine = if (Test-Path -LiteralPath 'local.properties' -PathType Leaf) {
Get-Content -LiteralPath 'local.properties' | Where-Object { $_ -match '^droiddeck.debugApplicationIdSuffix=' } | Select-Object -First 1
}
$suffix = if ($suffixLine) { $suffixLine.Substring('droiddeck.debugApplicationIdSuffix='.Length) } else { '' }
& adb @adbArgs shell am start -W -n "com.droiddeck.launcher$suffix/com.droiddeck.launcher.MainActivity"
if ($LASTEXITCODE -ne 0) { throw 'App launch failed.' }
} finally {
Pop-Location
}
+81
View File
@@ -0,0 +1,81 @@
const assert = require('assert');
const vm = require('vm');
const source = require('fs').readFileSync(process.argv[2], 'utf8');
const calls = [], requests = [], errors = [], timers = new Set();
let changed;
const apps = { RunGame(...args) { calls.push({receiver: this, args}); return 'native'; } };
const openvr = {
RegisterForVRModeChange(callback) { changed = callback; callback(false); return {unregister() {}}; },
StartVR(...args) { return args; },
};
const overview = new Map([
['611670', {vr_only: true}],
['620980', {vr_supported: true}],
['730', {vr_only: false, vr_supported: false}],
['18446744069414584320', {BSupportsVR() { return true; }}],
]);
const context = {
SteamClient: {Apps: apps, OpenVR: openvr},
appStore: {GetAppOverviewByGameID: id => overview.get(id)},
droiddeckLaunchSteamVr: id => requests.push(id),
setTimeout(callback, ms) { assert.strictEqual(ms, 600000); timers.add(callback); return callback; },
clearTimeout: timer => timers.delete(timer),
console: {error: message => errors.push(message)},
};
context.window = context;
vm.createContext(context);
assert.strictEqual(vm.runInContext(source, context), 'installed');
assert.strictEqual(vm.runInContext(source, context), 'present');
assert.strictEqual(apps.RunGame(730, '', -1, 100), 'native');
assert.deepStrictEqual(requests, []);
assert.strictEqual(calls[0].receiver, apps);
const receiver = {};
apps.RunGame.call(receiver, '611670', 'custom args', 2, 100);
apps.RunGame('611670', 'duplicate', -1, 100);
apps.RunGame(620980, '', -1, 100);
assert.deepStrictEqual(requests, ['611670', '620980']);
assert.strictEqual(calls.length, 1);
changed(false);
assert.strictEqual(calls.length, 1);
changed(true);
assert.strictEqual(calls.length, 3);
assert.strictEqual(calls[1].receiver, receiver);
assert.deepStrictEqual(calls[1].args, ['611670', 'custom args', 2, 100]);
assert.deepStrictEqual(calls[2].args, [620980, '', -1, 100]);
assert.strictEqual(timers.size, 0);
changed(true);
assert.strictEqual(calls.length, 3);
apps.RunGame('18446744069414584320', '', -1, 100);
assert.deepStrictEqual(calls[3].args, ['18446744069414584320', '', -1, 100]);
assert.strictEqual(requests.at(-1), '18446744069414584320');
assert.strictEqual(timers.size, 0);
apps.RunGame('250820', '', -1, 100);
openvr.StartVR(true);
assert.deepStrictEqual(requests.slice(-2), ['250820', '250820']);
assert.strictEqual(calls.length, 4);
changed(false);
apps.RunGame('611670', '', -1, 100);
for (const timeout of [...timers]) { timers.delete(timeout); timeout(); }
assert.strictEqual(context.__droiddeckSteamVr.pending.size, 0);
assert.strictEqual(errors.length, 1);
changed(true);
assert.strictEqual(calls.length, 4);
apps.RunGame('611670', '', -1, 100);
assert.strictEqual(calls.length, 5);
assert.strictEqual(context.__droiddeckSteamVr.lastError, null);
changed(false);
context.droiddeckLaunchSteamVr = () => { throw new Error('bridge disconnected'); };
assert.throws(() => apps.RunGame('611670', '', -1, 100), /bridge disconnected/);
assert.strictEqual(context.__droiddeckSteamVr.pending.size, 0);
assert.strictEqual(timers.size, 0);
delete context.droiddeckLaunchSteamVr;
assert.strictEqual(apps.RunGame('611670', '', -1, 100), 'native');
assert.strictEqual(apps.RunGame('250820', '', -1, 100), 'native');
assert.deepStrictEqual(openvr.StartVR(true), [true]);
assert.strictEqual(calls.length, 7);
+169
View File
@@ -0,0 +1,169 @@
import os
from pathlib import Path
import re
import shutil
import subprocess
import sys
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[2]
SOURCE = ROOT / 'app/src/main/cpp/waylandcomp/src/compositor.c'
def extract_function(source, name):
match = re.search(r'^(?:static )?(?:void|int|uint32_t) ' + re.escape(name) + r'\([^;]*?\) \{',
source, re.MULTILINE)
if match is None:
raise AssertionError(f'Missing compositor function: {name}')
cursor, depth = match.end(), 1
while depth:
depth += (source[cursor] == '{') - (source[cursor] == '}')
cursor += 1
return source[match.start():cursor]
PREAMBLE = r'''
#define _GNU_SOURCE
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
struct wl_resource { int version, modes, done, hz, w, h; struct wl_resource *next; };
struct surface { int xdg_toplevel, toplevel_committed; struct surface *next; };
struct input_msg { int type, p1, p2, p3, p4; };
static struct wl_resource second = { .version = 1 };
static struct wl_resource first = { .version = 4, .next = &second };
static struct surface surface = { .xdg_toplevel = 1, .toplevel_committed = 1 };
static int g_input_pipe[2] = {-1, -1};
static atomic_int g_output_refresh_mhz = 60000;
static atomic_int g_output_refresh_pending;
static int g_output_announced_refresh_mhz = 60000;
static int g_output_w = 1920, g_output_h = 1080;
static int flushes, resizes, pointers, ticks;
static int scale_changes, scale_mode, scale_alignment, renders;
static void *g_display;
#define WL_OUTPUT_MODE_CURRENT 1
#define WL_OUTPUT_MODE_PREFERRED 2
#define wl_resource_for_each(r, list) for ((r) = &first; (r); (r) = (r)->next)
#define wl_list_for_each(s, list, link) for ((s) = &surface; (s); (s) = (s)->next)
static int wl_resource_get_version(struct wl_resource *r) { return r->version; }
static void wl_output_send_mode(struct wl_resource *r, int flags, int w, int h, int hz) {
assert(flags == 3); r->modes++; r->hz = hz; r->w = w; r->h = h;
}
static void wl_output_send_done(struct wl_resource *r) { r->done++; }
static void wl_display_flush_clients(void *display) { (void)display; flushes++; }
static void send_toplevel_configure(struct surface *s) { (void)s; resizes++; }
#define droiddeck_log(...) ((void)0)
#define deliver_key(m) ((void)0)
#define pointer_event(...) ((void)0)
#define pointer_button(...) ((void)0)
#define scroll_event(...) ((void)0)
#define pointer_delta(...) ((void)0)
#define deliver_touch(m) ((void)0)
#define deliver_pointer(m) (pointers++)
static void on_vsync(int64_t ns) { assert(ns > 0); ticks++; }
static void vk_present_set_scale_mode(int mode, int alignment) {
scale_changes++; scale_mode = mode; scale_alignment = alignment;
}
static void schedule_render(void) { renders++; }
'''
CHECKS = r'''
static int queued(void) { int n; assert(ioctl(g_input_pipe[0], FIONREAD, &n) == 0); return n; }
static void drain(void) { on_input_readable(g_input_pipe[0], 0, NULL); }
static void expect(int modes, int hz, int resize_count) {
assert(first.modes == modes && second.modes == modes);
assert(first.hz == hz && second.hz == hz);
assert(first.w == 1920 && second.w == 1920 && first.h == 1080 && second.h == 1080);
assert(first.done == modes && second.done == 0);
assert(resizes == resize_count);
assert(pointers == 0);
}
int main(void) {
droiddeck_wayland_set_output_refresh_rate(120000);
assert(atomic_load(&g_output_refresh_mhz) == 120000);
assert(!atomic_load(&g_output_refresh_pending));
assert(first.modes == 0 && second.modes == 0);
assert(pipe2(g_input_pipe, O_CLOEXEC | O_NONBLOCK) == 0);
g_output_announced_refresh_mhz = atomic_load(&g_output_refresh_mhz);
droiddeck_wayland_set_scale_mode(1, 4);
assert(queued() == sizeof(struct input_msg));
drain();
assert(scale_changes == 1 && scale_mode == 1 && scale_alignment == 4 && renders == 1);
assert(first.modes == 0 && second.modes == 0);
droiddeck_wayland_set_output_refresh_rate(120000);
assert(queued() == 0);
droiddeck_wayland_set_output_refresh_rate(60000);
assert(first.modes == 0 && queued() == sizeof(struct input_msg));
drain(); expect(1, 60000, 0); assert(flushes == 1);
droiddeck_wayland_set_output_refresh_rate(60000);
assert(queued() == 0); drain(); expect(1, 60000, 0);
droiddeck_wayland_set_output_refresh_rate(90000);
droiddeck_wayland_set_output_refresh_rate(120000);
drain(); expect(2, 120000, 0);
droiddeck_wayland_set_output_refresh_rate(90000);
droiddeck_wayland_set_output_refresh_rate(120000);
drain(); expect(2, 120000, 0);
droiddeck_wayland_set_output_refresh_rate(60000);
droiddeck_wayland_output_resized();
drain(); expect(3, 60000, 1); assert(flushes == 3);
struct input_msg wake = {10, 0, 0, 0, 0};
int n = 0;
while (write(g_input_pipe[1], &wake, sizeof(wake)) == sizeof(wake)) n++;
assert(n > 0 && errno == EAGAIN);
droiddeck_wayland_set_output_refresh_rate(120000);
drain(); expect(4, 120000, 1);
droiddeck_wayland_set_output_refresh_rate(0);
drain(); expect(5, 60000, 1);
droiddeck_wayland_set_output_refresh_rate(-1);
assert(queued() == 0);
struct input_msg vsync = {5, 0, 16000000, 0, 0};
assert(write(g_input_pipe[1], &vsync, sizeof(vsync)) == sizeof(vsync));
drain(); expect(5, 60000, 1); assert(ticks == 1);
assert(nominal_refresh_ns() == 16666666);
assert(flushes == 5);
assert(scale_changes == 1 && scale_mode == 1 && scale_alignment == 4 && renders == 1);
droiddeck_wayland_set_scale_mode(1, 3);
droiddeck_wayland_set_output_refresh_rate(90000);
drain(); expect(6, 90000, 1);
assert(scale_changes == 2 && scale_mode == 1 && scale_alignment == 3 && renders == 2);
close(g_input_pipe[0]); close(g_input_pipe[1]);
puts("PASS");
}
'''
@unittest.skipUnless(sys.platform.startswith('linux'), 'needs Linux nonblocking pipes')
class OutputRefreshTest(unittest.TestCase):
def test_refresh_notifications_preserve_client_pacing_without_resizing(self):
compiler = shutil.which(os.environ.get('CC', 'cc'))
if compiler is None:
self.skipTest('A host C compiler is required')
source = SOURCE.read_text()
functions = ('nominal_refresh_ns', 'output_send_mode', 'output_notify_mode',
'output_resized', 'on_input_readable', 'droiddeck_wayland_output_resized',
'droiddeck_wayland_set_scale_mode',
'droiddeck_wayland_set_output_refresh_rate')
harness = PREAMBLE + '\n'.join(extract_function(source, name) for name in functions) + CHECKS
with tempfile.TemporaryDirectory(prefix='droiddeck-output-refresh-') as folder:
path = Path(folder)
(path / 'refresh.c').write_text(harness)
binary = path / 'refresh-test'
build = subprocess.run([compiler, '-std=c11', '-Wall', '-Wextra', '-Werror',
'-Wno-unused-parameter', '-O2', str(path / 'refresh.c'),
'-o', str(binary)], capture_output=True, text=True)
self.assertEqual(0, build.returncode, build.stderr)
result = subprocess.run([str(binary)], capture_output=True, text=True, timeout=10)
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual('PASS', result.stdout.strip())
if __name__ == '__main__':
unittest.main()
+178
View File
@@ -0,0 +1,178 @@
import importlib.util
from pathlib import Path
import struct
import tempfile
import unittest
from unittest import mock
from zipfile import ZipFile
spec = importlib.util.spec_from_file_location(
'prepare_windows_runtime', Path(__file__).resolve().parents[1] / 'prepare_windows_runtime.py')
prepare = importlib.util.module_from_spec(spec)
spec.loader.exec_module(prepare)
class WindowsRuntimePreparationTest(unittest.TestCase):
def test_archive_paths_cannot_escape_destination(self):
for name in ('../outside', '/absolute', 'dir/../../outside', 'C:/absolute', 'dir\\outside', '.'):
with self.subTest(name=name), self.assertRaises(ValueError):
prepare.validate_relative(name)
def test_tracked_outputs_cannot_be_overwritten(self):
with mock.patch.object(prepare, 'git', return_value=b'tracked/file\n'):
with self.assertRaisesRegex(ValueError, 'overwrite tracked'):
prepare.checked_output('tracked/file')
def test_rebuilt_libraries_must_match_each_architecture(self):
with tempfile.TemporaryDirectory() as folder:
path = Path(folder) / 'libblsession.so'
elf = bytearray(64)
elf[:4] = b'\x7fELF'
elf[5] = 1
elf[18:20] = struct.pack('<H', 183)
path.write_bytes(elf)
self.assertEqual(prepare.validate_rebuilt(path, 183), elf)
with self.assertRaisesRegex(ValueError, 'wrong ELF architecture'):
prepare.validate_rebuilt(path, 62)
path.write_bytes(b'not an ELF')
with self.assertRaisesRegex(ValueError, 'wrong ELF architecture'):
prepare.validate_rebuilt(path, 183)
def test_new_committed_native_changes_reject_donor_reuse(self):
with mock.patch.object(prepare, 'git', return_value=prepare.DONOR_TREE.encode()), \
mock.patch.object(prepare, 'changed', return_value={'tools/linuxfs/preload/net.c'}):
with self.assertRaisesRegex(ValueError, 'Sources changed since'):
prepare.check_sources()
def test_vr_sources_are_classified_as_rebuilt_including_local_and_new_files(self):
committed = {'tools/vr/src/vk_layer_droiddeck_vr.c', 'tools/vr/build.sh'}
local = {'tools/vr/src/presenter.cpp'}
new = {'tools/vr/src/fence_tracker.h'}
with mock.patch.object(prepare, 'changed', side_effect=[committed, local, committed]), \
mock.patch.object(prepare, 'git', side_effect=[prepare.DONOR_TREE.encode(),
b'tools/vr/src/fence_tracker.h\0']):
classifications = prepare.check_sources()
self.assertEqual(set(classifications), committed | local | new)
self.assertTrue(all('never imported from donor' in value for value in classifications.values()))
def test_preparation_excludes_all_donor_vr_files_and_records_replacement_origin(self):
with tempfile.TemporaryDirectory() as folder:
root = Path(folder)
apk = root / 'donor.apk'
vr_name = 'layer/libVkLayer_droiddeck_vr.so'
with ZipFile(apk, 'w') as package:
package.writestr(prepare.VR_PREFIX + vr_name, b'old library')
package.writestr(prepare.VR_PREFIX + 'obsolete.so', b'obsolete donor file')
package.writestr('assets/linuxfs/unchanged.so', b'unchanged component')
(root / 'app/build').mkdir(parents=True)
(root / 'app/build/runtime-inputs.json').write_text('{}')
vr_manifest = {'version': 1, 'source_commit': 'current', 'outputs': {vr_name: 'verified'}}
with mock.patch.object(prepare, 'ROOT', root), \
mock.patch.object(prepare, 'check_sources', return_value={}), \
mock.patch.object(prepare, 'DONOR_SHA256', prepare.runtime.digest(apk)), \
mock.patch.object(prepare, 'REBUILT', {}), \
mock.patch.object(prepare, 'JNI', ()), mock.patch.object(prepare, 'PREPARED', ()), \
mock.patch.object(prepare, 'LOCAL_BUILD_PLUMBING', set()), \
mock.patch.object(prepare, 'source_assets', return_value={}), \
mock.patch.object(prepare, 'read_gamescope', return_value=({}, {
'GAMESCOPE_REPO': 'repo', 'GAMESCOPE_TAG': 'tag', 'GAMESCOPE_SHA256': 'hash'})), \
mock.patch.object(prepare, 'validate_tracked_assets', return_value={}), \
mock.patch.object(prepare, 'git', return_value=b''), \
mock.patch.object(prepare.runtime, 'record'), mock.patch.object(prepare.runtime, 'verify'), \
mock.patch.object(prepare.vr_build, 'build', return_value=({vr_name: b'new library'}, vr_manifest)) as build:
prepare.prepare(apk, root, root, llvm_strip=root / 'bin/llvm-strip')
build.assert_called_once()
vr_folder = root / 'app/src/main' / prepare.VR_PREFIX
self.assertEqual((vr_folder / vr_name).read_bytes(), b'new library')
self.assertFalse((vr_folder / 'obsolete.so').exists())
import json
provenance = json.loads((root / 'app/build/windows-runtime-provenance.json').read_text())
self.assertEqual(provenance['vr_build'], vr_manifest)
self.assertIn('VR rebuilt', provenance['outputs']['app/src/main/' + prepare.VR_PREFIX + vr_name]['origin'])
def test_invalid_cached_vr_stops_before_reading_or_staging_the_donor(self):
with tempfile.TemporaryDirectory() as folder:
root = Path(folder)
apk = root / 'donor.apk'
apk.write_bytes(b'donor')
with mock.patch.object(prepare, 'check_sources', return_value={}), \
mock.patch.object(prepare, 'DONOR_SHA256', prepare.runtime.digest(apk)), \
mock.patch.object(prepare, 'REBUILT', {}), \
mock.patch.object(prepare, 'read_gamescope', return_value=({}, {})), \
mock.patch.object(prepare.vr_build, 'validate', side_effect=ValueError('VR build is stale')), \
mock.patch.object(prepare.vr_build, 'build') as build, \
mock.patch.object(prepare, 'ZipFile') as archive:
with self.assertRaisesRegex(ValueError, 'VR build is stale'):
prepare.prepare(apk, root, root, rebuilt_vr=root, vk_include=root)
build.assert_not_called()
archive.assert_not_called()
def test_current_helpers_are_allowed_only_when_restaged_from_source(self):
helpers = prepare.LOCAL_SESSION_HELPERS
sources = {prepare.ROOT / path: 'assets/linuxfs/' + path.split('/overlay/')[1]
for path in helpers}
for local_changes in (set(), helpers):
with self.subTest(local_changes=local_changes), \
mock.patch.object(prepare, 'source_assets', return_value=sources), \
mock.patch.object(prepare, 'changed', side_effect=[helpers, local_changes, helpers]), \
mock.patch.object(prepare, 'git', side_effect=[prepare.DONOR_TREE.encode(), b'']):
classifications = prepare.check_sources()
self.assertEqual(set(classifications), helpers)
self.assertTrue(all('restaged from current source' in action
for action in classifications.values()))
with mock.patch.object(prepare, 'source_assets', return_value={}), \
mock.patch.object(prepare, 'changed', return_value=helpers), \
mock.patch.object(prepare, 'git', return_value=prepare.DONOR_TREE.encode()):
with self.assertRaisesRegex(ValueError, 'Sources changed since'):
prepare.check_sources()
def test_cardboard_changes_are_compiled_by_gradle_not_reused_from_donor(self):
changed = {'app/src/main/cpp/waylandcomp/src/cardboard.c'}
with mock.patch.object(prepare, 'changed', side_effect=[changed, changed, changed]), \
mock.patch.object(prepare, 'git', side_effect=[prepare.DONOR_TREE.encode(), b'']):
classifications = prepare.check_sources()
self.assertEqual(set(classifications), changed)
self.assertIn('compiled by Gradle; not imported', next(iter(classifications.values())))
def test_other_helper_changes_still_need_an_audit(self):
with mock.patch.object(prepare, 'git', return_value=prepare.DONOR_TREE.encode()), \
mock.patch.object(prepare, 'changed', return_value={
'tools/linuxfs/overlay/usr/local/bin/droiddeck-session'}):
with self.assertRaisesRegex(ValueError, 'Sources changed since'):
prepare.check_sources()
def test_uncommitted_native_changes_reject_donor_reuse(self):
with mock.patch.object(prepare, 'git', side_effect=[prepare.DONOR_TREE.encode(), b'']), \
mock.patch.object(prepare, 'changed', side_effect=[set(), {'tools/linuxfs/preload/net.c'}]):
with self.assertRaisesRegex(ValueError, 'Unreviewed runtime'):
prepare.check_sources()
def test_wrong_apk_hash_fails_before_replacement_or_output_access(self):
with tempfile.TemporaryDirectory() as folder:
apk = Path(folder) / 'incorrect.apk'
apk.write_bytes(b'incorrect donor')
with mock.patch.object(prepare, 'check_sources', return_value={}), \
mock.patch.object(prepare, 'read_gamescope') as read_gamescope:
with self.assertRaisesRegex(ValueError, 'Donor APK checksum mismatch'):
prepare.prepare(apk, Path(folder) / 'missing', Path(folder) / 'missing.tzst')
read_gamescope.assert_not_called()
def test_sdk_bridge_is_compiled_locally_while_other_untracked_native_files_fail(self):
with mock.patch.object(prepare, 'source_assets', return_value={}), \
mock.patch.object(prepare, 'changed', side_effect=[set(), {'app/src/main/cpp/CMakeLists.txt'}, set()]), \
mock.patch.object(prepare, 'git', side_effect=[prepare.DONOR_TREE.encode(),
b'app/src/main/cpp/viture/sdk_bridge.cpp\0']):
classifications = prepare.check_sources()
self.assertEqual(set(classifications), {'app/src/main/cpp/CMakeLists.txt',
'app/src/main/cpp/viture/sdk_bridge.cpp'})
self.assertTrue(all('compiled by Gradle' in action for action in classifications.values()))
with mock.patch.object(prepare, 'changed', side_effect=[set(), set()]), \
mock.patch.object(prepare, 'git', side_effect=[prepare.DONOR_TREE.encode(),
b'app/src/main/cpp/unreviewed.cpp\0']):
with self.assertRaisesRegex(ValueError, 'Unreviewed runtime'):
prepare.check_sources()
if __name__ == '__main__':
unittest.main()
+76
View File
@@ -0,0 +1,76 @@
"""Exercise the Windows launcher without building an APK or contacting a phone."""
import os
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
POWERSHELL = shutil.which('pwsh') or shutil.which('powershell')
SCRIPT = Path(__file__).resolve().parents[1] / 'run_windows.ps1'
@unittest.skipUnless(os.name == 'nt' and POWERSHELL, 'Windows PowerShell launcher')
class WindowsLauncherTest(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory(prefix='droiddeck windows launcher ')
self.addCleanup(self.temp.cleanup)
self.root = Path(self.temp.name)
(self.root / 'tools').mkdir()
shutil.copyfile(SCRIPT, self.root / 'tools/run_windows.ps1')
self.calls = self.root / 'calls.txt'
(self.root / 'gradlew.bat').write_text(
'@echo off\necho gradle JAVA_HOME=%JAVA_HOME%>>"%DROIDDECK_TEST_CALLS%"\n'
'exit /b %DROIDDECK_TEST_GRADLE_EXIT%\n')
(self.root / 'adb.cmd').write_text(
'@echo off\necho adb %*>>"%DROIDDECK_TEST_CALLS%"\nexit /b 0\n')
self.env = dict(os.environ, JAVA_HOME=r'C:\provided-java',
DROIDDECK_TEST_CALLS=str(self.calls), DROIDDECK_TEST_GRADLE_EXIT='0')
self.env['PATH'] = str(self.root) + os.pathsep + self.env.get('PATH', '')
def launch(self):
return subprocess.run(
[POWERSHELL, '-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass',
'-File', str(self.root / 'tools/run_windows.ps1'), '-Serial', 'test-phone'],
env=self.env, cwd=self.root, capture_output=True, text=True, timeout=30)
def test_missing_optional_configs_keep_environment_java_and_base_package(self):
result = self.launch()
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
calls = self.calls.read_text().splitlines()
self.assertEqual(calls[0], r'gradle JAVA_HOME=C:\provided-java')
self.assertEqual(calls[1], 'adb -s test-phone install -r app/build/outputs/apk/debug/app-debug.apk')
self.assertEqual(calls[2], 'adb -s test-phone shell am start -W -n '
'com.droiddeck.launcher/com.droiddeck.launcher.MainActivity')
def test_existing_configs_select_java_and_suffixed_package(self):
(self.root / '.gradle').mkdir()
(self.root / '.gradle/config.properties').write_text('java.home=C\\:\\\\configured-java\n')
(self.root / 'local.properties').write_text('droiddeck.debugApplicationIdSuffix=.xr\n')
result = self.launch()
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
calls = self.calls.read_text().splitlines()
self.assertEqual(calls[0], r'gradle JAVA_HOME=C:\configured-java')
self.assertTrue(calls[2].endswith('com.droiddeck.launcher.xr/com.droiddeck.launcher.MainActivity'))
def test_empty_configs_use_defaults(self):
(self.root / '.gradle').mkdir()
(self.root / '.gradle/config.properties').touch()
(self.root / 'local.properties').touch()
result = self.launch()
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
calls = self.calls.read_text().splitlines()
self.assertEqual(calls[0], r'gradle JAVA_HOME=C:\provided-java')
self.assertTrue(calls[2].endswith('com.droiddeck.launcher/com.droiddeck.launcher.MainActivity'))
def test_build_failure_still_stops_before_installation(self):
self.env['DROIDDECK_TEST_GRADLE_EXIT'] = '1'
result = self.launch()
self.assertNotEqual(result.returncode, 0)
self.assertIn('Android build failed.', result.stderr)
self.assertEqual(self.calls.read_text().splitlines(), [r'gradle JAVA_HOME=C:\provided-java'])
if __name__ == '__main__':
unittest.main()
+25 -1
View File
@@ -1,11 +1,12 @@
import importlib.util
import gzip
import io
from pathlib import Path
from pathlib import Path, PureWindowsPath
import subprocess
import tarfile
import tempfile
import unittest
from unittest import mock
from zipfile import ZipFile
@@ -95,6 +96,29 @@ class RuntimeInputsTest(unittest.TestCase):
package.writestr(name, (self.root / output['path']).read_bytes())
runtime.check_apk(apk, self.root)
def test_windows_manifest_matches_portable_apk_paths(self):
path_type = type(self.root)
original_relative_to = path_type.relative_to
def windows_relative_to(path, *args, **kwargs):
return PureWindowsPath(original_relative_to(path, *args, **kwargs))
with mock.patch.object(path_type, 'relative_to', windows_relative_to):
runtime.record(self.root)
data = runtime.verify(self.root)
self.assertIn('tools/linuxfs/preload/test.c', data['sources'])
nested_asset = 'assets/linuxfs/usr/local/bin/gamescope'
self.assertEqual(data['outputs'][nested_asset]['path'], 'app/src/main/' + nested_asset)
for name in data['sources']:
self.assertNotIn('\\', name)
apk = self.root / 'portable.apk'
with ZipFile(apk, 'w') as package:
for name, output in data['outputs'].items():
self.assertNotIn('\\', name)
self.assertNotIn('\\', output['path'])
package.writestr(name, (self.root / output['path']).read_bytes())
runtime.check_apk(apk, self.root)
def test_android_expanding_or_renaming_a_compressed_inventory_is_rejected(self):
runtime.record(self.root)
data = runtime.verify(self.root)
@@ -0,0 +1,50 @@
import json
import os
from pathlib import Path
import subprocess
import tempfile
import unittest
BIN = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin'
class SteamCompatEnvironmentTest(unittest.TestCase):
def test_library_launch_preserves_guest_environment_without_steam_client_libraries(self):
source = (BIN / 'droiddeck-session').read_text()
setup = source[source.index(' _bl_compat_env='):source.index(' export SDL_VIDEO_X11_DGAMOUSE=')]
launch = next(line for line in source.splitlines()
if 'droiddeck-steam-compat "$steam_root"' in line)
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
helper = root / 'droiddeck-steam-compat'
helper.write_text('#!/usr/bin/env python3\nimport json, os, sys\n'
'json.dump({"env": dict(os.environ), "args": sys.argv[1:]}, '
'open(os.environ["BRIDGE_CAPTURE"], "w"))\n')
helper.chmod(0o755)
for inherited in ({}, {'LD_LIBRARY_PATH': '/guest/driver:/guest/libs'},
{'LD_LIBRARY_PATH': '', 'STEAM_RUNTIME': ''},
{'LD_LIBRARY_PATH': '/custom path/lib', 'STEAM_RUNTIME': '0'}):
with self.subTest(inherited=inherited):
env = dict(os.environ, PATH=str(root) + os.pathsep + os.environ['PATH'],
BRIDGE_CAPTURE=str(root / 'captured.json'),
VK_ICD_FILENAMES='/guest/driver/turnip.json', steam_root='/steam')
for key in ('LD_LIBRARY_PATH', 'STEAM_RUNTIME'):
env.pop(key, None)
env.update(inherited)
result = subprocess.run(['bash', '-c', setup + '\n' + launch + '\nwait $!\n'
'printf "%s\\n" "$STEAM_RUNTIME" "$LD_LIBRARY_PATH"'],
env=env, capture_output=True, text=True, timeout=10)
self.assertEqual(result.returncode, 0, result.stderr)
bridge = json.loads((root / 'captured.json').read_text())
self.assertEqual(bridge['args'], ['/steam'])
for key in ('LD_LIBRARY_PATH', 'STEAM_RUNTIME', 'VK_ICD_FILENAMES'):
self.assertEqual(bridge['env'].get(key), env.get(key), key)
client_path = '/steam/steamrtarm64:/steam/steamrtarm64/panorama'
if inherited.get('LD_LIBRARY_PATH'):
client_path += ':' + inherited['LD_LIBRARY_PATH']
self.assertEqual(result.stdout.splitlines(), ['1', client_path])
if __name__ == '__main__':
unittest.main()
+87
View File
@@ -108,6 +108,45 @@ class FakePage:
@unittest.skipUnless(shutil.which("node"), "node is not installed")
class PageScriptTest(unittest.TestCase):
def test_vr_games_wait_for_steamvr_and_preserve_the_original_launch(self):
with tempfile.TemporaryDirectory() as tmp:
script = Path(tmp) / "vr.js"
script.write_text(LIVE["VR_JS"])
harness = Path(__file__).with_name("steam_vr_launch_harness.js")
result = subprocess.run(["node", str(harness), str(script)],
capture_output=True, text=True, timeout=20)
self.assertEqual(result.returncode, 0, result.stderr)
def test_steamvr_library_launch_uses_binding_and_other_games_pass_through(self):
harness_source = r"""
const assert = require('assert');
globalThis.window = globalThis;
const calls = [], requests = [];
const apps = { RunGame(...args) { assert.strictEqual(this, apps); calls.push(args); return 'native'; } };
globalThis.SteamClient = { Apps: apps };
window.droiddeckLaunchSteamVr = id => requests.push(id);
const script = require('fs').readFileSync(process.argv[2], 'utf8');
assert.strictEqual(eval(script), 'installed');
assert.strictEqual(eval(script), 'present');
assert.strictEqual(apps.RunGame('250820', '', -1, 100), undefined);
apps.RunGame(250820, '', -1, 100);
assert.deepStrictEqual(requests, ['250820', '250820']);
assert.deepStrictEqual(calls, []);
assert.strictEqual(apps.RunGame('611670', 'custom args', -1, 100), 'native');
assert.strictEqual(apps.RunGame('2508200', '', -1, 100), 'native');
delete window.droiddeckLaunchSteamVr;
assert.strictEqual(apps.RunGame('250820', '', -1, 100), 'native');
assert.deepStrictEqual(calls, [['611670', 'custom args', -1, 100], ['2508200', '', -1, 100], ['250820', '', -1, 100]]);
"""
with tempfile.TemporaryDirectory() as tmp:
script = Path(tmp) / "vr.js"
script.write_text(LIVE["VR_JS"])
harness = Path(tmp) / "test.js"
harness.write_text(harness_source)
result = subprocess.run(["node", str(harness), str(script)],
capture_output=True, text=True, timeout=20)
self.assertEqual(result.returncode, 0, result.stderr)
def test_transport_framing_auth_and_notifications(self):
with tempfile.TemporaryDirectory() as tmp:
script = Path(tmp) / "install.js"
@@ -493,6 +532,54 @@ class SwitchToDesktopTest(unittest.TestCase):
self.assertNotIn(("Runtime.addBinding", LIVE["SWITCH_BINDING"]), helper.page.calls)
self.assertNotIn(LIVE["SWITCH_JS"], helper.page.scripts)
def test_vr_bridge_is_installed_for_both_steam_ui_modes(self):
for steamos in (False, True):
helper = LIVE["Helper"]("/steam", 1, steamos=steamos)
helper.attach()
self.assertIn(("Runtime.addBinding", LIVE["VR_BINDING"]), helper.page.calls)
self.assertIn(LIVE["VR_JS"], helper.page.scripts)
def test_vr_bridge_accepts_only_steamvr_and_coalesces_requests(self):
helper = LIVE["Helper"]("/steam", 1)
helper.page = mock.Mock()
event = lambda payload: {"method": "Runtime.bindingCalled", "params": {
"name": LIVE["VR_BINDING"], "payload": payload}}
with mock.patch.dict(self.globals, {"launch_steamvr": mock.Mock()}) as patched:
launch = patched["launch_steamvr"]
helper.page.drain.return_value = [event("0"), event("250820; touch /tmp/wrong"),
event("-1"), event("01"), event("18446744073709551616"),
event("\u0661"), event(None), event({})]
helper.drain()
launch.assert_not_called()
helper.page.drain.return_value = [event("250820"), event("250820")]
helper.drain()
launch.assert_called_once_with()
def test_vr_game_requests_share_an_in_progress_helper_and_retry_after_it_exits(self):
helper = LIVE["Helper"]("/steam", 1)
helper.page = mock.Mock()
event = lambda app: {"method": "Runtime.bindingCalled", "params": {
"name": LIVE["VR_BINDING"], "payload": app}}
child = mock.Mock()
child.poll.return_value = None
with mock.patch.dict(self.globals, {"launch_steamvr": mock.Mock(return_value=child)}) as patched:
helper.page.drain.return_value = [event("611670"), event("620980")]
helper.drain()
helper.drain()
patched["launch_steamvr"].assert_called_once_with()
child.poll.return_value = 0
helper.drain()
self.assertEqual(patched["launch_steamvr"].call_count, 2)
def test_vr_launch_runs_helper_without_shell_in_background(self):
with tempfile.TemporaryDirectory() as tmp, mock.patch.dict(os.environ, {"XDG_RUNTIME_DIR": tmp}):
with mock.patch.object(self.globals["subprocess"], "Popen") as popen:
LIVE["launch_steamvr"]()
self.assertEqual(popen.call_args.args, ([LIVE["VR_HELPER"], "launch"],))
self.assertTrue(popen.call_args.kwargs["start_new_session"])
self.assertNotIn("shell", popen.call_args.kwargs)
self.assertTrue((Path(tmp) / "droiddeck-steamvr/library-launch.log").exists())
def test_the_request_goes_to_steamos_session_select(self):
helper = LIVE["Helper"]("/steam", 1)
helper.page = mock.Mock()
+83
View File
@@ -0,0 +1,83 @@
import json
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest
HELPER = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin/droiddeck-steamvr'
SCRIPT = HELPER.read_text().split("<<'PY'\n", 1)[1].split('\nPY\n', 1)[0]
class SteamVrSettingsTest(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.path = Path(self.temp.name) / 'steamvr.vrsettings'
def run_defaults(self):
result = subprocess.run([sys.executable, '-', str(self.path)], input=SCRIPT,
capture_output=True, text=True, timeout=10)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(list(self.path.parent.iterdir()), [self.path])
return result
def test_new_install_defaults_to_manual_100_percent(self):
self.run_defaults()
self.assertEqual(json.loads(self.path.read_text()), {
'steamvr': {'supersampleManualOverride': True, 'supersampleScale': 1.0}})
def test_existing_automatic_gpu_estimate_does_not_override_app_resolution(self):
settings = {'GpuSpeed': {'gpuSpeedRenderTargetScale': 1.1},
'steamvr': {'environmentMode': 0}, 'driver_droiddeck': {'directMode': True}}
self.path.write_text(json.dumps(settings))
self.run_defaults()
settings['steamvr'].update(supersampleManualOverride=True, supersampleScale=1.0)
self.assertEqual(json.loads(self.path.read_text()), settings)
def test_explicit_user_and_per_game_settings_are_preserved_without_rewrite(self):
for manual in (False, True):
with self.subTest(manual=manual):
settings = {'steamvr': {'supersampleManualOverride': manual, 'supersampleScale': 0.7},
'steam.app.611670': {'resolutionScale': 80}}
content = json.dumps(settings)
self.path.write_text(content)
self.run_defaults()
self.assertEqual(self.path.read_text(), content)
def test_defaults_are_idempotent(self):
self.run_defaults()
before = self.path.read_bytes()
self.run_defaults()
self.assertEqual(self.path.read_bytes(), before)
def test_partial_user_settings_are_preserved(self):
self.path.write_text('{"steamvr": {"supersampleScale": 0.5}}')
self.run_defaults()
self.assertEqual(json.loads(self.path.read_text())['steamvr'], {
'supersampleScale': 0.5, 'supersampleManualOverride': True})
def test_a_crash_block_on_the_droiddeck_driver_is_cleared_and_others_kept(self):
settings = {'steamvr': {'supersampleManualOverride': True, 'supersampleScale': 1.0},
'driver_droiddeck': {'blocked_by_safe_mode': True, 'directMode': True},
'driver_lighthouse': {'blocked_by_safe_mode': True}}
self.path.write_text(json.dumps(settings))
self.assertIn('unblocked it', self.run_defaults().stdout)
settings['driver_droiddeck']['blocked_by_safe_mode'] = False
self.assertEqual(json.loads(self.path.read_text()), settings)
def test_the_crash_marker_is_removed_so_steamvr_does_not_start_in_safe_mode(self):
(self.path.parent / 'vrserver_crash_timestamp.txt').write_text('1792116926 1789606310 223')
self.assertIn('crash marker', self.run_defaults().stdout)
def test_invalid_settings_are_reported_without_losing_the_original(self):
for content in ('{broken', '[]', '{"steamvr": null}', '{"steamvr": []}'):
with self.subTest(content=content):
self.path.write_text(content)
self.assertIn('Could not set SteamVR resolution defaults', self.run_defaults().stderr)
self.assertEqual(self.path.read_text(), content)
if __name__ == '__main__':
unittest.main()
+125
View File
@@ -0,0 +1,125 @@
import importlib.util
import json
from pathlib import Path
import struct
import tempfile
import unittest
from unittest import mock
spec = importlib.util.spec_from_file_location(
'windows_vr_build', Path(__file__).resolve().parents[1] / 'vr/build_windows.py')
vr = importlib.util.module_from_spec(spec)
spec.loader.exec_module(vr)
class WindowsVrBuildTest(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.root = Path(self.temp.name)
self.out = self.root / 'build/vr'
self.include = self.root / 'ndk/include'
for name in vr.SOURCE_FILES:
self.write(self.root / name, b'build source\n')
for tree in vr.SOURCE_TREES:
self.write(self.root / tree / 'input.h', b'header\n')
for name, source in vr.CONFIGS.items():
self.write(self.root / source, b'{"name": "current"}\n')
self.write(self.out / name, b'{"name": "current"}\n')
for name in vr.LIBRARIES:
data = bytearray(64)
data[:6] = b'\x7fELF\x02\x01'
struct.pack_into('<HH', data, 16, 3, 183)
self.write(self.out / name, data)
for name in ('vulkan/vulkan_core.h', 'vulkan/vk_layer.h'):
self.write(self.include / name, b'vulkan header\n')
self.commit = mock.patch.object(vr, 'commit', return_value='current-revision')
self.commit.start()
self.addCleanup(self.commit.stop)
self.record()
@staticmethod
def write(path, data):
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(data)
def record(self):
self.manifest = {'version': 1, 'source_commit': 'current-revision',
'sources': vr.hashes(vr.sources(self.root)),
'headers': vr.hashes(vr.headers(self.include)),
'outputs': {n: vr.sha((self.out / n).read_bytes()) for n in vr.OUTPUTS}}
self.save_manifest()
def save_manifest(self):
(self.out / vr.MANIFEST).write_text(json.dumps(self.manifest))
def validate(self):
return vr.validate(self.out, self.include, self.root)
def test_complete_current_build_accepts_windows_line_endings(self):
self.write(self.root / 'tools/vr/src/input.h', b'header\r\n')
files, manifest = self.validate()
self.assertEqual(set(files), vr.OUTPUTS)
self.assertEqual(manifest['source_commit'], 'current-revision')
def test_missing_manifest_cannot_import_old_unattested_binaries(self):
(self.out / vr.MANIFEST).unlink()
with self.assertRaisesRegex(ValueError, 'Missing or invalid VR build manifest'):
self.validate()
def test_modified_or_new_source_rejects_cached_build(self):
for name in ('tools/vr/src/input.h', 'tools/vr/src/new.h'):
with self.subTest(name=name):
path = self.root / name
previous = path.read_bytes() if path.exists() else None
self.write(path, b'changed\n')
with self.assertRaisesRegex(ValueError, 'VR build is stale'):
self.validate()
if previous is None:
path.unlink()
else:
path.write_bytes(previous)
def test_changed_revision_or_vulkan_headers_reject_cached_build(self):
with mock.patch.object(vr, 'commit', return_value='new-revision'):
with self.assertRaisesRegex(ValueError, 'VR build is stale'):
self.validate()
self.write(self.include / 'vulkan/vulkan_core.h', b'new header\n')
with self.assertRaisesRegex(ValueError, 'VR build is stale'):
self.validate()
def test_changed_binary_rejects_cached_build(self):
path = self.out / sorted(vr.LIBRARIES)[0]
path.write_bytes(path.read_bytes() + b'changed')
with self.assertRaisesRegex(ValueError, 'output checksum mismatch'):
self.validate()
def test_wrong_architecture_rejected_even_with_matching_checksum(self):
path = self.out / sorted(vr.LIBRARIES)[0]
data = bytearray(path.read_bytes())
struct.pack_into('<H', data, 18, 62)
path.write_bytes(data)
self.record()
with self.assertRaisesRegex(ValueError, 'not an ARM64 ELF shared library'):
self.validate()
def test_missing_or_unexpected_outputs_reject_build(self):
path = self.out / sorted(vr.LIBRARIES)[0]
data = path.read_bytes()
path.unlink()
with self.assertRaisesRegex(ValueError, 'missing or unexpected'):
self.validate()
path.write_bytes(data)
self.write(self.out / 'old-library.so', data)
with self.assertRaisesRegex(ValueError, 'missing or unexpected'):
self.validate()
def test_old_configuration_rejected_even_with_matching_checksum(self):
self.write(self.out / sorted(vr.CONFIGS)[0], b'{"name": "old"}')
self.record()
with self.assertRaisesRegex(ValueError, 'configuration differs'):
self.validate()
if __name__ == '__main__':
unittest.main()
+13
View File
@@ -28,6 +28,19 @@ mkdir -p "$work/out/droiddeck/bin/linuxarm64" "$work/out/layer"
compile() {
local cmd=(sh -c '
set -e
cc -std=c11 -O2 -Wall -Wextra -Werror -Ibuild/vr/include -Itools/vr/src \
-o build/vr/test_fence_tracker tools/vr/tests/test_fence_tracker.c
build/vr/test_fence_tracker
c++ -std=c++17 -O2 -Wall -Wextra -Werror -Itools/vr/src \
-o build/vr/test_render_target tools/vr/tests/test_render_target.cpp
build/vr/test_render_target
c++ -std=c++17 -O2 -Wall -Wextra -Werror -Wno-unused-parameter -Itools/vr/src \
-o build/vr/test_joycon tools/vr/tests/test_joycon.cpp
build/vr/test_joycon
c++ -std=c++17 -O2 -Wall -Wextra -Werror -Wno-unused-parameter -Itools/vr/src \
-o build/vr/make_hand_models tools/vr/make_hand_models.cpp
mkdir -p build/vr/out/droiddeck/resources
build/vr/make_hand_models build/vr/out/droiddeck/resources/rendermodels
mkdir -p build/vr/stub
aarch64-linux-gnu-g++ -std=c++17 -O2 -Wall -Wextra -Werror -static-libstdc++ -static-libgcc -pthread \
-o build/vr/out/droiddeck/bin/linuxarm64/xr-display-mode tools/vr/src/xr_display_mode.cpp
+175
View File
@@ -0,0 +1,175 @@
"""Build DroidDeck's ARM64 VR components in Docker and attest their inputs."""
import argparse
import hashlib
import json
import os
from pathlib import Path
import shutil
import struct
import subprocess
import tempfile
ROOT = Path(__file__).resolve().parents[2]
IMAGE = 'droiddeck-local-cross:24.04-v3'
MANIFEST = 'vr-build.json'
SOURCE_TREES = ('tools/vr/src', 'tools/vr/tests', 'tools/vr/third_party',
'tools/vr/driver_droiddeck', 'tools/vr/layer')
SOURCE_FILES = ('tools/vr/build.sh', 'tools/vr/build_windows.py', 'tools/local-cross.Dockerfile',
'app/src/main/cpp/waylandcomp/src/headpose_shm.h',
'app/src/main/cpp/waylandcomp/generated/xdg-shell-protocol.c',
'app/src/main/cpp/waylandcomp/generated/xdg-shell-client-protocol.h')
LIBRARIES = {'droiddeck/bin/linuxarm64/driver_droiddeck.so', 'layer/libVkLayer_droiddeck_vr.so'}
CONFIGS = {
'droiddeck/driver.vrdrivermanifest': 'tools/vr/driver_droiddeck/driver.vrdrivermanifest',
'droiddeck/resources/settings/default.vrsettings': 'tools/vr/driver_droiddeck/resources/settings/default.vrsettings',
'layer/VK_LAYER_DROIDDECK_vr.json': 'tools/vr/layer/VK_LAYER_DROIDDECK_vr.json',
'layer/VK_LAYER_DROIDDECK_vr_implicit.json': 'tools/vr/layer/VK_LAYER_DROIDDECK_vr_implicit.json',
}
OUTPUTS = LIBRARIES | CONFIGS.keys()
def is_source(path):
return path in SOURCE_FILES or any(path.startswith(tree + '/') for tree in SOURCE_TREES)
def sha(data):
return hashlib.sha256(data).hexdigest()
def commit(root):
return subprocess.check_output(['git', '-C', str(root), 'rev-parse', 'HEAD']).decode().strip()
def sources(root):
paths = {root / name for name in SOURCE_FILES}
for tree in SOURCE_TREES:
paths.update(path for path in (root / tree).rglob('*') if path.is_file())
result = {}
for path in sorted(paths):
if path.is_symlink() or not path.resolve().is_relative_to(root.resolve()):
raise ValueError('VR source escapes the checkout: ' + str(path))
result[path.relative_to(root).as_posix()] = path.read_bytes().replace(b'\r\n', b'\n')
return result
def headers(include):
for name in ('vulkan/vulkan_core.h', 'vulkan/vk_layer.h'):
if not (include / name).is_file():
raise ValueError('Missing Vulkan header: ' + str(include / name))
return {path.relative_to(include).as_posix(): path.read_bytes().replace(b'\r\n', b'\n')
for tree in ('vulkan', 'vk_video') for path in sorted((include / tree).rglob('*'))
if path.is_file()}
def hashes(files):
return {name: sha(data) for name, data in files.items()}
def read_outputs(folder, root):
found = {p.relative_to(folder).as_posix() for p in folder.rglob('*') if p.is_file()}
if found - {MANIFEST} != OUTPUTS:
raise ValueError('VR build has missing or unexpected output files')
files = {}
for name in sorted(OUTPUTS):
path = folder / name
if path.is_symlink() or not path.resolve().is_relative_to(folder.resolve()):
raise ValueError('VR output escapes its build directory: ' + name)
data = path.read_bytes()
if name in LIBRARIES:
if (len(data) < 64 or data[:6] != b'\x7fELF\x02\x01' or
struct.unpack_from('<HH', data, 16) != (3, 183)):
raise ValueError('VR library is not an ARM64 ELF shared library: ' + name)
elif json.loads(data) != json.loads((root / CONFIGS[name]).read_bytes()):
raise ValueError('VR configuration differs from current source: ' + name)
files[name] = data
return files
def validate(folder, include, root=ROOT):
try:
manifest = json.loads((folder / MANIFEST).read_text(encoding='utf-8'))
except (OSError, ValueError) as error:
raise ValueError('Missing or invalid VR build manifest; rebuild with tools/vr/build_windows.py') from error
if (manifest.get('version') != 1 or manifest.get('source_commit') != commit(root) or
manifest.get('sources') != hashes(sources(root)) or
manifest.get('headers') != hashes(headers(include))):
raise ValueError('VR build is stale; rebuild with tools/vr/build_windows.py')
files = read_outputs(folder, root)
if manifest.get('outputs') != hashes(files):
raise ValueError('VR build output checksum mismatch; rebuild with tools/vr/build_windows.py')
return files, manifest
def build(folder, include, root=ROOT, image=IMAGE):
folder = folder.resolve()
workspace = (root / 'build/windows-setup').resolve()
if not workspace.is_relative_to(root.resolve()) or not folder.is_relative_to((root / 'build').resolve()):
raise ValueError('VR build output must be inside the checkout build directory')
input_files, input_headers, revision = sources(root), headers(include), commit(root)
docker = shutil.which('docker')
if not docker:
candidate = Path(os.environ.get('ProgramFiles', 'C:/Program Files')) / 'Docker/Docker/resources/bin/docker.exe'
if candidate.is_file():
docker = str(candidate)
if not docker:
raise ValueError('Docker Desktop is required to rebuild VR; start its Linux engine and retry')
subprocess.run([docker, 'info', '--format', '{{.OSType}}'], check=True)
workspace.mkdir(parents=True, exist_ok=True)
with tempfile.TemporaryDirectory(prefix='vr-build-', dir=workspace) as temp:
snapshot = Path(temp).resolve()
# build.sh's recursive output cleanup and TemporaryDirectory cleanup are
# confined to this new, verified directory, never the real checkout.
if not snapshot.is_relative_to(workspace):
raise ValueError('Temporary VR workspace escapes the checkout')
inspect = subprocess.run([docker, 'image', 'inspect', '--format', '{{.Id}}', image],
capture_output=True, text=True)
if inspect.returncode:
if image != IMAGE:
raise ValueError('VR compiler image is not available locally: ' + image)
subprocess.run([docker, 'build', '--tag', image, '--file', '-', str(snapshot)],
input=input_files['tools/local-cross.Dockerfile'], check=True)
image_id = subprocess.check_output([docker, 'image', 'inspect', '--format', '{{.Id}}', image]).decode().strip()
for name, data in input_files.items():
target = snapshot / name
target.parent.mkdir(parents=True, exist_ok=True)
target.write_bytes(data)
for name, data in input_headers.items():
target = snapshot / 'vk-include' / name
target.parent.mkdir(parents=True, exist_ok=True)
target.write_bytes(data)
subprocess.run([docker, 'run', '--rm', '--platform', 'linux/amd64',
'--mount', f'type=bind,source={snapshot},target=/src', '-w', '/src',
'-e', 'VK_INCLUDE=/src/vk-include', image_id,
'bash', 'tools/vr/build.sh', '--native', '/src/out'], check=True)
if sources(root) != input_files or headers(include) != input_headers or commit(root) != revision:
raise ValueError('VR inputs changed during compilation; retry the build')
files = read_outputs(snapshot / 'out', root)
manifest = {'version': 1, 'source_commit': revision, 'sources': hashes(input_files),
'headers': hashes(input_headers), 'compiler_image': image_id,
'outputs': hashes(files), 'native_tests': 'passed in tools/vr/build.sh'}
# Publish only after the complete build and native regression tests pass.
for name, data in files.items():
target = folder / name
if not target.resolve().is_relative_to(folder):
raise ValueError('VR output escapes the build directory: ' + name)
target.parent.mkdir(parents=True, exist_ok=True)
target.write_bytes(data)
(folder / MANIFEST).write_text(json.dumps(manifest, indent=2, sort_keys=True) + '\n', encoding='utf-8')
return validate(folder, include, root)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--vk-include', type=Path, required=True, help='NDK sysroot/usr/include directory')
parser.add_argument('--out', type=Path, default=ROOT / 'build/windows-setup/vr')
parser.add_argument('--image', default=IMAGE, help='Existing Docker compiler image; default is built if missing')
args = parser.parse_args()
try:
build(args.out, args.vk_include.resolve(), image=args.image)
except (OSError, ValueError, subprocess.CalledProcessError) as error:
parser.exit(1, str(error) + '\n')
print('VR build and native tests verified: ' + str(args.out))
if __name__ == '__main__':
main()
+42
View File
@@ -0,0 +1,42 @@
#include <cstdio>
#include <string>
#include <sys/stat.h>
#include "hand_mesh.h"
using namespace droiddeck;
static bool Write(const std::string &path, const void *data, size_t size) {
FILE *f = fopen(path.c_str(), "wb");
if (!f) return false;
const bool ok = fwrite(data, 1, size, f) == size;
return fclose(f) == 0 && ok;
}
static bool Write(const std::string &path, const std::string &text) { return Write(path, text.data(), text.size()); }
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "usage: make_hand_models <rendermodels dir>\n");
return 2;
}
const std::string root = argv[1];
mkdir(root.c_str(), 0755);
const std::vector<uint8_t> skin = GlovePng(64, {246, 246, 249}, {224, 227, 233}, glove::kDarkest);
for (int right = 0; right < 2; right++) {
const std::string name = right ? "droiddeck_hand_right" : "droiddeck_hand_left";
const std::string dir = root + "/" + name;
mkdir(dir.c_str(), 0755);
bool ok = Write(dir + "/" + name + ".json", HandModelJson(right)) && Write(dir + "/hand.mtl", HandMtl("hand", "hand.png")) &&
Write(dir + "/hand.png", skin.data(), skin.size()) &&
Write(dir + "/hand_open.obj", HandObj(BuildHandPart(right, HandPart::Hand, false), "hand")) &&
Write(dir + "/hand_fist.obj", HandObj(BuildHandPart(right, HandPart::Hand, true), "hand")) &&
Write(dir + "/thumb_index_open.obj", HandObj(BuildHandPart(right, HandPart::ThumbIndex, false), "hand")) &&
Write(dir + "/thumb_index_pinch.obj", HandObj(BuildHandPart(right, HandPart::ThumbIndex, true), "hand"));
if (!ok) {
fprintf(stderr, "make_hand_models: could not write %s\n", dir.c_str());
return 1;
}
}
return 0;
}
+120
View File
@@ -0,0 +1,120 @@
// Arm model ported from Google's GvrArmModel.cs (Google VR SDK for Unity, Apache-2.0), see docs/third-party/joycon.
#pragma once
#include <algorithm>
#include <cmath>
namespace droiddeck {
struct Vec3 {
double x = 0.0, y = 0.0, z = 0.0;
};
struct Quat {
double w = 1.0, x = 0.0, y = 0.0, z = 0.0;
};
inline Vec3 operator+(const Vec3 &a, const Vec3 &b) { return {a.x + b.x, a.y + b.y, a.z + b.z}; }
inline Vec3 operator-(const Vec3 &a, const Vec3 &b) { return {a.x - b.x, a.y - b.y, a.z - b.z}; }
inline Vec3 operator*(const Vec3 &a, double s) { return {a.x * s, a.y * s, a.z * s}; }
inline double Dot(const Vec3 &a, const Vec3 &b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
inline Vec3 Cross(const Vec3 &a, const Vec3 &b) { return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x}; }
inline double Length(const Vec3 &a) { return std::sqrt(Dot(a, a)); }
inline Quat operator*(const Quat &a, const Quat &b) {
return {a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z,
a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x,
a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w};
}
inline Quat Conjugate(const Quat &q) { return {q.w, -q.x, -q.y, -q.z}; }
inline Quat Normalize(const Quat &q) {
const double n = std::sqrt(q.w * q.w + q.x * q.x + q.y * q.y + q.z * q.z);
if (!(n > 1e-9)) return {};
return {q.w / n, q.x / n, q.y / n, q.z / n};
}
inline Vec3 Rotate(const Quat &q, const Vec3 &v) {
const Vec3 u{q.x, q.y, q.z};
const Vec3 t = Cross(u, v) * 2.0;
return v + t * q.w + Cross(u, t);
}
inline Quat AboutX(double radians) { return {std::cos(radians / 2), std::sin(radians / 2), 0.0, 0.0}; }
inline Quat AboutY(double radians) { return {std::cos(radians / 2), 0.0, std::sin(radians / 2), 0.0}; }
inline Quat FromTo(const Vec3 &from, const Vec3 &to) {
const double d = Dot(from, to);
if (d < -0.999999) {
Vec3 axis = Cross({1.0, 0.0, 0.0}, from);
if (Length(axis) < 1e-6) axis = Cross({0.0, 1.0, 0.0}, from);
axis = axis * (1.0 / Length(axis));
return {0.0, axis.x, axis.y, axis.z};
}
const Vec3 c = Cross(from, to);
return Normalize({1.0 + d, c.x, c.y, c.z});
}
inline Quat Nlerp(const Quat &a, const Quat &b, double t) {
const double sign = a.w * b.w + a.x * b.x + a.y * b.y + a.z * b.z < 0.0 ? -1.0 : 1.0;
return Normalize({a.w + (sign * b.w - a.w) * t, a.x + (sign * b.x - a.x) * t,
a.y + (sign * b.y - a.y) * t, a.z + (sign * b.z - a.z) * t});
}
inline double AngleDegrees(const Quat &q) { return 2.0 * std::acos(std::min(std::fabs(q.w), 1.0)) * 180.0 / M_PI; }
inline bool Heading(const Quat &q, double *radians) {
const Vec3 forward = Rotate(q, {0.0, 0.0, -1.0});
if (std::hypot(forward.x, forward.z) < 0.25) return false;
*radians = std::atan2(-forward.x, -forward.z);
return true;
}
class ArmModel {
public:
explicit ArmModel(bool right) : side_(right ? 1.0 : -1.0) {}
void Reset() { torsoSet_ = false; }
Vec3 Update(const Vec3 &head, const Quat &headRotation, const Quat &controller, double angularSpeed, double dt) {
Vec3 gaze = Rotate(headRotation, {0.0, 0.0, -1.0});
gaze.y = 0.0;
const double length = Length(gaze);
if (length > 1e-3) {
gaze = gaze * (1.0 / length);
if (!torsoSet_) {
torso_ = gaze;
torsoSet_ = true;
} else {
const double strength = std::clamp((angularSpeed - 0.2) / 45.0, 0.0, 0.1);
const double follow = 1.0 - std::pow(1.0 - strength, std::max(dt, 0.0) * 60.0);
const double angle = std::atan2(Cross(torso_, gaze).y, Dot(torso_, gaze));
torso_ = Rotate(AboutY(angle * follow), torso_);
}
}
const Quat torso = AboutY(std::atan2(-torso_.x, -torso_.z));
const Vec3 forward = Rotate(Conjugate(torso) * controller, {0.0, 0.0, -1.0});
const double elevation = std::asin(std::clamp(forward.y, -1.0, 1.0)) * 180.0 / M_PI;
const Quat swing = FromTo({0.0, 0.0, -1.0}, forward);
const double extension = std::clamp((elevation - kMinExtension) / (kMaxExtension - kMinExtension), 0.0, 1.0);
const Vec3 elbow = Vec3{0.195 * side_, -0.5, -0.005} + Vec3{-0.13 * side_, 0.14, -0.08} * extension;
const double suppression = 1.0 - std::pow(AngleDegrees(swing) / 180.0, 6.0);
const double share = ((1.0 - kElbowBend) + kElbowBend * extension * kExtensionWeight) * suppression;
const Quat lerp = Nlerp({}, swing, share);
const Quat elbowRotation = torso * Conjugate(lerp) * swing;
const Quat wristRotation = elbowRotation * lerp;
const Vec3 elbowAt = head + Rotate(torso, elbow);
const Vec3 wristAt = elbowAt + Rotate(elbowRotation, {0.0, 0.0, -0.25});
return wristAt + Rotate(wristRotation, {0.0, 0.0, -0.05});
}
private:
static constexpr double kMinExtension = 7.0, kMaxExtension = 60.0, kElbowBend = 0.6, kExtensionWeight = 0.4;
double side_;
bool torsoSet_ = false;
Vec3 torso_{0.0, 0.0, -1.0};
};
}
File diff suppressed because it is too large. Load diff
+104
View File
@@ -0,0 +1,104 @@
#pragma once
#include <stdint.h>
#include <stdlib.h>
#include <vulkan/vulkan.h>
/* All pool operations require the caller's tracker lock. Reserved fences are
* invisible to the completer until vkQueueSubmit has returned successfully. */
struct tracked {
struct tracked *next;
VkDevice dev;
VkFence fence;
PFN_vkWaitForFences wait;
PFN_vkResetFences reset;
PFN_vkDestroyFence destroy;
uint32_t mark;
enum { TRACK_IDLE, TRACK_RESERVED, TRACK_SUBMITTED } state;
};
static struct tracked *track_pool_take(struct tracked **pool, VkDevice dev, PFN_vkGetDeviceProcAddr gdpa) {
struct tracked *idle = NULL;
for (struct tracked *t = *pool; t; t = t->next) {
if (t->state != TRACK_IDLE) continue;
if (t->dev == dev) {
t->state = TRACK_RESERVED;
return t;
}
if (!idle) idle = t;
}
struct tracked t = {.dev = dev, .state = TRACK_RESERVED};
t.wait = (PFN_vkWaitForFences)gdpa(dev, "vkWaitForFences");
t.reset = (PFN_vkResetFences)gdpa(dev, "vkResetFences");
t.destroy = (PFN_vkDestroyFence)gdpa(dev, "vkDestroyFence");
PFN_vkCreateFence create = (PFN_vkCreateFence)gdpa(dev, "vkCreateFence");
if (!t.wait || !t.reset || !t.destroy || !create) return NULL;
/* Never wait for a slot: the next submission may signal a semaphore that
* every outstanding fence is waiting on. Grow only at the high-water mark. */
struct tracked *slot = idle ? idle : calloc(1, sizeof(*slot));
if (!slot) return NULL;
VkFenceCreateInfo fci = {VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, NULL, 0};
if (create(dev, &fci, NULL, &t.fence) != VK_SUCCESS) {
if (!idle) free(slot);
return NULL;
}
if (idle) {
idle->destroy(idle->dev, idle->fence, NULL);
t.next = idle->next;
} else {
t.next = *pool;
*pool = slot;
}
*slot = t;
return slot;
}
static uint32_t track_pool_submitted(struct tracked *t, VkResult result, uint32_t issued) {
if (result == VK_SUCCESS) {
t->mark = ++issued;
t->state = TRACK_SUBMITTED;
} else {
t->reset(t->dev, 1, &t->fence);
t->state = TRACK_IDLE;
}
return issued;
}
static int track_pool_reap(struct tracked *pool, uint32_t issued, uint32_t *done) {
int pending = 0;
uint32_t through = issued;
for (struct tracked *t = pool; t; t = t->next) {
if (t->state != TRACK_SUBMITTED) continue;
/* Scan all fences without blocking behind an earlier submission on a
* different queue. Device loss retires fences as in the previous path. */
if (t->wait(t->dev, 1, &t->fence, VK_TRUE, 0) == VK_TIMEOUT) {
pending = 1;
if ((int32_t)(t->mark - through) <= 0) through = t->mark - 1;
} else {
t->reset(t->dev, 1, &t->fence);
t->state = TRACK_IDLE;
}
}
/* done is a contiguous watermark, not a count of completed fences. Later
* work may free its slot but cannot release an unfinished presenter frame.
* Signed differences also handle the uint32_t counter wrapping. */
*done = through;
return pending;
}
static int track_pool_device_busy(struct tracked *pool, VkDevice dev) {
for (struct tracked *t = pool; t; t = t->next)
if (t->dev == dev && t->state != TRACK_IDLE) return 1;
return 0;
}
/* The caller must first wait for all this device's reservations and work. */
static void track_pool_forget(struct tracked **pool, VkDevice dev) {
for (struct tracked **p = pool; *p;) {
struct tracked *t = *p;
if (t->dev != dev) { p = &t->next; continue; }
t->destroy(t->dev, t->fence, NULL);
*p = t->next;
free(t);
}
}
+463
View File
@@ -0,0 +1,463 @@
#pragma once
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <string>
#include <vector>
#include "hand_pose.h"
namespace droiddeck {
struct HandMesh {
std::vector<Vec3> points, normals;
std::vector<double> tints, shades;
std::vector<std::array<int, 3>> faces;
int Add(const Vec3 &p, const Vec3 &n, double tint = 0.0, double shade = 1.0) {
points.push_back(p);
normals.push_back(n);
tints.push_back(tint);
shades.push_back(shade);
return (int)points.size() - 1;
}
void Face(int a, int b, int c, const Vec3 &outward) {
const Vec3 n = Cross(points[b] - points[a], points[c] - points[a]);
if (Dot(n, outward) < 0.0) faces.push_back({a, c, b});
else faces.push_back({a, b, c});
}
};
inline FingerCurls RelaxedCurls() {
FingerCurls c;
c.thumb = 0.15;
c.index = 0.1;
c.middle = c.ring = c.pinky = 0.15;
return c;
}
inline void HandShape(bool right, bool pinch, bool fist, Vec3 out[kHandJoints]) {
using namespace hand;
FingerCurls c = RelaxedCurls();
if (pinch) {
c.thumb = 0.7;
c.index = 0.55;
}
if (fist) c.middle = c.ring = c.pinky = 1.0;
HandJoints(right, c, out);
if (pinch) {
const Vec3 meet = out[kIndexTip] + (out[kThumbIp] - out[kIndexTip]) * 0.15;
const Vec3 shift = meet - out[kThumbTip];
out[kThumbTip] = meet;
out[kThumbIp] = out[kThumbIp] + shift * 0.5;
out[kThumbMcp] = out[kThumbMcp] + shift * 0.15;
}
}
enum class HandPart { Hand, ThumbIndex };
namespace glove {
constexpr double kStep = 0.0026;
constexpr double kPalmHalf = 0.0105;
constexpr double kPalmRound = 0.0085;
constexpr double kWristSide = 0.02;
constexpr double kWristSpan = 0.017;
constexpr double kWristRadius = 0.0165;
constexpr double kCuffStart = 0.006;
constexpr double kCuffLength = 0.016;
constexpr double kCuffWidth[2] = {0.0295, 0.029};
constexpr double kCuffDepth[2] = {0.021, 0.0205};
constexpr double kCuffRound = 0.006;
constexpr double kJointBlend = 0.001;
constexpr double kFingerBlend = 0.010;
constexpr double kThumbBlend = 0.016;
constexpr double kWristBlend = 0.012;
constexpr double kCuffBlend = 0.005;
constexpr double kTintEdge = 0.0015;
constexpr double kTouchBlend = 0.008;
constexpr double kCutBlend = 0.004;
constexpr double kDome[3] = {0.030, 0.040, 0.0135};
constexpr double kDomeAt = 0.55;
constexpr double kDomeBlend = 0.022;
constexpr double kFistRadius = 0.0095;
constexpr double kFistBlend = 0.006;
constexpr double kOcclusionStep = 0.004;
constexpr double kOcclusion = 3.0;
constexpr double kDarkest = 0.4;
constexpr double kInset = 0.0003;
constexpr double kReach = 0.03;
constexpr double kRadii[5][4] = {{0.0135, 0.0122, 0.0110, 0.0096},
{0.0106, 0.0098, 0.0092, 0.0087},
{0.0110, 0.0102, 0.0096, 0.0090},
{0.0105, 0.0097, 0.0091, 0.0086},
{0.0094, 0.0087, 0.0082, 0.0077}};
inline Vec3 Unit(const Vec3 &v) {
const double l = Length(v);
return l > 1e-12 ? v * (1.0 / l) : Vec3{0.0, 1.0, 0.0};
}
inline double Smin(double a, double b, double k) {
const double h = std::max(k - std::fabs(a - b), 0.0) / k;
return std::min(a, b) - h * h * k * 0.25;
}
inline double Cone(const Vec3 &p, const Vec3 &a, const Vec3 &b, double ra, double rb) {
const Vec3 ab = b - a;
const double t = std::min(1.0, std::max(0.0, Dot(p - a, ab) / std::max(Dot(ab, ab), 1e-12)));
return Length(p - (a + ab * t)) - (ra + (rb - ra) * t);
}
inline double Polygon(const std::vector<std::array<double, 2>> &v, double x, double y) {
double d = (x - v[0][0]) * (x - v[0][0]) + (y - v[0][1]) * (y - v[0][1]);
double sign = 1.0;
for (size_t i = 0, j = v.size() - 1; i < v.size(); j = i++) {
const double ex = v[j][0] - v[i][0], ey = v[j][1] - v[i][1];
const double wx = x - v[i][0], wy = y - v[i][1];
const double t = std::min(1.0, std::max(0.0, (wx * ex + wy * ey) / std::max(ex * ex + ey * ey, 1e-18)));
const double bx = wx - ex * t, by = wy - ey * t;
d = std::min(d, bx * bx + by * by);
const bool a = y >= v[i][1], b = y < v[j][1], c = ex * wy > ey * wx;
if ((a && b && c) || (!a && !b && !c)) sign = -sign;
}
return sign * std::sqrt(d);
}
inline double Rounded(double a, double b, double round) {
return std::min(std::max(a, b), 0.0) + std::hypot(std::max(a, 0.0), std::max(b, 0.0)) - round;
}
struct Finger {
Vec3 joints[4];
double radii[4];
double Distance(const Vec3 &p) const {
double d = Cone(p, joints[0], joints[1], radii[0], radii[1]);
for (int i = 1; i < 3; i++) d = Smin(d, Cone(p, joints[i], joints[i + 1], radii[i], radii[i + 1]), kJointBlend);
return d;
}
};
class Glove {
public:
Glove(bool right, HandPart part, bool closed) : part_(part) {
using namespace hand;
Vec3 open[kHandJoints], j[kHandJoints];
HandShape(right, false, false, open);
HandShape(right, part == HandPart::ThumbIndex && closed, part == HandPart::Hand && closed, j);
origin_ = open[kWrist];
along_ = Unit(open[kMiddleMcp] - open[kWrist]);
normal_ = Unit(Cross(open[kIndexMcp] - open[kWrist], open[kPinkyMcp] - open[kWrist]));
across_ = Unit(Cross(along_, normal_));
const Vec3 side = across_ * (Dot(open[kIndexMcp] - open[kPinkyMcp], across_) > 0.0 ? 1.0 : -1.0);
for (const Vec3 &q : {open[kWrist] + side * kWristSide, open[kThumbCmc], open[kIndexMcp], open[kMiddleMcp], open[kRingMcp],
open[kPinkyMcp], open[kWrist] - side * kWristSide})
outline_.push_back({Dot(q - origin_, across_), Dot(q - origin_, along_)});
wristA_ = open[kWrist] + across_ * kWristSpan;
wristB_ = open[kWrist] - across_ * kWristSpan;
const int first = part == HandPart::Hand ? 2 : 0, last = part == HandPart::Hand ? 4 : 1;
for (int f = first; f <= last; f++) {
Finger finger;
const int root = f == 0 ? kThumbCmc : kIndexMcp + 4 * (f - 1);
for (int i = 0; i < 4; i++) {
finger.joints[i] = j[root + i];
finger.radii[i] = kRadii[f][i];
}
finger.joints[3] = finger.joints[3] - Unit(finger.joints[3] - finger.joints[2]) * (finger.radii[3] * 0.7);
fingers_.push_back(finger);
}
dome_ = open[kWrist] + (open[kMiddleMcp] - open[kWrist]) * kDomeAt;
back_ = normal_ * (Dot(normal_, open[kThumbTip] - dome_) > 0.0 ? -1.0 : 1.0);
fist_ = part == HandPart::Hand && closed;
for (int f = 0; f < 3 && fist_; f++) {
fistLine_[f] = Vec3{};
for (int i = 0; i < 4; i++) fistLine_[f] = fistLine_[f] + j[kMiddleMcp + 4 * f + i] * 0.25;
}
lo_ = hi_ = origin_;
const auto grow = [&](const Vec3 &q, double r) {
lo_ = Vec3{std::min(lo_.x, q.x - r), std::min(lo_.y, q.y - r), std::min(lo_.z, q.z - r)};
hi_ = Vec3{std::max(hi_.x, q.x + r), std::max(hi_.y, q.y + r), std::max(hi_.z, q.z + r)};
};
for (int i : {kWrist, kThumbCmc, kIndexMcp, kMiddleMcp, kRingMcp, kPinkyMcp}) grow(open[i], kPalmHalf + kPalmRound + kWristSide);
grow(origin_ - along_ * kCuffLength, kCuffWidth[1] + kCuffRound);
for (const Finger &f : fingers_)
for (int i = 0; i < 4; i++) grow(f.joints[i], f.radii[i] + kThumbBlend);
}
double Distance(const Vec3 &p) const {
double palm = Smin(Smin(Slab(p), Cone(p, wristA_, wristB_, kWristRadius, kWristRadius), kWristBlend), Cuff(p), kCuffBlend);
if (part_ == HandPart::ThumbIndex) {
double reach = 1e9;
for (const Finger &f : fingers_) reach = std::min(reach, f.Distance(p));
palm = -Smin(-(palm + kInset), kReach - reach, kCutBlend);
}
double fingers = fingers_[0].Distance(p);
for (size_t i = 1; i < fingers_.size(); i++) fingers = Smin(fingers, fingers_[i].Distance(p), kTouchBlend);
if (fist_)
fingers = Smin(fingers, std::min(Cone(p, fistLine_[0], fistLine_[1], kFistRadius, kFistRadius),
Cone(p, fistLine_[1], fistLine_[2], kFistRadius, kFistRadius)), kFistBlend);
return Smin(palm, fingers, part_ == HandPart::ThumbIndex ? kThumbBlend : kFingerBlend);
}
double Tint(const Vec3 &p) const {
double rest = Smin(Slab(p), Cone(p, wristA_, wristB_, kWristRadius, kWristRadius), kWristBlend);
for (const Finger &f : fingers_) rest = std::min(rest, f.Distance(p));
return std::min(1.0, std::max(0.0, 0.5 + (rest - Cuff(p)) / (2.0 * kTintEdge)));
}
double Shade(const Vec3 &p, const Vec3 &n) const {
double hidden = 0.0, weight = 1.0;
for (int i = 1; i <= 5; i++) {
const double h = kOcclusionStep * i;
hidden += (h - Distance(p + n * h)) * weight;
weight *= 0.6;
}
const double open = std::min(1.0, std::max(0.0, 1.0 - kOcclusion * hidden / kOcclusionStep / 5.0));
const double light = 0.88 + 0.12 * Dot(n, back_);
return std::min(1.0, std::max(0.0, open * light));
}
Vec3 Low() const { return lo_ - Vec3{2.0, 2.0, 2.0} * kStep; }
Vec3 High() const { return hi_ + Vec3{2.0, 2.0, 2.0} * kStep; }
private:
double Slab(const Vec3 &p) const {
const Vec3 q = p - origin_;
const double slab = Rounded(Polygon(outline_, Dot(q, across_), Dot(q, along_)), std::fabs(Dot(q, normal_)) - (kPalmHalf - kPalmRound), kPalmRound);
const Vec3 d = p - dome_;
const double x = Dot(d, across_), y = Dot(d, along_), z = Dot(d, normal_);
const double k0 = std::sqrt(x * x / (kDome[0] * kDome[0]) + y * y / (kDome[1] * kDome[1]) + z * z / (kDome[2] * kDome[2]));
const double k1 = std::sqrt(x * x / std::pow(kDome[0], 4) + y * y / std::pow(kDome[1], 4) + z * z / std::pow(kDome[2], 4));
return Smin(slab, k1 > 1e-12 ? k0 * (k0 - 1.0) / k1 : -kDome[2], kDomeBlend);
}
double Cuff(const Vec3 &p) const {
const Vec3 q = p - origin_;
const double t = -Dot(q, along_) + kCuffStart;
const double s = std::min(1.0, std::max(0.0, t / kCuffLength));
const double a = kCuffWidth[0] + (kCuffWidth[1] - kCuffWidth[0]) * s - kCuffRound;
const double b = kCuffDepth[0] + (kCuffDepth[1] - kCuffDepth[0]) * s - kCuffRound;
const double x = Dot(q, across_), y = Dot(q, normal_);
const double k0 = std::hypot(x / a, y / b), k1 = std::hypot(x / (a * a), y / (b * b));
const double radial = k1 > 1e-12 ? k0 * (k0 - 1.0) / k1 : -std::min(a, b);
return Rounded(radial, std::fabs(t - kCuffLength * 0.5) - (kCuffLength * 0.5 - kCuffRound), kCuffRound);
}
HandPart part_;
bool fist_ = false;
Vec3 origin_, along_, across_, normal_, wristA_, wristB_, lo_, hi_, dome_, back_, fistLine_[3];
std::vector<std::array<double, 2>> outline_;
std::vector<Finger> fingers_;
};
template <typename Field> Vec3 Gradient(const Field &f, const Vec3 &p) {
constexpr double e = 1e-5;
return Vec3{f(p + Vec3{e, 0.0, 0.0}) - f(p - Vec3{e, 0.0, 0.0}), f(p + Vec3{0.0, e, 0.0}) - f(p - Vec3{0.0, e, 0.0}),
f(p + Vec3{0.0, 0.0, e}) - f(p - Vec3{0.0, 0.0, e})} *
(0.5 / e);
}
template <typename Field, typename Painter>
HandMesh SurfaceNet(const Field &f, const Painter &paint, const Vec3 &lo, const Vec3 &hi, double step) {
const int nx = (int)std::ceil((hi.x - lo.x) / step) + 1, ny = (int)std::ceil((hi.y - lo.y) / step) + 1,
nz = (int)std::ceil((hi.z - lo.z) / step) + 1;
const auto at = [&](int i, int j, int k) { return (size_t(k) * ny + j) * nx + i; };
const auto corner = [&](int i, int j, int k) { return Vec3{lo.x + i * step, lo.y + j * step, lo.z + k * step}; };
std::vector<double> value(size_t(nx) * ny * nz);
for (int k = 0; k < nz; k++)
for (int j = 0; j < ny; j++)
for (int i = 0; i < nx; i++) value[at(i, j, k)] = f(corner(i, j, k));
std::vector<int> vertex(value.size(), -1);
HandMesh m;
for (int k = 0; k + 1 < nz; k++)
for (int j = 0; j + 1 < ny; j++)
for (int i = 0; i + 1 < nx; i++) {
double c[8];
int inside = 0;
for (int n = 0; n < 8; n++) inside += (c[n] = value[at(i + (n & 1), j + ((n >> 1) & 1), k + (n >> 2))]) < 0.0;
if (inside == 0 || inside == 8) continue;
Vec3 sum;
int count = 0;
for (int a = 0; a < 8; a++)
for (int bit = 1; bit < 8; bit <<= 1) {
const int b = a | bit;
if (b == a || (c[a] < 0.0) == (c[b] < 0.0)) continue;
const Vec3 pa = corner(i + (a & 1), j + ((a >> 1) & 1), k + (a >> 2));
const Vec3 pb = corner(i + (b & 1), j + ((b >> 1) & 1), k + (b >> 2));
sum = sum + pa + (pb - pa) * (c[a] / (c[a] - c[b]));
count++;
}
Vec3 p = sum * (1.0 / count);
for (int it = 0; it < 3; it++) {
const Vec3 g = Gradient(f, p);
const double gg = Dot(g, g);
if (gg < 1e-12) break;
p = p - g * (f(p) / gg);
}
const Vec3 low = corner(i, j, k), high = corner(i + 1, j + 1, k + 1);
p = Vec3{std::clamp(p.x, low.x, high.x), std::clamp(p.y, low.y, high.y), std::clamp(p.z, low.z, high.z)};
const Vec3 n = Unit(Gradient(f, p));
const std::array<double, 2> colour = paint(p, n);
vertex[at(i, j, k)] = m.Add(p, n, colour[0], colour[1]);
}
for (int k = 1; k + 1 < nz; k++)
for (int j = 1; j + 1 < ny; j++)
for (int i = 1; i + 1 < nx; i++) {
const bool in = value[at(i, j, k)] < 0.0;
for (int axis = 0; axis < 3; axis++) {
const int di = axis == 0, dj = axis == 1, dk = axis == 2;
if ((value[at(i + di, j + dj, k + dk)] < 0.0) == in) continue;
int q[4];
if (axis == 0) q[0] = vertex[at(i, j - 1, k - 1)], q[1] = vertex[at(i, j, k - 1)], q[2] = vertex[at(i, j, k)], q[3] = vertex[at(i, j - 1, k)];
else if (axis == 1) q[0] = vertex[at(i - 1, j, k - 1)], q[1] = vertex[at(i, j, k - 1)], q[2] = vertex[at(i, j, k)], q[3] = vertex[at(i - 1, j, k)];
else q[0] = vertex[at(i - 1, j - 1, k)], q[1] = vertex[at(i, j - 1, k)], q[2] = vertex[at(i, j, k)], q[3] = vertex[at(i - 1, j, k)];
if (q[0] < 0 || q[1] < 0 || q[2] < 0 || q[3] < 0) continue;
if (in == (axis == 1)) std::swap(q[1], q[3]);
if (Length(m.points[q[0]] - m.points[q[2]]) < Length(m.points[q[1]] - m.points[q[3]])) {
m.faces.push_back({q[0], q[1], q[2]});
m.faces.push_back({q[0], q[2], q[3]});
} else {
m.faces.push_back({q[0], q[1], q[3]});
m.faces.push_back({q[1], q[2], q[3]});
}
}
}
return m;
}
}
inline HandMesh BuildHandPart(bool right, HandPart part, bool closed) {
const glove::Glove g(right, part, closed);
return glove::SurfaceNet([&](const Vec3 &p) { return g.Distance(p); }, [&](const Vec3 &p, const Vec3 &n) { return std::array<double, 2>{g.Tint(p), g.Shade(p, n)}; }, g.Low(), g.High(),
glove::kStep);
}
inline std::string HandObj(const HandMesh &m, const char *material) {
std::string out = "mtllib hand.mtl\n";
char line[160];
for (const Vec3 &p : m.points) {
snprintf(line, sizeof(line), "v %.5f %.5f %.5f\n", p.x, p.y, p.z);
out += line;
}
for (size_t i = 0; i < m.tints.size(); i++) {
snprintf(line, sizeof(line), "vt %.3f %.3f\n", 0.02 + 0.96 * m.shades[i], 0.02 + 0.46 * m.tints[i]);
out += line;
}
for (const Vec3 &n : m.normals) {
snprintf(line, sizeof(line), "vn %.4f %.4f %.4f\n", n.x, n.y, n.z);
out += line;
}
out += "usemtl ";
out += material;
out += "\n";
for (const auto &f : m.faces) {
snprintf(line, sizeof(line), "f %d/%d/%d %d/%d/%d %d/%d/%d\n", f[0] + 1, f[0] + 1, f[0] + 1, f[1] + 1, f[1] + 1, f[1] + 1, f[2] + 1,
f[2] + 1, f[2] + 1);
out += line;
}
return out;
}
inline std::string HandMtl(const char *material, const char *texture) {
std::string out = "newmtl ";
out += material;
out += "\nNs 12.0\nd 1.0\nillum 2\nKa 1.0 1.0 1.0\nKd 1.0 1.0 1.0\nKs 0.1 0.1 0.1\nmap_Kd ";
out += texture;
out += "\n";
return out;
}
inline std::string HandModelJson(bool right) {
const auto part = [&](const char *name, const char *path, int button, bool shown) {
char text[512];
snprintf(text, sizeof(text),
" \"%s\": {\n"
" \"filename\": \"%s.obj\",\n"
" \"motion\": {\n"
" \"type\": \"translate\",\n"
" \"pressed_path\": \"%s\",\n"
" \"controller_button\": %d,\n"
" \"value_mapping\": [0.0, 0.0],\n"
" \"axis\": [0.0, 0.0, 1.0]\n"
" },\n"
" \"visibility\": { \"default\": %s, \"press\": %s }\n"
" },\n",
name, name, path, button, shown ? "false" : "true", shown ? "true" : "false");
return std::string(text);
};
const auto pose = [](const char *name, const char *origin, const char *rotate) {
char text[256];
snprintf(text, sizeof(text), " \"%s\": { \"component_local\": { \"origin\": [%s], \"rotate_xyz\": [%s] } }", name, origin, rotate);
return std::string(text);
};
std::string out = "{\n \"components\": {\n";
out += part("hand_open", "/input/grip/touch", 2, false);
out += part("hand_fist", "/input/grip/touch", 2, true);
out += part("thumb_index_open", "/input/trigger/click", 33, false);
out += part("thumb_index_pinch", "/input/trigger/click", 33, true);
out += pose("tip", right ? "-0.006, -0.015, 0.02" : "0.006, -0.015, 0.02", right ? "-40.0, 5.0, 0.0" : "-40.0, -5.0, 0.0") + ",\n";
out += pose("base", right ? "-0.004758, -0.037977, 0.200466" : "0.004758, -0.037977, 0.200466",
right ? "-155.4, -0.427, -7.081" : "-155.4, -0.427, 7.081") + ",\n";
out += pose("handgrip", right ? "0.003851, 0.003715, 0.075948" : "-0.003851, 0.003715, 0.075948",
right ? "15.392, 2.071, -0.303" : "15.392, -2.071, 0.303") + ",\n";
out += pose("grip", "0.0, -0.015, 0.13", right ? "15.392, 2.071, -0.303" : "15.392, -2.071, 0.303") + ",\n";
out += pose("gdc2015", "0.0, 0.0, 0.0", "0, 0, 0") + "\n";
out += " }\n}\n";
return out;
}
inline uint32_t Crc32(const uint8_t *data, size_t size, uint32_t crc = 0) {
crc = ~crc;
for (size_t i = 0; i < size; i++) {
crc ^= data[i];
for (int k = 0; k < 8; k++) crc = (crc >> 1) ^ (0xEDB88320u & (0u - (crc & 1u)));
}
return ~crc;
}
inline std::vector<uint8_t> GlovePng(int size, const std::array<uint8_t, 3> &glove, const std::array<uint8_t, 3> &cuff, double darkest) {
std::vector<uint8_t> raw;
for (int y = 0; y < size; y++) {
raw.push_back(0);
const double t = 1.0 - std::fabs(2.0 * (y + 0.5) / size - 1.0);
for (int x = 0; x < size; x++) {
const double light = darkest + (1.0 - darkest) * x / double(size - 1);
for (int c = 0; c < 3; c++) raw.push_back(uint8_t(std::lround((glove[c] + (cuff[c] - glove[c]) * t) * light)));
}
}
std::vector<uint8_t> zlib = {0x78, 0x01, 0x01, uint8_t(raw.size() & 0xff), uint8_t(raw.size() >> 8),
uint8_t(~raw.size() & 0xff), uint8_t((~raw.size() >> 8) & 0xff)};
zlib.insert(zlib.end(), raw.begin(), raw.end());
uint32_t a = 1, s = 0;
for (uint8_t v : raw) {
a = (a + v) % 65521;
s = (s + a) % 65521;
}
const uint32_t adler = (s << 16) | a;
for (int i = 3; i >= 0; i--) zlib.push_back(uint8_t(adler >> (8 * i)));
std::vector<uint8_t> png = {0x89, 'P', 'N', 'G', 0x0d, 0x0a, 0x1a, 0x0a};
const auto chunk = [&](const char *type, const std::vector<uint8_t> &data) {
for (int i = 3; i >= 0; i--) png.push_back(uint8_t(data.size() >> (8 * i)));
std::vector<uint8_t> body(type, type + 4);
body.insert(body.end(), data.begin(), data.end());
png.insert(png.end(), body.begin(), body.end());
const uint32_t crc = Crc32(body.data(), body.size());
for (int i = 3; i >= 0; i--) png.push_back(uint8_t(crc >> (8 * i)));
};
std::vector<uint8_t> header;
for (int v : {size, size})
for (int i = 3; i >= 0; i--) header.push_back(uint8_t(v >> (8 * i)));
header.insert(header.end(), {8, 2, 0, 0, 0});
chunk("IHDR", header);
chunk("IDAT", zlib);
chunk("IEND", {});
return png;
}
}
+322
View File
@@ -0,0 +1,322 @@
#pragma once
#include <algorithm>
#include <cmath>
#include <cstdint>
#include "arm_model.h"
namespace droiddeck {
class HandFollower {
public:
void Reset() { placed_ = false; }
void Place(const Vec3 &at, const Vec3 &moved) {
at_ = at;
lastMoved_ = moved;
placed_ = true;
}
Vec3 Update(const Vec3 &anchor, const Vec3 &moved, double dt, double anchorSeconds, double maxOffset) {
if (!placed_) {
at_ = anchor;
placed_ = true;
} else {
at_ = at_ + (moved - lastMoved_);
const double pull = 1.0 - std::exp(-std::max(dt, 0.0) / anchorSeconds);
at_ = at_ + (anchor - at_) * pull;
const Vec3 off = at_ - anchor;
const double length = Length(off);
if (length > maxOffset) at_ = anchor + off * (maxOffset / length);
}
lastMoved_ = moved;
return at_;
}
private:
bool placed_ = false;
Vec3 at_, lastMoved_;
};
class TimeHistory {
public:
void Clear() { count_ = next_ = 0; }
void Add(int64_t timeNs, const Vec3 &value) {
if (count_ && timeNs <= time_[Last()]) {
if (timeNs == time_[Last()]) value_[Last()] = value;
return;
}
time_[next_] = timeNs;
value_[next_] = value;
next_ = (next_ + 1) % kSize;
count_ = std::min(count_ + 1, kSize);
}
bool At(int64_t timeNs, Vec3 *out) const {
if (!count_) return false;
int newer = Last();
if (timeNs >= time_[newer]) {
*out = value_[newer];
return true;
}
for (int i = 1; i < count_; i++) {
const int older = (newer - 1 + kSize) % kSize;
if (time_[older] <= timeNs) {
const double t = double(timeNs - time_[older]) / double(time_[newer] - time_[older]);
*out = value_[older] + (value_[newer] - value_[older]) * t;
return true;
}
newer = older;
}
*out = value_[newer];
return true;
}
private:
int Last() const { return (next_ - 1 + kSize) % kSize; }
static constexpr int kSize = 256;
int64_t time_[kSize] = {};
Vec3 value_[kSize];
int count_ = 0, next_ = 0;
};
class CameraYaw {
public:
void Reset() {
c_ = s_ = weight_ = 0.0;
outliers_ = 0;
}
void Add(double yaw, double dt) {
double current = 0.0;
if (Get(&current) && std::fabs(std::remainder(yaw - current, 2.0 * M_PI)) > kJump) {
if (++outliers_ < kOutlierFrames) return;
Reset();
}
outliers_ = 0;
const double keep = std::exp(-std::max(dt, 0.0) / kSeconds);
c_ = c_ * keep + std::cos(yaw);
s_ = s_ * keep + std::sin(yaw);
weight_ = weight_ * keep + 1.0;
}
bool Get(double *yaw) const {
if (weight_ < kMinWeight || std::hypot(c_, s_) < kMinAgreement * weight_) return false;
*yaw = std::atan2(s_, c_);
return true;
}
private:
static constexpr double kSeconds = 3.0, kMinWeight = 4.0, kMinAgreement = 0.7, kJump = 0.6;
static constexpr int kOutlierFrames = 8;
double c_ = 0.0, s_ = 0.0, weight_ = 0.0;
int outliers_ = 0;
};
class HandKalman {
public:
void Reset() {
ready_ = false;
haveInput_ = false;
count_ = 0;
outliers_ = 0;
seenNs_ = 0;
stillSeconds_ = 0.0;
}
void Forget() {
ready_ = false;
outliers_ = 0;
}
bool Ready() const { return ready_; }
int64_t SeenNs() const { return seenNs_; }
int64_t TimeNs() const { return lastNs_; }
Vec3 Position() const { return {axes_[0].x[0], axes_[1].x[0], axes_[2].x[0]}; }
Vec3 Velocity() const { return {axes_[0].x[1], axes_[1].x[1], axes_[2].x[1]}; }
bool Still() const { return stillSeconds_ > kStillSeconds; }
void Input(int64_t timeNs, const Vec3 &inertial, double turnRate) {
if (haveInput_ && timeNs <= lastNs_) return;
if (!haveInput_ || timeNs - lastNs_ > kGapNs) {
Reset();
haveInput_ = true;
lastNs_ = timeNs;
lastInertial_ = inertial;
return;
}
Step step;
step.timeNs = timeNs;
step.dt = (timeNs - lastNs_) * 1e-9;
step.du = inertial - lastInertial_;
lastNs_ = timeNs;
lastInertial_ = inertial;
const bool calm = turnRate < kStillTurn && Length(step.du) < kStillAccel * step.dt;
stillSeconds_ = calm ? stillSeconds_ + step.dt : 0.0;
if (ready_) {
Propagate(axes_, step.du, step.dt, Fade(timeNs - seenNs_));
if (Still()) Stop(axes_);
for (int a = 0; a < 3; a++) step.after[a] = axes_[a];
}
steps_[next_] = step;
next_ = (next_ + 1) % kSteps;
count_ = std::min(count_ + 1, kSteps);
}
bool Measure(int64_t timeNs, const Vec3 &at, double sigma) {
if (!haveInput_ || timeNs > lastNs_ + kFutureNs || (count_ == 0 && timeNs < lastNs_)) return false;
int k = -1;
for (int i = 0; i < count_; i++) {
const Step &s = steps_[Index(i)];
if (s.timeNs >= timeNs) {
k = i;
break;
}
}
const double z[3] = {at.x, at.y, at.z};
if (k < 0) {
if (!ready_) Start(axes_, z, sigma);
else if (!Accept(axes_, z, sigma)) return false;
else Correct(axes_, z, sigma);
ready_ = true;
seenNs_ = timeNs;
return true;
}
const Step &first = steps_[Index(k)];
if (timeNs < first.timeNs - int64_t(first.dt * 1e9)) return false;
Axis axes[3];
if (ready_) {
if (k == 0) return false;
for (int a = 0; a < 3; a++) axes[a] = steps_[Index(k - 1)].after[a];
}
const double frac = std::clamp((timeNs - (first.timeNs - first.dt * 1e9)) / (first.dt * 1e9), 0.0, 1.0);
if (ready_) {
Propagate(axes, first.du * frac, first.dt * frac, Fade(timeNs - seenNs_));
if (!Accept(axes, z, sigma)) return false;
Correct(axes, z, sigma);
} else {
Start(axes, z, sigma);
}
Propagate(axes, first.du * (1.0 - frac), first.dt * (1.0 - frac), kDampingSeconds);
for (int a = 0; a < 3; a++) steps_[Index(k)].after[a] = axes[a];
for (int i = k + 1; i < count_; i++) {
Step &s = steps_[Index(i)];
Propagate(axes, s.du, s.dt, Fade(s.timeNs - timeNs));
for (int a = 0; a < 3; a++) s.after[a] = axes[a];
}
for (int a = 0; a < 3; a++) axes_[a] = axes[a];
ready_ = true;
seenNs_ = timeNs;
return true;
}
private:
struct Axis {
double x[2] = {0.0, 0.0};
double P[2][2] = {};
};
struct Step {
int64_t timeNs = 0;
double dt = 0.0;
Vec3 du;
Axis after[3];
};
int Index(int i) const { return (next_ - count_ + i + kSteps) % kSteps; }
static void Start(Axis *axes, const double *z, double sigma) {
for (int a = 0; a < 3; a++) {
axes[a] = Axis();
axes[a].x[0] = z[a];
axes[a].P[0][0] = sigma * sigma;
axes[a].P[1][1] = kStartSpeed * kStartSpeed;
}
}
bool Accept(const Axis *axes, const double *z, double sigma) {
double d2 = 0.0;
for (int a = 0; a < 3; a++) {
const double y = z[a] - axes[a].x[0];
d2 += y * y / (axes[a].P[0][0] + sigma * sigma);
}
if (d2 <= kGate) {
outliers_ = 0;
return true;
}
if (++outliers_ < kOutliers) return false;
outliers_ = 0;
ready_ = false;
return true;
}
void Correct(Axis *axes, const double *z, double sigma) const {
if (!ready_) {
Start(axes, z, sigma);
return;
}
for (int a = 0; a < 3; a++) Update(axes[a], 0, z[a], sigma);
}
static void Update(Axis &s, int state, double z, double sigma) {
const double S = s.P[state][state] + sigma * sigma;
const double K[2] = {s.P[0][state] / S, s.P[1][state] / S};
const double y = z - s.x[state];
s.x[0] += K[0] * y;
s.x[1] += K[1] * y;
const double p00 = s.P[0][0] - K[0] * s.P[state][0];
const double p01 = s.P[0][1] - K[0] * s.P[state][1];
const double p11 = s.P[1][1] - K[1] * s.P[state][1];
s.P[0][0] = p00;
s.P[0][1] = s.P[1][0] = p01;
s.P[1][1] = p11;
}
static void Stop(Axis *axes) {
for (int a = 0; a < 3; a++) Update(axes[a], 1, 0.0, kStillSpeed);
}
static double Fade(int64_t sinceSeenNs) { return sinceSeenNs > kStaleNs ? kStaleDampingSeconds : kDampingSeconds; }
static void Propagate(Axis *axes, const Vec3 &du, double dt, double damping) {
if (dt <= 0.0) return;
const double motion = Length(du);
const double g = kMotionError * kMotionError * motion * motion;
const double d[3] = {du.x, du.y, du.z};
const double keep = std::exp(-dt / damping);
const double q00 = kNoise * dt * dt * dt / 3.0 + g * dt * dt / 4.0;
const double q01 = kNoise * dt * dt / 2.0 + g * dt / 2.0;
const double q11 = kNoise * dt + g;
for (int a = 0; a < 3; a++) {
Axis &s = axes[a];
s.x[0] += s.x[1] * dt + 0.5 * d[a] * dt;
s.x[1] = (s.x[1] + d[a]) * keep;
const double p00 = s.P[0][0] + 2.0 * dt * s.P[0][1] + dt * dt * s.P[1][1] + q00;
const double p01 = keep * (s.P[0][1] + dt * s.P[1][1]) + q01;
const double p11 = keep * keep * s.P[1][1] + q11;
s.P[0][0] = p00;
s.P[0][1] = s.P[1][0] = p01;
s.P[1][1] = p11;
}
}
static constexpr int kSteps = 192;
static constexpr int64_t kGapNs = 250000000;
static constexpr int64_t kFutureNs = 50000000, kStaleNs = 200000000;
static constexpr double kNoise = 2.0, kMotionError = 0.3, kDampingSeconds = 0.3, kStaleDampingSeconds = 0.1;
static constexpr double kStartSpeed = 0.5;
static constexpr double kStillTurn = 0.3, kStillAccel = 0.6, kStillSeconds = 0.1, kStillSpeed = 0.05;
static constexpr double kGate = 25.0;
static constexpr int kOutliers = 3;
Step steps_[kSteps];
Axis axes_[3];
Vec3 lastInertial_;
int64_t lastNs_ = 0, seenNs_ = 0;
double stillSeconds_ = 0.0;
int count_ = 0, next_ = 0, outliers_ = 0;
bool ready_ = false, haveInput_ = false;
};
}
+443
View File
@@ -0,0 +1,443 @@
#pragma once
#include <algorithm>
#include <cmath>
#include <cstdint>
#include "hand_skeleton.h"
namespace droiddeck {
constexpr int kHandJoints = 21;
namespace hand {
enum : int {
kWrist = 0, kThumbCmc = 1, kThumbMcp = 2, kThumbIp = 3, kThumbTip = 4,
kIndexMcp = 5, kIndexPip = 6, kIndexDip = 7, kIndexTip = 8,
kMiddleMcp = 9, kMiddleTip = 12, kRingMcp = 13, kRingTip = 16, kPinkyMcp = 17, kPinkyTip = 20,
};
constexpr int kPalm[5] = {kWrist, kIndexMcp, kMiddleMcp, kRingMcp, kPinkyMcp};
constexpr int kCurlSteps = 20;
inline double Angle(const Vec3 &a, const Vec3 &b) {
const double la = Length(a), lb = Length(b);
if (la < 1e-9 || lb < 1e-9) return 0.0;
return std::acos(std::clamp(Dot(a, b) / (la * lb), -1.0, 1.0));
}
inline double Bend(const Vec3 joints[kHandJoints], int finger) {
if (finger == 0)
return Angle(joints[kThumbMcp] - joints[kThumbCmc], joints[kThumbIp] - joints[kThumbMcp]) +
Angle(joints[kThumbIp] - joints[kThumbMcp], joints[kThumbTip] - joints[kThumbIp]);
const int mcp = kIndexMcp + (finger - 1) * 4;
return Angle(joints[mcp] - joints[kWrist], joints[mcp + 1] - joints[mcp]) +
Angle(joints[mcp + 1] - joints[mcp], joints[mcp + 2] - joints[mcp + 1]) +
Angle(joints[mcp + 2] - joints[mcp + 1], joints[mcp + 3] - joints[mcp + 2]);
}
inline double &CurlOf(FingerCurls &c, int finger) {
switch (finger) {
case 0: return c.thumb;
case 1: return c.index;
case 2: return c.middle;
case 3: return c.ring;
default: return c.pinky;
}
}
}
inline void HandJoints(bool right, const FingerCurls &curls, Vec3 out[kHandJoints]) {
using namespace hand;
vr::VRBoneTransform_t bones[kHandBones];
HandSkeleton(right, curls, bones);
const Quat wristRotation = ToQuat(bones[1].orientation);
const Vec3 wrist = ToVec(bones[1].position);
out[kWrist] = wrist;
const auto chain = [&](int first, int count, Vec3 *joints) {
Quat r = wristRotation;
Vec3 p = wrist;
for (int b = 0; b < count; b++) {
p = p + Rotate(r, ToVec(bones[first + b].position));
r = r * ToQuat(bones[first + b].orientation);
joints[b] = p;
}
};
chain(2, 4, &out[kThumbCmc]);
for (int f = 0; f < 4; f++) {
Vec3 joints[5];
chain(6 + f * 5, 5, joints);
for (int j = 0; j < 4; j++) out[kIndexMcp + f * 4 + j] = joints[j + 1];
}
}
class HandModel {
public:
explicit HandModel(bool right) : right_(right) {
Vec3 open[kHandJoints], closed[kHandJoints];
HandJoints(right, FingerCurls{}, open);
FingerCurls fist;
fist.thumb = fist.index = fist.middle = fist.ring = fist.pinky = 1.0;
HandJoints(right, fist, closed);
for (int i = 0; i < 5; i++) center_ = center_ + open[hand::kPalm[i]] * 0.2;
const Vec3 normal = Cross(open[hand::kIndexMcp] - open[hand::kWrist], open[hand::kPinkyMcp] - open[hand::kWrist]);
normalSign_ = Dot(normal, closed[hand::kMiddleTip] - open[hand::kMiddleTip]) >= 0.0 ? 1.0 : -1.0;
for (int s = 0; s <= hand::kCurlSteps; s++) {
FingerCurls c;
c.thumb = c.index = c.middle = c.ring = c.pinky = double(s) / hand::kCurlSteps;
Vec3 joints[kHandJoints];
HandJoints(right, c, joints);
for (int f = 0; f < 5; f++) bends_[f][s] = hand::Bend(joints, f);
}
}
bool Right() const { return right_; }
Vec3 PalmCenter() const { return center_; }
bool Fit(const Vec3 joints[kHandJoints], const FingerCurls &curls, Quat *rotation, Vec3 *center, double *rms) const {
Vec3 shape[kHandJoints], palm[5], model;
HandJoints(right_, curls, shape);
for (int i = 0; i < 5; i++) model = model + shape[hand::kPalm[i]] * 0.2;
for (int i = 0; i < 5; i++) palm[i] = shape[hand::kPalm[i]] - model;
Vec3 observed[5], mean;
for (int i = 0; i < 5; i++) mean = mean + joints[hand::kPalm[i]] * 0.2;
for (int i = 0; i < 5; i++) observed[i] = joints[hand::kPalm[i]] - mean;
double S[3][3] = {};
for (int i = 0; i < 5; i++) {
const double m[3] = {palm[i].x, palm[i].y, palm[i].z};
const double o[3] = {observed[i].x, observed[i].y, observed[i].z};
for (int r = 0; r < 3; r++)
for (int c = 0; c < 3; c++) S[r][c] += m[r] * o[c];
}
double N[4][4] = {
{S[0][0] + S[1][1] + S[2][2], S[1][2] - S[2][1], S[2][0] - S[0][2], S[0][1] - S[1][0]},
{S[1][2] - S[2][1], S[0][0] - S[1][1] - S[2][2], S[0][1] + S[1][0], S[2][0] + S[0][2]},
{S[2][0] - S[0][2], S[0][1] + S[1][0], -S[0][0] + S[1][1] - S[2][2], S[1][2] + S[2][1]},
{S[0][1] - S[1][0], S[2][0] + S[0][2], S[1][2] + S[2][1], -S[0][0] - S[1][1] + S[2][2]},
};
double q[4];
if (!LargestEigenvector(N, q)) return false;
const Quat fitted = Normalize({q[0], q[1], q[2], q[3]});
double squares = 0.0;
for (int i = 0; i < 5; i++) {
const Vec3 d = Rotate(fitted, palm[i]) - observed[i];
squares += Dot(d, d);
}
*rotation = fitted;
if (center) *center = model;
if (rms) *rms = std::sqrt(squares / 5.0);
return true;
}
Vec3 PalmNormal(const Vec3 joints[kHandJoints]) const {
const Vec3 n = Cross(joints[hand::kIndexMcp] - joints[hand::kWrist], joints[hand::kPinkyMcp] - joints[hand::kWrist]);
const double length = Length(n);
return length > 1e-9 ? n * (normalSign_ / length) : Vec3{};
}
FingerCurls Curls(const Vec3 joints[kHandJoints]) const {
FingerCurls out;
for (int f = 0; f < 5; f++) {
const double bend = hand::Bend(joints, f);
const double *table = bends_[f];
double curl = bend <= table[0] ? 0.0 : 1.0;
for (int s = 0; s < hand::kCurlSteps; s++) {
if (bend < table[s] || bend > table[s + 1]) continue;
const double span = table[s + 1] - table[s];
curl = (s + (span > 1e-9 ? (bend - table[s]) / span : 0.0)) / hand::kCurlSteps;
break;
}
hand::CurlOf(out, f) = curl;
}
return out;
}
private:
static bool LargestEigenvector(double A[4][4], double out[4]) {
double V[4][4] = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
for (int sweep = 0; sweep < 50; sweep++) {
double off = 0.0;
for (int p = 0; p < 4; p++)
for (int r = p + 1; r < 4; r++) off += A[p][r] * A[p][r];
if (off < 1e-24) break;
for (int p = 0; p < 4; p++)
for (int r = p + 1; r < 4; r++) {
if (std::fabs(A[p][r]) < 1e-30) continue;
const double theta = (A[r][r] - A[p][p]) / (2.0 * A[p][r]);
const double t = (theta >= 0.0 ? 1.0 : -1.0) / (std::fabs(theta) + std::sqrt(theta * theta + 1.0));
const double c = 1.0 / std::sqrt(t * t + 1.0), s = t * c;
for (int k = 0; k < 4; k++) {
const double akp = A[k][p], akr = A[k][r];
A[k][p] = c * akp - s * akr;
A[k][r] = s * akp + c * akr;
}
for (int k = 0; k < 4; k++) {
const double apk = A[p][k], ark = A[r][k];
A[p][k] = c * apk - s * ark;
A[r][k] = s * apk + c * ark;
}
for (int k = 0; k < 4; k++) {
const double vkp = V[k][p], vkr = V[k][r];
V[k][p] = c * vkp - s * vkr;
V[k][r] = s * vkp + c * vkr;
}
}
}
int best = 0;
for (int i = 1; i < 4; i++)
if (A[i][i] > A[best][best]) best = i;
double length = 0.0;
for (int k = 0; k < 4; k++) length += V[k][best] * V[k][best];
if (!(length > 1e-12)) return false;
for (int k = 0; k < 4; k++) out[k] = V[k][best] / std::sqrt(length);
return true;
}
bool right_;
Vec3 center_;
double normalSign_ = 1.0;
double bends_[5][hand::kCurlSteps + 1] = {};
};
class Debounce {
public:
bool Update(bool in, int64_t nowNs, int64_t onNs, int64_t offNs) {
if (in == on_) {
pending_ = false;
return on_;
}
if (!pending_) {
pending_ = true;
sinceNs_ = nowNs;
}
if (nowNs - sinceNs_ >= (in ? onNs : offNs)) {
on_ = in;
pending_ = false;
}
return on_;
}
void Reset() { on_ = pending_ = false; }
bool On() const { return on_; }
private:
bool on_ = false, pending_ = false;
int64_t sinceNs_ = 0;
};
struct HandButtons {
float trigger = 0.0f, grip = 0.0f;
bool triggerClick = false, triggerTouch = false, gripClick = false;
bool a = false, b = false, system = false, facing = false;
};
class HandGestures {
public:
explicit HandGestures(bool right) : right_(right) {}
HandButtons Update(const HandModel &model, const Vec3 joints[kHandJoints], const FingerCurls &curls, const Vec3 &palm,
const Vec3 &head, int64_t nowNs) {
using namespace hand;
const double index = Length(joints[kThumbTip] - joints[kIndexTip]);
const double middle = Length(joints[kThumbTip] - joints[kMiddleTip]);
const double ring = Length(joints[kThumbTip] - joints[kRingTip]);
const Vec3 toHead = head - palm;
const double distance = Length(toHead);
const bool towards = distance > 1e-6 && Dot(model.PalmNormal(joints), toHead * (1.0 / distance)) > kFacing;
HandButtons out;
out.facing = facing_.Update(towards, nowNs, kFacingOnNs, kOffNs);
const double fist = (curls.middle + curls.ring + curls.pinky) / 3.0;
out.grip = (float)std::clamp((fist - kGripStart) / (kGripFull - kGripStart), 0.0, 1.0);
out.gripClick = grip_.Update(gripClick_ ? out.grip > kGripRelease : out.grip >= 1.0f, nowNs, kOnNs, kOffNs);
gripClick_ = out.gripClick;
const bool pinch = pinch_.Update(index < (pinchHeld_ ? kPinchOff : kPinchOn), nowNs, kOnNs, kOffNs);
pinchHeld_ = pinch;
const bool loose = out.grip < 0.5f;
const bool aPinch = a_.Update(loose && !pinch && Touched(ring, a_.On()), nowNs, kOnNs, kOffNs);
const bool bPinch = b_.Update(loose && !pinch && Touched(middle, b_.On()), nowNs, kOnNs, kOffNs);
if (out.facing) {
if (right_) out.system = pinch;
else out.b = pinch;
return out;
}
out.trigger = (float)std::clamp((kPinchFar - index) / (kPinchFar - kPinchNear), 0.0, 1.0);
out.triggerClick = pinch;
out.triggerTouch = out.trigger > 0.0f;
out.a = aPinch;
out.b = bPinch;
return out;
}
void Reset() {
pinch_.Reset();
a_.Reset();
b_.Reset();
grip_.Reset();
facing_.Reset();
pinchHeld_ = gripClick_ = false;
}
static constexpr double kPinchOn = 0.035, kPinchOff = 0.045, kPinchNear = 0.03, kPinchFar = 0.06;
static constexpr double kTouchOn = 0.025, kTouchOff = 0.035;
static constexpr double kGripStart = 0.35, kGripFull = 0.75, kGripRelease = 0.7;
static constexpr double kFacing = 0.6;
static constexpr int64_t kOnNs = 50000000, kOffNs = 100000000, kFacingOnNs = 300000000;
private:
static bool Touched(double distance, bool held) { return distance < (held ? kTouchOff : kTouchOn); }
bool right_;
Debounce pinch_, a_, b_, grip_, facing_;
bool pinchHeld_ = false, gripClick_ = false;
};
class SmoothTurn {
public:
void Reset() { ready_ = false; }
Quat Update(const Quat &target, double dt) {
if (!ready_) {
at_ = last_ = target;
speed_ = 0.0;
ready_ = true;
return at_;
}
if (!(dt > 0.0)) return at_;
const double moved = AngleDegrees(Conjugate(last_) * target) * M_PI / 180.0;
last_ = target;
speed_ += (moved / dt - speed_) * Alpha(dt, kSpeedCutoff);
at_ = Nlerp(at_, target, Alpha(dt, kMinCutoff + kBeta * speed_));
return at_;
}
static constexpr double kMinCutoff = 1.0, kBeta = 0.2, kSpeedCutoff = 1.0;
private:
static double Alpha(double dt, double hz) { return 1.0 / (1.0 + 1.0 / (2.0 * M_PI * hz * dt)); }
bool ready_ = false;
Quat at_, last_;
double speed_ = 0.0;
};
class SmoothPoint {
public:
void Reset() { ready_ = false; }
Vec3 Update(const Vec3 &target, double dt) {
if (!ready_) {
at_ = target;
speed_ = 0.0;
velocity_ = Vec3{};
ready_ = true;
return at_;
}
if (!(dt > 0.0)) return at_;
const Vec3 before = at_;
speed_ += (Length(target - at_) / dt - speed_) * Alpha(dt, kSpeedCutoff);
at_ = at_ + (target - at_) * Alpha(dt, kMinCutoff + kBeta * speed_);
velocity_ = velocity_ + ((at_ - before) * (1.0 / dt) - velocity_) * Alpha(dt, kSpeedCutoff);
return at_;
}
Vec3 Velocity() const { return velocity_; }
static constexpr double kMinCutoff = 1.0, kBeta = 0.5, kSpeedCutoff = 1.0;
private:
static double Alpha(double dt, double hz) { return 1.0 / (1.0 + 1.0 / (2.0 * M_PI * hz * dt)); }
bool ready_ = false;
Vec3 at_, velocity_;
double speed_ = 0.0;
};
struct CameraHandPose {
bool visible = false;
Quat rotation;
Vec3 position, velocity;
FingerCurls curls;
HandButtons buttons;
};
class CameraHand {
public:
explicit CameraHand(bool right) : model_(right), gestures_(right) {}
void Reset() {
Hide();
shown_ = false;
}
void See(int64_t timeNs, const Vec3 &palm, const Vec3 *joints, const Vec3 &head, int64_t nowNs) {
if (!joints) return;
const FingerCurls curls = model_.Curls(joints);
Quat rotation;
Vec3 center;
double rms = 1.0;
if (!model_.Fit(joints, curls, &rotation, &center, &rms) || rms > kMaxRms) return;
palm_ = palm;
seenNs_ = timeNs;
rms_ = rms;
if (seen_ && AngleDegrees(Conjugate(target_) * rotation) > kMaxTurnDegrees) {
const bool agrees = pending_ && AngleDegrees(Conjugate(pendingTurn_) * rotation) < kAgreeDegrees;
pending_ = !agrees;
pendingTurn_ = rotation;
if (!agrees) return;
}
pending_ = false;
seen_ = true;
target_ = rotation;
center_ = center;
targetCurls_ = curls;
buttons_ = gestures_.Update(model_, joints, curls, palm, head, nowNs);
}
CameraHandPose Update(int64_t nowNs, double dt) {
CameraHandPose out;
if (!seen_ || nowNs - seenNs_ > kHoldNs) {
if (shown_) Hide();
shown_ = false;
return out;
}
if (!shown_) curls_ = targetCurls_;
shown_ = true;
const double follow = 1.0 - std::exp(-std::max(dt, 0.0) / kCurlSeconds);
for (int f = 0; f < 5; f++) hand::CurlOf(curls_, f) += (hand::CurlOf(targetCurls_, f) - hand::CurlOf(curls_, f)) * follow;
out.visible = true;
out.rotation = turn_.Update(target_, dt);
out.position = place_.Update(palm_, dt) - Rotate(out.rotation, center_);
out.velocity = place_.Velocity();
out.curls = curls_;
out.buttons = buttons_;
return out;
}
double Rms() const { return rms_; }
static constexpr double kMaxRms = 0.02, kCurlSeconds = 0.08, kMaxTurnDegrees = 45.0, kAgreeDegrees = 25.0;
static constexpr int64_t kHoldNs = 500000000;
private:
void Hide() {
place_.Reset();
turn_.Reset();
gestures_.Reset();
buttons_ = HandButtons();
seen_ = pending_ = false;
}
HandModel model_;
HandGestures gestures_;
SmoothPoint place_;
SmoothTurn turn_;
Quat target_, pendingTurn_;
Vec3 center_, palm_;
FingerCurls targetCurls_, curls_;
HandButtons buttons_;
int64_t seenNs_ = 0;
double rms_ = 0.0;
bool seen_ = false, pending_ = false, shown_ = false;
};
}
+128
View File
@@ -0,0 +1,128 @@
// Adapted from Valve's OpenVR handskeletonsimulation driver sample (BSD-3-Clause, see OPENVR_LICENSE),
// which follows Moshi Turner's hand simulation in Monado.
#pragma once
#include "arm_model.h"
#include "openvr_driver.h"
namespace droiddeck {
constexpr uint32_t kHandBones = 31;
struct FingerCurls {
double thumb = 0.0, index = 0.0, middle = 0.0, ring = 0.0, pinky = 0.0;
};
namespace hand {
constexpr double kDegree = M_PI / 180.0;
constexpr double kLengths[5][5] = {
{0.05, 0.05, 0.035, 0.025, 0.0},
{0.03, 0.073, 0.045, 0.025, 0.02},
{0.01, 0.091, 0.049, 0.03, 0.02},
{0.02, 0.073, 0.045, 0.03, 0.03},
{0.03, 0.067, 0.03, 0.025, 0.02},
};
struct Joint {
double swing[2] = {0.0, 0.0};
double twist = 0.0;
};
struct Finger {
Joint metacarpal, proximal;
double intermediate = 0.0, distal = 0.0;
};
inline Quat SwingTwist(const Joint &j) {
const double squared = j.swing[0] * j.swing[0] + j.swing[1] * j.swing[1];
const double cosTwist = std::cos(j.twist * 0.5), sinTwist = std::sin(j.twist * 0.5);
double cosSwing = 1.0, sinOver = 0.5;
if (squared > 0.0) {
const double theta = std::sqrt(squared);
cosSwing = std::cos(theta * 0.5);
sinOver = std::sin(theta * 0.5) / theta;
}
return {cosSwing * cosTwist, cosSwing * sinTwist,
j.swing[1] * cosTwist * sinOver - j.swing[0] * sinTwist * sinOver,
j.swing[0] * cosTwist * sinOver + j.swing[1] * sinTwist * sinOver};
}
inline Quat AboutZ(double radians) { return {std::cos(radians * 0.5), 0.0, 0.0, std::sin(radians * 0.5)}; }
inline vr::VRBoneTransform_t Bone(bool right, const Quat &q, Vec3 position) {
if (right) position.x = -position.x;
vr::VRBoneTransform_t t;
t.position = {{(float)position.x, (float)position.y, (float)position.z, 1.0f}};
t.orientation = {(float)q.w, (float)q.x, (float)q.y, (float)q.z};
return t;
}
inline vr::VRBoneTransform_t Metacarpal(bool right, const Quat &orientation, double length) {
Quat q = Quat{0.5, 0.5, -0.5, 0.5} * orientation;
const Vec3 position = Rotate(q, {length, 0.0, 0.0});
if (right) q = {q.x, -q.w, q.z, -q.y};
return Bone(right, q, position);
}
inline Quat ToQuat(const vr::HmdQuaternionf_t &q) { return {q.w, q.x, q.y, q.z}; }
inline Vec3 ToVec(const vr::HmdVector4_t &v) { return {v.v[0], v.v[1], v.v[2]}; }
}
inline void HandSkeleton(bool right, const FingerCurls &curls, vr::VRBoneTransform_t out[kHandBones]) {
using namespace hand;
out[0] = {{{0.0f, 0.0f, 0.0f, 1.0f}}, {1.0f, 0.0f, 0.0f, 0.0f}};
out[1] = {{{-0.034038f, 0.036503f, 0.164722f, 1.0f}}, {-0.055147f, -0.078608f, -0.920279f, 0.379296f}};
if (right) {
out[1].position.v[0] = -out[1].position.v[0];
out[1].orientation.y = -out[1].orientation.y;
out[1].orientation.z = -out[1].orientation.z;
}
Joint thumbMetacarpal, thumbProximal;
thumbMetacarpal.swing[0] = 10 * kDegree + curls.thumb * 5 * kDegree;
thumbMetacarpal.swing[1] = 40 * kDegree;
thumbProximal.swing[0] = curls.thumb * 90 * kDegree;
const double thumbDistal = curls.thumb * 90 * kDegree;
out[2] = Metacarpal(right, SwingTwist(thumbMetacarpal), kLengths[0][0]);
out[3] = Bone(right, SwingTwist(thumbProximal), {kLengths[0][1], 0.0, 0.0});
out[4] = Bone(right, AboutZ(thumbDistal), {kLengths[0][2], 0.0, 0.0});
out[5] = Bone(right, Quat{}, {kLengths[0][3], 0.0, 0.0});
const double metacarpalSplay[4] = {13.0, 0.0, -15.0, -27.0};
const double proximalSplay[4] = {3.0, 0.0, -1.0, -2.0};
const double fingerCurls[4] = {curls.index, curls.middle, curls.ring, curls.pinky};
for (int f = 0; f < 4; f++) {
Finger finger;
finger.metacarpal.swing[0] = fingerCurls[f] * 5 * kDegree;
finger.metacarpal.swing[1] = metacarpalSplay[f] * kDegree;
finger.proximal.swing[0] = fingerCurls[f] * 90 * kDegree;
finger.proximal.swing[1] = proximalSplay[f] * kDegree;
finger.intermediate = 5 * kDegree + fingerCurls[f] * 80 * kDegree;
finger.distal = 5 * kDegree + fingerCurls[f] * 80 * kDegree;
const int base = 6 + f * 5;
const double *length = kLengths[f + 1];
out[base] = Metacarpal(right, SwingTwist(finger.metacarpal), length[0]);
out[base + 1] = Bone(right, SwingTwist(finger.proximal), {length[1], 0.0, 0.0});
out[base + 2] = Bone(right, AboutZ(finger.intermediate), {length[2], 0.0, 0.0});
out[base + 3] = Bone(right, AboutZ(finger.distal), {length[3], 0.0, 0.0});
out[base + 4] = Bone(right, Quat{}, {length[4], 0.0, 0.0});
}
const int distal[5] = {4, 9, 14, 19, 24};
for (int f = 0; f < 5; f++) {
const int first = f == 0 ? 2 : 6 + (f - 1) * 5;
Quat rotation = ToQuat(out[1].orientation);
Vec3 position = ToVec(out[1].position);
for (int b = first; b <= distal[f]; b++) {
position = position + Rotate(rotation, ToVec(out[b].position));
rotation = rotation * ToQuat(out[b].orientation);
}
out[26 + f] = {{{(float)position.x, (float)position.y, (float)position.z, 1.0f}},
{(float)rotation.w, (float)rotation.x, (float)rotation.y, (float)rotation.z}};
}
}
}
+51
View File
@@ -0,0 +1,51 @@
#ifndef HANDCAM_SHM_H
#define HANDCAM_SHM_H
#include <stddef.h>
#include <stdint.h>
#define DROIDDECK_HANDCAM_MAGIC 0x4D434844u
#define DROIDDECK_HANDCAM_VERSION 2u
#define DROIDDECK_HANDCAM_JOINT_COUNT 21
#define DROIDDECK_HANDCAM_FILE "droiddeck-handcam"
enum {
DROIDDECK_HANDCAM_RUNNING = 1u << 0,
DROIDDECK_HANDCAM_FACE = 1u << 1,
DROIDDECK_HANDCAM_BARE_HANDS = 1u << 2,
};
enum {
DROIDDECK_HANDCAM_SEEN = 1u << 0,
DROIDDECK_HANDCAM_ESTIMATED = 1u << 1,
DROIDDECK_HANDCAM_JOINTS = 1u << 2,
};
struct droiddeck_handcam_hand {
uint32_t flags;
float position[3];
float joints[DROIDDECK_HANDCAM_JOINT_COUNT][3];
};
struct droiddeck_handcam {
uint32_t magic;
uint32_t version;
uint32_t seq;
uint32_t flags;
int64_t time_ns;
uint32_t frame;
float face_yaw;
struct droiddeck_handcam_hand hands[2];
};
#ifdef __cplusplus
#define DROIDDECK_HANDCAM_ASSERT static_assert
#else
#define DROIDDECK_HANDCAM_ASSERT _Static_assert
#endif
DROIDDECK_HANDCAM_ASSERT(offsetof(struct droiddeck_handcam_hand, joints) == 16, "handcam hand layout");
DROIDDECK_HANDCAM_ASSERT(sizeof(struct droiddeck_handcam_hand) == 268, "handcam hand layout");
DROIDDECK_HANDCAM_ASSERT(offsetof(struct droiddeck_handcam, time_ns) == 16, "handcam layout");
DROIDDECK_HANDCAM_ASSERT(offsetof(struct droiddeck_handcam, hands) == 32, "handcam layout");
DROIDDECK_HANDCAM_ASSERT(sizeof(struct droiddeck_handcam) == 568, "handcam layout");
#endif
+17
View File
@@ -0,0 +1,17 @@
// Index controller tip and grip offsets in its raw frame, as Monado's vive_poses.c (BSL-1.0) lists them; see docs/third-party/joycon.
#pragma once
#include "arm_model.h"
namespace droiddeck {
inline Quat IndexTipRotation(bool right) {
return AboutY((right ? 5.0 : -5.0) * M_PI / 180.0) * AboutX(-40.0 * M_PI / 180.0);
}
inline void IndexRawPose(bool right, const Quat &aim, const Vec3 &grip, Quat *raw, Vec3 *position) {
*raw = Normalize(aim * Conjugate(IndexTipRotation(right)));
*position = grip - Rotate(*raw, {0.0, -0.015, 0.13});
}
}
+61
View File
@@ -0,0 +1,61 @@
#ifndef JOYCON_SHM_H
#define JOYCON_SHM_H
#include <stddef.h>
#include <stdint.h>
#define DROIDDECK_JOYCON_MAGIC 0x4E434A44u
#define DROIDDECK_JOYCON_VERSION 3u
#define DROIDDECK_JOYCON_FILE "droiddeck-joycon"
#define DROIDDECK_JOYCON_RUMBLE_SLOT 0x100u
enum {
DROIDDECK_JOYCON_CONNECTED = 1u << 0,
DROIDDECK_JOYCON_CHARGING = 1u << 1,
};
enum {
DROIDDECK_JOYCON_TRIGGER = 1u << 0,
DROIDDECK_JOYCON_GRIP = 1u << 1,
DROIDDECK_JOYCON_A = 1u << 2,
DROIDDECK_JOYCON_B = 1u << 3,
DROIDDECK_JOYCON_STICK = 1u << 4,
DROIDDECK_JOYCON_SYSTEM = 1u << 5,
};
struct droiddeck_joycon_hand {
uint32_t flags;
uint32_t buttons;
float rotation[4];
float angular_velocity[3];
float stick[2];
float battery;
uint32_t recenter;
uint32_t pad;
int64_t time_ns;
float displacement[3];
float velocity[3];
float inertial_velocity[3];
uint32_t reserved;
};
struct droiddeck_joycon {
uint32_t magic;
uint32_t version;
uint32_t seq;
uint32_t reserved;
struct droiddeck_joycon_hand hands[2];
};
#ifdef __cplusplus
#define DROIDDECK_JOYCON_ASSERT static_assert
#else
#define DROIDDECK_JOYCON_ASSERT _Static_assert
#endif
DROIDDECK_JOYCON_ASSERT(sizeof(struct droiddeck_joycon_hand) == 104, "joycon hand layout");
DROIDDECK_JOYCON_ASSERT(offsetof(struct droiddeck_joycon_hand, time_ns) == 56, "joycon hand layout");
DROIDDECK_JOYCON_ASSERT(offsetof(struct droiddeck_joycon_hand, displacement) == 64, "joycon hand layout");
DROIDDECK_JOYCON_ASSERT(offsetof(struct droiddeck_joycon_hand, inertial_velocity) == 88, "joycon hand layout");
DROIDDECK_JOYCON_ASSERT(sizeof(struct droiddeck_joycon) == 224, "joycon layout");
DROIDDECK_JOYCON_ASSERT(offsetof(struct droiddeck_joycon, hands) == 16, "joycon layout");
#endif
+10
View File
@@ -0,0 +1,10 @@
#pragma once
#include <stdint.h>
#define DROIDDECK_PRESENT_SYNC_NAME "present-sync"
#define DROIDDECK_PRESENT_SYNC_SIZE 4096
struct droiddeck_present_sync {
uint32_t issued;
uint32_t done;
};
+134 -18
View File
@@ -8,11 +8,14 @@ typedef unsigned long VisualID;
#include <vulkan/vulkan_wayland.h>
#include "xdg-shell-client-protocol.h"
#include "../../../app/src/main/cpp/waylandcomp/src/headpose_shm.h"
#include "presenter.h"
#include "present_sync.h"
#include "xr_tracking_file.h"
#include "xr_frame_channel.h"
#include "producer_lease.h"
#include <algorithm>
#include <atomic>
#include <chrono>
#include <condition_variable>
@@ -22,7 +25,11 @@ typedef unsigned long VisualID;
#include <cstring>
#include <dlfcn.h>
#include <fcntl.h>
#include <linux/futex.h>
#include <mutex>
#include <string>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <thread>
#include <unistd.h>
#include <vector>
@@ -137,10 +144,12 @@ VkFormat UnormOf(uint32_t f) {
class Impl {
public:
static Impl &Get() {
static Impl impl;
return impl;
static Impl *impl = made_ = new Impl;
return *impl;
}
static Impl *Made() { return made_; }
void Add(const uint64_t handles[3], const int fds[3], uint32_t format, uint32_t w, uint32_t h, uint32_t producerPid) {
std::lock_guard<std::mutex> lock(mutex_);
if (failed_ || exit_) return;
@@ -168,6 +177,11 @@ public:
EnsureThread();
frame_ = f;
frameAt_ = std::chrono::steady_clock::now();
const droiddeck_present_sync *sync = MapSync(frameAt_);
const uint32_t issued = sync ? __atomic_load_n(&sync->issued, __ATOMIC_ACQUIRE) : 0;
frameTracked_ = sync && issued != lastIssued_;
frameMark_ = issued;
lastIssued_ = issued;
haveFrame_ = true;
submitted_++;
uint64_t ticket = ++ticket_;
@@ -220,12 +234,16 @@ private:
Log("presenter: unavailable, frames are not shown");
Fail(); return;
}
uint64_t shown = 0;
if (const char *g = getenv("DROIDDECK_VR_PRESENT_GUARD_MS")) guard_ = std::chrono::microseconds((long)(atof(g) * 1000));
uint64_t shown = 0, waited = 0, late = 0;
std::chrono::steady_clock::duration longest{};
auto lastLog = std::chrono::steady_clock::now();
while (true) {
EyeFrame f;
uint64_t ticket = 0;
std::chrono::steady_clock::time_point at;
uint32_t mark = 0;
bool tracked = false;
std::vector<PendingImport> imports;
std::vector<uint64_t> removals;
{
@@ -236,6 +254,8 @@ private:
removals.swap(removals_);
f = frame_;
at = frameAt_;
mark = frameMark_;
tracked = frameTracked_;
bool draw = haveFrame_;
haveFrame_ = false;
inFlight_ = draw; ticket = ticket_;
@@ -244,7 +264,19 @@ private:
if (wl_) { wl_display_dispatch_pending(wl_); wl_display_flush(wl_); }
for (auto &p : imports) Import(p);
for (auto h : removals) Forget(h);
if (f.texture[0] && !frameChannel_) std::this_thread::sleep_until(at + guard_);
if (f.texture[0] && !frameChannel_ && tracked) {
const auto start = std::chrono::steady_clock::now();
const int r = WaitDrawn(mark, start + kDrawnTimeout);
if (r > 0) {
waited++;
longest = std::max(longest, std::chrono::steady_clock::now() - start);
} else if (r < 0) {
late++;
}
} else if (f.texture[0] && !frameChannel_) {
std::this_thread::sleep_until(at + guard_);
}
if (f.texture[0] && wayland_) FollowPanel();
if (f.texture[0] && (frameChannel_ ? Capture(f) : Draw(f))) shown++;
{
std::lock_guard<std::mutex> lock(mutex_);
@@ -253,9 +285,11 @@ private:
}
auto now = std::chrono::steady_clock::now();
if (now - lastLog > std::chrono::seconds(10)) {
Log("presenter: %llu frames shown, %llu submitted (last 10 s)", (unsigned long long)shown,
(unsigned long long)submitted_.exchange(0));
shown = 0;
Log("presenter: %llu frames shown, %llu submitted, %llu waited for the GPU (longest %.1f ms), %llu late (last 10 s)",
(unsigned long long)shown, (unsigned long long)submitted_.exchange(0), (unsigned long long)waited,
std::chrono::duration<double, std::milli>(longest).count(), (unsigned long long)late);
shown = waited = late = 0;
longest = {};
lastLog = now;
}
}
@@ -278,7 +312,79 @@ private:
surface_ = VK_NULL_HANDLE; inst_ = VK_NULL_HANDLE;
}
bool PanelSize(uint32_t *w, uint32_t *h) {
if (!pose_) {
int fd = open("/run/droiddeck/" DROIDDECK_HEADPOSE_FILE, O_RDONLY | O_CLOEXEC);
if (fd < 0) return false;
void *m = mmap(nullptr, sizeof(droiddeck_headpose), PROT_READ, MAP_SHARED, fd, 0);
close(fd);
if (m == MAP_FAILED) return false;
pose_ = (const volatile droiddeck_headpose *)m;
}
for (int tries = 0; tries < 8; tries++) {
const uint32_t a = __atomic_load_n(&pose_->seq, __ATOMIC_ACQUIRE);
if (a & 1u) continue;
const uint32_t ew = pose_->eye_w, eh = pose_->eye_h;
__atomic_thread_fence(__ATOMIC_ACQUIRE);
if (__atomic_load_n(&pose_->seq, __ATOMIC_RELAXED) != a) continue;
if (!ew || !eh || pose_->magic != DROIDDECK_HEADPOSE_MAGIC) return false;
*w = ew * 2;
*h = eh;
return true;
}
return false;
}
void FollowPanel() {
uint32_t w = 0, h = 0;
if (!PanelSize(&w, &h)) return;
const bool resized = w != width_ || h != height_;
const auto now = std::chrono::steady_clock::now();
if (!resized && (swapchain_ || now < retryAt_)) return;
vk_.DeviceWaitIdle(dev_);
if (swapchain_) vk_.DestroySwapchainKHR(dev_, swapchain_, nullptr);
swapchain_ = VK_NULL_HANDLE;
images_.clear();
if (resized) Log("presenter: panel now %ux%u (was %ux%u)", w, h, width_, height_);
width_ = w;
height_ = h;
if (!CreateSwapchain()) retryAt_ = now + std::chrono::seconds(1);
}
std::chrono::microseconds guard_{4000};
std::chrono::steady_clock::time_point retryAt_{};
static constexpr std::chrono::milliseconds kDrawnTimeout{100};
const droiddeck_present_sync *MapSync(std::chrono::steady_clock::time_point now) {
if (sync_) return sync_;
if (syncTried_.time_since_epoch().count() && now - syncTried_ < std::chrono::seconds(1)) return nullptr;
syncTried_ = now;
const char *rt = getenv("XDG_RUNTIME_DIR");
std::string path = std::string(rt && *rt ? rt : "/run/droiddeck") + "/droiddeck-steamvr/" DROIDDECK_PRESENT_SYNC_NAME;
int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
if (fd < 0) return nullptr;
void *m = mmap(nullptr, DROIDDECK_PRESENT_SYNC_SIZE, PROT_READ, MAP_SHARED, fd, 0);
close(fd);
if (m == MAP_FAILED) return nullptr;
sync_ = (const droiddeck_present_sync *)m;
Log("presenter: frames wait for the compositor's GPU work (%s)", path.c_str());
return sync_;
}
int WaitDrawn(uint32_t mark, std::chrono::steady_clock::time_point until) {
const droiddeck_present_sync *s = sync_;
int slept = 0;
while (true) {
const uint32_t done = __atomic_load_n(&s->done, __ATOMIC_ACQUIRE);
if ((int32_t)(done - mark) >= 0) return slept;
const auto left = until - std::chrono::steady_clock::now();
if (left <= std::chrono::steady_clock::duration::zero()) return -1;
const auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(left).count();
struct timespec ts = {(time_t)(ns / 1000000000), (long)(ns % 1000000000)};
syscall(SYS_futex, const_cast<uint32_t *>(&s->done), FUTEX_WAIT, done, &ts, nullptr, 0);
slept = 1;
}
}
struct wl_display *wl_ = nullptr;
struct wl_compositor *wlComp_ = nullptr;
@@ -333,12 +439,7 @@ private:
wl_surface_commit(wlSurf_);
for (int i = 0; i < 20 && !configured_; i++) wl_display_roundtrip(wl_);
width_ = height_ = 0;
int fd = open("/run/droiddeck/droiddeck-headpose", O_RDONLY | O_CLOEXEC);
if (fd >= 0) {
uint32_t eye[2] = {0, 0};
if (pread(fd, eye, sizeof(eye), 72) == (ssize_t)sizeof(eye) && eye[0] && eye[1]) { width_ = eye[0] * 2; height_ = eye[1]; }
close(fd);
}
if (!PanelSize(&width_, &height_)) width_ = height_ = 0;
XrTrackingFile xrFile;
DroidDeckXrTracking tracking;
if (xrFile.Read(&tracking) && XrTrackingFile::Fresh(tracking)) {
@@ -351,7 +452,6 @@ private:
}
bool InitWindow() {
if (const char *g = getenv("DROIDDECK_VR_PRESENT_GUARD_MS")) guard_ = std::chrono::microseconds((long)(atof(g) * 1000));
if (!x_.Load()) { Log("presenter: no libX11"); return false; }
const char *name = getenv("DROIDDECK_VR_DISPLAY");
if (!name || !*name) name = ":0";
@@ -439,7 +539,7 @@ private:
VkSemaphoreCreateInfo semci = {VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, nullptr, 0};
vk_.CreateSemaphore(dev_, &semci, nullptr, &acquired_);
vk_.CreateSemaphore(dev_, &semci, nullptr, &rendered_);
if (!frameChannel_ && !CreateSwapchain()) return false;
if (!frameChannel_ && !CreateSwapchain() && !wayland_) return false;
Log("presenter: Vulkan on %s, queue family %u", props.deviceName, qfam_);
return true;
}
@@ -480,7 +580,11 @@ private:
sci.presentMode = pm;
sci.clipped = VK_TRUE;
VkResult r = vk_.CreateSwapchainKHR(dev_, &sci, nullptr, &swapchain_);
if (r != VK_SUCCESS) { Log("presenter: vkCreateSwapchainKHR %d", r); return false; }
if (r != VK_SUCCESS) {
swapchain_ = VK_NULL_HANDLE;
Log("presenter: vkCreateSwapchainKHR %d", r);
return false;
}
uint32_t ni = 0;
vk_.GetSwapchainImagesKHR(dev_, swapchain_, &ni, nullptr);
images_.resize(ni);
@@ -693,7 +797,7 @@ private:
bool Draw(const EyeFrame &f) {
const Imported *eye[2] = {Find(f.texture[0]), Find(f.texture[1])};
if (!eye[0] || !eye[1]) return false;
if (!eye[0] || !eye[1] || !swapchain_) return false;
vk_.WaitForFences(dev_, 1, &fence_, VK_TRUE, UINT64_MAX);
uint32_t idx = 0;
VkResult r = vk_.AcquireNextImageKHR(dev_, swapchain_, 100000000ull, acquired_, VK_NULL_HANDLE, &idx);
@@ -745,6 +849,10 @@ private:
VkPipelineStageFlags wait = VK_PIPELINE_STAGE_TRANSFER_BIT;
VkSubmitInfo si = {VK_STRUCTURE_TYPE_SUBMIT_INFO, nullptr, 1, &acquired_, &wait, 1, &cmd_, 1, &rendered_};
if ((r = vk_.QueueSubmit(queue_, 1, &si, fence_)) != VK_SUCCESS) { Log("presenter: submit %d", r); return false; }
if (vk_.WaitForFences(dev_, 1, &fence_, VK_TRUE, (uint64_t)std::chrono::nanoseconds(kDrawnTimeout).count()) == VK_TIMEOUT) {
static int said;
if (said++ < 3) Log("presenter: the copy took over %lld ms; showing it anyway", (long long)kDrawnTimeout.count());
}
VkPresentInfoKHR pi = {VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, nullptr, 1, &rendered_, 1, &swapchain_, &idx, nullptr};
r = vk_.QueuePresentKHR(queue_, &pi);
if (r != VK_SUCCESS && r != VK_SUBOPTIMAL_KHR) {
@@ -760,6 +868,11 @@ private:
std::vector<uint64_t> removals_;
EyeFrame frame_{};
std::chrono::steady_clock::time_point frameAt_{};
uint32_t frameMark_ = 0, lastIssued_ = 0;
bool frameTracked_ = false;
const droiddeck_present_sync *sync_ = nullptr;
const volatile droiddeck_headpose *pose_ = nullptr;
std::chrono::steady_clock::time_point syncTried_{};
bool haveFrame_ = false, exit_ = false, started_ = false, inFlight_ = false, failed_ = false;
uint64_t ticket_ = 0, completed_ = 0;
bool frameChannel_ = false;
@@ -770,6 +883,7 @@ private:
uint32_t captureWidth_ = 0, captureHeight_ = 0;
std::atomic<uint64_t> submitted_{0};
std::thread thread_;
static inline std::atomic<Impl *> made_{nullptr};
X11 x_;
Display *dpy_ = nullptr;
@@ -798,6 +912,8 @@ private:
void Presenter::AddTextures(const uint64_t h[3], const int fds[3], uint32_t f, uint32_t w, uint32_t hh, uint32_t pid) { Impl::Get().Add(h, fds, f, w, hh, pid); }
void Presenter::RemoveTexture(uint64_t h) { Impl::Get().Remove(h); }
void Presenter::Submit(const EyeFrame &f) { Impl::Get().Submit(f); }
void Presenter::Shutdown() { Impl::Get().Shutdown(); }
void Presenter::Shutdown() {
if (Impl *impl = Impl::Made()) impl->Shutdown();
}
}
+21
View File
@@ -0,0 +1,21 @@
#ifndef DROIDDECK_RENDER_TARGET_H
#define DROIDDECK_RENDER_TARGET_H
#include <algorithm>
#include <cmath>
#include <cstdint>
namespace droiddeck {
struct RenderTargetSize { uint32_t width, height; };
inline RenderTargetSize RenderTarget(float aspect, uint32_t selectedHeight, uint32_t panelHeight) {
uint32_t height = selectedHeight > 0 ? std::min(selectedHeight, 8192u) : 720u;
if (panelHeight > 0) height = std::min(height, panelHeight);
if (!std::isfinite(aspect) || aspect <= 0.0f) aspect = 1.0f;
const float width = std::clamp(height * aspect, 1.0f, 8192.0f);
return {static_cast<uint32_t>(std::lround(width)), height};
}
}
#endif
+353 -10
View File
@@ -291,6 +291,7 @@ static VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice pd, const VkDeviceCreat
}
static void forget_device_semaphores(VkDevice dev);
static void track_forget_device(VkDevice dev);
static void producer_destroy_device(struct dev *device, const VkAllocationCallbacks *allocator);
static void VKAPI_CALL DestroyDevice(VkDevice device, const VkAllocationCallbacks *alloc) {
@@ -301,6 +302,7 @@ static void VKAPI_CALL DestroyDevice(VkDevice device, const VkAllocationCallback
#endif
if (d->producer) producer_destroy_device(d, alloc);
forget_device_semaphores(device);
track_forget_device(device);
PFN_vkDestroyDevice f = d->destroy;
pthread_mutex_lock(&g_lock);
memset(d, 0, sizeof(*d));
@@ -309,19 +311,23 @@ static void VKAPI_CALL DestroyDevice(VkDevice device, const VkAllocationCallback
}
#include <fcntl.h>
#include <stdatomic.h>
#include <time.h>
#include "../../../app/src/main/cpp/waylandcomp/src/headpose_shm.h"
#define FAKE_DISPLAY ((VkDisplayKHR)(uintptr_t)0xdd0001)
#define FAKE_MODE ((VkDisplayModeKHR)(uintptr_t)0xdd0002)
static VkSurfaceKHR g_hmd_surface;
static uint32_t g_mode_w = 1920, g_mode_h = 1080, g_mode_mhz = 60000;
static int g_mode_fixed;
static void mode_init(void) {
static int done;
if (done++) return;
const char *m = getenv("DROIDDECK_VR_DISPLAY_MODE");
unsigned w, h, hz;
if (m && sscanf(m, "%ux%u@%u", &w, &h, &hz) == 3 && w && h && hz) { g_mode_w = w; g_mode_h = h; g_mode_mhz = hz * 1000; }
if (m && sscanf(m, "%ux%u@%u", &w, &h, &hz) == 3 && w && h && hz) { g_mode_w = w; g_mode_h = h; g_mode_mhz = hz * 1000; g_mode_fixed = 1; }
}
static uint64_t now_ns(void) {
@@ -329,7 +335,57 @@ static uint64_t now_ns(void) {
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
}
static uint64_t frame_ns(void) { return 1000000000000ull / g_mode_mhz; }
static _Atomic(const struct droiddeck_headpose *) g_pose;
static _Atomic uint64_t g_pose_tried;
static const struct droiddeck_headpose *pose_map(void) {
const struct droiddeck_headpose *p = atomic_load(&g_pose);
if (p) return p;
uint64_t t = now_ns(), last = atomic_load(&g_pose_tried);
if (last && t - last < 1000000000ull) return NULL;
atomic_store(&g_pose_tried, t);
int fd = open("/run/droiddeck/" DROIDDECK_HEADPOSE_FILE, O_RDONLY | O_CLOEXEC);
if (fd < 0) return NULL;
void *m = mmap(NULL, sizeof(struct droiddeck_headpose), PROT_READ, MAP_SHARED, fd, 0);
close(fd);
if (m == MAP_FAILED) return NULL;
const struct droiddeck_headpose *expected = NULL;
if (!atomic_compare_exchange_strong(&g_pose, &expected, (const struct droiddeck_headpose *)m)) {
munmap(m, sizeof(struct droiddeck_headpose));
return expected;
}
return m;
}
static uint32_t mode_mhz(void) {
mode_init();
if (g_mode_fixed) return g_mode_mhz;
const struct droiddeck_headpose *p = pose_map();
if (!p || __atomic_load_n(&p->magic, __ATOMIC_RELAXED) != DROIDDECK_HEADPOSE_MAGIC) return g_mode_mhz;
uint32_t bits = __atomic_load_n((const uint32_t *)&p->display_hz, __ATOMIC_RELAXED);
float hz;
memcpy(&hz, &bits, sizeof(hz));
return hz >= 24.0f && hz <= 240.0f ? (uint32_t)(hz * 1000.0f + 0.5f) : g_mode_mhz;
}
static uint64_t frame_ns(void) { return 1000000000000ull / mode_mhz(); }
static pthread_mutex_t g_vblank_lock = PTHREAD_MUTEX_INITIALIZER;
static uint64_t g_vblank_base, g_vblank_at, g_vblank_frame;
static uint64_t vblank_count(void) {
uint64_t f = frame_ns();
pthread_mutex_lock(&g_vblank_lock);
uint64_t t = now_ns();
if (f != g_vblank_frame) {
g_vblank_base = g_vblank_frame ? g_vblank_base + (t - g_vblank_at) / g_vblank_frame : t / f;
g_vblank_at = t;
g_vblank_frame = f;
}
uint64_t n = g_vblank_base + (t - g_vblank_at) / f;
pthread_mutex_unlock(&g_vblank_lock);
return n;
}
static VkDisplayPropertiesKHR display_props(void) {
mode_init();
@@ -379,7 +435,7 @@ static VkResult VKAPI_CALL GetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice
}
static VkDisplayModePropertiesKHR mode_props(void) {
mode_init();
VkDisplayModePropertiesKHR m = {FAKE_MODE, {{g_mode_w, g_mode_h}, g_mode_mhz}};
VkDisplayModePropertiesKHR m = {FAKE_MODE, {{g_mode_w, g_mode_h}, mode_mhz()}};
return m;
}
static VkResult VKAPI_CALL GetDisplayModePropertiesKHR(VkPhysicalDevice pd, VkDisplayKHR d, uint32_t *count, VkDisplayModePropertiesKHR *p) {
@@ -467,7 +523,7 @@ static VkResult VKAPI_CALL RegisterDisplayEventEXT(VkDevice d, VkDisplayKHR disp
}
static VkResult VKAPI_CALL GetSwapchainCounterEXT(VkDevice d, VkSwapchainKHR sc, VkSurfaceCounterFlagBitsEXT counter, uint64_t *value) {
mode_init();
*value = now_ns() / frame_ns();
*value = vblank_count();
return VK_SUCCESS;
}
@@ -661,6 +717,9 @@ struct bridged {
int fd;
int timeline;
unsigned long ino;
uint64_t submitted;
uint64_t seen, seen_us;
int imported;
};
static pthread_mutex_t g_br_lock = PTHREAD_MUTEX_INITIALIZER;
static struct bridged g_br[MAX_SEMS];
@@ -705,6 +764,12 @@ static int sem_trace_on(void) {
return on;
}
static uint64_t now_us(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000000u + (uint64_t)ts.tv_nsec / 1000u;
}
static void *poller(void *arg) {
unsigned ticks = 0;
for (;;) {
@@ -722,10 +787,11 @@ static void *poller(void *arg) {
uint64_t local = 0;
if (!get || get(b->dev, b->sem, &local) != VK_SUCCESS) continue;
uint64_t shared = atomic_load(b->counter);
if (shared != b->seen) { b->seen = shared; b->seen_us = now_us(); }
if (trace) {
char line[192];
snprintf(line, sizeof(line), "%s sem %p %s counter %lu local %llu shared %llu", program_invocation_short_name,
(void *)b->sem, b->fd >= 0 ? "made here" : "imported", b->ino, (unsigned long long)local, (unsigned long long)shared);
(void *)b->sem, b->imported ? "imported" : "made here", b->ino, (unsigned long long)local, (unsigned long long)shared);
say("%s", line);
}
if (local > shared) {
@@ -808,7 +874,7 @@ static VkResult VKAPI_CALL CreateSemaphore(VkDevice d, const VkSemaphoreCreateIn
atomic_store(counter, timeline ? type_info->initialValue : 0);
pthread_mutex_lock(&g_br_lock);
struct bridged *slot = bridged_find(VK_NULL_HANDLE);
if (slot) *slot = (struct bridged){*out, d, counter, fd, timeline, fd_ino(fd)};
if (slot) *slot = (struct bridged){*out, d, counter, fd, timeline, fd_ino(fd), 0, atomic_load(counter), now_us(), 0};
pthread_mutex_unlock(&g_br_lock);
if (!slot) {
munmap((void *)counter, 4096);
@@ -869,7 +935,7 @@ static VkResult VKAPI_CALL ImportSemaphoreFdKHR(VkDevice d, const VkImportSemaph
struct bridged *b = bridged_find(info->semaphore);
if (b) bridged_drop(b);
b = bridged_find(VK_NULL_HANDLE);
if (b) *b = (struct bridged){info->semaphore, d, counter, info->fd, timeline, ino};
if (b) *b = (struct bridged){info->semaphore, d, counter, info->fd, timeline, ino, 0, atomic_load(counter), now_us(), 1};
pthread_mutex_unlock(&g_br_lock);
if (!b) {
munmap((void *)counter, 4096);
@@ -877,10 +943,98 @@ static VkResult VKAPI_CALL ImportSemaphoreFdKHR(VkDevice d, const VkImportSemaph
}
if (timeline) poller_start();
static int said;
if (said++ < 4) say("imported a shared timeline semaphore (%s): waits retained", program_invocation_short_name);
if (said++ < 4) say("imported a shared timeline semaphore (%s): bridged", program_invocation_short_name);
return VK_SUCCESS;
}
#define SUBMIT_GRACE_US 3000
#define SHARED_IDLE_US 100000
static int quest_frames(void) {
static _Atomic uint64_t checked_us;
static _Atomic int present;
const uint64_t t = now_us();
const uint64_t last = atomic_load(&checked_us);
if (!last || t - last > 1000000) {
atomic_store(&present, access("/run/droiddeck/droiddeck-xr-frames", F_OK) == 0);
atomic_store(&checked_us, t);
}
return atomic_load(&present);
}
static int submit_drop_wait(VkSemaphore s, uint64_t value) {
pthread_mutex_lock(&g_br_lock);
struct bridged *b = bridged_find(s);
if (!b || !b->timeline || !b->imported) { pthread_mutex_unlock(&g_br_lock); return 0; }
const uint64_t queued = b->submitted;
const uint64_t seen_us = b->seen_us;
VkDevice dev = b->dev;
_Atomic uint64_t *counter = b->counter;
pthread_mutex_unlock(&g_br_lock);
if (!value || value <= queued) return 0;
struct dev *d = dev_for(dev);
if (!d || quest_frames()) return 0;
PFN_vkGetSemaphoreCounterValue get = (PFN_vkGetSemaphoreCounterValue)d->gdpa(dev, "vkGetSemaphoreCounterValue");
if (!get) get = (PFN_vkGetSemaphoreCounterValue)d->gdpa(dev, "vkGetSemaphoreCounterValueKHR");
PFN_vkSignalSemaphore sig = (PFN_vkSignalSemaphore)d->gdpa(dev, "vkSignalSemaphore");
if (!sig) sig = (PFN_vkSignalSemaphore)d->gdpa(dev, "vkSignalSemaphoreKHR");
if (!get) return 0;
const uint64_t start = now_us();
const uint64_t until = start + (start - seen_us > SHARED_IDLE_US ? 0 : SUBMIT_GRACE_US);
for (;;) {
uint64_t local = 0;
if (get(dev, s, &local) == VK_SUCCESS && local >= value) return 0;
uint64_t shared = atomic_load(counter);
if (shared >= value && sig) {
VkSemaphoreSignalInfo si = {VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO, NULL, s, shared};
if (sig(dev, &si) == VK_SUCCESS) return 0;
}
if (now_us() >= until) break;
usleep(100);
}
static _Atomic int dropped;
if (atomic_fetch_add(&dropped, 1) == 0)
say("%s: dropping a submit's wait on a shared value nobody has signalled yet (Turnip would block the submit)",
program_invocation_short_name);
return 1;
}
static void note_signal(VkSemaphore s, uint64_t value) {
pthread_mutex_lock(&g_br_lock);
struct bridged *b = bridged_find(s);
if (b && b->timeline && value > b->submitted) b->submitted = value;
pthread_mutex_unlock(&g_br_lock);
}
static void note_submit_signals(uint32_t count, const VkSubmitInfo *submits) {
for (uint32_t i = 0; i < count; i++) {
const VkTimelineSemaphoreSubmitInfo *t = NULL;
for (const VkBaseInStructure *p = submits[i].pNext; p; p = p->pNext)
if (p->sType == VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO) t = (const void *)p;
for (uint32_t j = 0; t && j < submits[i].signalSemaphoreCount && j < t->signalSemaphoreValueCount; j++)
note_signal(submits[i].pSignalSemaphores[j], t->pSignalSemaphoreValues[j]);
}
}
static void note_submit2_signals(uint32_t count, const VkSubmitInfo2 *submits) {
for (uint32_t i = 0; i < count; i++)
for (uint32_t j = 0; j < submits[i].signalSemaphoreInfoCount; j++)
note_signal(submits[i].pSignalSemaphoreInfos[j].semaphore, submits[i].pSignalSemaphoreInfos[j].value);
}
static uint32_t keep_waits(const VkSemaphore *in, const VkPipelineStageFlags *stages, const uint64_t *values, uint32_t n,
VkSemaphore *out, VkPipelineStageFlags *out_stages, uint64_t *out_values) {
uint32_t k = 0;
for (uint32_t i = 0; i < n; i++) {
if (submit_drop_wait(in[i], values ? values[i] : 0)) continue;
out[k] = in[i];
if (stages) out_stages[k] = stages[i];
if (values) out_values[k] = values[i];
k++;
}
return k;
}
static void trace_sem(const char *what, VkSemaphore sem, uint64_t value) {
if (!sem_trace_on() || value > 3) return;
pthread_mutex_lock(&g_br_lock);
@@ -894,6 +1048,113 @@ static void trace_sem(const char *what, VkSemaphore sem, uint64_t value) {
say("%s", line);
}
#include <limits.h>
#include <linux/futex.h>
#include <sys/syscall.h>
#include "present_sync.h"
#include "fence_tracker.h"
#include <time.h>
static pthread_mutex_t g_trk_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t g_trk_cond = PTHREAD_COND_INITIALIZER;
static struct tracked *g_trk;
static struct droiddeck_present_sync *g_sync;
static pthread_once_t g_sync_once = PTHREAD_ONCE_INIT;
static void sync_wake(struct droiddeck_present_sync *s) {
syscall(SYS_futex, &s->done, FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
}
static void *completer(void *arg) {
struct droiddeck_present_sync *s = arg;
pthread_mutex_lock(&g_trk_lock);
for (;;) {
uint32_t done;
int pending = track_pool_reap(g_trk, __atomic_load_n(&s->issued, __ATOMIC_ACQUIRE), &done);
if (done != __atomic_load_n(&s->done, __ATOMIC_RELAXED)) {
__atomic_store_n(&s->done, done, __ATOMIC_RELEASE);
sync_wake(s);
}
pthread_cond_broadcast(&g_trk_cond);
if (pending) {
struct timespec until;
clock_gettime(CLOCK_REALTIME, &until);
until.tv_nsec += 1000000; /* Poll pending GPU work at most every 1 ms. */
if (until.tv_nsec >= 1000000000) { until.tv_sec++; until.tv_nsec -= 1000000000; }
pthread_cond_timedwait(&g_trk_cond, &g_trk_lock, &until);
} else {
pthread_cond_wait(&g_trk_cond, &g_trk_lock);
}
}
return NULL;
}
static void sync_init(void) {
if (strcmp(program_invocation_short_name, "vrcompositor")) return;
const char *rt = getenv("XDG_RUNTIME_DIR");
char path[512];
snprintf(path, sizeof(path), "%s/droiddeck-steamvr/%s", rt && *rt ? rt : "/run/droiddeck", DROIDDECK_PRESENT_SYNC_NAME);
int fd = open(path, O_RDWR | O_CREAT | O_CLOEXEC, 0600);
if (fd < 0) return;
void *m = ftruncate(fd, DROIDDECK_PRESENT_SYNC_SIZE) == 0
? mmap(NULL, DROIDDECK_PRESENT_SYNC_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0) : MAP_FAILED;
close(fd);
if (m == MAP_FAILED) return;
struct droiddeck_present_sync *s = m;
__atomic_store_n(&s->done, __atomic_load_n(&s->issued, __ATOMIC_ACQUIRE), __ATOMIC_RELEASE);
sync_wake(s);
pthread_t t;
if (pthread_create(&t, NULL, completer, s) != 0) { munmap(m, DROIDDECK_PRESENT_SYNC_SIZE); return; }
pthread_detach(t);
g_sync = s;
say("%s", "frames go to the presenter once their GPU work is done");
}
static struct tracked *track_take(VkQueue q) {
pthread_once(&g_sync_once, sync_init);
if (!g_sync) return NULL;
struct dev *d = dev_for(q);
if (!d) return NULL;
pthread_mutex_lock(&g_trk_lock);
struct tracked *t = track_pool_take(&g_trk, d->handle, d->gdpa);
pthread_mutex_unlock(&g_trk_lock);
return t;
}
static void track_submitted(struct tracked *t, VkResult r) {
pthread_mutex_lock(&g_trk_lock);
uint32_t issued = track_pool_submitted(t, r, __atomic_load_n(&g_sync->issued, __ATOMIC_RELAXED));
__atomic_store_n(&g_sync->issued, issued, __ATOMIC_RELEASE);
pthread_cond_broadcast(&g_trk_cond);
pthread_mutex_unlock(&g_trk_lock);
}
static void track_forget_device(VkDevice dev) {
pthread_mutex_lock(&g_trk_lock);
while (track_pool_device_busy(g_trk, dev))
pthread_cond_wait(&g_trk_cond, &g_trk_lock);
track_pool_forget(&g_trk, dev);
pthread_mutex_unlock(&g_trk_lock);
}
static VkResult submit_tracked(PFN_vkQueueSubmit next, VkQueue q, uint32_t count, const VkSubmitInfo *submits, VkFence fence) {
struct tracked *slot = track_take(q);
if (!slot) return next(q, count, submits, fence);
const VkFence mine = slot->fence;
VkResult r = next(q, count, submits, fence ? fence : mine);
track_submitted(slot, r == VK_SUCCESS && fence ? next(q, 0, NULL, mine) : r);
return r;
}
static VkResult submit2_tracked(PFN_vkQueueSubmit2 next, VkQueue q, uint32_t count, const VkSubmitInfo2 *submits, VkFence fence) {
struct tracked *slot = track_take(q);
if (!slot) return next(q, count, submits, fence);
const VkFence mine = slot->fence;
VkResult r = next(q, count, submits, fence ? fence : mine);
track_submitted(slot, r == VK_SUCCESS && fence ? next(q, 0, NULL, mine) : r);
return r;
}
static VkResult VKAPI_CALL QueueSubmit(VkQueue q, uint32_t count, const VkSubmitInfo *submits, VkFence fence) {
PFN_vkQueueSubmit next = (PFN_vkQueueSubmit)dev_for(q)->gdpa(dev_for(q)->handle, "vkQueueSubmit");
if (sem_trace_on())
@@ -906,10 +1167,61 @@ static VkResult VKAPI_CALL QueueSubmit(VkQueue q, uint32_t count, const VkSubmit
for (uint32_t j = 0; j < submits[i].signalSemaphoreCount; j++)
trace_sem("signals", submits[i].pSignalSemaphores[j], t && j < t->signalSemaphoreValueCount ? t->pSignalSemaphoreValues[j] : 0);
}
int any = 0;
for (uint32_t i = 0; i < count && !any; i++) {
const VkTimelineSemaphoreSubmitInfo *t = NULL;
for (const VkBaseInStructure *p = submits[i].pNext; p; p = p->pNext)
if (p->sType == VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO) t = (const void *)p;
for (uint32_t j = 0; submits[i].pWaitSemaphores && j < submits[i].waitSemaphoreCount && !any; j++)
any = submit_drop_wait(submits[i].pWaitSemaphores[j], t && j < t->waitSemaphoreValueCount ? t->pWaitSemaphoreValues[j] : 0);
}
VkSubmitInfo *copy = NULL;
VkTimelineSemaphoreSubmitInfo *tl = NULL;
void **bufs = NULL;
if (any) {
copy = calloc(count, sizeof(*copy));
tl = calloc(count, sizeof(*tl));
bufs = calloc(count * 3, sizeof(void *));
if (!copy || !tl || !bufs) { free(copy); free(tl); free(bufs); return VK_ERROR_OUT_OF_HOST_MEMORY; }
for (uint32_t i = 0; i < count; i++) {
copy[i] = submits[i];
uint32_t n = submits[i].waitSemaphoreCount;
if (!n) continue;
VkSemaphore *sems = calloc(n, sizeof(*sems));
VkPipelineStageFlags *stages = calloc(n, sizeof(*stages));
uint64_t *vals = calloc(n, sizeof(*vals));
bufs[i * 3] = sems; bufs[i * 3 + 1] = stages; bufs[i * 3 + 2] = vals;
if (!sems || !stages || !vals) {
for (uint32_t k = 0; k < count * 3; k++) free(bufs[k]);
free(bufs); free(tl); free(copy);
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
const VkTimelineSemaphoreSubmitInfo *t = NULL;
for (const VkBaseInStructure *p = submits[i].pNext; p; p = p->pNext)
if (p->sType == VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO) t = (const void *)p;
uint32_t k = keep_waits(submits[i].pWaitSemaphores, submits[i].pWaitDstStageMask,
t && t->waitSemaphoreValueCount ? t->pWaitSemaphoreValues : NULL, n, sems, stages, vals);
copy[i].waitSemaphoreCount = k;
copy[i].pWaitSemaphores = sems;
copy[i].pWaitDstStageMask = stages;
if (t) {
tl[i] = *t;
tl[i].waitSemaphoreValueCount = t->waitSemaphoreValueCount ? k : 0;
tl[i].pWaitSemaphoreValues = vals;
tl[i].pNext = t->pNext;
copy[i].pNext = &tl[i];
}
}
}
struct dev *device = dev_for(q);
if (device->producer) { pthread_mutex_lock(&g_submission_lock); producer_record(device, q); }
VkResult result = next(q, count, submits, fence);
VkResult result = submit_tracked(next, q, count, any ? copy : submits, fence);
if (device->producer) pthread_mutex_unlock(&g_submission_lock);
note_submit_signals(count, submits);
if (any) {
for (uint32_t i = 0; i < count * 3; i++) free(bufs[i]);
free(bufs); free(tl); free(copy);
}
return result;
}
@@ -923,10 +1235,41 @@ static VkResult VKAPI_CALL QueueSubmit2(VkQueue q, uint32_t count, const VkSubmi
for (uint32_t j = 0; j < submits[i].signalSemaphoreInfoCount; j++)
trace_sem("signals", submits[i].pSignalSemaphoreInfos[j].semaphore, submits[i].pSignalSemaphoreInfos[j].value);
}
int any = 0;
for (uint32_t i = 0; i < count && !any; i++)
for (uint32_t j = 0; submits[i].pWaitSemaphoreInfos && j < submits[i].waitSemaphoreInfoCount && !any; j++)
any = submit_drop_wait(submits[i].pWaitSemaphoreInfos[j].semaphore, submits[i].pWaitSemaphoreInfos[j].value);
VkSubmitInfo2 *copy = NULL;
VkSemaphoreSubmitInfo **waits = NULL;
if (any) {
copy = calloc(count, sizeof(*copy));
waits = calloc(count, sizeof(*waits));
if (!copy || !waits) { free(copy); free(waits); return VK_ERROR_OUT_OF_HOST_MEMORY; }
for (uint32_t i = 0; i < count; i++) {
copy[i] = submits[i];
uint32_t n = submits[i].waitSemaphoreInfoCount, k = 0;
waits[i] = calloc(n + 1, sizeof(VkSemaphoreSubmitInfo));
if (!waits[i]) {
for (uint32_t j = 0; j < i; j++) free(waits[j]);
free(waits); free(copy);
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
for (uint32_t j = 0; j < n; j++)
if (!submit_drop_wait(submits[i].pWaitSemaphoreInfos[j].semaphore, submits[i].pWaitSemaphoreInfos[j].value))
waits[i][k++] = submits[i].pWaitSemaphoreInfos[j];
copy[i].waitSemaphoreInfoCount = k;
copy[i].pWaitSemaphoreInfos = waits[i];
}
}
struct dev *device = dev_for(q);
if (device->producer) { pthread_mutex_lock(&g_submission_lock); producer_record(device, q); }
VkResult result = next(q, count, submits, fence);
VkResult result = submit2_tracked(next, q, count, any ? copy : submits, fence);
if (device->producer) pthread_mutex_unlock(&g_submission_lock);
note_submit2_signals(count, submits);
if (any) {
for (uint32_t i = 0; i < count; i++) free(waits[i]);
free(waits); free(copy);
}
return result;
}
+146
View File
@@ -0,0 +1,146 @@
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "fence_tracker.h"
struct fake_fence { VkDevice dev; VkResult result; unsigned waits; };
static unsigned created, destroyed;
static int fail_create;
static struct fake_fence *fake(VkFence fence) { return (struct fake_fence *)(uintptr_t)fence; }
static VkResult VKAPI_CALL create_fence(VkDevice dev, const VkFenceCreateInfo *info,
const VkAllocationCallbacks *alloc, VkFence *out) {
(void)alloc;
assert(info->flags == 0);
if (fail_create) return VK_ERROR_OUT_OF_HOST_MEMORY;
struct fake_fence *f = calloc(1, sizeof(*f));
assert(f);
f->dev = dev;
f->result = VK_TIMEOUT;
*out = (VkFence)(uintptr_t)f;
created++;
return VK_SUCCESS;
}
static VkResult VKAPI_CALL wait_fence(VkDevice dev, uint32_t count, const VkFence *fences,
VkBool32 all, uint64_t timeout) {
assert(count == 1 && all && timeout == 0);
struct fake_fence *f = fake(*fences);
assert(f->dev == dev);
f->waits++;
return f->result;
}
static VkResult VKAPI_CALL reset_fence(VkDevice dev, uint32_t count, const VkFence *fences) {
assert(count == 1 && fake(*fences)->dev == dev);
fake(*fences)->result = VK_TIMEOUT;
return VK_SUCCESS;
}
static void VKAPI_CALL destroy_fence(VkDevice dev, VkFence fence, const VkAllocationCallbacks *alloc) {
(void)alloc;
assert(fake(fence)->dev == dev);
free(fake(fence));
destroyed++;
}
static PFN_vkVoidFunction VKAPI_CALL gdpa(VkDevice dev, const char *name) {
(void)dev;
if (!strcmp(name, "vkCreateFence")) return (PFN_vkVoidFunction)create_fence;
if (!strcmp(name, "vkWaitForFences")) return (PFN_vkVoidFunction)wait_fence;
if (!strcmp(name, "vkResetFences")) return (PFN_vkVoidFunction)reset_fence;
if (!strcmp(name, "vkDestroyFence")) return (PFN_vkVoidFunction)destroy_fence;
assert(0);
return NULL;
}
static struct tracked *submit(struct tracked **pool, VkDevice dev, uint32_t *issued) {
struct tracked *t = track_pool_take(pool, dev, gdpa);
assert(t && t->state == TRACK_RESERVED);
*issued = track_pool_submitted(t, VK_SUCCESS, *issued);
return t;
}
static void saturated_pool_and_out_of_order_completion(void) {
struct tracked *pool = NULL, *pending[130];
VkDevice dev = (VkDevice)(uintptr_t)1;
uint32_t issued = 0, done = 0;
/* These waits can all depend on a signal from a later queue submission.
* A fixed 64-slot pool blocks that signal before it reaches the driver. */
for (unsigned i = 0; i < 130; i++) pending[i] = submit(&pool, dev, &issued);
assert(issued == 130);
assert(track_pool_reap(pool, issued, &done) && done == 0);
for (unsigned i = 0; i < 130; i++) assert(fake(pending[i]->fence)->waits == 1);
fake(pending[129]->fence)->result = VK_SUCCESS;
assert(track_pool_reap(pool, issued, &done) && done == 0);
assert(pending[129]->state == TRACK_IDLE);
unsigned before = created;
struct tracked *signal = submit(&pool, dev, &issued);
assert(signal == pending[129] && created == before && issued == 131);
/* Freeing the latest fence must not mark the older work as drawn. */
for (unsigned i = 0; i < 129; i++) fake(pending[i]->fence)->result = VK_SUCCESS;
assert(track_pool_reap(pool, issued, &done) && done == 130);
fake(signal->fence)->result = VK_SUCCESS;
assert(!track_pool_reap(pool, issued, &done) && done == 131);
assert(!track_pool_device_busy(pool, dev));
track_pool_forget(&pool, dev);
assert(!pool);
}
static void wrapped_watermark(void) {
struct tracked *pool = NULL;
VkDevice dev = (VkDevice)(uintptr_t)1;
uint32_t issued = UINT32_MAX - 1, done = issued;
struct tracked *first = submit(&pool, dev, &issued);
struct tracked *second = submit(&pool, dev, &issued);
struct tracked *third = submit(&pool, dev, &issued);
assert(issued == 1);
fake(second->fence)->result = VK_SUCCESS;
fake(third->fence)->result = VK_SUCCESS;
assert(track_pool_reap(pool, issued, &done) && done == UINT32_MAX - 1);
fake(first->fence)->result = VK_SUCCESS;
assert(!track_pool_reap(pool, issued, &done) && done == 1);
track_pool_forget(&pool, dev);
}
static void reservations_failures_and_devices(void) {
struct tracked *pool = NULL;
VkDevice a = (VkDevice)(uintptr_t)1, b = (VkDevice)(uintptr_t)2;
uint32_t issued = 7, done = 7;
struct tracked *reserved = track_pool_take(&pool, a, gdpa);
assert(track_pool_device_busy(pool, a));
assert(!track_pool_reap(pool, issued, &done) && done == 7);
assert(fake(reserved->fence)->waits == 0);
issued = track_pool_submitted(reserved, VK_ERROR_DEVICE_LOST, issued);
assert(issued == 7 && !track_pool_device_busy(pool, a));
fail_create = 1;
assert(!track_pool_take(&pool, b, gdpa));
assert(pool == reserved && pool->dev == a);
fail_create = 0;
unsigned before = destroyed;
struct tracked *other = submit(&pool, b, &issued);
assert(other == reserved && destroyed == before + 1);
struct tracked *own = submit(&pool, a, &issued);
fake(other->fence)->result = VK_SUCCESS;
assert(track_pool_reap(pool, issued, &done) && done == 8);
track_pool_forget(&pool, b);
assert(pool == own && track_pool_device_busy(pool, a));
fake(own->fence)->result = VK_ERROR_DEVICE_LOST;
assert(!track_pool_reap(pool, issued, &done) && done == 9);
track_pool_forget(&pool, a);
assert(!pool);
fail_create = 1;
assert(!track_pool_take(&pool, a, gdpa) && !pool);
fail_create = 0;
}
int main(void) {
saturated_pool_and_out_of_order_completion();
wrapped_watermark();
reservations_failures_and_devices();
assert(created == destroyed);
puts("fence tracker: all regression scenarios passed");
return 0;
}
+549
View File
@@ -0,0 +1,549 @@
#include "arm_model.h"
#include "hand_motion.h"
#include "hand_mesh.h"
#include "hand_pose.h"
#include "hand_skeleton.h"
#include "index_pose.h"
#include "handcam_shm.h"
#include "joycon_shm.h"
#include <cstdio>
#include <cstdlib>
#include <map>
using namespace droiddeck;
static int failures = 0;
static void check(bool ok, const char *what) {
if (ok) return;
fprintf(stderr, "FAILED: %s\n", what);
failures++;
}
static bool near(double a, double b, double tolerance = 1e-6) { return std::fabs(a - b) <= tolerance; }
static Vec3 Forward(const Quat &q) { return Rotate(q, {0.0, 0.0, -1.0}); }
static void restingHandsHangInFrontAtElbowHeight() {
const Vec3 head{0.0, 1.6, 0.0};
ArmModel right(true), left(false);
const Vec3 r = right.Update(head, {}, {}, 0.0, 0.01);
const Vec3 l = left.Update(head, {}, {}, 0.0, 0.01);
check(near(r.x, 0.195, 1e-9) && near(l.x, -0.195, 1e-9), "rest hands sit 19.5 cm either side");
check(near(r.y, 1.1, 1e-9) && near(l.y, 1.1, 1e-9), "rest hands hang 50 cm below the eyes");
check(near(r.z, -0.305, 1e-9) && near(l.z, -0.305, 1e-9), "rest hands reach 30.5 cm forward");
}
static void raisingTheControllerExtendsTheArm() {
const Vec3 head{0.0, 1.6, 0.0};
ArmModel rest(true), raised(true);
const Vec3 low = rest.Update(head, {}, {}, 0.0, 0.01);
const Vec3 high = raised.Update(head, {}, AboutX(60.0 * M_PI / 180.0), 0.0, 0.01);
check(high.y > low.y + 0.2, "pointing up lifts the hand");
check(high.x < low.x, "an extended arm moves the elbow inward");
}
static void theTorsoOnlyFollowsTheHeadWhileTheControllerMoves() {
const Vec3 head{0.0, 1.6, 0.0};
ArmModel arm(true);
arm.Update(head, {}, {}, 0.0, 0.01);
const Quat lookLeft = AboutY(M_PI / 2);
const Vec3 still = arm.Update(head, lookLeft, {}, 0.0, 0.5);
check(near(still.x, 0.195, 1e-9), "a still controller keeps the torso where it was");
for (int i = 0; i < 400; i++) arm.Update(head, lookLeft, lookLeft, 5.0, 0.01);
const Vec3 turned = arm.Update(head, lookLeft, lookLeft, 0.0, 0.01);
check(near(turned.z, -0.195, 1e-3) && near(turned.x, -0.305, 1e-3), "a moving controller lets the torso turn to the gaze");
arm.Reset();
const Vec3 snapped = arm.Update(head, {}, {}, 0.0, 0.01);
check(near(snapped.x, 0.195, 1e-9), "a reset snaps the torso to the gaze");
}
static void headingAlignmentTurnsTheControllerToTheGaze() {
double heading = 0.0;
check(Heading(AboutY(0.7), &heading) && near(heading, 0.7), "heading of a yaw rotation");
check(!Heading(AboutX(M_PI / 2), &heading), "a controller pointing straight up has no heading");
const Quat hand = AboutY(-1.1) * AboutX(0.3);
const Quat head = AboutY(0.4) * AboutX(-0.2);
double headYaw = 0.0, handYaw = 0.0;
check(Heading(head, &headYaw) && Heading(hand, &handYaw), "both headings exist");
const Quat aligned = AboutY(headYaw - handYaw) * hand;
check(Heading(aligned, &heading) && near(heading, 0.4), "the aligned controller faces where the head faces");
check(near(Forward(aligned).y, Forward(hand).y), "alignment keeps the controller's pitch");
}
static void theIndexTipPointsWhereTheJoyConPoints() {
for (bool right : {false, true}) {
const Quat aim = AboutY(0.5) * AboutX(-0.3);
const Vec3 grip{0.2, 1.1, -0.3};
Quat raw;
Vec3 position;
IndexRawPose(right, aim, grip, &raw, &position);
const Vec3 tip = Forward(raw * IndexTipRotation(right)), want = Forward(aim);
check(near(tip.x, want.x) && near(tip.y, want.y) && near(tip.z, want.z), "the tip pose follows the Joy-Con");
const Vec3 gripAt = position + Rotate(raw, {0.0, -0.015, 0.13});
check(near(gripAt.x, grip.x) && near(gripAt.y, grip.y) && near(gripAt.z, grip.z), "the Index grip sits at the arm model's hand");
const Vec3 handle = Forward(raw);
check(handle.y > Forward(aim).y + 0.4, "the raw handle tilts up from the pointing direction");
}
}
static void skeletonsAreFiniteMirroredAndNormalised() {
vr::VRBoneTransform_t left[kHandBones], right[kHandBones];
for (double curl : {0.0, 0.5, 1.0}) {
FingerCurls curls;
curls.thumb = curls.index = curls.middle = curls.ring = curls.pinky = curl;
HandSkeleton(false, curls, left);
HandSkeleton(true, curls, right);
for (uint32_t b = 0; b < kHandBones; b++) {
for (const auto *bones : {left, right}) {
const auto &q = bones[b].orientation;
check(near(std::sqrt(q.w * q.w + q.x * q.x + q.y * q.y + q.z * q.z), 1.0, 1e-3), "bone orientations are unit quaternions");
}
for (int i = 0; i < 3; i++) check(std::isfinite(left[b].position.v[i]) && std::isfinite(right[b].position.v[i]), "bone positions are finite");
check(left[b].position.v[3] == 1.0f && right[b].position.v[3] == 1.0f, "bone positions are points");
}
check(near(left[1].position.v[0], -right[1].position.v[0]), "the wrist is mirrored between hands");
check(near(left[7].position.v[0], -right[7].position.v[0]), "finger bones are mirrored between hands");
}
FingerCurls open, fist;
fist.index = 1.0;
HandSkeleton(false, open, left);
HandSkeleton(false, fist, right);
const auto reach = [](const vr::VRBoneTransform_t *bones) {
return std::hypot(bones[27].position.v[0] - bones[1].position.v[0], bones[27].position.v[1] - bones[1].position.v[1],
bones[27].position.v[2] - bones[1].position.v[2]);
};
check(reach(right) < reach(left) - 0.05, "a curled index finger pulls its tip towards the wrist");
}
static void measuredMovementCarriesTheHandAndTheAnchorPullsItBack() {
HandFollower hand;
const Vec3 anchor{0.2, 1.1, -0.3};
const Vec3 start = hand.Update(anchor, {5.0, 5.0, 5.0}, 0.01, 0.6, 0.35);
check(near(start.x, anchor.x) && near(start.y, anchor.y) && near(start.z, anchor.z), "the hand starts at the anchor");
const Vec3 lifted = hand.Update(anchor, {5.0, 5.25, 5.0}, 0.0, 0.6, 0.35);
check(near(lifted.y, 1.35) && near(lifted.x, 0.2) && near(lifted.z, -0.3), "a measured 25 cm lift raises the hand 25 cm");
Vec3 held = lifted;
for (int i = 0; i < 60; i++) held = hand.Update(anchor, {5.0, 5.25, 5.0}, 0.01, 0.6, 0.35);
check(near(held.y, 1.1 + 0.25 * std::exp(-1.0), 1e-3), "a held hand settles back over the anchor's time constant");
hand.Update(anchor, {5.0, 6.25, 5.0}, 0.0, 0.6, 0.35);
const Vec3 far = hand.Update(anchor, {5.0, 6.25, 5.0}, 0.0, 0.6, 0.35);
check(near(Length(far - anchor), 0.35, 1e-9), "the hand stays within reach of the anchor");
hand.Reset();
const Vec3 reset = hand.Update({0.0, 1.0, 0.0}, {9.0, 9.0, 9.0}, 0.01, 0.6, 0.35);
check(near(reset.x, 0.0) && near(reset.y, 1.0) && near(reset.z, 0.0), "a reset puts the hand back on the anchor");
const Vec3 after = hand.Update({0.0, 1.0, 0.0}, {9.0, 9.0, 8.9}, 0.0, 0.6, 0.35);
check(near(after.z, -0.1), "movement after a reset counts from the reset");
}
static void feedJoyCon(HandKalman &k, double from, double to, double (*pos)(double), double bias, double turn) {
const double dt = 0.01;
for (double t = from + dt; t < to + 1e-9; t += dt) {
const double v = (pos(t + 1e-4) - pos(t - 1e-4)) / 2e-4;
k.Input(int64_t(t * 1e9), {0.0, v + bias * t, 0.0}, turn);
}
}
static double wave(double t) { return 1.0 + 0.2 * std::sin(2.0 * M_PI * 0.7 * t); }
static void theFilterPlacesTheHandWhereItIsNowNotWhenThePictureWasTaken() {
HandKalman k;
const int64_t ms = 1000000;
k.Input(0, {0.0, 0.0, 0.0}, 1.0);
check(!k.Measure(-5 * ms, {0.0, 1.0, 0.0}, 0.02), "a sighting from before the Joy-Con started is skipped");
double t = 0.0;
double worst = 0.0, squares = 0.0, pictureSquares = 0.0;
int counted = 0;
unsigned noise = 12345;
for (int frame = 0; frame < 150; frame++) {
feedJoyCon(k, t, t + 1.0 / 15.0, wave, 0.3, 1.0);
t += 1.0 / 15.0;
const double seen = t - 0.1;
noise = noise * 1103515245u + 12345u;
const double jitter = (int((noise >> 16) % 2001) - 1000) / 1000.0 * 0.02;
k.Measure(int64_t(seen * 1e9), {0.3, wave(seen) + jitter, -0.4}, 0.03);
if (frame > 45) {
const double error = k.Position().y - wave(t);
const double picture = wave(seen) + jitter - wave(t);
worst = std::max(worst, std::fabs(error));
squares += error * error;
pictureSquares += picture * picture;
counted++;
}
}
check(k.Ready(), "the filter runs once the camera has seen the hand");
check(std::sqrt(squares / counted) < 0.55 * std::sqrt(pictureSquares / counted) && worst < 0.07,
"a waving hand is shown nearer where it is now than the 0.1 s late, noisy picture, despite a drifting Joy-Con");
check(near(k.Position().x, 0.3, 0.01) && near(k.Position().z, -0.4, 0.01), "still axes stay on the camera");
}
static void aLateSightingCorrectsThePastAndKeepsTheMoveSince() {
HandKalman k;
const int64_t ms = 1000000;
for (int i = 0; i <= 20; i++) k.Input(i * 10 * ms, {0.0, 0.0, 0.0}, 1.0);
check(k.Measure(200 * ms, {0.0, 1.0, 0.0}, 0.01), "the first sighting starts the filter");
for (int i = 0; i < 30; i++) k.Measure((200 + i) * ms, {0.0, 1.0, 0.0}, 0.01);
for (int i = 21; i <= 30; i++) k.Input(i * 10 * ms, {0.0, 1.0, 0.0}, 1.0);
const double moved = k.Position().y - 1.0;
check(moved > 0.08, "a quick Joy-Con lift moves the hand at once");
k.Measure(205 * ms, {0.0, 1.0, 0.0}, 0.01);
check(near(k.Position().y - 1.0, moved, 0.03), "a late sighting from before the lift does not undo it");
}
static void aWildSightingIsIgnoredUntilItRepeats() {
HandKalman k;
const int64_t ms = 1000000;
for (int i = 0; i <= 60; i++) {
k.Input(i * 10 * ms, {0.0, 0.0, 0.0}, 1.0);
if (i % 7 == 0) k.Measure(i * 10 * ms, {0.2, 1.1, -0.3}, 0.02);
}
check(!k.Measure(600 * ms, {-0.4, 1.1, -0.3}, 0.02), "one sighting far from the hand is ignored");
check(!k.Measure(600 * ms, {-0.4, 1.1, -0.3}, 0.02), "a second one is ignored too");
check(near(k.Position().x, 0.2, 0.02), "the hand stays where it was");
check(k.Measure(600 * ms, {-0.4, 1.1, -0.3}, 0.02) && near(k.Position().x, -0.4, 0.03), "a third sighting in a row moves the hand there");
k.Input(2000 * ms, {0.0, 0.0, 0.0}, 1.0);
check(!k.Ready(), "a long gap in Joy-Con reports starts over");
}
static double slide(double t) { return t < 1.0 ? 0.5 * t : 0.5; }
static double flat(double) { return 0.0; }
static double lostAfterSlide(double turn, double seconds, double *speed) {
HandKalman k;
k.Input(0, {0.0, 0.0, 0.0}, 1.0);
double t = 0.0;
while (t < 1.0 - 1e-9) {
feedJoyCon(k, t, t + 1.0 / 15.0, flat, 0.0, 1.0);
t += 1.0 / 15.0;
k.Measure(int64_t(t * 1e9), {slide(t), 1.0, -0.4}, 0.03);
}
feedJoyCon(k, t, t + seconds, flat, 0.0, turn);
*speed = Length(k.Velocity());
return k.Position().x - slide(t);
}
static void aHandTheCameraLosesSettlesInsteadOfRunningAway() {
double speed = 0.0;
const double moving = lostAfterSlide(1.0, 2.0, &speed);
check(moving > 0.0 && moving < 0.1 && speed < 0.01,
"a hand the Joy-Con never felt moving settles within 10 cm of its last sighting instead of sliding on");
const double still = lostAfterSlide(0.0, 0.5, &speed);
check(still > -0.01 && still < 0.06 && speed < 0.01, "a still Joy-Con stops the hand within a few cm");
}
static void posed(const HandModel &model, const FingerCurls &curls, const Quat &turn, const Vec3 &at, Vec3 out[kHandJoints]) {
Vec3 joints[kHandJoints];
HandJoints(model.Right(), curls, joints);
for (int i = 0; i < kHandJoints; i++) out[i] = at + Rotate(turn, joints[i] - model.PalmCenter());
}
static void cameraFingerCurlsReadBackFromTheJoints() {
for (bool right : {false, true}) {
const HandModel model(right);
for (double c : {0.0, 0.2, 0.5, 0.8, 1.0}) {
FingerCurls curls;
curls.thumb = c;
curls.index = 1.0 - c;
curls.middle = curls.ring = curls.pinky = c;
Vec3 joints[kHandJoints];
posed(model, curls, Normalize(AboutY(0.7) * AboutX(-0.4)), {0.1, 1.2, -0.4}, joints);
const FingerCurls read = model.Curls(joints);
check(near(read.thumb, curls.thumb, 0.02) && near(read.index, curls.index, 0.02) && near(read.middle, c, 0.02) &&
near(read.ring, c, 0.02) && near(read.pinky, c, 0.02),
"finger curls read from camera joints match the curls that made them");
}
}
}
static void thePalmFitFindsTheHandsTurnAndPlace() {
for (bool right : {false, true}) {
const HandModel model(right);
const Quat turn = Normalize(AboutY(-2.1) * AboutX(0.9) * Quat{0.9, 0.0, 0.0, 0.43});
FingerCurls curls;
curls.index = 0.6;
curls.middle = curls.ring = curls.pinky = 0.9;
Vec3 joints[kHandJoints];
posed(model, curls, turn, {-0.2, 1.1, -0.35}, joints);
Quat fitted;
Vec3 center;
double rms = 1.0;
check(model.Fit(joints, model.Curls(joints), &fitted, &center, &rms), "the palm fit converges");
check(AngleDegrees(Conjugate(turn) * fitted) < 0.5 && rms < 0.001, "the palm fit recovers the hand's turn even with bent fingers");
Vec3 shape[kHandJoints];
HandJoints(right, curls, shape);
check(Length(Rotate(fitted, shape[hand::kIndexTip] - center) - (joints[hand::kIndexTip] - (joints[0] + joints[5] + joints[9] + joints[13] + joints[17]) * 0.2)) < 0.002,
"the fitted skeleton puts the fingertips where the camera saw them");
unsigned noise = 99;
for (int i = 0; i < kHandJoints; i++) {
noise = noise * 1103515245u + 12345u;
const double n = (int((noise >> 16) % 2001) - 1000) / 1000.0 * 0.004;
joints[i] = joints[i] + Vec3{n, -n, n * 0.5};
}
check(model.Fit(joints, model.Curls(joints), &fitted, &center, &rms) && AngleDegrees(Conjugate(turn) * fitted) < 6.0,
"4 mm of landmark noise moves the fit a few degrees");
}
}
static void thePalmNormalPointsOutOfThePalm() {
for (bool right : {false, true}) {
const HandModel model(right);
Vec3 open[kHandJoints], fist[kHandJoints];
FingerCurls closed;
closed.index = closed.middle = closed.ring = closed.pinky = 1.0;
posed(model, FingerCurls{}, Quat{}, {}, open);
posed(model, closed, Quat{}, {}, fist);
const Vec3 normal = model.PalmNormal(open);
check(near(Length(normal), 1.0, 1e-6) && Dot(normal, fist[hand::kIndexTip] - open[hand::kIndexTip]) > 0.0,
"the palm normal points the way the fingers close");
}
}
static void pinchesAndFistsBecomeButtons() {
const HandModel model(true);
HandGestures gestures(true);
const int64_t ms = 1000000;
Vec3 joints[kHandJoints];
posed(model, FingerCurls{}, Quat{}, {0.2, 1.2, -0.4}, joints);
const Vec3 palm{0.2, 1.2, -0.4};
const Vec3 normal = model.PalmNormal(joints);
const Vec3 away = palm - normal * 0.5;
HandButtons b = gestures.Update(model, joints, model.Curls(joints), palm, away, 0);
check(!b.triggerClick && !b.gripClick && !b.system && b.trigger == 0.0f, "an open hand presses nothing");
joints[hand::kThumbTip] = joints[hand::kIndexTip] + Vec3{0.01, 0.0, 0.0};
b = gestures.Update(model, joints, model.Curls(joints), palm, away, 10 * ms);
check(!b.triggerClick && b.trigger > 0.9f, "a pinch pulls the trigger at once but clicks only after it holds");
b = gestures.Update(model, joints, model.Curls(joints), palm, away, 70 * ms);
check(b.triggerClick && !b.system, "a held pinch clicks the trigger");
joints[hand::kThumbTip] = joints[hand::kIndexTip] + Vec3{0.04, 0.0, 0.0};
b = gestures.Update(model, joints, model.Curls(joints), palm, away, 90 * ms);
check(b.triggerClick, "a pinch that loosens a little stays clicked");
joints[hand::kThumbTip] = joints[hand::kIndexTip] + Vec3{0.08, 0.0, 0.0};
gestures.Update(model, joints, model.Curls(joints), palm, away, 100 * ms);
b = gestures.Update(model, joints, model.Curls(joints), palm, away, 210 * ms);
check(!b.triggerClick && b.trigger == 0.0f, "opening the pinch releases the trigger");
joints[hand::kThumbTip] = joints[hand::kIndexTip] + Vec3{0.0, 0.03, 0.0};
gestures.Update(model, joints, model.Curls(joints), palm, away, 300 * ms);
b = gestures.Update(model, joints, model.Curls(joints), palm, away, 360 * ms);
check(b.triggerClick && b.trigger == 1.0f, "fingertips the camera reads 3 cm apart still pinch");
HandGestures fist(true);
FingerCurls closed;
closed.middle = closed.ring = closed.pinky = 1.0;
posed(model, closed, Quat{}, palm, joints);
fist.Update(model, joints, model.Curls(joints), palm, away, 0);
b = fist.Update(model, joints, model.Curls(joints), palm, away, 60 * ms);
check(b.gripClick && b.grip == 1.0f && !b.a && !b.b, "a fist grips without pressing A or B");
}
static void aPalmTurnedToTheFaceOpensTheMenus() {
const HandModel rightModel(true), leftModel(false);
HandGestures right(true), left(false);
const int64_t ms = 1000000;
const Vec3 palm{0.0, 1.3, -0.35};
for (int side = 0; side < 2; side++) {
const HandModel &model = side ? rightModel : leftModel;
HandGestures &gestures = side ? right : left;
Vec3 joints[kHandJoints];
posed(model, FingerCurls{}, Quat{}, palm, joints);
const Vec3 face = palm + model.PalmNormal(joints) * 0.4;
HandButtons b = gestures.Update(model, joints, model.Curls(joints), palm, face, 0);
check(!b.facing, "a palm must face the headset for a moment before it counts");
joints[hand::kThumbTip] = joints[hand::kIndexTip];
gestures.Update(model, joints, model.Curls(joints), palm, face, 310 * ms);
b = gestures.Update(model, joints, model.Curls(joints), palm, face, 380 * ms);
check(b.facing && !b.triggerClick && b.trigger == 0.0f, "a pinch with the palm facing the headset is not a trigger pull");
check(side ? (b.system && !b.b) : (b.b && !b.system),
side ? "a right palm pinch presses the system button (SteamVR menu)" : "a left palm pinch presses B (game menu)");
}
}
static void aBareHandFollowsTheCameraAndHidesWhenLost() {
const int64_t ms = 1000000;
const Vec3 head{0.0, 1.6, 0.0};
for (int side = 0; side < 2; side++) {
CameraHand hand(side == 1);
const Quat turn = Normalize(AboutY(side ? -0.4 : 0.4) * AboutX(-0.3));
FingerCurls curls;
curls.index = 0.2;
curls.middle = curls.ring = curls.pinky = 0.4;
Vec3 shape[kHandJoints], mean, seen[kHandJoints];
HandJoints(side == 1, curls, shape);
for (int i : hand::kPalm) mean = mean + shape[i] * 0.2;
for (int i = 0; i < kHandJoints; i++) seen[i] = Rotate(turn, shape[i] - mean);
Vec3 palm{side ? 0.15 : -0.15, 1.2, -0.4};
CameraHandPose pose;
int64_t t = 0;
const auto run = [&](int64_t until, bool camera) {
for (; t <= until; t += 10 * ms) {
if (camera && t % (70 * ms) == 0) hand.See(t - 90 * ms, palm, seen, head, t);
pose = hand.Update(t, 0.01);
}
};
run(90 * ms, false);
check(!pose.visible, "a camera hand stays hidden until the camera sees it");
run(1000 * ms, true);
check(pose.visible && AngleDegrees(Conjugate(turn) * pose.rotation) < 1.0, "a camera hand turns the way the camera saw it");
check(Length(pose.position - (palm - Rotate(turn, mean))) < 0.01, "a camera hand's skeleton root sits so its palm lands on the camera's palm");
check(std::fabs(pose.curls.index - 0.2) < 0.05 && std::fabs(pose.curls.ring - 0.4) < 0.05, "a camera hand bends its fingers as seen");
check(!pose.buttons.triggerClick, "a loose hand pulls nothing");
seen[hand::kThumbTip] = seen[hand::kIndexTip];
run(1200 * ms, true);
check(pose.buttons.triggerClick, "a pinch the camera sees clicks the trigger");
run(1800 * ms, false);
check(!pose.visible && !pose.buttons.triggerClick, "a hand the camera loses hides and lets go");
palm = {side ? -0.1 : 0.1, 1.45, -0.3};
run(2400 * ms, true);
check(pose.visible && Length(pose.position - (palm - Rotate(turn, mean))) < 0.01, "a hand that comes back somewhere else shows up there");
}
}
static void theTurnSmootherSettlesAndKeepsUpWithFastTurns() {
SmoothTurn slow, fast;
Quat out = slow.Update(Quat{}, 0.01);
const Quat jitter = AboutX(4.0 * M_PI / 180.0);
double worst = 0.0;
for (int i = 0; i < 300; i++) {
out = slow.Update((i / 7) % 2 ? jitter : Conjugate(jitter), 0.01);
if (i > 100) worst = std::max(worst, AngleDegrees(out));
}
check(worst < 2.5, "frame-to-frame wobble of a still hand is damped");
fast.Update(Quat{}, 0.01);
for (int i = 1; i <= 50; i++) out = fast.Update(AboutY(3.0 * i * 0.01), 0.01);
check(AngleDegrees(Conjugate(AboutY(1.5)) * out) < 25.0, "a fast turn is followed closely");
}
static void theHandRenderModelIsAClosedOutwardFacingHand() {
for (int right = 0; right < 2; right++) {
for (HandPart part : {HandPart::Hand, HandPart::ThumbIndex}) {
for (int closed = 0; closed < 2; closed++) {
const HandMesh m = BuildHandPart(right, part, closed);
bool finite = !m.faces.empty() && m.points.size() < 65000, cuff = false;
size_t inward = 0;
for (const Vec3 &p : m.points)
finite = finite && std::isfinite(p.x) && std::isfinite(p.y) && std::isfinite(p.z) && Length(p) < 0.3;
for (double tint : m.tints) cuff = cuff || tint > 0.99;
std::map<std::pair<int, int>, int> edges;
for (const auto &f : m.faces) {
const Vec3 n = Cross(m.points[f[1]] - m.points[f[0]], m.points[f[2]] - m.points[f[0]]);
if (Dot(n, m.normals[f[0]] + m.normals[f[1]] + m.normals[f[2]]) < 0.0) inward++;
for (int e = 0; e < 3; e++) edges[{f[e], f[(e + 1) % 3]}]++;
}
bool sealed = true;
for (const auto &entry : edges) sealed = sealed && edges.count({entry.first.second, entry.first.first}) == 1;
check(finite, "hand model parts are small, finite meshes SteamVR can load");
check(inward * 1000 < m.faces.size(), "hand model faces point out");
check(sealed, "hand model parts are watertight");
check(cuff, "the glove has a cuff at the wrist");
}
}
Vec3 pinch[kHandJoints];
HandShape(right, true, false, pinch);
check(Length(pinch[hand::kThumbTip] - pinch[hand::kIndexTip]) < 0.015, "the pinch model's thumb meets the index finger");
Vec3 open[kHandJoints];
HandShape(right, false, false, open);
check((open[hand::kThumbTip].x > 0.0) == !right, "the hand models are mirrored for the left and right hands");
const std::string json = HandModelJson(right);
for (const char *name : {"hand_open.obj", "hand_fist.obj", "thumb_index_open.obj", "thumb_index_pinch.obj", "\"tip\"", "\"base\"",
"\"handgrip\"", "\"pressed_path\": \"/input/trigger/click\"", "\"pressed_path\": \"/input/grip/touch\"",
"\"controller_button\": 33", "\"press\": true"})
check(json.find(name) != std::string::npos, "the hand model lists its parts, their inputs and the pointer poses");
}
const std::vector<uint8_t> png = GlovePng(64, {246, 246, 249}, {224, 227, 233}, glove::kDarkest);
check(png.size() > 40 && png[1] == 'P' && png[2] == 'N' && png[3] == 'G', "the hand texture is a PNG");
}
static void sharedLayoutMatchesTheAppWriter() {
check(offsetof(droiddeck_joycon, seq) == 8, "sequence offset");
check(offsetof(droiddeck_joycon_hand, buttons) == 4, "buttons offset");
check(offsetof(droiddeck_joycon_hand, rotation) == 8, "rotation offset");
check(offsetof(droiddeck_joycon_hand, angular_velocity) == 24, "angular velocity offset");
check(offsetof(droiddeck_joycon_hand, stick) == 36, "stick offset");
check(offsetof(droiddeck_joycon_hand, battery) == 44, "battery offset");
check(offsetof(droiddeck_joycon_hand, recenter) == 48, "recenter offset");
check(offsetof(droiddeck_joycon_hand, velocity) == 76, "velocity offset");
check(offsetof(droiddeck_joycon_hand, inertial_velocity) == 88, "inertial velocity offset");
check(sizeof(droiddeck_joycon) == 224, "file size");
check(DROIDDECK_JOYCON_VERSION == 3, "file version");
check(DROIDDECK_JOYCON_TRIGGER == 1 && DROIDDECK_JOYCON_GRIP == 2 && DROIDDECK_JOYCON_A == 4 &&
DROIDDECK_JOYCON_B == 8 && DROIDDECK_JOYCON_STICK == 16 && DROIDDECK_JOYCON_SYSTEM == 32,
"button bits match JoyCon.kt");
}
static void historyInterpolatesBetweenSamples() {
TimeHistory history;
Vec3 out;
check(!history.At(0, &out), "an empty history has nothing to give");
history.Add(1000, {0.0, 0.0, 0.0});
history.Add(11000, {0.1, 0.0, -0.2});
history.Add(5000, {9.0, 9.0, 9.0});
check(history.At(6000, &out) && near(out.x, 0.05) && near(out.z, -0.1), "a time between samples is interpolated");
check(history.At(50000, &out) && near(out.x, 0.1), "a time after the newest sample uses the newest");
check(history.At(0, &out) && near(out.x, 0.0), "a time before the oldest sample uses the oldest");
history.Add(11000, {0.2, 0.0, 0.0});
check(history.At(11000, &out) && near(out.x, 0.2), "a repeated time replaces its sample");
for (int i = 0; i < 1000; i++) history.Add(20000 + i * 1000, {double(i), 0.0, 0.0});
check(history.At(0, &out) && out.x > 700.0, "only the newest samples are kept");
}
static void theCameraYawAveragesFaceReadingsAndFollowsARecenter() {
CameraYaw yaw;
double out = 0.0;
for (int i = 0; i < 3; i++) yaw.Add(0.5 + (i % 2 ? 0.1 : -0.1), 1.0 / 15.0);
check(!yaw.Get(&out), "a few readings are not enough");
for (int i = 3; i < 30; i++) yaw.Add(0.5 + (i % 2 ? 0.1 : -0.1), 1.0 / 15.0);
check(yaw.Get(&out) && near(out, 0.5, 0.02), "noisy readings average out");
for (int i = 0; i < 7; i++) yaw.Add(1.7, 1.0 / 15.0);
check(yaw.Get(&out) && near(out, 0.5, 0.02), "a short run of far readings is ignored");
for (int i = 0; i < 6; i++) yaw.Add(1.7, 1.0 / 15.0);
check(yaw.Get(&out) && near(out, 1.7, 1e-6), "a lasting change starts a new average");
CameraYaw wrapped;
for (int i = 0; i < 20; i++) wrapped.Add(i % 2 ? M_PI - 0.05 : -M_PI + 0.05, 1.0 / 15.0);
check(wrapped.Get(&out) && near(std::fabs(out), M_PI, 0.01), "readings either side of half a turn average to half a turn");
}
static void handCameraLayoutMatchesTheAppWriter() {
check(offsetof(droiddeck_handcam, seq) == 8, "camera sequence offset");
check(offsetof(droiddeck_handcam, flags) == 12, "camera flags offset");
check(offsetof(droiddeck_handcam, frame) == 24, "camera frame offset");
check(offsetof(droiddeck_handcam, face_yaw) == 28, "camera face yaw offset");
check(offsetof(droiddeck_handcam_hand, position) == 4, "camera hand position offset");
check(offsetof(droiddeck_handcam, hands) + sizeof(droiddeck_handcam_hand) + offsetof(droiddeck_handcam_hand, joints) == 316,
"second hand's joints offset");
check(DROIDDECK_HANDCAM_VERSION == 2 && DROIDDECK_HANDCAM_RUNNING == 1 && DROIDDECK_HANDCAM_FACE == 2 &&
DROIDDECK_HANDCAM_BARE_HANDS == 4 && DROIDDECK_HANDCAM_SEEN == 1 && DROIDDECK_HANDCAM_ESTIMATED == 2 &&
DROIDDECK_HANDCAM_JOINTS == 4 && DROIDDECK_HANDCAM_JOINT_COUNT == kHandJoints,
"camera flags match HandCameraShm.kt");
}
int main() {
restingHandsHangInFrontAtElbowHeight();
raisingTheControllerExtendsTheArm();
theTorsoOnlyFollowsTheHeadWhileTheControllerMoves();
headingAlignmentTurnsTheControllerToTheGaze();
theIndexTipPointsWhereTheJoyConPoints();
skeletonsAreFiniteMirroredAndNormalised();
measuredMovementCarriesTheHandAndTheAnchorPullsItBack();
sharedLayoutMatchesTheAppWriter();
historyInterpolatesBetweenSamples();
theFilterPlacesTheHandWhereItIsNowNotWhenThePictureWasTaken();
aLateSightingCorrectsThePastAndKeepsTheMoveSince();
aWildSightingIsIgnoredUntilItRepeats();
aHandTheCameraLosesSettlesInsteadOfRunningAway();
theCameraYawAveragesFaceReadingsAndFollowsARecenter();
handCameraLayoutMatchesTheAppWriter();
cameraFingerCurlsReadBackFromTheJoints();
thePalmFitFindsTheHandsTurnAndPlace();
thePalmNormalPointsOutOfThePalm();
pinchesAndFistsBecomeButtons();
aPalmTurnedToTheFaceOpensTheMenus();
aBareHandFollowsTheCameraAndHidesWhenLost();
theTurnSmootherSettlesAndKeepsUpWithFastTurns();
theHandRenderModelIsAClosedOutwardFacingHand();
if (failures) {
fprintf(stderr, "%d Joy-Con check(s) failed\n", failures);
return 1;
}
printf("Joy-Con arm model, movement, camera, pose and layout checks passed\n");
return 0;
}
+37
View File
@@ -0,0 +1,37 @@
#include "render_target.h"
#include "../../../app/src/main/cpp/waylandcomp/src/headpose_shm.h"
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <limits>
static void check(float aspect, uint32_t selected, uint32_t panel, uint32_t w, uint32_t h) {
const auto result = droiddeck::RenderTarget(aspect, selected, panel);
if (result.width != w || result.height != h) {
std::fprintf(stderr, "FAIL: aspect %g, height %u, panel %u gave %ux%u, expected %ux%u\n",
aspect, selected, panel, result.width, result.height, w, h);
std::abort();
}
}
int main() {
static_assert(sizeof(droiddeck_headpose) == 128);
static_assert(offsetof(droiddeck_headpose, render_height) == 84);
check(1920.0f / 1080, 720, 1080, 1280, 720);
check(1920.0f / 1080, 900, 1080, 1600, 900);
check(1920.0f / 1080, 1080, 1080, 1920, 1080);
check(1920.0f / 1080, 1440, 1080, 1920, 1080);
check(1920.0f / 1200, 1200, 1200, 1920, 1200);
check(960.0f / 1080, 720, 1080, 640, 720);
check(1200.0f / 1080, 720, 1080, 800, 720);
check(16.0f / 9, 720, 0, 1280, 720);
check(16.0f / 9, 0, 1080, 1280, 720);
check(16.0f / 9, 720, 480, 853, 480);
check(0, 0, 0, 720, 720);
check(std::numeric_limits<float>::quiet_NaN(), 720, 1080, 720, 720);
check(std::numeric_limits<float>::infinity(), 720, 1080, 720, 720);
check(-1, 720, 1080, 720, 720);
check(1, UINT32_MAX, 0, 8192, 8192);
check(100, 720, 1080, 8192, 720);
std::puts("PASS: VR render targets follow the selected height and fit the panel");
}
+30 -1
View File
@@ -9,6 +9,7 @@ static const VkSemaphoreWaitInfo *expected_wait;
static unsigned create_calls;
static unsigned destroyed;
static VkResult submit_result = VK_SUCCESS;
static int expect_dropped_wait;
static const void *expected_image_chain;
static const void *expected_external_tail;
static int expect_cloned_external;
@@ -67,6 +68,13 @@ static void check_shared_images(VkDevice device) {
static VkResult VKAPI_CALL mock_submit(VkQueue queue, uint32_t count, const VkSubmitInfo *submits, VkFence fence) {
assert(queue && count == 1 && fence == VK_NULL_HANDLE);
if (expect_dropped_wait) {
assert(submits != expected_submit && submits->waitSemaphoreCount == 0);
const VkTimelineSemaphoreSubmitInfo *timeline = submits->pNext;
assert(timeline->sType == VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO && timeline->waitSemaphoreValueCount == 0);
assert(expected_submit->waitSemaphoreCount == 1);
return submit_result;
}
assert(submits == expected_submit);
assert(submits->waitSemaphoreCount == 1);
return submit_result;
@@ -140,8 +148,22 @@ static VkResult VKAPI_CALL mock_device_idle(VkDevice device) {
assert(device); check_queue_lock(); return VK_ERROR_DEVICE_LOST;
}
static VkResult VKAPI_CALL mock_counter(VkDevice device, VkSemaphore semaphore, uint64_t *value) {
(void)device; (void)semaphore;
*value = 0;
return VK_SUCCESS;
}
static VkResult VKAPI_CALL mock_signal(VkDevice device, const VkSemaphoreSignalInfo *info) {
(void)device; (void)info;
assert(0);
return VK_SUCCESS;
}
static PFN_vkVoidFunction VKAPI_CALL mock_proc(VkDevice device, const char *name) {
(void)device;
if (!strcmp(name, "vkGetSemaphoreCounterValue")) return (PFN_vkVoidFunction)mock_counter;
if (!strcmp(name, "vkSignalSemaphore")) return (PFN_vkVoidFunction)mock_signal;
if (!strcmp(name, "vkCreateImage")) return (PFN_vkVoidFunction)mock_image;
if (!strcmp(name, "vkQueueWaitIdle")) return (PFN_vkVoidFunction)mock_queue_idle;
if (!strcmp(name, "vkDeviceWaitIdle")) return (PFN_vkVoidFunction)mock_device_idle;
@@ -223,6 +245,13 @@ int main(void) {
assert(ImportSemaphoreFdKHR(device, &import_info) == VK_ERROR_INVALID_EXTERNAL_HANDLE);
import_info.flags = 0;
assert(ImportSemaphoreFdKHR(device, &import_info) == VK_SUCCESS);
VkSubmitInfo imported_wait = submit;
imported_wait.pWaitSemaphores = &binary;
expected_submit = &imported_wait;
submit_result = VK_SUCCESS;
expect_dropped_wait = 1;
assert(QueueSubmit(queue, 1, &imported_wait, VK_NULL_HANDLE) == VK_SUCCESS);
expect_dropped_wait = 0;
VkSemaphoreGetFdInfoKHR get_info = {VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR, NULL, binary,
VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT};
int reexported = -1;
@@ -235,6 +264,6 @@ int main(void) {
assert(fcntl(descriptor, F_GETFD) == -1 && errno == EBADF);
DestroySemaphore(device, semaphore, NULL);
assert(destroyed == 2 && !bridged_find(semaphore));
puts("semaphore layer: pending waits retained, errors propagated, unsupported binary imports refused, fd ownership passed");
puts("semaphore layer: pending waits retained, unsignalled imported waits dropped, errors propagated, unsupported binary imports refused, fd ownership passed");
return 0;
}