Skip to content

Commit 4513632

Browse files
committed
修复pl2d的bug
1 parent c5720ee commit 4513632

File tree

9 files changed

+49
-68
lines changed

9 files changed

+49
-68
lines changed

include/define/config/plui.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
#define BETTER_COLOR_INTERPOLATE 1
4747
// 是否使用优化的插值算法
4848
// 目前并没有实现
49-
#define FAST_COLOR_INTERPOLATE 1
49+
#define FAST_COLOR_INTERPOLATE 0
5050

5151
#define COLOR_FASTIST_MIX 0 // 开启最快的颜色混合 (有误差)
5252
#define COLOR_FAST_MIX 0 // 开启更快的颜色混合 (不支持目标透明)

include/pl2d/pixel/mix.hpp

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,21 @@ namespace pl2d {
2020

2121
// 假如源和目标都没有透明度
2222
template <BasePixelTemplate>
23-
void BasePixelT::mix_ratio(const BasePixelT &s, T k) {
24-
T2 sw = k;
25-
T2 dw = T_MAX_ - k;
26-
r = (r * dw + s.r * sw) / T_MAX_;
27-
g = (g * dw + s.g * sw) / T_MAX_;
28-
b = (b * dw + s.b * sw) / T_MAX_;
29-
a = (a * dw + s.a * sw) / T_MAX_;
30-
}
31-
32-
// 假如源和目标都没有透明度
33-
template <BasePixelTemplate>
34-
auto BasePixelT::mix_ratio(const BasePixelT &c1, const BasePixelT &c2, T k) -> BasePixelT {
35-
T2 w1 = k;
36-
T2 w2 = T_MAX_ - k;
37-
return BasePixelT{
38-
(T)((c1.r * w1 + c2.r * w2) / T_MAX_),
39-
(T)((c1.g * w1 + c2.g * w2) / T_MAX_),
40-
(T)((c1.b * w1 + c2.b * w2) / T_MAX_),
41-
(T)((c1.a * w1 + c2.a * w2) / T_MAX_),
42-
};
23+
void BasePixelT::mix_ratio(const BasePixelT &s, FT k) {
24+
r = (FT)r * (1 - k) + (FT)s.r * k;
25+
g = (FT)g * (1 - k) + (FT)s.g * k;
26+
b = (FT)b * (1 - k) + (FT)s.b * k;
27+
a = (FT)a * (1 - k) + (FT)s.a * k;
4328
}
4429

4530
// 假如源和目标都没有透明度
4631
template <BasePixelTemplate>
47-
template <typename U>
48-
requires(cpp::is_float<U> && !std::is_same_v<T, U>)
49-
auto BasePixelT::mix_ratio(const BasePixelT &c1, const BasePixelT &c2, U k) -> BasePixelT {
32+
auto BasePixelT::mix_ratio(const BasePixelT &c1, const BasePixelT &c2, FT k) -> BasePixelT {
5033
return BasePixelT{
51-
(T)((U)c1.r * k + (U)c2.r * (1 - k)),
52-
(T)((U)c1.g * k + (U)c2.g * (1 - k)),
53-
(T)((U)c1.b * k + (U)c2.b * (1 - k)),
54-
(T)((U)c1.a * k + (U)c2.a * (1 - k)),
34+
(T)((FT)c1.r * (1 - k) + (FT)c2.r * k),
35+
(T)((FT)c1.g * (1 - k) + (FT)c2.g * k),
36+
(T)((FT)c1.b * (1 - k) + (FT)c2.b * k),
37+
(T)((FT)c1.a * (1 - k) + (FT)c2.a * k),
5538
};
5639
}
5740

include/pl2d/pixel/pixel.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,8 @@ struct BasePixel {
126126
CONST auto diff(const BasePixel &p) -> T;
127127

128128
// 按比例进行颜色混合函数
129-
HOT void mix_ratio(const BasePixel &s, T k);
130-
HOT static auto mix_ratio(const BasePixel &c1, const BasePixel &c2, T k) -> BasePixel;
131-
template <typename U>
132-
requires(cpp::is_float<U> && !std::is_same_v<T, U>)
133-
HOT static auto mix_ratio(const BasePixel &c1, const BasePixel &c2, U k) -> BasePixel;
129+
HOT void mix_ratio(const BasePixel &s, FT k);
130+
HOT static auto mix_ratio(const BasePixel &c1, const BasePixel &c2, FT k) -> BasePixel;
134131
// 背景色不透明的混合函数
135132
HOT void mix_opaque(const BasePixel &s);
136133
HOT static auto mix_opaque(const BasePixel &c1, const BasePixel &c2) -> BasePixel;

include/plds/server/var.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ dlimport i32 mouse_y;
99
dlimport i32 screen_w;
1010
dlimport i32 screen_h;
1111

12-
dlimport pl2d::FrameBuffer screen_fb;
13-
dlimport pl2d::TextureB screen_tex;
12+
// dlimport pl2d::FrameBuffer screen_fb;
13+
// dlimport pl2d::TextureB screen_tex;
1414

1515
} // namespace plds

src/pl2d/fb/flush-bgr.hpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ template <>
1515
INLINE void fb_flush_pix<PixFmt::BGR>(FrameBuffer &fb, const pl2d::TextureF &tex, u32 x, u32 y) {
1616
byte *_rest fb_p = &fb.pix8[0][y * fb.pitch + x * fb.padding];
1717
auto *_rest tex_p = &tex.pixels[y * tex.pitch + x];
18-
fb_p[0] = tex_p->b * 255.f;
19-
fb_p[1] = tex_p->g * 255.f;
20-
fb_p[2] = tex_p->r * 255.f;
18+
fb_p[0] = cpp::clamp(tex_p->b, 0.f, 1.f) * 255.f;
19+
fb_p[1] = cpp::clamp(tex_p->g, 0.f, 1.f) * 255.f;
20+
fb_p[2] = cpp::clamp(tex_p->r, 0.f, 1.f) * 255.f;
2121
}
2222
template <>
2323
INLINE void fb_copy_to_pix<PixFmt::BGR>(const FrameBuffer &fb, pl2d::TextureB &tex, u32 x, u32 y) {
@@ -51,10 +51,10 @@ template <>
5151
INLINE void fb_flush_pix<PixFmt::BGRA>(FrameBuffer &fb, const pl2d::TextureF &tex, u32 x, u32 y) {
5252
byte *_rest fb_p = &fb.pix8[0][y * fb.pitch + x * fb.padding];
5353
auto *_rest tex_p = &tex.pixels[y * tex.pitch + x];
54-
fb_p[0] = tex_p->b * 255.f;
55-
fb_p[1] = tex_p->g * 255.f;
56-
fb_p[2] = tex_p->r * 255.f;
57-
fb_p[3] = tex_p->a * 255.f;
54+
fb_p[0] = cpp::clamp(tex_p->b, 0.f, 1.f) * 255.f;
55+
fb_p[1] = cpp::clamp(tex_p->g, 0.f, 1.f) * 255.f;
56+
fb_p[2] = cpp::clamp(tex_p->r, 0.f, 1.f) * 255.f;
57+
fb_p[3] = cpp::clamp(tex_p->a, 0.f, 1.f) * 255.f;
5858
}
5959
template <>
6060
INLINE void fb_copy_to_pix<PixFmt::BGRA>(const FrameBuffer &fb, pl2d::TextureB &tex, u32 x, u32 y) {
@@ -88,10 +88,10 @@ template <>
8888
INLINE void fb_flush_pix<PixFmt::ABGR>(FrameBuffer &fb, const pl2d::TextureF &tex, u32 x, u32 y) {
8989
byte *_rest fb_p = &fb.pix8[0][y * fb.pitch + x * fb.padding];
9090
auto *_rest tex_p = &tex.pixels[y * tex.pitch + x];
91-
fb_p[0] = tex_p->a * 255.f;
92-
fb_p[1] = tex_p->b * 255.f;
93-
fb_p[2] = tex_p->g * 255.f;
94-
fb_p[3] = tex_p->r * 255.f;
91+
fb_p[0] = cpp::clamp(tex_p->a, 0.f, 1.f) * 255.f;
92+
fb_p[1] = cpp::clamp(tex_p->b, 0.f, 1.f) * 255.f;
93+
fb_p[2] = cpp::clamp(tex_p->g, 0.f, 1.f) * 255.f;
94+
fb_p[3] = cpp::clamp(tex_p->r, 0.f, 1.f) * 255.f;
9595
}
9696
template <>
9797
INLINE void fb_copy_to_pix<PixFmt::ABGR>(const FrameBuffer &fb, pl2d::TextureB &tex, u32 x, u32 y) {

src/pl2d/fb/flush-rgb.hpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ template <>
1515
INLINE void fb_flush_pix<PixFmt::RGB>(FrameBuffer &fb, const pl2d::TextureF &tex, u32 x, u32 y) {
1616
byte *_rest fb_p = &fb.pix8[0][y * fb.pitch + x * fb.padding];
1717
auto *_rest tex_p = &tex.pixels[y * tex.pitch + x];
18-
fb_p[0] = tex_p->r * 255.f;
19-
fb_p[1] = tex_p->g * 255.f;
20-
fb_p[2] = tex_p->b * 255.f;
18+
fb_p[0] = cpp::clamp(tex_p->r, 0.f, 1.f) * 255.f;
19+
fb_p[1] = cpp::clamp(tex_p->g, 0.f, 1.f) * 255.f;
20+
fb_p[2] = cpp::clamp(tex_p->b, 0.f, 1.f) * 255.f;
2121
}
2222
template <>
2323
INLINE void fb_copy_to_pix<PixFmt::RGB>(const FrameBuffer &fb, pl2d::TextureB &tex, u32 x, u32 y) {
@@ -51,10 +51,10 @@ template <>
5151
INLINE void fb_flush_pix<PixFmt::RGBA>(FrameBuffer &fb, const pl2d::TextureF &tex, u32 x, u32 y) {
5252
byte *_rest fb_p = &fb.pix8[0][y * fb.pitch + x * fb.padding];
5353
auto *_rest tex_p = &tex.pixels[y * tex.pitch + x];
54-
fb_p[0] = tex_p->r * 255.f;
55-
fb_p[1] = tex_p->g * 255.f;
56-
fb_p[2] = tex_p->b * 255.f;
57-
fb_p[3] = tex_p->a * 255.f;
54+
fb_p[0] = cpp::clamp(tex_p->r, 0.f, 1.f) * 255.f;
55+
fb_p[1] = cpp::clamp(tex_p->g, 0.f, 1.f) * 255.f;
56+
fb_p[2] = cpp::clamp(tex_p->b, 0.f, 1.f) * 255.f;
57+
fb_p[3] = cpp::clamp(tex_p->a, 0.f, 1.f) * 255.f;
5858
}
5959
template <>
6060
INLINE void fb_copy_to_pix<PixFmt::RGBA>(const FrameBuffer &fb, pl2d::TextureB &tex, u32 x, u32 y) {
@@ -88,10 +88,10 @@ template <>
8888
INLINE void fb_flush_pix<PixFmt::ARGB>(FrameBuffer &fb, const pl2d::TextureF &tex, u32 x, u32 y) {
8989
byte *_rest fb_p = &fb.pix8[0][y * fb.pitch + x * fb.padding];
9090
auto *_rest tex_p = &tex.pixels[y * tex.pitch + x];
91-
fb_p[0] = tex_p->a * 255.f;
92-
fb_p[1] = tex_p->r * 255.f;
93-
fb_p[2] = tex_p->g * 255.f;
94-
fb_p[3] = tex_p->b * 255.f;
91+
fb_p[0] = cpp::clamp(tex_p->a, 0.f, 1.f) * 255.f;
92+
fb_p[1] = cpp::clamp(tex_p->r, 0.f, 1.f) * 255.f;
93+
fb_p[2] = cpp::clamp(tex_p->g, 0.f, 1.f) * 255.f;
94+
fb_p[3] = cpp::clamp(tex_p->b, 0.f, 1.f) * 255.f;
9595
}
9696
template <>
9797
INLINE void fb_copy_to_pix<PixFmt::ARGB>(const FrameBuffer &fb, pl2d::TextureB &tex, u32 x, u32 y) {

src/pl2d/pix/it.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
namespace pl2d {
55

6-
// #if BETTER_COLOR_INTERPOLATE && FAST_COLOR_INTERPOLATE
7-
// # warning "启用 BETTER_COLOR_INTERPOLATE 的情况下启用 FAST_COLOR_INTERPOLATE 也快不到哪去"
8-
// #endif
6+
#if BETTER_COLOR_INTERPOLATE && FAST_COLOR_INTERPOLATE
7+
# warning "启用 BETTER_COLOR_INTERPOLATE 的情况下启用 FAST_COLOR_INTERPOLATE 也快不到哪去"
8+
#endif
99

1010
#if 0
1111
template <typename T>
@@ -31,7 +31,7 @@ void color_lerp(T *buf, size_t n, T src, T dst) {
3131
}
3232

3333
// 我不知道这个是否真的有效
34-
#if FAST_COLOR_INTERPOLATE && 0
34+
#if FAST_COLOR_INTERPOLATE
3535
template <>
3636
void color_lerp<PixelB>(PixelB *buf, size_t n, PixelB src, PixelB dst) {
3737
if (buf == null || n == 0) return;

src/pl2d/tex/glow.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ namespace pl2d {
66
template <typename T>
77
auto BaseTexture<T>::glow() -> BaseTexture & {
88
val tex = this->copy();
9-
this->minus_clamp(1).mulby(4).gaussian_blur(11, 5).add_clamp(*tex);
9+
for (usize i = 0; i < size; i++) {
10+
pixels[i].r = pixels[i].r > 1 ? pixels[i].r : 0;
11+
pixels[i].g = pixels[i].g > 1 ? pixels[i].g : 0;
12+
pixels[i].b = pixels[i].b > 1 ? pixels[i].b : 0;
13+
}
14+
this->gaussian_blur(11, 5).add_clamp(*tex);
1015
delete tex;
1116
return *this;
1217
}

test/plui/main.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,6 @@ auto init(void *buffer, u32 width, u32 height, pl2d::PixFmt fmt) -> int {
7272
std::cout << "load ../resource/test.qoi failed" << std::endl;
7373
exit(1);
7474
}
75-
val texf32 = image_tex.copy_f32();
76-
texf32->mulby(1.5).glow();
77-
image_tex.copy_from(*texf32);
78-
delete texf32;
7975

8076
return on::screen_resize(buffer, width, height, fmt);
8177
}
@@ -88,8 +84,8 @@ void flush() {
8884
float i = (f32)nframe * .01f;
8985
tex.fill(pl2d::PixelF::lab(.8f, cpp::cos(i) * .1f, cpp::sin(i) * .1f));
9086
tex.transform([](auto &pix, i32 x, i32 y) {
91-
f32 k = cpp::sin((x - y + nframe * 4) / 25.f) / 5.f + .8f;
92-
if ((x + y) / 25 % 2 == 0) pix.mix_ratio(pl2d::PixelF{k, k, k}, 64);
87+
val k = cpp::sin((x - y + nframe * 4) / 25.f) / 5.f + .8f + .5f;
88+
if ((x + y) / 25 % 2 == 0) pix.mix_ratio(pl2d::PixelF{k, k, k}, 0.25);
9389
});
9490
frame_tex[nframe / 60 % 19].paste_to_mix(tex, 20, 20);
9591
image_tex.paste_to_mix(tex, 900, 0);

0 commit comments

Comments
 (0)