diff --git a/.github/actions/layer-dependencies/action.yml b/.github/actions/layer-dependencies/action.yml new file mode 100644 index 00000000..4508976b --- /dev/null +++ b/.github/actions/layer-dependencies/action.yml @@ -0,0 +1,84 @@ +name: Layer build dependencies +description: >- + The pinned vcpkg registry and its OpenXR loader, and the archives the layer's CMake downloads + (Vulkan headers, DXC), cached between runs. GitHub's release downloads sometimes answer 5xx for + a while, so a cache hit downloads nothing, and every download that is still needed is retried. + +runs: + using: composite + steps: + - name: Checkout pinned vcpkg registry + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + repository: microsoft/vcpkg + ref: aae277acf4e7de287ddb5e208b5316614de6aad7 + path: .vcpkg + persist-credentials: false + + # After the registry's checkout, which would clear these. + - name: Cache vcpkg tool and OpenXR loader + uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: | + .vcpkg/vcpkg.exe + .vcpkg/installed + key: openxr-loader-v1-${{ runner.os }}-aae277acf4e7de287ddb5e208b5316614de6aad7 + + - name: Cache layer downloads + uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: .downloads + key: layer-downloads-v1-${{ runner.os }}-${{ hashFiles('layer/CMakeLists.txt') }} + restore-keys: | + layer-downloads-v1-${{ runner.os }}- + + - name: Install OpenXR loader + shell: pwsh + run: | + $vcpkgRoot = Join-Path $env:GITHUB_WORKSPACE ".vcpkg" + # Each download is tried a few times before failing. + function Invoke-WithRetry([string]$What, [int]$Attempts, [scriptblock]$Step) { + for ($attempt = 1; $attempt -le $Attempts; $attempt++) { + & $Step | Out-Host + if ($LASTEXITCODE -eq 0) { return $true } + if ($attempt -lt $Attempts) { + $delay = 20 * $attempt + Write-Host "$What failed (attempt $attempt of $Attempts); retrying in $delay s." + Start-Sleep -Seconds $delay + } + } + return $false + } + + $vcpkgExe = Join-Path $vcpkgRoot "vcpkg.exe" + if (Test-Path $vcpkgExe) { + Write-Host "Using the cached vcpkg tool." + } else { + $bootstrapped = Invoke-WithRetry "Bootstrapping the pinned vcpkg registry" 3 { + & "$vcpkgRoot\bootstrap-vcpkg.bat" -disableMetrics + } + if (-not $bootstrapped) { + # The runner image ships a vcpkg tool: use it with the pinned + # registry rather than fail on the tool's download. + $preinstalled = if ($env:VCPKG_INSTALLATION_ROOT) { Join-Path $env:VCPKG_INSTALLATION_ROOT "vcpkg.exe" } + if (-not $preinstalled -or -not (Test-Path $preinstalled)) { + throw "Failed to bootstrap the pinned vcpkg registry, and the runner has no vcpkg tool to fall back on." + } + Write-Host "Using the runner's vcpkg tool ($preinstalled) with the pinned registry." + Copy-Item -LiteralPath $preinstalled -Destination $vcpkgExe -Force + } + } + + "VCPKG_ROOT=$vcpkgRoot" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append + $env:VCPKG_ROOT = $vcpkgRoot + # Already installed (a cache hit), this only checks the package. + $installed = Invoke-WithRetry "Installing the pinned OpenXR loader dependency" 5 { + & $vcpkgExe install openxr-loader:x64-windows + } + if (-not $installed) { + throw "Failed to install the pinned OpenXR loader dependency." + } + + # layer/CMakeLists.txt keeps its pinned archives here (and retries their downloads). + "DEPTHXR_DOWNLOAD_CACHE=$(Join-Path $env:GITHUB_WORKSPACE '.downloads')" | + Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 88dceb00..4bd02a7a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,9 +9,12 @@ on: permissions: contents: read +# The layer's tests split across two runners that run at once: those that create no graphics device (beside +# the app's checks) and those that do, which run one at a time on WARP. "App tests and build", the check the +# master ruleset requires, passes only when both do. jobs: app: - name: App tests and build + name: App and layer CPU tests runs-on: windows-2022 steps: @@ -20,14 +23,6 @@ jobs: with: persist-credentials: false - - name: Checkout pinned vcpkg registry - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - repository: microsoft/vcpkg - ref: aae277acf4e7de287ddb5e208b5316614de6aad7 - path: .vcpkg - persist-credentials: false - - name: Set up Node uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0 with: @@ -40,21 +35,8 @@ jobs: with: toolchain: 1.94.1 - - name: Install OpenXR loader - shell: pwsh - run: | - $vcpkgRoot = Join-Path $env:GITHUB_WORKSPACE ".vcpkg" - & "$vcpkgRoot\bootstrap-vcpkg.bat" -disableMetrics - if ($LASTEXITCODE -ne 0) { - throw "Failed to bootstrap the pinned vcpkg registry." - } - - "VCPKG_ROOT=$vcpkgRoot" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append - $env:VCPKG_ROOT = $vcpkgRoot - & "$vcpkgRoot\vcpkg.exe" install openxr-loader:x64-windows - if ($LASTEXITCODE -ne 0) { - throw "Failed to install the pinned OpenXR loader dependency." - } + - name: Layer build dependencies + uses: ./.github/actions/layer-dependencies - name: Install dependencies working-directory: app @@ -68,12 +50,55 @@ jobs: working-directory: app run: npm run build - - name: Build and test OpenXR layer + - name: Build OpenXR layer and run its CPU tests shell: pwsh run: | - ./scripts/Build-Layer.ps1 + ./scripts/Build-Layer.ps1 -Tests Cpu ./scripts/Stage-ReleasePayload.ps1 - name: Check Tauri app working-directory: app/src-tauri run: cargo check --locked + + layer-graphics: + name: Layer graphics tests + runs-on: windows-2022 + + steps: + - name: Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Layer build dependencies + uses: ./.github/actions/layer-dependencies + + - name: Build OpenXR layer and run its graphics tests + shell: pwsh + run: ./scripts/Build-Layer.ps1 -Tests Graphics + + # The bridge layer tests keep each case's layer log under %TEMP%; a + # failing timing check needs it to be told apart from a slow runner. + - name: Upload layer test logs + if: failure() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: layer-graphics-test-logs + path: ~/AppData/Local/Temp/vectorxr-*/logs/ + if-no-files-found: ignore + retention-days: 14 + + required: + name: App tests and build + needs: [app, layer-graphics] + if: always() + runs-on: ubuntu-latest + + steps: + - name: Both halves passed + env: + APP: ${{ needs.app.result }} + GRAPHICS: ${{ needs.layer-graphics.result }} + run: | + echo "App and layer CPU tests: $APP; layer graphics tests: $GRAPHICS" + test "$APP" = success && test "$GRAPHICS" = success diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index fdd0e4ae..e4c7925b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -29,14 +29,6 @@ jobs: fetch-depth: 0 persist-credentials: false - - name: Checkout pinned vcpkg registry - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - repository: microsoft/vcpkg - ref: aae277acf4e7de287ddb5e208b5316614de6aad7 - path: .vcpkg - persist-credentials: false - - name: Verify tagged commit belongs to master if: github.event_name == 'push' shell: pwsh @@ -71,21 +63,8 @@ jobs: working-directory: app run: npm test - - name: Install OpenXR loader - shell: pwsh - run: | - $vcpkgRoot = Join-Path $env:GITHUB_WORKSPACE ".vcpkg" - & "$vcpkgRoot\bootstrap-vcpkg.bat" -disableMetrics - if ($LASTEXITCODE -ne 0) { - throw "Failed to bootstrap the pinned vcpkg registry." - } - - "VCPKG_ROOT=$vcpkgRoot" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append - $env:VCPKG_ROOT = $vcpkgRoot - & "$vcpkgRoot\vcpkg.exe" install openxr-loader:x64-windows - if ($LASTEXITCODE -ne 0) { - throw "Failed to install the pinned OpenXR loader dependency." - } + - name: Layer build dependencies + uses: ./.github/actions/layer-dependencies - name: Build and stage OpenXR layer shell: pwsh diff --git a/app/package-lock.json b/app/package-lock.json index 9051f546..7c7428d0 100644 --- a/app/package-lock.json +++ b/app/package-lock.json @@ -1,12 +1,12 @@ { "name": "vectorxr-app", - "version": "0.20.0", + "version": "0.20.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "vectorxr-app", - "version": "0.20.0", + "version": "0.20.1", "license": "MPL-2.0", "dependencies": { "@tauri-apps/api": "~2.10.0", diff --git a/app/package.json b/app/package.json index 388570a8..9687006e 100644 --- a/app/package.json +++ b/app/package.json @@ -1,7 +1,7 @@ { "name": "vectorxr-app", "private": true, - "version": "0.20.0", + "version": "0.20.1", "license": "MPL-2.0", "type": "module", "scripts": { diff --git a/app/src-tauri/Cargo.lock b/app/src-tauri/Cargo.lock index ebcdbb32..1d054bdb 100644 --- a/app/src-tauri/Cargo.lock +++ b/app/src-tauri/Cargo.lock @@ -4030,7 +4030,7 @@ dependencies = [ [[package]] name = "vectorxr-app" -version = "0.20.0" +version = "0.20.1" dependencies = [ "serde", "serde_json", diff --git a/app/src-tauri/Cargo.toml b/app/src-tauri/Cargo.toml index df4f4f60..d586aaf7 100644 --- a/app/src-tauri/Cargo.toml +++ b/app/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "vectorxr-app" -version = "0.20.0" +version = "0.20.1" description = "VectorXR configuration app" authors = ["mdiener"] edition = "2021" diff --git a/app/src-tauri/src/main.rs b/app/src-tauri/src/main.rs index 2b35c731..219951e3 100644 --- a/app/src-tauri/src/main.rs +++ b/app/src-tauri/src/main.rs @@ -916,6 +916,10 @@ fn default_framegen_diagnostic_view() -> String { "off".into() } +fn is_true(value: &bool) -> bool { + *value +} + fn is_false(value: &bool) -> bool { !*value } @@ -945,6 +949,12 @@ struct FrameGenSettings { // Head prediction past the runtime's limit. #[serde(default, skip_serializing_if = "is_false")] extend_head_prediction: bool, + // Edge fill: margins past the game's field of view (written only when on). + #[serde(default, skip_serializing_if = "is_false")] + edge_fill: bool, + // Use the game's depth (written only when off; absent means on). + #[serde(default = "default_true", skip_serializing_if = "is_true")] + use_game_depth: bool, } fn default_framegen_interpolate_flow() -> String { "auto".to_owned() } @@ -960,6 +970,8 @@ impl Default for FrameGenSettings { artifact_suppression: default_framegen_artifact_suppression(), motion_limit_percent: default_framegen_motion_limit_percent(), extend_head_prediction: false, + edge_fill: false, + use_game_depth: true, } } } @@ -3418,6 +3430,8 @@ mod tests { assert_eq!(defaults.defaults.artifact_suppression, 50); assert_eq!(defaults.defaults.motion_limit_percent, 10); assert!(!defaults.defaults.extend_head_prediction); + assert!(!defaults.defaults.edge_fill); + assert!(defaults.defaults.use_game_depth); assert!(defaults.profiles.is_empty()); // Older layers reject unknown keys (current ones ignore them with a @@ -3435,12 +3449,17 @@ mod tests { "settings": { "synthesisMode": "interpolate", "flowQuality": "performance", "motionEstimation": "shader", "interpolateFlow": "shader", "frameMultiplier": "3x", "artifactSuppression": 0, "motionLimitPercent": 25, - "extendHeadPrediction": true + "extendHeadPrediction": true, + "edgeFill": true, + "useGameDepth": false } }] }); let config: super::FrameGenModuleConfig = serde_json::from_value(input.clone()).unwrap(); assert!(config.profiles[0].settings.extend_head_prediction); + assert!(config.profiles[0].settings.edge_fill); + assert!(!config.profiles[0].settings.use_game_depth); + assert!(config.defaults.use_game_depth); assert_eq!(serde_json::to_value(config).unwrap(), input); // Settings saved without a frame multiplier load as Dynamic and save it. diff --git a/app/src-tauri/tauri.conf.json b/app/src-tauri/tauri.conf.json index 7606e7df..990be15d 100644 --- a/app/src-tauri/tauri.conf.json +++ b/app/src-tauri/tauri.conf.json @@ -1,7 +1,7 @@ { "$schema": "https://schema.tauri.app/config/2", "productName": "VectorXR", - "version": "0.20.0", + "version": "0.20.1", "identifier": "local.vectorxr.app", "build": { "beforeDevCommand": "npm run dev", diff --git a/app/src/App.vue b/app/src/App.vue index a761da7b..f015c18b 100644 --- a/app/src/App.vue +++ b/app/src/App.vue @@ -43,6 +43,11 @@ const turboInUse = computed( store.state.config.modules.turbo.enabled || store.state.config.modules.turbo.profiles.some((profile) => profile.enabled), ) +const frameGenInUse = computed( + () => + store.state.config.modules.framegen.enabled || + store.state.config.modules.framegen.profiles.some((profile) => profile.enabled), +) const quadviewsInUse = computed( () => store.state.config.modules.quadviews.enabled || @@ -596,6 +601,7 @@ async function confirmResetConfig() { :machine-writes-unlocked="openXrMachineWritesUnlocked" :turbo-in-use="turboInUse" :quadviews-in-use="quadviewsInUse" + :frame-gen-in-use="frameGenInUse" @refresh="refreshOpenXrLayers(true)" @machine-writes-unlocked="openXrMachineWritesUnlocked = $event" @snapshot-updated="openXrLayerSnapshot = $event; refreshOpenXrLayers(true)" @@ -634,6 +640,7 @@ async function confirmResetConfig() { :config="store.state.config" :applications="store.state.config.applications" @add-pivot-profile="store.addPivotProfile" + @duplicate-pivot-profile="store.duplicatePivotProfile" @remove-pivot-profile="store.removePivotProfile" @move-pivot-profile="store.movePivotProfile" @sync-pivot-profile-name="store.syncPivotProfileName" diff --git a/app/src/components/FrameGenSettingsFields.vue b/app/src/components/FrameGenSettingsFields.vue index 96c58d0b..2ab87cef 100644 --- a/app/src/components/FrameGenSettingsFields.vue +++ b/app/src/components/FrameGenSettingsFields.vue @@ -132,19 +132,30 @@ const advancedAtDefaults = computed(() => props.settings.flowQuality === advancedDefaults.flowQuality && props.settings.artifactSuppression === advancedDefaults.artifactSuppression && props.settings.motionLimitPercent === advancedDefaults.motionLimitPercent && - !props.settings.extendHeadPrediction) + !props.settings.extendHeadPrediction && + props.settings.useGameDepth !== false) const advancedSummary = computed(() => t(props.settings.extendHeadPrediction ? 'framegen.settings.advanced.summaryHeadPrediction' : 'framegen.settings.advanced.summary', { flow: flowQuality.value.summary, suppression: props.settings.artifactSuppression, motionLimit: props.settings.motionLimitPercent, - })) + }) + (props.settings.useGameDepth === false ? ' · ' + t('framegen.settings.gameDepth.summaryOff') : '')) + +// Stored only when off, like the layer reads it: absent means on. +const useGameDepth = computed({ + get: () => props.settings.useGameDepth !== false, + set: (on: boolean) => { + if (on) delete props.settings.useGameDepth + else props.settings.useGameDepth = false + }, +}) function resetAdvanced() { props.settings.flowQuality = advancedDefaults.flowQuality props.settings.artifactSuppression = advancedDefaults.artifactSuppression props.settings.motionLimitPercent = advancedDefaults.motionLimitPercent delete props.settings.extendHeadPrediction + delete props.settings.useGameDepth } @@ -289,6 +300,23 @@ function resetAdvanced() { + + + diff --git a/app/src/components/tabs/OpenXrLayersTab.vue b/app/src/components/tabs/OpenXrLayersTab.vue index f5c213d5..1fe2417e 100644 --- a/app/src/components/tabs/OpenXrLayersTab.vue +++ b/app/src/components/tabs/OpenXrLayersTab.vue @@ -24,6 +24,7 @@ const props = defineProps<{ machineWritesUnlocked: boolean turboInUse: boolean quadviewsInUse: boolean + frameGenInUse: boolean }>() const emit = defineEmits<{ @@ -86,6 +87,20 @@ const toolkitQuadSlices = computed(() => { }).map((slice) => sliceLabel(slice)) }) +// OpenXR Toolkit below VectorXR can't take Frame Generation's extra frames, so +// the layer keeps Frame Generation off in that order (unless the Toolkit is +// bypassed for the game, which the app can't see). +const toolkitFrameGenSlices = computed(() => { + if (!props.frameGenInUse) { + return [] + } + return (props.snapshot?.slices ?? []).filter((slice) => { + const vector = slice.layers.find((layer) => layer.isVectorXr && layer.enabled) + const toolkit = slice.layers.find((layer) => layer.enabled && layer.layerName.toLowerCase().includes('mbucchia_toolkit')) + return vector && toolkit && toolkit.order > vector.order + }).map((slice) => sliceLabel(slice)) +}) + // The backend names each slice in English; show the name for its id when the // catalog has one. function sliceLabel(slice: OpenXrLayerRegistrySlice): string { @@ -486,6 +501,15 @@ function signatureGuidance(layer: OpenXrLayerEntry): string { {{ t('layers.warnings.toolkitQuad', { slices: formatList(toolkitQuadSlices) }) }} +
+ {{ t('layers.warnings.frameGenOffLabel') }} + {{ t('layers.warnings.toolkitFrameGen', { slices: formatList(toolkitFrameGenSlices) }) }} +
+
diff --git a/app/src/components/tabs/PivotXrTab.vue b/app/src/components/tabs/PivotXrTab.vue index 05a8d43c..618dc3c2 100644 --- a/app/src/components/tabs/PivotXrTab.vue +++ b/app/src/components/tabs/PivotXrTab.vue @@ -25,14 +25,32 @@ const props = defineProps<{ applications: RegisteredApplication[] }>() -defineEmits<{ +const emit = defineEmits<{ addPivotProfile: [] + duplicatePivotProfile: [index: number] removePivotProfile: [index: number] movePivotProfile: [index: number, direction: -1 | 1] syncPivotProfileName: [index: number] }>() const compatibilityInfoOpen = ref(false) + +// After a duplicate, bring the copy into view and pulse it briefly so it's +// clear where it landed. 'nearest' leaves the page alone when it's visible. +const flashProfileId = ref(null) +let flashTimer: ReturnType | undefined + +async function duplicateProfile(index: number) { + emit('duplicatePivotProfile', index) + const copy = props.config.modules.pivotxr.profiles[index + 1] + if (!copy) return + await nextTick() + const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches + document.querySelector(`[data-profile-id="${CSS.escape(copy.id)}"]`)?.scrollIntoView({ behavior: reduceMotion ? 'auto' : 'smooth', block: 'nearest' }) // i18n-ignore + flashProfileId.value = copy.id + clearTimeout(flashTimer) + flashTimer = setTimeout(() => { flashProfileId.value = null }, 1200) +} const centeringInfoOpen = ref(false) // Bindings are edited on a dedicated sub-page (not a dialog) so the editors @@ -472,6 +490,9 @@ const profileOverviewWarningIndexes = computed(() => profileWarningState.value.o profileWarningState.value.o :warning-title="t('pivot.tab.bindingWarning')" :warnings="profileWarnings.get(index)" reorderable + duplicable :profile-count="config.modules.pivotxr.profiles.length" + @duplicate="duplicateProfile(index)" @remove="$emit('removePivotProfile', index)" @move="$emit('movePivotProfile', index, $event)" @sync-name="$emit('syncPivotProfileName', index)" @@ -613,9 +636,38 @@ const profileOverviewWarningIndexes = computed(() => profileWarningState.value.o transition: transform 0.3s ease; } +/* A new card (Add or Duplicate) fades and slides in; the cards below it slide + down via the move class above. */ +.pivot-profile-enter-active { + transition: opacity 0.25s ease, transform 0.25s ease; +} +.pivot-profile-enter-from { + opacity: 0; + transform: translateY(-0.5rem); +} + +/* Keeps a scrolled-to card clear of the sticky Custom Profiles header. */ +.pivot-profile-card { + scroll-margin-top: 6rem; + scroll-margin-bottom: 1rem; +} + +/* Brief accent ring on a fresh duplicate; it fades back out on its own. */ +.pivot-profile-flash { + animation: pivot-profile-flash 1.2s ease-out; +} +@keyframes pivot-profile-flash { + 0%, 35% { outline: 2px solid var(--app-accent); outline-offset: 2px; } + 100% { outline: 2px solid transparent; outline-offset: 2px; } +} + @media (prefers-reduced-motion: reduce) { - .pivot-profile-move { + .pivot-profile-move, + .pivot-profile-enter-active { transition: none; } + .pivot-profile-flash { + animation: none; + } } diff --git a/app/src/i18n/locales/de/app.json b/app/src/i18n/locales/de/app.json index 54d3d870..d9f297a4 100644 --- a/app/src/i18n/locales/de/app.json +++ b/app/src/i18n/locales/de/app.json @@ -144,6 +144,7 @@ "priority": "Prioritätsreihenfolge – wenn sich Profile bei einer Anwendung überschneiden, gehört jede gemeinsame Belegung dem höheren Profil.", "moveUp": "Profil nach oben verschieben (höhere Priorität)", "moveDown": "Profil nach unten verschieben (niedrigere Priorität)", + "duplicateTitle": "Dieses Profil mit seinen Einstellungen und Belegungen als neues Profil darunter kopieren.", "enabledTitle": "Dieses Profil wendet eigene {module}-Einstellungen auf die zugewiesenen Anwendungen an.", "disabledTitle": "Dieses Profil wird zur Laufzeit ignoriert.", "on": "Profil ein", diff --git a/app/src/i18n/locales/de/framegen.json b/app/src/i18n/locales/de/framegen.json index c73d5e91..97c013ae 100644 --- a/app/src/i18n/locales/de/framegen.json +++ b/app/src/i18n/locales/de/framegen.json @@ -229,6 +229,12 @@ "valueLabel": "Wert der Bewegungsgrenze in Prozent der Ansichtsbreite", "description": "Wie weit ein generiertes Bild das Bild höchstens verschieben darf, als Anteil der Ansichtsbreite. Bei schnellerer Bewegung wird das echte Bild angezeigt." }, + "gameDepth": { + "heading": "Spieltiefe verwenden", + "description": "Nutzt die Tiefe, die das Spiel liefert, damit nahe Objekte stabil bleiben. Schalte die Option aus, wenn ein Spiel mit Frame Generation ruckelt, hängt oder Tiefenartefakte zeigt; Frame Generation arbeitet dann ohne Tiefe.", + "restart": "Wirkt nach einem Neustart des Spiels", + "summaryOff": "Ohne Spieltiefe" + }, "headPrediction": { "heading": "Kopfvorhersage erweitern", "description": "Frame Generation lässt das Spiel bis zu ~150 ms im Voraus rendern, und manche Laufzeiten, etwa die von Pimax, sagen Ihren Kopf nur ~20 ms davon voraus. Diese Option führt die Drehung Ihres Kopfes über den Rest fort, für weniger Schwarz an dem Rand, zu dem Sie sich drehen. Schalten Sie sie vor dem Start des Spiels ein." diff --git a/app/src/i18n/locales/de/layers.json b/app/src/i18n/locales/de/layers.json index d0528e93..8a37602e 100644 --- a/app/src/i18n/locales/de/layers.json +++ b/app/src/i18n/locales/de/layers.json @@ -21,7 +21,9 @@ "turboLabel": "Turbo-Konflikt:", "toolkitTurbo": "OpenXR Toolkit ist aktiviert, während VectorXR Turbo verwendet wird. Zwei Layer zur Bildtaktung behindern sich gegenseitig – wenn der Turbo-Modus von OXRTK eingeschaltet ist, deaktivieren Sie ihn dort oder in VectorXR, niemals beide.", "quadviewsOffLabel": "Quadviews aus:", - "toolkitQuad": "OpenXR Toolkit steht über VectorXR ({slices}). Es unterstützt keine Quad Views, daher lässt VectorXR Quadviews für jedes Spiel ausgeschaltet, in dem OpenXR Toolkit läuft. Deaktivieren Sie OpenXR Toolkit oder schalten Sie es für diese Spiele aus, um Quadviews zu verwenden." + "toolkitQuad": "OpenXR Toolkit steht über VectorXR ({slices}). Es unterstützt keine Quad Views, daher lässt VectorXR Quadviews für jedes Spiel ausgeschaltet, in dem OpenXR Toolkit läuft. Deaktivieren Sie OpenXR Toolkit oder schalten Sie es für diese Spiele aus, um Quadviews zu verwenden.", + "frameGenOffLabel": "Frame Generation aus:", + "toolkitFrameGen": "OpenXR Toolkit liegt unter VectorXR ({slices}). Dort kann es die zusätzlichen Bilder von Frame Generation nicht verarbeiten, daher lässt VectorXR Frame Generation in jedem Spiel aus, in dem OpenXR Toolkit läuft. Verschiebe OpenXR Toolkit über VectorXR oder schalte es für diese Spiele aus, um Frame Generation zu nutzen." }, "status": { "unlocked": "Computerweite Schreibvorgänge für OpenXR-Layer sind für diese App-Sitzung entsperrt.", diff --git a/app/src/i18n/locales/de/pivot.json b/app/src/i18n/locales/de/pivot.json index 3a54e7c1..df4d6195 100644 --- a/app/src/i18n/locales/de/pivot.json +++ b/app/src/i18n/locales/de/pivot.json @@ -16,7 +16,8 @@ "noCustomProfiles": "Noch keine eigenen Profile. Fügen Sie ein Profil hinzu, um die Pivot-Drehung an eine bestimmte Anwendung zu binden.", "profileNumber": "Profil {number}", "nudgeSetDefaultName": "Nudge-Satz", - "nudgeSetCopyName": "{name} Kopie" + "nudgeSetCopyName": "{name} Kopie", + "profileCopyName": "{name} Kopie" }, "warnings": { "noActivationTitle": "Keine Aktivierung für Motion Assist", diff --git a/app/src/i18n/locales/en/app.json b/app/src/i18n/locales/en/app.json index a313ae27..f2aa1b5b 100644 --- a/app/src/i18n/locales/en/app.json +++ b/app/src/i18n/locales/en/app.json @@ -144,6 +144,7 @@ "priority": "Priority order — when profiles overlap on an application, the higher profile owns any binding they share.", "moveUp": "Move profile up (higher priority)", "moveDown": "Move profile down (lower priority)", + "duplicateTitle": "Copy this profile, with its settings and bindings, into a new profile below it.", "enabledTitle": "This profile applies custom {module} settings to its assigned applications.", "disabledTitle": "This profile is ignored at runtime.", "on": "Profile On", diff --git a/app/src/i18n/locales/en/framegen.json b/app/src/i18n/locales/en/framegen.json index a3ee720e..8073905f 100644 --- a/app/src/i18n/locales/en/framegen.json +++ b/app/src/i18n/locales/en/framegen.json @@ -229,6 +229,12 @@ "valueLabel": "Motion limit value in percent of view width", "description": "The farthest a generated frame may move the image, as a share of view width. Faster motion shows the real image." }, + "gameDepth": { + "heading": "Use game depth", + "description": "Uses the depth the game provides to keep near objects steady. Turn off if a game stutters, hangs or shows depth artifacts with Frame Generation; Frame Generation then works without depth.", + "restart": "Takes effect when the game restarts", + "summaryOff": "Without game depth" + }, "headPrediction": { "heading": "Extend head prediction", "description": "Frame Generation has the game render up to ~150 ms ahead, and some runtimes, such as Pimax's, predict your head only ~20 ms of that. This carries your head's turn over the rest, for less black at the edge you turn toward. Turn it on before starting the game." diff --git a/app/src/i18n/locales/en/layers.json b/app/src/i18n/locales/en/layers.json index 2d108056..139c3507 100644 --- a/app/src/i18n/locales/en/layers.json +++ b/app/src/i18n/locales/en/layers.json @@ -21,7 +21,9 @@ "turboLabel": "Turbo conflict:", "toolkitTurbo": "OpenXR Toolkit is enabled while VectorXR Turbo is in use. Two frame-pacing layers fight each other — if OXRTK's Turbo Mode is on, disable it there or in VectorXR, never both.", "quadviewsOffLabel": "Quadviews off:", - "toolkitQuad": "OpenXR Toolkit is above VectorXR ({slices}). It does not support quad views, so VectorXR keeps Quadviews off for every game OpenXR Toolkit runs in. Disable OpenXR Toolkit, or turn it off for those games, to use Quadviews." + "toolkitQuad": "OpenXR Toolkit is above VectorXR ({slices}). It does not support quad views, so VectorXR keeps Quadviews off for every game OpenXR Toolkit runs in. Disable OpenXR Toolkit, or turn it off for those games, to use Quadviews.", + "frameGenOffLabel": "Frame Generation off:", + "toolkitFrameGen": "OpenXR Toolkit is below VectorXR ({slices}). It can't handle Frame Generation's extra frames there, so VectorXR keeps Frame Generation off in every game OpenXR Toolkit runs in. Move OpenXR Toolkit above VectorXR, or turn it off for those games, to use Frame Generation." }, "status": { "unlocked": "Machine-wide OpenXR layer writes are unlocked for this app session.", diff --git a/app/src/i18n/locales/en/pivot.json b/app/src/i18n/locales/en/pivot.json index 17bd8511..d2f4c1c5 100644 --- a/app/src/i18n/locales/en/pivot.json +++ b/app/src/i18n/locales/en/pivot.json @@ -16,7 +16,8 @@ "noCustomProfiles": "No custom profiles yet. Add a profile to bind Pivot rotation to a specific application.", "profileNumber": "Profile {number}", "nudgeSetDefaultName": "Nudge Set", - "nudgeSetCopyName": "{name} Copy" + "nudgeSetCopyName": "{name} Copy", + "profileCopyName": "{name} Copy" }, "warnings": { "noActivationTitle": "No Motion Assist activation", diff --git a/app/src/i18n/locales/es-ES/app.json b/app/src/i18n/locales/es-ES/app.json index 5ffbb1c6..c11519a9 100644 --- a/app/src/i18n/locales/es-ES/app.json +++ b/app/src/i18n/locales/es-ES/app.json @@ -144,6 +144,7 @@ "priority": "Orden de prioridad: cuando los perfiles coinciden en una aplicación, el perfil superior se queda con las asignaciones que compartan.", "moveUp": "Subir perfil (mayor prioridad)", "moveDown": "Bajar perfil (menor prioridad)", + "duplicateTitle": "Copia este perfil, con sus ajustes y asignaciones, en un perfil nuevo debajo.", "enabledTitle": "Este perfil aplica una configuración personalizada de {module} a sus aplicaciones asignadas.", "disabledTitle": "Este perfil se ignora en tiempo de ejecución.", "on": "Perfil activado", diff --git a/app/src/i18n/locales/es-ES/framegen.json b/app/src/i18n/locales/es-ES/framegen.json index ad739d69..dd62a68d 100644 --- a/app/src/i18n/locales/es-ES/framegen.json +++ b/app/src/i18n/locales/es-ES/framegen.json @@ -229,6 +229,12 @@ "valueLabel": "Valor del límite de movimiento en porcentaje del ancho de la vista", "description": "Lo máximo que un fotograma generado puede desplazar la imagen, como proporción del ancho de la vista. Un movimiento más rápido muestra la imagen real." }, + "gameDepth": { + "heading": "Usar la profundidad del juego", + "description": "Usa la profundidad que proporciona el juego para mantener estables los objetos cercanos. Desactívalo si un juego da tirones, se bloquea o muestra artefactos de profundidad con Frame Generation; Frame Generation funcionará entonces sin profundidad.", + "restart": "Se aplica al reiniciar el juego", + "summaryOff": "Sin profundidad del juego" + }, "headPrediction": { "heading": "Ampliar la predicción de cabeza", "description": "Frame Generation hace que el juego renderice hasta ~150 ms por adelantado, y algunos entornos de ejecución, como el de Pimax, solo predicen tu cabeza unos ~20 ms de ese tiempo. Esto prolonga el giro de tu cabeza durante el resto, para que haya menos negro en el borde hacia el que giras. Actívalo antes de iniciar el juego." diff --git a/app/src/i18n/locales/es-ES/layers.json b/app/src/i18n/locales/es-ES/layers.json index fafb3a7b..1a7644a0 100644 --- a/app/src/i18n/locales/es-ES/layers.json +++ b/app/src/i18n/locales/es-ES/layers.json @@ -21,7 +21,9 @@ "turboLabel": "Conflicto de Turbo:", "toolkitTurbo": "OpenXR Toolkit está activado mientras se usa Turbo de VectorXR. Dos capas de ritmo de fotogramas compiten entre sí: si el modo Turbo de OXRTK está activado, desactívalo allí o en VectorXR, nunca en ambos.", "quadviewsOffLabel": "Quadviews desactivado:", - "toolkitQuad": "OpenXR Toolkit está por encima de VectorXR ({slices}). No admite cuatro vistas, así que VectorXR mantiene Quadviews desactivado en todos los juegos en los que se ejecuta OpenXR Toolkit. Desactiva OpenXR Toolkit, o desactívalo para esos juegos, para usar Quadviews." + "toolkitQuad": "OpenXR Toolkit está por encima de VectorXR ({slices}). No admite cuatro vistas, así que VectorXR mantiene Quadviews desactivado en todos los juegos en los que se ejecuta OpenXR Toolkit. Desactiva OpenXR Toolkit, o desactívalo para esos juegos, para usar Quadviews.", + "frameGenOffLabel": "Frame Generation desactivado:", + "toolkitFrameGen": "OpenXR Toolkit está debajo de VectorXR ({slices}). Ahí no puede gestionar los fotogramas adicionales de Frame Generation, así que VectorXR mantiene Frame Generation desactivado en todos los juegos en los que se ejecuta OpenXR Toolkit. Mueve OpenXR Toolkit por encima de VectorXR, o desactívalo para esos juegos, para usar Frame Generation." }, "status": { "unlocked": "La escritura de capas OpenXR para todo el equipo está desbloqueada durante esta sesión de la aplicación.", diff --git a/app/src/i18n/locales/es-ES/pivot.json b/app/src/i18n/locales/es-ES/pivot.json index e459260e..073ba013 100644 --- a/app/src/i18n/locales/es-ES/pivot.json +++ b/app/src/i18n/locales/es-ES/pivot.json @@ -16,7 +16,8 @@ "noCustomProfiles": "Todavía no hay perfiles personalizados. Agrega un perfil para vincular la rotación de Pivot a una aplicación concreta.", "profileNumber": "Perfil {number}", "nudgeSetDefaultName": "Conjunto de ajustes finos", - "nudgeSetCopyName": "Copia de {name}" + "nudgeSetCopyName": "Copia de {name}", + "profileCopyName": "Copia de {name}" }, "warnings": { "noActivationTitle": "Sin activación de la asistencia de movimiento", diff --git a/app/src/i18n/locales/fr/app.json b/app/src/i18n/locales/fr/app.json index 714d7359..c93b8965 100644 --- a/app/src/i18n/locales/fr/app.json +++ b/app/src/i18n/locales/fr/app.json @@ -144,6 +144,7 @@ "priority": "Ordre de priorité — lorsque des profils se chevauchent sur une application, le profil le plus haut détient toute affectation qu'ils partagent.", "moveUp": "Monter le profil (priorité plus élevée)", "moveDown": "Descendre le profil (priorité plus basse)", + "duplicateTitle": "Copier ce profil, avec ses réglages et ses assignations, dans un nouveau profil en dessous.", "enabledTitle": "Ce profil applique des paramètres {module} personnalisés aux applications qui lui sont attribuées.", "disabledTitle": "Ce profil est ignoré à l'exécution.", "on": "Profil activé", diff --git a/app/src/i18n/locales/fr/framegen.json b/app/src/i18n/locales/fr/framegen.json index 36a1002e..af3b626f 100644 --- a/app/src/i18n/locales/fr/framegen.json +++ b/app/src/i18n/locales/fr/framegen.json @@ -229,6 +229,12 @@ "valueLabel": "Valeur de la limite de mouvement en pourcentage de la largeur de vue", "description": "Le déplacement maximal qu'une image générée peut appliquer à l'image, en proportion de la largeur de vue. Un mouvement plus rapide affiche l'image réelle." }, + "gameDepth": { + "heading": "Utiliser la profondeur du jeu", + "description": "Utilise la profondeur fournie par le jeu pour garder les objets proches stables. Désactivez-la si un jeu saccade, se bloque ou présente des artefacts de profondeur avec Frame Generation ; Frame Generation fonctionne alors sans profondeur.", + "restart": "S’applique au redémarrage du jeu", + "summaryOff": "Sans profondeur du jeu" + }, "headPrediction": { "heading": "Étendre la prédiction de la tête", "description": "Frame Generation fait rendre le jeu jusqu'à ~150 ms à l'avance, et certains runtimes, comme celui de Pimax, ne prédisent votre tête que sur ~20 ms de cette durée. Cette option prolonge la rotation de votre tête sur le reste, pour moins de noir sur le bord vers lequel vous tournez. Activez-la avant de démarrer le jeu." diff --git a/app/src/i18n/locales/fr/layers.json b/app/src/i18n/locales/fr/layers.json index 44bb0443..33a1cf8f 100644 --- a/app/src/i18n/locales/fr/layers.json +++ b/app/src/i18n/locales/fr/layers.json @@ -21,7 +21,9 @@ "turboLabel": "Conflit Turbo :", "toolkitTurbo": "OpenXR Toolkit est activé alors que Turbo de VectorXR est utilisé. Deux couches de cadencement des images se contrarient — si le mode Turbo d'OXRTK est activé, désactivez-le là-bas ou dans VectorXR, jamais les deux.", "quadviewsOffLabel": "Quadviews désactivé :", - "toolkitQuad": "OpenXR Toolkit se trouve au-dessus de VectorXR ({slices}). Il ne prend pas en charge les quad views ; VectorXR garde donc Quadviews désactivé pour chaque jeu dans lequel OpenXR Toolkit s'exécute. Désactivez OpenXR Toolkit, ou désactivez-le pour ces jeux, pour utiliser Quadviews." + "toolkitQuad": "OpenXR Toolkit se trouve au-dessus de VectorXR ({slices}). Il ne prend pas en charge les quad views ; VectorXR garde donc Quadviews désactivé pour chaque jeu dans lequel OpenXR Toolkit s'exécute. Désactivez OpenXR Toolkit, ou désactivez-le pour ces jeux, pour utiliser Quadviews.", + "frameGenOffLabel": "Frame Generation désactivé :", + "toolkitFrameGen": "OpenXR Toolkit est sous VectorXR ({slices}). À cet endroit, il ne peut pas gérer les images supplémentaires de Frame Generation : VectorXR laisse donc Frame Generation désactivé dans tous les jeux où OpenXR Toolkit s’exécute. Placez OpenXR Toolkit au-dessus de VectorXR, ou désactivez-le pour ces jeux, pour utiliser Frame Generation." }, "status": { "unlocked": "Les écritures de couches OpenXR à l'échelle de la machine sont déverrouillées pour cette session de l'application.", diff --git a/app/src/i18n/locales/fr/pivot.json b/app/src/i18n/locales/fr/pivot.json index 0c265b71..5e79ab09 100644 --- a/app/src/i18n/locales/fr/pivot.json +++ b/app/src/i18n/locales/fr/pivot.json @@ -16,7 +16,8 @@ "noCustomProfiles": "Aucun profil personnalisé pour l'instant. Ajoutez un profil pour lier la rotation Pivot à une application précise.", "profileNumber": "Profil {number}", "nudgeSetDefaultName": "Jeu d'ajustements", - "nudgeSetCopyName": "Copie de {name}" + "nudgeSetCopyName": "Copie de {name}", + "profileCopyName": "Copie de {name}" }, "warnings": { "noActivationTitle": "Aucune activation de l'Assistance au mouvement", diff --git a/app/src/i18n/locales/ja/app.json b/app/src/i18n/locales/ja/app.json index b3c86892..4c3187e2 100644 --- a/app/src/i18n/locales/ja/app.json +++ b/app/src/i18n/locales/ja/app.json @@ -140,6 +140,7 @@ "priority": "優先順位 — 複数のプロファイルが同じアプリケーションで重なる場合、共有するバインドは上位のプロファイルが所有します。", "moveUp": "プロファイルを上へ移動 (優先度を上げる)", "moveDown": "プロファイルを下へ移動 (優先度を下げる)", + "duplicateTitle": "このプロファイルを設定と割り当てごと、すぐ下の新しいプロファイルにコピーします。", "enabledTitle": "このプロファイルは、割り当てられたアプリケーションにカスタムの {module} 設定を適用します。", "disabledTitle": "このプロファイルは実行時に無視されます。", "on": "プロファイル オン", diff --git a/app/src/i18n/locales/ja/framegen.json b/app/src/i18n/locales/ja/framegen.json index c41e4210..5f9b3a74 100644 --- a/app/src/i18n/locales/ja/framegen.json +++ b/app/src/i18n/locales/ja/framegen.json @@ -229,6 +229,12 @@ "valueLabel": "ビュー幅に対する割合 (%) で表した動きの上限の値", "description": "生成フレームが画像を移動できる最大量で、ビュー幅に対する割合で表します。これより速い動きでは実際の画像が表示されます。" }, + "gameDepth": { + "heading": "ゲームの深度を使用", + "description": "ゲームが提供する深度を使って、近くの物体を安定させます。Frame Generation 使用中にゲームがカクつく、停止する、または深度のアーティファクトが出る場合はオフにしてください。Frame Generation は深度なしで動作します。", + "restart": "ゲームの再起動後に反映されます", + "summaryOff": "ゲーム深度なし" + }, "headPrediction": { "heading": "頭の予測を延長", "description": "Frame Generation はゲームに最大約 150 ms 先までレンダリングさせますが、Pimax などの一部のランタイムはそのうち約 20 ms 分しか頭の動きを予測しません。この設定は残りの時間にも頭の回転を引き継ぎ、向いた側の端に出る黒い部分を減らします。ゲームを開始する前にオンにしてください。" diff --git a/app/src/i18n/locales/ja/layers.json b/app/src/i18n/locales/ja/layers.json index 2a8e4e4c..1e29ef8e 100644 --- a/app/src/i18n/locales/ja/layers.json +++ b/app/src/i18n/locales/ja/layers.json @@ -20,7 +20,9 @@ "turboLabel": "Turbo の競合:", "toolkitTurbo": "VectorXR Turbo の使用中に OpenXR Toolkit が有効になっています。2 つのフレーム ペーシング レイヤーは互いに干渉します。OXRTK の Turbo Mode がオンの場合は、OXRTK 側か VectorXR 側のどちらかで無効にしてください。両方で使用しないでください。", "quadviewsOffLabel": "Quadviews オフ:", - "toolkitQuad": "OpenXR Toolkit が VectorXR より上にあります ({slices})。OpenXR Toolkit はクアッド ビューに対応していないため、VectorXR は OpenXR Toolkit が動作するすべてのゲームで Quadviews をオフにします。Quadviews を使用するには、OpenXR Toolkit を無効にするか、それらのゲームでオフにしてください。" + "toolkitQuad": "OpenXR Toolkit が VectorXR より上にあります ({slices})。OpenXR Toolkit はクアッド ビューに対応していないため、VectorXR は OpenXR Toolkit が動作するすべてのゲームで Quadviews をオフにします。Quadviews を使用するには、OpenXR Toolkit を無効にするか、それらのゲームでオフにしてください。", + "frameGenOffLabel": "Frame Generation オフ:", + "toolkitFrameGen": "OpenXR Toolkit が VectorXR の下にあります({slices})。この位置では Frame Generation の追加フレームを処理できないため、OpenXR Toolkit が動作するすべてのゲームで VectorXR は Frame Generation をオフにします。Frame Generation を使うには、OpenXR Toolkit を VectorXR の上に移動するか、それらのゲームで無効にしてください。" }, "status": { "unlocked": "このアプリのセッションでは、コンピューター全体の OpenXR レイヤーへの書き込みがロック解除されています。", diff --git a/app/src/i18n/locales/ja/pivot.json b/app/src/i18n/locales/ja/pivot.json index 6322a73e..16a5bb0e 100644 --- a/app/src/i18n/locales/ja/pivot.json +++ b/app/src/i18n/locales/ja/pivot.json @@ -16,7 +16,8 @@ "noCustomProfiles": "カスタム プロファイルはまだありません。Pivot の回転を特定のアプリケーションにバインドするには、プロファイルを追加してください。", "profileNumber": "プロファイル {number}", "nudgeSetDefaultName": "微調整セット", - "nudgeSetCopyName": "{name} のコピー" + "nudgeSetCopyName": "{name} のコピー", + "profileCopyName": "{name} のコピー" }, "warnings": { "noActivationTitle": "モーション アシストの有効化がありません", diff --git a/app/src/i18n/locales/ko/app.json b/app/src/i18n/locales/ko/app.json index 7e2bcd74..686c9995 100644 --- a/app/src/i18n/locales/ko/app.json +++ b/app/src/i18n/locales/ko/app.json @@ -140,6 +140,7 @@ "priority": "우선순위: 여러 프로필이 같은 애플리케이션에 겹치면 위에 있는 프로필이 공유하는 바인딩을 소유합니다.", "moveUp": "프로필을 위로 이동(높은 우선순위)", "moveDown": "프로필을 아래로 이동(낮은 우선순위)", + "duplicateTitle": "이 프로필을 설정과 바인딩 그대로 바로 아래의 새 프로필로 복사합니다.", "enabledTitle": "이 프로필은 할당된 애플리케이션에 사용자 지정 {module} 설정을 적용합니다.", "disabledTitle": "이 프로필은 런타임에 무시됩니다.", "on": "프로필 켜짐", diff --git a/app/src/i18n/locales/ko/framegen.json b/app/src/i18n/locales/ko/framegen.json index 9c209114..6874308f 100644 --- a/app/src/i18n/locales/ko/framegen.json +++ b/app/src/i18n/locales/ko/framegen.json @@ -229,6 +229,12 @@ "valueLabel": "뷰 너비 대비 백분율로 나타낸 모션 한도 값", "description": "생성된 프레임이 이미지를 이동할 수 있는 최대 거리이며, 뷰 너비에 대한 비율입니다. 이보다 빠른 움직임에서는 실제 이미지가 표시됩니다." }, + "gameDepth": { + "heading": "게임 깊이 사용", + "description": "게임이 제공하는 깊이를 사용해 가까운 물체를 안정적으로 유지합니다. Frame Generation 사용 중 게임이 끊기거나 멈추거나 깊이 아티팩트가 보이면 끄세요. 그러면 Frame Generation이 깊이 없이 동작합니다.", + "restart": "게임을 다시 시작하면 적용됩니다", + "summaryOff": "게임 깊이 없음" + }, "headPrediction": { "heading": "머리 예측 확장", "description": "Frame Generation은 게임이 최대 약 150 ms 앞서 렌더링하도록 하는데, Pimax 등 일부 런타임은 그중 약 20 ms만큼만 머리 움직임을 예측합니다. 이 설정은 나머지 구간에도 머리 회전을 이어 적용하여, 머리를 돌리는 쪽 가장자리의 검은 영역을 줄입니다. 게임을 시작하기 전에 켜십시오." diff --git a/app/src/i18n/locales/ko/layers.json b/app/src/i18n/locales/ko/layers.json index e3ede637..51710d70 100644 --- a/app/src/i18n/locales/ko/layers.json +++ b/app/src/i18n/locales/ko/layers.json @@ -20,7 +20,9 @@ "turboLabel": "Turbo 충돌:", "toolkitTurbo": "VectorXR Turbo를 사용하는 동안 OpenXR Toolkit이 사용 설정되어 있습니다. 두 프레임 페이싱 레이어는 서로 충돌합니다. OXRTK의 Turbo Mode가 켜져 있으면 OXRTK 또는 VectorXR 중 한쪽에서만 끄고, 둘 다 켜 두지 마십시오.", "quadviewsOffLabel": "Quadviews 꺼짐:", - "toolkitQuad": "OpenXR Toolkit이 VectorXR보다 위에 있습니다({slices}). OpenXR Toolkit은 쿼드 뷰를 지원하지 않으므로 VectorXR은 OpenXR Toolkit이 실행되는 모든 게임에서 Quadviews를 꺼 둡니다. Quadviews를 사용하려면 OpenXR Toolkit을 사용 안 함으로 설정하거나 해당 게임에서 끄십시오." + "toolkitQuad": "OpenXR Toolkit이 VectorXR보다 위에 있습니다({slices}). OpenXR Toolkit은 쿼드 뷰를 지원하지 않으므로 VectorXR은 OpenXR Toolkit이 실행되는 모든 게임에서 Quadviews를 꺼 둡니다. Quadviews를 사용하려면 OpenXR Toolkit을 사용 안 함으로 설정하거나 해당 게임에서 끄십시오.", + "frameGenOffLabel": "Frame Generation 꺼짐:", + "toolkitFrameGen": "OpenXR Toolkit이 VectorXR 아래에 있습니다({slices}). 이 위치에서는 Frame Generation의 추가 프레임을 처리할 수 없으므로, VectorXR은 OpenXR Toolkit이 실행되는 모든 게임에서 Frame Generation을 끕니다. Frame Generation을 사용하려면 OpenXR Toolkit을 VectorXR 위로 옮기거나 해당 게임에서 끄세요." }, "status": { "unlocked": "이 앱 세션에서 시스템 전체 OpenXR 레이어 쓰기가 잠금 해제되었습니다.", diff --git a/app/src/i18n/locales/ko/pivot.json b/app/src/i18n/locales/ko/pivot.json index 6c5e114d..59dad490 100644 --- a/app/src/i18n/locales/ko/pivot.json +++ b/app/src/i18n/locales/ko/pivot.json @@ -16,7 +16,8 @@ "noCustomProfiles": "아직 사용자 지정 프로필이 없습니다. Pivot 회전을 특정 애플리케이션에 바인딩하려면 프로필을 추가하십시오.", "profileNumber": "프로필 {number}", "nudgeSetDefaultName": "미세 조정 세트", - "nudgeSetCopyName": "{name} 복사본" + "nudgeSetCopyName": "{name} 복사본", + "profileCopyName": "{name} 복사본" }, "warnings": { "noActivationTitle": "모션 보조 활성화 없음", diff --git a/app/src/i18n/locales/pl/app.json b/app/src/i18n/locales/pl/app.json index 8856330c..a44be9ba 100644 --- a/app/src/i18n/locales/pl/app.json +++ b/app/src/i18n/locales/pl/app.json @@ -152,6 +152,7 @@ "priority": "Kolejność priorytetów — gdy profile nakładają się na jedną aplikację, wyższy profil przejmuje wszystkie wspólne przypisania.", "moveUp": "Przenieś profil w górę (wyższy priorytet)", "moveDown": "Przenieś profil w dół (niższy priorytet)", + "duplicateTitle": "Skopiuj ten profil wraz z ustawieniami i przypisaniami do nowego profilu poniżej.", "enabledTitle": "Ten profil stosuje niestandardowe ustawienia {module} do przypisanych mu aplikacji.", "disabledTitle": "Ten profil jest ignorowany w czasie działania.", "on": "Profil wł.", diff --git a/app/src/i18n/locales/pl/framegen.json b/app/src/i18n/locales/pl/framegen.json index 9b34f2a6..3a391ba4 100644 --- a/app/src/i18n/locales/pl/framegen.json +++ b/app/src/i18n/locales/pl/framegen.json @@ -229,6 +229,12 @@ "valueLabel": "Wartość limitu ruchu w procentach szerokości widoku", "description": "Największe przesunięcie obrazu przez generowaną klatkę jako część szerokości widoku. Szybszy ruch pokazuje rzeczywisty obraz." }, + "gameDepth": { + "heading": "Używaj głębi z gry", + "description": "Używa głębi dostarczanej przez grę, aby bliskie obiekty pozostawały stabilne. Wyłącz, jeśli gra z Frame Generation zacina się, zawiesza lub pokazuje artefakty głębi; Frame Generation działa wtedy bez głębi.", + "restart": "Działa po ponownym uruchomieniu gry", + "summaryOff": "Bez głębi z gry" + }, "headPrediction": { "heading": "Rozszerz predykcję głowy", "description": "Frame Generation sprawia, że gra renderuje nawet ~150 ms do przodu, a niektóre środowiska uruchomieniowe, np. Pimax, przewidują ruch głowy tylko dla ~20 ms z tego czasu. Ta opcja przenosi obrót głowy na resztę, aby było mniej czerni na krawędzi, w którą się obracasz. Włącz ją przed uruchomieniem gry." diff --git a/app/src/i18n/locales/pl/layers.json b/app/src/i18n/locales/pl/layers.json index 78d24709..4b8dc969 100644 --- a/app/src/i18n/locales/pl/layers.json +++ b/app/src/i18n/locales/pl/layers.json @@ -23,7 +23,9 @@ "turboLabel": "Konflikt Turbo:", "toolkitTurbo": "OpenXR Toolkit jest włączony, gdy używany jest VectorXR Turbo. Dwie warstwy taktowania klatek walczą ze sobą — jeśli tryb Turbo Mode w OXRTK jest włączony, wyłącz go tam albo w VectorXR, nigdy w obu.", "quadviewsOffLabel": "Quadviews wyłączone:", - "toolkitQuad": "OpenXR Toolkit znajduje się powyżej VectorXR ({slices}). Nie obsługuje on quad views, więc VectorXR utrzymuje Quadviews wyłączone w każdej grze, w której działa OpenXR Toolkit. Wyłącz OpenXR Toolkit lub wyłącz go dla tych gier, aby używać Quadviews." + "toolkitQuad": "OpenXR Toolkit znajduje się powyżej VectorXR ({slices}). Nie obsługuje on quad views, więc VectorXR utrzymuje Quadviews wyłączone w każdej grze, w której działa OpenXR Toolkit. Wyłącz OpenXR Toolkit lub wyłącz go dla tych gier, aby używać Quadviews.", + "frameGenOffLabel": "Frame Generation wyłączone:", + "toolkitFrameGen": "OpenXR Toolkit jest poniżej VectorXR ({slices}). W tym miejscu nie obsługuje dodatkowych klatek Frame Generation, więc VectorXR utrzymuje Frame Generation wyłączone w każdej grze, w której działa OpenXR Toolkit. Aby używać Frame Generation, przenieś OpenXR Toolkit nad VectorXR lub wyłącz go dla tych gier." }, "status": { "unlocked": "Zapisy warstw OpenXR dla całego komputera są odblokowane na czas tej sesji aplikacji.", diff --git a/app/src/i18n/locales/pl/pivot.json b/app/src/i18n/locales/pl/pivot.json index a7f8a0ff..2e3e0287 100644 --- a/app/src/i18n/locales/pl/pivot.json +++ b/app/src/i18n/locales/pl/pivot.json @@ -16,7 +16,8 @@ "noCustomProfiles": "Brak profili niestandardowych. Dodaj profil, aby powiązać obrót Pivot z określoną aplikacją.", "profileNumber": "Profil {number}", "nudgeSetDefaultName": "Zestaw przesunięć", - "nudgeSetCopyName": "{name} — kopia" + "nudgeSetCopyName": "{name} — kopia", + "profileCopyName": "{name} — kopia" }, "warnings": { "noActivationTitle": "Brak aktywacji Wspomagania ruchu", diff --git a/app/src/i18n/locales/pt-BR/app.json b/app/src/i18n/locales/pt-BR/app.json index b456266c..5c72d843 100644 --- a/app/src/i18n/locales/pt-BR/app.json +++ b/app/src/i18n/locales/pt-BR/app.json @@ -144,6 +144,7 @@ "priority": "Ordem de prioridade — quando perfis se sobrepõem em um aplicativo, o perfil mais acima fica com qualquer atribuição que eles compartilhem.", "moveUp": "Mover o perfil para cima (prioridade mais alta)", "moveDown": "Mover o perfil para baixo (prioridade mais baixa)", + "duplicateTitle": "Copia este perfil, com suas configurações e atribuições, para um novo perfil logo abaixo.", "enabledTitle": "Este perfil aplica configurações personalizadas de {module} aos aplicativos atribuídos.", "disabledTitle": "Este perfil é ignorado durante a execução.", "on": "Perfil ligado", diff --git a/app/src/i18n/locales/pt-BR/framegen.json b/app/src/i18n/locales/pt-BR/framegen.json index feb48311..a7706356 100644 --- a/app/src/i18n/locales/pt-BR/framegen.json +++ b/app/src/i18n/locales/pt-BR/framegen.json @@ -229,6 +229,12 @@ "valueLabel": "Valor do limite de movimento em porcentagem da largura da visão", "description": "O máximo que um quadro gerado pode mover a imagem, como fração da largura da visão. Movimentos mais rápidos mostram a imagem real." }, + "gameDepth": { + "heading": "Usar profundidade do jogo", + "description": "Usa a profundidade fornecida pelo jogo para manter estáveis os objetos próximos. Desative se um jogo engasgar, travar ou mostrar artefatos de profundidade com o Frame Generation; o Frame Generation passa a funcionar sem profundidade.", + "restart": "Vale após reiniciar o jogo", + "summaryOff": "Sem profundidade do jogo" + }, "headPrediction": { "heading": "Estender a previsão da cabeça", "description": "A Frame Generation faz o jogo renderizar até ~150 ms adiante, e alguns runtimes, como o da Pimax, preveem a sua cabeça em apenas ~20 ms disso. Isto prolonga o giro da sua cabeça pelo restante, deixando menos preto na borda para onde você vira. Ligue antes de iniciar o jogo." diff --git a/app/src/i18n/locales/pt-BR/layers.json b/app/src/i18n/locales/pt-BR/layers.json index c9280542..0e710418 100644 --- a/app/src/i18n/locales/pt-BR/layers.json +++ b/app/src/i18n/locales/pt-BR/layers.json @@ -21,7 +21,9 @@ "turboLabel": "Conflito do Turbo:", "toolkitTurbo": "O OpenXR Toolkit está ativado enquanto o Turbo do VectorXR está em uso. Duas camadas de ritmo de quadros brigam entre si — se o Turbo Mode do OXRTK estiver ligado, desative-o lá ou no VectorXR, nunca nos dois.", "quadviewsOffLabel": "Quadviews desligado:", - "toolkitQuad": "O OpenXR Toolkit está acima do VectorXR ({slices}). Ele não oferece suporte a quad views, então o VectorXR mantém o Quadviews desligado em todos os jogos em que o OpenXR Toolkit é executado. Desative o OpenXR Toolkit, ou desligue-o para esses jogos, para usar o Quadviews." + "toolkitQuad": "O OpenXR Toolkit está acima do VectorXR ({slices}). Ele não oferece suporte a quad views, então o VectorXR mantém o Quadviews desligado em todos os jogos em que o OpenXR Toolkit é executado. Desative o OpenXR Toolkit, ou desligue-o para esses jogos, para usar o Quadviews.", + "frameGenOffLabel": "Frame Generation desligado:", + "toolkitFrameGen": "O OpenXR Toolkit está abaixo do VectorXR ({slices}). Nessa posição ele não consegue lidar com os quadros extras do Frame Generation, então o VectorXR mantém o Frame Generation desligado em todos os jogos em que o OpenXR Toolkit é executado. Mova o OpenXR Toolkit para cima do VectorXR, ou desative-o nesses jogos, para usar o Frame Generation." }, "status": { "unlocked": "As gravações de camadas OpenXR de toda a máquina estão desbloqueadas nesta sessão do aplicativo.", diff --git a/app/src/i18n/locales/pt-BR/pivot.json b/app/src/i18n/locales/pt-BR/pivot.json index 8877f62e..c8d94fa1 100644 --- a/app/src/i18n/locales/pt-BR/pivot.json +++ b/app/src/i18n/locales/pt-BR/pivot.json @@ -16,7 +16,8 @@ "noCustomProfiles": "Nenhum perfil personalizado ainda. Adicione um perfil para vincular a rotação do Pivot a um aplicativo específico.", "profileNumber": "Perfil {number}", "nudgeSetDefaultName": "Conjunto de deslocamentos", - "nudgeSetCopyName": "Cópia de {name}" + "nudgeSetCopyName": "Cópia de {name}", + "profileCopyName": "Cópia de {name}" }, "warnings": { "noActivationTitle": "Nenhuma ativação da Assistência de movimento", diff --git a/app/src/i18n/locales/ru/app.json b/app/src/i18n/locales/ru/app.json index d547ef5f..f8575045 100644 --- a/app/src/i18n/locales/ru/app.json +++ b/app/src/i18n/locales/ru/app.json @@ -152,6 +152,7 @@ "priority": "Порядок приоритета: если профили пересекаются по приложению, общей привязкой владеет профиль, расположенный выше.", "moveUp": "Переместить профиль вверх (выше приоритет)", "moveDown": "Переместить профиль вниз (ниже приоритет)", + "duplicateTitle": "Скопировать этот профиль с настройками и назначениями в новый профиль под ним.", "enabledTitle": "Этот профиль применяет свои параметры {module} к назначенным приложениям.", "disabledTitle": "Этот профиль игнорируется во время работы.", "on": "Профиль вкл.", diff --git a/app/src/i18n/locales/ru/framegen.json b/app/src/i18n/locales/ru/framegen.json index 56f0ed8f..aa7d8a0d 100644 --- a/app/src/i18n/locales/ru/framegen.json +++ b/app/src/i18n/locales/ru/framegen.json @@ -229,6 +229,12 @@ "valueLabel": "Предел движения в процентах от ширины обзора", "description": "Максимальное смещение изображения сгенерированным кадром, как доля ширины обзора. При более быстром движении показывается реальное изображение." }, + "gameDepth": { + "heading": "Использовать глубину игры", + "description": "Использует глубину, которую передаёт игра, чтобы близкие объекты оставались стабильными. Выключите, если с Frame Generation игра подтормаживает, зависает или показывает артефакты глубины; тогда Frame Generation работает без глубины.", + "restart": "Применяется после перезапуска игры", + "summaryOff": "Без глубины игры" + }, "headPrediction": { "heading": "Расширенный прогноз головы", "description": "Frame Generation заставляет игру рендерить до ~150 ms вперёд, а некоторые среды выполнения, например Pimax, прогнозируют положение головы лишь на ~20 ms из них. Этот параметр продлевает поворот головы на оставшееся время, чтобы у края, к которому вы поворачиваетесь, было меньше чёрного. Включите его до запуска игры." diff --git a/app/src/i18n/locales/ru/layers.json b/app/src/i18n/locales/ru/layers.json index c30b4696..2105e158 100644 --- a/app/src/i18n/locales/ru/layers.json +++ b/app/src/i18n/locales/ru/layers.json @@ -23,7 +23,9 @@ "turboLabel": "Конфликт Turbo:", "toolkitTurbo": "OpenXR Toolkit включён, пока используется Turbo VectorXR. Два слоя управления темпом кадров мешают друг другу: если в OXRTK включён режим Turbo Mode, отключите его там или в VectorXR, но не оба одновременно.", "quadviewsOffLabel": "Quadviews выключен:", - "toolkitQuad": "OpenXR Toolkit находится выше VectorXR ({slices}). Он не поддерживает quad views, поэтому VectorXR держит Quadviews выключенным во всех играх, в которых работает OpenXR Toolkit. Чтобы использовать Quadviews, отключите OpenXR Toolkit или выключите его для этих игр." + "toolkitQuad": "OpenXR Toolkit находится выше VectorXR ({slices}). Он не поддерживает quad views, поэтому VectorXR держит Quadviews выключенным во всех играх, в которых работает OpenXR Toolkit. Чтобы использовать Quadviews, отключите OpenXR Toolkit или выключите его для этих игр.", + "frameGenOffLabel": "Frame Generation выключен:", + "toolkitFrameGen": "OpenXR Toolkit находится ниже VectorXR ({slices}). В этом положении он не справляется с дополнительными кадрами Frame Generation, поэтому VectorXR оставляет Frame Generation выключенным во всех играх, где работает OpenXR Toolkit. Чтобы использовать Frame Generation, переместите OpenXR Toolkit выше VectorXR или отключите его для этих игр." }, "status": { "unlocked": "Запись слоёв OpenXR для всего компьютера разблокирована на этот сеанс приложения.", diff --git a/app/src/i18n/locales/ru/pivot.json b/app/src/i18n/locales/ru/pivot.json index 89ea128f..bdb601b3 100644 --- a/app/src/i18n/locales/ru/pivot.json +++ b/app/src/i18n/locales/ru/pivot.json @@ -16,7 +16,8 @@ "noCustomProfiles": "Своих профилей пока нет. Добавьте профиль, чтобы привязать поворот Pivot к определённому приложению.", "profileNumber": "Профиль {number}", "nudgeSetDefaultName": "Набор сдвигов", - "nudgeSetCopyName": "{name} (копия)" + "nudgeSetCopyName": "{name} (копия)", + "profileCopyName": "{name} (копия)" }, "warnings": { "noActivationTitle": "Нет активации усиления движения", diff --git a/app/src/i18n/locales/zh-CN/app.json b/app/src/i18n/locales/zh-CN/app.json index dd608e4f..cc92bad8 100644 --- a/app/src/i18n/locales/zh-CN/app.json +++ b/app/src/i18n/locales/zh-CN/app.json @@ -140,6 +140,7 @@ "priority": "优先级顺序——当多个配置文件作用于同一应用程序时,位置较高的配置文件拥有它们共用的任何绑定。", "moveUp": "上移配置文件(更高优先级)", "moveDown": "下移配置文件(更低优先级)", + "duplicateTitle": "将此配置文件连同其设置和绑定复制为下方的新配置文件。", "enabledTitle": "此配置文件会将自定义 {module} 设置应用于其分配的应用程序。", "disabledTitle": "此配置文件在运行时会被忽略。", "on": "配置文件已开启", diff --git a/app/src/i18n/locales/zh-CN/framegen.json b/app/src/i18n/locales/zh-CN/framegen.json index 005b080e..8c71c124 100644 --- a/app/src/i18n/locales/zh-CN/framegen.json +++ b/app/src/i18n/locales/zh-CN/framegen.json @@ -229,6 +229,12 @@ "valueLabel": "运动限制值(占视图宽度的百分比)", "description": "生成帧可以移动图像的最大距离,以视图宽度的比例表示。更快的运动会显示真实图像。" }, + "gameDepth": { + "heading": "使用游戏深度", + "description": "使用游戏提供的深度,让近处物体保持稳定。如果开启 Frame Generation 后游戏卡顿、卡死或出现深度伪影,请关闭此项;Frame Generation 将在没有深度的情况下工作。", + "restart": "重启游戏后生效", + "summaryOff": "不使用游戏深度" + }, "headPrediction": { "heading": "延长头部预测", "description": "Frame Generation 会让游戏提前最多约 150 ms 进行渲染,而某些运行时(例如 Pimax 的运行时)只预测其中约 20 ms 的头部运动。此选项会将您的头部转动延续到剩余时间,从而减少您转向一侧边缘的黑边。请在启动游戏之前开启。" diff --git a/app/src/i18n/locales/zh-CN/layers.json b/app/src/i18n/locales/zh-CN/layers.json index 162439f6..f13165da 100644 --- a/app/src/i18n/locales/zh-CN/layers.json +++ b/app/src/i18n/locales/zh-CN/layers.json @@ -20,7 +20,9 @@ "turboLabel": "Turbo 冲突:", "toolkitTurbo": "正在使用 VectorXR Turbo 时启用了 OpenXR Toolkit。两个帧节奏控制层会相互冲突——如果 OXRTK 的 Turbo 模式已开启,请在它或 VectorXR 中禁用其中一个,切勿同时开启。", "quadviewsOffLabel": "Quadviews 已关闭:", - "toolkitQuad": "OpenXR Toolkit 位于 VectorXR 之上({slices})。它不支持四视图,因此对于运行 OpenXR Toolkit 的所有游戏,VectorXR 都会保持 Quadviews 关闭。要使用 Quadviews,请禁用 OpenXR Toolkit,或为这些游戏关闭它。" + "toolkitQuad": "OpenXR Toolkit 位于 VectorXR 之上({slices})。它不支持四视图,因此对于运行 OpenXR Toolkit 的所有游戏,VectorXR 都会保持 Quadviews 关闭。要使用 Quadviews,请禁用 OpenXR Toolkit,或为这些游戏关闭它。", + "frameGenOffLabel": "Frame Generation 已关闭:", + "toolkitFrameGen": "OpenXR Toolkit 位于 VectorXR 之下({slices})。在此位置它无法处理 Frame Generation 的额外帧,因此在运行 OpenXR Toolkit 的所有游戏中,VectorXR 都会保持 Frame Generation 关闭。要使用 Frame Generation,请将 OpenXR Toolkit 移到 VectorXR 之上,或在这些游戏中将其关闭。" }, "status": { "unlocked": "已为本次应用会话解锁计算机全局 OpenXR 层的写入。", diff --git a/app/src/i18n/locales/zh-CN/pivot.json b/app/src/i18n/locales/zh-CN/pivot.json index d045261d..47c167e3 100644 --- a/app/src/i18n/locales/zh-CN/pivot.json +++ b/app/src/i18n/locales/zh-CN/pivot.json @@ -16,7 +16,8 @@ "noCustomProfiles": "还没有自定义配置文件。添加一个配置文件即可将 Pivot 旋转绑定到特定应用程序。", "profileNumber": "配置文件 {number}", "nudgeSetDefaultName": "微调组", - "nudgeSetCopyName": "{name} 副本" + "nudgeSetCopyName": "{name} 副本", + "profileCopyName": "{name} 副本" }, "warnings": { "noActivationTitle": "没有运动辅助激活", diff --git a/app/src/i18n/locales/zh-TW/app.json b/app/src/i18n/locales/zh-TW/app.json index 83b6d98a..d2a18eb0 100644 --- a/app/src/i18n/locales/zh-TW/app.json +++ b/app/src/i18n/locales/zh-TW/app.json @@ -140,6 +140,7 @@ "priority": "優先順序:當多個設定檔重疊套用於同一應用程式時,較上方的設定檔擁有它們共用的任何繫結。", "moveUp": "將設定檔上移(較高優先順序)", "moveDown": "將設定檔下移(較低優先順序)", + "duplicateTitle": "將此設定檔連同其設定與綁定複製為下方的新設定檔。", "enabledTitle": "此設定檔會將自訂 {module} 設定套用至其指派的應用程式。", "disabledTitle": "執行階段會忽略此設定檔。", "on": "設定檔開啟", diff --git a/app/src/i18n/locales/zh-TW/framegen.json b/app/src/i18n/locales/zh-TW/framegen.json index c39ec41c..f6f5ae90 100644 --- a/app/src/i18n/locales/zh-TW/framegen.json +++ b/app/src/i18n/locales/zh-TW/framegen.json @@ -229,6 +229,12 @@ "valueLabel": "動態限制值,以視野寬度的百分比表示", "description": "生成影格可移動影像的最大距離,以視野寬度的比例表示。更快的動態會顯示真實影像。" }, + "gameDepth": { + "heading": "使用遊戲深度", + "description": "使用遊戲提供的深度,讓近處物體保持穩定。如果開啟 Frame Generation 後遊戲卡頓、當住或出現深度瑕疵,請關閉此項;Frame Generation 將在沒有深度的情況下運作。", + "restart": "重新啟動遊戲後生效", + "summaryOff": "不使用遊戲深度" + }, "headPrediction": { "heading": "延伸頭部預測", "description": "Frame Generation 會讓遊戲最多提前約 150 ms 渲染,而某些執行階段(例如 Pimax 的)只會預測其中約 20 ms 的頭部動作。此選項會將您頭部的轉動延續到剩餘的時間,讓您轉向的那一側邊緣出現較少黑邊。請在啟動遊戲前開啟。" diff --git a/app/src/i18n/locales/zh-TW/layers.json b/app/src/i18n/locales/zh-TW/layers.json index dc0b8783..0068c6b7 100644 --- a/app/src/i18n/locales/zh-TW/layers.json +++ b/app/src/i18n/locales/zh-TW/layers.json @@ -20,7 +20,9 @@ "turboLabel": "Turbo 衝突:", "toolkitTurbo": "使用 VectorXR Turbo 時,OpenXR Toolkit 處於啟用狀態。兩個影格節奏層會互相干擾:如果 OXRTK 的 Turbo Mode 已開啟,請在該處或在 VectorXR 中停用其中一個,切勿兩者同時開啟。", "quadviewsOffLabel": "Quadviews 已關閉:", - "toolkitQuad": "OpenXR Toolkit 位於 VectorXR 之上({slices})。它不支援四視圖,因此在所有執行 OpenXR Toolkit 的遊戲中,VectorXR 都會讓 Quadviews 保持關閉。若要使用 Quadviews,請停用 OpenXR Toolkit,或針對那些遊戲將其關閉。" + "toolkitQuad": "OpenXR Toolkit 位於 VectorXR 之上({slices})。它不支援四視圖,因此在所有執行 OpenXR Toolkit 的遊戲中,VectorXR 都會讓 Quadviews 保持關閉。若要使用 Quadviews,請停用 OpenXR Toolkit,或針對那些遊戲將其關閉。", + "frameGenOffLabel": "Frame Generation 已關閉:", + "toolkitFrameGen": "OpenXR Toolkit 位於 VectorXR 之下({slices})。在此位置它無法處理 Frame Generation 的額外影格,因此在執行 OpenXR Toolkit 的所有遊戲中,VectorXR 都會保持 Frame Generation 關閉。若要使用 Frame Generation,請將 OpenXR Toolkit 移到 VectorXR 之上,或在這些遊戲中將其關閉。" }, "status": { "unlocked": "已為此應用程式工作階段解除鎖定全機 OpenXR 層寫入。", diff --git a/app/src/i18n/locales/zh-TW/pivot.json b/app/src/i18n/locales/zh-TW/pivot.json index 4832e320..a4dd30ad 100644 --- a/app/src/i18n/locales/zh-TW/pivot.json +++ b/app/src/i18n/locales/zh-TW/pivot.json @@ -16,7 +16,8 @@ "noCustomProfiles": "尚無自訂設定檔。新增設定檔即可將 Pivot 旋轉繫結到特定應用程式。", "profileNumber": "設定檔 {number}", "nudgeSetDefaultName": "微調組", - "nudgeSetCopyName": "{name} 複本" + "nudgeSetCopyName": "{name} 複本", + "profileCopyName": "{name} 複本" }, "warnings": { "noActivationTitle": "沒有動態輔助啟用方式", diff --git a/app/src/lib/model.ts b/app/src/lib/model.ts index aabd556c..1f2e7b04 100644 --- a/app/src/lib/model.ts +++ b/app/src/lib/model.ts @@ -701,6 +701,16 @@ export interface FrameGenSettings { // Present only when on; absent means off here and in the layer, so a // profile never inherits it from the defaults. extendHeadPrediction?: boolean + // Edge fill: frames Frame Generation writes reach a few degrees past the + // game's field of view, the margin repeating the frame's edge. Present only + // when on, like extendHeadPrediction; not in the app's pages until it has + // flown (set in settings.json), but kept when the app saves. + edgeFill?: boolean + // Use the game's depth. Present only when off (absent means on, here and + // in the layer). Off, Frame Generation works without depth and the bridges + // keep a Direct3D 11 or Vulkan game's depth to themselves; the bridges read + // it when the game's VR session starts. + useGameDepth?: boolean } export interface FrameGenProfileConfig { @@ -772,6 +782,8 @@ export function normalizeFrameGenSettings(value: unknown, fallback: FrameGenSett // Absent means off, as in the layer: a profile does not inherit it from // the defaults (new profiles copy the defaults' value when created). ...(source.extendHeadPrediction === true ? { extendHeadPrediction: true } : {}), + ...(source.edgeFill === true ? { edgeFill: true } : {}), + ...(source.useGameDepth === false ? { useGameDepth: false } : {}), } } @@ -1231,6 +1243,16 @@ export function createPivotProfile( } } +// A deep copy with fresh ids, so the copy's bindings and Quick Views are keyed +// apart from the source. The nudge set stays shared, like any other profile. +export function duplicatePivotProfile(source: PivotXRProfileConfig): PivotXRProfileConfig { + const copy = JSON.parse(JSON.stringify(source)) as PivotXRProfileConfig + copy.id = newPivotProfileId() + copy.name = t('pivot.tab.profileCopyName', { name: source.name }) + copy.viewControls.quickViews = copy.viewControls.quickViews.map((view) => ({ ...view, id: newPivotQuickViewId() })) + return copy +} + export function createQuadViewsProfile(defaultSettings: QuadViewsSettings, applicationIds: string[] = []): QuadViewsProfileConfig { return { name: defaultProfileName(), diff --git a/app/src/lib/patchNotes.json b/app/src/lib/patchNotes.json index 754375a4..3f8e7de5 100644 --- a/app/src/lib/patchNotes.json +++ b/app/src/lib/patchNotes.json @@ -1,4 +1,56 @@ [ + { + "version": "0.20.1", + "date": "2026-11-08", + "title": "Depth for Direct3D 11 Games, PimaxXR Fixes, and Duplicate Pivot Profiles", + "summary": "A fix release for 0.20.0. Frame Generation can now use depth from Direct3D 11 games, including UEVR titles. Frame Generation no longer stalls games on PimaxXR. Turbo's Auto mode gives async pacing a fairer trial, and eye-tracked foveation is placed better on Pimax. VectorXR works beside OpenXR Toolkit and Quad-Views-Foveated in more setups, and custom Pivot profiles can now be duplicated.", + "items": [ + { + "html": "Frame Generation", + "items": [ + "Depth for Direct3D 11 games. Frame Generation can now use the depth that Direct3D 11 games provide, including UEVR titles, for steadier generated frames.", + "Use game depth option. If a game stutters, hangs or shows depth artifacts with Frame Generation, turn off Use game depth in the profile's advanced settings and restart the game. Frame Generation then works without depth.", + "No more stalls on PimaxXR. With Frame Generation engaged, PimaxXR could hold up the game's own rendering, and the game's frame rate dropped sharply. Frame Generation now works around it.", + "Smoother output on PimaxXR 0.4.4 and earlier, which could skip a refresh when no new frame was ready.", + "Frame Generation stays off when OpenXR Toolkit is below VectorXR, because Toolkit can't handle the extra frames there. The OSD and the OpenXR Layers page now say when this applies; move OpenXR Toolkit above VectorXR there to use both.", + "OpenXR Toolkit's prediction dampening is respected instead of being undone by Frame Generation.", + "No rejected frames from misplaced depth. Some games, such as UEVR titles, can describe depth that doesn't fit its image, and PimaxXR and Virtual Desktop then reject the whole frame. VectorXR now leaves that depth out." + ] + }, + { + "html": "Turbo", + "items": [ + "Auto gives async pacing a fair trial. A stutter right as a game left its loading screen could make Auto settle on sequenced pacing for good. Auto now rides out early stutters. It also tries async again after a quick failure, a VectorXR or runtime update, or 30 days.", + "More accurate frame timing with sequenced pacing in games that prepare frames ahead, such as DCS.", + "Turbo stands aside when OpenXR Toolkit's Turbo runs above VectorXR, instead of both pacing frames." + ] + }, + { + "html": "Quadviews and eye tracking", + "items": [ + "Better-placed focus areas on Pimax. On headsets with angled displays, VectorXR's emulated quad views placed each eye's sharp area off to the side. Both eyes now line up on where you're looking.", + "A frozen eye tracker no longer pins the focus area. If the tracker stops updating, the focus area follows your head until the tracker recovers.", + "Eye-tracked foveation is no longer switched off in PimaxXR's native quad views.", + "DCS's focus views stay aligned on PimaxXR's native quad views." + ] + }, + { + "html": "Pivot", + "items": [ + "Duplicate custom profiles. A new Duplicate button next to Remove copies a profile with all its settings and bindings, places the copy just below the original, and scrolls to it. Change the copy's applications or bindings so the two profiles don't overlap.", + "Pivot stays centred after a recenter on Pimax. Recentering in Pimax Play during a session could leave Pivot turning around your old head position.", + "A restarted VR session starts Pivot fresh, instead of picking up the previous session's origin." + ] + }, + { + "html": "Compatibility", + "items": [ + "Games start with OpenXR Toolkit or Quad-Views-Foveated on VIVE runtimes.", + "Stability fixes for PimaxXR around starting VR again in the same game, and around head tracking." + ] + } + ] + }, { "version": "0.20.0", "date": "2026-10-29", diff --git a/app/src/lib/validation.ts b/app/src/lib/validation.ts index 9060ec64..7ae18fd0 100644 --- a/app/src/lib/validation.ts +++ b/app/src/lib/validation.ts @@ -364,6 +364,12 @@ function validateFrameGenSettings(prefix: string, settings: FrameGenSettings): s if (settings.extendHeadPrediction !== undefined && typeof settings.extendHeadPrediction !== 'boolean') { errors.push(t('validation.field.trueOrFalse', { field: prefix + 'extendHeadPrediction' })) } + if (settings.edgeFill !== undefined && typeof settings.edgeFill !== 'boolean') { + errors.push(t('validation.field.trueOrFalse', { field: prefix + 'edgeFill' })) + } + if (settings.useGameDepth !== undefined && typeof settings.useGameDepth !== 'boolean') { + errors.push(t('validation.field.trueOrFalse', { field: prefix + 'useGameDepth' })) + } const bounded = [ ['artifactSuppression', settings.artifactSuppression, 0, 100], diff --git a/app/src/stores/configStore.ts b/app/src/stores/configStore.ts index 24302097..2040fdc7 100644 --- a/app/src/stores/configStore.ts +++ b/app/src/stores/configStore.ts @@ -16,7 +16,7 @@ import { import { errorText } from '../lib/errors' import { t } from '../i18n/index.ts' import { createTaskQueue } from '../lib/taskQueue' -import { cloneConfig, createApplication, createFrameGenProfile, createProfile, createPivotProfile, createQuadViewsProfile, createTurboProfile, defaultConfig, defaultProfileName, isDefaultProfileName } from '../lib/model' +import { cloneConfig, createApplication, createFrameGenProfile, createProfile, createPivotProfile, createQuadViewsProfile, duplicatePivotProfile as clonePivotProfile, createTurboProfile, defaultConfig, defaultProfileName, isDefaultProfileName } from '../lib/model' import type { AppTab, ModuleId, RuntimePacingObservation, TurboMetricsSession, VectorXRConfig } from '../lib/model' interface StoreState { @@ -332,6 +332,17 @@ export function useConfigStore() { ) } + // The copy lands right below its source, so it starts one step lower in + // priority order than the profile it came from. + function duplicatePivotProfile(index: number) { + const profiles = state.config.modules.pivotxr.profiles + const source = profiles[index] + if (!source) { + return + } + profiles.splice(index + 1, 0, clonePivotProfile(source)) + } + function removePivotProfile(index: number) { state.config.modules.pivotxr.profiles.splice(index, 1) } @@ -535,6 +546,7 @@ export function useConfigStore() { removeProfile, syncProfileName, addPivotProfile, + duplicatePivotProfile, removePivotProfile, movePivotProfile, syncPivotProfileName, diff --git a/app/tests/framegen.test.mjs b/app/tests/framegen.test.mjs index ba9be14a..fa317607 100644 --- a/app/tests/framegen.test.mjs +++ b/app/tests/framegen.test.mjs @@ -43,6 +43,16 @@ test('invalid imported values normalize safely: enums fall back, integers clamp' assert.equal('extendHeadPrediction' in normalizeFrameGenSettings({ extendHeadPrediction: 'yes' }), false) // A profile saved without it is off, as the layer reads it, whatever the defaults say. assert.equal('extendHeadPrediction' in normalizeFrameGenSettings({}, { ...defaultFrameGenSettings(), extendHeadPrediction: true }), false) + // Edge fill likewise: kept when on, absent otherwise, never inherited. + assert.deepEqual(normalizeFrameGenSettings({ edgeFill: true }), { ...defaultFrameGenSettings(), edgeFill: true }) + assert.equal('edgeFill' in normalizeFrameGenSettings({ edgeFill: false }), false) + assert.equal('edgeFill' in normalizeFrameGenSettings({ edgeFill: 1 }), false) + assert.equal('edgeFill' in normalizeFrameGenSettings({}, { ...defaultFrameGenSettings(), edgeFill: true }), false) + // useGameDepth is stored only when off; absent means on. + assert.deepEqual(normalizeFrameGenSettings({ useGameDepth: false }), { ...defaultFrameGenSettings(), useGameDepth: false }) + assert.equal('useGameDepth' in normalizeFrameGenSettings({ useGameDepth: true }), false) + assert.equal('useGameDepth' in normalizeFrameGenSettings({ useGameDepth: 0 }), false) + assert.equal('useGameDepth' in normalizeFrameGenSettings({}, { ...defaultFrameGenSettings(), useGameDepth: false }), false) assert.deepEqual(normalizeFrameGenSettings({ artifactSuppression: -5, motionLimitPercent: 99 }), { ...defaultFrameGenSettings(), artifactSuppression: 0, motionLimitPercent: 25 }) assert.equal(normalizeFrameGenSettings({ artifactSuppression: 33.6 }).artifactSuppression, 34) assert.equal(normalizeFrameGenSettings({ motionLimitPercent: Infinity }).motionLimitPercent, 10) diff --git a/app/tests/model.test.mjs b/app/tests/model.test.mjs index 3053924c..81429e6b 100644 --- a/app/tests/model.test.mjs +++ b/app/tests/model.test.mjs @@ -7,6 +7,7 @@ import { createPivotQuickView, defaultConfig, createPivotProfile, + duplicatePivotProfile, normalizeConfig, normalizeKeyboardKey, pivotBindingConflictWarnings, @@ -613,3 +614,31 @@ test('0.15 and 0.15.1 configs preserve existing settings, profiles, and bindings assert.deepEqual(validateConfig(normalized), []) assert.deepEqual(normalizeConfig(normalized), normalized) }) + +test('Duplicating a Pivot profile copies its settings under fresh ids', () => { + const config = defaultConfig() + config.applications = [{ id: 'dcs-world', name: 'DCS World', enabled: true, match: { exe: 'DCS.exe' } }] + const appId = 'dcs-world' + const source = createPivotProfile(config.modules.pivotxr.defaults, [appId], false, [activation('hold', keyboard('F1'))]) + source.name = 'DCS Hornet' + source.settings.rotationMultiplier = 1.7 + source.viewControls.quickViews.push(createPivotQuickView('Left')) + const copy = duplicatePivotProfile(source) + + assert.notEqual(copy.id, source.id) + assert.equal(copy.name, 'DCS Hornet Copy') + assert.deepEqual(copy.applicationIds, [appId]) + assert.equal(copy.settings.rotationMultiplier, 1.7) + assert.deepEqual(copy.activationBindings, source.activationBindings) + assert.equal(copy.nudgeSetId, source.nudgeSetId) + assert.notEqual(copy.viewControls.quickViews[0].id, source.viewControls.quickViews[0].id) + + copy.applicationIds = [] + copy.settings.rotationMultiplier = 1 + assert.deepEqual(source.applicationIds, [appId]) + assert.equal(source.settings.rotationMultiplier, 1.7) + + copy.applicationIds = [appId] + config.modules.pivotxr.profiles.push(source, copy) + assert.deepEqual(validateConfig(config), []) +}) diff --git a/config/vectorxr.schema.json b/config/vectorxr.schema.json index 488463ee..0c8bfa42 100644 --- a/config/vectorxr.schema.json +++ b/config/vectorxr.schema.json @@ -1721,6 +1721,16 @@ "type": "boolean", "default": false, "description": "Frame Generation has the game render for a display time up to ~150 ms ahead; some runtimes (Pimax's) predict the head only ~20 ms of that. When on, VectorXR measures how far the runtime predicts and carries the head's turn over the rest, which shrinks the black band at the edge you turn toward. Turn it on before the game starts: it needs the runtime's clock, which is requested at startup." + }, + "edgeFill": { + "type": "boolean", + "default": false, + "description": "Frames Frame Generation writes reach a few degrees past the game's field of view (3 degrees left and right, 1.5 up and down), the extra margin repeating the frame's edge. On a fast head turn the runtime then shows a smeared edge on the side you turn toward instead of a black band. Costs some GPU time and video memory for the wider images. Direct3D 12 games and Direct3D 11 or Vulkan games through the bridge; it can be changed while the game runs." + }, + "useGameDepth": { + "type": "boolean", + "default": true, + "description": "Use the depth the game provides. Turn off if a game stutters, hangs or shows depth artifacts with Frame Generation: Frame Generation then works without depth, and Direct3D 11 and Vulkan games through the bridge keep their depth to themselves (the runtime gets none from them), as before 0.20.1. Written only when off. The bridges read it when the game's VR session starts." } } }, diff --git a/docs/architecture.md b/docs/architecture.md index 9de6f69f..420cd2e4 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -175,7 +175,7 @@ it at headset size. Generation works on whatever projection views reach the runt so it follows Quadviews composition and Varjo-native quad views alike. When a game submits depth with its projection views (`XR_KHR_composition_layer_depth`) and the -backend can read it (Direct3D 12 so far), the depth image is converted to linear view depth at +backend can read it (Direct3D 12, which the bridges' sessions are), the depth image is converted to linear view depth at release, next to the color capture, and the previous frame is mapped into the newest view by the full relative head pose instead of rotation alone. Content fixed in the head's reference space, such as a cockpit, then lines up exactly and the flow carries only motion relative to that space. diff --git a/docs/usage.md b/docs/usage.md index 2011c102..92e67de1 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -765,7 +765,16 @@ stand still for a refresh and then jump. #### Game depth When a game sends depth to the headset, Frame Generation uses it automatically in Direct3D 12 -games, and in Vulkan games through the Vulkan bridge. +games, in Direct3D 11 games through the Direct3D 11 bridge, and in Vulkan games through the +Vulkan bridge. + +If a game stutters, hangs or shows depth artifacts with Frame Generation, turn off **Use game +depth** under the advanced settings of that game's Frame Generation profile (or of the default +profile), then restart the game. Frame Generation then works without depth, and Direct3D 11 and +Vulkan games through the bridges keep their depth to themselves, as before 0.20.1: the bridge +makes no copy of it, and the runtime gets none from them. Direct3D 12 games still send their +depth to the runtime; only Frame Generation leaves it out. + MSFS 2024 only fills the depth it sends while its **Motion reprojection** setting is **Depth**; with it off, the depth is empty and Frame Generation does without. With depth, head movement is compensated exactly, so cockpit instruments, panels and text stay still in generated frames @@ -774,10 +783,39 @@ cockpit from where your eyes will be when each frame is displayed: they keep mov between the game's last two frames. Most headset runtimes only correct head rotation between frames, so without this, near cockpit detail would jump at the game's frame rate whenever your head moved. Depth also lets generated frames resample nearby content with a sharper (cubic) -filter, so cockpit detail stays crisper. Direct3D 11 games such as DCS keep their depth to -themselves through the Direct3D 11 bridge, so they use head rotation only for now, and Star -Citizen's depth currently arrives empty. When a game supplies useful depth for only one eye, -Frame Generation can use it for the other eye too. +filter, so cockpit detail stays crisper. Direct3D 11 games that send depth, such as iRacing, +and Unreal Engine games through UEVR with its **Enable Depth-based Latency Reduction** option +on, have it copied from the game's private depth images into the headset runtime's at each +release, so the runtime gets it as well (about 0.1 to 0.2 ms of the game's GPU time on an +RTX 5090 at headset resolutions). DCS sends no depth, so it uses head rotation only. When a +game supplies useful depth for only one eye, Frame Generation can use it for the other eye too. + +Star Citizen currently writes its depth after it has handed the image over for the frame, so the +depth is still empty when the frame ends (CIG plans to fix this). While Frame Generation finds a +Vulkan game's depth empty, the Vulkan bridge checks its depth image about once a second at the +frame's end and again just before the game's next frame. When depth turns up only at the second check, twice in a row, the bridge takes it +from there: each frame then carries the depth of the frame before. Frame Generation checks that +depth against the other eye like any other, and uses it only when it lines the eyes up. The log +says when this starts (**the game writes its depth after its frame's end**) and when it stops +again, as soon as the depth is there at the frame's end. Taking depth this way costs one more +copy of each depth image per frame, and one more depth image of memory per depth swapchain +(about 110 MB for each eye at Star Citizen's 5424x5356). `VECTORXR_VULKAN_BRIDGE_DEPTH=end-frame` +only takes a Vulkan game's depth at the frame's end, and `VECTORXR_VULKAN_BRIDGE_DEPTH=private` +keeps every Vulkan game's depth to itself, out of the frames Frame Generation and the runtime get. + + +The bridge keeps a Direct3D 11 game's depth to itself, as before, when it cannot copy it: depth +images with more than one sample, or a runtime that does not make its depth images depth +targets. The log says so for each such depth swapchain, and the game's depth then reaches +neither Frame Generation nor the runtime. `VECTORXR_D3D11_BRIDGE_DEPTH=private` keeps every +Direct3D 11 game's depth to itself, if copying it ever causes a problem in a game. While the +bridge copies a game's depth, it turns the game's Direct3D 11 multithread protection on (if the +game has not), so no other thread of the game's renders in the middle of a copy. + +Depth whose rectangle lies outside its depth image, as UEVR sends with an upscaler or a screen +percentage that makes its depth image smaller than its color image, is left out of the frame for +both eyes, in every kind of game: some runtimes (PimaxXR, Virtual Desktop) refuse a whole frame +for it. The log says so once a session. The color goes on as before. Games do not always describe their depth correctly, so Frame Generation first checks it against the other eye: depth that is right lines the two eyes' images up. It starts using depth once @@ -860,7 +898,9 @@ at 6864x6908 per eye and 0.2 GB at 3000x3000. In all, plan for about 3 to 4 GB o 6864x6908 per eye, and under 1 GB at 3000x3000. Direct3D 11 and Vulkan games (DCS, Star Citizen) also keep a shared copy of each of the game's swapchain images for the bridge, about as much again as the output images (the bridge logs it as `twinMiB`): plan for about 4 to 5 GB for them at -6864x6908 per eye. +6864x6908 per eye. A Direct3D 11 game that sends depth adds a shared copy of each depth image +too: 2 bytes per pixel for 16-bit depth, 4 for the others (iRacing's 6904x3184 16-bit depth: +about 130 MB for three images). Outside quad views, the log says how many real frames are kept (**Frame Generation keeps the last 5 real frames to generate from**, or 3 with the reason; quad views sessions always keep five), and the **Frame Generation process video memory** lines @@ -994,7 +1034,7 @@ engages, or sooner once the game's own frames show the runtime giving them every #### Current limits - Frame Generation does not use game motion vectors yet; motion is estimated from color - images, with the game's depth (Direct3D 12, and Vulkan through the bridge) used to remove head + images, with the game's depth (Direct3D 12, and Direct3D 11 and Vulkan through the bridges) used to remove head movement and sharpen nearby detail. - There is no HUD masking yet. Text, HUDs, and gauges baked into the 3D image can shimmer on generated frames. diff --git a/layer/CMakeLists.txt b/layer/CMakeLists.txt index 89235a23..3f5addf3 100644 --- a/layer/CMakeLists.txt +++ b/layer/CMakeLists.txt @@ -91,10 +91,58 @@ if(WIN32 AND DEPTHXR_BUILD_LAYER) endif() endif() + # Pinned archives (the Vulkan headers, DXC) are downloaded once into a + # cache directory, DEPTHXR_DOWNLOAD_CACHE or /_downloads, and + # FetchContent extracts the cached file. Downloads are retried: GitHub's + # release downloads sometimes answer 5xx for a while. + function(depthxr_cached_archive out_var name url sha256) + if(DEFINED ENV{DEPTHXR_DOWNLOAD_CACHE} AND NOT "$ENV{DEPTHXR_DOWNLOAD_CACHE}" STREQUAL "") + file(TO_CMAKE_PATH "$ENV{DEPTHXR_DOWNLOAD_CACHE}" _dir) + else() + set(_dir "${CMAKE_BINARY_DIR}/_downloads") + endif() + set(_file "${_dir}/${name}") + string(TOLOWER "${sha256}" _want) + if(EXISTS "${_file}") + file(SHA256 "${_file}" _have) + if(NOT _have STREQUAL _want) + file(REMOVE "${_file}") + endif() + endif() + if(NOT EXISTS "${_file}") + set(_attempts 5) + foreach(_attempt RANGE 1 ${_attempts}) + file(DOWNLOAD "${url}" "${_file}.part" STATUS _status TLS_VERIFY ON) + list(GET _status 0 _code) + list(GET _status 1 _message) + if(_code EQUAL 0) + file(SHA256 "${_file}.part" _have) + if(NOT _have STREQUAL _want) + file(REMOVE "${_file}.part") + message(FATAL_ERROR "${url} has SHA256 ${_have}, not the pinned ${_want}") + endif() + file(RENAME "${_file}.part" "${_file}") + break() + endif() + file(REMOVE "${_file}.part") + if(_attempt EQUAL _attempts) + message(FATAL_ERROR "Downloading ${url} failed ${_attempts} times: ${_message}") + endif() + math(EXPR _delay "15 * ${_attempt}") + message(STATUS "Downloading ${name} failed (${_message}); retrying in ${_delay} s") + execute_process(COMMAND "${CMAKE_COMMAND}" -E sleep ${_delay}) + endforeach() + endif() + set(${out_var} "${_file}" PARENT_SCOPE) + endfunction() + set(VULKAN_HEADERS_ENABLE_MODULE OFF CACHE BOOL "" FORCE) include(FetchContent) + depthxr_cached_archive(_vulkan_headers_archive vulkan-headers-b379292b2ab6df5771ba9870d53cf8b2c9295daf.tar.gz + https://github.com/KhronosGroup/Vulkan-Headers/archive/b379292b2ab6df5771ba9870d53cf8b2c9295daf.tar.gz + 2ad177c0e468ac2eb3db9b5be6b8a18a6730d339378ac670e5fbda3503ac7d64) FetchContent_Declare(vulkan_headers - URL https://github.com/KhronosGroup/Vulkan-Headers/archive/b379292b2ab6df5771ba9870d53cf8b2c9295daf.tar.gz + URL "${_vulkan_headers_archive}" URL_HASH SHA256=2ad177c0e468ac2eb3db9b5be6b8a18a6730d339378ac670e5fbda3503ac7d64 DOWNLOAD_EXTRACT_TIMESTAMP TRUE) FetchContent_MakeAvailable(vulkan_headers) @@ -123,8 +171,11 @@ if(WIN32 AND DEPTHXR_BUILD_LAYER) find_program(DEPTHXR_FXC fxc HINTS ${_framegen_fxc_hints} REQUIRED) set(DEPTHXR_DXC "") if(DEPTHXR_FRAMEGEN_SPIRV) + depthxr_cached_archive(_dxc_archive dxc_2025_07_14.zip + https://github.com/microsoft/DirectXShaderCompiler/releases/download/v1.8.2505.1/dxc_2025_07_14.zip + 9ad895a6b039e3a8f8c22a1009f866800b840a74b50db9218d13319e215ea8a4) FetchContent_Declare(dxc_release - URL https://github.com/microsoft/DirectXShaderCompiler/releases/download/v1.8.2505.1/dxc_2025_07_14.zip + URL "${_dxc_archive}" URL_HASH SHA256=9ad895a6b039e3a8f8c22a1009f866800b840a74b50db9218d13319e215ea8a4 DOWNLOAD_EXTRACT_TIMESTAMP TRUE) FetchContent_MakeAvailable(dxc_release) @@ -204,6 +255,33 @@ if(WIN32 AND DEPTHXR_BUILD_LAYER) DEPENDS "${_framegen_hlsl}" COMMENT "Compiling Frame Generation FLOW_MERGE (DXBC)" VERBATIM) + # The Direct3D 11 bridge's depth: a Direct3D 11 compute shader copying the + # game's depth into a shared twin, and the Direct3D 12 draw writing the + # twin into the runtime's depth image, included by the bridge as headers. + set(_bridge_depth_hlsl "${CMAKE_CURRENT_SOURCE_DIR}/shaders/bridge_depth.hlsl") + set(_bridge_depth_headers "") + foreach(_bridge_depth_shader TO_TWIN VS PS) + if(_bridge_depth_shader STREQUAL "TO_TWIN") + set(_bridge_depth_args -T cs_5_0 -E CSMain) + set(_bridge_depth_name kBridgeDepthToTwinDxbc) + elseif(_bridge_depth_shader STREQUAL "VS") + set(_bridge_depth_args -T vs_5_0 -E VSMain) + set(_bridge_depth_name kBridgeDepthVsDxbc) + else() + set(_bridge_depth_args -T ps_5_0 -E PSMain) + set(_bridge_depth_name kBridgeDepthPsDxbc) + endif() + string(TOLOWER "${_bridge_depth_shader}" _bridge_depth_file) + set(_bridge_depth_header "${_framegen_out}/bridge_depth_${_bridge_depth_file}.h") + add_custom_command( + OUTPUT "${_bridge_depth_header}" + COMMAND "${DEPTHXR_FXC}" -nologo ${_bridge_depth_args} -D "BRIDGE_DEPTH_${_bridge_depth_shader}=1" -O3 -WX + -Vn ${_bridge_depth_name} -Fh "${_bridge_depth_header}" "${_bridge_depth_hlsl}" + DEPENDS "${_bridge_depth_hlsl}" + COMMENT "Compiling the Direct3D 11 bridge's depth ${_bridge_depth_shader} (DXBC)" + VERBATIM) + list(APPEND _bridge_depth_headers "${_bridge_depth_header}") + endforeach() string(REPLACE ";" "," _framegen_pass_list "${_framegen_passes}") string(REPLACE ";" "," _framegen_draw_list "${_framegen_draw_shaders}") set(_framegen_spirv_dir "") @@ -241,7 +319,8 @@ if(WIN32 AND DEPTHXR_BUILD_LAYER) # ...and the engine motion record and census made from it. src/engine_motion.cpp "${_framegen_out}/framegen_shaders.cpp" - "${_framegen_out}/framegen_flow_merge.h") + "${_framegen_out}/framegen_flow_merge.h" + ${_bridge_depth_headers}) target_include_directories(depthxr_framegen PUBLIC include) target_include_directories(depthxr_framegen PRIVATE "${_framegen_out}") target_compile_features(depthxr_framegen PUBLIC cxx_std_20) @@ -558,7 +637,7 @@ if(WIN32 AND DEPTHXR_BUILD_LAYER) # Frame Generation's Direct3D 11 bridge against a stand-in runtime, on WARP # (`depthxr_d3d11_bridge_tests hw` runs it on the GPU). add_executable(depthxr_d3d11_bridge_tests tests/d3d11_bridge_tests.cpp) - target_link_libraries(depthxr_d3d11_bridge_tests PRIVATE depthxr_framegen d3d11 d3d12 dxgi) + target_link_libraries(depthxr_d3d11_bridge_tests PRIVATE depthxr_framegen d3d11 d3d12 dxgi d3dcompiler) target_compile_definitions(depthxr_d3d11_bridge_tests PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX XR_USE_GRAPHICS_API_D3D11 XR_USE_GRAPHICS_API_D3D12) add_test(NAME depthxr_d3d11_bridge_tests COMMAND depthxr_d3d11_bridge_tests) add_test(NAME depthxr_d3d11_bridge_hw_tests COMMAND depthxr_d3d11_bridge_tests hw) @@ -576,10 +655,18 @@ if(WIN32 AND DEPTHXR_BUILD_LAYER) add_executable(depthxr_framegen_presenter_tests tests/framegen_presenter_tests.cpp) target_link_libraries(depthxr_framegen_presenter_tests PRIVATE depthxr_framegen) target_compile_definitions(depthxr_framegen_presenter_tests PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) - add_test(NAME depthxr_framegen_presenter_tests COMMAND depthxr_framegen_presenter_tests) - # Paced by the wall clock: about 375 s on a fast desktop (0.19.2), longer on a shared CI runner. The lead - # ceiling's tests add about 3 minutes; serially (FG_TEST_SERIAL=1) the suite takes about 16 minutes. - set_tests_properties(depthxr_framegen_presenter_tests PROPERTIES TIMEOUT 1200) + # Paced by the wall clock, mostly waiting: about 680 s on a fast desktop (0.20.1) in one process beside its + # policy tests (no argument). CTest runs its groups (--group, GROUP in the test) as tests of their own, side by + # side under ctest -j: 45-160 s each on a desktop, about 18 minutes in all. -R depthxr_framegen_presenter_tests_ + # selects them. They create no graphics device. + set(_presenter_tests "") + foreach(_group IN ITEMS pacing lifecycle quad clocks probe_burst lead ceiling policy policy_late) + add_test(NAME depthxr_framegen_presenter_tests_${_group} + COMMAND depthxr_framegen_presenter_tests --group ${_group}) + list(APPEND _presenter_tests depthxr_framegen_presenter_tests_${_group}) + endforeach() + # COST: CTest starts the longest tests first, before it has timings of its own. + set_tests_properties(${_presenter_tests} PROPERTIES TIMEOUT 600 COST 200) add_executable(depthxr_osd_tests tests/osd_tests.cpp) target_link_libraries(depthxr_osd_tests PRIVATE depthxr_osd) target_compile_definitions(depthxr_osd_tests PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX XR_USE_GRAPHICS_API_D3D11) @@ -852,3 +939,48 @@ if(DEPTHXR_BUILD_TESTS) add_test(NAME depthxr_action_set_attachment_tests COMMAND depthxr_action_set_attachment_tests) set_tests_properties(depthxr_action_set_attachment_tests PROPERTIES TIMEOUT 10) endif() + +if(DEPTHXR_BUILD_TESTS) + # CTest may run tests side by side (ctest -j, as Build-Layer.ps1 does), but a test that touches a graphics device + # runs alone (RUN_SERIAL), WARP and the software Vulkan device included: never two at once (0x139 bug checks on + # this machine's GPU), and never beside the presenter's wall-clock pacing, which WARP's load on every core makes + # late (four presenter groups failed beside it under -j 8; none failed with only the CPU tests beside them). + # Every test is such a test except those listed here, which create no Direct3D or Vulkan device at all + # (stand-in runtimes and devices, or none). + set(_cpu_only_tests + vectorxr_hardware_probe_self_test + depthxr_layer_crt_imports_tests + depthxr_layer_load_tests + depthxr_crt_failure_report_tests + depthxr_test_adapter_tests + ${_presenter_tests} + depthxr_turbo_frame_tests + depthxr_pivot_pose_tests + depthxr_pivot_snap_tests + depthxr_pivot_gaze_tests + depthxr_eye_gaze_tests + depthxr_head_prediction_layer_tests + depthxr_spacewarp_probe_tests + depthxr_layer_tests + depthxr_head_prediction_tests + depthxr_turbo_trace_tests + depthxr_turbo_recovery_tests + depthxr_runtime_relay_tests + depthxr_external_status_tests + depthxr_process_exit_tests + depthxr_path_unicode_tests + depthxr_sound_player_tests + depthxr_thread_census_tests + depthxr_action_set_attachment_tests) + get_property(_layer_tests DIRECTORY PROPERTY TESTS) + # Labelled too, so CI runs the two kinds on separate runners at once + # (Build-Layer.ps1 -Tests Cpu or Graphics): ctest -L graphics / -LE graphics. + foreach(_test IN LISTS _layer_tests) + if(NOT _test IN_LIST _cpu_only_tests) + set_property(TEST ${_test} PROPERTY RUN_SERIAL TRUE) + set_property(TEST ${_test} APPEND PROPERTY LABELS graphics) + else() + set_property(TEST ${_test} APPEND PROPERTY LABELS cpu) + endif() + endforeach() +endif() diff --git a/layer/include/depthxr/framegen_d3d11_bridge.h b/layer/include/depthxr/framegen_d3d11_bridge.h index 938c0ebc..48e6bb15 100644 --- a/layer/include/depthxr/framegen_d3d11_bridge.h +++ b/layer/include/depthxr/framegen_d3d11_bridge.h @@ -33,8 +33,21 @@ namespace depthxr { // acquired again. Neither API waits for the other on the CPU. // // Images a twin cannot be: multisampled ones are rendered privately and -// resolved into the twin at release; depth stays private and never reaches -// the runtime (the caller keeps it out of submitted frames). +// resolved into the twin at release. Depth is rendered privately too +// (Direct3D 12 shares no depth with simultaneous access): at release, +// Direct3D 11 copies its depth plane into a twin (R16_UNORM for D16, else +// R32_FLOAT), and the bridge's +// queue draws the twin into the runtime's depth image as depth, ahead of +// Frame Generation's capture as for colour, so the runtime and Frame +// Generation both get the game's depth plane (D16, D24S8, D32 and D32S8 +// alike: value for value, but for D24S8, whose 24-bit depth is read +// normalized and may arrive one 24-bit step off; stencil is not carried). +// Depth the bridge cannot mirror (multisampled, a runtime image that is no +// depth target, or VECTORXR_D3D11_BRIDGE_DEPTH=private) stays private and +// never reaches the runtime (the caller keeps it out of submitted frames). +// A mirrored depth swapchain turns the game device's multithread protection +// on (never off): each copy holds the immediate context through its calls, +// which keeps out only threads whose calls are protected. // // Every Direct3D 11 call is made inside the application's own OpenXR calls, // on its thread; nothing here is called from the presenter thread. @@ -47,6 +60,9 @@ class D3D11Bridge { // Where retirements that leaked, skipped copies and second threads are reported. void SetLog(std::function log); + // Before Begin: keep every depth swapchain private (Frame Generation's + // Use game depth off), as VECTORXR_D3D11_BRIDGE_DEPTH=private does. + void SetKeepDepthPrivate(bool keep); // Before xrCreateSession, for a session whose first structure is a // Direct3D 11 binding: makes a Direct3D 12 device on the game's adapter @@ -83,14 +99,16 @@ class D3D11Bridge { // At each xrEndFrame (the application's thread): the game's immediate // context is used by the bridge from the threads its swapchain calls come // from; when those (or the frame's) differ, the device's multithread - // protection is turned on, so the bridge's calls cannot race the game's. + // protection is turned on at the next StageReleases (before any lock, + // as it waits for the device's lock), so the bridge's calls cannot race + // the game's. // With VECTORXR_D3D11_BRIDGE_DEBUG=1, also reports what the Direct3D 12 // debug layer found. void FrameEnded(); // The runtime's create info for the application's: the bridge's copies - // write the runtime's colour images as render targets do, and - // multisampled images are resolved to one sample. + // write the runtime's colour images as render targets do, multisampled + // images are resolved to one sample, and depth images are depth targets. static XrSwapchainCreateInfo RuntimeCreateInfo(const XrSwapchainCreateInfo& info); // After the runtime created `swapchain` from RuntimeCreateInfo(info): // makes an image for each of its images (`enumerate` is the runtime's). @@ -102,15 +120,16 @@ class D3D11Bridge { // fails: their releases need not wait on the runtime's queue. void SetComposed(XrSwapchain swapchain, bool composed); bool Composed(XrSwapchain swapchain) const; - // Bridged, and the runtime's images never get anything (depth): the - // runtime's calls on it need no ordering with the bridge's queue, and its - // depth stays out of submitted frames. + // Bridged, and the runtime's images never get anything (depth the + // bridge cannot mirror): the runtime's calls on it need no ordering with + // the bridge's queue, and its depth stays out of submitted frames. bool Unmirrored(XrSwapchain swapchain) const; bool HasUnmirrored() const; // The Direct3D 11 textures rendered into, by image index (empty when not bridged). std::vector Images(XrSwapchain swapchain) const; // After the runtime handed out image `index`: Direct3D 11 waits (on the - // GPU) until the bridge's queue is done with the image's last release. + // GPU) until the bridge's queue is done with the image's last release + // (queued on the game's context without the bridge's lock). void Acquired(XrSwapchain swapchain, std::uint32_t index); // At the frame's end after the application released the image (the layer // hands releases over inside xrEndFrame, or the next acquire on the @@ -120,12 +139,33 @@ class D3D11Bridge { // copied into the runtime's image on the bridge's queue. False when the // copy was skipped (the bridge's queue is stalled): the runtime's image // holds an older frame, which Frame Generation must not capture as new. + // Released calls the game's immediate context only for a release whose + // Direct3D 11 half StageReleases did not run, taking it first. bool Released(XrSwapchain swapchain); // The application's xrReleaseSwapchainImage of a bridged image, whose // hand-over (Released) waits for the frame's end: its thread counts among // the game's swapchain and frame calls' threads, as it did when the copy - // ran there (a second thread turns multithread protection on). - void ReleaseCalled(); + // ran there (a second thread turns multithread protection on), and the + // release is counted for StageReleases. + void ReleaseCalled(XrSwapchain swapchain); + // At a hand-over, before the caller takes any lock of its own (xrEndFrame + // for every swapchain: XR_NULL_HANDLE; the next acquire or wait on + // `swapchain`): runs the Direct3D 11 half of each release ReleaseCalled + // counted (a depth image's copy into its twin, a multisampled image's + // resolve, the "written" signal), holding the game's immediate context + // first. Released then only submits the copy into the runtime's image. + // A game thread may hold the device's lock (ID3D11Multithread::Enter) + // while it makes an OpenXR call that takes the caller's locks: this waits + // for that lock holding none of them. Nothing to run: returns at once. + void StageReleases(XrSwapchain swapchain); + // Tests: called inside a depth copy into its twin, holding the game's + // immediate context with the copy's compute state bound, before its + // dispatch and before the game's state goes back. + void SetDepthCopyHookForTest(std::function hook); + // Tests: called just before the bridge enters the game's immediate + // context's lock (multithread protection on), on the thread entering it. + // Set and cleared while no bridge call runs. + void SetContextWaitHookForTest(std::function hook); // A release of an image nothing was drawn into: no copy; the runtime's // image keeps what it had. void Abandoned(XrSwapchain swapchain); @@ -144,6 +184,7 @@ class D3D11Bridge { std::unique_ptr impl_; std::function log_; bool queried_d3d12_requirements_{false}; + bool keep_depth_private_{false}; }; } // namespace depthxr diff --git a/layer/include/depthxr/framegen_gpu.h b/layer/include/depthxr/framegen_gpu.h index b70a6fd3..3352d123 100644 --- a/layer/include/depthxr/framegen_gpu.h +++ b/layer/include/depthxr/framegen_gpu.h @@ -23,6 +23,12 @@ struct FrameGenImage { std::uint32_t array_index{0}; XrRect2Di rect{}; std::int64_t format{0}; // swapchain creation format, native to the graphics API + // Edge fill (an output image, FrameGenGpu::SupportsEdgeFill): where the + // view's frame starts in `rect`, which is then wider than the frame (its + // FrameGenViewRequest::extent, or a composed eye's) by a margin on each + // side; every margin pixel repeats the frame's nearest edge pixel. {0, 0} + // with `rect` the frame's own size: no fill. + XrOffset2Di content{0, 0}; }; struct FrameGenViewSubmission { @@ -282,6 +288,18 @@ class FrameGenGpu { // Whether generated frames are drawn straight into the output images // (no intermediate image per view). virtual bool DrawsOutputs() const { return false; } + // Edge fill: output images wider than their frame, the frame placed at + // FrameGenImage::content and the margin around it its nearest edge + // repeated (Direct3D 12, while it draws generated frames straight into + // the outputs). Without it, outputs are the frame's own size. + virtual bool SupportsEdgeFill() const { return false; } + // Edge fill: whether the last Execute, Synthesize, SynthesizeStale or + // SynthesizeComposed(Stale) drew the margin of every output it wrote + // that has one. False when an output was written by the copy fallback + // (its margin keeps what it held): the caller then shows that frame from + // its own region (FrameGenImage::content, the frame's extent) with the + // game's field of view. Under the caller's GPU lock, right after the call. + virtual bool EdgesFilled() const { return false; } // Tests: the draws of generated frames into the output images recorded // so far (DrawsOutputs; frames drawn together count once). virtual std::uint64_t DrawsForTest() const { return 0; } diff --git a/layer/include/depthxr/framegen_plan.h b/layer/include/depthxr/framegen_plan.h index aeba7a0f..26139ae8 100644 --- a/layer/include/depthxr/framegen_plan.h +++ b/layer/include/depthxr/framegen_plan.h @@ -511,6 +511,50 @@ struct FrameGenFov { float down{0.0f}; }; +// Edge fill (FrameGenSettings::edge_fill): the runtime turns each frame it is +// given to where the head is when it shows it, and where it turns past the +// frame's edge it has nothing to show (black on the leading edge of a fast +// turn, widest for generated frames, which keep their real frame's pose). +// Output images are made wider than the frame by a margin each side, the +// margin filled with the frame's nearest edge (FrameGenImage::content), and +// submitted with the field of view widened to match: the runtime then turns +// a smeared edge into view instead of black. Margins in pixels. +struct FrameGenEdgeMargins { + std::uint32_t left{0}; + std::uint32_t right{0}; + std::uint32_t up{0}; + std::uint32_t down{0}; + bool Any() const { return left || right || up || down; } + bool operator==(const FrameGenEdgeMargins& other) const { + return left == other.left && right == other.right && up == other.up && down == other.down; + } + bool operator!=(const FrameGenEdgeMargins& other) const { return !(*this == other); } +}; + +// The margin each side the fill adds by default: degrees beyond each edge +// (VECTORXR_FRAMEGEN_EDGE_FILL=h[,v] in the research file changes them). +inline constexpr float kFrameGenEdgeFillHorizontalDegrees = 3.0f; +inline constexpr float kFrameGenEdgeFillVerticalDegrees = 1.5f; +// Output images are never made wider or taller than this (Direct3D 12's +// largest texture); margins shrink alike to fit. +inline constexpr std::uint32_t kFrameGenEdgeFillMaxExtent = 16384; + +// The margins widening a frame of `extent` with field of view `fov` by +// `horizontal_degrees` beyond its left and right edges and +// `vertical_degrees` beyond its top and bottom, at the frame's own pixel +// spacing (the image stays one projection, its tangents spaced alike): no +// edge goes past 85 degrees off the view axis, no margin is wider than half +// the frame, and the image fits kFrameGenEdgeFillMaxExtent. None for a field +// of view that is not one (right not right of left, up not above down). +FrameGenEdgeMargins FrameGenEdgeFillMargins(const FrameGenFov& fov, FrameGenExtent extent, float horizontal_degrees, + float vertical_degrees); +// The widened image's field of view: the frame's, its tangents carried on +// across each margin at the frame's spacing (what the margins' pixels show). +FrameGenFov FrameGenEdgeFillFov(const FrameGenFov& fov, FrameGenExtent extent, const FrameGenEdgeMargins& margins); +// Parses VECTORXR_FRAMEGEN_EDGE_FILL: "h" or "h,v" degrees (0-15 each; one +// value for both). Nullopt for anything else. +std::optional> ParseFrameGenEdgeFillDegrees(const std::string& value); + // The quad views diagnostic overlay over one composed eye, drawn last, as the // layer's Direct3D 11 compositor draws it (QuadViewsD3D11ShaderSource): the // periphery, blend band and inset tinted, the inset's outline and the band's diff --git a/layer/include/depthxr/framegen_presenter.h b/layer/include/depthxr/framegen_presenter.h index 1dadefa6..3b60a456 100644 --- a/layer/include/depthxr/framegen_presenter.h +++ b/layer/include/depthxr/framegen_presenter.h @@ -26,6 +26,8 @@ namespace depthxr { +class GameContextLock; + class Logger; // Downstream calls the presenter makes for the frames it submits. `submit` @@ -158,6 +160,12 @@ struct FrameGenStatus { std::uint64_t runtime_clock_displacements{0}; bool threaded{false}; // presenting from the presenter thread FrameGenDepthState depth{FrameGenDepthState::kUnknown}; + // The last depth check found the game's depth the same all over in both + // eyes (no varying cells): sent and readable, but empty or never written + // (Star Citizen's, October 2026). The Vulkan bridge then checks when the + // game writes it (VulkanBridge::WantDepthTimingCheck). + bool depth_uniform{false}; + // The game's motion vectors steer the generated frames. Engine motion // vectors are research only so far: nothing sets this yet; the OSD and // the app say so once it does. @@ -225,6 +233,9 @@ struct FrameGenStatus { // its swapchains by then (the runtime shows a swapchain's newest), and no // newer frame shown as it is could take its place. std::uint64_t stale_skipped{0}; + // Such refreshes shown with a later frame as it is instead, past frames + // written first (a runtime that blanks frames left open, PimaxXR 0.4.4). + std::uint64_t stale_shown_ahead{0}; }; // What the On-Screen Display and the desktop app say about a Frame Generation @@ -290,6 +301,7 @@ struct FrameGenDynamicPolicy { double generated_share{0.2}; // (b) generated frames per second over the refreshes the game misses double unready_share{0.05}; // (c) refreshes whose real frame was not on the GPU yet, at most double late_analyses_share{0.5}; // (d) analyses ending after their frames were needed, at most + double game_rate_share{0.75}; // (e) the game's own rate engaged, at least this share of its rate before }; // Interpolate's hardware-flow fallback. Tests shorten these times; the @@ -431,6 +443,13 @@ class FrameGenPresenter { // session begun on quad views natively (Varjo) has four. Stereo until // the layer says otherwise. void SetRuntimeViewConfiguration(XrViewConfigurationType type); + // The runtime shows a blank refresh for a frame begun and never ended + // (PimaxXR 0.4.4 and earlier: its submission thread completes the PVR + // frame left out with one disabled layer, then waits a refresh more), so + // the presenter thread never leaves one open where anything consistent + // can be shown: the frame shown as it is nearest after the last, even + // past frames it writes, rather than nothing. Off unless the layer says. + void SetRuntimeBlanksOpenFrames(bool blanks); // Started with a working GPU backend; such a session keeps frame pacing // away from Turbo even while toggled off, so toggling never races Turbo. bool OwnsPacing() const; @@ -440,6 +459,12 @@ class FrameGenPresenter { // Settings can turn a running session off (frames pass through) and back // on; only starting Frame Generation needs a new session. void SetConfigured(bool enabled); + // The game's Direct3D 11 device's lock, in the game's own Direct3D 11 + // session (none otherwise): the backend's work on the game's immediate + // context waits for it before the presenter's locks (GameContextLock). + // Captures at a release are made under the layer's locks, which take it + // first themselves. Outlives the session it is set for. + void SetGameContextLock(GameContextLock* lock); // Steady renderable frames and session age required before engaging. void SetEngagementPolicy(std::uint32_t steady_frames, std::chrono::milliseconds minimum_session_age); // How long the video memory budget must have room for another set of @@ -687,7 +712,8 @@ class FrameGenPresenter { struct Output { XrSwapchain swapchain{XR_NULL_HANDLE}; - FrameGenExtent extent; + FrameGenExtent extent; // the whole image: with edge fill, the frame and its margins + FrameGenEdgeMargins margins; std::int64_t format{0}; FrameGenOutputKind requested{FrameGenOutputKind::kSrgb8}; // what the source asked for FrameGenOutputKind kind{FrameGenOutputKind::kSrgb8}; // what the swapchain format holds @@ -864,9 +890,21 @@ class FrameGenPresenter { // An output image per output index: its extent and what it holds. struct OutputSpec { - FrameGenExtent extent; + FrameGenExtent extent; // the frame's (the image adds `margins`) FrameGenOutputKind kind{FrameGenOutputKind::kSrgb8}; + FrameGenEdgeMargins margins; // edge fill (EdgeMarginsLocked) }; + // Edge fill: the margins of the output image for a frame of `extent` + // seen with `fov` (none while the setting is off or the backend cannot + // fill them: FrameGenGpu::SupportsEdgeFill). + FrameGenEdgeMargins EdgeMarginsLocked(const XrFovf& fov, FrameGenExtent extent) const; + // An output image's region and where its frame starts in it. + static FrameGenImage OutputImage(const Output& output); + // A view shown from `output`: its image, and with edge fill its field of + // view widened across the margins; unless the backend did not draw them + // (`filled` false: FrameGenGpu::EdgesFilled), then the frame's own region + // with the game's field of view. + static void ShowOutput(XrCompositionLayerProjectionView& view, const Output& output, bool filled); bool WantsEngagementLocked() const; // Frames are generated or about to be: the game's releases are captured. @@ -895,7 +933,10 @@ class FrameGenPresenter { // refresh period. void UpdateDynamicLocked(std::chrono::steady_clock::time_point now, XrDuration period); void DynamicEngageLocked(std::chrono::steady_clock::time_point now, double rate, double refresh); - void DynamicStandDownLocked(std::chrono::steady_clock::time_point now, const std::string& reason, bool probe); + // With check_engaged_fps (the game's rate engaged, (e)), it stands by + // without a hold while the game's rate standing by decides. + void DynamicStandDownLocked(std::chrono::steady_clock::time_point now, const std::string& reason, bool probe, + std::optional check_engaged_fps = std::nullopt); // Turned back on (toggled, configured, retried after a pause): Dynamic // stands by again and judges the game's rate afresh. void DynamicReenableLocked(std::chrono::steady_clock::time_point now); @@ -1047,7 +1088,10 @@ class FrameGenPresenter { std::uint64_t id{0}; XrTime time{0}; }; - AsIs CurrentAsIsLocked(const FrameCopy& copy, std::uint64_t id, XrTime time) const; + // `past_written`: frames Frame Generation writes do not end the search + // (a runtime that shows a blank refresh for a frame left open: a step + // back later beats it). + AsIs CurrentAsIsLocked(const FrameCopy& copy, std::uint64_t id, XrTime time, bool past_written = false) const; // How far each eye moves per unit of generated-frame time from the real // frame shown at `newest_time` (FrameGenViewRequest::shift), in that // eye's view space: as far as it moved since the previous real frame @@ -1164,7 +1208,15 @@ class FrameGenPresenter { // Arms GPU-aware pacing once a window shows the game's frames reaching // the GPU late (real frames uncaptured when needed, or captures ending // after their refresh was due). - void NoteGpuEvidenceLocked(std::uint64_t real_frames, std::uint64_t late_gpu, std::vector capture_done_ms); + struct GpuEvidence { + std::uint64_t real_frames{0}; + std::uint64_t late_gpu{0}; + std::vector capture_done_ms; + }; + // `window`'s evidence, if `clean` (no pause, loading, unrendered refresh + // or restart); with `pending`, it waits there for the next window and is + // taken only if that one is clean too. + void NoteGpuEvidenceLocked(std::optional* pending, bool clean, GpuEvidence window); // Wakes the presenter thread and app threads waiting on slots. void WakeSlots(); // Every runtime frame call goes through these, so the presenter knows the @@ -1546,6 +1598,7 @@ class FrameGenPresenter { std::chrono::steady_clock::time_point dynamic_engaged_at_{}; bool dynamic_probing_{false}; double passthrough_fps_{0.0}; // the game's own rate when Dynamic last engaged + std::optional dynamic_rate_check_fps_; // (e): its rate engaged, while standing by checks it std::string dynamic_reason_; std::uint64_t dynamic_engagements_{0}; std::uint64_t dynamic_stand_downs_{0}; @@ -1577,6 +1630,9 @@ class FrameGenPresenter { std::uint64_t copies_skipped{0}; std::uint64_t ring_exhausted{0}; // refreshes whose real frame's images newer captures had replaced std::uint64_t stale_skipped{0}; // refreshes not submitted: nothing current to show + // Refreshes that would have been left open, shown with a later frame + // as it is instead (SetRuntimeBlanksOpenFrames). + std::uint64_t stale_shown_ahead{0}; std::uint64_t unrendered{0}; // refreshes the runtime did not render (shouldRender false) std::uint64_t late_cpu{0}; std::uint64_t late_gpu{0}; @@ -1621,6 +1677,10 @@ class FrameGenPresenter { // that found it so (NoteRuntimeClockLocked). std::uint64_t clock_displacements{0}; std::uint64_t clock_unsteady_waits{0}; + // Runtime waits made (RuntimeWait), and of those, ones that blocked + // (at least max(1 ms, a tenth of a refresh), as NotePhaseLocked counts). + std::uint64_t runtime_waits_timed{0}; + std::uint64_t runtime_waits_blocked{0}; }; // ", quad=..." for summaries over the window since `start` (empty when // no frame of the quad compose site ended in it). @@ -1639,7 +1699,7 @@ class FrameGenPresenter { Totals totals; }; GuardWindow guard_; - std::array guard_failing_{}; // windows in a row each trigger (a)-(d) failed + std::array guard_failing_{}; // windows in a row each trigger (a)-(e) failed std::vector guard_capture_done_ms_; // GPU-aware pacing (VECTORXR_FRAMEGEN_GPU_AHEAD: off, 0, 1, 2 or @@ -1650,6 +1710,9 @@ class FrameGenPresenter { // no capture running meanwhile (a slow GPU is held for, a stuck one not). bool gpu_gate_enabled_{true}; bool gpu_gate_armed_{false}; + // The last summary window's evidence for GPU-aware pacing, pending the + // next window (NoteGpuEvidenceLocked). + std::optional summary_evidence_; void ArmGpuGateLocked(const std::string& why); int gpu_frames_ahead_{1}; std::uint32_t gpu_timeouts_in_row_{0}; @@ -1904,6 +1967,7 @@ class FrameGenPresenter { bool logged_order_guard_{false}; double display_offset_periods_{0.0}; std::uint32_t display_offset_samples_{0}; + std::uint64_t display_offset_earlier_{0}; // frames ended for an earlier time than given (not compensated) // For the watchdog logs and PresenterActivity (read without locks). std::atomic compositor_phase_{"stopped"}; bool logged_watchdog_{false}; @@ -2077,6 +2141,8 @@ class FrameGenPresenter { // The runtime session's view configuration (SetRuntimeViewConfiguration): // the one the probe's locates ask for. XrViewConfigurationType runtime_view_configuration_{XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO}; + bool runtime_blanks_open_frames_{false}; // SetRuntimeBlanksOpenFrames + bool logged_open_frame_shown_{false}; // Probed frames waiting for the real frame after their time: the first // eye where the runtime put it and where the frame was shown from, in // the space of the real frame it was written from. `ahead`: past that @@ -2153,7 +2219,26 @@ class FrameGenPresenter { std::vector gpu_wait_ms_; std::vector end_call_ms_; std::vector runtime_end_ms_; + // The runtime's xrWaitFrame time (RuntimeWait): beside runtime_end_ms_, + // it tells a runtime pacing in xrEndFrame (PimaxXR's "Prefer framerate + // over latency": no wait ever blocks) from a late caller. + std::vector runtime_wait_ms_; std::vector synth_lock_ms_; + // A runtime whose xrEndFrame holds each frame until its own slot + // (PimaxXR with defer_frame_wait, as its async submission waits there) + // holds its swapchain lock all that time, and the game's swapchain calls + // wait for it. Its frames are then submitted shortly before the slot + // (RuntimeEnd): `runtime_end_block_ms_` is how long its calls made at + // once took (smoothed), `runtime_end_return_` the last one's return, and + // `runtime_end_margin_ms_` how long before the slot a frame goes. + // `runtime_end_hold_ms_`: since the last summary, how long frames waited. + double runtime_end_block_ms_{0.0}; + std::uint32_t runtime_end_block_samples_{0}; + std::optional runtime_end_return_; + double runtime_end_margin_ms_{0.0}; + std::vector runtime_end_hold_ms_; + bool runtime_end_hold_{true}; // VECTORXR_FRAMEGEN_END_HOLD (on unless "off") + bool logged_runtime_end_hold_{false}; // Since the last summary, for the game's frames (ThreadedWaitLocked, // ThreadedEndJit): std::uint64_t told_lead_sum_{0}; // the leads told, summed @@ -2354,6 +2439,11 @@ class FrameGenPresenter { // sub-pixel motion, with VECTORXR_FRAMEGEN_SYNTH_FLOW_VIEW=fine, read at // Start and at each config reload. bool flow_view_fine_{false}; + // Edge fill (FrameGenSettings::edge_fill): degrees each margin reaches + // past the frame's edge, horizontally and vertically; research + // VECTORXR_FRAMEGEN_EDGE_FILL=h[,v] changes them, read at Start and at + // each config reload. + std::array edge_fill_degrees_{kFrameGenEdgeFillHorizontalDegrees, kFrameGenEdgeFillVerticalDegrees}; // Research (the head-motion shimmer diagnosis, D): where extrapolated // frames' eyes go (FrameGenPredictEye; VECTORXR_FRAMEGEN_SYNTH_EYE), read // at Start. @@ -2419,6 +2509,7 @@ class FrameGenPresenter { EngineWindow engine_window_; // Timed: teardown never waits on them for long (LockForTeardown). std::timed_mutex submit_mutex_; // presenter-thread submissions vs output swapchain calls + std::atomic game_context_lock_{nullptr}; // SetGameContextLock std::timed_mutex gpu_mutex_; // the GPU backend (Track, Capture, Execute, WaitIdle, Forget, Reset) double gpu_ms_{-1.0}; // last generation GPU time, read without the backend // The GPU did not finish this session's Frame Generation work before diff --git a/layer/include/depthxr/framegen_runtime_queue.h b/layer/include/depthxr/framegen_runtime_queue.h index ad261c7d..5e7461a9 100644 --- a/layer/include/depthxr/framegen_runtime_queue.h +++ b/layer/include/depthxr/framegen_runtime_queue.h @@ -78,6 +78,15 @@ class FrameGenQueueSubstitution { bool Active() const; // Where a held-back call the runtime failed is reported (a few times). void SetLog(std::function log); + // The runtime queues work for an application image on the session's + // queue only once, before its first wait (PimaxXR and Virtual Desktop's + // runtime: a barrier batch at the first xrEnumerateSwapchainImages; their + // compositors read images on a device of their own, after a fence on the + // queue at xrEndFrame). Then only each image's first wait makes the + // game's queue wait for the runtime's: later ones would only make the + // game's next frame wait for Frame Generation's work queued there (each + // frame it writes, which waits for the game's last frame to be measured). + void SetRuntimeQueuesOnlyFirstUse(bool only_first_use); // The chain Frame Generation's backend is created from: the runtime's // queue (a FrameGenRuntimeQueue) ahead of the game's own `game_chain`. const void* FrameGenChain(const void* game_chain); diff --git a/layer/include/depthxr/framegen_vulkan_bridge.h b/layer/include/depthxr/framegen_vulkan_bridge.h index edb68f8c..71ed56a5 100644 --- a/layer/include/depthxr/framegen_vulkan_bridge.h +++ b/layer/include/depthxr/framegen_vulkan_bridge.h @@ -49,7 +49,21 @@ struct VulkanBridgeGame { // Depth is a twin as well (the runtime gets the game's depth). Multisampled // colour is rendered privately and resolved into the twin at release; // multisampled depth stays private and never reaches the runtime (the caller -// keeps it out of submitted frames). +// keeps it out of submitted frames), as does all depth with +// VECTORXR_VULKAN_BRIDGE_DEPTH=private. +// +// A game may write its depth after the frame's end (Star Citizen in October +// 2026, which CIG said it will fix). About once a second the bridge reads a +// grid of a depth swapchain's image at its hand-over and again at the +// swapchain's next acquire; depth that varies only at the acquire, twice in a +// row, is taken from there: each acquire copies the image handed over last, +// and each hand-over gives the runtime that copy (the frame before's depth) +// until depth is in the image at the hand-over again. +// VECTORXR_VULKAN_BRIDGE_DEPTH=end-frame keeps it at the hand-over, =late takes +// it at the acquire from the start; VECTORXR_VULKAN_BRIDGE_DEPTH_CHECK_MS sets +// the check's interval (tests: 0, every hand-over). The check runs only while +// Frame Generation finds the game's depth empty (WantDepthTimingCheck). + // // Every Vulkan call that uses the game's queue is made inside the // application's own xrAcquireSwapchainImage, xrReleaseSwapchainImage and @@ -133,6 +147,15 @@ class VulkanBridge { // queue just after the copy, and logs both (nonzero texels, least and // most). Off, nothing of it is made or submitted. void SetDepthProbe(bool on); + // Before Begin: keep every depth swapchain private (Frame Generation's + // Use game depth off), as VECTORXR_VULKAN_BRIDGE_DEPTH=private does. + void SetKeepDepthPrivate(bool keep); + // Whether the bridge checks when the game's depth arrives (above): while + // Frame Generation finds the game's depth the same all over (empty, or + // written after the frame's end), and while the bridge takes it late. + // Otherwise a game's submissions are exactly as without the check. + // VECTORXR_VULKAN_BRIDGE_DEPTH_CHECK=always checks every depth swapchain. + void WantDepthTimingCheck(bool want); // Swapchain formats: a VkFormat's DXGI format for the runtime, and back // (0 when there is none). @@ -237,6 +260,7 @@ class VulkanBridge { std::function log_; std::function info_log_; bool queried_d3d12_requirements_{false}; + bool keep_depth_private_{false}; }; } // namespace depthxr diff --git a/layer/include/depthxr/game_context_lock.h b/layer/include/depthxr/game_context_lock.h new file mode 100644 index 00000000..8b17bfa8 --- /dev/null +++ b/layer/include/depthxr/game_context_lock.h @@ -0,0 +1,126 @@ +#pragma once + +// Included by .cpp files only: it brings in Direct3D 11's headers. +#include +#include +#include + +#include +#include +#include +#include + +namespace depthxr { + +// The game's Direct3D 11 immediate context's lock (ID3D11Multithread), held +// by GameContextLock::Hold until the hold goes. +class GameContextHold { + public: + GameContextHold() = default; + explicit GameContextHold(Microsoft::WRL::ComPtr entered) : entered_(std::move(entered)) {} + GameContextHold(GameContextHold&& other) noexcept : entered_(std::move(other.entered_)) {} + GameContextHold& operator=(GameContextHold&& other) noexcept { + if (this != &other) { + Release(); + entered_ = std::move(other.entered_); + } + return *this; + } + GameContextHold(const GameContextHold&) = delete; + GameContextHold& operator=(const GameContextHold&) = delete; + ~GameContextHold() { Release(); } + + bool Held() const { return entered_ != nullptr; } + // Lets the lock go now (on the thread that took it); again does nothing. + void Release() { + if (entered_) { + entered_->Leave(); + entered_.Reset(); + } + } + + private: + Microsoft::WRL::ComPtr entered_; +}; + +// The lock order for the game's Direct3D 11 immediate context: the device's +// lock (ID3D11Multithread::Enter) before any lock of the layer's. +// +// With multithread protection on, every call on the immediate context takes +// the device's lock (Flush and a few others take it even without). A game +// may hold that lock (Enter) on one thread through an OpenXR call, which +// waits for the layer's locks; a layer thread that waited for the device's +// lock while holding one of them would deadlock with it. So code that calls +// the game's context under its own locks takes the device's lock first, +// holding none of them: Hold, before the first of its locks, kept until the +// last of its calls on the context is made. Enter is recursive: calls on the +// context, and holds taken again inside, then never wait. +// +// Only while the game's device is multithread protected: without protection +// the context's calls take no lock a game could hold across its OpenXR calls +// (but for Flush), and the layer adds no Enter/Leave to them. Protection is +// read at each Hold (holding no lock: Direct3D 11's debug layer waits for +// the device's lock to read it) and never changed here. +// +// Set and Hold may be called from any thread. +class GameContextLock { + public: + // The game's device (its immediate context's lock), or none. + void Set(ID3D11Device* device) { + Microsoft::WRL::ComPtr multithread; + if (device) { + Microsoft::WRL::ComPtr context; + device->GetImmediateContext(&context); + if (context) { + context.As(&multithread); + } + } + std::scoped_lock lock(mutex_); + multithread_ = std::move(multithread); + active_.store(multithread_ != nullptr, std::memory_order_release); + } + bool Active() const { return active_.load(std::memory_order_acquire); } + + // Before taking any lock of the caller's: enters the device's lock when + // its multithread protection is on, else holds nothing. + GameContextHold Hold() { + if (!Active()) { + return {}; + } + Microsoft::WRL::ComPtr multithread; + std::function hook; + { + std::scoped_lock lock(mutex_); + multithread = multithread_; + if (has_hook_) hook = wait_hook_; + } + if (!multithread) { + return {}; + } + if (hook) { + hook(); + } + if (multithread->GetMultithreadProtected() == FALSE) { + return {}; + } + multithread->Enter(); + return GameContextHold(std::move(multithread)); + } + + // Tests: called in Hold, on its thread, before it reads the device's + // protection and enters its lock (holding none of the caller's locks). + void SetWaitHookForTest(std::function hook) { + std::scoped_lock lock(mutex_); + has_hook_ = static_cast(hook); + wait_hook_ = std::move(hook); + } + + private: + mutable std::mutex mutex_; // only while copying the members: no call is made under it + Microsoft::WRL::ComPtr multithread_; + std::atomic active_{false}; + bool has_hook_{false}; + std::function wait_hook_; +}; + +} // namespace depthxr diff --git a/layer/include/depthxr/openxr_layer.h b/layer/include/depthxr/openxr_layer.h index 5f03e6b6..d81a8897 100644 --- a/layer/include/depthxr/openxr_layer.h +++ b/layer/include/depthxr/openxr_layer.h @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -46,6 +47,8 @@ #include "depthxr/framegen_runtime_queue.h" #include "depthxr/turbo_recovery.h" #include "depthxr/turbo_prediction_history.h" +#include "depthxr/turbo_probe_guard.h" +#include "depthxr/upstream_turbo.h" #include "depthxr/turbo_trace.h" #include "depthxr/settings_resolver.h" #include "depthxr/handshake_census.h" @@ -69,6 +72,9 @@ struct ID3D11VertexShader; namespace depthxr { +class GameContextLock; +class GameContextHold; + #if defined(DEPTHXR_TESTING) class TurboFrameTestPeer; #endif @@ -85,8 +91,34 @@ class OpenXrLayer { void SetLayerDirectory(std::filesystem::path dll_directory); void SetNextProcAddr(PFN_xrGetInstanceProcAddr next_get_instance_proc_addr); bool CanCreateInstance(); + // A second instance passed straight through (layer_entry.cpp's + // PassThroughInstance): said in the first instance's log. + void NotePassThroughInstance(const std::string& application_name, XrResult result); + // Another instance is being created while VectorXR's is alive. An instance + // that enabled no graphics extension and never asked for a session cannot + // have a session VectorXR could serve: OpenXR Toolkit's and + // Quad-Views-Foveated's probe instance, which they keep alive on VIVE + // runtimes. VectorXR lets it go as at xrDestroyInstance, without + // destroying it in the runtime, and answers true with what its calls + // go on to (it passes straight through from here); the new instance is + // VectorXR's. Any other instance keeps VectorXR (false). + struct RetiredInstance { + XrInstance instance{XR_NULL_HANDLE}; + PFN_xrGetInstanceProcAddr next_get_instance_proc_addr{nullptr}; + PFN_xrDestroyInstance next_destroy_instance{nullptr}; + }; + bool RetireIdleInstance(const std::string& next_application_name, RetiredInstance* retired); + // The application's name as it created the instance. + std::string application_name_; + // Native quad views on a runtime that shows the focus views with the FOVs + // it located for the frame's display time (PimaxXR, DCS World). + bool RuntimeTakesFocusFovsFromLocateLocked() const; + bool native_quad_focus_shares_eye_poses_{false}; + bool has_logged_focus_fov_quirk_convergence_{false}; struct InstanceCreateDiagnostics { + // "absent", "above" or "below" VectorXR. + std::string quad_views_foveated{"absent"}; bool turbo_clock_enabled{false}; bool app_requested_quad_views{false}; bool app_requested_varjo_foveated_rendering{false}; @@ -145,9 +177,9 @@ class OpenXrLayer { // The names of the layers below VectorXR, nearest first. std::string layers_below; // Where OpenXR Toolkit is: "absent", "below", "above", or - // "above-bypassed" (its per-application bypass passes everything - // through). Above VectorXR it gets a game's quad views, which it does - // not support. + // "above-bypassed" / "below-bypassed" (its per-application bypass + // passes everything through). Above VectorXR it gets a game's quad + // views, which it does not support; below it, Frame Generation's frames. std::string openxr_toolkit{"absent"}; // The space-warp probe (spacewarp_probe.h): VECTORXR_SPACEWARP_PROBE // (parsed, and as set: empty when it is not), what the application @@ -195,6 +227,9 @@ class OpenXrLayer { void AbandonInstance(); XrResult GetInstanceProcAddr(XrInstance instance, const char* name, PFN_xrVoidFunction* function); XrResult DestroyInstance(XrInstance instance); + // DestroyInstance's teardown; `in_runtime` false keeps the runtime's + // instance (RetireIdleInstance). + XrResult EndInstance(XrInstance instance, bool in_runtime); XrResult CreateSession(XrInstance instance, const XrSessionCreateInfo* create_info, XrSession* session); XrResult DestroySession(XrSession session); XrResult BeginSession(XrSession session, const XrSessionBeginInfo* begin_info); @@ -316,7 +351,7 @@ class OpenXrLayer { friend class SpaceWarpProbeTestPeer; #endif - OpenXrLayer() = default; + OpenXrLayer(); ~OpenXrLayer(); struct SwapchainInfo { @@ -599,7 +634,8 @@ class OpenXrLayer { // (and again after a config change); the verdict/stall helpers run on the // frame thread after the lock is released. void ResolveTurboPacingModeLocked(); - void RecordTurboPacingVerdict(TurboPacingMode mode, const char* source, std::int64_t stable_seconds); + void RecordTurboPacingVerdict(TurboPacingMode mode, const char* source, std::int64_t stable_seconds, + std::int64_t early_probe_failures = 0); void QueueRuntimePacingWrite(RuntimePacingObservation observation); void DrainRuntimePacingWrites(); void NoteTurboPacingStableFrame(double app_frame_delta_ms); @@ -609,10 +645,16 @@ class OpenXrLayer { // Releases config_lock (mutex_) before any blocking runtime call so // xrLocateViews/xrLocateSpace from other app threads are never serialized // behind the compositor pacing wait. Call as the tail of EndFrame only. + // `context_hold`: EndFrame's hold of the game's Direct3D 11 context + // (game_context_), let go with config_lock. XrResult ForwardEndFrame(XrSession session, const XrFrameEndInfo* frame_end_info, - std::unique_lock& config_lock); + std::unique_lock& config_lock, + GameContextHold* context_hold = nullptr); void ObserveCompositionLayerTopology(const XrFrameEndInfo* frame_end_info); + // At the app's xrEndFrame: whether a layer above pipelines its frames + // already (upstream_turbo_); logs once when that is first seen. + bool NoteUpstreamTurboFrame(); void ArmTurboAsyncHandoffLocked(const char* reason); void PublishTurboAsyncHandoffLocked(); void CancelTurboAsyncHandoffLocked(const char* reason); @@ -724,9 +766,21 @@ class OpenXrLayer { // swapchain's: while the Vulkan bridge gives the game's motion vector // images back at their releases (vulkan_bridge_motion_), each // swapchain's copies are made first, then the captures and releases. + // `forward_later`: the releases due on to the runtime are added to it + // (a swapchain per release) instead, for ForwardHandedOverReleases. XrResult HandOverBridgedReleasesLocked(XrSwapchain swapchain, SwapchainInfo& info, std::string_view reason, - bool copy = true); - XrResult HandOverBridgedReleasesLocked(std::string_view reason, bool copy = true); + bool copy = true, std::vector* forward_later = nullptr); + XrResult HandOverBridgedReleasesLocked(std::string_view reason, bool copy = true, + std::vector* forward_later = nullptr); + // The releases a hand-over left for later, on to the runtime with + // `lock` (of mutex_) let go meanwhile: a runtime's swapchain calls can + // stall for a refresh (PimaxXR's xrEndFrame holds their lock, called from + // Frame Generation's thread), and the game's other threads' calls into + // the layer must not wait for that. The caller holds + // bridge_handover_mutex_, so no other hand-over or swapchain call of the + // game's comes between the hand-over and its releases. + XrResult ForwardHandedOverReleases(std::unique_lock& lock, std::vector& releases, + std::string_view reason); // At a session's (or an instance's) end, before the Vulkan bridge's // Escape: releases no frame's end handed over still give the game's // rendering into their images to the bridge (its Released, not Frame @@ -890,13 +944,23 @@ class OpenXrLayer { bool EnsureD3D11QuadViewsPixelProbeResources(int64_t format); void ReleaseD3D11QuadViewsPixelProbeResources(bool preserve_history); void PollD3D11QuadViewsPixelProbe(); + // `eye_in_view`: each stereo eye's orientation relative to VIEW when the + // runtime's eyes are canted (CantedEyeOrientations), else null. bool SynthesizeQuadViewsFromStereo(std::span stereo_views, const QuadViewsSettings& quadviews_settings, double focus_yaw_radians, double focus_pitch_radians, uint32_t view_capacity_input, uint32_t* view_count_output, - XrView* views) const; + XrView* views, + const std::array* eye_in_view = nullptr) const; + // Whether the two stereo eyes the runtime located are turned from each + // other (a canted display), and if so each eye's orientation relative to + // VIEW at the locate's display time. Caller holds mutex_. + bool CantedEyeOrientations(XrSession session, + const XrViewLocateInfo& view_locate_info, + std::span stereo_views, + std::array* eye_in_view); XrResult LocateSpaceWithPivot(XrSpace space, XrSpace base_space, XrTime time, @@ -982,7 +1046,16 @@ class OpenXrLayer { // be sharpened is left untouched (submits the app's unsharpened focus). void SharpenNativeFocusViews(std::vector& views, XrTime display_time); + // The game's Direct3D 11 device's lock (a Direct3D 11 session's, bridged + // or not; set at xrCreateSession): taken before bridge_handover_mutex_ + // and mutex_ by every call that uses the game's immediate context under + // them (GameContextLock), while the game's device is multithread + // protected. Made with the layer, never replaced. + std::unique_ptr game_context_; std::mutex mutex_; + // Held across a bridged hand-over and its releases' runtime calls + // (ForwardHandedOverReleases), which go without mutex_; taken before mutex_. + std::mutex bridge_handover_mutex_; std::filesystem::path dll_directory_; std::filesystem::path config_path_; std::filesystem::path log_path_; @@ -1088,6 +1161,7 @@ class OpenXrLayer { D3D11Bridge d3d11_bridge_; bool d3d11_bridge_lost_logged_{false}; // mutex_ bool d3d11_bridge_depth_logged_{false}; // mutex_ + bool depth_rect_logged_{false}; // mutex_: a depth sub-image outside its image was left out (any session) bool d3d11_bridge_sharpen_logged_{false}; // mutex_ std::chrono::steady_clock::time_point d3d11_bridge_last_summary_; // mutex_ // Quad views frames of a bridged session (mutex_): composed by Frame @@ -1120,6 +1194,53 @@ class OpenXrLayer { std::optional pending_trace_quad_space_; D3D11BridgeQuadFrames d3d11_bridge_quad_frames_; D3D11BridgeQuadFrames d3d11_bridge_quad_frames_summarized_; + // The game's swapchain calls while the bridge is active: how long each + // took in the layer and the runtime, for the bridge's summary (a game + // blocked inside its own frame, DCS 2026-10-22: 57 ms of a frame with + // its thread 41% busy, the GPU half idle). + struct BridgeCallTiming { + std::atomic calls{0}; + std::atomic total_ns{0}; + std::atomic max_ns{0}; + void Add(std::uint64_t ns) { + ++calls; + total_ns += ns; + for (std::uint64_t seen = max_ns.load(); ns > seen && !max_ns.compare_exchange_weak(seen, ns);) { + } + } + // "avg/max" ms since the last call, then starts over. + std::string Take() { + const std::uint64_t count = calls.exchange(0); + const std::uint64_t total = total_ns.exchange(0); + const std::uint64_t most = max_ns.exchange(0); + if (!count) return "-"; + const auto ms = [](double ns) { + const long long hundredths = std::llround(ns / 1e4); + return std::to_string(hundredths / 100) + "." + (hundredths % 100 < 10 ? "0" : "") + + std::to_string(hundredths % 100); + }; + return ms(static_cast(total) / static_cast(count)) + "/" + ms(static_cast(most)); + } + }; + std::array bridge_call_timing_; // acquire, wait, release + class BridgeCallTimer { + public: + explicit BridgeCallTimer(BridgeCallTiming* timing) + : timing_(timing), start_(timing ? std::chrono::steady_clock::now() : std::chrono::steady_clock::time_point{}) {} + ~BridgeCallTimer() { + if (timing_) { + timing_->Add(static_cast( + std::chrono::duration_cast(std::chrono::steady_clock::now() - start_) + .count())); + } + } + BridgeCallTimer(const BridgeCallTimer&) = delete; + BridgeCallTimer& operator=(const BridgeCallTimer&) = delete; + + private: + BridgeCallTiming* timing_; + std::chrono::steady_clock::time_point start_; + }; // Vulkan games with Frame Generation: the session is Direct3D 12 and the // game renders into images shared with it (see VulkanBridge); decided at // xrCreateSession. Frame Generation runs a Vulkan session only through it. @@ -1132,6 +1253,8 @@ class OpenXrLayer { // Frame Generation's captures (HandOverBridgedReleasesLocked). mutex_. bool vulkan_bridge_motion_{false}; std::chrono::steady_clock::time_point vulkan_bridge_last_summary_; // mutex_ + std::chrono::steady_clock::time_point vulkan_bridge_last_depth_poll_; // mutex_ + // Either bridge's swapchain, and one whose runtime images never get // anything (its calls need no ordering with the game's queue). bool BridgedSwapchain(XrSwapchain swapchain) const { @@ -1319,6 +1442,12 @@ class OpenXrLayer { // number of extensions it enabled for it. bool instance_requested_session_{false}; uint32_t instance_enabled_extension_count_{0}; + // The application enabled a graphics API's extension (or a headless + // one): the instance can create a session. + bool instance_enabled_session_extension_{false}; + // RetireIdleInstance's line for the next instance's log (the retired + // instance's own log is a probe's, removed with it). + std::string retired_instance_note_; std::vector pivotxr_profile_input_states_; // IsPivotXrActive's per-call claim lists (under mutex_), kept so the // locate paths do not allocate. @@ -1387,6 +1516,19 @@ class OpenXrLayer { }; std::vector pivotxr_reference_changes_; bool PivotAnchorAffectedBy(XrReferenceSpaceType type) const; + // A runtime that recenters without the event (PimaxXR): the head located + // at the previous drive, against which a jump no head can make is taken + // as a recenter (NoteSilentRecenterLocked). Not watched once the runtime + // has sent a reference space change itself. + struct PivotDriveSample { + XrTime time{0}; + XrSpace reference_space{XR_NULL_HANDLE}; + XrPosef view_pose{}; + }; + std::optional pivotxr_last_drive_; + bool pivotxr_runtime_reports_recenters_{false}; + uint32_t pivotxr_silent_recenter_logs_{0}; + void NoteSilentRecenterLocked(XrTime time, XrSpace reference_space, const XrPosef& view_pose); bool depthxr_toggle_enabled_{true}; bool depthxr_toggle_binding_was_down_{false}; std::optional pivotxr_binding_last_poll_time_; @@ -1528,7 +1670,18 @@ class OpenXrLayer { // runtime, or the in-session fallback after async tripped. enum class TurboPacingSource { kForced, kPinned, kDiscovered, kProbing, kFallback }; std::uint64_t turbo_test_started_at_{0}; - bool TryTurboAutoFallbackLocked(const std::string& reason); + bool TryTurboAutoFallbackLocked(const std::string& reason, + std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now()); + // Auto's async probe (frame thread): stalls in its first seconds are not + // counted, and a fallback within seconds of engaging is "early" + // (TurboProbeGuard, runtime_pacing.h). + TurboProbeGuard turbo_probe_guard_; + int turbo_probe_grace_log_budget_{0}; + // This session's sequenced fallback came from a probe that failed early. + bool turbo_fallback_early_{false}; + // The recorded sequenced verdict a re-probe is testing again: its outcome + // is recorded at once if async falls back (sequenced is already proven). + std::optional turbo_reprobe_of_; // Frame-submission-thread only (resolved under the config lock at // EndFrame, consumed after it is released); the sequenced state machine // below is turbo_mutex_-guarded because WaitFrame/BeginFrame consult it. @@ -1558,6 +1711,11 @@ class OpenXrLayer { std::optional session_begin_wall_time_; bool turbo_cadence_pause_logged_{false}; double turbo_last_frame_blocked_ms_{0.0}; + // A layer above already pipelining the game's frames (OpenXR Toolkit's + // Turbo, upstream_turbo.h), under turbo_mutex_. Detected, Turbo stands + // aside for the session. + UpstreamTurboDetector upstream_turbo_; + bool upstream_turbo_logged_{false}; // Sequenced pacing state machine (turbo_mutex_). DCS overlaps xrWaitFrame // (sim thread) with xrEndFrame (render thread) — spec-legal — so the // fabrication shield must never have a per-frame gap, and establishment @@ -1796,6 +1954,24 @@ class OpenXrLayer { // Quadviews stays off and the game runs stereo. bool openxr_toolkit_above_{false}; bool openxr_toolkit_quadviews_logged_{false}; + // OpenXR Toolkit is below VectorXR for this instance (not bypassed): it + // holds every release back until its next xrEndFrame and then releases + // every swapchain it held, and is not safe to call from two threads, so + // Frame Generation (more frames than the game's, from its own thread) + // stays off. + bool openxr_toolkit_below_{false}; + bool openxr_toolkit_framegen_logged_{false}; + // Frame Generation was asked for but is kept off for that reason; the + // OSD says so instead of a plain Off (read on the OSD's thread). + std::atomic framegen_off_for_toolkit_{false}; + // The layers below VectorXR (names, nearest first) for this instance: + // with Frame Generation they get every frame it shows, from its thread, + // and (Direct3D 12) its queue as the session's. The session's frames the + // game ended and the frames the runtime got, for the session's end. + std::string layers_below_; + std::atomic game_frames_ended_{0}; + std::atomic runtime_frames_ended_{0}; + bool layers_below_get_framegen_{false}; // the session's (StartFrameGen) bool defer_quadviews_swapchain_releases_{false}; // Quadviews changes the application's view/swapchain topology. Once a session // is created, keep its initial activation state until teardown so a config @@ -1811,6 +1987,7 @@ class OpenXrLayer { XrSpace varjo_native_view_space_{XR_NULL_HANDLE}; XrSpace varjo_native_combined_eye_space_{XR_NULL_HANDLE}; bool varjo_native_rendering_gaze_tracked_{false}; + bool has_logged_varjo_native_rendering_gaze_uninformative_{false}; bool has_logged_varjo_native_rendering_gaze_active_{false}; bool has_logged_varjo_native_rendering_gaze_unavailable_{false}; bool varjo_native_foveation_resources_attempted_{false}; @@ -1853,6 +2030,7 @@ class OpenXrLayer { uint32_t pending_quadviews_pixel_diagnostics_{0}; bool has_active_primary_view_configuration_{false}; bool has_logged_quad_view_short_count_{false}; + bool has_logged_canted_quadviews_eyes_{false}; bool has_logged_pivotxr_spike_mode_{false}; bool has_logged_quadviews_view_configuration_capabilities_{false}; uint64_t varjo_native_view_configuration_calls_{0}; @@ -1944,6 +2122,26 @@ class OpenXrLayer { std::optional last_app_action_sync_result_; std::optional last_eye_gaze_self_sync_result_; XrSessionState observed_session_state_{XR_SESSION_STATE_UNKNOWN}; + // A new session's first state event not yet seen: events the runtime kept + // from the previous session (PimaxXR) are not taken as this one's state. + // Head prediction's answer to one xrLocateSpace query (space, base) at one + // display time, as its first locate gave it: later locates of the query + // at that time answer the same (LocateSpace). Never used for another time. + struct HeadExtensionAnswer { + XrTime time{0}; + XrSpace space{XR_NULL_HANDLE}; + XrSpace base_space{XR_NULL_HANDLE}; + XrTime limit{0}; + bool applied{false}; + XrPosef pose{}; + }; + static constexpr std::size_t kHeadExtensionAnswersKept = 8; + std::vector head_extension_answers_; + bool awaiting_session_start_event_{false}; + XrTime session_create_time_{0}; + uint32_t stale_session_event_logs_{0}; + void NoteSessionCreatedLocked(XrTime create_time); + bool IsStaleSessionEventLocked(const XrEventDataSessionStateChanged& change); bool eye_gaze_sync_not_focused_{false}; uint32_t eye_gaze_sync_transition_logs_remaining_{32}; std::optional last_eye_gaze_bound_sources_probe_; @@ -1953,6 +2151,19 @@ class OpenXrLayer { std::string eye_gaze_unfocused_outcome_; std::optional eye_gaze_unfocused_last_orientation_; uint32_t eye_gaze_unfocused_identical_frames_{0}; + // The focused session's gaze as last located, at which display time, and + // for how many display times in a row it has not changed at all. + std::optional eye_gaze_focused_last_orientation_; + XrTime eye_gaze_focused_last_time_{0}; + uint32_t eye_gaze_focused_identical_frames_{0}; + bool eye_gaze_focused_frozen_logged_{false}; + // Diagnostic: the display time since which the focused gaze has stayed + // near (yaw, pitch); logged once per episode, a few times per session. + std::optional eye_gaze_still_since_; + double eye_gaze_still_yaw_{0.0}; + double eye_gaze_still_pitch_{0.0}; + bool eye_gaze_still_logged_{false}; + int eye_gaze_still_logs_remaining_{8}; uint32_t eye_gaze_unfocused_logs_remaining_{16}; // Focused and inactive since (reset by valid gaze or focus loss); the // "no data" line is logged once per session. diff --git a/layer/include/depthxr/osd_graphics.h b/layer/include/depthxr/osd_graphics.h index dd3b88e5..645c1bb7 100644 --- a/layer/include/depthxr/osd_graphics.h +++ b/layer/include/depthxr/osd_graphics.h @@ -21,6 +21,8 @@ class OsdGraphics { virtual OsdUpload Upload(std::uint32_t, const OsdBitmap&, std::string& error) = 0; // Only teardown/retry may wait for this backend's outstanding copy. virtual void Reset() = 0; + // Direct3D 11: the game's device, whose immediate context Upload uses. + virtual ID3D11Device* D3D11Device() const { return nullptr; } }; std::unique_ptr CreateOsdGraphics(const void* binding_chain, std::string& error); std::unique_ptr CreateOsdD3D11(ID3D11Device*); diff --git a/layer/include/depthxr/osd_renderer.h b/layer/include/depthxr/osd_renderer.h index d0e55db7..a77182ea 100644 --- a/layer/include/depthxr/osd_renderer.h +++ b/layer/include/depthxr/osd_renderer.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -160,8 +161,11 @@ void FeatherOsdPixels(const std::uint32_t* bits, std::uint32_t* pixels, int pane // Produces premultiplied sRGB pixels for an sRGB OpenXR swapchain. OsdBitmap RasterizeOsd(const OsdSettings&, bool compact, const OsdSnapshot&, const OsdTelemetry&, bool rgba); +class GameContextLock; + class OsdRenderer { public: + OsdRenderer(); ~OsdRenderer(); void Initialize(XrSession, ID3D11Device*, std::uint32_t max_layers, OsdDispatch); void InitializeGraphics(XrSession, const void* binding_chain, std::uint32_t max_layers, OsdDispatch, Logger*, std::string identity); @@ -184,8 +188,13 @@ class OsdRenderer { // Called for every runtime xrEndFrame, after game-layer transforms: on the // application's end-frame thread, or Frame Generation's presenter thread. // Makes runtime calls (swapchain images; a world-anchored panel's space and - // head location) under the renderer's lock. + // head location) under the renderer's lock. Direct3D 11: while the panel + // is drawn, the game's device's lock is taken before the renderer's + // (GameContextLock), as its upload uses the game's immediate context. const XrCompositionLayerBaseHeader* Append(const XrFrameEndInfo&, bool should_render); + // Tests: called on Append's thread just before it takes the game's + // device's lock (GameContextLock::SetWaitHookForTest). + void SetContextWaitHookForTest(std::function hook); OsdStatus Status() const; // Places the panel in the world at each frame's head pose, located at // that frame's own display time, instead of in the VIEW space. A runtime @@ -202,6 +211,9 @@ class OsdRenderer { void Event(bool error, std::string message); std::vector> CollectDiagnosticsLocked(bool final); std::size_t Mode() const; + // The Direct3D 11 game's device's lock (InitializeGraphics), made with + // the renderer: read by Append before mutex_. + const std::unique_ptr context_lock_; mutable std::mutex mutex_; XrSession session_{XR_NULL_HANDLE}; XrSwapchain swapchain_{XR_NULL_HANDLE}; diff --git a/layer/include/depthxr/runtime_pacing.h b/layer/include/depthxr/runtime_pacing.h index 435af49f..1c493a87 100644 --- a/layer/include/depthxr/runtime_pacing.h +++ b/layer/include/depthxr/runtime_pacing.h @@ -31,8 +31,29 @@ struct RuntimePacingObservation { // verdict, and how long the winning mode ran clean. std::int64_t probe_timeouts{0}; std::int64_t stable_seconds{0}; + // Async probes in a row that fell back to sequenced within seconds of + // engaging (TurboProbeGuard). One such verdict is provisional: the next + // session probes async again. 0 in records from before 0.20.1. + std::int64_t early_probe_failures{0}; }; +// A recorded sequenced verdict is probed with async again once its evidence +// may be stale; the re-probe records its outcome either way, so each reason +// costs at most one probe (two when the re-probe also fails early): +// - the async probe behind it fell back early, once (kEarlyProbeFailureLimit +// early failures in a row and the verdict stands); +// - it was recorded by another VectorXR version, or last used with another +// runtime version (both empty-tolerant: no version, no reason); +// - it is older than kReprobeAge (testStartedAt, the session that tested it). +// Async verdicts are never re-probed here: async failing later falls back in +// the session. Returns the reason, or nothing when the verdict stands. +inline constexpr std::int64_t kEarlyProbeFailureLimit = 2; +inline constexpr std::int64_t kReprobeAgeMs = 30LL * 24 * 60 * 60 * 1000; +std::optional RuntimePacingReprobeReason(const RuntimePacingObservation& observation, + const std::string& layer_version, + const std::string& runtime_version, + std::int64_t now_unix_ms); + std::filesystem::path ResolveRuntimePacingPath(); std::vector ReadRuntimePacingObservations(const std::filesystem::path& path); diff --git a/layer/include/depthxr/settings.h b/layer/include/depthxr/settings.h index 9b0aa105..71fef8ad 100644 --- a/layer/include/depthxr/settings.h +++ b/layer/include/depthxr/settings.h @@ -581,6 +581,16 @@ struct FrameGenSettings { // Generation tells the game that the runtime does not predict (see // head_prediction.h). Off by default. bool extend_head_prediction{false}; + // Edge fill: frames Frame Generation writes reach a few degrees past the + // game's field of view, the margin repeating the frame's edge, so a fast + // head turn shows a smeared edge instead of black on its leading side + // (FrameGenEdgeMargins). Direct3D 12 and the bridges only. Off by default. + bool edge_fill{false}; + // Use the game's depth (on by default). Off, Frame Generation works + // without it and the bridges keep a bridged game's depth to itself, as + // before 0.20.1: no copy, and no multithread protection turned on for it. + // The bridges read it when the session starts. + bool use_game_depth{true}; }; struct FrameGenProfile { diff --git a/layer/include/depthxr/turbo_probe_guard.h b/layer/include/depthxr/turbo_probe_guard.h new file mode 100644 index 00000000..064faf11 --- /dev/null +++ b/layer/include/depthxr/turbo_probe_guard.h @@ -0,0 +1,107 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +namespace depthxr { + +// When a stalled async xrWaitFrame counts against Auto's async pacing. +// +// DCS on Pimax (Oct 29 S4 flight) left its loading screen at about 18 fps. The +// first two pipelined waits after Turbo engaged both ran into the drain bound, +// half a second apart, which tripped the probe's two-stall fallback the same +// moment async engaged, and Auto recorded a sequenced verdict. A session with +// async pinned then ran clean for 17 minutes, with one stall at its own +// engagement. So stalls right after engaging, or while the game is still +// hitching, are not counted. They still cost the frame its drain, so the grace +// is short and capped: a runtime that interlocks every off-thread wait (the +// case the fallback exists for) still falls back a few seconds after engaging. +// +// A fallback soon after engaging is "early": its sequenced verdict is recorded +// as provisional, so the next session probes async again (runtime_pacing.h). +// +// The times come from the caller (steady_clock in the layer, a script in its +// tests). The frame thread makes the calls; the times are atomic so an Auto +// fallback on another thread may read Early and SinceEngaged. +class TurboProbeGuard { + public: + using Clock = std::chrono::steady_clock; + + struct Limits { + // Stalls this soon after async pacing engages are not counted. + std::chrono::milliseconds grace{2000}; + // A game hitch (app time only, Turbo's own blocking subtracted) keeps + // stalls uncounted for this long after it... + std::chrono::milliseconds hitch_grace{1000}; + // ...but never past this point after engaging. + std::chrono::milliseconds max_grace{5000}; + // An app frame this slow is a hitch (the cadence gate's healthy bound). + double hitch_ms{50.0}; + // A fallback this soon after engaging is early. + std::chrono::milliseconds early_failure{10000}; + }; + + // Limits are set before use (tests); never while frames run. + void Configure(const Limits& limits) { limits_ = limits; } + const Limits& Settings() const { return limits_; } + void Reset() { + engaged_ns_.store(kNotEngaged, std::memory_order_relaxed); + grace_until_ns_.store(kNotEngaged, std::memory_order_relaxed); + } + + // Async pacing engaged (each engage, re-engages after a toggle included). + void Engaged(Clock::time_point now) { + grace_until_ns_.store(Ns(now + limits_.grace), std::memory_order_relaxed); + engaged_ns_.store(Ns(now), std::memory_order_relaxed); + } + + // Every app frame, with its app-only frame time (negative when unknown). + void AppFrame(Clock::time_point now, double app_frame_ms) { + const std::int64_t engaged = engaged_ns_.load(std::memory_order_relaxed); + if (engaged == kNotEngaged || app_frame_ms < limits_.hitch_ms) return; + const std::int64_t cap = engaged + Ns(limits_.max_grace); + const std::int64_t until = std::min(cap, Ns(now + limits_.hitch_grace)); + if (until > grace_until_ns_.load(std::memory_order_relaxed)) + grace_until_ns_.store(until, std::memory_order_relaxed); + } + + bool InGrace(Clock::time_point now) const { + return engaged_ns_.load(std::memory_order_relaxed) != kNotEngaged && + Ns(now) < grace_until_ns_.load(std::memory_order_relaxed); + } + + std::optional SinceEngaged(Clock::time_point now) const { + const std::int64_t engaged = engaged_ns_.load(std::memory_order_relaxed); + if (engaged == kNotEngaged) return std::nullopt; + return std::chrono::duration_cast(std::chrono::nanoseconds(Ns(now) - engaged)); + } + + bool Early(Clock::time_point now) const { + const auto since = SinceEngaged(now); + return since && *since < limits_.early_failure; + } + + private: + // Written on the frame thread; an Auto fallback can read them from the + // app's wait/begin thread (NoteTurboRuntimeFailure), so they are atomic + // (single words: no torn reads, no lock on the frame path). + static constexpr std::int64_t kNotEngaged = INT64_MIN; + template + static std::int64_t Ns(T value) { + if constexpr (std::is_same_v) { + return std::chrono::duration_cast(value.time_since_epoch()).count(); + } else { + return std::chrono::duration_cast(value).count(); + } + } + Limits limits_{}; + std::atomic engaged_ns_{kNotEngaged}; + std::atomic grace_until_ns_{kNotEngaged}; +}; + +} // namespace depthxr diff --git a/layer/include/depthxr/upstream_turbo.h b/layer/include/depthxr/upstream_turbo.h new file mode 100644 index 00000000..9f2a9acc --- /dev/null +++ b/layer/include/depthxr/upstream_turbo.h @@ -0,0 +1,81 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace depthxr { + +// OpenXR Toolkit's Turbo above VectorXR (its layer.cpp xrEndFrame): xrWaitFrame +// on a pool thread as soon as the app's xrEndFrame returns, xrBeginFrame right +// before the next xrEndFrame. Games that wait on a thread of their own begin +// before they render (DCS, MSFS), so all three together are the layer's. +// +// Neither the OS nor the Toolkit promises the short intervals: a pool thread +// can start late, and a begin can be preempted before its end. A frame that +// misses one is not evidence against the pattern, so the frames are judged +// over a window: enough of the last kWindow frames in the pattern is a layer +// pipelining them. With the Toolkit known to be above (its manifest's place in +// the chain) seven in ten of them are; with no such layer known, nearly all. +// Once seen it stays seen until Reset (the session's end). +// +// The times come from the caller (steady_clock in the layer, a script in its +// tests); the caller serializes the calls. +class UpstreamTurboDetector { + public: + using Clock = std::chrono::steady_clock; + static constexpr std::size_t kWindow = 60; + static constexpr std::size_t kNeededWithToolkitAbove = 42; + static constexpr std::size_t kNeededOtherwise = 54; + static constexpr auto kWaitAfterEnd = std::chrono::milliseconds(4); + static constexpr auto kBeginBeforeEnd = std::chrono::milliseconds(1); + + void Reset() { *this = {}; } + void Wait(std::thread::id thread, Clock::time_point at) { + wait_thread_ = thread; + wait_at_ = at; + } + void Begin(Clock::time_point at) { begin_at_ = at; } + void EndReturned(Clock::time_point at) { end_returned_ = at; } + + // At the app's xrEndFrame on `thread`: the frame is judged, and the + // answer is whether a layer above pipelines the frames. + bool End(std::thread::id thread, Clock::time_point at, bool toolkit_above) { + const bool pattern = wait_thread_ != std::thread::id{} && wait_thread_ != thread && + end_returned_ != Clock::time_point{} && wait_at_ >= end_returned_ && + wait_at_ - end_returned_ < kWaitAfterEnd && begin_at_ > wait_at_ && at >= begin_at_ && + at - begin_at_ < kBeginBeforeEnd; + if (frames_ == kWindow) { + matching_ -= window_[next_] ? 1 : 0; + } else { + ++frames_; + } + window_[next_] = pattern; + matching_ += pattern ? 1 : 0; + next_ = (next_ + 1) % kWindow; + if (!seen_ && frames_ == kWindow && + matching_ >= (toolkit_above ? kNeededWithToolkitAbove : kNeededOtherwise)) { + seen_ = true; + } + return seen_; + } + bool Seen() const { return seen_; } + // Of the last frames judged (up to kWindow), how many were in the pattern. + std::size_t Matching() const { return matching_; } + std::size_t Frames() const { return frames_; } + + private: + std::thread::id wait_thread_{}; + Clock::time_point wait_at_{}; + Clock::time_point begin_at_{}; + Clock::time_point end_returned_{}; + std::array window_{}; + std::size_t next_{0}; + std::size_t frames_{0}; + std::size_t matching_{0}; + bool seen_{false}; +}; + +} // namespace depthxr diff --git a/layer/shaders/bridge_depth.hlsl b/layer/shaders/bridge_depth.hlsl new file mode 100644 index 00000000..bee6a921 --- /dev/null +++ b/layer/shaders/bridge_depth.hlsl @@ -0,0 +1,48 @@ +// The Direct3D 11 bridge's depth (framegen_d3d11_bridge.cpp). Direct3D 12 +// shares no depth texture with simultaneous access, and Direct3D 11 opens a +// Direct3D 12 texture only as a render target, so a game's depth reaches the +// runtime's Direct3D 12 depth image in two passes through a shared twin +// (R16_UNORM for D16, R32_FLOAT otherwise): Direct3D 11 copies the game's +// depth into the twin at its release +// (BRIDGE_DEPTH_TO_TWIN), and the bridge's queue writes the twin into the +// runtime's image as depth (BRIDGE_DEPTH_VS and BRIDGE_DEPTH_PS). The depth +// view gives every format's depth as a float (D16 and D24 normalized), and +// writing it as SV_Depth converts it back: D16, D32 and D32S8's depth +// arrives as it was, and D24's may arrive one 24-bit step off (its +// normalized float is not always converted back to the same step). + +#if defined(BRIDGE_DEPTH_TO_TWIN) + +// The game's depth, viewed as its depth plane (R32_FLOAT, +// R32_FLOAT_X8X24_TYPELESS, R24_UNORM_X8_TYPELESS or R16_UNORM). +Texture2DArray g_depth : register(t0); +RWTexture2DArray g_twin : register(u0); + +[numthreads(8, 8, 1)] void CSMain(uint3 id : SV_DispatchThreadID) { + uint width, height, slices; + g_twin.GetDimensions(width, height, slices); + if (id.x < width && id.y < height) { + g_twin[id] = g_depth.Load(int4(id, 0)); + } +} + +#elif defined(BRIDGE_DEPTH_VS) + +// One triangle covering the viewport. +float4 VSMain(uint vertex : SV_VertexID) : SV_Position { + const float2 uv = float2((vertex << 1) & 2, vertex & 2); + return float4(uv * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0); +} + +#elif defined(BRIDGE_DEPTH_PS) + +Texture2DArray g_twin : register(t0); +cbuffer Slice : register(b0) { + uint g_slice; +}; + +float PSMain(float4 position : SV_Position) : SV_Depth { + return g_twin.Load(int4(int2(position.xy), g_slice, 0)); +} + +#endif diff --git a/layer/shaders/framegen.hlsl b/layer/shaders/framegen.hlsl index adf658aa..b0ec1ed0 100644 --- a/layer/shaders/framegen.hlsl +++ b/layer/shaders/framegen.hlsl @@ -95,6 +95,18 @@ cbuffer FrameGenConstantBuffer : register(b0) { }; #endif +#if defined(FG_PASS_SYNTHESIZE_DRAW) || defined(FG_PASS_COMPOSE_DRAW) +// Edge fill (Direct3D 12 draws, FrameGenImage::content): xy is where the +// frame's own pixels start in the render target, which is wider than the +// frame by a margin on each side. Every pixel of the margin repeats the +// frame's nearest edge pixel, so a runtime that turns the frame toward the +// head finds the edge smeared outward there instead of nothing (black). Zero +// for an output the frame's own size. +cbuffer FrameGenFillBuffer : register(b1) { + uint4 g_fill; +}; +#endif + #define FG_FLAG_SRGB_ENCODE 1u #define FG_FLAG_EXTRAPOLATE 2u #define FG_FLAG_TEMPORAL 4u @@ -2410,7 +2422,9 @@ struct DrawOutput { }; DrawOutput PSMain(float4 position : SV_Position) { - const float2 pixel = position.xy; + // Edge fill: a margin pixel is the frame's nearest edge pixel (its + // centre; a coarse pixel's centre inside the frame is left as it is). + const float2 pixel = clamp(position.xy - float2(g_fill.xy), 0.5, float2(g.size.xy) - 0.5); DrawOutput output; output.frame0 = Synthesize(pixel, g.tuning.x); #if FG_DRAW_OUTPUTS > 1 @@ -2539,8 +2553,9 @@ float3 Overlay(float3 composed, float2 uv, bool inside, float alpha) { } float4 PSMain(float4 position : SV_Position) : SV_Target { - // The pixel's centre: the Direct3D 11 compositor's interpolated uv. - const float2 uv = saturate(position.xy / float2(g.size.xy)); + // The pixel's centre: the Direct3D 11 compositor's interpolated uv. With + // edge fill (g_fill) the margin around the eye reads its nearest edge. + const float2 uv = saturate((position.xy - float2(g_fill.xy)) / float2(g.size.xy)); const float4 rect = g.fov_current; float4 composed = g_peripheral.SampleLevel(g_linear, uv, 0.0); const bool inside = all(uv >= rect.xy) && all(uv <= rect.zw); @@ -4346,9 +4361,11 @@ FG_BIND(4) FG_FORMAT("r8ui") RWTexture2D g_rates : register(u0); // angle) is shaded per 2x2 pixels unless it shows cockpit content: a wide // view's periphery is rendered far denser than the headset shows it, while // cockpit edges there are what generated frames must keep sharp. size.xy: -// tiles, size.zw: the output extent, options.x: the tile's side in pixels, +// tiles, size.zw: the frame's extent, options.x: the tile's side in pixels, // options.y: FG_FLAG_DEPTH when the newest frame's depth is bound (without -// it, the angle alone decides). +// it, the angle alone decides), options.zw: with edge fill, where the frame +// starts in the output image (the tiles cover the whole image; a tile of +// the margin is judged as the frame's nearest edge, which it repeats). [numthreads(8, 8, 1)] void CSMain(uint3 id : SV_DispatchThreadID) { if (any(id.xy >= g.size.xy)) { @@ -4356,7 +4373,8 @@ void CSMain(uint3 id : SV_DispatchThreadID) { } const float tile = float(g.options.x); const float2 extent = float2(g.size.zw); - const float2 centre = min((float2(id.xy) + 0.5) * tile, extent) / extent; + const float2 start = float2(id.xy) * tile - float2(g.options.zw); + const float2 centre = clamp(start + 0.5 * tile, 0.0, extent) / extent; const float2 tangent = float2(lerp(g.fov_current.x, g.fov_current.y, centre.x), lerp(g.fov_current.z, g.fov_current.w, centre.y)); uint rate = length(tangent) >= g.tuning.x ? FG_RATE_2X2 : FG_RATE_1X1; @@ -4364,8 +4382,8 @@ void CSMain(uint3 id : SV_DispatchThreadID) { uint width, height; g_depth.GetDimensions(width, height); const float2 scale = float2(width, height) / extent; - const int2 first = int2(floor(float2(id.xy) * tile * scale)); - const int2 last = min(int2(ceil((float2(id.xy) + 1.0) * tile * scale)), int2(width, height)) - 1; + const int2 first = min(int2(floor(max(start, 0.0) * scale)), int2(width, height) - 1); + const int2 last = max(min(int2(ceil((start + tile) * scale)), int2(width, height)) - 1, first); // Four texels a fetch: the 2x2 block from (x, y) (clamped at edges). const float2 texel = 1.0 / float2(width, height); [loop] diff --git a/layer/src/config_parser.cpp b/layer/src/config_parser.cpp index 8be7440a..aa95a8e7 100644 --- a/layer/src/config_parser.cpp +++ b/layer/src/config_parser.cpp @@ -2463,6 +2463,8 @@ bool ParseFrameGenSettings(const JsonValue::Object& object, const char* field, F "artifactSuppression", "motionLimitPercent", "extendHeadPrediction", + "edgeFill", + "useGameDepth", }; NoteUnknownKeys(object, allowed); @@ -2525,6 +2527,20 @@ bool ParseFrameGenSettings(const JsonValue::Object& object, const char* field, F if (extend_head_prediction) { out.extend_head_prediction = *extend_head_prediction; } + std::optional edge_fill; + if (!ReadOptionalBool(object, "edgeFill", edge_fill, error)) { + return false; + } + if (edge_fill) { + out.edge_fill = *edge_fill; + } + std::optional use_game_depth; + if (!ReadOptionalBool(object, "useGameDepth", use_game_depth, error)) { + return false; + } + if (use_game_depth) { + out.use_game_depth = *use_game_depth; + } return ReadOptionalBoundedInt(object, "artifactSuppression", field, 0, 100, out.artifact_suppression, error) && ReadOptionalBoundedInt(object, "motionLimitPercent", field, 2, 25, out.motion_limit_percent, error); } diff --git a/layer/src/framegen/framegen_d3d11_bridge.cpp b/layer/src/framegen/framegen_d3d11_bridge.cpp index f40e22d9..9b10910a 100644 --- a/layer/src/framegen/framegen_d3d11_bridge.cpp +++ b/layer/src/framegen/framegen_d3d11_bridge.cpp @@ -8,6 +8,10 @@ #include "depthxr/framegen_d3d11_bridge.h" +#include "bridge_depth_ps.h" +#include "bridge_depth_to_twin.h" +#include "bridge_depth_vs.h" + #include #include #include @@ -16,6 +20,7 @@ #include #include #include +#include #include #include @@ -81,6 +86,50 @@ DXGI_FORMAT DepthStorageFormat(DXGI_FORMAT format) { } } +// The depth plane of private depth, as the bridge's copy into the twin reads +// it (normalized for D16 and D24). +DXGI_FORMAT DepthReadFormat(DXGI_FORMAT storage) { + switch (storage) { + case DXGI_FORMAT_R32_TYPELESS: + return DXGI_FORMAT_R32_FLOAT; + case DXGI_FORMAT_R32G8X24_TYPELESS: + return DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS; + case DXGI_FORMAT_R24G8_TYPELESS: + return DXGI_FORMAT_R24_UNORM_X8_TYPELESS; + case DXGI_FORMAT_R16_TYPELESS: + return DXGI_FORMAT_R16_UNORM; + default: + return DXGI_FORMAT_UNKNOWN; + } +} + +// A depth twin's format: D16's depth fits R16_UNORM exactly (half the +// memory of iRacing's 6904x3184 depth images), every other R32_FLOAT. +DXGI_FORMAT DepthTwinFormat(DXGI_FORMAT storage) { + return storage == DXGI_FORMAT_R16_TYPELESS ? DXGI_FORMAT_R16_UNORM : DXGI_FORMAT_R32_FLOAT; +} + +// The depth-stencil view format of a runtime's depth image, typed or +// typeless: what the bridge's draw writes it through. +DXGI_FORMAT DepthTargetFormat(DXGI_FORMAT format) { + switch (format) { + case DXGI_FORMAT_D32_FLOAT: + case DXGI_FORMAT_R32_TYPELESS: + return DXGI_FORMAT_D32_FLOAT; + case DXGI_FORMAT_D32_FLOAT_S8X24_UINT: + case DXGI_FORMAT_R32G8X24_TYPELESS: + return DXGI_FORMAT_D32_FLOAT_S8X24_UINT; + case DXGI_FORMAT_D24_UNORM_S8_UINT: + case DXGI_FORMAT_R24G8_TYPELESS: + return DXGI_FORMAT_D24_UNORM_S8_UINT; + case DXGI_FORMAT_D16_UNORM: + case DXGI_FORMAT_R16_TYPELESS: + return DXGI_FORMAT_D16_UNORM; + default: + return DXGI_FORMAT_UNKNOWN; + } +} + // Twins are typeless, as runtimes' Direct3D 11 images usually are: the game // may view an image as any member of its format's family (an sRGB // swapchain's images are often rendered through UNORM views). @@ -196,6 +245,41 @@ std::uint64_t MiB(std::uint64_t bytes) { return (bytes + (1u << 19)) >> 20; } +// Holds the game's immediate context for the bridge's calls on it at a +// release (a depth copy changes its state and puts it back). Multithread +// protection serializes each Direct3D 11 call on its own: without the hold, +// another thread's draw could land between the bridge's calls, under the +// bridge's state, or have its own state overwritten when the game's goes +// back. Entered whether protection is on or not (a thread entering it alone +// does not wait; Direct3D 11 takes the same lock for some calls, Flush among +// them, even without protection, and its debug layer to read whether +// protection is on). It serializes the CPU's calls only, nothing waits for +// the GPU inside it. +// +// Lock order: taken before the bridge's mutex, and, at a hand-over +// (D3D11Bridge::StageReleases), before any lock of the layer's. A game thread +// may hold the device's lock (ID3D11Multithread::Enter) through an OpenXR +// call that takes those: waiting for the context while holding one of them +// would deadlock with it. +class ContextHold { + public: + ContextHold(ID3D11Multithread* multithread, const std::function* before_enter) { + if (multithread) { + if (before_enter && *before_enter) (*before_enter)(); + multithread_ = multithread; + multithread_->Enter(); + } + } + ~ContextHold() { + if (multithread_) multithread_->Leave(); + } + ContextHold(const ContextHold&) = delete; + ContextHold& operator=(const ContextHold&) = delete; + + private: + ID3D11Multithread* multithread_{nullptr}; +}; + // The session's mean and longest handover, "mean/max" in milliseconds. std::string HandoverText(double total_ms, std::uint64_t count, double max_ms) { char buffer[48]; @@ -209,9 +293,11 @@ struct D3D11Bridge::Impl { enum class Mode { kTwin, // rendered into the twin kResolve, // multisampled: rendered privately, resolved into the twin at release - kPrivate, // depth: rendered privately, never reaches the runtime's images + kDepth, // depth: rendered privately, copied into a twin at release and drawn into the runtime's + kPrivate, // depth the bridge cannot mirror: rendered privately, never reaches the runtime's images }; - // A recording of the copy of a twin into the runtime's image. + // A recording of the copy of a twin into the runtime's image (for depth, + // the draw of the twin into it). struct Copy { ComPtr allocator; ComPtr list; @@ -221,24 +307,45 @@ struct D3D11Bridge::Impl { ComPtr twin; ComPtr twin11; // the twin, opened on the game's device ComPtr private11; // rendered into instead of the twin + // Depth: the game's depth as read into the twin, and the twin as written. + ComPtr depth_read11; + ComPtr twin_write11; + D3D12_GPU_DESCRIPTOR_HANDLE twin_read{}; // the twin, read by the draw + std::vector depth_targets; // the runtime's image, by slice // The runtime's image, referenced: a copy still queued when the // runtime lets the image go writes memory that still exists. ComPtr runtime; std::vector copies; UINT64 done{0}; // the done value after its last copy }; + // A release whose Direct3D 11 half ran ahead of its hand-over + // (StageReleases): the image, its "written" value and that half's time. + struct Staged { + std::uint32_t index{0}; + UINT64 written{0}; + double ms{0.0}; + }; struct Swapchain { Mode mode{Mode::kTwin}; DXGI_FORMAT format{DXGI_FORMAT_UNKNOWN}; // as the application asked DXGI_FORMAT storage{DXGI_FORMAT_UNKNOWN}; + DXGI_FORMAT depth_target{DXGI_FORMAT_UNKNOWN}; // depth: the runtime images' depth-stencil view format + std::string private_reason; // depth kept private: why + std::string protection; // mirrored depth: the device's multithread protection (NoteDepthProtection) std::uint32_t width{0}; std::uint32_t height{0}; std::uint32_t array_size{1}; std::uint32_t mip_count{1}; std::uint32_t sample_count{1}; bool composed{false}; + // Depth: the twins' views read by the draws (shader-visible, kept + // while the draws are queued) and the runtime images' depth targets. + ComPtr twin_reads; + ComPtr depth_targets; std::vector images; std::deque acquired; // acquired and not released yet, oldest first + std::uint32_t pending{0}; // the application's releases whose hand-over waits (ReleaseCalled) + std::deque staged; // releases whose Direct3D 11 half ran (StageReleases), oldest first UINT64 last_done{0}; std::uint64_t twin_bytes{0}; std::uint64_t copies{0}; @@ -249,6 +356,7 @@ struct D3D11Bridge::Impl { ComPtr device11; ComPtr context11; + ComPtr multithread; // the immediate context's lock (ContextHold) bool multithread_protected{false}; ComPtr device12; ComPtr adapter; // for video memory figures @@ -258,6 +366,16 @@ struct D3D11Bridge::Impl { ComPtr idle; // the bridge's queue's progress, for retiring ComPtr written11; ComPtr done11; + // Depth reaches the runtime's images unless VECTORXR_D3D11_BRIDGE_DEPTH=private. + bool mirror_depth{true}; + std::string depth_private_reason; // why mirror_depth is off + + // The depth passes, made with the first depth swapchain mirrored (each + // with `pipeline_mutex`; a draw's pipeline only for its target format). + std::mutex pipeline_mutex; + ComPtr depth_to_twin; + ComPtr depth_root; + std::unordered_map> depth_pipelines; // Everything below, and the order of signals on both APIs: each fence's // values are signalled in the order they are handed out. @@ -267,6 +385,10 @@ struct D3D11Bridge::Impl { UINT64 idle_value{0}; DWORD thread{0}; // the first thread a swapchain or frame call came from bool reported_second_thread{false}; + DWORD second_thread{0}; // the first other thread NoteThread saw + // A second thread was seen and protection is off: ApplyWantedProtection + // turns it on (read without `mutex`). + std::atomic protection_wanted{false}; bool debug_layer{false}; // VECTORXR_D3D11_BRIDGE_DEBUG=1 std::uint32_t debug_reported{0}; // debug-layer messages reported std::uint64_t next_skip_report{1}; // skipped copies are reported at 1, 10, 100... @@ -279,7 +401,15 @@ struct D3D11Bridge::Impl { std::atomic recordings{0}; // also counted outside the mutex, as swapchains are made std::uint64_t done_waits{0}; std::uint64_t unmatched_releases{0}; - // The game thread's time in a release's handover (Direct3D 11's flush, a + std::uint64_t depth_copies{0}; // releases of depth copied into a twin + std::uint64_t depth_target_unbinds{0}; // of them, with the image still the game's depth target + // The game thread's time issuing those copies (CopyDepthToTwin, its + // Direct3D 11 calls only; the GPU's work is not timed), milliseconds: the + // session's total and longest. + double depth_copy_ms_total{0.0}; + double depth_copy_ms_max{0.0}; + // The game thread's time in a release's handover (a depth image's copy + // into its twin or a multisampled image's resolve, Direct3D 11's flush, a // recording of the copy, the bridge queue's submission), milliseconds: // the session's total and longest, and how many took kSlowHandoverMs. double handover_ms_total{0.0}; @@ -287,10 +417,16 @@ struct D3D11Bridge::Impl { std::uint64_t handovers{0}; std::uint64_t slow_handovers{0}; std::function log; - - // Counts a handover that started at `start` (holding `mutex`). - void NoteHandover(std::chrono::steady_clock::time_point start) { - const double ms = std::chrono::duration(std::chrono::steady_clock::now() - start).count(); + std::function depth_copy_hook; // SetDepthCopyHookForTest + // SetContextWaitHookForTest: read without `mutex` (set while nothing runs). + std::function context_wait_hook; + bool depth_protection_reported{false}; // the device has none to turn on: said once + + // Counts a handover that started at `start`, `ahead_ms` of it having run + // before (its Direct3D 11 half, StageReleases), holding `mutex`. + void NoteHandover(std::chrono::steady_clock::time_point start, double ahead_ms = 0.0) { + const double ms = + ahead_ms + std::chrono::duration(std::chrono::steady_clock::now() - start).count(); handover_ms_total += ms; handover_ms_max = std::max(handover_ms_max, ms); ++handovers; @@ -303,7 +439,11 @@ struct D3D11Bridge::Impl { // A swapchain or frame call's thread (holding `mutex`): Direct3D 11's // immediate context is the game's, and is only safe from another thread // when the device is multithread protected. Calls from more than one - // thread turn that protection on. + // thread want that protection on: ApplyWantedProtection turns it on, + // never here. SetMultithreadProtected waits for the device's lock, which + // a thread holding the context (ContextHold, or a game thread of its + // own) may hold while it waits for `mutex`, or for the layer's locks + // these calls are made under. void NoteThread() { const DWORD current = GetCurrentThreadId(); if (!thread) { @@ -314,22 +454,66 @@ struct D3D11Bridge::Impl { return; } reported_second_thread = true; - ComPtr multithread; - if (SUCCEEDED(context11.As(&multithread))) { + second_thread = current; + protection_wanted = true; + } + + // Turns protection on once NoteThread wanted it, holding neither `mutex` + // nor any lock of the layer's (or holding the context already, which + // makes the device's lock this thread's): at the start of a hand-over + // (StageReleases, called before any of the layer's locks), before the + // bridge's Direct3D 11 calls there. Until then a second thread's calls + // are not serialized, as the calls before it never were. + void ApplyWantedProtection() { + if (!protection_wanted.exchange(false)) { + return; + } + bool on = false; + if (multithread) { multithread->SetMultithreadProtected(TRUE); - multithread_protected = multithread->GetMultithreadProtected() != FALSE; + on = multithread->GetMultithreadProtected() != FALSE; } + std::scoped_lock lock(mutex); + multithread_protected = multithread_protected || on; if (log) { log("Direct3D 11 bridge: the game's swapchain and frame calls come from more than one thread (" + - std::to_string(current) + ", first " + std::to_string(thread) + "); " + - (multithread_protected - ? "turned the game device's multithread protection on, so each of the bridge's Direct3D 11 " - "calls is serialized with the game's (calls made before this one were not)." - : "its device has no multithread protection to turn on: the bridge's Direct3D 11 calls there " - "may race the game's rendering.")); + std::to_string(second_thread) + ", first " + std::to_string(thread) + "); " + + (on ? "turned the game device's multithread protection on, so each of the bridge's Direct3D 11 " + "calls is serialized with the game's (calls made before this one were not)." + : "its device has no multithread protection to turn on: the bridge's Direct3D 11 calls there " + "may race the game's rendering.")); } } + // Holds the game's immediate context (ContextHold): before `mutex`. + ContextHold HoldContext() { + return ContextHold(multithread.Get(), &context_wait_hook); + } + + // When a depth swapchain is mirrored, before its first copy (holding + // `mutex`, AddSwapchain having turned protection on, and read it as + // `now_protected`, before: Direct3D 11's debug layer reads it under the + // device's lock, which a thread holding the context may hold while it + // waits for `mutex`): the copy + // changes the immediate context's state and puts it back over many + // calls, which only multithread protection keeps the game's other + // threads out of (ContextHold), and a game rendering on a thread that + // never calls OpenXR would otherwise never get it (NoteThread sees + // OpenXR's threads only). Turned on once, never off; a game's own is + // left as it is. What the chain's description says of it. + std::string NoteDepthProtection(bool was_protected, bool now_protected) { + multithread_protected = now_protected; + if (multithread_protected) { + return was_protected ? "on" : "turned on"; + } + if (log && !depth_protection_reported) { + depth_protection_reported = true; + log("Direct3D 11 bridge: the game's device has no multithread protection to turn on: its depth copies " + "may race a thread of the game's rendering at the same time."); + } + return "unavailable"; + } + // The Direct3D 12 debug layer's errors since the last call (holding `mutex`). void ReportDebugMessages() { ComPtr queue12; @@ -361,6 +545,7 @@ struct D3D11Bridge::Impl { // For the process: work still queued may use them. static void DetachGpu(Swapchain& chain) { + chain.twin_reads.Detach(); for (Image& image : chain.images) { image.twin.Detach(); image.runtime.Detach(); @@ -370,32 +555,55 @@ struct D3D11Bridge::Impl { } } } + // ...and the depth draws' pipelines. + void DetachPipelines() { + depth_root.Detach(); + for (auto& [format, pipeline] : depth_pipelines) { + pipeline.Detach(); + } + } std::string VideoMemory() const { DXGI_QUERY_VIDEO_MEMORY_INFO memory{}; if (!adapter || FAILED(adapter->QueryVideoMemoryInfo(0, DXGI_MEMORY_SEGMENT_GROUP_LOCAL, &memory))) { return "unavailable"; } - return std::to_string(MiB(memory.CurrentUsage)) + "/" + std::to_string(MiB(memory.Budget)) + " MiB"; + std::string text = std::to_string(MiB(memory.CurrentUsage)) + "/" + std::to_string(MiB(memory.Budget)) + " MiB"; + // The process's system memory on the adapter: it grows when Windows + // moves video memory out over the budget (the GPU then waits on it). + DXGI_QUERY_VIDEO_MEMORY_INFO shared{}; + if (SUCCEEDED(adapter->QueryVideoMemoryInfo(0, DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL, &shared))) { + text += ", sharedMemory=" + std::to_string(MiB(shared.CurrentUsage)) + " MiB"; + } + return text; } std::string Describe(const Swapchain& chain) const { std::ostringstream stream; stream << "mode=" - << (chain.mode == Mode::kTwin ? "twin" : chain.mode == Mode::kResolve ? "resolve" : "private-depth") - << ", images=" << chain.images.size() << ", format=" << static_cast(chain.format) + << (chain.mode == Mode::kTwin ? "twin" + : chain.mode == Mode::kResolve ? "resolve" + : chain.mode == Mode::kDepth ? "depth" + : "private-depth"); + if (!chain.private_reason.empty()) { + stream << " (" << chain.private_reason << ")"; + } + stream << ", images=" << chain.images.size() << ", format=" << static_cast(chain.format) << ", storage=" << static_cast(chain.storage) << ", size=" << chain.width << "x" << chain.height << ", array=" << chain.array_size << ", mips=" << chain.mip_count << ", samples=" << chain.sample_count << ", twinMiB=" << MiB(chain.twin_bytes) << ", composed=" << (chain.composed ? 1 : 0) << ", copies=" << chain.copies << ", doneWaits=" << chain.done_waits << ", skippedCopies=" << chain.skipped_copies << ", unmatchedReleases=" << chain.unmatched_releases; + if (!chain.protection.empty()) { + stream << ", multithreadProtection=" << chain.protection; + } return stream.str(); } // Records the twin's copy into the runtime's image. The runtime's colour // images are render targets whenever the game holds them; the twin, with // simultaneous access, needs no transition of its own. - bool RecordCopy(Image& image, Copy& copy, std::string& reason) { + bool RecordCopy(const Swapchain& chain, Image& image, Copy& copy, std::string& reason) { HRESULT hr = device12->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(©.allocator)); if (SUCCEEDED(hr)) { hr = device12->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, copy.allocator.Get(), nullptr, @@ -405,6 +613,9 @@ struct D3D11Bridge::Impl { reason = Failure("Creating the bridge's copy commands", hr); return false; } + if (chain.mode == Mode::kDepth) { + return RecordDepthDraw(chain, image, copy, reason); + } D3D12_RESOURCE_BARRIER barrier{}; barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; barrier.Transition.pResource = image.runtime.Get(); @@ -427,7 +638,7 @@ struct D3D11Bridge::Impl { // A recording of the image's copy whose last execution is done (holding // `mutex`): one more is made while fewer than kCopyRecordings exist, and // otherwise the oldest is waited for, a while. nullptr: none in time. - Copy* ReadyCopy(Image& image) { + Copy* ReadyCopy(const Swapchain& chain, Image& image) { const UINT64 completed = done->GetCompletedValue(); for (Copy& copy : image.copies) { if (copy.done <= completed) { @@ -437,7 +648,7 @@ struct D3D11Bridge::Impl { if (image.copies.size() < kCopyRecordings) { std::string reason; image.copies.emplace_back(); - if (RecordCopy(image, image.copies.back(), reason)) { + if (RecordCopy(chain, image, image.copies.back(), reason)) { return &image.copies.back(); } image.copies.pop_back(); @@ -515,7 +726,461 @@ struct D3D11Bridge::Impl { return false; } image.copies.emplace_back(); - return RecordCopy(image, image.copies.back(), reason); + return RecordCopy(chain, image, image.copies.back(), reason); + } + + // Records the draw of a depth twin into the runtime's depth image, slice + // by slice (mip 0). The runtime's depth images are depth targets whenever + // the game holds them (XR_KHR_D3D12_enable), so the draw needs no + // transition; the twin, with simultaneous access, neither. + bool RecordDepthDraw(const Swapchain& chain, Image& image, Copy& copy, std::string& reason) { + ID3D12PipelineState* pipeline = nullptr; + { + std::scoped_lock lock(pipeline_mutex); + const auto it = depth_pipelines.find(chain.depth_target); + pipeline = it == depth_pipelines.end() ? nullptr : it->second.Get(); + } + if (!pipeline || !depth_root) { + reason = "the bridge's depth draw has no pipeline"; + return false; + } + ID3D12DescriptorHeap* heaps[] = {chain.twin_reads.Get()}; + copy.list->SetDescriptorHeaps(1, heaps); + copy.list->SetGraphicsRootSignature(depth_root.Get()); + copy.list->SetPipelineState(pipeline); + copy.list->SetGraphicsRootDescriptorTable(1, image.twin_read); + const D3D12_VIEWPORT viewport{0.0f, 0.0f, static_cast(chain.width), static_cast(chain.height), + 0.0f, 1.0f}; + const D3D12_RECT scissor{0, 0, static_cast(chain.width), static_cast(chain.height)}; + copy.list->RSSetViewports(1, &viewport); + copy.list->RSSetScissorRects(1, &scissor); + copy.list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + for (std::uint32_t slice = 0; slice < chain.array_size; ++slice) { + copy.list->SetGraphicsRoot32BitConstant(0, slice, 0); + copy.list->OMSetRenderTargets(0, nullptr, FALSE, &image.depth_targets[slice]); + copy.list->DrawInstanced(3, 1, 0, 0); + } + const HRESULT hr = copy.list->Close(); + if (FAILED(hr)) { + reason = Failure("Recording the bridge's depth draw", hr); + return false; + } + ++recordings; + return true; + } + + // The depth passes: the Direct3D 11 copy into twins (once), the draw's + // root signature (once) and its pipeline for `target`. + bool MakeDepthPasses(DXGI_FORMAT target, std::string& reason) { + std::scoped_lock lock(pipeline_mutex); + if (!depth_to_twin) { + const HRESULT hr = device11->CreateComputeShader(kBridgeDepthToTwinDxbc, sizeof(kBridgeDepthToTwinDxbc), + nullptr, &depth_to_twin); + if (FAILED(hr)) { + reason = Failure("Creating the Direct3D 11 depth copy (the game's device needs feature level 11)", hr); + return false; + } + } + if (!depth_root) { + D3D12_DESCRIPTOR_RANGE range{}; + range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV; + range.NumDescriptors = 1; + D3D12_ROOT_PARAMETER parameters[2]{}; + parameters[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; + parameters[0].Constants.Num32BitValues = 1; + parameters[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL; + parameters[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; + parameters[1].DescriptorTable.NumDescriptorRanges = 1; + parameters[1].DescriptorTable.pDescriptorRanges = ⦥ + parameters[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL; + D3D12_ROOT_SIGNATURE_DESC desc{}; + desc.NumParameters = 2; + desc.pParameters = parameters; + desc.Flags = D3D12_ROOT_SIGNATURE_FLAG_DENY_HULL_SHADER_ROOT_ACCESS | + D3D12_ROOT_SIGNATURE_FLAG_DENY_DOMAIN_SHADER_ROOT_ACCESS | + D3D12_ROOT_SIGNATURE_FLAG_DENY_GEOMETRY_SHADER_ROOT_ACCESS; + ComPtr blob; + ComPtr error; + HRESULT hr = D3D12SerializeRootSignature(&desc, D3D_ROOT_SIGNATURE_VERSION_1, &blob, &error); + if (SUCCEEDED(hr)) { + hr = device12->CreateRootSignature(0, blob->GetBufferPointer(), blob->GetBufferSize(), + IID_PPV_ARGS(&depth_root)); + } + if (FAILED(hr)) { + reason = Failure("Creating the bridge's depth root signature", hr); + return false; + } + } + if (depth_pipelines.count(target)) { + return true; + } + D3D12_GRAPHICS_PIPELINE_STATE_DESC desc{}; + desc.pRootSignature = depth_root.Get(); + desc.VS = {kBridgeDepthVsDxbc, sizeof(kBridgeDepthVsDxbc)}; + desc.PS = {kBridgeDepthPsDxbc, sizeof(kBridgeDepthPsDxbc)}; + desc.BlendState.RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL; + desc.SampleMask = UINT_MAX; + desc.RasterizerState.FillMode = D3D12_FILL_MODE_SOLID; + desc.RasterizerState.CullMode = D3D12_CULL_MODE_NONE; + desc.RasterizerState.DepthClipEnable = TRUE; + // Every texel written, whatever the image held. + desc.DepthStencilState.DepthEnable = TRUE; + desc.DepthStencilState.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL; + desc.DepthStencilState.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS; + desc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE; + desc.NumRenderTargets = 0; + desc.DSVFormat = target; + desc.SampleDesc.Count = 1; + ComPtr pipeline; + const HRESULT hr = device12->CreateGraphicsPipelineState(&desc, IID_PPV_ARGS(&pipeline)); + if (FAILED(hr)) { + reason = Failure("Creating the bridge's depth draw", hr) + " for format " + + std::to_string(static_cast(target)); + return false; + } + pipeline->SetName(L"VectorXR Direct3D 11 bridge depth"); + depth_pipelines[target] = pipeline; + return true; + } + + // Whether the runtime's images can take the game's depth, and how: depth + // targets of the depth format's family, one sample, the chain's size. + bool CheckRuntimeDepth(Swapchain& chain, std::string& reason) { + for (const Image& image : chain.images) { + if (!image.runtime) { + reason = "the runtime's image is null"; + return false; + } + const D3D12_RESOURCE_DESC runtime = image.runtime->GetDesc(); + const DXGI_FORMAT target = DepthTargetFormat(runtime.Format); + if (runtime.Width != chain.width || runtime.Height != chain.height || + runtime.DepthOrArraySize != chain.array_size || runtime.SampleDesc.Count != 1 || + target == DXGI_FORMAT_UNKNOWN || DepthStorageFormat(target) != chain.storage) { + std::ostringstream stream; + stream << "the runtime's depth image (" << runtime.Width << "x" << runtime.Height << ", array " + << runtime.DepthOrArraySize << ", samples " << runtime.SampleDesc.Count << ", format " + << static_cast(runtime.Format) << ") does not match the game's (" << chain.width << "x" + << chain.height << ", array " << chain.array_size << ", format " + << static_cast(chain.format) << ")"; + reason = stream.str(); + return false; + } + if ((runtime.Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL) == 0) { + reason = "the runtime's depth image is not a depth target"; + return false; + } + chain.depth_target = target; + } + return true; + } + + // A depth image's twin (DepthTwinFormat, shared, opened on the game's + // device), the views each pass uses and the first recording of its draw. + // The chain's descriptor heaps hold `index`'s views. + bool MakeDepthTwin(Swapchain& chain, Image& image, std::uint32_t index, std::string& reason) { + const DXGI_FORMAT twin_format = DepthTwinFormat(chain.storage); + D3D12_HEAP_PROPERTIES heap{}; + heap.Type = D3D12_HEAP_TYPE_DEFAULT; + D3D12_RESOURCE_DESC desc{}; + desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + desc.Width = chain.width; + desc.Height = chain.height; + desc.DepthOrArraySize = static_cast(chain.array_size); + desc.MipLevels = 1; + desc.Format = twin_format; + desc.SampleDesc.Count = 1; + desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; + // Direct3D 11 opens it only as a render target, and writes it as an + // unordered-access view, which needs simultaneous access. + desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS | + D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; + HRESULT hr = device12->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_SHARED, &desc, + D3D12_RESOURCE_STATE_COMMON, nullptr, + IID_PPV_ARGS(&image.twin)); + if (FAILED(hr)) { + reason = Failure("Creating a shared Direct3D 12 depth twin", hr); + return false; + } + image.twin->SetName(L"VectorXR Direct3D 11 bridge depth twin"); + chain.twin_bytes += device12->GetResourceAllocationInfo(0, 1, &desc).SizeInBytes; + HANDLE handle = nullptr; + hr = device12->CreateSharedHandle(image.twin.Get(), nullptr, GENERIC_ALL, nullptr, &handle); + if (FAILED(hr)) { + reason = Failure("Sharing a Direct3D 12 depth twin", hr); + return false; + } + hr = device11->OpenSharedResource1(handle, IID_PPV_ARGS(&image.twin11)); + CloseHandle(handle); + if (FAILED(hr)) { + reason = Failure("Opening a shared depth twin on the game's Direct3D 11 device", hr); + return false; + } + + D3D11_SHADER_RESOURCE_VIEW_DESC read{}; + read.Format = DepthReadFormat(chain.storage); + read.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY; + read.Texture2DArray.MipLevels = 1; + read.Texture2DArray.ArraySize = chain.array_size; + hr = device11->CreateShaderResourceView(image.private11.Get(), &read, &image.depth_read11); + if (SUCCEEDED(hr)) { + D3D11_UNORDERED_ACCESS_VIEW_DESC write{}; + write.Format = twin_format; + write.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2DARRAY; + write.Texture2DArray.ArraySize = chain.array_size; + hr = device11->CreateUnorderedAccessView(image.twin11.Get(), &write, &image.twin_write11); + } + if (FAILED(hr)) { + reason = Failure("Making the Direct3D 11 views of the depth copy", hr); + return false; + } + + const UINT read_size = device12->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); + D3D12_SHADER_RESOURCE_VIEW_DESC twin_read{}; + twin_read.Format = twin_format; + twin_read.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2DARRAY; + twin_read.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; + twin_read.Texture2DArray.MipLevels = 1; + twin_read.Texture2DArray.ArraySize = chain.array_size; + D3D12_CPU_DESCRIPTOR_HANDLE read_cpu = chain.twin_reads->GetCPUDescriptorHandleForHeapStart(); + read_cpu.ptr += static_cast(index) * read_size; + device12->CreateShaderResourceView(image.twin.Get(), &twin_read, read_cpu); + image.twin_read = chain.twin_reads->GetGPUDescriptorHandleForHeapStart(); + image.twin_read.ptr += static_cast(index) * read_size; + + const UINT target_size = device12->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_DSV); + for (std::uint32_t slice = 0; slice < chain.array_size; ++slice) { + D3D12_DEPTH_STENCIL_VIEW_DESC target{}; + target.Format = chain.depth_target; + target.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2DARRAY; + target.Texture2DArray.FirstArraySlice = slice; + target.Texture2DArray.ArraySize = 1; + D3D12_CPU_DESCRIPTOR_HANDLE handle12 = chain.depth_targets->GetCPUDescriptorHandleForHeapStart(); + handle12.ptr += static_cast(index * chain.array_size + slice) * target_size; + device12->CreateDepthStencilView(image.runtime.Get(), &target, handle12); + image.depth_targets.push_back(handle12); + } + image.copies.emplace_back(); + return RecordCopy(chain, image, image.copies.back(), reason); + } + + // The twins and views of a depth chain whose images are made privately: + // false (with `reason`) when the runtime's images or the devices cannot + // take them, the chain then stays private. + bool MirrorDepth(Swapchain& chain, std::string& reason) { + if (!CheckRuntimeDepth(chain, reason) || !MakeDepthPasses(chain.depth_target, reason)) { + return false; + } + const auto images = static_cast(chain.images.size()); + D3D12_DESCRIPTOR_HEAP_DESC reads{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, images, + D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 0}; + D3D12_DESCRIPTOR_HEAP_DESC targets{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, images * chain.array_size, + D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0}; + HRESULT hr = device12->CreateDescriptorHeap(&reads, IID_PPV_ARGS(&chain.twin_reads)); + if (SUCCEEDED(hr)) { + hr = device12->CreateDescriptorHeap(&targets, IID_PPV_ARGS(&chain.depth_targets)); + } + if (FAILED(hr)) { + reason = Failure("Making the bridge's depth descriptors", hr); + return false; + } + for (std::uint32_t i = 0; i < images; ++i) { + if (!MakeDepthTwin(chain, chain.images[i], i, reason)) { + reason = "image " + std::to_string(i) + ": " + reason; + return false; + } + } + return true; + } + + // Lets a depth chain's twins go, nothing having used them (MirrorDepth failed). + static void KeepDepthPrivate(Swapchain& chain, const std::string& reason) { + for (Image& image : chain.images) { + image.twin.Reset(); + image.twin11.Reset(); + image.depth_read11.Reset(); + image.twin_write11.Reset(); + image.depth_targets.clear(); + image.copies.clear(); + } + chain.twin_reads.Reset(); + chain.depth_targets.Reset(); + chain.twin_bytes = 0; + chain.mode = Mode::kPrivate; + chain.private_reason = reason; + } + + // Copies the game's depth into the image's twin on the game's immediate + // context (holding the context, ContextHold, then `mutex`), leaving the + // context's state as it was. A game still holding the image as its depth + // target gets the target back after the copy: Direct3D 11 reads no + // texture bound for writing (it would unbind the copy's read instead). + // The caller's hold makes the whole sequence one: no other thread's call + // lands inside it. + void CopyDepthToTwin(const Swapchain& chain, Image& image) { + const auto start = std::chrono::steady_clock::now(); + CopyDepthToTwinHeld(chain, image, context11.Get()); + const double ms = std::chrono::duration(std::chrono::steady_clock::now() - start).count(); + depth_copy_ms_total += ms; + depth_copy_ms_max = std::max(depth_copy_ms_max, ms); + ++depth_copies; + } + + // CopyDepthToTwin's calls, holding the context. + void CopyDepthToTwinHeld(const Swapchain& chain, Image& image, ID3D11DeviceContext4* context) { + // The output merger: only its depth target changes, and only while it + // is the image. Its render targets are set again up to the last one + // bound, with its unordered-access views kept: a view in a slot the + // targets set would be unbound (the keep flag keeps only the slots + // past them), and Direct3D 11 keeps none in a slot below a bound + // target, so every one stays where it was, its hidden counter with it + // (which a view set again could not be given back). OMSetRenderTargets + // would unbind them all. + ID3D11RenderTargetView* targets[D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT]{}; + ID3D11DepthStencilView* depth_target_raw = nullptr; + context->OMGetRenderTargets(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT, targets, &depth_target_raw); + ComPtr held_targets[D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT]; + UINT target_count = 0; + for (UINT i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; ++i) { + held_targets[i].Attach(targets[i]); + if (targets[i]) target_count = i + 1; + } + ComPtr depth_target; + depth_target.Attach(depth_target_raw); + bool unbound = false; + if (depth_target) { + ComPtr bound; + depth_target->GetResource(&bound); + if (bound.Get() == image.private11.Get()) { + context->OMSetRenderTargetsAndUnorderedAccessViews(target_count, target_count ? targets : nullptr, + nullptr, 0, D3D11_KEEP_UNORDERED_ACCESS_VIEWS, + nullptr, nullptr); + unbound = true; + ++depth_target_unbinds; + } + } + + ID3D11ComputeShader* shader_raw = nullptr; + ID3D11ClassInstance* instances[256]{}; + UINT instance_count = 256; + context->CSGetShader(&shader_raw, instances, &instance_count); + ComPtr shader; + shader.Attach(shader_raw); + ComPtr held_instances[256]; + for (UINT i = 0; i < 256; ++i) { + held_instances[i].Attach(instances[i]); + } + ComPtr read; + context->CSGetShaderResources(0, 1, &read); + ComPtr write; + context->CSGetUnorderedAccessViews(0, 1, &write); + + ID3D11ShaderResourceView* depth_read = image.depth_read11.Get(); + ID3D11UnorderedAccessView* twin_write = image.twin_write11.Get(); + context->CSSetShader(depth_to_twin.Get(), nullptr, 0); + context->CSSetShaderResources(0, 1, &depth_read); + context->CSSetUnorderedAccessViews(0, 1, &twin_write, nullptr); + if (depth_copy_hook) { + depth_copy_hook(); + } + context->Dispatch((chain.width + 7) / 8, (chain.height + 7) / 8, chain.array_size); + + // Unbound before the game's views go back, whichever slot they held. + ID3D11UnorderedAccessView* no_write = nullptr; + ID3D11ShaderResourceView* no_read = nullptr; + context->CSSetUnorderedAccessViews(0, 1, &no_write, nullptr); + context->CSSetShaderResources(0, 1, &no_read); + // -1: the game's view keeps its hidden counter. + const UINT keep_counter = static_cast(-1); + context->CSSetUnorderedAccessViews(0, 1, write.GetAddressOf(), &keep_counter); + context->CSSetShaderResources(0, 1, read.GetAddressOf()); + context->CSSetShader(shader.Get(), instance_count ? instances : nullptr, instance_count); + if (unbound) { + context->OMSetRenderTargetsAndUnorderedAccessViews(target_count, target_count ? targets : nullptr, + depth_target.Get(), 0, D3D11_KEEP_UNORDERED_ACCESS_VIEWS, + nullptr, nullptr); + } + } + + // Whether the release of `chain`'s oldest acquired image needs the + // game's immediate context (holding `mutex`): it is copied, and its + // Direct3D 11 half has not run yet. + static bool NeedsContext(const Swapchain& chain) { + return chain.staged.empty() && !chain.acquired.empty() && chain.mode != Mode::kPrivate && + chain.acquired.front() < chain.images.size(); + } + + // The Direct3D 11 half of the release of `chain`'s oldest acquired image + // (holding the context, then `mutex`; NeedsContext): a depth image's copy + // into its twin, a multisampled image's resolve, and the "written" + // signal the bridge's queue waits for, flushed. + Staged Stage(Swapchain& chain) { + const auto start = std::chrono::steady_clock::now(); + Staged staged; + staged.index = chain.acquired.front(); + chain.acquired.pop_front(); + NoteThread(); + Image& image = chain.images[staged.index]; + if (chain.mode == Mode::kDepth) { + // The twin is written again only after the bridge's queue is done + // drawing its last release (a wait on the GPU, ending as the + // colour's at acquire does); then the game's depth goes into it. + const UINT64 value = image.done; + if (value && done->GetCompletedValue() < value) { + context11->Wait(done11.Get(), value); + ++chain.done_waits; + ++done_waits; + } + CopyDepthToTwin(chain, image); + } + if (chain.mode == Mode::kResolve) { + const DXGI_FORMAT format = ResolveFormat(chain.format); + for (UINT slice = 0; slice < chain.array_size; ++slice) { + const UINT subresource = D3D11CalcSubresource(0, slice, 1); + context11->ResolveSubresource(image.twin11.Get(), subresource, image.private11.Get(), subresource, + format); + } + } + staged.written = ++written_value; + context11->Signal(written11.Get(), staged.written); + context11->Flush(); + staged.ms = std::chrono::duration(std::chrono::steady_clock::now() - start).count(); + return staged; + } + + // The Direct3D 12 half (holding `mutex`; nothing of the game's context): + // the bridge's queue copies the twin into the runtime's image once + // Direct3D 11's "written" is signalled, ahead of anything else queued on + // it for the release (Frame Generation's capture). False when the copy + // was skipped (the bridge's queue is stalled). + bool Submit(Swapchain& chain, const Staged& staged) { + const auto start = std::chrono::steady_clock::now(); + Image& image = chain.images[staged.index]; + Copy* copy = ReadyCopy(chain, image); + if (!copy) { + NoteHandover(start, staged.ms); + // Every recording of the copy is still queued: the bridge's queue is + // stalled. The runtime's image keeps what it had. + ++chain.skipped_copies; + ++skipped_copies; + if (skipped_copies == next_skip_report && log) { + next_skip_report *= 10; + log("Direct3D 11 bridge: its queue is stalled; releases not copied into the runtime's images so " + "far: " + std::to_string(skipped_copies) + "."); + } + return false; + } + queue->Wait(written.Get(), staged.written); + ID3D12CommandList* lists[] = {copy->list.Get()}; + queue->ExecuteCommandLists(1, lists); + const UINT64 done_at = ++done_value; + queue->Signal(done.Get(), done_at); + copy->done = done_at; + image.done = done_at; + chain.last_done = done_at; + ++chain.copies; + ++copies; + NoteHandover(start, staged.ms); + return true; } bool MakePrivate(const Swapchain& chain, Image& image, const XrSwapchainCreateInfo& info, std::string& reason) { @@ -527,17 +1192,19 @@ struct D3D11Bridge::Impl { desc.Format = chain.storage; desc.SampleDesc.Count = chain.sample_count; desc.Usage = D3D11_USAGE_DEFAULT; - if (chain.mode == Mode::kPrivate) { + const bool depth = chain.mode == Mode::kDepth || chain.mode == Mode::kPrivate; + if (depth) { desc.BindFlags = D3D11_BIND_DEPTH_STENCIL; - if (info.usageFlags & XR_SWAPCHAIN_USAGE_SAMPLED_BIT) desc.BindFlags |= D3D11_BIND_SHADER_RESOURCE; + // Mirrored depth is read by the copy into its twin. + if ((info.usageFlags & XR_SWAPCHAIN_USAGE_SAMPLED_BIT) || chain.mode == Mode::kDepth) { + desc.BindFlags |= D3D11_BIND_SHADER_RESOURCE; + } } else { desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE; } const HRESULT hr = device11->CreateTexture2D(&desc, nullptr, &image.private11); if (FAILED(hr)) { - reason = Failure(chain.mode == Mode::kPrivate ? "Creating a private depth texture" - : "Creating a private multisampled texture", - hr); + reason = Failure(depth ? "Creating a private depth texture" : "Creating a private multisampled texture", hr); return false; } return true; @@ -560,9 +1227,12 @@ D3D11Bridge::~D3D11Bridge() { if (impl_) { const auto detach = [](Impl::Swapchain& chain) { Impl::DetachGpu(chain); + chain.depth_targets.Detach(); for (Impl::Image& image : chain.images) { image.twin11.Detach(); image.private11.Detach(); + image.depth_read11.Detach(); + image.twin_write11.Detach(); } }; for (auto& [swapchain, chain] : impl_->swapchains) { @@ -571,6 +1241,8 @@ D3D11Bridge::~D3D11Bridge() { for (Impl::Swapchain& chain : impl_->retired) { detach(chain); } + impl_->DetachPipelines(); + impl_->depth_to_twin.Detach(); impl_->queue.Detach(); impl_->written.Detach(); impl_->done.Detach(); @@ -579,6 +1251,7 @@ D3D11Bridge::~D3D11Bridge() { impl_->done11.Detach(); impl_->adapter.Detach(); impl_->device12.Detach(); + impl_->multithread.Detach(); impl_->context11.Detach(); impl_->device11.Detach(); } @@ -592,6 +1265,10 @@ void D3D11Bridge::SetLog(std::function log) { } } +void D3D11Bridge::SetKeepDepthPrivate(bool keep) { + keep_depth_private_ = keep; +} + const XrSessionCreateInfo* D3D11Bridge::Begin(XrInstance instance, const XrSessionCreateInfo* info, PFN_xrGetInstanceProcAddr get_instance_proc_addr, std::string& reason, std::string& description) { @@ -625,9 +1302,8 @@ const XrSessionCreateInfo* D3D11Bridge::Begin(XrInstance instance, const XrSessi reason = "the game's immediate context has no ID3D11DeviceContext4"; return nullptr; } - ComPtr multithread; - if (SUCCEEDED(immediate.As(&multithread))) { - impl->multithread_protected = multithread->GetMultithreadProtected() != FALSE; + if (SUCCEEDED(immediate.As(&impl->multithread))) { + impl->multithread_protected = impl->multithread->GetMultithreadProtected() != FALSE; } ComPtr dxgi_device; @@ -770,6 +1446,16 @@ const XrSessionCreateInfo* D3D11Bridge::Begin(XrInstance instance, const XrSessi } } + // VECTORXR_D3D11_BRIDGE_DEPTH=private keeps depth the game's own, as + // before the bridge mirrored it (left out of submitted frames). + if (const char* depth = std::getenv("VECTORXR_D3D11_BRIDGE_DEPTH"); depth && std::string(depth) == "private") { + impl->mirror_depth = false; + impl->depth_private_reason = "VECTORXR_D3D11_BRIDGE_DEPTH=private"; + } else if (keep_depth_private_) { + impl->mirror_depth = false; + impl->depth_private_reason = "Frame Generation's Use game depth is off"; + } + impl->binding.next = game.next; impl->binding.device = impl->device12.Get(); impl->binding.queue = impl->queue.Get(); @@ -784,6 +1470,7 @@ const XrSessionCreateInfo* D3D11Bridge::Begin(XrInstance instance, const XrSessi << ", sharedResourceCompatibilityTier=" << static_cast(options4.SharedResourceCompatibilityTier) << ", d3d11SharedResourceTier=" << static_cast(options5.SharedResourceTier) << ", multithreadProtected=" << (impl->multithread_protected ? "yes" : "no") + << ", depth=" << (impl->mirror_depth ? std::string("mirrored") : "private (" + impl->depth_private_reason + ")") << ", videoMemory=" << impl->VideoMemory() << (impl->debug_layer ? ", debugLayer=on" : ""); description = stream.str(); impl_ = std::move(impl); @@ -853,6 +1540,7 @@ void D3D11Bridge::End() { for (Impl::Swapchain& chain : impl_->retired) { Impl::DetachGpu(chain); } + impl_->DetachPipelines(); impl_->queue.Detach(); impl_->written.Detach(); impl_->done.Detach(); @@ -888,6 +1576,11 @@ XrSwapchainCreateInfo D3D11Bridge::RuntimeCreateInfo(const XrSwapchainCreateInfo if (!IsDepthSwapchain(info)) { runtime.usageFlags |= XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT | XR_SWAPCHAIN_USAGE_TRANSFER_DST_BIT; runtime.sampleCount = 1; + } else { + // The bridge's draw writes depth through a depth target: PimaxXR and + // Virtual Desktop make an image one (in the depth-write state) only + // when asked for depth-stencil use. + runtime.usageFlags |= XR_SWAPCHAIN_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; } return runtime; } @@ -907,8 +1600,19 @@ bool D3D11Bridge::AddSwapchain(XrSwapchain swapchain, const XrSwapchainCreateInf chain.mip_count = std::max(1u, info.mipCount); chain.sample_count = std::max(1u, info.sampleCount); const bool depth = IsDepthSwapchain(info); - chain.mode = depth ? Impl::Mode::kPrivate : chain.sample_count > 1 ? Impl::Mode::kResolve : Impl::Mode::kTwin; + chain.mode = depth ? Impl::Mode::kDepth : chain.sample_count > 1 ? Impl::Mode::kResolve : Impl::Mode::kTwin; chain.storage = depth ? DepthStorageFormat(chain.format) : ColorStorageFormat(chain.format); + // Depth the bridge cannot mirror stays the game's own (with why). + if (depth && !impl_->mirror_depth) { + chain.mode = Impl::Mode::kPrivate; + chain.private_reason = impl_->depth_private_reason; + } else if (depth && chain.sample_count > 1) { + chain.mode = Impl::Mode::kPrivate; + chain.private_reason = "multisampled"; + } else if (depth && !IsDepthFormat(chain.format)) { + chain.mode = Impl::Mode::kPrivate; + chain.private_reason = "format " + std::to_string(static_cast(chain.format)) + " is not depth"; + } std::uint32_t count = 0; if (!enumerate || XR_FAILED(enumerate(swapchain, 0, &count, nullptr)) || count == 0) { @@ -927,7 +1631,8 @@ bool D3D11Bridge::AddSwapchain(XrSwapchain swapchain, const XrSwapchainCreateInf for (std::uint32_t i = 0; i < count; ++i) { Impl::Image& image = chain.images[i]; image.runtime = runtime_images[i].texture; - if (chain.mode != Impl::Mode::kPrivate && !impl_->MakeTwin(chain, image, unordered_access, reason)) { + const bool twin = chain.mode == Impl::Mode::kTwin || chain.mode == Impl::Mode::kResolve; + if (twin && !impl_->MakeTwin(chain, image, unordered_access, reason)) { reason = "image " + std::to_string(i) + ": " + reason; return false; } @@ -936,7 +1641,28 @@ bool D3D11Bridge::AddSwapchain(XrSwapchain swapchain, const XrSwapchainCreateInf return false; } } + if (chain.mode == Impl::Mode::kDepth) { + std::string why; + if (!impl_->MirrorDepth(chain, why)) { + Impl::KeepDepthPrivate(chain, why); + } + } + // Before taking `mutex`: turning protection on may wait for the + // context's lock. + const bool protect = chain.mode == Impl::Mode::kDepth && impl_->multithread; + const bool was_protected = protect && impl_->multithread->GetMultithreadProtected() != FALSE; + if (protect && !was_protected) { + impl_->multithread->SetMultithreadProtected(TRUE); + } + const bool now_protected = protect && impl_->multithread->GetMultithreadProtected() != FALSE; + if (chain.mode == Impl::Mode::kPrivate && chain.private_reason != impl_->depth_private_reason && impl_->log) { + impl_->log("Direct3D 11 bridge: depth swapchain " + Hex(reinterpret_cast(swapchain)) + + " stays the game's own (its depth is left out of submitted frames): " + chain.private_reason + "."); + } std::scoped_lock lock(impl_->mutex); + if (protect) { + chain.protection = impl_->NoteDepthProtection(was_protected, now_protected); + } impl_->swapchains[swapchain] = std::move(chain); ++impl_->swapchains_added; return true; @@ -1005,13 +1731,14 @@ void D3D11Bridge::Acquired(XrSwapchain swapchain, std::uint32_t index) { if (!impl_) { return; } - std::scoped_lock lock(impl_->mutex); + std::unique_lock lock(impl_->mutex); Impl::Swapchain* chain = impl_->Find(swapchain); if (!chain) { return; } chain->acquired.push_back(index); - if (chain->mode == Impl::Mode::kPrivate || index >= chain->images.size()) { + // Depth is rendered privately: its twin is written only at release. + if (chain->mode == Impl::Mode::kPrivate || chain->mode == Impl::Mode::kDepth || index >= chain->images.size()) { return; } // Until the bridge's queue is done reading the image's last release, the @@ -1022,82 +1749,136 @@ void D3D11Bridge::Acquired(XrSwapchain swapchain, std::uint32_t index) { return; } impl_->NoteThread(); - impl_->context11->Wait(impl_->done11.Get(), value); ++chain->done_waits; ++impl_->done_waits; + // Queued without `mutex`: a call on the game's context may wait for its + // lock, which a game thread may hold while it waits for `mutex`. + lock.unlock(); + impl_->context11->Wait(impl_->done11.Get(), value); } bool D3D11Bridge::Released(XrSwapchain swapchain) { if (!impl_) { return true; } - std::scoped_lock lock(impl_->mutex); + std::unique_lock lock(impl_->mutex); Impl::Swapchain* chain = impl_->Find(swapchain); if (!chain) { return true; } + // A release whose Direct3D 11 half did not run ahead (StageReleases) + // runs it here, holding the context first (ContextHold's lock order). + std::optional hold; + if (Impl::NeedsContext(*chain)) { + lock.unlock(); + hold.emplace(impl_->multithread.Get(), &impl_->context_wait_hook); + lock.lock(); + chain = impl_->Find(swapchain); + if (!chain) { + return true; + } + } ++impl_->releases; + if (chain->pending > 0) { + --chain->pending; + } + if (!chain->staged.empty()) { + // ...and the bridge's queue then copies it into the runtime's image. + const Impl::Staged staged = chain->staged.front(); + chain->staged.pop_front(); + hold.reset(); + return impl_->Submit(*chain, staged); + } if (chain->acquired.empty()) { ++chain->unmatched_releases; ++impl_->unmatched_releases; return true; } - const std::uint32_t index = chain->acquired.front(); - chain->acquired.pop_front(); - if (chain->mode == Impl::Mode::kPrivate || index >= chain->images.size()) { + if (!Impl::NeedsContext(*chain)) { + // Private depth (or an index past the images): nothing to copy. + chain->acquired.pop_front(); return true; } - impl_->NoteThread(); - Impl::Image& image = chain->images[index]; - if (chain->mode == Impl::Mode::kResolve) { - const DXGI_FORMAT format = ResolveFormat(chain->format); - for (UINT slice = 0; slice < chain->array_size; ++slice) { - const UINT subresource = D3D11CalcSubresource(0, slice, 1); - impl_->context11->ResolveSubresource(image.twin11.Get(), subresource, image.private11.Get(), - subresource, format); - } - } - // The game's rendering into the image is handed to the bridge's queue... - const auto handover = std::chrono::steady_clock::now(); - const UINT64 written = ++impl_->written_value; - impl_->context11->Signal(impl_->written11.Get(), written); - impl_->context11->Flush(); - Impl::Copy* copy = impl_->ReadyCopy(image); - if (!copy) { - impl_->NoteHandover(handover); - // Every recording of the copy is still queued: the bridge's queue is - // stalled. The runtime's image keeps what it had. - ++chain->skipped_copies; - ++impl_->skipped_copies; - if (impl_->skipped_copies == impl_->next_skip_report && impl_->log) { - impl_->next_skip_report *= 10; - impl_->log("Direct3D 11 bridge: its queue is stalled; releases not copied into the runtime's images so " - "far: " + std::to_string(impl_->skipped_copies) + "."); + // The game's rendering into the image is handed to the bridge's queue + // (depth copied into its twin first, multisampled images resolved)... + const Impl::Staged staged = impl_->Stage(*chain); + hold.reset(); + return impl_->Submit(*chain, staged); +} + +void D3D11Bridge::StageReleases(XrSwapchain swapchain) { + if (!impl_) { + return; + } + const auto waiting = [&](const Impl::Swapchain& chain) { + return chain.pending > chain.staged.size() && chain.acquired.size() > 0; + }; + bool any = false; + { + std::scoped_lock lock(impl_->mutex); + // This call's thread counts (a frame's or swapchain call's, as + // FrameEnded and ReleaseCalled note theirs). + impl_->NoteThread(); + if (swapchain != XR_NULL_HANDLE) { + const Impl::Swapchain* chain = impl_->Find(swapchain); + any = chain && waiting(*chain); + } else { + any = std::any_of(impl_->swapchains.begin(), impl_->swapchains.end(), + [&](const auto& entry) { return waiting(entry.second); }); } - return false; } - // ...which copies it into the runtime's image, ahead of anything else - // queued on it for the release (Frame Generation's capture). - impl_->queue->Wait(impl_->written.Get(), written); - ID3D12CommandList* lists[] = {copy->list.Get()}; - impl_->queue->ExecuteCommandLists(1, lists); - const UINT64 done = ++impl_->done_value; - impl_->queue->Signal(impl_->done.Get(), done); - copy->done = done; - image.done = done; - chain->last_done = done; - ++chain->copies; - ++impl_->copies; - impl_->NoteHandover(handover); - return true; + // Before the context and `mutex`: a second thread seen since the last + // hand-over (this one included) turns protection on first. + impl_->ApplyWantedProtection(); + if (!any) { + return; + } + // The context first, holding nothing else (ContextHold's lock order)... + const ContextHold hold = impl_->HoldContext(); + std::scoped_lock lock(impl_->mutex); + const auto stage = [&](Impl::Swapchain& chain) { + // ...each release the application made, oldest first: the image + // acquired earliest, as Released hands it over. + while (waiting(chain)) { + if (chain.mode == Impl::Mode::kPrivate || chain.acquired.front() >= chain.images.size()) { + return; // nothing to run ahead: Released hands it over as it is + } + chain.staged.push_back(impl_->Stage(chain)); + } + }; + if (swapchain != XR_NULL_HANDLE) { + if (Impl::Swapchain* chain = impl_->Find(swapchain)) { + stage(*chain); + } + } else { + for (auto& [handle, chain] : impl_->swapchains) { + stage(chain); + } + } +} + +void D3D11Bridge::SetContextWaitHookForTest(std::function hook) { + if (impl_) { + impl_->context_wait_hook = std::move(hook); + } +} + +void D3D11Bridge::SetDepthCopyHookForTest(std::function hook) { + if (impl_) { + std::scoped_lock lock(impl_->mutex); + impl_->depth_copy_hook = std::move(hook); + } } -void D3D11Bridge::ReleaseCalled() { +void D3D11Bridge::ReleaseCalled(XrSwapchain swapchain) { if (!impl_) { return; } std::scoped_lock lock(impl_->mutex); impl_->NoteThread(); + if (Impl::Swapchain* chain = impl_->Find(swapchain)) { + ++chain->pending; + } } void D3D11Bridge::Abandoned(XrSwapchain swapchain) { @@ -1184,6 +1965,9 @@ std::string D3D11Bridge::Summary() const { << ", releases=" << impl_->releases << ", copies=" << impl_->copies << ", skippedCopies=" << impl_->skipped_copies << ", copyRecordings=" << impl_->recordings.load() << ", doneWaits=" << impl_->done_waits << ", unmatchedReleases=" << impl_->unmatched_releases + << ", depthCopies=" << impl_->depth_copies << ", depthTargetUnbinds=" << impl_->depth_target_unbinds + << ", depthCopyMs=" + << HandoverText(impl_->depth_copy_ms_total, impl_->depth_copies, impl_->depth_copy_ms_max) << ", handoverMs=" << HandoverText(impl_->handover_ms_total, impl_->handovers, impl_->handover_ms_max) << ", slowHandovers=" << impl_->slow_handovers << ", writtenValue=" << impl_->written_value << ", doneValue=" << impl_->done_value diff --git a/layer/src/framegen/framegen_d3d12.cpp b/layer/src/framegen/framegen_d3d12.cpp index 39196bb3..abfefafc 100644 --- a/layer/src/framegen/framegen_d3d12.cpp +++ b/layer/src/framegen/framegen_d3d12.cpp @@ -58,6 +58,8 @@ constexpr UINT kDescriptorsPerSynthesis = 256; // dispatch per capture slot, after every frame's range. constexpr UINT kShaderResources = 6; constexpr UINT kFirstUav = kShaderResources; +// The root parameter holding the draws' edge fill (b1, FrameGenImage::content). +constexpr UINT kFillParameter = 3; constexpr UINT kDescriptorsPerDispatch = kShaderResources + 2; // Engine motion vectors (CaptureMotion): submissions in flight, never waited // for, each with a block of descriptors per dispatch (the two eyes' @@ -270,6 +272,10 @@ struct ViewResources { // frames read again (0: no frame's). std::uint64_t shading_frame{0}; const Texture* shading_depth{nullptr}; + // Edge fill: the output image the rates cover and where the frame starts + // in it (the frame's own extent and {0, 0} without the fill). + FrameGenExtent shading_covered{}; + XrOffset2Di shading_offset{0, 0}; bool trust_stereo{false}; // this frame's stereo evidence is in trust[newest] bool depth_ready{false}; // captured since the last Execute bool depth_fresh{false}; // captured for this Execute's frame (for the check) @@ -562,7 +568,7 @@ class D3D12FrameGen final : public FrameGenGpu { D3D12_DESCRIPTOR_RANGE ranges[2]{}; ranges[0] = {D3D12_DESCRIPTOR_RANGE_TYPE_SRV, kShaderResources, 0, 0, 0}; ranges[1] = {D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 2, 0, 0, 0}; - D3D12_ROOT_PARAMETER parameters[3]{}; + D3D12_ROOT_PARAMETER parameters[4]{}; parameters[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; parameters[0].Constants = {0, 0, sizeof(FrameGenConstants) / 4}; parameters[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL; @@ -571,12 +577,16 @@ class D3D12FrameGen final : public FrameGenGpu { parameters[1 + i].DescriptorTable = {1, &ranges[i]}; parameters[1 + i].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL; } + // b1: the draws' edge fill (g_fill), set by every draw. + parameters[kFillParameter].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; + parameters[kFillParameter].Constants = {1, 0, 4}; + parameters[kFillParameter].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL; D3D12_STATIC_SAMPLER_DESC sampler{}; sampler.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR; sampler.AddressU = sampler.AddressV = sampler.AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP; sampler.MaxLOD = D3D12_FLOAT32_MAX; sampler.ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL; - D3D12_ROOT_SIGNATURE_DESC root{3, parameters, 1, &sampler, D3D12_ROOT_SIGNATURE_FLAG_NONE}; + D3D12_ROOT_SIGNATURE_DESC root{4, parameters, 1, &sampler, D3D12_ROOT_SIGNATURE_FLAG_NONE}; ComPtr serialized, diagnostics; if (!check(D3D12SerializeRootSignature(&root, D3D_ROOT_SIGNATURE_VERSION_1, &serialized, &diagnostics), "D3D12 Frame Generation root signature") || @@ -1256,6 +1266,20 @@ class D3D12FrameGen final : public FrameGenGpu { error = "Frame Generation cannot read depth format " + std::to_string(static_cast(desc.Format)); return false; } + // Texels past the image read as zero, which decodes as near or + // infinitely far: a rect the game placed by its colour image's size + // in a smaller depth image (UEVR with an upscaler or screen + // percentage) is refused instead. + const XrRect2Di& rect = depth.rect; + if (rect.offset.x < 0 || rect.offset.y < 0 || rect.extent.width <= 0 || rect.extent.height <= 0 || + static_cast(rect.offset.x) + static_cast(rect.extent.width) > desc.Width || + static_cast(rect.offset.y) + static_cast(rect.extent.height) > desc.Height) { + error = "Frame Generation's depth rect (" + std::to_string(rect.offset.x) + "," + + std::to_string(rect.offset.y) + " " + std::to_string(rect.extent.width) + "x" + + std::to_string(rect.extent.height) + ") lies outside its " + std::to_string(desc.Width) + "x" + + std::to_string(desc.Height) + " image"; + return false; + } HRESULT hr = DeviceRemovedReason(); if (FAILED(hr)) { error = Failure("D3D12 device", hr); @@ -2538,9 +2562,24 @@ class D3D12FrameGen final : public FrameGenGpu { return pipeline; } + // Whether `image`'s region holds a frame of `extent` at its content + // offset: the frame's own size, or (edge fill) wider by a margin each + // side. + static bool HoldsFrame(const FrameGenImage& image, FrameGenExtent extent) { + return image.content.x >= 0 && image.content.y >= 0 && + static_cast(image.content.x) + extent.width <= image.rect.extent.width && + static_cast(image.content.y) + extent.height <= image.rect.extent.height; + } + + // The draws' edge fill constants (b1) for an output image. + static std::array FillConstants(const FrameGenImage& image) { + return {static_cast(image.content.x), static_cast(image.content.y), 0u, 0u}; + } + // Whether every generated frame of the view can be drawn straight into // its output image: renderable, single-sampled, a typed format, and the - // whole image at the view's size; together, or `one_at_a_time`. + // whole image at the view's size (edge fill: wider, the frame at the same + // place in each); together, or `one_at_a_time`. bool CanDraw(const FrameGenViewSubmission& submission, const std::vector& outputs, std::size_t count, bool one_at_a_time) { if (!direct_output_ || !rtv_heap_ || count == 0 || count > kFrameGenMaxGenerated || outputs.size() < count) { @@ -2550,13 +2589,17 @@ class D3D12FrameGen final : public FrameGenGpu { for (std::size_t i = 0; i < count; ++i) { formats[i] = static_cast(submission.outputs[i].format); } + const FrameGenImage& first_image = submission.outputs[0]; for (std::size_t i = 0; i < count; ++i) { const FrameGenImage& image = submission.outputs[i]; const D3D12_RESOURCE_DESC desc = outputs[i]->GetDesc(); if (!(desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET) || desc.SampleDesc.Count != 1 || image.array_index >= desc.DepthOrArraySize || image.rect.offset.x != 0 || image.rect.offset.y != 0 || - static_cast(image.rect.extent.width) != submission.request.extent.width || - static_cast(image.rect.extent.height) != submission.request.extent.height || + !HoldsFrame(image, submission.request.extent) || + image.rect.extent.width != first_image.rect.extent.width || + image.rect.extent.height != first_image.rect.extent.height || image.content.x != first_image.content.x || + image.content.y != first_image.content.y || static_cast(image.rect.extent.width) > desc.Width || + static_cast(image.rect.extent.height) > desc.Height || image.format == DXGI_FORMAT_R8G8B8A8_TYPELESS || image.format == DXGI_FORMAT_B8G8R8A8_TYPELESS || image.format == DXGI_FORMAT_R16G16B16A16_TYPELESS || image.format == DXGI_FORMAT_UNKNOWN) { return false; @@ -2739,6 +2782,19 @@ class D3D12FrameGen final : public FrameGenGpu { void SetDirectOutput(bool on) override { direct_output_ = on; } bool DrawsOutputs() const override { return direct_output_ && rtv_heap_; } + // The margin is drawn with the frame (FG_PASS_SYNTHESIZE_DRAW, and the + // quad views compose draw); a frame copied by compute fills none. + bool SupportsEdgeFill() const override { return DrawsOutputs(); } + bool EdgesFilled() const override { return edges_filled_; } + // Edge fill: no output with a margin written by the copy fallback since + // the frame function began (EdgesFilled). + bool edges_filled_{true}; + // Whether image has a margin around a frame of extent. + static bool HasMargin(const FrameGenImage& image, FrameGenExtent extent) { + return image.content.x != 0 || image.content.y != 0 || + static_cast(image.rect.extent.width) != extent.width || + static_cast(image.rect.extent.height) != extent.height; + } std::uint64_t DrawsForTest() const override { return draws_; } std::uint64_t ShadingRatesForTest() const override { return shading_rates_; } @@ -2763,8 +2819,20 @@ class D3D12FrameGen final : public FrameGenGpu { if (!shading_tile_) { return false; } + // Edge fill: the tiles cover the whole output image, the frame at its + // content offset (every output of a view's draw alike, CanDraw). + const FrameGenImage* image = submission.outputs.empty() ? nullptr : &submission.outputs.front(); + const XrOffset2Di offset = image ? image->content : XrOffset2Di{0, 0}; + const FrameGenExtent covered = + image && (image->content.x || image->content.y || + static_cast(image->rect.extent.width) != submission.request.extent.width || + static_cast(image->rect.extent.height) != submission.request.extent.height) + ? FrameGenExtent{static_cast(image->rect.extent.width), + static_cast(image->rect.extent.height)} + : submission.request.extent; if (drawing_frame_ && view.shading_frame == drawing_frame_ && view.shading_depth == depth && - view.shading_rate.resource) { + view.shading_rate.resource && view.shading_offset.x == offset.x && view.shading_offset.y == offset.y && + view.shading_covered.width == covered.width && view.shading_covered.height == covered.height) { return true; } view.shading_frame = 0; @@ -2789,10 +2857,16 @@ class D3D12FrameGen final : public FrameGenGpu { if (!view.shading_coarse) { return false; } - const UINT tiles_x = (extent.width + shading_tile_ - 1) / shading_tile_; - const UINT tiles_y = (extent.height + shading_tile_ - 1) / shading_tile_; + const UINT tiles_x = (covered.width + shading_tile_ - 1) / shading_tile_; + const UINT tiles_y = (covered.height + shading_tile_ - 1) / shading_tile_; if (!view.shading_rate.resource || view.shading_rate.resource->GetDesc().Width != tiles_x || view.shading_rate.resource->GetDesc().Height != tiles_y) { + // Replaced (edge fill turned on or off, a view of another size): + // earlier submissions may still read the old image. + if (view.shading_rate.resource && !WaitIdle()) { + error = "D3D12 Frame Generation GPU work did not finish before reallocation"; + return false; + } if (!Create(view.shading_rate, {tiles_x, tiles_y}, DXGI_FORMAT_R8_UINT, DXGI_FORMAT_R8_UINT, true, error)) { view.shading_coarse = false; return false; @@ -2817,7 +2891,8 @@ class D3D12FrameGen final : public FrameGenGpu { FrameGenConstants constants{}; constants.fov_current = tangents; constants.size = {tiles_x, tiles_y, extent.width, extent.height}; - constants.options = {shading_tile_, depth ? kFrameGenFlagDepth : 0u, 0u, 0u}; + constants.options = {shading_tile_, depth ? kFrameGenFlagDepth : 0u, static_cast(offset.x), + static_cast(offset.y)}; constants.tuning = {static_cast(coarse_tangent_), 0.0f, 0.0f, 0.0f}; commands_->SetPipelineState(pipelines_[static_cast(FrameGenPass::kShadingRate)].Get()); commands_->SetComputeRoot32BitConstants(0, sizeof(FrameGenConstants) / 4, &constants, 0); @@ -2828,6 +2903,8 @@ class D3D12FrameGen final : public FrameGenGpu { Stamp(static_cast(FrameGenPass::kShadingRate)); view.shading_frame = drawing_frame_; view.shading_depth = depth; + view.shading_offset = offset; + view.shading_covered = covered; ++shading_rates_; return true; } @@ -2904,13 +2981,18 @@ class D3D12FrameGen final : public FrameGenGpu { const float fraction = frames[i]->constants.tuning[0]; std::memcpy(i < 3 ? &constants.size[1 + i] : &constants.options[0], &fraction, sizeof(fraction)); } - const D3D12_VIEWPORT viewport{0.0f, 0.0f, static_cast(submission.request.extent.width), - static_cast(submission.request.extent.height), 0.0f, 1.0f}; - const D3D12_RECT scissor{0, 0, static_cast(submission.request.extent.width), - static_cast(submission.request.extent.height)}; + // The whole output image: with edge fill, the margin around the frame + // too (CanDraw: every output of the draw alike). + const FrameGenImage& image = submission.outputs[indices[0]]; + const D3D12_VIEWPORT viewport{0.0f, 0.0f, static_cast(image.rect.extent.width), + static_cast(image.rect.extent.height), 0.0f, 1.0f}; + const D3D12_RECT scissor{0, 0, static_cast(image.rect.extent.width), + static_cast(image.rect.extent.height)}; + const std::array fill = FillConstants(image); commands_->SetGraphicsRootSignature(root_signature_.Get()); commands_->SetPipelineState(pipeline); commands_->SetGraphicsRoot32BitConstants(0, sizeof(FrameGenConstants) / 4, &constants, 0); + commands_->SetGraphicsRoot32BitConstants(kFillParameter, 4, fill.data(), 0); commands_->SetGraphicsRootDescriptorTable(1, Gpu(base)); commands_->SetGraphicsRootDescriptorTable(2, Gpu(base + kFirstUav)); commands_->RSSetViewports(1, &viewport); @@ -2973,6 +3055,7 @@ class D3D12FrameGen final : public FrameGenGpu { bool Execute(const std::vector& views, const FrameGenTuning& tuning, FrameGenExecuteResult& result, std::string& error) override { + edges_filled_ = true; result.synthesized.assign(views.size(), false); result.depth.assign(views.size(), false); result.merged.assign(views.size(), false); @@ -3471,6 +3554,7 @@ class D3D12FrameGen final : public FrameGenGpu { bool Synthesize(std::uint64_t frame_id, FrameGenSynthesisMode mode, float fraction, const std::vector& outputs, std::string& error) override { + edges_filled_ = true; HRESULT hr = DeviceRemovedReason(); if (FAILED(hr)) { error = Failure("D3D12 device", hr); @@ -3543,6 +3627,7 @@ class D3D12FrameGen final : public FrameGenGpu { bool SynthesizeStale(std::uint64_t frame_id, std::uint64_t motion_id, float fraction, const std::vector& outputs, std::string& error) override { + edges_filled_ = true; HRESULT hr = DeviceRemovedReason(); if (FAILED(hr)) { error = Failure("D3D12 Frame Generation device", hr); @@ -3634,6 +3719,9 @@ class D3D12FrameGen final : public FrameGenGpu { bool ComposeSynthesis(std::uint64_t frame_id, std::uint64_t motion_id, FrameGenSynthesisMode mode, float fraction, const FrameGenComposition& composition, const std::vector& outputs, std::string& error) { + // The compose draw fills every eye's margin (ComposeOutput refuses + // an output it cannot draw into). + edges_filled_ = true; if (!supports_composition_) { error = "this Direct3D 12 device cannot compose quad views"; return false; @@ -4284,8 +4372,8 @@ class D3D12FrameGen final : public FrameGenGpu { const DXGI_FORMAT format = static_cast(image.format); if (!(desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET) || desc.SampleDesc.Count != 1 || image.array_index >= desc.DepthOrArraySize || image.rect.offset.x != 0 || image.rect.offset.y != 0 || - static_cast(image.rect.extent.width) != extent.width || - static_cast(image.rect.extent.height) != extent.height || format == DXGI_FORMAT_UNKNOWN || + !HoldsFrame(image, extent) || static_cast(image.rect.extent.width) > desc.Width || + static_cast(image.rect.extent.height) > desc.Height || format == DXGI_FORMAT_UNKNOWN || format == DXGI_FORMAT_R8G8B8A8_TYPELESS || format == DXGI_FORMAT_B8G8R8A8_TYPELESS || format == DXGI_FORMAT_R16G16B16A16_TYPELESS) { error = "a composed eye's output image cannot be drawn into"; @@ -4338,12 +4426,18 @@ class D3D12FrameGen final : public FrameGenGpu { D3D12_CPU_DESCRIPTOR_HANDLE target = rtv_heap_->GetCPUDescriptorHandleForHeapStart(); target.ptr += static_cast(rtv_cursor_++ % kRenderTargetViews) * rtv_size_; device_->CreateRenderTargetView(output, &rtv, target); - const D3D12_VIEWPORT viewport{0.0f, 0.0f, static_cast(extent.width), static_cast(extent.height), - 0.0f, 1.0f}; - const D3D12_RECT scissor{0, 0, static_cast(extent.width), static_cast(extent.height)}; + // The whole output image (ComposeOutput): with edge fill, the margin + // around the eye too. + (void)extent; + const D3D12_VIEWPORT viewport{0.0f, 0.0f, static_cast(image.rect.extent.width), + static_cast(image.rect.extent.height), 0.0f, 1.0f}; + const D3D12_RECT scissor{0, 0, static_cast(image.rect.extent.width), + static_cast(image.rect.extent.height)}; + const std::array fill = FillConstants(image); commands_->SetGraphicsRootSignature(root_signature_.Get()); commands_->SetPipelineState(pipeline); commands_->SetGraphicsRoot32BitConstants(0, sizeof(FrameGenConstants) / 4, &constants, 0); + commands_->SetGraphicsRoot32BitConstants(kFillParameter, 4, fill.data(), 0); commands_->SetGraphicsRootDescriptorTable(1, Gpu(base)); commands_->SetGraphicsRootDescriptorTable(2, Gpu(base + kFirstUav)); commands_->RSSetViewports(1, &viewport); @@ -6817,8 +6911,13 @@ class D3D12FrameGen final : public FrameGenGpu { out_destination.SubresourceIndex = image.array_index * output_desc.MipLevels; D3D12_TEXTURE_COPY_LOCATION out_source{view.output.resource.Get(), D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX}; out_source.SubresourceIndex = 0; - commands_->CopyTextureRegion(&out_destination, static_cast(image.rect.offset.x), - static_cast(image.rect.offset.y), 0, &out_source, nullptr); + // Copied, a frame keeps its place in an image with edge fill; + // its margin keeps what it held, so the caller shows the frame + // from its own region (EdgesFilled). + if (HasMargin(image, submission.request.extent)) edges_filled_ = false; + commands_->CopyTextureRegion(&out_destination, static_cast(image.rect.offset.x + image.content.x), + static_cast(image.rect.offset.y + image.content.y), 0, &out_source, + nullptr); Transition(barriers, output, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_RENDER_TARGET); Flush(barriers); Stamp(kProfileOutputCopy); diff --git a/layer/src/framegen/framegen_presenter.cpp b/layer/src/framegen/framegen_presenter.cpp index 40efb966..a5c9c890 100644 --- a/layer/src/framegen/framegen_presenter.cpp +++ b/layer/src/framegen/framegen_presenter.cpp @@ -3,6 +3,7 @@ #include "depthxr/framegen_presenter.h" #include "depthxr/config_path.h" +#include "depthxr/game_context_lock.h" #include "depthxr/logger.h" #include "depthxr/process_info.h" @@ -60,6 +61,15 @@ constexpr std::chrono::milliseconds kPauseCeiling{1000}; // this share of the refresh rate of stay_above (what it runs at standing by // is not much faster: the generated frames' own cost is about that). constexpr double kProbeReach = 0.10; +// A runtime whose xrEndFrame calls made at once take at least this share of +// a refresh (smoothed, after kEndPacedSamples of them) holds frames there +// until its slot; frames then go kEndMarginMs before the slot (RuntimeEnd), +// more when the slot comes earlier than that, never within kEndMarginCeiling +// of a whole refresh (made at once again). +constexpr double kEndPacedShare = 0.5; +constexpr std::uint32_t kEndPacedSamples = 8; +constexpr double kEndMarginMs = 2.0; +constexpr double kEndMarginCeiling = 0.9; // Pauses logged one by one per session; later ones only in summaries. constexpr std::uint32_t kLoggedPauses = 3; // Moves of the lead's ceiling from the history ring logged per session; @@ -279,6 +289,37 @@ FrameGenExtent Extent(const XrCompositionLayerProjectionView& view) { static_cast(std::max(0, view.subImage.imageRect.extent.height))}; } +FrameGenFov Fov(const XrFovf& fov) { + return {fov.angleLeft, fov.angleRight, fov.angleUp, fov.angleDown}; +} + +// Edge fill: whether a view written by Frame Generation shows the game's +// own field of view (an exact copy may then carry the game's depth, which +// describes that image), not one widened by a margin. +bool SameFov(const XrCompositionLayerProjectionView& a, const XrCompositionLayerProjectionView& b) { + return a.fov.angleLeft == b.fov.angleLeft && a.fov.angleRight == b.fov.angleRight && + a.fov.angleUp == b.fov.angleUp && a.fov.angleDown == b.fov.angleDown; +} + +// Edge fill: output images whose margins are this many pixels wider than a +// frame asks for at most are kept (EnsureOutputs). +constexpr std::uint32_t kEdgeFillSlack = 64; +// Edge fill: margins made anew are rounded up to whole tiles of this many +// pixels, the largest shading-rate tile: the frame then lies on the tiles +// and the 2x2 coarse pixels of its draw as without the fill, and its own +// pixels come out the same. +constexpr std::uint32_t kEdgeFillRound = 32; + +// Edge fill's margins in degrees (research: VECTORXR_FRAMEGEN_EDGE_FILL=h[,v]). +std::array EdgeFillDegrees() { + if (const std::optional value = ReadResearchSwitch("VECTORXR_FRAMEGEN_EDGE_FILL")) { + if (const auto parsed = ParseFrameGenEdgeFillDegrees(*value)) { + return *parsed; + } + } + return {kFrameGenEdgeFillHorizontalDegrees, kFrameGenEdgeFillVerticalDegrees}; +} + // One history slot of these views, estimated: each view's image, and // depth, trust and flow at half resolution or less (about one and a half // images' worth); with the engine motion map (`map`, stage 7: @@ -650,6 +691,7 @@ void FrameGenPresenter::Start(XrSession session, const void* binding_chain, cons // Research: the Flow view tinting sub-pixel motion (again at each config // reload). flow_view_fine_ = ReadResearchSwitch("VECTORXR_FRAMEGEN_SYNTH_FLOW_VIEW") == "fine"; + edge_fill_degrees_ = EdgeFillDegrees(); // again at each config reload // Research (the head-motion shimmer diagnosis, D), for the session: where // extrapolated frames' eyes go (VECTORXR_FRAMEGEN_SYNTH_EYE). eye_prediction_ = ParseFrameGenEyePrediction(ReadResearchSwitch("VECTORXR_FRAMEGEN_SYNTH_EYE").value_or("")); @@ -729,6 +771,10 @@ void FrameGenPresenter::Start(XrSession session, const void* binding_chain, cons ring_ceiling_env_ = false; } } + // VECTORXR_FRAMEGEN_END_HOLD=off: frames go to a runtime that holds them + // in xrEndFrame as soon as they are written (RuntimeEnd). + const char* end_hold = std::getenv("VECTORXR_FRAMEGEN_END_HOLD"); + runtime_end_hold_ = !(end_hold && std::string(end_hold) == "off"); std::string gpu_gate_setting; if (const char* ahead = std::getenv("VECTORXR_FRAMEGEN_GPU_AHEAD"); ahead && *ahead) { gpu_gate_setting = ahead; @@ -1111,8 +1157,14 @@ void FrameGenPresenter::Stop() { runtime_waits_ = runtime_begins_ = runtime_ends_ = 0; presenter_frame_open_ = false; logged_order_guard_ = logged_watchdog_ = false; + runtime_end_block_ms_ = runtime_end_margin_ms_ = 0.0; + runtime_end_block_samples_ = 0; + runtime_end_return_.reset(); + logged_runtime_end_hold_ = false; + summary_evidence_.reset(); display_offset_periods_ = 0.0; display_offset_samples_ = 0; + display_offset_earlier_ = 0; slot_index_ = 0; slot_time_ = slot_period_ = 0; app_latency_ms_ = 0.0; @@ -1130,6 +1182,7 @@ void FrameGenPresenter::Stop() { dynamic_engaged_at_ = {}; dynamic_probing_ = false; passthrough_fps_ = 0.0; + dynamic_rate_check_fps_.reset(); dynamic_reason_.clear(); dynamic_engagements_ = dynamic_stand_downs_ = dynamic_probes_ = 0; standby_since_.reset(); @@ -1142,6 +1195,7 @@ void FrameGenPresenter::Stop() { guard_failing_ = {}; guard_capture_done_ms_.clear(); gpu_gate_armed_ = false; + summary_evidence_.reset(); gpu_timeouts_in_row_ = 0; gpu_reached_mark_ = 0; last_capture_mark_ = 0; @@ -1157,6 +1211,8 @@ void FrameGenPresenter::Stop() { gpu_wait_ms_.clear(); end_call_ms_.clear(); runtime_end_ms_.clear(); + runtime_end_hold_ms_.clear(); + runtime_wait_ms_.clear(); synth_lock_ms_.clear(); ring_short_write_ms_.clear(); told_lead_sum_ = 0; @@ -1238,10 +1294,21 @@ void FrameGenPresenter::Configure(const FrameGenResolvedSettings& settings) { const bool engine_cockpit = ReadResearchSwitch("VECTORXR_FRAMEGEN_ENGINE_COCKPIT") == "1"; const bool engine_tint = ReadResearchSwitch("VECTORXR_FRAMEGEN_ENGINE_TINT") == "1"; const bool flow_view_fine = ReadResearchSwitch("VECTORXR_FRAMEGEN_SYNTH_FLOW_VIEW") == "fine"; + const std::array edge_fill_degrees = EdgeFillDegrees(); std::scoped_lock lock(mutex_); ReconfigureEngineLocked(ParseFrameGenEngineMode(engine_mode.value_or("").c_str()), engine_filter, engine_cockpit, engine_tint); flow_view_fine_ = flow_view_fine; + // Edge fill on or off, or its margins changed: the next frame written + // makes its output images anew (EnsureOutputs), and the log says so. + if (started_ && logger_ && (settings.edge_fill != settings_.edge_fill || edge_fill_degrees != edge_fill_degrees_)) { + logger_->Info(std::string("Frame Generation edge fill ") + (settings.edge_fill ? "on" : "off") + " (" + + Number(edge_fill_degrees[0], 1) + " degrees left and right, " + Number(edge_fill_degrees[1], 1) + + " up and down" + + (gpu_ && !gpu_->SupportsEdgeFill() ? "; this graphics API's frames are written without it" : "") + + ")."); + } + edge_fill_degrees_ = edge_fill_degrees; // Just in time, the presenter thread keeps the mode it started with // (StayThreadedLocked): another one starts it again, after a fresh run of // steady frames, as turning Frame Generation off and on does. Frames @@ -1636,6 +1703,11 @@ void FrameGenPresenter::SetRuntimeViewConfiguration(XrViewConfigurationType type runtime_view_configuration_ = type; } +void FrameGenPresenter::SetRuntimeBlanksOpenFrames(bool blanks) { + std::scoped_lock lock(mutex_); + runtime_blanks_open_frames_ = blanks; +} + void FrameGenPresenter::SetInterpolateOnShaders(bool on) { std::scoped_lock lock(mutex_); interpolate_on_shaders_ = on; @@ -1830,6 +1902,10 @@ bool FrameGenPresenter::UpdateInterpolateFlowLocked(Clock::time_point now, Frame return true; } +void FrameGenPresenter::SetGameContextLock(GameContextLock* lock) { + game_context_lock_.store(lock, std::memory_order_release); +} + void FrameGenPresenter::SetEngagementPolicy(std::uint32_t steady_frames, std::chrono::milliseconds minimum_session_age) { std::scoped_lock lock(mutex_); engage_streak_frames_ = steady_frames; @@ -1892,6 +1968,11 @@ bool FrameGenPresenter::ResetFrames() { dropped_unbegun_ = 0; // A new session starts with no runtime frame in flight. runtime_waits_ = runtime_begins_ = runtime_ends_ = 0; + runtime_end_block_ms_ = runtime_end_margin_ms_ = 0.0; + runtime_end_block_samples_ = 0; + runtime_end_return_.reset(); + logged_runtime_end_hold_ = false; + summary_evidence_.reset(); presenter_frame_open_ = false; frames_.clear(); healthy_streak_ = 0; @@ -2242,8 +2323,10 @@ void FrameGenPresenter::CaptureOnRelease(XrSwapchain swapchain, std::uint32_t im return; } } - // Lock order everywhere: submit, queue, GPU, presenter. The copy may wait - // on a fence, so it runs without the presenter lock. + // Lock order everywhere: the game's Direct3D 11 device (in its own + // session; the layer's release and xrEndFrame take it before theirs, + // GameContextLock), submit, queue, GPU, presenter. The copy may wait on a + // fence, so it runs without the presenter lock. std::unique_lock queue_lock; if (queue_mutex_) { queue_lock = std::unique_lock(*queue_mutex_); @@ -2896,7 +2979,8 @@ void FrameGenPresenter::RecordCaptureTargetsLocked(const XrCompositionLayerProje color.rect = sub.imageRect; color.view = i; capture_targets_.push_back(color); - if (const auto* depth = DepthInfo(projection.views[i])) { + const auto* depth = settings_.use_game_depth ? DepthInfo(projection.views[i]) : nullptr; + if (depth) { CaptureTarget target; target.swapchain = depth->subImage.swapchain; target.array_index = depth->subImage.imageArrayIndex; @@ -3384,7 +3468,9 @@ std::string FrameGenPresenter::DynamicSummaryLocked() const { } // standDownHoldS: how long the next stand-down passes frames straight // through (doubling after short engagements, DynamicStandDownLocked). - return std::string(", dynamic=") + (dynamic_standby_ ? (dynamic_probing_ ? "probing" : "standby") : "engaged") + + return std::string(", dynamic=") + + (dynamic_standby_ ? (dynamic_probing_ ? "probing" : dynamic_rate_check_fps_ ? "checking" : "standby") + : "engaged") + (dynamic_reason_.empty() ? std::string() : " (" + dynamic_reason_ + ")") + ", standDownHoldS=" + Number(std::chrono::duration(dynamic_hold_).count(), 1) + ", standDowns=" + std::to_string(dynamic_stand_downs_); @@ -3743,6 +3829,33 @@ void FrameGenPresenter::UpdateDynamicLocked(Clock::time_point now, XrDuration pe return; } const double rate = RateSince(app_frames_, now, multiplier_changed_at_); + if (dynamic_rate_check_fps_) { + // (e)'s check: the game's rate standing by, against its rate + // engaged. Clearly faster standing by, engaging slowed it: a + // stand-down, held as any. Not, its scene slowed it: engaged + // again at once, the guard judging from its rate now. + if (now - multiplier_changed_at_ < policy.settle + policy.engage_after) { + return; + } + const double engaged = *dynamic_rate_check_fps_; + dynamic_rate_check_fps_.reset(); + if (rate > 0.0 && engaged < policy.game_rate_share * rate) { + DynamicStandDownLocked(now, + "the game ran at " + Number(engaged, 1) + " fps engaged against " + + Number(rate, 1) + " passing through", + false); + return; + } + if (logger_) { + logger_->Info("Frame Generation (dynamic): the game runs at " + Number(rate, 1) + + " fps passing through too, not slowed by engaging (" + Number(engaged, 1) + + " fps engaged)."); + } + if (rate > 0.0 && rate < policy.engage_below * refresh) { + DynamicEngageLocked(now, rate, refresh); + } + return; + } const double share = dynamic_probing_ ? policy.stay_above : policy.engage_below; if (rate > 0.0 && rate < share * refresh) { if (!dynamic_below_since_) { @@ -3803,8 +3916,16 @@ void FrameGenPresenter::UpdateDynamicLocked(Clock::time_point now, XrDuration pe const std::uint64_t pauses = totals_.pauses - start.pauses; std::vector capture_done = std::move(guard_capture_done_ms_); guard_capture_done_ms_.clear(); - NoteGpuEvidenceLocked(app_frames, totals_.late_gpu - start.late_gpu, std::move(capture_done)); const bool mostly_pauses = app_frames >= 4 && pauses * 2 >= app_frames; + // Nor is a window with a pause evidence for GPU-aware pacing, nor one a + // pause ends (judged at the frame after it): a GPU stall's frames before + // a pause are late and captured seconds after their refresh (DCS, + // 2026-10-22: "2444.7 ms after their refresh" armed it for the session). + const bool paused_after = + last_app_end_ && now - *last_app_end_ > std::chrono::nanoseconds(PauseLimitLocked()); + const bool clean = pauses == 0 && !paused_after && totals_.non_renderable == start.non_renderable && + totals_.unrendered == start.unrendered && totals_.restarts == start.restarts; + NoteGpuEvidenceLocked(nullptr, clean, {app_frames, totals_.late_gpu - start.late_gpu, std::move(capture_done)}); if ((pauses > 0 && !mostly_pauses) || totals_.non_renderable != start.non_renderable || totals_.unrendered != start.unrendered || totals_.restarts != start.restarts) { return; @@ -3825,12 +3946,18 @@ void FrameGenPresenter::UpdateDynamicLocked(Clock::time_point now, XrDuration pe // window that still shows more. (b), (c) and (d) then judge nothing; // probes check whether standing by would hold. const bool winning = passthrough_fps_ > 0.0 && distinct >= passthrough_fps_ + policy.distinct_margin_fps; - const std::array failing{ + // (e) judges winning engagements too: generated frames fill every + // refresh however slow the game's own frames become, so distinct frames + // alone cannot tell a game that engaging slowed (DCS through the + // Direct3D 11 bridge, 2026-10-22: 44 fps passing through, 17 engaged, + // still 90 distinct, engaged until Frame Generation was turned off). + const std::array failing{ passthrough_fps_ > 0.0 && !winning, !winning && generated < policy.generated_share * gap, !winning && refreshes > 0 && static_cast(unready) > policy.unready_share * static_cast(refreshes), !winning && timed > 0 && static_cast(late) > policy.late_analyses_share * static_cast(timed), + passthrough_fps_ > 0.0 && app_rate < policy.game_rate_share * passthrough_fps_, }; for (std::size_t i = 0; i < failing.size(); ++i) { guard_failing_[i] = failing[i] ? guard_failing_[i] + 1 : 0; @@ -3838,20 +3965,28 @@ void FrameGenPresenter::UpdateDynamicLocked(Clock::time_point now, XrDuration pe auto percent = [](std::uint64_t part, std::uint64_t whole) { return Number(whole ? 100.0 * static_cast(part) / static_cast(whole) : 0.0, 0) + "%"; }; - const std::array reasons{ + const std::array reasons{ "it showed " + Number(distinct, 0) + " distinct frames/s against " + Number(passthrough_fps_, 0) + " passing through", "it generated too few frames (" + Number(generated, 1) + "/s of a " + Number(gap, 1) + "/s gap)", "real frames reached the GPU too late (" + percent(unready, refreshes) + " of refreshes)", "motion was measured too late (" + percent(late, timed) + " of analyses)", + "the game ran at " + Number(app_rate, 1) + " fps engaged against " + Number(passthrough_fps_, 1) + + " passing through", }; // Two failing windows in a row: one can be a hitch. A cause failing now // (frames on the GPU too late, motion measured too late, too few - // generated) is named rather than its effect (no more distinct frames - // than the game alone). - const std::array causes{2, 3, 1}; + // generated, the game slowed) is named rather than its effect (no more + // distinct frames than the game alone). + const std::array causes{2, 3, 1, 4}; for (const std::size_t i : causes) { if (guard_failing_[i] >= 2) { + if (i == 4) { + // A heavier scene slows the game engaged or not: standing by + // tells which (UpdateDynamicLocked). + DynamicStandDownLocked(now, reasons[i], false, app_rate); + return; + } DynamicStandDownLocked(now, reasons[i], false); return; } @@ -3895,6 +4030,7 @@ void FrameGenPresenter::DynamicEngageLocked(Clock::time_point now, double rate, } guard_.valid = false; guard_failing_ = {}; + dynamic_rate_check_fps_.reset(); ++dynamic_engagements_; if (probe_failed) { dynamic_probing_ = false; @@ -3912,7 +4048,8 @@ void FrameGenPresenter::DynamicEngageLocked(Clock::time_point now, double rate, } } -void FrameGenPresenter::DynamicStandDownLocked(Clock::time_point now, const std::string& reason, bool probe) { +void FrameGenPresenter::DynamicStandDownLocked(Clock::time_point now, const std::string& reason, bool probe, + std::optional check_engaged_fps) { const FrameGenDynamicPolicy& policy = dynamic_policy_; dynamic_standby_ = true; dynamic_below_since_.reset(); @@ -3922,6 +4059,16 @@ void FrameGenPresenter::DynamicStandDownLocked(Clock::time_point now, const std: guard_.valid = false; guard_failing_ = {}; dynamic_reason_ = reason; + dynamic_rate_check_fps_ = check_engaged_fps; + if (check_engaged_fps) { + // No hold yet: standing by, the game's own rate decides (e). + dynamic_probing_ = false; + if (logger_) { + logger_->Info("Frame Generation (dynamic): " + reason + + "; frames pass straight through while it checks the game's rate standing by."); + } + return; + } if (probe) { // No hold: the probe engages again as soon as the game falls short. dynamic_probing_ = true; @@ -3959,6 +4106,7 @@ void FrameGenPresenter::DynamicReenableLocked(Clock::time_point now) { } dynamic_below_since_.reset(); dynamic_probing_ = false; + dynamic_rate_check_fps_.reset(); multiplier_changed_at_ = now; cadence_.clear(); guard_.valid = false; @@ -5466,6 +5614,11 @@ XrResult FrameGenPresenter::RunCycle(XrSession session, const XrFrameEndInfo& in generated_layer.layerFlags = flags; generated_layer.viewCount = static_cast(views.size()); generated_layer.views = views.data(); + // Not on an image widened by edge fill: the game's depth describes + // its own field of view. + for (std::size_t i = 0; depth && i < views.size() && i < projection.viewCount; ++i) { + depth = SameFov(views[i], projection.views[i]); + } if (depth && views.size() == projection.viewCount) { // The newest real frame's depth chain, valid during this end: its // depth image is the one the game just released. @@ -5964,7 +6117,11 @@ void FrameGenPresenter::NoteGpuTimingLocked(std::uint64_t submission, const Pend if (timing.capture_end != Clock::time_point{}) { const double done = Milliseconds(timing.capture_end - due->real_due); AddSample(capture_done_ms_, done); - AddSample(guard_capture_done_ms_, done); + // Later than a pause, a GPU stall: not a game running frames + // ahead, so no evidence for GPU-aware pacing (NoteGpuEvidenceLocked). + if (done <= static_cast(PauseLimitLocked()) / 1e6) { + AddSample(guard_capture_done_ms_, done); + } if (due->capture_pending_from != Clock::time_point{}) { auto_capture_pending_ms_[auto_capture_pending_count_++ % auto_capture_pending_ms_.size()] = std::max(0.0, Milliseconds(timing.capture_end - due->capture_pending_from)); @@ -6228,7 +6385,7 @@ bool FrameGenPresenter::Generate(const XrCompositionLayerProjection& projection, if (i < depth_decoded_.size()) { decoded = std::exchange(depth_decoded_[i], std::nullopt); } - if (const auto* depth = DepthInfo(view)) { + if (const auto* depth = settings_.use_game_depth ? DepthInfo(view) : nullptr) { const auto depth_record = swapchains_.find(depth->subImage.swapchain); submission.request.depth = depth_record != swapchains_.end() && depth_record->second.depth_capturable; @@ -6267,8 +6424,12 @@ bool FrameGenPresenter::Generate(const XrCompositionLayerProjection& projection, EngineDeltaLocked(frame, submissions); } std::vector specs(projection.viewCount); - for (std::uint32_t i = 0; i < projection.viewCount; ++i) { - specs[i] = {Extent(projection.views[i]), kinds[i]}; + { + std::scoped_lock lock(mutex_); + for (std::uint32_t i = 0; i < projection.viewCount; ++i) { + const FrameGenExtent extent = Extent(projection.views[i]); + specs[i] = {extent, kinds[i], EdgeMarginsLocked(projection.views[i].fov, extent)}; + } } if (count && !EnsureOutputs(specs, count, error)) { std::scoped_lock lock(mutex_); @@ -6291,6 +6452,7 @@ bool FrameGenPresenter::Generate(const XrCompositionLayerProjection& projection, FrameGenExecuteResult executed; bool ok = true; + bool edges_filled = false; // FrameGenGpu::EdgesFilled after Execute double gpu_ms = -1.0; std::uint64_t working_bytes = 0; double execute_ms = 0.0; // the backend's Execute, on this thread @@ -6300,6 +6462,12 @@ bool FrameGenPresenter::Generate(const XrCompositionLayerProjection& projection, FrameGenWorldDeltaStatus world_delta; std::string hardware_status; { + // The game's Direct3D 11 context (the game's own Direct3D 11 + // session): its device's lock before any of these (GameContextLock). + GameContextHold context_hold; + if (GameContextLock* context_lock = game_context_lock_.load(std::memory_order_acquire)) { + context_hold = context_lock->Hold(); + } // Vulkan queues need external synchronization with the runtime's own // submissions on the session queue. Lock order: queue, GPU, // presenter; the backend may wait on fences, so not under mutex_. @@ -6315,10 +6483,7 @@ bool FrameGenPresenter::Generate(const XrCompositionLayerProjection& projection, for (std::size_t set = 0; set < count && ok; ++set) { const Output& output = output_sets_[set][i]; submissions[i].request.output = output.kind; - submissions[i].outputs.push_back({output.swapchain, output.index, 0, - {{0, 0}, {static_cast(output.extent.width), - static_cast(output.extent.height)}}, - output.format}); + submissions[i].outputs.push_back(OutputImage(output)); ok = gpu_->Track(output.swapchain, xr_.images, error); } } @@ -6333,6 +6498,7 @@ bool FrameGenPresenter::Generate(const XrCompositionLayerProjection& projection, } const auto execute_start = Clock::now(); ok = ok && gpu_->Execute(submissions, tuning, executed, error); + edges_filled = ok && gpu_ && gpu_->EdgesFilled(); execute_ms = Milliseconds(Clock::now() - execute_start); if (gpu_) { gpu_ms = gpu_->LastGpuMilliseconds(); @@ -6593,9 +6759,7 @@ bool FrameGenPresenter::Generate(const XrCompositionLayerProjection& projection, views.assign(projection.views, projection.views + projection.viewCount); for (std::size_t i = 0; i < views.size(); ++i) { views[i].next = nullptr; // a generated image has no matching depth - views[i].subImage.swapchain = output_sets_[set][i].swapchain; - views[i].subImage.imageRect = submissions[i].outputs[set].rect; - views[i].subImage.imageArrayIndex = 0; + ShowOutput(views[i], output_sets_[set][i], edges_filled); if (i < executed.depth.size() && executed.depth[i]) { // Seen from where the eye will be (FrameGenViewRequest::shift); // the runtime still corrects the rotation. @@ -6829,6 +6993,43 @@ void FrameGenPresenter::WriteCaptureNotes(const std::string& directory, const Ca } } +FrameGenEdgeMargins FrameGenPresenter::EdgeMarginsLocked(const XrFovf& fov, FrameGenExtent extent) const { + if (!settings_.edge_fill || !gpu_ || !gpu_->SupportsEdgeFill()) { + return {}; + } + return FrameGenEdgeFillMargins(Fov(fov), extent, edge_fill_degrees_[0], edge_fill_degrees_[1]); +} + +FrameGenImage FrameGenPresenter::OutputImage(const Output& output) { + FrameGenImage image{output.swapchain, output.index, 0, + {{0, 0}, {static_cast(output.extent.width), static_cast(output.extent.height)}}, + output.format}; + image.content = {static_cast(output.margins.left), static_cast(output.margins.up)}; + return image; +} + +void FrameGenPresenter::ShowOutput(XrCompositionLayerProjectionView& view, const Output& output, bool filled) { + view.subImage.swapchain = output.swapchain; + view.subImage.imageArrayIndex = 0; + view.subImage.imageRect = {{0, 0}, {static_cast(output.extent.width), + static_cast(output.extent.height)}}; + if (output.margins.Any() && !filled) { + // The backend copied the frame in (FrameGenGpu::EdgesFilled): its + // margin holds whatever it held, so only the frame's own region is + // shown, with the game's field of view. + view.subImage.imageRect = {{static_cast(output.margins.left), static_cast(output.margins.up)}, + {static_cast(output.extent.width - output.margins.left - output.margins.right), + static_cast(output.extent.height - output.margins.up - output.margins.down)}}; + } else if (output.margins.Any()) { + // The image's own projection: the frame's tangents carried on across + // the margins, which the runtime turns into view on a fast turn. + const FrameGenExtent frame{output.extent.width - output.margins.left - output.margins.right, + output.extent.height - output.margins.up - output.margins.down}; + const FrameGenFov widened = FrameGenEdgeFillFov(Fov(view.fov), frame, output.margins); + view.fov = {widened.left, widened.right, widened.up, widened.down}; + } +} + bool FrameGenPresenter::EnsureOutputs(const std::vector& specs, std::size_t sets, std::string& error, bool keep_replaced) { const std::uint32_t count = static_cast(specs.size()); @@ -6876,10 +7077,37 @@ bool FrameGenPresenter::EnsureOutputs(const std::vector& specs, std: for (std::size_t set = 0; set < sets; ++set) { for (std::uint32_t i = 0; i < count; ++i) { Output& output = output_sets_[set][i]; - const FrameGenExtent extent = specs[i].extent; + // With edge fill, the frame and its margins. Margins a little + // wider than asked keep their images: a field of view that wavers + // (a runtime's per-frame eye poses) never makes them anew. + FrameGenEdgeMargins margins = specs[i].margins; + const auto close = [](std::uint32_t kept, std::uint32_t asked) { + return kept >= asked && kept <= asked + kEdgeFillSlack; + }; + if (output.swapchain && margins.Any() && output.margins.Any() && + output.extent.width == specs[i].extent.width + output.margins.left + output.margins.right && + output.extent.height == specs[i].extent.height + output.margins.up + output.margins.down && + close(output.margins.left, margins.left) && close(output.margins.right, margins.right) && + close(output.margins.up, margins.up) && close(output.margins.down, margins.down)) { + margins = output.margins; + } else if (margins.Any()) { + // Made anew: each margin rounded up to whole tiles + // (kEdgeFillRound), within the largest image. + const auto round = [](std::uint32_t margin, std::uint32_t frame, std::uint32_t other) { + const std::uint32_t rounded = + margin ? (margin + kEdgeFillRound - 1) / kEdgeFillRound * kEdgeFillRound : 0u; + return frame + other + rounded <= kFrameGenEdgeFillMaxExtent ? rounded : margin; + }; + margins.left = round(margins.left, specs[i].extent.width, margins.right); + margins.right = round(margins.right, specs[i].extent.width, margins.left); + margins.up = round(margins.up, specs[i].extent.height, margins.down); + margins.down = round(margins.down, specs[i].extent.height, margins.up); + } + const FrameGenExtent extent{specs[i].extent.width + margins.left + margins.right, + specs[i].extent.height + margins.up + margins.down}; const FrameGenOutputKind kind = specs[i].kind; if (output.swapchain && output.extent.width == extent.width && output.extent.height == extent.height && - output.requested == kind) { + output.margins == margins && output.requested == kind) { continue; } if (output.swapchain) { @@ -6939,6 +7167,7 @@ bool FrameGenPresenter::EnsureOutputs(const std::vector& specs, std: } std::scoped_lock lock(mutex_); output.extent = extent; + output.margins = margins; output.format = format; output.requested = kind; // An sRGB fallback for a UNORM or float source must also encode. @@ -6989,7 +7218,21 @@ bool FrameGenPresenter::EnsureOutputs(const std::vector& specs, std: ", imagesPerSwapchain=" + std::to_string(output_sets_.front().front().image_count) + ", estimated video memory " + Number((working + swapchains) / megabytes, 0) + " MB (working " + Number(working / megabytes, 0) + " MB" + (working_bytes_ ? "" : " estimated") + - ", output swapchains " + Number(swapchains / megabytes, 0) + " MB)."); + ", output swapchains " + Number(swapchains / megabytes, 0) + " MB)" + + [&] { + // Edge fill: each view's margins (left/right/up/down pixels). + std::string fill; + for (const Output& output : output_sets_.front()) { + if (!output.margins.Any()) continue; + fill += std::string(fill.empty() ? ", edge fill margins " : "; ") + + std::to_string(output.margins.left) + "/" + std::to_string(output.margins.right) + + "/" + std::to_string(output.margins.up) + "/" + + std::to_string(output.margins.down) + " px of " + + std::to_string(output.extent.width) + "x" + std::to_string(output.extent.height); + } + return fill; + }() + + "."); LogMemoryBudgetLocked(); } } @@ -7200,7 +7443,8 @@ bool FrameGenPresenter::ColorReleasedSinceLocked(const FrameCopy& copy) const { return false; } -FrameGenPresenter::AsIs FrameGenPresenter::CurrentAsIsLocked(const FrameCopy& copy, std::uint64_t id, XrTime time) const { +FrameGenPresenter::AsIs FrameGenPresenter::CurrentAsIsLocked(const FrameCopy& copy, std::uint64_t id, XrTime time, + bool past_written) const { // A copy without its projection (a swapchain or space it used was // destroyed) shows nothing of the scene either. const auto current = [&](const FrameCopy& candidate) { @@ -7219,6 +7463,7 @@ FrameGenPresenter::AsIs FrameGenPresenter::CurrentAsIsLocked(const FrameCopy& co continue; } if (!frame.as_is) { + if (past_written) continue; return {}; } if (current(frame.copy)) { @@ -7332,10 +7577,15 @@ XrFrameEndInfo FrameGenPresenter::BuildFrame(FrameCopy& copy, const std::vector< // position, so such a runtime only rotates them. An exact // copy of the real frame keeps it (while the game has not // released a newer depth image: DropStaleDepthLocked). + // Nor on an image widened by edge fill (SameFov). + bool depth = generated_depth && layer.views.size() == generated->size(); + for (std::size_t view = 0; depth && view < layer.views.size(); ++view) { + depth = SameFov(layer.views[view], (*generated)[view]); + } layer.views = *generated; layer.depth.resize(layer.views.size(), XrCompositionLayerDepthInfoKHR{XR_TYPE_COMPOSITION_LAYER_DEPTH_INFO_KHR}); layer.has_depth.resize(layer.views.size(), 0); - if (!generated_depth) { + if (!depth) { std::fill(layer.has_depth.begin(), layer.has_depth.end(), std::uint8_t{0}); } layer.projection.space = generated_space; @@ -7391,13 +7641,20 @@ XrFrameEndInfo FrameGenPresenter::BuildFrame(FrameCopy& copy, const std::vector< XrResult FrameGenPresenter::RuntimeWait(const FrameGenRuntimeCalls& calls, XrSession session, const XrFrameWaitInfo* info, XrFrameState* state) { XrResult result = XR_SUCCESS; + const auto called = Clock::now(); { const PresenterCall call(*this, "xrWaitFrame"); result = calls.wait(session, info, state); } if (XR_SUCCEEDED(result)) { + const double call_ms = Milliseconds(Clock::now() - called); std::scoped_lock lock(mutex_); ++runtime_waits_; + AddSample(runtime_wait_ms_, call_ms); + // Blocked as NotePhaseLocked counts it: by the period this wait reported. + const double period_ms = state ? static_cast(state->predictedDisplayPeriod) / 1e6 : 0.0; + ++totals_.runtime_waits_timed; + totals_.runtime_waits_blocked += call_ms >= std::max(1.0, period_ms / 10.0) ? 1 : 0; } return result; } @@ -7443,13 +7700,69 @@ XrResult FrameGenPresenter::RuntimeEnd(const FrameGenRuntimeCalls& calls, XrSess } } const auto entered = Clock::now(); + // A runtime holding frames in xrEndFrame until its slot holds its + // swapchain lock meanwhile (PimaxXR): the frame goes shortly before the + // slot instead, the runtime's next after its last return. + std::optional call_at; + double period_ms = 0.0; + { + std::scoped_lock lock(mutex_); + period_ms = static_cast(last_period_) / 1e6; + if (runtime_end_hold_ && paced && period_ms > 0.0 && runtime_end_return_ && + runtime_end_block_samples_ >= kEndPacedSamples && + runtime_end_block_ms_ >= kEndPacedShare * period_ms && + runtime_end_margin_ms_ < kEndMarginCeiling * period_ms) { + const auto period = std::chrono::nanoseconds(last_period_); + auto slot = *runtime_end_return_ + period; + while (slot <= entered) slot += period; + call_at = slot - std::chrono::duration_cast( + std::chrono::duration(std::max(runtime_end_margin_ms_, kEndMarginMs))); + if (!logged_runtime_end_hold_ && logger_) { + logged_runtime_end_hold_ = true; + logger_->Info("Frame Generation: the runtime holds frames in xrEndFrame until its refresh (" + + Number(runtime_end_block_ms_, 1) + " ms of " + Number(period_ms, 1) + + "), and its swapchain calls wait for it meanwhile (PimaxXR with \"Prefer framerate " + "over latency\"): frames now go shortly before the refresh, so the game's swapchain " + "calls are not held (VECTORXR_FRAMEGEN_END_HOLD=off turns this off)."); + } + } + } + const bool held = call_at && *call_at > entered; + if (held) { + thread_local struct EndTimer { + HANDLE handle = CreateWaitableTimerExW(nullptr, nullptr, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS); + ~EndTimer() { + if (handle) CloseHandle(handle); + } + } timer; + SleepUntil(timer.handle, *call_at); + } + const auto called = Clock::now(); { const PresenterCall call(*this, "xrEndFrame"); result = calls.submit(session, info); } { std::scoped_lock lock(mutex_); - AddSample(runtime_end_ms_, Milliseconds(Clock::now() - entered)); + const auto returned = Clock::now(); + const double call_ms = Milliseconds(returned - called); + AddSample(runtime_end_ms_, call_ms); + runtime_end_return_ = returned; + if (held) { + AddSample(runtime_end_hold_ms_, Milliseconds(called - entered)); + // Returned at once: the slot came before the frame (or the + // runtime no longer holds frames), so frames go earlier; else + // back towards the margin. + runtime_end_margin_ms_ = call_ms < 0.5 ? runtime_end_margin_ms_ * 1.5 + 0.5 + : std::max(kEndMarginMs, runtime_end_margin_ms_ * 0.98); + } else if (paced && !call_at) { + runtime_end_block_ms_ = + runtime_end_block_samples_ == 0 ? call_ms : runtime_end_block_ms_ * 0.9 + call_ms * 0.1; + if (runtime_end_block_samples_ < kEndPacedSamples) ++runtime_end_block_samples_; + // Made at once (the margin grew to a refresh) and held to the slot + // again: the slot is where it was. + if (period_ms > 0.0 && call_ms >= kEndPacedShare * period_ms) runtime_end_margin_ms_ = kEndMarginMs; + } // Counted even when the runtime rejects the frame: it is over either way. if (runtime_ends_ < runtime_begins_) { ++runtime_ends_; @@ -7473,10 +7786,22 @@ void FrameGenPresenter::NoteDisplayOffsetLocked(XrTime submitted, XrTime returne if (period <= 0 || returned <= 0 || submitted <= 0) { return; } - const double sample = static_cast(submitted - returned) / static_cast(period); + double sample = static_cast(submitted - returned) / static_cast(period); if (sample < -2.0 || sample > 3.0) { return; // not a convention, just a stray time } + if (sample < 0.0) { + // Ended for an earlier time than it was given: a layer above scaling + // the prediction down (OpenXR Toolkit's prediction dampening, which + // never restores the time it changed). Telling engaged frames a later + // time would undo what the user set there: counted as no offset. + if (++display_offset_earlier_ == 30 && logger_) { + logger_->Info("Frame Generation: the game ends frames for earlier times than it was given (" + + Number(sample, 2) + " periods; a layer above VectorXR scales its predictions down, as " + "OpenXR Toolkit's prediction dampening does); engaged frames keep the times they are given."); + } + sample = 0.0; + } display_offset_periods_ = display_offset_samples_ == 0 ? sample : display_offset_periods_ * 0.9 + sample * 0.1; if (display_offset_samples_ < 1000000) { @@ -8171,17 +8496,29 @@ void FrameGenPresenter::NoteGpuWaitLocked(double waited_ms, bool reached, bool p } } -void FrameGenPresenter::NoteGpuEvidenceLocked(std::uint64_t real_frames, std::uint64_t late_gpu, - std::vector capture_done_ms) { - if (gpu_gate_armed_ || !gpu_gate_enabled_) { +void FrameGenPresenter::NoteGpuEvidenceLocked(std::optional* pending, bool clean, GpuEvidence window) { + // With `pending`, a window is taken once the next is clean too (a + // summary's ends before the pause that follows a stall shows). + std::optional taken; + if (!pending) { + if (clean) taken = std::move(window); + } else if (clean) { + taken = std::exchange(*pending, std::move(window)); + } else { + pending->reset(); + } + if (!taken || gpu_gate_armed_ || !gpu_gate_enabled_) { return; } - const std::optional capture_done = Median(std::move(capture_done_ms)); - const bool late_frames = real_frames > 0 && late_gpu * 100 > real_frames; + // Late frames say the game runs ahead on its GPU only while that GPU + // finishes them: a stalled one (a hitch) finishes none in the window. + const bool finishing = taken->capture_done_ms.size() * 2 >= taken->real_frames; + const std::optional capture_done = Median(std::move(taken->capture_done_ms)); + const bool late_frames = taken->real_frames > 0 && taken->late_gpu * 100 > taken->real_frames && finishing; if (!late_frames && !(capture_done && *capture_done > 0.0)) { return; } - ArmGpuGateLocked(late_frames ? std::to_string(late_gpu) + " of " + std::to_string(real_frames) + + ArmGpuGateLocked(late_frames ? std::to_string(taken->late_gpu) + " of " + std::to_string(taken->real_frames) + " real frames were not on the GPU when first needed" : "its frames were captured " + Number(*capture_done, 1) + " ms after their refresh, typically"); @@ -8594,7 +8931,11 @@ XrResult FrameGenPresenter::ThreadedEnd(XrSession session, const XrFrameEndInfo& FindProjection(info, &projection_index); layers.assign(info.layers, info.layers + info.layerCount); generated_views = between[skip]; - const bool depth = skip < between_exact.size() && between_exact[skip]; + bool depth = skip < between_exact.size() && between_exact[skip]; + // Not on an image widened by edge fill (SameFov). + for (std::size_t i = 0; depth && i < generated_views.size() && i < projection->viewCount; ++i) { + depth = SameFov(generated_views[i], projection->views[i]); + } for (std::size_t i = 0; i < generated_views.size() && i < projection->viewCount; ++i) { generated_views[i].next = depth ? projection->views[i].next : nullptr; } @@ -8780,7 +9121,21 @@ void FrameGenPresenter::SummaryLocked(Clock::time_point now) { const Totals& start = summary_totals_; const auto delta = [&](std::uint64_t Totals::*field) { return totals_.*field - start.*field; }; const double standby = StandbySecondsLocked(now); - NoteGpuEvidenceLocked(delta(&Totals::app_frames), delta(&Totals::late_gpu), capture_done_ms_); + // Evidence for GPU-aware pacing as the benefit guard takes it: none from + // a window with a pause, a frame without a projection, an unrendered + // refresh or a restart, nor a capture later than a pause (a GPU stall's, + // DCS 2026-10-22: "captured 2444.7 ms after their refresh" armed it for + // the session from this window). Thirty seconds of it are enough. + { + const double pause_ms = static_cast(PauseLimitLocked()) / 1e6; + GpuEvidence evidence{delta(&Totals::app_frames), delta(&Totals::late_gpu), {}}; + std::copy_if(capture_done_ms_.begin(), capture_done_ms_.end(), std::back_inserter(evidence.capture_done_ms), + [&](double done) { return done <= pause_ms; }); + NoteGpuEvidenceLocked(&summary_evidence_, + delta(&Totals::pauses) == 0 && delta(&Totals::non_renderable) == 0 && + delta(&Totals::unrendered) == 0 && delta(&Totals::restarts) == 0, + std::move(evidence)); + } if (logger_) { LogMemoryBudgetLocked(); // A fixed or Auto rate is the most the game is let run: a slower @@ -8803,11 +9158,18 @@ void FrameGenPresenter::SummaryLocked(Clock::time_point now) { // nothing to write. endCallMs: the game's xrEndFrame in Frame // Generation. runtimeEndMs: the runtime's xrEndFrame for the frames // Frame Generation submits (long where the runtime waits for its - // queue). synthLockMs: the presenter thread's wait for the GPU + // queue). runtimeEndHeldMs: frames held back before it, for a + // runtime that holds them there until its slot. runtimeWaitMs: the + // runtime's xrWaitFrame (every caller's), and runtimeWaitsBlocked: + // of those waits, the ones that blocked (none: a runtime pacing in + // xrEndFrame instead, as PimaxXR does with "Prefer framerate over + // latency"; few: a caller coming late). synthLockMs: the presenter thread's wait for the GPU // lock before writing. ringExhausted: refreshes whose real frame's // images newer captures had replaced (written unmoved, or the frame // before). staleSkipped: refreshes not submitted, nothing current to // show (a frame shown as it is had newer images in its swapchains). + // staleShownAhead (when any): such refreshes shown with a later frame + // as it is instead, for a runtime blanking frames left open. // leadAvg/leadMax: the lead told to the game's frames; leadHist: how // many frames were told each lead ("lead:frames"). toldAheadMs // (median/90th percentile): gameAheadMs measured from the presenter @@ -8914,6 +9276,10 @@ void FrameGenPresenter::SummaryLocked(Clock::time_point now) { std::to_string(delta(&Totals::gpu_wait_timeouts)) + (gpu_gate_enabled_ && gpu_gate_armed_ ? "" : gpu_gate_enabled_ ? " (not armed)" : " (off)") + ", endCallMs=" + MedianAndMax(end_call_ms_) + ", runtimeEndMs=" + MedianAndMax(runtime_end_ms_) + + ", runtimeEndHeldMs=" + MedianAndMax(runtime_end_hold_ms_) + + ", runtimeWaitMs=" + MedianAndMax(runtime_wait_ms_) + + ", runtimeWaitsBlocked=" + std::to_string(delta(&Totals::runtime_waits_blocked)) + "/" + + std::to_string(delta(&Totals::runtime_waits_timed)) + ", synthLockMs=" + MedianAndMax(synth_lock_ms_) + (jit_ ? ", justInTime=1, covers=" + std::to_string(jit_covers_) + ", unwritten=" + std::to_string(delta(&Totals::unwritten_wait)) + "/" + @@ -8925,7 +9291,10 @@ void FrameGenPresenter::SummaryLocked(Clock::time_point now) { ", staleMotion=" + std::to_string(delta(&Totals::stale_motion)) + ", ring=" + std::to_string(JitRingLocked(quad_mode_)) + ", ringExhausted=" + std::to_string(delta(&Totals::ring_exhausted)) + ring_text + - ", staleSkipped=" + std::to_string(delta(&Totals::stale_skipped)) + ", staleSkipped=" + std::to_string(delta(&Totals::stale_skipped)) + + (delta(&Totals::stale_shown_ahead) + ? ", staleShownAhead=" + std::to_string(delta(&Totals::stale_shown_ahead)) + : std::string()) : std::string()) + ", runtimeHalved=" + std::to_string(delta(&Totals::halving_halved)) + "/" + std::to_string(delta(&Totals::halving_measured)) + (runtime_halving_ ? " (halving)" : "") + @@ -8960,6 +9329,8 @@ void FrameGenPresenter::NewSummaryWindowLocked(Clock::time_point now, double sta gpu_wait_ms_.clear(); end_call_ms_.clear(); runtime_end_ms_.clear(); + runtime_end_hold_ms_.clear(); + runtime_wait_ms_.clear(); synth_lock_ms_.clear(); ring_short_write_ms_.clear(); told_lead_sum_ = 0; @@ -9568,15 +9939,20 @@ bool FrameGenPresenter::SynthesizeJit(std::optional& source, JitChoice if (composition.eyes.size() != quad->eyes.size()) { return false; } - for (const FrameGenComposeEye& eye : composition.eyes) { + std::scoped_lock lock(mutex_); + for (std::size_t e = 0; e < composition.eyes.size(); ++e) { + const FrameGenComposeEye& eye = composition.eyes[e]; if (eye.peripheral >= frame.kinds.size() || eye.focus >= frame.kinds.size()) { return false; } - specs.push_back({composition.output, frame.kinds[eye.focus]}); + specs.push_back({composition.output, frame.kinds[eye.focus], + EdgeMarginsLocked(quad->eyes[e].fov, composition.output)}); } } else { + std::scoped_lock lock(mutex_); for (std::size_t i = 0; i < layer.views.size(); ++i) { - specs.push_back({Extent(layer.views[i]), frame.kinds[i]}); + const FrameGenExtent extent = Extent(layer.views[i]); + specs.push_back({extent, frame.kinds[i], EdgeMarginsLocked(layer.views[i].fov, extent)}); } } std::string error; @@ -9646,11 +10022,10 @@ bool FrameGenPresenter::SynthesizeJit(std::optional& source, JitChoice } output.waited = true; } - images.push_back({output.swapchain, output.index, 0, - {{0, 0}, {static_cast(output.extent.width), static_cast(output.extent.height)}}, - output.format}); + images.push_back(OutputImage(output)); } bool ok = false; + bool edges_filled = false; // FrameGenGpu::EdgesFilled after the write { const auto asked = Clock::now(); std::scoped_lock gpu_lock(gpu_mutex_); @@ -9698,6 +10073,8 @@ bool FrameGenPresenter::SynthesizeJit(std::optional& source, JitChoice quad->composition, images, error) : gpu_->Synthesize(frame.analyzed, choice.mode, choice.fraction, images, error)); } + // Edge fill: a frame the backend copied in has no margin drawn. + edges_filled = ok && gpu_ && gpu_->EdgesFilled(); } const bool extrapolate = choice.mode == FrameGenSynthesisMode::kExtrapolate; if (!ok) { @@ -9736,10 +10113,7 @@ bool FrameGenPresenter::SynthesizeJit(std::optional& source, JitChoice for (std::size_t i = 0; i < views.size(); ++i) { const Output& output = output_sets_.front()[i]; views[i].next = nullptr; - views[i].subImage.swapchain = output.swapchain; - views[i].subImage.imageArrayIndex = 0; - views[i].subImage.imageRect = {{0, 0}, {static_cast(output.extent.width), - static_cast(output.extent.height)}}; + ShowOutput(views[i], output, edges_filled); const std::size_t moved_as = quad ? quad->composition.eyes[i].peripheral : i; if (eye_time != 0.0f && moved_as < frame.velocity.size()) { const std::array& velocity = frame.velocity[moved_as]; @@ -10096,8 +10470,25 @@ void FrameGenPresenter::CompositorLoop() { // images are still the newest in their swapchains, else a newer // frame the layer composed, early, else nothing. What is shown is // what a refresh nothing is written for shows again. + // A runtime that shows a blank refresh for a frame left open + // (SetRuntimeBlanksOpenFrames) gets the next frame the layer + // composed, past frames written first, while one is current: its + // own images and views, early (a written frame shown after it + // steps back a frame, which beats a blank refresh). const auto show_as_is = [&](FrameCopy copy, std::uint64_t id, XrTime made_for) { - const AsIs current = CurrentAsIsLocked(copy, id, made_for); + AsIs current = CurrentAsIsLocked(copy, id, made_for); + if (!current.copy && runtime_blanks_open_frames_) { + current = CurrentAsIsLocked(copy, id, made_for, true); + if (current.copy) { + ++totals_.stale_shown_ahead; + if (logger_ && !logged_open_frame_shown_) { + logged_open_frame_shown_ = true; + logger_->Info("Frame Generation: a refresh with nothing current to show goes with the next " + "frame shown as it is, early: this runtime shows a blank refresh for a frame " + "left open (PimaxXR 0.4.4)."); + } + } + } if (!current.copy) { submit = false; ++totals_.stale_skipped; @@ -10179,10 +10570,16 @@ void FrameGenPresenter::CompositorLoop() { } else { // Composing quad views, with nothing the layer composed since // the last frame written: that frame again, as the runtime - // shows it. + // shows it (a runtime blanking frames left open: a later + // frame shown as it is, as above, if any). path = 8; - submit = false; - ++totals_.stale_skipped; + if (runtime_blanks_open_frames_) { + const auto shown = jit_shown_ ? find_kept(std::get<0>(*jit_shown_)) : jit_frames_.end(); + show_as_is(FrameCopy{}, 0, shown != jit_frames_.end() ? shown->time : jit_content_time_); + } else { + submit = false; + ++totals_.stale_skipped; + } } DropStaleDepthLocked(content); const bool usable = content.valid && content.has_projection && content.projection_index < content.layers.size(); @@ -10466,6 +10863,7 @@ FrameGenStatus FrameGenPresenter::Status() const { totals_.unwritten_source; status.ring_exhausted = totals_.ring_exhausted; status.stale_skipped = totals_.stale_skipped; + status.stale_shown_ahead = totals_.stale_shown_ahead; status.analyses_timed = totals_.analyses_timed; status.analyses_late = totals_.analyses_late; status.quad = quad_mode_ && compositor_running_; @@ -10486,8 +10884,11 @@ FrameGenStatus FrameGenPresenter::Status() const { ? FrameGenDepthState::kEmpty : FrameGenDepthState::kUnusable) : FrameGenDepthState::kInUse; + status.depth_uniform = depth_sent_ && depth_readable_ && evidence.binocular > 0.0 && + evidence.source_varied == 0.0 && evidence.other_varied == 0.0; } return status; + } FrameGenOsdText DescribeFrameGen(const FrameGenStatus& status, bool started) { diff --git a/layer/src/framegen/framegen_runtime_queue.cpp b/layer/src/framegen/framegen_runtime_queue.cpp index 5185366c..45d9980c 100644 --- a/layer/src/framegen/framegen_runtime_queue.cpp +++ b/layer/src/framegen/framegen_runtime_queue.cpp @@ -26,7 +26,8 @@ using Microsoft::WRL::ComPtr; namespace { // A held-back wait reaches the runtime once the release before it has. The -// runtime shows none of the swapchain's images by then, so it returns at +// runtime shows none of the swapchain's images by then (of a layer it shows, +// not the image waited for: it has a newer release, Wait), so it returns at // once; bounded all the same, retried at the next call when it is not. constexpr XrDuration kHeldBackWaitTimeout = 1'000'000'000; constexpr DWORD kReleaseWaitMs = 100; @@ -47,6 +48,12 @@ struct FrameGenQueueSubstitution::Impl { int failures{0}; // Shown layers are lazy (Prepare), and what that did (ShownLayerCounts). bool shown_layers_lazy{false}; + // VECTORXR_FRAMEGEN_AFTER_WAIT=off: the game's queue does not wait for the + // runtime's after its image waits (research). + bool after_wait{true}; + // SetRuntimeQueuesOnlyFirstUse. + bool only_first_use{false}; + std::atomic unordered_waits{0}; // waits it left unordered std::atomic shown_releases{0}; std::atomic shown_cpu_waits{0}; std::atomic shown_gpu_waits{0}; @@ -68,6 +75,16 @@ struct FrameGenQueueSubstitution::Impl { std::uint32_t direct_waits{0}; // waits not held back: an image was shown since bool shown_lazily{false}; // the frame showing it last did so lazily (Flush) bool runtime_released{false}; // the runtime got a release of it + // Which image the runtime shows (a layer it shows lazily, Wait): + // the images it handed out and has no release of yet (in its order), + // those the application has not waited for, the runtime's releases + // so far, and the one (counted so) that last released each image. + std::deque runtime_acquired; + std::deque unwaited; + std::uint64_t forwarded{0}; + std::vector released_as; + // Images whose first wait ordered the game's queue (AfterWait; under `mutex`). + std::vector ordered; HANDLE event{nullptr}; ~Swapchain() { if (event) CloseHandle(event); @@ -137,7 +154,7 @@ struct FrameGenQueueSubstitution::Impl { } XrSwapchainImageReleaseInfo info{XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO}; Note(next.release(swapchain, &info), "release"); - state.runtime_released = true; + Released(state); state.held_back.pop_front(); --state.releases; if (until == Until::kOldest) break; @@ -145,6 +162,27 @@ struct FrameGenQueueSubstitution::Impl { return true; } + // The runtime got a release (holding `state.calls`): of the image it + // handed out earliest. + static constexpr std::uint32_t kUnknownImage = ~std::uint32_t{0}; + static void Released(Swapchain& state) { + state.runtime_released = true; + ++state.forwarded; + if (state.runtime_acquired.empty()) return; + const std::uint32_t image = state.runtime_acquired.front(); + state.runtime_acquired.pop_front(); + if (image == kUnknownImage) return; + if (image >= state.released_as.size()) state.released_as.resize(image + 1); + state.released_as[image] = state.forwarded; + } + + // Whether `image` is the newest the runtime has of the swapchain (holding + // `state.calls`): a frame showing the layer shows it. Unknown: as if so. + static bool NewestShown(const Swapchain& state, std::uint32_t image) { + return image == kUnknownImage || (image < state.released_as.size() && state.released_as[image] != 0 && + state.released_as[image] == state.forwarded); + } + // Held-back releases whose work has not run (holding `state.calls`). std::uint64_t Pending(const Swapchain& state) { const UINT64 done = released->GetCompletedValue(); @@ -154,9 +192,20 @@ struct FrameGenQueueSubstitution::Impl { } // After the runtime handed an image back: anything it queued for the - // image runs before the game's rendering into it. - void AfterWait() { + // image runs before the game's rendering into it. A runtime that queues + // work for an image only before its first wait (only_first_use): only + // then (and for an image not known). + void AfterWait(Swapchain* state, std::uint32_t image) { + if (!after_wait) return; std::scoped_lock lock(mutex); + if (only_first_use && state && image != kUnknownImage) { + if (image >= state->ordered.size()) state->ordered.resize(image + 1, false); + if (state->ordered[image]) { + ++unordered_waits; + return; + } + state->ordered[image] = true; + } const UINT64 value = ++handed_back_value; runtime->Signal(handed_back.Get(), value); game->Wait(handed_back.Get(), value); @@ -227,6 +276,8 @@ const XrSessionCreateInfo* FrameGenQueueSubstitution::Prepare(const XrSessionCre impl->log = log_; const std::string shown_choice = shown_layers ? shown_layers : ""; impl->shown_layers_lazy = shown_choice == "lazy" || (shown_layers_lazy && shown_choice != "off"); + const char* after_wait = std::getenv("VECTORXR_FRAMEGEN_AFTER_WAIT"); + impl->after_wait = !(after_wait && std::string(after_wait) == "off"); impl_ = std::move(impl); note = "Frame Generation gives the runtime a Direct3D 12 queue of its own: frames it writes reach the runtime " "as soon as they are written, not after the game's queued work; the game's images are kept in order " @@ -246,6 +297,13 @@ void FrameGenQueueSubstitution::SetLog(std::function l } } +void FrameGenQueueSubstitution::SetRuntimeQueuesOnlyFirstUse(bool only_first_use) { + if (impl_) { + std::scoped_lock lock(impl_->mutex); + impl_->only_first_use = only_first_use; + } +} + const void* FrameGenQueueSubstitution::FrameGenChain(const void* game_chain) { if (!impl_) { return game_chain; @@ -290,6 +348,8 @@ XrResult FrameGenQueueSubstitution::Acquire(XrSwapchain swapchain, std::uint32_t const XrResult result = next.acquire(swapchain, info, index); if (XR_SUCCEEDED(result)) { ++state->held; + state->runtime_acquired.push_back(index ? *index : Impl::kUnknownImage); + state->unwaited.push_back(index ? *index : Impl::kUnknownImage); } return result; } @@ -299,27 +359,47 @@ XrResult FrameGenQueueSubstitution::Wait(XrSwapchain swapchain, bool lazy, const if (!impl_) { return next.wait(swapchain, info); } - if (const auto state = impl_->Find(swapchain, false)) { + const auto state = impl_->Find(swapchain, false); + // The image waited for: the application's oldest acquired. + std::uint32_t image = Impl::kUnknownImage; + if (state) { std::unique_lock calls(state->calls); + if (!state->unwaited.empty()) { + image = state->unwaited.front(); + state->unwaited.pop_front(); + } impl_->Forward(swapchain, *state, next, Impl::Until::kDone); if (!state->held_back.empty() && lazy && !state->direct_waits) { // A release before it is still held back: so is this wait. state->held_back.push_back({true, 0}); calls.unlock(); - impl_->AfterWait(); + impl_->AfterWait(state.get(), image); return XR_SUCCESS; } if (state->shown_lazily && state->releases) { // A layer the runtime shows: the releases before this wait go - // first, once their work has run. Waited for on the CPU without - // `calls` (a frame showing the layer meanwhile takes what has - // run), on the runtime's queue only when that takes too long. + // first, once their work has run, only as far as the runtime + // needs: while it has a release of another image newer than the + // waited image's last, no frame showing the layer shows that + // image, and the wait is held back behind them (as a lazy + // swapchain's; the game's queue still waits for the runtime's + // work queued so far, AfterWait). Waiting for the newest instead + // made the game's next frame wait for its whole last frame's GPU + // work: a GPU-bound game's GPU idled through its CPU work (an + // OpenKneeboard or game interface quad at 2026-11-01: 32 fps + // engaged against 48 passing through). Otherwise (the image is + // the newest the runtime has, or unknown) the oldest release (the + // newest when the image is unknown) is waited for on the CPU + // without `calls` (a frame showing the layer meanwhile takes what + // has run), on the runtime's queue only when that takes too long. + const auto oldest = std::find_if(state->held_back.begin(), state->held_back.end(), + [](const Impl::Call& call) { return !call.wait; }); const auto newest = std::find_if(state->held_back.rbegin(), state->held_back.rend(), [](const Impl::Call& call) { return !call.wait; }); - if (newest->value > impl_->released->GetCompletedValue()) { + const UINT64 value = image == Impl::kUnknownImage ? newest->value : oldest->value; + if (Impl::NewestShown(*state, image) && value > impl_->released->GetCompletedValue()) { ++impl_->shown_cpu_waits; if (!state->event) state->event = CreateEventW(nullptr, FALSE, FALSE, nullptr); - const UINT64 value = newest->value; calls.unlock(); // The event may still be set for an earlier value: waited // for until this one has passed. @@ -333,6 +413,13 @@ XrResult FrameGenQueueSubstitution::Wait(XrSwapchain swapchain, bool lazy, const } } calls.lock(); + impl_->Forward(swapchain, *state, next, Impl::Until::kDone); + } + if (state->releases && !Impl::NewestShown(*state, image)) { + state->held_back.push_back({true, 0}); + calls.unlock(); + impl_->AfterWait(state.get(), image); + return XR_SUCCESS; } impl_->shown_gpu_waits += impl_->Pending(*state); } @@ -347,7 +434,11 @@ XrResult FrameGenQueueSubstitution::Wait(XrSwapchain swapchain, bool lazy, const } const XrResult result = next.wait(swapchain, info); if (XR_SUCCEEDED(result) && result != XR_TIMEOUT_EXPIRED) { - impl_->AfterWait(); + impl_->AfterWait(state.get(), image); + } else if (state && result == XR_TIMEOUT_EXPIRED) { + // Waited for again. + std::scoped_lock calls(state->calls); + state->unwaited.push_front(image); } return result; } @@ -372,7 +463,7 @@ XrResult FrameGenQueueSubstitution::Release(XrSwapchain swapchain, bool lazy, co return XR_ERROR_RUNTIME_FAILURE; } impl_->runtime->Wait(impl_->released.Get(), value); - state->runtime_released = true; + Impl::Released(*state); return next.release(swapchain, info); } @@ -440,6 +531,7 @@ std::string FrameGenQueueSubstitution::Describe() const { std::ostringstream text; text << "released=" << impl_->released_value << '/' << impl_->released->GetCompletedValue() << " handedBack=" << impl_->handed_back_value << '/' << impl_->handed_back->GetCompletedValue(); + if (impl_->only_first_use) text << " unorderedWaits=" << impl_->unordered_waits.load(); std::vector>> swapchains(impl_->swapchains.begin(), impl_->swapchains.end()); lock.unlock(); diff --git a/layer/src/framegen/framegen_vulkan_bridge.cpp b/layer/src/framegen/framegen_vulkan_bridge.cpp index a27d2c2a..473ef777 100644 --- a/layer/src/framegen/framegen_vulkan_bridge.cpp +++ b/layer/src/framegen/framegen_vulkan_bridge.cpp @@ -61,6 +61,14 @@ constexpr std::uint32_t kProbeGrid = 32; constexpr std::uint32_t kProbePoints = kProbeGrid * kProbeGrid; constexpr std::chrono::milliseconds kProbeInterval{1000}; constexpr std::chrono::milliseconds kProbeDescribeInterval{5000}; +// The depth timing check (LateDepth): about once a second +// (VECTORXR_VULKAN_BRIDGE_DEPTH_CHECK_MS), a grid like the probe's, at a +// hand-over and at the swapchain's next acquire. Depth found only at the +// acquire this many checks in a row is taken from there; once taken, this +// many checks in a row with depth nowhere give it up. +constexpr std::chrono::milliseconds kLateCheckInterval{1000}; +constexpr int kLateAfterChecks = 2; +constexpr int kLateQuietChecks = 3; std::string Hex(std::uint64_t value) { char buffer[32]; @@ -310,6 +318,9 @@ struct VulkanBridge::Impl { struct Copy { ComPtr allocator; ComPtr list; + // The same copy from the swapchain's late depth (LateDepth::resource) + // instead of the twin, recorded when first needed. + ComPtr late; UINT64 done{0}; // the done value after its last execution std::uint32_t query{0}; // its two timestamps' first index bool timed{false}; // executed, its timestamps not read yet @@ -365,6 +376,63 @@ struct VulkanBridge::Impl { // too: a depth swapchain never handed over is never probed. std::chrono::steady_clock::time_point next_describe{}; }; + // When the game's depth is in a depth swapchain's image. OpenXR: by its + // release. The hand-over is at the frame's end, so depth written before + // xrEndFrame arrives too. Star Citizen (PTU and EPTU, October 2026) writes + // its depth after the release, CIG said, and every hand-over at the + // frame's end found its image all zero (1,126 probe records on Oct 24, 100 + // on Oct 28): it is not there by then. + // + // The check, about once a second: a grid of the twin read on the game's + // queue at its hand-over (as the copy reads it), and of the same image at + // the swapchain's next acquire (what the game left there after the + // frame's end, before its next frame). Depth that varies only at the + // acquire, kLateAfterChecks checks in a row (DepthTiming::kAuto), is taken + // from there: at each acquire the image handed over last is copied into + // `resource` (it goes to Direct3D 12 and straight back on the game's + // queue), and each hand-over copies `resource` into the runtime's image + // instead of its twin. That is the frame before's depth with this frame's + // colour: Frame Generation checks it against the other eye as any depth. + // Depth that varies at the hand-over again (the game fixed) gives it up + // at once, and so do kLateQuietChecks checks in a row with depth nowhere. + struct LateDepth { + VkBuffer buffer{VK_NULL_HANDLE}; // the check's grids: [hand-over, acquire][slice][point] + VkDeviceMemory memory{VK_NULL_HANDLE}; + const std::uint32_t* grid{nullptr}; // mapped + std::vector reads; // by image: 2 * index (hand-over), 2 * index + 1 (acquire) + bool unavailable{false}; // the check could not be made (reported once) + enum class Step { + kIdle, + kHandedOver, // the hand-over's grid is queued; the acquire's comes next + kQueued, // both are queued (`serial`) + }; + Step step{Step::kIdle}; + std::uint32_t image{0}; + std::uint64_t serial{0}; + std::chrono::steady_clock::time_point next{}; + std::uint64_t checks{0}; + int streak{0}; // checks in a row with depth only at the acquire + int quiet{0}; // taken: checks in a row with depth nowhere + bool engaged{false}; + bool reported_end_frame{false}; // kEndFrame: told once that it would take it + std::optional last_at_end, last_after; // the last check's findings (logged as they change) + // Taken from the acquire (engaged): + ComPtr resource; // the twin's shape, the bridge's alone + std::vector> allocators; + std::vector> lists; // by image: its twin into `resource` + bool resource_failed{false}; + std::optional handed; // the image the last hand-over named + bool fresh{false}; // `resource` holds a copy no hand-over took yet + std::uint64_t copies{0}; // into `resource` + std::uint64_t used{0}; // hand-overs that copied `resource` + std::uint64_t engagements{0}; + std::uint64_t bytes{0}; + // The checks' findings: depth varying at the hand-over; only at the + // acquire; at neither. + std::uint64_t at_end_checks{0}; + std::uint64_t late_only_checks{0}; + std::uint64_t neither_checks{0}; + }; // What a swapchain's images are, from the application's create info. struct Plan { Mode mode{Mode::kTwin}; @@ -416,6 +484,7 @@ struct VulkanBridge::Impl { std::uint64_t failed_submits{0}; std::uint64_t end_frame_copies{0}; // EndFrameMotion's DepthProbe probe; + LateDepth late; // depth swapchains' (kTwinDepth) }; struct Fence { VkFence fence{VK_NULL_HANDLE}; @@ -486,6 +555,26 @@ struct VulkanBridge::Impl { bool reported_second_thread{false}; bool debug_layer{false}; // VECTORXR_VULKAN_BRIDGE_DEBUG=1 bool depth_probe{false}; // VECTORXR_FRAMEGEN_DEPTH_BRIDGE_PROBE=1 (SetDepthProbe) + // VECTORXR_VULKAN_BRIDGE_DEPTH (read at Begin): when the game's depth is + // taken (LateDepth), or kept private. + enum class DepthTiming { + kAuto, // at the hand-over, or at the next acquire when only that has it (default, "auto") + kEndFrame, // at the hand-over only ("end-frame"; the check still runs and reports) + kLate, // at the next acquire from the start ("late") + kPrivate, // depth stays private and out of submitted frames ("private") + }; + DepthTiming depth_timing{DepthTiming::kAuto}; + std::string depth_private_reason{"VECTORXR_VULKAN_BRIDGE_DEPTH=private"}; // why kPrivate + std::chrono::milliseconds late_check_interval{kLateCheckInterval}; // VECTORXR_VULKAN_BRIDGE_DEPTH_CHECK_MS + bool check_wanted{false}; // WantDepthTimingCheck + bool check_always{false}; // VECTORXR_VULKAN_BRIDGE_DEPTH_CHECK=always + bool check_reported{false}; + std::uint64_t late_copies{0}; + // The CPU time of each hand-over (Released), all of it: its submissions, + // the copy queued and the hand-back. + std::vector handover_us; + std::size_t next_handover_sample{0}; + double handover_us_window_max{0.0}; std::uint32_t debug_reported{0}; // debug-layer messages reported std::uint64_t next_skip_report{1}; // skipped copies are reported at 1, 10, 100... std::uint64_t next_failure_report{1}; @@ -785,6 +874,12 @@ struct VulkanBridge::Impl { image.private_memory = VK_NULL_HANDLE; image.first_acquire = image.acquire = image.release = image.probe = VK_NULL_HANDLE; } + if (chain.late.buffer) vk.vkDestroyBuffer(device, chain.late.buffer, nullptr); + if (chain.late.memory) vk.vkFreeMemory(device, chain.late.memory, nullptr); // unmaps it + chain.late.buffer = VK_NULL_HANDLE; + chain.late.memory = VK_NULL_HANDLE; + chain.late.grid = nullptr; + chain.late.reads.clear(); // freed with the pool if (chain.probe.buffer) vk.vkDestroyBuffer(device, chain.probe.buffer, nullptr); if (chain.probe.memory) vk.vkFreeMemory(device, chain.probe.memory, nullptr); // unmaps it chain.probe.buffer = VK_NULL_HANDLE; @@ -820,9 +915,13 @@ struct VulkanBridge::Impl { image.runtime.Detach(); for (Copy& copy : image.copies) { copy.list.Detach(); + copy.late.Detach(); copy.allocator.Detach(); } } + chain.late.resource.Detach(); + for (auto& list : chain.late.lists) list.Detach(); + for (auto& allocator : chain.late.allocators) allocator.Detach(); chain.timestamps.Detach(); chain.stamps.Detach(); chain.probe.readback.Detach(); @@ -867,6 +966,16 @@ struct VulkanBridge::Impl { if (chain.motion) { stream << ", motion vectors (endFrameCopies=" << chain.end_frame_copies << ")"; } + if (plan.mode == Mode::kTwinDepth) { + const LateDepth& late = chain.late; + stream << ", depth=" << DepthTimingName(depth_timing) + << ", depthTaken=" << (late.engaged ? "late" : "end-frame") + << ", depthChecks=" << late.at_end_checks << "/" << late.late_only_checks << "/" + << late.neither_checks << " (at the hand-over/only at the next acquire/neither)" + << ", lateCopies=" << late.copies << ", lateUsed=" << late.used + << ", lateTaken=" << late.engagements; + if (late.bytes) stream << ", lateMiB=" << MiB(late.bytes); + } if (!chain.private_reason.empty()) { stream << ", private because " << chain.private_reason; } @@ -1262,15 +1371,28 @@ struct VulkanBridge::Impl { // depth targets; colour twins, with simultaneous access, need no // transition of their own, depth twins go to copy source and back. bool RecordCopy(Swapchain& chain, Image& image, Copy& copy, std::uint32_t query, std::string& reason) { - HRESULT hr = device12->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(©.allocator)); - if (SUCCEEDED(hr)) { - hr = device12->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, copy.allocator.Get(), nullptr, - IID_PPV_ARGS(©.list)); + const HRESULT hr = + device12->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(©.allocator)); + if (FAILED(hr)) { + reason = Failure("Creating the bridge's copy commands", hr); + return false; } + copy.query = query; + return RecordCopyList(chain, image, copy, image.twin.Get(), copy.list, reason); + } + + // The copy of `source` (the twin, or the late depth copy) into the + // runtime's image, recorded into `list` from `copy`'s allocator, with + // `copy`'s timestamps. + bool RecordCopyList(Swapchain& chain, Image& image, Copy& copy, ID3D12Resource* source, + ComPtr& list, std::string& reason) { + HRESULT hr = device12->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, copy.allocator.Get(), nullptr, + IID_PPV_ARGS(&list)); if (FAILED(hr)) { reason = Failure("Creating the bridge's copy commands", hr); return false; } + const std::uint32_t query = copy.query; const bool depth = chain.plan.mode == Mode::kTwinDepth; D3D12_RESOURCE_BARRIER barriers[2]{}; barriers[0].Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; @@ -1280,29 +1402,28 @@ struct VulkanBridge::Impl { depth ? D3D12_RESOURCE_STATE_DEPTH_WRITE : D3D12_RESOURCE_STATE_RENDER_TARGET; barriers[0].Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST; barriers[1].Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; - barriers[1].Transition.pResource = image.twin.Get(); + barriers[1].Transition.pResource = source; barriers[1].Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; barriers[1].Transition.StateBefore = D3D12_RESOURCE_STATE_COMMON; barriers[1].Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_SOURCE; const UINT count = depth ? 2 : 1; - copy.list->ResourceBarrier(count, barriers); - copy.query = query; + list->ResourceBarrier(count, barriers); if (chain.timestamps) { - copy.list->EndQuery(chain.timestamps.Get(), D3D12_QUERY_TYPE_TIMESTAMP, query); + list->EndQuery(chain.timestamps.Get(), D3D12_QUERY_TYPE_TIMESTAMP, query); } - copy.list->CopyResource(image.runtime.Get(), image.twin.Get()); + list->CopyResource(image.runtime.Get(), source); if (chain.timestamps) { - copy.list->EndQuery(chain.timestamps.Get(), D3D12_QUERY_TYPE_TIMESTAMP, query + 1); + list->EndQuery(chain.timestamps.Get(), D3D12_QUERY_TYPE_TIMESTAMP, query + 1); } for (D3D12_RESOURCE_BARRIER& barrier : barriers) { std::swap(barrier.Transition.StateBefore, barrier.Transition.StateAfter); } - copy.list->ResourceBarrier(count, barriers); + list->ResourceBarrier(count, barriers); if (chain.timestamps) { - copy.list->ResolveQueryData(chain.timestamps.Get(), D3D12_QUERY_TYPE_TIMESTAMP, query, 2, - chain.stamps.Get(), query * sizeof(UINT64)); + list->ResolveQueryData(chain.timestamps.Get(), D3D12_QUERY_TYPE_TIMESTAMP, query, 2, chain.stamps.Get(), + query * sizeof(UINT64)); } - hr = copy.list->Close(); + hr = list->Close(); if (FAILED(hr)) { reason = Failure("Recording the bridge's copy", hr); return false; @@ -1346,9 +1467,9 @@ struct VulkanBridge::Impl { // (holding `mutex`), behind the game's rendering into it ("written" // reaching `after`) and ahead of anything else queued there for the // image (Frame Generation's capture). Returns the done value it signals. - UINT64 QueueCopy(Swapchain& chain, Image& image, Copy& copy, UINT64 after) { + UINT64 QueueCopy(Swapchain& chain, Image& image, Copy& copy, UINT64 after, bool late = false) { queue->Wait(written.Get(), after); - ID3D12CommandList* lists[] = {copy.list.Get()}; + ID3D12CommandList* lists[] = {late ? copy.late.Get() : copy.list.Get()}; queue->ExecuteCommandLists(1, lists); const UINT64 value = ++done_value; queue->Signal(done.Get(), value); @@ -1583,6 +1704,65 @@ struct VulkanBridge::Impl { return true; } + // A recording reading a grid of kProbeGrid x kProbeGrid texels of each + // slice of `image`'s twin (the depth plane, mip 0) on the game's queue + // into `buffer`, four bytes a texel from texel `first` on (slice by + // slice, row by row), from the attachment layout and back (holding + // `mutex`). Null when it could not be made. + VkCommandBuffer RecordGridRead(Swapchain& chain, Image& image, VkBuffer buffer, std::uint32_t first) { + const Plan& plan = chain.plan; + VkCommandBufferAllocateInfo allocate{VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO}; + allocate.commandPool = chain.pool; + allocate.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; + allocate.commandBufferCount = 1; + VkCommandBuffer commands = VK_NULL_HANDLE; + if (vk.vkAllocateCommandBuffers(device, &allocate, &commands) != VK_SUCCESS) { + return VK_NULL_HANDLE; + } + std::vector regions; + for (std::uint32_t slice = 0; slice < plan.array_size; ++slice) { + for (std::uint32_t j = 0; j < kProbeGrid; ++j) { + for (std::uint32_t i = 0; i < kProbeGrid; ++i) { + VkBufferImageCopy region{}; + region.bufferOffset = + (VkDeviceSize{first} + VkDeviceSize{slice} * kProbePoints + j * kProbeGrid + i) * sizeof(float); + region.imageSubresource = {VK_IMAGE_ASPECT_DEPTH_BIT, 0, slice, 1}; + region.imageOffset = {static_cast(GridX(plan, i)), + static_cast(GridY(plan, j)), 0}; + region.imageExtent = {1, 1, 1}; + regions.push_back(region); + } + } + } + const bool recorded = Record(commands, [&](VkCommandBuffer cb) { + const VkImageMemoryBarrier before = + Barrier(image.twin_image, plan.aspect, plan, 1, plan.layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, + VK_ACCESS_MEMORY_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT, VK_QUEUE_FAMILY_IGNORED, + VK_QUEUE_FAMILY_IGNORED); + vk.vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, + nullptr, 0, nullptr, 1, &before); + vk.vkCmdCopyImageToBuffer(cb, image.twin_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, buffer, + static_cast(regions.size()), regions.data()); + const VkImageMemoryBarrier after = + Barrier(image.twin_image, plan.aspect, plan, 1, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, plan.layout, + VK_ACCESS_TRANSFER_READ_BIT, VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT, + VK_QUEUE_FAMILY_IGNORED, VK_QUEUE_FAMILY_IGNORED); + VkMemoryBarrier host{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; + host.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; + host.dstAccessMask = VK_ACCESS_HOST_READ_BIT; + vk.vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_TRANSFER_BIT, + VK_PIPELINE_STAGE_ALL_COMMANDS_BIT | VK_PIPELINE_STAGE_HOST_BIT, 0, 1, &host, 0, + nullptr, 1, &after); + }); + return recorded ? commands : VK_NULL_HANDLE; + } + static std::uint32_t GridX(const Plan& plan, std::uint32_t i) { + return std::min(plan.width - 1, (2 * i + 1) * plan.width / (2 * kProbeGrid)); + } + static std::uint32_t GridY(const Plan& plan, std::uint32_t j) { + return std::min(plan.height - 1, (2 * j + 1) * plan.height / (2 * kProbeGrid)); + } + // The recording reading the grid of `image`'s twin into the probe's // buffer on the game's queue, from the attachment layout and back // (holding `mutex`), when a probe of the swapchain is due; else null. @@ -1593,55 +1773,11 @@ struct VulkanBridge::Impl { return VK_NULL_HANDLE; } if (!image.probe) { - const Plan& plan = chain.plan; - VkCommandBufferAllocateInfo allocate{VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO}; - allocate.commandPool = chain.pool; - allocate.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; - allocate.commandBufferCount = 1; - VkCommandBuffer commands = VK_NULL_HANDLE; - if (vk.vkAllocateCommandBuffers(device, &allocate, &commands) != VK_SUCCESS) { + image.probe = RecordGridRead(chain, image, probe.buffer, 0); + if (!image.probe) { probe.unavailable = true; return VK_NULL_HANDLE; } - std::vector regions; - for (std::uint32_t slice = 0; slice < plan.array_size; ++slice) { - for (std::uint32_t j = 0; j < kProbeGrid; ++j) { - for (std::uint32_t i = 0; i < kProbeGrid; ++i) { - VkBufferImageCopy region{}; - region.bufferOffset = (VkDeviceSize{slice} * kProbePoints + j * kProbeGrid + i) * sizeof(float); - region.imageSubresource = {VK_IMAGE_ASPECT_DEPTH_BIT, 0, slice, 1}; - region.imageOffset = {static_cast(std::min(plan.width - 1, (2 * i + 1) * plan.width / (2 * kProbeGrid))), - static_cast(std::min(plan.height - 1, (2 * j + 1) * plan.height / (2 * kProbeGrid))), 0}; - region.imageExtent = {1, 1, 1}; - regions.push_back(region); - } - } - } - const bool recorded = Record(commands, [&](VkCommandBuffer cb) { - const VkImageMemoryBarrier before = - Barrier(image.twin_image, plan.aspect, plan, 1, plan.layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, - VK_ACCESS_MEMORY_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT, VK_QUEUE_FAMILY_IGNORED, - VK_QUEUE_FAMILY_IGNORED); - vk.vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, - nullptr, 0, nullptr, 1, &before); - vk.vkCmdCopyImageToBuffer(cb, image.twin_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, probe.buffer, - static_cast(regions.size()), regions.data()); - const VkImageMemoryBarrier after = - Barrier(image.twin_image, plan.aspect, plan, 1, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, plan.layout, - VK_ACCESS_TRANSFER_READ_BIT, VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT, - VK_QUEUE_FAMILY_IGNORED, VK_QUEUE_FAMILY_IGNORED); - VkMemoryBarrier host{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; - host.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; - host.dstAccessMask = VK_ACCESS_HOST_READ_BIT; - vk.vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_TRANSFER_BIT, - VK_PIPELINE_STAGE_ALL_COMMANDS_BIT | VK_PIPELINE_STAGE_HOST_BIT, 0, 1, &host, 0, - nullptr, 1, &after); - }); - if (!recorded) { - probe.unavailable = true; - return VK_NULL_HANDLE; - } - image.probe = commands; } probe.next = now + kProbeInterval; return image.probe; @@ -1743,6 +1879,330 @@ struct VulkanBridge::Impl { } } + // --- When the game's depth is taken (LateDepth) ------------------------ + + static const char* DepthTimingName(DepthTiming timing) { + switch (timing) { + case DepthTiming::kEndFrame: + return "end-frame"; + case DepthTiming::kLate: + return "late"; + case DepthTiming::kPrivate: + return "private"; + default: + return "auto"; + } + } + + // The bits of a depth texel a grid read holds (four bytes a texel; D16's + // depth takes two of them, D24's three). + static std::uint32_t DepthBits(VkFormat format) { + switch (format) { + case VK_FORMAT_D16_UNORM: + case VK_FORMAT_D16_UNORM_S8_UINT: + return 0xFFFFu; + case VK_FORMAT_X8_D24_UNORM_PACK32: + case VK_FORMAT_D24_UNORM_S8_UINT: + return 0xFFFFFFu; + default: + return 0xFFFFFFFFu; + } + } + + // The check's buffer, at its first check (holding `mutex`). False: the + // swapchain is not checked (reported once), and its depth is taken at the + // hand-over. + bool MakeLateCheck(Swapchain& chain) { + LateDepth& late = chain.late; + if (late.unavailable) return false; + if (late.buffer) return true; + const Plan& plan = chain.plan; + const auto fail = [&](const std::string& why) { + late.unavailable = true; + Info("the depth timing check leaves a depth swapchain out, its depth is taken at the hand-over: " + why + + " (" + Describe(chain) + ")."); + return false; + }; + VkBufferCreateInfo info{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; + info.size = VkDeviceSize{2} * kProbePoints * plan.array_size * sizeof(std::uint32_t); + info.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT; + if (vk.vkCreateBuffer(device, &info, nullptr, &late.buffer) != VK_SUCCESS) { + late.buffer = VK_NULL_HANDLE; + return fail("its buffer could not be made"); + } + VkMemoryRequirements requirements{}; + vk.vkGetBufferMemoryRequirements(device, late.buffer, &requirements); + VkMemoryAllocateInfo allocate{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO}; + allocate.allocationSize = requirements.size; + allocate.memoryTypeIndex = UINT32_MAX; + constexpr VkMemoryPropertyFlags kHost = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; + for (std::uint32_t i = 0; i < memory.memoryTypeCount && allocate.memoryTypeIndex == UINT32_MAX; ++i) { + if ((requirements.memoryTypeBits & (1u << i)) && (memory.memoryTypes[i].propertyFlags & kHost) == kHost) { + allocate.memoryTypeIndex = i; + } + } + void* mapped = nullptr; + if (allocate.memoryTypeIndex == UINT32_MAX || + vk.vkAllocateMemory(device, &allocate, nullptr, &late.memory) != VK_SUCCESS) { + late.memory = VK_NULL_HANDLE; + return fail("no host memory for its buffer"); + } + if (vk.vkBindBufferMemory(device, late.buffer, late.memory, 0) != VK_SUCCESS || + vk.vkMapMemory(device, late.memory, 0, VK_WHOLE_SIZE, 0, &mapped) != VK_SUCCESS) { + return fail("its buffer could not be mapped"); + } + late.grid = static_cast(mapped); + late.reads.assign(chain.images.size() * 2, VK_NULL_HANDLE); + return true; + } + + // The check's grid read of `image` (`which` 0 at its hand-over, 1 at the + // next acquire), recorded at its first use (holding `mutex`); null when + // it could not be. + VkCommandBuffer LateRead(Swapchain& chain, Image& image, std::uint32_t index, std::uint32_t which) { + LateDepth& late = chain.late; + if (late.unavailable || !late.buffer || 2 * index + which >= late.reads.size()) return VK_NULL_HANDLE; + VkCommandBuffer& commands = late.reads[2 * index + which]; + if (!commands) { + commands = RecordGridRead(chain, image, late.buffer, which * chain.plan.array_size * kProbePoints); + if (!commands) { + late.unavailable = true; + Info("the depth timing check leaves a depth swapchain out, its depth is taken at the hand-over: its " + "grid read could not be recorded (" + + Describe(chain) + ")."); + } + } + return commands; + } + + // A check whose grid reads both finished, judged (holding `mutex`). + void FinishLateCheck(XrSwapchain swapchain, Swapchain& chain) { + LateDepth& late = chain.late; + if (late.step != LateDepth::Step::kQueued) return; + PollFences(); + if (lost) { + late.step = LateDepth::Step::kIdle; + return; + } + if (completed_serial < late.serial) return; + late.step = LateDepth::Step::kIdle; + ++late.checks; + const Plan& plan = chain.plan; + const std::uint32_t bits = DepthBits(plan.format); + struct Found { + bool varies{false}; + std::uint32_t nonzero{0}; + std::uint32_t first{0}; + }; + Found found[2]; + for (std::uint32_t which = 0; which < 2; ++which) { + for (std::uint32_t slice = 0; slice < plan.array_size; ++slice) { + const std::uint32_t* grid = + late.grid + (std::size_t{which} * plan.array_size + slice) * kProbePoints; + const std::uint32_t first = grid[0] & bits; + if (slice == 0) found[which].first = first; + for (std::uint32_t point = 0; point < kProbePoints; ++point) { + const std::uint32_t value = grid[point] & bits; + found[which].varies = found[which].varies || value != first; + found[which].nonzero += value != 0 ? 1 : 0; + } + } + } + const bool at_end = found[0].varies; + const bool after = found[1].varies; + const std::string name = "swapchain " + Hex(reinterpret_cast(swapchain)); + // Counted always; logged as it changes to or from depth found only + // at the acquire (a game that keeps its depth uniform or empty, or + // writes it in time, is not worth a line a check). + const bool late_only = after && !at_end; + ++(at_end ? late.at_end_checks : late_only ? late.late_only_checks : late.neither_checks); + const bool was_late_only = late.last_at_end && late.last_after && !*late.last_at_end && *late.last_after; + const bool changed = late.last_at_end != at_end || late.last_after != after; + late.last_at_end = at_end; + late.last_after = after; + if (changed && (late_only || was_late_only)) { + const std::uint32_t points = kProbePoints * plan.array_size; + const auto finding = [&](const Found& f) { + std::ostringstream text; + if (f.varies) { + text << "varies (" << f.nonzero << " of " << points << " texels nonzero)"; + } else { + text << "is uniform (" << Hex(f.first) << " in all " << points << " texels)"; + } + return text.str(); + }; + Info("depth timing check " + std::to_string(late.checks) + " of " + name + ", image " + + std::to_string(late.image) + ": at its hand-over (the frame's end) the game's depth " + + finding(found[0]) + "; at the swapchain's next acquire it " + finding(found[1]) + " (" + + Describe(chain) + ")."); + } + switch (depth_timing) { + case DepthTiming::kAuto: + if (!late.engaged) { + if (!at_end && after) { + if (++late.streak >= kLateAfterChecks && !late.resource_failed) { + late.engaged = true; + late.streak = 0; + late.quiet = 0; + ++late.engagements; + Info("the game writes its depth after its frame's end (" + name + ": " + + std::to_string(kLateAfterChecks) + + " checks in a row found it only at the swapchain's next acquire): from now on each " + "acquire copies the image handed over last, and each hand-over gives the runtime that " + "copy, the frame before's depth (" + + Describe(chain) + ")."); + } + } else { + late.streak = 0; + } + } else if (at_end || (!after && ++late.quiet >= kLateQuietChecks)) { + late.engaged = false; + late.fresh = false; + late.streak = 0; + late.quiet = 0; + Info("the game's depth is taken at its hand-over again (" + name + ": " + + (at_end ? std::string("it is in its image at the frame's end") + : std::to_string(kLateQuietChecks) + " checks in a row found no depth either way") + + ")."); + } else if (after) { + late.quiet = 0; + } + break; + case DepthTiming::kEndFrame: + if (!at_end && after && !late.reported_end_frame) { + late.reported_end_frame = true; + Info("the game's depth (" + name + + ") varies only at the swapchain's next acquire, after its frame's end; " + "VECTORXR_VULKAN_BRIDGE_DEPTH=end-frame keeps taking it at the hand-over, where it is not."); + } + break; + default: + break; + } + } + + // The late copy and its recordings, when first taken (holding `mutex`). + // False: it could not be made (reported once), and depth stays the + // hand-over's. + bool EnsureLateResource(Swapchain& chain) { + LateDepth& late = chain.late; + if (late.resource) return true; + if (late.resource_failed || chain.images.empty() || !chain.images.front().twin) return false; + const D3D12_RESOURCE_DESC desc = chain.images.front().twin->GetDesc(); + D3D12_HEAP_PROPERTIES heap{D3D12_HEAP_TYPE_DEFAULT}; + HRESULT hr = device12->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COMMON, + nullptr, IID_PPV_ARGS(&late.resource)); + late.allocators.assign(chain.images.size(), nullptr); + late.lists.assign(chain.images.size(), nullptr); + for (std::size_t i = 0; SUCCEEDED(hr) && i < chain.images.size(); ++i) { + hr = device12->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&late.allocators[i])); + if (SUCCEEDED(hr)) { + hr = device12->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, late.allocators[i].Get(), nullptr, + IID_PPV_ARGS(&late.lists[i])); + } + if (FAILED(hr)) break; + D3D12_RESOURCE_BARRIER barriers[2]{}; + for (D3D12_RESOURCE_BARRIER& barrier : barriers) { + barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; + barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; + barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COMMON; + } + barriers[0].Transition.pResource = chain.images[i].twin.Get(); + barriers[0].Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_SOURCE; + barriers[1].Transition.pResource = late.resource.Get(); + barriers[1].Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST; + late.lists[i]->ResourceBarrier(2, barriers); + late.lists[i]->CopyResource(late.resource.Get(), chain.images[i].twin.Get()); + for (D3D12_RESOURCE_BARRIER& barrier : barriers) { + std::swap(barrier.Transition.StateBefore, barrier.Transition.StateAfter); + } + late.lists[i]->ResourceBarrier(2, barriers); + hr = late.lists[i]->Close(); + } + if (FAILED(hr)) { + late.resource.Reset(); + late.allocators.clear(); + late.lists.clear(); + late.resource_failed = true; + late.engaged = false; + Info(Failure("Making the late depth copy", hr) + ": the game's depth is taken at the hand-over (" + + Describe(chain) + ")."); + return false; + } + late.bytes = device12->GetResourceAllocationInfo(0, 1, &desc).SizeInBytes; + Info("the late depth copy of a depth swapchain holds " + std::to_string(MiB(late.bytes)) + " MiB (" + + Describe(chain) + ")."); + return true; + } + + // At an acquire on a depth swapchain, before the image acquired is + // handled (holding `mutex`): what the game left in the image handed over + // last, after its frame's end, is read for a pending check and, while + // depth is taken from here, copied into the late copy: the image goes to + // Direct3D 12 and straight back to the game's queue, behind the copy. + void LateAtAcquire(XrSwapchain swapchain, Swapchain& chain) { + LateDepth& late = chain.late; + FinishLateCheck(swapchain, chain); + const std::optional handed = std::exchange(late.handed, std::nullopt); + const bool checking = late.step == LateDepth::Step::kHandedOver; + if (checking) late.step = LateDepth::Step::kIdle; // unless queued below + if (!handed || *handed >= chain.images.size()) return; + // Only between frames: with another of its images acquired and not + // handed over yet (a game acquiring ahead, or its next frame begun + // before this one ended), the game may be rendering into it now. + if (chain.acquired.size() != 1) return; + Image& image = chain.images[*handed]; + if (image.state != State::kGame || !image.acquire) return; + const VkCommandBuffer check = + checking && late.image == *handed ? LateRead(chain, image, *handed, 1) : VK_NULL_HANDLE; + const bool copy = late.engaged && EnsureLateResource(chain); + if (!check && !copy) return; + std::uint64_t serial = 0; + if (!copy) { + if (Submit(check, 0, 0, serial) != VK_SUCCESS) { + ++chain.failed_submits; + return; + } + chain.last_serial = serial; + late.step = LateDepth::Step::kQueued; + late.serial = serial; + return; + } + const UINT64 written_next = written_value + 1; + const VkCommandBuffer commands[2] = {check, image.release}; + if (Submit(check ? std::span(commands, 2) + : std::span(commands + 1, 1), + 0, written_next, serial) != VK_SUCCESS) { + ++chain.failed_submits; + return; + } + written_value = written_next; + image.state = State::kExternal; + chain.last_serial = serial; + if (check) { + late.step = LateDepth::Step::kQueued; + late.serial = serial; + } + queue->Wait(written.Get(), written_next); + ID3D12CommandList* lists[] = {late.lists[*handed].Get()}; + queue->ExecuteCommandLists(1, lists); + const UINT64 value = ++done_value; + queue->Signal(done.Get(), value); + image.done = value; + chain.last_done = value; + late.fresh = true; + ++late.copies; + std::uint64_t back = 0; + if (Submit(image.acquire, value, 0, back) == VK_SUCCESS) { + image.state = State::kGame; + image.handed_back = true; + chain.last_serial = back; + } else { + // It stays with the bridge: its next acquire gives it back. + ++chain.failed_submits; + } + } + Swapchain* Find(XrSwapchain swapchain) { const auto it = swapchains.find(swapchain); return it == swapchains.end() ? nullptr : &it->second; @@ -1977,6 +2437,25 @@ const XrSessionCreateInfo* VulkanBridge::Begin(XrInstance instance, const XrSess impl->debug_layer = true; } } + if (const char* timing = std::getenv("VECTORXR_VULKAN_BRIDGE_DEPTH")) { + const std::string value = timing; + impl->depth_timing = value == "private" ? Impl::DepthTiming::kPrivate + : value == "late" ? Impl::DepthTiming::kLate + : value == "end-frame" ? Impl::DepthTiming::kEndFrame + : Impl::DepthTiming::kAuto; + } + if (keep_depth_private_ && impl->depth_timing != Impl::DepthTiming::kPrivate) { + impl->depth_timing = Impl::DepthTiming::kPrivate; + impl->depth_private_reason = "Frame Generation's Use game depth is off"; + } + if (const char* check = std::getenv("VECTORXR_VULKAN_BRIDGE_DEPTH_CHECK"); check && std::string(check) == "always") { + impl->check_always = true; + } + if (const char* interval = std::getenv("VECTORXR_VULKAN_BRIDGE_DEPTH_CHECK_MS"); interval && *interval) { + char* end = nullptr; + const long ms = std::strtol(interval, &end, 10); + if (end && *end == '\0' && ms >= 0) impl->late_check_interval = std::chrono::milliseconds(ms); + } ComPtr factory; ComPtr adapter; HRESULT hr = CreateDXGIFactory1(IID_PPV_ARGS(&factory)); @@ -2092,6 +2571,7 @@ const XrSessionCreateInfo* VulkanBridge::Begin(XrInstance instance, const XrSess << ", queueFamily=" << impl->family << ", queueIndex=" << impl->queue_index << ", semaphores=" << (impl->timeline ? "timeline" : "binary") << ", formatLists=" << (impl->api12 ? "yes" : "no") << ", videoMemory=" << impl->VideoMemory() + << ", depth=" << Impl::DepthTimingName(impl->depth_timing) << (impl->debug_layer ? ", debugLayer=on" : ""); description = stream.str(); impl_ = std::move(impl); @@ -2274,7 +2754,25 @@ void VulkanBridge::FrameEnded() { } } +void VulkanBridge::WantDepthTimingCheck(bool want) { + if (!impl_) { + return; + } + std::scoped_lock lock(impl_->mutex); + if (want && !impl_->check_reported && !impl_->check_always) { + impl_->check_reported = true; + impl_->Info("Frame Generation finds the game's depth the same all over: the bridge checks when the game " + "writes it (at the frame's end, or only by its next frame)."); + } + impl_->check_wanted = want; +} + +void VulkanBridge::SetKeepDepthPrivate(bool keep) { + keep_depth_private_ = keep; +} + void VulkanBridge::SetDepthProbe(bool on) { + if (!impl_) { return; } @@ -2342,6 +2840,11 @@ bool VulkanBridge::AddSwapchain(XrSwapchain swapchain, const XrSwapchainCreateIn } if (chain.plan.mode == Impl::Mode::kPrivate) { chain.private_reason = "it is multisampled"; + } else if (chain.plan.mode == Impl::Mode::kTwinDepth && impl.depth_timing == Impl::DepthTiming::kPrivate) { + // The switch to rule the bridge's depth out (logged once a session, + // in the session's line). + chain.plan.mode = Impl::Mode::kPrivate; + chain.private_reason = impl.depth_private_reason; } else if (chain.plan.mode == Impl::Mode::kTwinDepth) { for (const XrSwapchainImageD3D12KHR& image : runtime_images) { if (image.texture && (image.texture->GetDesc().Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL) == 0) { @@ -2355,6 +2858,7 @@ bool VulkanBridge::AddSwapchain(XrSwapchain swapchain, const XrSwapchainCreateIn } } chain.hand_back = chain.plan.mode == Impl::Mode::kTwinDepth; + chain.late.engaged = chain.plan.mode == Impl::Mode::kTwinDepth && impl.depth_timing == Impl::DepthTiming::kLate; const Impl::Plan& plan = chain.plan; // Everything made here is the swapchain's alone until it is added, and @@ -2515,6 +3019,11 @@ void VulkanBridge::Acquired(XrSwapchain swapchain, std::uint32_t index) { if (impl.lost) { return; } + if (chain->plan.mode == Impl::Mode::kTwinDepth) { + // What the game left in the image handed over last, after its frame's + // end (LateDepth), before anything of this acquire. + impl.LateAtAcquire(swapchain, *chain); + } Impl::Image& image = chain->images[index]; const Impl::Mode mode = chain->plan.mode; if (mode == Impl::Mode::kPrivate) { @@ -2599,6 +3108,15 @@ bool VulkanBridge::Released(XrSwapchain swapchain, bool teardown) { return true; } impl.NoteThread(); + // The hand-over's whole CPU time, its submissions, copy and hand-back. + struct Timed { + Impl& impl; + std::chrono::steady_clock::time_point start; + ~Timed() { + AddSample(impl.handover_us, impl.next_handover_sample, impl.handover_us_window_max, + std::chrono::duration(std::chrono::steady_clock::now() - start).count()); + } + } timed{impl, std::chrono::steady_clock::now()}; Impl::Image& image = chain->images[index]; if (impl.lost || image.state != Impl::State::kGame) { // The game's device is lost, or the acquire's submission was refused: @@ -2612,12 +3130,32 @@ bool VulkanBridge::Released(XrSwapchain swapchain, bool teardown) { impl.FinishProbe(swapchain, *chain); probe = impl.ProbeCommands(*chain, image); } + // When the game's depth is in its image (LateDepth): about once a second, + // a grid of the twin as the copy reads it, read just before its release + // (the same image is read again at the swapchain's next acquire). + const bool depth = chain->plan.mode == Impl::Mode::kTwinDepth && !teardown; + VkCommandBuffer check = VK_NULL_HANDLE; + if (depth) { + Impl::LateDepth& late = chain->late; + impl.FinishLateCheck(swapchain, *chain); + const auto now = std::chrono::steady_clock::now(); + // Only while depth looks empty to Frame Generation, or is taken late: + // a game whose depth arrives in time submits exactly as before. + const bool wanted = impl.check_wanted || impl.check_always || late.engaged; + if (wanted && late.step == Impl::LateDepth::Step::kIdle && now >= late.next && impl.MakeLateCheck(*chain)) { + check = impl.LateRead(*chain, image, index, 0); + if (check) late.next = now + impl.late_check_interval; + } + } // The game's rendering into the image is handed to the bridge's queue... const UINT64 written = impl.written_value + 1; std::uint64_t serial = 0; - const VkCommandBuffer handed[2] = {probe, image.release}; - if (impl.Submit(probe ? std::span(handed, 2) : std::span(handed + 1, 1), - 0, written, serial) != VK_SUCCESS) { + VkCommandBuffer handed[3]{}; + std::size_t handed_count = 0; + if (probe) handed[handed_count++] = probe; + if (check) handed[handed_count++] = check; + handed[handed_count++] = image.release; + if (impl.Submit(std::span(handed, handed_count), 0, written, serial) != VK_SUCCESS) { // Nothing was handed over: no copy, and the image stays the game's. // At teardown its preceding rendering still needs a later marker; // the acquire's old fence cannot safely retire this image. @@ -2628,6 +3166,10 @@ bool VulkanBridge::Released(XrSwapchain swapchain, bool teardown) { impl.written_value = written; image.state = Impl::State::kExternal; chain->last_serial = serial; + if (check) { + chain->late.step = Impl::LateDepth::Step::kHandedOver; + chain->late.image = index; + } Impl::Copy* copy = impl.ReadyCopy(*chain, image, index); if (!copy) { if (probe) { @@ -2645,8 +3187,25 @@ bool VulkanBridge::Released(XrSwapchain swapchain, bool teardown) { return false; } // ...which copies it into the runtime's image, ahead of anything else - // queued on it for the release (Frame Generation's capture). - const UINT64 done = impl.QueueCopy(*chain, image, *copy, written); + // queued on it for the release (Frame Generation's capture): while the + // game's depth is taken after its frame's end, the copy the last acquire + // made instead of the twin. + bool late = false; + if (depth) { + Impl::LateDepth& taken = chain->late; + late = taken.engaged && taken.fresh && taken.resource; + std::string why; + if (late && !copy->late && !impl.RecordCopyList(*chain, image, *copy, taken.resource.Get(), copy->late, why)) { + late = false; + } + taken.fresh = false; + taken.handed = index; + if (late) { + ++taken.used; + ++impl.late_copies; + } + } + const UINT64 done = impl.QueueCopy(*chain, image, *copy, written, late); if (probe) { // ...and the runtime's image just after the copy. impl.QueueRuntimeProbe(*chain, image, index, serial, true); @@ -2932,8 +3491,12 @@ std::string VulkanBridge::Summary() const { std::scoped_lock lock(impl_->mutex); impl_->ReadAllCopyTimes(); std::uint64_t twin_bytes = 0; + std::uint64_t checks[3]{}; for (const auto& [swapchain, chain] : impl_->swapchains) { - twin_bytes += chain.twin_bytes; + twin_bytes += chain.twin_bytes + chain.late.bytes; + checks[0] += chain.late.at_end_checks; + checks[1] += chain.late.late_only_checks; + checks[2] += chain.late.neither_checks; } const auto whole = [](double value) { return static_cast(value + 0.5); }; std::ostringstream stream; @@ -2945,7 +3508,15 @@ std::string VulkanBridge::Summary() const { << ", unmatchedReleases=" << impl_->unmatched_releases << ", endFrameCopies=" << impl_->end_frame_copies << ", submits=" << impl_->submits << ", failedSubmits=" << impl_->failed_submits << ", submitUs p50/max=" << whole(Percentile(impl_->submit_us, 0.5)) << "/" - << whole(std::exchange(impl_->submit_us_window_max, 0.0)) << ", liveFences=" << impl_->fence_count + << whole(std::exchange(impl_->submit_us_window_max, 0.0)) + << ", handoverUs p50/max=" << whole(Percentile(impl_->handover_us, 0.5)) << "/" + << whole(std::exchange(impl_->handover_us_window_max, 0.0)) + << ", depth=" << Impl::DepthTimingName(impl_->depth_timing) + + << ", depthChecks=" << checks[0] << "/" << checks[1] << "/" << checks[2] + << ", lateDepthCopies=" << impl_->late_copies + + << ", liveFences=" << impl_->fence_count << ", fenceGrowth=" << impl_->fence_growth << ", unfencedSubmits=" << impl_->unfenced_submits << ", semaphores=" << (impl_->timeline ? "timeline" : "binary") << ", copyGpuUs p50/p99=" << whole(Percentile(impl_->copy_gpu_us, 0.5)) << "/" diff --git a/layer/src/framegen_plan.cpp b/layer/src/framegen_plan.cpp index c3ae6784..19953c1e 100644 --- a/layer/src/framegen_plan.cpp +++ b/layer/src/framegen_plan.cpp @@ -4014,4 +4014,76 @@ std::filesystem::path WriteFrameGenEngineRemoved(const std::string& cause) { return path; } +FrameGenEdgeMargins FrameGenEdgeFillMargins(const FrameGenFov& fov, FrameGenExtent extent, float horizontal_degrees, + float vertical_degrees) { + FrameGenEdgeMargins margins; + constexpr double kDegree = 3.14159265358979323846 / 180.0; + constexpr double kFarthest = 85.0 * kDegree; + if (!extent.width || !extent.height || !(fov.right > fov.left) || !(fov.up > fov.down) || + !(fov.left > -kFarthest) || !(fov.right < kFarthest) || !(fov.down > -kFarthest) || !(fov.up < kFarthest)) { + return margins; + } + // Pixels a margin needs to reach `degrees` past an edge at `angle` + // (outward `sign`), at `spacing` tangent per pixel; within half the frame. + const auto margin = [&](double angle, double sign, double degrees, double spacing, std::uint32_t frame) { + if (!(degrees > 0.0)) return 0u; + const double reached = std::clamp(angle + sign * degrees * kDegree, -kFarthest, kFarthest); + const double pixels = std::ceil(std::abs(std::tan(reached) - std::tan(angle)) / spacing - 1e-6); + return static_cast(std::clamp(pixels, 0.0, static_cast(frame / 2))); + }; + const double h_spacing = (std::tan(double(fov.right)) - std::tan(double(fov.left))) / extent.width; + const double v_spacing = (std::tan(double(fov.up)) - std::tan(double(fov.down))) / extent.height; + margins.left = margin(fov.left, -1.0, horizontal_degrees, h_spacing, extent.width); + margins.right = margin(fov.right, 1.0, horizontal_degrees, h_spacing, extent.width); + margins.up = margin(fov.up, 1.0, vertical_degrees, v_spacing, extent.height); + margins.down = margin(fov.down, -1.0, vertical_degrees, v_spacing, extent.height); + // The image fits the largest texture: both margins shrink alike. + const auto fit = [](std::uint32_t frame, std::uint32_t& a, std::uint32_t& b) { + if (frame >= kFrameGenEdgeFillMaxExtent) { + a = b = 0; + return; + } + const std::uint64_t room = kFrameGenEdgeFillMaxExtent - frame; + if (std::uint64_t{a} + b > room) { + const double scale = static_cast(room) / (static_cast(a) + b); + a = static_cast(std::floor(a * scale)); + b = static_cast(std::floor(b * scale)); + } + }; + fit(extent.width, margins.left, margins.right); + fit(extent.height, margins.up, margins.down); + return margins; +} + +FrameGenFov FrameGenEdgeFillFov(const FrameGenFov& fov, FrameGenExtent extent, const FrameGenEdgeMargins& margins) { + if (!margins.Any() || !extent.width || !extent.height) { + return fov; + } + const double left = std::tan(double(fov.left)), right = std::tan(double(fov.right)); + const double up = std::tan(double(fov.up)), down = std::tan(double(fov.down)); + const double h_spacing = (right - left) / extent.width; + const double v_spacing = (up - down) / extent.height; + FrameGenFov widened = fov; + if (margins.left) widened.left = static_cast(std::atan(left - margins.left * h_spacing)); + if (margins.right) widened.right = static_cast(std::atan(right + margins.right * h_spacing)); + if (margins.up) widened.up = static_cast(std::atan(up + margins.up * v_spacing)); + if (margins.down) widened.down = static_cast(std::atan(down - margins.down * v_spacing)); + return widened; +} + +std::optional> ParseFrameGenEdgeFillDegrees(const std::string& value) { + float h = 0.0f, v = 0.0f; + char extra = 0; + const int read = std::sscanf(value.c_str(), "%f,%f%c", &h, &v, &extra); + if (read == 1) { + v = h; + } else if (read != 2) { + return std::nullopt; + } + if (!(h >= 0.0f && h <= 15.0f) || !(v >= 0.0f && v <= 15.0f)) { + return std::nullopt; + } + return std::array{h, v}; +} + } // namespace depthxr diff --git a/layer/src/layer_entry.cpp b/layer/src/layer_entry.cpp index d4c62a40..0787e0f7 100644 --- a/layer/src/layer_entry.cpp +++ b/layer/src/layer_entry.cpp @@ -7,13 +7,16 @@ #include "depthxr/vulkan_enable_hooks.h" #include +#include #include #include #include #include #include +#include #include #include +#include #include #include @@ -37,7 +40,83 @@ std::filesystem::path GetModuleDirectory() { return std::filesystem::path(buffer).parent_path(); } +// An instance VectorXR passes straight through: its functions are the +// layers' below. VectorXR serves one instance. OpenXR Toolkit and +// Quad-Views-Foveated create a probe instance through the chain before the +// game's, and on a VIVE runtime ("Vive Reality system") never destroy it: the +// probe (no graphics extension, no session) is let go as the game's instance +// is created (OpenXrLayer::RetireIdleInstance), and the game's is VectorXR's. +// An instance created while VectorXR's can have a session passes through. +struct PassThroughInstance { + PFN_xrGetInstanceProcAddr next_get_instance_proc_addr{}; + PFN_xrDestroyInstance next_destroy_instance{}; +}; +std::mutex g_pass_through_mutex; +std::unordered_map g_pass_through_instances; +// The map's size, so the per-frame hooks below skip its lock while it is empty. +std::atomic g_pass_through_count{0}; + +void RememberPassThroughInstance(XrInstance instance, const PassThroughInstance& pass_through) { + std::scoped_lock lock(g_pass_through_mutex); + g_pass_through_instances[instance] = pass_through; + g_pass_through_count.store(g_pass_through_instances.size(), std::memory_order_release); +} + +void ForgetPassThroughInstance(XrInstance instance) { + std::scoped_lock lock(g_pass_through_mutex); + g_pass_through_instances.erase(instance); + g_pass_through_count.store(g_pass_through_instances.size(), std::memory_order_release); +} + +bool FindPassThroughInstance(XrInstance instance, PassThroughInstance* found) { + if (instance == XR_NULL_HANDLE || g_pass_through_count.load(std::memory_order_acquire) == 0) { + return false; + } + std::scoped_lock lock(g_pass_through_mutex); + const auto entry = g_pass_through_instances.find(instance); + if (entry == g_pass_through_instances.end()) { + return false; + } + *found = entry->second; + return true; +} + +XrResult XRAPI_CALL PassThroughDestroyInstance(XrInstance instance) { + PFN_xrDestroyInstance next_destroy_instance = nullptr; + { + std::scoped_lock lock(g_pass_through_mutex); + const auto entry = g_pass_through_instances.find(instance); + if (entry != g_pass_through_instances.end()) { + next_destroy_instance = entry->second.next_destroy_instance; + g_pass_through_instances.erase(entry); + g_pass_through_count.store(g_pass_through_instances.size(), std::memory_order_release); + } + } + return next_destroy_instance ? next_destroy_instance(instance) : XR_ERROR_HANDLE_INVALID; +} + +// A retired probe's caller may hold VectorXR's functions from before it was +// retired: an instance-level one called with its handle goes to the layers +// below, as the instance's own functions would (true, with their `result`). +template +bool ForwardPassThrough(XrInstance instance, const char* name, XrResult& result, Args... args) { + PassThroughInstance pass_through; + if (!FindPassThroughInstance(instance, &pass_through)) { + return false; + } + PFN_xrVoidFunction function = nullptr; + result = pass_through.next_get_instance_proc_addr(instance, name, &function); + if (XR_SUCCEEDED(result)) { + result = function ? reinterpret_cast(function)(instance, args...) : XR_ERROR_FUNCTION_UNSUPPORTED; + } + return true; +} + XrResult XRAPI_CALL DepthxrDestroyInstance(XrInstance instance) { + PassThroughInstance pass_through; + if (FindPassThroughInstance(instance, &pass_through)) { + return PassThroughDestroyInstance(instance); + } return OpenXrLayer::Instance().DestroyInstance(instance); } @@ -73,6 +152,10 @@ std::string CensusFilled(uint32_t capacity, const uint32_t* count, const T* valu XrResult XRAPI_CALL DepthxrCreateSession(XrInstance instance, const XrSessionCreateInfo* create_info, XrSession* session) { + if (XrResult result; + ForwardPassThrough(instance, "xrCreateSession", result, create_info, session)) { + return result; + } OpenXrLayer& layer = OpenXrLayer::Instance(); if (!layer.HandshakeCensusActive()) { return layer.CreateSession(instance, create_info, session); @@ -140,6 +223,11 @@ XrResult XRAPI_CALL DepthxrEndFrame(XrSession session, const XrFrameEndInfo* fra XrResult XRAPI_CALL DepthxrGetSystemProperties(XrInstance instance, XrSystemId system_id, XrSystemProperties* properties) { + if (XrResult result; + ForwardPassThrough(instance, "xrGetSystemProperties", result, system_id, + properties)) { + return result; + } OpenXrLayer& layer = OpenXrLayer::Instance(); if (!layer.HandshakeCensusActive()) { return layer.GetSystemProperties(instance, system_id, properties); @@ -159,6 +247,14 @@ XrResult XRAPI_CALL DepthxrEnumerateEnvironmentBlendModes( uint32_t environment_blend_mode_capacity_input, uint32_t* environment_blend_mode_count_output, XrEnvironmentBlendMode* environment_blend_modes) { + if (XrResult result; + ForwardPassThrough(instance, "xrEnumerateEnvironmentBlendModes", result, + system_id, view_configuration_type, + environment_blend_mode_capacity_input, + environment_blend_mode_count_output, + environment_blend_modes)) { + return result; + } OpenXrLayer& layer = OpenXrLayer::Instance(); if (layer.HandshakeCensusActive()) { layer.CensusCall(); @@ -186,6 +282,13 @@ XrResult XRAPI_CALL DepthxrEnumerateViewConfigurations(XrInstance instance, uint32_t view_configuration_type_capacity_input, uint32_t* view_configuration_type_count_output, XrViewConfigurationType* view_configuration_types) { + if (XrResult result; + ForwardPassThrough(instance, "xrEnumerateViewConfigurations", result, + system_id, view_configuration_type_capacity_input, + view_configuration_type_count_output, + view_configuration_types)) { + return result; + } OpenXrLayer& layer = OpenXrLayer::Instance(); if (layer.HandshakeCensusActive()) { layer.CensusCall(); @@ -211,6 +314,12 @@ XrResult XRAPI_CALL DepthxrGetViewConfigurationProperties( XrSystemId system_id, XrViewConfigurationType view_configuration_type, XrViewConfigurationProperties* configuration_properties) { + if (XrResult result; + ForwardPassThrough(instance, "xrGetViewConfigurationProperties", result, + system_id, view_configuration_type, + configuration_properties)) { + return result; + } OpenXrLayer& layer = OpenXrLayer::Instance(); if (!layer.HandshakeCensusActive()) { return layer.GetViewConfigurationProperties(instance, system_id, view_configuration_type, @@ -236,6 +345,12 @@ XrResult XRAPI_CALL DepthxrEnumerateViewConfigurationViews(XrInstance instance, uint32_t view_capacity_input, uint32_t* view_count_output, XrViewConfigurationView* views) { + if (XrResult result; + ForwardPassThrough(instance, "xrEnumerateViewConfigurationViews", result, + system_id, view_configuration_type, + view_capacity_input, view_count_output, views)) { + return result; + } OpenXrLayer& layer = OpenXrLayer::Instance(); if (!layer.HandshakeCensusActive()) { return layer.EnumerateViewConfigurationViews(instance, system_id, view_configuration_type, @@ -362,6 +477,10 @@ F CensusNextFunction(OpenXrLayer::CensusFunction function) { } XrResult XRAPI_CALL CensusGetInstanceProperties(XrInstance instance, XrInstanceProperties* properties) { + if (XrResult result; + ForwardPassThrough(instance, "xrGetInstanceProperties", result, properties)) { + return result; + } const auto next = CensusNextFunction(OpenXrLayer::CensusFunction::kGetInstanceProperties); if (!next) { @@ -383,6 +502,10 @@ XrResult XRAPI_CALL CensusGetInstanceProperties(XrInstance instance, XrInstanceP } XrResult XRAPI_CALL CensusGetSystem(XrInstance instance, const XrSystemGetInfo* get_info, XrSystemId* system_id) { + if (XrResult result; + ForwardPassThrough(instance, "xrGetSystem", result, get_info, system_id)) { + return result; + } const auto next = CensusNextFunction(OpenXrLayer::CensusFunction::kGetSystem); if (!next) { return XR_ERROR_FUNCTION_UNSUPPORTED; @@ -487,6 +610,11 @@ XrResult XRAPI_CALL DepthxrDestroyActionSet(XrActionSet set) { return OpenXrLayer::Instance().DestroyActionSet(set); } XrResult XRAPI_CALL DepthxrSuggestInteractionProfileBindings(XrInstance instance, const XrInteractionProfileSuggestedBinding* bindings) { + if (XrResult result; + ForwardPassThrough(instance, "xrSuggestInteractionProfileBindings", result, + bindings)) { + return result; + } return OpenXrLayer::Instance().SuggestInteractionProfileBindings(instance, bindings); } XrResult XRAPI_CALL DepthxrCreateActionSpace(XrSession session, const XrActionSpaceCreateInfo* info, XrSpace* space) { @@ -494,6 +622,10 @@ XrResult XRAPI_CALL DepthxrCreateActionSpace(XrSession session, const XrActionSp } XrResult XRAPI_CALL DepthxrPollEvent(XrInstance instance, XrEventDataBuffer* event_data) { + if (XrResult result; + ForwardPassThrough(instance, "xrPollEvent", result, event_data)) { + return result; + } return OpenXrLayer::Instance().PollEvent(instance, event_data); } @@ -594,12 +726,23 @@ bool OpenXrToolkitBypasses(const XrInstanceCreateInfo& create_info) { bypass != 0; } +// Quad-Views-Foveated (its manifest's and DLL's name), above or below +// VectorXR, as for the Toolkit: support can then tell who does the quad views. +std::string QuadViewsFoveatedPosition(const XrApiLayerNextInfo* next_info) { + for (const XrApiLayerNextInfo* below = next_info ? next_info->next : nullptr; below; below = below->next) { + if (LayerName(*below) == "XR_APILAYER_MBUCCHIA_quad_views_foveated") { + return "below"; + } + } + return GetModuleHandleW(L"XR_APILAYER_MBUCCHIA_quad_views_foveated.dll") ? "above" : "absent"; +} + // The loader loads every layer's library before it creates the chain, so a // loaded Toolkit that is not below VectorXR is above it. std::string OpenXrToolkitPosition(const XrInstanceCreateInfo& create_info, const XrApiLayerNextInfo* next_info) { for (const XrApiLayerNextInfo* below = next_info ? next_info->next : nullptr; below; below = below->next) { if (LayerName(*below) == "XR_APILAYER_MBUCCHIA_toolkit") { - return "below"; + return OpenXrToolkitBypasses(create_info) ? "below-bypassed" : "below"; } } if (!GetModuleHandleW(L"XR_APILAYER_MBUCCHIA_toolkit.dll")) { @@ -921,6 +1064,22 @@ static XrResult XRAPI_CALL LayerGetInstanceProcAddr(XrInstance instance, const c } XrResult XRAPI_CALL xrGetInstanceProcAddr(XrInstance instance, const char* name, PFN_xrVoidFunction* function) { + // An instance VectorXR passes straight through: the functions below it, + // but its xrDestroyInstance forgets it first (a handle can come back). + depthxr::PassThroughInstance pass_through; + if (instance != XR_NULL_HANDLE && name && function && + depthxr::FindPassThroughInstance(instance, &pass_through)) { + const std::string_view requested(name); + if (requested == "xrDestroyInstance") { + *function = reinterpret_cast(depthxr::PassThroughDestroyInstance); + return XR_SUCCESS; + } + if (requested == "xrGetInstanceProcAddr") { + *function = reinterpret_cast(xrGetInstanceProcAddr); + return XR_SUCCESS; + } + return pass_through.next_get_instance_proc_addr(instance, name, function); + } const XrResult result = LayerGetInstanceProcAddr(instance, name, function); depthxr::OpenXrLayer& layer = depthxr::OpenXrLayer::Instance(); if (layer.HandshakeCensusActive() && name) { @@ -952,8 +1111,37 @@ XrResult XRAPI_CALL xrCreateApiLayerInstance(const XrInstanceCreateInfo* instanc return XR_ERROR_INITIALIZATION_FAILED; } + const std::string application_name( + instance_create_info->applicationInfo.applicationName, + strnlen(instance_create_info->applicationInfo.applicationName, XR_MAX_APPLICATION_NAME_SIZE)); + if (OpenXrLayer::RetiredInstance retired; + !OpenXrLayer::Instance().CanCreateInstance() && + OpenXrLayer::Instance().RetireIdleInstance(application_name, &retired)) { + // A probe instance kept alive above (see PassThroughInstance): it + // passes through from here, and this instance is VectorXR's. + PassThroughInstance pass_through; + pass_through.next_get_instance_proc_addr = retired.next_get_instance_proc_addr; + pass_through.next_destroy_instance = retired.next_destroy_instance; + RememberPassThroughInstance(retired.instance, pass_through); + } if (!OpenXrLayer::Instance().CanCreateInstance()) { - return XR_ERROR_LIMIT_REACHED; + // A second instance while VectorXR's can have a session: straight + // through, untouched. + XrApiLayerCreateInfo pass_through_info = *layer_info; + pass_through_info.nextInfo = next_info->next; + const XrResult result = + next_info->nextCreateApiLayerInstance(instance_create_info, &pass_through_info, instance); + if (XR_SUCCEEDED(result)) { + PassThroughInstance pass_through; + pass_through.next_get_instance_proc_addr = next_info->nextGetInstanceProcAddr; + PFN_xrVoidFunction destroy = nullptr; + if (XR_SUCCEEDED(next_info->nextGetInstanceProcAddr(*instance, "xrDestroyInstance", &destroy))) { + pass_through.next_destroy_instance = reinterpret_cast(destroy); + } + RememberPassThroughInstance(*instance, pass_through); + } + OpenXrLayer::Instance().NotePassThroughInstance(application_name, result); + return result; } OpenXrLayer::Instance().SetNextProcAddr(next_info->nextGetInstanceProcAddr); @@ -972,6 +1160,7 @@ XrResult XRAPI_CALL xrCreateApiLayerInstance(const XrInstanceCreateInfo* instanc diagnostics.layers_below += std::string(below->layerName, strnlen(below->layerName, XR_MAX_API_LAYER_NAME_SIZE)); } diagnostics.openxr_toolkit = OpenXrToolkitPosition(*instance_create_info, next_info); + diagnostics.quad_views_foveated = QuadViewsFoveatedPosition(next_info); diagnostics.app_requested_quad_views = ApplicationRequestedExtension(instance_create_info, XR_VARJO_QUAD_VIEWS_EXTENSION_NAME); diagnostics.app_requested_varjo_foveated_rendering = @@ -1265,6 +1454,9 @@ XrResult XRAPI_CALL xrCreateApiLayerInstance(const XrInstanceCreateInfo* instanc "xrCreateInstance extensions: " + DescribeInstanceExtensions(requested, probe_scan.complete, probe_scan.detail, probe_scan.names, sent)); } + // A handle a pass-through instance had (destroyed some other way) is + // this instance's now. + ForgetPassThroughInstance(*instance); // The runtime's instance exists from here. An exception must never leave // the layer (the loader turns it into a failure, or the game ends), and a // failure must not keep the instance: the runtime's is destroyed and the diff --git a/layer/src/openxr_layer.cpp b/layer/src/openxr_layer.cpp index 13762dfc..23866935 100644 --- a/layer/src/openxr_layer.cpp +++ b/layer/src/openxr_layer.cpp @@ -24,6 +24,7 @@ #include "depthxr/crt_failure_report.h" #include "depthxr/effects.h" #include "depthxr/framegen_plan.h" +#include "depthxr/game_context_lock.h" #include "depthxr/input_devices.h" #include "depthxr/process_exit.h" #include "depthxr/process_info.h" @@ -61,6 +62,14 @@ bool NearlyEqual(double lhs, double rhs) { return std::abs(lhs - rhs) < 0.0001; } +// Runtimes whose Direct3D 12 xrAcquire/xrWaitSwapchainImage queue nothing on +// the session's queue, which gets work for an image only at its first +// xrEnumerateSwapchainImages (PimaxXR 0.4.x and HEAD, Virtual Desktop's, +// whose names start so: "PimaxXR (Unofficial)", "VirtualDesktopXR"). +bool RuntimeQueuesOnlyFirstUse(std::string_view runtime_name) { + return runtime_name.rfind("PimaxXR", 0) == 0 || runtime_name.rfind("VirtualDesktopXR", 0) == 0; +} + template double PositionSeparationMeters(const Position& left, const Position& right) { const double x = left.x - right.x; @@ -202,13 +211,12 @@ AngularWindow ClampProjectedWindow(float base_negative, return {std::atan(negative_tan), std::atan(positive_tan)}; } -XrFovf BuildFocusFov(const XrFovf& base_fov, - const QuadViewsSettings& settings, - double focus_yaw_radians, - double focus_pitch_radians) { - const double horizontal_center = DegreesToRadians(settings.horizontal_offset_degrees) + focus_yaw_radians; - const double vertical_center = DegreesToRadians(settings.vertical_offset_degrees) + focus_pitch_radians; - +// The focus window centred at `horizontal_center` / `vertical_center`, angles +// in the eye's own tangent space. +XrFovf BuildFocusFovAt(const XrFovf& base_fov, + const QuadViewsSettings& settings, + double horizontal_center, + double vertical_center) { const AngularWindow horizontal = ClampProjectedWindow(base_fov.angleLeft, base_fov.angleRight, @@ -228,6 +236,16 @@ XrFovf BuildFocusFov(const XrFovf& base_fov, }; } +XrFovf BuildFocusFov(const XrFovf& base_fov, + const QuadViewsSettings& settings, + double focus_yaw_radians, + double focus_pitch_radians) { + return BuildFocusFovAt(base_fov, + settings, + DegreesToRadians(settings.horizontal_offset_degrees) + focus_yaw_radians, + DegreesToRadians(settings.vertical_offset_degrees) + focus_pitch_radians); +} + template void SafeRelease(T*& pointer) { if (pointer) { @@ -612,8 +630,9 @@ const void* FindStructInChain(const void* next, XrStructureType type) { return nullptr; } -// A frame without the depth of some swapchains (a bridged session's depth -// stays the game's, see D3D11Bridge), and the copies it needs. +// A frame without some of its depth (a bridged session's depth that stays +// the game's, see D3D11Bridge, or whose sub-image lies outside its image), +// and the copies it needs. struct DepthlessFrame { XrFrameEndInfo info{XR_TYPE_FRAME_END_INFO}; std::vector layers; @@ -621,15 +640,26 @@ struct DepthlessFrame { std::deque> views; }; -// `frame` itself when no projection view's depth is to be dropped. A view's -// chain loses its depth structure when that comes first, otherwise the whole -// chain (another structure ahead of it cannot be copied without knowing it). +// `frame` itself when no projection view's depth is to be dropped (`drop` +// of its XrCompositionLayerDepthInfoKHR). A projection with any view's depth +// dropped loses every view's, so the runtime never gets depth for one eye +// and not the other. A view's chain loses its depth structure when that +// comes first, otherwise the whole chain (another structure ahead of it +// cannot be copied without knowing it). template const XrFrameEndInfo* WithoutDepth(const XrFrameEndInfo* frame, DepthlessFrame& storage, const Drop& drop) { const auto dropped = [&](const XrCompositionLayerProjectionView& view) { for (auto* base = static_cast(view.next); base; base = base->next) { if (base->type == XR_TYPE_COMPOSITION_LAYER_DEPTH_INFO_KHR && - drop(reinterpret_cast(base)->subImage.swapchain)) { + drop(*reinterpret_cast(base))) { + return true; + } + } + return false; + }; + const auto has_depth = [](const XrCompositionLayerProjectionView& view) { + for (auto* base = static_cast(view.next); base; base = base->next) { + if (base->type == XR_TYPE_COMPOSITION_LAYER_DEPTH_INFO_KHR) { return true; } } @@ -661,7 +691,7 @@ const XrFrameEndInfo* WithoutDepth(const XrFrameEndInfo* frame, DepthlessFrame& } storage.views.emplace_back(projection->views, projection->views + projection->viewCount); for (XrCompositionLayerProjectionView& view : storage.views.back()) { - if (dropped(view)) { + if (has_depth(view)) { const auto* first = static_cast(view.next); view.next = first->type == XR_TYPE_COMPOSITION_LAYER_DEPTH_INFO_KHR ? first->next : nullptr; } @@ -674,6 +704,72 @@ const XrFrameEndInfo* WithoutDepth(const XrFrameEndInfo* frame, DepthlessFrame& return &storage.info; } +// Why `sub` does not lie inside an image of `width`x`height` with +// `array_size` slices (empty: it does), as a runtime checks a submitted +// depth sub-image (PimaxXR's and Virtual Desktop's xrEndFrame fail with +// XR_ERROR_SWAPCHAIN_RECT_INVALID otherwise): offsets from zero, extents of +// at least a texel, its far edges inside the image (without overflow), and +// its slice one of the image's. +std::string SubImageOutsideImage(const XrSwapchainSubImage& sub, std::uint32_t width, std::uint32_t height, + std::uint32_t array_size) { + const XrRect2Di& rect = sub.imageRect; + const std::int64_t right = static_cast(rect.offset.x) + rect.extent.width; + const std::int64_t bottom = static_cast(rect.offset.y) + rect.extent.height; + if (rect.offset.x >= 0 && rect.offset.y >= 0 && rect.extent.width > 0 && rect.extent.height > 0 && + right <= static_cast(width) && bottom <= static_cast(height) && + sub.imageArrayIndex < std::max(1u, array_size)) { + return {}; + } + return "rect " + std::to_string(rect.offset.x) + "," + std::to_string(rect.offset.y) + " " + + std::to_string(rect.extent.width) + "x" + std::to_string(rect.extent.height) + " slice " + + std::to_string(sub.imageArrayIndex) + " on a " + std::to_string(width) + "x" + std::to_string(height) + + " image of " + std::to_string(std::max(1u, array_size)) + " slices"; +} + +// What WithSubmittableDepth left out, for the log. +struct LeftOutDepth { + bool kept_private{false}; // depth a bridge keeps the game's + std::string outside; // the first depth sub-image outside its image: why (SubImageOutsideImage) +}; + +// `frame` without the depth no runtime may be given, in any session that +// forwards XR_KHR_composition_layer_depth: depth a bridge keeps the game's +// (`kept_private(swapchain)`), and depth whose sub-image lies outside its +// image (`image_of(swapchain, width, height, array_size)`, false for a +// swapchain the layer does not know: left as it is). A runtime refuses the +// whole frame for the latter (PimaxXR and Virtual Desktop: +// XR_ERROR_SWAPCHAIN_RECT_INVALID); UEVR places its depth rect by its colour +// image's size, which an upscaler or screen percentage makes larger than its +// depth image. A projection with any such depth loses every view's +// (WithoutDepth), so no eye has depth the other has not. Never clamped: a +// smaller rect would no longer match its colour, and depth reprojection would +// misplace what it moves. `frame` itself when nothing is left out. +template +const XrFrameEndInfo* WithSubmittableDepth(const XrFrameEndInfo* frame, DepthlessFrame& storage, + const KeptPrivate& kept_private, const ImageOf& image_of, + LeftOutDepth& left_out) { + return WithoutDepth(frame, storage, [&](const XrCompositionLayerDepthInfoKHR& depth) { + if (kept_private(depth.subImage.swapchain)) { + left_out.kept_private = true; + return true; + } + std::uint32_t width = 0; + std::uint32_t height = 0; + std::uint32_t array_size = 0; + if (!image_of(depth.subImage.swapchain, width, height, array_size)) { + return false; + } + std::string why = SubImageOutsideImage(depth.subImage, width, height, array_size); + if (why.empty()) { + return false; + } + if (left_out.outside.empty()) { + left_out.outside = std::move(why); + } + return true; + }); +} + const void* StripVarjoFoveatedViewLocateNextChain(const void* next) { const auto* header = reinterpret_cast(next); if (!header) { @@ -1214,6 +1310,24 @@ GazeRayAngles ExtractGazeRayAngles(const XrQuaternionf& orientation, bool flip_z }; } +// Where a direction `yaw` / `pitch` from head-forward (VIEW) falls in an eye +// turned `eye_in_view` from the head: angles in the eye's own tangent space, +// as Quad-Views-Foveated's and PimaxXR's ProjectPoint place the gaze (here a +// direction, a point at infinity, so the eye's offset from the head's centre +// drops out). None when the direction is not in front of the eye. +std::optional> ProjectViewDirectionIntoEye(const XrQuaternionf& eye_in_view, + double yaw_radians, + double pitch_radians) { + const XrVector3f direction{static_cast(std::tan(yaw_radians)), + static_cast(std::tan(pitch_radians)), -1.0f}; + const XrVector3f in_eye = RotateVector(ConjugateQuaternion(NormalizeQuaternion(eye_in_view)), direction); + if (in_eye.z > -0.000001f) { + return std::nullopt; + } + return std::pair{std::atan2(static_cast(in_eye.x), -static_cast(in_eye.z)), + std::atan2(static_cast(in_eye.y), -static_cast(in_eye.z))}; +} + XrQuaternionf YawQuaternion(float yaw_radians) { const float half = yaw_radians * 0.5f; return {0.0f, std::sin(half), 0.0f, std::cos(half)}; @@ -1583,6 +1697,21 @@ constexpr uint32_t kEyeGazeUnavailableLogThreshold = 30; // after which the runtime is taken to be returning a stale pose. Live eye // tracking jitters every sample. ~1/2s at 90Hz. constexpr uint32_t kEyeGazeUnfocusedFrozenFrames = 45; +// The same, while focused: frames (display times) in a row with the very same +// gaze, after which a tracker the runtime still reports valid is taken to +// have stopped (PimaxXR's Droolon / aSeeVR path keeps its last sample once +// ready). Far past any fixation: live gaze never repeats bit for bit. ~2 s +// at 90 Hz. +constexpr uint32_t kEyeGazeFocusedFrozenFrames = 180; +// Diagnostic only: a focused, valid gaze that stays this close to one point +// (either angle) for this long. The Oct 29 M3 flight (headset taken off) showed +// ~26 s of such a gaze, wobbling by about 0.002 rad, before it froze bit for +// bit, and even then it moved by under 0.001 rad now and then, which resets the +// bit-for-bit count. Whether live gaze ever stays this still is what the line +// is for: it changes nothing. +constexpr double kEyeGazeStillRadians = 0.003; +constexpr XrDuration kEyeGazeStillWindow = 10'000'000'000; +constexpr int kEyeGazeStillLogBudget = 8; // How long a focused session's bound eye-gaze input may stay inactive before // the log says the eye tracker is sending no data. constexpr std::chrono::seconds kEyeGazeNoDataLogDelay{10}; @@ -1754,6 +1883,8 @@ bool SameSettings(const ResolvedRuntimeConfig& lhs, const ResolvedRuntimeConfig& lhs.framegen.artifact_suppression == rhs.framegen.artifact_suppression && lhs.framegen.motion_limit_percent == rhs.framegen.motion_limit_percent && lhs.framegen.extend_head_prediction == rhs.framegen.extend_head_prediction && + lhs.framegen.edge_fill == rhs.framegen.edge_fill && + lhs.framegen.use_game_depth == rhs.framegen.use_game_depth && lhs.framegen.diagnostic_view == rhs.framegen.diagnostic_view && SameInputBindings(lhs.framegen.toggle_bindings, rhs.framegen.toggle_bindings); } @@ -1890,6 +2021,44 @@ void AppendViewSummary(std::ostringstream& stream, std::span PimaxXrSetting(const char* name) { + DWORD value = 0; + DWORD size = sizeof(value); + if (RegGetValueA(HKEY_LOCAL_MACHINE, "SOFTWARE\\PimaxXR", name, RRF_RT_REG_DWORD, nullptr, &value, &size) != + ERROR_SUCCESS) { + return std::nullopt; + } + return value; +} + +// The PimaxXR settings that shape its frame loop, as one log line: each +// "name=value", or "name=default (value)" when unset (pimax-openxr 0.4.4 +// session.cpp and instance.cpp defaults). +std::string PimaxXrFrameSettingsText() { + struct Setting { + const char* name; + DWORD fallback; + }; + static constexpr Setting kSettings[] = { + {"defer_frame_wait", 0}, // "Prefer framerate over latency" + {"quirk_sync_gpu_work_in_end_frame", 0}, // "Wait for GPU work in EndFrame" + {"async_submission", 1}, + {"quirk_complete_discarded_frames", 1}, + {"quirk_disable_running_start", 0}, + {"lock_framerate", 0}, + }; + std::string text; + for (const Setting& setting : kSettings) { + const std::optional value = PimaxXrSetting(setting.name); + if (!text.empty()) text += ", "; + text += std::string(setting.name) + "=" + + (value ? std::to_string(*value) : "default (" + std::to_string(setting.fallback) + ")"); + } + return text; +} + } // namespace OpenXrLayer& OpenXrLayer::Instance() { @@ -1915,6 +2084,8 @@ OpenXrLayer& OpenXrLayer::Instance() { return *owner.layer; } +OpenXrLayer::OpenXrLayer() : game_context_(std::make_unique()) {} + OpenXrLayer::~OpenXrLayer() { // Safety net for an unload without xrDestroyInstance: join the watcher // before members are destroyed so a still-joinable std::thread can't trip @@ -2062,11 +2233,31 @@ bool OpenXrLayer::PollInputBindingDown(const InputBinding& binding) { return poll.down; } +bool OpenXrLayer::RuntimeTakesFocusFovsFromLocateLocked() const { + // PimaxXR (0.4.4, quad views on), for an application named "DCS World", + // keeps the focus FOVs each quad xrLocateViews returns for its display + // time and shows the focus views with those at xrEndFrame, not with the + // submitted ones (DCS submits other ones; Quad-Views-Foveated does the + // same, but VectorXR never forwards quad views to it). Only a runtime + // that names itself Pimax's ("PimaxXR (Unofficial)", Pimax Play's) is + // taken to: Varjo's and any other native quad views runtime keep + // VectorXR's behaviour. + return varjo_compatible_quadviews_active_ && application_name_ == "DCS World" && + runtime_name_.find("Pimax") != std::string::npos; +} + bool OpenXrLayer::CanCreateInstance() { std::scoped_lock lock(mutex_); return instance_ == XR_NULL_HANDLE; } +void OpenXrLayer::NotePassThroughInstance(const std::string& application_name, XrResult result) { + logger_.Info("A second OpenXR instance (application \"" + application_name + "\", result=" + + std::to_string(static_cast(result)) + + ") was created while VectorXR's, which can have a session, is alive; it passes straight through " + "VectorXR, which stays with its first instance."); +} + XrResult OpenXrLayer::OnInstanceCreated(const XrInstanceCreateInfo* create_info, XrInstance instance, bool eye_gaze_extension_enabled, @@ -2095,6 +2286,9 @@ XrResult OpenXrLayer::OnInstanceCreated(const XrInstanceCreateInfo* create_info, spacewarp_ext_owned_ = diagnostics.frame_synthesis_owned; openxr_toolkit_above_ = diagnostics.openxr_toolkit == "above"; openxr_toolkit_quadviews_logged_ = false; + openxr_toolkit_below_ = diagnostics.openxr_toolkit == "below"; + openxr_toolkit_framegen_logged_ = false; + layers_below_ = diagnostics.layers_below; resolved_settings_generation_ = ~0ull; // resolve again with the Toolkit's position ResetSessionState(); config_path_ = ResolveConfigPath(); @@ -2105,6 +2299,10 @@ XrResult OpenXrLayer::OnInstanceCreated(const XrInstanceCreateInfo* create_info, logger_.Info(std::string("VectorXR layer version: ") + VECTORXR_VERSION); logger_.Info(std::string("VectorXR layer build: ") + __DATE__ + " " + __TIME__); logger_.Info("VectorXR attached to process: " + current_exe_name_); + if (!retired_instance_note_.empty()) { + logger_.Info(retired_instance_note_); + retired_instance_note_.clear(); + } { const ProcessInteropSnapshot interop = GetProcessInteropSnapshot(); std::ostringstream stream; @@ -2138,6 +2336,8 @@ XrResult OpenXrLayer::OnInstanceCreated(const XrInstanceCreateInfo* create_info, ExtensionRequested(create_info, XR_KHR_D3D11_ENABLE_EXTENSION_NAME); std::ostringstream stream; + application_name_.assign(create_info->applicationInfo.applicationName, + strnlen(create_info->applicationInfo.applicationName, XR_MAX_APPLICATION_NAME_SIZE)); const XrVersion api_version = create_info->applicationInfo.apiVersion; application_can_select_quad_views_ = quad_views_extension_requested_ || varjo_foveated_rendering_extension_requested_ || @@ -2150,6 +2350,12 @@ XrResult OpenXrLayer::OnInstanceCreated(const XrInstanceCreateInfo* create_info, logger_.Info(stream.str()); instance_enabled_extension_count_ = create_info->enabledExtensionCount; + for (const char* extension : {"XR_KHR_D3D11_enable", "XR_KHR_D3D12_enable", "XR_KHR_vulkan_enable", + "XR_KHR_vulkan_enable2", "XR_KHR_opengl_enable", "XR_KHR_opengl_es_enable", + "XR_MND_headless", "XR_EXT_headless"}) { + instance_enabled_session_extension_ = + instance_enabled_session_extension_ || ExtensionRequested(create_info, extension); + } std::ostringstream ext_stream; ext_stream << "Application enabled OpenXR extensions (" << create_info->enabledExtensionCount << "):"; for (uint32_t i = 0; i < create_info->enabledExtensionCount; ++i) { @@ -2159,12 +2365,20 @@ XrResult OpenXrLayer::OnInstanceCreated(const XrInstanceCreateInfo* create_info, } logger_.Info(ext_stream.str()); logger_.Info("OpenXR API layers below VectorXR: [" + diagnostics.layers_below + "]"); + if (diagnostics.quad_views_foveated == "above") { + logger_.Info("Quad-Views-Foveated is loaded above VectorXR: for a Direct3D 11 application it does the " + "quad views itself and VectorXR sees a stereo application."); + } else if (diagnostics.quad_views_foveated == "below") { + logger_.Info("Quad-Views-Foveated is loaded below VectorXR: it sees the views VectorXR sends on."); + } if (diagnostics.openxr_toolkit != "absent") { logger_.Info("OpenXR Toolkit is loaded " + - std::string(diagnostics.openxr_toolkit == "below" ? "below VectorXR." - : diagnostics.openxr_toolkit == "above" - ? "above VectorXR." - : "above VectorXR, bypassed for this application.")); + std::string(diagnostics.openxr_toolkit == "below" ? "below VectorXR." + : diagnostics.openxr_toolkit == "below-bypassed" ? "below VectorXR, bypassed for " + "this application." + : diagnostics.openxr_toolkit == "above" ? "above VectorXR." + : "above VectorXR, bypassed for this " + "application.")); } // The bridge the instance's XR_KHR_D3D12_enable is for, and what Frame // Generation does without it. @@ -2381,6 +2595,24 @@ XrResult OpenXrLayer::OnInstanceCreated(const XrInstanceCreateInfo* create_info, if (defer_quadviews_swapchain_releases_) { logger_.Info("Varjo runtime detected; deferring app quadviews swapchain releases until xrEndFrame."); } + if (runtime_name_.find("PimaxXR") != std::string::npos) { + // Its frame loop changes with these (defer_frame_wait: its + // xrEndFrame waits for the refresh instead of xrWaitFrame). + logger_.Info("PimaxXR frame settings: " + PimaxXrFrameSettingsText() + "."); + // Up to 0.4.4, a frame begun and never ended becomes a PVR + // frame with one disabled layer (a blank refresh) and a + // refresh's wait more (pimax-openxr frame.cpp + // asyncSubmissionThread, quirk_complete_discarded_frames; + // later versions repeat the last layers instead). + const bool blanks = rt < XR_MAKE_VERSION(0, 4, 5) && + PimaxXrSetting("async_submission").value_or(1) != 0 && + PimaxXrSetting("quirk_complete_discarded_frames").value_or(1) != 0; + framegen_.SetRuntimeBlanksOpenFrames(blanks); + if (blanks) { + logger_.Info("Frame Generation: this PimaxXR shows a blank refresh for a frame left open, so its " + "presenter thread leaves none open while anything consistent can be shown."); + } + } } else { logger_.Info("OpenXR runtime identity unavailable: xrGetInstanceProperties returned " + std::to_string(static_cast(props_result))); @@ -2444,8 +2676,36 @@ XrResult OpenXrLayer::GetInstanceProcAddr(XrInstance instance, const char* name, return next_get_instance_proc_addr_(instance, name, function); } +bool OpenXrLayer::RetireIdleInstance(const std::string& next_application_name, RetiredInstance* retired) { + { + std::scoped_lock lock(mutex_); + if (instance_ == XR_NULL_HANDLE || instance_requested_session_ || instance_enabled_session_extension_ || + active_session_ != XR_NULL_HANDLE) { + return false; + } + retired->instance = instance_; + retired->next_get_instance_proc_addr = next_get_instance_proc_addr_; + retired->next_destroy_instance = next_destroy_instance_; + retired_instance_note_ = + "An earlier OpenXR instance (application \"" + application_name_ + "\", " + + std::to_string(instance_enabled_extension_count_) + + " extensions, no graphics extension, no session) was alive as \"" + next_application_name + + "\" created this one: it passes straight through VectorXR from here and VectorXR is this instance's. " + "OpenXR Toolkit and Quad-Views-Foveated keep such a probe instance alive on VIVE runtimes."; + } + logger_.Info("Another OpenXR instance (application \"" + next_application_name + + "\") is being created; this instance has no graphics extension and no session, so VectorXR moves to " + "the new one and this one passes straight through."); + EndInstance(retired->instance, false); + return true; +} + XrResult OpenXrLayer::DestroyInstance(XrInstance instance) { logger_.Info("xrDestroyInstance requested by application."); + return EndInstance(instance, true); +} + +XrResult OpenXrLayer::EndInstance(XrInstance instance, bool in_runtime) { if (handshake_census_.Active()) { CensusFlushProcAddrs("since the last of them"); if (const std::string repeats = handshake_census_.ProcAddrRepeats(); !repeats.empty()) { @@ -2518,7 +2778,7 @@ XrResult OpenXrLayer::DestroyInstance(XrInstance instance) { return XR_SUCCESS; } watch.Step("in the runtime's xrDestroyInstance"); - const XrResult result = next_destroy_instance_(instance); + const XrResult result = in_runtime ? next_destroy_instance_(instance) : XR_SUCCESS; watch.Step("ending the bridges"); framegen_queue_.Reset(); EndD3D11Bridge(); @@ -2631,12 +2891,15 @@ XrResult OpenXrLayer::CreateSession(XrInstance instance, std::string vulkan_unbridged; bool vulkan_queried_d3d12 = false; // the Vulkan bridge asked for Direct3D 12's graphics requirements bool framegen_wanted = false; + bool use_game_depth = true; // the profile's, for the bridges { { std::scoped_lock lock(mutex_); ReloadConfigIfNeeded(); const ResolvedRuntimeConfig resolved = ResolveRuntimeConfig(config_, current_exe_name_); - framegen_wanted = resolved.core.enabled && resolved.framegen.enabled; + // OpenXR Toolkit below keeps it off (RefreshResolvedSettings). + framegen_wanted = resolved.core.enabled && resolved.framegen.enabled && !openxr_toolkit_below_; + use_game_depth = resolved.framegen.use_game_depth; } framegen_queue_.Reset(); d3d11_bridge_.End(); @@ -2656,6 +2919,7 @@ XrResult OpenXrLayer::CreateSession(XrInstance instance, } else { std::string reason; d3d11_bridge_.SetLog([this](const std::string& message) { logger_.Error(message); }); + d3d11_bridge_.SetKeepDepthPrivate(!use_game_depth); if (const XrSessionCreateInfo* bridged = d3d11_bridge_.Begin( instance, create_info, next_get_instance_proc_addr_, reason, bridge_description)) { game_info = bridged; @@ -2690,6 +2954,7 @@ XrResult OpenXrLayer::CreateSession(XrInstance instance, std::string reason; vulkan_bridge_.SetLog([this](const std::string& message) { logger_.Error(message); }); vulkan_bridge_.SetInfoLog([this](const std::string& message) { logger_.Info(message); }); + vulkan_bridge_.SetKeepDepthPrivate(!use_game_depth); const XrSessionCreateInfo* bridged = vulkan_bridge_.Begin( instance, create_info, next_get_instance_proc_addr_, game, reason, bridge_description); vulkan_queried_d3d12 = vulkan_bridge_.QueriedD3D12Requirements(); @@ -2708,6 +2973,22 @@ XrResult OpenXrLayer::CreateSession(XrInstance instance, // for the game's GPU work: on a GPU-bound game, a refresh missed // wherever the runtime's xrEndFrame waits for its queue (Varjo). downstream_info = framegen_queue_.Prepare(game_info, note, true); + if (framegen_queue_.Active() && RuntimeQueuesOnlyFirstUse(runtime_name_)) { + // PimaxXR and Virtual Desktop's runtime queue work for a game + // image only at its first enumeration (a barrier batch); they + // compose on a device of their own. Opt-in until flown + // (VECTORXR_FRAMEGEN_AFTER_WAIT=first): with OpenKneeboard's + // per-frame quad on the PimaxXR stand-in the game stalled for + // about a second now and then (notes, swapchains-fixes). + const char* after_wait = std::getenv("VECTORXR_FRAMEGEN_AFTER_WAIT"); + if (after_wait && std::string_view(after_wait) == "first") { + framegen_queue_.SetRuntimeQueuesOnlyFirstUse(true); + logger_.Info("Frame Generation: " + runtime_name_ + + " queues work for the game's images only before their first use, so the game's " + "queue waits for the runtime's only then, not behind Frame Generation's frames at " + "every image wait (VECTORXR_FRAMEGEN_AFTER_WAIT=first)."); + } + } const char* queue_diagnostics = std::getenv("VECTORXR_FRAMEGEN_QUEUE_DIAGNOSTICS"); framegen_queue_diagnostics_ = queue_diagnostics && std::string_view(queue_diagnostics) == "1"; framegen_queue_diagnostics_next_ms_.store(0, std::memory_order_relaxed); @@ -2775,6 +3056,9 @@ XrResult OpenXrLayer::CreateSession(XrInstance instance, if (!vulkan_bridge_.Active()) { requery_vulkan_requirements(); } + // Before the runtime's xrCreateSession: the new session's first event is + // queued in it, so anything stamped earlier is not this session's. + const XrTime session_create_time = TurboClockNow(); XrResult result = next_create_session_(instance, downstream_info, session); if (XR_FAILED(result) && d3d11_bridge_.Active()) { logger_.Error("xrCreateSession refused the bridged Direct3D 12 session (result=" + @@ -2869,8 +3153,10 @@ XrResult OpenXrLayer::CreateSession(XrInstance instance, } ResetSessionState(); active_session_ = *session; + NoteSessionCreatedLocked(session_create_time); d3d11_bridge_lost_logged_ = false; d3d11_bridge_depth_logged_ = false; + depth_rect_logged_ = false; d3d11_bridge_last_summary_ = std::chrono::steady_clock::now(); d3d11_bridge_quad_frames_ = {}; d3d11_bridge_quad_frames_summarized_ = {}; @@ -2884,6 +3170,13 @@ XrResult OpenXrLayer::CreateSession(XrInstance instance, FindStructInChain(create_info->next, XR_TYPE_GRAPHICS_BINDING_D3D11_KHR)) : nullptr; graphics_api_ = d3d11_binding && d3d11_binding->device ? "D3D11" : "Other"; + // The game's device's lock, taken before the layer's locks wherever its + // immediate context is used under them (bridged or not); Frame + // Generation's backend uses that context only in the game's own + // Direct3D 11 sessions (a bridged session's is Direct3D 12). + game_context_->Set(d3d11_binding ? d3d11_binding->device : nullptr); + framegen_.SetGameContextLock(d3d11_binding && d3d11_binding->device && !d3d11_bridge_.Active() ? game_context_.get() + : nullptr); if (d3d11_binding && d3d11_binding->device) { d3d11_quadviews_compositor_.device = d3d11_binding->device; d3d11_quadviews_compositor_.device->AddRef(); @@ -3067,6 +3360,16 @@ XrResult OpenXrLayer::DestroySession(XrSession session) { watch.Step("ending the bridges"); if (destroying_active && XR_SUCCEEDED(result)) { if (const std::string shown = framegen_queue_.ShownLayersSummary(); !shown.empty()) logger_.Info(shown); + const std::uint64_t game_frames = game_frames_ended_.load(std::memory_order_relaxed); + const std::uint64_t runtime_frames = runtime_frames_ended_.load(std::memory_order_relaxed); + if (layers_below_get_framegen_ && game_frames > 0) { + std::ostringstream ratio; + ratio << std::fixed << std::setprecision(2) + << static_cast(runtime_frames) / static_cast(game_frames); + logger_.Info("Frame Generation at session end: the runtime and the layers below VectorXR [" + + layers_below_ + "] got " + std::to_string(runtime_frames) + " frames for the game's " + + std::to_string(game_frames) + " (" + ratio.str() + " per game frame)."); + } framegen_queue_.Reset(); EndD3D11Bridge(); } @@ -3081,6 +3384,11 @@ XrResult OpenXrLayer::DestroySession(XrSession session) { if (session == active_session_) { ResetSwapchainState(); ResetSessionState(); + // As at xrEndSession, which an application may skip: PimaxXR and + // VDXR give the next session the same handle and can give its + // LOCAL space the same address, so a kept origin, anchor or + // engagement would pass for the next session's own. + ResetPivotActivationState(); } } @@ -4414,6 +4722,11 @@ XrResult OpenXrLayer::DestroySwapchain(XrSwapchain swapchain) { DrainTurboAsyncWait(kSwapchainDrainTimeout); { watch.Step("releasing the swapchain's deferred releases"); + // The Direct3D 11 bridge's work on the game's context, before mutex_ + // (D3D11Bridge::StageReleases); a release it did not stage runs under + // mutex_, the device's lock taken first (GameContextLock). + d3d11_bridge_.StageReleases(swapchain); + const GameContextHold context_hold = game_context_->Hold(); std::scoped_lock lock(mutex_); auto it = tracked_swapchains_.find(swapchain); if (it != tracked_swapchains_.end()) { @@ -4552,10 +4865,21 @@ XrResult OpenXrLayer::AcquireSwapchainImage(XrSwapchain swapchain, const XrSwapchainImageAcquireInfo* acquire_info, uint32_t* index) { const auto trace_call = turbo_trace_.Watch("app.acquireImage"); + const BridgeCallTimer timed(d3d11_bridge_.Active() ? &bridge_call_timing_[0] : nullptr); NoteThread(ThreadCensus::Call::AcquireSwapchainImage); std::uint32_t image_count = 0; { - std::scoped_lock lock(mutex_); + // The Direct3D 11 bridge's work on the game's context for the + // hand-over below, before any lock of the layer's (a game thread may + // hold the context's lock through an OpenXR call that takes them: + // D3D11Bridge::StageReleases). + d3d11_bridge_.StageReleases(swapchain); + // A release landing after it is handed over under the locks below: + // the device's lock first (GameContextLock). + const GameContextHold context_hold = game_context_->Hold(); + // The releases go on without mutex_ (ForwardHandedOverReleases). + std::unique_lock handover(bridge_handover_mutex_); + std::unique_lock lock(mutex_); auto it = tracked_swapchains_.find(swapchain); if (it != tracked_swapchains_.end()) { image_count = it->second.image_count; @@ -4563,14 +4887,21 @@ XrResult OpenXrLayer::AcquireSwapchainImage(XrSwapchain swapchain, // before goes on first (the runtime hands out an image only while // the application holds fewer than all, and the bridge copies // the image acquired earliest). - const XrResult handed = HandOverBridgedReleasesLocked(swapchain, it->second, "swapchain acquire"); + std::vector releases; + XrResult handed = + HandOverBridgedReleasesLocked(swapchain, it->second, "swapchain acquire", true, &releases); + const XrResult forwarded = ForwardHandedOverReleases(lock, releases, "swapchain acquire"); + if (XR_SUCCEEDED(handed)) handed = forwarded; if (XR_FAILED(handed)) { return handed; } - const XrResult flush_result = - FlushDeferredSwapchainReleaseLocked(swapchain, it->second, "swapchain acquire"); - if (XR_FAILED(flush_result)) { - return flush_result; + it = tracked_swapchains_.find(swapchain); + if (it != tracked_swapchains_.end()) { + const XrResult flush_result = + FlushDeferredSwapchainReleaseLocked(swapchain, it->second, "swapchain acquire"); + if (XR_FAILED(flush_result)) { + return flush_result; + } } } } @@ -4626,16 +4957,25 @@ XrResult OpenXrLayer::AcquireSwapchainImage(XrSwapchain swapchain, XrResult OpenXrLayer::WaitSwapchainImage(XrSwapchain swapchain, const XrSwapchainImageWaitInfo* wait_info) { const auto trace_call = turbo_trace_.Watch("app.waitImage"); + const BridgeCallTimer timed(d3d11_bridge_.Active() ? &bridge_call_timing_[1] : nullptr); NoteThread(ThreadCensus::Call::WaitSwapchainImage); { // An image acquired ahead is waited for once the one before it is // released (OpenXR's order): a bridged release kept for the frame's - // end goes on first, as at the next acquire. - std::scoped_lock lock(mutex_); + // end goes on first, as at the next acquire (without mutex_: + // ForwardHandedOverReleases). The Direct3D 11 bridge's work on the + // game's context first, as at an acquire. + d3d11_bridge_.StageReleases(swapchain); + const GameContextHold context_hold = game_context_->Hold(); + std::unique_lock handover(bridge_handover_mutex_); + std::unique_lock lock(mutex_); if (const auto it = tracked_swapchains_.find(swapchain); it != tracked_swapchains_.end() && !it->second.bridge_handovers.empty()) { - if (const XrResult handed = HandOverBridgedReleasesLocked(swapchain, it->second, "swapchain wait"); - XR_FAILED(handed)) { + std::vector releases; + XrResult handed = HandOverBridgedReleasesLocked(swapchain, it->second, "swapchain wait", true, &releases); + const XrResult forwarded = ForwardHandedOverReleases(lock, releases, "swapchain wait"); + if (XR_SUCCEEDED(handed)) handed = forwarded; + if (XR_FAILED(handed)) { return handed; } } @@ -4675,7 +5015,15 @@ XrResult OpenXrLayer::WaitSwapchainImage(XrSwapchain swapchain, const XrSwapchai XrResult OpenXrLayer::ReleaseSwapchainImage(XrSwapchain swapchain, const XrSwapchainImageReleaseInfo* release_info) { const auto trace_call = turbo_trace_.Watch("app.releaseImage"); + const BridgeCallTimer timed(d3d11_bridge_.Active() ? &bridge_call_timing_[2] : nullptr); NoteThread(ThreadCensus::Call::ReleaseSwapchainImage); + // In the game's own Direct3D 11 session, Frame Generation captures the + // released image on the game's context under mutex_: the device's lock + // first (GameContextLock). A bridged release only counts the release. + GameContextHold context_hold; + if (!d3d11_bridge_.Active()) { + context_hold = game_context_->Hold(); + } { std::scoped_lock lock(mutex_); const auto bridged = tracked_swapchains_.find(swapchain); @@ -4691,7 +5039,7 @@ XrResult OpenXrLayer::ReleaseSwapchainImage(XrSwapchain swapchain, bridged->second.bridge_handovers.push_back(*released_index); // The game may release on another thread than it ends frames // on: Direct3D 11's immediate context is then shared. - d3d11_bridge_.ReleaseCalled(); + d3d11_bridge_.ReleaseCalled(swapchain); if (framegen_.TracesSwapchainCalls()) { framegen_.Trace().Record("rel", reinterpret_cast(swapchain), *released_index, 2); } @@ -4733,6 +5081,8 @@ XrResult OpenXrLayer::ReleaseSwapchainImage(XrSwapchain swapchain, } } } + // Nothing below uses the game's context (the runtime's release may wait). + context_hold.Release(); const bool diag = TurboSequencedDebugTick(); if (diag) { @@ -6284,6 +6634,7 @@ XrResult OpenXrLayer::TraceRuntimeEndFrame(XrSession session, const XrFrameEndIn } XrResult OpenXrLayer::SubmitRuntimeFrame(XrSession session, const XrFrameEndInfo* info) { + runtime_frames_ended_.fetch_add(1, std::memory_order_relaxed); const bool queue_diagnostics = framegen_queue_diagnostics_ && framegen_queue_.Active(); std::vector submitted_swapchains; if (framegen_queue_.Active() && info) { @@ -6543,6 +6894,10 @@ XrResult OpenXrLayer::PaceWaitFrame(XrSession session, } const auto trace_id = turbo_trace_.NextId(); turbo_trace_.Record("wait.enter", trace_id); + if (turbo_frame_interception_required_.load(std::memory_order_acquire)) { + std::scoped_lock lock(turbo_mutex_); + upstream_turbo_.Wait(std::this_thread::get_id(), std::chrono::steady_clock::now()); + } const bool experimental_active = turbo_experiment_.enabled && turbo_effective_active_.load(std::memory_order_relaxed) && turbo_effective_async_.load(std::memory_order_relaxed); if (experimental_active && turbo_experiment_.frame_limit > 0) { @@ -6795,14 +7150,19 @@ XrResult OpenXrLayer::PaceWaitFrame(XrSession session, const auto steps = static_cast(unended) + (turbo_async_wait_completed_ ? 0 : 1); predicted += steps * turbo_prediction_history_.FrameStep(turbo_last_predicted_display_period_); extrapolated = steps > 0; - } else if (turbo_pacing_mode_ == TurboPacingMode::kSequenced && turbo_seq_wait_in_flight_ && + } else if (turbo_pacing_mode_ == TurboPacingMode::kSequenced && turbo_last_predicted_display_period_ > 0) { - // This app wait overlaps the post-submit runtime wait, so its - // frame ends a runtime frame after that wait's: hand it the - // prediction that wait is about to return, not the cached - // one a frame older (SteamVR rejects a time older than the - // previous frame's prediction). - predicted += turbo_last_predicted_display_period_; + // The async rule on the frame thread: each app end is + // followed by the wait+begin of the next runtime frame, + // so a frame waited with app frames still unended ends + // that many runtime frames after the cached one, and one + // more while the post-submit wait is still to return (the + // SteamVR fault: a frame older than the previous frame's + // prediction is rejected). DCS waits on its main thread + // up to two frames ahead of the render thread. + const auto unended = std::min(turbo_prediction_history_.Unended(), 2); + const auto steps = static_cast(unended) + (turbo_seq_wait_in_flight_ ? 1 : 0); + predicted += steps * turbo_prediction_history_.FrameStep(turbo_last_predicted_display_period_); } turbo_last_wait_frame_wall_time_ = now; @@ -6944,6 +7304,10 @@ XrResult OpenXrLayer::BeginFrame(XrSession session, const XrFrameBeginInfo* fram if (performance_.Wanted()) { performance_.Frames().Begin(static_cast(GetCurrentThreadId())); } + if (turbo_frame_interception_required_.load(std::memory_order_acquire)) { + std::scoped_lock lock(turbo_mutex_); + upstream_turbo_.Begin(std::chrono::steady_clock::now()); + } { std::scoped_lock lock(mutex_); if (session == active_session_) { @@ -7042,9 +7406,47 @@ void OpenXrLayer::ObserveCompositionLayerTopology(const XrFrameEndInfo* frame_en : ".")); } +bool OpenXrLayer::NoteUpstreamTurboFrame() { + std::scoped_lock lock(turbo_mutex_); + const bool seen = + upstream_turbo_.End(std::this_thread::get_id(), std::chrono::steady_clock::now(), openxr_toolkit_above_); + if (seen && !upstream_turbo_logged_) { + upstream_turbo_logged_ = true; + logger_.Info("Turbo: a layer above VectorXR pipelines the game's frames already (" + + std::to_string(upstream_turbo_.Matching()) + " of the last " + + std::to_string(upstream_turbo_.Frames()) + + " frames: each xrWaitFrame from another thread as the last xrEndFrame returns, each " + "xrBeginFrame right before its xrEndFrame" + + (openxr_toolkit_above_ ? "; OpenXR Toolkit is loaded above VectorXR" : "") + + "). That is OpenXR Toolkit's Turbo mode: VectorXR's Turbo stands aside for this session and " + "lets its pipelined frame go; turn one of the two off."); + } + return seen; +} + XrResult OpenXrLayer::ForwardEndFrame(XrSession session, const XrFrameEndInfo* frame_end_info, - std::unique_lock& config_lock) { + std::unique_lock& config_lock, + GameContextHold* context_hold) { + // EndFrame's hold of the game's context goes with config_lock: nothing + // after uses the context under the layer's locks, and the runtime's + // xrEndFrame (Frame Generation's frames, the OSD) may wait. + const auto unlock_config = [&] { + config_lock.unlock(); + if (context_hold) { + context_hold->Release(); + } + }; + // When the app's xrEndFrame returns (NoteUpstreamTurboFrame: a layer + // above waits right after); this is its tail. + struct EndReturned { + OpenXrLayer& layer; + ~EndReturned() { + if (!layer.turbo_frame_interception_required_.load(std::memory_order_acquire)) return; + std::scoped_lock lock(layer.turbo_mutex_); + layer.upstream_turbo_.EndReturned(std::chrono::steady_clock::now()); + } + } end_returned{*this}; // Runtime wait/begin can fail before TraceRuntimeEndFrame is reached: a // quad views frame Frame Generation claimed that never reached // TraceRuntimeEndFrame is abandoned. @@ -7090,7 +7492,7 @@ XrResult OpenXrLayer::ForwardEndFrame(XrSession session, if (timing_enabled) { pacing_start = std::chrono::steady_clock::now(); } - config_lock.unlock(); + unlock_config(); const XrResult result = TraceRuntimeEndFrame(session, frame_end_info); if (XR_FAILED(result) && end_frame_error_log_budget_ > 0) { --end_frame_error_log_budget_; @@ -7135,6 +7537,12 @@ XrResult OpenXrLayer::ForwardEndFrame(XrSession session, } bool turbo_engaged = IsTurboActive(); + // A layer above pipelining the game's frames already: Turbo stands aside + // (two pipelined frames would only add latency). + const bool upstream_turbo = NoteUpstreamTurboFrame(); + if (upstream_turbo) { + turbo_engaged = false; + } // Recovery is independent of Auto/forced/pinned pacing. Disabling the // setting bypasses only interrupted-session protection, not live stalls. if (!resolved_settings_.turbo.interrupted_session_recovery) { @@ -7169,6 +7577,9 @@ XrResult OpenXrLayer::ForwardEndFrame(XrSession session, // ahead while SteamVR is not presenting; when visibility returns, the // runtime rejects every submit with XR_ERROR_TIME_INVALID. const bool renderable_frame = runtime_should_render && submitting_layers; + if (renderable_frame) { + turbo_probe_guard_.AppFrame(std::chrono::steady_clock::now(), app_frame_delta_ms); + } if (renderable_frame && app_frame_delta_ms >= 0.0 && app_frame_delta_ms < kHealthyFrameMs) { ++turbo_cadence_healthy_streak_; } else { @@ -7248,6 +7659,9 @@ XrResult OpenXrLayer::ForwardEndFrame(XrSession session, } turbo_effective_async_.store(turbo_pacing_mode_ == TurboPacingMode::kAsync); const bool was_effective = turbo_effective_active_.exchange(turbo_engaged); + if (turbo_engaged && !was_effective && turbo_pacing_mode_ == TurboPacingMode::kAsync) { + turbo_probe_guard_.Engaged(std::chrono::steady_clock::now()); + } if (turbo_engaged && !was_effective) { SoundPlayer::Instance().PlayTransition(ActingBinding(turbo_toggle_pressed_, resolved_settings_.turbo.toggle_bindings).sound, true, dll_directory_, resolved_settings_.core.sound_volume, L"turbo-on.wav", L"turbo-off.wav"); @@ -7275,6 +7689,7 @@ XrResult OpenXrLayer::ForwardEndFrame(XrSession session, const char* async_release_reason = turbo_engaged ? nullptr : !metrics_available ? "Turbo disabled in settings" : + upstream_turbo ? "a layer above pipelines the frames already" : suspended_now ? "Turbo safety suspension" : recovery_blocked_now ? "Turbo recovery block" : turbo_async_recouple_stalled_ ? "runtime wait stalled while Turbo was off" : nullptr; @@ -7283,7 +7698,7 @@ XrResult OpenXrLayer::ForwardEndFrame(XrSession session, // block a full frame interval), the deferred begin, and the runtime end. // Release the config/render lock so locate calls from other app threads // keep flowing — holding it here caused visible judder at locked fps. - config_lock.unlock(); + unlock_config(); const auto pacing_start = std::chrono::steady_clock::now(); @@ -7582,6 +7997,16 @@ XrResult OpenXrLayer::ForwardEndFrame(XrSession session, if (!app_end_noted) { std::scoped_lock lock(turbo_mutex_); turbo_prediction_history_.AppEnd(submitted_display_time); + // Sequenced: this end is followed by the frame-thread wait for the + // next runtime frame. From here an app wait targets the frame that + // wait returns, so it counts as in flight from the end on, not only + // once the runtime wait starts (an app wait during the submit was + // handed the cached prediction, a frame early). + if (turbo_pacing_mode_ == TurboPacingMode::kSequenced && + turbo_seq_state_ == TurboSequencedState::kActive && !turbo_async_wait_.valid() && + !turbo_begin_owed_) { + turbo_seq_wait_in_flight_ = true; + } } const auto pacing_after_drain = std::chrono::steady_clock::now(); const bool diag_end = TurboSequencedDebugTick(); @@ -7834,6 +8259,8 @@ XrResult OpenXrLayer::ForwardEndFrame(XrSession session, } else { std::scoped_lock lock(turbo_mutex_); CancelTurboAsyncHandoffLocked("runtime xrEndFrame failed"); + // No frame-thread wait follows a failed submit. + turbo_seq_wait_in_flight_ = false; if (turbo_auto_suspended_.load()) { turbo_valve_open_ = false; turbo_valve_cv_.notify_all(); @@ -8140,6 +8567,9 @@ void OpenXrLayer::ResolveTurboPacingModeLocked() { turbo_stable_accumulated_ms_ = 0.0; turbo_drain_timeout_count_ = 0; turbo_timeout_window_start_.reset(); + turbo_fallback_early_ = false; + turbo_reprobe_of_.reset(); + turbo_probe_grace_log_budget_ = 3; const TurboPacingSetting setting = turbo_experiment_.enabled ? TurboPacingSetting::kAsync : resolved_settings_.turbo.pacing_mode; @@ -8162,11 +8592,16 @@ void OpenXrLayer::ResolveTurboPacingModeLocked() { } } - if (const auto observation = FindRuntimePacingObservation(ResolveRuntimePacingPath(), - runtime_name_, - system_name_, - system_vendor_id_, - graphics_api_)) { + const auto observation = FindRuntimePacingObservation(ResolveRuntimePacingPath(), + runtime_name_, + system_name_, + system_vendor_id_, + graphics_api_); + const std::optional reprobe_reason = + observation ? RuntimePacingReprobeReason(*observation, VECTORXR_VERSION, runtime_version_, + static_cast(RuntimeRelayUnixMilliseconds())) + : std::nullopt; + if (observation && !reprobe_reason) { turbo_pacing_mode_ = observation->mode; turbo_pacing_source_ = TurboPacingSource::kDiscovered; logger_.Info(std::string("Turbo pacing: ") + ToString(turbo_pacing_mode_) + @@ -8188,13 +8623,22 @@ void OpenXrLayer::ResolveTurboPacingModeLocked() { turbo_pacing_mode_ = TurboPacingMode::kAsync; turbo_pacing_source_ = TurboPacingSource::kProbing; turbo_pacing_verdict_pending_ = true; + if (reprobe_reason) { + turbo_reprobe_of_ = *observation; + logger_.Info("Turbo pacing: re-probing async for runtime \"" + runtime_name_ + + "\": its recorded sequenced verdict is due a retest (" + *reprobe_reason + + "; earlyProbeFailures=" + std::to_string(observation->early_probe_failures) + + "). Falls back to sequenced on stalls or submission errors, and records the outcome."); + return; + } logger_.Info("Turbo pacing: no tested decision for runtime \"" + runtime_name_ + "\"; probing async pacing first (falls back to sequenced on stalls or submission errors)."); } void OpenXrLayer::RecordTurboPacingVerdict(TurboPacingMode mode, const char* source, - std::int64_t stable_seconds) { + std::int64_t stable_seconds, + std::int64_t early_probe_failures) { if (runtime_name_.empty() || turbo_experiment_.enabled) { return; } @@ -8216,10 +8660,16 @@ void OpenXrLayer::RecordTurboPacingVerdict(TurboPacingMode mode, .count(); observation.probe_timeouts = turbo_probe_timeout_total_; observation.stable_seconds = stable_seconds; + observation.early_probe_failures = early_probe_failures; QueueRuntimePacingWrite(std::move(observation)); logger_.Info(std::string("Turbo pacing: queued ") + ToString(mode) + - " verdict for runtime \"" + runtime_name_ + "\" (" + source + ")."); + " verdict for runtime \"" + runtime_name_ + "\" (" + source + + ", earlyProbeFailures=" + std::to_string(early_probe_failures) + + (mode == TurboPacingMode::kSequenced && early_probe_failures > 0 && + early_probe_failures < kEarlyProbeFailureLimit + ? "; provisional: the next session probes async again)." + : ").")); } void OpenXrLayer::QueueRuntimePacingWrite(RuntimePacingObservation observation) { @@ -8275,15 +8725,52 @@ void OpenXrLayer::NoteTurboPacingStableFrame(double app_frame_delta_ms) { turbo_pacing_source_ == TurboPacingSource::kFallback) { turbo_pacing_source_ = TurboPacingSource::kDiscovered; } + const std::int64_t early_failures = + turbo_pacing_mode_ == TurboPacingMode::kSequenced && turbo_fallback_early_ + ? (turbo_reprobe_of_ ? turbo_reprobe_of_->early_probe_failures : 0) + 1 + : 0; RecordTurboPacingVerdict(turbo_pacing_mode_, source, - static_cast(turbo_stable_accumulated_ms_ / 1000.0)); + static_cast(turbo_stable_accumulated_ms_ / 1000.0), early_failures); } -bool OpenXrLayer::TryTurboAutoFallbackLocked(const std::string& reason) { +bool OpenXrLayer::TryTurboAutoFallbackLocked(const std::string& reason, + std::chrono::steady_clock::time_point now) { if (turbo_pacing_mode_ != TurboPacingMode::kAsync || turbo_pacing_source_ == TurboPacingSource::kForced || turbo_pacing_source_ == TurboPacingSource::kPinned) return false; turbo_recovery_.Fail(reason + " Auto is trying sequenced pacing next."); if (!turbo_recovery_.Prepare(resolved_settings_.turbo.interrupted_session_recovery, true, "sequenced")) return false; + const bool probing = turbo_pacing_source_ == TurboPacingSource::kProbing; + turbo_fallback_early_ = probing && turbo_probe_guard_.Early(now); + const auto since_engaged = turbo_probe_guard_.SinceEngaged(now); + const std::string after = since_engaged + ? std::to_string(std::chrono::duration_cast(*since_engaged).count()) + "ms" + : std::string("an unknown time"); + if (probing && turbo_reprobe_of_) { + // Sequenced is this runtime's proven verdict already: record the + // re-probe's outcome now, so a session cut short cannot re-probe + // again next time for the same reason. + const std::int64_t early_failures = + turbo_fallback_early_ ? turbo_reprobe_of_->early_probe_failures + 1 : 0; + logger_.Info("Turbo Auto: the async re-probe fell back " + after + + " after engaging; keeping the recorded sequenced verdict."); + turbo_pacing_mode_ = TurboPacingMode::kSequenced; + RecordTurboPacingVerdict(TurboPacingMode::kSequenced, "discovered", + std::max(turbo_reprobe_of_->stable_seconds, 60), early_failures); + turbo_reprobe_of_.reset(); + turbo_pacing_source_ = TurboPacingSource::kDiscovered; + turbo_pacing_verdict_pending_ = false; + turbo_stable_accumulated_ms_ = 0.0; + turbo_runtime_error_streak_.store(0); + turbo_drain_timeout_count_ = 0; + turbo_timeout_window_start_.reset(); + logger_.Info("Turbo Auto: async failed; trying sequenced. reason=" + reason); + return true; + } + if (turbo_fallback_early_) { + logger_.Info("Turbo Auto: the async probe fell back " + after + + " after engaging (early); the sequenced verdict it leads to is provisional, and the next " + "session probes async again."); + } turbo_pacing_mode_ = TurboPacingMode::kSequenced; turbo_pacing_source_ = TurboPacingSource::kFallback; turbo_pacing_verdict_pending_ = true; @@ -8489,6 +8976,24 @@ bool OpenXrLayer::HandleTurboDrainTimeout(std::chrono::steady_clock::time_point turbo_stable_accumulated_ms_ = 0.0; ++turbo_probe_timeout_total_; + // Auto's async pacing right after it engages, or while the game still + // hitches: such a stall is not evidence against async (TurboProbeGuard). + // Forced and pinned modes keep their plain safety thresholds. + if (turbo_pacing_mode_ == TurboPacingMode::kAsync && turbo_pacing_source_ != TurboPacingSource::kForced && + turbo_pacing_source_ != TurboPacingSource::kPinned && turbo_probe_guard_.InGrace(now)) { + turbo_trace_.Record("probe.grace", 0, turbo_probe_timeout_total_); + if (turbo_probe_grace_log_budget_ > 0) { + --turbo_probe_grace_log_budget_; + const auto since_engaged = turbo_probe_guard_.SinceEngaged(now); + const auto ms = since_engaged + ? std::chrono::duration_cast(*since_engaged).count() + : 0; + logger_.Info("Turbo Auto: not counting an async xrWaitFrame stall " + std::to_string(ms) + + "ms after async pacing engaged (grace while the game's pacing settles)."); + } + return false; + } + // Rolling-window count: stalled frames interleave with clean ones on // these runtimes, so a consecutive-streak check never fires. constexpr std::chrono::seconds kTimeoutWindow{30}; @@ -8512,7 +9017,7 @@ bool OpenXrLayer::HandleTurboDrainTimeout(std::chrono::steady_clock::time_point return false; } std::scoped_lock lock(mutex_); - if (TryTurboAutoFallbackLocked("Repeated xrWaitFrame stalls in async pacing.")) return false; + if (TryTurboAutoFallbackLocked("Repeated xrWaitFrame stalls in async pacing.", now)) return false; } // Level 2 (sequenced pacing still stalling), or a forced/pinned mode the @@ -9205,6 +9710,8 @@ void OpenXrLayer::ResetTurboFrameState(bool stop_recorder) { pacing_end_sum_ms_ = 0.0; pacing_end_max_ms_ = 0.0; std::scoped_lock lock(turbo_mutex_); + upstream_turbo_.Reset(); + upstream_turbo_logged_ = false; should_render_log_budget_ = 8; last_noted_should_render_.reset(); pacing_wait_sum_ms_ = 0.0; @@ -9245,6 +9752,7 @@ void OpenXrLayer::ResetTurboFrameState(bool stop_recorder) { turbo_fabricated_wait_log_budget_ = 0; turbo_drain_timeout_count_ = 0; turbo_timeout_window_start_.reset(); + turbo_probe_guard_.Reset(); turbo_auto_suspended_.store(false, std::memory_order_relaxed); turbo_seq_state_ = TurboSequencedState::kInactive; turbo_begin_owed_ = false; @@ -9278,6 +9786,7 @@ XrResult OpenXrLayer::EndFrame(XrSession session, const XrFrameEndInfo* frame_en static_cast(GetCurrentThreadId())); } NoteThread(ThreadCensus::Call::EndFrame); + game_frames_ended_.fetch_add(1, std::memory_order_relaxed); // Hang forensics: if a log ends between these two markers, a thread is // parked inside a runtime call while holding mutex_. @@ -9285,6 +9794,24 @@ XrResult OpenXrLayer::EndFrame(XrSession session, const XrFrameEndInfo* frame_en if (diag) { logger_.Debug("Turbo-diag: xrEndFrame entered; acquiring config lock."); } + // A bridged session's hand-over below forwards its releases without + // mutex_ (ForwardHandedOverReleases); no other hand-over comes between. + // The Direct3D 11 bridge's work on the game's context for the frame's + // hand-over, before any lock of the layer's: a game thread may hold the + // context's lock (ID3D11Multithread::Enter) through an OpenXR call that + // takes them (D3D11Bridge::StageReleases). + d3d11_bridge_.StageReleases(XR_NULL_HANDLE); + // Everything else this call does on the game's Direct3D 11 context under + // the locks below (a release StageReleases did not stage, Quadviews' + // composition and its own swapchains, Frame Generation's captures in the + // game's own session) waits for the device's lock first, holding none of + // them (GameContextLock). Let go with mutex_, before the runtime's + // xrEndFrame (ForwardEndFrame). + GameContextHold context_hold = game_context_->Hold(); + std::unique_lock handover(bridge_handover_mutex_, std::defer_lock); + if (d3d11_bridge_.Active() || vulkan_bridge_.Active()) { + handover.lock(); + } // unique_lock because ForwardEndFrame releases it before blocking runtime // calls (turbo drain + next_end_frame_). std::unique_lock lock(mutex_); @@ -9316,10 +9843,21 @@ XrResult OpenXrLayer::EndFrame(XrSession session, const XrFrameEndInfo* frame_en // runtime reads them: everything the game submitted before this call is // in them, its rendering after a release included. Frame Generation // captures them here too, before anything below reads or composes them. - if (const XrResult handed = HandOverBridgedReleasesLocked("end frame"); XR_FAILED(handed)) { - DropPendingFrameGenQuad(false); - framegen_.AbandonFrame(frame_end_info->displayTime); - return handed; + { + std::vector releases; + XrResult handed = HandOverBridgedReleasesLocked("end frame", true, &releases); + // The runtime's swapchain calls may wait out Frame Generation's + // xrEndFrame (PimaxXR's lock): the game's other threads do not. + const XrResult forwarded = ForwardHandedOverReleases(lock, releases, "end frame"); + if (handover.owns_lock()) { + handover.unlock(); + } + if (XR_SUCCEEDED(handed)) handed = forwarded; + if (XR_FAILED(handed)) { + DropPendingFrameGenQuad(false); + framegen_.AbandonFrame(frame_end_info->displayTime); + return handed; + } } // ...and the game's motion vector images, which it may never release: // as they are now (through the Vulkan bridge, as a hand-over copies them @@ -9354,8 +9892,8 @@ XrResult OpenXrLayer::EndFrame(XrSession session, const XrFrameEndInfo* frame_en } framegen_.CaptureEngineMotion(hand_over); } - // A bridged session lives on the bridge's device; depth that stays the - // game's is left out of its frames. + // A bridged session lives on the bridge's device. Depth no runtime may + // be given is left out of every session's frames below (the storage). std::optional depthless_frame; if (d3d11_bridge_.Active()) { std::string lost; @@ -9378,7 +9916,12 @@ XrResult OpenXrLayer::EndFrame(XrSession session, const XrFrameEndInfo* frame_en d3d11_bridge_quad_frames_summarized_.split); } d3d11_bridge_quad_frames_summarized_ = d3d11_bridge_quad_frames_; - logger_.Info("Frame Generation's Direct3D 11 bridge: " + d3d11_bridge_.Summary() + quad_frames); + // The game's swapchain calls this window (avg/max ms each): where a + // game slower engaged than passing through spends its frame. + const std::string calls = ", gameCallsMs=acquire:" + bridge_call_timing_[0].Take() + + ",wait:" + bridge_call_timing_[1].Take() + + ",release:" + bridge_call_timing_[2].Take(); + logger_.Info("Frame Generation's Direct3D 11 bridge: " + d3d11_bridge_.Summary() + quad_frames + calls); } if (d3d11_bridge_.Lost(lost)) { if (!d3d11_bridge_lost_logged_) { @@ -9389,17 +9932,6 @@ XrResult OpenXrLayer::EndFrame(XrSession session, const XrFrameEndInfo* frame_en framegen_.AbandonFrame(frame_end_info->displayTime); return XR_ERROR_SESSION_LOST; } - if (d3d11_bridge_.HasUnmirrored()) { - const XrFrameEndInfo* without_depth = WithoutDepth( - frame_end_info, depthless_frame.emplace(), - [this](XrSwapchain swapchain) { return d3d11_bridge_.Unmirrored(swapchain); }); - if (without_depth != frame_end_info && !d3d11_bridge_depth_logged_) { - d3d11_bridge_depth_logged_ = true; - logger_.Info("Frame Generation's Direct3D 11 bridge keeps the game's depth to itself: it is left " - "out of the frames the runtime and Frame Generation get."); - } - frame_end_info = without_depth; - } } if (vulkan_bridge_.Active()) { std::string lost; @@ -9409,6 +9941,14 @@ XrResult OpenXrLayer::EndFrame(XrSession session, const XrFrameEndInfo* frame_en vulkan_bridge_last_summary_ = now; logger_.Info("Frame Generation's Vulkan bridge: " + vulkan_bridge_.Summary()); } + // Depth Frame Generation found the same all over (Star Citizen's, + // written after the frame's end): the bridge checks when it arrives. + // Other games' submissions stay as they were. + if (now - vulkan_bridge_last_depth_poll_ >= std::chrono::milliseconds(500)) { + vulkan_bridge_last_depth_poll_ = now; + vulkan_bridge_.WantDepthTimingCheck(framegen_.Status().depth_uniform); + } + if (vulkan_bridge_.Lost(lost)) { if (!vulkan_bridge_lost_logged_) { vulkan_bridge_lost_logged_ = true; @@ -9418,17 +9958,42 @@ XrResult OpenXrLayer::EndFrame(XrSession session, const XrFrameEndInfo* frame_en framegen_.AbandonFrame(frame_end_info->displayTime); return XR_ERROR_SESSION_LOST; } - if (vulkan_bridge_.HasUnmirrored()) { - const XrFrameEndInfo* without_depth = WithoutDepth( - frame_end_info, depthless_frame.emplace(), - [this](XrSwapchain swapchain) { return vulkan_bridge_.Unmirrored(swapchain); }); - if (without_depth != frame_end_info && !vulkan_bridge_depth_logged_) { - vulkan_bridge_depth_logged_ = true; - logger_.Info("Frame Generation's Vulkan bridge keeps the game's depth to itself (multisampled, or the " - "runtime's depth images are not depth targets: its swapchain line says which): it is " - "left out of the frames the runtime and Frame Generation get."); - } - frame_end_info = without_depth; + } + // Depth no runtime may be given is left out, in every session, before + // Frame Generation or the runtime gets the frame (WithSubmittableDepth). + { + LeftOutDepth left_out; + frame_end_info = WithSubmittableDepth( + frame_end_info, depthless_frame.emplace(), + [this](XrSwapchain swapchain) { return UnmirroredSwapchain(swapchain); }, + [this](XrSwapchain swapchain, std::uint32_t& width, std::uint32_t& height, std::uint32_t& array_size) { + const auto tracked = tracked_swapchains_.find(swapchain); + if (tracked == tracked_swapchains_.end()) { + return false; + } + width = tracked->second.width; + height = tracked->second.height; + array_size = tracked->second.array_size; + return true; + }, + left_out); + if (left_out.kept_private && d3d11_bridge_.Active() && !d3d11_bridge_depth_logged_) { + d3d11_bridge_depth_logged_ = true; + logger_.Info("Frame Generation's Direct3D 11 bridge keeps the game's depth to itself: it is left " + "out of the frames the runtime and Frame Generation get."); + } + if (left_out.kept_private && vulkan_bridge_.Active() && !vulkan_bridge_depth_logged_) { + vulkan_bridge_depth_logged_ = true; + logger_.Info("Frame Generation's Vulkan bridge keeps the game's depth to itself (multisampled, the " + "runtime's depth images are not depth targets, Frame Generation's Use game depth is off, or " + "VECTORXR_VULKAN_BRIDGE_DEPTH=private: " + "its swapchain line says which): it is left out of the frames the runtime and Frame Generation get."); + } + if (!left_out.outside.empty() && !depth_rect_logged_) { + depth_rect_logged_ = true; + logger_.Info("The game's depth sub-image lies outside its depth image (" + left_out.outside + + "): its projection's depth is left out of the frames the runtime and Frame Generation get " + "while it does (a runtime refuses such a frame)."); } } ReloadConfigIfNeeded(); @@ -9497,7 +10062,7 @@ XrResult OpenXrLayer::EndFrame(XrSession session, const XrFrameEndInfo* frame_en framegen_.AbandonFrame(frame_end_info->displayTime); return release_result; } - return ForwardEndFrame(session, frame_end_info, lock); + return ForwardEndFrame(session, frame_end_info, lock, &context_hold); } const bool quadviews_projection_split_active = @@ -9815,7 +10380,7 @@ XrResult OpenXrLayer::EndFrame(XrSession session, const XrFrameEndInfo* frame_en return release_result; } log_depth_runtime_submission(*frame_end_info, "passthrough"); - return ForwardEndFrame(session, frame_end_info, lock); + return ForwardEndFrame(session, frame_end_info, lock, &context_hold); } std::vector& resolved_pivot_space_deltas = @@ -10255,7 +10820,7 @@ XrResult OpenXrLayer::EndFrame(XrSession session, const XrFrameEndInfo* frame_en } else { log_depth_runtime_submission(adjusted_frame_end_info, "rewritten"); } - return ForwardEndFrame(session, &adjusted_frame_end_info, lock); + return ForwardEndFrame(session, &adjusted_frame_end_info, lock, &context_hold); } XrResult OpenXrLayer::GetReferenceSpaceBoundsRect(XrSession session, @@ -10532,6 +11097,9 @@ XrResult OpenXrLayer::PollEvent(XrInstance instance, XrEventDataBuffer* event_da logger_.Info("Runtime session state changed: state=" + std::to_string(static_cast(change.state)) + " (" + SessionStateName(change.state) + "), time=" + std::to_string(change.time)); std::scoped_lock lock(mutex_); + if (change.session == active_session_ && IsStaleSessionEventLocked(change)) { + return result; + } if (change.session == active_session_) { observed_session_state_ = change.state; if (change.state == XR_SESSION_STATE_FOCUSED) { @@ -10563,6 +11131,10 @@ XrResult OpenXrLayer::PollEvent(XrInstance instance, XrEventDataBuffer* event_da return result; } std::scoped_lock lock(mutex_); + if (change.session == active_session_) { + // This runtime says when it recenters: a jump is then real motion. + pivotxr_runtime_reports_recenters_ = true; + } // Only Pivot keeps a seated position to re-anchor. if (change.session == active_session_ && resolved_settings_.core.enabled && resolved_settings_.pivotxr.enabled) { // A bounded queue keeps the newest changes: a later recenter must @@ -10579,6 +11151,43 @@ XrResult OpenXrLayer::PollEvent(XrInstance instance, XrEventDataBuffer* event_da return result; } +void OpenXrLayer::NoteSessionCreatedLocked(XrTime create_time) { + awaiting_session_start_event_ = true; + session_create_time_ = create_time; +} + +bool OpenXrLayer::IsStaleSessionEventLocked(const XrEventDataSessionStateChanged& change) { + // PimaxXR keeps its session event queue across sessions and stamps every + // event with the one handle it gives each session: an application that + // destroyed its session without reading the last events (STOPPING, IDLE, + // EXITING) gets them for its next session, ahead of that session's IDLE. + // They are not this session's state. With the runtime's clock, an event + // stamped before this session was created is the last session's; without + // it, anything before the session's first IDLE is (an old IDLE among them + // then closes the window early). The application still gets every event + // as the runtime sent it. + if (!awaiting_session_start_event_) { + return false; + } + const bool stale = session_create_time_ > 0 + ? change.time > 0 && change.time < session_create_time_ + : change.state != XR_SESSION_STATE_IDLE; + if (!stale) { + awaiting_session_start_event_ = false; + return false; + } + if (stale_session_event_logs_ < 8) { + ++stale_session_event_logs_; + logger_.Info("Runtime session event from before this session (state=" + + std::to_string(static_cast(change.state)) + " " + SessionStateName(change.state) + + ", time=" + std::to_string(change.time) + ", session created at " + + std::to_string(session_create_time_) + + "): the runtime kept it from the previous session. VectorXR does not take it as this " + "session's state; the application receives it as sent."); + } + return true; +} + bool OpenXrLayer::PivotAnchorAffectedBy(XrReferenceSpaceType type) const { if (!pivotxr_translation_anchor_) { return false; @@ -10595,6 +11204,59 @@ bool OpenXrLayer::PivotAnchorAffectedBy(XrReferenceSpaceType type) const { return true; } +void OpenXrLayer::NoteSilentRecenterLocked(XrTime time, XrSpace reference_space, const XrPosef& view_pose) { + // PimaxXR recenters (Pimax Play, its system button held, a key, each + // session's start) without queueing a reference space change: LOCAL + // jumps under the seated anchor, and Pivot would orbit the old point. + // The anchor is a position: how far it is off after a recenter is how + // far the head jumped, so the jump is judged by position alone (a turn + // made at the anchor leaves it right, and game-side snaps and instant + // turns are no recenter). A head moves at most about 2 m/s even in a + // fast check over the shoulder: a step between two consecutive frames of + // more than 10 cm that is also faster than 3 m/s cannot be the head, and + // is taken as the recenter the runtime did not announce (a smaller one + // leaves the anchor off by under 10 cm, as a lean would). Drives under + // 1 ms apart are not two frames; past 100 ms (a pause, a load) real + // motion can be anything, so nothing is judged there. + constexpr XrTime kMinGap = 1'000'000; + constexpr XrTime kMaxGap = 100'000'000; + constexpr double kMinJumpMeters = 0.10; + constexpr double kHeadMetersPerSecond = 3.0; // above any real head speed (~2 m/s) + const std::optional previous = pivotxr_last_drive_; + pivotxr_last_drive_ = PivotDriveSample{time, reference_space, view_pose}; + if (!previous || pivotxr_runtime_reports_recenters_ || !pivotxr_translation_anchor_ || + previous->reference_space != reference_space) { + return; + } + const XrTime gap = time - previous->time; + if (gap < kMinGap || gap > kMaxGap) { + return; + } + const double seconds = static_cast(gap) / 1e9; + const double dx = view_pose.position.x - previous->view_pose.position.x; + const double dy = view_pose.position.y - previous->view_pose.position.y; + const double dz = view_pose.position.z - previous->view_pose.position.z; + const double meters = std::sqrt(dx * dx + dy * dy + dz * dz); + const bool jumped = meters > std::max(kMinJumpMeters, kHeadMetersPerSecond * seconds); + if (!jumped) { + return; + } + const XrReferenceSpaceType type = tracked_stage_spaces_.contains(pivotxr_translation_anchor_->space) + ? XR_REFERENCE_SPACE_TYPE_STAGE + : XR_REFERENCE_SPACE_TYPE_LOCAL; + if (pivotxr_reference_changes_.size() >= 16) { + pivotxr_reference_changes_.erase(pivotxr_reference_changes_.begin()); + } + pivotxr_reference_changes_.push_back({type, time}); + if (pivotxr_silent_recenter_logs_ < 5) { + ++pivotxr_silent_recenter_logs_; + logger_.Info("PivotXR: the head moved " + FormatDiagnosticDouble(meters) + " m in " + + FormatDiagnosticDouble(seconds * 1000.0) + + " ms, faster than a head can, and the runtime sent no recenter event; taking it as a " + "recenter."); + } +} + XrResult OpenXrLayer::LocateSpace(XrSpace space, XrSpace base_space, XrTime time, XrSpaceLocation* location) { bool pivotxr_active = false; bool pivot_processing_required = false; @@ -10645,10 +11307,45 @@ XrResult OpenXrLayer::LocateSpace(XrSpace space, XrSpace base_space, XrTime time (location->locationFlags & kHeadPredictionPoseFlags) != kHeadPredictionPoseFlags) { return; } + // DCS locates VIEW several times a frame. Each query answers what its + // first locate at this display time did: the same pose (not one + // extended against a later "now"), for one runtime locate instead of + // four. Kept for that display time only. Under mutex_. + const XrPosef runtime_pose = location->pose; + const auto answer_kept = [&] { + const auto answered = std::find_if( + head_extension_answers_.begin(), head_extension_answers_.end(), [&](const HeadExtensionAnswer& answer) { + return answer.time == time && answer.space == space && answer.base_space == base_space && + answer.limit == head_plan.limit; + }); + if (answered == head_extension_answers_.end()) { + return false; + } + location->pose = answered->applied ? answered->pose : runtime_pose; + return true; + }; + { + std::scoped_lock lock(mutex_); + if (answer_kept()) { + return; + } + } + // Extended without the lock (its locates go to the runtime). const HeadExtension extension = ExtendHead(head_plan, head_world_space, time, TurboClockNow()); const HeadPredictionPose pose = ToHeadPose(location->pose); location->pose = ToXrPose(space == head_world_space ? ApplyHeadExtensionInverse(extension, pose) : ApplyHeadExtension(extension, pose)); + std::scoped_lock lock(mutex_); + // Another thread's locate of the query, extended beside this one, + // kept its answer first: it is the query's answer here too. + if (answer_kept()) { + return; + } + if (head_extension_answers_.size() >= kHeadExtensionAnswersKept) { + head_extension_answers_.erase(head_extension_answers_.begin()); + } + head_extension_answers_.push_back( + {time, space, base_space, head_plan.limit, extension.applied, location->pose}); }; if (!pivot_processing_required) { @@ -10718,6 +11415,12 @@ XrResult OpenXrLayer::LocateViews(XrSession session, if (XR_FAILED(result) || !views || !view_count_output) { return result; } + // A runtime that could not locate the views leaves them as they were + // (PimaxXR before its first valid head pose: flags 0, all or nothing): + // nothing is adjusted, cached or derived from them. + if (view_state && (view_state->viewStateFlags & XR_VIEW_STATE_ORIENTATION_VALID_BIT) == 0) { + return result; + } std::scoped_lock lock(mutex_); ReloadConfigIfNeeded(); @@ -11043,6 +11746,23 @@ XrResult OpenXrLayer::LocateViews(XrSession session, } if (depthxr_active && !NearlyEqual(resolved_settings_.depthxr.convergence, 0.0)) { ApplyConvergence(adjusted_views, resolved_settings_.depthxr.convergence, view_layout); + // The runtime shows these focus views with the FOVs it located itself + // (RuntimeTakesFocusFovsFromLocateLocked). Without Depth Anchor the + // periphery is shown as rendered, so the focus views are rendered with + // the runtime's FOVs too; with it, both are shown shifted back to the + // runtime's and convergence applies to all four. + if (!depth_anchor_active_ && count >= 4 && IsQuadViewConfiguration(view_configuration_type) && + RuntimeTakesFocusFovsFromLocateLocked()) { + for (uint32_t i = 2; i < 4; ++i) { + adjusted_views[i].fov = original_views[i].fov; + } + if (!has_logged_focus_fov_quirk_convergence_) { + has_logged_focus_fov_quirk_convergence_ = true; + logger_.Info("Depth convergence on " + application_name_ + "'s native quad views (" + runtime_name_ + + "): the runtime shows the focus views with the FOVs it located, so without Depth Anchor " + "convergence is applied to the peripheral views only."); + } + } } if (cache_depth_submission_geometry) { CacheDepthSubmissionGeometry(view_locate_info->displayTime, @@ -11069,6 +11789,18 @@ XrResult OpenXrLayer::LocateViews(XrSession session, CacheQuadViewsFrame( view_locate_info->displayTime, std::span(views, count), gaze_diagnostic); if (varjo_compatible_quadviews_active_) { + // Whether the runtime's focus views sit at their eyes' poses (as + // PimaxXR's do): then a stereo locate gives every view's offset. + const auto same_pose = [&](uint32_t a, uint32_t b) { + return original_views[a].position.x == original_views[b].position.x && + original_views[a].position.y == original_views[b].position.y && + original_views[a].position.z == original_views[b].position.z && + original_views[a].orientation.x == original_views[b].orientation.x && + original_views[a].orientation.y == original_views[b].orientation.y && + original_views[a].orientation.z == original_views[b].orientation.z && + original_views[a].orientation.w == original_views[b].orientation.w; + }; + native_quad_focus_shares_eye_poses_ = same_pose(2, 0) && same_pose(3, 1); NoteVarjoNativeLocatedFocusLocked(view_locate_info->displayTime, views); } } @@ -11646,7 +12378,15 @@ OsdSnapshot OpenXrLayer::BuildOsdSnapshot() const { if (quadviews_session_active_.value_or(false) && (!has_active_primary_view_configuration_ || IsQuadViewConfiguration(active_primary_view_configuration_type_))) add(OsdText::ModuleQuadviews); - const FrameGenOsdText framegen=framegen_.OsdDescription(); + FrameGenOsdText framegen=framegen_.OsdDescription(); + // Asked for but kept off for OpenXR Toolkit below VectorXR: a plain + // "Disabled" would read as the user's own setting. + if (framegen_off_for_toolkit_.load(std::memory_order_acquire)) { + framegen=FrameGenOsdText{}; + framegen.badge="OFF"; + framegen.row=framegen.compact="Off / Layer conflict with OpenXR Toolkit"; + framegen.detail="Move OpenXR Toolkit above VectorXR in the VectorXR app"; + } snapshot.framegen=framegen.row; snapshot.compact_framegen=framegen.compact; snapshot.framegen_detail=framegen.detail; @@ -11811,6 +12551,25 @@ void OpenXrLayer::RefreshResolvedSettings() { if (!resolved_settings_.core.enabled) { resolved_settings_.core.osd.enabled = false; } + // OpenXR Toolkit below VectorXR holds every release back until its next + // xrEndFrame, then releases every swapchain it held, and writes its + // upscaled image only for swapchains that frame shows; its swapchain map + // is not safe from two threads. Frame Generation's frames (several per + // game frame, from its own thread) would make it release the game's + // images without its work, and race the game's swapchain calls. + framegen_off_for_toolkit_.store(openxr_toolkit_below_ && resolved_settings_.core.enabled && + resolved_settings_.framegen.enabled, + std::memory_order_release); + if (openxr_toolkit_below_ && resolved_settings_.framegen.enabled) { + resolved_settings_.framegen.enabled = false; + if (!openxr_toolkit_framegen_logged_) { + openxr_toolkit_framegen_logged_ = true; + logger_.Error("Frame Generation is off for this game: OpenXR Toolkit is loaded below VectorXR, and it " + "cannot take Frame Generation's frames. Move OpenXR Toolkit above VectorXR (in the " + "VectorXR app's layer order), turn it off for this game, or set its per-game bypass to " + "use Frame Generation."); + } + } framegen_.Configure(resolved_settings_.framegen); framegen_.SetConfigured(resolved_settings_.core.enabled && resolved_settings_.framegen.enabled); // Turbo decouples the app from runtime pacing; Frame Generation locks it @@ -12291,6 +13050,7 @@ void OpenXrLayer::ResetSessionState() { varjo_native_view_space_ = XR_NULL_HANDLE; varjo_native_combined_eye_space_ = XR_NULL_HANDLE; varjo_native_rendering_gaze_tracked_ = false; + has_logged_varjo_native_rendering_gaze_uninformative_ = false; has_logged_varjo_native_rendering_gaze_active_ = false; has_logged_varjo_native_rendering_gaze_unavailable_ = false; varjo_native_foveation_resources_attempted_ = false; @@ -12324,6 +13084,15 @@ void OpenXrLayer::ResetSessionState() { active_runtime_view_configuration_type_ = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO; has_active_primary_view_configuration_ = false; has_logged_quad_view_short_count_ = false; + has_logged_canted_quadviews_eyes_ = false; + pivotxr_last_drive_.reset(); + pivotxr_runtime_reports_recenters_ = false; + pivotxr_silent_recenter_logs_ = 0; + awaiting_session_start_event_ = false; + session_create_time_ = 0; + head_extension_answers_.clear(); + native_quad_focus_shares_eye_poses_ = false; + has_logged_focus_fov_quirk_convergence_ = false; has_logged_pivotxr_spike_mode_ = false; has_logged_quadviews_view_configuration_capabilities_ = false; has_logged_visibility_mask_mapping_ = false; @@ -12351,6 +13120,7 @@ void OpenXrLayer::ResetSessionState() { void OpenXrLayer::ResetInstanceState() { instance_requested_session_ = false; instance_enabled_extension_count_ = 0; + instance_enabled_session_extension_ = false; application_action_spaces_.clear(); application_pose_actions_.clear(); application_action_owners_.clear(); @@ -12542,6 +13312,13 @@ void OpenXrLayer::ResetEyeGazeInputState() { eye_gaze_unfocused_last_orientation_.reset(); eye_gaze_unfocused_identical_frames_ = 0; eye_gaze_unfocused_logs_remaining_ = 16; + eye_gaze_focused_last_orientation_.reset(); + eye_gaze_focused_last_time_ = 0; + eye_gaze_focused_identical_frames_ = 0; + eye_gaze_focused_frozen_logged_ = false; + eye_gaze_still_since_.reset(); + eye_gaze_still_logged_ = false; + eye_gaze_still_logs_remaining_ = kEyeGazeStillLogBudget; eye_gaze_inactive_since_.reset(); eye_gaze_no_data_logged_ = false; } @@ -13009,6 +13786,35 @@ bool OpenXrLayer::LocateEyeGazeFocusOffsets(XrSession session, return false; } } + if (!unfocused_visible && XR_SUCCEEDED(locate_result) && + (gaze_location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_VALID_BIT) != 0) { + // Valid but frozen: see kEyeGazeFocusedFrozenFrames. A frame's + // repeated locates (one display time) are not counted. + const XrQuaternionf& q = gaze_location.pose.orientation; + const auto& last = eye_gaze_focused_last_orientation_; + const bool identical = last && last->x == q.x && last->y == q.y && last->z == q.z && last->w == q.w; + if (!identical) { + eye_gaze_focused_identical_frames_ = 0; + eye_gaze_focused_frozen_logged_ = false; + } else if (time != eye_gaze_focused_last_time_) { + ++eye_gaze_focused_identical_frames_; + } + eye_gaze_focused_last_orientation_ = q; + eye_gaze_focused_last_time_ = time; + if (eye_gaze_focused_identical_frames_ >= kEyeGazeFocusedFrozenFrames) { + if (!eye_gaze_focused_frozen_logged_) { + eye_gaze_focused_frozen_logged_ = true; + logger_.Info("Quadviews eye tracking: the runtime has reported the very same gaze for " + + std::to_string(eye_gaze_focused_identical_frames_) + + " frames while calling it valid; the eye tracker has likely stopped. Quadviews uses " + "head focus until the gaze moves again."); + } + log_eye_gaze_diagnostic("gaze has not changed for " + std::to_string(eye_gaze_focused_identical_frames_) + + " frames", + true); + return false; + } + } if (XR_FAILED(locate_result) || (gaze_location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_VALID_BIT) == 0) { log_eye_gaze_diagnostic("gaze space locate failed, result=" + @@ -13029,6 +13835,33 @@ bool OpenXrLayer::LocateEyeGazeFocusOffsets(XrSession session, const double euler_pitch = ExtractPitchRadians(diagnostic_orientation); double target_yaw = gaze_ray_angles.yaw_radians; double target_pitch = gaze_ray_angles.pitch_radians; + if (!unfocused_visible) { + // See kEyeGazeStillRadians: logged, never acted on. + const bool moved = !eye_gaze_still_since_ || + std::abs(target_yaw - eye_gaze_still_yaw_) > kEyeGazeStillRadians || + std::abs(target_pitch - eye_gaze_still_pitch_) > kEyeGazeStillRadians || + time < *eye_gaze_still_since_; + if (moved) { + eye_gaze_still_since_ = time; + eye_gaze_still_yaw_ = target_yaw; + eye_gaze_still_pitch_ = target_pitch; + eye_gaze_still_logged_ = false; + } else if (!eye_gaze_still_logged_ && time - *eye_gaze_still_since_ >= kEyeGazeStillWindow) { + eye_gaze_still_logged_ = true; + if (eye_gaze_still_logs_remaining_ > 0) { + --eye_gaze_still_logs_remaining_; + logger_.Info("Quadviews eye tracking: the gaze has stayed within " + + FormatDiagnosticDouble(kEyeGazeStillRadians) + " rad of yaw=" + + FormatDiagnosticDouble(eye_gaze_still_yaw_) + ", pitch=" + + FormatDiagnosticDouble(eye_gaze_still_pitch_) + " for " + + std::to_string((time - *eye_gaze_still_since_) / 1'000'000'000) + + " s while the runtime calls it valid (tracker stalled or headset off?). Diagnostic " + "only: focus still follows it." + + (eye_gaze_still_logs_remaining_ == 0 ? " Further such lines suppressed this session." + : "")); + } + } + } quadviews_raw_focus_yaw_radians_ = target_yaw; quadviews_raw_focus_pitch_radians_ = target_pitch; quadviews_raw_focus_valid_ = true; @@ -13298,8 +14131,29 @@ bool OpenXrLayer::LocateVarjoRenderingGaze(XrTime display_time, } else if (combined_eye_space != XR_NULL_HANDLE && view_space != XR_NULL_HANDLE) { result = next_locate_space_(combined_eye_space, view_space, display_time, &location); } - const bool tracked = XR_SUCCEEDED(result) && - (location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_TRACKED_BIT) != 0; + bool tracked = XR_SUCCEEDED(result) && + (location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_TRACKED_BIT) != 0; + // PimaxXR's combined-eye space carries ORIENTATION_TRACKED alone (no VALID + // bit) while its tracker has gaze, and its xrLocateSpace answers flags 0 + // for any pose that is not valid: located, it never says tracked, and + // asking for the fixed focus would switch its gaze foveation off for good. + // A locate that succeeds with no flags at all says nothing about gaze + // (Varjo's own runtime always gives VALID bits): the runtime is asked for + // gaze foveation and decides itself (PimaxXR falls back to its fixed focus + // FOVs while its tracker has no gaze). + bool uninformative = false; + { + std::scoped_lock lock(mutex_); + uninformative = XR_SUCCEEDED(result) && combined_eye_space != XR_NULL_HANDLE && view_space != XR_NULL_HANDLE && + location.locationFlags == 0 && runtime_name_.find("Varjo") == std::string::npos; + if (uninformative && !has_logged_varjo_native_rendering_gaze_uninformative_) { + has_logged_varjo_native_rendering_gaze_uninformative_ = true; + logger_.Info("Native quad views: the runtime's combined-eye space locates with no flags (" + runtime_name_ + + "), so it cannot tell whether gaze is tracked; asking the runtime for its gaze foveation " + "and leaving gaze validity to it."); + } + } + tracked = tracked || uninformative; if (locate_result) { *locate_result = result; @@ -13389,18 +14243,33 @@ bool OpenXrLayer::EnsureEyeOffsets(XrSession session, return true; } + // A quad locate of VectorXR's own at the game's display time would replace + // the focus FOVs a runtime keeps from the game's locate for that time + // (RuntimeTakesFocusFovsFromLocateLocked), with the fixed focus: the frame + // would be shown with the inset off the gaze. Where the runtime's focus + // views sit at their eyes' poses, the stereo locate gives the same offsets + // and leaves that entry alone. + const bool stereo_for_quad = IsQuadViewConfiguration(view_configuration_type) && view_count == 4 && + native_quad_focus_shares_eye_poses_ && RuntimeTakesFocusFovsFromLocateLocked(); + const uint32_t locate_count = stereo_for_quad ? 2 : view_count; std::vector& eye_views = eye_offset_views_scratch_; eye_views.assign(view_count, XrView{XR_TYPE_VIEW}); XrViewState eye_view_state{XR_TYPE_VIEW_STATE}; uint32_t eye_view_count = 0; XrViewLocateInfo eye_view_locate_info{XR_TYPE_VIEW_LOCATE_INFO}; - eye_view_locate_info.viewConfigurationType = view_configuration_type; + eye_view_locate_info.viewConfigurationType = + stereo_for_quad ? XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO : view_configuration_type; eye_view_locate_info.displayTime = display_time; eye_view_locate_info.space = internal_view_space_; - const XrResult result = - next_locate_views_(session, &eye_view_locate_info, &eye_view_state, view_count, &eye_view_count, eye_views.data()); - if (XR_FAILED(result) || eye_view_count < view_count) { + const XrResult result = next_locate_views_(session, &eye_view_locate_info, &eye_view_state, locate_count, + &eye_view_count, eye_views.data()); + if (XR_SUCCEEDED(result) && (eye_view_state.viewStateFlags & XR_VIEW_STATE_ORIENTATION_VALID_BIT) == 0) { + // Not located (the views are left as they were): nothing to keep. + pivot_diagnostic_.has_eye_offsets = false; + return false; + } + if (XR_FAILED(result) || eye_view_count < locate_count) { logger_.Error("Failed to capture internal eye offsets for PivotXR view-space recomposition."); pivot_diagnostic_.has_eye_offsets = false; return false; @@ -13408,7 +14277,7 @@ bool OpenXrLayer::EnsureEyeOffsets(XrSession session, cached_eye_offset_poses_.resize(view_count); for (uint32_t i = 0; i < view_count; ++i) { - cached_eye_offset_poses_[i] = eye_views[i].pose; + cached_eye_offset_poses_[i] = eye_views[stereo_for_quad ? i % 2 : i].pose; } cached_eye_offsets_view_configuration_ = view_configuration_type; cached_eye_offsets_display_time_ = display_time; @@ -13969,8 +14838,10 @@ void OpenXrLayer::NoteVarjoNativeSubmittedFocusLocked(XrTime display_time, const "displayTime=" + std::to_string(display_time) + ", submittedFocusView2=" + FormatFov(views[2].fov) + ", locatedFocusView2=" + FormatFov((*located)[0]) + ", submittedFocusView3=" + FormatFov(views[3].fov) + ", locatedFocusView3=" + - FormatFov((*located)[1]) + ". The runtime places the focus insets by the FOVs submitted; counts " - "are in the foveation summary (submittedFocusFov=checked/mismatched/unlocated)."); + FormatFov((*located)[1]) + ". A runtime places the focus insets by the FOVs submitted, except " + "one that keeps the located ones for the display time (PimaxXR for an application named DCS " + "World): there this mismatch is not what is shown. Counts are in the foveation summary " + "(submittedFocusFov=checked/mismatched/unlocated)."); } } @@ -14260,8 +15131,13 @@ XrResult OpenXrLayer::LocateRuntimeViews(XrSession session, double focus_yaw_radians = 0.0; double focus_pitch_radians = 0.0; + std::array eye_in_view{}; + bool canted_eyes = false; { std::scoped_lock lock(mutex_); + // Only from eyes the runtime located (it may leave them untouched). + canted_eyes = (!view_state || (runtime_view_state.viewStateFlags & XR_VIEW_STATE_ORIENTATION_VALID_BIT) != 0) && + CantedEyeOrientations(session, *view_locate_info, stereo_views, &eye_in_view); const bool has_eye_focus = LocateEyeGazeFocusOffsets(session, view_locate_info->space, ClampInternalLocateTime(view_locate_info->displayTime), @@ -14313,7 +15189,8 @@ XrResult OpenXrLayer::LocateRuntimeViews(XrSession session, focus_pitch_radians, view_capacity_input, view_count_output, - views)) { + views, + canted_eyes ? &eye_in_view : nullptr)) { return XR_ERROR_RUNTIME_FAILURE; } if (synthesized_quad_views) { @@ -14328,7 +15205,8 @@ bool OpenXrLayer::SynthesizeQuadViewsFromStereo(std::span stereo_v double focus_pitch_radians, uint32_t view_capacity_input, uint32_t* view_count_output, - XrView* views) const { + XrView* views, + const std::array* eye_in_view) const { if (stereo_views.size() < 2 || !views || !view_count_output || view_capacity_input < 4) { return false; } @@ -14346,8 +15224,68 @@ bool OpenXrLayer::SynthesizeQuadViewsFromStereo(std::span stereo_v views[i].next = app_next[i]; } - views[2].fov = BuildFocusFov(stereo_views[0].fov, quadviews_settings, focus_yaw_radians, focus_pitch_radians); - views[3].fov = BuildFocusFov(stereo_views[1].fov, quadviews_settings, focus_yaw_radians, focus_pitch_radians); + for (uint32_t eye = 0; eye < 2; ++eye) { + // Canted eyes: head-forward plus the offsets and the gaze is a + // direction in VIEW, placed in each eye through its own turn (both + // insets then cover the same direction). Parallel eyes keep the + // angles as they are. + const std::optional> center = + eye_in_view ? ProjectViewDirectionIntoEye( + (*eye_in_view)[eye], + DegreesToRadians(quadviews_settings.horizontal_offset_degrees) + focus_yaw_radians, + DegreesToRadians(quadviews_settings.vertical_offset_degrees) + focus_pitch_radians) + : std::nullopt; + views[2 + eye].fov = + center ? BuildFocusFovAt(stereo_views[eye].fov, quadviews_settings, center->first, center->second) + : BuildFocusFov(stereo_views[eye].fov, quadviews_settings, focus_yaw_radians, focus_pitch_radians); + } + return true; +} + +bool OpenXrLayer::CantedEyeOrientations(XrSession session, + const XrViewLocateInfo& view_locate_info, + std::span stereo_views, + std::array* eye_in_view) { + if (!eye_in_view || stereo_views.size() < 2) { + return false; + } + // The turn between the two eyes, from this locate alone: none (the same + // orientation, as every runtime with parallel projection answers) costs + // nothing and changes nothing. + constexpr double kParallelEyesRadians = 0.0001; + const XrQuaternionf relative = MultiplyQuaternion(ConjugateQuaternion(NormalizeQuaternion(stereo_views[0].pose.orientation)), + NormalizeQuaternion(stereo_views[1].pose.orientation)); + const double sine = std::sqrt(static_cast(relative.x) * relative.x + + static_cast(relative.y) * relative.y + + static_cast(relative.z) * relative.z); + if (2.0 * std::atan2(sine, std::abs(static_cast(relative.w))) < kParallelEyesRadians) { + return false; + } + if (IsTrackedViewSpace(view_locate_info.space)) { + (*eye_in_view)[0] = NormalizeQuaternion(stereo_views[0].pose.orientation); + (*eye_in_view)[1] = NormalizeQuaternion(stereo_views[1].pose.orientation); + return true; + } + // The eyes relative to VIEW for this display time (Pivot's eye offsets, + // shared with it and kept for this display time only). + if (!EnsureEyeOffsets(session, XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO, + ClampInternalLocateTime(view_locate_info.displayTime), 2) || + cached_eye_offset_poses_.size() < 2) { + return false; + } + (*eye_in_view)[0] = cached_eye_offset_poses_[0].orientation; + (*eye_in_view)[1] = cached_eye_offset_poses_[1].orientation; + if (!has_logged_canted_quadviews_eyes_) { + has_logged_canted_quadviews_eyes_ = true; + const double left_yaw = ExtractYawRadians({(*eye_in_view)[0].x, (*eye_in_view)[0].y, (*eye_in_view)[0].z, + (*eye_in_view)[0].w}); + const double right_yaw = ExtractYawRadians({(*eye_in_view)[1].x, (*eye_in_view)[1].y, (*eye_in_view)[1].z, + (*eye_in_view)[1].w}); + logger_.Info("Quadviews: the runtime's eyes are canted (left eye yaw " + + FormatDiagnosticDouble(left_yaw * 180.0 / 3.14159265358979323846) + " deg, right eye " + + FormatDiagnosticDouble(right_yaw * 180.0 / 3.14159265358979323846) + + " deg from the head); the focus insets are placed through each eye's turn."); + } return true; } @@ -14479,6 +15417,9 @@ XrResult OpenXrLayer::ApplyPivotToLocatedSpace(XrSpace space, const XrPosef view_pose = space_is_view ? location->pose : InvertPose(location->pose); const XrSpace reference_space = space_is_view ? base_space : space; const bool position_valid = (location->locationFlags & XR_SPACE_LOCATION_POSITION_VALID_BIT) != 0; + if (update_smoothing && position_valid) { + NoteSilentRecenterLocked(time, reference_space, view_pose); + } if (update_smoothing && !pivotxr_reference_changes_.empty()) { // A recenter reached this frame: the stored anchor coordinates now // name a different physical point, which would orbit the camera. @@ -14859,6 +15800,8 @@ void OpenXrLayer::LogResolvedSettings(const ResolvedRuntimeConfig& settings) { << ", framegenArtifactSuppression=" << settings.framegen.artifact_suppression << ", framegenMotionLimitPercent=" << settings.framegen.motion_limit_percent << ", framegenExtendHeadPrediction=" << settings.framegen.extend_head_prediction + << ", framegenEdgeFill=" << settings.framegen.edge_fill + << ", framegenUseGameDepth=" << settings.framegen.use_game_depth << ", framegenDiagnosticView=" << ToString(settings.framegen.diagnostic_view) << ", framegenToggleBindings=" << BindingListLabel(settings.framegen.toggle_bindings); logger_.Debug(stream.str()); @@ -14897,6 +15840,7 @@ void OpenXrLayer::ResetPivotActivationState() { pivotxr_origin_capture_pending_ = false; pivotxr_translation_anchor_.reset(); pivotxr_reference_changes_.clear(); + pivotxr_last_drive_.reset(); pivotxr_binding_last_poll_time_.reset(); pivot_diagnostic_stride_counter_ = 0; pivot_diagnostic_ = PivotDiagnosticState{}; @@ -15091,6 +16035,17 @@ void OpenXrLayer::PinModulesForAbandonedPresenter() { void OpenXrLayer::StartFrameGen(XrSession session, const void* binding_chain, const std::string& unavailable) { framegen_session_enabled_ = resolved_settings_.core.enabled && resolved_settings_.framegen.enabled; + game_frames_ended_.store(0, std::memory_order_relaxed); + runtime_frames_ended_.store(0, std::memory_order_relaxed); + layers_below_get_framegen_ = framegen_session_enabled_ && !layers_below_.empty(); + if (layers_below_get_framegen_) { + logger_.Info("Frame Generation: the API layers below VectorXR [" + layers_below_ + + "] get every frame it shows (up to four for each of the game's), from its own thread, and " + + (framegen_queue_.Active() ? std::string("its high-priority Direct3D 12 queue as the session's. ") + : std::string()) + + "A layer there that waits for something that never comes stalls Frame Generation, and through " + "it the game: if the game freezes, move that layer above VectorXR."); + } framegen_toggle_binding_was_down_ = false; framegen_binding_down_cached_ = false; framegen_binding_last_poll_time_.reset(); @@ -15456,7 +16411,8 @@ XrResult OpenXrLayer::FlushDeferredSwapchainReleasesLocked(std::string_view reas } XrResult OpenXrLayer::HandOverBridgedReleasesLocked(XrSwapchain swapchain, SwapchainInfo& info, - std::string_view reason, bool copy) { + std::string_view reason, bool copy, + std::vector* forward_later) { while (!info.bridge_handovers.empty()) { const uint32_t index = info.bridge_handovers.front(); info.bridge_handovers.pop_front(); @@ -15487,6 +16443,10 @@ XrResult OpenXrLayer::HandOverBridgedReleasesLocked(XrSwapchain swapchain, Swapc ++info.deferred_release_count; continue; } + if (forward_later) { + forward_later->push_back(swapchain); + continue; + } XrSwapchainImageReleaseInfo release_info{XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO}; const XrResult result = ForwardAppRelease(swapchain, &release_info); if (XR_FAILED(result)) { @@ -15517,7 +16477,43 @@ void OpenXrLayer::SubmitKeptVulkanBridgeReleasesLocked() { } } -XrResult OpenXrLayer::HandOverBridgedReleasesLocked(std::string_view reason, bool copy) { +XrResult OpenXrLayer::ForwardHandedOverReleases(std::unique_lock& lock, + std::vector& releases, std::string_view reason) { + if (releases.empty()) { + return XR_SUCCESS; + } + std::vector results; + results.reserve(releases.size()); + lock.unlock(); + for (const XrSwapchain swapchain : releases) { + XrSwapchainImageReleaseInfo release_info{XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO}; + results.push_back(ForwardAppRelease(swapchain, &release_info)); + } + lock.lock(); + XrResult first_failure = XR_SUCCESS; + for (std::size_t i = 0; i < releases.size(); ++i) { + if (XR_FAILED(results[i])) { + logger_.Error("Bridged swapchain release failed: handle=" + FormatHandle(releases[i]) + + ", reason=" + std::string(reason) + + ", result=" + FormatHex(static_cast(results[i]))); + if (XR_SUCCEEDED(first_failure)) first_failure = results[i]; + continue; + } + const auto it = tracked_swapchains_.find(releases[i]); + if (it == tracked_swapchains_.end()) { + continue; + } + it->second.image_states.ReleaseDownstreamOldest(); + if (it->second.quadviews_session && it->second.release_count <= 3) { + LogSwapchainSummary(releases[i], it->second, "released"); + } + } + releases.clear(); + return first_failure; +} + +XrResult OpenXrLayer::HandOverBridgedReleasesLocked(std::string_view reason, bool copy, + std::vector* forward_later) { // While the Vulkan bridge gives the game's motion vector images back at // their releases, every swapchain's copies go first: a giving back waits // on the GPU for its copy, and a capture of Frame Generation's queued @@ -15532,7 +16528,7 @@ XrResult OpenXrLayer::HandOverBridgedReleasesLocked(std::string_view reason, boo } XrResult first_failure = XR_SUCCESS; for (auto& [swapchain, info] : tracked_swapchains_) { - const XrResult result = HandOverBridgedReleasesLocked(swapchain, info, reason, copy); + const XrResult result = HandOverBridgedReleasesLocked(swapchain, info, reason, copy, forward_later); if (XR_FAILED(result) && XR_SUCCEEDED(first_failure)) { first_failure = result; } @@ -16266,6 +17262,9 @@ void OpenXrLayer::ResetD3D11QuadViewsCompositor() { SafeRelease(d3d11_quadviews_compositor_.layer_context_state); SafeRelease(d3d11_quadviews_compositor_.context1); SafeRelease(d3d11_quadviews_compositor_.context); + // The session's device goes with it. + game_context_->Set(nullptr); + framegen_.SetGameContextLock(nullptr); SafeRelease(d3d11_quadviews_compositor_.device); d3d11_quadviews_compositor_.initialized = false; d3d11_quadviews_compositor_.failed = false; diff --git a/layer/src/osd_graphics.cpp b/layer/src/osd_graphics.cpp index ca5bf77d..fcb0a0ef 100644 --- a/layer/src/osd_graphics.cpp +++ b/layer/src/osd_graphics.cpp @@ -55,6 +55,7 @@ class D3D11Graphics final : public OsdGraphics { if(FAILED(hr)){error=Result("D3D11 device removed",hr);return OsdUpload::Failed;} return OsdUpload::Complete; } void Reset() override { images.clear(); image_rows.clear(); zeros.clear(); } + ID3D11Device* D3D11Device() const override { return device.Get(); } }; class D3D12Graphics final : public OsdGraphics { diff --git a/layer/src/osd_renderer.cpp b/layer/src/osd_renderer.cpp index 118e8416..b612f765 100644 --- a/layer/src/osd_renderer.cpp +++ b/layer/src/osd_renderer.cpp @@ -3,6 +3,7 @@ #include #include #include "depthxr/osd_renderer.h" +#include "depthxr/game_context_lock.h" #include "depthxr/logger.h" #include "depthxr/osd_strings.h" #include @@ -474,6 +475,7 @@ OsdBitmap RasterizeOsd(const OsdSettings& settings, bool compact, const OsdSnaps return output; } +OsdRenderer::OsdRenderer() : context_lock_(std::make_unique()) {} OsdRenderer::~OsdRenderer() { Shutdown(); } void OsdRenderer::RasterWorker() { SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL); @@ -533,7 +535,7 @@ void OsdRenderer::ShutdownLocked() { if(graphics_) graphics_->Reset(); if (swapchain_ && api_.destroy) api_.destroy(swapchain_); if (space_ && api_.destroy_space) api_.destroy_space(space_); - WriteDiagnostics(logger_,CollectDiagnosticsLocked(true)); graphics_.reset(); + WriteDiagnostics(logger_,CollectDiagnosticsLocked(true)); graphics_.reset(); context_lock_->Set(nullptr); if (anchor_space_ && api_.destroy_space) api_.destroy_space(anchor_space_); swapchain_=XR_NULL_HANDLE; space_=XR_NULL_HANDLE; anchor_space_=XR_NULL_HANDLE; session_=XR_NULL_HANDLE; anchor_failed_=false; anchor_logged_=false; @@ -568,6 +570,7 @@ void OsdRenderer::InitializeGraphics(XrSession session,const void* chain,std::ui session_=session;max_layers_=max_layers;api_=api;logger_=logger;identity_=std::move(identity);world_anchored_=false; measurements_={};events_.clear();dropped_events_=0;report_start_=Clock::now();cadence_last_={};cadence_mode_=3; std::string error;graphics_=CreateOsdGraphics(chain,error); + context_lock_->Set(graphics_?graphics_->D3D11Device():nullptr); status_.available=graphics_!=nullptr;status_.message=graphics_?"Disabled":error.empty()?"OSD graphics binding unavailable":error; Event(false,"initialize api="+std::string(graphics_?graphics_->Name():"Unsupported")+" maxLayers="+std::to_string(max_layers_)+" status="+status_.message); } @@ -692,9 +695,22 @@ bool OsdRenderer::CreateResources() { Event(false,"resources ready api="+std::string(graphics_->Name())+" format="+std::to_string(format)+" texture=960x768 usage=transfer-dst|color initializationCpuMs="+std::to_string(Milliseconds(initialization_start))); return true; } +void OsdRenderer::SetContextWaitHookForTest(std::function hook) { context_lock_->SetWaitHookForTest(std::move(hook)); } const XrCompositionLayerBaseHeader* OsdRenderer::Append(const XrFrameEndInfo& info, bool should_render) { const auto entry=Clock::now(); - std::scoped_lock lock(mutex_); + // Direct3D 11: the upload below runs on the game's immediate context, + // under mutex_. A game thread may hold the device's lock through an + // OpenXR call that waits for mutex_ (through the layer's own lock, which + // the status poll holds while it reads Status), so while the panel is + // drawn the device's lock comes first, holding none (GameContextLock). + GameContextHold context_hold; + std::unique_lock lock(mutex_); + if (context_lock_->Active() && settings_.enabled && visible_ && graphics_ && session_ && !failed_ && should_render && + info.layerCount>0) { + lock.unlock(); + context_hold=context_lock_->Hold(); + lock.lock(); + } auto& stats=measurements_[Mode()]; MetricTimer append_timer(stats.append,entry); status_.visible=false; diff --git a/layer/src/runtime_pacing.cpp b/layer/src/runtime_pacing.cpp index ddc108ce..42a71508 100644 --- a/layer/src/runtime_pacing.cpp +++ b/layer/src/runtime_pacing.cpp @@ -141,7 +141,8 @@ std::string SerializeObservations(const std::vector& o stream << " \"firstUsedUnixSeconds\": " << observation.first_used_unix_seconds << ",\n"; stream << " \"lastUsedUnixSeconds\": " << observation.last_used_unix_seconds << ",\n"; stream << " \"probeTimeouts\": " << observation.probe_timeouts << ",\n"; - stream << " \"stableSeconds\": " << observation.stable_seconds << "\n"; + stream << " \"stableSeconds\": " << observation.stable_seconds << ",\n"; + stream << " \"earlyProbeFailures\": " << observation.early_probe_failures << "\n"; stream << " }"; if (i + 1 < observations.size()) { stream << ","; @@ -240,6 +241,7 @@ std::vector ReadRuntimePacingObservations(const std::f observation.last_used_unix_seconds = ExtractInteger(object, "lastUsedUnixSeconds", 0); observation.probe_timeouts = ExtractInteger(object, "probeTimeouts", 0); observation.stable_seconds = ExtractInteger(object, "stableSeconds", 0); + observation.early_probe_failures = ExtractInteger(object, "earlyProbeFailures", 0); observations.push_back(std::move(observation)); } @@ -279,6 +281,28 @@ std::optional FindRuntimePacingObservation(const std:: return std::nullopt; } +std::optional RuntimePacingReprobeReason(const RuntimePacingObservation& observation, + const std::string& layer_version, + const std::string& runtime_version, + std::int64_t now_unix_ms) { + if (observation.mode != TurboPacingMode::kSequenced) return std::nullopt; + if (observation.early_probe_failures > 0 && observation.early_probe_failures < kEarlyProbeFailureLimit) { + return std::string("its async probe fell back within seconds of engaging"); + } + if (!observation.layer_version.empty() && !layer_version.empty() && observation.layer_version != layer_version) { + return "it was recorded by VectorXR " + observation.layer_version; + } + if (!observation.runtime_version.empty() && !runtime_version.empty() && + observation.runtime_version != runtime_version) { + return "it was last used with runtime version " + observation.runtime_version; + } + const auto tested = static_cast(observation.test_started_at); + if (tested > 0 && now_unix_ms - tested > kReprobeAgeMs) { + return std::string("it is more than 30 days old"); + } + return std::nullopt; +} + bool RecordRuntimePacingObservation(const std::filesystem::path& path, const RuntimePacingObservation& observation, std::string* error) { diff --git a/layer/tests/config_tests.cpp b/layer/tests/config_tests.cpp index 27096e5d..8e293995 100644 --- a/layer/tests/config_tests.cpp +++ b/layer/tests/config_tests.cpp @@ -1571,6 +1571,30 @@ void TestFrameGenProfileResolution() { Expect(inherited.ok && depthxr::ResolveFrameGenSettings(inherited.document, "DCS.exe").multiplier == depthxr::FrameGenMultiplier::kDynamic, "A profile without a frame multiplier should run Dynamic"); + // Edge fill is off unless a profile (or the defaults) turns it on. + Expect(!inherited.document.framegen.defaults.edge_fill && + !depthxr::ResolveFrameGenSettings(inherited.document, "DCS.exe").edge_fill, + "Edge fill should be off by default"); + const depthxr::ParseResult filled = depthxr::ParseConfig(FrameGenConfigJson( + R"json({ "enabled": false, "profiles": [ { "name": "MSFS", "applicationIds": ["app-msfs"], "settings": { "edgeFill": true } } ] })json")); + Expect(filled.ok && filled.warnings.empty() && + depthxr::ResolveFrameGenSettings(filled.document, "FlightSimulator2024.exe").edge_fill && + !depthxr::ResolveFrameGenSettings(filled.document, "DCS.exe").edge_fill, + "A profile's edgeFill was not read: " + filled.error); + Expect(!depthxr::ParseConfig(FrameGenConfigJson(R"json({ "enabled": true, "defaults": { "edgeFill": "yes" } })json")).ok, + "A non-boolean edgeFill was accepted"); + // The game's depth is used unless a profile (or the defaults) turns it off. + Expect(inherited.document.framegen.defaults.use_game_depth && + depthxr::ResolveFrameGenSettings(inherited.document, "DCS.exe").use_game_depth, + "Use game depth should be on by default"); + const depthxr::ParseResult no_depth = depthxr::ParseConfig(FrameGenConfigJson( + R"json({ "enabled": false, "profiles": [ { "name": "MSFS", "applicationIds": ["app-msfs"], "settings": { "useGameDepth": false } } ] })json")); + Expect(no_depth.ok && no_depth.warnings.empty() && + !depthxr::ResolveFrameGenSettings(no_depth.document, "FlightSimulator2024.exe").use_game_depth && + depthxr::ResolveFrameGenSettings(no_depth.document, "DCS.exe").use_game_depth, + "A profile's useGameDepth was not read: " + no_depth.error); + Expect(!depthxr::ParseConfig(FrameGenConfigJson(R"json({ "enabled": true, "defaults": { "useGameDepth": "no" } })json")).ok, + "A non-boolean useGameDepth was accepted"); } // Frame Generation's A/B toggle takes a list too, and its legacy binding. diff --git a/layer/tests/d3d11_bridge_layer_tests.cpp b/layer/tests/d3d11_bridge_layer_tests.cpp index 13f84595..3a89c46c 100644 --- a/layer/tests/d3d11_bridge_layer_tests.cpp +++ b/layer/tests/d3d11_bridge_layer_tests.cpp @@ -21,11 +21,13 @@ #include #include #include +#include #include #include #include "depthxr/framegen_gpu.h" #include "depthxr/framegen_plan.h" +#include "depthxr/game_context_lock.h" #include "depthxr/openxr_layer.h" #include "depthxr/openxr_loader_api_layer.h" #include "depthxr/quadviews_sizing.h" @@ -76,9 +78,16 @@ class D3D11BridgeLayerTestPeer { static D3D11Bridge& Bridge() { return OpenXrLayer::Instance().d3d11_bridge_; } + // The game's device's lock, as the layer takes it before its own locks. + static GameContextLock& GameContext() { + return *OpenXrLayer::Instance().game_context_; + } static bool QueueSubstituted() { return OpenXrLayer::Instance().framegen_queue_.Active(); } + static std::string QueueState() { + return OpenXrLayer::Instance().framegen_queue_.Describe(); + } // What keeping shown layers lazy did in the session so far. static FrameGenQueueSubstitution::ShownLayerCounts ShownLayers() { return OpenXrLayer::Instance().framegen_queue_.ShownLayers(); @@ -93,6 +102,7 @@ class D3D11BridgeLayerTestPeer { return layer.instance_ == XR_NULL_HANDLE && layer.active_session_ == XR_NULL_HANDLE && !layer.d3d11_bridge_.Active() && !layer.framegen_queue_.Active() && !layer.framegen_.Started(); } + static OsdSnapshot Osd() { return OpenXrLayer::Instance().BuildOsdSnapshot(); } // The layer's log so far: an error reaches the file at once, with // everything logged before it. static std::string Log() { @@ -116,6 +126,15 @@ class D3D11BridgeLayerTestPeer { static void SetFrameGenEngagement(std::uint32_t steady_frames, std::chrono::milliseconds minimum_age) { OpenXrLayer::Instance().framegen_.SetEngagementPolicy(steady_frames, minimum_age); } + static void SetFrameGenDynamicPolicy(const FrameGenDynamicPolicy& policy) { + OpenXrLayer::Instance().framegen_.SetDynamicPolicy(policy); + } + static std::filesystem::path ActiveLogPath() { + return OpenXrLayer::Instance().logger_.ActiveLogPath(); + } + static void SetFrameGenSummaryInterval(std::chrono::milliseconds interval) { + OpenXrLayer::Instance().framegen_.SetSummaryInterval(interval); + } // Frame Generation's hotkey: whether it is on afterwards. static bool ToggleFrameGen() { return OpenXrLayer::Instance().framegen_.Toggle(); @@ -519,7 +538,11 @@ void Clear(Game& game, ID3D11Texture2D* texture, const Color& color, std::uint32 game.context->ClearRenderTargetView(target.Get(), rgba); } -void ClearDepth(Game& game, ID3D11Texture2D* texture, std::uint32_t slice) { +// The depth Frame clears its depth target's first slice to (the second to +// half of it): a test sets it per frame to see each frame's depth arrive. +float g_frame_depth = 0.5f; + +void ClearDepth(Game& game, ID3D11Texture2D* texture, std::uint32_t slice, float value = 0.5f) { D3D11_DEPTH_STENCIL_VIEW_DESC view{}; view.Format = DXGI_FORMAT_D32_FLOAT; view.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DARRAY; @@ -527,7 +550,7 @@ void ClearDepth(Game& game, ID3D11Texture2D* texture, std::uint32_t slice) { view.Texture2DArray.ArraySize = 1; ComPtr target; Check(SUCCEEDED(game.device->CreateDepthStencilView(texture, &view, &target)), "a depth view of an image"); - game.context->ClearDepthStencilView(target.Get(), D3D11_CLEAR_DEPTH, 0.5f, 0); + game.context->ClearDepthStencilView(target.Get(), D3D11_CLEAR_DEPTH, value, 0); } // Direct3D 11 still works: an event query after a flush completes. @@ -610,6 +633,7 @@ struct StandInSwapchain { std::deque acquired; // oldest first std::uint32_t next{0}; std::optional released; // the image released last + std::uint32_t current{0}; // StandIn::pimax: PVR's current image, the next once shown (committed) int acquires{0}; int waits{0}; int releases{0}; @@ -622,8 +646,12 @@ struct ShownView { std::uint32_t slice{0}; XrStructureType next_type{XR_TYPE_UNKNOWN}; // the first structure of the view's chain bool depth{false}; // the chain had XrCompositionLayerDepthInfoKHR + XrSwapchain depth_swapchain{XR_NULL_HANDLE}; // ...naming this depth swapchain + std::uint32_t depth_slice{0}; // ...and slice + XrStructureType after_depth{XR_TYPE_UNKNOWN}; // ...followed by this structure std::optional texel; // the centre of the image released last std::vector probed; // its texels at StandIn::probes + std::optional depth_value; // the centre of the depth image released last (Direct3D 12) }; struct ShownFrame { @@ -650,8 +678,15 @@ struct StandIn { // xrAcquireSwapchainImage hands out the image released last again, as // Pimax's runtime does (else each in turn, as Virtual Desktop's does). bool same_image{false}; + // PimaxXR 0.4.4's frame loop and swapchains (g_pimax): its compositor + // thread paces the frames, xrEndFrame waits for it, and a swapchain's + // images advance only when shown. + bool pimax{false}; // Where each shown view's image is read besides its centre (fractions of its size). std::vector> probes; + // xrEndFrame reads nothing back (it never waits for the frame's work, as + // a compositor's does not): a GPU-bound game's frames overlap its CPU work. + bool unread{false}; // Direct3D 11 held back behind it: the next readback is checked to wait // for the game's rendering, then the gate is opened. Gate* hold{nullptr}; @@ -696,6 +731,10 @@ struct StandIn { std::vector created; std::vector destroyed; std::vector frames; + // Frames xrEndFrame refused for a projection's sub-image outside its + // image (SubImageRefusal), and the first one's reason. + int refused_frames{0}; + std::string refusal; std::function on_destroy_swapchain; std::function on_destroy_session; std::function on_destroy_instance; // before a session still alive goes with the instance @@ -710,6 +749,18 @@ struct StandIn { std::uint64_t frame_waits{0}; std::uint64_t frame_begins{0}; std::uint64_t frame_ends{0}; + // A frame begun and not ended yet, and how many such frames a begin + // discarded (the runtime shows its last frame again; PimaxXR 0.4.4 a + // blank refresh, PimaxCompositor::blank_refreshes). + bool frame_open{false}; + std::uint64_t frames_left_open{0}; + // pimax: the next `pimax_clamp` waits report a display time 1 ns after + // the last (PimaxXR clamps a PVR prediction that did not advance), every + // `pimax_clamp_every`th wait (0: never). `reported` is the last reported. + int pimax_clamp_every{0}; + int pimax_clamp{0}; + std::uint64_t pimax_clamped{0}; + XrTime reported{0}; std::uintptr_t handles{0x1000}; // The index of the first `call` at or after `from`, or -1. @@ -726,6 +777,8 @@ StandIn g_runtime; // the runtime as the application's thread does. The test reads what the // stand-in saw under it too (Shown). std::recursive_mutex g_runtime_mutex; +// With g_runtime_mutex: a frame began (StandIn::pimax's xrWaitFrame waits for it). +std::condition_variable_any g_frame_begun; // Frame loop calls in progress: xrWaitFrame never overlaps another wait, nor // xrBeginFrame or xrEndFrame another begin or end, whichever threads make them. std::atomic g_waits_in{0}; @@ -786,6 +839,189 @@ void SleepUntil(std::chrono::steady_clock::time_point until) { if (timer) CloseHandle(timer); } +// PimaxXR 0.4.4's frame loop with async_submission (its default; +// pimax-openxr frame.cpp, d3d12_interop.cpp). Its submission thread calls +// pvr_waitToBeginFrame (the next refresh, once the frame before has rendered) +// then is idle until xrEndFrame hands it the frame for pvr_endFrame. +// xrWaitFrame waits for it to be idle, giving up 2 ms early ("running +// start"); xrEndFrame waits for it to be idle with no timeout, then +// serializes the session's queue to PVR's submission device (a fence +// signalled on the queue, which that device waits for before composing). +// The submission device is a queue of the stand-in's own here. +struct PimaxCompositor { + using Clock = std::chrono::steady_clock; + std::mutex mutex; + std::condition_variable cv; + bool running{false}; + bool stop{false}; + bool idle{false}; // no frame handed over (PimaxXR: m_layersForAsyncSubmission empty) + bool handed{false}; // a frame handed over, not yet taken + UINT64 serialized{0}; // the session queue's value the frame handed over waits for + Clock::time_point began{}; // the last pvr_waitToBeginFrame's return + ComPtr serialize; // signalled on the session's queue at xrEndFrame + ComPtr submission; + ComPtr rendered; + UINT64 rendered_value{0}; + std::thread thread; + // What the frame loop's calls waited. + double end_wait_ms{0.0}; + double wait_wait_ms{0.0}; + std::uint64_t ends{0}; + std::uint64_t waits{0}; + // Frames begun and never ended (Gap): each a PVR frame with one disabled + // layer, shown as a blank refresh, that waits a refresh of its own. + int gaps{0}; + std::uint64_t blank_refreshes{0}; + // quirk_sync_gpu_work_in_end_frame ("Wait for GPU work in EndFrame"): + // xrEndFrame waits on the CPU for the session queue's work so far, its + // locks held (d3d11_native.cpp serializeD3D12Frame's CPU wait). + bool sync_gpu_in_end{false}; + double end_sync_ms{0.0}; + + void Start(ID3D12Device* device) { + Check(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&serialize))) && + SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&rendered))), + "PimaxXR's fences"); + D3D12_COMMAND_QUEUE_DESC desc{}; + Check(SUCCEEDED(device->CreateCommandQueue(&desc, IID_PPV_ARGS(&submission))), "PVR's submission queue"); + running = true; + thread = std::thread([this] { Run(); }); + } + void Stop() { + { + std::scoped_lock lock(mutex); + if (!running) return; + stop = true; + idle = true; + } + cv.notify_all(); + thread.join(); + WaitFence(rendered.Get(), rendered_value, 2000); + std::scoped_lock lock(mutex); + running = stop = idle = handed = defer = sync_gpu_in_end = false; + serialize.Reset(); + submission.Reset(); + rendered.Reset(); + serialized = rendered_value = ends = waits = blank_refreshes = 0; + gaps = 0; + end_wait_ms = wait_wait_ms = end_sync_ms = 0.0; + } + + // A frame begun and never ended (xrBeginFrame over it). + void Gap() { + std::scoped_lock lock(mutex); + if (!running) return; + ++gaps; + ++blank_refreshes; + } + + void Run() { + const std::chrono::nanoseconds period(kPeriod); + Clock::time_point refresh = Clock::now(); + for (;;) { + // Frame IDs left out by frames begun and never ended: each waited + // for and completed with a disabled layer first. + int gap = 0; + { + std::scoped_lock lock(mutex); + gap = std::exchange(gaps, 0); + } + for (; gap > 0; --gap) { + WaitFence(rendered.Get(), rendered_value, 1000); + const auto now = Clock::now(); + while (refresh <= now) refresh += period; + SleepUntil(refresh); + } + // pvr_waitToBeginFrame: the frame before rendered, then the next refresh. + WaitFence(rendered.Get(), rendered_value, 1000); + const auto now = Clock::now(); + while (refresh <= now) refresh += period; + SleepUntil(refresh); + UINT64 value = 0; + { + std::unique_lock lock(mutex); + began = Clock::now(); + idle = true; + cv.notify_all(); + cv.wait(lock, [&] { return stop || handed; }); + if (stop) return; + handed = false; + value = serialized; + } + // pvr_endFrame: PVR's submission device composes once the session's queue is serialized. + submission->Wait(serialize.Get(), value); + submission->Signal(rendered.Get(), ++rendered_value); + } + } + + // defer_frame_wait (its "Prefer framerate over latency"): xrWaitFrame does not wait. + bool defer{false}; + + // xrWaitFrame's wait (running start). + void Wait() { + const auto start = Clock::now(); + std::unique_lock lock(mutex); + if (!running || defer) return; + const auto until = began + std::chrono::nanoseconds(kPeriod) - std::chrono::microseconds(2000); + cv.wait_until(lock, until, [&] { return idle; }); + wait_wait_ms += std::chrono::duration(Clock::now() - start).count(); + ++waits; + } + + // xrEndFrame: waits for the thread to be idle, serializes `queue`, hands the frame over. + void End(ID3D12CommandQueue* queue) { + const auto start = Clock::now(); + std::unique_lock lock(mutex); + if (!running) return; + cv.wait(lock, [&] { return idle; }); + end_wait_ms += std::chrono::duration(Clock::now() - start).count(); + ++ends; + if (stop) return; + ++serialized; + queue->Signal(serialize.Get(), serialized); + if (sync_gpu_in_end) { + // On the CPU, its locks held (bounded: a stand-in never waits forever). + const auto sync = Clock::now(); + lock.unlock(); + WaitFence(serialize.Get(), serialized, 1000); + lock.lock(); + end_sync_ms += std::chrono::duration(Clock::now() - sync).count(); + } + idle = false; + handed = true; + cv.notify_all(); + } +}; +PimaxCompositor g_pimax; +// PimaxXR's m_swapchainsMutex: xrEndFrame holds it across its wait for the +// submission thread (frame.cpp), and its swapchain calls take it +// (swapchain.cpp), so a game's swapchain calls wait out the presenter's end. +std::mutex g_pimax_swapchains; +std::atomic g_pimax_swapchain_calls{0}; +std::atomic g_pimax_swapchain_wait_us{0}; +std::atomic g_pimax_swapchain_wait_max_us{0}; +std::thread::id g_pimax_game_thread; // the game's calls are the ones timed +std::unique_lock PimaxSwapchainsLock(bool timed) { + bool pimax = false; + { + std::scoped_lock lock(g_runtime_mutex); + pimax = g_runtime.pimax; + } + if (!pimax) return {}; + const auto start = std::chrono::steady_clock::now(); + std::unique_lock lock(g_pimax_swapchains); + if (timed && std::this_thread::get_id() == g_pimax_game_thread) { + const auto waited = static_cast( + std::chrono::duration_cast(std::chrono::steady_clock::now() - start).count()); + g_pimax_swapchain_wait_us += waited; + ++g_pimax_swapchain_calls; + std::uint64_t max = g_pimax_swapchain_wait_max_us.load(); + while (waited > max && !g_pimax_swapchain_wait_max_us.compare_exchange_weak(max, waited)) { + } + } + return lock; +} + StandInSwapchain* FindSwapchain(XrSwapchain swapchain) { const auto it = g_runtime.swapchains.find(swapchain); return it == g_runtime.swapchains.end() ? nullptr : &it->second; @@ -815,6 +1051,7 @@ struct Source { ID3D12Resource* image{nullptr}; std::uint32_t slice{0}; std::array at{0.5f, 0.5f}; + bool depth{false}; // a depth image (D32): in the depth-write state while the runtime holds it }; using Sources = std::vector; @@ -854,14 +1091,17 @@ bool SubmitReadback(ID3D12CommandQueue* queue, ID3D12Fence* fence, UINT64& fence IID_PPV_ARGS(&readback.list)))) { return false; } - // Colour images are render targets whenever the runtime holds them. + // Colour images are render targets whenever the runtime holds them, depth + // images depth targets. std::vector barriers; for (const Source& texel : sources) { if (std::none_of(barriers.begin(), barriers.end(), [&](const D3D12_RESOURCE_BARRIER& barrier) { return barrier.Transition.pResource == texel.image; })) { - barriers.push_back( - Transition(texel.image, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE)); + barriers.push_back(Transition(texel.image, + texel.depth ? D3D12_RESOURCE_STATE_DEPTH_WRITE + : D3D12_RESOURCE_STATE_RENDER_TARGET, + D3D12_RESOURCE_STATE_COPY_SOURCE)); } } readback.list->ResourceBarrier(static_cast(barriers.size()), barriers.data()); @@ -871,7 +1111,14 @@ bool SubmitReadback(ID3D12CommandQueue* queue, ID3D12Fence* fence, UINT64& fence source.SubresourceIndex = sources[i].slice * image.MipLevels; D3D12_TEXTURE_COPY_LOCATION destination{readback.buffer.Get(), D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT}; destination.PlacedFootprint.Offset = kReadbackStride * i; - destination.PlacedFootprint.Footprint = {image.Format, 1, 1, 1, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT}; + DXGI_FORMAT format = image.Format; + if (sources[i].depth) { + // A depth plane copies through its own format (D32_FLOAT's: R32_TYPELESS). + D3D12_PLACED_SUBRESOURCE_FOOTPRINT plane{}; + device->GetCopyableFootprints(&image, source.SubresourceIndex, 1, 0, &plane, nullptr, nullptr, nullptr); + format = plane.Footprint.Format; + } + destination.PlacedFootprint.Footprint = {format, 1, 1, 1, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT}; const UINT x = TexelAt(sources[i].at[0], static_cast(image.Width)); const UINT y = TexelAt(sources[i].at[1], image.Height); const D3D12_BOX box{x, y, 0, x + 1, y + 1, 1}; @@ -1047,8 +1294,15 @@ XrResult XRAPI_CALL RtDestroyInstance(XrInstance instance) { } XrResult XRAPI_CALL RtGetInstanceProperties(XrInstance, XrInstanceProperties* properties) { - properties->runtimeVersion = XR_MAKE_VERSION(1, 0, 0); - std::strncpy(properties->runtimeName, "VectorXR stand-in runtime", XR_MAX_RUNTIME_NAME_SIZE - 1); + bool pimax = false; + { + std::scoped_lock lock(g_runtime_mutex); + pimax = g_runtime.pimax; + } + // StandIn::pimax: as PimaxXR 0.4.4 names itself (pimax-openxr instance.cpp). + properties->runtimeVersion = pimax ? XR_MAKE_VERSION(0, 4, 4) : XR_MAKE_VERSION(1, 0, 0); + std::strncpy(properties->runtimeName, pimax ? "PimaxXR (Unofficial)" : "VectorXR stand-in runtime", + XR_MAX_RUNTIME_NAME_SIZE - 1); return XR_SUCCESS; } @@ -1262,13 +1516,36 @@ XrResult XRAPI_CALL RtWaitFrame(XrSession, const XrFrameWaitInfo*, XrFrameState* } std::optional until; XrTime time = 0; + bool pimax = false; + { + std::unique_lock lock(g_runtime_mutex); + pimax = g_runtime.pimax; + if (pimax) { + // PimaxXR waits, with no timeout, until the frame waited before + // has begun (frame.cpp xrWaitFrame, m_frameCondVar): a second + // wait from the thread that would begin hangs it for good. A + // watchdog fails the case instead. + Check(g_frame_begun.wait_for(lock, std::chrono::seconds(2), + [] { return g_runtime.frame_waits == g_runtime.frame_begins; }), + "xrWaitFrame waited 2 s for the frame waited before it to begin (PimaxXR would hang there)"); + } + } + if (pimax) g_pimax.Wait(); { std::scoped_lock lock(g_runtime_mutex); StandIn& rt = g_runtime; Check(rt.frame_waits == rt.frame_begins, "xrWaitFrame comes only once the frame waited before it began"); ++rt.frame_waits; XrDuration refreshes = 1; - if (rt.paced) { + if (rt.pimax) { + // A refresh on from the last wait's, or as many as went by. + const auto now = std::chrono::steady_clock::now(); + const std::chrono::nanoseconds period(kPeriod); + if (rt.refresh != std::chrono::steady_clock::time_point{}) { + refreshes = std::max(1, static_cast((now - rt.refresh + period / 2) / period)); + } + rt.refresh = now; + } else if (rt.paced) { // The next refresh, or the first one after a wait that comes late. const auto now = std::chrono::steady_clock::now(); const std::chrono::nanoseconds period(kPeriod); @@ -1284,6 +1561,17 @@ XrResult XRAPI_CALL RtWaitFrame(XrSession, const XrFrameWaitInfo*, XrFrameState* } rt.time += kPeriod * refreshes; time = rt.time; + if (rt.pimax && rt.pimax_clamp_every > 0 && rt.frame_waits % rt.pimax_clamp_every == 0) { + rt.pimax_clamp = 2; + } + if (rt.pimax && rt.pimax_clamp > 0 && rt.reported > 0) { + // PVR's prediction did not advance: PimaxXR's clamp (frame.cpp xrWaitFrame). + --rt.pimax_clamp; + ++rt.pimax_clamped; + time = rt.reported + 1; + } + time = std::max(time, rt.reported + 1); + rt.reported = time; } if (until) SleepUntil(*until); state->predictedDisplayTime = time; @@ -1298,16 +1586,110 @@ XrResult XRAPI_CALL RtBeginFrame(XrSession, const XrFrameBeginInfo*) { StandIn& rt = g_runtime; Check(rt.frame_begins < rt.frame_waits, "xrBeginFrame begins a frame waited for"); ++rt.frame_begins; + g_frame_begun.notify_all(); + const bool discarded = rt.frame_open; + rt.frame_open = true; + if (discarded) { + // A begin over an open frame discards it. PimaxXR 0.4.4's submission + // thread completes that PVR frame with one disabled layer (a blank + // refresh) and waits a refresh more for it (frame.cpp + // asyncSubmissionThread, quirk_complete_discarded_frames). + ++rt.frames_left_open; + if (rt.pimax) g_pimax.Gap(); + // (PimaxXR says so; the plain stand-in has always reported success.) + if (rt.pimax) return XR_FRAME_DISCARDED; + } + return XR_SUCCESS; +} + +// As PimaxXR's and Virtual Desktop's xrEndFrame check a projection's +// sub-images (holding g_runtime_mutex): each view's colour and depth rect +// inside its image (XR_ERROR_SWAPCHAIN_RECT_INVALID), and its depth's slice +// one of the image's (XR_ERROR_VALIDATION_FAILURE). XR_SUCCESS: none +// outside. `why` says which. +XrResult SubImageRefusal(const XrFrameEndInfo& info, std::string& why) { + const auto check = [&](const XrSwapchainSubImage& sub, const char* what, bool depth) { + const StandInSwapchain* chain = FindSwapchain(sub.swapchain); + if (!chain) { + why = std::string(what) + " names no swapchain"; + return XR_ERROR_HANDLE_INVALID; + } + const XrRect2Di& rect = sub.imageRect; + const std::int64_t right = static_cast(rect.offset.x) + rect.extent.width; + const std::int64_t bottom = static_cast(rect.offset.y) + rect.extent.height; + std::ostringstream text; + text << what << " rect " << rect.offset.x << "," << rect.offset.y << " " << rect.extent.width << "x" + << rect.extent.height << " slice " << sub.imageArrayIndex << " on " << chain->info.width << "x" + << chain->info.height << " of " << chain->info.arraySize; + if (depth && sub.imageArrayIndex >= chain->info.arraySize) { + why = text.str(); + return XR_ERROR_VALIDATION_FAILURE; + } + if (rect.offset.x < 0 || rect.offset.y < 0 || rect.extent.width <= 0 || rect.extent.height <= 0 || + right > static_cast(chain->info.width) || + bottom > static_cast(chain->info.height)) { + why = text.str(); + return XR_ERROR_SWAPCHAIN_RECT_INVALID; + } + return XR_SUCCESS; + }; + for (std::uint32_t i = 0; i < info.layerCount; ++i) { + const XrCompositionLayerBaseHeader* base = info.layers[i]; + if (!base || base->type != XR_TYPE_COMPOSITION_LAYER_PROJECTION) continue; + const auto& projection = *reinterpret_cast(base); + for (std::uint32_t v = 0; v < projection.viewCount; ++v) { + const XrCompositionLayerProjectionView& view = projection.views[v]; + if (const XrResult result = check(view.subImage, "colour", false); result != XR_SUCCESS) return result; + for (auto* chain = static_cast(view.next); chain; chain = chain->next) { + if (chain->type != XR_TYPE_COMPOSITION_LAYER_DEPTH_INFO_KHR) continue; + const auto& depth = *reinterpret_cast(chain); + if (const XrResult result = check(depth.subImage, "depth", true); result != XR_SUCCESS) return result; + } + } + } return XR_SUCCESS; } XrResult XRAPI_CALL RtEndFrame(XrSession, const XrFrameEndInfo* info) { const InCall in(g_begins_ends_in, "xrBeginFrame or xrEndFrame"); + ComPtr pimax_queue; + { + std::scoped_lock lock(g_runtime_mutex); + if (g_runtime.pimax) pimax_queue = g_runtime.queue; + } + const std::unique_lock pimax_swapchains = PimaxSwapchainsLock(false); + if (pimax_queue) g_pimax.End(pimax_queue.Get()); std::scoped_lock lock(g_runtime_mutex); StandIn& rt = g_runtime; rt.calls.push_back("xrEndFrame"); + if (rt.pimax) { + // Committing a swapchain shown moves PVR's current image on. + std::set committed; + for (std::uint32_t i = 0; i < info->layerCount; ++i) { + const XrCompositionLayerBaseHeader* base = info->layers[i]; + if (base && base->type == XR_TYPE_COMPOSITION_LAYER_PROJECTION) { + const auto& projection = *reinterpret_cast(base); + for (std::uint32_t v = 0; v < projection.viewCount; ++v) { + committed.insert(projection.views[v].subImage.swapchain); + } + } else if (base && base->type == XR_TYPE_COMPOSITION_LAYER_QUAD) { + committed.insert(reinterpret_cast(base)->subImage.swapchain); + } + } + for (const XrSwapchain swapchain : committed) { + if (StandInSwapchain* chain = FindSwapchain(swapchain); chain && chain->Count()) { + chain->current = (chain->current + 1) % static_cast(chain->Count()); + } + } + } Check(rt.frame_ends < rt.frame_begins, "xrEndFrame ends a begun frame"); ++rt.frame_ends; + rt.frame_open = false; + std::string why; + if (const XrResult refused = SubImageRefusal(*info, why); refused != XR_SUCCESS) { + if (rt.refused_frames++ == 0) rt.refusal = why; + return refused; + } ShownFrame frame; frame.time = info->displayTime; for (std::uint32_t i = 0; i < info->layerCount; ++i) { @@ -1325,7 +1707,14 @@ XrResult XRAPI_CALL RtEndFrame(XrSession, const XrFrameEndInfo* info) { const auto* chain = static_cast(view.next); shown.next_type = chain ? chain->type : XR_TYPE_UNKNOWN; for (; chain; chain = chain->next) { - shown.depth = shown.depth || chain->type == XR_TYPE_COMPOSITION_LAYER_DEPTH_INFO_KHR; + if (chain->type == XR_TYPE_COMPOSITION_LAYER_DEPTH_INFO_KHR) { + const auto& depth = *reinterpret_cast(chain); + shown.depth = true; + shown.depth_swapchain = depth.subImage.swapchain; + shown.depth_slice = depth.subImage.imageArrayIndex; + const auto* after = static_cast(depth.next); + shown.after_depth = after ? after->type : XR_TYPE_UNKNOWN; + } } frame.views.push_back(shown); } @@ -1338,7 +1727,9 @@ XrResult XRAPI_CALL RtEndFrame(XrSession, const XrFrameEndInfo* info) { std::size_t per_view = 1; // texels read per view: the centre, then the probes (Direct3D 12) // Nothing is read on a removed device: its fences read as done at once, // and WARP may still be running the readback when it is let go. - if (rt.device12 && SUCCEEDED(rt.device12->GetDeviceRemovedReason())) { + if (rt.unread) { + // A compositor whose xrEndFrame never waits for the frame's work. + } else if (rt.device12 && SUCCEEDED(rt.device12->GetDeviceRemovedReason())) { Sources sources; per_view += rt.probes.size(); for (const ShownView& view : frame.views) { @@ -1353,7 +1744,23 @@ XrResult XRAPI_CALL RtEndFrame(XrSession, const XrFrameEndInfo* info) { sources.push_back({image, view.slice, probe}); } } + // Then each view's depth, as a compositor reprojecting with it reads it. + std::vector depth_views; + for (std::size_t i = 0; !sources.empty() && i < frame.views.size(); ++i) { + const StandInSwapchain* chain = FindSwapchain(frame.views[i].depth_swapchain); + if (chain && chain->released && !chain->images12.empty()) { + sources.push_back({chain->images12[*chain->released].Get(), frame.views[i].depth_slice, {0.5f, 0.5f}, + true}); + depth_views.push_back(i); + } + } read = !sources.empty() && ReadBack12(sources, frame, texels); + for (std::size_t i = 0; read && i < depth_views.size(); ++i) { + const Color& texel = texels[frame.views.size() * per_view + i]; + float value = 0.0f; + std::memcpy(&value, texel.data(), sizeof(value)); + frame.views[depth_views[i]].depth_value = value; + } } else if (rt.device11) { std::vector> sources; for (const ShownView& view : frame.views) { @@ -1484,6 +1891,7 @@ XrResult XRAPI_CALL RtEnumerateSwapchainImages(XrSwapchain swapchain, std::uint3 } XrResult XRAPI_CALL RtAcquire(XrSwapchain swapchain, const XrSwapchainImageAcquireInfo*, std::uint32_t* index) { + const std::unique_lock pimax_swapchains = PimaxSwapchainsLock(true); std::scoped_lock lock(g_runtime_mutex); StandInSwapchain* chain = FindSwapchain(swapchain); if (!chain) return XR_ERROR_HANDLE_INVALID; @@ -1494,7 +1902,12 @@ XrResult XRAPI_CALL RtAcquire(XrSwapchain swapchain, const XrSwapchainImageAcqui } if (chain->acquired.size() >= chain->Count()) return XR_ERROR_CALL_ORDER_INVALID; // Pimax's runtime hands the image back released last, every frame. - *index = g_runtime.same_image && chain->released && chain->acquired.empty() ? *chain->released : chain->next; + if (g_runtime.pimax) { + // PimaxXR resynchronizes to PVR's current image when nothing is acquired. + *index = chain->acquired.empty() ? chain->current : chain->next; + } else { + *index = g_runtime.same_image && chain->released && chain->acquired.empty() ? *chain->released : chain->next; + } chain->next = (*index + 1) % static_cast(chain->Count()); chain->acquired.push_back(*index); ++chain->acquires; @@ -1505,6 +1918,7 @@ XrResult XRAPI_CALL RtAcquire(XrSwapchain swapchain, const XrSwapchainImageAcqui } XrResult XRAPI_CALL RtWait(XrSwapchain swapchain, const XrSwapchainImageWaitInfo*) { + const std::unique_lock pimax_swapchains = PimaxSwapchainsLock(true); std::scoped_lock lock(g_runtime_mutex); StandInSwapchain* chain = FindSwapchain(swapchain); if (!chain) return XR_ERROR_HANDLE_INVALID; @@ -1522,6 +1936,7 @@ XrResult XRAPI_CALL RtWait(XrSwapchain swapchain, const XrSwapchainImageWaitInfo } XrResult XRAPI_CALL RtRelease(XrSwapchain swapchain, const XrSwapchainImageReleaseInfo*) { + const std::unique_lock pimax_swapchains = PimaxSwapchainsLock(true); std::scoped_lock lock(g_runtime_mutex); StandIn& rt = g_runtime; StandInSwapchain* chain = FindSwapchain(swapchain); @@ -1611,6 +2026,22 @@ struct FrameGenChain { }; FrameGenChain g_framegen_chain; +// Set: the stand-in's captures and generations call the game's context +// (Flush), as Direct3D 11's backend does, and note their threads. +std::atomic g_capture_context{nullptr}; +struct CaptureThreads { + std::mutex mutex; + std::vector threads; + void push(DWORD thread) { + std::scoped_lock lock(mutex); + threads.push_back(thread); + } + std::size_t Count(DWORD thread) { + std::scoped_lock lock(mutex); + return static_cast(std::count(threads.begin(), threads.end(), thread)); + } +} g_capture_threads, g_execute_threads; + class StandInGpu final : public depthxr::FrameGenGpu { public: const char* Name() const override { return "Stand-in"; } @@ -1626,11 +2057,22 @@ class StandInGpu final : public depthxr::FrameGenGpu { void Forget(XrSwapchain) override {} bool Capture(std::size_t, const depthxr::FrameGenImage&, const depthxr::FrameGenViewRequest&, depthxr::FrameGenFlowQuality, std::string&) override { + // As Direct3D 11's backend copies the released image on the game's + // immediate context, under Frame Generation's (and the layer's) locks. + if (ID3D11DeviceContext* context = g_capture_context.load()) { + g_capture_threads.push(GetCurrentThreadId()); + context->Flush(); + } return true; } bool WaitIdle() override { return true; } bool Execute(const std::vector& views, const depthxr::FrameGenTuning&, depthxr::FrameGenExecuteResult& result, std::string&) override { + // As Direct3D 11's backend generates on the game's immediate context. + if (ID3D11DeviceContext* context = g_capture_context.load()) { + g_execute_threads.push(GetCurrentThreadId()); + context->Flush(); + } result.synthesized.assign(views.size(), true); return true; } @@ -1927,14 +2369,17 @@ struct Eye { // One application frame: each colour target acquired, waited, cleared to its // colour and released, in the order given (with `depth`, cleared, when given); // then a stereo projection of `eyes`, each view chaining its slice of `depth` -// (and a structure of the application's after it) when given. With `hold`, +// (and a structure of the application's after it) when given, as +// `edit_depth` leaves the views' depth structures. With `hold`, // Direct3D 11 waits for the gate from just before the LAST colour target's // release on (after the others' releases): that image alone is not written // yet, and nothing queued for the releases before it waits for the gate. The // stand-in opens the gate once it has seen its readback wait all the same. XrResult Frame(App& app, Game& game, const std::vector& targets, const std::vector& colors, const std::array& eyes, const Target* depth = nullptr, Gate* hold = nullptr, - bool unknown_after_depth = false) { + bool unknown_after_depth = false, + const std::function&)>& edit_depth = {}, + const std::function& end_frame = {}) { XrFrameWaitInfo wait_info{XR_TYPE_FRAME_WAIT_INFO}; XrFrameState state{XR_TYPE_FRAME_STATE}; Check(app.api.wait_frame(app.session, &wait_info, &state) == XR_SUCCESS, "xrWaitFrame"); @@ -1961,8 +2406,8 @@ XrResult Frame(App& app, Game& game, const std::vector& targets, Draw(game, targets[i]->images[indices[i]], colors[i]); } if (depth) { - ClearDepth(game, depth->images[depth_index], 0); - ClearDepth(game, depth->images[depth_index], 1); + ClearDepth(game, depth->images[depth_index], 0, g_frame_depth); + ClearDepth(game, depth->images[depth_index], 1, g_frame_depth * 0.5f); } for (std::size_t i = 0; i < targets.size(); ++i) { if (hold && i + 1 == targets.size()) { @@ -2001,6 +2446,9 @@ XrResult Frame(App& app, Game& game, const std::vector& targets, views[eye].next = &depth_infos[eye]; } } + if (depth && edit_depth) { + edit_depth(depth_infos); + } XrCompositionLayerProjection projection{XR_TYPE_COMPOSITION_LAYER_PROJECTION}; projection.space = app.space; projection.viewCount = 2; @@ -2012,7 +2460,8 @@ XrResult Frame(App& app, Game& game, const std::vector& targets, end.layerCount = 1; end.layers = layers; g_runtime.hold = hold; - const XrResult result = app.api.end_frame(app.session, &end); + // `end_frame`: ends it instead (the frame's images all rendered and released). + const XrResult result = end_frame ? end_frame(end) : app.api.end_frame(app.session, &end); if (g_runtime.hold) { // The stand-in never read the frame back: let the game run on. g_runtime.hold->Open(); @@ -2286,6 +2735,8 @@ struct QuadFrameOptions { // XR_ERROR_SESSION_LOST ends the frame there (returned as it is); any // other failure of theirs fails the test. bool session_may_be_lost{false}; + // Ends the frame instead of xrEndFrame on this thread. + std::function end_frame; }; // One application frame of quad views: the four views located at the frame's @@ -2355,7 +2806,7 @@ XrResult QuadFrame(App& app, Game& game, const QuadTargets& targets, int frame, end.environmentBlendMode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE; end.layerCount = 1; end.layers = layers; - return app.api.end_frame(app.session, &end); + return options.end_frame ? options.end_frame(end) : app.api.end_frame(app.session, &end); } // --- The cases -------------------------------------------------------------- @@ -2418,6 +2869,7 @@ struct CaseSettings { // Auto was the default, so they keep it unless they choose another. std::string frame_multiplier{"auto"}; std::string synthesis_mode; // Frame Generation's (its default when empty) + bool use_game_depth{true}; // Frame Generation's useGameDepth (written only when off) }; // A fresh stand-in runtime, a log of the case's own, and the test's settings. @@ -2427,6 +2879,10 @@ void BeginCase(const std::string& name, const Game& game, const CaseSettings& ca Check(Peer::Idle(), name + ": nothing is left of the cases before it"); g_case = name; g_device12 = MakeDevice12(game); + // Each case's game starts without multithread protection: a case before + // it may have left it on (a mirrored depth swapchain turns it on). + ComPtr multithread; + if (SUCCEEDED(game.context.As(&multithread))) multithread->SetMultithreadProtected(FALSE); // The runtime's clock runs on across sessions, as a runtime's does. const XrTime time = g_runtime.time; g_runtime = StandIn{}; @@ -2452,15 +2908,19 @@ void BeginCase(const std::string& name, const Game& game, const CaseSettings& ca } settings << R"("turbo": {"enabled": false}, "framegen": {"enabled": )" << (case_settings.framegen ? "true" : "false"); - if (!case_settings.frame_multiplier.empty() || !case_settings.synthesis_mode.empty()) { + std::vector defaults; + if (!case_settings.frame_multiplier.empty()) { + defaults.push_back(R"("frameMultiplier": ")" + case_settings.frame_multiplier + R"(")"); + } + if (!case_settings.synthesis_mode.empty()) { + defaults.push_back(R"("synthesisMode": ")" + case_settings.synthesis_mode + R"(")"); + } + if (!case_settings.use_game_depth) { + defaults.push_back(R"("useGameDepth": false)"); + } + if (!defaults.empty()) { settings << R"(, "defaults": {)"; - if (!case_settings.frame_multiplier.empty()) { - settings << R"("frameMultiplier": ")" << case_settings.frame_multiplier << R"(")" - << (case_settings.synthesis_mode.empty() ? "" : ", "); - } - if (!case_settings.synthesis_mode.empty()) { - settings << R"("synthesisMode": ")" << case_settings.synthesis_mode << R"(")"; - } + for (std::size_t i = 0; i < defaults.size(); ++i) settings << (i ? ", " : "") << defaults[i]; settings << "}"; } settings << "}}}\n"; @@ -2686,11 +3146,14 @@ void CheckFrames(App& app, Game& game, Target& left, Target& right) { } } -// 4. Depth on a bridged session stays the game's: its swapchain's calls reach -// the runtime, its depth never reaches submitted frames. +// 4. Depth on a bridged session reaches the runtime: its swapchain's calls go +// on, each frame's views name it (with the rest of their chains), and the +// runtime's depth image holds the frame's depth when it reads it, each eye +// its slice. void CheckDepth(App& app, Game& game, Target& left, Target& right, Target& depth) { - Check(Peer::Bridge().Bridged(depth.swapchain) && Peer::Bridge().Unmirrored(depth.swapchain), - "the depth swapchain is the game's own"); + Check(Peer::Bridge().Bridged(depth.swapchain) && !Peer::Bridge().Unmirrored(depth.swapchain) && + Contains(Peer::Bridge().Describe(depth.swapchain), "mode=depth,"), + "the depth swapchain is mirrored: " + Peer::Bridge().Describe(depth.swapchain)); for (ID3D11Texture2D* image : depth.images) { D3D11_TEXTURE2D_DESC desc{}; image->GetDesc(&desc); @@ -2698,9 +3161,16 @@ void CheckDepth(App& app, Game& game, Target& left, Target& right, Target& depth desc.Format == DXGI_FORMAT_R32_TYPELESS, "the game gets typeless depth images"); } + { + std::scoped_lock lock(g_runtime_mutex); + const StandInSwapchain* runtime = FindSwapchain(depth.swapchain); + Check(runtime && (runtime->info.usageFlags & XR_SWAPCHAIN_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT), + "the runtime's depth swapchain is asked for as a depth target"); + } constexpr int kFrames = 8; for (int frame = 0; frame < kFrames; ++frame) { const std::vector colors{FrameColor(frame + 10, 0), FrameColor(frame + 10, 1)}; + g_frame_depth = 0.1f + 0.1f * static_cast(frame); const XrResult result = Frame(app, game, {&left, &right}, colors, {{{&left, 0}, {&right, 0}}}, &depth, nullptr, true); // explicit unknown metadata: preserve it in pass-through Check(result == XR_SUCCESS, "depth frame " + std::to_string(frame) + ": xrEndFrame " + std::to_string(result)); @@ -2708,18 +3178,29 @@ void CheckDepth(App& app, Game& game, Target& left, Target& right, Target& depth Check(shown.views.size() == 2, "depth frame: two views"); for (int eye = 0; eye < 2; ++eye) { const ShownView& view = shown.views[eye]; - Check(!view.depth, "the runtime gets no depth in a bridged session's views"); - Check(view.next_type == kApplicationStructType, - "the rest of a view's chain reaches the runtime: " + std::to_string(view.next_type)); + const float expected = eye == 0 ? g_frame_depth : g_frame_depth * 0.5f; + Check(view.depth && view.depth_swapchain == depth.swapchain && + view.depth_slice == static_cast(eye), + "the runtime gets each view's depth in a bridged session"); + Check(view.after_depth == kApplicationStructType, + "the rest of a view's chain reaches the runtime after its depth: " + + std::to_string(view.after_depth)); + Check(view.depth_value && *view.depth_value == expected, + "depth frame " + std::to_string(frame) + " eye " + std::to_string(eye) + ": the runtime's depth " + + (view.depth_value ? std::to_string(*view.depth_value) : "unread") + ", rendered " + + std::to_string(expected)); Check(Near(view.texel, colors[eye]), "depth frame " + std::to_string(frame) + " eye " + std::to_string(eye) + ": the runtime shows " + (view.texel ? Text(*view.texel) : "nothing") + ", rendered " + Text(colors[eye])); } } + g_frame_depth = 0.5f; const StandInSwapchain* runtime = FindSwapchain(depth.swapchain); Check(runtime && runtime->acquires == kFrames && runtime->waits == kFrames && runtime->releases == kFrames, "the depth swapchain's acquires, waits and releases reach the runtime"); - Check(Contains(Peer::Log(), "keeps the game's depth to itself"), "the log tells depth is left out"); + Check(!Contains(Peer::Log(), "keeps the game's depth to itself"), "the log tells of no depth left out"); + Check(SummaryValue(Peer::Bridge().Summary(), "depthCopies") == static_cast(kFrames), + "a depth copy per frame: " + Peer::Bridge().Summary()); // CheckFrames' 10 frames, CheckRenderAfterRelease's 16 and // CheckAcquireAhead's two releases before these. Check(Contains(Peer::Bridge().Describe(left.swapchain), "copies=" + std::to_string(10 + 16 + 2 + kFrames)), @@ -2896,6 +3377,769 @@ void TestReleaseOnAnotherThread(Game& game) { EndCase(game); } +// 4a. Lock order. A game thread holds the device's lock +// (ID3D11Multithread::Enter, protection on) through its +// xrReleaseSwapchainImage of the next frame's image, while the frame's +// thread is in xrEndFrame, whose hand-over needs that lock (the bridge's +// depth copy and "written" signal on the game's context). Driven by events: +// the game thread enters the lock, the frame's thread ends its frame, and +// the game thread releases only once the bridge is about to wait for the +// lock (SetContextWaitHookForTest). Waiting for it while holding the layer's +// mutex (the hand-over's lock) deadlocks: the release waits for that mutex. +// The bridge waits for it before taking any of the layer's locks +// (StageReleases): the release goes through, the game thread lets the lock +// go, and the frame ends. A deadlock fails the case after a timeout (the +// process ends: its threads cannot be got back). +void TestContextHeldAcrossRelease(Game& game) { + const std::string name = "context-held-across-release"; + BeginCase(name, game); + ComPtr multithread; + Check(SUCCEEDED(game.context.As(&multithread)), name + ": the game's context has ID3D11Multithread"); + multithread->SetMultithreadProtected(TRUE); // a game entering the lock has protection on + App app = StartApp(); + SessionCreate create(game.device.Get(), app.system); + Check(app.api.create_session(app.api.instance, &create.info, &app.session) == XR_SUCCESS && Peer::Bridge().Active(), + name + ": a bridged session"); + BeginSession(app); + Target left = MakeTarget(app, ColorInfo(), "the left eye"); + Target right = MakeTarget(app, ColorInfo(), "the right eye"); + Target depth = MakeTarget(app, DepthInfo(2), "depth"); + Target next = MakeTarget(app, ColorInfo(), "the next frame's image"); + Check(!Peer::Bridge().Unmirrored(depth.swapchain), name + ": depth is mirrored"); + const std::array eyes{{{&left, 0}, {&right, 0}}}; + Check(Frame(app, game, {&left, &right}, {FrameColor(1, 0), FrameColor(1, 1)}, eyes, &depth) == XR_SUCCESS, + name + ": a first frame"); + + // Ends the case if `event` is not set in time: a deadlock. + const auto await = [&](HANDLE event, const char* what) { + if (WaitForSingleObject(event, 10000) != WAIT_OBJECT_0) { + std::cout << "FAILED: " << name << ": " << what + << " within 10 s: deadlocked (the frame's thread waits for the device's lock holding a lock " + "the game thread's release waits for)\n" + << std::flush; + TerminateProcess(GetCurrentProcess(), 1); + } + }; + const HANDLE entered = CreateEventW(nullptr, TRUE, FALSE, nullptr); + const HANDLE waiting = CreateEventW(nullptr, TRUE, FALSE, nullptr); + const HANDLE released = CreateEventW(nullptr, TRUE, FALSE, nullptr); + const HANDLE ended = CreateEventW(nullptr, TRUE, FALSE, nullptr); + std::atomic frame_thread{0}; + std::atomic hooked{false}; + Peer::Bridge().SetContextWaitHookForTest([&] { + if (GetCurrentThreadId() == frame_thread.load() && !hooked.exchange(true)) { + SetEvent(waiting); + } + }); + XrResult released_result = XR_ERROR_RUNTIME_FAILURE; + XrResult end_result = XR_ERROR_RUNTIME_FAILURE; + const std::size_t shown_before = ShownCount(); + std::thread game_thread; + std::thread frame; + // The frame rendered and released on this thread; then the game thread + // takes the lock, and the frame's thread ends the frame. + const XrResult framed = Frame( + app, game, {&left, &right}, {FrameColor(3, 0), FrameColor(3, 1)}, eyes, &depth, nullptr, false, {}, + [&](const XrFrameEndInfo& end) { + game_thread = std::thread([&] { + // The next frame's image, acquired and rendered before the lock. + const std::uint32_t index = Acquire(app, next); + Draw(game, next.images[index], FrameColor(2, 0)); + multithread->Enter(); + SetEvent(entered); + await(waiting, "the bridge did not come to the device's lock"); + XrSwapchainImageReleaseInfo release{XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO}; + released_result = app.api.release(next.swapchain, &release); + SetEvent(released); + multithread->Leave(); + }); + await(entered, "the game thread did not enter the device's lock"); + frame = std::thread([&] { + frame_thread = GetCurrentThreadId(); + end_result = app.api.end_frame(app.session, &end); + SetEvent(ended); + }); + await(released, "the game thread's xrReleaseSwapchainImage did not return"); + await(ended, "the frame's xrEndFrame did not return"); + game_thread.join(); + frame.join(); + return end_result; + }); + Check(framed == end_result, name + ": the frame's result"); + Peer::Bridge().SetContextWaitHookForTest(nullptr); + for (const HANDLE event : {entered, waiting, released, ended}) CloseHandle(event); + Check(hooked, name + ": the frame's thread came to the device's lock while the game thread held it"); + Check(released_result == XR_SUCCESS, name + ": the release " + std::to_string(released_result)); + Check(end_result == XR_SUCCESS, name + ": the frame " + std::to_string(end_result)); + const std::vector shown = Shown(shown_before); + Check(shown.size() == 1 && shown[0].views.size() == 2 && Near(shown[0].views[0].texel, FrameColor(3, 0)) && + Near(shown[0].views[1].texel, FrameColor(3, 1)) && shown[0].views[0].depth, + name + ": the frame is shown, with its depth"); + // And on: the next frame's image too. + Check(Frame(app, game, {&left, &right}, {FrameColor(4, 0), FrameColor(4, 1)}, eyes, &depth) == XR_SUCCESS, + name + ": a frame after"); + CheckValidation(game, name); + StopApp(app); + multithread->SetMultithreadProtected(FALSE); + Check(GameRuns(game), "the game's device works"); + EndCase(game); +} + +// 4c-4e. Lock order past the bridge's staging: the device's lock +// (ID3D11Multithread::Enter) before any of the layer's locks, wherever the +// layer uses the game's immediate context under them (GameContextLock). A +// game thread holds the device's lock through an OpenXR call that takes the +// layer's lock (`game_call`; xrLocateViews, as a render thread may), while +// `layer_call`, on a thread of its own, uses the game's context under the +// layer's locks. Driven by events: +// - `layer_call`'s thread comes to the layer's GameContextLock (its test +// hook): it holds none of the layer's locks yet; +// - there, the game thread enters the device's lock (and runs +// `while_entered`, a release landing at that moment), and the hook +// returns; +// - the game thread gives `layer_call` 200 ms (to take the layer's locks, +// would it use the context without the device's lock first), then makes +// `game_call` and lets the lock go. +// Both calls must return. A layer call that took its locks first and then +// waited for the device's lock deadlocks with `game_call`, which waits for +// those locks: each wait ends the process after 10 s (FAILED). `hold`: which +// of `layer_call`'s holds of the device's lock is the one (1: its first). +struct HeldContextRun { + bool hooked{false}; // `layer_call` came to the device's lock before its own locks + DWORD layer_thread{0}; + XrResult game_result{XR_ERROR_RUNTIME_FAILURE}; + XrResult layer_result{XR_ERROR_RUNTIME_FAILURE}; +}; + +HeldContextRun RunWithHeldContext(const std::string& name, ID3D11Multithread* multithread, + const std::function& layer_call, + const std::function& game_call, + const std::function& while_entered = {}, int hold = 1) { + const auto await = [&](HANDLE event, const char* what) { + if (WaitForSingleObject(event, 10000) != WAIT_OBJECT_0) { + std::cout << "FAILED: " << name << ": " << what + << " within 10 s: deadlocked (a call waits for the device's lock holding a lock of the " + "layer's that the game thread, holding the device's lock, waits for)\n" + << std::flush; + TerminateProcess(GetCurrentProcess(), 1); + } + }; + const HANDLE at_lock = CreateEventW(nullptr, TRUE, FALSE, nullptr); + const HANDLE entered = CreateEventW(nullptr, TRUE, FALSE, nullptr); + const HANDLE called = CreateEventW(nullptr, TRUE, FALSE, nullptr); + const HANDLE done = CreateEventW(nullptr, TRUE, FALSE, nullptr); + std::atomic layer_thread{0}; + std::atomic hooked{false}; + std::atomic holds{0}; + Peer::GameContext().SetWaitHookForTest([&] { + if (GetCurrentThreadId() != layer_thread.load() || ++holds != hold || hooked.exchange(true)) return; + SetEvent(at_lock); + await(entered, "the game thread did not enter the device's lock"); + }); + HeldContextRun run; + std::thread game_thread([&] { + await(at_lock, "the layer's call did not come to the device's lock ahead of its own locks"); + multithread->Enter(); + if (while_entered) while_entered(); + SetEvent(entered); + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + run.game_result = game_call(); + SetEvent(called); + multithread->Leave(); + }); + std::thread layer_thread_([&] { + layer_thread = GetCurrentThreadId(); + run.layer_result = layer_call(); + SetEvent(done); + }); + await(called, "the game thread's call did not return"); + await(done, "the layer's call did not return"); + game_thread.join(); + layer_thread_.join(); + Peer::GameContext().SetWaitHookForTest(nullptr); + for (const HANDLE event : {at_lock, entered, called, done}) CloseHandle(event); + run.hooked = hooked; + run.layer_thread = layer_thread; + return run; +} + +// The game's views at the runtime's time (xrLocateViews takes the layer's lock). +XrResult LocateViewsNow(App& app, XrViewConfigurationType configuration, std::uint32_t count) { + XrViewLocateInfo locate{XR_TYPE_VIEW_LOCATE_INFO}; + locate.viewConfigurationType = configuration; + { + std::scoped_lock lock(g_runtime_mutex); + locate.displayTime = g_runtime.time; + } + locate.space = app.space; + XrViewState state{XR_TYPE_VIEW_STATE}; + std::array views{}; + for (XrView& view : views) view.type = XR_TYPE_VIEW; + std::uint32_t located = 0; + return app.api.locate_views(app.session, &locate, &state, count, &located, views.data()); +} + +// The game's device's lock, with protection on (as a game entering it has). +ComPtr ProtectedContext(Game& game, const std::string& name) { + ComPtr multithread; + Check(SUCCEEDED(game.context.As(&multithread)), name + ": the game's context has ID3D11Multithread"); + multithread->SetMultithreadProtected(TRUE); + return multithread; +} + +// 4c. Quad views in a bridged session: the layer composes the four views on +// the game's context in xrEndFrame, into swapchains of its own whose acquire +// and release (the bridge's wait, and its "written" signal: a release +// StageReleases never stages) also use the game's context, all under the +// layer's locks. A game thread holds the device's lock through xrLocateViews +// meanwhile. +void TestQuadViewsUnderHeldContext(Game& game) { + const std::string name = "quad-views-context-held"; + CaseSettings settings; + settings.quadviews = true; + BeginCase(name, game, settings); + g_runtime.recommended_width = g_runtime.recommended_height = kQuadStereoSize; + g_runtime.paced = true; + ComPtr multithread = ProtectedContext(game, name); + App app = StartApp({kD3D11Extension, kQuadViewsExtension, kFoveatedExtension}); + std::uint32_t count = 0; + std::array configuration{}; + for (XrViewConfigurationView& view : configuration) view.type = XR_TYPE_VIEW_CONFIGURATION_VIEW; + Check(app.api.enumerate_views(app.api.instance, app.system, XR_VIEW_CONFIGURATION_TYPE_PRIMARY_QUAD_VARJO, 4, + &count, configuration.data()) == XR_SUCCESS && + count == 4, + name + ": four quad views"); + SessionCreate create(game.device.Get(), app.system); + Check(app.api.create_session(app.api.instance, &create.info, &app.session) == XR_SUCCESS && Peer::Bridge().Active(), + name + ": a bridged session"); + Check(!Peer::ToggleFrameGen(), name + ": Frame Generation toggled off (the layer composes)"); + BeginSession(app, XR_VIEW_CONFIGURATION_TYPE_PRIMARY_QUAD_VARJO); + QuadTargets targets; + for (std::size_t i = 0; i < targets.size(); ++i) { + targets[i] = MakeTarget(app, + ColorInfo(1, configuration[i].recommendedImageRectWidth, + configuration[i].recommendedImageRectHeight), + i < 2 ? "a peripheral view" : "a focus view"); + } + for (int frame = 0; frame < 3; ++frame) { + Check(QuadFrame(app, game, targets, frame) == XR_SUCCESS, name + ": a quad frame before"); + } + const std::uint64_t composed = Peer::QuadFrames().first; + const std::size_t shown_before = ShownCount(); + HeldContextRun run; + QuadFrameOptions options; + options.end_frame = [&](const XrFrameEndInfo& end) { + run = RunWithHeldContext( + name, multithread.Get(), [&] { return app.api.end_frame(app.session, &end); }, + [&] { return LocateViewsNow(app, XR_VIEW_CONFIGURATION_TYPE_PRIMARY_QUAD_VARJO, 4); }); + return run.layer_result; + }; + const XrResult framed = QuadFrame(app, game, targets, 3, options); + Check(framed == XR_SUCCESS, name + ": the frame " + std::to_string(framed)); + Check(run.hooked, name + ": xrEndFrame came to the device's lock before the layer's locks"); + Check(run.game_result == XR_SUCCESS, name + ": the game thread's xrLocateViews " + std::to_string(run.game_result)); + Check(Peer::QuadFrames().first == composed + 1, name + ": the frame was composed on Direct3D 11"); + const std::array outputs = Peer::CompositorOutputs(); + const std::vector shown = Shown(shown_before); + Check(shown.size() == 1 && shown[0].views.size() == 2 && shown[0].views[0].swapchain == outputs[0] && + shown[0].views[1].swapchain == outputs[1], + name + ": the runtime shows the composed frame"); + Check(QuadFrame(app, game, targets, 4) == XR_SUCCESS && Peer::QuadFrames().first == composed + 2, + name + ": a frame after"); + CheckValidation(game, name); + StopApp(app); + multithread->SetMultithreadProtected(FALSE); + Check(GameRuns(game), "the game's device works"); + EndCase(game); +} + +// 4d. A release landing between xrEndFrame's staging (StageReleases) and its +// locks: the hand-over runs its Direct3D 11 half under the layer's locks +// (Released). The game thread makes that release while holding the device's +// lock, then xrLocateViews. +void TestUnstagedReleaseUnderHeldContext(Game& game) { + const std::string name = "unstaged-release-context-held"; + BeginCase(name, game); + ComPtr multithread = ProtectedContext(game, name); + App app = StartApp(); + SessionCreate create(game.device.Get(), app.system); + Check(app.api.create_session(app.api.instance, &create.info, &app.session) == XR_SUCCESS && Peer::Bridge().Active(), + name + ": a bridged session"); + BeginSession(app); + Target left = MakeTarget(app, ColorInfo(), "the left eye"); + Target right = MakeTarget(app, ColorInfo(), "the right eye"); + Target late = MakeTarget(app, ColorInfo(), "an image released late"); + const std::array eyes{{{&left, 0}, {&right, 0}}}; + Check(Frame(app, game, {&left, &right}, {FrameColor(1, 0), FrameColor(1, 1)}, eyes) == XR_SUCCESS, + name + ": a first frame"); + // Acquired and rendered before; released by the game thread, holding the + // device's lock, once xrEndFrame's staging is done. + Draw(game, late.images[Acquire(app, late)], FrameColor(2, 0)); + // The bridge's waits for the context on the frame's thread after its + // staging: the late release's hand-over. + std::atomic frame_thread{0}; + std::atomic staged{false}; + std::atomic unstaged_holds{0}; + Peer::Bridge().SetContextWaitHookForTest([&] { + if (GetCurrentThreadId() == frame_thread.load() && staged.load()) ++unstaged_holds; + }); + HeldContextRun run; + XrResult late_release = XR_ERROR_RUNTIME_FAILURE; + const std::size_t shown_before = ShownCount(); + const XrResult framed = Frame( + app, game, {&left, &right}, {FrameColor(3, 0), FrameColor(3, 1)}, eyes, nullptr, nullptr, false, {}, + [&](const XrFrameEndInfo& end) { + run = RunWithHeldContext( + name, multithread.Get(), + [&] { + frame_thread = GetCurrentThreadId(); + return app.api.end_frame(app.session, &end); + }, + [&] { return LocateViewsNow(app, XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO, 2); }, + [&] { + staged = true; + XrSwapchainImageReleaseInfo release{XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO}; + late_release = app.api.release(late.swapchain, &release); + }); + return run.layer_result; + }); + Peer::Bridge().SetContextWaitHookForTest(nullptr); + Check(framed == XR_SUCCESS, name + ": the frame " + std::to_string(framed)); + Check(run.hooked, name + ": xrEndFrame came to the device's lock before the layer's locks"); + Check(late_release == XR_SUCCESS, name + ": the late release " + std::to_string(late_release)); + Check(run.game_result == XR_SUCCESS, name + ": the game thread's xrLocateViews " + std::to_string(run.game_result)); + Check(unstaged_holds.load() >= 1, name + ": the late release was handed over unstaged, in xrEndFrame"); + const std::vector shown = Shown(shown_before); + Check(shown.size() == 1 && shown[0].views.size() == 2 && Near(shown[0].views[0].texel, FrameColor(3, 0)) && + Near(shown[0].views[1].texel, FrameColor(3, 1)), + name + ": the frame is shown"); + Check(Frame(app, game, {&left, &right}, {FrameColor(4, 0), FrameColor(4, 1)}, eyes) == XR_SUCCESS, + name + ": a frame after"); + Render(app, game, late, FrameColor(5, 0)); + CheckValidation(game, name); + StopApp(app); + multithread->SetMultithreadProtected(FALSE); + Check(GameRuns(game), "the game's device works"); + EndCase(game); +} + +// 4e. The game's own Direct3D 11 session, Frame Generation engaged: it +// captures each released eye image on the game's context, under the +// layer's lock (and its own). The game releases the left eye on one thread +// while another holds the device's lock through xrLocateViews. +void TestNativeCaptureUnderHeldContext(Game& game) { + const std::string name = "native-capture-context-held"; + CaseSettings settings; + settings.frame_multiplier = "2x"; + BeginCase(name, game, settings); + SetEnv("VECTORXR_D3D11_BRIDGE", "off"); + g_runtime.paced = true; + Peer::SetFrameGenEngagement(2, std::chrono::milliseconds(0)); + ComPtr multithread = ProtectedContext(game, name); + App app = StartApp(); + SessionCreate create(game.device.Get(), app.system); + Check(app.api.create_session(app.api.instance, &create.info, &app.session) == XR_SUCCESS && + !Peer::Bridge().Active() && g_runtime.binding == XR_TYPE_GRAPHICS_BINDING_D3D11_KHR, + name + ": the game's own Direct3D 11 session"); + BeginSession(app); + Target left = MakeTarget(app, ColorInfo(), "the left eye"); + Target right = MakeTarget(app, ColorInfo(), "the right eye"); + const std::array eyes{{{&left, 0}, {&right, 0}}}; + int frame = 0; + do { + Check(Frame(app, game, {&left, &right}, {FrameColor(frame, 0), FrameColor(frame, 1)}, eyes) == XR_SUCCESS, + name + ": a frame before"); + } while (Peer::FrameGen().generated == 0 && ++frame < 96); + Check(Peer::FrameGen().generated > 0, name + ": Frame Generation generates frames"); + + // One frame, its left eye released on a thread of its own. + XrFrameWaitInfo wait_info{XR_TYPE_FRAME_WAIT_INFO}; + XrFrameState state{XR_TYPE_FRAME_STATE}; + Check(app.api.wait_frame(app.session, &wait_info, &state) == XR_SUCCESS, name + ": xrWaitFrame"); + XrFrameBeginInfo begin_info{XR_TYPE_FRAME_BEGIN_INFO}; + Check(app.api.begin_frame(app.session, &begin_info) == XR_SUCCESS, name + ": xrBeginFrame"); + XrViewLocateInfo locate{XR_TYPE_VIEW_LOCATE_INFO}; + locate.viewConfigurationType = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO; + locate.displayTime = state.predictedDisplayTime; + locate.space = app.space; + XrViewState view_state{XR_TYPE_VIEW_STATE}; + std::array located{}; + for (XrView& view : located) view.type = XR_TYPE_VIEW; + std::uint32_t located_count = 0; + Check(app.api.locate_views(app.session, &locate, &view_state, 2, &located_count, located.data()) == XR_SUCCESS, + name + ": xrLocateViews"); + const std::uint32_t left_index = Acquire(app, left); + const std::uint32_t right_index = Acquire(app, right); + Draw(game, left.images[left_index], FrameColor(100, 0)); + Draw(game, right.images[right_index], FrameColor(100, 1)); + Release(app, right); + g_capture_context = game.context.Get(); + const HeldContextRun run = RunWithHeldContext( + name, multithread.Get(), + [&] { + XrSwapchainImageReleaseInfo release{XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO}; + return app.api.release(left.swapchain, &release); + }, + [&] { return LocateViewsNow(app, XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO, 2); }); + g_capture_context = nullptr; + Check(run.hooked, name + ": the release came to the device's lock before the layer's lock"); + Check(run.layer_result == XR_SUCCESS, name + ": the release " + std::to_string(run.layer_result)); + Check(run.game_result == XR_SUCCESS, name + ": the game thread's xrLocateViews " + std::to_string(run.game_result)); + Check(g_capture_threads.Count(run.layer_thread) == 1, name + ": Frame Generation captured the released image"); + std::array views{}; + for (std::uint32_t eye = 0; eye < 2; ++eye) { + views[eye] = {XR_TYPE_COMPOSITION_LAYER_PROJECTION_VIEW}; + views[eye].pose = located[eye].pose; + views[eye].fov = located[eye].fov; + views[eye].subImage.swapchain = eyes[eye].target->swapchain; + views[eye].subImage.imageRect = {{0, 0}, + {static_cast(kWidth), static_cast(kHeight)}}; + } + XrCompositionLayerProjection projection{XR_TYPE_COMPOSITION_LAYER_PROJECTION}; + projection.space = app.space; + projection.viewCount = 2; + projection.views = views.data(); + const XrCompositionLayerBaseHeader* layers[] = {reinterpret_cast(&projection)}; + XrFrameEndInfo end{XR_TYPE_FRAME_END_INFO}; + end.displayTime = state.predictedDisplayTime; + end.environmentBlendMode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE; + end.layerCount = 1; + end.layers = layers; + Check(app.api.end_frame(app.session, &end) == XR_SUCCESS, name + ": xrEndFrame"); + Check(Frame(app, game, {&left, &right}, {FrameColor(101, 0), FrameColor(101, 1)}, eyes) == XR_SUCCESS, + name + ": a frame after"); + CheckValidation(game, name); + StopApp(app); + Peer::SetFrameGenEngagement(45, std::chrono::milliseconds(3000)); // the presenter's own + SetEnv("VECTORXR_D3D11_BRIDGE", ""); + multithread->SetMultithreadProtected(FALSE); + Check(GameRuns(game), "the game's device works"); + EndCase(game); +} + +// 4f. The game's own Direct3D 11 session, Frame Generation engaged: its +// xrEndFrame generates on the game's context (inline: no presenter thread on +// Direct3D 11), holding Frame Generation's GPU lock, which its captures at a +// release take as well (under the layer's lock). A game thread holds the +// device's lock through a release of the next frame's left eye meanwhile. +void TestNativeGenerateUnderHeldContext(Game& game) { + const std::string name = "native-generate-context-held"; + CaseSettings settings; + settings.frame_multiplier = "2x"; + BeginCase(name, game, settings); + SetEnv("VECTORXR_D3D11_BRIDGE", "off"); + g_runtime.paced = true; + Peer::SetFrameGenEngagement(2, std::chrono::milliseconds(0)); + ComPtr multithread = ProtectedContext(game, name); + App app = StartApp(); + SessionCreate create(game.device.Get(), app.system); + Check(app.api.create_session(app.api.instance, &create.info, &app.session) == XR_SUCCESS && + !Peer::Bridge().Active() && g_runtime.binding == XR_TYPE_GRAPHICS_BINDING_D3D11_KHR, + name + ": the game's own Direct3D 11 session"); + BeginSession(app); + Target left = MakeTarget(app, ColorInfo(), "the left eye"); + Target right = MakeTarget(app, ColorInfo(), "the right eye"); + const std::array eyes{{{&left, 0}, {&right, 0}}}; + int frame = 0; + do { + Check(Frame(app, game, {&left, &right}, {FrameColor(frame, 0), FrameColor(frame, 1)}, eyes) == XR_SUCCESS, + name + ": a frame before"); + } while (Peer::FrameGen().generated == 0 && ++frame < 96); + Check(Peer::FrameGen().generated > 0, name + ": Frame Generation generates frames"); + const std::uint64_t generated = Peer::FrameGen().generated; + HeldContextRun run; + XrResult released = XR_ERROR_RUNTIME_FAILURE; + const XrResult framed = Frame( + app, game, {&left, &right}, {FrameColor(200, 0), FrameColor(200, 1)}, eyes, nullptr, nullptr, false, {}, + [&](const XrFrameEndInfo& end) { + // The next frame's left eye, acquired and rendered ahead. + const std::uint32_t next = Acquire(app, left); + Draw(game, left.images[next], FrameColor(201, 0)); + g_capture_context = game.context.Get(); + // xrEndFrame's own hold first (for the layer's locks), then Frame + // Generation's, before its GPU lock. + run = RunWithHeldContext( + name, multithread.Get(), [&] { return app.api.end_frame(app.session, &end); }, + [&] { + XrSwapchainImageReleaseInfo release{XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO}; + released = app.api.release(left.swapchain, &release); + return released; + }, + {}, 2); + g_capture_context = nullptr; + return run.layer_result; + }); + Check(framed == XR_SUCCESS, name + ": the frame " + std::to_string(framed)); + Check(run.hooked, name + ": Frame Generation came to the device's lock before its GPU lock"); + Check(released == XR_SUCCESS, name + ": the game thread's release " + std::to_string(released)); + Check(g_execute_threads.Count(run.layer_thread) >= 1 && Peer::FrameGen().generated > generated, + name + ": the frame's xrEndFrame generated frames"); + // The next frame: its right eye, and an end without layers. + XrFrameWaitInfo wait_info{XR_TYPE_FRAME_WAIT_INFO}; + XrFrameState state{XR_TYPE_FRAME_STATE}; + Check(app.api.wait_frame(app.session, &wait_info, &state) == XR_SUCCESS, name + ": xrWaitFrame after"); + XrFrameBeginInfo begin_info{XR_TYPE_FRAME_BEGIN_INFO}; + Check(app.api.begin_frame(app.session, &begin_info) == XR_SUCCESS, name + ": xrBeginFrame after"); + Render(app, game, right, FrameColor(201, 1)); + XrFrameEndInfo empty{XR_TYPE_FRAME_END_INFO}; + empty.displayTime = state.predictedDisplayTime; + empty.environmentBlendMode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE; + Check(app.api.end_frame(app.session, &empty) == XR_SUCCESS, name + ": an empty frame after"); + Check(Frame(app, game, {&left, &right}, {FrameColor(202, 0), FrameColor(202, 1)}, eyes) == XR_SUCCESS, + name + ": a frame after"); + CheckValidation(game, name); + StopApp(app); + Peer::SetFrameGenEngagement(45, std::chrono::milliseconds(3000)); // the presenter's own + SetEnv("VECTORXR_D3D11_BRIDGE", ""); + multithread->SetMultithreadProtected(FALSE); + Check(GameRuns(game), "the game's device works"); + EndCase(game); +} + +// 4b. VECTORXR_D3D11_BRIDGE_DEPTH=private, or Frame Generation's Use game +// depth off (by_profile): depth stays the game's own, as before the bridge +// mirrored it. Its swapchain's calls reach the runtime; its depth never +// reaches submitted frames, the rest of each view's chain does, and the game's +// multithread protection is left as it was. +void TestPrivateDepthSession(Game& game, bool by_profile) { + CaseSettings case_settings; + case_settings.use_game_depth = !by_profile; + BeginCase(by_profile ? "private-depth-profile" : "private-depth", game, case_settings); + const std::string switch_name = + by_profile ? "Frame Generation's Use game depth is off" : "VECTORXR_D3D11_BRIDGE_DEPTH=private"; + if (!by_profile) SetEnv("VECTORXR_D3D11_BRIDGE_DEPTH", "private"); + App app = StartApp(); + SessionCreate create(game.device.Get(), app.system); + Check(app.api.create_session(app.api.instance, &create.info, &app.session) == XR_SUCCESS && Peer::Bridge().Active(), + "private depth: a bridged session"); + BeginSession(app); + Target left = MakeTarget(app, ColorInfo(), "the left eye"); + Target right = MakeTarget(app, ColorInfo(), "the right eye"); + Target depth = MakeTarget(app, DepthInfo(2), "depth"); + Check(Peer::Bridge().Bridged(depth.swapchain) && Peer::Bridge().Unmirrored(depth.swapchain) && + Contains(Peer::Bridge().Describe(depth.swapchain), switch_name), + "the depth swapchain is the game's own: " + Peer::Bridge().Describe(depth.swapchain)); + if (by_profile) { + ComPtr multithread; + Check(SUCCEEDED(game.context.As(&multithread)) && !multithread->GetMultithreadProtected(), + "private depth (profile): the game's multithread protection stays off"); + Check(!Contains(Peer::Log(), "stays the game's own (its depth is left out of submitted frames)"), + "private depth (profile): no error line per depth swapchain for the user's own choice"); + } + constexpr int kFrames = 6; + for (int frame = 0; frame < kFrames; ++frame) { + const std::vector colors{FrameColor(frame + 30, 0), FrameColor(frame + 30, 1)}; + const XrResult result = Frame(app, game, {&left, &right}, colors, {{{&left, 0}, {&right, 0}}}, &depth, + nullptr, true); // explicit unknown metadata: preserve it in pass-through + Check(result == XR_SUCCESS, "private depth frame " + std::to_string(frame) + ": xrEndFrame " + + std::to_string(result)); + const ShownFrame& shown = g_runtime.frames.back(); + Check(shown.views.size() == 2, "private depth frame: two views"); + for (int eye = 0; eye < 2; ++eye) { + const ShownView& view = shown.views[eye]; + Check(!view.depth, "the runtime gets no private depth in a bridged session's views"); + Check(view.next_type == kApplicationStructType, + "the rest of a view's chain reaches the runtime: " + std::to_string(view.next_type)); + Check(Near(view.texel, colors[eye]), "private depth frame " + std::to_string(frame) + " eye " + + std::to_string(eye) + ": the runtime shows " + + (view.texel ? Text(*view.texel) : "nothing")); + } + } + { + std::scoped_lock lock(g_runtime_mutex); + const StandInSwapchain* runtime = FindSwapchain(depth.swapchain); + Check(runtime && runtime->acquires == kFrames && runtime->waits == kFrames && runtime->releases == kFrames, + "the private depth swapchain's acquires, waits and releases reach the runtime"); + } + Check(Contains(Peer::Log(), "keeps the game's depth to itself"), "the log tells depth is left out"); + Check(SummaryValue(Peer::Bridge().Summary(), "depthCopies") == 0, "no depth copies: " + Peer::Bridge().Summary()); + CheckValidation(game, "private depth"); + StopApp(app); + if (!by_profile) SetEnv("VECTORXR_D3D11_BRIDGE_DEPTH", ""); + EndCase(game); +} + +// 1d. Depth sub-images a runtime refuses: mirrored depth reaches the runtime, +// and PimaxXR and Virtual Desktop fail the whole xrEndFrame for a depth rect +// outside its image (as the stand-in does, SubImageRefusal). UEVR places its +// depth rect by its colour image's size, which an upscaler or screen +// percentage makes larger than its depth image. Such depth is left out of +// every view of its projection (never one eye's alone), never clamped, and +// the frame's colour and the rest of the views' chains still reach the +// runtime; valid depth, at a nonzero origin, side by side or on either slice, +// reaches it as before. Standing by (each frame passed through), and with +// Frame Generation engaged (frames presented from its copies of the game's, +// and the game's depth no longer sent to Frame Generation either). +// `bridged` false: the game's own Direct3D 11 session (VECTORXR_D3D11_BRIDGE=off), +// whose depth the layer forwards as it is: the same depth is left out. +void TestDepthRects(Game& game, bool engaged, bool bridged = true) { + CaseSettings settings; + if (engaged) settings.frame_multiplier = "2x"; + const std::string name = + std::string(engaged ? "depth-rects-engaged" : "depth-rects") + (bridged ? "" : "-native-d3d11"); + BeginCase(name, game, settings); + if (!bridged) SetEnv("VECTORXR_D3D11_BRIDGE", "off"); + if (engaged) { + g_runtime.paced = true; + Peer::SetFrameGenEngagement(2, std::chrono::milliseconds(0)); + } + App app = StartApp(); + SessionCreate create(game.device.Get(), app.system); + Check(app.api.create_session(app.api.instance, &create.info, &app.session) == XR_SUCCESS && + Peer::Bridge().Active() == bridged && + g_runtime.binding == (bridged ? XR_TYPE_GRAPHICS_BINDING_D3D12_KHR : XR_TYPE_GRAPHICS_BINDING_D3D11_KHR), + name + (bridged ? ": a bridged session" : ": the game's own Direct3D 11 session")); + BeginSession(app); + Target left = MakeTarget(app, ColorInfo(), "the left eye"); + Target right = MakeTarget(app, ColorInfo(), "the right eye"); + const auto depth_target = [&](std::uint32_t width, std::uint32_t height, const char* what) { + XrSwapchainCreateInfo info = DepthInfo(2); + info.width = width; + info.height = height; + Target target = MakeTarget(app, info, what); + Check(!Peer::Bridge().Unmirrored(target.swapchain), std::string(what) + ": mirrored"); + return target; + }; + // The colour images are 64x48. An upscaler's smaller depth, a larger + // one, and one image for both eyes side by side. + Target smaller = depth_target(48, 36, "smaller depth"); + Target larger = depth_target(80, 60, "larger depth"); + Target side_by_side = depth_target(2 * kWidth, kHeight, "side-by-side depth"); + constexpr std::int32_t kMax = std::numeric_limits::max(); + struct Case { + const char* what; + const Target* depth; + std::array rects; + std::array slices; + bool kept; + }; + const XrRect2Di colour_rect{{0, 0}, {static_cast(kWidth), static_cast(kHeight)}}; + const std::vector cases{ + {"valid, both slices", &smaller, {{{{0, 0}, {48, 36}}, {{0, 0}, {48, 36}}}}, {0, 1}, true}, + {"the colour's rect on a smaller depth image (UEVR)", &smaller, {colour_rect, colour_rect}, {0, 1}, false}, + {"one texel too wide, left eye only", &smaller, {{{{0, 0}, {49, 36}}, {{0, 0}, {48, 36}}}}, {0, 1}, false}, + {"one texel too high, right eye only", &smaller, {{{{0, 0}, {48, 36}}, {{0, 0}, {48, 37}}}}, {0, 1}, false}, + {"valid at a nonzero origin on a larger image", &larger, {{{{8, 6}, {64, 48}}, {{16, 12}, {64, 48}}}}, {0, 1}, + true}, + {"one texel past the larger image's bottom", &larger, {{{{8, 13}, {64, 48}}, {{8, 12}, {64, 48}}}}, {0, 1}, + false}, + {"side by side, one slice", &side_by_side, {{colour_rect, {{static_cast(kWidth), 0}, colour_rect.extent}}}, + {0, 0}, true}, + {"side by side, the right eye one texel past", + &side_by_side, + {{colour_rect, {{static_cast(kWidth) + 1, 0}, colour_rect.extent}}}, + {0, 0}, + false}, + {"a negative offset", &smaller, {{{{-1, 0}, {48, 36}}, {{0, 0}, {48, 36}}}}, {0, 1}, false}, + {"a zero extent", &smaller, {{{{0, 0}, {0, 36}}, {{0, 0}, {48, 36}}}}, {0, 1}, false}, + {"a negative extent", &smaller, {{{{0, 0}, {48, 36}}, {{0, 0}, {48, -36}}}}, {0, 1}, false}, + {"a slice past the image", &smaller, {{{{0, 0}, {48, 36}}, {{0, 0}, {48, 36}}}}, {0, 2}, false}, + {"an offset whose far edge overflows", &smaller, {{{{kMax, 0}, {1, 36}}, {{0, 0}, {48, 36}}}}, {0, 1}, false}, + {"an extent whose far edge overflows", &smaller, {{{{0, 0}, {48, 36}}, {{1, 1}, {kMax, kMax}}}}, {0, 1}, false}, + {"valid again", &smaller, {{{{0, 0}, {48, 36}}, {{0, 0}, {48, 36}}}}, {0, 1}, true}, + }; + + // Why the runtime refused its first frame, if it did (its thread may be Frame Generation's). + const auto refusal = [] { + std::scoped_lock lock(g_runtime_mutex); + return std::to_string(g_runtime.refused_frames) + " refused" + + (g_runtime.refusal.empty() ? "" : ", the first: " + g_runtime.refusal); + }; + int frame_index = 0; + const auto run = [&](const Case& test, int frames) { + for (int frame = 0; frame < frames; ++frame) { + const std::string at = name + ": " + test.what + ", frame " + std::to_string(frame); + const std::vector colors{FrameColor(frame_index + 50, 0), FrameColor(frame_index + 50, 1)}; + ++frame_index; + const std::size_t before = ShownCount(); + const XrResult result = + Frame(app, game, {&left, &right}, colors, {{{&left, 0}, {&right, 0}}}, test.depth, nullptr, !engaged, + [&](std::array& depth) { + for (int eye = 0; eye < 2; ++eye) { + depth[eye].subImage.imageRect = test.rects[eye]; + depth[eye].subImage.imageArrayIndex = test.slices[eye]; + } + }); + Check(result == XR_SUCCESS, at + ": xrEndFrame " + std::to_string(result) + " (" + refusal() + ")"); + if (engaged) continue; // shown from Frame Generation's thread, checked below + const std::vector shown = Shown(before); + Check(shown.size() == 1 && shown[0].views.size() == 2, at + ": one frame of two views shown"); + for (int eye = 0; eye < 2; ++eye) { + const ShownView& view = shown[0].views[eye]; + Check(view.depth == test.kept, at + ": eye " + std::to_string(eye) + (test.kept ? " has" : " has no") + + " depth"); + Check(view.next_type == (test.kept ? XR_TYPE_COMPOSITION_LAYER_DEPTH_INFO_KHR : kApplicationStructType) && + (!test.kept || view.after_depth == kApplicationStructType), + at + ": the rest of the view's chain reaches the runtime"); + Check(!test.kept || view.depth_swapchain == test.depth->swapchain, + at + ": the view's depth is the game's"); + Check(Near(view.texel, colors[eye]), at + ": eye " + std::to_string(eye) + " shows " + + (view.texel ? Text(*view.texel) : "nothing") + ", rendered " + + Text(colors[eye])); + } + } + }; + + if (!engaged) { + for (const Case& test : cases) run(test, 2); + } else { + // Engaged on valid depth first. + int frames = 0; + do { + run(cases.front(), 1); + } while (Peer::FrameGen().generated == 0 && ++frames < 96); + Check(Peer::FrameGen().generated > 0, name + ": Frame Generation generates frames"); + // Every refused case, each for a while: once the frames before them + // are shown, nothing the runtime gets carries depth, and Frame + // Generation is sent none. + for (std::size_t i = 1; i + 1 < cases.size(); ++i) { + if (cases[i].kept) continue; + run(cases[i], 4); + const std::size_t from = ShownCount(); + const std::uint64_t generated = Peer::FrameGen().generated; + run(cases[i], 6); + const std::vector shown = Shown(from); + Check(!shown.empty(), name + ": " + cases[i].what + ": frames shown"); + for (const ShownFrame& frame : shown) { + for (const ShownView& view : frame.views) { + Check(!view.depth, name + ": " + cases[i].what + ": no frame shown carries the refused depth"); + } + } + Check(Peer::FrameGen().generated > generated, + name + ": " + cases[i].what + ": Frame Generation keeps generating"); + Check(Peer::FrameGen().depth == depthxr::FrameGenDepthState::kNotSent, + name + ": " + cases[i].what + ": Frame Generation is sent no depth (" + + depthxr::ToString(Peer::FrameGen().depth) + ")"); + } + // Valid depth again reaches the runtime with the game's frames. + run(cases.back(), 4); + const std::size_t from = ShownCount(); + run(cases.back(), 8); + bool depth_shown = false; + for (const ShownFrame& frame : Shown(from)) { + for (const ShownView& view : frame.views) { + depth_shown = depth_shown || (view.depth && view.depth_swapchain == smaller.swapchain); + } + } + Check(depth_shown, name + ": valid depth reaches the runtime again"); + Check(Peer::FrameGen().depth != depthxr::FrameGenDepthState::kNotSent, + name + ": Frame Generation is sent depth again (" + depthxr::ToString(Peer::FrameGen().depth) + ")"); + } + Check(refusal() == "0 refused", name + ": the runtime refused no frame (" + refusal() + ")"); + const std::string log = Peer::Log(); + Check(Count(log, "The game's depth sub-image lies outside its depth image") == 1, + name + ": the log tells once of depth outside its image"); + Check(!Contains(log, "keeps the game's depth to itself"), name + ": no depth swapchain is private"); + CheckValidation(game, name); + StopApp(app); + if (engaged) { + Peer::SetFrameGenEngagement(45, std::chrono::milliseconds(3000)); // the presenter's own + } + if (!bridged) SetEnv("VECTORXR_D3D11_BRIDGE", ""); + Check(GameRuns(game), "the game's device works"); + EndCase(game); +} + // 1a. VECTORXR_FRAMEGEN_QUEUE=game: the runtime would get the bridge's queue, // so the session stays Direct3D 11, asking for its requirements again. void TestKeepGameQueue(Game& game) { @@ -4239,8 +5483,9 @@ Color QuadLayerColor(int release) { } // Each eye's view of the side-by-side session, and the quad layer's swapchain. -constexpr std::uint32_t kSideBySideEyeWidth = 128; -constexpr std::uint32_t kSideBySideEyeHeight = 96; +// BRIDGE_TEST_EYE=WxH sizes the eyes for the GPU-bound cases. +std::uint32_t g_side_by_side_eye_width = 128; +std::uint32_t g_side_by_side_eye_height = 96; constexpr std::uint32_t kQuadLayerSize = 128; // How long the game's GPU work of a loaded frame takes (three refreshes). constexpr DWORD kGameLoadMs = 30; @@ -4380,8 +5625,25 @@ struct SideBySideFrameOptions { bool quad_layer{true}; // the UI rendered, released and submitted as a quad layer after the projection bool depth{false}; // depth rendered and chained to each view, its half GameLoad* load{nullptr}; // the game's work held back from before the eyes' rendering + std::function before_render; // the game's own CPU and GPU work, before the eyes' rendering bool unknown_after_depth{false}; // separate from the supported depth-only generation case + // With quad_layer: the quad's image is acquired, waited for, drawn and + // released after the eyes' release, right before xrEndFrame, as + // OpenKneeboard above VectorXR does inside the game's xrEndFrame + // (OpenXRKneeboard.cpp), not at the frame's start. + bool kneeboard{false}; +}; + +// The game's time in its calls on the side-by-side session's swapchains +// (acquire with its wait, release), per kind, milliseconds since cleared. +struct CallTimes { + double eyes_acquire{0}, eyes_release{0}, ui_acquire{0}, ui_release{0}, end{0}, wait{0}, max_frame{0}; + int frames{0}; }; +CallTimes g_call_times; +double MsSince(std::chrono::steady_clock::time_point since) { + return std::chrono::duration(std::chrono::steady_clock::now() - since).count(); +} // One frame of the X-Plane-shaped game: both eyes rendered into their halves // of one image and released, depth likewise, then the UI rendered and @@ -4393,7 +5655,9 @@ void SideBySideFrame(SideBySideSession& session, Game& game, const SideBySideFra const std::string at = "side-by-side frame " + std::to_string(frame); XrFrameWaitInfo wait_info{XR_TYPE_FRAME_WAIT_INFO}; XrFrameState state{XR_TYPE_FRAME_STATE}; + const auto frame_start = std::chrono::steady_clock::now(); Check(app.api.wait_frame(app.session, &wait_info, &state) == XR_SUCCESS, at + ": xrWaitFrame"); + g_call_times.wait += MsSince(frame_start); XrFrameBeginInfo begin_info{XR_TYPE_FRAME_BEGIN_INFO}; Check(app.api.begin_frame(app.session, &begin_info) == XR_SUCCESS, at + ": xrBeginFrame"); XrViewLocateInfo locate{XR_TYPE_VIEW_LOCATE_INFO}; @@ -4408,32 +5672,49 @@ void SideBySideFrame(SideBySideSession& session, Game& game, const SideBySideFra located_count == 2, at + ": xrLocateViews"); + auto timed = std::chrono::steady_clock::now(); const std::uint32_t eyes_index = Acquire(app, session.eyes); + g_call_times.eyes_acquire += MsSince(timed); const std::uint32_t depth_index = options.depth ? Acquire(app, session.depth) : 0; - const std::uint32_t ui_index = options.quad_layer ? Acquire(app, session.ui) : 0; + timed = std::chrono::steady_clock::now(); + std::uint32_t ui_index = options.quad_layer && !options.kneeboard ? Acquire(app, session.ui) : 0; + g_call_times.ui_acquire += MsSince(timed); + ++g_call_times.frames; if (options.load) { options.load->HoldGame(game); } + if (options.before_render) { + options.before_render(); + } ClearHalves(game, session.eyes.images[eyes_index], {SideBySideColor(frame, 0), SideBySideColor(frame, 1)}); if (options.depth) { ClearDepth(game, session.depth.images[depth_index], 0); } + timed = std::chrono::steady_clock::now(); Release(app, session.eyes); + g_call_times.eyes_release += MsSince(timed); if (options.depth) { Release(app, session.depth); } + if (options.quad_layer && options.kneeboard) { + timed = std::chrono::steady_clock::now(); + ui_index = Acquire(app, session.ui); + g_call_times.ui_acquire += MsSince(timed); + } if (options.quad_layer) { Draw(game, session.ui.images[ui_index], QuadLayerColor(session.ui_releases++)); + timed = std::chrono::steady_clock::now(); Release(app, session.ui); + g_call_times.ui_release += MsSince(timed); } XrBaseInStructure after_depth{kApplicationStructType}; std::array depth_infos{}; std::array views{}; for (std::uint32_t eye = 0; eye < 2; ++eye) { - const XrRect2Di half{{static_cast(eye * kSideBySideEyeWidth), 0}, - {static_cast(kSideBySideEyeWidth), - static_cast(kSideBySideEyeHeight)}}; + const XrRect2Di half{{static_cast(eye * g_side_by_side_eye_width), 0}, + {static_cast(g_side_by_side_eye_width), + static_cast(g_side_by_side_eye_height)}}; views[eye] = {XR_TYPE_COMPOSITION_LAYER_PROJECTION_VIEW}; views[eye].pose = located[eye].pose; views[eye].fov = located[eye].fov; @@ -4470,7 +5751,10 @@ void SideBySideFrame(SideBySideSession& session, Game& game, const SideBySideFra end.environmentBlendMode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE; end.layerCount = options.quad_layer ? 2 : 1; end.layers = layers; + timed = std::chrono::steady_clock::now(); const XrResult result = app.api.end_frame(app.session, &end); + g_call_times.end += MsSince(timed); + g_call_times.max_frame = std::max(g_call_times.max_frame, MsSince(frame_start)); Check(result == XR_SUCCESS, at + ": xrEndFrame " + std::to_string(result)); if (options.quad_layer) { // A bridged release reaches the runtime at the frame's end. @@ -4520,18 +5804,19 @@ std::string Spread(std::vector values) { // otherwise, for what that costs): the quad layer's // releases are kept lazy instead, so under the same load no frame of Frame // Generation's waits for the game's work on the runtime's queue (the -// game's next wait on the quad's swapchain waits for its release on the -// CPU), and every refresh shows the quad's newest finished release, the -// game's latest or the one before it; the runtime still sees every call on -// it in order. +// game's next wait on the quad's swapchain waits on the CPU for its oldest +// held-back release, only when the image it waits for is the newest the +// runtime has), and every refresh shows the quad's newest finished release, +// at most two behind the game's latest (with its three images); the runtime +// still sees every call on it in order. void TestSideBySideFrameGen(Game& game, const std::string& mode, bool shown_lazy = false) { CaseSettings settings; settings.frame_multiplier = "2x"; settings.synthesis_mode = mode; BeginCase("side-by-side-framegen-" + mode + (shown_lazy ? "-shown-lazy" : ""), game, settings); SetEnv("VECTORXR_FRAMEGEN_SHOWN_LAYERS", shown_lazy ? "" : "off"); - g_runtime.recommended_width = kSideBySideEyeWidth; - g_runtime.recommended_height = kSideBySideEyeHeight; + g_runtime.recommended_width = g_side_by_side_eye_width; + g_runtime.recommended_height = g_side_by_side_eye_height; g_runtime.paced = true; { std::scoped_lock lock(g_runtime_mutex); @@ -4556,25 +5841,25 @@ void TestSideBySideFrameGen(Game& game, const std::string& mode, bool shown_lazy std::string("the log tells once that shown layers are kept lazy, only when they are (") + (shown_lazy ? "on" : "off") + ")"); BeginSession(app); - session.eyes = MakeTarget(app, XPlaneInfo(2 * kSideBySideEyeWidth, kSideBySideEyeHeight), "both eyes side by side"); + session.eyes = MakeTarget(app, XPlaneInfo(2 * g_side_by_side_eye_width, g_side_by_side_eye_height), "both eyes side by side"); session.ui = MakeTarget(app, XPlaneInfo(kQuadLayerSize, kQuadLayerSize), "the quad layer's"); XrSwapchainCreateInfo depth_info = DepthInfo(1); - depth_info.width = 2 * kSideBySideEyeWidth; - depth_info.height = kSideBySideEyeHeight; + depth_info.width = 2 * g_side_by_side_eye_width; + depth_info.height = g_side_by_side_eye_height; session.depth = MakeTarget(app, depth_info, "both eyes' depth side by side"); { std::scoped_lock lock(g_runtime_mutex); const StandInSwapchain* eyes = FindSwapchain(session.eyes.swapchain); const StandInSwapchain* ui = FindSwapchain(session.ui.swapchain); - Check(eyes && ui && eyes->info.width == 2 * kSideBySideEyeWidth && eyes->info.format == 29 && + Check(eyes && ui && eyes->info.width == 2 * g_side_by_side_eye_width && eyes->info.format == 29 && (eyes->info.usageFlags & XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT) && (eyes->info.usageFlags & XR_SWAPCHAIN_USAGE_TRANSFER_DST_BIT) && ui->info.width == kQuadLayerSize && (ui->info.usageFlags & XR_SWAPCHAIN_USAGE_TRANSFER_DST_BIT), "the runtime's swapchains are the bridge's copy destinations, whatever usage the game asked for"); } Check(Peer::Bridge().Bridged(session.eyes.swapchain) && Peer::Bridge().Bridged(session.ui.swapchain) && - Peer::Bridge().Unmirrored(session.depth.swapchain), - "both colour swapchains are bridged, depth is the game's own"); + Peer::Bridge().Bridged(session.depth.swapchain) && !Peer::Bridge().Unmirrored(session.depth.swapchain), + "both colour swapchains and depth are bridged, depth mirrored"); // What the stand-in saw of each frame: its layers, and the runtime's // queue reaching it (with the quad layer read there). @@ -4656,8 +5941,8 @@ void TestSideBySideFrameGen(Game& game, const std::string& mode, bool shown_lazy // layer after the projection (`quad_layer`) showing the game's latest // release; Frame Generation's outputs, or the game's image, each eye // its own half. How many showed Frame Generation's outputs. - constexpr auto kEyeWidth = static_cast(kSideBySideEyeWidth); - constexpr auto kEyeHeight = static_cast(kSideBySideEyeHeight); + static const auto kEyeWidth = static_cast(g_side_by_side_eye_width); + static const auto kEyeHeight = static_cast(g_side_by_side_eye_height); // Refreshes whose quad layer showed the game's release before its latest. std::size_t quad_behind = 0; const auto check_shown = [&](std::size_t from, const std::string& at, bool quad_layer) { @@ -4678,10 +5963,13 @@ void TestSideBySideFrameGen(Game& game, const std::string& mode, bool shown_lazy where + ": the quad layer shows " + (extra.quad_texel ? Text(*extra.quad_texel) : "nothing") + ", the game's latest release (" + std::to_string(extra.quad_releases) + ") rendered " + Text(expected)); - // Lazy, the newest release whose rendering is done: the - // game's latest, or the one before it. + // Lazy, the newest release whose rendering is done: at most + // two behind the game's latest (the game's wait on the quad's + // swapchain waits for the oldest of them only when the image + // it waits for is the runtime's newest, so a GPU-bound game's + // CPU stays a frame ahead of its GPU). const int behind = extra.game_quad_releases - extra.quad_releases; - Check(behind <= (shown_lazy ? 1 : 0), + Check(behind <= (shown_lazy ? 2 : 0), where + ": the runtime had the game's releases of the quad layer's swapchain but " + std::to_string(behind) + " (" + std::to_string(extra.quad_releases) + " of " + std::to_string(extra.game_quad_releases) + ")"); @@ -4791,7 +6079,7 @@ void TestSideBySideFrameGen(Game& game, const std::string& mode, bool shown_lazy std::uint64_t moved{0}; // frames Frame Generation generated meanwhile std::uint64_t repeated{0}; // refreshes it repeated the frame before at depthxr::FrameGenQueueSubstitution::ShownLayerCounts shown; // the quad's, meanwhile - std::size_t behind{0}; // refreshes whose quad layer showed the release before the game's latest + std::size_t behind{0}; // refreshes whose quad layer showed a release before the game's latest double game_ms{0.0}; // the game's 16 frames took }; const auto loaded = [&](bool quad_layer, const std::string& at) { @@ -4869,14 +6157,15 @@ void TestSideBySideFrameGen(Game& game, const std::string& mode, bool shown_lazy // The quad layer's releases kept lazy: no frame of Frame // Generation's waits for the game's work, with the quad layer or // without it. Each of the game's releases of the quad (one a frame) - // was held back, its next wait waiting for it on the CPU, on the - // runtime's queue only past that wait's bound (far beyond the game's + // was held back, a later wait waiting for it on the CPU (when the + // image waited for was the runtime's newest), on the runtime's + // queue only past that wait's bound (far beyond the game's // held-back work). const auto counts = [](const Backlog& backlog) { return std::to_string(backlog.shown.releases) + " lazy releases, " + std::to_string(backlog.shown.cpu_waits) + " waited for on the CPU, " + std::to_string(backlog.shown.gpu_waits) + " on the runtime's queue; " + - std::to_string(backlog.behind) + " refreshes showed the release before the game's latest"; + std::to_string(backlog.behind) + " refreshes showed a release before the game's latest"; }; std::cout << g_case << ": shown layers kept lazy: the game's GPU work held back " << kGameLoadMs << " ms a frame, the runtime's queue waited for it before frames of Frame Generation's ended " @@ -4888,10 +6177,12 @@ void TestSideBySideFrameGen(Game& game, const std::string& mode, bool shown_lazy describe(with_quad)); // A release whose work outlasts the CPU wait's bound (100 ms: a busy // or software GPU) goes on the runtime's queue instead, as designed; - // one of six local runs saw one (2026-10-14). - Check(with_quad.shown.releases == 16 && with_quad.shown.cpu_waits > 0 && with_quad.shown.gpu_waits <= 2, + // one of six local runs saw one (2026-10-14). CPU waits may be + // none: a wait for an image the runtime does not show is held + // back behind the releases instead (FrameGenQueueSubstitution::Wait). + Check(with_quad.shown.releases == 16 && with_quad.shown.gpu_waits <= 2, "GPU-bound, with the quad layer kept lazy: its releases are held back until their work has run, " - "the game's next wait waiting for them on the CPU: " + + "the game's waits held back behind them or waiting for them on the CPU: " + counts(with_quad)); Check(without_quad.during > 0 && without_quad.waited == 0 && without_quad.shown.releases == 0, "GPU-bound, without the quad layer: no frame of Frame Generation's waits for the game's work: " + @@ -4931,7 +6222,7 @@ void TestSideBySideFrameGen(Game& game, const std::string& mode, bool shown_lazy // Depth in the same shape (one swapchain, each view's depth its half): // colour still captured and shown through Frame Generation; then, Frame // Generation toggled off, the game's frames reach the runtime as they - // are, without the bridge-private depth. Unknown metadata is checked + // are, with the depth the bridge mirrors. Unknown metadata is checked // separately below, once supported depth has passed generation. settle({}); const depthxr::FrameGenStatus before_depth = Peer::FrameGen(); @@ -4958,10 +6249,18 @@ void TestSideBySideFrameGen(Game& game, const std::string& mode, bool shown_lazy std::scoped_lock lock(g_runtime_mutex); for (std::size_t i = depth_from; i < g_runtime.frames.size(); ++i) { for (const ShownView& view : g_runtime.frames[i].views) { - Check(!view.depth, "depth: the runtime gets no depth in a bridged session's views"); + // Frame Generation's own frames carry no depth (they show its + // image); any frame that does carries the game's. + if (view.depth) { + Check(view.depth_swapchain == session.depth.swapchain && view.depth_value && + *view.depth_value == 0.5f, + "depth: a view's depth is the game's (" + + (view.depth_value ? std::to_string(*view.depth_value) : std::string("unread")) + ")"); + } if (i >= as_is_from) { - Check(view.next_type == XR_TYPE_UNKNOWN, - "depth-only, Frame Generation off: the stripped view has no other metadata"); + Check(view.depth && view.next_type == XR_TYPE_COMPOSITION_LAYER_DEPTH_INFO_KHR && + view.after_depth == XR_TYPE_UNKNOWN, + "depth-only, Frame Generation off: the view has its depth and no other metadata"); } } } @@ -4969,9 +6268,9 @@ void TestSideBySideFrameGen(Game& game, const std::string& mode, bool shown_lazy Check(depth && depth->releases == 26, "depth: the depth swapchain's releases reach the runtime"); } std::string log = Peer::Log(); - Check(Contains(log, "keeps the game's depth to itself"), "depth: the log tells depth is left out"); + Check(!Contains(log, "keeps the game's depth to itself"), "depth: the log tells of no depth left out"); - for (const Target* target : {&session.eyes, &session.ui}) { + for (const Target* target : {&session.eyes, &session.ui, &session.depth}) { const std::string describe = Peer::Bridge().Describe(target->swapchain); Check(Contains(describe, "skippedCopies=0"), "every release copied: " + describe); } @@ -5021,8 +6320,8 @@ void TestSideBySideFrameGen(Game& game, const std::string& mode, bool shown_lazy Check(g_runtime.frames.size() >= unknown_from + 6, "unknown metadata frames reached the runtime"); for (std::size_t i = unknown_from; i < g_runtime.frames.size(); ++i) { for (const ShownView& view : g_runtime.frames[i].views) { - Check(!view.depth && view.next_type == kApplicationStructType, - "the bridge strips private depth and preserves the following unknown metadata"); + Check(view.depth && view.after_depth == kApplicationStructType, + "mirrored depth and the unknown metadata after it reach the runtime"); } } } @@ -5049,6 +6348,654 @@ void TestSideBySideFrameGen(Game& game, const std::string& mode, bool shown_lazy EndCase(game); } +// A GPU-bound game's Direct3D 11 frames on a timeline of their own: each +// frame's work is held back until `gpu` after the one before it finished +// (or after it was submitted, if later), as a GPU running one frame at a +// time does, with nothing actually drawn (no real GPU load, no stall of the +// device: software adapter only). +struct SerialGameGpu { + using Clock = std::chrono::steady_clock; + struct Hold { + std::unique_ptr gate; + Clock::time_point due; + bool open{false}; + }; + std::chrono::microseconds gpu; + std::mutex mutex; + std::condition_variable cv; + std::deque holds; + Clock::time_point last_due{}; + bool stop{false}; + std::thread opener; + + explicit SerialGameGpu(std::chrono::microseconds frame) : gpu(frame) { + opener = std::thread([this] { + std::unique_lock lock(mutex); + for (;;) { + const auto next = std::find_if(holds.begin(), holds.end(), [](const Hold& hold) { return !hold.open; }); + if (stop && next == holds.end()) return; + if (next == holds.end()) { + cv.wait(lock); + continue; + } + if (Clock::now() < next->due) { + cv.wait_until(lock, next->due); + continue; + } + next->gate->Open(); + next->open = true; + // The game's device may still be past a gate's wait only + // once later frames were held behind it: keep a few. + while (holds.size() > 16 && holds.front().open) holds.pop_front(); + } + }); + } + ~SerialGameGpu() { + { + std::scoped_lock lock(mutex); + stop = true; + } + cv.notify_all(); + opener.join(); + } + void HoldFrame(Game& game) { + std::scoped_lock lock(mutex); + Hold& hold = holds.emplace_back(); + hold.gate = std::make_unique(g_runtime.device12.Get(), game); + hold.gate->HoldGame(game); + hold.due = std::max(last_due, Clock::now()) + gpu; + last_due = hold.due; + cv.notify_all(); + } +}; + +// A GPU-bound game's real Direct3D 11 work on a GPU: full-screen draws on the +// game's own context, each about `chunk` long (short, so nothing holds the +// GPU), as many per frame as make up `frame`. Calibrated with timestamps. +struct GameDraws { + Game& game; + ComPtr vs; + ComPtr ps; + ComPtr target; + ComPtr rtv; + ComPtr constants; + std::uint32_t iterations{64}; + double chunk_ms{0.0}; + int chunks{1}; + static constexpr UINT kSize = 2048; + + GameDraws(Game& g, double frame_ms, double chunk_target_ms = 0.5) : game(g) { + static const char source[] = R"( +cbuffer Load : register(b0) { uint iterations; uint3 pad; }; +float4 VSMain(uint id : SV_VertexID) : SV_Position { + const float2 uv = float2((id << 1) & 2, id & 2); + return float4(uv * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0); +} +float4 PSMain(float4 position : SV_Position) : SV_Target { + float4 v = float4(position.xy * 0.001, 0.5, 1.0); + [loop] for (uint i = 0; i < iterations; ++i) { v = frac(v * 1.0001 + v.yzwx * 0.37 + 0.11); } + return v; +} +)"; + ComPtr vs_blob, ps_blob; + Check(SUCCEEDED(D3DCompile(source, std::strlen(source), "load", nullptr, nullptr, "VSMain", "vs_5_0", 0, 0, + &vs_blob, nullptr)) && + SUCCEEDED(D3DCompile(source, std::strlen(source), "load", nullptr, nullptr, "PSMain", "ps_5_0", 0, 0, + &ps_blob, nullptr)) && + SUCCEEDED(game.device->CreateVertexShader(vs_blob->GetBufferPointer(), vs_blob->GetBufferSize(), + nullptr, &vs)) && + SUCCEEDED(game.device->CreatePixelShader(ps_blob->GetBufferPointer(), ps_blob->GetBufferSize(), + nullptr, &ps)), + "the game load's shaders"); + D3D11_TEXTURE2D_DESC desc{}; + desc.Width = desc.Height = kSize; + desc.MipLevels = desc.ArraySize = 1; + desc.Format = DXGI_FORMAT_R16G16B16A16_FLOAT; + desc.SampleDesc.Count = 1; + desc.BindFlags = D3D11_BIND_RENDER_TARGET; + D3D11_BUFFER_DESC buffer{16, D3D11_USAGE_DEFAULT, D3D11_BIND_CONSTANT_BUFFER, 0, 0, 0}; + Check(SUCCEEDED(game.device->CreateTexture2D(&desc, nullptr, &target)) && + SUCCEEDED(game.device->CreateRenderTargetView(target.Get(), nullptr, &rtv)) && + SUCCEEDED(game.device->CreateBuffer(&buffer, nullptr, &constants)), + "the game load's target"); + double ms = Measure(iterations); + while (ms < chunk_target_ms * 0.5 && iterations < (1u << 22)) { + iterations *= 2; + ms = Measure(iterations); + } + iterations = std::max(1, static_cast(iterations * chunk_target_ms / std::max(ms, 1e-3))); + chunk_ms = Measure(iterations); + chunks = std::max(1, static_cast(std::lround(frame_ms / std::max(chunk_ms, 1e-3)))); + } + + void Bind(std::uint32_t count) { + const std::uint32_t data[4]{count, 0, 0, 0}; + ID3D11DeviceContext* context = game.context.Get(); + context->UpdateSubresource(constants.Get(), 0, nullptr, data, 0, 0); + const D3D11_VIEWPORT viewport{0.0f, 0.0f, static_cast(kSize), static_cast(kSize), 0.0f, 1.0f}; + context->RSSetViewports(1, &viewport); + context->OMSetRenderTargets(1, rtv.GetAddressOf(), nullptr); + context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + context->IASetInputLayout(nullptr); + context->VSSetShader(vs.Get(), nullptr, 0); + context->PSSetShader(ps.Get(), nullptr, 0); + context->PSSetConstantBuffers(0, 1, constants.GetAddressOf()); + } + + void Unbind() { + ID3D11DeviceContext* context = game.context.Get(); + ID3D11RenderTargetView* none = nullptr; + context->OMSetRenderTargets(1, &none, nullptr); + context->VSSetShader(nullptr, nullptr, 0); + context->PSSetShader(nullptr, nullptr, 0); + } + + // One draw's GPU time at `count` iterations (median of five). + double Measure(std::uint32_t count) { + ComPtr disjoint, begin, end; + const D3D11_QUERY_DESC disjoint_desc{D3D11_QUERY_TIMESTAMP_DISJOINT, 0}, stamp{D3D11_QUERY_TIMESTAMP, 0}; + Check(SUCCEEDED(game.device->CreateQuery(&disjoint_desc, &disjoint)) && + SUCCEEDED(game.device->CreateQuery(&stamp, &begin)) && SUCCEEDED(game.device->CreateQuery(&stamp, &end)), + "the game load's timing queries"); + ID3D11DeviceContext* context = game.context.Get(); + Bind(count); + std::vector times; + for (int i = 0; i < 5; ++i) { + context->Begin(disjoint.Get()); + context->End(begin.Get()); + context->Draw(3, 0); + context->End(end.Get()); + context->End(disjoint.Get()); + D3D11_QUERY_DATA_TIMESTAMP_DISJOINT frequency{}; + while (context->GetData(disjoint.Get(), &frequency, sizeof(frequency), 0) == S_FALSE) std::this_thread::yield(); + UINT64 a = 0, b = 0; + while (context->GetData(begin.Get(), &a, sizeof(a), 0) == S_FALSE) std::this_thread::yield(); + while (context->GetData(end.Get(), &b, sizeof(b), 0) == S_FALSE) std::this_thread::yield(); + if (!frequency.Disjoint && b > a) times.push_back(static_cast(b - a) * 1000.0 / frequency.Frequency); + } + Unbind(); + std::sort(times.begin(), times.end()); + return times.empty() ? 0.0 : times[times.size() / 2]; + } + + // One frame's work, on the game's context (`share` of it). + void Frame(double share = 1.0) { + Bind(iterations); + const int count = std::max(1, static_cast(std::lround(chunks * share))); + for (int chunk = 0; chunk < count; ++chunk) { + game.context->Draw(3, 0); + if (chunk % 4 == 3) game.context->Flush(); + } + Unbind(); + game.context->Flush(); + } +}; + +// A DCS-shaped game, GPU-bound at about half the refresh rate (its frame's +// GPU work 20 ms, its CPU work 9 ms of it before rendering), in Dynamic +// interpolate through the bridge: engaged, the game must keep most of the +// rate it ran at passing through (DCS on a 4080 SUPER, 2026-10-22: 44 fps +// passing through, 17 engaged with the GPU half idle). `pimax`: on PimaxXR +// 0.4.4's frame loop and swapchains (PimaxCompositor) with "Prefer framerate +// over latency", the reporter's runtime: its xrEndFrame holds Frame +// Generation's frames until its refresh with its swapchain lock taken, and +// the game's swapchain calls must not wait for it (each did, up to a +// refresh: 36 fps passing through, 13 engaged). `quad`: the game also +// acquires and waits for a quad layer's image at each frame's start, draws +// it after releasing the eyes and shows it (OpenKneeboard above VectorXR, or +// a game's interface): the layer's wait must not wait for the game's last +// frame's GPU work (it did, for the newest held-back release: 32 fps +// engaged against 48 passing through, the GPU idle through the game's CPU +// work). On WARP the game's GPU time is held back by gates; on a GPU it is +// real draws (a GPU's device is never held: gpu-test-bsod-hazard). +void TestBridgedGpuBoundDynamic(Game& game, bool pimax, bool quad = false, bool kneeboard = false) { + const std::string name = std::string(pimax ? "gpu-bound-dynamic-pimax" : quad ? "gpu-bound-dynamic-quad" : "gpu-bound-dynamic") + + (pimax && quad ? "-quad" : "") + (kneeboard ? "-kneeboard" : ""); + const bool software = SoftwareAdapter(game); + // A headset's eyes (the reporter's: 3456x4035): Frame Generation's GPU work in proportion. + const std::uint32_t small_width = g_side_by_side_eye_width, small_height = g_side_by_side_eye_height; + if (const char* eye = std::getenv("BRIDGE_TEST_EYE")) { + unsigned width = 0, height = 0; + if (std::sscanf(eye, "%ux%u", &width, &height) == 2 && width && height) { + g_side_by_side_eye_width = width; + g_side_by_side_eye_height = height; + } + } + // The game's GPU and CPU work per frame, and the views it renders into + // swapchains of their own before the eyes (DCS: four, 932x1092 and + // 1556x1636 pairs), each acquired, rendered and released in turn. + // On PimaxXR the game is DCS-shaped (on a 4080 SUPER, 2026-10-22: about + // 26 ms of CPU and 27 of GPU a frame, four views besides the eyes); on + // WARP, whose Frame Generation takes the CPU's time, a lighter one. + double game_gpu_ms = pimax && !software ? 26.0 : 20.0; + if (const char* ms = std::getenv("BRIDGE_TEST_GAME_GPU_MS")) game_gpu_ms = std::atof(ms); + double game_cpu_ms = !pimax ? 9.0 : software ? 12.0 : 25.0; + if (const char* ms = std::getenv("BRIDGE_TEST_GAME_CPU_MS")) game_cpu_ms = std::atof(ms); + int extra_views = !pimax ? 0 : software ? 2 : 4; + if (const char* views = std::getenv("BRIDGE_TEST_EXTRA_VIEWS")) extra_views = std::atoi(views); + CaseSettings settings; + settings.frame_multiplier = "dynamic"; + settings.synthesis_mode = "interpolate"; + settings.debug_log = true; + BeginCase(name, game, settings); + g_runtime.recommended_width = g_side_by_side_eye_width; + g_runtime.recommended_height = g_side_by_side_eye_height; + g_runtime.paced = !pimax; + g_runtime.pimax = pimax; + g_runtime.unread = true; + if (const char* same = std::getenv("BRIDGE_TEST_SAME_IMAGE")) g_runtime.same_image = std::string(same) == "1"; + Peer::SetFrameGenEngagement(2, std::chrono::milliseconds(0)); + Peer::UseFrameGenGpu(depthxr::CreateFrameGenGpu); + depthxr::FrameGenDynamicPolicy policy; + policy.engage_after = std::chrono::milliseconds(60000); // passing through first + policy.settle = std::chrono::milliseconds(500); + policy.guard_window = std::chrono::milliseconds(1000); + policy.hold = std::chrono::milliseconds(60000); + policy.probe_after = std::chrono::milliseconds(600000); + Peer::SetFrameGenDynamicPolicy(policy); + Peer::SetFrameGenSummaryInterval(std::chrono::milliseconds(1000)); + // The plain case has a forwarding layer below VectorXR (OpenKneeboard + // after an update, neighbor-layers.md): it gets every frame Frame + // Generation shows, and the log says so and how many. + const bool layer_below = !pimax && !quad && !kneeboard; + depthxr::XrApiLayerNextInfo below{}; + below.structType = depthxr::XR_LOADER_INTERFACE_STRUCT_API_LAYER_NEXT_INFO; + below.structVersion = depthxr::XR_API_LAYER_NEXT_INFO_STRUCT_VERSION; + below.structSize = sizeof(below); + std::strncpy(below.layerName, "XR_APILAYER_FREDEMMOTT_OpenKneeboard", XR_MAX_API_LAYER_NAME_SIZE - 1); + below.nextGetInstanceProcAddr = RtGetInstanceProcAddr; + below.nextCreateApiLayerInstance = RtCreateApiLayerInstance; + if (layer_below) g_layer_below = &below; + SideBySideSession session; + App& app = session.app; + app = StartApp(); + session.create = std::make_unique(game.device.Get(), app.system); + Check(app.api.create_session(app.api.instance, &session.create->info, &app.session) == XR_SUCCESS && + Peer::Bridge().Active() && Peer::QueueSubstituted(), + name + ": the session is bridged"); + if (pimax) { + std::scoped_lock lock(g_runtime_mutex); + g_pimax_game_thread = std::this_thread::get_id(); + // "Prefer framerate over latency" (the reporter's: xrEndFrame 10.7 ms + // for Frame Generation's frames); BRIDGE_TEST_PIMAX_DEFER=0 for PimaxXR's default. + const char* defer = std::getenv("BRIDGE_TEST_PIMAX_DEFER"); + g_pimax.defer = !(defer && std::string(defer) == "0"); + // BRIDGE_TEST_PIMAX_SYNC_GPU=1: "Wait for GPU work in EndFrame" + // (quirk_sync_gpu_work_in_end_frame) on PimaxXR's Experimental page. + const char* sync = std::getenv("BRIDGE_TEST_PIMAX_SYNC_GPU"); + g_pimax.sync_gpu_in_end = sync && std::string(sync) == "1"; + // BRIDGE_TEST_PIMAX_CLAMP_EVERY=N: every Nth wait, two waits whose + // display times PimaxXR clamps to 1 ns after the last. + if (const char* clamp = std::getenv("BRIDGE_TEST_PIMAX_CLAMP_EVERY")) g_runtime.pimax_clamp_every = std::atoi(clamp); + g_pimax.Start(g_runtime.device12.Get()); + } + BeginSession(app); + session.eyes = MakeTarget(app, XPlaneInfo(2 * g_side_by_side_eye_width, g_side_by_side_eye_height), "both eyes side by side"); + session.ui = MakeTarget(app, XPlaneInfo(kQuadLayerSize, kQuadLayerSize), "the quad layer's"); + session.depth = MakeTarget(app, DepthInfo(1), "depth"); + std::optional gates; + std::optional draws; + if (software) { + gates.emplace(std::chrono::microseconds(static_cast(game_gpu_ms * 1000.0))); + } else { + draws.emplace(game, game_gpu_ms); + std::cout << name << ": game load " << draws->chunks << " draws of " << draws->chunk_ms << " ms\n"; + } + SideBySideFrameOptions options; + // BRIDGE_TEST_QUAD=1: a quad layer the game renders, waits for and shows each frame (OpenKneeboard's). + const char* quad_env = std::getenv("BRIDGE_TEST_QUAD"); + options.quad_layer = quad || kneeboard || (quad_env && std::string(quad_env) == "1"); + options.kneeboard = kneeboard; + std::vector extras; + for (int view = 0; view < extra_views; ++view) { + extras.push_back(MakeTarget(app, view % 4 < 2 ? XPlaneInfo(932, 1092) : XPlaneInfo(1556, 1636), + "an extra view's")); + } + options.before_render = [&] { + const double slices = static_cast(extras.size() + 1); + const auto cpu = std::chrono::microseconds(static_cast(game_cpu_ms * 1000.0 / slices)); + for (const Target& extra : extras) { + SleepUntil(std::chrono::steady_clock::now() + cpu); + const std::uint32_t index = Acquire(app, extra); + if (draws) draws->Frame(1.0 / slices); + Clear(game, extra.images[index], {40, 80, 120, 255}, 0); + Release(app, extra); + } + SleepUntil(std::chrono::steady_clock::now() + cpu); + if (gates) gates->HoldFrame(game); + if (draws) draws->Frame(1.0 / slices); + }; + const auto calls_line = [&] { + const CallTimes t = std::exchange(g_call_times, {}); + if (!t.frames) return std::string(); + std::ostringstream line; + line << std::fixed << std::setprecision(2) << " [game calls ms/frame: eyes acquire " << t.eyes_acquire / t.frames + << ", release " << t.eyes_release / t.frames << "; quad acquire " << t.ui_acquire / t.frames + << ", release " << t.ui_release / t.frames << "; xrWaitFrame " << t.wait / t.frames << ", xrEndFrame " + << t.end / t.frames << "; longest frame " << t.max_frame << "]"; + return line.str(); + }; + const auto pimax_line = [&] { + if (!pimax) return std::string(); + std::scoped_lock lock(g_pimax.mutex); + std::ostringstream line; + const std::uint64_t calls = g_pimax_swapchain_calls.exchange(0); + const std::uint64_t waited_us = g_pimax_swapchain_wait_us.exchange(0); + const std::uint64_t max_us = g_pimax_swapchain_wait_max_us.exchange(0); + line << std::fixed << std::setprecision(2) << " (PimaxXR xrEndFrame waited " + << (g_pimax.ends ? g_pimax.end_wait_ms / g_pimax.ends : 0.0) << " ms"; + if (g_pimax.sync_gpu_in_end) { + line << " (" << (g_pimax.ends ? g_pimax.end_sync_ms / g_pimax.ends : 0.0) << " for GPU work)"; + } + line << ", xrWaitFrame " << (g_pimax.waits ? g_pimax.wait_wait_ms / g_pimax.waits : 0.0) + << " ms, swapchain calls " << (calls ? waited_us / 1000.0 / calls : 0.0) << " ms on average (" << calls + << " calls, at most " << max_us / 1000.0 << " ms), blank refreshes " << g_pimax.blank_refreshes << ")"; + g_pimax.end_wait_ms = g_pimax.wait_wait_ms = g_pimax.end_sync_ms = 0.0; + g_pimax.ends = g_pimax.waits = 0; + return line.str(); + }; + // Direct3D 11's waits for the bridge's copy of the image it acquires + // (D3D11Bridge::Acquired), per game frame since asked last. + std::uint64_t done_waits_seen = 0; + int done_frames_seen = 0; + double done_waits_per_frame = 0.0; + const auto done_waits_line = [&] { + const std::uint64_t waits = SummaryValue(Peer::Bridge().Summary(), "doneWaits"); + const int frames = session.frames; + done_waits_per_frame = frames > done_frames_seen ? static_cast(waits - done_waits_seen) / + (frames - done_frames_seen) + : 0.0; + done_waits_seen = waits; + done_frames_seen = frames; + std::ostringstream line; + line << std::fixed << std::setprecision(2) << " [bridge doneWaits/frame " << done_waits_per_frame << "]"; + return line.str(); + }; + // BRIDGE_TEST_STALL_DUMP=1: a game frame taking over 300 ms prints the + // queues' state while it is held (diagnostics). + std::atomic frame_began{0}; + std::atomic watching{true}; + std::thread watchdog; + if (const char* dump = std::getenv("BRIDGE_TEST_STALL_DUMP"); dump && std::string(dump) == "1") { + watchdog = std::thread([&] { + std::int64_t dumped = 0, dumped_at = 0; + while (watching) { + Sleep(20); + const std::int64_t began = frame_began.load(); + const std::int64_t now = std::chrono::steady_clock::now().time_since_epoch().count(); + if (began && now - began > 300'000'000 && (began != dumped || now - dumped_at > 400'000'000)) { + dumped = began; + dumped_at = now; + std::cout << name << ": STALL " << (now - began) / 1'000'000 << " ms\n queue: " << Peer::QueueState() + << "\n bridge: " << Peer::Bridge().Summary(); + if (pimax) { + std::scoped_lock lock(g_pimax.mutex); + std::cout << "\n pimax: idle=" << g_pimax.idle << " handed=" << g_pimax.handed + << " serialized=" << g_pimax.serialized << "/" + << (g_pimax.serialize ? g_pimax.serialize->GetCompletedValue() : 0) + << " rendered=" << g_pimax.rendered_value << "/" + << (g_pimax.rendered ? g_pimax.rendered->GetCompletedValue() : 0); + } + const depthxr::FrameGenStatus status = Peer::FrameGen(); + std::cout << "\n framegen: generated=" << status.generated << " gpuWaits=" << status.gpu_waits + << " reason=" << Peer::FrameGenStatusReason(status) << std::endl; + } + } + }); + } + const auto rate = [&](std::chrono::milliseconds duration, const std::function& until = {}) { + const auto start = std::chrono::steady_clock::now(); + int frames = 0; + while (std::chrono::steady_clock::now() - start < duration) { + frame_began = std::chrono::steady_clock::now().time_since_epoch().count(); + SideBySideFrame(session, game, options); + ++frames; + if (until && until()) break; + } + return frames / std::chrono::duration(std::chrono::steady_clock::now() - start).count(); + }; + const auto engaged = [] { + const depthxr::FrameGenStatus status = Peer::FrameGen(); + return status.threaded && !status.standby; + }; + rate(std::chrono::milliseconds(1000)); + pimax_line(); + calls_line(); + done_waits_line(); + const double through = rate(std::chrono::milliseconds(2000)); + const std::string through_pimax = pimax_line() + calls_line() + done_waits_line(); + Check(Peer::FrameGen().standby, name + ": standing by first"); + policy.engage_after = std::chrono::milliseconds(500); + Peer::SetFrameGenDynamicPolicy(policy); + rate(std::chrono::milliseconds(6000), engaged); + Check(engaged(), name + ": engages for a game at half the refresh rate"); + // Engaging sets Frame Generation's GPU work up on the game's frame thread + // (its pipelines and views, made by its first Executes and again when its + // interpolation flow switches: on WARP 180 ms, then 1.2-1.5 s, now and + // then a third; one game frame held each time). Measured before that is + // done, the window took such a frame in or not by chance: the PimaxXR + // case failed about 3 runs in 8 right after the quad case, and any case + // could (Nov 2 2026). The rate is measured once two settling windows in a + // row kept the rate (6 s at most): a game slowed on every frame never + // does, and fails below. + for (int settled = 0, tries = 0; settled < 2 && tries < 20; ++tries) { + settled = rate(std::chrono::milliseconds(300)) >= 0.75 * through ? settled + 1 : 0; + } + pimax_line(); + calls_line(); + done_waits_line(); + std::uint64_t blank_before = 0; + { + std::scoped_lock lock(g_pimax.mutex); + blank_before = g_pimax.blank_refreshes; + } + const double during = rate(std::chrono::milliseconds(1500)); + const std::string during_pimax = pimax_line() + calls_line() + done_waits_line(); + watching = false; + if (watchdog.joinable()) watchdog.join(); + const depthxr::FrameGenStatus status = Peer::FrameGen(); + std::cout << name << ": " << through << " fps passing through" << through_pimax << ", " << during + << " fps engaged" << during_pimax << " (generated " << status.generated << ", gpuWaits " + << status.gpu_waits << ", gating " << status.gpu_gating << ")\n"; + // BRIDGE_TEST_LOG_DIR: the case's log kept (its summaries every second), + // before the session's end can discard it. + if (const char* keep = std::getenv("BRIDGE_TEST_LOG_DIR")) { + std::error_code error; + std::filesystem::create_directories(keep, error); + std::filesystem::copy_file(Peer::ActiveLogPath(), std::filesystem::path(keep) / (name + ".log"), + std::filesystem::copy_options::overwrite_existing, error); + if (error) std::cout << name << ": the log was not kept (" << error.message() << ")\n"; + } + Check(during >= 0.75 * through, name + ": engaged, the game keeps most of its rate (" + std::to_string(during) + + " fps against " + std::to_string(through) + " passing through)"); + if (pimax) { + // PimaxXR 0.4.4 shows a frame begun and never ended as a blank + // refresh. Engaged, none; the first refreshes after engaging, before + // anything is written, still leave a few open (nothing consistent to + // show: the frame passed through has newer images in its swapchains). + std::scoped_lock lock(g_pimax.mutex); + Check(g_pimax.blank_refreshes == blank_before, + name + ": engaged, no frame is left open (" + std::to_string(g_pimax.blank_refreshes - blank_before) + + " blank refreshes; " + std::to_string(blank_before) + " while engaging)"); + const std::string log = Peer::Log(); + // (Read from this machine's registry, as PimaxXR reads them.) + Check(Contains(log, "PimaxXR frame settings: defer_frame_wait="), name + ": the log names PimaxXR's frame settings"); + Check(Contains(log, "runtimeWaitMs=") && Contains(log, "runtimeWaitsBlocked="), + name + ": the summaries tell how long the runtime's waits took"); + } + for (const Target& extra : extras) { + Check(app.api.destroy_swapchain(extra.swapchain) == XR_SUCCESS, "an extra view's swapchain is destroyed"); + } + for (const Target* target : {&session.eyes, &session.ui, &session.depth}) { + Check(app.api.destroy_swapchain(target->swapchain) == XR_SUCCESS, "a swapchain is destroyed"); + } + if (layer_below) { + Check(app.api.destroy_session(app.session) == XR_SUCCESS, name + ": xrDestroySession"); + app.session = XR_NULL_HANDLE; + const std::string log = Peer::Log(); + const std::string end_line = "Frame Generation at session end: the runtime and the layers below VectorXR " + "[XR_APILAYER_FREDEMMOTT_OpenKneeboard] got "; + const std::size_t at = log.find(end_line); + Check(Contains(log, "Frame Generation: the API layers below VectorXR [XR_APILAYER_FREDEMMOTT_OpenKneeboard] get " + "every frame it shows") && + at != std::string::npos, + name + ": the log names the layer below and what it gets"); + const std::string counts = log.substr(at + end_line.size(), log.find('\n', at) - at - end_line.size()); + const std::size_t per = counts.find('('); + const double ratio = per == std::string::npos ? 0.0 : std::atof(counts.c_str() + per + 1); + std::cout << name << ": the layer below got " << counts << "\n"; + Check(ratio > 1.1, name + ": the layer below got more frames than the game ended (" + counts + ")"); + } + StopApp(app); + g_layer_below = nullptr; + g_pimax.Stop(); + Peer::SetFrameGenSummaryInterval(std::chrono::milliseconds(30000)); + g_side_by_side_eye_width = small_width; + g_side_by_side_eye_height = small_height; + Peer::UseFrameGenGpu(MakeStandInGpu); + Peer::SetFrameGenEngagement(45, std::chrono::milliseconds(3000)); // the presenter's own + Peer::SetFrameGenDynamicPolicy({}); + EndCase(game); +} + +// OpenXR Toolkit below VectorXR, not bypassed: Frame Generation stays off. +// The Toolkit holds every release back until its next xrEndFrame and then +// releases every swapchain it held, shown in that frame or not, and writes +// its upscaled image into the runtime's only for swapchains the frame shows +// (OpenXR-Toolkit layer.cpp xrReleaseSwapchainImage, xrEndFrame). Frame +// Generation's frames (2-4 per game frame, from its own thread) would make +// it release the game's images without its work and race its unlocked +// swapchain map against the game's thread. +void TestToolkitBelowKeepsFrameGenOff(Game& game) { + const std::string name = "toolkit-below-framegen-off"; + BeginCase(name, game); + depthxr::XrApiLayerNextInfo toolkit{}; + toolkit.structType = depthxr::XR_LOADER_INTERFACE_STRUCT_API_LAYER_NEXT_INFO; + toolkit.structVersion = depthxr::XR_API_LAYER_NEXT_INFO_STRUCT_VERSION; + toolkit.structSize = sizeof(toolkit); + std::strncpy(toolkit.layerName, "XR_APILAYER_MBUCCHIA_toolkit", XR_MAX_API_LAYER_NAME_SIZE - 1); + toolkit.nextGetInstanceProcAddr = RtGetInstanceProcAddr; + toolkit.nextCreateApiLayerInstance = RtCreateApiLayerInstance; + g_layer_below = &toolkit; + App app = StartApp(); + SessionCreate create(game.device.Get(), app.system); + Check(app.api.create_session(app.api.instance, &create.info, &app.session) == XR_SUCCESS, name + ": a session"); + Check(!Peer::Bridge().Active() && !Peer::QueueSubstituted(), + name + ": the session is kept as it is (no bridge, no queue of Frame Generation's)"); + const std::string log = Peer::Log(); + Check(Contains(log, "OpenXR Toolkit is loaded below VectorXR.") && + Contains(log, "Frame Generation is off for this game: OpenXR Toolkit is loaded below VectorXR"), + name + ": the log says why Frame Generation is off"); + const depthxr::OsdSnapshot osd = Peer::Osd(); + Check(osd.framegen == "Off / Layer conflict with OpenXR Toolkit" && + osd.compact_framegen == osd.framegen && Contains(osd.framegen_detail, "Move OpenXR Toolkit above VectorXR"), + name + ": the OSD says Frame Generation is off for OpenXR Toolkit, not just Disabled (" + osd.framegen + ")"); + StopApp(app); + g_layer_below = nullptr; + EndCase(game); +} + +// A runtime swapchain call the layer makes for the game stalls on PimaxXR's +// swapchain lock (its xrEndFrame holds it for a refresh, called from Frame +// Generation's thread): the game's other threads' calls into the layer must +// not wait for it. The bridge hands the game's releases over at its +// xrEndFrame; that forwarded them to the runtime with the layer's lock held, +// so the game's xrLocateViews on another thread (DCS's frame loop has a few) +// waited out the runtime's stall too. +void TestRuntimeStallOutsideLayerLock(Game& game) { + const std::string name = "runtime-stall-outside-layer-lock"; + CaseSettings settings; + settings.frame_multiplier = "dynamic"; + BeginCase(name, game, settings); + g_runtime.pimax = true; + depthxr::FrameGenDynamicPolicy policy; + policy.engage_after = std::chrono::milliseconds(600000); // passing through: releases go at once + Peer::SetFrameGenDynamicPolicy(policy); + App app = StartApp(); + SessionCreate create(game.device.Get(), app.system); + Check(app.api.create_session(app.api.instance, &create.info, &app.session) == XR_SUCCESS && + Peer::Bridge().Active() && Peer::QueueSubstituted(), + name + ": the session is bridged"); + BeginSession(app); + Target left = MakeTarget(app, ColorInfo(), "left eye"); + Target right = MakeTarget(app, ColorInfo(), "right eye"); + for (int frame = 0; frame < 3; ++frame) { + Check(Frame(app, game, {&left, &right}, {FrameColor(frame, 0), FrameColor(frame, 1)}, {{{&left}, {&right}}}) == + XR_SUCCESS, + name + ": a frame"); + } + XrFrameWaitInfo wait_info{XR_TYPE_FRAME_WAIT_INFO}; + XrFrameState state{XR_TYPE_FRAME_STATE}; + Check(app.api.wait_frame(app.session, &wait_info, &state) == XR_SUCCESS, name + ": xrWaitFrame"); + XrFrameBeginInfo begin_info{XR_TYPE_FRAME_BEGIN_INFO}; + Check(app.api.begin_frame(app.session, &begin_info) == XR_SUCCESS, name + ": xrBeginFrame"); + for (const Target* target : {&left, &right}) { + Draw(game, target->images[Acquire(app, *target)], FrameColor(3, target == &left ? 0 : 1)); + Release(app, *target); // a bridged release: handed over at xrEndFrame + } + std::array views{}; + for (std::uint32_t eye = 0; eye < 2; ++eye) { + views[eye] = {XR_TYPE_COMPOSITION_LAYER_PROJECTION_VIEW}; + views[eye].pose = {{0, 0, 0, 1}, {0, 0, 0}}; + views[eye].fov = g_runtime.fovs[eye]; + views[eye].subImage.swapchain = (eye ? right : left).swapchain; + views[eye].subImage.imageRect = {{0, 0}, {static_cast(kWidth), static_cast(kHeight)}}; + } + XrCompositionLayerProjection projection{XR_TYPE_COMPOSITION_LAYER_PROJECTION}; + projection.space = app.space; + projection.viewCount = 2; + projection.views = views.data(); + const XrCompositionLayerBaseHeader* layers[] = {reinterpret_cast(&projection)}; + XrFrameEndInfo end{XR_TYPE_FRAME_END_INFO}; + end.displayTime = state.predictedDisplayTime; + end.environmentBlendMode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE; + end.layerCount = 1; + end.layers = layers; + // PimaxXR's xrEndFrame (Frame Generation's) holds its swapchain lock. + constexpr auto kStall = std::chrono::milliseconds(400); + std::unique_lock stalled(g_pimax_swapchains); + std::atomic ended{false}; + XrResult end_result = XR_ERROR_RUNTIME_FAILURE; + std::thread render([&] { + end_result = app.api.end_frame(app.session, &end); + ended = true; + }); + Sleep(50); // the game's xrEndFrame reaches the runtime's release + std::atomic located_ms{0.0}; + std::thread frame_loop([&] { + XrViewLocateInfo locate{XR_TYPE_VIEW_LOCATE_INFO}; + locate.viewConfigurationType = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO; + locate.displayTime = state.predictedDisplayTime; + locate.space = app.space; + XrViewState view_state{XR_TYPE_VIEW_STATE}; + std::array located{}; + for (XrView& view : located) view.type = XR_TYPE_VIEW; + std::uint32_t count = 0; + const auto start = std::chrono::steady_clock::now(); + app.api.locate_views(app.session, &locate, &view_state, 2, &count, located.data()); + located_ms = MsSince(start); + }); + const auto stall_start = std::chrono::steady_clock::now(); + while (std::chrono::steady_clock::now() - stall_start < kStall && located_ms == 0.0) Sleep(5); + const bool end_held = !ended; + Sleep(20); + stalled.unlock(); + frame_loop.join(); + render.join(); + std::cout << name << ": xrLocateViews on another thread took " << located_ms.load() + << " ms while the runtime held the game's release " << kStall.count() << " ms\n"; + Check(end_held, name + ": the game's xrEndFrame waited for the runtime's swapchain lock"); + Check(end_result == XR_SUCCESS, name + ": xrEndFrame"); + Check(located_ms < 100.0, name + ": xrLocateViews did not wait for the runtime's stalled swapchain call (" + + std::to_string(located_ms.load()) + " ms)"); + for (const Target* target : {&left, &right}) { + Check(app.api.destroy_swapchain(target->swapchain) == XR_SUCCESS, "a swapchain is destroyed"); + } + StopApp(app); + Peer::SetFrameGenDynamicPolicy({}); + EndCase(game); +} + // 15. A game quitting while Frame Generation presents from its own thread, // the runtime holding that thread inside xrWaitFrame until the session ends // (DCS hung so on quitting, 2026-10-09). With `ends_session` the game ends @@ -5064,8 +7011,8 @@ void TestQuitWhilePresenterHeld(Game& game, bool ends_session) { settings.synthesis_mode = "extrapolate"; BeginCase(std::string("quit-presenter-held") + (ends_session ? "-session-ended" : ""), game, settings); SetEnv("VECTORXR_FRAMEGEN_TEARDOWN_WAIT_MS", "400"); - g_runtime.recommended_width = kSideBySideEyeWidth; - g_runtime.recommended_height = kSideBySideEyeHeight; + g_runtime.recommended_width = g_side_by_side_eye_width; + g_runtime.recommended_height = g_side_by_side_eye_height; g_runtime.paced = true; Peer::SetFrameGenEngagement(2, std::chrono::milliseconds(0)); Peer::UseFrameGenGpu(depthxr::CreateFrameGenGpu); @@ -5078,7 +7025,7 @@ void TestQuitWhilePresenterHeld(Game& game, bool ends_session) { "a bridged session"); SetEnv("VECTORXR_FRAMEGEN_TEARDOWN_WAIT_MS", ""); BeginSession(app); - session.eyes = MakeTarget(app, XPlaneInfo(2 * kSideBySideEyeWidth, kSideBySideEyeHeight), "both eyes side by side"); + session.eyes = MakeTarget(app, XPlaneInfo(2 * g_side_by_side_eye_width, g_side_by_side_eye_height), "both eyes side by side"); SideBySideFrameOptions options; options.quad_layer = false; int frames = 0; @@ -5373,7 +7320,38 @@ int main(int argc, char** argv) { } CheckFrameGenStatusLine(); const bool bridged = TestBridgedSession(game); - if (bridged) { + const char* only = std::getenv("BRIDGE_TEST_ONLY"); + if (bridged && only && std::string(only) == "lock-order") { + TestContextHeldAcrossRelease(game); + TestQuadViewsUnderHeldContext(game); + TestUnstagedReleaseUnderHeldContext(game); + TestNativeCaptureUnderHeldContext(game); + TestNativeGenerateUnderHeldContext(game); + } else if (bridged && only && std::string(only) == "native-generate-context-held") { + TestNativeGenerateUnderHeldContext(game); + } else if (bridged && only && std::string(only) == "quad-views-context-held") { + TestQuadViewsUnderHeldContext(game); + } else if (bridged && only && std::string(only) == "unstaged-release-context-held") { + TestUnstagedReleaseUnderHeldContext(game); + } else if (bridged && only && std::string(only) == "native-capture-context-held") { + TestNativeCaptureUnderHeldContext(game); + } else if (bridged && only && std::string(only) == "depth-rects") { + TestDepthRects(game, false); + TestDepthRects(game, true); + TestDepthRects(game, false, false); + } else if (bridged && only && std::string(only) == "runtime-stall-outside-layer-lock") { + TestRuntimeStallOutsideLayerLock(game); + } else if (bridged && only && std::string(only) == "toolkit-below-framegen-off") { + TestToolkitBelowKeepsFrameGenOff(game); + } else if (bridged && only && std::string(only).rfind("gpu-bound-dynamic", 0) == 0) { + const std::string which = only; + if (which == "gpu-bound-dynamic" || which == "gpu-bound-dynamic-plain") TestBridgedGpuBoundDynamic(game, false); + if (which == "gpu-bound-dynamic" || which == "gpu-bound-dynamic-quad") TestBridgedGpuBoundDynamic(game, false, true); + if (which == "gpu-bound-dynamic" || which == "gpu-bound-dynamic-pimax") TestBridgedGpuBoundDynamic(game, true); + if (which == "gpu-bound-dynamic-kneeboard") TestBridgedGpuBoundDynamic(game, false, false, true); + if (which == "gpu-bound-dynamic-pimax-quad") TestBridgedGpuBoundDynamic(game, true, true); + if (which == "gpu-bound-dynamic-pimax-kneeboard") TestBridgedGpuBoundDynamic(game, true, false, true); + } else if (bridged) { TestKeepGameQueue(game); TestBridgeOffSession(game); TestRefusedD3D12Session(game); @@ -5381,6 +7359,18 @@ int main(int argc, char** argv) { TestDestroyInstanceWithSession(game); TestStatusFile(game); TestReleaseOnAnotherThread(game); + TestContextHeldAcrossRelease(game); + TestQuadViewsUnderHeldContext(game); + TestUnstagedReleaseUnderHeldContext(game); + TestNativeCaptureUnderHeldContext(game); + TestNativeGenerateUnderHeldContext(game); + TestPrivateDepthSession(game, false); + TestPrivateDepthSession(game, true); + TestDepthRects(game, false); + TestDepthRects(game, true); + TestDepthRects(game, false, false); + TestRuntimeStallOutsideLayerLock(game); + TestToolkitBelowKeepsFrameGenOff(game); TestBridgedQuadViewsComposed(game, false); TestBridgedQuadViewsComposed(game, true); TestBridgedQuadFrameGen(game, "extrapolate"); @@ -5390,6 +7380,12 @@ int main(int argc, char** argv) { TestSideBySideFrameGen(game, "interpolate"); TestSideBySideFrameGen(game, "extrapolate", true); TestSideBySideFrameGen(game, "interpolate", true); + TestBridgedGpuBoundDynamic(game, false); + TestBridgedGpuBoundDynamic(game, false, true); + TestBridgedGpuBoundDynamic(game, true); + // OpenKneeboard above VectorXR on PimaxXR (the report's setup): its + // quad acquired, waited for, drawn and released inside xrEndFrame. + TestBridgedGpuBoundDynamic(game, true, false, true); if (!skip_negative_controls) { TestQuitWhilePresenterHeld(game, false); TestQuitWhilePresenterHeld(game, true); diff --git a/layer/tests/d3d11_bridge_tests.cpp b/layer/tests/d3d11_bridge_tests.cpp index c6590a4d..9103d4f4 100644 --- a/layer/tests/d3d11_bridge_tests.cpp +++ b/layer/tests/d3d11_bridge_tests.cpp @@ -9,21 +9,29 @@ #include #include #include +#include #include #include #include #include "depthxr/framegen_d3d11_bridge.h" +#include "depthxr/framegen_gpu.h" +#include "depthxr/framegen_plan.h" #include "depthxr/session_graphics.h" #include "test_adapter.h" #include "test_gates.h" +#include #include +#include #include +#include #include #include #include #include +#include +#include #include #include #include @@ -533,11 +541,24 @@ void TestCreateInfo() { "copies need the runtime's colour images as render targets and copy destinations"); Check(runtime.sampleCount == 1, "multisampled images reach the runtime resolved"); XrSwapchainCreateInfo depth{XR_TYPE_SWAPCHAIN_CREATE_INFO}; - depth.usageFlags = XR_SWAPCHAIN_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; + depth.usageFlags = XR_SWAPCHAIN_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | XR_SWAPCHAIN_USAGE_SAMPLED_BIT; depth.format = DXGI_FORMAT_D32_FLOAT; depth.sampleCount = 1; runtime = D3D11Bridge::RuntimeCreateInfo(depth); - Check(runtime.usageFlags == depth.usageFlags, "depth swapchains reach the runtime as asked"); + Check(runtime.usageFlags == depth.usageFlags && runtime.sampleCount == 1, + "depth swapchains reach the runtime as asked"); + // UEVR's: sampled and mutable as well. + depth.usageFlags |= XR_SWAPCHAIN_USAGE_MUTABLE_FORMAT_BIT; + depth.format = DXGI_FORMAT_D32_FLOAT_S8X24_UINT; + runtime = D3D11Bridge::RuntimeCreateInfo(depth); + Check(runtime.usageFlags == depth.usageFlags, "UEVR's depth usage reaches the runtime as asked"); + // A depth format asked for without depth-stencil use: the bridge's draw + // needs the runtime's images to be depth targets. + depth.usageFlags = XR_SWAPCHAIN_USAGE_SAMPLED_BIT; + depth.format = DXGI_FORMAT_D16_UNORM; + runtime = D3D11Bridge::RuntimeCreateInfo(depth); + Check(runtime.usageFlags == (XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT), + "depth images are asked for as depth targets"); } // Frames through a three-image swapchain: each release reaches its runtime @@ -740,36 +761,1736 @@ void TestArray(Game& game) { bridge.End(); } -// Depth stays private: no copies, and the caller learns to leave it out. -void TestDepth(Game& game) { - D3D11Bridge bridge; - Session session(game.device.Get()); - BeginBridge(game, bridge, session); - MakeRuntimeImages(BridgeDevice(bridge), 2, 32, 32, DXGI_FORMAT_R8G8B8A8_UNORM); // never touched +// Depth: the four depth formats a runtime offers, as the game asks for them. +struct DepthFormat { + DXGI_FORMAT format; + const char* name; + DXGI_FORMAT typeless; // the family, as some runtimes' images are +}; +constexpr DepthFormat kDepthFormats[] = { + {DXGI_FORMAT_D16_UNORM, "D16", DXGI_FORMAT_R16_TYPELESS}, + {DXGI_FORMAT_D24_UNORM_S8_UINT, "D24S8", DXGI_FORMAT_R24G8_TYPELESS}, + {DXGI_FORMAT_D32_FLOAT, "D32", DXGI_FORMAT_R32_TYPELESS}, + {DXGI_FORMAT_D32_FLOAT_S8X24_UINT, "D32S8", DXGI_FORMAT_R32G8X24_TYPELESS}, +}; + +XrSwapchainCreateInfo DepthSwapchainInfo(DXGI_FORMAT format, std::uint32_t width, std::uint32_t height, + std::uint32_t array_size, std::uint32_t samples = 1) { XrSwapchainCreateInfo info{XR_TYPE_SWAPCHAIN_CREATE_INFO}; info.usageFlags = XR_SWAPCHAIN_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | XR_SWAPCHAIN_USAGE_SAMPLED_BIT; - info.format = DXGI_FORMAT_D32_FLOAT; - info.sampleCount = 1; - info.width = 32; - info.height = 32; + info.format = format; + info.sampleCount = samples; + info.width = width; + info.height = height; info.faceCount = 1; - info.arraySize = 1; + info.arraySize = array_size; info.mipCount = 1; - const XrSwapchain swapchain = Handle(0x50); + return info; +} + +// The stand-in runtime's depth swapchain: images in the depth-write state, as +// XR_KHR_D3D12_enable hands depth out (PimaxXR and Virtual Desktop make them +// so when asked for depth-stencil use). `depth_target` false: a runtime that +// made them no depth targets. +void MakeRuntimeDepthImages(ID3D12Device* device, std::uint32_t count, std::uint32_t width, std::uint32_t height, + DXGI_FORMAT format, std::uint32_t array_size, bool depth_target = true) { + g_runtime_images.clear(); + for (std::uint32_t i = 0; i < count; ++i) { + D3D12_HEAP_PROPERTIES heap{D3D12_HEAP_TYPE_DEFAULT}; + D3D12_RESOURCE_DESC desc{}; + desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + desc.Width = width; + desc.Height = height; + desc.DepthOrArraySize = static_cast(array_size); + desc.MipLevels = 1; + desc.Format = format; + desc.SampleDesc.Count = 1; + desc.Flags = depth_target ? D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL : D3D12_RESOURCE_FLAG_NONE; + ComPtr image; + Check(SUCCEEDED(device->CreateCommittedResource( + &heap, D3D12_HEAP_FLAG_NONE, &desc, + depth_target ? D3D12_RESOURCE_STATE_DEPTH_WRITE : D3D12_RESOURCE_STATE_COMMON, nullptr, + IID_PPV_ARGS(&image))), + "the stand-in runtime makes its depth images"); + g_runtime_images.push_back(image); + } +} + +// A depth texel as stored, without stencil: D16's 16 bits, D24's 24, D32's +// float bits. +std::uint32_t DepthBits(DXGI_FORMAT family, const std::uint8_t* texel) { + std::uint32_t bits = 0; + if (family == DXGI_FORMAT_R16_TYPELESS) { + std::uint16_t value = 0; + std::memcpy(&value, texel, sizeof(value)); + return value; + } + std::memcpy(&bits, texel, sizeof(bits)); + return family == DXGI_FORMAT_R24G8_TYPELESS ? bits & 0xFFFFFFu : bits; +} + +std::uint32_t DepthTexelBytes(DXGI_FORMAT family) { + return family == DXGI_FORMAT_R16_TYPELESS ? 2 : family == DXGI_FORMAT_R32G8X24_TYPELESS ? 8 : 4; +} + +// Every depth texel of the game's private texture's slice `slice`, read on +// its own device. +std::vector ReadGameDepth(Game& game, ID3D11Texture2D* texture, std::uint32_t slice) { + D3D11_TEXTURE2D_DESC desc{}; + texture->GetDesc(&desc); + D3D11_TEXTURE2D_DESC staging_desc = desc; + staging_desc.Usage = D3D11_USAGE_STAGING; + staging_desc.BindFlags = 0; + staging_desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; + ComPtr staging; + Check(SUCCEEDED(game.device->CreateTexture2D(&staging_desc, nullptr, &staging)), "a staging copy of game depth"); + game.context->CopyResource(staging.Get(), texture); + D3D11_MAPPED_SUBRESOURCE mapped{}; + const UINT subresource = D3D11CalcSubresource(0, slice, 1); + Check(SUCCEEDED(game.context->Map(staging.Get(), subresource, D3D11_MAP_READ, 0, &mapped)), "map game depth"); + std::vector texels; + const std::uint32_t bytes = DepthTexelBytes(desc.Format); + for (UINT y = 0; y < desc.Height; ++y) { + const auto* row = static_cast(mapped.pData) + static_cast(y) * mapped.RowPitch; + for (UINT x = 0; x < desc.Width; ++x) { + texels.push_back(DepthBits(desc.Format, row + static_cast(x) * bytes)); + } + } + game.context->Unmap(staging.Get(), subresource); + return texels; +} + +// Every depth texel of the runtime's depth image `index`, slice `slice`, read +// on a queue of the stand-in runtime's own (the depth plane: D24S8 and D32S8 +// are planar in Direct3D 12). +std::vector ReadRuntimeDepth(ID3D12Device* device, std::uint32_t index, DXGI_FORMAT family, + std::uint32_t slice) { + ID3D12Resource* image = g_runtime_images[index].Get(); + const D3D12_RESOURCE_DESC desc = image->GetDesc(); + D3D12_PLACED_SUBRESOURCE_FOOTPRINT footprint{}; + UINT64 bytes = 0; + const UINT subresource = slice; // plane 0, mip 0 + device->GetCopyableFootprints(&desc, subresource, 1, 0, &footprint, nullptr, nullptr, &bytes); + D3D12_HEAP_PROPERTIES heap{D3D12_HEAP_TYPE_READBACK}; + D3D12_RESOURCE_DESC buffer_desc{}; + buffer_desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; + buffer_desc.Width = bytes; + buffer_desc.Height = 1; + buffer_desc.DepthOrArraySize = 1; + buffer_desc.MipLevels = 1; + buffer_desc.SampleDesc.Count = 1; + buffer_desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; + ComPtr buffer; + ComPtr queue; + ComPtr allocator; + ComPtr list; + ComPtr fence; + D3D12_COMMAND_QUEUE_DESC queue_desc{D3D12_COMMAND_LIST_TYPE_DIRECT}; + Check(SUCCEEDED(device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &buffer_desc, + D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&buffer))) && + SUCCEEDED(device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&queue))) && + SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator))) && + SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.Get(), nullptr, + IID_PPV_ARGS(&list))) && + SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence))), + "depth readback objects"); + D3D12_RESOURCE_BARRIER barrier{}; + barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; + barrier.Transition.pResource = image; + barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; + barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_DEPTH_WRITE; + barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_SOURCE; + list->ResourceBarrier(1, &barrier); + D3D12_TEXTURE_COPY_LOCATION source{image, D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX}; + source.SubresourceIndex = subresource; + D3D12_TEXTURE_COPY_LOCATION destination{buffer.Get(), D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT}; + destination.PlacedFootprint = footprint; + list->CopyTextureRegion(&destination, 0, 0, 0, &source, nullptr); + std::swap(barrier.Transition.StateBefore, barrier.Transition.StateAfter); + list->ResourceBarrier(1, &barrier); + list->Close(); + ID3D12CommandList* lists[] = {list.Get()}; + queue->ExecuteCommandLists(1, lists); + queue->Signal(fence.Get(), 1); + const HANDLE event = CreateEventW(nullptr, FALSE, FALSE, nullptr); + fence->SetEventOnCompletion(1, event); + Check(WaitForSingleObject(event, 10000) == WAIT_OBJECT_0, "the depth readback finishes"); + CloseHandle(event); + void* mapped = nullptr; + Check(SUCCEEDED(buffer->Map(0, nullptr, &mapped)), "map the depth readback"); + // The depth plane's own texel size (D32S8's plane 0 is 4 bytes a texel). + const std::uint32_t texel_bytes = family == DXGI_FORMAT_R16_TYPELESS ? 2 : 4; + const DXGI_FORMAT plane_family = family == DXGI_FORMAT_R32G8X24_TYPELESS ? DXGI_FORMAT_R32_TYPELESS : family; + std::vector texels; + for (UINT y = 0; y < desc.Height; ++y) { + const auto* row = static_cast(mapped) + footprint.Offset + + static_cast(y) * footprint.Footprint.RowPitch; + for (UINT x = 0; x < desc.Width; ++x) { + texels.push_back(DepthBits(plane_family, row + static_cast(x) * texel_bytes)); + } + } + buffer->Unmap(0, nullptr); + return texels; +} + +// The game's depth rendering: a draw over slice `slice` of a depth image +// whose pixel shader writes a hash of the texel, the slice and `frame` as +// depth (with 0 and 1 at the first texels, the range's ends), so every texel +// differs from its neighbours and from earlier frames. +class GameDepthDraw { + public: + explicit GameDepthDraw(Game& game) { + const char* source = R"( +cbuffer Frame : register(b0) { uint g_slice; uint g_frame; }; +float4 VSMain(uint vertex : SV_VertexID) : SV_Position { + const float2 uv = float2((vertex << 1) & 2, vertex & 2); + return float4(uv * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0); +} +float PSMain(float4 position : SV_Position) : SV_Depth { + const uint x = uint(position.x); + const uint y = uint(position.y); + if (x == 0 && y == 0) return 0.0; + if (x == 1 && y == 0) return 1.0; + uint h = x * 73856093u ^ y * 19349663u ^ g_slice * 83492791u ^ g_frame * 2654435761u; + h ^= h >> 13; h *= 0x5bd1e995u; h ^= h >> 15; + return float(h & 0xFFFFFFu) / 16777215.0; +})"; + ComPtr vs; + ComPtr ps; + ComPtr errors; + Check(SUCCEEDED(D3DCompile(source, std::strlen(source), "game_depth", nullptr, nullptr, "VSMain", "vs_5_0", 0, 0, + &vs, &errors)) && + SUCCEEDED(D3DCompile(source, std::strlen(source), "game_depth", nullptr, nullptr, "PSMain", "ps_5_0", + 0, 0, &ps, &errors)), + "the game's depth shaders compile"); + Check(SUCCEEDED(game.device->CreateVertexShader(vs->GetBufferPointer(), vs->GetBufferSize(), nullptr, &vs_)) && + SUCCEEDED(game.device->CreatePixelShader(ps->GetBufferPointer(), ps->GetBufferSize(), nullptr, &ps_)), + "the game's depth shaders"); + D3D11_DEPTH_STENCIL_DESC depth{}; + depth.DepthEnable = TRUE; + depth.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; + depth.DepthFunc = D3D11_COMPARISON_ALWAYS; + D3D11_RASTERIZER_DESC raster{}; + raster.FillMode = D3D11_FILL_SOLID; + raster.CullMode = D3D11_CULL_NONE; + raster.DepthClipEnable = TRUE; + D3D11_BUFFER_DESC constants{}; + constants.ByteWidth = 16; + constants.Usage = D3D11_USAGE_DEFAULT; + constants.BindFlags = D3D11_BIND_CONSTANT_BUFFER; + Check(SUCCEEDED(game.device->CreateDepthStencilState(&depth, &depth_)) && + SUCCEEDED(game.device->CreateRasterizerState(&raster, &raster_)) && + SUCCEEDED(game.device->CreateBuffer(&constants, nullptr, &constants_)), + "the game's depth draw state"); + } + + // Draws, leaving `texture`'s slice bound as the depth target when `keep_bound`. + void Draw(Game& game, ID3D11Texture2D* texture, DXGI_FORMAT format, std::uint32_t slice, std::uint32_t frame, + bool keep_bound = false) { + D3D11_TEXTURE2D_DESC desc{}; + texture->GetDesc(&desc); + D3D11_DEPTH_STENCIL_VIEW_DESC view{}; + view.Format = format; + view.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DARRAY; + view.Texture2DArray.FirstArraySlice = slice; + view.Texture2DArray.ArraySize = 1; + ComPtr target; + Check(SUCCEEDED(game.device->CreateDepthStencilView(texture, &view, &target)), "a depth view of game depth"); + const std::uint32_t values[4] = {slice, frame, 0, 0}; + game.context->UpdateSubresource(constants_.Get(), 0, nullptr, values, 0, 0); + const D3D11_VIEWPORT viewport{0.0f, 0.0f, static_cast(desc.Width), static_cast(desc.Height), + 0.0f, 1.0f}; + game.context->ClearDepthStencilView(target.Get(), D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 0.5f, 0x5a); + game.context->OMSetRenderTargets(0, nullptr, target.Get()); + game.context->OMSetDepthStencilState(depth_.Get(), 0); + game.context->RSSetState(raster_.Get()); + game.context->RSSetViewports(1, &viewport); + game.context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + game.context->IASetInputLayout(nullptr); + game.context->VSSetShader(vs_.Get(), nullptr, 0); + game.context->PSSetShader(ps_.Get(), nullptr, 0); + game.context->PSSetConstantBuffers(0, 1, constants_.GetAddressOf()); + game.context->Draw(3, 0); + if (!keep_bound) { + game.context->OMSetRenderTargets(0, nullptr, nullptr); + } + } + + private: + ComPtr vs_; + ComPtr ps_; + ComPtr depth_; + ComPtr raster_; + ComPtr constants_; +}; + +// Texels of `runtime` that differ from `game`: none for D16, D32 and D32S8 +// (each value written back as read), and none off by more than one step of +// 24 bits for D24S8 (its value is read normalized; most arrive exact). +struct DepthCompare { + std::size_t texels{0}; + std::size_t exact{0}; + std::size_t off_by_one{0}; + std::size_t wrong{0}; + std::string first_wrong; +}; + +DepthCompare CompareDepth(const std::vector& game, const std::vector& runtime, + DXGI_FORMAT family) { + DepthCompare compare; + compare.texels = game.size(); + if (game.size() != runtime.size()) { + compare.wrong = game.size(); + compare.first_wrong = "sizes differ"; + return compare; + } + for (std::size_t i = 0; i < game.size(); ++i) { + if (game[i] == runtime[i]) { + ++compare.exact; + continue; + } + const auto difference = static_cast(game[i]) - static_cast(runtime[i]); + if (family == DXGI_FORMAT_R24G8_TYPELESS && (difference == 1 || difference == -1)) { + ++compare.off_by_one; + continue; + } + if (compare.first_wrong.empty()) { + compare.first_wrong = "texel " + std::to_string(i) + ": game " + std::to_string(game[i]) + ", runtime " + + std::to_string(runtime[i]); + } + ++compare.wrong; + } + return compare; +} + +// Depth reaches the runtime's depth images, value for value (D24S8: within +// one 24-bit step, CompareDepth), every slice, every image its last +// release, with the runtime's images typed or typeless (PimaxXR's and +// Virtual Desktop's are the game's format, opened from Direct3D 11): drawn +// by the game, copied into the twin by Direct3D 11 and drawn into the +// runtime's image by the bridge's queue. +void TestDepthFormats(Game& game) { + GameDepthDraw draw(game); + for (const DepthFormat& format : kDepthFormats) { + for (const bool typeless : {false, true}) { + const std::string name = std::string(format.name) + (typeless ? " (typeless runtime images)" : ""); + D3D11Bridge bridge; + Session session(game.device.Get()); + BeginBridge(game, bridge, session); + ID3D12Device* device = BridgeDevice(bridge); + constexpr std::uint32_t kWidth = 61; // not a multiple of the copy's 8x8 groups + constexpr std::uint32_t kHeight = 45; + MakeRuntimeDepthImages(device, 3, kWidth, kHeight, typeless ? format.typeless : format.format, 2); + const XrSwapchain swapchain = Handle(0x50); + std::string reason; + Check(bridge.AddSwapchain(swapchain, DepthSwapchainInfo(format.format, kWidth, kHeight, 2), FakeEnumerate, + reason), + name + ": depth is accepted: " + reason); + Check(bridge.Bridged(swapchain) && !bridge.Unmirrored(swapchain) && !bridge.HasUnmirrored() && + Contains(bridge.Describe(swapchain), "mode=depth,"), + name + ": depth is mirrored: " + bridge.Describe(swapchain)); + const std::vector images = bridge.Images(swapchain); + Check(images.size() == 3, name + ": one private depth texture per runtime image"); + for (ID3D11Texture2D* image : images) { + D3D11_TEXTURE2D_DESC desc{}; + image->GetDesc(&desc); + Check((desc.BindFlags & D3D11_BIND_DEPTH_STENCIL) && (desc.BindFlags & D3D11_BIND_SHADER_RESOURCE) && + desc.Format == format.typeless && desc.ArraySize == 2, + name + ": the game gets typeless depth textures it can sample"); + } + // Seven frames through three images: each image's last release + // is what its runtime image holds. + std::array, 2>, 3> last; + for (std::uint32_t frame = 0; frame < 7; ++frame) { + const std::uint32_t index = frame % 3; + bridge.Acquired(swapchain, index); + for (std::uint32_t slice = 0; slice < 2; ++slice) { + draw.Draw(game, images[index], format.format, slice, frame); + } + Check(bridge.Released(swapchain), name + ": release " + std::to_string(frame) + " is copied"); + for (std::uint32_t slice = 0; slice < 2; ++slice) { + last[index][slice] = ReadGameDepth(game, images[index], slice); + } + } + Check(bridge.WaitIdle(10000), name + ": the bridge's queue finishes"); + DepthCompare total; + for (std::uint32_t index = 0; index < 3; ++index) { + for (std::uint32_t slice = 0; slice < 2; ++slice) { + const DepthCompare compare = + CompareDepth(last[index][slice], ReadRuntimeDepth(device, index, format.typeless, slice), + format.typeless); + Check(compare.wrong == 0, name + ": runtime image " + std::to_string(index) + " slice " + + std::to_string(slice) + " holds its last release's depth (" + + std::to_string(compare.wrong) + " texels wrong, " + + compare.first_wrong + ")"); + total.texels += compare.texels; + total.exact += compare.exact; + total.off_by_one += compare.off_by_one; + } + } + // The game's own first texels hold the range's ends: both arrive. + Check(last[0][0][0] == 0 && ReadRuntimeDepth(device, 0, format.typeless, 0)[0] == 0, + name + ": depth 0 arrives as 0"); + std::cout << name << ": " << total.exact << "/" << total.texels << " texels exact, " << total.off_by_one + << " one 24-bit step off\n"; + if (format.format != DXGI_FORMAT_D24_UNORM_S8_UINT) { + Check(total.exact == total.texels, name + ": every texel exact"); + } + const std::string summary = bridge.Summary(); + Check(SummaryValue(summary, "depthCopies") == 7 && SummaryValue(summary, "copies") == 7 && + SummaryValue(summary, "skippedCopies") == 0 && SummaryValue(summary, "depthTargetUnbinds") == 0, + name + ": seven depth copies, each drawn: " + summary); + CheckClean(bridge, game, "depth " + name); + Check(bridge.RemoveSwapchain(swapchain), name + ": the draws into a removed swapchain finished"); + bridge.End(); + } + } + Check(GameRuns(game), "the game's device works after the depth formats"); +} + +// A game still holding its depth image as the depth target at release (it +// draws, then releases without unbinding), with compute state of its own +// bound: the copy reads the depth all the same, and the game's depth target +// and compute state are as it left them. +void TestDepthStillBound(Game& game) { + GameDepthDraw draw(game); + D3D11Bridge bridge; + Session session(game.device.Get()); + BeginBridge(game, bridge, session); + ID3D12Device* device = BridgeDevice(bridge); + MakeRuntimeDepthImages(device, 2, 40, 24, DXGI_FORMAT_D32_FLOAT, 1); + const XrSwapchain swapchain = Handle(0x54); std::string reason; - Check(bridge.AddSwapchain(swapchain, info, FakeEnumerate, reason), "depth is accepted: " + reason); - Check(bridge.Bridged(swapchain) && bridge.Unmirrored(swapchain) && bridge.HasUnmirrored(), "depth is not mirrored"); + Check(bridge.AddSwapchain(swapchain, DepthSwapchainInfo(DXGI_FORMAT_D32_FLOAT, 40, 24, 1), FakeEnumerate, reason), + "bridged: " + reason); + ID3D11Texture2D* image = bridge.Images(swapchain)[1]; + + // The game's compute state: a shader, a read and a write of its own. + const char* source = "RWTexture2D o : register(u0); Texture2D i : register(t0);" + "[numthreads(1,1,1)] void CSMain(uint3 id : SV_DispatchThreadID) { o[id.xy] = i[id.xy]; }"; + ComPtr blob; + Check(SUCCEEDED(D3DCompile(source, std::strlen(source), "game_cs", nullptr, nullptr, "CSMain", "cs_5_0", 0, 0, + &blob, nullptr)), + "the game's compute shader compiles"); + ComPtr game_shader; + Check(SUCCEEDED(game.device->CreateComputeShader(blob->GetBufferPointer(), blob->GetBufferSize(), nullptr, + &game_shader)), + "the game's compute shader"); D3D11_TEXTURE2D_DESC desc{}; - bridge.Images(swapchain)[0]->GetDesc(&desc); - Check((desc.BindFlags & D3D11_BIND_DEPTH_STENCIL) && desc.Format == DXGI_FORMAT_R32_TYPELESS, - "the game gets a typeless depth texture"); + desc.Width = 8; + desc.Height = 8; + desc.MipLevels = 1; + desc.ArraySize = 1; + desc.Format = DXGI_FORMAT_R32_FLOAT; + desc.SampleDesc.Count = 1; + desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS; + ComPtr game_read_texture; + ComPtr game_write_texture; + ComPtr game_read; + ComPtr game_write; + Check(SUCCEEDED(game.device->CreateTexture2D(&desc, nullptr, &game_read_texture)) && + SUCCEEDED(game.device->CreateTexture2D(&desc, nullptr, &game_write_texture)) && + SUCCEEDED(game.device->CreateShaderResourceView(game_read_texture.Get(), nullptr, &game_read)) && + SUCCEEDED(game.device->CreateUnorderedAccessView(game_write_texture.Get(), nullptr, &game_write)), + "the game's compute views"); + game.context->CSSetShader(game_shader.Get(), nullptr, 0); + game.context->CSSetShaderResources(0, 1, game_read.GetAddressOf()); + game.context->CSSetUnorderedAccessViews(0, 1, game_write.GetAddressOf(), nullptr); + + bridge.Acquired(swapchain, 1); + draw.Draw(game, image, DXGI_FORMAT_D32_FLOAT, 0, 3, /*keep_bound=*/true); + Check(bridge.Released(swapchain), "the release is copied"); + + ComPtr bound; + game.context->OMGetRenderTargets(0, nullptr, &bound); + ComPtr bound_resource; + if (bound) bound->GetResource(&bound_resource); + Check(bound_resource.Get() == image, "the game's depth target is bound again after the copy"); + ComPtr shader; + game.context->CSGetShader(&shader, nullptr, nullptr); + ComPtr read; + game.context->CSGetShaderResources(0, 1, &read); + ComPtr write; + game.context->CSGetUnorderedAccessViews(0, 1, &write); + Check(shader.Get() == game_shader.Get() && read.Get() == game_read.Get() && write.Get() == game_write.Get(), + "the game's compute shader, read and write are bound as it left them"); + + game.context->OMSetRenderTargets(0, nullptr, nullptr); + const std::vector game_depth = ReadGameDepth(game, image, 0); + Check(bridge.WaitIdle(10000), "the bridge's queue finishes"); + const DepthCompare compare = + CompareDepth(game_depth, ReadRuntimeDepth(device, 1, DXGI_FORMAT_R32_TYPELESS, 0), DXGI_FORMAT_R32_TYPELESS); + Check(compare.exact == compare.texels, "depth still bound at release arrives exact (" + + std::to_string(compare.wrong) + " wrong, " + compare.first_wrong + ")"); + Check(SummaryValue(bridge.Summary(), "depthTargetUnbinds") == 1, "the unbind is counted: " + bridge.Summary()); + // The copy's own time on the game's thread, apart from the handover's. + Check(Contains(bridge.Summary(), ", depthCopyMs=") && Contains(bridge.Summary(), ", handoverMs="), + "the depth copy's time is summarized: " + bridge.Summary()); + game.context->CSSetShader(nullptr, nullptr, 0); + ID3D11ShaderResourceView* no_read = nullptr; + ID3D11UnorderedAccessView* no_write = nullptr; + game.context->CSSetShaderResources(0, 1, &no_read); + game.context->CSSetUnorderedAccessViews(0, 1, &no_write, nullptr); + CheckClean(bridge, game, "depth still bound"); + bridge.End(); +} + +// The game's output-merger and compute bindings, as Direct3D 11 reports them: +// what a depth copy must leave as it found. +struct ContextState { + std::array, D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT> targets; + ComPtr depth_target; + std::array, D3D11_PS_CS_UAV_REGISTER_COUNT> om_writes; + ComPtr shader; + std::array, 8> cs_reads; + std::array, D3D11_PS_CS_UAV_REGISTER_COUNT> cs_writes; +}; + +ContextState ReadContextState(Game& game) { + ContextState state; + ID3D11RenderTargetView* targets[D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT]{}; + ID3D11DepthStencilView* depth_target = nullptr; + ID3D11UnorderedAccessView* om_writes[D3D11_PS_CS_UAV_REGISTER_COUNT]{}; + game.context->OMGetRenderTargetsAndUnorderedAccessViews(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT, targets, + &depth_target, 0, D3D11_PS_CS_UAV_REGISTER_COUNT, + om_writes); + for (std::size_t i = 0; i < state.targets.size(); ++i) state.targets[i].Attach(targets[i]); + state.depth_target.Attach(depth_target); + for (std::size_t i = 0; i < state.om_writes.size(); ++i) state.om_writes[i].Attach(om_writes[i]); + game.context->CSGetShader(&state.shader, nullptr, nullptr); + ID3D11ShaderResourceView* cs_reads[8]{}; + game.context->CSGetShaderResources(0, 8, cs_reads); + for (std::size_t i = 0; i < state.cs_reads.size(); ++i) state.cs_reads[i].Attach(cs_reads[i]); + ID3D11UnorderedAccessView* cs_writes[D3D11_PS_CS_UAV_REGISTER_COUNT]{}; + game.context->CSGetUnorderedAccessViews(0, D3D11_PS_CS_UAV_REGISTER_COUNT, cs_writes); + for (std::size_t i = 0; i < state.cs_writes.size(); ++i) state.cs_writes[i].Attach(cs_writes[i]); + return state; +} + +// Where `after` differs from `before`, slot by slot (empty: nowhere). +std::string StateDifferences(const ContextState& before, const ContextState& after) { + std::string differences; + const auto compare = [&](const char* name, std::size_t slot, const void* was, const void* is) { + if (was != is) { + differences += std::string(name) + "[" + std::to_string(slot) + "] " + (was ? "bound" : "empty") + + " -> " + (is ? (was ? "another view" : "bound") : "empty") + "; "; + } + }; + for (std::size_t i = 0; i < before.targets.size(); ++i) + compare("render target", i, before.targets[i].Get(), after.targets[i].Get()); + compare("depth target", 0, before.depth_target.Get(), after.depth_target.Get()); + for (std::size_t i = 0; i < before.om_writes.size(); ++i) + compare("output-merger UAV", i, before.om_writes[i].Get(), after.om_writes[i].Get()); + compare("compute shader", 0, before.shader.Get(), after.shader.Get()); + for (std::size_t i = 0; i < before.cs_reads.size(); ++i) + compare("compute SRV", i, before.cs_reads[i].Get(), after.cs_reads[i].Get()); + for (std::size_t i = 0; i < before.cs_writes.size(); ++i) + compare("compute UAV", i, before.cs_writes[i].Get(), after.cs_writes[i].Get()); + return differences; +} + +// An append buffer's hidden counter, as the GPU holds it. +std::uint32_t Counter(Game& game, ID3D11UnorderedAccessView* view) { + D3D11_BUFFER_DESC desc{}; + desc.ByteWidth = 16; + desc.Usage = D3D11_USAGE_STAGING; + desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; + ComPtr staging; + Check(SUCCEEDED(game.device->CreateBuffer(&desc, nullptr, &staging)), "a counter readback buffer"); + game.context->CopyStructureCount(staging.Get(), 0, view); + D3D11_MAPPED_SUBRESOURCE mapped{}; + Check(SUCCEEDED(game.context->Map(staging.Get(), 0, D3D11_MAP_READ, 0, &mapped)), "map the counter readback"); + const std::uint32_t value = *static_cast(mapped.pData); + game.context->Unmap(staging.Get(), 0); + return value; +} + +// The game's views for the bindings below: colour targets and unordered- +// access textures of the depth's size (a texture each: views 8 to 11 are +// for compute, apart from the output merger's 0 to 7), and append buffers. +struct GameViews { + std::vector> targets; + std::vector> writes; + std::vector> appends; + std::vector> reads; + ComPtr shader; + ComPtr other_depth; // a depth target that is not the swapchain's + + GameViews(Game& game, std::uint32_t width, std::uint32_t height) { + D3D11_TEXTURE2D_DESC desc{}; + desc.Width = width; + desc.Height = height; + desc.MipLevels = 1; + desc.ArraySize = 1; + desc.Format = DXGI_FORMAT_R32_FLOAT; + desc.SampleDesc.Count = 1; + desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_SHADER_RESOURCE; + for (int i = 0; i < 12; ++i) { + ComPtr texture; + ComPtr target; + ComPtr write; + ComPtr read; + Check(SUCCEEDED(game.device->CreateTexture2D(&desc, nullptr, &texture)) && + SUCCEEDED(game.device->CreateRenderTargetView(texture.Get(), nullptr, &target)) && + SUCCEEDED(game.device->CreateUnorderedAccessView(texture.Get(), nullptr, &write)) && + SUCCEEDED(game.device->CreateShaderResourceView(texture.Get(), nullptr, &read)), + "the game's views"); + targets.push_back(target); + writes.push_back(write); + reads.push_back(read); + } + for (int i = 0; i < 2; ++i) { + D3D11_BUFFER_DESC buffer{}; + buffer.ByteWidth = 64 * 4; + buffer.BindFlags = D3D11_BIND_UNORDERED_ACCESS; + buffer.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED; + buffer.StructureByteStride = 4; + D3D11_UNORDERED_ACCESS_VIEW_DESC view{}; + view.Format = DXGI_FORMAT_UNKNOWN; + view.ViewDimension = D3D11_UAV_DIMENSION_BUFFER; + view.Buffer.NumElements = 64; + view.Buffer.Flags = D3D11_BUFFER_UAV_FLAG_APPEND; + ComPtr resource; + ComPtr append; + Check(SUCCEEDED(game.device->CreateBuffer(&buffer, nullptr, &resource)) && + SUCCEEDED(game.device->CreateUnorderedAccessView(resource.Get(), &view, &append)), + "the game's append buffers"); + appends.push_back(append); + } + const char* source = "AppendStructuredBuffer o : register(u0); Texture2D i : register(t0);" + "[numthreads(1,1,1)] void CSMain() { o.Append(asuint(i[uint2(0,0)])); }"; + ComPtr blob; + Check(SUCCEEDED(D3DCompile(source, std::strlen(source), "game_cs", nullptr, nullptr, "CSMain", "cs_5_0", 0, 0, + &blob, nullptr)) && + SUCCEEDED(game.device->CreateComputeShader(blob->GetBufferPointer(), blob->GetBufferSize(), nullptr, + &shader)), + "the game's compute shader"); + D3D11_TEXTURE2D_DESC depth = desc; + depth.Format = DXGI_FORMAT_D32_FLOAT; + depth.BindFlags = D3D11_BIND_DEPTH_STENCIL; + ComPtr depth_texture; + Check(SUCCEEDED(game.device->CreateTexture2D(&depth, nullptr, &depth_texture)) && + SUCCEEDED(game.device->CreateDepthStencilView(depth_texture.Get(), nullptr, &other_depth)), + "the game's other depth target"); + } +}; + +// Unbinds everything the cases below bind. +void ClearContextState(Game& game) { + game.context->OMSetRenderTargetsAndUnorderedAccessViews(0, nullptr, nullptr, 0, 0, nullptr, nullptr); + game.context->CSSetShader(nullptr, nullptr, 0); + ID3D11ShaderResourceView* no_reads[8]{}; + ID3D11UnorderedAccessView* no_writes[D3D11_PS_CS_UAV_REGISTER_COUNT]{}; + game.context->CSSetShaderResources(0, 8, no_reads); + game.context->CSSetUnorderedAccessViews(0, D3D11_PS_CS_UAV_REGISTER_COUNT, no_writes, nullptr); +} + +// The game's output merger at a release, its depth image still bound or not, +// beside render targets (sparse ones too) and pixel-shader unordered-access +// views in the slots past them (an append buffer's hidden counter among +// them), with compute state of its own (another append buffer in the copy's +// slot): after the copy every binding is in its slot as the game left it, +// both counters hold their values, and the depth arrives all the same. A +// copy that set all eight render target slots again would unbind every +// one of those views; a depth swapchain the bridge keeps private changes +// nothing either. +void TestDepthKeepsGameState(Game& game) { + constexpr std::uint32_t kWidth = 40; + constexpr std::uint32_t kHeight = 24; + GameDepthDraw draw(game); + GameViews views(game, kWidth, kHeight); + struct Case { + const char* name; + std::vector targets; // render target slots: a view, or -1 for none + bool depth_bound; // the swapchain's depth target (else `other_depth`, or none) + bool other_depth; + UINT write_start; + std::vector writes; // output-merger UAVs from write_start: a view, -1 none, 100 + n append n + }; + const Case cases[] = { + {"no render targets, UAV 1", {}, true, false, 1, {0}}, + {"no render targets, UAV 0", {}, true, false, 0, {0}}, + {"render target 0, UAV 1", {0}, true, false, 1, {1}}, + {"sparse render targets 0 and 2, UAVs 3 and 5", {0, -1, 2}, true, false, 3, {3, -1, 4}}, + {"render target 0, UAVs 1 to 4 with an append buffer", {0}, true, false, 1, {1, 100, 2, 3}}, + {"render targets 0 to 6, UAV 7", {0, 1, 2, 3, 4, 5, 6}, true, false, 7, {7}}, + {"depth already unbound, render target 0, UAV 1", {0}, false, false, 1, {1}}, + {"another depth target, UAV 1", {}, false, true, 1, {1}}, + }; + for (const bool mirrored : {true, false}) { + if (!mirrored) _putenv_s("VECTORXR_D3D11_BRIDGE_DEPTH", "private"); + D3D11Bridge bridge; + Session session(game.device.Get()); + BeginBridge(game, bridge, session); + if (!mirrored) _putenv_s("VECTORXR_D3D11_BRIDGE_DEPTH", ""); + ID3D12Device* device = BridgeDevice(bridge); + MakeRuntimeDepthImages(device, 2, kWidth, kHeight, DXGI_FORMAT_D32_FLOAT, 1); + const XrSwapchain swapchain = Handle(0x55); + std::string reason; + Check(bridge.AddSwapchain(swapchain, DepthSwapchainInfo(DXGI_FORMAT_D32_FLOAT, kWidth, kHeight, 1), + FakeEnumerate, reason), + "bridged: " + reason); + Check(bridge.Unmirrored(swapchain) == !mirrored, "the depth is mirrored or private as asked"); + std::uint64_t unbinds = 0; + std::uint32_t frame = 0; + for (const Case& test : cases) { + const std::string name = std::string(mirrored ? "" : "private depth: ") + test.name; + const std::uint32_t index = frame % 2; + ID3D11Texture2D* image = bridge.Images(swapchain)[index]; + bridge.Acquired(swapchain, index); + draw.Draw(game, image, DXGI_FORMAT_D32_FLOAT, 0, frame++, /*keep_bound=*/true); + ComPtr depth_target; + game.context->OMGetRenderTargets(0, nullptr, &depth_target); + ClearContextState(game); + + std::vector targets; + for (const int target : test.targets) targets.push_back(target < 0 ? nullptr : views.targets[target].Get()); + std::vector writes; + std::vector counts; + ID3D11UnorderedAccessView* om_append = nullptr; + for (const int write : test.writes) { + if (write >= 100) { + om_append = views.appends[0].Get(); + writes.push_back(om_append); + counts.push_back(5); + } else { + writes.push_back(write < 0 ? nullptr : views.writes[write].Get()); + counts.push_back(0); + } + } + ID3D11DepthStencilView* bound_depth = test.depth_bound ? depth_target.Get() + : test.other_depth ? views.other_depth.Get() + : nullptr; + game.context->OMSetRenderTargetsAndUnorderedAccessViews( + static_cast(targets.size()), targets.empty() ? nullptr : targets.data(), bound_depth, + test.write_start, static_cast(writes.size()), writes.data(), counts.data()); + // Compute state of the game's own: its shader, reads in slots 0 + // and 3, an append buffer in slot 0 (the copy's) and a write in 2. + const UINT cs_count = 7; + ID3D11UnorderedAccessView* cs_append = views.appends[1].Get(); + game.context->CSSetShader(views.shader.Get(), nullptr, 0); + game.context->CSSetShaderResources(0, 1, views.reads[8].GetAddressOf()); + game.context->CSSetShaderResources(3, 1, views.reads[9].GetAddressOf()); + game.context->CSSetUnorderedAccessViews(0, 1, &cs_append, &cs_count); + game.context->CSSetUnorderedAccessViews(2, 1, views.writes[10].GetAddressOf(), nullptr); + + const ContextState before = ReadContextState(game); + // Every view went where it was asked to (the case is the state it names). + for (std::size_t i = 0; i < targets.size(); ++i) + Check(before.targets[i].Get() == targets[i], name + ": render target " + std::to_string(i) + " bound"); + for (std::size_t i = 0; i < writes.size(); ++i) + Check(before.om_writes[test.write_start + i].Get() == writes[i], + name + ": UAV " + std::to_string(test.write_start + i) + " bound"); + Check(before.depth_target.Get() == bound_depth, name + ": depth target as asked"); + + bridge.Released(swapchain); + const ContextState after = ReadContextState(game); + const std::string differences = StateDifferences(before, after); + Check(differences.empty(), name + ": the game's bindings are as it left them: " + differences); + if (om_append) { + Check(Counter(game, om_append) == 5, name + ": the output merger's append counter keeps its value (" + + std::to_string(Counter(game, om_append)) + ")"); + } + Check(Counter(game, cs_append) == 7, + name + ": the compute append counter keeps its value (" + std::to_string(Counter(game, cs_append)) + ")"); + unbinds += mirrored && test.depth_bound ? 1 : 0; + Check(SummaryValue(bridge.Summary(), "depthTargetUnbinds") == unbinds, + name + ": the depth target is unbound only while it is the image: " + bridge.Summary()); + + ClearContextState(game); + if (mirrored) { + const std::vector game_depth = ReadGameDepth(game, image, 0); + Check(bridge.WaitIdle(10000), name + ": the bridge's queue finishes"); + const DepthCompare compare = CompareDepth( + game_depth, ReadRuntimeDepth(device, index, DXGI_FORMAT_R32_TYPELESS, 0), DXGI_FORMAT_R32_TYPELESS); + Check(compare.exact == compare.texels, name + ": the depth arrives exact (" + + std::to_string(compare.wrong) + " wrong, " + + compare.first_wrong + ")"); + } + } + const std::string summary = bridge.Summary(); + Check(SummaryValue(summary, "depthCopies") == (mirrored ? std::size(cases) : 0), + std::string(mirrored ? "" : "private depth: ") + "one copy a release: " + summary); + Check(bridge.WaitIdle(10000), "the bridge's queue finishes"); + CheckClean(bridge, game, mirrored ? "depth keeps the game's state" : "private depth keeps the game's state"); + bridge.End(); + } + Check(GameRuns(game), "the game's device works after its state was kept"); +} + +// Another thread of the game's, with multithread protection on, issues its +// own compute work while a depth copy has its state bound: it runs only once +// the copy has put the game's state back (it never sees the copy's shader), +// and what it binds stays bound (the copy's restore does not overwrite it). +// Each Direct3D 11 call being serialized on its own would let it in between. +// `game_protected` false: the game never turned protection on, and the other +// thread is a render thread that never calls OpenXR (the bridge sees one +// thread only): mirroring a depth swapchain turns it on, says so once, and +// the other thread is kept out all the same. Without that, its calls ran +// inside the copy (protection stayed off). +void TestDepthCopyHoldsContext(Game& game, bool game_protected = true) { + const std::string name = game_protected ? "depth copy holds the context" + : "depth copy holds the context from a thread that never calls OpenXR"; + ComPtr multithread; + Check(SUCCEEDED(game.context.As(&multithread)), "the game's context has ID3D11Multithread"); + multithread->SetMultithreadProtected(game_protected ? TRUE : FALSE); + GameDepthDraw draw(game); + GameViews views(game, 32, 32); + D3D11Bridge bridge; + Session session(game.device.Get()); + BeginBridge(game, bridge, session); + ID3D12Device* device = BridgeDevice(bridge); + MakeRuntimeDepthImages(device, 2, 32, 32, DXGI_FORMAT_D32_FLOAT, 1); + const XrSwapchain swapchain = Handle(0x56); + std::string reason; + Check(bridge.AddSwapchain(swapchain, DepthSwapchainInfo(DXGI_FORMAT_D32_FLOAT, 32, 32, 1), FakeEnumerate, reason), + "bridged: " + reason); + ID3D11Texture2D* image = bridge.Images(swapchain)[0]; + + // The game's other compute shader, the other thread's. + const char* source = "RWTexture2D o : register(u0);" + "[numthreads(1,1,1)] void CSMain(uint3 id : SV_DispatchThreadID) { o[id.xy] = 42.0; }"; + ComPtr blob; + ComPtr other_shader; + Check(SUCCEEDED(D3DCompile(source, std::strlen(source), "other_cs", nullptr, nullptr, "CSMain", "cs_5_0", 0, 0, + &blob, nullptr)) && + SUCCEEDED(game.device->CreateComputeShader(blob->GetBufferPointer(), blob->GetBufferSize(), nullptr, + &other_shader)), + "the other thread's compute shader"); + game.context->CSSetShader(views.shader.Get(), nullptr, 0); + + const HANDLE inside = CreateEventW(nullptr, TRUE, FALSE, nullptr); + std::atomic copy_bound{false}; // the copy's state is bound (between the hook's start and its end) + std::atomic other_ran_inside{false}; + ComPtr other_saw; + bridge.SetDepthCopyHookForTest([&] { + copy_bound = true; + SetEvent(inside); + // Long enough for the other thread's calls to land here, were they let in. + Sleep(200); + copy_bound = false; + }); + std::thread other([&] { + WaitForSingleObject(inside, 10000); + // With the copy's state bound, a call let in reads the copy's shader. + game.context->CSGetShader(&other_saw, nullptr, nullptr); + other_ran_inside = other_ran_inside || copy_bound; + game.context->CSSetShader(other_shader.Get(), nullptr, 0); + game.context->CSSetUnorderedAccessViews(0, 1, views.writes[0].GetAddressOf(), nullptr); + other_ran_inside = other_ran_inside || copy_bound; + game.context->Dispatch(1, 1, 1); + }); bridge.Acquired(swapchain, 0); - bridge.Released(swapchain); - Check(SummaryValue(bridge.Summary(), "copies") == 0, "no copies of depth: " + bridge.Summary()); - Check(bridge.RemoveSwapchain(swapchain), "depth removes at once"); - Check(!bridge.HasUnmirrored(), "no depth once removed"); - CheckClean(bridge, game, "depth"); + draw.Draw(game, image, DXGI_FORMAT_D32_FLOAT, 0, 1); + Check(bridge.Released(swapchain), "the release is copied"); + other.join(); + CloseHandle(inside); + bridge.SetDepthCopyHookForTest(nullptr); + + Check(!other_ran_inside, name + ": the other thread's calls wait for the copy to put the game's state back"); + Check(other_saw.Get() == views.shader.Get(), name + ": the other thread sees the game's own compute shader"); + // Checked after the interleaving, which shows what protection being off would let in. + Check(multithread->GetMultithreadProtected() != FALSE, + name + ": a mirrored depth swapchain leaves multithread protection on"); + Check(Contains(bridge.Describe(swapchain), + game_protected ? "multithreadProtection=on" : "multithreadProtection=turned on"), + name + ": the swapchain's description says so: " + bridge.Describe(swapchain)); + Check(g_log.empty(), name + ": nothing to report"); + Check(bridge.AddSwapchain(Handle(0x57), DepthSwapchainInfo(DXGI_FORMAT_D32_FLOAT, 32, 32, 1), FakeEnumerate, + reason) && + Contains(bridge.Describe(Handle(0x57)), "multithreadProtection=on") && + !Contains(bridge.Describe(Handle(0x57)), "turned on"), + name + ": a second depth swapchain finds it on: " + bridge.Describe(Handle(0x57))); + Check(Contains(bridge.Summary(), "secondThread=no, multithreadProtected=yes"), name + ": " + bridge.Summary()); + ComPtr shader; + game.context->CSGetShader(&shader, nullptr, nullptr); + ComPtr write; + game.context->CSGetUnorderedAccessViews(0, 1, &write); + Check(shader.Get() == other_shader.Get() && write.Get() == views.writes[0].Get(), + "what the other thread bound stays bound"); + + ClearContextState(game); + const std::vector game_depth = ReadGameDepth(game, image, 0); + Check(bridge.WaitIdle(10000), "the bridge's queue finishes"); + const DepthCompare compare = + CompareDepth(game_depth, ReadRuntimeDepth(device, 0, DXGI_FORMAT_R32_TYPELESS, 0), DXGI_FORMAT_R32_TYPELESS); + Check(compare.exact == compare.texels, "the depth arrives exact beside the other thread's work (" + + std::to_string(compare.wrong) + " wrong, " + compare.first_wrong + ")"); + g_log.clear(); + CheckClean(bridge, game, name); bridge.End(); + multithread->SetMultithreadProtected(FALSE); +} + +// A depth image released again before the bridge's queue drew its last +// release: Direct3D 11 waits for that draw before writing the twin again, so +// the runtime gets each release in turn and the last one at the end. +void TestDepthDoneWait(Game& game) { + GameDepthDraw draw(game); + D3D11Bridge bridge; + Session session(game.device.Get()); + BeginBridge(game, bridge, session); + ID3D12Device* device = BridgeDevice(bridge); + const std::uint32_t width = 2048; // large, so a draw is still running at the next release + const std::uint32_t height = 2048; + MakeRuntimeDepthImages(device, 1, width, height, DXGI_FORMAT_D16_UNORM, 1); + const XrSwapchain swapchain = Handle(0x58); + std::string reason; + Check(bridge.AddSwapchain(swapchain, DepthSwapchainInfo(DXGI_FORMAT_D16_UNORM, width, height, 1), FakeEnumerate, + reason), + "bridged: " + reason); + ID3D11Texture2D* image = bridge.Images(swapchain)[0]; + for (std::uint32_t frame = 0; frame < 6; ++frame) { + bridge.Acquired(swapchain, 0); + draw.Draw(game, image, DXGI_FORMAT_D16_UNORM, 0, frame); + Check(bridge.Released(swapchain), "release " + std::to_string(frame) + " is copied"); + } + const std::vector game_depth = ReadGameDepth(game, image, 0); + Check(bridge.WaitIdle(10000), "the bridge's queue finishes"); + const DepthCompare compare = + CompareDepth(game_depth, ReadRuntimeDepth(device, 0, DXGI_FORMAT_R16_TYPELESS, 0), DXGI_FORMAT_R16_TYPELESS); + Check(compare.exact == compare.texels, "the last release's depth arrives exact (" + std::to_string(compare.wrong) + + " wrong, " + compare.first_wrong + ")"); + std::cout << "depth done waits (releases whose twin was still being drawn): " << bridge.Summary() << '\n'; + // D16's twin is R16_UNORM: 2048x2048 at two bytes a texel. + Check(SummaryValue(bridge.Summary(), "liveTwinMiB") == 8, "a D16 twin takes two bytes a texel: " + bridge.Summary()); + CheckClean(bridge, game, "depth done wait"); + bridge.End(); +} + +// Depth the bridge cannot mirror stays private (no copies; the caller learns +// to leave it out): multisampled, runtime images that are no depth targets +// (reported), and VECTORXR_D3D11_BRIDGE_DEPTH=private (not reported). +void TestDepthPrivate(Game& game) { + const auto check_private = [&](const std::string& name, const XrSwapchainCreateInfo& info, bool depth_target, + const std::string& why, bool reported) { + D3D11Bridge bridge; + Session session(game.device.Get()); + BeginBridge(game, bridge, session); + MakeRuntimeDepthImages(BridgeDevice(bridge), 2, 32, 32, static_cast(info.format), 1, depth_target); + const XrSwapchain swapchain = Handle(0x5c); + std::string reason; + Check(bridge.AddSwapchain(swapchain, info, FakeEnumerate, reason), name + ": depth is accepted: " + reason); + Check(bridge.Bridged(swapchain) && bridge.Unmirrored(swapchain) && bridge.HasUnmirrored(), + name + ": depth is not mirrored"); + Check(Contains(bridge.Describe(swapchain), "mode=private-depth (" + why), + name + ": the swapchain says why: " + bridge.Describe(swapchain)); + D3D11_TEXTURE2D_DESC desc{}; + bridge.Images(swapchain)[0]->GetDesc(&desc); + Check((desc.BindFlags & D3D11_BIND_DEPTH_STENCIL) && desc.Format == DXGI_FORMAT_R32_TYPELESS, + name + ": the game gets a typeless depth texture"); + bridge.Acquired(swapchain, 0); + bridge.Released(swapchain); + Check(SummaryValue(bridge.Summary(), "copies") == 0 && SummaryValue(bridge.Summary(), "depthCopies") == 0, + name + ": no copies of depth: " + bridge.Summary()); + Check(g_log.size() == (reported ? 1u : 0u) && (!reported || Contains(g_log[0], "stays the game's own")), + name + ": " + (reported ? "reported once" : "not reported")); + g_log.clear(); + Check(bridge.RemoveSwapchain(swapchain), name + ": depth removes at once"); + Check(!bridge.HasUnmirrored(), name + ": no depth once removed"); + CheckClean(bridge, game, "depth " + name); + bridge.End(); + }; + check_private("multisampled", DepthSwapchainInfo(DXGI_FORMAT_D32_FLOAT, 32, 32, 1, 4), true, "multisampled", true); + check_private("no depth target", DepthSwapchainInfo(DXGI_FORMAT_D32_FLOAT, 32, 32, 1), false, + "the runtime's depth image is not a depth target", true); + _putenv_s("VECTORXR_D3D11_BRIDGE_DEPTH", "private"); + check_private("switched off", DepthSwapchainInfo(DXGI_FORMAT_D32_FLOAT, 32, 32, 1), true, + "VECTORXR_D3D11_BRIDGE_DEPTH=private", false); + _putenv_s("VECTORXR_D3D11_BRIDGE_DEPTH", ""); +} + +// Frame Generation's own swapchains' images (its Track's enumerations), by swapchain. +std::map> g_framegen_images; + +XrResult XRAPI_CALL FrameGenEnumerate(XrSwapchain swapchain, uint32_t capacity, uint32_t* count, + XrSwapchainImageBaseHeader* images) { + const std::vector& list = g_framegen_images[swapchain]; + *count = static_cast(list.size()); + if (capacity == 0) return XR_SUCCESS; + if (capacity < list.size()) return XR_ERROR_SIZE_INSUFFICIENT; + auto* d3d12_images = reinterpret_cast(images); + for (std::size_t i = 0; i < list.size(); ++i) d3d12_images[i].texture = list[i]; + return XR_SUCCESS; +} + +// The raw depth `encoding` stores for `meters` (1/z linear from minDepth at +// nearZ to maxDepth at farZ, either of them infinitely far). +float RawDepth(const depthxr::FrameGenDepthEncoding& encoding, double meters) { + const auto inverse = [](double z) { return std::isfinite(z) && z > 0.0 ? 1.0 / z : 0.0; }; + const double near_inverse = inverse(encoding.near_z); + const double normalized = (1.0 / meters - near_inverse) / (inverse(encoding.far_z) - near_inverse); + return static_cast(encoding.min_depth + normalized * (encoding.max_depth - encoding.min_depth)); +} + +// Frame Generation reads a bridged game's depth: the game clears its depth to +// a plane `meters` away as its encoding stores it, the bridge mirrors it into +// the runtime's image at release, and Frame Generation's Direct3D 12 backend +// (on the bridge's device and queue, as in a session) captures it there and +// decodes it to that distance. Its depth check is locked to the depth as +// described: what it decides is the backend's own tests', not the bridge's. +void TestDepthReachesFrameGen(Game& game) { + struct Case { + const char* name; + DXGI_FORMAT format; + depthxr::FrameGenDepthEncoding encoding; + double meters; + double tolerance; // relative + }; + depthxr::FrameGenDepthEncoding uevr; // reversed, infinitely far plane at 0, near plane 10 cm at 1 + uevr.near_z = std::numeric_limits::max(); + uevr.far_z = 0.1f; + depthxr::FrameGenDepthEncoding iracing; // standard, 5 cm to 1 km + iracing.near_z = 0.05f; + iracing.far_z = 1000.0f; + depthxr::FrameGenDepthEncoding reversed; // reversed and finite + reversed.near_z = 2000.0f; + reversed.far_z = 0.2f; + const Case cases[] = { + {"UEVR (D32S8, reversed, infinite)", DXGI_FORMAT_D32_FLOAT_S8X24_UINT, uevr, 2.0, 1e-3}, + {"iRacing (D16, standard)", DXGI_FORMAT_D16_UNORM, iracing, 5.0, 0.02}, + {"D24S8 (reversed, finite)", DXGI_FORMAT_D24_UNORM_S8_UINT, reversed, 12.0, 1e-3}, + }; + constexpr std::uint32_t kSize = 64; + for (const Case& test : cases) { + D3D11Bridge bridge; + Session session(game.device.Get()); + BeginBridge(game, bridge, session); + ID3D12Device* device = BridgeDevice(bridge); + std::string error; + auto gpu = depthxr::CreateFrameGenGpu(bridge.Binding(), error); + Check(gpu != nullptr && gpu->SupportsDepth(), std::string(test.name) + ": Frame Generation's backend: " + error); + gpu->LockDepthForTest(depthxr::FrameGenDepthReading::kAsSubmitted, false, 1.0f); + + // The runtime's depth swapchain (the bridge's), a colour source and + // Frame Generation's output. + MakeRuntimeDepthImages(device, 2, kSize, kSize, test.format, 1); + const XrSwapchain depth = Handle(0x70); + std::string reason; + Check(bridge.AddSwapchain(depth, DepthSwapchainInfo(test.format, kSize, kSize, 1), FakeEnumerate, reason), + std::string(test.name) + ": bridged: " + reason); + std::vector> runtime_depth = g_runtime_images; + MakeRuntimeImages(device, 1, kSize, kSize, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB); + std::vector> source = g_runtime_images; + const std::int64_t output_format = gpu->OutputFormats(depthxr::FrameGenOutputKind::kSrgb8).front(); + MakeRuntimeImages(device, 1, kSize, kSize, static_cast(output_format)); + std::vector> output = g_runtime_images; + const XrSwapchain source_chain = Handle(0x71); + const XrSwapchain output_chain = Handle(0x72); + g_framegen_images = {{depth, {runtime_depth[0].Get(), runtime_depth[1].Get()}}, + {source_chain, {source[0].Get()}}, + {output_chain, {output[0].Get()}}}; + for (const XrSwapchain chain : {depth, source_chain, output_chain}) { + Check(gpu->Track(chain, FrameGenEnumerate, error), std::string(test.name) + ": track: " + error); + } + + const XrRect2Di rect{{0, 0}, {static_cast(kSize), static_cast(kSize)}}; + depthxr::FrameGenViewSubmission view; + view.source = {source_chain, 0, 0, rect, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB}; + view.outputs = {{output_chain, 0, 0, rect, output_format}}; + view.request.extent = {kSize, kSize}; + view.request.output = depthxr::FrameGenOutputKind::kSrgb8; + for (auto* geometry : {&view.request.newest, &view.request.previous}) { + geometry->angle_left = geometry->angle_down = -std::atan(1.0f); + geometry->angle_right = geometry->angle_up = std::atan(1.0f); + } + depthxr::FrameGenTuning tuning; + depthxr::FrameGenExecuteResult result; + // A first frame sets the view up: nothing was captured for it, so it + // is not generated from ("not captured when the game released it"). + const bool primed = gpu->Execute({view}, tuning, result, error); + Check(!primed, std::string(test.name) + ": the first frame only sets the view up"); + // The game's frames: colour captured at release, its depth cleared + // to the plane, released (the bridge's draw), then captured as the + // layer captures a release; generated from once a frame came before. + const float raw = RawDepth(test.encoding, test.meters); + view.request.depth = true; + view.request.synthesize = true; + for (std::uint32_t frame = 0; frame < 3; ++frame) { + const bool colour = gpu->Capture(0, view.source, view.request, tuning.quality, error); + Check(colour, std::string(test.name) + ": colour captured at release: " + error); + const std::uint32_t index = frame % 2; + bridge.Acquired(depth, index); + D3D11_DEPTH_STENCIL_VIEW_DESC target_desc{}; + target_desc.Format = test.format; + target_desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DARRAY; + target_desc.Texture2DArray.ArraySize = 1; + ComPtr target; + Check(SUCCEEDED(game.device->CreateDepthStencilView(bridge.Images(depth)[index], &target_desc, &target)), + "a depth view"); + game.context->ClearDepthStencilView(target.Get(), D3D11_CLEAR_DEPTH, raw, 0); + Check(bridge.Released(depth), std::string(test.name) + ": the release is copied"); + const bool captured = + gpu->CaptureDepth(0, {depth, index, 0, rect, test.format}, test.encoding, view.request, error); + Check(captured, std::string(test.name) + ": Frame Generation captures the runtime's depth: " + error); + const bool executed = gpu->Execute({view}, tuning, result, error); + Check(executed, std::string(test.name) + ": execute: " + error); + } + Check(result.depth.size() == 1 && result.depth[0], std::string(test.name) + ": the frame used the depth"); + // UEVR places its depth rect by its colour image's size: in a smaller + // depth image (an upscaler's internal resolution) it reaches past + // the image, and is refused rather than read as zeros. + const XrRect2Di past{{static_cast(kSize / 2), 0}, {static_cast(kSize), static_cast(kSize)}}; + const bool outside = gpu->CaptureDepth(0, {depth, 0, 0, past, test.format}, test.encoding, view.request, error); + Check(!outside && Contains(error, "lies outside its 64x64 image"), + std::string(test.name) + ": a depth rect past its image is refused: " + error); + + std::vector linear; + depthxr::FrameGenExtent extent{}; + std::uint32_t channels = 0; + Check(gpu->ReadTexture(0, {depthxr::FrameGenTextureKind::kDepth, 0}, linear, extent, channels) && + channels == 1 && !linear.empty(), + std::string(test.name) + ": Frame Generation's linear depth read back"); + double worst = 0.0; + for (const float meters : linear) { + worst = std::max(worst, std::abs(meters - test.meters) / test.meters); + } + std::cout << test.name << ": raw " << raw << " decoded to " << linear[linear.size() / 2] << " m (plane " + << test.meters << " m, worst " << worst * 100.0 << "% off over " << extent.width << "x" + << extent.height << ")\n"; + Check(worst <= test.tolerance, std::string(test.name) + ": Frame Generation decodes the bridged depth to " + + std::to_string(linear[linear.size() / 2]) + " m, the plane is " + + std::to_string(test.meters) + " m"); + + Check(gpu->WaitIdle(), std::string(test.name) + ": Frame Generation's work finishes"); + for (const XrSwapchain chain : {depth, source_chain, output_chain}) gpu->Forget(chain); + gpu.reset(); + Check(bridge.WaitIdle(10000), std::string(test.name) + ": the bridge's queue finishes"); + CheckClean(bridge, game, std::string("depth to Frame Generation ") + test.name); + Check(bridge.RemoveSwapchain(depth), std::string(test.name) + ": removed"); + bridge.End(); + } + g_framegen_images.clear(); +} + +// BRIDGE_TEST_DEPTH_PERF=1 (`hw`): what mirroring a game's depth costs at +// games' sizes, on the GPU, per release: the Direct3D 11 copy into the twin +// (timestamps around the release on the game's context), and the time from +// just before the release to the runtime's image drawn on the bridge's queue +// (which waits for that copy first). Nothing is held back: each release +// finishes before the next. +void MeasureDepthCost(Game& game) { + struct Size { + const char* name; + DXGI_FORMAT format; + std::uint32_t width, height, slices; + }; + const Size sizes[] = { + {"iRacing (D16 6904x3184)", DXGI_FORMAT_D16_UNORM, 6904, 3184, 1}, + {"iRacing's size as D32 (6904x3184)", DXGI_FORMAT_D32_FLOAT, 6904, 3184, 1}, + {"UEVR double wide (D32S8 5120x2560)", DXGI_FORMAT_D32_FLOAT_S8X24_UINT, 5120, 2560, 1}, + {"two slices (D24S8 2x2560x2560)", DXGI_FORMAT_D24_UNORM_S8_UINT, 2560, 2560, 2}, + }; + for (const Size& size : sizes) { + D3D11Bridge bridge; + Session session(game.device.Get()); + BeginBridge(game, bridge, session); + ID3D12Device* device = BridgeDevice(bridge); + ID3D12CommandQueue* queue = BridgeQueue(bridge); + MakeRuntimeDepthImages(device, 3, size.width, size.height, size.format, size.slices); + const XrSwapchain swapchain = Handle(0x80); + std::string reason; + Check(bridge.AddSwapchain(swapchain, DepthSwapchainInfo(size.format, size.width, size.height, size.slices), + FakeEnumerate, reason), + std::string(size.name) + ": bridged: " + reason); + const std::vector images = bridge.Images(swapchain); + + D3D11_QUERY_DESC disjoint_desc{D3D11_QUERY_TIMESTAMP_DISJOINT}; + D3D11_QUERY_DESC stamp_desc{D3D11_QUERY_TIMESTAMP}; + ComPtr disjoint, before11, after11; + Check(SUCCEEDED(game.device->CreateQuery(&disjoint_desc, &disjoint)) && + SUCCEEDED(game.device->CreateQuery(&stamp_desc, &before11)) && + SUCCEEDED(game.device->CreateQuery(&stamp_desc, &after11)), + "Direct3D 11 timestamps"); + D3D12_QUERY_HEAP_DESC heap_desc{D3D12_QUERY_HEAP_TYPE_TIMESTAMP, 2, 0}; + ComPtr stamps; + ComPtr stamp_buffer; + D3D12_HEAP_PROPERTIES readback{D3D12_HEAP_TYPE_READBACK}; + D3D12_RESOURCE_DESC buffer_desc{}; + buffer_desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; + buffer_desc.Width = 16; + buffer_desc.Height = buffer_desc.DepthOrArraySize = buffer_desc.MipLevels = 1; + buffer_desc.SampleDesc.Count = 1; + buffer_desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; + ComPtr allocators[2]; + ComPtr lists[2]; + Check(SUCCEEDED(device->CreateQueryHeap(&heap_desc, IID_PPV_ARGS(&stamps))) && + SUCCEEDED(device->CreateCommittedResource(&readback, D3D12_HEAP_FLAG_NONE, &buffer_desc, + D3D12_RESOURCE_STATE_COPY_DEST, nullptr, + IID_PPV_ARGS(&stamp_buffer))), + "Direct3D 12 timestamps"); + for (int i = 0; i < 2; ++i) { + Check(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocators[i]))) && + SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocators[i].Get(), nullptr, + IID_PPV_ARGS(&lists[i]))), + "timestamp commands"); + lists[i]->EndQuery(stamps.Get(), D3D12_QUERY_TYPE_TIMESTAMP, i); + if (i == 1) lists[i]->ResolveQueryData(stamps.Get(), D3D12_QUERY_TYPE_TIMESTAMP, 0, 2, stamp_buffer.Get(), 0); + lists[i]->Close(); + } + UINT64 frequency12 = 0; + queue->GetTimestampFrequency(&frequency12); + + // The GPU's clocks up first: an idle GPU's first milliseconds of work + // run several times slower. + for (int release = 0; release < 90; ++release) { + bridge.Acquired(swapchain, static_cast(release) % 3); + Check(bridge.Released(swapchain), std::string(size.name) + ": warmed"); + } + Check(bridge.WaitIdle(10000) && GameRuns(game), std::string(size.name) + ": warm-up finished"); + + std::vector copy_ms, ready_ms; + for (int iteration = 0; iteration < 40; ++iteration) { + const std::uint32_t index = static_cast(iteration) % 3; + bridge.Acquired(swapchain, index); + for (std::uint32_t slice = 0; slice < size.slices; ++slice) { + D3D11_DEPTH_STENCIL_VIEW_DESC view{}; + view.Format = size.format; + view.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DARRAY; + view.Texture2DArray.FirstArraySlice = slice; + view.Texture2DArray.ArraySize = 1; + ComPtr target; + Check(SUCCEEDED(game.device->CreateDepthStencilView(images[index], &view, &target)), "a depth view"); + game.context->ClearDepthStencilView(target.Get(), D3D11_CLEAR_DEPTH, 0.25f + 0.01f * iteration, 0); + } + Check(GameRuns(game), "the game's clear finishes"); + game.context->Begin(disjoint.Get()); + game.context->End(before11.Get()); + ID3D12CommandList* first[] = {lists[0].Get()}; + queue->ExecuteCommandLists(1, first); + Check(bridge.Released(swapchain), std::string(size.name) + ": copied"); + game.context->End(after11.Get()); + game.context->End(disjoint.Get()); + game.context->Flush(); // the end stamp right after the copy, not at a later flush + ID3D12CommandList* second[] = {lists[1].Get()}; + queue->ExecuteCommandLists(1, second); + Check(bridge.WaitIdle(10000) && GameRuns(game), std::string(size.name) + ": finished"); + D3D11_QUERY_DATA_TIMESTAMP_DISJOINT timing{}; + UINT64 begin11 = 0, end11 = 0; + while (game.context->GetData(disjoint.Get(), &timing, sizeof(timing), 0) != S_OK) Sleep(0); + game.context->GetData(before11.Get(), &begin11, sizeof(begin11), 0); + game.context->GetData(after11.Get(), &end11, sizeof(end11), 0); + void* mapped = nullptr; + Check(SUCCEEDED(stamp_buffer->Map(0, nullptr, &mapped)), "map the timestamps"); + UINT64 stamps12[2]{}; + std::memcpy(stamps12, mapped, sizeof(stamps12)); + stamp_buffer->Unmap(0, nullptr); + if (iteration >= 5 && !timing.Disjoint) { + copy_ms.push_back(1000.0 * static_cast(end11 - begin11) / static_cast(timing.Frequency)); + ready_ms.push_back(1000.0 * static_cast(stamps12[1] - stamps12[0]) / + static_cast(frequency12)); + } + } + const auto median = [](std::vector values) { + std::sort(values.begin(), values.end()); + return values.empty() ? -1.0 : values[values.size() / 2]; + }; + const auto worst = [](const std::vector& values) { + return values.empty() ? -1.0 : *std::max_element(values.begin(), values.end()); + }; + // Back to back, as a session releases one image after another: the + // bridge's queue's time from before the first release to the last + // drawn, per release (the copies and draws of releases overlapping as + // they may). + constexpr int kBackToBack = 30; + ID3D12CommandList* first[] = {lists[0].Get()}; + queue->ExecuteCommandLists(1, first); + for (int release = 0; release < kBackToBack; ++release) { + bridge.Acquired(swapchain, static_cast(release) % 3); + Check(bridge.Released(swapchain), std::string(size.name) + ": copied back to back"); + } + ID3D12CommandList* second[] = {lists[1].Get()}; + queue->ExecuteCommandLists(1, second); + Check(bridge.WaitIdle(10000) && GameRuns(game), std::string(size.name) + ": back to back finished"); + void* mapped = nullptr; + Check(SUCCEEDED(stamp_buffer->Map(0, nullptr, &mapped)), "map the timestamps"); + UINT64 span[2]{}; + std::memcpy(span, mapped, sizeof(span)); + stamp_buffer->Unmap(0, nullptr); + const double per_release = + 1000.0 * static_cast(span[1] - span[0]) / static_cast(frequency12) / kBackToBack; + std::cout << "depth cost " << size.name << ": Direct3D 11 copy " << median(copy_ms) << " ms (max " + << worst(copy_ms) << "), release to runtime image drawn " << median(ready_ms) << " ms (max " + << worst(ready_ms) << "), back to back " << per_release << " ms a release, " + << bridge.Describe(swapchain) << '\n'; + CheckClean(bridge, game, std::string("depth cost ") + size.name); + Check(bridge.RemoveSwapchain(swapchain), std::string(size.name) + ": removed"); + bridge.End(); + } +} + +// BRIDGE_TEST_MT_PERF=1 (or `only`: nothing else runs): what the device's +// multithread protection, which mirroring a game's depth (and a second +// thread's swapchain calls) turns on, costs the game's own Direct3D 11 +// calls. A device of its own, made as a game makes it (no debug layer, which +// would dwarf the lock), submits game-like frames on its immediate context +// with protection off, on, and on with a second thread taking the device's +// lock as the bridge's releases do. The modes alternate in rounds (the order +// turning each round) after a warm-up, so drift falls on all of them alike. +// CPU submit time per frame (from the first call to the frame's Flush), two +// frames in flight, the GPU's waits outside the timing. +void MeasureMultithreadCost(bool hardware) { + ComPtr adapter; + if (hardware) { + ComPtr factory; + Check(SUCCEEDED(CreateDXGIFactory1(IID_PPV_ARGS(&factory))), "multithread cost: a DXGI factory"); + if (const std::optional wanted = test_adapter::Wanted()) { + std::string why; + adapter = test_adapter::FindDxgiAdapter(factory.Get(), *wanted, why); + Check(adapter != nullptr, "multithread cost: " + why); + } else { + Check(SUCCEEDED(factory->EnumAdapters1(0, &adapter)), "multithread cost: an adapter"); + } + } + const D3D_FEATURE_LEVEL levels[] = {D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0}; + ComPtr device; + ComPtr context; + Check(SUCCEEDED(D3D11CreateDevice(adapter.Get(), hardware ? D3D_DRIVER_TYPE_UNKNOWN : D3D_DRIVER_TYPE_WARP, + nullptr, 0, levels, 2, D3D11_SDK_VERSION, &device, nullptr, &context)), + "multithread cost: a Direct3D 11 device"); + ComPtr context4; + ComPtr device5; + ComPtr multithread; + Check(SUCCEEDED(context.As(&context4)) && SUCCEEDED(device.As(&device5)) && SUCCEEDED(context.As(&multithread)), + "multithread cost: ID3D11DeviceContext4, ID3D11Device5, ID3D11Multithread"); + { + ComPtr dxgi; + ComPtr used; + DXGI_ADAPTER_DESC desc{}; + if (SUCCEEDED(device.As(&dxgi)) && SUCCEEDED(dxgi->GetAdapter(&used)) && SUCCEEDED(used->GetDesc(&desc))) { + std::string name; + for (const wchar_t* c = desc.Description; *c; ++c) name += *c < 128 ? static_cast(*c) : '?'; + std::cout << "multithread cost on " << name << std::endl; + } + } + + const char* source = R"( +cbuffer Draw : register(b0) { float4 offset; float4 tint; }; +Texture2D image : register(t0); +SamplerState linear_sampler : register(s0); +struct Out { float4 position : SV_Position; float2 uv : TEXCOORD0; }; +Out VSMain(float3 position : POSITION) { + Out o; + o.position = float4(position.xy * 0.05 + offset.xy + VARIANT * 0.001, 0.5, 1.0); + o.uv = position.xy * 0.5 + 0.5; + return o; +} +float4 PSMain(Out i) : SV_Target { return image.Sample(linear_sampler, i.uv) * tint + VARIANT * 0.01; } +Out FullVS(uint id : SV_VertexID) { + Out o; + o.uv = float2((id << 1) & 2, id & 2); + o.position = float4(o.uv * float2(2, -2) + float2(-1, 1), 0.5, 1.0); + return o; +} +float4 HeavyPS(Out i) : SV_Target { + float4 sum = 0; + [loop] for (int k = 0; k < 48; ++k) sum += image.SampleLevel(linear_sampler, i.uv + k * 0.001, 0) * sin(k + i.uv.x); + return sum * tint; +} +RWTexture2D twin : register(u0); +[numthreads(8, 8, 1)] void CopyCS(uint3 id : SV_DispatchThreadID) { twin[id.xy] = image[id.xy].r; } +)"; + const auto compile = [&](const char* entry, const char* target, int variant) { + const std::string value = std::to_string(variant); + const D3D_SHADER_MACRO macros[] = {{"VARIANT", value.c_str()}, {nullptr, nullptr}}; + ComPtr code, errors; + Check(SUCCEEDED(D3DCompile(source, std::strlen(source), "mt_cost", macros, nullptr, entry, target, 0, 0, &code, + &errors)), + std::string("multithread cost: ") + entry + " compiles: " + + (errors ? static_cast(errors->GetBufferPointer()) : "")); + return code; + }; + constexpr int kVariants = 4; + ComPtr vs[kVariants]; + ComPtr ps[kVariants]; + ComPtr layout; + for (int v = 0; v < kVariants; ++v) { + const ComPtr vs_code = compile("VSMain", "vs_5_0", v); + const ComPtr ps_code = compile("PSMain", "ps_5_0", v); + Check(SUCCEEDED(device->CreateVertexShader(vs_code->GetBufferPointer(), vs_code->GetBufferSize(), nullptr, &vs[v])) && + SUCCEEDED(device->CreatePixelShader(ps_code->GetBufferPointer(), ps_code->GetBufferSize(), nullptr, + &ps[v])), + "multithread cost: shaders"); + if (v == 0) { + const D3D11_INPUT_ELEMENT_DESC element{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, + D3D11_INPUT_PER_VERTEX_DATA, 0}; + Check(SUCCEEDED(device->CreateInputLayout(&element, 1, vs_code->GetBufferPointer(), vs_code->GetBufferSize(), + &layout)), + "multithread cost: input layout"); + } + } + ComPtr full_vs; + ComPtr heavy_ps; + ComPtr copy_cs; + { + const ComPtr a = compile("FullVS", "vs_5_0", 0); + const ComPtr b = compile("HeavyPS", "ps_5_0", 0); + const ComPtr c = compile("CopyCS", "cs_5_0", 0); + Check(SUCCEEDED(device->CreateVertexShader(a->GetBufferPointer(), a->GetBufferSize(), nullptr, &full_vs)) && + SUCCEEDED(device->CreatePixelShader(b->GetBufferPointer(), b->GetBufferSize(), nullptr, &heavy_ps)) && + SUCCEEDED(device->CreateComputeShader(c->GetBufferPointer(), c->GetBufferSize(), nullptr, ©_cs)), + "multithread cost: heavy and copy shaders"); + } + + // A cube's corners and triangles. + const float corners[8][3] = {{-1, -1, -1}, {1, -1, -1}, {1, 1, -1}, {-1, 1, -1}, + {-1, -1, 1}, {1, -1, 1}, {1, 1, 1}, {-1, 1, 1}}; + const std::uint16_t indices[36] = {0, 1, 2, 0, 2, 3, 4, 6, 5, 4, 7, 6, 0, 4, 5, 0, 5, 1, + 3, 2, 6, 3, 6, 7, 1, 5, 6, 1, 6, 2, 0, 3, 7, 0, 7, 4}; + ComPtr vertex_buffer, index_buffer; + { + D3D11_BUFFER_DESC desc{sizeof(corners), D3D11_USAGE_IMMUTABLE, D3D11_BIND_VERTEX_BUFFER}; + D3D11_SUBRESOURCE_DATA data{corners}; + Check(SUCCEEDED(device->CreateBuffer(&desc, &data, &vertex_buffer)), "multithread cost: vertices"); + desc = {sizeof(indices), D3D11_USAGE_IMMUTABLE, D3D11_BIND_INDEX_BUFFER}; + data = {indices}; + Check(SUCCEEDED(device->CreateBuffer(&desc, &data, &index_buffer)), "multithread cost: indices"); + } + constexpr int kConstantBuffers = 8; + ComPtr constants[kConstantBuffers]; + for (auto& buffer : constants) { + D3D11_BUFFER_DESC desc{256, D3D11_USAGE_DYNAMIC, D3D11_BIND_CONSTANT_BUFFER, D3D11_CPU_ACCESS_WRITE}; + Check(SUCCEEDED(device->CreateBuffer(&desc, nullptr, &buffer)), "multithread cost: constant buffers"); + } + constexpr int kTextures = 64; + ComPtr textures[kTextures]; + { + std::vector pixels(64 * 64); + for (int t = 0; t < kTextures; ++t) { + for (std::size_t p = 0; p < pixels.size(); ++p) pixels[p] = 0xff000000u | static_cast(t * 2654435761u + p); + D3D11_TEXTURE2D_DESC desc{64, 64, 1, 1, DXGI_FORMAT_R8G8B8A8_UNORM, {1, 0}, D3D11_USAGE_IMMUTABLE, + D3D11_BIND_SHADER_RESOURCE}; + D3D11_SUBRESOURCE_DATA data{pixels.data(), 64 * 4}; + ComPtr texture; + Check(SUCCEEDED(device->CreateTexture2D(&desc, &data, &texture)) && + SUCCEEDED(device->CreateShaderResourceView(texture.Get(), nullptr, &textures[t])), + "multithread cost: textures"); + } + } + const auto target = [&](UINT width, UINT height, ComPtr& view) { + D3D11_TEXTURE2D_DESC desc{width, height, 1, 1, DXGI_FORMAT_R8G8B8A8_UNORM, {1, 0}, D3D11_USAGE_DEFAULT, + D3D11_BIND_RENDER_TARGET}; + ComPtr texture; + Check(SUCCEEDED(device->CreateTexture2D(&desc, nullptr, &texture)) && + SUCCEEDED(device->CreateRenderTargetView(texture.Get(), nullptr, &view)), + "multithread cost: a render target"); + }; + ComPtr small_target, large_target; + target(1024, 1024, small_target); + // Pimax scale: both eyes of a ~6000x6000 frame are the GPU's time, not the CPU's. + if (hardware) target(6000, 6000, large_target); + ComPtr sampler; + ComPtr blends[2]; + ComPtr rasters[2]; + { + D3D11_SAMPLER_DESC desc{D3D11_FILTER_MIN_MAG_MIP_LINEAR, D3D11_TEXTURE_ADDRESS_WRAP, D3D11_TEXTURE_ADDRESS_WRAP, + D3D11_TEXTURE_ADDRESS_WRAP}; + desc.MaxLOD = D3D11_FLOAT32_MAX; + Check(SUCCEEDED(device->CreateSamplerState(&desc, &sampler)), "multithread cost: sampler"); + for (int b = 0; b < 2; ++b) { + D3D11_BLEND_DESC blend{}; + blend.RenderTarget[0].BlendEnable = b; + blend.RenderTarget[0].SrcBlend = blend.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA; + blend.RenderTarget[0].DestBlend = blend.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA; + blend.RenderTarget[0].BlendOp = blend.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; + blend.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; + D3D11_RASTERIZER_DESC raster{}; + raster.FillMode = D3D11_FILL_SOLID; + raster.CullMode = b ? D3D11_CULL_BACK : D3D11_CULL_NONE; + raster.DepthClipEnable = TRUE; + Check(SUCCEEDED(device->CreateBlendState(&blend, &blends[b])) && + SUCCEEDED(device->CreateRasterizerState(&raster, &rasters[b])), + "multithread cost: blend and raster states"); + } + } + // The second thread's stand-in for a release's depth copy: a small compute + // copy into a texture of its own. + ComPtr twin_write; + { + D3D11_TEXTURE2D_DESC desc{64, 64, 1, 1, DXGI_FORMAT_R32_FLOAT, {1, 0}, D3D11_USAGE_DEFAULT, + D3D11_BIND_UNORDERED_ACCESS}; + ComPtr texture; + Check(SUCCEEDED(device->CreateTexture2D(&desc, nullptr, &texture)) && + SUCCEEDED(device->CreateUnorderedAccessView(texture.Get(), nullptr, &twin_write)), + "multithread cost: the second thread's texture"); + } + ComPtr fence; + Check(SUCCEEDED(device5->CreateFence(0, D3D11_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence))), "multithread cost: a fence"); + constexpr int kInFlight = 2; + ComPtr frame_done[kInFlight + 1]; + for (auto& query : frame_done) { + D3D11_QUERY_DESC desc{D3D11_QUERY_EVENT}; + Check(SUCCEEDED(device->CreateQuery(&desc, &query)), "multithread cost: event queries"); + } + + // One frame's calls; returns how many Direct3D 11 calls it made. + enum class Shape { kDraws, kMaps, kHeavy }; + const auto frame = [&](Shape shape, int count) -> std::uint64_t { + std::uint64_t calls = 0; + const D3D11_VIEWPORT small_view{0, 0, 1024, 1024, 0, 1}; + if (shape == Shape::kHeavy) { + const D3D11_VIEWPORT view{0, 0, 6000, 6000, 0, 1}; + context->OMSetRenderTargets(1, large_target.GetAddressOf(), nullptr); + context->RSSetViewports(1, &view); + context->RSSetState(rasters[0].Get()); + context->OMSetBlendState(blends[0].Get(), nullptr, 0xffffffff); + context->IASetInputLayout(nullptr); + context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + context->VSSetShader(full_vs.Get(), nullptr, 0); + context->PSSetShader(heavy_ps.Get(), nullptr, 0); + context->PSSetSamplers(0, 1, sampler.GetAddressOf()); + calls += 9; + for (int draw = 0; draw < count; ++draw) { + D3D11_MAPPED_SUBRESOURCE mapped{}; + context->Map(constants[draw % kConstantBuffers].Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); + static_cast(mapped.pData)[4] = 1.0f / (draw + 1); + context->Unmap(constants[draw % kConstantBuffers].Get(), 0); + context->PSSetConstantBuffers(0, 1, constants[draw % kConstantBuffers].GetAddressOf()); + context->PSSetShaderResources(0, 1, textures[draw % kTextures].GetAddressOf()); + context->Draw(3, 0); + calls += 5; + } + } else if (shape == Shape::kDraws) { + const float clear[4] = {0, 0, 0, 1}; + context->OMSetRenderTargets(1, small_target.GetAddressOf(), nullptr); + context->ClearRenderTargetView(small_target.Get(), clear); + context->RSSetViewports(1, &small_view); + context->IASetInputLayout(layout.Get()); + context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + context->IASetIndexBuffer(index_buffer.Get(), DXGI_FORMAT_R16_UINT, 0); + context->PSSetSamplers(0, 1, sampler.GetAddressOf()); + calls += 7; + const UINT stride = 12, offset = 0; + for (int draw = 0; draw < count; ++draw) { + // Per material: shaders and fixed-function state (every 8th draw). + if (draw % 8 == 0) { + const int material = (draw / 8) % kVariants; + context->VSSetShader(vs[material].Get(), nullptr, 0); + context->PSSetShader(ps[material].Get(), nullptr, 0); + context->OMSetBlendState(blends[material & 1].Get(), nullptr, 0xffffffff); + context->RSSetState(rasters[(material >> 1) & 1].Get()); + calls += 4; + } + // Per object: its constants (a dynamic buffer, discarded), its + // texture and mesh, the draw. + ID3D11Buffer* buffer = constants[draw % kConstantBuffers].Get(); + D3D11_MAPPED_SUBRESOURCE mapped{}; + context->Map(buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); + float* values = static_cast(mapped.pData); + values[0] = static_cast((draw * 37) % 200) / 100.0f - 1.0f; + values[1] = static_cast((draw * 91) % 200) / 100.0f - 1.0f; + values[4] = values[5] = values[6] = values[7] = 0.5f; + context->Unmap(buffer, 0); + context->VSSetConstantBuffers(0, 1, &buffer); + context->PSSetConstantBuffers(0, 1, &buffer); + context->PSSetShaderResources(0, 1, textures[draw % kTextures].GetAddressOf()); + context->IASetVertexBuffers(0, 1, vertex_buffer.GetAddressOf(), &stride, &offset); + context->DrawIndexed(36, 0, 0); + calls += 7; + } + } else { + for (int map = 0; map < count; ++map) { + ID3D11Buffer* buffer = constants[map % kConstantBuffers].Get(); + D3D11_MAPPED_SUBRESOURCE mapped{}; + context->Map(buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); + std::memset(mapped.pData, map & 0xff, 64); + context->Unmap(buffer, 0); + calls += 2; + } + } + context->Flush(); + return calls + 1; + }; + + enum class Mode { kOff, kOn, kOnSecondThread, kOnBusySecondThread, kCount }; + const char* mode_names[] = {"off", "on", "on + 2nd thread 270/s", "on + 2nd thread 1000/s"}; + // The second thread: a release's work under the device's lock, as the + // bridge does it (its state saved, a small compute copy, its state back, + // a fence signal, a flush), at three releases a 90 Hz frame (two eyes and + // depth), or at 1000 a second for an upper bound. + std::atomic second_period_us{0}; + std::atomic stop{false}; + std::atomic second_bursts{0}; + std::atomic second_hold_us{0.0}; + std::thread second([&] { + std::uint64_t value = 0; + auto next = std::chrono::steady_clock::now(); + while (!stop.load()) { + const int period = second_period_us.load(); + if (period == 0) { + Sleep(1); + next = std::chrono::steady_clock::now(); + continue; + } + next += std::chrono::microseconds(period); + while (std::chrono::steady_clock::now() < next) { + const auto left = next - std::chrono::steady_clock::now(); + if (left > std::chrono::microseconds(1500)) Sleep(1); + else std::this_thread::yield(); + } + if (second_period_us.load() == 0) continue; // the mode ended while it waited + const auto start = std::chrono::steady_clock::now(); + multithread->Enter(); + ID3D11ComputeShader* shader = nullptr; + ID3D11ClassInstance* instances[256]; + UINT instance_count = 256; + context->CSGetShader(&shader, instances, &instance_count); + ID3D11ShaderResourceView* read = nullptr; + ID3D11UnorderedAccessView* write = nullptr; + context->CSGetShaderResources(0, 1, &read); + context->CSGetUnorderedAccessViews(0, 1, &write); + context->CSSetShader(copy_cs.Get(), nullptr, 0); + context->CSSetShaderResources(0, 1, textures[0].GetAddressOf()); + context->CSSetUnorderedAccessViews(0, 1, twin_write.GetAddressOf(), nullptr); + context->Dispatch(8, 8, 1); + ID3D11UnorderedAccessView* no_write = nullptr; + ID3D11ShaderResourceView* no_read = nullptr; + context->CSSetUnorderedAccessViews(0, 1, &no_write, nullptr); + context->CSSetShaderResources(0, 1, &no_read); + context->CSSetUnorderedAccessViews(0, 1, &write, nullptr); + context->CSSetShaderResources(0, 1, &read); + context->CSSetShader(shader, instance_count ? instances : nullptr, instance_count); + context4->Signal(fence.Get(), ++value); + context->Flush(); + multithread->Leave(); + if (shader) shader->Release(); + for (UINT i = 0; i < instance_count; ++i) if (instances[i]) instances[i]->Release(); + if (read) read->Release(); + if (write) write->Release(); + second_hold_us = second_hold_us.load() + + std::chrono::duration(std::chrono::steady_clock::now() - start).count(); + ++second_bursts; + } + }); + + struct Case { + const char* name; + Shape shape; + int count; + int frames; + }; + const std::vector cases = + hardware ? std::vector{{"5k draws (UE4-like)", Shape::kDraws, 5000, 100}, + {"10k draws", Shape::kDraws, 10000, 80}, + {"20k draws (iRacing-like)", Shape::kDraws, 20000, 60}, + {"20k Map/Unmap of dynamic constants", Shape::kMaps, 20000, 100}, + {"8 heavy draws at 6000x6000", Shape::kHeavy, 8, 40}} + : std::vector{{"5k draws (UE4-like)", Shape::kDraws, 5000, 30}, + {"20k Map/Unmap of dynamic constants", Shape::kMaps, 20000, 30}}; + const char* rounds_text = std::getenv("BRIDGE_TEST_MT_PERF_ROUNDS"); + const int rounds = rounds_text ? std::max(1, std::atoi(rounds_text)) : (hardware ? 8 : 3); + const int modes = static_cast(Mode::kCount); + LARGE_INTEGER frequency{}; + QueryPerformanceFrequency(&frequency); + const auto percentile = [](std::vector values, double p) { + std::sort(values.begin(), values.end()); + if (values.empty()) return -1.0; + return values[std::min(values.size() - 1, static_cast(p * values.size()))]; + }; + const auto wait_query = [&](ID3D11Query* query) { + while (context->GetData(query, nullptr, 0, 0) == S_FALSE) std::this_thread::yield(); + }; + // A frame that does not come back in 20 s ends the run (it would hold the + // GPU lock). + std::atomic last_frame{static_cast(GetTickCount64())}; + std::atomic where{"setup"}; + std::thread watchdog([&] { + while (!stop.load()) { + Sleep(250); + if (static_cast(GetTickCount64()) - last_frame.load() > 20000) { + std::cout << "FAILED: multithread cost: no frame for 20 s (" << where.load() << ")" << std::endl; + TerminateProcess(GetCurrentProcess(), 1); + } + } + }); + std::cout << std::fixed; + std::cout.precision(3); + for (const Case& test : cases) { + where = test.name; + std::cout << "mt cost case: " << test.name << std::endl; + std::vector submit_ms[4], frame_ms[4], round_medians[4]; + std::uint64_t calls = 0; + double hold_us[4]{}; + std::uint64_t bursts[4]{}; + for (int round = 0; round < rounds; ++round) { + for (int step = 0; step < modes; ++step) { + const int mode = (step + round) % modes; // each mode first in turn + multithread->SetMultithreadProtected(mode == static_cast(Mode::kOff) ? FALSE : TRUE); + second_period_us = mode == static_cast(Mode::kOnSecondThread) ? 3704 + : mode == static_cast(Mode::kOnBusySecondThread) ? 1000 + : 0; + where = mode_names[mode]; + const std::uint64_t bursts_before = second_bursts.load(); + const double hold_before = second_hold_us.load(); + std::vector round_ms; + const int warm = std::max(10, test.frames / 4); + for (int index = 0; index < warm + test.frames; ++index) { + ID3D11Query* done = frame_done[index % (kInFlight + 1)].Get(); + if (index > kInFlight) wait_query(done); // that slot's frame, kInFlight + 1 frames back + LARGE_INTEGER begin{}, end{}; + QueryPerformanceCounter(&begin); + calls = frame(test.shape, test.count); + QueryPerformanceCounter(&end); + context->End(done); + const double ms = 1000.0 * static_cast(end.QuadPart - begin.QuadPart) / + static_cast(frequency.QuadPart); + if (test.shape == Shape::kHeavy) { + wait_query(done); // the frame to the GPU's last pixel + LARGE_INTEGER finished{}; + QueryPerformanceCounter(&finished); + if (index >= warm) { + frame_ms[mode].push_back(1000.0 * static_cast(finished.QuadPart - begin.QuadPart) / + static_cast(frequency.QuadPart)); + } + } + last_frame = static_cast(GetTickCount64()); + if (index >= warm) { + submit_ms[mode].push_back(ms); + round_ms.push_back(ms); + } + } + for (auto& query : frame_done) wait_query(query.Get()); + round_medians[mode].push_back(percentile(round_ms, 0.5)); + second_period_us = 0; + bursts[mode] += second_bursts.load() - bursts_before; + hold_us[mode] += second_hold_us.load() - hold_before; + } + } + const double off_median = percentile(submit_ms[0], 0.5); + std::cout << "mt cost " << (hardware ? "hw" : "WARP") << " | " << test.name << " | " << calls + << " calls a frame | " << rounds << " rounds x " << test.frames << " frames a mode\n"; + for (int mode = 0; mode < modes; ++mode) { + const double median = percentile(submit_ms[mode], 0.5); + // Paired by round: each round's median against the same round's off. + std::vector deltas; + for (int round = 0; round < rounds; ++round) { + deltas.push_back(round_medians[mode][round] - round_medians[0][round]); + } + const double delta = percentile(deltas, 0.5); + std::cout << " " << mode_names[mode] << ": submit median " << median << " ms, p95 " + << percentile(submit_ms[mode], 0.95) << ", p99 " << percentile(submit_ms[mode], 0.99) << ", " + << static_cast(calls) / median / 1000.0 << " M calls/s"; + if (mode != 0) { + std::cout << " | vs off: " << (median - off_median) << " ms pooled, " << delta + << " ms paired (rounds " << percentile(deltas, 0.0) << " .. " << percentile(deltas, 1.0) + << "), " << 1e6 * delta / static_cast(calls) << " ns a call"; + } + if (bursts[mode]) { + std::cout << " | 2nd thread " << bursts[mode] << " holds, " << hold_us[mode] / bursts[mode] + << " us each"; + } + if (!frame_ms[mode].empty()) { + std::cout << " | to GPU done median " << percentile(frame_ms[mode], 0.5) << " ms"; + } + std::cout << std::endl; + } + } + stop = true; + second.join(); + watchdog.join(); + multithread->SetMultithreadProtected(FALSE); + std::cout.unsetf(std::ios::fixed); + std::cout.precision(6); } // A runtime image that does not match is refused, with the reason. @@ -959,6 +2680,9 @@ void TestSecondThread(Game& game) { bridge.Acquired(swapchain, 0); std::thread release([&] { bridge.Released(swapchain); }); release.join(); + Check(!multithread->GetMultithreadProtected() && g_log.empty(), + "the second thread's call itself does not turn protection on (it waits for the device's lock)"); + bridge.StageReleases(XR_NULL_HANDLE); // the next hand-over, before the layer's locks Check(multithread->GetMultithreadProtected() != FALSE, "a second thread turns multithread protection on"); Check(g_log.size() == 1 && Contains(g_log[0], "turned the game device's multithread protection on"), "and says so"); @@ -973,6 +2697,77 @@ void TestSecondThread(Game& game) { multithread->SetMultithreadProtected(FALSE); } +// A colour-only game (DCS: no depth, protection off) whose second thread +// makes its first swapchain call while another thread holds the device's +// lock and then needs the bridge: the hand-over's context hold +// (StageReleases), or a game thread of its own. Turning protection on waits +// for that lock; done inside the second thread's call, holding the bridge's +// mutex, it deadlocked with the holder's next bridge call. Events drive it: +// a thread enters the lock, the second thread releases (ReleaseCalled, as +// the layer calls it), and the holder waits for that release (2 s), then +// calls StageReleases, which takes the context (its own lock again) and the +// bridge's mutex. Both must finish; a deadlock fails the test after 10 s +// (the process ends: its threads cannot be got back). Protection goes on in +// that StageReleases, reported once. +void TestSecondThreadUnderHeldContext(Game& game) { + ComPtr multithread; + Check(SUCCEEDED(game.context.As(&multithread)), "the game's context has ID3D11Multithread"); + multithread->SetMultithreadProtected(FALSE); + D3D11Bridge bridge; + Session session(game.device.Get()); + BeginBridge(game, bridge, session); + MakeRuntimeImages(BridgeDevice(bridge), 2, 16, 16, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB); + const XrSwapchain swapchain = Handle(0xc1); + std::string reason; + Check(bridge.AddSwapchain(swapchain, ColorInfo(16, 16), FakeEnumerate, reason), "bridged: " + reason); + bridge.FrameEnded(); // the frame's thread (this one) + bridge.Acquired(swapchain, 0); + + const auto await = [](HANDLE event, DWORD ms, const char* what) { + if (WaitForSingleObject(event, ms) == WAIT_OBJECT_0) return true; + if (what) { + std::cout << "FAILED: second thread under a held context: " << what + << ": deadlocked (turning protection on waits for the device's lock, holding the bridge's " + "mutex the lock's holder waits for)\n" + << std::flush; + TerminateProcess(GetCurrentProcess(), 1); + } + return false; + }; + const HANDLE entered = CreateEventW(nullptr, TRUE, FALSE, nullptr); + const HANDLE released = CreateEventW(nullptr, TRUE, FALSE, nullptr); + const HANDLE staged = CreateEventW(nullptr, TRUE, FALSE, nullptr); + std::atomic released_while_held{false}; + std::thread holder([&] { + multithread->Enter(); + SetEvent(entered); + released_while_held = await(released, 2000, nullptr); + bridge.StageReleases(XR_NULL_HANDLE); + multithread->Leave(); + SetEvent(staged); + }); + await(entered, 10000, "the holder did not enter the device's lock"); + std::thread second([&] { + bridge.ReleaseCalled(swapchain); + SetEvent(released); + }); + await(released, 10000, "the second thread's release did not return"); + await(staged, 10000, "the holder's StageReleases did not return"); + holder.join(); + second.join(); + for (const HANDLE event : {entered, released, staged}) CloseHandle(event); + Check(released_while_held, "the second thread's release returned while the device's lock was held"); + Check(multithread->GetMultithreadProtected() != FALSE, "the hand-over turned protection on"); + Check(g_log.size() == 1 && Contains(g_log[0], "turned the game device's multithread protection on"), + "and said so, once"); + Check(bridge.Released(swapchain), "the release is handed over"); + Check(bridge.WaitIdle(10000), "the bridge's queue finishes"); + g_log.clear(); + CheckClean(bridge, game, "second thread under a held context"); + bridge.End(); + multithread->SetMultithreadProtected(FALSE); +} + // The bridge's device removed: the session is lost, and teardown does not wait. // Last: Direct3D 12 makes one device per adapter in a process. void TestDeviceLost(Game& game) { @@ -1010,6 +2805,17 @@ void TestDeviceLost(Game& game) { Check(GameRuns(game), "the game's Direct3D 11 device runs on"); } +// Whether the game's adapter renders on the processor (WARP, or the Basic +// Render Driver of a machine without a GPU). +bool SoftwareAdapter(const Game& game) { + ComPtr factory; + ComPtr adapter; + DXGI_ADAPTER_DESC1 desc{}; + return FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory))) || + FAILED(factory->EnumAdapterByLuid(game.luid, IID_PPV_ARGS(&adapter))) || FAILED(adapter->GetDesc1(&desc)) || + test_adapter::SoftwareDxgiAdapter(desc); +} + } // namespace int main(int argc, char** argv) { @@ -1021,11 +2827,21 @@ int main(int argc, char** argv) { if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debug)))) { debug->EnableDebugLayer(); } + if (const char* perf = std::getenv("BRIDGE_TEST_MT_PERF"); + perf && (std::string(perf) == "1" || std::string(perf) == "only")) { + MeasureMultithreadCost(hardware); + if (std::string(perf) == "only") return 0; + } Game game; if (!MakeGame(hardware, game)) { std::cout << "skipped: no Direct3D 11 device\n"; return kSkip; } + if (hardware && SoftwareAdapter(game)) { + // A hosted CI runner's Basic Render Driver: the WARP run covers it. + std::cout << "skipped: `hw` needs a hardware adapter (the default adapter renders on the processor)\n"; + return kSkip; + } TestBegin(game); TestSessionGraphics(game, hardware); TestCreateInfo(); @@ -1039,7 +2855,17 @@ int main(int argc, char** argv) { TestDoneWait(game); TestResolve(game); TestArray(game); - TestDepth(game); + TestDepthFormats(game); + TestDepthStillBound(game); + TestDepthKeepsGameState(game); + TestDepthCopyHoldsContext(game); + TestDepthCopyHoldsContext(game, false); + TestDepthDoneWait(game); + TestDepthPrivate(game); + TestDepthReachesFrameGen(game); + if (const char* perf = std::getenv("BRIDGE_TEST_DEPTH_PERF"); hardware && perf && std::string(perf) == "1") { + MeasureDepthCost(game); + } TestMismatch(game); TestComposed(game); TestUnmatched(game); @@ -1049,6 +2875,7 @@ int main(int argc, char** argv) { TestDrainPastGame(game); } TestSecondThread(game); + TestSecondThreadUnderHeldContext(game); if (!skip_negative_controls) { TestDeviceLost(game); } else { diff --git a/layer/tests/eye_gaze_tests.cpp b/layer/tests/eye_gaze_tests.cpp index 4c58a4f8..e4429665 100644 --- a/layer/tests/eye_gaze_tests.cpp +++ b/layer/tests/eye_gaze_tests.cpp @@ -109,11 +109,14 @@ XrResult XRAPI_CALL State(XrSession, const XrActionStateGetInfo*, XrActionStateP XrResult XRAPI_CALL Sources(XrSession, const XrBoundSourcesForActionEnumerateInfo*, uint32_t, uint32_t* count, XrPath*) { ++source_queries; *count = 1; return XR_SUCCESS; } +// The gaze, turned `gaze_turn` radians about the head's up axis. +float gaze_turn = 0.0f; +XrTime gaze_time = 123456; XrResult XRAPI_CALL Locate(XrSpace space, XrSpace, XrTime time, XrSpaceLocation* out) { ++gaze_locates; - Expect(space == Handle(8) && time == 123456, "private gaze space and requested time must reach runtime"); + Expect(space == Handle(8) && time == gaze_time, "private gaze space and requested time must reach runtime"); out->locationFlags = XR_SPACE_LOCATION_ORIENTATION_VALID_BIT; - out->pose.orientation.w = 1; + out->pose.orientation = {0.0f, std::sin(gaze_turn / 2.0f), 0.0f, std::cos(gaze_turn / 2.0f)}; return XR_SUCCESS; } XrResult XRAPI_CALL DestroyAction(XrAction) { return XR_SUCCESS; } @@ -122,6 +125,42 @@ XrResult XRAPI_CALL Poll(XrInstance, XrEventDataBuffer* out) { if (!event_pending) return XR_EVENT_UNAVAILABLE; *out = event; event_pending = false; return XR_SUCCESS; } +// Native quad views (XR_VARJO_quad_views forwarded): the runtime's +// combined-eye space and the foveated-rendering request the layer adds to +// the application's locate. +const auto combined_eye = Handle(40); +const auto native_view = Handle(41); +XrSpaceLocationFlags combined_eye_flags = 0; +int combined_eye_locates = 0; +std::optional requested_foveation; +// PimaxXR 0.4.4's locateSpace (space.cpp): COMBINED_EYE_VARJO carries +// ORIENTATION_TRACKED alone while its eye tracker has gaze, and a pose that +// is not valid (Pose::IsPoseValid: both VALID bits) locates with flags 0. +XrResult XRAPI_CALL PimaxCombinedEyeLocate(XrSpace space, XrSpace base, XrTime, XrSpaceLocation* out) { + ++combined_eye_locates; + Expect(space == combined_eye && base == native_view, "rendering gaze is the combined eye in VIEW"); + constexpr XrSpaceLocationFlags valid = XR_SPACE_LOCATION_ORIENTATION_VALID_BIT | XR_SPACE_LOCATION_POSITION_VALID_BIT; + out->locationFlags = (combined_eye_flags & valid) == valid ? combined_eye_flags : 0; + out->pose.orientation.w = 1; + return XR_SUCCESS; +} +XrResult XRAPI_CALL NativeQuadLocateViews(XrSession, const XrViewLocateInfo* info, XrViewState* state, uint32_t capacity, + uint32_t* count, XrView* views) { + requested_foveation.reset(); + for (auto* chain = static_cast(info->next); chain; chain = chain->next) { + if (chain->type == XR_TYPE_VIEW_LOCATE_FOVEATED_RENDERING_VARJO) { + requested_foveation = reinterpret_cast(chain)->foveatedRenderingActive; + } + } + *count = 4; + state->viewStateFlags = XR_VIEW_STATE_ORIENTATION_VALID_BIT | XR_VIEW_STATE_POSITION_VALID_BIT; + for (uint32_t i = 0; views && i < capacity && i < 4; ++i) { + views[i].pose.orientation.w = 1; + views[i].fov = {-0.5f, 0.5f, 0.5f, -0.5f}; + } + return XR_SUCCESS; +} + // A stand-in runtime for Varjo focus sharpening's output swapchains. int sharpen_creates = 0, sharpen_destroys = 0, sharpen_acquires = 0, sharpen_waits = 0, sharpen_releases = 0; uint32_t sharpen_next_image = 0; @@ -179,11 +218,11 @@ XrResult SyncApp(const void* next = nullptr) { info.countActiveActionSets = 1; info.activeActionSets = &active; return Layer().SyncActions(session, &info); } -void FocusEvent(XrSessionState state, XrSession target = session) { +void FocusEvent(XrSessionState state, XrSession target = session, XrTime time = 123456) { event = {XR_TYPE_EVENT_DATA_BUFFER}; auto& change = reinterpret_cast(event); change.type = XR_TYPE_EVENT_DATA_SESSION_STATE_CHANGED; change.session = target; change.state = state; - change.time = 123456; event_pending = true; + change.time = time; event_pending = true; XrEventDataBuffer out{XR_TYPE_EVENT_DATA_BUFFER}; Expect(Layer().PollEvent(instance, &out) == XR_SUCCESS && std::memcmp(&out, &event, sizeof(event)) == 0, "session event must reach application unchanged"); @@ -214,6 +253,8 @@ class EyeGazeTestPeer { suggestions = attachments = syncs = state_queries = source_queries = gaze_locates = 0; suggestion_result = attachment_result = sync_result = XR_SUCCESS; reject_private = event_pending = runtime_gaze_active = false; + gaze_turn = 0.0f; + gaze_time = 123456; runtime_bindings.clear(); runtime_sets.clear(); runtime_active_sets.clear(); runtime_next = nullptr; runtime_attached.clear(); runtime_owner.clear(); } @@ -226,6 +267,13 @@ class EyeGazeTestPeer { double yaw{}, pitch{}; return Layer().LocateEyeGazeFocusOffsets(session, Handle(9), 123456, settings, &yaw, &pitch); } + // The gaze for a frame at `time`. + static bool GazeAt(XrTime time) { + gaze_time = time; + QuadViewsResolvedSettings settings{}; settings.tracking_mode = QuadViewsTrackingMode::Eye; + double yaw{}, pitch{}; + return Layer().LocateEyeGazeFocusOffsets(session, Handle(9), time, settings, &yaw, &pitch); + } static bool GoodAppSync() { return Layer().last_app_action_sync_time_.has_value(); } static bool GoodPrivateSync() { return Layer().last_eye_gaze_self_sync_time_.has_value(); } static void AgeAttempts() { @@ -247,6 +295,55 @@ class EyeGazeTestPeer { l.UpdateEyeGazeResources(previous); } static bool Ready() { return Layer().eye_gaze_resources_ready_; } + // An application's own native quad locate (no foveated-rendering + // request) with Eye tracking on, on runtime `runtime_name`: what + // foveatedRenderingActive the layer asks the runtime for. + static std::optional NativeQuadFoveationRequest(const char* runtime_name) { + auto& l = Layer(); + const auto settings = l.resolved_settings_; + const auto generation = l.resolved_settings_generation_; + const auto resolved_session = l.resolved_settings_session_; + const bool loaded = l.has_loaded_config_; + l.has_loaded_config_ = true; + l.resolved_settings_ = {}; + l.resolved_settings_.core.enabled = true; + l.resolved_settings_.quadviews.enabled = true; + l.resolved_settings_.quadviews.tracking_mode = QuadViewsTrackingMode::Eye; + l.resolved_settings_generation_ = l.config_generation_; + l.resolved_settings_session_ = l.active_session_; + l.quadviews_session_active_ = true; + l.varjo_compatible_quadviews_active_ = true; + l.varjo_foveated_rendering_extension_requested_ = true; + l.varjo_native_combined_eye_space_ = combined_eye; + l.varjo_native_view_space_ = native_view; + l.runtime_name_ = runtime_name; + l.next_locate_space_ = &PimaxCombinedEyeLocate; + l.next_locate_views_ = &NativeQuadLocateViews; + XrViewLocateInfo info{XR_TYPE_VIEW_LOCATE_INFO}; + info.viewConfigurationType = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_QUAD_VARJO; + info.displayTime = 5000; + info.space = Handle(9); + XrViewState state{XR_TYPE_VIEW_STATE}; + std::array views{}; + for (XrView& view : views) view = {XR_TYPE_VIEW}; + uint32_t count = 0; + Expect(l.LocateViews(session, &info, &state, 4, &count, views.data()) == XR_SUCCESS && count == 4, + "a native quad locate must succeed"); + l.has_loaded_config_ = loaded; + l.resolved_settings_ = settings; + l.resolved_settings_generation_ = generation; + l.resolved_settings_session_ = resolved_session; + l.quadviews_session_active_.reset(); + l.varjo_compatible_quadviews_active_ = false; + l.varjo_native_combined_eye_space_ = XR_NULL_HANDLE; + l.varjo_native_view_space_ = XR_NULL_HANDLE; + l.next_locate_space_ = &Locate; + l.next_locate_views_ = nullptr; + return requested_foveation; + } + // As xrCreateSession notes its session, with the runtime's clock then. + static void SessionCreated(XrTime time) { Layer().NoteSessionCreatedLocked(time); } + static XrSessionState Observed() { return Layer().observed_session_state_; } // A swapchain of the quad views session with enumerated images, as // CreateSwapchain and EnumerateSwapchainImages track it. static void QuadSwapchain(uintptr_t handle, XrSwapchainUsageFlags usage) { @@ -642,6 +739,90 @@ int main() { "gaze inactive for the whole delay must be reported once per session"); runtime_gaze_active = true; Expect(Peer::Gaze(), "gaze that arrives later must be used"); + // A tracker that stops while its runtime still reports gaze valid + // (PimaxXR's Droolon / aSeeVR path keeps its last sample once ready) + // leaves the inset stuck. Two seconds of frames with the very same + // gaze are taken as gaze lost; repeated locates of one frame are not + // counted, and any change brings the gaze back. + Peer::Prepare(); Peer::Resources(); AttachApp(); FocusEvent(XR_SESSION_STATE_FOCUSED); + sync_result = XR_SUCCESS; runtime_gaze_active = true; gaze_turn = 0.05f; + bool live = true; + for (XrTime frame = 0; frame <= 180 && live; ++frame) { + live = Peer::GazeAt(1'000'000 + frame * 11'111'111) && Peer::GazeAt(1'000'000 + frame * 11'111'111); + Expect(live || frame == 180, "a gaze repeated for under two seconds of frames must be used"); + } + Expect(!live, "the same gaze for two seconds of frames must be taken as lost"); + gaze_turn = 0.051f; + Expect(Peer::GazeAt(3'000'000'000), "a gaze that moves again must be used"); + + // Diagnostic only (Oct 29 M3, headset off): before it froze bit for bit + // the gaze wobbled by about 0.002 rad for ~26 s, which the count above + // never sees. Ten seconds of a valid gaze within 0.003 rad of one point + // are logged once; the gaze is still used. A gaze that moves is not. + Peer::Prepare(); Peer::Resources(); AttachApp(); FocusEvent(XR_SESSION_STATE_FOCUSED); + sync_result = XR_SUCCESS; runtime_gaze_active = true; + const std::size_t still_before = Occurrences(Peer::Log(), "the gaze has stayed within"); + for (XrTime frame = 0; frame <= 920; ++frame) { + gaze_turn = 0.2f + 0.001f * static_cast(frame % 3 - 1); + Expect(Peer::GazeAt(5'000'000'000 + frame * 11'111'111), "a still but changing gaze must still be used"); + } + Expect(Occurrences(Peer::Log(), "the gaze has stayed within") == still_before + 1, + "ten seconds of a near-still valid gaze must be logged once"); + Peer::Prepare(); Peer::Resources(); AttachApp(); FocusEvent(XR_SESSION_STATE_FOCUSED); + sync_result = XR_SUCCESS; runtime_gaze_active = true; + for (XrTime frame = 0; frame <= 1800; ++frame) { + // Fixations of about a second, 0.01 rad apart, with sub-mrad jitter. + gaze_turn = 0.2f + 0.01f * static_cast((frame / 90) % 2) + 0.0005f * static_cast(frame % 2); + Expect(Peer::GazeAt(20'000'000'000 + frame * 11'111'111), "a live gaze must be used"); + } + Expect(Occurrences(Peer::Log(), "the gaze has stayed within") == still_before + 1, + "a gaze that moves must not be logged as still"); + + // PimaxXR's native quad views (enable_quad_views): its combined-eye + // space never locates with flags (ORIENTATION_TRACKED alone is not a + // valid pose), so it says nothing about gaze. Eye tracking must ask + // the runtime for its gaze foveation and leave validity to it (it + // falls back to fixed focus FOVs without gaze); Varjo's runtime still + // gets the tracked bit's answer. + Peer::Prepare(); + combined_eye_flags = XR_SPACE_LOCATION_ORIENTATION_TRACKED_BIT; + auto request = Peer::NativeQuadFoveationRequest("PimaxXR (Unofficial)"); + Expect(combined_eye_locates == 1 && request && *request == XR_TRUE, + "PimaxXR's flagless combined eye must not switch its gaze foveation off"); + combined_eye_flags = XR_SPACE_LOCATION_ORIENTATION_VALID_BIT | XR_SPACE_LOCATION_POSITION_VALID_BIT; + request = Peer::NativeQuadFoveationRequest("Varjo OpenXR Runtime"); + Expect(request && *request == XR_FALSE, "Varjo's untracked combined eye must still ask for the fixed focus"); + combined_eye_flags |= XR_SPACE_LOCATION_ORIENTATION_TRACKED_BIT; + request = Peer::NativeQuadFoveationRequest("Varjo OpenXR Runtime"); + Expect(request && *request == XR_TRUE, "Varjo's tracked combined eye must ask for gaze foveation"); + combined_eye_flags = XR_SPACE_LOCATION_ORIENTATION_TRACKED_BIT; + request = Peer::NativeQuadFoveationRequest("Varjo OpenXR Runtime"); + Expect(request && *request == XR_FALSE, "a flagless combined eye on Varjo must keep its answer"); + + // PimaxXR keeps its session event queue across sessions and stamps + // every event (XrSession)1: the last session's STOPPING, IDLE and + // EXITING, never read, come first in the next session. They pass to + // the application as sent but are not the new session's state. + Peer::Prepare(); Peer::LogTo(logs / "stale-events.log"); Peer::SessionCreated(1000); + FocusEvent(XR_SESSION_STATE_STOPPING, session, 400); + FocusEvent(XR_SESSION_STATE_IDLE, session, 500); + FocusEvent(XR_SESSION_STATE_EXITING, session, 600); + Expect(Peer::Observed() == XR_SESSION_STATE_UNKNOWN, + "the previous session's events must not become this session's state"); + FocusEvent(XR_SESSION_STATE_IDLE, session, 1500); + Expect(Peer::Observed() == XR_SESSION_STATE_IDLE, "the session's own IDLE must be taken"); + FocusEvent(XR_SESSION_STATE_READY, session, 1600); + FocusEvent(XR_SESSION_STATE_FOCUSED, session, 1700); + Expect(Peer::Observed() == XR_SESSION_STATE_FOCUSED, "the session's later events must be taken"); + Expect(Occurrences(Peer::Log(), "from before this session") == 3, "each stale event must be logged"); + // Without the runtime's clock: whatever comes before the first IDLE. + Peer::Prepare(); Peer::SessionCreated(0); + FocusEvent(XR_SESSION_STATE_EXITING, session, 600); + Expect(Peer::Observed() == XR_SESSION_STATE_UNKNOWN, "an EXITING before the first IDLE must not be taken"); + FocusEvent(XR_SESSION_STATE_IDLE, session, 1500); + FocusEvent(XR_SESSION_STATE_READY, session, 1600); + Expect(Peer::Observed() == XR_SESSION_STATE_READY, "events from the first IDLE on must be taken"); + Peer::LogTo(logs / "eye-gaze.log"); std::error_code error; std::filesystem::remove_all(logs, error); diff --git a/layer/tests/framegen_backend_tests.cpp b/layer/tests/framegen_backend_tests.cpp index 3557115e..61b357ea 100644 --- a/layer/tests/framegen_backend_tests.cpp +++ b/layer/tests/framegen_backend_tests.cpp @@ -788,6 +788,155 @@ int RunScenarios(Harness& harness) { harness.name + " 4x frames are not placed at 1/4 and 3/4 of the motion"); Check(copy_error < 0.002, harness.name + " the frame at fraction 1 is not the newest real frame"); + // 2d'. Edge fill (Direct3D 12): an output wider than the frame by a + // margin each side (whole 32-pixel tiles, as the presenter makes + // them) holds the frame at its content offset exactly as drawn into + // an output the frame's size, and every margin pixel repeats the + // frame's nearest edge pixel, none black: written together + // (Execute), one at a time (Synthesize), and as an exact copy. + if (gpu->SupportsEdgeFill() && harness.readback_sized) { + const FrameGenTuning saved = tuning; + const FrameGenEdgeMargins margins{32, 64, 32, 32}; + const std::uint32_t width = kSize + margins.left + margins.right; + const std::uint32_t height = kSize + margins.up + margins.down; + const XrSwapchain fill_first = reinterpret_cast(0x5c); + const XrSwapchain fill_second = reinterpret_cast(0x5d); + g_swapchains[fill_first] = {harness.create(width, height)}; + g_swapchains[fill_second] = {harness.create(width, height)}; + Check(gpu->Track(fill_first, EnumerateImages, error) && gpu->Track(fill_second, EnumerateImages, error), error); + auto filled = [&](XrSwapchain chain) { + FrameGenImage image{chain, 0, 0, {{0, 0}, {static_cast(width), static_cast(height)}}, output_format}; + image.content = {static_cast(margins.left), static_cast(margins.up)}; + return image; + }; + auto check_fill = [&](XrSwapchain chain, const Pixels& plain, const std::string& what) { + const Pixels wide = harness.readback_sized(g_swapchains[chain][0]); + Check(wide.size() == std::size_t{width} * height, harness.name + " edge fill: " + what + " read back"); + // A margin pixel is its nearest edge pixel. Where the + // periphery is shaded per 2x2 (a GPU's variable-rate shading + // past tangent 1: the corners here), the margin's coarse + // pixel is the edge pixel's own centre while the frame's + // coarse pixel shows a point between pixels: within a few + // levels of the edge pixels around it. + std::size_t inside_off = 0, margin_exact = 0, margin_off = 0, black = 0, margin = 0; + for (std::uint32_t y = 0; y < height; ++y) { + for (std::uint32_t x = 0; x < width; ++x) { + const int fx = std::clamp(static_cast(x) - static_cast(margins.left), 0, int(kSize) - 1); + const int fy = std::clamp(static_cast(y) - static_cast(margins.up), 0, int(kSize) - 1); + const bool inside = x >= margins.left && x < margins.left + kSize && y >= margins.up && + y < margins.up + kSize; + const std::uint32_t got = wide[std::size_t{y} * width + x] & 0xffffffu; + const std::uint32_t want = plain[std::size_t(fy) * kSize + fx] & 0xffffffu; + if (inside) { + inside_off += got != want ? 1 : 0; + continue; + } + ++margin; + black += got == 0 ? 1 : 0; + if (got == want) { + ++margin_exact; + continue; + } + const double tx = -kTan + 2.0 * kTan * (fx + 0.5) / kSize; + const double ty = kTan - 2.0 * kTan * (fy + 0.5) / kSize; + const int slack = std::hypot(tx, ty) >= 0.9 ? 8 : -1000; // exact off the corners + bool within = slack > 0; + for (int c = 0; c < 3 && within; ++c) { + int lo = 255, hi = 0; + for (int dy = -1; dy <= 1; ++dy) { + for (int dx = -1; dx <= 1; ++dx) { + const int ex = std::clamp(fx + dx, 0, int(kSize) - 1); + const int ey = std::clamp(fy + dy, 0, int(kSize) - 1); + const int value = static_cast((plain[std::size_t(ey) * kSize + ex] >> (8 * c)) & 255u); + lo = std::min(lo, value); + hi = std::max(hi, value); + } + } + const int value = static_cast((got >> (8 * c)) & 255u); + within = within && value >= lo - slack && value <= hi + slack; + } + if (!within && margin_off < 6) { + std::printf(" margin pixel (%u, %u): %06x, edge pixel (%d, %d) %06x\n", x, y, got, fx, fy, want); + } + margin_off += within ? 0 : 1; + } + } + std::cout << harness.name << " edge fill " << what << ": " << inside_off << " frame pixels differ; " + << margin_exact << " of " << margin << " margin pixels repeat their edge pixel exactly, " + << margin_off << " are not a blend of the edge beside them, " << black << " black\n"; + Check(inside_off == 0, harness.name + " edge fill: " + what + " changes the frame's own pixels"); + Check(margin_off == 0 && black == 0 && margin_exact * 2 > margin, + harness.name + " edge fill: " + what + "'s margin does not repeat the frame's nearest edge"); + }; + tuning.generated = 2; + tuning.fractions = {1.0f / 3.0f, 2.0f / 3.0f, 0.0f, 0.0f}; + gpu->ResetHistory(); + run(0, true, 0, 0); + const Pixels plain_third = run(1, true, 0, 0); + const Pixels plain_two_thirds = harness.readback(g_swapchains[kOutputSecond][0]); + auto run_filled = [&](std::uint32_t index) { + std::vector views{Submission(index, harness.format, output_format, true, 0, 0)}; + views[0].outputs = {filled(fill_first), filled(fill_second)}; + Check(gpu->Execute(views, tuning, result, error), harness.name + " edge fill execute: " + error); + }; + gpu->ResetHistory(); + run_filled(0); + const std::uint64_t fill_draws = gpu->DrawsForTest(); + run_filled(1); + Check(gpu->DrawsForTest() == fill_draws + 1, harness.name + " edge fill: both frames in one draw"); + Check(gpu->EdgesFilled(), harness.name + " edge fill: drawn frames report their margins filled"); + check_fill(fill_first, plain_third, "Execute's first frame"); + check_fill(fill_second, plain_two_thirds, "Execute's second frame"); + // The copy fallback (generated frames written by compute and + // copied, as when an output cannot be drawn into): the frame lands + // at its content offset, its margin is not drawn, and the backend + // says so, so the presenter shows the frame's own region alone. + gpu->SetDirectOutput(false); + gpu->ResetHistory(); + run_filled(0); + run_filled(1); + const bool copied_filled = gpu->EdgesFilled(); + gpu->SetDirectOutput(true); + Check(!copied_filled, harness.name + " edge fill: frames copied in report their margins filled"); + { + const Pixels wide = harness.readback_sized(g_swapchains[fill_first][0]); + Pixels inner(std::size_t{kSize} * kSize); + for (std::uint32_t y = 0; y < kSize; ++y) { + std::copy_n(wide.begin() + (std::size_t{y + margins.up} * width + margins.left), kSize, + inner.begin() + std::size_t{y} * kSize); + } + const double copied_difference = MeanError(inner, plain_third, 0, 0, kSize, kSize); + std::cout << harness.name << " edge fill copied in: frame vs drawn " << copied_difference << '\n'; + Check(copied_difference < 0.001, harness.name + " edge fill: a frame copied in is not at its content offset"); + } + gpu->ResetHistory(); + run_filled(0); + run_filled(1); + Check(gpu->EdgesFilled(), harness.name + " edge fill: drawn again, the margins are filled"); + if (gpu->SupportsJustInTime()) { + FrameGenTuning analyze = tuning; + analyze.analyze_only = true; + gpu->ResetHistory(); + std::uint64_t frame = 0; + for (std::uint32_t index = 0; index < 2; ++index) { + const std::vector views{ + Submission(index, harness.format, output_format, true, 0, 0)}; + Check(gpu->Execute(views, analyze, result, error), harness.name + " edge fill analyze: " + error); + frame = result.frame; + } + const FrameGenImage plain_output = Submission(1, harness.format, output_format, true, 0, 0).outputs[0]; + for (const float fraction : {0.5f, 1.0f}) { + Check(gpu->Synthesize(frame, FrameGenSynthesisMode::kInterpolate, fraction, {plain_output}, error), + harness.name + " edge fill synthesize: " + error); + const Pixels plain = harness.readback(g_swapchains[kOutput][0]); + Check(gpu->Synthesize(frame, FrameGenSynthesisMode::kInterpolate, fraction, {filled(fill_first)}, error), + harness.name + " edge fill synthesize: " + error); + check_fill(fill_first, plain, fraction < 1.0f ? "Synthesize" : "Synthesize's exact copy"); + } + } + tuning = saved; + } + // 2e. Just-in-time synthesis (Direct3D 12): the motion measured once // per real frame and each generated frame written on its own, later, // is what writing them together gives; from the same measurement a @@ -5756,6 +5905,29 @@ int RunProfile(Harness& harness) { std::array depths{reinterpret_cast(0x63), reinterpret_cast(0x64)}; std::array vectors{reinterpret_cast(0x65), reinterpret_cast(0x66)}; std::array, 2> outputs{}; + // FG_PROFILE_EDGE_FILL=1 (or h[,v] degrees): outputs widened by edge + // fill's margins (whole 32-pixel tiles, as the presenter makes them), the + // frames drawn at their corner: what the margins cost. + std::array fill_margins{}; + if (const char* fill = std::getenv("FG_PROFILE_EDGE_FILL"); fill && *fill && std::string(fill) != "0") { + std::array degrees{kFrameGenEdgeFillHorizontalDegrees, kFrameGenEdgeFillVerticalDegrees}; + if (std::string(fill) != "1") { + const auto parsed = ParseFrameGenEdgeFillDegrees(fill); + Check(parsed.has_value(), "profile: FG_PROFILE_EDGE_FILL is 1 or h[,v] degrees"); + degrees = *parsed; + } + for (std::size_t eye = 0; eye < 2; ++eye) { + FrameGenEdgeMargins& margins = fill_margins[eye]; + margins = FrameGenEdgeFillMargins({std::atan(eye == 0 ? -outer : -inner), std::atan(eye == 0 ? inner : outer), + std::atan(vertical), -std::atan(vertical)}, + {width, height}, degrees[0], degrees[1]); + for (std::uint32_t* margin : {&margins.left, &margins.right, &margins.up, &margins.down}) { + *margin = (*margin + 31) / 32 * 32; + } + std::printf("Edge fill: eye %zu margins %u/%u/%u/%u px, outputs %ux%u\n", eye, margins.left, margins.right, + margins.up, margins.down, width + margins.left + margins.right, height + margins.up + margins.down); + } + } for (std::size_t eye = 0; eye < 2; ++eye) { const float head = eye == 0 ? -half_ipd : half_ipd; const float left = eye == 0 ? -outer : -inner, right = eye == 0 ? inner : outer; @@ -5776,7 +5948,8 @@ int RunProfile(Harness& harness) { g_swapchains[depths[eye]] = {harness.create_depth_sized(depth_width, depth_height, panel_depth, panel, 0.0f)}; for (std::size_t set = 0; set < 4; ++set) { outputs[eye][set] = reinterpret_cast(0x70 + eye * 4 + set); - g_swapchains[outputs[eye][set]] = {harness.create(width, height)}; + g_swapchains[outputs[eye][set]] = {harness.create(width + fill_margins[eye].left + fill_margins[eye].right, + height + fill_margins[eye].up + fill_margins[eye].down)}; } Check(gpu->Track(sources[eye], EnumerateImages, error) && gpu->Track(depths[eye], EnumerateImages, error), error); for (const XrSwapchain output : outputs[eye]) { @@ -5816,7 +5989,14 @@ int RunProfile(Harness& harness) { const XrRect2Di rect{{0, 0}, {static_cast(width), static_cast(height)}}; view.source = {sources[eye], frame, 0, rect, harness.format}; for (const XrSwapchain output : outputs[eye]) { - view.outputs.push_back({output, 0, 0, rect, output_format}); + const FrameGenEdgeMargins& margins = fill_margins[eye]; + FrameGenImage image{output, 0, 0, + {{0, 0}, + {static_cast(width + margins.left + margins.right), + static_cast(height + margins.up + margins.down)}}, + output_format}; + image.content = {static_cast(margins.left), static_cast(margins.up)}; + view.outputs.push_back(image); } view.request.extent = {width, height}; view.request.output = FrameGenOutputKind::kSrgb8; @@ -6551,6 +6731,101 @@ Pixels TurnTo(const ReplayView& view, const ReplayView& next, std::vector& return out; } +// Edge fill (FG_REPLAY_EDGE_FILL, "Edge reveal"): a generated frame turned +// from its view's pose to `truth`'s, as a runtime turns a frame to where the +// head is when it shows it (rotation only, bilinear). `wide` is the output +// image, the frame at `margins`' corner. Of the truth's pixels: those the +// frame itself does not reach (revealed: the runtime shows black there) and +// of those the ones the margins do not reach either (beyond), and the mean +// error against the truth over the revealed ones shown black and shown from +// the margins. `black` and `filled` (optional) get the truth's view showing +// the frame without and with its margins. +struct ReplayReveal { + std::size_t pixels{0}, revealed{0}, beyond{0}; + double black_error{0.0}, fill_error{0.0}; +}; +ReplayReveal RevealTurned(const ReplayView& view, const Pixels& wide, const FrameGenEdgeMargins& margins, + const ReplayView& truth, Pixels* black, Pixels* filled) { + const auto a = ReplayRotation(truth.newest.orientation); + const auto b = ReplayRotation(view.newest.orientation); + const double l = std::tan(truth.newest.angle_left), r = std::tan(truth.newest.angle_right); + const double u = std::tan(truth.newest.angle_up), d = std::tan(truth.newest.angle_down); + const double nl = std::tan(view.newest.angle_left), nr = std::tan(view.newest.angle_right); + const double nu = std::tan(view.newest.angle_up), nd = std::tan(view.newest.angle_down); + const std::uint32_t wide_width = view.width + margins.left + margins.right; + const std::uint32_t wide_height = view.height + margins.up + margins.down; + if (black) black->assign(std::size_t{truth.width} * truth.height, 0xff000000u); + if (filled) filled->assign(std::size_t{truth.width} * truth.height, 0xff000000u); + const std::uint32_t threads = std::max(1u, std::thread::hardware_concurrency()); + std::vector parts(threads); + auto rows = [&](std::uint32_t first, std::uint32_t step) { + ReplayReveal& part = parts[first]; + for (std::uint32_t y = first; y < truth.height; y += step) { + for (std::uint32_t x = 0; x < truth.width; ++x) { + const double dir[3] = {l + (r - l) * (x + 0.5) / truth.width, u + (d - u) * (y + 0.5) / truth.height, -1.0}; + double world[3], local[3]; + for (int i = 0; i < 3; ++i) world[i] = a[i][0] * dir[0] + a[i][1] * dir[1] + a[i][2] * dir[2]; + for (int i = 0; i < 3; ++i) local[i] = b[0][i] * world[0] + b[1][i] * world[1] + b[2][i] * world[2]; + ++part.pixels; + // In the frame's texels (centres at whole numbers), then the + // output image's. + const bool ahead = local[2] < -1e-6; + const double sx = ahead ? ((local[0] / -local[2]) - nl) / (nr - nl) * view.width - 0.5 : -1e9; + const double sy = ahead ? ((local[1] / -local[2]) - nu) / (nd - nu) * view.height - 0.5 : -1e9; + const bool reached = sx >= -0.5 && sy >= -0.5 && sx <= view.width - 0.5 && sy <= view.height - 0.5; + const double wx = sx + margins.left, wy = sy + margins.up; + const bool margin = wx >= -0.5 && wy >= -0.5 && wx <= wide_width - 0.5 && wy <= wide_height - 0.5; + std::uint32_t pixel = 0xff000000u; + if (margin) { + const double cx = std::clamp(wx, 0.0, wide_width - 1.0), cy = std::clamp(wy, 0.0, wide_height - 1.0); + const int ix = std::min(static_cast(cx), static_cast(wide_width) - 2); + const int iy = std::min(static_cast(cy), static_cast(wide_height) - 2); + const double fx = cx - ix, fy = cy - iy; + for (int c = 0; c < 3; ++c) { + auto at = [&](int px, int py) { + return static_cast((wide[std::size_t(py) * wide_width + px] >> (8 * c)) & 255u); + }; + const double value = (at(ix, iy) * (1 - fx) + at(ix + 1, iy) * fx) * (1 - fy) + + (at(ix, iy + 1) * (1 - fx) + at(ix + 1, iy + 1) * fx) * fy; + pixel |= static_cast(std::lround(value)) << (8 * c); + } + } + const std::size_t index = std::size_t{y} * truth.width + x; + if (black && reached) (*black)[index] = pixel; + if (filled) (*filled)[index] = pixel; + if (reached) continue; + ++part.revealed; + part.beyond += margin ? 0 : 1; + const std::uint32_t want = truth.color[index]; + double black_sum = 0.0, fill_sum = 0.0; + for (int c = 0; c < 3; ++c) { + const double t = static_cast((want >> (8 * c)) & 255u); + black_sum += t; + fill_sum += std::abs(t - static_cast((pixel >> (8 * c)) & 255u)); + } + part.black_error += black_sum / (3.0 * 255.0); + part.fill_error += fill_sum / (3.0 * 255.0); + } + } + }; + std::vector workers; + for (std::uint32_t t = 0; t < threads; ++t) workers.emplace_back(rows, t, threads); + for (auto& worker : workers) worker.join(); + ReplayReveal total; + for (const ReplayReveal& part : parts) { + total.pixels += part.pixels; + total.revealed += part.revealed; + total.beyond += part.beyond; + total.black_error += part.black_error; + total.fill_error += part.fill_error; + } + if (total.revealed) { + total.black_error /= static_cast(total.revealed); + total.fill_error /= static_cast(total.revealed); + } + return total; +} + // Mean error over valid pixels, all and split by the newest frame's depth // (nearer than 4 m: cockpit), and the share of pixels off by more than a // tenth (visible artifacts: torn or misplaced edges rather than blur). @@ -7926,6 +8201,57 @@ int RunReplay(Harness& harness) { if (depth_scale != 1.0f) { std::printf("Depth given to the backend times %.3f (FG_REPLAY_DEPTH_SCALE)\n", static_cast(depth_scale)); } + // FG_REPLAY_EDGE_FILL=h[,v] (degrees past each edge; 1: the defaults): + // edge fill, as a session with it on writes frames: each view's output + // wider by the margins (whole 32-pixel tiles, as the presenter makes + // them), the frame at their corner. Every measure below reads the + // frame's own pixels as without it (the output hash too, so a run + // without the fill gives the same hash), and "Edge reveal" turns each + // generated frame to its truth's pose as a runtime turns it to the head, + // comparing what the revealed edge shows black and shown from the + // margins (FG_REPLAY_REVEAL=1 measures it without the fill as well). + std::vector fill_margins(view_count); + const char* fill_text = std::getenv("FG_REPLAY_EDGE_FILL"); + const bool edge_fill = fill_text && *fill_text && std::string(fill_text) != "0"; + const bool reveal = edge_fill || (std::getenv("FG_REPLAY_REVEAL") && std::string(std::getenv("FG_REPLAY_REVEAL")) == "1"); + if (edge_fill) { + std::array degrees{kFrameGenEdgeFillHorizontalDegrees, kFrameGenEdgeFillVerticalDegrees}; + if (std::string(fill_text) != "1") { + const auto parsed = ParseFrameGenEdgeFillDegrees(fill_text); + Check(parsed.has_value(), "replay: FG_REPLAY_EDGE_FILL is 1 or h[,v] degrees (0-15)"); + degrees = *parsed; + } + for (std::size_t v = 0; v < view_count; ++v) { + const ReplayView& view = frames[0].views[v]; + FrameGenEdgeMargins& margins = fill_margins[v]; + margins = FrameGenEdgeFillMargins({view.newest.angle_left, view.newest.angle_right, view.newest.angle_up, + view.newest.angle_down}, + {view.width, view.height}, degrees[0], degrees[1]); + for (std::uint32_t* margin : {&margins.left, &margins.right, &margins.up, &margins.down}) { + *margin = (*margin + 31) / 32 * 32; + } + std::printf("Edge fill (FG_REPLAY_EDGE_FILL, %.1f / %.1f degrees): view %zu margins %u/%u/%u/%u px " + "(left/right/up/down), output %ux%u (%.1f%% more pixels)\n", + static_cast(degrees[0]), static_cast(degrees[1]), v, margins.left, margins.right, + margins.up, margins.down, view.width + margins.left + margins.right, + view.height + margins.up + margins.down, + 100.0 * (double(view.width + margins.left + margins.right) * (view.height + margins.up + margins.down) / + (double(view.width) * view.height) - + 1.0)); + } + } + // An output image of view `v` (with edge fill, wider, the frame at the + // margins' corner). + auto output_image = [&](XrSwapchain chain, std::size_t v, const ReplayView& view, std::int64_t format) { + const FrameGenEdgeMargins& margins = fill_margins[v]; + FrameGenImage image{chain, 0, 0, + {{0, 0}, + {static_cast(view.width + margins.left + margins.right), + static_cast(view.height + margins.up + margins.down)}}, + format}; + image.content = {static_cast(margins.left), static_cast(margins.up)}; + return image; + }; std::vector sources(view_count), depths(view_count), outputs(view_count); for (std::size_t v = 0; v < view_count; ++v) { const ReplayView& view = frames[0].views[v]; @@ -7950,7 +8276,8 @@ int RunReplay(Harness& harness) { } g_swapchains[depths[v]].push_back(harness.create_depth_data(view.half_width, view.half_height, raw)); } - g_swapchains[outputs[v]] = {harness.create(view.width, view.height)}; + g_swapchains[outputs[v]] = {harness.create(view.width + fill_margins[v].left + fill_margins[v].right, + view.height + fill_margins[v].up + fill_margins[v].down)}; Check(gpu->Track(sources[v], EnumerateImages, error) && gpu->Track(depths[v], EnumerateImages, error) && gpu->Track(outputs[v], EnumerateImages, error), error); @@ -8248,7 +8575,7 @@ int RunReplay(Harness& harness) { FrameGenViewSubmission& submission = views[v]; const XrRect2Di rect{{0, 0}, {static_cast(view.width), static_cast(view.height)}}; submission.source = {sources[v], static_cast(k), 0, rect, harness.format}; - submission.outputs = {{outputs[v], 0, 0, rect, output_format}}; + submission.outputs = {output_image(outputs[v], v, view, output_format)}; submission.request.extent = {view.width, view.height}; submission.request.output = FrameGenOutputKind::kSrgb8; submission.request.synthesize = before.has_value(); @@ -8530,6 +8857,12 @@ int RunReplay(Harness& harness) { } }; BandSums rotation_sums, aligned_sums; + // Edge reveal (FG_REPLAY_EDGE_FILL, FG_REPLAY_REVEAL): shares of the + // views revealed, the revealed pixels' errors summed by pixel, the turns. + struct RevealSums { + double revealed{0}, beyond{0}, black{0}, filled{0}, pixels{0}, degrees{0}; + std::size_t views{0}; + } reveal_sums; ReplayMasked masked_sums; // Per view: with one eye's depth the eyes differ. std::vector view_bad(view_count, 0.0); @@ -8593,7 +8926,59 @@ int RunReplay(Harness& harness) { std::printf("frame %zu view %zu cockpit motion: mean %.3f px, %.1f%% of cells above 0.5 px\n", k, v, cockpit_motion / cockpit_cells, 100.0 * cockpit_moving / cockpit_cells); } - const Pixels generated = harness.readback_sized(g_swapchains[outputs[v]][0]); + // With edge fill the output image and the frame's own pixels in it. + const Pixels wide = harness.readback_sized(g_swapchains[outputs[v]][0]); + const FrameGenEdgeMargins& margins = fill_margins[v]; + Pixels generated; + if (margins.Any()) { + const std::uint32_t wide_width = view.width + margins.left + margins.right; + generated.resize(std::size_t{view.width} * view.height); + for (std::uint32_t y = 0; y < view.height; ++y) { + std::copy_n(wide.begin() + (std::size_t{y + margins.up} * wide_width + margins.left), view.width, + generated.begin() + std::size_t{y} * view.width); + } + } else { + generated = wide; + } + if (reveal) { + // The frame turned to its truth's pose, as a runtime turns it. + const bool images = !g_replay_output.empty() && v == 0 && !crop; + Pixels black, filled; + const ReplayReveal turned = RevealTurned(view, wide, margins, frames[truth_frame].views[v], + images ? &black : nullptr, images ? &filled : nullptr); + const auto a = ReplayRotation(view.newest.orientation); + const auto b = ReplayRotation(frames[truth_frame].views[v].newest.orientation); + double trace = 0.0; + for (int i = 0; i < 3; ++i) { + for (int j = 0; j < 3; ++j) trace += a[j][i] * b[j][i]; + } + const double turned_degrees = std::acos(std::clamp((trace - 1.0) / 2.0, -1.0, 1.0)) * 57.29577951308232; + const double revealed = turned.pixels ? 100.0 * turned.revealed / turned.pixels : 0.0; + const double beyond = turned.pixels ? 100.0 * turned.beyond / turned.pixels : 0.0; + std::printf("frame %zu view %zu edge reveal: turned %.2f degrees to the truth's pose; %.2f%% of the view " + "revealed (%.2f%% past the margins too); there error black %.4f, %s %.4f\n", + k, v, turned_degrees, revealed, beyond, turned.black_error, + margins.Any() ? "filled" : "unfilled", turned.fill_error); + reveal_sums.revealed += revealed; + reveal_sums.beyond += beyond; + reveal_sums.black += turned.black_error * turned.revealed; + reveal_sums.filled += turned.fill_error * turned.revealed; + reveal_sums.pixels += static_cast(turned.revealed); + reveal_sums.degrees += turned_degrees; + ++reveal_sums.views; + if (images) { + WriteHalfPpm(prefix + "-reveal-black.ppm", black, frames[truth_frame].views[v].width, + frames[truth_frame].views[v].height); + WriteHalfPpm(prefix + "-reveal-filled.ppm", filled, frames[truth_frame].views[v].width, + frames[truth_frame].views[v].height); + WriteHalfPpm(prefix + "-reveal-truth.ppm", frames[truth_frame].views[v].color, + frames[truth_frame].views[v].width, frames[truth_frame].views[v].height); + if (margins.Any()) { + WriteHalfPpm(prefix + "-wide.ppm", wide, view.width + margins.left + margins.right, + view.height + margins.up + margins.down); + } + } + } std::vector valid; const Pixels truth = TurnTo(view, frames[truth_frame].views[v], valid, cubic_truth); const std::vector depth_edges = ReplayDepthEdges(view); @@ -8762,10 +9147,7 @@ int RunReplay(Harness& harness) { const auto jit_images = [&](std::size_t frame) { std::vector images; for (std::size_t v = 0; v < view_count; ++v) { - const ReplayView& view = frames[frame].views[v]; - images.push_back({outputs[v], 0, 0, - {{0, 0}, {static_cast(view.width), static_cast(view.height)}}, - output_format}); + images.push_back(output_image(outputs[v], v, frames[frame].views[v], output_format)); } return images; }; @@ -8924,6 +9306,14 @@ int RunReplay(Harness& harness) { if (delta_pairs) { std::printf("Paired still appSpaceDeltaPose (FG_REPLAY_DELTA_PAIRS=still): %zu views\n", paired_still); } + if (reveal && reveal_sums.views) { + const double n = static_cast(reveal_sums.views); + std::printf("Edge reveal: %zu views turned %.2f degrees on average to their truth's pose; %.2f%% of a view " + "revealed (%.2f%% past the margins too); there mean error black %.4f, %s %.4f\n", + reveal_sums.views, reveal_sums.degrees / n, reveal_sums.revealed / n, reveal_sums.beyond / n, + reveal_sums.pixels ? reveal_sums.black / reveal_sums.pixels : 0.0, edge_fill ? "filled" : "unfilled", + reveal_sums.pixels ? reveal_sums.filled / reveal_sums.pixels : 0.0); + } std::printf("Output hash: %zu frames written %016llx, %zu depth reads %016llx\n", hashed_frames, static_cast(output_hash), hashed_depths, static_cast(depth_hash)); if (evaluated) { @@ -10203,8 +10593,8 @@ void SubstitutionTest(ID3D12Device* device, ID3D12CommandQueue* game_queue) { // releases go at once (the runtime's queue waiting for the game's work); // after, a release whose work has not run is held back, a frame showing the // layer does not get it and the runtime's queue never waits for the game's -// work, and the game's next wait first waits for it on the CPU (then it -// goes, before the wait); only once that takes too long does the runtime's +// work, and the game's next wait for the image the runtime shows first waits +// for it on the CPU (then it goes, before the wait); only once that takes too long does the runtime's // queue wait instead. Off by default: a frame showing it so changes nothing. void SubstitutionShownTest(ID3D12Device* device, ID3D12CommandQueue* game_queue) { if (!StallChecksAllowed()) { @@ -10244,6 +10634,10 @@ void SubstitutionShownTest(ID3D12Device* device, ID3D12CommandQueue* game_queue) const XrSwapchainImageWaitInfo wait{XR_TYPE_SWAPCHAIN_IMAGE_WAIT_INFO, nullptr, XR_INFINITE_DURATION}; const XrSwapchainImageReleaseInfo release{XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO}; g_strict = {}; + // Two images: each wait below is for the image the runtime shows (with + // more, a wait for another is held back behind the release instead: + // framegen_runtime_queue_tests' ShownLayerPipelinedWaitTest). + g_strict.images = 2; ComPtr busy; ComPtr idle; ComPtr marks; @@ -12715,6 +13109,7 @@ struct LeadFrame { std::array, 2> measured; std::array, 2> labels; double measured_ms{0.0}; + std::array pass_ms{}; // downsample, search, refine }; std::vector TraceLeadFrames(Harness& harness, const EngineFrames& frames, EngineRunOptions options, @@ -12730,8 +13125,10 @@ std::vector TraceLeadFrames(Harness& harness, const EngineFrames& fra } LeadFrame& frame = trace[k]; const std::vector times = gpu.PassMilliseconds(); + std::size_t p = 0; for (const FrameGenPass pass : {FrameGenPass::kDownsample, FrameGenPass::kSearch, FrameGenPass::kRefine}) { - frame.measured_ms += static_cast(pass) < times.size() ? times[static_cast(pass)] : 0.0; + frame.pass_ms[p] = static_cast(pass) < times.size() ? times[static_cast(pass)] : 0.0; + frame.measured_ms += frame.pass_ms[p++]; } frame.merged = merged[k][0] && merged[k][1]; for (int e = 0; e < 2 && frame.merged; ++e) { @@ -12752,6 +13149,15 @@ void EngineLeadScenarios(Harness& harness) { const EngineFrames still = RenderEngineFrames(EngineScene{}); std::vector> off; RunEngineFrames(harness, still, {.mode = FrameGenEngineMode::kOff, .outputs = &off}); + // The median of a run's frames' measured passes (frames after the first). + const auto median_ms = [](std::vector times) { + if (times.empty()) return 0.0; + std::nth_element(times.begin(), times.begin() + times.size() / 2, times.end()); + return times[times.size() / 2]; + }; + // The measured passes where the verdict held the vectors back: run, but + // predicated as where they are skipped (below). + double held_ms = 0.0; // Every frame's verdict holding the vectors back: the measured flow // copied bit for bit, the generated frames mk1's. @@ -12763,10 +13169,12 @@ void EngineLeadScenarios(Harness& harness) { harness, still, {.mode = FrameGenEngineMode::kLead, .fault = EngineFault::kReversed, .outputs = &reversed, .led = &led}, &run); std::size_t merged = 0, held = 0, copied = 0, frames_led = 0; + std::vector held_times; for (std::size_t k = 0; k < trace.size(); ++k) { if (!trace[k].merged) continue; ++merged; held += trace[k].held ? 1 : 0; + if (k > 0 && trace[k].held) held_times.push_back(trace[k].measured_ms); copied += trace[k].held && trace[k].flow == trace[k].measured ? 1 : 0; frames_led += led[k] ? 1 : 0; } @@ -12774,9 +13182,10 @@ void EngineLeadScenarios(Harness& harness) { for (std::size_t k = 0; k < off.size() && k < reversed.size(); ++k) { for (int e = 0; e < 2; ++e) differing += off[k][e] == reversed[k][e] ? 0 : 1; } + held_ms = median_ms(held_times); std::cout << "D3D12 engine lead: reversed vectors: " << DescribeEngineRun(run) << "; " << merged << " frames merged, " - << frames_led << " led, " << held << " held back, " << copied << " the measured flow bit for bit; " - << differing << " generated frames differ from mk1's" << std::endl; + << frames_led << " led, " << held << " held back (the measured passes " << held_ms << " ms), " << copied + << " the measured flow bit for bit; " << differing << " generated frames differ from mk1's" << std::endl; Check(merged > kEngineFrames / 2 && frames_led == merged && held == merged && copied == merged && differing == 0, "engine lead: frames whose verdict holds the vectors back are not mk1's (" + std::to_string(held) + " held of " + std::to_string(merged) + ", " + std::to_string(copied) + " copied, " + std::to_string(differing) + @@ -12847,17 +13256,23 @@ void EngineLeadScenarios(Harness& harness) { {.mode = FrameGenEngineMode::kLead, .before = [](FrameGenTuning& tuning, std::size_t) { tuning.engine_predicate = false; }}); double lead_ms = 0.0, assist_ms = 0.0, unmeasured_ms = 0.0; + std::array lead_pass{}, assist_pass{}; + std::vector lead_times, assist_times; std::size_t valid = 0, assisted = 0, pairs = 0, kept = 0, compared = 0, same = 0; bool all_valid = true; for (std::size_t k = 1; k < lead.size(); ++k) { if (assist[k].merged) { assist_ms += assist[k].measured_ms; + for (std::size_t p = 0; p < 3; ++p) assist_pass[p] += assist[k].pass_ms[p]; + assist_times.push_back(assist[k].measured_ms); ++assisted; } unmeasured_ms += unmeasured[k].merged ? unmeasured[k].measured_ms : 0.0; if (!lead[k].merged || lead[k].held) continue; ++valid; lead_ms += lead[k].measured_ms; + for (std::size_t p = 0; p < 3; ++p) lead_pass[p] += lead[k].pass_ms[p]; + lead_times.push_back(lead[k].measured_ms); // Skipped: the measured flow as the frame before left it. if (lead[k - 1].merged && !lead[k - 1].held) { ++pairs; @@ -12873,6 +13288,13 @@ void EngineLeadScenarios(Harness& harness) { } lead_ms /= std::max(valid, 1); assist_ms /= std::max(assisted, 1); + for (std::size_t p = 0; p < 3; ++p) { + lead_pass[p] /= std::max(valid, 1); + assist_pass[p] /= std::max(assisted, 1); + } + std::cout << "D3D12 engine lead: downsample, search, refine " << lead_pass[0] << ", " << lead_pass[1] << ", " + << lead_pass[2] << " ms skipped against " << assist_pass[0] << ", " << assist_pass[1] << ", " + << assist_pass[2] << " ms run" << std::endl; std::cout << "D3D12 engine lead: " << DescribeEngineRun(run) << "; " << valid << " frames led with a verdict letting " << "the vectors be used: the measured passes " << lead_ms << " ms (Assist " << assist_ms << " ms), " << kept << " of " << pairs << " frames after one such left the measured flow as it was, " << same << " of " @@ -12881,9 +13303,23 @@ void EngineLeadScenarios(Harness& harness) { Check(valid > kEngineFrames / 2 && pairs > 10 && kept == pairs, "engine lead: the measured passes ran where the verdict let the vectors be used (" + std::to_string(kept) + " of " + std::to_string(pairs) + " frames kept the measured flow)"); - Check(assist_ms > 0.0 && lead_ms <= 0.1 * assist_ms, - "engine lead: the skipped passes' stamps are not about 0 ms (" + std::to_string(lead_ms) + " ms against " + - std::to_string(assist_ms) + ")"); + // About 0 ms against Assist's: within 10% of them, beyond what + // predication itself costs. Predicated passes carry a fixed cost of + // their own whether they run or not (the held-back frames' passes + // took about 0.013 ms more than Assist's on an RTX 5090, and the + // skipped downsample's stamp about as much; at this size Assist's + // passes take only about 0.17 ms there, so that cost alone came to + // 10%). It is counted as the held-back median over Assist's, up to + // 20% of Assist's. Passes that ran where they should be skipped + // cost what they do in Assist. + const double lead_median = median_ms(lead_times), assist_median = median_ms(assist_times); + const double predication_ms = std::clamp(held_ms - assist_median, 0.0, 0.2 * assist_median); + std::cout << "D3D12 engine lead: medians: skipped " << lead_median << " ms, Assist " << assist_median + << " ms, held back " << held_ms << " ms (predication " << predication_ms << " ms)" << std::endl; + Check(assist_median > 0.0 && lead_median <= 0.1 * assist_median + predication_ms, + "engine lead: the skipped passes' stamps are not about 0 ms (median " + std::to_string(lead_median) + + " ms against Assist's " + std::to_string(assist_median) + " ms, predication " + + std::to_string(predication_ms) + " ms)"); Check(compared > 10 && same == compared && unmeasured_ms == 0.0, "engine lead: the merged flow read the measured flow it skipped (" + std::to_string(same) + " of " + std::to_string(compared) + " as without it)"); diff --git a/layer/tests/framegen_presenter_tests.cpp b/layer/tests/framegen_presenter_tests.cpp index e919618e..3171c1ce 100644 --- a/layer/tests/framegen_presenter_tests.cpp +++ b/layer/tests/framegen_presenter_tests.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -330,6 +331,12 @@ struct GpuLog { }; std::mutex syntheses_mutex; std::vector syntheses; + // The output images each Synthesize was given (under syntheses_mutex). + std::vector> synthesis_outputs; + // Edge fill (SupportsEdgeFill): the backend fills output images' margins. + bool edge_fill{false}; + // EdgesFilled: false as when the backend copies frames in (margins not drawn). + std::atomic edges_filled{true}; std::atomic synthesis_failures{0}; // SynthesizeStale: supported, written, and asked of a motion frame that // was not the frame's previous one or not measured yet. @@ -436,6 +443,8 @@ class FakeGpu final : public FrameGenGpu { public: explicit FakeGpu(GpuLog* log) : log_(log) {} const char* Name() const override { return "Fake"; } + bool SupportsEdgeFill() const override { return log_->edge_fill; } + bool EdgesFilled() const override { return log_->edges_filled; } std::string HardwareFlowStatus() const override { return log_->hardware_available ? "active" : "fake GPU has no optical flow hardware"; } @@ -715,6 +724,7 @@ class FakeGpu final : public FrameGenGpu { { std::scoped_lock lock(log_->syntheses_mutex); log_->syntheses.push_back({frame, mode, fraction, now}); + log_->synthesis_outputs.push_back(outputs); } // Seen from the eye's position at the frame's time, as the presenter // places the views (where depth is used). @@ -1154,6 +1164,18 @@ struct Runtime { // Every Nth wait misses a refresh: it is released a refresh later than // asked for (paced runtimes only; 0: none). std::atomic miss_every{0}; + // Throttle::kPeriod reports the throttled period one wait late, as + // PimaxXR does with Pimax's Smart Smoothing (its frame duration is + // updated in xrBeginFrame, the next xrWaitFrame reports it; pimax-openxr + // frame.cpp): the first held wait still reports the refresh period, the + // first wait after the hold ends still twice it. + bool period_one_late{false}; // set before the first wait + bool period_throttled{false}; // under g_xr_mutex: what the next wait reports + // The next waits (paced runtimes only) report a display time 1 ns after + // the last, still blocking a refresh each: PimaxXR clamps a prediction of + // PVR's that did not advance (pimax-openxr frame.cpp xrWaitFrame). + std::atomic clamp_next{0}; + std::atomic clamped_waits{0}; // ...and reports twice the period for that wait, as SteamVR with Pimax's // driver does after its compositor misses a refresh. std::atomic miss_reports_multiple{false}; @@ -1232,6 +1254,9 @@ struct Runtime { std::vector submitted_times; std::vector submitted_at; std::vector> submitted_swapchains; + // Each submission's first layer's projection views as submitted (their + // `next` pointers are not followed). + std::vector> submitted_views; std::vector submitted_depth; std::vector submitted_left_x; // the first projection view's position @@ -1315,15 +1340,25 @@ struct Runtime { if (const XrDuration lag = lag_next.exchange(0)) { display = std::max(clock_floor + kClockCreep, display - lag); } + if (clamp_next > 0 && clock_floor > 0) { + --clamp_next; + ++clamped_waits; + display = clock_floor + 1; + } if (display <= clock_floor || (stall_clock && clock_floor > 0)) { display = clock_floor + kClockCreep; ++crept_waits; } clock_floor = display; } - return report(display, throttling && throttle == Throttle::kPeriod ? throttle_refreshes * period - : miss && miss_reports_multiple ? 2 * period - : period); + bool reported_throttled = throttling; + if (period_one_late) { + std::scoped_lock lock(g_xr_mutex); + reported_throttled = std::exchange(period_throttled, throttling); + } + return report(display, reported_throttled && throttle == Throttle::kPeriod ? throttle_refreshes * period + : miss && miss_reports_multiple ? 2 * period + : period); } return report(next_display.fetch_add(period), period); }; @@ -1407,6 +1442,12 @@ struct Runtime { left_x = projection->viewCount ? projection->views[0].pose.position.x : 0.0f; } submitted_swapchains.push_back(swapchains); + if (info->layerCount) { + const auto* projection = reinterpret_cast(info->layers[0]); + submitted_views.emplace_back(projection->views, projection->views + projection->viewCount); + } else { + submitted_views.emplace_back(); + } submitted_depth.push_back(depth); submitted_left_x.push_back(left_x); log.push_back({info->displayTime, swapchains, info->layerCount, releases}); @@ -1788,6 +1829,191 @@ void TestOutputsKeptAcrossMultipliers() { Check(!runtime.ordering_violation && !g_release_violation, "outputs: calls stayed ordered"); } +// Edge fill: the views of a submission shown from output images widened by +// `margins` (the game's views: ±0.8 rad, 1600 x 1600): each image whole, its +// field of view the game's carried on across the margins at the game's +// pixel spacing, and no depth. +bool ShowsFilled(const std::vector& views, const FrameGenEdgeMargins& margins) { + const double spacing = (std::tan(0.8) - std::tan(-0.8)) / 1600.0; + for (const XrCompositionLayerProjectionView& view : views) { + const XrRect2Di& rect = view.subImage.imageRect; + const double tl = std::tan(double(view.fov.angleLeft)), tr = std::tan(double(view.fov.angleRight)); + const double tu = std::tan(double(view.fov.angleUp)), td = std::tan(double(view.fov.angleDown)); + if (rect.offset.x != 0 || rect.offset.y != 0 || + rect.extent.width != static_cast(1600 + margins.left + margins.right) || + rect.extent.height != static_cast(1600 + margins.up + margins.down) || + std::abs((tr - tl) / rect.extent.width - spacing) > 1e-6 * spacing * 10 || + std::abs((tu - td) / rect.extent.height - spacing) > 1e-6 * spacing * 10 || + std::abs(tl - (std::tan(-0.8) - margins.left * spacing)) > 1e-5 || + std::abs(tu - (std::tan(0.8) + margins.up * spacing)) > 1e-5 || view.next) { + return false; + } + } + return !views.empty(); +} + +// The margins an output image gets for the test app's views: as asked +// (FrameGenEdgeFillMargins), each rounded up to whole 32-pixel tiles. +FrameGenEdgeMargins TestAppMargins() { + FrameGenEdgeMargins margins = + FrameGenEdgeFillMargins({-0.8f, 0.8f, 0.8f, -0.8f}, {1600, 1600}, kFrameGenEdgeFillHorizontalDegrees, + kFrameGenEdgeFillVerticalDegrees); + for (std::uint32_t* margin : {&margins.left, &margins.right, &margins.up, &margins.down}) { + *margin = (*margin + 31) / 32 * 32; + } + return margins; +} + +// Edge fill's margins: degrees past each edge at the frame's own pixel +// spacing (the outer edge of a wide view needs far more pixels than its inner +// one), the widened field of view exactly what the margins' pixels show, +// within half the frame and the largest image; the research switch's values. +void TestEdgeFillMargins() { + // A Pimax Crystal Super left eye (round 1): 63.5 degrees out, 51.5 in, + // 58.7 up and down, 6240 x 6280. + constexpr double kDegree = 3.14159265358979323846 / 180.0; + const FrameGenFov pimax{static_cast(-63.5 * kDegree), static_cast(51.5 * kDegree), + static_cast(58.7 * kDegree), static_cast(-58.7 * kDegree)}; + const FrameGenExtent extent{6240, 6280}; + const FrameGenEdgeMargins margins = FrameGenEdgeFillMargins(pimax, extent, 3.0f, 1.5f); + Check(margins.left > 2 * margins.right && margins.up == margins.down && margins.up > 0, + "edge fill margins: the outer edge needs the widest margin (" + std::to_string(margins.left) + "/" + + std::to_string(margins.right) + "/" + std::to_string(margins.up) + ")"); + const FrameGenFov widened = FrameGenEdgeFillFov(pimax, extent, margins); + Check(widened.left <= pimax.left - 3.0f * kDegree + 1e-4 && widened.left > pimax.left - 3.3f * kDegree && + widened.right >= pimax.right + 3.0f * kDegree - 1e-4 && widened.up >= pimax.up + 1.5f * kDegree - 1e-4 && + widened.down <= pimax.down - 1.5f * kDegree + 1e-4, + "edge fill margins: each edge reaches its degrees, no more than a pixel past them"); + const double spacing = (std::tan(double(pimax.right)) - std::tan(double(pimax.left))) / extent.width; + const double widened_spacing = (std::tan(double(widened.right)) - std::tan(double(widened.left))) / + (extent.width + margins.left + margins.right); + Check(std::abs(widened_spacing / spacing - 1.0) < 1e-5, "edge fill margins: the margins keep the frame's pixel spacing"); + Check(!FrameGenEdgeFillMargins(pimax, extent, 0.0f, 0.0f).Any(), "edge fill margins: none for no degrees"); + Check(!FrameGenEdgeFillMargins({0.5f, -0.5f, 0.5f, -0.5f}, extent, 3.0f, 3.0f).Any(), + "edge fill margins: none for a field of view that is not one"); + const FrameGenEdgeMargins capped = FrameGenEdgeFillMargins(pimax, {16000, 16000}, 15.0f, 15.0f); + Check(16000 + capped.left + capped.right <= kFrameGenEdgeFillMaxExtent && + 16000 + capped.up + capped.down <= kFrameGenEdgeFillMaxExtent, + "edge fill margins: the image fits the largest texture"); + const FrameGenEdgeMargins halved = FrameGenEdgeFillMargins({-1.4f, 1.4f, 1.4f, -1.4f}, {100, 100}, 15.0f, 15.0f); + Check(halved.left <= 50 && halved.up <= 50, "edge fill margins: no wider than half the frame"); + Check(ParseFrameGenEdgeFillDegrees("4") == std::array{4.0f, 4.0f} && + ParseFrameGenEdgeFillDegrees("4,2.5") == std::array{4.0f, 2.5f} && + !ParseFrameGenEdgeFillDegrees("") && !ParseFrameGenEdgeFillDegrees("20") && + !ParseFrameGenEdgeFillDegrees("-1,2") && !ParseFrameGenEdgeFillDegrees("on"), + "edge fill margins: the research switch's values"); +} + +// Edge fill on the frames written inline (Interpolate at 2x): generated +// frames go out in output images wider than the game's by a margin each +// side, the frame placed at the margins' corner (FrameGenImage::content), +// submitted with the field of view widened to match and without the game's +// depth; the game's own frames are as it submitted them. Without a backend +// that fills margins, or turned off live, the outputs are the game's size. +void TestEdgeFill() { + const FrameGenEdgeMargins asked = + FrameGenEdgeFillMargins({-0.8f, 0.8f, 0.8f, -0.8f}, {1600, 1600}, kFrameGenEdgeFillHorizontalDegrees, + kFrameGenEdgeFillVerticalDegrees); + // 3 degrees past tan 1.03 at 1600 px across tangents 2.06: ~88 px; 1.5 + // degrees vertically ~43 px. + Check(asked.left == asked.right && asked.up == asked.down && asked.left >= 80 && asked.left <= 96 && + asked.up >= 38 && asked.up <= 48, + "edge fill: margins for the test view (" + std::to_string(asked.left) + ", " + std::to_string(asked.up) + ")"); + FrameGenResolvedSettings settings; + settings.enabled = true; + settings.synthesis_mode = FrameGenSynthesisMode::kInterpolate; + settings.multiplier = FrameGenMultiplier::k2; + settings.edge_fill = true; + { + // A backend that cannot fill margins: outputs at the frame's size. + FrameGenPresenter presenter; + GpuLog gpu; + Runtime runtime; + StartPresenter(presenter, gpu, FrameGenSynthesisMode::kInterpolate); + presenter.Configure(settings); + App app(presenter, runtime); + for (int i = 0; i < 6; ++i) app.Frame(); + Check(g_creates == 2 && g_create_sizes[0] == std::make_pair(1600u, 1600u), + "edge fill: without the backend's support the outputs are the frame's size"); + Check(!gpu.last.empty() && gpu.last[0].outputs[0].content.x == 0 && gpu.last[0].outputs[0].content.y == 0, + "edge fill: without the backend's support the frame fills its output"); + } + FrameGenPresenter presenter; + GpuLog gpu; + gpu.edge_fill = true; + Runtime runtime; + StartPresenter(presenter, gpu, FrameGenSynthesisMode::kInterpolate); + presenter.Configure(settings); + App app(presenter, runtime); + for (int i = 0; i < 6; ++i) app.Frame(); + const FrameGenEdgeMargins margins = TestAppMargins(); + Check(g_creates == 2 && g_create_sizes[0].first == 1600 + margins.left + margins.right && + g_create_sizes[0].second == 1600 + margins.up + margins.down, + "edge fill: each eye's output is the frame and its margins (" + std::to_string(g_create_sizes[0].first) + "x" + + std::to_string(g_create_sizes[0].second) + ")"); + Check(!gpu.last.empty() && gpu.last[0].outputs[0].content.x == static_cast(margins.left) && + gpu.last[0].outputs[0].content.y == static_cast(margins.up) && + gpu.last[0].outputs[0].rect.extent.width == static_cast(g_create_sizes[0].first) && + gpu.last[1].outputs[0].content.x == static_cast(margins.left), + "edge fill: the backend is told where the frame lies in its output"); + std::size_t generated = 0, real = 0; + { + std::scoped_lock lock(g_xr_mutex); + for (std::size_t i = 0; i < runtime.submitted_views.size(); ++i) { + const auto& views = runtime.submitted_views[i]; + if (views.empty()) continue; + if (IsAppImage(runtime.submitted_swapchains[i])) { + ++real; + Check(views[0].fov.angleLeft == -0.8f && views[0].subImage.imageRect.extent.width == 1600, + "edge fill: the game's own frames keep its field of view"); + } else { + ++generated; + Check(ShowsFilled(views, margins) && !runtime.submitted_depth[i], + "edge fill: a generated frame is shown widened across its margins, without depth"); + } + } + } + Check(generated >= 2 && real >= 2, "edge fill: generated and real frames were shown"); + // A frame the backend copied in (its margin not drawn: the copy fallback) + // is shown from its own region with the game's field of view: what the + // margin holds never reaches the runtime. + gpu.edges_filled = false; + std::size_t before = 0; + { + std::scoped_lock lock(g_xr_mutex); + before = runtime.submitted_views.size(); + } + for (int i = 0; i < 4; ++i) app.Frame(); + std::size_t unfilled = 0; + { + std::scoped_lock lock(g_xr_mutex); + for (std::size_t i = before; i < runtime.submitted_views.size(); ++i) { + const auto& views = runtime.submitted_views[i]; + if (views.empty() || IsAppImage(runtime.submitted_swapchains[i])) continue; + ++unfilled; + for (const XrCompositionLayerProjectionView& view : views) { + const XrRect2Di& rect = view.subImage.imageRect; + Check(rect.offset.x == static_cast(margins.left) && + rect.offset.y == static_cast(margins.up) && rect.extent.width == 1600 && + rect.extent.height == 1600 && view.fov.angleLeft == -0.8f && view.fov.angleRight == 0.8f && + view.fov.angleUp == 0.8f && view.fov.angleDown == -0.8f, + "edge fill: a frame copied in is shown from its own region with the game's field of view"); + } + } + } + Check(unfilled >= 2, "edge fill: frames copied in were shown"); + gpu.edges_filled = true; + // Off while running: the next frames' outputs are the frame's size again. + settings.edge_fill = false; + presenter.Configure(settings); + for (int i = 0; i < 4; ++i) app.Frame(); + Check(g_creates == 4 && g_create_sizes[2] == std::make_pair(1600u, 1600u), + "edge fill: turned off, the outputs are made anew at the frame's size (" + std::to_string(g_creates) + ")"); + Check(gpu.last[0].outputs[0].content.x == 0 && gpu.last[0].outputs[0].rect.extent.width == 1600, + "edge fill: turned off, the frame fills its output"); + Check(!runtime.ordering_violation && !g_release_violation, "edge fill: calls stayed ordered"); +} + void TestSubmissionSpacing() { FrameGenPresenter presenter; GpuLog gpu; @@ -2222,6 +2448,42 @@ struct HorizonLog { } }; +// A session log of its own in the temporary folder (not beside this +// executable: on a worktree whose build is the installed layer that is the +// active layer folder), read back once the presenter has stopped. +struct SessionLog { + std::filesystem::path directory; + std::unique_ptr logger{std::make_unique()}; + std::filesystem::path path; // the log, once Lines closed it + + explicit SessionLog(const std::string& name) + : directory(std::filesystem::temp_directory_path() / + ("vectorxr-presenter-test-log-" + std::to_string(GetCurrentProcessId()) + "-" + name)) { + std::error_code ignored; + std::filesystem::remove_all(directory, ignored); + logger->Initialize(directory / "session.log"); + } + ~SessionLog() { + logger.reset(); + std::error_code ignored; + std::filesystem::remove_all(directory, ignored); + } + // The lines holding `text` (closes the log: call once the presenter has + // stopped). Later calls read the closed log again. + std::vector Lines(const std::string& text) { + if (logger) { + path = logger->ActiveLogPath(); + logger.reset(); // flushed + } + std::ifstream file(path); + std::vector lines; + for (std::string line; std::getline(file, line);) { + if (line.find(text) != std::string::npos) lines.push_back(line); + } + return lines; + } +}; + // A summary's "key=low/high" (or "key=value": both the same); none when // missing or "-". std::optional> SummaryValues(const std::string& line, const std::string& key) { @@ -2819,6 +3081,48 @@ void TestThreadedMsfsTiming() { Check(!runtime.ordering_violation && !g_release_violation, "msfs timing: calls stayed ordered"); } +// OpenXR Toolkit's prediction dampening above VectorXR: the game is handed +// now + d * (time - now) and ends its frames for that, which OpenXR Toolkit +// never restores (its layer.cpp xrWaitFrame/xrEndFrame), so frames end for an +// earlier time than VectorXR gave (here a period of the game's earlier). That is not a game convention to compensate (as MSFS's +// period later is): engaged frames keep being told their slot's time. The +// game is told the same times as one ending at the time it was given, while +// MSFS's frames (a period later) are still told a period earlier. +void TestDampenedEndsAreNotCompensated() { + const auto told_lead = [](int end_offset_periods) { + FrameGenPresenter presenter; + GpuLog gpu; + Runtime runtime; + runtime.paced = true; + StartThreaded(presenter, gpu, FrameGenMultiplier::k2); + App app(presenter, runtime); + app.end_offset_periods = end_offset_periods; + RunPipelined(app, 600ms, 8ms, 10ms); + Check(presenter.Status().threaded, "dampened ends: presents from its own thread"); + std::vector leads; + RunPipelined(app, 1200ms, 8ms, 10ms, [&](int, const XrFrameState& state) { + std::scoped_lock lock(g_xr_mutex); + if (!runtime.submitted_times.empty()) leads.push_back(state.predictedDisplayTime - runtime.submitted_times.back()); + }); + presenter.Stop(); + Check(!runtime.ordering_violation && !g_release_violation, "dampened ends: calls stayed ordered"); + std::sort(leads.begin(), leads.end()); + Check(!leads.empty(), "dampened ends: frames ended"); + return leads[leads.size() / 2]; + }; + const XrDuration as_given = told_lead(0); + const XrDuration dampened = told_lead(-1); + const XrDuration msfs = told_lead(1); + const auto periods = [](XrDuration duration) { return std::to_string(static_cast(duration) / kPeriod); }; + Check(std::abs(dampened - as_given) < kPeriod / 2, + "dampened ends: frames ending earlier than given are told the same times (" + periods(dampened) + " against " + + periods(as_given) + " periods ahead)"); + // At 2x the game's period is two refreshes. + Check(std::abs(as_given - msfs - 2 * kPeriod) < kPeriod / 2, + "dampened ends: frames ending a period later are still told a period earlier (" + periods(msfs) + + " against " + periods(as_given) + " periods ahead)"); +} + void TestThreadedPauseRetryMsfsTiming() { FrameGenPresenter presenter; GpuLog gpu; @@ -3598,6 +3902,107 @@ void TestJitExtrapolation() { Check(!runtime.ordering_violation && !g_release_violation, "jit extrapolation: calls stayed ordered"); } +// Edge fill just in time (Extrapolate at 3x): every frame the presenter +// thread writes, real frames' exact copies too, goes to an output widened +// by the margins, the frame at their corner, and is shown with the field of +// view widened to match, never with the game's depth. +void TestJitEdgeFill() { + FrameGenPresenter presenter; + GpuLog gpu; + gpu.edge_fill = true; + Runtime runtime; + runtime.paced = true; + std::unique_ptr owned; + App* app = nullptr; + StartJit(presenter, gpu, FrameGenSynthesisMode::kExtrapolate, FrameGenMultiplier::k3, app, runtime, owned); + FrameGenResolvedSettings settings; + settings.enabled = true; + settings.synthesis_mode = FrameGenSynthesisMode::kExtrapolate; + settings.multiplier = FrameGenMultiplier::k3; + settings.edge_fill = true; + presenter.Configure(settings); + for (int i = 0; i < 3; ++i) app->Frame(); + const std::size_t first = runtime.Submissions(); + const auto start = std::chrono::steady_clock::now(); + while (std::chrono::steady_clock::now() - start < 700ms) { + const XrFrameState state = app->Wait(); + app->Begin(); + std::this_thread::sleep_for(4ms); + app->Release(); + Check(XR_SUCCEEDED(app->End(state.predictedDisplayTime)), "jit edge fill: end"); + } + const FrameGenStatus status = presenter.Status(); + Check(status.threaded && status.state == FrameGenState::kActive, "jit edge fill: presents from its own thread"); + const FrameGenEdgeMargins margins = TestAppMargins(); + std::size_t outputs = 0, misplaced = 0; + { + std::scoped_lock lock(gpu.syntheses_mutex); + for (const auto& images : gpu.synthesis_outputs) { + for (const FrameGenImage& image : images) { + ++outputs; + misplaced += image.content.x != static_cast(margins.left) || + image.content.y != static_cast(margins.up) || + image.rect.extent.width != static_cast(1600 + margins.left + margins.right) + ? 1 + : 0; + } + } + } + Check(outputs > 20 && misplaced == 0, "jit edge fill: every frame written is placed inside its margins (" + + std::to_string(misplaced) + " of " + std::to_string(outputs) + " not)"); + std::size_t shown = 0, plain = 0; + { + std::scoped_lock lock(g_xr_mutex); + for (std::size_t i = first + 10; i < runtime.submitted_views.size(); ++i) { + if (runtime.submitted_views[i].empty()) continue; + ++shown; + plain += ShowsFilled(runtime.submitted_views[i], margins) && !runtime.submitted_depth[i] ? 0 : 1; + } + } + Check(shown > 20 && plain == 0, "jit edge fill: every refresh is shown widened across the margins (" + + std::to_string(plain) + " of " + std::to_string(shown) + " not)"); + int creates = 0; + { + std::scoped_lock lock(g_xr_mutex); + creates = g_creates; + } + Check(creates == 2, "jit edge fill: one output per eye, made once (" + std::to_string(creates) + ")"); + // Frames the backend copied in: shown from their own region, unwidened. + gpu.edges_filled = false; + std::size_t copied_from = 0; + { + std::scoped_lock lock(g_xr_mutex); + copied_from = runtime.submitted_views.size(); + } + const auto copied_start = std::chrono::steady_clock::now(); + while (std::chrono::steady_clock::now() - copied_start < 300ms) { + const XrFrameState state = app->Wait(); + app->Begin(); + std::this_thread::sleep_for(4ms); + app->Release(); + Check(XR_SUCCEEDED(app->End(state.predictedDisplayTime)), "jit edge fill: end"); + } + std::size_t copied_shown = 0, widened = 0; + { + std::scoped_lock lock(g_xr_mutex); + // A few refreshes in: frames written before the switch may still show. + for (std::size_t i = copied_from + 6; i < runtime.submitted_views.size(); ++i) { + const auto& views = runtime.submitted_views[i]; + if (views.empty()) continue; + ++copied_shown; + const XrRect2Di& rect = views[0].subImage.imageRect; + widened += rect.offset.x == static_cast(margins.left) && rect.extent.width == 1600 && + views[0].fov.angleLeft == -0.8f + ? 0 + : 1; + } + } + Check(copied_shown > 5 && widened == 0, "jit edge fill: frames copied in are shown from their own region (" + + std::to_string(widened) + " of " + std::to_string(copied_shown) + " not)"); + presenter.Stop(); + Check(!runtime.ordering_violation && !g_release_violation, "jit edge fill: calls stayed ordered"); +} + // Interpolate at 3x just in time: the presenter thread shows every refresh, // each real frame's own refresh its exact copy, the two refreshes before it // the frames between it and the previous one, and the game keeps its pace. @@ -6648,6 +7053,7 @@ void TestBenefitGuardBacksOff() { StartJit(presenter, gpu, test.mode, FrameGenMultiplier::kDynamic, app, runtime, owned); FrameGenDynamicPolicy policy = GuardDynamic(); test.isolate(policy); + policy.game_rate_share = 0.0; // (e): each fault slows the game too (TestBenefitGuardGameSlowedByEngaging) presenter.SetDynamicPolicy(policy); EngageDynamic(presenter, *app, name); RunTicks(*app, 300ms, {1, 2, 2}); @@ -6672,6 +7078,130 @@ void TestBenefitGuardBacksOff() { } } +// (e) The game itself slowed by engaging: a game passing through at 45 fps +// that runs at 17 engaged still shows 90 distinct frames a second, the +// generated ones filling every refresh, so (a)-(d) never fail; the guard +// passes frames straight through to compare, finds the game faster there, +// stands down (held as any stand-down), and the game runs at its own rate +// again. One that keeps most of its rate engaged (45 to 40, Frame +// Generation's own GPU cost) stays engaged; one whose scene slows it as +// much passing through is engaged again at once, without a stand-down. DCS +// on a 4080 SUPER through the Direct3D 11 bridge, 2026-10-22: 44-71 fps +// passing through, 17 engaged with the GPU half idle, engaged 90 s until +// Frame Generation was turned off. +void TestBenefitGuardGameSlowedByEngaging() { + struct Case { + const char* name; + std::chrono::microseconds engaged_work; + bool stands_down; + bool scene{false}; // the engaged work holds passing through too, once engaged + }; + const auto through_work = std::chrono::microseconds(19'000); // two refreshes: 45 fps passing through + const std::vector cases{ + {"slowed by engaging", std::chrono::microseconds(58'000), true}, + {"own cost engaged", std::chrono::microseconds(25'000), false}, + {"heavier scene", std::chrono::microseconds(58'000), false, true}, + }; + for (const FrameGenSynthesisMode mode : {FrameGenSynthesisMode::kInterpolate, FrameGenSynthesisMode::kExtrapolate}) { + for (const Case& test : cases) { + const std::string name = std::string(test.name) + + (mode == FrameGenSynthesisMode::kInterpolate ? " (interpolate)" : " (extrapolate)"); + FrameGenPresenter presenter; + GpuLog gpu; + Runtime runtime; + runtime.paced = true; + std::unique_ptr owned; + App* app = nullptr; + StartJit(presenter, gpu, mode, FrameGenMultiplier::kDynamic, app, runtime, owned); + FrameGenDynamicPolicy policy = GuardDynamic(); + policy.hold = 3000ms; // long enough to see the game's rate standing by + presenter.SetDynamicPolicy(policy); + // The game's frame takes its engaged time while Frame Generation + // presents from its thread, as the DCS frames did on its GPU. + bool heavier = false; + auto frames = [&](std::chrono::milliseconds duration, const std::function& until = {}) { + const auto start = std::chrono::steady_clock::now(); + int count = 0; + while (std::chrono::steady_clock::now() - start < duration) { + const XrFrameState state = app->Wait(); + app->Begin(); + heavier = heavier || (test.scene && Engaged(presenter)); + SleepPrecise(std::chrono::steady_clock::now() + + (heavier || Engaged(presenter) ? test.engaged_work : through_work)); + app->Release(); + Check(XR_SUCCEEDED(app->End(state.predictedDisplayTime)), name + ": end"); + ++count; + if (until && until()) break; + } + return count / Seconds(std::chrono::steady_clock::now() - start); + }; + frames(3000ms, [&] { return Engaged(presenter); }); + Check(Engaged(presenter), name + ": dynamic engages for a game at half the refresh rate"); + frames(4000ms, [&] { + const FrameGenStatus now = presenter.Status(); + return now.dynamic_stand_downs > 0 || now.dynamic_engagements > 1; + }); + frames(300ms); // engaging again takes a few frames + FrameGenStatus status = presenter.Status(); + if (test.scene) { + Check(Engaged(presenter) && status.dynamic_stand_downs == 0 && status.dynamic_engagements == 2, + name + ": engaged again at once, no stand-down (" + status.dynamic_reason + ")"); + } else if (!test.stands_down) { + Check(Engaged(presenter) && status.dynamic_stand_downs == 0 && status.dynamic_engagements == 1, + name + ": stays engaged (" + status.dynamic_reason + ")"); + } else { + Check(status.standby && status.dynamic_stand_downs == 1 && + status.dynamic_reason.find("engaged against") != std::string::npos, + name + ": stands down on the game's own rate (" + status.dynamic_reason + ")"); + frames(300ms); + const double after = frames(700ms); + Check(presenter.Status().standby && after > 35.0, + name + ": the game runs at its own rate again (" + std::to_string(after) + " fps)"); + } + presenter.Stop(); + Check(!runtime.ordering_violation && !g_release_violation, name + ": calls stayed ordered"); + } + } +} + +// A guard window left out (a pause, a loading frame) is not evidence for +// GPU-aware pacing either: a GPU stall's frames, captured seconds after +// their refresh before the game paused, are a hitch, not a game running +// frames ahead on its GPU (DCS 2026-10-22: a 2.5 s pause with frames +// "captured 2444.7 ms after their refresh" armed it for the session). +void TestGpuPacingIgnoresPausedWindows() { + SessionLog log("paused-windows"); + FrameGenPresenter presenter; + GpuLog gpu; + Runtime runtime; + runtime.paced = true; + std::unique_ptr owned; + App* app = nullptr; + StartJit(presenter, gpu, FrameGenSynthesisMode::kInterpolate, FrameGenMultiplier::kDynamic, app, runtime, owned, + log.logger.get()); + FrameGenDynamicPolicy policy = GuardDynamic(); + policy.distinct_margin_fps = -1000.0; // always winning: the pauses stand nothing down + presenter.SetDynamicPolicy(policy); + presenter.SetSummaryInterval(1000ms); // the summaries' windows are evidence too + EngageDynamic(presenter, *app, "paused windows"); + RunTicks(*app, 900ms, {1, 2, 2}); + Check(Engaged(presenter) && !presenter.Status().gpu_gating, "paused windows: engaged, not holding waits"); + for (int i = 0; i < 3; ++i) { + gpu.capture_lag = 2400ms; // the GPU stalls... + RunTicks(*app, 300ms, {1, 2, 2}); + gpu.capture_lag = 0us; + std::this_thread::sleep_for(2500ms); // ...and the game sends nothing + RunTicks(*app, 700ms, {1, 2, 2}); + } + const FrameGenStatus status = presenter.Status(); + presenter.Stop(); + const std::vector armed = log.Lines("holds the game's waits"); + Check(!status.gpu_gating && status.gpu_waits == 0 && armed.empty(), + "paused windows: pauses never arm GPU-aware pacing (" + std::to_string(status.gpu_waits) + " waits" + + (armed.empty() ? std::string() : ": " + armed.front()) + ")"); + Check(!runtime.ordering_violation && !g_release_violation, "paused windows: calls stayed ordered"); +} + // Real frames on the GPU too late for more than (c)'s share of refreshes do // not stand down an engagement showing more distinct frames than the game // alone (DCS, flight 2: a hitch's late frames in two 2.5 s windows stood @@ -6688,6 +7218,9 @@ void TestWinningEngagementSurvivesLateCaptures() { policy.distinct_margin_fps = -1000.0; // always winning policy.generated_share = 0.0; policy.late_analyses_share = 2.0; + // (e) off: the fake's fixed capture latency holds the game's waits to it + // once GPU-aware pacing arms, which a real GPU's queue would not. + policy.game_rate_share = 0.0; presenter.SetDynamicPolicy(policy); EngageDynamic(presenter, *app, "winning late captures"); RunTicks(*app, 300ms, {1, 2, 2}); @@ -9339,6 +9872,58 @@ void TestQuadAsIsShowsNewestImage() { Check(!runtime.ordering_violation && !g_release_violation, "as is newest: calls stayed ordered"); } +// A runtime that shows a blank refresh for a frame begun and never ended +// (PimaxXR 0.4.4: its submission thread completes the PVR frame left out with +// one disabled layer, then waits a refresh more; pimax-openxr frame.cpp +// asyncSubmissionThread). Interpolating at 3x, frames the layer composes +// alternate with claimed frames whose writes fail: a refresh with nothing +// written finds the frame last shown as it is no longer current (the next +// one the layer composed is in its images) with a frame to write between +// them, and was left open (the runtime shows its last frame again, but this +// one a blank refresh). Told about the runtime, the presenter shows the next +// frame the layer composed there instead: no frame is left open, and no image +// is shown with another frame's views. FG_TEST_BLANKS_OFF=1 leaves it +// untold (the refreshes left open are counted, and the test fails). +void TestQuadNoOpenFrameOnBlankingRuntime() { + const bool told = !std::getenv("FG_TEST_BLANKS_OFF"); + FrameGenPresenter presenter; + GpuLog gpu; + Runtime runtime; + runtime.paced = true; + StartQuad(presenter, gpu, FrameGenSynthesisMode::kInterpolate, FrameGenMultiplier::k3); + presenter.SetRuntimeBlanksOpenFrames(told); + presenter.SetQuadDecline(3000ms, 3000ms); + QuadApp app(presenter, runtime); + for (int i = 0; i < 3; ++i) app.Frame(); + app.Run(400ms); + Check(presenter.Status().quad, "blanking runtime: composing quad views first"); + const std::size_t first = runtime.Submissions(); + const int open_before = runtime.discarded; + const auto start = std::chrono::steady_clock::now(); + for (int i = 0; std::chrono::steady_clock::now() - start < 1500ms; ++i) { + // The layer composes every second frame; the claimed frame after + // each fails to be written. + app.force = i % 4 == 0 || i % 4 == 2 ? std::optional(FrameGenQuadRoute::kBusy) : std::nullopt; + gpu.compose_fail = i % 4 == 1; + app.Frame(4000us); + } + gpu.compose_fail = false; + app.force.reset(); + app.Run(100ms); + const FrameGenStatus status = presenter.Status(); + const int left_open = runtime.discarded - open_before; + std::cerr << " blanking runtime (" << (told ? "told" : "untold") << "): " << left_open << " refreshes left open, " + << status.stale_skipped << " stale, " << status.stale_shown_ahead << " shown with a later frame, " + << StepsBack(runtime, first) << " steps back, " << runtime.mismatched_images << " mismatched, " + << gpu.compose_failures << " failed writes" << std::endl; + Check(gpu.compose_failures >= 5, "blanking runtime: writes failed (" + std::to_string(gpu.compose_failures.load()) + ")"); + Check(left_open == 0, "blanking runtime: no refresh is left open (" + std::to_string(left_open) + ")"); + Check(runtime.mismatched_images == 0 && runtime.not_stereo == 0 && !runtime.used_destroyed, + "blanking runtime: images matching their views, two views each, nothing destroyed"); + presenter.Stop(); + Check(!runtime.ordering_violation && !g_release_violation, "blanking runtime: calls stayed ordered"); +} + // A refresh nothing can be written for shows again what was submitted last: // after a claimed frame written, then a frame the layer composed shown as it // is, a claimed frame whose write fails shows the composed frame again (its @@ -9755,41 +10340,6 @@ constexpr const char* kHalvingDetail = "Runtime holds frames to half rate: check constexpr const char* kHalvingReason = "the runtime holds frames to half rate: check its smoothing"; constexpr const char* kHalvingStandDown = "the runtime holds frames to half rate"; -// A session log of its own in the temporary folder (not beside this -// executable: on a worktree whose build is the installed layer that is the -// active layer folder), read back once the presenter has stopped. -struct SessionLog { - std::filesystem::path directory; - std::unique_ptr logger{std::make_unique()}; - std::filesystem::path path; // the log, once Lines closed it - - explicit SessionLog(const std::string& name) - : directory(std::filesystem::temp_directory_path() / - ("vectorxr-presenter-test-log-" + std::to_string(GetCurrentProcessId()) + "-" + name)) { - std::error_code ignored; - std::filesystem::remove_all(directory, ignored); - logger->Initialize(directory / "session.log"); - } - ~SessionLog() { - logger.reset(); - std::error_code ignored; - std::filesystem::remove_all(directory, ignored); - } - // The lines holding `text` (closes the log: call once the presenter has - // stopped). Later calls read the closed log again. - std::vector Lines(const std::string& text) { - if (logger) { - path = logger->ActiveLogPath(); - logger.reset(); // flushed - } - std::ifstream file(path); - std::vector lines; - for (std::string line; std::getline(file, line);) { - if (line.find(text) != std::string::npos) lines.push_back(line); - } - return lines; - } -}; // Research switches from %LOCALAPPDATA%\VectorXR\framegen-research.txt (a // folder of this test's own, which the policy tests' process never sees): @@ -11115,6 +11665,7 @@ void TestRuntimeHalvingStandsDown() { bool after_full_rate{false}; FrameGenMultiplier multiplier{FrameGenMultiplier::kDynamic}; const char* evidence; + bool one_late{false}; // Runtime::period_one_late (PimaxXR's Smart Smoothing) }; const std::vector cases{ {"halving waits", Runtime::Throttle::kWaits, 2, true, false, false, FrameGenMultiplier::kDynamic, @@ -11131,6 +11682,11 @@ void TestRuntimeHalvingStandsDown() { {"a third, the period", Runtime::Throttle::kPeriod, 3, false, false, true, FrameGenMultiplier::kDynamic, "to every third refresh at 90 Hz for 1 s (its frame waits blocked; it reported a frame period of 33.3 ms, " "three times its refresh period)."}, + {"halving the period a wait late", Runtime::Throttle::kPeriod, 2, false, false, true, + FrameGenMultiplier::kDynamic, + "to every other refresh at 90 Hz for 1 s (its frame waits blocked; it reported a frame period of 22.2 ms, " + "twice its refresh period).", + true}, }; const double refresh = 1e9 / static_cast(kPeriod); for (const Case& test : cases) { @@ -11141,6 +11697,7 @@ void TestRuntimeHalvingStandsDown() { runtime.throttle = test.throttle; runtime.throttle_refreshes = test.refreshes; runtime.throttled = test.from_start; + runtime.period_one_late = test.one_late; // Late at full rate (a frame of 1.35 refreshes), and every frame late // for a runtime wanting them two refreshes before they are shown. std::vector work = kLightWork; @@ -11201,6 +11758,16 @@ void TestRuntimeHalvingStandsDown() { status = presenter.Status(); Check(!cleared && (dynamic ? status.standby && status.dynamic_engagements == 1 : status.threaded), name + ": the report stays while the runtime halves the frame rate, and nothing engages again"); + if (test.one_late) { + // The period reported a wait late, the switch's first held wait + // read as a refresh's: no two submissions went to one refresh. + std::scoped_lock lock(g_xr_mutex); + std::size_t paired = 0; + for (std::size_t i = 1; i < runtime.submitted_times.size(); ++i) { + paired += runtime.submitted_times[i] - runtime.submitted_times[i - 1] < kPeriod / 2 ? 1 : 0; + } + Check(paired == 0, name + ": no two submissions for one refresh (" + std::to_string(paired) + ")"); + } presenter.Stop(); Check(!runtime.ordering_violation && !g_release_violation, name + ": calls stayed ordered"); const std::vector lines = log.Lines(kHalvingLogged); @@ -12511,6 +13078,114 @@ void TestStalledClockWhilePresenting() { name + ": the log says the display times stopped advancing"); } +// OpenXR Toolkit's Turbo above VectorXR (its layer.cpp xrWaitFrame, +// xrBeginFrame, xrEndFrame): after each xrEndFrame it calls the next layer's +// xrWaitFrame on a pool thread (std::async), the game's own xrWaitFrame +// returns a time it makes up without waiting, its xrBeginFrame does nothing, +// and the next xrEndFrame first waits for that pool wait (1 s at most), then +// calls xrBeginFrame and xrEndFrame back to back. Frame Generation, fixed 2x +// and Dynamic, sees each wait from another thread than the one ending, its +// begin right before its end: it engages and stays engaged, demotes, +// abandons and pauses nothing, holds no pool wait past OpenXR Toolkit's 1 s, +// keeps the runtime's calls ordered, and fills the refreshes. +void TestToolkitTurboAbove() { + for (const FrameGenMultiplier multiplier : {FrameGenMultiplier::k2, FrameGenMultiplier::kDynamic}) { + const std::string name = + std::string("toolkit turbo above, ") + (multiplier == FrameGenMultiplier::k2 ? "2x" : "dynamic"); + FrameGenPresenter presenter; + GpuLog gpu; + Runtime runtime; + runtime.paced = true; + std::unique_ptr owned; + App* app = nullptr; + StartJit(presenter, gpu, FrameGenSynthesisMode::kInterpolate, multiplier, app, runtime, owned); + app->eye_speed = 0.5; + std::future pending = std::async(std::launch::async, [&] { return app->Wait(); }); + int late_waits = 0; + const auto run = [&](std::chrono::milliseconds duration) { + const auto start = std::chrono::steady_clock::now(); + while (std::chrono::steady_clock::now() - start < duration) { + // The game renders its frame (for OpenXR Toolkit's made-up time). + SleepPrecise(std::chrono::steady_clock::now() + 18ms); + app->Release(); + if (pending.wait_for(1s) != std::future_status::ready) { + ++late_waits; // OpenXR Toolkit would go on without it + pending.wait(); + } + const XrFrameState state = pending.get(); + app->Begin(); + Check(XR_SUCCEEDED(app->End(state.predictedDisplayTime)), name + ": end"); + pending = std::async(std::launch::async, [&] { return app->Wait(); }); + } + }; + run(1500ms); + const FrameGenStatus engaged = presenter.Status(); + Check(engaged.threaded && !engaged.standby, name + ": engages"); + const std::size_t first = runtime.Submissions(); + const auto start = std::chrono::steady_clock::now(); + run(2000ms); + const double seconds = Seconds(std::chrono::steady_clock::now() - start); + const double output = static_cast(runtime.Submissions() - first) / seconds; + const FrameGenStatus status = presenter.Status(); + pending.get(); + app->Begin(); + app->End(0, false); + presenter.Stop(); + Check(status.threaded && !status.standby && status.demoted == 0 && status.abandoned == 0 && status.pauses == 0, + name + ": stays engaged, nothing demoted, abandoned or paused (demoted " + std::to_string(status.demoted) + + ", abandoned " + std::to_string(status.abandoned) + ", pauses " + std::to_string(status.pauses) + ")"); + Check(late_waits == 0, name + ": no pool wait held past OpenXR Toolkit's second (" + std::to_string(late_waits) + ")"); + const double refresh = 1e9 / static_cast(kPeriod); + Check(output > 0.9 * refresh, name + ": every refresh gets a frame (" + std::to_string(output) + " fps)"); + Check(!runtime.ordering_violation && !g_release_violation, name + ": calls stayed ordered"); + } +} + +// PimaxXR clamps a prediction of PVR's that did not advance to 1 ns after the +// last (pimax-openxr frame.cpp xrWaitFrame), as after a wait that returned +// without pacing. Now and then two waits in a row come back so, each still a +// refresh apart: Frame Generation stays engaged (two waits are no stalled +// clock), never gives two of the game's frames the same refresh (their times +// told at least half a refresh apart), and the runtime's submissions keep +// their order. +void TestClampedDisplayTimes() { + const std::string name = "clamped display times"; + FrameGenPresenter presenter; + GpuLog gpu; + Runtime runtime; + runtime.paced = true; + std::unique_ptr owned; + App* app = nullptr; + StartJit(presenter, gpu, FrameGenSynthesisMode::kInterpolate, FrameGenMultiplier::kDynamic, app, runtime, owned); + app->eye_speed = 0.5; + RunPipelined(*app, 1000ms, 16ms, 26ms, {}, nullptr, 8ms); + Check(presenter.Status().threaded && !presenter.Status().standby, name + ": engages"); + const std::size_t first = runtime.Submissions(); + std::vector told; + bool left = false; + RunPipelined( + *app, 2500ms, 16ms, 26ms, + [&](int frame, const XrFrameState& state) { + if (frame % 20 == 10) runtime.clamp_next = 2; + told.push_back(state.predictedDisplayTime); + left = left || !presenter.Status().threaded; + }, + nullptr, 8ms); + const FrameGenStatus status = presenter.Status(); + Check(runtime.clamped_waits >= 10, name + ": the runtime clamped waits (" + + std::to_string(runtime.clamped_waits.load()) + ")"); + Check(!left && status.threaded && !status.standby && !status.runtime_clock_displaced, + name + ": stays engaged, the clock not displaced"); + std::size_t close = 0; + for (std::size_t i = 1; i < told.size(); ++i) close += told[i] - told[i - 1] < kPeriod / 2 ? 1 : 0; + Check(close == 0, name + ": the game's frames are told times at least half a refresh apart (" + + std::to_string(close) + " of " + std::to_string(told.size()) + " closer)"); + const auto log = runtime.Snapshot(); + CheckOrderedTimes(std::vector(log.begin() + static_cast(first), log.end()), name); + presenter.Stop(); + Check(!runtime.ordering_violation && !g_release_violation, name + ": calls stayed ordered"); +} + // Auto, its multiplier chosen for a slow game, while the runtime's clock is // displaced: frames pass straight through, and the multiplier is held (each // frame, judged against one refresh, would otherwise step it up to 4x for @@ -13167,29 +13842,59 @@ void TestSteamVrMissedRefresh() { RunPipelined(*app, 900ms, 16ms, 26ms, {}, nullptr, 8ms); Check(presenter.Status().threaded && !presenter.Status().standby, name + ": engages"); // About every 0.4 s, one of the runtime's waits misses a refresh. + const FrameGenStatus engaged = presenter.Status(); runtime.miss_every = 37; std::vector leads; + std::vector told; // the presenter's lead as each frame ended RunPipelined( *app, 3000ms, 16ms, 26ms, [&](int, const XrFrameState& state) { + const std::uint32_t lead_slots = presenter.Status().lead_slots; std::scoped_lock lock(g_xr_mutex); - if (!runtime.submitted_times.empty()) leads.push_back(state.predictedDisplayTime - runtime.submitted_times.back()); + if (!runtime.submitted_times.empty()) { + leads.push_back(state.predictedDisplayTime - runtime.submitted_times.back()); + told.push_back(lead_slots); + } }, nullptr, 8ms); const FrameGenStatus status = presenter.Status(); Check(status.threaded && !status.standby && !status.runtime_halving, name + ": stays engaged, the misses too rare to be halving (" + status.dynamic_reason + ")"); - std::vector sorted = leads; + // Each frame's lead less the presenter's own lead's change: the lead + // moving (a probe, or frames late in a hitch of the test's own + // threads, which raises it a few refreshes for a while) is the + // presenter's choice, not the period's timing this checks. Counted + // as they were, 4-7 of about 120 frames went over in 2 of ~70 runs + // here, each while the lead had risen (TestSteamVrMissedRefresh, Nov 2). + std::vector told_sorted = told; + std::sort(told_sorted.begin(), told_sorted.end()); + const std::int64_t told_median = told_sorted.empty() ? 0 : told_sorted[told_sorted.size() / 2]; + std::vector own(leads.size()); + for (std::size_t i = 0; i < leads.size(); ++i) { + own[i] = leads[i] - (static_cast(told[i]) - told_median) * kPeriod; + } + std::vector sorted = own; std::sort(sorted.begin(), sorted.end()); const XrDuration median = sorted.empty() ? 0 : sorted[sorted.size() / 2]; - const auto ahead = std::count_if(leads.begin(), leads.end(), + const auto ahead = std::count_if(own.begin(), own.end(), [&](XrDuration lead) { return lead > median + 2 * kPeriod; }); // Timed by the reported period, 8-20% of the frames ran more than 2 // refreshes past the median (up to 9); on the refresh grid none do. + if (std::getenv("FG_TEST_STATS")) { + std::cerr << name << ": lead " << engaged.lead_slots << " -> " << status.lead_slots << ", ups " + << status.lead_ups - engaged.lead_ups << ", downs " << status.lead_downs - engaged.lead_downs + << ", " << ahead << " frames over 2 refreshes past the median" << std::endl; + } + std::string sequence; + for (std::size_t i = 0; i < leads.size(); ++i) { + sequence += " " + std::to_string((leads[i] + kPeriod / 2) / kPeriod) + "/" + std::to_string(told[i]); + } Check(!leads.empty() && ahead <= 2, name + ": the game's frames stay about as far ahead of the refreshes (" + std::to_string(ahead) + " of " + std::to_string(leads.size()) + " over 2 refreshes past the median of " + - std::to_string(static_cast(median) / kPeriod) + ")"); + std::to_string(static_cast(median) / kPeriod) + "; lead " + + std::to_string(status.lead_slots) + ", ups " + std::to_string(status.lead_ups) + ", downs " + + std::to_string(status.lead_downs) + "; leads in refreshes:" + sequence + ")"); Check(status.distinct_fps > 0.8 * status.output_fps, name + ": refreshes show new frames (" + std::to_string(status.distinct_fps) + " of " + std::to_string(status.output_fps) + " fps)"); @@ -15253,17 +15958,31 @@ void TestInterpolateFlowAcceptanceDelay() { "flow delay: switches and recovery are logged with reasons"); } +// --group runs one group of the tests below (GROUP marks where each +// begins), so CTest can run the groups side by side; without it, all of them. +const char* g_group = nullptr; +bool g_group_active = true; +int g_tests_run = 0; +#define GROUP(name) (g_group_active = !g_group || std::string(g_group) == (name)) + #define RUN(test) \ do { \ const char* only = std::getenv("FG_TEST_ONLY"); \ + if (!g_group_active) break; \ if (only && std::string(#test).find(only) == std::string::npos) break; \ std::cerr << #test << (g_jit ? " (just in time)" : "") << std::endl; \ + ++g_tests_run; \ + const auto run_started = std::chrono::steady_clock::now(); \ test(); \ + std::cerr << " " #test " took " \ + << std::chrono::duration(std::chrono::steady_clock::now() - run_started).count() \ + << " s" << std::endl; \ } while (0) // Dynamic stand-by and its benefit guard, GPU-aware pacing, pauses and // telemetry. void RunPolicyTests() { + GROUP("policy"); for (const bool jit : {false, true}) { g_jit = jit; RUN(TestCaptureOnlyWhileArmed); @@ -15278,12 +15997,15 @@ void RunPolicyTests() { RUN(TestDynamicOwnRate); RUN(TestDynamicHysteresis); RUN(TestBenefitGuardBacksOff); + RUN(TestBenefitGuardGameSlowedByEngaging); + RUN(TestGpuPacingIgnoresPausedWindows); RUN(TestDynamicProbe); RUN(TestExtrapolateDuplicateTargetGuard); RUN(TestNoExtrapolationAcrossPauses); RUN(TestLeadResetOnRestart); RUN(TestLeadStepsDownAfterHitches); RUN(TestLeadDoesNotFlap); + GROUP("policy_late"); RUN(TestLeadCeilingKeepsLeadTheRingHolds); RUN(TestLeadCeilingRespectsProvenLead); RUN(TestLeadCeilingResetsOnRestart); @@ -15387,7 +16109,13 @@ int main(int argc, char** argv) { RunPolicyTests(); return 0; } - const HANDLE policy = std::getenv("FG_TEST_SERIAL") ? nullptr : StartPolicyTests(); + if (argc > 2 && std::string(argv[1]) == "--group") { + // One group, in this process: of the tests below or of the policy + // tests. + g_group = argv[2]; + } + const HANDLE policy = g_group || std::getenv("FG_TEST_SERIAL") ? nullptr : StartPolicyTests(); + GROUP("pacing"); RUN(TestCompositionExtensions); RUN(TestCompositionUnsupportedChains); RUN(TestCompositionRefusalSurvivesRuntimeFailure); @@ -15399,6 +16127,8 @@ int main(int argc, char** argv) { RUN(TestQuadExtrapolation); RUN(TestSubmissionSpacing); RUN(TestOutputsKeptAcrossMultipliers); + RUN(TestEdgeFillMargins); + RUN(TestEdgeFill); RUN(TestDepthCapture); RUN(TestEyePredictionSwitch); RUN(TestEngineMotionCensus); @@ -15422,6 +16152,7 @@ int main(int argc, char** argv) { RUN(TestSlowDynamicHorizonJitterReported); RUN(TestThreadedInterpolation); RUN(TestJitExtrapolation); + RUN(TestJitEdgeFill); RUN(TestJitEyeShiftReported); RUN(TestEyeProbe); RUN(TestJitDynamicExtrapolation); @@ -15435,6 +16166,7 @@ int main(int argc, char** argv) { RUN(TestToggleOffLogsNoEngagement); // The threaded presenter's lifecycle with frames written together, then // just in time. + GROUP("lifecycle"); for (const bool jit : {false, true}) { g_jit = jit; RUN(TestThreadedToggleAndReset); @@ -15451,6 +16183,7 @@ int main(int argc, char** argv) { } g_jit = false; RUN(TestThreadedMsfsTiming); + RUN(TestDampenedEndsAreNotCompensated); RUN(TestThreadedUnsupportedLayerPresentsInline); RUN(TestOverlappingWaitIsHeld); RUN(TestUnclaimedFrameIsDemoted); @@ -15508,6 +16241,7 @@ int main(int argc, char** argv) { RUN(TestEchoRuntimeClearsHalving); RUN(TestEchoHitchKeepsRefresh); // Quad views Frame Generation composes itself. + GROUP("quad"); RUN(TestQuadClaimAndCompose); RUN(TestQuadSetupThroughComposedFrame); RUN(TestQuadFallbackShownAsIs); @@ -15520,6 +16254,7 @@ int main(int argc, char** argv) { RUN(TestQuadClaimNeedsLatestCapture); RUN(TestQuadOutputsResized); RUN(TestQuadAsIsShowsNewestImage); + RUN(TestQuadNoOpenFrameOnBlankingRuntime); RUN(TestQuadFailedWriteRepeatsLastShown); RUN(TestQuadRetiredOutputsFreed); RUN(TestQuadHeadsetOffBreak); @@ -15530,12 +16265,15 @@ int main(int argc, char** argv) { RUN(TestQuadComposeStallOnce); // Runtime clocks that misbehave (Varjo's, SteamVR's): last, so the tests // above keep the timing they had beside the policy tests. + GROUP("clocks"); RUN(TestVarjoClockJumpDynamic); RUN(TestVarjoClockJumpWhilePresenting); RUN(TestVarjoTransientThrottle); RUN(TestSteamVrLagIsNotDisplaced); RUN(TestLowFirstClockSample); RUN(TestStalledClockWhilePresenting); + RUN(TestClampedDisplayTimes); + RUN(TestToolkitTurboAbove); RUN(TestAutoHoldsWhileClockDisplaced); RUN(TestAutoSettlesAfterClockRecovery); RUN(TestAutoLeavesLateCeiling); @@ -15545,8 +16283,11 @@ int main(int argc, char** argv) { } RUN(TestAutoGpuBoundProbeReturnsPromptly); RUN(TestAutoProbeAllowsLatencyAndShortHitch); + GROUP("probe_burst"); RUN(TestAutoProbeSurvivesTransitionBurst); g_jit = false; + // The lead against hitches and late frames. + GROUP("lead"); RUN(TestClockLogPairs); RUN(TestSteamVrMissedRefresh); RUN(TestLeadAfterRepeatedHitches); @@ -15564,6 +16305,7 @@ int main(int argc, char** argv) { } g_jit = false; // The lead's ceiling from the history ring. + GROUP("ceiling"); RUN(TestRingLeadFloor); RUN(TestLeadCeilingFollowsRing); RUN(TestLeadCeilingDynamicFollowsSpacing); @@ -15576,7 +16318,13 @@ int main(int argc, char** argv) { RUN(TestLeadCeilingProbeAndTrial); RUN(TestLeadCeilingRaisesOnBursts); RUN(TestLeadCeilingActsThroughHitches); - if (!policy) { + if (g_group) { + RunPolicyTests(); + if (g_tests_run == 0 && !std::getenv("FG_TEST_ONLY")) { + std::cerr << "FAILED: no tests in group " << g_group << std::endl; + return 1; + } + } else if (!policy) { RunPolicyTests(); } else { DWORD code = 1; diff --git a/layer/tests/framegen_runtime_queue_tests.cpp b/layer/tests/framegen_runtime_queue_tests.cpp index 9e7dda74..87af128a 100644 --- a/layer/tests/framegen_runtime_queue_tests.cpp +++ b/layer/tests/framegen_runtime_queue_tests.cpp @@ -312,6 +312,104 @@ void FirstShownImageTest(ID3D12Device* device) { std::cout << "First shown image synchronization passed\n"; } +// A GPU-bound game's shown layer (OpenKneeboard's, or its interface quad), +// acquired and waited for at each frame's start: its wait must not wait for +// the game's last frame's work while the runtime has a release of another +// image newer than the waited image's (it did, for the newest held-back +// release: the game's GPU idled through its CPU work, 2026-11-01). The wait +// is held back behind the release instead, in order. Waiting for the image +// the runtime shows waits on the CPU for the oldest held-back release only. +void ShownLayerPipelinedWaitTest(ID3D12Device* device) { + Harness h(device); + h.Frame(kUi, false); + h.Submit({kUi}, true); + h.HoldGame(1); + h.Frame(kUi, false); + XrSwapchainImageAcquireInfo acquire{XR_TYPE_SWAPCHAIN_IMAGE_ACQUIRE_INFO}; + XrSwapchainImageWaitInfo wait{XR_TYPE_SWAPCHAIN_IMAGE_WAIT_INFO, nullptr, XR_INFINITE_DURATION}; + XrSwapchainImageReleaseInfo release{XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO}; + std::uint32_t image = 0; + Check(h.substitution.Acquire(kUi, 3, kCalls, &acquire, &image) == XR_SUCCESS && image == 2, + "the third frame's acquire hands out an image the runtime never showed"); + auto start = std::chrono::steady_clock::now(); + Check(h.substitution.Wait(kUi, false, kCalls, &wait) == XR_SUCCESS, "the third frame's wait"); + const auto waited = std::chrono::steady_clock::now() - start; + Check(waited < std::chrono::milliseconds(50) && h.runtime.chains.at(kUi).calls == "awrawa" && + h.Chain(kUi).find("heldWaits=1 heldReleases=1") != std::string::npos && + h.substitution.ShownLayers().cpu_waits == 0 && h.substitution.ShownLayers().gpu_waits == 0, + "the wait is held back behind the unfinished release, not waited for (" + h.runtime.chains.at(kUi).calls + + ", " + h.Chain(kUi) + ")"); + Check(h.runtime_marker.Reaches(h.runtime_queue), "the runtime's queue does not wait for the game's work"); + Check(h.substitution.Release(kUi, false, kCalls, &release) == XR_SUCCESS, "the third frame's release"); + // The fourth frame's image is the one the runtime shows: its wait waits + // for the oldest held-back release (finished meanwhile), then everything + // goes in order. + Check(h.substitution.Acquire(kUi, 3, kCalls, &acquire, &image) == XR_SUCCESS && image == 0, + "the fourth frame's acquire hands out the image the runtime shows"); + std::thread work([&] { + Sleep(30); + h.gate->Signal(1); + }); + start = std::chrono::steady_clock::now(); + Check(h.substitution.Wait(kUi, false, kCalls, &wait) == XR_SUCCESS, "the fourth frame's wait"); + const auto waited_shown = std::chrono::steady_clock::now() - start; + work.join(); + // The CPU wait ends with the oldest release's work; the third frame's + // (signalled on the game's queue just after it) may not have run yet: + // then that release stays held back and so does this wait, behind it + // (the waited image is no longer the runtime's newest), in order. + const std::string& calls = h.runtime.chains.at(kUi).calls; + const bool all_ran = calls == "awrawaarwrw"; + const bool third_pending = calls == "awrawaarw" && h.Chain(kUi).find("heldWaits=1 heldReleases=1") != std::string::npos; + Check(waited_shown >= std::chrono::milliseconds(20) && (all_ran || third_pending) && + h.substitution.ShownLayers().cpu_waits == 1 && h.substitution.ShownLayers().gpu_waits == 0, + "waiting for the image the runtime shows waits for the oldest release before it on the CPU, in order (" + + calls + ", " + h.Chain(kUi) + ")"); + Check(h.substitution.Release(kUi, false, kCalls, &release) == XR_SUCCESS, "the fourth frame's release"); + h.FinishGame(1); + h.Submit({kUi}, true); + Check(h.runtime.chains.at(kUi).calls == "awrawaarwrwr" && h.runtime_marker.Reaches(h.runtime_queue), + "the last release reaches the runtime once its work has run"); + std::cout << "Shown layer waits pipelined behind unfinished releases passed\n"; +} + +// The game's queue waits for the runtime's after an image wait only where +// the runtime may have queued work for the image. On PimaxXR and Virtual +// Desktop's runtime that is only before each image's first wait (barriers at +// the first enumerate): after that, each wait made the game's next frame wait +// for every frame Frame Generation had queued on the runtime's queue, and a +// frame written just in time waits there for the game's last frame to be +// measured (a gate stands for it here). Other runtimes keep the wait. +void FirstUseOrderingTest(ID3D12Device* device, bool only_first_use) { + Harness h(device); + h.substitution.SetRuntimeQueuesOnlyFirstUse(only_first_use); + // Each image waited for once. + for (int i = 0; i < 3; ++i) h.Frame(kSource, true); + Check(h.game_marker.Reaches(h.game.Get()) && h.runtime_marker.Reaches(h.runtime_queue), "the first frames run"); + ComPtr synthesis; + Check(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&synthesis))), "synthesis gate"); + const test_gates::Guard guard(synthesis.Get()); + Check(SUCCEEDED(h.runtime_queue->Wait(synthesis.Get(), 1)), "a frame written just in time, still waiting"); + h.Frame(kSource, true); + const bool game_ran = h.game_marker.Reaches(h.game.Get(), 200); + if (only_first_use) { + Check(game_ran, "an image waited for before does not make the game's queue wait for the runtime's"); + // A swapchain's first image: its first wait still orders the game's + // queue after the runtime's. + h.Frame(kUi, false); + Check(!h.game_marker.Reaches(h.game.Get(), 50), "an image's first wait still waits for the runtime's queue"); + } else { + Check(!game_ran, "another runtime's waits keep the game's queue after the runtime's"); + } + Check(SUCCEEDED(synthesis->Signal(1)), "the frame's measurement is done"); + Check(h.runtime_marker.Reaches(h.runtime_queue) && h.game_marker.Reaches(h.game.Get()), "both queues run on"); + const Swapchain& source = h.runtime.chains.at(kSource); + Check(source.calls.rfind("awra", 0) == 0 && source.calls.find("ww") == std::string::npos && + source.calls.find("rr") == std::string::npos, + "the runtime's calls stay in order (" + source.calls + ")"); + std::cout << "Image waits ordered " << (only_first_use ? "at first use only" : "every time") << " passed\n"; +} + void AcquireExhaustionTest(ID3D12Device* device) { Harness h(device); h.HoldGame(1); @@ -389,7 +487,10 @@ int main(int argc, char** argv) { StoppedShownLayerTest(device.Get()); PassThroughUnderLoadTest(device.Get()); FirstShownImageTest(device.Get()); + ShownLayerPipelinedWaitTest(device.Get()); AcquireExhaustionTest(device.Get()); + FirstUseOrderingTest(device.Get(), false); + FirstUseOrderingTest(device.Get(), true); } else { std::cout << "FG_TEST_NO_STALL=1: skipped GPU fence-hold controls\n"; } diff --git a/layer/tests/head_prediction_layer_tests.cpp b/layer/tests/head_prediction_layer_tests.cpp index 7725bfda..75537664 100644 --- a/layer/tests/head_prediction_layer_tests.cpp +++ b/layer/tests/head_prediction_layer_tests.cpp @@ -3,14 +3,19 @@ #include "depthxr/openxr_layer.h" #include +#include +#include #include #include #include +#include #include #include #include +#include #include #include +#include #include #include @@ -90,7 +95,40 @@ struct FakeRuntime { inline static XrTime now = 0; // the runtime's clock, as VectorXR reads it inline static XrTime limit = 20 * kMs; // how far past now it predicts inline static double degrees_per_second = 120.0; - inline static int locates = 0; + inline static std::atomic locates = 0; + + // Two threads locating one query at once (RacedLocatesOfOneDisplayTimeAnswerTheSame): + // both runtime locates return together, and the first thread's head + // extension holds until the second has missed the kept answers too and + // is extending as well; the second goes on once the first has answered. + struct Race { + std::mutex mutex; + std::condition_variable changed; + int arrived{0}; + bool second_extending{false}; + bool first_answered{false}; + }; + inline static Race race; + inline static thread_local int race_role = 0; // 1: the first thread, 2: the second + inline static thread_local int race_locates = 0; + + static void RaceStep() { + if (race_role == 0) { + return; + } + std::unique_lock lock(race.mutex); + if (++race_locates == 1) { + ++race.arrived; + race.changed.notify_all(); + race.changed.wait(lock, [] { return race.arrived == 2; }); + } else if (race_locates == 2 && race_role == 1) { + race.changed.wait(lock, [] { return race.second_extending; }); + } else if (race_locates == 2) { + race.second_extending = true; + race.changed.notify_all(); + race.changed.wait(lock, [] { return race.first_answered; }); + } + } // Yawing at a constant rate from a pitched-down seated start. static XrPosef Head(XrTime time) { @@ -103,11 +141,21 @@ struct FakeRuntime { static XrPosef Answer(XrTime time) { return Head(std::min(time, now + limit)); } + // Each eye turned outward by this much (a canted display: Pimax without + // parallel projection, as pvr_calcEyePoses gives them); 0 is parallel. + inline static double cant_degrees = 0.0; + // False: the runtime cannot locate the views yet (PimaxXR before its first + // valid head pose): flags 0 and the views left as they were. + inline static bool views_valid = true; + static XrPosef EyeOffset(uint32_t eye) { - return {{0.0f, 0.0f, 0.0f, 1.0f}, {eye == 0 ? -0.032f : 0.032f, 0.0f, 0.0f}}; + const double half = Radians(eye == 0 ? cant_degrees : -cant_degrees) / 2.0; + return {{0.0f, static_cast(std::sin(half)), 0.0f, static_cast(std::cos(half))}, + {eye == 0 ? -0.032f : 0.032f, 0.0f, 0.0f}}; } static XrResult XRAPI_CALL LocateSpace(XrSpace space, XrSpace base, XrTime time, XrSpaceLocation* location) { + RaceStep(); ++locates; location->locationFlags = kTrackedPose; if (space == kViewSpace && base == kLocalSpace) { @@ -125,10 +173,12 @@ struct FakeRuntime { ++locates; *count = 2; if (state) { - state->viewStateFlags = XR_VIEW_STATE_ORIENTATION_VALID_BIT | XR_VIEW_STATE_POSITION_VALID_BIT | - XR_VIEW_STATE_ORIENTATION_TRACKED_BIT | XR_VIEW_STATE_POSITION_TRACKED_BIT; + state->viewStateFlags = views_valid ? XR_VIEW_STATE_ORIENTATION_VALID_BIT | XR_VIEW_STATE_POSITION_VALID_BIT | + XR_VIEW_STATE_ORIENTATION_TRACKED_BIT | + XR_VIEW_STATE_POSITION_TRACKED_BIT + : 0; } - if (!views || capacity < 2) { + if (!views || capacity < 2 || !views_valid) { return XR_SUCCESS; } const XrPosef base = @@ -177,9 +227,53 @@ class HeadPredictionTestPeer { FakeRuntime::now = 2'000 * kMs; FakeRuntime::limit = 20 * kMs; FakeRuntime::degrees_per_second = 120.0; + FakeRuntime::cant_degrees = 0.0; + FakeRuntime::views_valid = true; FakeRuntime::locates = 0; } + static bool QuadFrameCached(XrTime time) { + OpenXrLayer::QuadViewsFrameState frame; + XrTime matched = 0; + return Layer().FindQuadViewsFrame(time, &frame, &matched) && matched == time; + } + static XrTime EyeOffsetsTime() { return Layer().cached_eye_offsets_display_time_; } + static bool EnsureEyeOffsets(XrTime time) { + return Layer().EnsureEyeOffsets(kSession, XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO, time, 2); + } + + // Emulated quad views from the stand-in's stereo eyes, the focus held + // `focus_yaw` right of head-forward (as a gaze the layer holds): each + // focus view's centre, the tangent mid-way across it in its eye's frame. + static std::array FocusCentres(XrTime time, XrSpace space, double focus_yaw) { + auto& layer = Layer(); + layer.resolved_settings_.quadviews.enabled = true; + layer.resolved_settings_.quadviews.tracking_mode = QuadViewsTrackingMode::Head; + layer.quadviews_session_active_ = true; + layer.d3d11_graphics_extension_requested_ = true; + layer.varjo_compatible_quadviews_active_ = false; + layer.quadviews_smoothed_focus_yaw_radians_ = focus_yaw; + layer.quadviews_smoothed_focus_pitch_radians_ = 0.0; + layer.quadviews_last_valid_gaze_wall_time_.reset(); + XrViewLocateInfo info{XR_TYPE_VIEW_LOCATE_INFO}; + info.viewConfigurationType = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO_WITH_FOVEATED_INSET; + info.displayTime = time; + info.space = space; + XrViewState state{XR_TYPE_VIEW_STATE}; + XrView views[4]{{XR_TYPE_VIEW}, {XR_TYPE_VIEW}, {XR_TYPE_VIEW}, {XR_TYPE_VIEW}}; + uint32_t count = 0; + Expect(layer.LocateViews(kSession, &info, &state, 4, &count, views) == XR_SUCCESS && count == 4, + "quad xrLocateViews must succeed"); + Expect(std::memcmp(&views[2].pose, &views[0].pose, sizeof(XrPosef)) == 0 && + std::memcmp(&views[3].pose, &views[1].pose, sizeof(XrPosef)) == 0, + "each focus view must keep its eye's pose"); + std::array centres{}; + for (uint32_t eye = 0; eye < 2; ++eye) { + centres[eye] = (std::tan(views[2 + eye].fov.angleLeft) + std::tan(views[2 + eye].fov.angleRight)) / 2.0; + } + return centres; + } + static void Finish() { auto& layer = Layer(); layer.framegen_owns_pacing_.store(false, std::memory_order_release); @@ -427,6 +521,138 @@ void NoClockNoExtension() { Peer::Finish(); } +void CantedEyesPlaceTheInsetThroughEachEye() { + // Head-forward and a gaze are directions in VIEW: in an eye turned c + // outward they fall c further in (QVF's ProjectPoint), so both insets + // cover the same direction. Parallel eyes keep the angles as they were. + Peer::Reset(false, false); + const double cant = 10.0; + for (const XrSpace space : {kLocalSpace, kViewSpace}) { + for (const double gaze : {0.0, 8.0, -15.0}) { + FakeRuntime::cant_degrees = cant; + const XrTime time = FakeRuntime::now + static_cast(10 + gaze) * kMs; + const auto canted = Peer::FocusCentres(time, space, Radians(gaze)); + const double left = std::tan(Radians(gaze + cant)); + const double right = std::tan(Radians(gaze - cant)); + Expect(std::abs(canted[0] - left) < 1e-4 && std::abs(canted[1] - right) < 1e-4, + "a canted eye's inset must sit where the direction falls in it: gaze " + Describe(gaze) + + " deg, left " + Describe(canted[0]) + " (want " + Describe(left) + "), right " + + Describe(canted[1]) + " (want " + Describe(right) + ")"); + FakeRuntime::cant_degrees = 0.0; + const auto parallel = Peer::FocusCentres(time + 1, space, Radians(gaze)); + Expect(std::abs(parallel[0] - std::tan(Radians(gaze))) < 1e-6 && + std::abs(parallel[1] - std::tan(Radians(gaze))) < 1e-6, + "parallel eyes must keep the inset at the gaze's own angle"); + } + } + // The eyes' turn from the head is asked for once per display time (the + // same one Pivot's eye offsets keep), never for parallel eyes. + FakeRuntime::cant_degrees = cant; + const XrTime time = FakeRuntime::now + 50 * kMs; + FakeRuntime::locates = 0; + Peer::FocusCentres(time, kLocalSpace, 0.0); + Peer::FocusCentres(time, kLocalSpace, 0.0); + Expect(FakeRuntime::locates == 3, "canted eyes: one extra locate per display time (" + + std::to_string(FakeRuntime::locates) + ")"); + Peer::FocusCentres(time + kMs, kLocalSpace, 0.0); + Expect(FakeRuntime::locates == 5, "a new display time must locate the eyes again (" + + std::to_string(FakeRuntime::locates) + ")"); + FakeRuntime::cant_degrees = 0.0; + FakeRuntime::locates = 0; + Peer::FocusCentres(time + 2 * kMs, kLocalSpace, 0.0); + Expect(FakeRuntime::locates == 1, "parallel eyes: no extra locate (" + std::to_string(FakeRuntime::locates) + ")"); + Peer::Finish(); +} + +void ViewsTheRuntimeDidNotLocateAreLeftAlone() { + // PimaxXR answers "all or nothing": before its first valid head pose it + // returns flags 0 and leaves the views untouched. Nothing is cached or + // derived from such views. + Peer::Reset(false, false); + FakeRuntime::cant_degrees = 10.0; + FakeRuntime::views_valid = false; + const XrTime time = FakeRuntime::now + 20 * kMs; + FakeRuntime::locates = 0; + Peer::FocusCentres(time, kLocalSpace, 0.0); + Expect(!Peer::QuadFrameCached(time), "a quad frame of views the runtime did not locate must not be cached"); + Expect(FakeRuntime::locates == 1 && Peer::EyeOffsetsTime() != time, + "no eye offsets may be derived from views the runtime did not locate"); + Expect(!Peer::EnsureEyeOffsets(time + kMs) && Peer::EyeOffsetsTime() != time + kMs, + "eye offsets the runtime did not locate must not be kept"); + FakeRuntime::views_valid = true; + Peer::FocusCentres(time + 2 * kMs, kLocalSpace, 0.0); + Expect(Peer::QuadFrameCached(time + 2 * kMs) && Peer::EyeOffsetsTime() == time + 2 * kMs, + "located views must be cached as before"); + Peer::Finish(); +} + +void RepeatedSpaceLocatesOfOneDisplayTimeAnswerTheSame() { + // DCS locates VIEW in LOCAL several times a frame at one display time. + // The first locate extends the head (three extra runtime locates); the + // others give the same answer for one locate each. A new display time is + // extended afresh: nothing is kept across display times. + Peer::Reset(); + Peer::SettleLimit(); + const XrTime time = FakeRuntime::now + 100 * kMs; + FakeRuntime::locates = 0; + const XrPosef first = Peer::SpaceHead(time); + Expect(FakeRuntime::locates == 4, "the first locate extends the head with three extra locates (" + + std::to_string(FakeRuntime::locates) + ")"); + Expect(AngleBetween(first, FakeRuntime::Answer(time)) > 1.0, "the head must be extended"); + for (int i = 1; i <= 3; ++i) { + FakeRuntime::now += 4 * kMs; + const XrPosef again = Peer::SpaceHead(time); + Expect(std::memcmp(&again, &first, sizeof(first)) == 0, + "a later locate of the same display time must answer the same (off by " + + Describe(AngleBetween(again, first)) + " deg)"); + } + Expect(FakeRuntime::locates == 7, "later locates of the same display time cost one runtime locate each (" + + std::to_string(FakeRuntime::locates) + ")"); + const XrPosef next = Peer::SpaceHead(time + 11 * kMs); + Expect(FakeRuntime::locates == 11, "a new display time is extended afresh (" + + std::to_string(FakeRuntime::locates) + ")"); + Expect(AngleBetween(next, first) > 0.5, "a new display time must not take the last one's answer"); + Peer::Finish(); +} + +void RacedLocatesOfOneDisplayTimeAnswerTheSame() { + // A game's two threads locate VIEW at one display time together: both miss + // the kept answers and extend the head, the second against a later "now". + // Both must answer the same, the first one kept. + Peer::Reset(); + Peer::SettleLimit(); + const XrTime time = FakeRuntime::now + 100 * kMs; + FakeRuntime::locates = 0; + FakeRuntime::race.arrived = 0; + FakeRuntime::race.second_extending = false; + FakeRuntime::race.first_answered = false; + XrPosef first{}; + XrPosef second{}; + std::thread first_thread([&] { + FakeRuntime::race_role = 1; + first = Peer::SpaceHead(time); + std::scoped_lock lock(FakeRuntime::race.mutex); + FakeRuntime::now += 4 * kMs; + FakeRuntime::race.first_answered = true; + FakeRuntime::race.changed.notify_all(); + }); + std::thread second_thread([&] { + FakeRuntime::race_role = 2; + second = Peer::SpaceHead(time); + }); + first_thread.join(); + second_thread.join(); + Expect(FakeRuntime::locates == 8, "both threads must have missed and extended the head (" + + std::to_string(FakeRuntime::locates.load()) + " locates)"); + Expect(AngleBetween(first, FakeRuntime::Answer(time)) > 1.0, "the head must be extended"); + Expect(std::memcmp(&first, &second, sizeof(first)) == 0, + "two threads' locates of one display time must answer the same (off by " + + Describe(AngleBetween(first, second)) + " deg)"); + const XrPosef again = Peer::SpaceHead(time); + Expect(std::memcmp(&again, &first, sizeof(first)) == 0, "a later locate must answer as the first thread did"); + Peer::Finish(); +} + struct Scenario { const char* name; std::function run; @@ -446,6 +672,10 @@ int main() { {"off by default and only under Frame Generation", OffByDefaultAndOnlyUnderFrameGeneration}, {"a runtime that predicts is left alone", ARuntimeThatPredictsIsLeftAlone}, {"no runtime clock, no extension", NoClockNoExtension}, + {"repeated space locates of one display time answer the same", RepeatedSpaceLocatesOfOneDisplayTimeAnswerTheSame}, + {"raced locates of one display time answer the same", RacedLocatesOfOneDisplayTimeAnswerTheSame}, + {"canted eyes place the inset through each eye", CantedEyesPlaceTheInsetThroughEachEye}, + {"views the runtime did not locate are left alone", ViewsTheRuntimeDidNotLocateAreLeftAlone}, }; int failures = 0; for (const Scenario& scenario : scenarios) { diff --git a/layer/tests/layer_load_tests.cpp b/layer/tests/layer_load_tests.cpp index 4b8bc0f1..0e40bf8e 100644 --- a/layer/tests/layer_load_tests.cpp +++ b/layer/tests/layer_load_tests.cpp @@ -7,9 +7,14 @@ #include #include +#include #include +#include #include +#include #include +#include +#include namespace { @@ -30,6 +35,151 @@ constexpr const char* kRuntimeDlls[] = {"msvcp140.dll", "vcruntime140.dll", "v "msvcp140d.dll", "vcruntime140d.dll", "vcruntime140_1d.dll", "ucrtbased.dll"}; +// A stand-in runtime below the layer: instances 1, 2, ... and functions that +// succeed (the x64 calling convention lets one that takes nothing stand in +// for one that takes arguments: the caller owns them). +int g_instances = 0; +int g_destroyed = 0; +XrInstance g_last_destroyed = XR_NULL_HANDLE; +// The next instance's handle when set: a handle the runtime gives again. +XrInstance g_reuse_handle = XR_NULL_HANDLE; +// The instance the runtime's instance-level functions below were last called with. +XrInstance g_last_called = XR_NULL_HANDLE; + +XrResult XRAPI_CALL RtSucceed() { + return XR_SUCCESS; +} + +XrResult XRAPI_CALL RtPollEvent(XrInstance instance, XrEventDataBuffer*) { + g_last_called = instance; + return XR_EVENT_UNAVAILABLE; +} + +XrResult XRAPI_CALL RtGetSystem(XrInstance instance, const XrSystemGetInfo*, XrSystemId* system) { + g_last_called = instance; + *system = 1; + return XR_SUCCESS; +} + +XrResult XRAPI_CALL RtGetSystemProperties(XrInstance instance, XrSystemId, XrSystemProperties* properties) { + g_last_called = instance; + std::strncpy(properties->systemName, "Vive Reality system", XR_MAX_SYSTEM_NAME_SIZE - 1); + return XR_SUCCESS; +} + +XrResult XRAPI_CALL RtDestroyInstance(XrInstance instance) { + ++g_destroyed; + g_last_destroyed = instance; + return XR_SUCCESS; +} + +XrResult XRAPI_CALL RtEnumerateInstanceExtensions(const char*, std::uint32_t, std::uint32_t* count, + XrExtensionProperties*) { + *count = 0; + return XR_SUCCESS; +} + +XrResult XRAPI_CALL RtGetInstanceProperties(XrInstance, XrInstanceProperties* properties) { + std::strncpy(properties->runtimeName, "Layer-load stand-in", XR_MAX_RUNTIME_NAME_SIZE - 1); + return XR_SUCCESS; +} + +XrResult XRAPI_CALL RtCreateApiLayerInstance(const XrInstanceCreateInfo* info, const depthxr::XrApiLayerCreateInfo*, + XrInstance* instance) { + // The runtime has no quad views: an instance asking it for them is + // refused (VectorXR's instance creation keeps them from it). + for (std::uint32_t i = 0; info->enabledExtensionNames && i < info->enabledExtensionCount; ++i) { + if (std::string_view(info->enabledExtensionNames[i]) == "XR_VARJO_quad_views") { + return XR_ERROR_EXTENSION_NOT_PRESENT; + } + } + ++g_instances; + *instance = g_reuse_handle != XR_NULL_HANDLE ? g_reuse_handle + : reinterpret_cast(static_cast(g_instances)); + g_reuse_handle = XR_NULL_HANDLE; + return XR_SUCCESS; +} + +XrResult XRAPI_CALL RtGetInstanceProcAddr(XrInstance instance, const char* name, PFN_xrVoidFunction* function) { + *function = nullptr; + const std::string_view requested(name); + if (instance == XR_NULL_HANDLE) { + if (requested == "xrEnumerateInstanceExtensionProperties") { + *function = reinterpret_cast(RtEnumerateInstanceExtensions); + return XR_SUCCESS; + } + return XR_ERROR_FUNCTION_UNSUPPORTED; + } + if (requested == "xrDestroyInstance") { + *function = reinterpret_cast(RtDestroyInstance); + } else if (requested == "xrGetInstanceProperties") { + *function = reinterpret_cast(RtGetInstanceProperties); + } else if (requested == "xrPollEvent") { + *function = reinterpret_cast(RtPollEvent); + } else if (requested == "xrGetSystem") { + *function = reinterpret_cast(RtGetSystem); + } else if (requested == "xrGetSystemProperties") { + *function = reinterpret_cast(RtGetSystemProperties); + } else if (requested.starts_with("xr") && !requested.ends_with("KHR") && !requested.ends_with("EXT") && + !requested.ends_with("VARJO") && !requested.ends_with("FB")) { + *function = reinterpret_cast(RtSucceed); + } else { + return XR_ERROR_FUNCTION_UNSUPPORTED; + } + return XR_SUCCESS; +} + +using CreateFn = XrResult(XRAPI_PTR*)(const XrInstanceCreateInfo*, const depthxr::XrApiLayerCreateInfo*, XrInstance*); + +// An extension, as a game enables: the log of an instance with none (a +// probe) is removed at its xrDestroyInstance. +const std::vector kGameExtensions = {"XR_KHR_composition_layer_depth"}; + +// xrCreateInstance through the layer, as the layer above it calls it, with +// `below` as the name of the layer the stand-in runtime sits behind. +XrResult CreateInstance(CreateFn create, const char* application, const char* below, XrInstance& instance, + const std::vector& extensions = kGameExtensions) { + XrInstanceCreateInfo info{XR_TYPE_INSTANCE_CREATE_INFO}; + std::strncpy(info.applicationInfo.applicationName, application, XR_MAX_APPLICATION_NAME_SIZE - 1); + info.applicationInfo.apiVersion = XR_MAKE_VERSION(1, 0, 0); + info.enabledExtensionCount = static_cast(extensions.size()); + info.enabledExtensionNames = extensions.empty() ? nullptr : extensions.data(); + depthxr::XrApiLayerNextInfo runtime{}; + runtime.structType = depthxr::XR_LOADER_INTERFACE_STRUCT_API_LAYER_NEXT_INFO; + runtime.structVersion = depthxr::XR_API_LAYER_NEXT_INFO_STRUCT_VERSION; + runtime.structSize = sizeof(runtime); + std::strncpy(runtime.layerName, below, XR_MAX_API_LAYER_NAME_SIZE - 1); + runtime.nextGetInstanceProcAddr = RtGetInstanceProcAddr; + runtime.nextCreateApiLayerInstance = RtCreateApiLayerInstance; + depthxr::XrApiLayerNextInfo layer = runtime; + std::strncpy(layer.layerName, "XR_APILAYER_DIENERTECH_VECTORXR", XR_MAX_API_LAYER_NAME_SIZE - 1); + layer.next = &runtime; + depthxr::XrApiLayerCreateInfo create_info{}; + create_info.structType = depthxr::XR_LOADER_INTERFACE_STRUCT_API_LAYER_CREATE_INFO; + create_info.structVersion = depthxr::XR_API_LAYER_CREATE_INFO_STRUCT_VERSION; + create_info.structSize = sizeof(create_info); + create_info.nextInfo = &layer; + instance = XR_NULL_HANDLE; + return create(&info, &create_info, &instance); +} + +PFN_xrVoidFunction Function(PFN_xrGetInstanceProcAddr get, XrInstance instance, const char* name) { + PFN_xrVoidFunction function = nullptr; + return XR_SUCCEEDED(get(instance, name, &function)) ? function : nullptr; +} + +std::string ReadLogs(const std::filesystem::path& folder) { + std::string text; + std::error_code error; + for (const auto& entry : std::filesystem::recursive_directory_iterator(folder, error)) { + if (entry.is_regular_file() && entry.path().extension() == ".log") { + std::ifstream stream(entry.path(), std::ios::binary); + text.append(std::istreambuf_iterator(stream), {}); + } + } + return text; +} + } // namespace int wmain(int argc, wchar_t** argv) { @@ -88,6 +238,185 @@ int wmain(int argc, wchar_t** argv) { Expect(!GetModuleHandleW(layer_path.c_str()), label + "the layer unloads"); } - std::cout << "Layer load tests passed: loaded, negotiated and unloaded three times without the VC++ runtime DLLs\n"; + // A second instance while the layer's first is alive. OpenXR Toolkit and + // Quad-Views-Foveated create a probe instance through the chain (the + // game's name, no extension; xrGetSystem and xrGetSystemProperties) and, + // on a VIVE runtime ("Vive Reality system"), never destroy it before the + // game creates its own. The game's instance must be VectorXR's. + { + std::error_code error; + std::filesystem::remove_all(app_data, error); + const HMODULE layer = LoadLibraryW(layer_path.c_str()); + Expect(layer != nullptr, "instances: the layer loads"); + const auto negotiate = + reinterpret_cast(GetProcAddress(layer, "xrNegotiateLoaderApiLayerInterface")); + depthxr::XrNegotiateLoaderInfo loader_info{}; + loader_info.structType = depthxr::XR_LOADER_INTERFACE_STRUCT_LOADER_INFO; + loader_info.structVersion = depthxr::XR_LOADER_INFO_STRUCT_VERSION; + loader_info.structSize = sizeof(loader_info); + loader_info.minInterfaceVersion = 1; + loader_info.maxInterfaceVersion = depthxr::XR_CURRENT_LOADER_API_LAYER_VERSION; + loader_info.minApiVersion = XR_MAKE_VERSION(1, 0, 0); + loader_info.maxApiVersion = XR_CURRENT_API_VERSION; + depthxr::XrNegotiateApiLayerRequest request{}; + request.structType = depthxr::XR_LOADER_INTERFACE_STRUCT_API_LAYER_REQUEST; + request.structVersion = depthxr::XR_API_LAYER_INFO_STRUCT_VERSION; + request.structSize = sizeof(request); + Expect(negotiate && negotiate(&loader_info, "XR_APILAYER_DIENERTECH_VECTORXR", &request) == XR_SUCCESS, + "instances: the layer negotiates"); + const auto create = reinterpret_cast(request.createApiLayerInstance); + const PFN_xrGetInstanceProcAddr get = request.getInstanceProcAddr; + const auto runtime_function = [](PFN_xrVoidFunction function) { + return function == reinterpret_cast(RtSucceed) || + function == reinterpret_cast(RtPollEvent); + }; + const auto intercepted = [&](XrInstance instance) { + for (const char* name : {"xrEndFrame", "xrWaitFrame", "xrLocateViews", "xrCreateSession", "xrPollEvent"}) { + const PFN_xrVoidFunction function = Function(get, instance, name); + if (!function || runtime_function(function)) { + return false; + } + } + return true; + }; + const auto passes_through = [&](XrInstance instance) { + return Function(get, instance, "xrEndFrame") == reinterpret_cast(RtSucceed) && + Function(get, instance, "xrPollEvent") == reinterpret_cast(RtPollEvent); + }; + // A game's instance with an extension VectorXR keeps from a runtime + // without it (the stand-in refuses it): only VectorXR's instance + // creation lets it through. + const std::vector game_extensions = {"XR_KHR_composition_layer_depth", "XR_VARJO_quad_views"}; + const std::vector no_extensions; + + // The probe, and the functions the layer above takes for it while it + // is VectorXR's. + struct Probe { + XrInstance instance{XR_NULL_HANDLE}; + PFN_xrPollEvent poll{}; + PFN_xrDestroyInstance destroy{}; + PFN_xrGetSystemProperties properties{}; + }; + const auto make_probe = [&] { + Probe probe; + Expect(CreateInstance(create, "Game", "XR_APILAYER_TEST_stand_in_runtime", probe.instance, + no_extensions) == XR_SUCCESS && + probe.instance != XR_NULL_HANDLE, + "instances: the probe instance is created"); + Expect(intercepted(probe.instance), "instances: the probe is VectorXR's while it is the only instance"); + probe.poll = reinterpret_cast(Function(get, probe.instance, "xrPollEvent")); + probe.destroy = reinterpret_cast(Function(get, probe.instance, "xrDestroyInstance")); + probe.properties = + reinterpret_cast(Function(get, probe.instance, "xrGetSystemProperties")); + Expect(probe.poll && probe.destroy && probe.properties, "instances: the probe's functions"); + XrSystemProperties system{XR_TYPE_SYSTEM_PROPERTIES}; + Expect(probe.properties(probe.instance, 1, &system) == XR_SUCCESS && + std::string_view(system.systemName) == "Vive Reality system", + "instances: the probe sees a VIVE runtime"); + return probe; + }; + const auto create_game = [&](const std::string& label) { + XrInstance game = XR_NULL_HANDLE; + const XrResult result = + CreateInstance(create, "Game", "XR_APILAYER_TEST_stand_in_runtime", game, game_extensions); + Expect(result == XR_SUCCESS && game != XR_NULL_HANDLE, + "instances: " + label + ": the game's instance is created through VectorXR's extension handling " + + "(result " + std::to_string(static_cast(result)) + ")"); + Expect(intercepted(game), "instances: " + label + + ": the game's instance keeps VectorXR's frame, view and session functions"); + return game; + }; + const auto destroy_through_layer = [&](XrInstance instance, const std::string& what) { + const auto destroy = reinterpret_cast(Function(get, instance, "xrDestroyInstance")); + const int before = g_destroyed; + Expect(destroy && destroy(instance) == XR_SUCCESS && g_destroyed == before + 1 && + g_last_destroyed == instance, + "instances: " + what + " is destroyed in the runtime"); + }; + const auto destroy_probe = [&](const Probe& probe, const std::string& what) { + const int before = g_destroyed; + Expect(probe.destroy(probe.instance) == XR_SUCCESS && g_destroyed == before + 1 && + g_last_destroyed == probe.instance, + "instances: " + what + " is destroyed in the runtime"); + }; + + // The probe stays; the game's instance is VectorXR's, and the probe's + // functions (and those VectorXR gave for it before) go to the runtime. + Probe probe = make_probe(); + XrInstance game = create_game("probe kept"); + Expect(game != probe.instance, "instances: the game's instance is a new handle"); + Expect(passes_through(probe.instance), "instances: the retained probe passes straight through"); + XrEventDataBuffer event{XR_TYPE_EVENT_DATA_BUFFER}; + g_last_called = XR_NULL_HANDLE; + Expect(probe.poll(probe.instance, &event) == XR_EVENT_UNAVAILABLE && g_last_called == probe.instance, + "instances: VectorXR's xrPollEvent, given for the probe before, goes to the runtime with its handle"); + XrSystemProperties system{XR_TYPE_SYSTEM_PROPERTIES}; + g_last_called = XR_NULL_HANDLE; + Expect(probe.properties(probe.instance, 1, &system) == XR_SUCCESS && g_last_called == probe.instance, + "instances: VectorXR's xrGetSystemProperties, given for the probe before, goes to the runtime"); + // The probe goes first (through the function VectorXR gave for it): + // the game's instance is untouched. + destroy_probe(probe, "the probe, before the game"); + Expect(intercepted(game), "instances: the game's instance is still VectorXR's after the probe went"); + destroy_through_layer(game, "the game's instance, after the probe"); + + // The game goes first, then the probe; a new game in between (VR off + // and on again) is VectorXR's too. + probe = make_probe(); + game = create_game("game destroyed first"); + destroy_through_layer(game, "the game's instance, before the probe"); + const XrInstance next_game = create_game("a second game, the probe still alive"); + Expect(passes_through(probe.instance), "instances: the probe still passes through after a new game"); + destroy_through_layer(next_game, "the second game's instance"); + destroy_probe(probe, "the probe, after the games"); + + // A handle the destroyed probe had comes back for the next game. + probe = make_probe(); + game = create_game("before the probe's handle comes back"); + destroy_probe(probe, "the probe"); + destroy_through_layer(game, "the game's instance, the probe gone"); + g_reuse_handle = probe.instance; + game = create_game("the probe's handle reused"); + Expect(game == probe.instance, "instances: the runtime gave the probe's handle again"); + destroy_through_layer(game, "the game's instance on the probe's handle"); + + // An instance that can have a session keeps VectorXR: one after it + // passes straight through. + XrInstance first = XR_NULL_HANDLE; + const std::vector d3d11 = {"XR_KHR_D3D11_enable"}; + Expect(CreateInstance(create, "First", "XR_APILAYER_TEST_stand_in_runtime", first, d3d11) == XR_SUCCESS && + intercepted(first), + "instances: an instance with a graphics extension is VectorXR's"); + XrInstance second = XR_NULL_HANDLE; + const XrResult result = CreateInstance(create, "Second", "XR_APILAYER_TEST_stand_in_runtime", second); + Expect(result == XR_SUCCESS && second != XR_NULL_HANDLE && second != first, + "instances: a second instance while the first is alive must be created, not refused (result " + + std::to_string(static_cast(result)) + ")"); + Expect(passes_through(second) && Function(get, second, "xrGetInstanceProperties") == + reinterpret_cast(RtGetInstanceProperties), + "instances: the second instance's functions must be the runtime's"); + Expect(intercepted(first), "instances: the first instance keeps the layer's functions"); + destroy_through_layer(second, "the second instance"); + Expect(!passes_through(second), "instances: a destroyed pass-through handle is forgotten"); + destroy_through_layer(first, "the first instance"); + + // Quad-Views-Foveated below the layer is named in its log. + XrInstance with_qvf = XR_NULL_HANDLE; + Expect(CreateInstance(create, "Game", "XR_APILAYER_MBUCCHIA_quad_views_foveated", with_qvf) == XR_SUCCESS, + "instances: an instance above Quad-Views-Foveated is created"); + destroy_through_layer(with_qvf, "the instance above Quad-Views-Foveated"); + Expect(FreeLibrary(layer) != FALSE, "instances: the layer is freed"); + // The logs as written once the layer has gone. + const std::string log = ReadLogs(app_data); + Expect(log.find("An earlier OpenXR instance (application \"Game\", 0 extensions") != std::string::npos, + "instances: the retired probe is said in the game's log"); + Expect(log.find("A second OpenXR instance (application \"Second\"") != std::string::npos, + "instances: the pass-through is said in the log"); + Expect(log.find("Quad-Views-Foveated is loaded below VectorXR") != std::string::npos, + "instances: Quad-Views-Foveated below the layer is named in the log"); + } + + std::cout << "Layer load tests passed: loaded, negotiated and unloaded three times without the VC++ runtime DLLs; " + "a retained probe leaves the game's instance VectorXR's\n"; return 0; } diff --git a/layer/tests/osd_tests.cpp b/layer/tests/osd_tests.cpp index ad3f21d3..0b57e8b2 100644 --- a/layer/tests/osd_tests.cpp +++ b/layer/tests/osd_tests.cpp @@ -1,14 +1,17 @@ #include #include +#include #include "depthxr/osd_renderer.h" #include "depthxr/logger.h" #include "depthxr/osd_strings.h" #include #include #include +#include #include #include #include +#include using namespace depthxr; namespace { @@ -986,6 +989,80 @@ int main(int argc, char** argv) { Check(hz>=18 && hz<=22,"The OSD lost its 20 Hz refresh rate to frame timing"); file.close(); std::error_code ignored; std::filesystem::remove(log,ignored); } + { + // Lock order: a game thread holds the device's lock + // (ID3D11Multithread::Enter, protection on) through a call that waits + // for the renderer's lock (in the layer: xrReleaseSwapchainImage waits + // for its lock, held by the status poll while it reads Status), while + // the frame's thread draws the panel, whose upload needs the device's + // lock. The upload must wait for it holding none of the renderer's. + // Driven by events: the game thread enters the lock, the frame's + // thread calls Append (an upload pending), and the game thread reads + // Status once Append is about to take the device's lock (its test + // hook), or after 2 s. Waiting for the device's lock under the + // renderer's deadlocks: the case then fails after 10 s. + OsdRenderer renderer; unsupported=false; timeouts=0; renderer.Initialize(session,device,4,dispatch); + auto settings=OsdSettings{}; settings.enabled=true; settings.show_clock=false; settings.update_hz=20; + renderer.Prepare(settings,Snapshot(),false,false); + XrCompositionLayerProjection game{XR_TYPE_COMPOSITION_LAYER_PROJECTION}; + const auto* original=reinterpret_cast(&game); + XrFrameEndInfo frame{XR_TYPE_FRAME_END_INFO}; frame.layerCount=1; frame.layers=&original; + Check(WaitVisible(renderer,frame),"Lock order: the panel was not shown"); + // The next refresh acquires its image and finds it busy: its upload + // waits for the next Append. + timeouts=1000000; + const int acquired=acquires; + Check(PumpUntil(renderer,frame,[&]{return acquires>acquired;}),"Lock order: no refresh acquired an image"); + timeouts=0; + const int released=releases; + ID3D11Multithread* multithread{}; + Check(SUCCEEDED(context->QueryInterface(__uuidof(ID3D11Multithread),reinterpret_cast(&multithread))), + "Lock order: the context has no ID3D11Multithread"); + multithread->SetMultithreadProtected(TRUE); + const auto await=[](HANDLE event,const char* what) { + if (WaitForSingleObject(event,10000)!=WAIT_OBJECT_0) { + std::cerr<<"FAILED: OSD lock order: "< frame_thread{0}; + std::atomic hooked{false}; + renderer.SetContextWaitHookForTest([&] { + if (GetCurrentThreadId()==frame_thread.load() && !hooked.exchange(true)) SetEvent(waiting); + }); + OsdStatus status; + std::thread game_thread([&] { + multithread->Enter(); + SetEvent(entered); + WaitForSingleObject(waiting,2000); + // Time for the frame's thread to take the renderer's lock, would it + // wait for the device's under it. + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + status=renderer.Status(); + SetEvent(read); + multithread->Leave(); + }); + await(entered,"the game thread did not enter the device's lock"); + const XrCompositionLayerBaseHeader* layer=nullptr; + std::thread frame_thread_([&] { + frame_thread=GetCurrentThreadId(); + layer=renderer.Append(frame,true); + SetEvent(appended); + }); + await(read,"the game thread's Status did not return"); + await(appended,"the frame's Append did not return"); + game_thread.join(); frame_thread_.join(); + renderer.SetContextWaitHookForTest(nullptr); + for(const HANDLE event:{entered,waiting,read,appended}) CloseHandle(event); + multithread->SetMultithreadProtected(FALSE); multithread->Release(); + Check(hooked,"Lock order: Append did not take the device's lock before the renderer's"); + Check(layer && releases==released+1,"Lock order: the pending refresh was not uploaded and released"); + Check(status.available,"Lock order: Status read nothing"); + renderer.Shutdown(); + } { OsdMetric metric; for(int i=0;i<100;++i)metric.Add(i<95?.11:20);Check(metric.count==100&&std::abs(metric.P95Upper()-.15)<.001&&metric.maximum==20,"OSD bounded metric histogram incorrect"); } context->Release(); device->Release(); std::cout<<"OSD telemetry, rasterization, bindings, live settings, and swapchain lifecycle passed.\n"; diff --git a/layer/tests/pivot_pose_tests.cpp b/layer/tests/pivot_pose_tests.cpp index 6e7b6c2e..7710121f 100644 --- a/layer/tests/pivot_pose_tests.cpp +++ b/layer/tests/pivot_pose_tests.cpp @@ -53,6 +53,7 @@ class PivotPoseTestPeer { static void Prepare(Mode mode, double yaw, double pitch = 0) { auto& l=OpenXrLayer::Instance(); l.ResetPivotActivationState(); + l.pivotxr_runtime_reports_recenters_=false; // a new session's runtime, not yet heard from l.resolved_settings_={}; l.resolved_settings_.pivotxr.enabled=true; PivotXrResolvedProfile profile; @@ -127,6 +128,22 @@ class PivotPoseTestPeer { } static void Gain(double gain) { OpenXrLayer::Instance().pivotxr_activation_gain_=gain; } static bool HasAnchor() { return OpenXrLayer::Instance().pivotxr_translation_anchor_.has_value(); } + static XrVector3f AnchorPosition() { return OpenXrLayer::Instance().pivotxr_translation_anchor_->position; } + static bool HasOrigin() { return OpenXrLayer::Instance().pivotxr_origin_.has_value(); } + static bool Engaged() { return OpenXrLayer::Instance().pivotxr_engaged_; } + static void Engage() { OpenXrLayer::Instance().pivotxr_engaged_=true; } + static XrResult XRAPI_CALL DestroyRuntimeSession(XrSession) { return XR_SUCCESS; } + // xrDestroySession of the running session without xrEndSession first (a + // VR toggle, a session restart). PimaxXR and VDXR hand the next session + // the same handle, (XrSession)1, and can reuse its spaces' addresses. + static void DestroyWithoutEnd() { + auto& l=OpenXrLayer::Instance(); + l.active_session_=reinterpret_cast(uintptr_t{1}); + l.next_destroy_session_=&DestroyRuntimeSession; + Expect(l.DestroySession(l.active_session_)==XR_SUCCESS,"xrDestroySession must succeed"); + l.next_destroy_session_=nullptr; + l.active_session_=reinterpret_cast(uintptr_t{1}); // the next session, the same handle + } static void FullyRelease(XrPosef pose) { auto& l=OpenXrLayer::Instance(); l.pivotxr_activation_gain_=0; @@ -290,6 +307,53 @@ int main() { "Lean after a recenter must rotate about the new seated position"); } + // PimaxXR recenters without the event (it queues only session-state + // and interaction-profile events): LOCAL jumps under the anchor + // between two frames. A jump no head can make re-anchors; fast real + // motion, a long gap and a runtime that sends the event do not. + constexpr XrTime frame=11'111'111, start=1'000'000'000; + for (bool explicit_origin : {false,true}) { + Peer::Prepare(Peer::Mode::GlobalNudge,pi/2); + Peer::TrackLocal(Space(2)); + if (explicit_origin) Peer::Origin(Pose()); + Peer::Apply(Pose(),nullptr,false,Space(2),valid_pose,true,start); + const auto recentered=Pose(pi/6,{0,1.6f,0}); + Expect(Close(Peer::Apply(recentered,nullptr,false,Space(2),valid_pose,true,start+frame).position, + recentered.position), + "A silent recenter (36 cm, 30 deg in one frame) must re-anchor the seated position"); + auto leaned_after=recentered; leaned_after.position.x+=.02f; + Expect(Close(Peer::Apply(leaned_after,nullptr,false,Space(2),valid_pose,true,start+2*frame).position, + {0,1.6f,-.02f}), + "Lean after a silent recenter must rotate about the new seated position"); + } + { + // A fast check over the shoulder: 600 deg/s and 2 m/s for a second. + Peer::Prepare(Peer::Mode::GlobalNudge,pi/2); + Peer::TrackLocal(Space(2)); + Peer::Apply(Pose(),nullptr,false,Space(2),valid_pose,true,start); + const XrVector3f anchor=Peer::AnchorPosition(); + for (int i=1; i<=90; ++i) { + const double seconds=i*static_cast(frame)/1e9; + const auto moving=Pose(seconds*600*pi/180,{.3f+static_cast(2*seconds),1.6f,-.2f}); + Peer::Apply(moving,nullptr,false,Space(2),valid_pose,true,start+i*frame); + } + Expect(Close(Peer::AnchorPosition(),anchor),"Fast real head motion must keep the seated anchor"); + // The same jump after a 200 ms gap (a pause) is not judged. + Peer::Prepare(Peer::Mode::GlobalNudge,pi/2); + Peer::TrackLocal(Space(2)); + Peer::Apply(Pose(),nullptr,false,Space(2),valid_pose,true,start); + Peer::Apply(Pose(pi/6,{0,1.6f,0}),nullptr,false,Space(2),valid_pose,true,start+200'000'000); + Expect(Close(Peer::AnchorPosition(),Pose().position),"A jump across a pause must not be judged"); + // A runtime that announces recenters (VDXR) is taken at its word. + Peer::Prepare(Peer::Mode::GlobalNudge,pi/2); + Peer::TrackLocal(Space(2)); + Peer::Apply(Pose(),nullptr,false,Space(2),valid_pose,true,start); + Peer::Recenter(XR_REFERENCE_SPACE_TYPE_LOCAL,start+1'000'000'000); + Peer::Apply(Pose(pi/6,{0,1.6f,0}),nullptr,false,Space(2),valid_pose,true,start+frame); + Expect(Close(Peer::AnchorPosition(),Pose().position), + "A runtime that sends recenter events must not have jumps taken as recenters"); + } + Peer::Prepare(Peer::Mode::GlobalNudge,pi/2); Peer::TrackLocal(XR_NULL_HANDLE); Peer::Apply(Pose()); @@ -307,6 +371,18 @@ int main() { Expect(Close(Peer::Apply(alternate_raw,nullptr,false,Space(3)).position,alternate_raw.position), "Failed reference conversion must not apply an anchor from the wrong space"); } + // A session destroyed without xrEndSession leaves no Pivot state for + // the next: the same handle and LOCAL address would take the old + // origin, anchor and engagement as their own. + Peer::Prepare(Peer::Mode::GlobalNudge,pi/2); + Peer::TrackLocal(Space(2)); + Peer::Origin(Pose()); + Peer::Engage(); + Peer::Apply(Pose()); + Expect(Peer::HasAnchor() && Peer::HasOrigin() && Peer::Engaged(),"Pivot fixture must be engaged and anchored"); + Peer::DestroyWithoutEnd(); + Expect(!Peer::HasAnchor() && !Peer::HasOrigin() && !Peer::Engaged(), + "xrDestroySession without xrEndSession must clear Pivot's origin, anchor and engagement"); // Snap View stacking scenarios live in pivot_snap_tests.cpp. std::cout << "Pivot pose regression tests passed\n"; } catch (const std::exception& error) { diff --git a/layer/tests/turbo_frame_tests.cpp b/layer/tests/turbo_frame_tests.cpp index 55a2ff9c..6fb559a8 100644 --- a/layer/tests/turbo_frame_tests.cpp +++ b/layer/tests/turbo_frame_tests.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -23,7 +24,10 @@ #include "depthxr/openxr_layer.h" #include "depthxr/framegen_gpu.h" +#include "depthxr/runtime_pacing.h" #include "depthxr/turbo_metrics.h" +#include "depthxr/turbo_probe_guard.h" +#include "depthxr/upstream_turbo.h" using namespace std::chrono_literals; @@ -58,6 +62,9 @@ class TurboFrameTestPeer { OpenXrLayer& layer = Layer(); layer.StopTurboAsyncWorker(); layer.ResetTurboFrameState(); + layer.turbo_probe_guard_.Configure({}); + layer.turbo_fallback_early_ = false; + layer.turbo_reprobe_of_.reset(); { std::scoped_lock lock(layer.mutex_); @@ -431,6 +438,11 @@ class TurboFrameTestPeer { layer.deferred_quadviews_config_active_.reset(); } static bool TurboEffective() { return Layer().turbo_effective_active_.load(); } + static void SetToolkitAbove(bool above) { Layer().openxr_toolkit_above_ = above; } + static bool UpstreamTurboSeen() { + std::scoped_lock lock(Layer().turbo_mutex_); + return Layer().upstream_turbo_.Seen(); + } static bool TurboYielded() { auto& layer = Layer(); std::scoped_lock lock(layer.mutex_); @@ -537,7 +549,49 @@ class TurboFrameTestPeer { layer.turbo_timeout_window_start_.reset(); layer.turbo_pacing_verdict_pending_ = false; layer.turbo_auto_suspended_.store(false, std::memory_order_relaxed); + // These policy tests count every stall: no engagement grace. The + // probe-grace tests restore the shipping limits. + TurboProbeGuard::Limits immediate; + immediate.grace = immediate.hitch_grace = immediate.max_grace = 0ms; + layer.turbo_probe_guard_.Configure(immediate); + } + + static void ProbeGuardDefaults() { Layer().turbo_probe_guard_.Configure({}); } + static void EngageProbe(std::chrono::steady_clock::time_point at) { Layer().turbo_probe_guard_.Engaged(at); } + static void ProbeAppFrame(std::chrono::steady_clock::time_point at, double app_frame_ms) { + Layer().turbo_probe_guard_.AppFrame(at, app_frame_ms); + } + static bool FallbackEarly() { return Layer().turbo_fallback_early_; } + static bool Reprobing() { return Layer().turbo_reprobe_of_.has_value(); } + static bool SourceIsProbing() { + return Layer().turbo_pacing_source_ == OpenXrLayer::TurboPacingSource::kProbing; } + static bool SourceIsDiscovered() { + return Layer().turbo_pacing_source_ == OpenXrLayer::TurboPacingSource::kDiscovered; + } + static bool VerdictPending() { return Layer().turbo_pacing_verdict_pending_; } + + // Auto resolution against the sidecar at VECTORXR_RUNTIME_PACING_PATH, + // keeping the runtime identity so verdicts are written for it. + static void ResolveAutoFor(const std::string& runtime, const std::string& system) { + auto& layer = Layer(); + layer.turbo_experiment_.enabled = false; + layer.runtime_name_ = runtime; + layer.runtime_version_ = "test"; + layer.system_name_ = system; + layer.system_vendor_id_ = 7; + layer.graphics_api_ = "D3D11"; + layer.turbo_test_started_at_ = static_cast(RuntimeRelayUnixMilliseconds()); + layer.resolved_settings_.turbo.pacing_mode = TurboPacingSetting::kAuto; + layer.resolved_settings_.turbo.runtime_pins.clear(); + layer.turbo_auto_suspended_.store(false, std::memory_order_relaxed); + layer.ResolveTurboPacingModeLocked(); + } + + // A minute of healthy frames in the current mode (the verdict window). + static void StableMinute() { Layer().NoteTurboPacingStableFrame(60000.0); } + static void FlushPacingWrites() { Layer().DrainRuntimePacingWrites(); } + static void ForgetRuntime() { Layer().runtime_name_.clear(); } static TurboPacingMode PacingMode() { return Layer().turbo_pacing_mode_; @@ -783,6 +837,50 @@ class TurboFrameTestPeer { return result; } // Depth on with convergence: LocateViews shifts each eye's projection. + // DCS World on PimaxXR, Depth Anchor on or off; an internal VIEW space + // for Pivot's eye offsets. + // Whether the session's runtime is taken to show the focus views with the + // FOVs it located, as PimaxXR does for DCS World, on runtime `runtime`. + static bool TakesFocusFovsFromLocate(const char* runtime) { + auto& layer = Layer(); + std::scoped_lock lock(layer.mutex_); + const std::string kept = layer.runtime_name_; + layer.runtime_name_ = runtime; + const bool takes = layer.RuntimeTakesFocusFovsFromLocateLocked(); + layer.runtime_name_ = kept; + return takes; + } + static void PimaxDcs(bool depth_anchor) { + auto& layer = Layer(); + std::scoped_lock lock(layer.mutex_); + layer.application_name_ = "DCS World"; + layer.runtime_name_ = "PimaxXR (Unofficial)"; + layer.resolved_settings_.depthxr.depth_anchor = depth_anchor; + layer.depth_anchor_toggle_inverted_ = false; + layer.active_primary_view_configuration_type_ = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO_WITH_FOVEATED_INSET; + layer.has_active_primary_view_configuration_ = true; + layer.internal_view_space_ = reinterpret_cast(std::uintptr_t{0x7701}); + layer.cached_eye_offset_poses_.clear(); + layer.cached_eye_offsets_display_time_ = 0; + } + // Pivot's eye offsets for a display time when the views it recomposes + // could not give them: VectorXR's own quad locate. + static bool EyeOffsets(XrTime time) { + auto& layer = Layer(); + std::scoped_lock lock(layer.mutex_); + return layer.EnsureEyeOffsets(layer.active_session_, XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO_WITH_FOVEATED_INSET, + time, 4); + } + static void EndPimaxDcs() { + auto& layer = Layer(); + std::scoped_lock lock(layer.mutex_); + layer.application_name_.clear(); + layer.runtime_name_.clear(); + layer.internal_view_space_ = XR_NULL_HANDLE; + layer.active_primary_view_configuration_type_ = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO; + layer.has_active_primary_view_configuration_ = false; + layer.cached_depth_submission_geometry_.clear(); + } static void ConvergeVarjoDepth(double convergence) { auto& layer = Layer(); std::scoped_lock lock(layer.mutex_); @@ -1010,6 +1108,7 @@ class FakeRuntime { frame_end_info->type == XR_TYPE_FRAME_END_INFO; runtime.calls_.push_back(RuntimeCall::kEnd); if (frame_end_info) runtime.submitted_predictions_.push_back(frame_end_info->displayTime); + runtime.ended_in_predictions_.push_back(runtime.current_begun_); runtime.frame_thread_ = std::this_thread::get_id(); ++runtime.end_entered_; runtime.cv_.notify_all(); @@ -1081,6 +1180,12 @@ class FakeRuntime { return submitted_predictions_; } + // The prediction of the runtime frame open at each end (the frame it ends). + std::vector EndedInPredictions() const { + std::scoped_lock lock(mutex_); + return ended_in_predictions_; + } + std::vector LocatedPredictions() const { std::scoped_lock lock(mutex_); return located_predictions_; @@ -1270,7 +1375,7 @@ class FakeRuntime { mutable std::mutex mutex_; std::condition_variable cv_; std::vector calls_; - std::vector native_predictions_, submitted_predictions_, located_predictions_; + std::vector native_predictions_, submitted_predictions_, located_predictions_, ended_in_predictions_; std::vector> scripted_predictions_; std::thread::id frame_thread_; bool interlock_{false}; @@ -2008,6 +2113,348 @@ void TestAsyncTwoThreadFramesNeedNoRaise() { std::to_string(leads) + " runtime frames"); } +// Sequenced pacing, the async rule (0.20.1): the frame thread's wait+begin +// after each app end opens the next runtime frame, so an app frame waited with +// frames still unended ends that many frames after the cached prediction. DCS +// on Pimax sequenced (Oct 29 S4 flight) had ~24% of its frames raised by a +// whole period: the main thread waits ahead of the render thread. +void TestSequencedAppFramesTargetTheirRuntimeFrame() { + FakeRuntime runtime; + runtime.RejectStaleDisplayTimes(); + TurboHarness harness(runtime, depthxr::TurboPacingMode::kSequenced); + auto end = FrameEndInfo(); + Expect(depthxr::TurboFrameTestPeer::ForwardEndFrame(TestSession(), &end) == XR_SUCCESS, + "Sequenced target-frame establishment submit failed"); + XrFrameState handshake{XR_TYPE_FRAME_STATE}; + Expect(AppWait(&handshake) == XR_SUCCESS && XR_SUCCEEDED(AppBegin()), "Sequenced target-frame handshake failed"); + end.displayTime = handshake.predictedDisplayTime; + Expect(depthxr::TurboFrameTestPeer::ForwardEndFrame(TestSession(), &end) == XR_SUCCESS, + "Sequenced target-frame first steady submit failed"); + std::deque waited; + const auto wait = [&] { + XrFrameState state{XR_TYPE_FRAME_STATE}; + Expect(AppWait(&state) == XR_SUCCESS && state.shouldRender, "Sequenced target-frame app wait failed"); + waited.push_back(state.predictedDisplayTime); + }; + // Ends the oldest waited frame; returns how far its time is past the + // prediction of the runtime frame it ended in. + const auto finish = [&] { + const XrTime time = waited.front(); + waited.pop_front(); + Expect(XR_SUCCEEDED(AppBegin()), "Sequenced target-frame app begin failed"); + end.displayTime = time; + Expect(depthxr::TurboFrameTestPeer::ForwardEndFrame(TestSession(), &end) == XR_SUCCESS, + "Sequenced target-frame submit failed"); + Expect(runtime.SubmittedPredictions().back() == time && runtime.StaleRejections() == 0, + "A sequenced app frame's display time had to be raised to the runtime's floor"); + return time - runtime.EndedInPredictions().back(); + }; + + wait(); + Expect(finish() == 0, "A frame waited after the previous one ended must target the next runtime frame"); + for (int ahead = 1; ahead <= 2; ++ahead) { + for (int frame = 0; frame <= ahead; ++frame) wait(); + for (int frame = 0; frame <= ahead; ++frame) { + const XrDuration miss = finish(); + Expect(miss == 0, "A sequenced frame waited with " + std::to_string(ahead) + + " ahead missed its runtime frame by " + std::to_string(miss) + " ns"); + } + } + // Steady state one ahead, as DCS mostly runs. + wait(); + for (int frame = 0; frame < 4; ++frame) { + wait(); + Expect(finish() == 0, "A sequenced frame waited one ahead must target the runtime frame it ends in"); + } + Expect(finish() == 0, "The last sequenced frame one ahead must target the runtime frame it ends in"); + Expect(runtime.MaxConcurrentWaits() == 1 && runtime.EndRejections() == 0 && runtime.ValidStructs(), + "Sequenced target-frame pacing broke runtime frame order"); +} + +// The DCS shape on real threads under sequenced pacing: a main thread waits up +// to two frames ahead of a render thread that begins and ends them. No frame +// may need a raise, lead the runtime frame it ends in, or fall short of it. +void TestSequencedTwoThreadFramesNeedNoRaise() { + FakeRuntime runtime; + runtime.RejectStaleDisplayTimes(); + TurboHarness harness(runtime, depthxr::TurboPacingMode::kSequenced); + auto end = FrameEndInfo(); + Expect(depthxr::TurboFrameTestPeer::ForwardEndFrame(TestSession(), &end) == XR_SUCCESS, + "Sequenced two-thread establishment submit failed"); + XrFrameState handshake{XR_TYPE_FRAME_STATE}; + Expect(AppWait(&handshake) == XR_SUCCESS && XR_SUCCEEDED(AppBegin()), "Sequenced two-thread handshake failed"); + end.displayTime = handshake.predictedDisplayTime; + Expect(depthxr::TurboFrameTestPeer::ForwardEndFrame(TestSession(), &end) == XR_SUCCESS, + "Sequenced two-thread first steady submit failed"); + constexpr int kFrames = 150; + constexpr int kInFlight = 3; // waited and not yet ended, the newest included + std::mutex mutex; + std::condition_variable cv; + std::deque waited; + int in_flight = 0; + std::atomic wait_failed{false}; + std::thread main_thread([&] { + for (int frame = 0; frame < kFrames; ++frame) { + { + std::unique_lock lock(mutex); + cv.wait(lock, [&] { return in_flight < kInFlight; }); + ++in_flight; + } + XrFrameState state{XR_TYPE_FRAME_STATE}; + if (AppWait(&state) != XR_SUCCESS || !state.shouldRender) wait_failed = true; + if (frame % 3 == 0) std::this_thread::sleep_for(1ms); + { + std::scoped_lock lock(mutex); + waited.push_back(state.predictedDisplayTime); + } + cv.notify_all(); + } + }); + int raises = 0, leads = 0, early = 0; + for (int frame = 0; frame < kFrames && !wait_failed; ++frame) { + XrTime time = 0; + { + std::unique_lock lock(mutex); + cv.wait(lock, [&] { return !waited.empty(); }); + time = waited.front(); + waited.pop_front(); + } + Expect(XR_SUCCEEDED(AppBegin()), "Sequenced two-thread app begin failed"); + if (frame % 2 == 0) std::this_thread::sleep_for(1ms); + end.displayTime = time; + Expect(depthxr::TurboFrameTestPeer::ForwardEndFrame(TestSession(), &end) == XR_SUCCESS, + "Sequenced two-thread submit failed"); + if (runtime.SubmittedPredictions().back() != time) ++raises; + const XrTime ended_in = runtime.EndedInPredictions().back(); + if (time > ended_in) ++leads; + if (time < ended_in) ++early; + { + std::scoped_lock lock(mutex); + --in_flight; + } + cv.notify_all(); + } + main_thread.join(); + Expect(!wait_failed && runtime.StaleRejections() == 0, "A sequenced two-thread app frame failed or was rejected"); + Expect(raises == 0 && leads == 0 && early == 0, + "Sequenced two-thread frames needed " + std::to_string(raises) + " raises, led " + std::to_string(leads) + + " and fell short of " + std::to_string(early) + " runtime frames"); + Expect(runtime.MaxConcurrentWaits() == 1 && runtime.EndRejections() == 0, + "Sequenced two-thread pacing duplicated a runtime wait or broke frame order"); +} + +// The detector on scripted times (no OS scheduling): OpenXR Toolkit's Turbo +// above (a pool thread waits as each end returns, begin right before the +// next end) with the misses a busy PC gives it, against games that are not +// it. `frame` scripts one frame's calls, `at` running on in microseconds. +void TestUpstreamTurboDetectorWindow() { + using Detector = depthxr::UpstreamTurboDetector; + using Clock = Detector::Clock; + std::thread pool_a([] {}), pool_b([] {}), sim([] {}); + const std::thread::id a = pool_a.get_id(), b = pool_b.get_id(), game_sim = sim.get_id(); + pool_a.join(); + pool_b.join(); + sim.join(); + const std::thread::id end_thread = std::this_thread::get_id(); + + struct Frame { + std::thread::id wait_thread; + int wait_after_end_us; // from the last end's return to the wait + int begin_before_end_us; // from the begin to the end + }; + // Frames until the detector first answers true (0: never, in `frames`). + const auto run = [&](bool toolkit_above, int frames, auto frame_of) { + Detector detector; + Clock::time_point at = Clock::time_point{} + std::chrono::seconds(1); + detector.EndReturned(at); + for (int i = 1; i <= frames; ++i) { + const Frame frame = frame_of(i); + at += std::chrono::microseconds(frame.wait_after_end_us); + detector.Wait(frame.wait_thread, at); + at += std::chrono::microseconds(10'000 - frame.wait_after_end_us); // the game renders + detector.Begin(at); + at += std::chrono::microseconds(frame.begin_before_end_us); + if (detector.End(end_thread, at, toolkit_above)) { + return i; + } + at += std::chrono::microseconds(500); // the runtime's xrEndFrame + detector.EndReturned(at); + } + return 0; + }; + const auto toolkit = [&](int i) { return Frame{i % 2 ? a : b, 200, 50}; }; + // Every 15th pool wait starts 4 ms late (the reviewer's reproduction), and + // every 20th begin is preempted 2 ms before its end. + const auto toolkit_jitter = [&](int i) { + Frame frame = toolkit(i); + if (i % 15 == 0) frame.wait_after_end_us = 4'200; + if (i % 20 == 0) frame.begin_before_end_us = 2'000; + return frame; + }; + // A busy PC: one frame in four misses a window. + const auto toolkit_busy = [&](int i) { + Frame frame = toolkit(i); + if (i % 4 == 0) frame.wait_after_end_us = 6'000; + return frame; + }; + const auto dcs = [&](int) { return Frame{game_sim, 200, 8'000}; }; + // A game rendering on two threads whose begin lands near its end on half + // its frames (short frames on its render thread). + const auto two_thread_game = [&](int i) { return Frame{game_sim, 200, i % 2 ? 300 : 6'000}; }; + const auto single_thread = [&](int) { return Frame{end_thread, 200, 50}; }; + + Expect(run(true, 60, toolkit) == 60 && run(false, 60, toolkit) == 60, + "Toolkit's Turbo above must be seen at the end of the first window"); + Expect(run(true, 60, toolkit_jitter) == 60, + "Toolkit's Turbo above must be seen through periodic pool-thread and begin delays"); + Expect(run(false, 60, toolkit_jitter) == 60, + "A few late frames must not hide the pattern with no layer known above either"); + Expect(run(true, 120, toolkit_busy) == 60, + "With OpenXR Toolkit known above, a quarter of the frames late must not hide its Turbo"); + Expect(run(false, 300, toolkit_busy) == 0, + "With no layer known above, a pattern that holds on only three frames in four must not be taken"); + for (const bool above : {true, false}) { + Expect(run(above, 300, dcs) == 0, "A game that renders between its begin and end must keep Turbo"); + Expect(run(above, 300, two_thread_game) == 0, + "A two-thread game whose begin sometimes lands near its end must keep Turbo"); + Expect(run(above, 300, single_thread) == 0, "A game that waits on its frame thread must keep Turbo"); + } + // The window slides: a game's first frames (loading, no pattern) do not + // keep a Toolkit pipeline from being seen later. + Expect(run(true, 200, [&](int i) { return i <= 100 ? dcs(i) : toolkit(i); }) == 142, + "The pattern must be seen once a whole window of it has passed"); + + Detector detector; + Clock::time_point at = Clock::time_point{} + std::chrono::seconds(1); + for (int i = 0; i < 60; ++i) { + detector.EndReturned(at); + detector.Wait(a, at += 100us); + detector.Begin(at += 10ms); + detector.End(end_thread, at += 50us, true); + at += 500us; + } + Expect(detector.Seen(), "The scripted Toolkit session must be seen"); + detector.End(end_thread, at += 10ms, true); + Expect(detector.Seen(), "Once seen, the pattern stays seen for the session"); + detector.Reset(); + Expect(!detector.Seen() && detector.Frames() == 0, "A session's end must forget the pattern"); +} + +// OpenXR Toolkit's Turbo mode above VectorXR (its layer.cpp xrEndFrame): as +// each xrEndFrame returns it calls xrWaitFrame on a pool thread, and calls +// xrBeginFrame right before the next xrEndFrame. VectorXR's Turbo stacked on +// it would pipeline the frames twice: it sees the pattern and stands aside, +// letting its engaged pipeline go, through the scheduling delays of a busy +// PC. A game that waits on a thread of its own and begins before rendering +// (as DCS does) keeps Turbo. +// +// This runs on real threads and steady_clock, through the layer's own hooks; +// how many frames the window needs is TestUpstreamTurboDetectorWindow's job, +// on scripted times. Here a loaded machine (other builds) can make a pool +// thread wake late for whole stretches, so the Toolkit shape gets as many +// frames as it needs (up to kMaxFrames, several seconds) and must be seen and +// stood aside from on the frame after; the game shape runs a long session and +// must never be seen. Load only removes matches (a late wake, a preempted +// begin), never adds them: the game's 3 ms render between begin and end can +// never fit the 1 ms begin bound. +void TestUpstreamToolkitTurboStandsAside() { + constexpr int kMaxFrames = 1200; + constexpr int kGameFrames = 300; + for (const bool toolkit : {true, false}) { + FakeRuntime runtime; + TurboHarness harness(runtime, depthxr::TurboPacingMode::kAsync); + depthxr::TurboFrameTestPeer::SetToolkitAbove(true); + auto end = FrameEndInfo(); + Expect(depthxr::TurboFrameTestPeer::ForwardEndFrame(TestSession(), &end) == XR_SUCCESS && + depthxr::TurboFrameTestPeer::WaitForAsyncPrediction(), + "Upstream Turbo establishment did not publish its runtime wait"); + Expect(depthxr::TurboFrameTestPeer::TurboEffective(), "Turbo must be engaged before the pattern is seen"); + + // One long-lived pool thread, as the Toolkit's: woken as each end + // returns, it waits the next frame (every 15th starts 5 ms late, as on + // a busy PC, whatever the machine adds). + std::mutex pool_mutex; + std::condition_variable pool_cv; + int requested = 0, done = 0; + bool stop = false; + XrFrameState pool_state{XR_TYPE_FRAME_STATE}; + std::thread pool([&] { + std::unique_lock lock(pool_mutex); + for (;;) { + pool_cv.wait(lock, [&] { return stop || requested > done; }); + if (stop) return; + const int frame = done + 1; + lock.unlock(); + if (frame % 15 == 0) std::this_thread::sleep_for(5ms); + XrFrameState state{XR_TYPE_FRAME_STATE}; + Expect(AppWait(&state) == XR_SUCCESS, "Upstream Turbo app wait failed"); + lock.lock(); + pool_state = state; + ++done; + pool_cv.notify_all(); + } + }); + const auto pool_wait = [&] { + std::unique_lock lock(pool_mutex); + ++requested; + pool_cv.notify_all(); + pool_cv.wait(lock, [&] { return done == requested; }); + return pool_state; + }; + + int seen_at = 0, frames = 0; + bool stood_aside_next = false; + const int limit = toolkit ? kMaxFrames : kGameFrames; + for (int frame = 1; frame <= limit; ++frame) { + const XrFrameState state = pool_wait(); + if (toolkit) { + std::this_thread::sleep_for(3ms); // the game renders, then the layer above begins and ends + Expect(XR_SUCCEEDED(AppBegin()), "Upstream Turbo app begin failed"); + } else { + Expect(XR_SUCCEEDED(AppBegin()), "Upstream Turbo app begin failed"); + std::this_thread::sleep_for(3ms); // the game renders between its begin and end + } + end.displayTime = state.predictedDisplayTime; + Expect(XR_SUCCEEDED(depthxr::TurboFrameTestPeer::ForwardEndFrame(TestSession(), &end)), + "Upstream Turbo submit failed (frame " + std::to_string(frame) + ")"); + frames = frame; + if (seen_at) { + // The frame after the one that saw it: Turbo has stood aside. + stood_aside_next = !depthxr::TurboFrameTestPeer::TurboEffective(); + break; + } + if (depthxr::TurboFrameTestPeer::UpstreamTurboSeen()) seen_at = frame; + } + { + std::scoped_lock lock(pool_mutex); + stop = true; + pool_cv.notify_all(); + } + pool.join(); + + if (toolkit) { + std::cout << "Upstream Turbo: Toolkit's pattern seen at frame " << seen_at << " (window " + << depthxr::UpstreamTurboDetector::kWindow << ")" << std::endl; + Expect(seen_at > 0, "Toolkit's Turbo was not seen in " + std::to_string(frames) + " frames"); + // The establishment end above is the window's first frame. + Expect(seen_at + 1 >= static_cast(depthxr::UpstreamTurboDetector::kWindow), + "Toolkit's Turbo was seen at frame " + std::to_string(seen_at) + + ", before a window could hold enough frames"); + Expect(stood_aside_next && !depthxr::TurboFrameTestPeer::TurboEffective(), + "Turbo must stand aside for OpenXR Toolkit's Turbo above it (seen at frame " + + std::to_string(seen_at) + ")"); + Expect(!depthxr::TurboFrameTestPeer::Handoff().wait_valid, + "Standing aside must let the engaged async pipeline's worker wait go"); + } else { + Expect(!seen_at && frames == kGameFrames && depthxr::TurboFrameTestPeer::TurboEffective(), + "Turbo must stay engaged for a game that begins before it renders (seen at frame " + + std::to_string(seen_at) + ")"); + } + Expect(runtime.StaleRejections() == 0, "Standing aside must not submit a stale frame"); + depthxr::TurboFrameTestPeer::SetToolkitAbove(false); + } +} + void TestRepeatedSubmissionFailuresSuspend() { FakeRuntime runtime; TurboHarness harness(runtime, depthxr::TurboPacingMode::kAsync); @@ -2063,9 +2510,11 @@ void LocatePrediction(XrTime time) { // preserve exact wait -> locate -> submit identity across a scripted -30. // The script rejects the overlapping frame: it exercises recovery. In the field // that frame got the cache + 2 ns (T+2), older than SteamVR's previous frame -// prediction, so SteamVR rejected it; an app wait overlapping the in-flight -// runtime wait now gets the cache plus one period, and the completed-before-poll -// control returns the fresh native timing. +// prediction, so SteamVR rejected it. Since 0.20.1 each app wait gets the frame +// it ends in: the queued frame (one unended ahead of it) the cache plus a +// period, the overlapping one (one unended, the runtime wait in flight) two +// periods, and against the completed-before-poll control's fresh native timing +// one period. void TestSequencedPredictionOverlapReplay(bool native_completed_before_poll) { constexpr XrTime t = 18'956'319'169'600; constexpr XrTime next = 18'956'327'499'300; @@ -2085,7 +2534,8 @@ void TestSequencedPredictionOverlapReplay(bool native_completed_before_poll) { "Prediction replay's handshake did not expose its native time"); LocatePrediction(initial.predictedDisplayTime); XrFrameState ahead{XR_TYPE_FRAME_STATE}; - Expect(AppWait(&ahead) == XR_SUCCESS && ahead.predictedDisplayTime == t + 1 && AppBegin() == XR_SUCCESS, + const XrTime queued = t + first_period; + Expect(AppWait(&ahead) == XR_SUCCESS && ahead.predictedDisplayTime == queued && AppBegin() == XR_SUCCESS, "Prediction replay did not expose the queued app prediction"); LocatePrediction(ahead.predictedDisplayTime); @@ -2108,7 +2558,7 @@ void TestSequencedPredictionOverlapReplay(bool native_completed_before_poll) { Expect(poll.wait_for(2s) == std::future_status::ready, "The sequenced interception shield blocked behind the native wait"); const XrFrameState overlapping = poll.get(); - const XrTime expected = native_completed_before_poll ? next : t + first_period; + const XrTime expected = native_completed_before_poll ? next + next_period : t + 2 * first_period; Expect(overlapping.predictedDisplayTime == expected && overlapping.predictedDisplayPeriod == (native_completed_before_poll ? next_period : first_period) && overlapping.shouldRender == XR_TRUE, @@ -2132,7 +2582,7 @@ void TestSequencedPredictionOverlapReplay(bool native_completed_before_poll) { Expect(AppBegin() == XR_SUCCESS, "Queued replay frame did not begin"); end.displayTime = ahead.predictedDisplayTime; Expect(depthxr::TurboFrameTestPeer::ForwardEndFrame(TestSession(), &end) == XR_SUCCESS, - "The accepted T+1 precision control was rejected"); + "The accepted queued-frame control was rejected"); Expect(AppBegin() == XR_SUCCESS, "Overlapping replay frame did not begin"); runtime.SetEndResult(XR_ERROR_TIME_INVALID, true, 1); end.displayTime = overlapping.predictedDisplayTime; @@ -2145,8 +2595,8 @@ void TestSequencedPredictionOverlapReplay(bool native_completed_before_poll) { log.find("nativeAtWait=" + std::to_string(native_completed_before_poll ? next : t)) != std::string::npos, "The first timestamp failure did not retain its original prediction provenance"); - const std::vector located{t, t + 1, expected}; - const std::vector submitted{20'000'000'000, t, t + 1, expected}; + const std::vector located{t, queued, expected}; + const std::vector submitted{20'000'000'000, t, queued, expected}; Expect(runtime.LocatedPredictions() == located && runtime.SubmittedPredictions() == submitted, "The runtime received a substitute timestamp unrelated to the located app views, or a retry"); Expect(runtime.NativePredictions() == std::vector{t, next, last} && @@ -2908,6 +3358,334 @@ void TestSequencedStallPolicySuspends() { Expect(runtime.Calls().empty(), "A policy-only test unexpectedly issued runtime calls"); } +// The guard on scripted times: a grace after engaging, extended by game hitches +// up to a cap, and "early" for the first ten seconds. +void TestProbeGuardScriptedTimes() { + using depthxr::TurboProbeGuard; + const auto t0 = TurboProbeGuard::Clock::time_point{} + 1000s; + TurboProbeGuard guard; + Expect(!guard.InGrace(t0) && !guard.Early(t0) && !guard.SinceEngaged(t0), + "A guard that never engaged must count every stall"); + guard.AppFrame(t0, 500.0); + Expect(!guard.InGrace(t0), "A hitch before engaging must not open a grace"); + guard.Engaged(t0); + Expect(guard.InGrace(t0 + 250ms) && guard.InGrace(t0 + 1999ms) && !guard.InGrace(t0 + 2000ms), + "The engagement grace must last two seconds"); + guard.AppFrame(t0 + 1500ms, 49.0); + Expect(!guard.InGrace(t0 + 2100ms), "A healthy frame must not extend the grace"); + guard.AppFrame(t0 + 1800ms, 120.0); + Expect(guard.InGrace(t0 + 2700ms) && !guard.InGrace(t0 + 2800ms), + "A hitch must keep stalls uncounted for a second after it"); + guard.AppFrame(t0 + 4500ms, 300.0); + Expect(guard.InGrace(t0 + 4999ms) && !guard.InGrace(t0 + 5000ms), "The grace must end five seconds in"); + guard.AppFrame(t0 + 6s, 300.0); + Expect(!guard.InGrace(t0 + 6s + 1ms), "A hitch past the cap must not reopen the grace"); + Expect(guard.Early(t0 + 9999ms) && !guard.Early(t0 + 10s), "A fallback is early for ten seconds"); + guard.Engaged(t0 + 20s); + Expect(guard.InGrace(t0 + 21s) && guard.Early(t0 + 25s), "A re-engagement must start a new grace"); + guard.Reset(); + Expect(!guard.InGrace(t0 + 21s) && !guard.Early(t0 + 21s), "A reset guard must count every stall"); +} + +// S4 (DCS on Pimax, Oct 29): async engaged as the game left its loading screen +// and the first two drains timed out within half a second, so Auto fell back +// the moment it engaged. Those stalls are not counted now; stalls that persist +// past the grace still fall back, and that fallback is early. +void TestAutoProbeGraceIgnoresEngagementStalls() { + FakeRuntime runtime; + TurboHarness harness(runtime, depthxr::TurboPacingMode::kAsync); + using Peer = depthxr::TurboFrameTestPeer; + Peer::SetPolicy(depthxr::TurboPacingMode::kAsync, Peer::Source::kProbing); + Peer::ProbeGuardDefaults(); + const auto t0 = std::chrono::steady_clock::now(); + Peer::EngageProbe(t0); + Expect(!Peer::HandleTimeout(t0 + 251ms) && !Peer::HandleTimeout(t0 + 503ms) && + Peer::PacingMode() == depthxr::TurboPacingMode::kAsync, + "Two stalls in the first half second after engaging must not end the async probe"); + Expect(!Peer::HandleTimeout(t0 + 1900ms) && Peer::PacingMode() == depthxr::TurboPacingMode::kAsync, + "A third stall inside the grace must not end the async probe"); + Expect(!Peer::HandleTimeout(t0 + 3s) && Peer::PacingMode() == depthxr::TurboPacingMode::kAsync, + "The first counted stall must not end the probe on its own"); + Expect(!Peer::HandleTimeout(t0 + 3250ms) && Peer::PacingMode() == depthxr::TurboPacingMode::kSequenced && + !Peer::AutoSuspended() && Peer::FallbackEarly(), + "Two counted stalls after the grace must fall back to sequenced, as an early fallback"); + + // A game still hitching: stalls stay uncounted until a second after its + // last hitch, never past five seconds. + Peer::SetPolicy(depthxr::TurboPacingMode::kAsync, Peer::Source::kProbing); + Peer::ProbeGuardDefaults(); + const auto t1 = t0 + 60s; + Peer::EngageProbe(t1); + Peer::ProbeAppFrame(t1 + 1800ms, 140.0); + Peer::ProbeAppFrame(t1 + 2600ms, 90.0); + Expect(!Peer::HandleTimeout(t1 + 2500ms) && !Peer::HandleTimeout(t1 + 3500ms) && + Peer::PacingMode() == depthxr::TurboPacingMode::kAsync, + "Stalls while the game hitches must not count"); + Expect(!Peer::HandleTimeout(t1 + 3700ms) && Peer::PacingMode() == depthxr::TurboPacingMode::kAsync, + "The first stall after the hitch grace must count, not fall back on its own"); + Expect(!Peer::HandleTimeout(t1 + 3800ms) && Peer::PacingMode() == depthxr::TurboPacingMode::kSequenced, + "Two stalls after the hitch grace must fall back"); + + // Past the early window, a fallback is ordinary evidence. + Peer::SetPolicy(depthxr::TurboPacingMode::kAsync, Peer::Source::kProbing); + Peer::ProbeGuardDefaults(); + const auto t2 = t0 + 120s; + Peer::EngageProbe(t2); + Expect(!Peer::HandleTimeout(t2 + 20s) && !Peer::HandleTimeout(t2 + 20100ms) && + Peer::PacingMode() == depthxr::TurboPacingMode::kSequenced && !Peer::FallbackEarly(), + "A fallback twenty seconds in must not count as early"); + + // Forced async keeps its plain safety threshold: no grace. + Peer::SetPolicy(depthxr::TurboPacingMode::kAsync, Peer::Source::kForced); + Peer::ProbeGuardDefaults(); + const auto t3 = t0 + 180s; + Peer::EngageProbe(t3); + for (int stall = 0; stall < 4; ++stall) + Expect(!Peer::HandleTimeout(t3 + std::chrono::milliseconds(100 * (stall + 1))), + "Forced async suspended before its five-stall threshold"); + Expect(Peer::HandleTimeout(t3 + 500ms) && Peer::AutoSuspended(), + "Forced async stalls inside the probe grace must still suspend Turbo"); + Expect(runtime.Calls().empty(), "A policy-only test unexpectedly issued runtime calls"); +} + +// The real frame loop on an interlocking runtime (every off-thread wait stalls +// until the next submit) with the shipping grace: Auto still falls back to +// sequenced, a few seconds after engaging instead of two frames. +void TestInterlockedRuntimeAutoFallsBackAfterProbeGrace() { + FakeRuntime runtime; + runtime.InterlockOffThreadWaits(); + TurboHarness harness(runtime, depthxr::TurboPacingMode::kAsync); + using Peer = depthxr::TurboFrameTestPeer; + Peer::SetPolicy(depthxr::TurboPacingMode::kAsync, Peer::Source::kProbing); + Peer::ProbeGuardDefaults(); + const auto start = std::chrono::steady_clock::now(); + const auto end_info = FrameEndInfo(); + Expect(Peer::ForwardEndFrame(TestSession(), &end_info) == XR_SUCCESS && runtime.WaitForWaitEntered(1), + "Interlock grace test could not establish async pacing"); + int frames = 0; + while (Peer::SequencedState() != Peer::ActiveState() && frames < 24) { + Expect(AppFrame(end_info) == XR_SUCCESS, "An interlocked async frame failed during the probe grace"); + ++frames; + } + const double elapsed = ElapsedMs(start); + Expect(Peer::PacingMode() == depthxr::TurboPacingMode::kSequenced && + Peer::SequencedState() == Peer::ActiveState() && !Peer::AutoSuspended(), + "Auto did not fall back from an interlocked runtime after the probe grace (frames=" + + std::to_string(frames) + ")"); + Expect(elapsed >= 2000.0 && elapsed < 4500.0 && frames >= 6, + "The interlocked fallback took " + std::to_string(elapsed) + " ms over " + std::to_string(frames) + + " frames; the grace is two seconds"); + Expect(Peer::FallbackEarly(), "A fallback seconds after engaging must be early"); + ExpectHealthyFrames(runtime, 32, "Interlocked fallback after the grace"); +} + +struct ScopedPacingSidecar { + explicit ScopedPacingSidecar(const char* name) { + char previous_value[1024]{}; + std::size_t size = 0; + had_previous = getenv_s(&size, previous_value, sizeof(previous_value), "VECTORXR_RUNTIME_PACING_PATH") == 0 && + size > 1; + if (had_previous) previous = previous_value; + path = std::filesystem::temp_directory_path() / + ("vectorxr-turbo-" + std::string(name) + "-" + std::to_string(GetCurrentProcessId())) / + "runtime-pacing.json"; + std::error_code ec; + std::filesystem::remove_all(path.parent_path(), ec); + _putenv_s("VECTORXR_RUNTIME_PACING_PATH", path.string().c_str()); + } + ~ScopedPacingSidecar() { + depthxr::TurboFrameTestPeer::FlushPacingWrites(); + depthxr::TurboFrameTestPeer::ForgetRuntime(); + _putenv_s("VECTORXR_RUNTIME_PACING_PATH", had_previous ? previous.c_str() : ""); + std::error_code ec; + std::filesystem::remove_all(path.parent_path(), ec); + } + std::optional Read() const { + depthxr::TurboFrameTestPeer::FlushPacingWrites(); + const auto observations = depthxr::ReadRuntimePacingObservations(path); + if (observations.size() != 1) return std::nullopt; + return observations.front(); + } + void Write(const depthxr::RuntimePacingObservation& observation) const { + depthxr::TurboFrameTestPeer::FlushPacingWrites(); + std::error_code ec; + std::filesystem::remove(path, ec); + std::string error; + Expect(depthxr::RecordRuntimePacingObservation(path, observation, &error), "Sidecar rewrite failed: " + error); + } + std::filesystem::path path; + std::string previous; + bool had_previous{false}; +}; + +// An early fallback leaves a provisional sequenced verdict: the next session +// probes async again. A second early failure in a row lets the verdict stand, +// so a runtime that really interlocks is not probed every session. +void TestEarlyProbeFailureVerdictIsProvisional() { + FakeRuntime runtime; + TurboHarness harness(runtime, depthxr::TurboPacingMode::kAsync); + ScopedPacingSidecar sidecar("early-probe"); + using Peer = depthxr::TurboFrameTestPeer; + const std::string kRuntime = "Probe Test Runtime"; + const std::string kSystem = "probe-test headset"; + const auto t0 = std::chrono::steady_clock::now(); + + Peer::ResolveAutoFor(kRuntime, kSystem); + Expect(Peer::PacingMode() == depthxr::TurboPacingMode::kAsync && Peer::SourceIsProbing() && !Peer::Reprobing(), + "A runtime without a verdict must probe async"); + Peer::EngageProbe(t0); + Peer::HandleTimeout(t0 + 3s); + Peer::HandleTimeout(t0 + 3100ms); + Expect(Peer::PacingMode() == depthxr::TurboPacingMode::kSequenced && Peer::FallbackEarly() && Peer::VerdictPending(), + "The probe did not fall back early"); + Expect(!sidecar.Read(), "An early fallback must not record anything before sequenced has run clean"); + Peer::StableMinute(); + auto recorded = sidecar.Read(); + Expect(recorded && recorded->mode == depthxr::TurboPacingMode::kSequenced && recorded->early_probe_failures == 1 && + recorded->source == "discovered" && recorded->stable_seconds >= 60, + "An early fallback must record a provisional sequenced verdict (earlyProbeFailures=1)"); + Expect(depthxr::RuntimePacingReprobeReason(*recorded, VECTORXR_VERSION, "test", + static_cast(recorded->test_started_at)).has_value(), + "A provisional verdict must be due a re-probe"); + + // Next session: re-probe; it fails early again, which is recorded at once. + Peer::ResolveAutoFor(kRuntime, kSystem); + Expect(Peer::PacingMode() == depthxr::TurboPacingMode::kAsync && Peer::SourceIsProbing() && Peer::Reprobing(), + "A provisional sequenced verdict must be re-probed with async"); + const auto t1 = t0 + 60s; + Peer::EngageProbe(t1); + Expect(!Peer::HandleTimeout(t1 + 300ms), "A re-probe stall in the grace must not count"); + Peer::HandleTimeout(t1 + 2500ms); + Peer::HandleTimeout(t1 + 2600ms); + Expect(Peer::PacingMode() == depthxr::TurboPacingMode::kSequenced && Peer::SourceIsDiscovered() && + !Peer::VerdictPending() && !Peer::Reprobing(), + "A failed re-probe must fall back to the recorded sequenced verdict at once"); + recorded = sidecar.Read(); + Expect(recorded && recorded->mode == depthxr::TurboPacingMode::kSequenced && recorded->early_probe_failures == 2 && + recorded->stable_seconds >= 60 && recorded->layer_version == VECTORXR_VERSION, + "A second early failure must be recorded at once (earlyProbeFailures=2)"); + + // The third session uses the verdict: no probing loop. + Peer::ResolveAutoFor(kRuntime, kSystem); + Expect(Peer::PacingMode() == depthxr::TurboPacingMode::kSequenced && Peer::SourceIsDiscovered() && !Peer::Reprobing(), + "Two early failures in a row must let the sequenced verdict stand"); + Expect(runtime.Calls().empty(), "A policy-only test unexpectedly issued runtime calls"); +} + +// A sequenced verdict from another VectorXR version is re-probed once. A late +// failure confirms it (recorded at once, so the next session does not probe); +// an async that runs clean replaces it. +void TestSequencedVerdictReprobe() { + FakeRuntime runtime; + TurboHarness harness(runtime, depthxr::TurboPacingMode::kAsync); + ScopedPacingSidecar sidecar("reprobe"); + using Peer = depthxr::TurboFrameTestPeer; + const std::string kRuntime = "Reprobe Test Runtime"; + const std::string kSystem = "reprobe-test headset"; + depthxr::RuntimePacingObservation old; + old.test_started_at = static_cast(depthxr::RuntimeRelayUnixMilliseconds()) - 1000; + old.runtime_name = kRuntime; + old.runtime_version = "test"; + old.system_name = kSystem; + old.vendor_id = 7; + old.graphics_api = "D3D11"; + old.mode = depthxr::TurboPacingMode::kSequenced; + old.source = "discovered"; + old.layer_version = "0.17.0"; + old.last_used_unix_seconds = 1; + old.stable_seconds = 75; + sidecar.Write(old); + + Peer::ResolveAutoFor(kRuntime, kSystem); + Expect(Peer::PacingMode() == depthxr::TurboPacingMode::kAsync && Peer::Reprobing(), + "A sequenced verdict from another version must be re-probed"); + const auto t0 = std::chrono::steady_clock::now(); + Peer::EngageProbe(t0); + Peer::HandleTimeout(t0 + 30s); + Peer::HandleTimeout(t0 + 30100ms); + Expect(Peer::PacingMode() == depthxr::TurboPacingMode::kSequenced && !Peer::FallbackEarly(), + "The late re-probe failure did not fall back"); + auto recorded = sidecar.Read(); + Expect(recorded && recorded->mode == depthxr::TurboPacingMode::kSequenced && recorded->early_probe_failures == 0 && + recorded->layer_version == VECTORXR_VERSION && recorded->stable_seconds == 75 && + recorded->test_started_at > old.test_started_at, + "A late re-probe failure must re-record the sequenced verdict for this version at once"); + Peer::ResolveAutoFor(kRuntime, kSystem); + Expect(Peer::PacingMode() == depthxr::TurboPacingMode::kSequenced && !Peer::Reprobing(), + "A confirmed sequenced verdict must not be re-probed again"); + + // Old again, and this time async runs clean for its minute. + sidecar.Write(old); + Peer::ResolveAutoFor(kRuntime, kSystem); + Expect(Peer::Reprobing(), "The rewritten old verdict must be re-probed"); + Peer::StableMinute(); + recorded = sidecar.Read(); + Expect(recorded && recorded->mode == depthxr::TurboPacingMode::kAsync && recorded->early_probe_failures == 0, + "A clean async re-probe must replace the sequenced verdict"); + Peer::ResolveAutoFor(kRuntime, kSystem); + Expect(Peer::PacingMode() == depthxr::TurboPacingMode::kAsync && Peer::SourceIsDiscovered() && !Peer::Reprobing(), + "The async verdict from the re-probe must be used"); + Expect(runtime.Calls().empty(), "A policy-only test unexpectedly issued runtime calls"); +} + +// The re-probe rule itself: only sequenced verdicts, each reason bounded. +void TestRuntimePacingReprobeReasons() { + depthxr::RuntimePacingObservation verdict; + verdict.mode = depthxr::TurboPacingMode::kSequenced; + verdict.layer_version = "0.20.1"; + verdict.runtime_version = "1.2"; + constexpr std::int64_t now = 2'000'000'000'000; + verdict.test_started_at = static_cast(now - 1000); + const auto reason = [&](const depthxr::RuntimePacingObservation& o, const std::string& layer = "0.20.1", + const std::string& rt = "1.2", std::int64_t at = 2'000'000'000'000) { + return depthxr::RuntimePacingReprobeReason(o, layer, rt, at); + }; + Expect(!reason(verdict), "A fresh sequenced verdict for this version and runtime must stand"); + auto o = verdict; + o.early_probe_failures = 1; + Expect(reason(o).has_value(), "One early failure must be re-probed"); + o.early_probe_failures = depthxr::kEarlyProbeFailureLimit; + Expect(!reason(o), "Early failures at the limit must let the verdict stand"); + Expect(reason(verdict, "0.20.2").has_value(), "A verdict from another VectorXR version must be re-probed"); + Expect(reason(verdict, "0.20.1", "1.3").has_value(), "A runtime update must re-probe"); + Expect(!reason(verdict, "", "").has_value(), "Unknown versions are no reason to re-probe"); + o = verdict; + o.layer_version.clear(); + o.runtime_version.clear(); + Expect(!reason(o), "A record without versions is no reason to re-probe"); + Expect(reason(verdict, "0.20.1", "1.2", now + depthxr::kReprobeAgeMs).has_value(), + "A verdict older than 30 days must be re-probed"); + Expect(!reason(verdict, "0.20.1", "1.2", now + depthxr::kReprobeAgeMs - 2000), + "A verdict younger than 30 days must stand"); + o = verdict; + o.mode = depthxr::TurboPacingMode::kAsync; + o.early_probe_failures = 1; + Expect(!reason(o, "0.20.2", "1.3", now + 2 * depthxr::kReprobeAgeMs), "Async verdicts are never re-probed"); + + // The new field round-trips, and records from before it read as zero. + const auto path = std::filesystem::temp_directory_path() / + ("vectorxr-reprobe-reasons-" + std::to_string(GetCurrentProcessId())) / "runtime-pacing.json"; + std::error_code ec; + std::filesystem::remove_all(path.parent_path(), ec); + o = verdict; + o.runtime_name = "Round Trip"; + o.source = "discovered"; + o.stable_seconds = 60; + o.early_probe_failures = 1; + std::string error; + Expect(depthxr::RecordRuntimePacingObservation(path, o, &error), "Round-trip write failed: " + error); + auto read = depthxr::ReadRuntimePacingObservations(path); + Expect(read.size() == 1 && read.front().early_probe_failures == 1, "earlyProbeFailures did not round-trip"); + { + std::ofstream legacy(path, std::ios::trunc); + legacy << R"({"version":2,"observations":[{"testStartedAt":5,"runtimeName":"Legacy","mode":"sequenced",)" + << R"("source":"discovered","stableSeconds":60}]})"; + } + read = depthxr::ReadRuntimePacingObservations(path); + Expect(read.size() == 1 && read.front().early_probe_failures == 0, "A legacy record must read zero early failures"); + std::filesystem::remove_all(path.parent_path(), ec); +} + } // namespace // Frame Generation owns pacing through the layer's real hooks: Turbo yields, @@ -3315,8 +4093,14 @@ void TestQuadViewsOsdTagFollowsBegunConfiguration() { constexpr XrFovf kVarjoFocusFov{-0.3f, 0.3f, 0.25f, -0.25f}; int g_varjo_ends = 0; -XrResult XRAPI_PTR VarjoLocateViews(XrSession, const XrViewLocateInfo*, XrViewState*, std::uint32_t capacity, +XrResult XRAPI_PTR VarjoLocateViews(XrSession, const XrViewLocateInfo*, XrViewState* state, std::uint32_t capacity, std::uint32_t* count, XrView* views) { + // Located and tracked, as the runtime says (a view state of 0 would be + // views it left untouched). + if (state) { + state->viewStateFlags = XR_VIEW_STATE_ORIENTATION_VALID_BIT | XR_VIEW_STATE_POSITION_VALID_BIT | + XR_VIEW_STATE_ORIENTATION_TRACKED_BIT | XR_VIEW_STATE_POSITION_TRACKED_BIT; + } *count = 4; if (capacity < 4) return capacity ? XR_ERROR_SIZE_INSUFFICIENT : XR_SUCCESS; for (std::uint32_t view = 0; view < 4; ++view) { @@ -3325,6 +4109,66 @@ XrResult XRAPI_PTR VarjoLocateViews(XrSession, const XrViewLocateInfo*, XrViewSt } return XR_SUCCESS; } +// PimaxXR 0.4.4 with enable_quad_views, for an application named "DCS World" +// (instance.cpp's m_needFocusFovCorrectionQuirk): each quad xrLocateViews +// keeps the focus FOVs it returned for that displayTime (space.cpp, +// insert_or_assign), and xrEndFrame shows the focus views with those instead +// of the submitted ones (frame.cpp), the periphery with the submitted ones. +// Its focus follows the gaze only when the locate asks for foveated +// rendering (prefer_foveated_rendering is off by default). +constexpr XrFovf kPimaxPeripheryFov{-0.9f, 0.9f, 0.9f, -0.9f}; +constexpr XrFovf kPimaxFixedFocusFov{-0.3f, 0.3f, 0.3f, -0.3f}; +constexpr XrFovf kPimaxGazeFocusFov{-0.1f, 0.5f, 0.35f, -0.25f}; +struct PimaxDcsStandIn { + std::map> focus_for_time; + std::map quad_locates; + std::array shown{}; + int ends = 0; +}; +PimaxDcsStandIn g_pimax_dcs; +XrResult XRAPI_PTR PimaxDcsLocateViews(XrSession, const XrViewLocateInfo* info, XrViewState* state, + std::uint32_t capacity, std::uint32_t* count, XrView* views) { + const bool quad = info->viewConfigurationType == XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO_WITH_FOVEATED_INSET; + *count = quad ? 4 : 2; + if (state) { + state->viewStateFlags = XR_VIEW_STATE_ORIENTATION_VALID_BIT | XR_VIEW_STATE_POSITION_VALID_BIT | + XR_VIEW_STATE_ORIENTATION_TRACKED_BIT | XR_VIEW_STATE_POSITION_TRACKED_BIT; + } + if (capacity == 0) return XR_SUCCESS; + if (capacity < *count) return XR_ERROR_SIZE_INSUFFICIENT; + bool foveated = false; + for (auto* chain = static_cast(info->next); chain; chain = chain->next) { + if (chain->type == XR_TYPE_VIEW_LOCATE_FOVEATED_RENDERING_VARJO) { + foveated = reinterpret_cast(chain)->foveatedRenderingActive; + } + } + for (std::uint32_t view = 0; view < *count; ++view) { + views[view].pose = {{0.0f, 0.0f, 0.0f, 1.0f}, {view % 2 == 0 ? -0.032f : 0.032f, 0.0f, 0.0f}}; + views[view].fov = view < 2 ? kPimaxPeripheryFov : foveated ? kPimaxGazeFocusFov : kPimaxFixedFocusFov; + } + if (quad) { + g_pimax_dcs.focus_for_time.insert_or_assign(info->displayTime, std::make_pair(views[2].fov, views[3].fov)); + ++g_pimax_dcs.quad_locates[info->displayTime]; + } + return XR_SUCCESS; +} +XrResult XRAPI_PTR PimaxDcsEndFrame(XrSession, const XrFrameEndInfo* info) { + ++g_pimax_dcs.ends; + for (std::uint32_t i = 0; i < info->layerCount; ++i) { + if (info->layers[i]->type != XR_TYPE_COMPOSITION_LAYER_PROJECTION) continue; + const auto* projection = reinterpret_cast(info->layers[i]); + for (std::uint32_t view = 0; view < projection->viewCount && view < 4; ++view) { + XrFovf fov = projection->views[view].fov; + const auto stored = g_pimax_dcs.focus_for_time.find(info->displayTime); + if (view >= 2 && stored != g_pimax_dcs.focus_for_time.end()) { + fov = view == 2 ? stored->second.first : stored->second.second; + } + g_pimax_dcs.shown[view] = fov; + } + } + return XR_SUCCESS; +} + XrResult XRAPI_PTR VarjoEndFrame(XrSession, const XrFrameEndInfo*) { ++g_varjo_ends; return XR_SUCCESS; @@ -4333,6 +5177,81 @@ void TestVarjoNativeSubmittedFocusWithConvergence() { Peer::EndVarjoNative(); } +// DCS World on PimaxXR's native quad views: the runtime shows the focus views +// with the FOVs it located for the frame's display time, whatever the game +// submits (the game's own are not right, hence the runtime's quirk). What it +// shows of each focus view must stand to what the game rendered as the +// periphery does, with Depth's convergence set: as rendered without Depth +// Anchor, shifted back to the runtime's own with it. A locate of VectorXR's +// own at the game's display time must not replace the game's entry. +void TestPimaxDcsFocusFovQuirk() { + using Peer = depthxr::TurboFrameTestPeer; + for (const bool anchor : {false, true}) { + g_pimax_dcs = {}; + Peer::PrepareVarjoNative(&PimaxDcsLocateViews, &PimaxDcsEndFrame); + Peer::ConvergeVarjoDepth(0.05); + Peer::PimaxDcs(anchor); + Expect(Peer::TakesFocusFovsFromLocate("PimaxXR (Unofficial)") && + !Peer::TakesFocusFovsFromLocate("Varjo OpenXR Runtime") && + !Peer::TakesFocusFovsFromLocate("Some other native quad views runtime"), + "Only a Pimax runtime is taken to show DCS's focus views with its located FOVs"); + const XrTime time = 32'000'000'000 + (anchor ? 11'111'111 : 0); + XrViewLocateFoveatedRenderingVARJO foveated{XR_TYPE_VIEW_LOCATE_FOVEATED_RENDERING_VARJO}; + foveated.foveatedRenderingActive = XR_TRUE; + XrViewLocateInfo locate{XR_TYPE_VIEW_LOCATE_INFO, &foveated}; + locate.viewConfigurationType = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO_WITH_FOVEATED_INSET; + locate.displayTime = time; + XrViewState state{XR_TYPE_VIEW_STATE}; + std::array located{}; + for (auto& view : located) view = {XR_TYPE_VIEW}; + std::uint32_t count = 0; + Expect(Peer::Layer().LocateViews(TestSession(), &locate, &state, 4, &count, located.data()) == XR_SUCCESS && + count == 4, + "A PimaxXR quad locate failed"); + Expect(located[0].fov.angleLeft != kPimaxPeripheryFov.angleLeft, + "Convergence did not shift the periphery (the test would prove nothing)"); + std::array views{}; + for (std::uint32_t view = 0; view < 4; ++view) { + views[view] = {XR_TYPE_COMPOSITION_LAYER_PROJECTION_VIEW}; + views[view].pose = located[view].pose; + // DCS submits its focus views with other FOVs than located. + views[view].fov = view < 2 ? located[view].fov : kPimaxFixedFocusFov; + views[view].subImage.swapchain = reinterpret_cast(static_cast(0x91 + view)); + views[view].subImage.imageRect = {{0, 0}, {1024, 1024}}; + } + XrCompositionLayerProjection projection{XR_TYPE_COMPOSITION_LAYER_PROJECTION}; + projection.viewCount = 4; + projection.views = views.data(); + const auto* layer = reinterpret_cast(&projection); + XrFrameEndInfo info = FrameEndInfo(); + info.displayTime = time; + info.layerCount = 1; + info.layers = &layer; + Expect(Peer::Layer().EndFrame(TestSession(), &info) == XR_SUCCESS && g_pimax_dcs.ends == 1, + "A PimaxXR quad frame did not end"); + const std::string mode = anchor ? "with Depth Anchor" : "without Depth Anchor"; + // Convergence moves each view's projection centre by one tangent. + const auto moved = [](const XrFovf& shown, const XrFovf& rendered) { + return std::tan(static_cast(shown.angleLeft)) - std::tan(static_cast(rendered.angleLeft)); + }; + for (std::uint32_t eye = 0; eye < 2; ++eye) { + const double periphery = moved(g_pimax_dcs.shown[eye], located[eye].fov); + const double focus = moved(g_pimax_dcs.shown[2 + eye], located[2 + eye].fov); + Expect(std::abs(focus - periphery) < 1e-5, + "The focus view must be shown as the periphery is against what was rendered, " + mode + + " (eye " + std::to_string(eye) + ": periphery " + std::to_string(periphery) + ", focus " + + std::to_string(focus) + ")"); + } + const auto entry = g_pimax_dcs.focus_for_time.at(time); + Expect(Peer::EyeOffsets(time) && g_pimax_dcs.quad_locates[time] == 1 && + entry.first.angleLeft == g_pimax_dcs.focus_for_time.at(time).first.angleLeft && + g_pimax_dcs.focus_for_time.at(time).first.angleLeft == kPimaxGazeFocusFov.angleLeft, + "Pivot's eye offsets must not replace the game's focus FOVs at its display time, " + mode); + Peer::EndPimaxDcs(); + Peer::EndVarjoNative(); + } +} + // A settings file with a key this layer does not know (a newer app's) still // loads. Each unknown key is logged once per log: at the first load, not again // on reloads, and a reload that adds one logs only that one; the next @@ -4557,6 +5476,10 @@ int main() { TestAsyncAppFramesTargetTheirRuntimeFrame(); TestAsyncUnechoedAbandonedFramesDoNotLead(); TestAsyncTwoThreadFramesNeedNoRaise(); + TestSequencedAppFramesTargetTheirRuntimeFrame(); + TestSequencedTwoThreadFramesNeedNoRaise(); + TestUpstreamTurboDetectorWindow(); + TestUpstreamToolkitTurboStandsAside(); TestFrameGenOwnsFramePacing(); TestConfigReloadWhileFrameGenOwnsPacing(); TestAsyncTogglePreservesQueuedFrame(false); @@ -4584,12 +5507,19 @@ int main() { TestAutoSubmissionErrorsTryBothModes(); TestAutoHasNoRuntimeMappings(); TestSequencedStallPolicySuspends(); + TestProbeGuardScriptedTimes(); + TestAutoProbeGraceIgnoresEngagementStalls(); + TestInterlockedRuntimeAutoFallsBackAfterProbeGrace(); + TestEarlyProbeFailureVerdictIsProvisional(); + TestSequencedVerdictReprobe(); + TestRuntimePacingReprobeReasons(); Expect(depthxr::TurboFrameTestPeer::OsdStateTransitions(), "OSD lost enabled/off Turbo or applied/ready/disabled Pivot state"); TestVarjoNativeSubmittedFocusCountsEveryFrame(); TestVarjoDeferredReleasesKeepFrameGenUnblocked(); TestFrameGenSessionKeepsShownLayersLazy(); TestQueueSlowRuntimeDiagnosticsAreOptInAndBounded(); TestVarjoNativeSubmittedFocusWithConvergence(); + TestPimaxDcsFocusFovQuirk(); TestTopologyFollowsFrameGenClaimsApart(); TestQuadViewsInactiveReasons(); TestQuadViewsOsdTagFollowsBegunConfiguration(); diff --git a/layer/tests/vulkan_bridge_layer_tests.cpp b/layer/tests/vulkan_bridge_layer_tests.cpp index 1792d9af..cc87d8a5 100644 --- a/layer/tests/vulkan_bridge_layer_tests.cpp +++ b/layer/tests/vulkan_bridge_layer_tests.cpp @@ -57,6 +57,7 @@ #include #include #include +#include #include #include #include @@ -595,10 +596,10 @@ void ClearVulkan(VulkanGame& game, VkImage image, std::uint32_t layers, const Co SubmitCommands(game, slot); } -void ClearVulkanDepth(VulkanGame& game, VkImage image, std::uint32_t layers, float depth) { +void ClearVulkanDepth(VulkanGame& game, VkImage image, std::uint32_t layers, float depth, std::uint32_t first = 0) { std::uint32_t slot = 0; const VkCommandBuffer commands = BeginCommands(game, slot); - const VkImageSubresourceRange range{VK_IMAGE_ASPECT_DEPTH_BIT, 0, 1, 0, layers}; + const VkImageSubresourceRange range{VK_IMAGE_ASPECT_DEPTH_BIT, 0, 1, first, layers}; VkImageMemoryBarrier barrier{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER}; barrier.srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; @@ -923,6 +924,8 @@ struct StandIn { XrTime time{1'000'000'000}; std::chrono::steady_clock::time_point refresh{}; // paced: the refresh the last wait returned at std::uintptr_t handles{0x1000}; + int refused_frames{0}; // xrEndFrame refused for a sub-image outside its image (SubImageRefusal) + std::string refusal; // the first refused frame's: why // The index of the first `call` at or after `from`, or -1. std::ptrdiff_t Position(const std::string& call, std::ptrdiff_t from = 0) const { @@ -1628,10 +1631,63 @@ XrResult XRAPI_CALL RtBeginFrame(XrSession, const XrFrameBeginInfo*) { return XR_SUCCESS; } +// As PimaxXR's and Virtual Desktop's xrEndFrame check a projection's +// sub-images (holding g_runtime_mutex): each view's colour and depth rect +// inside its image (XR_ERROR_SWAPCHAIN_RECT_INVALID), and its depth's slice +// one of the image's (XR_ERROR_VALIDATION_FAILURE). XR_SUCCESS: none +// outside. `why` says which. +XrResult SubImageRefusal(const XrFrameEndInfo& info, std::string& why) { + const auto check = [&](const XrSwapchainSubImage& sub, const char* what, bool depth) { + const StandInSwapchain* chain = FindSwapchain(sub.swapchain); + if (!chain) { + why = std::string(what) + " names no swapchain"; + return XR_ERROR_HANDLE_INVALID; + } + const XrRect2Di& rect = sub.imageRect; + const std::int64_t right = static_cast(rect.offset.x) + rect.extent.width; + const std::int64_t bottom = static_cast(rect.offset.y) + rect.extent.height; + std::ostringstream text; + text << what << " rect " << rect.offset.x << "," << rect.offset.y << " " << rect.extent.width << "x" + << rect.extent.height << " slice " << sub.imageArrayIndex << " on " << chain->info.width << "x" + << chain->info.height << " of " << chain->info.arraySize; + if (depth && sub.imageArrayIndex >= chain->info.arraySize) { + why = text.str(); + return XR_ERROR_VALIDATION_FAILURE; + } + if (rect.offset.x < 0 || rect.offset.y < 0 || rect.extent.width <= 0 || rect.extent.height <= 0 || + right > static_cast(chain->info.width) || + bottom > static_cast(chain->info.height)) { + why = text.str(); + return XR_ERROR_SWAPCHAIN_RECT_INVALID; + } + return XR_SUCCESS; + }; + for (std::uint32_t i = 0; i < info.layerCount; ++i) { + const XrCompositionLayerBaseHeader* base = info.layers[i]; + if (!base || base->type != XR_TYPE_COMPOSITION_LAYER_PROJECTION) continue; + const auto& projection = *reinterpret_cast(base); + for (std::uint32_t v = 0; v < projection.viewCount; ++v) { + const XrCompositionLayerProjectionView& view = projection.views[v]; + if (const XrResult result = check(view.subImage, "colour", false); result != XR_SUCCESS) return result; + for (auto* chain = static_cast(view.next); chain; chain = chain->next) { + if (chain->type != XR_TYPE_COMPOSITION_LAYER_DEPTH_INFO_KHR) continue; + const auto& depth = *reinterpret_cast(chain); + if (const XrResult result = check(depth.subImage, "depth", true); result != XR_SUCCESS) return result; + } + } + } + return XR_SUCCESS; +} + XrResult XRAPI_CALL RtEndFrame(XrSession, const XrFrameEndInfo* info) { std::scoped_lock lock(g_runtime_mutex); StandIn& rt = g_runtime; rt.calls.push_back("xrEndFrame"); + std::string why; + if (const XrResult refused = SubImageRefusal(*info, why); refused != XR_SUCCESS) { + if (rt.refused_frames++ == 0) rt.refusal = why; + return refused; + } ShownFrame frame; frame.time = info->displayTime; for (std::uint32_t i = 0; i < info->layerCount; ++i) { @@ -2561,6 +2617,8 @@ struct FrameOptions { // depth slice too) ahead of its depth. Needs `depth`. const Target* motion{nullptr}; Color motion_color{64, 128, 32, 255}; + // Changes the views' depth structures before the frame ends (with `depth`). + std::function&)> edit_depth; }; // What a Star Citizen-ordered frame's images hold when released (FrameOptions::release_before_begin). @@ -2711,6 +2769,9 @@ XrResult Frame(App& app, const Renderer& renderer, const std::vector(mapped); + for (std::uint32_t y = 0; y < height; ++y) { + for (std::uint32_t x = 0; x < width; ++x) values[y * width + x] = y < height / 2 ? top : bottom; + } + std::uint32_t slot = 0; + const VkCommandBuffer commands = BeginCommands(game, slot); + VkImageMemoryBarrier barrier{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER}; + barrier.srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; + barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; + barrier.oldLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; + barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; + barrier.srcQueueFamilyIndex = barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; + barrier.image = image; + barrier.subresourceRange = {VK_IMAGE_ASPECT_DEPTH_BIT, 0, 1, layer, 1}; + const VkPipelineStageFlags depth_stages = + VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT; + g_vk.vkCmdPipelineBarrier(commands, depth_stages, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, + &barrier); + VkBufferImageCopy region{}; + region.imageSubresource = {VK_IMAGE_ASPECT_DEPTH_BIT, 0, layer, 1}; + region.imageExtent = {width, height, 1}; + g_vk.vkCmdCopyBufferToImage(commands, staging, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion); + std::swap(barrier.oldLayout, barrier.newLayout); + barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; + barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; + g_vk.vkCmdPipelineBarrier(commands, VK_PIPELINE_STAGE_TRANSFER_BIT, depth_stages, 0, 0, nullptr, 0, nullptr, 1, + &barrier); + SubmitCommands(game, slot); + Check(g_vk.vkWaitForFences(game.device, 1, &game.fences[slot], VK_TRUE, 10'000'000'000ull) == VK_SUCCESS, + "the depth upload finishes"); + g_vk.vkDestroyBuffer(game.device, staging, nullptr); + g_vk.vkFreeMemory(game.device, memory, nullptr); +} +// 13b. Depth Star Citizen writes after its frame's end (CIG, 2026-10-29: after +// xrReleaseSwapchainImage; every hand-over at xrEndFrame found it all zero). +// Each frame the game's depth swapchain holds zero at its release and gets the +// frame's depth (each eye's slice its own value) after xrEndFrame. The bridge +// checks at every hand-over here (VECTORXR_VULKAN_BRIDGE_DEPTH_CHECK_MS=0): +// the runtime's depth is zero until the bridge finds the depth at the next +// acquire, then each frame's views carry the frame before's depth, for each +// eye its own. With VECTORXR_VULKAN_BRIDGE_DEPTH=end-frame it stays zero; +// with =private the views carry no depth at all (the colour still arrives). +void TestLateDepthSession() { + SetEnv("VECTORXR_VULKAN_BRIDGE_DEPTH_CHECK_MS", "0"); + // Frame Generation stands by here (it never sees the depth to find it + // empty): the check is forced on. + SetEnv("VECTORXR_VULKAN_BRIDGE_DEPTH_CHECK", "always"); + for (const std::string timing : {"", "end-frame", "private"}) { + SetEnv("VECTORXR_VULKAN_BRIDGE_DEPTH", timing); + // Frame Generation on (the bridge is made for it), standing by: the + // runtime gets the game's frames as they are. + BeginCase(timing.empty() ? "late-depth" : "late-depth-" + timing); + App app = StartApp({kVulkan2Extension}); + VulkanGame game = MakeVulkanGame(app); + SessionCreate create(game, app.system); + Check(app.api.create_session(app.api.instance, &create.info, &app.session) == XR_SUCCESS && + Peer::Bridge().Active(), + "a bridged session (" + Unbridged(Peer::Log()) + ")"); + BeginSession(app); + const Renderer vulkan{&game, nullptr}; + Target left = MakeTarget(app, ColorInfo(), "the left eye"); + Target right = MakeTarget(app, ColorInfo(), "the right eye"); + Target depth = MakeTarget(app, DepthInfo(VK_FORMAT_D32_SFLOAT, 2), "depth"); + // Each eye's slice: its upper half 0.1 nearer than its lower, which + // the stand-in reads (the centre). + const auto written = [](int frame, int eye) { return 0.2f + 0.05f * static_cast(frame) + 0.01f * eye; }; + g_runtime.read_depth = true; + int late_from = -1; + constexpr int kFrames = 10; + for (int frame = 0; frame < kFrames; ++frame) { + const std::vector colors{FrameColor(frame + 30, 0), FrameColor(frame + 30, 1)}; + FrameOptions options; + options.depth = &depth; + options.depth_value = 0.0f; // what it holds at its release + const bool depth_mirrored = timing != "private"; + const std::uint64_t used_before = + depth_mirrored ? SummaryValue(Peer::Bridge().Describe(depth.swapchain), "lateUsed") : 0; + Check(Frame(app, vulkan, {&left, &right}, colors, {{{&left, 0}, {&right, 0}}}, options) == XR_SUCCESS, + "late depth frame " + std::to_string(frame)); + // The frame's hand-over copied the late copy (which the frame's + // acquire made, once the check had decided; at its end or at that + // acquire, as the game's queue got through the check's reads). + const bool late = + depth_mirrored && SummaryValue(Peer::Bridge().Describe(depth.swapchain), "lateUsed") > used_before; + if (late && late_from < 0) late_from = frame; + Check(!(late_from >= 0 && !late), "late depth: once taken late, every frame is (frame " + + std::to_string(frame) + ")"); + std::uint32_t released = 0; + { + std::scoped_lock lock(g_runtime_mutex); + const ShownFrame& shown = g_runtime.frames.back(); + Check(shown.views.size() == 2, "late depth: two views"); + for (int eye = 0; eye < 2 && shown.views.size() == 2; ++eye) { + const ShownView& view = shown.views[eye]; + Check(Near(view.texel, colors[eye]), "late depth frame: the runtime shows " + Text(view.texel)); + if (timing == "private") { + Check(!view.depth, "private depth: the views carry none"); + continue; + } + const float expected = late ? written(frame - 1, eye) : 0.0f; + Check(view.depth && view.depth_value && *view.depth_value == expected, + "late depth (" + timing + ") frame " + std::to_string(frame) + " eye " + std::to_string(eye) + + ": the runtime's depth holds " + + (view.depth_value ? std::to_string(*view.depth_value) : "nothing") + ", expected " + + std::to_string(expected) + (late ? " (the frame before's)" : "") + "; " + + Peer::Bridge().Describe(depth.swapchain)); + } + const StandInSwapchain* chain = FindSwapchain(depth.swapchain); + Check(chain && chain->released, "late depth: the depth image is released to the runtime"); + if (chain && chain->released) released = *chain->released; + } + // After xrEndFrame: the game's depth for this frame, each eye's slice. + for (int eye = 0; eye < 2; ++eye) { + WriteVulkanDepthHalves(game, reinterpret_cast(depth.images[released]), depth.info.width, + depth.info.height, static_cast(eye), written(frame, eye) + 0.1f, + written(frame, eye)); + } + + } + g_runtime.read_depth = false; + const std::string log = Peer::Log(); + if (timing.empty()) { + Check(late_from >= 3 && late_from <= 6, + "late depth: the bridge takes it late within a few frames (from frame " + std::to_string(late_from) + + "): " + Peer::Bridge().Describe(depth.swapchain)); + Check(Contains(log, "the game writes its depth after its frame's end"), "late depth: the log says so"); + } else { + Check(late_from < 0, "late depth (" + timing + "): never taken late"); + } + if (timing == "end-frame") { + Check(Contains(log, "VECTORXR_VULKAN_BRIDGE_DEPTH=end-frame keeps taking it"), + "late depth (end-frame): the log says where the depth is"); + } + if (timing == "private") { + Check(Contains(log, "keeps the game's depth to itself") && + Contains(log, "private because VECTORXR_VULKAN_BRIDGE_DEPTH=private"), + "late depth (private): the log says the depth is left out, and why"); + } + CheckValidation("late depth " + timing); + Check(app.api.destroy_swapchain(depth.swapchain) == XR_SUCCESS, "the depth swapchain is destroyed"); + StopApp(app); + DestroyGame(game); + EndCase(); + } + SetEnv("VECTORXR_VULKAN_BRIDGE_DEPTH", ""); + SetEnv("VECTORXR_VULKAN_BRIDGE_DEPTH_CHECK_MS", ""); + SetEnv("VECTORXR_VULKAN_BRIDGE_DEPTH_CHECK", ""); +} // 13c. Star Citizen's shapes (its log, 2026-10-09): XR_KHR_vulkan_enable2, an // sRGB swapchain per eye the game only copies into (TRANSFER_DST alone), and // its interface on a quad swapchain (TRANSFER_DST alone too) shown over the @@ -5431,6 +5663,147 @@ void TestNativeD3D12(const std::string& name) { EndCase(); } +// 14b. Depth whose sub-image lies outside its image, in each session that +// forwards XR_KHR_composition_layer_depth here: the game's own Vulkan +// session (VECTORXR_VULKAN_BRIDGE=off), a native Direct3D 12 one and a +// Vulkan-bridged one. The stand-in refuses such a frame as PimaxXR does; the +// layer leaves its projection's depth out of both views, the colour going on +// (as the Direct3D 11 sessions' TestDepthRects in d3d11_bridge_layer_tests), +// and valid depth reaches the runtime, again after the refused cases. +void TestDepthRects(const std::string& kind) { + const bool d3d12 = kind == "native-d3d12"; + const bool bridged = kind == "vulkan-bridge"; + const std::string name = "depth-rects-" + kind; + BeginCase(name); + // Frame Generation does not engage: the runtime shows the game's own frames. + Peer::SetFrameGenEngagement(1'000'000, std::chrono::hours(1)); + if (kind == "vulkan-native") SetEnv("VECTORXR_VULKAN_BRIDGE", "off"); + App app = StartApp({d3d12 ? kD3D12Extension : kVulkan2Extension}); + std::optional game; + D3D12Game game12; + std::optional create; + XrBaseInStructure application_struct{kApplicationStructType}; + XrGraphicsBindingD3D12KHR binding12{XR_TYPE_GRAPHICS_BINDING_D3D12_KHR}; + XrSessionCreateInfo info12{XR_TYPE_SESSION_CREATE_INFO}; + if (d3d12) { + XrGraphicsRequirementsD3D12KHR requirements{XR_TYPE_GRAPHICS_REQUIREMENTS_D3D12_KHR}; + Check(app.api.d3d12_requirements && + app.api.d3d12_requirements(app.api.instance, app.system, &requirements) == XR_SUCCESS, + name + ": xrGetD3D12GraphicsRequirementsKHR"); + Check(MakeD3D12Game(g_gpu.luid, game12), name + ": the Direct3D 12 game's device and queue"); + binding12.next = &application_struct; + binding12.device = game12.device.Get(); + binding12.queue = game12.queue.Get(); + info12.next = &binding12; + info12.systemId = app.system; + } else { + game = MakeVulkanGame(app); + create.emplace(*game, app.system); + } + Check(app.api.create_session(app.api.instance, d3d12 ? &info12 : &create->info, &app.session) == XR_SUCCESS && + Peer::Bridge().Active() == bridged, + name + ": xrCreateSession, " + (bridged ? "bridged" : "not bridged") + " (" + Unbridged(Peer::Log()) + ")"); + Check(!d3d12 || Peer::GraphicsApi() == "D3D12", name + ": a Direct3D 12 session (" + Peer::GraphicsApi() + ")"); + BeginSession(app); + const std::int64_t colour_format = d3d12 ? DXGI_FORMAT_R8G8B8A8_UNORM_SRGB : VK_FORMAT_R8G8B8A8_SRGB; + const std::int64_t depth_format = d3d12 ? DXGI_FORMAT_D32_FLOAT : VK_FORMAT_D32_SFLOAT; + Target left = MakeTarget(app, ColorInfo(colour_format), "the left eye", d3d12); + Target right = MakeTarget(app, ColorInfo(colour_format), "the right eye", d3d12); + const auto depth_target = [&](std::uint32_t width, std::uint32_t height, const char* what) { + XrSwapchainCreateInfo info = DepthInfo(depth_format, 2); + info.width = width; + info.height = height; + Target target = MakeTarget(app, info, what, d3d12); + Check(!Peer::Bridge().Unmirrored(target.swapchain), name + ": " + what + ": not kept private"); + return target; + }; + // The colour images are 64x48. An upscaler's smaller depth, a larger + // one, and one image for both eyes side by side. + Target smaller = depth_target(48, 36, "smaller depth"); + Target larger = depth_target(80, 60, "larger depth"); + Target side_by_side = depth_target(2 * kWidth, kHeight, "side-by-side depth"); + constexpr std::int32_t kMax = std::numeric_limits::max(); + struct Case { + const char* what; + const Target* depth; + std::array rects; + std::array slices; + bool kept; + }; + const XrRect2Di colour_rect{{0, 0}, {static_cast(kWidth), static_cast(kHeight)}}; + const XrRect2Di right_half{{static_cast(kWidth), 0}, colour_rect.extent}; + const XrRect2Di right_half_past{{static_cast(kWidth) + 1, 0}, colour_rect.extent}; + const std::vector cases{ + {"valid, both slices", &smaller, {{{{0, 0}, {48, 36}}, {{0, 0}, {48, 36}}}}, {0, 1}, true}, + {"the colour's rect on a smaller depth image (UEVR)", &smaller, {colour_rect, colour_rect}, {0, 1}, false}, + {"one texel too wide, left eye only", &smaller, {{{{0, 0}, {49, 36}}, {{0, 0}, {48, 36}}}}, {0, 1}, false}, + {"valid at a nonzero origin on a larger image", &larger, {{{{8, 6}, {64, 48}}, {{16, 12}, {64, 48}}}}, {0, 1}, + true}, + {"one texel past the larger image's bottom", &larger, {{{{8, 13}, {64, 48}}, {{8, 12}, {64, 48}}}}, {0, 1}, + false}, + {"side by side, one slice", &side_by_side, {colour_rect, right_half}, {0, 0}, true}, + {"side by side, the right eye one texel past", &side_by_side, {colour_rect, right_half_past}, {0, 0}, false}, + {"a negative offset", &smaller, {{{{-1, 0}, {48, 36}}, {{0, 0}, {48, 36}}}}, {0, 1}, false}, + {"a zero extent", &smaller, {{{{0, 0}, {0, 36}}, {{0, 0}, {48, 36}}}}, {0, 1}, false}, + {"a slice past the image", &smaller, {{{{0, 0}, {48, 36}}, {{0, 0}, {48, 36}}}}, {0, 2}, false}, + {"an extent whose far edge overflows", &smaller, {{{{0, 0}, {48, 36}}, {{1, 1}, {kMax, kMax}}}}, {0, 1}, false}, + {"valid again", &smaller, {{{{0, 0}, {48, 36}}, {{0, 0}, {48, 36}}}}, {0, 1}, true}, + }; + const auto refusal = [] { + std::scoped_lock lock(g_runtime_mutex); + return std::to_string(g_runtime.refused_frames) + " refused" + + (g_runtime.refusal.empty() ? "" : ", the first: " + g_runtime.refusal); + }; + const Renderer renderer{game ? &*game : nullptr, d3d12 ? &game12 : nullptr}; + int frame_index = 0; + for (const Case& test : cases) { + for (int frame = 0; frame < 2; ++frame) { + const std::string at = name + ": " + test.what + ", frame " + std::to_string(frame); + const std::vector colors{FrameColor(frame_index, 0), FrameColor(frame_index, 1)}; + ++frame_index; + const std::size_t before = ShownCount(); + FrameOptions options; + options.depth = test.depth; + options.edit_depth = [&](std::array& depth) { + for (int eye = 0; eye < 2; ++eye) { + depth[eye].subImage.imageRect = test.rects[eye]; + depth[eye].subImage.imageArrayIndex = test.slices[eye]; + } + }; + const XrResult result = Frame(app, renderer, {&left, &right}, colors, {{{&left, 0}, {&right, 0}}}, options); + Check(result == XR_SUCCESS, at + ": xrEndFrame " + std::to_string(result) + " (" + refusal() + ")"); + const std::vector shown = Shown(before); + Check(shown.size() == 1 && shown[0].views.size() == 2, at + ": one frame of two views shown"); + if (shown.size() != 1 || shown[0].views.size() != 2) continue; + for (int eye = 0; eye < 2; ++eye) { + const ShownView& view = shown[0].views[eye]; + Check(view.depth == test.kept, + at + ": eye " + std::to_string(eye) + (test.kept ? " has" : " has no") + " depth"); + Check(!test.kept || view.depth_swapchain == test.depth->swapchain, + at + ": the view's depth is the game's swapchain"); + Check(view.swapchain == (eye ? right.swapchain : left.swapchain), at + ": the view's colour goes on"); + // The stand-in reads a Direct3D 12 session's images back. + Check(!(d3d12 || bridged) || Near(view.texel, colors[eye]), + at + ": eye " + std::to_string(eye) + " shows " + (view.texel ? Text(*view.texel) : "nothing") + + ", rendered " + Text(colors[eye])); + } + } + } + Check(refusal() == "0 refused", name + ": the runtime refused no frame (" + refusal() + ")"); + const std::string log = Peer::Log(); + Check(Count(log, "The game's depth sub-image lies outside its depth image") == 1, + name + ": the log tells once of depth outside its image"); + Check(!Contains(log, "keeps the game's depth to itself"), name + ": no depth swapchain is private"); + StopApp(app); + if (kind == "vulkan-native") SetEnv("VECTORXR_VULKAN_BRIDGE", ""); + Peer::SetFrameGenEngagement(45, std::chrono::milliseconds(3000)); // the presenter's own + if (game) { + Check(GameRuns(*game), name + ": the game's device works"); + DestroyGame(*game); + } + EndCase(); +} + // 15. The bridge's device removed: the session is lost. Last: Direct3D 12 // makes one device per adapter in a process. void TestDeviceLost() { @@ -5684,6 +6057,7 @@ int main(int argc, char** argv) { g_unbridged_only = argc > 1 && std::string(argv[1]) == "unbridged"; const bool depth_paths_only = argc > 1 && std::string(argv[1]) == "depth-paths"; const bool teardown_only = argc > 1 && std::string(argv[1]) == "teardown"; + const bool late_depth_only = argc > 1 && std::string(argv[1]) == "late-depth"; const char* no_stall = std::getenv("FG_TEST_NO_STALL"); const bool skip_negative_controls = no_stall && std::string(no_stall) == "1"; const bool require = vulkan_test::RequireHardware(); @@ -5712,8 +6086,10 @@ int main(int argc, char** argv) { IsolateHost(); Peer::UseFrameGenGpu(MakeStandInGpu); - if (depth_paths_only || teardown_only) { - if (teardown_only) { + if (depth_paths_only || teardown_only || late_depth_only) { + if (late_depth_only) { + TestLateDepthSession(); + } else if (teardown_only) { TestBridgedSession(2); TestBridgedSession(1); TestDestroyInstanceWithSession(); @@ -5725,8 +6101,8 @@ int main(int argc, char** argv) { Peer::ReleaseLog(g_root / "released" / "vectorxr.log"); std::error_code error; std::filesystem::remove_all(g_root, error); - std::cout << "vulkan bridge layer tests passed (" << (teardown_only ? "teardown" : "depth paths") - << " only)\n"; + std::cout << "vulkan bridge layer tests passed (" + << (teardown_only ? "teardown" : late_depth_only ? "late depth" : "depth paths") << " only)\n"; return 0; } // The cases that need no working bridge. @@ -5738,6 +6114,8 @@ int main(int argc, char** argv) { TestBridgeOffWithD3D11(); TestVulkanBindingNotFirst(); TestNativeD3D12("native-d3d12"); + TestDepthRects("vulkan-native"); + TestDepthRects("native-d3d12"); if (!g_unbridged_only) { // The bridge's. TestCachedCreateDevice(); @@ -5752,7 +6130,9 @@ int main(int argc, char** argv) { TestBridgedFrameGen("extrapolate"); TestStarCitizenShapes(); TestStarCitizenMotion(); + TestLateDepthSession(); TestDepthWritePaths(); + TestDepthRects("vulkan-bridge"); if (!skip_negative_controls) { TestStalledTeardown(false); TestStalledTeardown(true); diff --git a/layer/tests/vulkan_bridge_tests.cpp b/layer/tests/vulkan_bridge_tests.cpp index db0b0a71..9d7870c1 100644 --- a/layer/tests/vulkan_bridge_tests.cpp +++ b/layer/tests/vulkan_bridge_tests.cpp @@ -23,6 +23,7 @@ #include #include +#include "depthxr/framegen_gpu.h" #include "depthxr/framegen_plan.h" #include "depthxr/framegen_vulkan_bridge.h" #include "test_adapter.h" @@ -2135,6 +2136,642 @@ void TestDepthValues(Game& game) { CheckClean(nullptr, "depth values (End)"); } +// The game's depth texels written from host memory, as a game's own copy +// into its depth swapchain's image would (a 32-bit float depth image, one +// slice at a time): from the attachment layout and back. +struct DepthUpload { + VkBuffer buffer{VK_NULL_HANDLE}; + VkDeviceMemory memory{VK_NULL_HANDLE}; + float* values{nullptr}; + std::uint32_t width{0}; + std::uint32_t height{0}; +}; + +DepthUpload MakeDepthUpload(Game& game, std::uint32_t width, std::uint32_t height) { + DepthUpload upload; + upload.width = width; + upload.height = height; + VkBufferCreateInfo info{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; + info.size = VkDeviceSize{width} * height * sizeof(float); + info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; + Check(game.vkCreateBuffer(game.device, &info, nullptr, &upload.buffer) == VK_SUCCESS, "a depth upload buffer"); + VkMemoryRequirements requirements{}; + game.vkGetBufferMemoryRequirements(game.device, upload.buffer, &requirements); + VkMemoryAllocateInfo allocate{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO}; + allocate.allocationSize = requirements.size; + allocate.memoryTypeIndex = MemoryType(game, requirements.memoryTypeBits, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); + void* mapped = nullptr; + Check(game.vkAllocateMemory(game.device, &allocate, nullptr, &upload.memory) == VK_SUCCESS && + game.vkBindBufferMemory(game.device, upload.buffer, upload.memory, 0) == VK_SUCCESS && + game.vkMapMemory(game.device, upload.memory, 0, VK_WHOLE_SIZE, 0, &mapped) == VK_SUCCESS, + "depth upload memory"); + upload.values = static_cast(mapped); + return upload; +} + +void FreeDepthUpload(Game& game, DepthUpload& upload) { + GameIdle(game); + game.vkDestroyBuffer(game.device, upload.buffer, nullptr); + game.vkFreeMemory(game.device, upload.memory, nullptr); // unmaps it + upload = {}; +} + +// The game writes `value(x, y)` into slice `layer` of `image`. The game's +// queue is idle first: the upload is free to fill again. +void WriteDepth(Game& game, VkImage image, DepthUpload& upload, std::uint32_t layer, + const std::function& value) { + GameIdle(game); + for (std::uint32_t y = 0; y < upload.height; ++y) { + for (std::uint32_t x = 0; x < upload.width; ++x) upload.values[y * upload.width + x] = value(x, y); + } + GameSubmit(game, [&](VkCommandBuffer commands) { + VkImageMemoryBarrier barrier = + GameBarrier(image, VK_IMAGE_ASPECT_DEPTH_BIT, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, + VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 0, 1, layer, 1); + game.vkCmdPipelineBarrier(commands, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, + 0, nullptr, 0, nullptr, 1, &barrier); + VkBufferImageCopy region{}; + region.imageSubresource = {VK_IMAGE_ASPECT_DEPTH_BIT, 0, layer, 1}; + region.imageExtent = {upload.width, upload.height, 1}; + game.vkCmdCopyBufferToImage(commands, upload.buffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion); + std::swap(barrier.oldLayout, barrier.newLayout); + game.vkCmdPipelineBarrier(commands, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, + 0, nullptr, 0, nullptr, 1, &barrier); + }); +} + +float ReadRuntimeDepth(ID3D12Device* device, std::uint32_t index, std::uint32_t slice = 0) { + const auto texel = ReadRuntimeImage(device, index, slice, RuntimeKind::kDepth); + float value = -1.0f; + if (texel.size() >= sizeof(float)) std::memcpy(&value, texel.data(), sizeof(float)); + return value; +} + +// The bridge's lines matching `part`, taken out of the log (CheckClean +// wants it empty). +std::vector TakeLog(const std::string& part) { + std::vector taken; + for (auto it = g_log.begin(); it != g_log.end();) { + if (Contains(*it, part)) { + taken.push_back(*it); + it = g_log.erase(it); + } else { + ++it; + } + } + return taken; +} + +// When the game's depth is in its image (VectorXR's LateDepth). Star Citizen +// (PTU and EPTU, October 2026) writes its depth after releasing the image, and +// every hand-over at the frame's end found it all zero. Each frame here: the +// depth swapchain's image is acquired, holds what the game wrote "in time" +// (before its release: zero, or this frame's gradient), is released (the +// hand-over, as at xrEndFrame), and then, in Star Citizen's order, gets this +// frame's gradient after the frame's end. The runtime's image is read after +// each hand-over: +// auto, Star Citizen's order: zero for the three frames the check needs +// (two checks in a row), then each frame the frame before's gradient; +// once the game writes in time again, the next frame has its own; +// auto, in time: every frame its own gradient, never taken late; +// end-frame: zero throughout (and said once); +// late: from the second frame on, the frame before's; +// private: the depth stays the game's (not mirrored, nothing copied). +// Images in turn (Virtual Desktop) and the same image every frame (Pimax), +// two slices. +void TestLateDepth(Game& game) { + constexpr std::uint32_t kWidth = 64; + constexpr std::uint32_t kHeight = 48; + constexpr int kFrames = 9; + // Frame `frame`'s depth in slice `slice`: varying over the image, its + // centre telling frames and slices apart. + const auto gradient = [](int frame, std::uint32_t slice) { + return [frame, slice](std::uint32_t x, std::uint32_t y) { + return 0.1f + 0.05f * static_cast(frame) + 0.02f * static_cast(slice) + + 1e-5f * static_cast(x + y * kWidth); + }; + }; + const auto centre = [&](int frame, std::uint32_t slice) { return gradient(frame, slice)(kWidth / 2, kHeight / 2); }; + _putenv_s("VECTORXR_VULKAN_BRIDGE_DEPTH_CHECK_MS", "0"); // a check at every hand-over + // The check runs only when wanted (Frame Generation finding the depth + // empty, WantDepthTimingCheck); here always, but for the case after. + _putenv_s("VECTORXR_VULKAN_BRIDGE_DEPTH_CHECK", "always"); + struct Case { + const char* name; + const char* timing; // VECTORXR_VULKAN_BRIDGE_DEPTH + bool late_writes; // Star Citizen's order + bool same_image; + bool writes{true}; // false: the game never writes its depth (all zero, Oct 2026's Star Citizen) + }; + const Case cases[] = { + {"auto, written after the frame's end, images in turn", "", true, false}, + {"auto, written after the frame's end, the same image", "auto", true, true}, + {"auto, written in time", "", false, false}, + {"end-frame, written after the frame's end", "end-frame", true, false}, + {"late, written after the frame's end", "late", true, true}, + {"late, written in time", "late", false, false}, + {"auto, never written (all zero)", "", true, false, false}, + }; + for (const Case& test : cases) { + _putenv_s("VECTORXR_VULKAN_BRIDGE_DEPTH", test.timing); + VulkanBridge bridge; + Session session(game); + BeginBridge(game, bridge, session); + ID3D12Device* device = BridgeDevice(bridge); + MakeRuntimeImages(device, 3, kWidth, kHeight, DXGI_FORMAT_D32_FLOAT, 2, RuntimeKind::kDepth); + const XrSwapchain swapchain = Handle(0x140); + std::string reason; + Check(bridge.AddSwapchain(swapchain, DepthInfo(kWidth, kHeight, VK_FORMAT_D32_SFLOAT, 2), FakeEnumerate, + reason), + std::string(test.name) + ": bridged: " + reason); + const std::vector images = GameImages(bridge, swapchain); + DepthUpload upload = MakeDepthUpload(game, kWidth, kHeight); + const bool late_mode = std::string(test.timing) == "late"; + int engaged_at = -1; // the first frame whose acquire took depth late + for (int frame = 0; frame < kFrames; ++frame) { + const std::uint32_t index = test.same_image ? 0 : static_cast(frame % 3); + const bool engaged_before = Contains(bridge.Describe(swapchain), "depthTaken=late"); + Acquire(bridge, swapchain, index); + for (std::uint32_t slice = 0; slice < 2; ++slice) { + if (test.late_writes) { + WriteDepth(game, images[index], upload, slice, [](std::uint32_t, std::uint32_t) { return 0.0f; }); + } else { + WriteDepth(game, images[index], upload, slice, gradient(frame, slice)); + } + } + GameIdle(game); // the check's reads are done: deterministic + Check(Release(bridge, swapchain), std::string(test.name) + ": copied"); + Check(bridge.WaitIdle(10000), "the bridge's queue finishes"); + // Taken late: the copy the acquire made of the frame before's. + const bool took_late = engaged_before && frame > 0; + if (took_late && engaged_at < 0) engaged_at = frame; + for (std::uint32_t slice = 0; slice < 2; ++slice) { + const float got = ReadRuntimeDepth(device, index, slice); + float expected = test.late_writes ? 0.0f : centre(frame, slice); + if (took_late) expected = test.late_writes ? centre(frame - 1, slice) : centre(frame - 1, slice); + Check(got == expected, std::string(test.name) + ", frame " + std::to_string(frame) + " slice " + + std::to_string(slice) + ": the runtime's depth is " + std::to_string(got) + + ", expected " + std::to_string(expected) + + (took_late ? " (the frame before's)" : "") + "; " + + bridge.Describe(swapchain)); + } + if (test.late_writes && test.writes) { + for (std::uint32_t slice = 0; slice < 2; ++slice) { + WriteDepth(game, images[index], upload, slice, gradient(frame, slice)); + } + } + } + const std::string described = bridge.Describe(swapchain); + const std::string mode(test.timing); + if (mode == "late") { + Check(engaged_at == 1, std::string(test.name) + ": taken late from the second frame on (" + + std::to_string(engaged_at) + "): " + described); + } else if (test.late_writes && test.writes && mode != "end-frame") { + // Two checks (hand-over and next acquire) in a row: decided at the + // third frame's hand-over, taken from the fourth's acquire. + Check(engaged_at == 3, std::string(test.name) + ": taken late from the fourth frame on (" + + std::to_string(engaged_at) + "): " + described); + Check(TakeLog("the game writes its depth after its frame's end").size() == 1, + std::string(test.name) + ": the decision is logged once"); + Check(TakeLog("holds").size() == 1, std::string(test.name) + ": the late copy's memory is logged"); + Check(Contains(described, "lateMiB="), std::string(test.name) + ": its memory is described: " + described); + } else { + Check(engaged_at < 0 && !Contains(described, "depthTaken=late"), + std::string(test.name) + ": never taken late: " + described); + } + if (mode == "end-frame") { + Check(TakeLog("VECTORXR_VULKAN_BRIDGE_DEPTH=end-frame keeps taking it").size() == 1, + std::string(test.name) + ": the depth it leaves is said once"); + } + if (mode == "late") { + Check(TakeLog("holds").size() == 1, std::string(test.name) + ": the late copy's memory is logged"); + } + TakeLog("varies only"); // the end-frame line's text holds it too + TakeLog("depth timing check"); // findings that changed to or from late only + if (!test.writes) { + Check(Contains(described, "depthChecks=0/0/"), std::string(test.name) + ": the checks found no depth: " + + described); + } + Check(Contains(described, test.late_writes ? "depthChecks=0/" : "/0/0 ("), + + std::string(test.name) + ": the checks found " + + (test.late_writes ? "no depth at the hand-over: " : "depth at the hand-over: ") + described); + FreeDepthUpload(game, upload); + Check(bridge.RemoveSwapchain(swapchain), std::string(test.name) + ": removed"); + CheckClean(&bridge, std::string("late depth: ") + test.name); + bridge.End(); + CheckClean(nullptr, std::string("late depth: ") + test.name + " (End)"); + } + + // The game fixed (CIG's fix): written late at first, then in time. Taken + // late from the fourth frame; the first frame written in time still gets + // the frame before's late copy (the acquire made it), its hand-over's + // check sees depth in time, and the next frame's hand-over (judging that + // check) takes it in time again. + _putenv_s("VECTORXR_VULKAN_BRIDGE_DEPTH", ""); + { + VulkanBridge bridge; + Session session(game); + BeginBridge(game, bridge, session); + ID3D12Device* device = BridgeDevice(bridge); + MakeRuntimeImages(device, 3, kWidth, kHeight, DXGI_FORMAT_D32_FLOAT, 1, RuntimeKind::kDepth); + const XrSwapchain swapchain = Handle(0x141); + std::string reason; + Check(bridge.AddSwapchain(swapchain, DepthInfo(kWidth, kHeight), FakeEnumerate, reason), "bridged: " + reason); + const std::vector images = GameImages(bridge, swapchain); + DepthUpload upload = MakeDepthUpload(game, kWidth, kHeight); + constexpr int kFixedFrom = 6; + for (int frame = 0; frame < 10; ++frame) { + const std::uint32_t index = static_cast(frame % 3); + Acquire(bridge, swapchain, index); + const bool in_time = frame >= kFixedFrom; + const std::function zero = [](std::uint32_t, std::uint32_t) { + return 0.0f; + }; + WriteDepth(game, images[index], upload, 0, in_time ? gradient(frame, 0) : zero); + GameIdle(game); + Check(Release(bridge, swapchain), "copied"); + Check(bridge.WaitIdle(10000), "the bridge's queue finishes"); + const float got = ReadRuntimeDepth(device, index); + const float expected = frame < 3 ? 0.0f + : frame <= kFixedFrom ? centre(frame - 1, 0) + : centre(frame, 0); + Check(got == expected, "fixed game, frame " + std::to_string(frame) + ": the runtime's depth is " + + std::to_string(got) + ", expected " + std::to_string(expected) + "; " + + bridge.Describe(swapchain)); + if (!in_time) WriteDepth(game, images[index], upload, 0, gradient(frame, 0)); + } + Check(TakeLog("the game writes its depth after its frame's end").size() == 1, "fixed game: taken late once"); + Check(TakeLog("is taken at its hand-over again").size() == 1 && + !Contains(bridge.Describe(swapchain), "depthTaken=late"), + "fixed game: taken in time again, logged: " + bridge.Describe(swapchain)); + TakeLog("holds"); + TakeLog("depth timing check"); + FreeDepthUpload(game, upload); + Check(bridge.RemoveSwapchain(swapchain), "removed"); + CheckClean(&bridge, "late depth: the game fixed"); + bridge.End(); + } + + // Not wanted (the default: Frame Generation has not found the depth + // empty): Star Citizen's order is never checked, the hand-over submits + // exactly the release barrier (the QFOT test's pattern), and nothing is + // taken late; once wanted, the check finds it and takes it late. + _putenv_s("VECTORXR_VULKAN_BRIDGE_DEPTH_CHECK", ""); + { + VulkanBridge bridge; + Session session(game); + BeginBridge(game, bridge, session); + ID3D12Device* device = BridgeDevice(bridge); + MakeRuntimeImages(device, 3, kWidth, kHeight, DXGI_FORMAT_D32_FLOAT, 1, RuntimeKind::kDepth); + const XrSwapchain swapchain = Handle(0x143); + std::string reason; + Check(bridge.AddSwapchain(swapchain, DepthInfo(kWidth, kHeight), FakeEnumerate, reason), "bridged: " + reason); + const std::vector images = GameImages(bridge, swapchain); + DepthUpload upload = MakeDepthUpload(game, kWidth, kHeight); + const std::function zero = [](std::uint32_t, std::uint32_t) { + return 0.0f; + }; + int late_from = -1; + for (int frame = 0; frame < 12; ++frame) { + if (frame == 6) bridge.WantDepthTimingCheck(true); + const std::uint32_t index = static_cast(frame % 3); + const std::uint64_t before_acquire = SummaryValue(bridge.Summary(), "submits"); + Acquire(bridge, swapchain, index); + const std::uint64_t submits = SummaryValue(bridge.Summary(), "submits"); + if (frame >= 3 && frame < 6) { + Check(submits == before_acquire, "not wanted, frame " + std::to_string(frame) + + ": nothing at the acquire (handed back at the release)"); + } + WriteDepth(game, images[index], upload, 0, zero); + GameIdle(game); + g_recorder.Take(); + Check(Release(bridge, swapchain), "not wanted: copied"); + const std::vector released = g_recorder.Take(); + if (frame < 6) { + // As before the check: one submission of the release barrier + // alone (and the hand-back), nothing at the acquire. + Check(SummaryValue(bridge.Summary(), "submits") == submits + 2 && !released.empty() && + released[0].commands.size() == 1, + "not wanted, frame " + std::to_string(frame) + ": the hand-over submits as before: " + + bridge.Summary()); + } + Check(bridge.WaitIdle(10000), "the bridge's queue finishes"); + const float got = ReadRuntimeDepth(device, index); + const bool late = Contains(bridge.Describe(swapchain), "lateUsed=0") == false; + if (late && late_from < 0) late_from = frame; + Check(got == (late ? centre(frame - 1, 0) : 0.0f), + "not wanted, frame " + std::to_string(frame) + ": the runtime's depth is " + std::to_string(got) + + "; " + bridge.Describe(swapchain)); + WriteDepth(game, images[index], upload, 0, gradient(frame, 0)); + } + Check(late_from == 9, "not wanted until frame 6, then taken late from frame 9 (" + std::to_string(late_from) + + "): " + bridge.Describe(swapchain)); + Check(TakeLog("finds the game's depth the same all over").size() == 1, "the wanted check is logged once"); + TakeLog("the game writes its depth after its frame's end"); + TakeLog("holds"); + TakeLog("depth timing check"); + FreeDepthUpload(game, upload); + Check(bridge.RemoveSwapchain(swapchain), "removed"); + CheckClean(&bridge, "late depth: not wanted, then wanted"); + bridge.End(); + } + + // private: the switch keeps depth the game's. + _putenv_s("VECTORXR_VULKAN_BRIDGE_DEPTH", "private"); + { + VulkanBridge bridge; + Session session(game); + BeginBridge(game, bridge, session); + MakeRuntimeImages(BridgeDevice(bridge), 2, 32, 32, DXGI_FORMAT_D32_FLOAT, 1, RuntimeKind::kDepth); + const XrSwapchain swapchain = Handle(0x142); + std::string reason; + Check(bridge.AddSwapchain(swapchain, DepthInfo(32, 32), FakeEnumerate, reason), "bridged: " + reason); + Check(bridge.Unmirrored(swapchain) && + Contains(bridge.Describe(swapchain), "private because VECTORXR_VULKAN_BRIDGE_DEPTH=private"), + "VECTORXR_VULKAN_BRIDGE_DEPTH=private keeps depth private: " + bridge.Describe(swapchain)); + const VkImage image = GameImages(bridge, swapchain)[0]; + for (int frame = 0; frame < 2; ++frame) { + Acquire(bridge, swapchain, 0); + ClearDepth(game, image, VK_IMAGE_ASPECT_DEPTH_BIT, 0.75f, 0); + Release(bridge, swapchain); + } + Check(SummaryValue(bridge.Summary(), "copies") == 0 && Contains(bridge.Summary(), "depth=private"), + "nothing is copied: " + bridge.Summary()); + GameIdle(game); + Check(bridge.RemoveSwapchain(swapchain), "removed"); + CheckClean(&bridge, "late depth: private"); + bridge.End(); + } + _putenv_s("VECTORXR_VULKAN_BRIDGE_DEPTH", ""); + _putenv_s("VECTORXR_VULKAN_BRIDGE_DEPTH_CHECK_MS", ""); + CheckClean(nullptr, "late depth (End)"); +} + +// Frame Generation's own swapchains' images (its Track's enumerations), by swapchain. +std::unordered_map> g_framegen_images; + +XrResult XRAPI_CALL FrameGenEnumerate(XrSwapchain swapchain, uint32_t capacity, uint32_t* count, + XrSwapchainImageBaseHeader* images) { + const std::vector& list = g_framegen_images[swapchain]; + *count = static_cast(list.size()); + if (capacity == 0) return XR_SUCCESS; + if (capacity < list.size()) return XR_ERROR_SIZE_INSUFFICIENT; + auto* d3d12_images = reinterpret_cast(images); + for (std::size_t i = 0; i < list.size(); ++i) d3d12_images[i].texture = list[i]; + return XR_SUCCESS; +} + +// The raw depth `encoding` stores for `meters` (1/z linear from minDepth at +// nearZ to maxDepth at farZ, either of them infinitely far). +float RawDepth(const depthxr::FrameGenDepthEncoding& encoding, double meters) { + const auto inverse = [](double z) { return std::isfinite(z) && z > 0.0 ? 1.0 / z : 0.0; }; + const double near_inverse = inverse(encoding.near_z); + const double normalized = (1.0 / meters - near_inverse) / (inverse(encoding.far_z) - near_inverse); + return static_cast(encoding.min_depth + normalized * (encoding.max_depth - encoding.min_depth)); +} + +// Frame Generation reads a bridged Vulkan game's depth: the game writes its +// depth (Star Citizen's description: D32, reversed, its near plane 2 cm and +// its far one 1e12 m away), the bridge mirrors it into the runtime's image at +// the hand-over, and Frame Generation's Direct3D 12 backend (on the bridge's +// device and queue, as in a session) captures it there after the copy and +// decodes it. Its depth check is locked to the depth as described: what it +// decides is the backend's own tests', not the bridge's. +// - written in time: one plane, 3 m away; +// - Star Citizen's order (zero at the hand-over, written after the frame's +// end): the top half 2 m away and the bottom half 8 m; the bridge takes +// it late (auto), and Frame Generation decodes the frame before's. +void TestDepthReachesFrameGen(Game& game) { + depthxr::FrameGenDepthEncoding sc; // Star Citizen's, from its logs (Oct 2026) + sc.near_z = 1e12f; + sc.far_z = 0.02f; + constexpr std::uint32_t kSize = 64; + _putenv_s("VECTORXR_VULKAN_BRIDGE_DEPTH_CHECK_MS", "0"); + _putenv_s("VECTORXR_VULKAN_BRIDGE_DEPTH_CHECK", "always"); + for (const bool late : {false, true}) { + const std::string name = late ? "Star Citizen's order (written after the frame's end)" : "written in time"; + VulkanBridge bridge; + Session session(game); + BeginBridge(game, bridge, session); + ID3D12Device* device = BridgeDevice(bridge); + std::string error; + auto gpu = depthxr::CreateFrameGenGpu(bridge.Binding(), error); + Check(gpu != nullptr && gpu->SupportsDepth(), name + ": Frame Generation's backend: " + error); + gpu->LockDepthForTest(depthxr::FrameGenDepthReading::kAsSubmitted, false, 1.0f); + + // The runtime's depth swapchain (the bridge's), a colour source and + // Frame Generation's output. + MakeRuntimeImages(device, 3, kSize, kSize, DXGI_FORMAT_D32_FLOAT, 1, RuntimeKind::kDepth); + const XrSwapchain depth = Handle(0x150); + std::string reason; + Check(bridge.AddSwapchain(depth, DepthInfo(kSize, kSize), FakeEnumerate, reason), + name + ": bridged: " + reason); + const std::vector> runtime_depth = g_runtime_images; + MakeRuntimeImages(device, 1, kSize, kSize, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB); + const std::vector> source = g_runtime_images; + const std::int64_t output_format = gpu->OutputFormats(depthxr::FrameGenOutputKind::kSrgb8).front(); + MakeRuntimeImages(device, 1, kSize, kSize, static_cast(output_format)); + const std::vector> output = g_runtime_images; + const XrSwapchain source_chain = Handle(0x151); + const XrSwapchain output_chain = Handle(0x152); + g_framegen_images = {{depth, {runtime_depth[0].Get(), runtime_depth[1].Get(), runtime_depth[2].Get()}}, + {source_chain, {source[0].Get()}}, + {output_chain, {output[0].Get()}}}; + for (const XrSwapchain chain : {depth, source_chain, output_chain}) { + Check(gpu->Track(chain, FrameGenEnumerate, error), name + ": track: " + error); + } + const std::vector images = GameImages(bridge, depth); + DepthUpload upload = MakeDepthUpload(game, kSize, kSize); + + const XrRect2Di rect{{0, 0}, {static_cast(kSize), static_cast(kSize)}}; + depthxr::FrameGenViewSubmission view; + view.source = {source_chain, 0, 0, rect, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB}; + view.outputs = {{output_chain, 0, 0, rect, output_format}}; + view.request.extent = {kSize, kSize}; + view.request.output = depthxr::FrameGenOutputKind::kSrgb8; + for (auto* geometry : {&view.request.newest, &view.request.previous}) { + geometry->angle_left = geometry->angle_down = -std::atan(1.0f); + geometry->angle_right = geometry->angle_up = std::atan(1.0f); + } + depthxr::FrameGenTuning tuning; + depthxr::FrameGenExecuteResult result; + // A first frame sets the view up: nothing was captured for it. + Check(!gpu->Execute({view}, tuning, result, error), name + ": the first frame only sets the view up"); + const double top = late ? 2.0 : 3.0; + const double bottom = late ? 8.0 : 3.0; + const float raw_top = RawDepth(sc, top); + const float raw_bottom = RawDepth(sc, bottom); + const auto planes = [&](std::uint32_t, std::uint32_t y) { return y < kSize / 2 ? raw_top : raw_bottom; }; + view.request.depth = true; + view.request.synthesize = true; + const int frames = late ? 6 : 3; + for (int frame = 0; frame < frames; ++frame) { + Check(gpu->Capture(0, view.source, view.request, tuning.quality, error), + name + ": colour captured at release: " + error); + const std::uint32_t index = static_cast(frame % 3); + Acquire(bridge, depth, index); + if (late) { + WriteDepth(game, images[index], upload, 0, [](std::uint32_t, std::uint32_t) { return 0.0f; }); + } else { + WriteDepth(game, images[index], upload, 0, planes); + } + GameIdle(game); + Check(Release(bridge, depth), name + ": the release is copied"); + Check(gpu->CaptureDepth(0, {depth, index, 0, rect, DXGI_FORMAT_D32_FLOAT}, sc, view.request, error), + name + ": Frame Generation captures the runtime's depth: " + error); + Check(gpu->Execute({view}, tuning, result, error), name + ": execute: " + error); + if (late) WriteDepth(game, images[index], upload, 0, planes); + } + Check(result.depth.size() == 1 && result.depth[0], name + ": the frame used the depth"); + if (late) { + Check(Contains(bridge.Describe(depth), "depthTaken=late"), + name + ": taken late: " + bridge.Describe(depth)); + + } + std::vector linear; + depthxr::FrameGenExtent extent{}; + std::uint32_t channels = 0; + Check(gpu->ReadTexture(0, {depthxr::FrameGenTextureKind::kDepth, 0}, linear, extent, channels) && + channels == 1 && !linear.empty() && extent.width * extent.height == linear.size(), + name + ": Frame Generation's linear depth read back"); + double worst = 0.0; + for (std::uint32_t y = 0; y < extent.height; ++y) { + // Rows by the boundary between the planes may blend them. + const double at = (y + 0.5) * kSize / extent.height; + if (std::abs(at - kSize / 2.0) < 2.0) continue; + const double meters = at < kSize / 2.0 ? top : bottom; + for (std::uint32_t x = 0; x < extent.width; ++x) { + worst = std::max(worst, std::abs(linear[y * extent.width + x] - meters) / meters); + } + } + std::cout << "depth to Frame Generation, " << name << ": raw " << raw_top << "/" << raw_bottom + << " decoded to " << linear[extent.width / 2] << "/" << linear[linear.size() - extent.width / 2] + << " m (planes " << top << "/" << bottom << " m, worst " << worst * 100.0 << "% off over " + << extent.width << "x" << extent.height << ")\n"; + Check(worst <= 1e-3, name + ": Frame Generation decodes the bridged depth to " + + std::to_string(linear[extent.width / 2]) + "/" + + std::to_string(linear[linear.size() - extent.width / 2]) + " m, the planes are " + + std::to_string(top) + "/" + std::to_string(bottom) + " m"); + + Check(gpu->WaitIdle(), name + ": Frame Generation's work finishes"); + for (const XrSwapchain chain : {depth, source_chain, output_chain}) gpu->Forget(chain); + gpu.reset(); + FreeDepthUpload(game, upload); + Check(bridge.WaitIdle(10000), name + ": the bridge's queue finishes"); + TakeLog("the game writes its depth after its frame's end"); + TakeLog("holds"); + TakeLog("depth timing check"); + CheckClean(&bridge, "depth to Frame Generation, " + name); + Check(bridge.RemoveSwapchain(depth), name + ": removed"); + bridge.End(); + } + _putenv_s("VECTORXR_VULKAN_BRIDGE_DEPTH_CHECK_MS", ""); + _putenv_s("VECTORXR_VULKAN_BRIDGE_DEPTH_CHECK", ""); + g_framegen_images.clear(); + CheckClean(nullptr, "depth to Frame Generation (End)"); +} + +// BRIDGE_TEST_DEPTH_PERF=1: what mirroring a Vulkan game's depth costs at +// games' sizes, per frame: the hand-over (the release barrier, the copy into +// the runtime's image on the bridge's queue, the hand-back), from just before +// it to the runtime's image written (wall time, the bridge's queue idle +// after), its copy's GPU time (the bridge's timestamps) and its CPU time; and, +// taken late (VECTORXR_VULKAN_BRIDGE_DEPTH=late), the acquire's extra copy, +// from just before the acquire to the game's queue having the image back. +// Each frame finishes before the next; then 30 frames back to back. 90 +// frames warm the GPU's clocks first. +void MeasureDepthCost(Game& game) { + struct Size { + const char* name; + std::uint32_t width, height, slices; + }; + const Size sizes[] = { + {"Star Citizen (D32 5424x5356, a swapchain an eye)", 5424, 5356, 1}, + {"two slices (D32 2x2560x2560)", 2560, 2560, 2}, + }; + const auto median = [](std::vector values) { + if (values.empty()) return 0.0; + std::sort(values.begin(), values.end()); + return values[values.size() / 2]; + }; + const auto maximum = [](const std::vector& values) { + return values.empty() ? 0.0 : *std::max_element(values.begin(), values.end()); + }; + for (const Size& size : sizes) { + for (const char* timing : {"end-frame", "late"}) { + _putenv_s("VECTORXR_VULKAN_BRIDGE_DEPTH", timing); + VulkanBridge bridge; + Session session(game); + BeginBridge(game, bridge, session); + ID3D12Device* device = BridgeDevice(bridge); + MakeRuntimeImages(device, 3, size.width, size.height, DXGI_FORMAT_D32_FLOAT, size.slices, + RuntimeKind::kDepth); + const XrSwapchain swapchain = Handle(0x160); + std::string reason; + Check(bridge.AddSwapchain(swapchain, DepthInfo(size.width, size.height, VK_FORMAT_D32_SFLOAT, size.slices), + FakeEnumerate, reason), + std::string(size.name) + ": bridged: " + reason); + const std::vector images = GameImages(bridge, swapchain); + std::vector handover_ms, acquire_ms; + using Clock = std::chrono::steady_clock; + const auto ms = [](Clock::time_point a, Clock::time_point b) { + return std::chrono::duration(b - a).count(); + }; + for (int frame = 0; frame < 130; ++frame) { + const std::uint32_t index = static_cast(frame) % 3; + Check(bridge.WaitIdle(10000), "idle"); + GameIdle(game); + const auto acquire_start = Clock::now(); + Acquire(bridge, swapchain, index); + GameIdle(game); // the image is the game's again (late: behind the acquire's copy) + const auto acquired = Clock::now(); + for (std::uint32_t slice = 0; slice < size.slices; ++slice) { + ClearDepth(game, images[index], VK_IMAGE_ASPECT_DEPTH_BIT, 0.25f + 0.001f * frame, 0, slice); + } + GameIdle(game); + const auto start = Clock::now(); + Check(Release(bridge, swapchain), std::string(size.name) + ": copied"); + Check(bridge.WaitIdle(10000), std::string(size.name) + ": the bridge's queue finishes"); + const auto end = Clock::now(); + if (frame >= 90) { + handover_ms.push_back(ms(start, end)); + acquire_ms.push_back(ms(acquire_start, acquired)); + } + } + const std::string single = bridge.Summary(); // its samples so far: the single frames + const auto copy_single = SummaryPair(single, "copyGpuUs p50/p99"); + const auto cpu_single = SummaryPair(single, "handoverUs p50/max"); + const auto back_start = Clock::now(); + for (int frame = 0; frame < 30; ++frame) { + const std::uint32_t index = static_cast(frame) % 3; + Acquire(bridge, swapchain, index); + ClearDepth(game, images[index], VK_IMAGE_ASPECT_DEPTH_BIT, 0.5f, 0); + Release(bridge, swapchain); + } + Check(bridge.WaitIdle(10000), "the back-to-back frames finish"); + GameIdle(game); + const double back_ms = ms(back_start, Clock::now()) / 30.0; + const double mib = static_cast(size.width) * size.height * size.slices * 4.0 / (1024.0 * 1024.0); + std::printf("depth cost, %s, %s: hand-over to the runtime's image written %.3f / %.3f ms (median / max), " + "acquire to the image the game's again %.3f / %.3f ms, copy GPU p50/p99 %llu/%llu us, " + "hand-over CPU p50/max %llu/%llu us, back to back %.3f ms a frame; %.0f MiB an image\n", + size.name, timing, median(handover_ms), maximum(handover_ms), median(acquire_ms), + maximum(acquire_ms), static_cast(copy_single.first), + static_cast(copy_single.second), + static_cast(cpu_single.first), + static_cast(cpu_single.second), back_ms, mib); + TakeLog("holds"); + Check(bridge.RemoveSwapchain(swapchain), "removed"); + CheckClean(&bridge, std::string("depth cost ") + size.name); + bridge.End(); + } + } + _putenv_s("VECTORXR_VULKAN_BRIDGE_DEPTH", ""); + g_runtime_images.clear(); +} + // The images get their attachment, transfer and sampled usage whatever the // application asked (OpenXR hands them out in the attachment layout). @@ -3786,7 +4423,13 @@ int main(int argc, char** argv) { TestResolve(game); TestArray(game); TestDepthValues(game); + TestLateDepth(game); + TestDepthReachesFrameGen(game); + if (const char* perf = std::getenv("BRIDGE_TEST_DEPTH_PERF"); perf && std::string(perf) == "1") { + MeasureDepthCost(game); + } TestUsage(game); + TestMismatch(game); TestPrivateDepth(game); TestUnmatched(game); diff --git a/release/notes/v0.20.1.md b/release/notes/v0.20.1.md new file mode 100644 index 00000000..0131c1ec --- /dev/null +++ b/release/notes/v0.20.1.md @@ -0,0 +1,44 @@ +# VectorXR 0.20.1 - Depth for Direct3D 11 Games, PimaxXR Fixes, and Duplicate Pivot Profiles + +A fix release for 0.20.0. Frame Generation can now use depth from Direct3D 11 games, including UEVR titles. Frame Generation no longer stalls games on PimaxXR. Turbo's Auto mode gives async pacing a fairer trial, and eye-tracked foveation is placed better on Pimax. VectorXR works beside OpenXR Toolkit and Quad-Views-Foveated in more setups, and custom Pivot profiles can now be duplicated. + +## Frame Generation + +- **Depth for Direct3D 11 games.** Frame Generation can now use the depth that Direct3D 11 games provide, including UEVR titles, for steadier generated frames. +- **Use game depth option.** If a game stutters, hangs or shows depth artifacts with Frame Generation, turn off **Use game depth** in the profile's advanced settings and restart the game. Frame Generation then works without depth. +- **No more stalls on PimaxXR.** With Frame Generation engaged, PimaxXR could hold up the game's own rendering, and the game's frame rate dropped sharply. Frame Generation now works around it. +- **Smoother output on PimaxXR 0.4.4 and earlier,** which could skip a refresh when no new frame was ready. +- **Frame Generation stays off when OpenXR Toolkit is below VectorXR,** because Toolkit can't handle the extra frames there. The OSD and the OpenXR Layers page now say when this applies; move OpenXR Toolkit above VectorXR there to use both. +- **OpenXR Toolkit's prediction dampening is respected** instead of being undone by Frame Generation. +- **No rejected frames from misplaced depth.** Some games, such as UEVR titles, can describe depth that doesn't fit its image, and PimaxXR and Virtual Desktop then reject the whole frame. VectorXR now leaves that depth out. + +## Turbo + +- **Auto gives async pacing a fair trial.** A stutter right as a game left its loading screen could make Auto settle on sequenced pacing for good. Auto now rides out early stutters. It also tries async again after a quick failure, a VectorXR or runtime update, or 30 days. +- **More accurate frame timing with sequenced pacing** in games that prepare frames ahead, such as DCS. +- **Turbo stands aside when OpenXR Toolkit's Turbo runs above VectorXR,** instead of both pacing frames. + +## Quadviews and eye tracking + +- **Better-placed focus areas on Pimax.** On headsets with angled displays, VectorXR's emulated quad views placed each eye's sharp area off to the side. Both eyes now line up on where you're looking. +- **A frozen eye tracker no longer pins the focus area.** If the tracker stops updating, the focus area follows your head until the tracker recovers. +- **Eye-tracked foveation is no longer switched off** in PimaxXR's native quad views. +- **DCS's focus views stay aligned** on PimaxXR's native quad views. + +## Pivot + +- **Duplicate custom profiles.** A new Duplicate button next to Remove copies a profile with all its settings and bindings, places the copy just below the original, and scrolls to it. Change the copy's applications or bindings so the two profiles don't overlap. +- **Pivot stays centred after a recenter on Pimax.** Recentering in Pimax Play during a session could leave Pivot turning around your old head position. +- **A restarted VR session starts Pivot fresh,** instead of picking up the previous session's origin. + +## Compatibility + +- **Games start with OpenXR Toolkit or Quad-Views-Foveated on VIVE runtimes.** +- **Stability fixes for PimaxXR** around starting VR again in the same game, and around head tracking. + +## Known issues + +- In helicopters, Interpolate can show artifacts around the rotors and in the sky behind them. +- On Varjo headsets with Quadviews, focus sharpening is off in DCS while Frame Generation runs. +- On Varjo headsets with Quadviews, overlays such as OpenKneeboard are drawn at the Peripheral resolution outside the focus area, and can look jagged there at low Peripheral settings. Raise Peripheral (1.0 matches Varjo's default) if overlays matter more than the performance gain. +- Turbo prevents runtime reprojection such as SteamVR Motion Smoothing from working. Use one or the other. diff --git a/scripts/Build-And-Install-Layer.ps1 b/scripts/Build-And-Install-Layer.ps1 index 37f27b8f..5c0533a5 100644 --- a/scripts/Build-And-Install-Layer.ps1 +++ b/scripts/Build-And-Install-Layer.ps1 @@ -6,6 +6,9 @@ param( [switch]$SkipTests, [switch]$SkipFresh, [switch]$CleanScratch, + # Tests run side by side (Build-Layer.ps1's -TestJobs; 0: its default). + [ValidateRange(0, 64)] + [int]$TestJobs = 0, # Signs the built layer locally and installs that signed copy instead # (Sign-LocalLayer.ps1): for testing in games with anti-cheat only. [switch]$Sign, @@ -35,6 +38,10 @@ if ($CleanScratch) { $buildArgs.CleanScratch = $true } +if ($TestJobs -gt 0) { + $buildArgs.TestJobs = $TestJobs +} + & $buildScript @buildArgs if ($Sign) { & (Join-Path $PSScriptRoot "Sign-LocalLayer.ps1") -BuildDir $BuildDir -Configuration $Configuration -Install -Note $SignNote diff --git a/scripts/Build-Layer.ps1 b/scripts/Build-Layer.ps1 index 7a96f0bb..07496ec2 100644 --- a/scripts/Build-Layer.ps1 +++ b/scripts/Build-Layer.ps1 @@ -5,7 +5,17 @@ param( [string]$Configuration = "Release", [switch]$SkipTests, [switch]$SkipFresh, - [switch]$CleanScratch + [switch]$CleanScratch, + # Tests CTest runs side by side; a test that touches a graphics device still runs alone (RUN_SERIAL in + # layer/CMakeLists.txt). The default, three quarters of the logical processors (up to 8), leaves the presenter + # tests' wall-clock pacing room: 3 on a 4-processor CI runner (on four of this desktop's processors 3 passed + # and 4 made the presenter tests late). 1 runs them one after another. + [ValidateRange(1, 64)] + [int]$TestJobs = [Math]::Max(1, [Math]::Min([Math]::Floor([Environment]::ProcessorCount * 3 / 4), 8)), + # Which tests run: All, only those that create no graphics device (Cpu), or only those that do (Graphics, + # which run one at a time). CI runs Cpu and Graphics on separate runners at once. + [ValidateSet("All", "Cpu", "Graphics")] + [string]$Tests = "All" ) Set-StrictMode -Version Latest @@ -153,13 +163,19 @@ if ($LASTEXITCODE -ne 0) { throw "CMake configure failed (exit code $LASTEXITCODE)." } -& $cmake --build $buildPath --config $Configuration +# Projects build side by side (MSBuild /m); one at a time took most of CI's build time. +& $cmake --build $buildPath --config $Configuration --parallel if ($LASTEXITCODE -ne 0) { throw "CMake build failed (exit code $LASTEXITCODE)." } if (-not $SkipTests) { - & $ctest --test-dir $buildPath -C $Configuration --output-on-failure + $testFilter = switch ($Tests) { + "Cpu" { @("-LE", "graphics") } + "Graphics" { @("-L", "graphics") } + default { @() } + } + & $ctest --test-dir $buildPath -C $Configuration --output-on-failure --parallel $TestJobs @testFilter if ($LASTEXITCODE -ne 0) { throw "Layer tests failed (exit code $LASTEXITCODE)." }