Skip to content

Commit ce71b5c

Browse files
committed
extract scalign logic to a separate function
1 parent 925f4ee commit ce71b5c

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

src/logic/index.zig

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -673,29 +673,21 @@ pub fn updateCache() void {
673673
1 / shared.render_scale,
674674
true,
675675
);
676-
const init_width = bounds[0].distance(bounds[1]) * shared.render_scale; // * shared.render_scale to revert to logical scale, nothing screen/camera/zoom related
677-
const initial_size = texture_size.get_size(bounds);
678-
const init_cache_scale = initial_size.w / init_width;
679676

680-
// Cost control: scale down texture if blur cost is too high
681-
const initial_sigma = types.Point{
682-
.x = filter.gaussianBlur.x * init_cache_scale,
683-
.y = filter.gaussianBlur.y * init_cache_scale,
684-
};
685-
const size, const sigma = texture_size.get_safe_blur_dims(
686-
initial_size,
687-
initial_sigma,
677+
const size, const sigma, const cache_scale = texture_size.get_safe_blur_dims(
678+
bounds,
679+
filter.gaussianBlur,
688680
);
689681

690682
if (size.w < 1.001 or size.h < 1.001) continue;
691683
// just to make sure device.createTexture won't round number down to 0
692684

693-
shape.cache_scale = size.w / init_width;
685+
shape.cache_scale = cache_scale;
694686

695687
const extra_padding = shape.getFilterMargin();
696688
const scaled_extra_padding = types.Point{
697-
.x = extra_padding.x * shape.cache_scale,
698-
.y = extra_padding.y * shape.cache_scale,
689+
.x = extra_padding.x * cache_scale,
690+
.y = extra_padding.y * cache_scale,
699691
};
700692
const bb = bounding_box.BoundingBox{
701693
.min_x = -scaled_extra_padding.x,

src/logic/texture_size.zig

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,36 @@ pub fn get_size(bounds: [4]PointUV) TextureSize {
4242
}
4343

4444
const MAX_COST = 90050924; // it's just chosen base on my own preferences
45-
// returns new safe size and new sigma
46-
pub fn get_safe_blur_dims(tex_size: TextureSize, scaled_sigma: Point) struct { TextureSize, Point } {
45+
// returns new safe size, new sigma and cache scale
46+
pub fn get_safe_blur_dims(bounds: [4]PointUV, gaussianBlur: Point) struct { TextureSize, Point, f32 } {
47+
var size = get_size(bounds);
48+
const init_width = bounds[0].distance(bounds[1]) * shared.render_scale; // * shared.render_scale to revert to logical scale, nothing screen/camera/zoom related
49+
50+
const init_cache_scale = size.w / init_width;
51+
var sigma = Point{
52+
.x = gaussianBlur.x * init_cache_scale,
53+
.y = gaussianBlur.y * init_cache_scale,
54+
};
4755

4856
// Cost control: scale down texture if blur cost is too high
49-
var new_sigma = Point{ .x = scaled_sigma.x, .y = scaled_sigma.y };
5057
// in the future would be nice to measure speed of the blur and base on that calculate MAX_COST
51-
var new_tex_size = TextureSize{ .w = tex_size.w, .h = tex_size.h };
52-
const pixels = tex_size.w * tex_size.h;
53-
const cost = 3 * new_sigma.x * pixels + 3 * new_sigma.y * pixels;
58+
const pixels = size.w * size.h;
59+
const cost = 3 * sigma.x * pixels + 3 * sigma.y * pixels;
5460

5561
if (cost > MAX_COST) {
5662
const scale_down = std.math.pow(f32, cost / MAX_COST, 1.0 / 3.0); // Cube root
57-
new_tex_size.w /= scale_down;
58-
new_tex_size.h /= scale_down;
63+
size.w /= scale_down;
64+
size.h /= scale_down;
5965

6066
// Scale both texture and sigma proportionally
61-
new_sigma.x /= scale_down;
62-
new_sigma.y /= scale_down;
67+
sigma.x /= scale_down;
68+
sigma.y /= scale_down;
6369

6470
// Verify new cost
6571
// const new_pixels = size.w * size.h;
6672
// const new_cost = 3 * sigma_x * new_pixels + 3 * sigma_y * new_pixels;
6773
// std.debug.print("prev cost: {d}\n new cost: {d}\n target: {d}\n", .{ cost, new_cost, MAX_COST });
6874
}
6975

70-
return .{ new_tex_size, new_sigma };
76+
return .{ size, sigma, size.w / init_width };
7177
}

0 commit comments

Comments
 (0)