8000 [Experiment] Clip/cull (partially-)offscreen glyphs by rcombs · Pull Request #874 · libass/libass · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[Experiment] Clip/cull (partially-)offscreen glyphs #874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

rcombs
Copy link
Member
@rcombs rcombs commented Mar 31, 2025

This vastly improves performance on some samples by culling (parts of) glyphs that are far enough offscreen that they can't possibly affect our final output. It's a very large strict improvement on things like "star wars title crawls", but results in increased CPU usage (though still generally better worst-frame performance and fewer framedrops) on some simple 2D-scrolling signs (since it results in more cache misses when the same glyph with the same parameters is drawn with multiple clips).

TODO:

  • ass_blur_padding() is trash and should be rewritten (@MrSmile, any interest?)
  • USE_RECT_CLIP is currently disabled because I expect it would worsen clip-sliced gradients
  • Probably would be good to have a "cache peek" routine to check if an unclipped bitmap already exists in cache and fetch it immediately (this would mean we'd also want a second clip in ass_composite_construct)
  • There might be some heuristics we could use to improve behavior on 2D-scroll cases? Maybe something like "never actually render a partial glyph if position is animated but rotation isn't; instead, make a decision on whether to reject rasterizing at all [if fully offscreen], or to re-request the unclipped glyph from cache"?

rcombs added 3 commits March 31, 2025 21:04
This function sucks and should be rewritten by someone who understands the blur math better.
@rcombs rcombs requested review from astiob, TheOneric and MrSmile March 31, 2025 12:21
@MrSmile
Copy link
Member
MrSmile commented Mar 31, 2025

I'll do in-depth review later, only couple of thoughts now.

  • There's no need to exactly estimate blur border padding, something like A * sqrt(r2) + B with some constants should suffice. Also it would be good to have clipping blur (where result image can be smaller than input) in the future.
  • Exact padding is detrimental for cache, clip rectangle should be snapped to some large (~256 pixel wide) grid. A good place for such snapping is quantize_transform(). I'm planing to do viewport-aware quantization for perspective coefficients in the future (but math for that is quite hard).

@rcombs
Copy link
Member Author
rcombs commented Mar 31, 2025

Pushed grid-tile-snapping; currently I'm using a 64-pixel grid, and it does seem to help a bit; I didn't see a substantial difference between a 64 and a 256-pixel grid on some quick-and-dirty testing, but I'd imagine it'll vary depending on content in practice and we should test a wider variety of samples to determine what works best.

If you can write a routine that calculates a blur padding estimate guaranteed to always be ≥ what's required, I'd very much appreciate it. I think it'd probably be best to compute that alongside the blur value itself, and then store it in RenderContext so we don't have to redo the math on every glyph (even if it's cheap, no sense doing it more than we have to).

Copy link
Member
@MrSmile MrSmile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed grid-tile-snapping; currently I'm using a 64-pixel grid, and it does seem to help a bit;

I think it would be better to do a two-stage snapping: first on composite level and second on individual glyph level. Snapping only on glyph level can pollute composite cache (for example, for long scrolling text) and it can be costly in case of heavy blur. I've done something similar for subpixel shift quantization.

If you can write a routine that calculates a blur padding estimate guaranteed to always be ≥ what's required

Padding generated by the blur algorithm is not a strict requirement, it's mostly due to the padding requirements of individual stages. So we can choose arbitrary padding that doesn't introduce visible artifacts. I think (int) (4 * sqrt(r2) + 1) is good enough (internal algorithm padding is between 4 * sqrt(r2) and 6 * sqrt(r2)).

#define RECT(member) \
a->member.x_min == b->member.x_min && a->member.y_min == b->member.y_min && \
a->member.x_max == b->member.x_max && a->member.y_max == b->member.y_max &&

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other entries do not have empty lines.

@@ -167,6 +168,19 @@ bool ass_outline_to_bitmap(RenderContext *state, Bitmap *bm,
int32_t y_min = (rst->bbox.y_min - 1) >> 6;
int32_t x_max = (rst->bbox.x_max + 127) >> 6;
int32_t y_max = (rst->bbox.y_max + 127) >> 6;


if (clip->x_min != 0 || clip->y_min != 0 || clip->x_max != 0 || clip->y_max != 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to use default no-clip rectangle {INT32_MIN, INT32_MIN, INT32_MAX, INT32_MAX} instead of {0, 0, 0, 0} so that condition could be eliminated.

render_priv->settings.frame_height,
};

#ifdef USE_RECT_CLIP
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With grid snapping it should be safe to remove USE_RECT_CLIP.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, retesting the perf now would be good

}
#endif

bool potentially_clipped = (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to do the other way around: calculate clip rectangle unconditionally, but crop by outline cbox before cache request.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, can we actually skip the cache request altogether if the clip rectangle doesn't intersect with the cbox?

((qr[0] + key->outline->cbox.x_max) > (int64_t)((uint64_t)clip->x_max << SUBPIXEL_ORDER)) ||
((qr[1] + key->outline->cbox.y_max) > (int64_t)((uint64_t)clip->y_max << SUBPIXEL_ORDER))
))
clip = NULL;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to set clip to shifted outline cbox instead of using it as flag.

Actually, using outline cbox is incorrect, it should be transformed outline cbox.
As first approximation, we could transform 4 corners of source outline cbox and use it.

Copy link
Member
@TheOneric TheOneric left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8000

There might be some heuristics we could use to improve behavior on 2D-scroll cases? Maybe something like "never actually render a partial glyph if position is animated but rotation isn't; instead, make a decision on whether to reject rasterizing at all [if fully offscreen], or to re-request the unclipped glyph from cache"?

I like the idea. How about always rendering each individual glyph fully (or not at all), unless it exceeds a certain size threshold (too costly) or rotation is animated (unlikely the cached version actually will be used again)?

In pathological cases even an unanimated rotation might lead to very large bitmaps for a single glyph (rotation around x or y which puts the glyph close to the camera plane just outside the render frame). While perhaps less likely (scripted) scale effects may achieve the same without rotation.

@@ -1372,6 +1379,8 @@ static void calc_transform_matrix(RenderContext *state,
}
}

int ass_be_padding(int be);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should use a proper header

render_priv->settings.frame_height,
};

#ifdef USE_RECT_CLIP
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, retesting the perf now would be good

8000
clip_rect.x_min -= (shadow_x + 63) >> 6;
else
clip_rect.x_max -= (shadow_x - 63) >> 6;
if (shadow_y > 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: different if/else constructs should be separated by an empty line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0