-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblur.cpp
More file actions
executable file
·140 lines (126 loc) · 4.02 KB
/
Copy pathblur.cpp
File metadata and controls
executable file
·140 lines (126 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "blur.hpp"
#include "blur/math.hpp"
#include <cstddef>
#include <span>
#include <utility>
void cpuApply(std::span<const uint8_t> input,
std::span<uint8_t> output,
int w, int h, float radius, int downscale, int type);
Blur::Blur(Hardware backend_in) : backend(backend_in) {}
Blur::~Blur() { release(); }
void Blur::process(int x, int y, int w, int h, float radius, int downscale) {
x_ = x;
y_ = y;
w_ = w;
h_ = h;
radius_ = radius;
downscale_ = downscale;
apply();
}
void Blur::process(const Rect& r) {
process(r.x, r.y, r.w, r.h, r.radius, r.downscale);
}
#if defined(IMGUI_VERSION) && !defined(BLUR_NO_IMGUI)
void Blur::process(ImDrawList* draw, float corner_radius, float radius, int downscale, Hardware hw) {
if (!draw) {
return;
}
backend = hw;
ImVec2 pos = ImGui::GetWindowPos();
ImVec2 size = ImGui::GetWindowSize();
process(static_cast<int>(pos.x), static_cast<int>(pos.y),
static_cast<int>(size.x), static_cast<int>(size.y),
radius, downscale);
draw->AddImageRounded(
reinterpret_cast<ImTextureID>(static_cast<intptr_t>(tex)),
pos,
ImVec2(pos.x + size.x, pos.y + size.y),
ImVec2(0.0f, 1.0f),
ImVec2(1.0f, 0.0f),
IM_COL32(255, 255, 255, 255),
corner_radius,
ImDrawFlags_RoundCornersAll
);
}
void Blur::process(const ImRect& rect, float radius, int downscale) {
process(static_cast<int>(rect.Min.x), static_cast<int>(rect.Min.y),
static_cast<int>(rect.Max.x - rect.Min.x),
static_cast<int>(rect.Max.y - rect.Min.y),
radius, downscale);
}
void Blur::process(const ImVec2& pos, const ImVec2& size, float radius, int downscale) {
process(static_cast<int>(pos.x), static_cast<int>(pos.y),
static_cast<int>(size.x), static_cast<int>(size.y),
radius, downscale);
}
#endif
void Blur::release() {
gpuRelease(*this);
if (tex) {
glDeleteTextures(1, &tex);
tex = 0;
}
cpu_input_.reset();
cpu_output_.reset();
cpu_size_ = 0;
cpu_ready_ = false;
}
void Blur::reset() {
release();
x_ = 0;
y_ = 0;
w_ = 0;
h_ = 0;
width_ = 0;
height_ = 0;
blur_w_ = 0;
blur_h_ = 0;
}
void Blur::ensure() {
if (backend == Hardware::GPU) {
gpuEnsure(*this);
}
if (backend == Hardware::CPU || !cpu_ready_) {
if (tex == 0) {
glGenTextures(1, &tex);
}
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
if (width_ > 0 && height_ > 0) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width_, height_, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
}
cpu_ready_ = true;
}
}
void Blur::apply() {
if (w_ <= 0 || h_ <= 0) [[unlikely]] {
return;
}
if (width_ != w_ || height_ != h_) {
width_ = w_;
height_ = h_;
}
ensure();
if (backend == Hardware::CPU) {
size_t required = static_cast<size_t>(w_) * static_cast<size_t>(h_) * 4u;
if (cpu_size_ != required) {
cpu_input_ = std::make_unique<uint8_t[]>(required);
cpu_output_ = std::make_unique<uint8_t[]>(required);
cpu_size_ = required;
}
glReadPixels(x_, y_, w_, h_, GL_RGBA, GL_UNSIGNED_BYTE, cpu_input_.get());
cpuApply(
std::span<const uint8_t>(cpu_input_.get(), cpu_size_),
std::span<uint8_t>(cpu_output_.get(), cpu_size_),
w_, h_, radius_, downscale_,
static_cast<int>(std::to_underlying(type))
);
glBindTexture(GL_TEXTURE_2D, tex);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w_, h_, GL_RGBA, GL_UNSIGNED_BYTE, cpu_output_.get());
return;
}
gpuApply(*this);
}