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

+16
View File
@@ -103,6 +103,22 @@ void lgDisplaysRepack(
unsigned count,
unsigned bytesPerPixel);
/**
* Translate a rectangle from one display's own coordinate space, where (0, 0)
* is that display's top left, into packed atlas space.
*
* The rectangle is clipped to the display: a capture API may report a damaged
* region larger than the display it came from, and a rectangle that escaped
* into a neighbour's part of the atlas would make a consumer redraw the wrong
* display from the wrong pixels.
*
* Returns false if nothing of the rectangle falls inside the display, in
* which case `out` is untouched and the rectangle should be dropped.
*/
bool lgDisplaysRectToAtlas(const KVMFRDisplay * display,
int32_t x, int32_t y, int32_t width, int32_t height,
FrameDamageRect * out);
/**
* Returns the index of the display in `displays` containing the virtual
* desktop point (x, y), or -1 if the point falls in a gap between displays.
+27
View File
@@ -306,6 +306,33 @@ void lgDisplaysRepack(
}
}
bool lgDisplaysRectToAtlas(const KVMFRDisplay * d,
int32_t x, int32_t y, int32_t width, int32_t height,
FrameDamageRect * out)
{
if (width <= 0 || height <= 0)
return false;
int64_t x0 = x, y0 = y;
int64_t x1 = (int64_t)x + width;
int64_t y1 = (int64_t)y + height;
// clip to the display it came from
if (x0 < 0) x0 = 0;
if (y0 < 0) y0 = 0;
if (x1 > (int64_t)d->atlasWidth ) x1 = d->atlasWidth;
if (y1 > (int64_t)d->atlasHeight) y1 = d->atlasHeight;
if (x0 >= x1 || y0 >= y1)
return false;
out->x = (uint32_t)(d->atlasX + x0);
out->y = (uint32_t)(d->atlasY + y0);
out->width = (uint32_t)(x1 - x0);
out->height = (uint32_t)(y1 - y0);
return true;
}
int lgDisplaysFindAtPoint(const KVMFRDisplay * displays, unsigned count,
int32_t x, int32_t y)
{
+71
View File
@@ -183,6 +183,76 @@ static void testRepack(void)
free(src);
}
/* Damage rectangles arrive in a display's own coordinates. Getting the
* translation into atlas space wrong is uniquely nasty: it does not crash, it
* silently redraws the wrong region, leaving stale pixels on screen. */
static void testDamageTranslation(void)
{
printf("\n== damage translation ==\n");
/* a display parked away from the atlas origin, so an offset that is
* forgotten or applied twice is visible */
const KVMFRDisplay d =
{
.atlasX = 1000, .atlasY = 500,
.atlasWidth = 800, .atlasHeight = 600,
.desktopX = 0, .desktopY = 0,
.desktopWidth = 800, .desktopHeight = 600
};
FrameDamageRect r;
check(lgDisplaysRectToAtlas(&d, 0, 0, 800, 600, &r) &&
r.x == 1000 && r.y == 500 && r.width == 800 && r.height == 600,
"a full display rect maps to the whole atlas region");
check(lgDisplaysRectToAtlas(&d, 10, 20, 30, 40, &r) &&
r.x == 1010 && r.y == 520 && r.width == 30 && r.height == 40,
"an interior rect is offset by the display's atlas position");
// a rect running past the display must not bleed into its neighbour
check(lgDisplaysRectToAtlas(&d, 700, 500, 400, 400, &r) &&
r.x == 1700 && r.y == 1000 && r.width == 100 && r.height == 100,
"a rect overhanging the display is clipped to it");
check(lgDisplaysRectToAtlas(&d, -50, -50, 100, 100, &r) &&
r.x == 1000 && r.y == 500 && r.width == 50 && r.height == 50,
"a rect starting before the origin is clipped");
check(!lgDisplaysRectToAtlas(&d, 800, 0, 10, 10, &r),
"a rect entirely past the right edge is dropped");
check(!lgDisplaysRectToAtlas(&d, 0, 600, 10, 10, &r),
"a rect entirely past the bottom edge is dropped");
check(!lgDisplaysRectToAtlas(&d, -100, 0, 50, 10, &r),
"a rect entirely before the left edge is dropped");
check(!lgDisplaysRectToAtlas(&d, 0, 0, 0, 10, &r),
"a zero width rect is dropped");
check(!lgDisplaysRectToAtlas(&d, 0, 0, 10, -5, &r),
"a negative height rect is dropped");
/* Every translated rect must stay inside the display's atlas region.
* Sweep a range of awkward inputs and assert containment. */
bool contained = true;
for(int x = -900; x <= 900 && contained; x += 37)
for(int y = -700; y <= 700; y += 29)
for(int w = 1; w <= 1600; w += 311)
{
if (!lgDisplaysRectToAtlas(&d, x, y, w, w, &r))
continue;
if (r.x < d.atlasX || r.y < d.atlasY ||
r.x + r.width > d.atlasX + d.atlasWidth ||
r.y + r.height > d.atlasY + d.atlasHeight)
{
printf(" escaped at %d,%d %dx%d -> %u,%u %ux%u\n",
x, y, w, w, r.x, r.y, r.width, r.height);
contained = false;
break;
}
}
check(contained, "no translated rect ever escapes its display's region");
}
int main(void)
{
debug_init();
@@ -284,6 +354,7 @@ int main(void)
}
testRepack();
testDamageTranslation();
printf("\n%s (%d failure%s)\n", failures ? "FAILED" : "ALL PASSED",
failures, failures == 1 ? "" : "s");
+1 -1
View File
@@ -1 +1 @@
B7-386-g76207dce41+
B7-388-ge425b15a87+
+1 -1
View File
@@ -1,2 +1,2 @@
#define LG_VERSION_STR "B7-386-g76207dce41+"
#define LG_VERSION_STR "B7-388-ge425b15a87+"
#define LG_CURRENT_YEAR 2026
+1 -1
View File
@@ -1 +1 @@
const char * BUILD_VERSION = "B7-386-g76207dce41+";
const char * BUILD_VERSION = "B7-388-ge425b15a87+";
@@ -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;