Files
reims-vgpu/guest/kext/tests/surface_limits_test.cpp
T
Anees Iqbal 9e2f9e90fd 🐛 Lower IOSurface's limits to what the device's textures take
IOSurfaceRoot answers INT_MAX for the largest surface's width, height and bytes
per row (IOSurfaceGetPropertyMaximum) until an accelerator lowers them at its
start, as a Mac's GPUs do: 16384 wide and tall on the Intel and AMD Mac, with
131072 bytes a row, and 16384 and 32768 on the M3. The kext lowered none, so
our guests answered INT_MAX. Safari 16.6 under macOS 13 divides the maximum
width by the screen's scale in float to size the tiles of a page that scrolls
only vertically: at 1x that is 2^31, its int overflows, and every such page drew
only its scroll bar, on every host GPU. In a HiDPI mode the same page drew.

The kext now lowers them at its start, from the device info: the width and the
height to the device's max_texture_width_2d and max_texture_height_2d, which
the bundle answers to Metal, and the bytes per row to that width times 16, the
largest pixel a texture over a surface has (RGBA32Float). updateLimits' three
limits are named from a measurement: a probe kext's three distinct values, read
back in a 13.7.8 guest, came out as the bytes per row, the width and the height.
The row alignment the kext asked for before is asked for as it was.

What the guest sees: IOSurfaceGetPropertyMaximum answers the device's width and
height (16384) and 16 times the width a row, where it answered 2147483647.
surface_limits_test holds the limits it takes from the device info.

Checked: make -C guest test, both sets and prebuilt-check on the M3 Max. Cold
boots with this change's library and set, through the desktop, Finder,
Launchpad, Mission Control, Safari and the tall pages: 11.7.11, 12.7.6, 14.8.9
and 15.8.2 on the 780M, 13.7.8 on the 780M and the 3090, 26.7.2 on the 780M,
and arm64 27.0.1 under HVF (check-boot, 15 of 15). Each answered 16384, 16384
and 262144, and 13.7.8 drew the tall pages at 1x.
2026-11-11 05:03:01 -08:00

68 lines
2.6 KiB
C++

// The native test of RgfxSurfaceLimits.hpp: the largest surface the kext has IOSurface answer,
// from the device's widest and tallest 2D texture in the device info.
#include "RgfxSurfaceLimits.hpp"
#include "../../bundle/src/RgfxCaps.hpp"
#include <cstdio>
#include <cstdlib>
#include <cstring>
// The entries are the bundle's, which answers them to Metal.
static_assert(kRgfxSurfaceLimitsWidthEntry == kRgfxLimitMaxTextureWidth2D, "width entry");
static_assert(kRgfxSurfaceLimitsHeightEntry == kRgfxLimitMaxTextureHeight2D, "height entry");
static_assert(kRgfxSurfaceLimitsEnd <= sizeof(RgfxDeviceInfo), "inside the device info");
namespace {
int checks = 0;
void check(bool ok, const char *what)
{
checks++;
if (!ok) {
std::fprintf(stderr, "surface_limits_test: FAILED: %s\n", what);
std::exit(1);
}
}
// The limits as the kext reads them: out of a whole device info's bytes, the host's layout.
RgfxSurfaceLimits fromDeviceInfo(uint64_t width, uint64_t height)
{
static RgfxDeviceInfo info;
std::memset(&info, 0xa5, sizeof(info));
info.limits[kRgfxLimitMaxTextureWidth2D] = width;
info.limits[kRgfxLimitMaxTextureHeight2D] = height;
return rgfxSurfaceLimitsFrom(reinterpret_cast<const uint8_t *>(&info) + kRgfxSurfaceLimitsFirst);
}
bool is(RgfxSurfaceLimits l, uint32_t width, uint32_t height, uint32_t bytesPerRow)
{
return l.width == width && l.height == height && l.bytesPerRow == bytesPerRow;
}
} // namespace
int main()
{
// The device's 16384 a side (Metal's answer for the GPU families it reports): surfaces as
// wide and tall, and rows of 16384 RGBA32Float pixels.
check(is(rgfxSurfaceLimits(16384, 16384), 16384, 16384, 262144), "16384 a side");
check(is(fromDeviceInfo(16384, 16384), 16384, 16384, 262144),
"16384 a side, out of the device info");
// Width and height each from its own entry.
check(is(fromDeviceInfo(8192, 4096), 8192, 4096, 131072), "8192 wide, 4096 tall");
// A device that answers none leaves IOSurface's own.
check(is(rgfxSurfaceLimits(0, 0), kRgfxSurfaceNoLimit, kRgfxSurfaceNoLimit, kRgfxSurfaceNoLimit),
"no answer");
check(is(fromDeviceInfo(0, 16384), kRgfxSurfaceNoLimit, 16384, kRgfxSurfaceNoLimit),
"no width, out of the device info");
// Past what IOSurface holds: its own.
check(is(rgfxSurfaceLimits(1ull << 32, 16384), kRgfxSurfaceNoLimit, 16384, kRgfxSurfaceNoLimit),
"4 Gi wide");
// A row past IOSurface's own: its own row, the width kept.
check(is(rgfxSurfaceLimits(1u << 28, 16384), 1u << 28, 16384, kRgfxSurfaceNoLimit),
"a row past INT_MAX");
std::printf("surface_limits_test: OK (%d checks)\n", checks);
return 0;
}