fix: a density grid is bounded in cells, not by the window's pixels
A grid was the axes rect in device pixels, so dragging a window wide made the image wider with it -- and the per-frame pass over it is O(cells): the fade multiplies every cell, a host's ink()/peak() read every cell, and a frame the fade moved converts and uploads every cell as one byte. At 2x device scale a 1520x850 window's axes rect is 2877 device pixels wide, so four rows of one trace are 4.5 M cells and 4.9 MB uploaded a frame; measured headless on llvmpipe with a 2 s window, 12.9 ms of tick and 12.8 ms of draw a frame, every frame under a time fade -- a cost that follows the window and has no ceiling. The grid is now the axes rect up to kDensityCellCap (2^18 cells, a mebibyte of counts), scaled down uniformly past it so cells stay square and the picture keeps the axes' aspect, and magnified back over the rect with a linear filter: a cell is a count, so a fragment between two of them is between their two ink levels, which a point sample cannot give once a cell is several pixels wide. The same four rows at the cap are 1.05 M cells, 3.9 ms of tick and 5.6 ms of draw and 1.0 MB uploaded, while a grid under the cap is untouched -- a 640x360 viewport's 573x313 axes is still 179 349 cells, one a device pixel. The image's footprint is the axes rect rounded up to whole device pixels on both sides of the pipe, so a grid under the cap keeps the exact one-texel-a-pixel mapping it has always had (sx/sy come out exactly 1) and only a capped grid reads its cell index through a scale. Moving the decay itself to the GPU was considered and left out: the CPU grid is what ink()/peak() read, what the deposit adds to and what the per-sweep dedupe keys on, so a GPU image would have to be reconciled with it per frame -- a larger change than this bound, and not one to half-do. Tests: a_density_grid_is_capped_whatever_the_window reads the bound off a 3840x2160 viewport (261 504 cells of a 3784x2138 axes, aspect kept), a_capped_density_is_magnified_over_the_axes_rect reads the screen side of it (569x460 cells covering all 832 of the rect's columns), and the density cases now assert their capped figures. 399 cases, 0 failed.
This commit is contained in:
1 parent
1b8b55d3ee
commit
f79a97be9b
12 files changed
+414
-108
No files matched your search
+84
-14
@@ -2839,14 +2839,34 @@ struct DensityFixture {
|
||||
|
||||
} // namespace
|
||||
|
||||
/// The grid is the screen's, not the data's: one cell per device pixel of the axes rect, and
|
||||
/// nothing at all until the density is switched on. The figures here are the ones the API's
|
||||
/// memory claim rests on -- `cell_count * 4` bytes, whatever the trigger has done.
|
||||
/// The grid is the screen's, not the data's: one cell per device pixel of the axes rect **up to
|
||||
/// `kDensityCellCap`**, and nothing at all until the density is switched on. The figures here are
|
||||
/// the ones the API's memory claim rests on -- `cell_count * 4` bytes, whatever the trigger has
|
||||
/// done and whatever the window is.
|
||||
PLOT_TEST(a_density_grid_is_the_axes_in_device_pixels) {
|
||||
DensityFixture f;
|
||||
const plot::Rect axes = f.plot.layout().axes;
|
||||
const plot::u32 columns = static_cast<plot::u32>(std::ceil(axes.w));
|
||||
const plot::u32 rows = static_cast<plot::u32>(std::ceil(axes.h));
|
||||
// The 640x480 viewport this fixture asks for is past the cap, so its grid is the axes rect
|
||||
// *scaled down* -- the rule the library states, written out here so the assertion is arithmetic
|
||||
// rather than a recorded number. A grid under the cap is the rect itself, which the 320x240 leg
|
||||
// at the end reads.
|
||||
const auto capped = [](plot::u32 w, plot::u32 h) {
|
||||
std::pair<plot::u32, plot::u32> out{w, h};
|
||||
const plot::u64 pixels = static_cast<plot::u64>(w) * static_cast<plot::u64>(h);
|
||||
if (pixels > static_cast<plot::u64>(plot::kDensityCellCap)) {
|
||||
const plot::f64 s = std::sqrt(static_cast<plot::f64>(plot::kDensityCellCap) /
|
||||
static_cast<plot::f64>(pixels));
|
||||
out.first = std::max(1u, static_cast<plot::u32>(static_cast<plot::f64>(w) * s));
|
||||
out.second = std::max(1u, static_cast<plot::u32>(static_cast<plot::f64>(h) * s));
|
||||
}
|
||||
return out;
|
||||
};
|
||||
const auto [columns, rows] =
|
||||
capped(static_cast<plot::u32>(std::ceil(axes.w)), static_cast<plot::u32>(std::ceil(axes.h)));
|
||||
// The row a device y lands in is read through the grid's cells-per-*whole*-pixel, which is what
|
||||
// the deposit uses (see `deposit_density_segment()`): 1 while the grid is the rect.
|
||||
const plot::f32 sy = static_cast<plot::f32>(rows) / std::ceil(axes.h);
|
||||
PLOT_CHECK(static_cast<plot::u64>(columns) * rows <= plot::kDensityCellCap);
|
||||
|
||||
// Off is the default, and off is *nothing*: no grid, no ink, and an accumulation that
|
||||
// deposits nothing at all.
|
||||
@@ -2866,8 +2886,9 @@ PLOT_TEST(a_density_grid_is_the_axes_in_device_pixels) {
|
||||
PLOT_CHECK(grid.columns() == columns);
|
||||
PLOT_CHECK(grid.rows() == rows);
|
||||
PLOT_CHECK(grid.cell_count() == static_cast<plot::usize>(columns) * rows);
|
||||
// A 640x480 viewport, minus the chrome: `4 bytes * cells`.
|
||||
// `4 bytes * cells`, and never more than the cap: the whole point of the bound.
|
||||
const plot::usize bytes = grid.cell_count() * sizeof(plot::f32);
|
||||
PLOT_CHECK(bytes <= static_cast<plot::usize>(plot::kDensityCellCap) * sizeof(plot::f32));
|
||||
std::printf("[density] grid=%ux%u cells=%zu grid_bytes=%zu ink=%.0f peak=%.0f\n",
|
||||
grid.columns(), grid.rows(), grid.cell_count(), bytes, grid.ink(),
|
||||
static_cast<double>(grid.peak()));
|
||||
@@ -2879,7 +2900,7 @@ PLOT_TEST(a_density_grid_is_the_axes_in_device_pixels) {
|
||||
|
||||
// ... and all of it in one row, the one y = 0 maps to.
|
||||
const plot::Transform ty = f.plot.y_transform(f.ay);
|
||||
const int row = static_cast<int>(std::floor(ty.to_screen_y(0.0) - axes.y));
|
||||
const int row = static_cast<int>(std::floor((ty.to_screen_y(0.0) - axes.y) * sy));
|
||||
plot::u32 lit = 0;
|
||||
for (plot::u32 r = 0; r < grid.rows(); ++r) {
|
||||
for (plot::u32 c = 0; c < grid.columns(); ++c) {
|
||||
@@ -2891,13 +2912,53 @@ PLOT_TEST(a_density_grid_is_the_axes_in_device_pixels) {
|
||||
PLOT_CHECK(lit == columns);
|
||||
|
||||
// The accumulation is a *screen* image: a plot that has moved to another axes rect gets a
|
||||
// grid of that size and nothing carried over from the old one.
|
||||
// grid of that size and nothing carried over from the old one -- and that rect is under the
|
||||
// cap, so the grid is it *exactly*: one cell a device pixel, no scaling at all.
|
||||
f.plot.set_viewport({0.0f, 0.0f, 320.0f, 240.0f});
|
||||
f.plot.layout();
|
||||
f.plot.accumulate_density();
|
||||
PLOT_CHECK(f.plot.density_grid(0u).columns() < columns);
|
||||
PLOT_CHECK_NEAR(f.plot.density_grid(0u).ink(),
|
||||
static_cast<double>(f.plot.density_grid(0u).columns()), 1.0);
|
||||
const plot::DensityGrid& small = f.plot.density_grid(0u);
|
||||
const plot::Rect small_axes = f.plot.layout().axes;
|
||||
PLOT_CHECK(small.columns() == static_cast<plot::u32>(std::ceil(small_axes.w)));
|
||||
PLOT_CHECK(small.rows() == static_cast<plot::u32>(std::ceil(small_axes.h)));
|
||||
PLOT_CHECK(small.cell_count() <= plot::kDensityCellCap);
|
||||
PLOT_CHECK(small.columns() < columns);
|
||||
PLOT_CHECK_NEAR(small.ink(), static_cast<double>(small.columns()), 1.0);
|
||||
}
|
||||
|
||||
/// The cap is what keeps the resolution from following the window: a grid is never more than
|
||||
/// `kDensityCellCap` cells, whatever the axes rect is, so the memory a display holds and the frame
|
||||
/// pass over it are numbers and not functions of how wide the window was dragged. The scaling is
|
||||
/// **uniform**, so a cell stays square and the picture keeps the axes' aspect -- the reading that
|
||||
/// separates this from a cap on one axis, which would stretch every cell by the other one's ratio.
|
||||
PLOT_TEST(a_density_grid_is_capped_whatever_the_window) {
|
||||
DensityFixture f;
|
||||
f.plot.set_viewport({0.0f, 0.0f, 3840.0f, 2160.0f});
|
||||
f.plot.layout();
|
||||
plot::DensityConfig cfg;
|
||||
cfg.enabled = true;
|
||||
f.plot.set_density(cfg);
|
||||
f.plot.accumulate_density();
|
||||
|
||||
const plot::DensityGrid& grid = f.plot.density_grid(0u);
|
||||
const plot::Rect axes = f.plot.layout().axes;
|
||||
PLOT_CHECK(static_cast<plot::u64>(grid.columns()) * grid.rows() <= plot::kDensityCellCap);
|
||||
PLOT_CHECK(grid.cell_count() * sizeof(plot::f32) <= 1'048'576u);
|
||||
// ... and it is as large as the cap allows: flooring a uniform scale costs at most the two
|
||||
// columns and rows the truncation drops, so the grid is within a hair of the whole budget.
|
||||
PLOT_CHECK(static_cast<plot::f64>(grid.cell_count()) >
|
||||
0.99 * static_cast<plot::f64>(plot::kDensityCellCap));
|
||||
// Uniform: the grid's aspect is the axes rect's, to the rounding of the two floors.
|
||||
PLOT_CHECK_NEAR(static_cast<plot::f64>(grid.columns()) / grid.rows(),
|
||||
static_cast<plot::f64>(axes.w) / axes.h, 0.01);
|
||||
|
||||
// A cap on the *cells* also returns a full sweep's worth of ink: one flat pass over the window
|
||||
// is one per column however fine or coarse the grid is.
|
||||
PLOT_CHECK_NEAR(grid.ink(), static_cast<double>(grid.columns()), 1.0);
|
||||
PLOT_CHECK_NEAR(grid.peak(), 1.0, 1e-4);
|
||||
std::printf("[density] capped grid=%ux%u cells=%zu bytes=%zu of a %ux%u axes\n", grid.columns(),
|
||||
grid.rows(), grid.cell_count(), grid.cell_count() * sizeof(plot::f32),
|
||||
static_cast<plot::u32>(std::ceil(axes.w)), static_cast<plot::u32>(std::ceil(axes.h)));
|
||||
}
|
||||
|
||||
/// Per acquisition the image is multiplied by the factor *before* the new acquisition is
|
||||
@@ -3045,11 +3106,15 @@ PLOT_TEST(a_density_deposits_only_where_the_trace_is) {
|
||||
PLOT_CHECK(grid.ink() >= static_cast<double>(grid.columns()));
|
||||
const plot::Transform ty = split.plot.y_transform(split.ay);
|
||||
const plot::Rect axes = split.plot.layout().axes;
|
||||
// The row a device y lands in is the grid's own mapping: the axis rect scaled by the grid's
|
||||
// cells-per-pixel (1 while the rect is under `kDensityCellCap`, less past it).
|
||||
const plot::f32 sy =
|
||||
axes.h > 0.0f ? static_cast<plot::f32>(grid.rows()) / std::ceil(axes.h) : 1.0f;
|
||||
// The deposit keeps one ULP of the bottom edge off its row, so `y = -1` -- which maps *onto*
|
||||
// the bottom edge -- lands in the last row rather than one past the end.
|
||||
const auto row_of = [&](plot::f64 y) {
|
||||
return static_cast<plot::u32>(std::clamp(
|
||||
static_cast<int>(std::floor(ty.to_screen_y(y) - axes.y)), 0,
|
||||
static_cast<int>(std::floor((ty.to_screen_y(y) - axes.y) * sy)), 0,
|
||||
static_cast<int>(grid.rows()) - 1));
|
||||
};
|
||||
PLOT_CHECK(grid.at(0u, row_of(-1.0)) > 0.0f);
|
||||
@@ -3077,8 +3142,11 @@ PLOT_TEST(a_density_deposits_only_where_the_trace_is) {
|
||||
const plot::DensityGrid& gap_grid = gap.plot.density_grid(0u);
|
||||
const plot::Transform tx = gap.plot.x_transform(gap.ax);
|
||||
const plot::Rect gap_axes = gap.plot.layout().axes;
|
||||
const plot::f32 gsx = gap_axes.w > 0.0f
|
||||
? static_cast<plot::f32>(gap_grid.columns()) / std::ceil(gap_axes.w)
|
||||
: 1.0f;
|
||||
const plot::u32 column = static_cast<plot::u32>(std::clamp(
|
||||
static_cast<int>(std::floor(tx.to_screen_x(0.5) - gap_axes.x)), 0,
|
||||
static_cast<int>(std::floor((tx.to_screen_x(0.5) - gap_axes.x) * gsx)), 0,
|
||||
static_cast<int>(gap_grid.columns()) - 1));
|
||||
PLOT_CHECK(column_ink(whole_grid, column) > 0.0); // the sample is there: the column is lit
|
||||
PLOT_CHECK(column_ink(gap_grid, column) == 0.0); // the NaN is a gap: nothing joins across it
|
||||
@@ -3123,8 +3191,10 @@ PLOT_TEST(a_density_holds_one_grid_per_trace) {
|
||||
|
||||
const plot::Transform ty = p.y_transform(ay);
|
||||
const plot::Rect axes = p.layout().axes;
|
||||
const plot::f32 sy =
|
||||
axes.h > 0.0f ? static_cast<plot::f32>(g0.rows()) / std::ceil(axes.h) : 1.0f;
|
||||
const auto row_of = [&](plot::f64 y) {
|
||||
return static_cast<plot::u32>(std::floor(ty.to_screen_y(y) - axes.y));
|
||||
return static_cast<plot::u32>(std::floor((ty.to_screen_y(y) - axes.y) * sy));
|
||||
};
|
||||
const plot::u32 lo_row = row_of(-0.5);
|
||||
const plot::u32 hi_row = row_of(0.5);
|
||||
|
||||
@@ -7725,3 +7725,127 @@ PLOT_TEST(a_phosphor_density_is_the_trace_in_its_own_colour) {
|
||||
bg_g, cell_g, faded_g, steady_g, quarter_g, before, after);
|
||||
ctx.destroy();
|
||||
}
|
||||
|
||||
/// A grid **past `kDensityCellCap`** is the axes rect scaled down, and the frame magnifies the image
|
||||
/// back over that rect: this is the screen-side half of the bound --
|
||||
/// `a_density_grid_is_capped_whatever_the_window` reads the counts of a capped grid, and the two
|
||||
/// agree only if the deposit and the draw use the same footprint, the axes rect in *whole* device
|
||||
/// pixels.
|
||||
///
|
||||
/// What is read here is what a wrong scale moves: the image covers the rect's own columns rather
|
||||
/// than the smaller grid's (the assertion a quad drawn at the grid's size fails), it stays inside
|
||||
/// the rect, and the sweep is on the row its own transform maps y = 0 to, in the trace's colour.
|
||||
PLOT_TEST(a_capped_density_is_magnified_over_the_axes_rect) {
|
||||
const plot::u32 w = 900, h = 720;
|
||||
plottest::HeadlessContext ctx(w, h);
|
||||
plot::Status st;
|
||||
PLOT_CHECK(ctx.create(&st));
|
||||
PLOT_CHECK(plot::gl::load(nullptr));
|
||||
plot::Renderer r;
|
||||
PLOT_CHECK(r.init(&st));
|
||||
|
||||
plot::data::ArraySeries s(std::vector<plot::f32>{0.0f, 0.0f}, 0.0, 1.0);
|
||||
plot::Plot p;
|
||||
p.set_viewport({0.0f, 0.0f, static_cast<plot::f32>(w), static_cast<plot::f32>(h)});
|
||||
const plot::u32 ax = p.add_x_axis({"t", {0.0, 1.0}, false, plot::AxisPlace::Bottom, false});
|
||||
const plot::u32 ay = p.add_y_axis({"v", {-1.0, 1.0}, false, plot::AxisPlace::Left, false});
|
||||
const plot::Color colour = plot::Color::hex(0x00E070);
|
||||
PLOT_CHECK(p.add_trace({&s, colour, 1.5f, ax, ay, "flat"}) == 0u);
|
||||
p.set_x_range(ax, {0.0, 1.0});
|
||||
p.set_y_range(ay, {-1.0, 1.0});
|
||||
|
||||
const plot::Rect frame{0.0f, 0.0f, static_cast<plot::f32>(w), static_cast<plot::f32>(h)};
|
||||
r.begin_frame(w, h, frame);
|
||||
r.draw(p);
|
||||
r.end_frame();
|
||||
PLOT_CHECK(!r.last_frame_had_gl_error());
|
||||
const std::vector<plot::u8> without = read_frame(ctx);
|
||||
|
||||
plot::DensityConfig cfg;
|
||||
cfg.enabled = true;
|
||||
p.set_density(cfg);
|
||||
p.accumulate_density();
|
||||
const plot::DensityGrid& grid = p.density_grid(0u);
|
||||
const plot::Rect axes = p.layout().axes;
|
||||
// The rect is past the cap, so the grid is smaller than it and the frame has to scale the image
|
||||
// up -- and the one flat sweep still lights every column, once.
|
||||
PLOT_CHECK(grid.cell_count() > 0u);
|
||||
PLOT_CHECK(grid.cell_count() <= plot::kDensityCellCap);
|
||||
PLOT_CHECK(static_cast<plot::u64>(grid.columns()) < static_cast<plot::u64>(std::ceil(axes.w)));
|
||||
PLOT_CHECK(static_cast<plot::u64>(grid.rows()) < static_cast<plot::u64>(std::ceil(axes.h)));
|
||||
PLOT_CHECK_NEAR(grid.ink(), static_cast<double>(grid.columns()), 1.0);
|
||||
|
||||
r.begin_frame(w, h, frame);
|
||||
r.draw(p);
|
||||
r.end_frame();
|
||||
PLOT_CHECK(!r.last_frame_had_gl_error());
|
||||
const plot::FrameStats stats = r.stats();
|
||||
PLOT_CHECK(stats.density_cells == static_cast<plot::u32>(grid.cell_count()));
|
||||
PLOT_CHECK(stats.density_upload_bytes == static_cast<plot::u32>(grid.cell_count()));
|
||||
const std::vector<plot::u8> capped = read_frame(ctx);
|
||||
|
||||
const auto differs = [&](plot::u32 x, plot::u32 y) {
|
||||
const plot::usize k = (plot::usize(y) * w + x) * 4u;
|
||||
return capped[k] != without[k] || capped[k + 1u] != without[k + 1u] ||
|
||||
capped[k + 2u] != without[k + 2u];
|
||||
};
|
||||
const auto at = [&](const std::vector<plot::u8>& f, plot::u32 x, plot::u32 y, int c) {
|
||||
return static_cast<int>(f[(plot::usize(y) * w + x) * 4u + static_cast<plot::usize>(c)]);
|
||||
};
|
||||
|
||||
// **The image covers the rect's columns, not the grid's.** A quad drawn at the grid's own size --
|
||||
// the temptation once a cell is no longer a pixel -- would paint the left `columns` of the rect
|
||||
// and leave the rest of it blank, which this counts.
|
||||
plot::u32 changed_cols = 0;
|
||||
plot::u32 lo_x = w, hi_x = 0, lo_y = h, hi_y = 0;
|
||||
for (plot::u32 x = 0; x < w; ++x) {
|
||||
bool any = false;
|
||||
for (plot::u32 y = 0; y < h; ++y) {
|
||||
if (!differs(x, y)) continue;
|
||||
any = true;
|
||||
lo_x = std::min(lo_x, x);
|
||||
hi_x = std::max(hi_x, x);
|
||||
lo_y = std::min(lo_y, y);
|
||||
hi_y = std::max(hi_y, y);
|
||||
}
|
||||
if (any) ++changed_cols;
|
||||
}
|
||||
PLOT_CHECK(changed_cols > 0u);
|
||||
PLOT_CHECK(changed_cols + 2u >= static_cast<plot::u32>(axes.w));
|
||||
// ... and it stays inside the rect it is an image of.
|
||||
PLOT_CHECK(static_cast<plot::f32>(lo_x) >= axes.x - 1.0f);
|
||||
PLOT_CHECK(static_cast<plot::f32>(hi_x) <= axes.right() + 1.0f);
|
||||
PLOT_CHECK(static_cast<plot::f32>(lo_y) >= axes.y - 1.0f);
|
||||
PLOT_CHECK(static_cast<plot::f32>(hi_y) <= axes.bottom() + 1.0f);
|
||||
|
||||
// The sweep is on the row its own transform maps y = 0 to, in the trace's own colour: the cell's
|
||||
// one sweep is spread over the block of device pixels it was magnified into, so what is asserted
|
||||
// is that the ink is *there* -- the brightest row of the rect is that row -- and that its hue is
|
||||
// the trace's rather than the background's.
|
||||
const plot::u32 mid_x = static_cast<plot::u32>(p.x_transform(ax).to_screen_x(0.5));
|
||||
const plot::u32 trace_row = static_cast<plot::u32>(p.y_transform(ay).to_screen_y(0.0));
|
||||
// The background to read the ink against is a row of the *without* frame away from the sweep's
|
||||
// own: the trace itself is drawn there (the density is off in that frame), so the trace's own row
|
||||
// is the trace's colour and not a background at all.
|
||||
const plot::u32 blank_row = trace_row + 40u < h ? trace_row + 40u : trace_row - 40u;
|
||||
const int bg_g = at(without, mid_x, blank_row, 1);
|
||||
int band_g = 0;
|
||||
int band_y = 0;
|
||||
for (plot::u32 y = static_cast<plot::u32>(axes.y); y < static_cast<plot::u32>(axes.bottom()); ++y) {
|
||||
const int g = at(capped, mid_x, y, 1);
|
||||
if (g > band_g) {
|
||||
band_g = g;
|
||||
band_y = static_cast<int>(y);
|
||||
}
|
||||
}
|
||||
PLOT_CHECK(band_g > bg_g + 40); // the sweep is on screen at all
|
||||
PLOT_CHECK(at(capped, mid_x, trace_row, 0) < 40); // ... in the trace's own hue ...
|
||||
PLOT_CHECK(at(capped, mid_x, trace_row, 1) > bg_g + 40);
|
||||
PLOT_CHECK(std::abs(band_y - static_cast<int>(trace_row)) <= 2);
|
||||
|
||||
std::printf("[density gl] capped grid=%ux%u cells=%zu of a %.0fx%.0f axes: changed cols=%u"
|
||||
" brightest row=%d (transform says %u), green=%d against bg=%d\n",
|
||||
grid.columns(), grid.rows(), grid.cell_count(), axes.w, axes.h, changed_cols, band_y,
|
||||
trace_row, band_g, bg_g);
|
||||
ctx.destroy();
|
||||
}
|
||||
Reference in new issue
Block a user