diff --git a/app/src/main/java/com/droiddeck/launcher/QuestXrActivity.kt b/app/src/main/java/com/droiddeck/launcher/QuestXrActivity.kt index e5a7f3ff42..3709b5fa50 100644 --- a/app/src/main/java/com/droiddeck/launcher/QuestXrActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/QuestXrActivity.kt @@ -49,7 +49,7 @@ class QuestXrActivity : NativeActivity() { } } if (!started) { - leaveXr("SteamVR start failed", true) + if (!closing) leaveXr("SteamVR start failed", sessionAlive()) return@Thread } val startupDeadline = SystemClock.elapsedRealtime() + 30_000 diff --git a/docs/quest.md b/docs/quest.md index efa539906f..f99049217c 100644 --- a/docs/quest.md +++ b/docs/quest.md @@ -96,8 +96,9 @@ then acknowledges that buffer. Missing acknowledgements quarantine the transfer instead of allowing a producer to overwrite a possible reader. This still includes a CPU read and GLES upload; it is not GPU image import into OpenXR. -The helper enables SteamVR's Linux Vulkan asynchronous queue and disables motion -smoothing. It excludes the pinned Steam Frame package's optional CVP backend, +The helper temporarily enables SteamVR's Linux Vulkan asynchronous queue and disables motion +smoothing, backing up those preferences and restoring them on stop or the next start +after an interrupted session. It excludes the pinned Steam Frame package's optional CVP backend, whose library constructor exits on Quest's unsupported SoC. The original library is retained with a `.droiddeck-disabled` suffix. SteamVR binaries are not patched. Cardboard retains its existing direct-mode path; `questframes` remains an explicit @@ -125,8 +126,14 @@ images per second despite a 90 Hz XR session. That earlier measurement used the driver-direct presenter. With SteamVR's normal compositor, a native Vulkan scene passed its first submission and continued submitting stereo frames. Thumper and The Thrill of the Fight produced clean -immersive scenes. SUPERHOT's Windows OpenXR build still showed shared-image -corruption. A subsequent 60-second shared-buffer dashboard run delivered about +immersive scenes. SUPERHOT's Windows OpenXR build initially showed shared-image +corruption: DXVK supplied a restricted mutable-image format list while SteamVR +created its corresponding opaque shared images without one. Turnip can select +different memory layouts for those descriptions. On the Quest bridge, the layer +normalizes the known opaque-FD creation chains to SteamVR's unrestricted list, +without changing DMA-BUF/modifier images or other targets. A headset capture then +showed clean SUPERHOT scene geometry; interactive gameplay remains unverified. +A subsequent 60-second shared-buffer dashboard run delivered about 43 new stereo images per second. Reusing Android pixel buffers raised the next 60-second run to about 68 received and 65 uploaded images per second, with valid focused tracking and no rejected frames. These dashboard results do not establish refresh-rate gameplay: game input, diff --git a/tools/droiddeckctl b/tools/droiddeckctl index 6ccf7a8fdf..65be5580cf 100755 --- a/tools/droiddeckctl +++ b/tools/droiddeckctl @@ -421,7 +421,15 @@ def xr_capture(adb: str, serial: str, destination: Path, timeout: float) -> dict deadline = time.monotonic() + timeout metadata = None while time.monotonic() < deadline: - result = run_adb(adb, serial, 'shell', 'cat', remote + '.json') + remaining = deadline - time.monotonic() + if remaining <= 0: + break + try: + result = run_adb(adb, serial, 'shell', 'cat', remote + '.json', timeout=remaining) + except ControlError as error: + if not isinstance(error.__cause__, subprocess.TimeoutExpired): + raise + break try: answer = json.loads(result.stdout) if answer.get('id') == token: @@ -431,7 +439,10 @@ def xr_capture(adb: str, serial: str, destination: Path, timeout: float) -> dict pass time.sleep(0.2) if metadata is None: - run_adb(adb, serial, 'shell', 'rm', '-f', remote + '.request') + try: + run_adb(adb, serial, 'shell', 'rm', '-f', remote + '.request', timeout=1) + except ControlError: + pass raise ControlError('No fresh XR capture. Enter SteamVR using a build with XR capture support.', 'TIMEOUT') if not metadata.get('ok'): detail = 'No valid XR projection layer is being submitted' if not metadata.get('validLayer') else 'GPU readback or image write failed' @@ -454,10 +465,13 @@ def xr_capture(adb: str, serial: str, destination: Path, timeout: float) -> dict unchanged = False if not unchanged: raise ControlError('XR capture was replaced by another request; retry', 'XR_CAPTURE_FAILED') - destination.mkdir(parents=True, exist_ok=True) - for eye, image in images.items(): - (destination / (eye + '.png')).write_bytes(image) - (destination / 'capture.json').write_text(json.dumps(metadata, indent=2), encoding='utf-8') + try: + destination.mkdir(parents=True, exist_ok=True) + for eye, image in images.items(): + (destination / (eye + '.png')).write_bytes(image) + (destination / 'capture.json').write_text(json.dumps(metadata, indent=2), encoding='utf-8') + except OSError as error: + raise ControlError(str(error), 'FILE_IO') from error return {'ok': True, 'command': 'xr-frame', 'deviceSerial': serial, 'capture': metadata, 'left': str((destination / 'left.png').resolve()), 'right': str((destination / 'right.png').resolve()), **({'source': str((destination / 'source.png').resolve())} if 'source' in images else {}), diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-steamvr b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steamvr index a289186923..05f2c9a7a8 100644 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-steamvr +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steamvr @@ -16,6 +16,12 @@ PKG_SHA256=90a600775eb3e8856dc475f83166a93a290a0b1f3b4e82a69c8e2a8f06b9ddc9 running() { pgrep -x vrserver >/dev/null 2>&1; } installed() { [ -x "$steamvr/bin/linuxarm64/vrstartup" ]; } +quest_settings() { + backup=$HOME/.local/share/droiddeck/steamvr-quest-settings.json + [ "$1" != restore ] || [ -f "$backup" ] || return 0 + python3 "${DROIDDECK_VR_SETTINGS_HELPER:-/usr/local/bin/droiddeck-steamvr-settings}" "$1" \ + "$HOME/.local/share/Steam/config/steamvr.vrsettings" "$backup" +} stop() { rm -f "$state/active" @@ -23,7 +29,16 @@ stop() { for p in $(pgrep -x "$n"); do kill "$p" 2>/dev/null; done done for p in $(pgrep -f "$steamvr/bin/linuxarm64/vrcompositor-launcher"); do kill "$p" 2>/dev/null; done - return 0 + attempts=0 + while pgrep -x 'vrserver|vrcompositor' >/dev/null 2>&1; do + attempts=$((attempts + 1)) + if [ "$attempts" = 10 ]; then + for p in $(pgrep -x 'vrserver|vrcompositor'); do kill -KILL "$p" 2>/dev/null; done + fi + [ "$attempts" -lt 20 ] || { echo 'SteamVR did not stop; preferences remain backed up' >&2; return 1; } + sleep 0.1 + done + quest_settings restore } case "${1:-status}" in @@ -54,12 +69,12 @@ install) echo "SteamVR installed in $steamvr" ;; stop) - stop + stop || exit 1 ;; start) 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; } - stop + stop || exit 1 sleep 1 mkdir -p "$HOME/.config/openvr" "$HOME/.config/openxr/1" "$logs" "$HOME/.local/share/Steam/config" cat > "$HOME/.config/openvr/openvrpaths.vrpath" <&2; exit 1; } - mv "$cvp" "$cvp.droiddeck-disabled" || exit 1 + disabled=$cvp.droiddeck-disabled + if [ -e "$disabled" ]; then disabled=$(mktemp "$disabled.XXXXXX") || exit 1; fi + mv "$cvp" "$disabled" || exit 1 echo "Disabled Frame-only CVP backend for Quest; SteamVR uses its normal compositor" fi fi @@ -127,6 +129,7 @@ PY startup_log=$logs/droiddeck-vrstartup.log ln -sf "$startup_log" "$state/vrstartup.log" setsid ./vrstartup > "$startup_log" 2>&1 < /dev/null & + startup_handed_off=1 echo "SteamVR starting; logs in $logs and $state/vrstartup.log" ;; *) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-steamvr-game b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steamvr-game index 4fba6e5b57..d1e18827fc 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-steamvr-game +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steamvr-game @@ -11,7 +11,7 @@ export VK_LAYER_PATH="$vrdir/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~}" # Quest's guest GPU images use Vulkan sharing rather than DMA-BUF modifiers. -if [ -S "${XDG_RUNTIME_DIR:-/run/droiddeck}/droiddeck-xr-frames" ]; then +if [ -S "/run/droiddeck/droiddeck-xr-frames" ]; then export gamescope_vr_use_modifiers=${gamescope_vr_use_modifiers:-false} fi export gamescope_vr_nudge_to_visible=${gamescope_vr_nudge_to_visible:-true} diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-steamvr-settings b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steamvr-settings new file mode 100644 index 0000000000..7214160bd6 --- /dev/null +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steamvr-settings @@ -0,0 +1,63 @@ +#!/usr/bin/python3 +"""Preserve SteamVR preferences across Quest's required compositor overrides.""" +import json +import os +from pathlib import Path +import sys + +OVERRIDES = {'enableLinuxVulkanAsync': True, 'motionSmoothing': False} + + +def read(path): + try: + with path.open() as source: + settings = json.load(source) + except FileNotFoundError: + return {} + if not isinstance(settings, dict) or not isinstance(settings.get('steamvr', {}), dict): + raise ValueError('Invalid SteamVR settings object') + return settings + + +def write(path, value): + path.parent.mkdir(parents=True, exist_ok=True, mode=0o700) + temporary = path.with_name(path.name + '.tmp') + with os.fdopen(os.open(temporary, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o600), 'w') as destination: + json.dump(value, destination, indent=3) + destination.write('\n') + os.replace(temporary, path) + + +def update(operation, settings_path, backup_path): + settings = read(settings_path) + if operation == 'apply': + if not backup_path.exists(): + section = settings.get('steamvr', {}) + write(backup_path, {'section': 'steamvr' in settings, + 'values': {key: [key in section, section.get(key)] for key in OVERRIDES}}) + settings.setdefault('steamvr', {}).update(OVERRIDES) + write(settings_path, settings) + elif operation == 'restore': + if not backup_path.exists(): + return + backup = read(backup_path) + section = settings.setdefault('steamvr', {}) + for key in OVERRIDES: + present, value = backup['values'][key] + if present: + section[key] = value + else: + section.pop(key, None) + if not section and not backup['section']: + settings.pop('steamvr') + write(settings_path, settings) + backup_path.unlink() + else: + raise ValueError('Expected apply or restore') + + +if __name__ == '__main__': + try: + update(sys.argv[1], Path(sys.argv[2]), Path(sys.argv[3])) + except (OSError, ValueError, KeyError, IndexError, TypeError) as error: + sys.exit('Could not update SteamVR settings: ' + str(error)) diff --git a/tools/tests/test_droiddeckctl_xr.py b/tools/tests/test_droiddeckctl_xr.py index 758b3c0c01..f1b314f7bf 100644 --- a/tools/tests/test_droiddeckctl_xr.py +++ b/tools/tests/test_droiddeckctl_xr.py @@ -87,6 +87,33 @@ class XrCaptureTest(unittest.TestCase): self.assertEqual(result['source'], str((root / 'source.png').resolve())) self.assertEqual(len([c for c in calls if c[0] == 'exec-out']), 3) + def test_output_creation_and_write_failures_are_file_errors(self): + for operation in ('mkdir', 'write_bytes'): + with self.subTest(operation=operation), tempfile.TemporaryDirectory() as directory: + with patch.object(Path, operation, side_effect=PermissionError('output denied')): + with self.assertRaises(ctl.ControlError) as caught: + self.capture(Path(directory) / 'capture') + self.assertEqual(caught.exception.code, 'FILE_IO') + self.assertEqual(ctl.exit_code(caught.exception), 6) + + def test_stalled_metadata_read_uses_capture_deadline_and_reports_timeout(self): + calls = [] + def adb(_adb, _serial, *args, **kwargs): + calls.append((args, kwargs)) + if args[:2] == ('shell', 'cat'): + self.assertGreater(kwargs['timeout'], 0) + self.assertLessEqual(kwargs['timeout'], 0.1) + try: + raise subprocess.TimeoutExpired('adb', kwargs['timeout']) + except subprocess.TimeoutExpired as cause: + raise ctl.ControlError('metadata stalled', 'ADB_FAILED') from cause + return subprocess.CompletedProcess(args, 0, b'', b'') + with tempfile.TemporaryDirectory() as directory, patch.object(ctl, 'run_adb', side_effect=adb): + with self.assertRaises(ctl.ControlError) as caught: + ctl.xr_capture('adb', 'quest', Path(directory) / 'capture', 0.1) + self.assertEqual(caught.exception.code, 'TIMEOUT') + self.assertTrue(any(args[:3] == ('shell', 'rm', '-f') for args, _ in calls)) + if __name__ == '__main__': unittest.main() diff --git a/tools/tests/test_steamvr_game.py b/tools/tests/test_steamvr_game.py index 2280aceaa8..68c94d00d5 100644 --- a/tools/tests/test_steamvr_game.py +++ b/tools/tests/test_steamvr_game.py @@ -16,7 +16,9 @@ class SteamVrGameTest(unittest.TestCase): self.addCleanup(temporary.cleanup) self.root = Path(temporary.name) self.helper = self.root / 'helper' - self.helper.write_text(SOURCE.read_text(), encoding='utf-8') + self.assertIn('/run/droiddeck/droiddeck-xr-frames', SOURCE.read_text()) + self.helper.write_text(SOURCE.read_text().replace('/run/droiddeck/droiddeck-xr-frames', + str(self.root / 'droiddeck-xr-frames')), encoding='utf-8') self.command('pgrep', '#!/bin/sh\nexit 0\n') self.command('gamescope', '#!/usr/bin/env python3\nimport json, os, sys\n' 'print(json.dumps(dict(argv=sys.argv[1:], modifiers=os.getenv("gamescope_vr_use_modifiers"), ' diff --git a/tools/tests/test_steamvr_helper.py b/tools/tests/test_steamvr_helper.py index 686d84c983..2a8a58fe4e 100644 --- a/tools/tests/test_steamvr_helper.py +++ b/tools/tests/test_steamvr_helper.py @@ -25,7 +25,9 @@ class SteamVrHelperTest(unittest.TestCase): for directory in (self.commands, self.driver, self.steamvr / 'bin/linuxarm64'): directory.mkdir(parents=True) self.helper = self.root / 'droiddeck-steamvr' - self.helper.write_text(SOURCE.read_text(), encoding='utf-8') + self.assertIn('/run/droiddeck/droiddeck-xr-frames', SOURCE.read_text()) + self.helper.write_text(SOURCE.read_text().replace('/run/droiddeck/droiddeck-xr-frames', + str(self.runtime / 'droiddeck-xr-frames')), encoding='utf-8') self.executable(self.commands / 'pgrep', 'exit 1') self.executable(self.commands / 'sleep', 'exit 0') self.executable(self.commands / 'xset', 'exit 0') @@ -40,6 +42,7 @@ class SteamVrHelperTest(unittest.TestCase): self.environment.pop('DROIDDECK_VR_PRODUCER_LEASE', None) self.environment.update(HOME=str(self.home), XDG_RUNTIME_DIR=str(self.runtime), STEAMVR=str(self.steamvr), DROIDDECK_VR_DIR=str(self.root / 'driver'), + DROIDDECK_VR_SETTINGS_HELPER=str(SOURCE.with_name('droiddeck-steamvr-settings')), PATH=str(self.commands) + os.pathsep + os.environ['PATH']) def executable(self, destination, body): @@ -118,6 +121,46 @@ class SteamVrHelperTest(unittest.TestCase): self.assertNotEqual(result.returncode, 0) self.assertEqual(path.read_text(), 'invalid json') + def test_quest_stop_restores_only_overridden_preferences(self): + self.environment['DROIDDECK_VR_PRESENT'] = 'questwsi' + path = self.home / '.local/share/Steam/config/steamvr.vrsettings' + path.parent.mkdir(parents=True) + path.write_text(json.dumps({'steamvr': {'motionSmoothing': True, 'somePreference': 42}})) + self.start() + settings = json.loads(path.read_text()) + settings['dashboard'] = {'changedDuringSession': True} + path.write_text(json.dumps(settings)) + subprocess.run(['sh', str(self.helper), 'stop'], env=self.environment, check=True) + self.assertEqual(json.loads(path.read_text()), {'steamvr': {'motionSmoothing': True, 'somePreference': 42}, + 'dashboard': {'changedDuringSession': True}}) + + def test_quest_update_preserves_previous_cvp_backup(self): + self.environment['DROIDDECK_VR_PRESENT'] = 'questwsi' + cvp = self.steamvr / 'drivers/cv/bin/linuxarm64/libArcturusPerception.so' + cvp.parent.mkdir(parents=True) + cvp.write_bytes(b'updated library') + backup = Path(str(cvp) + '.droiddeck-disabled') + backup.write_bytes(b'previous library') + self.start() + self.assertFalse(cvp.exists()) + self.assertEqual(backup.read_bytes(), b'previous library') + self.assertEqual([p.read_bytes() for p in cvp.parent.glob('*.droiddeck-disabled.*')], [b'updated library']) + + def test_start_failure_after_overrides_restores_preferences(self): + self.environment['DROIDDECK_VR_PRESENT'] = 'questwsi' + path = self.home / '.local/share/Steam/config/steamvr.vrsettings' + path.parent.mkdir(parents=True) + original = {'steamvr': {'enableLinuxVulkanAsync': False, 'motionSmoothing': True}} + path.write_text(json.dumps(original)) + cvp = self.steamvr / 'drivers/cv/bin/linuxarm64/libArcturusPerception.so' + cvp.parent.mkdir(parents=True) + cvp.touch() + self.executable(self.commands / 'mv', 'exit 1') + result = subprocess.run(['sh', str(self.helper), 'start'], env=self.environment, + capture_output=True, text=True, timeout=5) + self.assertNotEqual(result.returncode, 0) + self.assertEqual(json.loads(path.read_text()), original) + def test_explicit_quest_presenter_enables_the_lease(self): self.environment['DROIDDECK_VR_PRESENT'] = 'questframes' self.assertIn('present=questframes lease=1', self.lease_log()) diff --git a/tools/vr/src/driver_droiddeck.cpp b/tools/vr/src/driver_droiddeck.cpp index a43a949656..a38920eb7c 100644 --- a/tools/vr/src/driver_droiddeck.cpp +++ b/tools/vr/src/driver_droiddeck.cpp @@ -559,13 +559,8 @@ public: VRProperties()->SetFloatProperty(c, Prop_DisplayFrequency_Float, hz); VRProperties()->SetFloatProperty(c, Prop_SecondsFromVsyncToPhotons_Float, 0.011f); VRProperties()->SetUint64Property(c, Prop_CurrentUniverseId_Uint64, 2); - if (xrMode_) { - char timestamp[32]; - std::time_t now = std::time(nullptr); std::tm utc{}; gmtime_r(&now, &utc); - std::strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%SZ", &utc); - const auto chaperone = xr_chaperone_json(xr.head, timestamp); - VRProperties()->SetStringProperty(c, Prop_DriverProvidedChaperoneJson_String, chaperone.c_str()); - } + chaperone_.Reset(); + if (xrMode_) PublishChaperone(xr); VRProperties()->SetBoolProperty(c, Prop_IsOnDesktop_Bool, false); VRProperties()->SetBoolProperty(c, Prop_DisplayDebugMode_Bool, false); directMode_ = VRSettings()->GetBool(kSettingsSection, "directMode"); @@ -680,6 +675,16 @@ public: } private: + void PublishChaperone(const DroidDeckXrTracking &tracking) { + chaperone_.Update(tracking, [this](const DroidDeckXrPose &head) { + char timestamp[32]; + std::time_t now = std::time(nullptr); std::tm utc{}; gmtime_r(&now, &utc); + std::strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%SZ", &utc); + const auto json = xr_chaperone_json(head, timestamp); + const auto container = VRProperties()->TrackedDeviceToPropertyContainer(id_); + return VRProperties()->SetStringProperty(container, Prop_DriverProvidedChaperoneJson_String, json.c_str()) == TrackedProp_Success; + }); + } void PoseLoop() { uint32_t lastSeq = 0; auto lastSent = std::chrono::steady_clock::now(); @@ -687,6 +692,7 @@ private: DroidDeckXrTracking xr; if (xrMode_) { bool fresh = xr_.Read(&xr) && XrTrackingFile::Fresh(xr); + if (fresh) PublishChaperone(xr); DriverPose_t head = GetPose(); VRServerDriverHost()->TrackedDevicePoseUpdated(id_, head, sizeof(head)); for (int hand = 0; hand < 2; hand++) if (hands_[hand]) { @@ -732,6 +738,7 @@ public: private: PoseFile pose_; XrTrackingFile xr_; + XrChaperoneOrigin chaperone_; bool xrMode_ = false; PadRing pad_; PadState lastPad_; diff --git a/tools/vr/src/vk_layer_droiddeck_vr.c b/tools/vr/src/vk_layer_droiddeck_vr.c index 71ca63fd91..bde81f4a77 100644 --- a/tools/vr/src/vk_layer_droiddeck_vr.c +++ b/tools/vr/src/vk_layer_droiddeck_vr.c @@ -101,6 +101,7 @@ struct dev { int active; int display; int producer; + int quest_images; }; static int steamvr_running(void) { @@ -256,7 +257,7 @@ static VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice pd, const VkDeviceCreat for (int i = 0; i < MAX_OBJS; i++) { if (!g_dev[i].handle) { g_dev[i] = (struct dev){*out, next_gdpa, (PFN_vkDestroyDevice)next_gdpa(*out, "vkDestroyDevice"), active, display, - in && in->producer}; + in && in->producer, active && access("/run/droiddeck/droiddeck-xr-frames", F_OK) == 0}; break; } } @@ -526,6 +527,7 @@ static PFN_vkVoidFunction display_device_hook(const char *name) { #define POLL_US 300 static pthread_mutex_t g_submission_lock = PTHREAD_MUTEX_INITIALIZER; +static unsigned g_producer_idle_calls; struct producer_queue { VkQueue queue; VkDevice device; @@ -547,7 +549,7 @@ static int producer_acquire(int64_t deadline) { usleep(100); } int queues = 0; - if (g_producer_overflow) goto fail; + if (g_producer_overflow || g_producer_idle_calls) goto fail; for (unsigned index = 0; index < MAX_OBJS; index++) { struct producer_queue *entry = &g_producer_queues[index]; if (!entry->queue) continue; @@ -931,18 +933,34 @@ static VkResult VKAPI_CALL QueueBindSparse(VkQueue queue, uint32_t count, const static VkResult VKAPI_CALL QueueWaitIdle(VkQueue queue) { struct dev *device = dev_for(queue); PFN_vkQueueWaitIdle next = (PFN_vkQueueWaitIdle)device->gdpa(device->handle, "vkQueueWaitIdle"); - if (device->producer) pthread_mutex_lock(&g_submission_lock); + if (device->producer) { + pthread_mutex_lock(&g_submission_lock); + g_producer_idle_calls++; + pthread_mutex_unlock(&g_submission_lock); + } VkResult result = next(queue); - if (device->producer) pthread_mutex_unlock(&g_submission_lock); + if (device->producer) { + pthread_mutex_lock(&g_submission_lock); + g_producer_idle_calls--; + pthread_mutex_unlock(&g_submission_lock); + } return result; } static VkResult VKAPI_CALL DeviceWaitIdle(VkDevice handle) { struct dev *device = dev_for(handle); PFN_vkDeviceWaitIdle next = (PFN_vkDeviceWaitIdle)device->gdpa(handle, "vkDeviceWaitIdle"); - if (device->producer) pthread_mutex_lock(&g_submission_lock); + if (device->producer) { + pthread_mutex_lock(&g_submission_lock); + g_producer_idle_calls++; + pthread_mutex_unlock(&g_submission_lock); + } VkResult result = next(handle); - if (device->producer) pthread_mutex_unlock(&g_submission_lock); + if (device->producer) { + pthread_mutex_lock(&g_submission_lock); + g_producer_idle_calls--; + pthread_mutex_unlock(&g_submission_lock); + } return result; } @@ -959,6 +977,29 @@ static VkResult VKAPI_CALL GetSemaphoreCounterValue(VkDevice d, VkSemaphore s, u } static VkResult VKAPI_CALL CreateImage(VkDevice d, const VkImageCreateInfo *ci, const VkAllocationCallbacks *a, VkImage *out) { + VkImageCreateInfo compatible = *ci; + VkExternalMemoryImageCreateInfo shared; + const VkBaseInStructure *first = ci->pNext; + // SteamVR omits a format list, but DXVK adds one; Turnip can choose a different shared-memory layout. + if (dev_for(d)->quest_images && (ci->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) && ci->tiling == VK_IMAGE_TILING_OPTIMAL + && first && first->sType == VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO) { + const VkBaseInStructure *second = first->pNext; + if (second && second->sType == VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO + && ((const VkExternalMemoryImageCreateInfo *)second)->handleTypes == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT) { + compatible.pNext = second; ci = &compatible; + say("%s", "matching opaque shared image layout to SteamVR's unrestricted format list"); + } + } + if (dev_for(d)->quest_images && (ci->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) && ci->tiling == VK_IMAGE_TILING_OPTIMAL + && first && first->sType == VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO) { + const VkExternalMemoryImageCreateInfo *external = (const void *)first; + const VkBaseInStructure *second = external->pNext; + if (external->handleTypes == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT && second + && second->sType == VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO) { + shared = *external; shared.pNext = second->pNext; compatible.pNext = &shared; ci = &compatible; + say("%s", "matching opaque shared image layout to SteamVR's unrestricted format list"); + } + } VkResult r = NEXT(d, CreateImage)(d, ci, a, out); if (!sem_trace_on()) return r; const VkExternalMemoryImageCreateInfo *ext = NULL; @@ -974,6 +1015,8 @@ static VkResult VKAPI_CALL CreateImage(VkDevice d, const VkImageCreateInfo *ci, ci->tiling, ci->arrayLayers, ci->mipLevels, ci->samples, ext->handleTypes); for (uint32_t i = 0; list && i < list->viewFormatCount && n < (int)sizeof(line) - 8; i++) n += snprintf(line + n, sizeof(line) - n, " %d", list->pViewFormats[i]); + for (const VkBaseInStructure *p = ci->pNext; p && n < (int)sizeof(line) - 16; p = p->pNext) + n += snprintf(line + n, sizeof(line) - n, " chain:%d", p->sType); say("%s", line); return r; } diff --git a/tools/vr/src/xr_chaperone.h b/tools/vr/src/xr_chaperone.h index 5473cc5a66..146e7735e9 100644 --- a/tools/vr/src/xr_chaperone.h +++ b/tools/vr/src/xr_chaperone.h @@ -1,5 +1,5 @@ #pragma once -#include "xr_tracking.h" +#include "xr_tracking_file.h" #include #include #include @@ -10,6 +10,17 @@ struct XrSeatedOrigin { double yaw, translation[3]; }; +class XrChaperoneOrigin { +public: + void Reset() { published_ = false; } + template void Update(const DroidDeckXrTracking &tracking, Publish publish) { + if (!published_ && XrTrackingFile::Fresh(tracking) && XrTrackingFile::Tracked(tracking.head)) + published_ = publish(tracking.head); + } +private: + bool published_ = false; +}; + inline XrSeatedOrigin xr_seated_origin(const DroidDeckXrPose &head) { const auto *q = head.orientation; double yaw = std::atan2(2 * (q[0] * q[2] + q[3] * q[1]), 1 - 2 * (q[0] * q[0] + q[1] * q[1])); diff --git a/tools/vr/src/xr_frame_channel.h b/tools/vr/src/xr_frame_channel.h index 530a3e1a3c..36e5cb8fd3 100644 --- a/tools/vr/src/xr_frame_channel.h +++ b/tools/vr/src/xr_frame_channel.h @@ -175,6 +175,12 @@ private: if (!xr_frame_peer(client)) { rejected_++; close(client); continue; } uint64_t lastFrame = 0; while (!stopping_) { + pollfd idle{client, POLLIN, 0}; + int available; + do { + available = poll(&idle, 1, 50); + } while (!stopping_ && (available == 0 || (available < 0 && errno == EINTR))); + if (stopping_ || available <= 0 || !(idle.revents & POLLIN)) break; DroidDeckXrPixels frame; XrFrameDescriptor shared; int64_t deadline = xr_frame_now() + 200000000LL; diff --git a/tools/vr/tests/test_producer_gpu.c b/tools/vr/tests/test_producer_gpu.c index 2c683b4a93..b75d515f5f 100644 --- a/tools/vr/tests/test_producer_gpu.c +++ b/tools/vr/tests/test_producer_gpu.c @@ -54,7 +54,7 @@ int main(void) { VkDeviceCreateInfo create_device = {VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, &timeline_feature, 0, 1, &queue_info, 0, NULL, 0, NULL, NULL}; assert(vkCreateDevice(physical, &create_device, NULL, &test_device) == VK_SUCCESS); vkGetDeviceQueue(test_device, family, 0, &test_queue); - g_dev[0] = (struct dev){test_device, vkGetDeviceProcAddr, NULL, 1, 0, 1}; + g_dev[0] = (struct dev){test_device, vkGetDeviceProcAddr, NULL, 1, 0, 1, 0}; g_producer_started = 1; VkBufferCreateInfo create_buffer = {VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, NULL, 0, 4096, VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_SHARING_MODE_EXCLUSIVE, 0, NULL}; diff --git a/tools/vr/tests/test_semaphore_layer.c b/tools/vr/tests/test_semaphore_layer.c index 81a4b5a74a..dc265e4a93 100644 --- a/tools/vr/tests/test_semaphore_layer.c +++ b/tools/vr/tests/test_semaphore_layer.c @@ -9,6 +9,58 @@ static const VkSemaphoreWaitInfo *expected_wait; static unsigned create_calls; static unsigned destroyed; static VkResult submit_result = VK_SUCCESS; +static const void *expected_image_chain; +static const void *expected_external_tail; +static int expect_cloned_external; + +static VkResult VKAPI_CALL mock_image(VkDevice device, const VkImageCreateInfo *info, + const VkAllocationCallbacks *allocator, VkImage *output) { + (void)device; (void)allocator; (void)output; + if (expect_cloned_external) { + const VkExternalMemoryImageCreateInfo *external = info->pNext; + assert(external != expected_image_chain); + assert(external->sType == VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO); + assert(external->handleTypes == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT); + assert(external->pNext == expected_external_tail); + } else assert(info->pNext == expected_image_chain); + return VK_ERROR_FORMAT_NOT_SUPPORTED; +} + +static void check_shared_images(VkDevice device) { + VkBaseInStructure tail = {VK_STRUCTURE_TYPE_APPLICATION_INFO, NULL}; + VkExternalMemoryImageCreateInfo external = {VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO, + &tail, VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT}; + VkFormat formats[] = {VK_FORMAT_R8G8B8A8_SRGB, VK_FORMAT_R8G8B8A8_UNORM}; + VkImageFormatListCreateInfo list = {VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO, &external, 2, formats}; + VkImageCreateInfo image = {0}; + image.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; + image.pNext = &list; + image.flags = VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT; + image.tiling = VK_IMAGE_TILING_OPTIMAL; + VkImage output; + /* Other targets, nonmutable, linear and DMA-BUF images retain their caller's chain. */ + expected_image_chain = &list; + assert(CreateImage(device, &image, NULL, &output) == VK_ERROR_FORMAT_NOT_SUPPORTED); + g_dev[0].quest_images = 1; + image.flags = 0; + assert(CreateImage(device, &image, NULL, &output) == VK_ERROR_FORMAT_NOT_SUPPORTED); + image.flags = VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT; + image.tiling = VK_IMAGE_TILING_LINEAR; + assert(CreateImage(device, &image, NULL, &output) == VK_ERROR_FORMAT_NOT_SUPPORTED); + image.tiling = VK_IMAGE_TILING_OPTIMAL; + external.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT; + assert(CreateImage(device, &image, NULL, &output) == VK_ERROR_FORMAT_NOT_SUPPORTED); + external.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT; + expected_image_chain = &external; + assert(CreateImage(device, &image, NULL, &output) == VK_ERROR_FORMAT_NOT_SUPPORTED); + assert(image.pNext == &list && list.pNext == &external && external.pNext == &tail); + /* The reverse chain order preserves its tail without modifying caller memory. */ + external.pNext = &list; list.pNext = &tail; image.pNext = &external; + expect_cloned_external = 1; expected_external_tail = &tail; + assert(CreateImage(device, &image, NULL, &output) == VK_ERROR_FORMAT_NOT_SUPPORTED); + assert(image.pNext == &external && external.pNext == &list && list.pNext == &tail); + expect_cloned_external = 0; g_dev[0].quest_images = 0; +} static VkResult VKAPI_CALL mock_submit(VkQueue queue, uint32_t count, const VkSubmitInfo *submits, VkFence fence) { assert(queue && count == 1 && fence == VK_NULL_HANDLE); @@ -49,15 +101,36 @@ static void VKAPI_CALL mock_destroy(VkDevice device, VkSemaphore semaphore, cons } static int expect_queue_lock; +static VkQueue other_queue; +static _Atomic int other_submitted; + +static void *signal_from_other_queue(void *unused) { + (void)unused; + assert(QueueSubmit(other_queue, 1, expected_submit, VK_NULL_HANDLE) == VK_SUCCESS); + atomic_store(&other_submitted, 1); + return NULL; +} static void check_queue_lock(void) { int acquired = pthread_mutex_trylock(&g_submission_lock); - assert(acquired == (expect_queue_lock ? EBUSY : 0)); + assert(acquired == 0); if (!acquired) pthread_mutex_unlock(&g_submission_lock); + assert(g_producer_idle_calls == (unsigned)expect_queue_lock); + if (expect_queue_lock) assert(!producer_acquire(droiddeck_producer_now() + 10000000LL)); } static VkResult VKAPI_CALL mock_queue_idle(VkQueue queue) { - assert(queue); check_queue_lock(); return VK_ERROR_DEVICE_LOST; + assert(queue); check_queue_lock(); + if (expect_queue_lock) { + VkSubmitInfo signal = {VK_STRUCTURE_TYPE_SUBMIT_INFO, NULL, 1, NULL, NULL, 0, NULL, 0, NULL}; + expected_submit = &signal; + pthread_t worker; atomic_store(&other_submitted, 0); + assert(pthread_create(&worker, NULL, signal_from_other_queue, NULL) == 0); + int64_t deadline = droiddeck_producer_now() + 1000000000LL; + while (!atomic_load(&other_submitted) && droiddeck_producer_now() < deadline) usleep(1000); + assert(atomic_load(&other_submitted)); pthread_join(worker, NULL); + } + return VK_ERROR_DEVICE_LOST; } static VkResult VKAPI_CALL mock_device_idle(VkDevice device) { @@ -66,6 +139,7 @@ static VkResult VKAPI_CALL mock_device_idle(VkDevice device) { static PFN_vkVoidFunction VKAPI_CALL mock_proc(VkDevice device, const char *name) { (void)device; + 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; if (!strcmp(name, "vkQueueSubmit")) return (PFN_vkVoidFunction)mock_submit; @@ -82,7 +156,11 @@ int main(void) { void *dispatch = &dispatch; VkDevice device = (VkDevice)&dispatch; VkQueue queue = (VkQueue)&dispatch; - g_dev[0] = (struct dev){device, mock_proc, NULL, 1, 0, 0}; + void *other_dispatch = dispatch; + other_queue = (VkQueue)&other_dispatch; + g_producer_retry_at = INT64_MAX; + g_dev[0] = (struct dev){device, mock_proc, NULL, 1, 0, 0, 0}; + check_shared_images(device); g_poller_started = 1; PFN_vkQueueWaitIdle queue_idle = (PFN_vkQueueWaitIdle)semaphore_hook("vkQueueWaitIdle"); PFN_vkDeviceWaitIdle device_idle = (PFN_vkDeviceWaitIdle)semaphore_hook("vkDeviceWaitIdle"); @@ -92,6 +170,7 @@ int main(void) { expect_queue_lock = producer; assert(queue_idle(queue) == VK_ERROR_DEVICE_LOST); assert(device_idle(device) == VK_ERROR_DEVICE_LOST); + assert(g_producer_idle_calls == 0); } g_dev[0].producer = 0; VkExportSemaphoreCreateInfo export_info = {VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO, NULL, diff --git a/tools/vr/tests/test_xr_chaperone.cpp b/tools/vr/tests/test_xr_chaperone.cpp index fb60a20c48..9051047f86 100644 --- a/tools/vr/tests/test_xr_chaperone.cpp +++ b/tools/vr/tests/test_xr_chaperone.cpp @@ -4,6 +4,22 @@ int main() { DroidDeckXrPose head{{0, 0, 0, 1}, {2, 1.6f, -3}, 15}; + XrChaperoneOrigin deferred; + DroidDeckXrTracking tracking{}; + tracking.active = 1; tracking.period_ns = 11111111; tracking.sampled_ns = XrTrackingFile::Now(); + tracking.eyes[0].width = tracking.eyes[0].height = 4; + unsigned publications = 0; + auto publish = [&](const auto &pose) { publications++; assert(pose.position[1] == 1.6f); return true; }; + deferred.Update(tracking, publish); assert(publications == 0); + tracking.head = head; tracking.head.flags = 0; + deferred.Update(tracking, publish); assert(publications == 0); + tracking.head = head; + deferred.Update(tracking, publish); assert(publications == 1); + tracking.head.position[1] = 2; + deferred.Update(tracking, publish); assert(publications == 1); + deferred.Reset(); tracking.head = head; + deferred.Update(tracking, [&](const auto &) { publications++; return false; }); + deferred.Update(tracking, publish); assert(publications == 3); auto origin = xr_seated_origin(head); assert(origin.translation[0] == -2 && std::abs(origin.translation[1] + 1.6) < 1e-6 && origin.translation[2] == 3); head.orientation[1] = head.orientation[3] = std::sqrt(0.5f); diff --git a/tools/vr/tests/test_xr_frames.cpp b/tools/vr/tests/test_xr_frames.cpp index ecc03088de..f47b4ea56a 100644 --- a/tools/vr/tests/test_xr_frames.cpp +++ b/tools/vr/tests/test_xr_frames.cpp @@ -86,6 +86,7 @@ int main(int argc, char **argv) { assert(taken.header.frame_id == 1 && taken.pixels[0] == 17 && taken.header.eye_pose[0][0][3] == -0.032f); assert(taken.header.color_space == DROIDDECK_XR_COLOR_SRGB_RGBA8); assert(!receiver.Take(&taken)); + usleep(350000); for (uint64_t id = 2; id <= 100; id++) { auto header = Header(id); header.eye_pose[0][0][3] = static_cast(id); pixels.assign(header.bytes, static_cast(id));