Skip to content

fix(linux): allow NvFBC direct capture to attach after startup - #6199

Open
medialaxis wants to merge 3 commits into
SunshineStream:masterfrom
medialaxis:fix/linux-nvfbc-direct-capture
Open

medialaxis wants to merge 3 commits into
SunshineStream:masterfrom
medialaxis:fix/linux-nvfbc-direct-capture

Conversation

@medialaxis

Copy link
Copy Markdown

Description

Remove the 3-frame NVFBC_TOCUDA_GRAB_FLAGS_NOWAIT startup probe in cuda::nvfbc::display_t::reinit() that disabled bAllowDirectCapture and bPushModel (and recreated the capture session) whenever grab_info.bDirectCapture was not immediately true.

That probe prevented NvFBC direct capture in two common cases when cursor capture is disabled:

  • Fullscreen application already running: Three consecutive NOWAIT grabs can execute within the same millisecond—returning one initial non-direct frame followed by two No new frame results before the application presents—triggering an immediate fallback to non-direct desktop capture.
  • Fullscreen application launched after stream start (e.g. connecting to Desktop, then launching a game): Direct capture is not active on the desktop at session startup, so the probe disables bAllowDirectCapture for the rest of the session and NvFBC can never attach once a fullscreen application starts.

Per third-party/nvfbc/NvFBC.h, when bAllowDirectCapture and bPushModel remain enabled (with bWithCursor = NVFBC_FALSE), the NVIDIA X driver automatically attaches to a qualifying fullscreen unoccluded application and transparently resumes X-driver desktop frame generation when it detaches.

Trade-off: when cursor capture is disabled and direct capture is not attached, bPushModel stays NVFBC_TRUE (a prerequisite for bAllowDirectCapture), so the NVIDIA X driver generates desktop frames on X damage events rather than at dwSamplingRateMs. Sunshine's capture/encode loop remains paced at the configured stream framerate, and the default path (cursor == true) is unchanged.

Screenshot

Issues Fixed or Closed

Roadmap Issues

Testing

  • Why no automated regression test was added: cuda::nvfbc::display_t::reinit() is thin NvFBC driver glue and the change only deletes the startup probe; verifying automatic bDirectCapture attach/detach transitions requires a real NVIDIA X11 driver and fullscreen application. Happy to extract a testable ops seam for reinit() if preferred.
  • Manual validation (Linux X11, NVIDIA NvFBC, Moonlight, cursor capture disabled):
    • With temporary diagnostic logging on grab_info.bDirectCapture (removed from this PR), confirmed automatic transitions No (desktop) → Yes (fullscreen frame_lab) → No (windowed) repeating across multiple fullscreen toggles without reconnecting or toggling cursor capture (__GL_SHOW_GRAPHICS_OSD=1 also switched BLIT ↔ FLIP).
    • Verified the simplified patch without diagnostic logging in a live stream.
  • Formatting checks:
    • uv run --locked --no-sync lb-update-clang-format — no changes.
    • clang-format --dry-run --Werror src/platform/linux/cuda.cpp — passed.
  • Checks not run:
    • Live hardware validation was performed on an earlier local build before rebasing onto current master; test_sunshine was not re-run locally after the rebase.
    • Not tested across multiple X11 compositors.

Type of Change

  • feat: New feature (non-breaking change which adds functionality)
  • fix: Bug fix (non-breaking change which fixes an issue)
  • docs: Documentation only changes
  • style: Changes that do not affect the meaning of the code (white-space, formatting, missing semicolons, etc.)
  • refactor: Code change that neither fixes a bug nor adds a feature
  • perf: Code change that improves performance
  • test: Adding missing tests or correcting existing tests
  • build: Changes that affect the build system or external dependencies
  • ci: Changes to CI configuration files and scripts
  • chore: Other changes that don't modify src or test files
  • revert: Reverts a previous commit
  • BREAKING CHANGE: Introduces a breaking change (can be combined with any type above)

Checklist

  • Code follows the style guidelines of this project
  • Code has been self-reviewed
  • Code has been commented, particularly in hard-to-understand areas
  • Code docstring/documentation-blocks for new or existing methods/components have been added or updated
  • Tests have been added or updated for new or modified functionality, including regression tests for bug fixes

AI Usage

See our AI usage policy.

  • AI tools were used while preparing this PR
  • Codex (GPT-6 Sol) assisted with investigating the NvFBC direct-capture behavior and implementing the fix.
  • Antigravity (Gemini 4 Neon, high thinking level) assisted with reviewing correctness/trade-offs, cleaning up the commit, and drafting this PR description.

This pull request was generated with Codex (GPT-6 Sol) for investigation/implementation and Antigravity (Gemini 4 Neon, high thinking level) for review and PR drafting

@medialaxis

Copy link
Copy Markdown
Author

This pull request was generated with Codex (GPT-6 Sol) for investigation/implementation and Antigravity (Gemini 4 Neon, high thinking level) for review and PR drafting

@medialaxis
medialaxis force-pushed the fix/linux-nvfbc-direct-capture branch 2 times, most recently from 4d4eadd to 9ca55fd Compare December 4, 2026 22:07
Remove the startup probe in cuda::nvfbc::display_t::reinit() that ran three immediate NOWAIT grabs and disabled bAllowDirectCapture and bPushModel whenever bDirectCapture was false.

That probe failed both when no fullscreen application was active at session startup and when consecutive NOWAIT grabs completed before a new frame arrived, permanently preventing direct capture for the rest of the session. Per NvFBC.h, NvFBC automatically attaches and detaches from suitable fullscreen applications while direct capture remains enabled and falls back to X driver desktop frames in between.
@medialaxis
medialaxis force-pushed the fix/linux-nvfbc-direct-capture branch from 9ca55fd to 689e13b Compare December 5, 2026 17:25

@ReenigneArcher ReenigneArcher left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The change preserves late attachment by keeping direct capture enabled for the cursor-free session. A temporary fake-driver harness reproduced the startup fallback on master and passed on this head. Please add the regression test and update the session documentation.

The patch applies cleanly to current master and ClangFormat passed. GitHub marked CI, common lint, and CodeQL action_required before any jobs ran. I could verify Sunshine’s startup behavior with the fake driver. Live NVIDIA X11 attachment remains covered by the manual test described in the PR.

Review completed by Codex | Model: gpt-6.2-sol | Thinking: xhigh

}
}

return platf::capture_e::ok;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[P2] Cover cursor-free NvFBC startup with a regression test

The behavior this patch changes is under Sunshine’s control. When the first desktop frames are indirect, Sunshine must create one cursor-free session, keep direct capture and push mode enabled, and leave the first grab to the capture loop. The hardware run checks that the driver attaches later. A fake function table can catch Sunshine recreating the session and disabling direct capture before that happens.

In tests/unit/platform/linux/test_cuda.cpp, add this case under SUNSHINE_BUILD_CUDA. Wire fake_nvfbc_driver_t::reinit() to the real display_t::reinit() through a small test entry point in cuda.cpp that accepts an NvFBC function table. The fake should record nvFBCCreateCaptureSession parameters, return success from setup and teardown, and return a successful grab with the configured bDirectCapture value.

TEST(NvFbcCaptureSessionTest, KeepsDirectCaptureEnabledWhenStartingOnDesktop) {
  fake_nvfbc_driver_t driver;
  driver.direct_capture = NVFBC_FALSE;

  ASSERT_EQ(driver.reinit(false), platf::capture_e::ok);
  ASSERT_EQ(driver.sessions.size(), 1U);
  EXPECT_EQ(driver.sessions.front().bAllowDirectCapture, NVFBC_TRUE);
  EXPECT_EQ(driver.sessions.front().bPushModel, NVFBC_TRUE);
  EXPECT_EQ(driver.sessions.front().bWithCursor, NVFBC_FALSE);
  EXPECT_EQ(driver.grab_calls, 0U);
}

Run ./cmake-build-linux/tests/test_sunshine --gtest_filter='NvFbcCaptureSessionTest.*' from the repo root with a CUDA-enabled Linux build in cmake-build-linux. I verified the startup assertions in a temporary C++ harness around the exact method body. master made three grabs and created two sessions, while this head made zero grabs and created one session. The proposed GoogleTest fixture still needs to be integrated and run.

Review completed by Codex | Model: gpt-6.2-sol | Thinking: xhigh

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added cuda::test::reinit_nvfbc_display() and NvFbcCaptureSessionTest.KeepsDirectCaptureEnabledWhenStartingOnDesktop in 241c4e3. Verified locally with a CUDA-enabled Linux build (./cmake-build-linux/tests/test_sunshine --gtest_filter=NvFbcCaptureSessionTest.*, 1/1 passed).

}

return platf::capture_e::ok;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[P3] Document why the session keeps direct capture enabled

With the probe gone, cursor-free sessions deliberately keep push mode enabled even when the desktop is supplying indirect frames. Update the existing Doxygen block immediately above display_t::reinit() so a future change to this setup preserves late attachment and accounts for the driver’s desktop frame cadence.

      /**
       * @brief Create the NvFBC session for the current cursor visibility.
       *
       * With the cursor hidden, keep push mode and direct capture enabled so NvFBC
       * can attach to a fullscreen application after startup. The driver supplies
       * desktop frames on damage events while direct capture is inactive.
       *
       * @param cursor Whether to composite the mouse cursor into captured frames.
       * @return Capture status for session creation.
       */

Review completed by Codex | Model: gpt-6.2-sol | Thinking: xhigh

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Updated the display_t::reinit() Doxygen block in 9b8dd21.

@sonarqubecloud

sonarqubecloud Bot commented Dec 5, 2026

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants