dxgi: track damage per output and translate it into atlas space
build / client (Debug, map[cc:clang cxx:clang++], xdg-shell) (push) Failing after 20s
build / client (Debug, map[cc:gcc cxx:g++], libdecor) (push) Failing after 20s
build / client (Debug, map[cc:clang cxx:clang++], libdecor) (push) Failing after 21s
build / client (Release, map[cc:clang cxx:clang++], libdecor) (push) Failing after 20s
build / client (Debug, map[cc:gcc cxx:g++], xdg-shell) (push) Failing after 20s
build / client (Release, map[cc:clang cxx:clang++], xdg-shell) (push) Failing after 20s
build / module (push) Failing after 16s
build / client (Release, map[cc:gcc cxx:g++], libdecor) (push) Failing after 20s
build / client (Release, map[cc:gcc cxx:g++], xdg-shell) (push) Failing after 20s
build / host-linux (push) Failing after 19s
build / host-windows-cross (push) Failing after 19s
build / obs (clang) (push) Failing after 20s
build / docs (push) Failing after 3s
build / obs (gcc) (push) Failing after 20s
build / idd (push) Canceled after 0s
build / host-windows-native (push) Canceled after 0s

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.
This commit is contained in:
donny committed 2026-07-28 08:43:57 +00:00
1 parent e425b15a87
commit e582faed84
12 files changed
+253 -24

No files matched your search

@@ -56,15 +56,30 @@ when
- `dxgi:output` names a specific output
- fewer than two outputs are attached
## Known gaps
## Damage
**Damage is reported as full frame while composing.** `GetFrameDirtyRects`
returns rectangles for the primary alone, in its own coordinates, so they say
nothing about the other displays. Reporting them unchanged would leave stale
pixels on screen. Fixing this means collecting dirty rectangles per output,
translating each into atlas space, and falling back to full damage when the
combined count exceeds `KVMFR_MAX_DAMAGE_RECTS`. This is the single biggest
performance win available here.
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
+113 -13
View File
@@ -182,6 +182,9 @@ struct DXGIInterface
IDXGIOutput ** extraOutput [KVMFR_MAX_DISPLAYS];
IDXGIOutputDuplication ** extraDup [KVMFR_MAX_DISPLAYS];
bool extraNeedsRelease[KVMFR_MAX_DISPLAYS];
// which displays delivered new content this cycle, so damage is only
// gathered from the outputs that actually changed
bool displayHasFrame [KVMFR_MAX_DISPLAYS];
ID3D11Texture2D ** atlasTex;
unsigned atlasWidth, atlasHeight;
unsigned desktopWidth, desktopHeight;
@@ -1309,6 +1312,100 @@ static void rectToFrameDamageRect(RECT * src, FrameDamageRect * dst)
};
}
/* Place one rectangle, given in a display's own coordinates, into the frame's
* damage list in atlas space.
*
* Returns false only when there is no room left, which the caller must treat
* as "cannot describe this frame's damage" and fall back to full damage.
* A rectangle that falls outside its display is dropped, which is not a
* failure: there is simply nothing to record. */
static bool appendAtlasDamage(const KVMFRDisplay * d,
int32_t x, int32_t y, int32_t width, int32_t height,
FrameDamageRect * out, int * count, int max)
{
FrameDamageRect atlas;
if (!lgDisplaysRectToAtlas(d, x, y, width, height, &atlas))
return true;
if (*count >= max)
return false;
/* The frame may be downscaled as a whole, so scaling is applied after the
* rectangle has been placed in the atlas, never before. */
out[(*count)++] = (FrameDamageRect)
{
.x = floor((double)atlas.x * this->scaleX),
.y = floor((double)atlas.y * this->scaleY),
.width = ceil ((double)atlas.width * this->scaleX),
.height = ceil ((double)atlas.height * this->scaleY)
};
return true;
}
/* Gather damage from every output that presented this cycle, translated into
* atlas space. Leaves the count at zero, meaning full damage, if the combined
* damage cannot be described; reporting too much only costs redraw time,
* whereas reporting too little leaves stale pixels on screen. */
static void computeFrameDamageMulti(Texture * tex)
{
const int maxRects = ARRAY_LENGTH(tex->damageRects);
int count = 0;
for(unsigned i = 0; i < this->displayCount; ++i)
{
if (!this->displayHasFrame[i])
continue;
IDXGIOutputDuplication * dup = i == 0 ? *this->dup : *this->extraDup[i];
const KVMFRDisplay * d = &this->displays[i];
RECT dirtyRects[maxRects];
UINT required;
if (FAILED(IDXGIOutputDuplication_GetFrameDirtyRects(dup,
sizeof(dirtyRects), dirtyRects, &required)))
return;
const int dirtyCount = required / sizeof(*dirtyRects);
DXGI_OUTDUPL_MOVE_RECT moveRects[maxRects / 2];
if (FAILED(IDXGIOutputDuplication_GetFrameMoveRects(dup,
sizeof(moveRects), moveRects, &required)))
return;
const int moveCount = required / sizeof(*moveRects);
for(int r = 0; r < dirtyCount; ++r)
if (!appendAtlasDamage(d,
dirtyRects[r].left, dirtyRects[r].top,
dirtyRects[r].right - dirtyRects[r].left,
dirtyRects[r].bottom - dirtyRects[r].top,
tex->damageRects, &count, maxRects))
return;
// a move damages both where the content came from and where it went
for(int r = 0; r < moveCount; ++r)
{
const DXGI_OUTDUPL_MOVE_RECT * m = &moveRects[r];
if (m->SourcePoint.x == m->DestinationRect.left &&
m->SourcePoint.y == m->DestinationRect.top)
continue;
const int32_t w = m->DestinationRect.right - m->DestinationRect.left;
const int32_t h = m->DestinationRect.bottom - m->DestinationRect.top;
if (!appendAtlasDamage(d, m->SourcePoint.x, m->SourcePoint.y, w, h,
tex->damageRects, &count, maxRects))
return;
if (!appendAtlasDamage(d, m->DestinationRect.left, m->DestinationRect.top,
w, h, tex->damageRects, &count, maxRects))
return;
}
}
tex->damageRectsCount = count;
}
static void computeFrameDamage(Texture * tex)
{
// By default, damage the full frame.
@@ -1317,6 +1414,12 @@ static void computeFrameDamage(Texture * tex)
if (this->disableDamage)
return;
if (this->displayCount)
{
computeFrameDamageMulti(tex);
return;
}
const int maxDamageRectsCount = ARRAY_LENGTH(tex->damageRects);
// Compute dirty rectangles.
@@ -1409,6 +1512,9 @@ static CaptureResult dxgi_compositeDisplays(ID3D11Texture2D * primary)
CaptureResult result = CAPTURE_RESULT_OK;
comRef_scopePush(KVMFR_MAX_DISPLAYS * 2 + 4);
memset(this->displayHasFrame, 0, sizeof(this->displayHasFrame));
this->displayHasFrame[0] = true;
const KVMFRDisplay * pd = &this->displays[0];
LG_LOCK(this->deviceContextLock);
ID3D11DeviceContext_CopySubresourceRegion(*this->deviceContext,
@@ -1467,6 +1573,8 @@ static CaptureResult dxgi_compositeDisplays(ID3D11Texture2D * primary)
(ID3D11Resource *)*this->atlasTex, 0, d->atlasX, d->atlasY, 0,
(ID3D11Resource *)*tex, 0, NULL);
LG_UNLOCK(this->deviceContextLock);
this->displayHasFrame[i] = true;
}
exit:
@@ -1874,19 +1982,11 @@ static CaptureResult dxgi_waitFrame(unsigned frameBufferIndex,
memcpy(frame->displays, this->displays,
this->displayCount * sizeof(*this->displays));
/* Damage rectangles come from the primary output alone and are in its own
* coordinates, so they say nothing about what changed on the other
* displays. Report full damage instead until per output damage is tracked
* and translated into atlas space; an incorrect damage rectangle leaves
* stale pixels on screen, which is far worse than redrawing too much. */
if (this->displayCount)
frame->damageRectsCount = 0;
else
{
frame->damageRectsCount = tex->damageRectsCount;
memcpy(frame->damageRects, tex->damageRects,
tex->damageRectsCount * sizeof(*tex->damageRects));
}
/* Damage is already in atlas space, whether it came from one output or was
* gathered from several and translated. */
frame->damageRectsCount = tex->damageRectsCount;
memcpy(frame->damageRects, tex->damageRects,
tex->damageRectsCount * sizeof(*tex->damageRects));
atomic_fetch_sub_explicit(&this->texReady, 1, memory_order_release);
return CAPTURE_RESULT_OK;