Damage is now gathered from every output that presented this cycle rather than reporting the whole frame. An output that did not present contributes nothing, so a static monitor costs no transfer at all — which matters when the atlas is tens of megabytes and only one display is moving. The translation clips each rectangle to the display it came from, so one output's damage can never reach into another's part of the atlas, and scaling is applied after placement since the downscale applies to the frame as a whole. If the combined damage cannot be described the count is left at zero, meaning full damage: redrawing too much only costs time, whereas redrawing too little leaves stale pixels on screen. lgDisplaysRectToAtlas is in common and unit tested, including a sweep of awkward inputs asserting no translated rectangle ever escapes its display. The DXGI plumbing around it still needs hardware.
5.3 KiB
Multi-monitor capture on DXGI
This backend duplicates every attached output on the chosen adapter and composes them into a single atlas frame. It is on by default and can be turned off with:
dxgi:multiMonitor=no
This code has never run on hardware. It cross compiles cleanly, which catches type and API errors, but acquire, release and timing behaviour can only be proven with a real multi-monitor Windows machine. Treat the first run as a bring-up exercise, and see "If it misbehaves" below.
How it works
dxgi_initMultiMonitor() runs once the primary duplication exists and the
capture format is known, but before the copy backend is created, since it
changes the frame dimensions to those of the atlas.
- Enumerates the adapter's attached outputs and duplicates each one. The
primary keeps
this->dup; the rest live inthis->extraDup[]. - Builds an
LGDisplayInfoper output fromDesktopCoordinates, which is already in virtual desktop space, and packs them withlgDisplaysPack(). - Creates an atlas
ID3D11Texture2Dof the packed size and setsthis->width/this->heightto it.
Each capture composes into that atlas with CopySubresourceRegion and hands
it to ppRun() in place of the acquired texture, so the post processors, the
copy backend and everything past them are unaware of the difference.
dxgi_waitFrame then reports the layout, with the virtual desktop as the
screen size and the atlas as the frame.
Deliberate choices
Secondary outputs are polled, not waited on. Outputs present
independently, so AcquireNextFrame is called with a zero timeout for each
secondary; waiting on each in turn would drag the frame rate down to that of
the slowest monitor. An output with nothing new keeps the pixels already in
its atlas region, which is correct because it has not changed.
Frames are released late. Acquired frames are released at the start of
the next capture, not after their copy. CopySubresourceRegion only records
into the device context, and releasing a frame invalidates its surface. This
mirrors what the primary output already did.
Anything unsafe falls back to a single output. That is not an error path, it is the design: a wrong atlas is worse than one working display. It happens when
- capture formats differ between outputs, since a subresource copy cannot convert, which occurs when one monitor is HDR and another is not
- any output is rotated, since the packer expects pixels in their final orientation
dxgi:outputnames a specific output- fewer than two outputs are attached
Damage
Damage is gathered from every output that presented this cycle and translated
into atlas space by lgDisplaysRectToAtlas(), which clips each rectangle to
the display it came from so one output's damage can never reach into another's
part of the atlas. Outputs that did not present contribute nothing, so a
static monitor costs no transfer at all.
Scaling is applied after a rectangle is placed in the atlas, never before, since the downscale factor applies to the frame as a whole.
If the combined damage cannot be described — more rectangles than
KVMFR_MAX_DAMAGE_RECTS, or either query failing — the count is left at zero,
which means full damage. Reporting too much only costs redraw time, whereas
reporting too little leaves stale pixels on screen.
The translation is unit tested in common/tests/test_displays.c, including a
sweep asserting that no translated rectangle ever escapes its display's
region. The plumbing that feeds it is not, and needs hardware.
Note the client still redraws the whole composed view each frame; damage governs the expensive part, which is the shared memory to texture upload.
Known gaps
HDR metadata is per output. this->outputDesc, dxgiColorSpace and
displayPathInfo describe the primary only, and the frame format carries one
set of HDR static metadata. Mixed HDR and SDR monitors are refused rather than
guessed at.
Rotation is refused rather than handled. KVMFRDisplay::rotation exists
for it, but composition would have to rotate during the copy, which
CopySubresourceRegion cannot do — it needs a shader pass.
If it misbehaves
Start with dxgi:multiMonitor=no to confirm the fault is in this path.
- Only one display, and the log says so — read the reason it logged; it will name the precondition that failed.
- One display is frozen while others update — a
ReleaseFrameis being missed for that output, so its duplication stopped delivering. Look atextraNeedsReleasehandling indxgi_releaseFrame. - Torn or interleaved pixels — a copy is racing the release. Check that frames are still held until the next capture.
- A display shows stale content — expected for a display that has not presented; suspicious if it never updates.
Testing
Cross compile from Linux to type check without a Windows machine:
cd host && mkdir build-win && cd build-win
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchain-mingw64.cmake ..
make
host/tests/lg-verify-atlas.c subscribes to a running host as a client would
and checks the published layout: every display inside the frame, no overlaps,
packed without scaling. It is backend agnostic, so pointing it at a Windows
host over the same shared memory is the fastest way to confirm this backend's
output is well formed.