From 76207dce41d71a4ee4a05a57198cfe1174f4dec1 Mon Sep 17 00:00:00 2001 From: Angel Mankel Date: Tue, 28 Jul 2026 08:23:22 +0000 Subject: [PATCH] common: consider every subset sum as a candidate shelf width Only trying running totals in packing order missed layouts where the displays sharing a shelf are not the first few. A 4K above two 1440p side by side is the common case: the 4K sorts first by height but belongs on a shelf of its own, so the useful width is the sum of the other two. For that arrangement the atlas drops from 3840x5040 to 5120x3600, about 5% fewer pixels per frame. With at most 8 displays this is 255 subsets, so the extra search costs nothing. --- common/src/displays.c | 47 ++++++++++++------ common/tests/test_displays.c | 21 ++++++++ .../platform/Windows/resource.rc.res | Bin 93144 -> 93144 bytes .../looking-glass-host.dir/version.c.obj | Bin 700 -> 700 bytes host/build-win/VERSION | 2 +- .../lg_common.dir/src/displays.c.obj | Bin 9987 -> 10035 bytes host/build-win/include/version.h | 2 +- host/build-win/version.c | 2 +- 8 files changed, 56 insertions(+), 18 deletions(-) diff --git a/common/src/displays.c b/common/src/displays.c index a3d7658f..e6eac1f8 100644 --- a/common/src/displays.c +++ b/common/src/displays.c @@ -126,29 +126,46 @@ bool lgDisplaysPack( order[j] = v; } - unsigned maxWidth = 0, totalWidth = 0; + unsigned maxWidth = 0; for(unsigned i = 0; i < count; ++i) - { if (in[i].width > maxWidth) maxWidth = in[i].width; - totalWidth += in[i].width; - } - /* Candidate shelf widths: the widest single display (a pure vertical stack), - * every running total in packing order (which produces the natural "N per - * row" arrangements), and the sum of all widths (a single row). Every - * candidate is at least maxWidth so no display is ever too wide to place. */ - unsigned candidates[KVMFR_MAX_DISPLAYS + 2]; + /* Candidate shelf widths: the total width of every subset of displays. + * + * Any shelf is some set of displays sitting side by side, so its width is a + * subset sum; enumerating them covers every arrangement worth trying. Only + * considering running totals in packing order misses layouts where the + * displays that share a shelf are not the first few — a 4K above two 1440p + * side by side, for instance, since the 4K sorts first but belongs alone. + * + * With at most 8 displays this is 255 subsets, which costs nothing. Every + * candidate is raised to maxWidth so no display is ever too wide to place. */ + unsigned candidates[1u << KVMFR_MAX_DISPLAYS]; unsigned candidateCount = 0; - candidates[candidateCount++] = maxWidth; - unsigned running = 0; - for(unsigned i = 0; i < count; ++i) + for(uint32_t mask = 1; mask < (1u << count); ++mask) { - running += in[order[i]].width; - candidates[candidateCount++] = running < maxWidth ? maxWidth : running; + unsigned width = 0; + for(unsigned i = 0; i < count; ++i) + if (mask & (1u << i)) + width += in[i].width; + + if (width < maxWidth) + width = maxWidth; + + // duplicates are common when displays share a width, so drop them + bool seen = false; + for(unsigned c = 0; c < candidateCount; ++c) + if (candidates[c] == width) + { + seen = true; + break; + } + + if (!seen) + candidates[candidateCount++] = width; } - candidates[candidateCount++] = totalWidth < maxWidth ? maxWidth : totalWidth; unsigned bestX[KVMFR_MAX_DISPLAYS], bestY[KVMFR_MAX_DISPLAYS]; unsigned bestWidth = 0, bestHeight = 0; diff --git a/common/tests/test_displays.c b/common/tests/test_displays.c index 186a769c..a66e7002 100644 --- a/common/tests/test_displays.c +++ b/common/tests/test_displays.c @@ -203,6 +203,27 @@ int main(void) }; run("3 identical 1080p displays", triple, 3); + /* One 4K next to two 1440p. The best layout puts the 4K on its own shelf + * with the two 1440p side by side below it, which is only found if the + * candidate shelf widths include sums of displays that are not the first + * few in packing order. */ + const LGDisplayInfo mixed[3] = { + { .id = 0, .name = "4K", .x = 0, .y = 0, .width = 3840, .height = 2160, + .primary = true }, + { .id = 1, .name = "1440pA", .x = 3840, .y = 0, .width = 2560, .height = 1440 }, + { .id = 2, .name = "1440pB", .x = 6400, .y = 0, .width = 2560, .height = 1440 }, + }; + run("4K plus two 1440p", mixed, 3); + { + KVMFRDisplay o[KVMFR_MAX_DISPLAYS]; + unsigned aw, ah, dw, dh; + if (lgDisplaysPack(mixed, 3, 1, o, &aw, &ah, &dw, &dh)) + check((uint64_t)aw * ah <= 18432000ULL, + "4K + 2x1440p packs into at most 5120x3600 (got %ux%u)", aw, ah); + else + check(false, "4K + 2x1440p packed"); + } + const LGDisplayInfo single[1] = { { .id = 0, .name = "only", .x = 0, .y = 0, .width = 2560, .height = 1440 }, }; diff --git a/host/build-win/CMakeFiles/looking-glass-host.dir/platform/Windows/resource.rc.res b/host/build-win/CMakeFiles/looking-glass-host.dir/platform/Windows/resource.rc.res index 4d6d530bfb0f1382e22751fc104404fc9684bedb..81c23877ba7c962ff7a48885d3b90fa6f36859ee 100644 GIT binary patch delta 75 zcmca{o%P0b)`l&Ne94lg47v>I3`q>;48{y;K$ys22;>I3`q>e3`Pv*42eKi8iN@_GK1N6^<>8RjCkblr!f9x F0|3In5s?4@ diff --git a/host/build-win/CMakeFiles/looking-glass-host.dir/version.c.obj b/host/build-win/CMakeFiles/looking-glass-host.dir/version.c.obj index 524f2027e8e29f6cdc26b8a0fe052eccbae55efa..7950528d7e2d2862bd134de504cf514dcb87413f 100644 GIT binary patch delta 30 lcmdnPx`%ZGCnKM!ZhDfraavlUVUn@2_C!b7%_fW;i~xy>2$28) delta 30 lcmdnPx`%ZGCnKMMZhDfjk$GZLnpv`$_C!b7%_fW;i~xvb2#^2( diff --git a/host/build-win/VERSION b/host/build-win/VERSION index 9580216f..4f69cdb3 100644 --- a/host/build-win/VERSION +++ b/host/build-win/VERSION @@ -1 +1 @@ -B7-380-gb327abf6c6+ \ No newline at end of file +B7-385-gb73ffa1b33+ \ No newline at end of file diff --git a/host/build-win/common/CMakeFiles/lg_common.dir/src/displays.c.obj b/host/build-win/common/CMakeFiles/lg_common.dir/src/displays.c.obj index bb9ebc7cf54ef5d4de8e4e0a915cf3af544f27fb..3ba210b381296ba2ec04a7a7f41ff4caeb781e55 100644 GIT binary patch delta 3115 zcmZuzU2IfE6rS0ic3ZdI1(v4J1xi~}kVXR$lBK8@D0+zv*%*lW0BHgVnrI}YMNE)| z-i|QbY%IClgcn~75+6{SRGUI*+!n)DgUO09A(00(#8B4+d_av6>N#ibOjp|bu=CA1 z-}#-tb#{#GsM+^HBg1dc>6$VhJpJIMnxQ0PzegGS*xSZi`4R6;-X1;Kz}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z?GMhQ!a+ymu1}!#>5(ud;l9|mH3Y-SwyiHO04-@1h(bcuC^hmb$*W&8zSLM;3KyZv z9jX>=57w}cHZpo~AG9=uR{E)Jo$dl#oc zle#kvt+l&3lA$aVu zyw1I*71lTFc-YJ@$66z(%Q|aOfp9G>(0XgFHp`f`0y9Zwj$cWtSlN+f&`eUXkTdMzD} zUc>evc@|n5*HeQcZMcZxl7?&1j^aAKtXU*$R;dZ`gXFIOpJ_jYq_#{)c2VcZX`>Wh ztJJcKT4j|IoOj<-ZJAmO$+f~-4Bcym_>J@f+@Xc~>a`2mL?E@Da&P~{R;ZA!bs)Hv zehGdIuyk;!anM_O9<~&8#$fA|hZn2F7~i95QcoEIxb5&5@5Cw;k?2EBY%vt!i6+s< zD6;U%W%h-eb1=SfqsXnWu<9awKjT>tuw$h{dws*`(MI^&gK3x@i9kq%P8n2gVhnyW zK{{*Dyg_#jS~lokgCfn^c7s7326g#p6kPWja<4%L4LWR4)}W&Xy=BlzgYpKQGw8BG kpBYpFl-Nhv-2%9u&=lOB2MGNbEsb~PL;P52TXJLL|N3(mGXMYp diff --git a/host/build-win/include/version.h b/host/build-win/include/version.h index e9b48932..fd114bce 100644 --- a/host/build-win/include/version.h +++ b/host/build-win/include/version.h @@ -1,2 +1,2 @@ -#define LG_VERSION_STR "B7-380-gb327abf6c6+" +#define LG_VERSION_STR "B7-385-gb73ffa1b33+" #define LG_CURRENT_YEAR 2026 diff --git a/host/build-win/version.c b/host/build-win/version.c index 0d28d0ca..8f6cfbf1 100644 --- a/host/build-win/version.c +++ b/host/build-win/version.c @@ -1 +1 @@ -const char * BUILD_VERSION = "B7-380-gb327abf6c6+"; \ No newline at end of file +const char * BUILD_VERSION = "B7-385-gb73ffa1b33+"; \ No newline at end of file