Skip to content

Commit 23a9903

Browse files
authored
Merge pull request #1 from korarei/refactor/samples
Refactor: Update required samples calculation
2 parents 504b0fc + 759e529 commit 23a9903

File tree

5 files changed

+36
-44
lines changed

5 files changed

+36
-44
lines changed

dll_src/main.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,9 @@ calc_delta(const Param &param, Input &input, const std::array<std::uint32_t, 2>
5757
}
5858

5959
static std::array<int, 4>
60-
calc_size(Delta &delta, float amt, const Input &input, bool enable) noexcept {
60+
calc_size(Delta &delta, float amt, const Input &input) noexcept {
6161
std::array<int, 4> margin{};
6262

63-
if (!enable)
64-
return margin;
65-
6663
auto htm = delta.calc_htm(amt);
6764
auto c_prev = Vec3<float>(delta.get_center(), 1.0f);
6865
auto pos = (htm * c_prev).to_vec2() + input.pivot;
@@ -72,10 +69,10 @@ calc_size(Delta &delta, float amt, const Input &input, bool enable) noexcept {
7269
auto upper_left = static_cast<Vec2<int>>((diff - pos).ceil());
7370
auto lower_right = static_cast<Vec2<int>>((diff + pos).ceil());
7471

75-
margin[0] = std::max(margin[0], upper_left.get_y());
76-
margin[1] = std::max(margin[1], lower_right.get_y());
77-
margin[2] = std::max(margin[2], upper_left.get_x());
78-
margin[3] = std::max(margin[3], lower_right.get_x());
72+
margin[0] = std::max(upper_left.get_y(), 0);
73+
margin[1] = std::max(lower_right.get_y(), 0);
74+
margin[2] = std::max(upper_left.get_x(), 0);
75+
margin[3] = std::max(lower_right.get_x(), 0);
7976

8077
return margin;
8178
}
@@ -124,24 +121,33 @@ process_motion_blur(const CParam *c_param, const CInput *c_input, COutput *c_out
124121
if (flag) {
125122
const float amt = param.shutter_angle / 360.0f;
126123

124+
std::array<int, 4> margin{};
125+
std::uint32_t req_smp = 0u;
126+
std::uint32_t smp = 0u;
127+
127128
if (param.geo_cache && !input.frame)
128129
set_init_geo(param.ext, k);
129130

130131
auto delta = calc_delta(param, input, {k, (param.geo_cache == 2u && input.frame) ? 7u : input.frame});
131-
std::uint32_t smp = std::clamp(delta.calc_req_smp(amt, input.res), 0u, param.smp_lim - 1u);
132+
133+
if (delta.is_moved()) {
134+
margin = calc_size(delta, amt, input);
135+
req_smp = static_cast<std::uint32_t>(Vec2<int>(margin[2] + margin[3], margin[0] + margin[1]).norm(2));
136+
smp = std::min(req_smp, param.smp_lim - 1u);
137+
}
132138

133139
if (smp) {
134140
auto init_htm = delta.calc_htm(amt, smp, true);
135141
auto drift = delta.calc_drift(amt, smp);
136-
auto margin = calc_size(delta, amt, input, param.resize);
137142

138-
*c_output = COutput(margin, init_htm, drift, smp);
143+
*c_output = COutput(margin, init_htm, drift, smp, req_smp);
139144
} else {
140145
flag = false;
141146
*c_output = COutput();
142147
}
143-
} else
148+
} else {
144149
*c_output = COutput();
150+
}
145151

146152
if (param.geo_cache == 2u)
147153
if (auto geo = geo_map.read({k, 7u}); !geo || !geo->is_cached(input.frame))

dll_src/structs.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ struct COutput {
3434
float init_htm[3][3];
3535
float drift[2];
3636
int smp;
37+
int req_smp;
3738

3839
constexpr COutput() noexcept = default;
3940
constexpr COutput(const std::array<int, 4> &margin_, const Mat3<float> &init_htm_, const Vec2<float> &drift_,
40-
std::uint32_t smp_) noexcept :
41-
smp(smp_) {
41+
std::uint32_t smp_, std::uint32_t req_smp_) noexcept :
42+
smp(static_cast<int>(smp_)), req_smp(static_cast<int>(req_smp_)) {
4243
std::copy(margin_.begin(), margin_.end(), margin);
4344
for (std::size_t i = 0; i < 3; ++i) std::copy(init_htm_[i].begin(), init_htm_[i].end(), init_htm[i]);
4445
std::copy(drift_.begin(), drift_.end(), drift);
@@ -56,7 +57,7 @@ struct Param {
5657

5758
constexpr Param(const CParam &c_param) noexcept :
5859
shutter_angle(std::clamp(c_param.shutter_angle, 0.0f, 360.0f)),
59-
smp_lim(static_cast<std::uint32_t>(std::max(c_param.smp_lim, 0))),
60+
smp_lim(static_cast<std::uint32_t>(std::max(c_param.smp_lim, 1))),
6061
ext(static_cast<std::uint32_t>(std::clamp(c_param.ext, 0, 2))),
6162
resize(c_param.resize),
6263
geo_cache(static_cast<std::uint32_t>(std::clamp(c_param.geo_cache, 0, 2))),

dll_src/transform_utils.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ Delta::Delta(const Transform &from, const Transform &to) noexcept :
88
rel_center(from.get_center() - to.get_center()),
99
center_to(-to.get_center()),
1010
center_from(-from.get_center()),
11-
rel_dist(rel_pos.norm(2) + rel_center.norm(2)),
12-
flag(is_zero(rel_dist) && are_equal(rel_scale, 1.0f) && is_zero(rel_rot)) {}
11+
flag(is_zero(rel_pos.norm(2)) && is_zero(rel_center.norm(2)) && are_equal(rel_scale, 1.0f) && is_zero(rel_rot)) {}
1312

1413
Mat3<float>
1514
Delta::calc_htm(float amt, std::uint32_t smp, bool is_inv) const noexcept {
16-
float step_amt = smp > 1 ? amt / static_cast<float>(smp) : amt;
15+
float step_amt = smp > 1u ? amt / static_cast<float>(smp) : amt;
1716
float rot = rel_rot * step_amt;
1817
float scale = std::pow(rel_scale, step_amt);
1918
auto pos = rel_pos * step_amt;

dll_src/transform_utils.hpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ class Delta {
7979
[[nodiscard]] constexpr const Vec2<float> &get_center() const noexcept { return center_to; }
8080
[[nodiscard]] constexpr const bool is_moved() const noexcept { return !flag; }
8181

82-
// Calculate the required samples.
83-
[[nodiscard]] constexpr std::uint32_t calc_req_smp(float amt, const Vec2<float> &res) const noexcept;
84-
8582
// Calculate the HTM (Homogeneous Transformation Matrix).
8683
[[nodiscard]] Mat3<float> calc_htm(float amt = 1.0f, std::uint32_t smp = 1u, bool is_inv = false) const noexcept;
8784

@@ -90,23 +87,11 @@ class Delta {
9087
private:
9188
float rel_rot, rel_scale;
9289
Vec2<float> rel_pos, rel_center, center_to, center_from;
93-
float rel_dist;
9490
bool flag;
9591
};
9692

97-
inline constexpr std::uint32_t
98-
Delta::calc_req_smp(float amt, const Vec2<float> &res) const noexcept {
99-
if (flag)
100-
return 0u;
101-
102-
auto size = res + center_from.abs();
103-
float r = size.norm(2) * 0.5f;
104-
auto req_smps = Vec3<float>(rel_dist, (rel_scale - 1.0f) * r, rel_rot * r) * amt;
105-
return static_cast<std::uint32_t>(std::ceil(req_smps.norm(-1)));
106-
}
107-
10893
inline constexpr Vec2<float>
10994
Delta::calc_drift(float amt, std::uint32_t smp) const noexcept {
110-
float step_amt = smp > 1 ? amt / static_cast<float>(smp) : amt;
95+
float step_amt = smp > 1u ? amt / static_cast<float>(smp) : amt;
11196
return -rel_center * step_amt;
11297
}

scripts/ObjectMotionBlur_LK_template.anm2

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,9 @@ pcall(ffi.cdef, [[
6868
float init_htm[3][3];
6969
float drift[2];
7070
int smp;
71+
int req_smp;
7172
} Output;
7273

73-
typedef struct {
74-
float ox, oy, zoom, rz;
75-
} Geo;
76-
7774
int process_motion_blur(const Param *param, const Input *input, Output *output);
7875
]])
7976

@@ -136,12 +133,16 @@ if (state == 0) then
136133
local drift = output.drift
137134
local add = output.margin
138135

139-
obj.effect("領域拡張", "上", add[0], "下", add[1], "左", add[2], "右", add[3])
140-
obj.cx = obj.cx + (add[2] - add[3]) * 0.5 -- beta 11a
141-
obj.cy = obj.cy + (add[0] - add[1]) * 0.5 -- beta 11a
136+
local w_new, h_new = w, h
142137

143-
local w_new = w + add[2] + add[3]
144-
local h_new = h + add[0] + add[1]
138+
if (resize) then
139+
obj.effect("領域拡張", "上", add[0], "下", add[1], "左", add[2], "右", add[3])
140+
obj.cx = obj.cx + (add[2] - add[3]) * 0.5
141+
obj.cy = obj.cy + (add[0] - add[1]) * 0.5
142+
143+
w_new = w + add[2] + add[3]
144+
h_new = h + add[0] + add[1]
145+
end
145146

146147
obj.pixelshader("motion_blur", "object", "object", {
147148
htm[0][0], htm[0][1], htm[0][2], 0.0,
@@ -162,5 +163,5 @@ if (print_info) then
162163
Object ID : %d
163164
Index : %d
164165
Required Samples: %d
165-
]], obj_id, obj.index, output.smp))
166+
]], obj_id, obj.index, output.req_smp))
166167
end

0 commit comments

Comments
 (0)