dsh-watch-together 0.2.9 → 0.2.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +144 -0
- package/README.md +15 -5
- package/docs/INTERFACES.zh.md +29 -12
- package/dsh.plugin.json +12 -12
- package/examples/floating-chat/glass_capture.py +286 -0
- package/examples/floating-chat/glass_cpu.py +174 -0
- package/examples/floating-chat/glass_geometry.py +288 -0
- package/examples/floating-chat/glass_gpu.py +509 -0
- package/examples/floating-chat/glass_window.py +4 -7
- package/examples/floating-chat/glass_window_gpu.py +14 -516
- package/examples/floating-chat/requirements-glass.txt +5 -0
- package/examples/floating-chat/run-glass.cmd +5 -1
- package/examples/floating-chat/window.py +1210 -1932
- package/examples/floating-chat/window_widgets.py +288 -0
- package/package.json +115 -115
|
@@ -1,526 +1,24 @@
|
|
|
1
|
+
"""GPU 版悬浮窗入口:`python glass_window_gpu.py`。
|
|
1
2
|
|
|
2
|
-
|
|
3
|
+
这里只是把后端切成 GPU 就交给 `window.py` —— UI、交互、聊天流、状态机全部是
|
|
4
|
+
同一份代码,不存在「GPU 版另一个窗口」这种东西。
|
|
3
5
|
|
|
4
|
-
|
|
5
|
-
Only the per-frame capture -> blur -> warp -> lens composite is moved to the GPU.
|
|
6
|
-
"""
|
|
7
|
-
import ctypes
|
|
8
|
-
import os
|
|
9
|
-
import sys
|
|
10
|
-
import time
|
|
11
|
-
|
|
12
|
-
import moderngl
|
|
13
|
-
import numpy as np
|
|
14
|
-
from PIL import Image
|
|
15
|
-
|
|
16
|
-
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
17
|
-
sys.path.insert(0, HERE)
|
|
18
|
-
import glass_window as G
|
|
19
|
-
|
|
20
|
-
VERT = r"""
|
|
21
|
-
#version 330
|
|
22
|
-
in vec2 in_pos;
|
|
23
|
-
out vec2 v_uv;
|
|
24
|
-
void main() {
|
|
25
|
-
v_uv = in_pos * 0.5 + 0.5;
|
|
26
|
-
gl_Position = vec4(in_pos, 0.0, 1.0);
|
|
27
|
-
}
|
|
28
|
-
"""
|
|
29
|
-
|
|
30
|
-
FRAG_DOWN = r"""
|
|
31
|
-
#version 330
|
|
32
|
-
uniform sampler2D tex;
|
|
33
|
-
in vec2 v_uv;
|
|
34
|
-
out vec4 frag;
|
|
35
|
-
void main() {
|
|
36
|
-
vec4 c = texture(tex, v_uv);
|
|
37
|
-
frag = vec4(c.bgr, 1.0);
|
|
38
|
-
}
|
|
39
|
-
"""
|
|
40
|
-
|
|
41
|
-
FRAG_BLUR = r"""
|
|
42
|
-
#version 330
|
|
43
|
-
uniform sampler2D tex;
|
|
44
|
-
uniform vec2 texel;
|
|
45
|
-
uniform float sigma;
|
|
46
|
-
uniform vec2 dir;
|
|
47
|
-
in vec2 v_uv;
|
|
48
|
-
out vec4 frag;
|
|
49
|
-
void main() {
|
|
50
|
-
// Pair adjacent taps through a linear-filtered sample: halves the
|
|
51
|
-
// texture fetches while keeping the same Gaussian weights.
|
|
52
|
-
int radius = int(min(32.0, ceil(sigma * 3.0)));
|
|
53
|
-
float total = 0.0;
|
|
54
|
-
vec3 sum = vec3(0.0);
|
|
55
|
-
for (int i = -32; i <= 32; i += 2) {
|
|
56
|
-
if (i < -radius) continue;
|
|
57
|
-
float fi = float(i);
|
|
58
|
-
float w0 = exp(-0.5 * (fi * fi) / (sigma * sigma));
|
|
59
|
-
int j = i + 1;
|
|
60
|
-
if (j > radius) {
|
|
61
|
-
sum += texture(tex, v_uv + dir * texel * fi).rgb * w0;
|
|
62
|
-
total += w0;
|
|
63
|
-
} else {
|
|
64
|
-
float fj = float(j);
|
|
65
|
-
float w1 = exp(-0.5 * (fj * fj) / (sigma * sigma));
|
|
66
|
-
float x = (fi * w0 + fj * w1) / (w0 + w1);
|
|
67
|
-
sum += texture(tex, v_uv + dir * texel * x).rgb * (w0 + w1);
|
|
68
|
-
total += w0 + w1;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
frag = vec4(sum / total, 1.0);
|
|
72
|
-
}
|
|
73
|
-
"""
|
|
74
|
-
|
|
75
|
-
FRAG_WARP = r"""
|
|
76
|
-
#version 330
|
|
77
|
-
uniform sampler2D src;
|
|
78
|
-
uniform sampler2D coord;
|
|
79
|
-
uniform vec2 offset;
|
|
80
|
-
uniform vec2 src_size;
|
|
81
|
-
in vec2 v_uv;
|
|
82
|
-
out vec4 frag;
|
|
83
|
-
void main() {
|
|
84
|
-
vec2 c = texelFetch(coord, ivec2(gl_FragCoord.xy), 0).rg + offset;
|
|
85
|
-
ivec2 p = ivec2(floor(c + 0.5));
|
|
86
|
-
p = clamp(p, ivec2(0), ivec2(src_size) - 1);
|
|
87
|
-
frag = texelFetch(src, p, 0);
|
|
88
|
-
}
|
|
89
|
-
"""
|
|
90
|
-
|
|
91
|
-
FRAG_BOX = r"""
|
|
92
|
-
#version 330
|
|
93
|
-
uniform sampler2D warped;
|
|
94
|
-
uniform sampler2D strongw;
|
|
95
|
-
uniform sampler2D raw;
|
|
96
|
-
uniform sampler2D alpha;
|
|
97
|
-
uniform sampler2D edge;
|
|
98
|
-
uniform sampler2D coord;
|
|
99
|
-
uniform vec2 offset;
|
|
100
|
-
uniform vec2 src_size;
|
|
101
|
-
in vec2 v_uv;
|
|
102
|
-
out vec4 frag;
|
|
103
|
-
void main() {
|
|
104
|
-
ivec2 p = ivec2(gl_FragCoord.xy);
|
|
105
|
-
vec3 base = texelFetch(warped, p, 0).rgb;
|
|
106
|
-
vec3 strong = texelFetch(strongw, p, 0).rgb;
|
|
107
|
-
float a = texelFetch(alpha, p, 0).r;
|
|
108
|
-
float e = texelFetch(edge, p, 0).r;
|
|
109
|
-
vec2 ec = texelFetch(coord, p, 0).rg + offset;
|
|
110
|
-
ivec2 ep = clamp(ivec2(floor(ec + 0.5)), ivec2(0), ivec2(src_size) - 1);
|
|
111
|
-
// 边缘从小框自己的原始帧取,避免 warped 在贴边小框上采到黑边。
|
|
112
|
-
vec3 edgep = texelFetch(raw, ep, 0).rgb;
|
|
113
|
-
// CPU 里 edge_w=0 是边缘(取折射样本),=1 是中心(取强模糊);
|
|
114
|
-
// 这里方向必须一致,否则内框边缘反而没有扭曲。
|
|
115
|
-
vec3 patch = mix(edgep, strong, e);
|
|
116
|
-
frag = vec4(mix(base, patch, a), 1.0);
|
|
117
|
-
}
|
|
118
|
-
"""
|
|
6
|
+
需要 `moderngl` 和 OpenGL 3.3+:
|
|
119
7
|
|
|
120
|
-
|
|
121
|
-
#version 330
|
|
122
|
-
uniform sampler2D warped;
|
|
123
|
-
uniform sampler2D lens;
|
|
124
|
-
uniform sampler2D union_mask;
|
|
125
|
-
uniform sampler2D overlay;
|
|
126
|
-
in vec2 v_uv;
|
|
127
|
-
out vec4 frag;
|
|
128
|
-
void main() {
|
|
129
|
-
vec3 base = texture(warped, v_uv).rgb;
|
|
130
|
-
vec3 lensc = texture(lens, v_uv).rgb;
|
|
131
|
-
float m = texture(union_mask, v_uv).r;
|
|
132
|
-
vec3 c = mix(base, lensc, m);
|
|
133
|
-
vec4 o = texture(overlay, v_uv);
|
|
134
|
-
c = c * (1.0 - o.a) + o.rgb * o.a;
|
|
135
|
-
frag = vec4(c, 1.0);
|
|
136
|
-
}
|
|
137
|
-
"""
|
|
8
|
+
pip install -r requirements-glass.txt moderngl
|
|
138
9
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
uniform sampler2D curr;
|
|
142
|
-
uniform sampler2D prev;
|
|
143
|
-
uniform vec2 texel;
|
|
144
|
-
uniform float blend;
|
|
145
|
-
uniform float chroma;
|
|
146
|
-
in vec2 v_uv;
|
|
147
|
-
out vec4 frag;
|
|
148
|
-
void main() {
|
|
149
|
-
vec3 c = texture(curr, v_uv).rgb;
|
|
150
|
-
vec3 p = texture(prev, v_uv).rgb;
|
|
151
|
-
// 运动残影:只混一点点上一帧,遮住延迟造成的逐帧跳变/撕裂感
|
|
152
|
-
c = mix(c, p, blend);
|
|
153
|
-
// 外框色散:只在靠近窗口圆角外缘的窄带里把 R/B 沿径向错开
|
|
154
|
-
vec2 d = v_uv - 0.5;
|
|
155
|
-
float r = length(d);
|
|
156
|
-
float edge = smoothstep(0.38, 0.48, r) * (1.0 - smoothstep(0.52, 0.585, r));
|
|
157
|
-
if (edge > 0.001) {
|
|
158
|
-
vec2 dir = normalize(d + vec2(1e-5));
|
|
159
|
-
float k = chroma * edge;
|
|
160
|
-
vec3 cc;
|
|
161
|
-
cc.r = texture(curr, v_uv + dir * texel * k).r;
|
|
162
|
-
cc.g = c.g;
|
|
163
|
-
cc.b = texture(curr, v_uv - dir * texel * k).b;
|
|
164
|
-
c = mix(c, cc, edge);
|
|
165
|
-
}
|
|
166
|
-
frag = vec4(c, 1.0);
|
|
167
|
-
}
|
|
168
|
-
"""
|
|
10
|
+
没有 moderngl 时 `window.py` 会打印一行原因并回落到 CPU,窗口照样能开
|
|
11
|
+
(而不是像 0.2.10 之前那样每帧抛一次 `GPU capture failed` 刷屏)。
|
|
169
12
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
in vec2 v_uv;
|
|
174
|
-
out vec4 frag;
|
|
175
|
-
void main() { frag = texture(tex, v_uv); }
|
|
13
|
+
`DSH_GLASS_NATIVE=1` 让采样走全分辨率(边缘透镜没有 1/4 放大的像素感)。
|
|
14
|
+
正常使用时不需要手动跑这个文件:插件会拉起 `window.py`,设 `DSH_GLASS_GPU=1`
|
|
15
|
+
或 `DSH_GLASS_NATIVE=1` 即可切到 GPU。
|
|
176
16
|
"""
|
|
17
|
+
import os
|
|
177
18
|
|
|
178
|
-
|
|
179
|
-
chans = []
|
|
180
|
-
for i in range(arr.shape[2]):
|
|
181
|
-
im = Image.fromarray(np.ascontiguousarray(arr[:, :, i]), mode="F")
|
|
182
|
-
chans.append(np.asarray(im.resize(size, Image.Resampling.BILINEAR), dtype=np.float32))
|
|
183
|
-
return np.stack(chans, axis=-1)
|
|
184
|
-
|
|
185
|
-
def _resize_mask(arr, size):
|
|
186
|
-
im = Image.fromarray((np.clip(arr, 0.0, 1.0) * 255.0).astype(np.uint8), mode="L")
|
|
187
|
-
im = im.resize(size, Image.Resampling.BILINEAR)
|
|
188
|
-
return np.asarray(im, dtype=np.float32) / 255.0
|
|
189
|
-
|
|
190
|
-
def _boost_worker():
|
|
191
|
-
"""GPU 工作线程略高于普通优先级,减少被系统调度挤开的抖动。
|
|
192
|
-
只提到 ABOVE_NORMAL + HIGHEST,不用 REALTIME,避免抢 UI/音频。"""
|
|
193
|
-
try:
|
|
194
|
-
k = ctypes.windll.kernel32
|
|
195
|
-
k.SetPriorityClass(k.GetCurrentProcess(), 0x00008000) # ABOVE_NORMAL
|
|
196
|
-
k.SetThreadPriority(k.GetCurrentThread(), 2) # THREAD_PRIORITY_HIGHEST
|
|
197
|
-
ctypes.windll.winmm.timeBeginPeriod(1)
|
|
198
|
-
except Exception:
|
|
199
|
-
pass
|
|
200
|
-
|
|
201
|
-
class GPULens:
|
|
202
|
-
def __init__(self):
|
|
203
|
-
_boost_worker()
|
|
204
|
-
self.ctx = moderngl.create_standalone_context(require=330)
|
|
205
|
-
tri = np.array([-1.0, -1.0, 3.0, -1.0, -1.0, 3.0], dtype="f4")
|
|
206
|
-
self.vbo = self.ctx.buffer(tri.tobytes())
|
|
207
|
-
self.progs = {}
|
|
208
|
-
for name, fs in (("down", FRAG_DOWN), ("blur", FRAG_BLUR),
|
|
209
|
-
("warp", FRAG_WARP), ("box", FRAG_BOX),
|
|
210
|
-
("panel", FRAG_PANEL), ("final", FRAG_FINAL), ("post", FRAG_POST)):
|
|
211
|
-
p = self.ctx.program(vertex_shader=VERT, fragment_shader=fs)
|
|
212
|
-
self.progs[name] = p
|
|
213
|
-
p.vao = self.ctx.vertex_array(p, [(self.vbo, "2f", "in_pos")])
|
|
214
|
-
self.native = os.environ.get("DSH_GLASS_NATIVE") == "1"
|
|
215
|
-
self._prev_valid = False
|
|
216
|
-
self._motion = 0.0
|
|
217
|
-
self.src = None
|
|
218
|
-
self.geo = None
|
|
219
|
-
self.geo_version = -1
|
|
220
|
-
self.fbos = {}
|
|
221
|
-
self.texs = {}
|
|
222
|
-
|
|
223
|
-
def _fbo(self, key, size):
|
|
224
|
-
old = self.fbos.get(key)
|
|
225
|
-
if old is not None and old.size == size:
|
|
226
|
-
return old
|
|
227
|
-
if old is not None:
|
|
228
|
-
old.release()
|
|
229
|
-
tex = self.ctx.texture(size, 4)
|
|
230
|
-
tex.filter = (moderngl.LINEAR, moderngl.LINEAR)
|
|
231
|
-
tex.repeat_x = False
|
|
232
|
-
tex.repeat_y = False
|
|
233
|
-
fbo = self.ctx.framebuffer(color_attachments=[tex])
|
|
234
|
-
self.fbos[key] = fbo
|
|
235
|
-
self.texs[key] = tex
|
|
236
|
-
return fbo
|
|
237
|
-
|
|
238
|
-
def _tex(self, key, size, components=1, dtype="f4", data=None):
|
|
239
|
-
old = self.texs.get(key)
|
|
240
|
-
if old is not None and old.size == size and old.components == components:
|
|
241
|
-
if data is not None:
|
|
242
|
-
old.write(data)
|
|
243
|
-
return old
|
|
244
|
-
if old is not None:
|
|
245
|
-
old.release()
|
|
246
|
-
t = self.ctx.texture(size, components, dtype=dtype, data=data)
|
|
247
|
-
t.filter = (moderngl.NEAREST, moderngl.NEAREST)
|
|
248
|
-
t.repeat_x = False
|
|
249
|
-
t.repeat_y = False
|
|
250
|
-
self.texs[key] = t
|
|
251
|
-
return t
|
|
252
|
-
|
|
253
|
-
def _set(self, prog, **kw):
|
|
254
|
-
for k, v in kw.items():
|
|
255
|
-
try:
|
|
256
|
-
prog[k] = v
|
|
257
|
-
except KeyError:
|
|
258
|
-
pass
|
|
259
|
-
|
|
260
|
-
def _build_geo(self):
|
|
261
|
-
W, H = G.W, G.H
|
|
262
|
-
qw, qh = max(4, W // 4), max(4, H // 4)
|
|
263
|
-
alpha_q = np.zeros((qh, qw), dtype="f4")
|
|
264
|
-
edge_q = np.zeros((qh, qw), dtype="f4")
|
|
265
|
-
coord_q = np.zeros((qh, qw, 2), dtype="f4")
|
|
266
|
-
for qx0, qy0, bqw, bqh, a, e, bsx, bsy in G.BOX_MAPS:
|
|
267
|
-
alpha_q[qy0:qy0 + bqh, qx0:qx0 + bqw] = a
|
|
268
|
-
edge_q[qy0:qy0 + bqh, qx0:qx0 + bqw] = e.astype(np.float32)
|
|
269
|
-
coord_q[qy0:qy0 + bqh, qx0:qx0 + bqw, 0] = bsx
|
|
270
|
-
coord_q[qy0:qy0 + bqh, qx0:qx0 + bqw, 1] = bsy
|
|
271
|
-
warp_q = np.stack([G.WARP_X, G.WARP_Y], axis=-1).astype("f4")
|
|
272
|
-
if self.native:
|
|
273
|
-
hw, hh = W, H
|
|
274
|
-
HW, HH = W, H
|
|
275
|
-
alpha = _resize_mask(alpha_q, (hw, hh))
|
|
276
|
-
edge = _resize_mask(edge_q, (hw, hh))
|
|
277
|
-
# Everything in the inner boxes is sampled from the native
|
|
278
|
-
# capture (no quarter-res raw), so the lens edges have no
|
|
279
|
-
# upscaled pixel feel.
|
|
280
|
-
coord = _resize_f32(coord_q * 4.0, (hw, hh))
|
|
281
|
-
warp = _resize_f32(warp_q * 4.0, (hw, hh))
|
|
282
|
-
overlay = np.asarray(G.OVERLAY.convert("RGBA"), dtype=np.uint8)
|
|
283
|
-
union = np.asarray(G.BOX_UNION_2X.resize((W, H), Image.Resampling.BILINEAR), dtype=np.uint8)
|
|
284
|
-
else:
|
|
285
|
-
hw, hh = qw, qh
|
|
286
|
-
HW, HH = W // 2, H // 2
|
|
287
|
-
alpha, edge, coord, warp = alpha_q, edge_q, coord_q, warp_q
|
|
288
|
-
overlay = np.asarray(G.OVERLAY_2X.convert("RGBA"), dtype=np.uint8)
|
|
289
|
-
union = np.asarray(G.BOX_UNION_2X, dtype=np.uint8)
|
|
290
|
-
self._tex("alpha", (hw, hh), 1, "f4", np.ascontiguousarray(alpha).tobytes())
|
|
291
|
-
self._tex("edge", (hw, hh), 1, "f4", np.ascontiguousarray(edge).tobytes())
|
|
292
|
-
self._tex("coord", (hw, hh), 2, "f4", np.ascontiguousarray(coord).tobytes())
|
|
293
|
-
self._tex("warpcoord", (hw, hh), 2, "f4", np.ascontiguousarray(warp).tobytes())
|
|
294
|
-
t = self._tex("overlay", (HW, HH), 4, "f1", overlay.tobytes())
|
|
295
|
-
t.filter = (moderngl.LINEAR, moderngl.LINEAR)
|
|
296
|
-
t = self._tex("union", (HW, HH), 1, "f1", union.tobytes())
|
|
297
|
-
t.filter = (moderngl.LINEAR, moderngl.LINEAR)
|
|
298
|
-
self.geo = (W, H, hw, hh, HW, HH)
|
|
299
|
-
self.geo_version = G.SHAPES_VERSION
|
|
300
|
-
self.fbos.clear()
|
|
301
|
-
self.texs = {k: v for k, v in self.texs.items()
|
|
302
|
-
if k in ("alpha", "edge", "coord", "warpcoord", "overlay", "union")}
|
|
303
|
-
|
|
304
|
-
def _ensure_src(self, size, data):
|
|
305
|
-
if self.src is None or self.src.size != size:
|
|
306
|
-
if self.src is not None:
|
|
307
|
-
self.src.release()
|
|
308
|
-
self.src = self.ctx.texture(size, 4)
|
|
309
|
-
self.src.filter = (moderngl.LINEAR, moderngl.LINEAR)
|
|
310
|
-
self.src.repeat_x = False
|
|
311
|
-
self.src.repeat_y = False
|
|
312
|
-
self.src.write(data)
|
|
313
|
-
|
|
314
|
-
def render(self, bgra, size, px, py, motion=0.0, moved=False):
|
|
315
|
-
if (self.geo is None or self.geo[:2] != (G.W, G.H)
|
|
316
|
-
or getattr(self, "geo_version", -1) != G.SHAPES_VERSION):
|
|
317
|
-
self._build_geo()
|
|
318
|
-
W, H, hw, hh, HW, HH = self.geo
|
|
319
|
-
gw, gh = size
|
|
320
|
-
self._ensure_src((gw, gh), bgra)
|
|
321
|
-
|
|
322
|
-
if self.native:
|
|
323
|
-
cw, ch = gw, gh
|
|
324
|
-
raw = self._fbo("raw", (cw, ch))
|
|
325
|
-
raw.use(); self.ctx.viewport = (0, 0, cw, ch)
|
|
326
|
-
self.src.use(0)
|
|
327
|
-
p = self.progs["down"]; p["tex"] = 0; p.vao.render(moderngl.TRIANGLES)
|
|
328
|
-
# 内框也走原生 raw;边缘不带 1/4 放大的像素感。
|
|
329
|
-
# 内框的“厚模糊”由下面更强的 strong sigma 提供。
|
|
330
|
-
base_raw = raw
|
|
331
|
-
bw, bh = cw, ch
|
|
332
|
-
ox, oy = float(px), float(py)
|
|
333
|
-
box_ox, box_oy = float(px), float(py)
|
|
334
|
-
box_bw, box_bh = cw, ch
|
|
335
|
-
else:
|
|
336
|
-
bw, bh = max(2, gw // 4), max(2, gh // 4)
|
|
337
|
-
raw = self._fbo("raw", (bw, bh))
|
|
338
|
-
raw.use(); self.ctx.viewport = (0, 0, bw, bh)
|
|
339
|
-
self.src.use(0)
|
|
340
|
-
p = self.progs["down"]; p["tex"] = 0; p.vao.render(moderngl.TRIANGLES)
|
|
341
|
-
base_raw = raw
|
|
342
|
-
ox, oy = float(px), float(py)
|
|
343
|
-
box_raw = raw
|
|
344
|
-
box_ox, box_oy = float(px), float(py)
|
|
345
|
-
box_bw, box_bh = bw, bh
|
|
346
|
-
|
|
347
|
-
small = self._fbo("small", (bw, bh))
|
|
348
|
-
strong = self._fbo("strong", (bw, bh))
|
|
349
|
-
tmp = self._fbo("tmp", (bw, bh))
|
|
350
|
-
warped = self._fbo("warped", (hw, hh))
|
|
351
|
-
strongw = self._fbo("strongw", (hw, hh))
|
|
352
|
-
lens = self._fbo("lens", (hw, hh))
|
|
353
|
-
panel2x = self._fbo("panel2x", (HW, HH))
|
|
354
|
-
out = self._fbo("out", (W, H))
|
|
355
|
-
|
|
356
|
-
def blur(dst, srctex, sigma):
|
|
357
|
-
tmp.use(); self.ctx.viewport = (0, 0, bw, bh)
|
|
358
|
-
srctex.use(0)
|
|
359
|
-
p = self.progs["blur"]; p["tex"] = 0; p["texel"] = (1.0 / bw, 1.0 / bh)
|
|
360
|
-
p["sigma"] = sigma; p["dir"] = (1.0, 0.0); p.vao.render(moderngl.TRIANGLES)
|
|
361
|
-
dst.use(); self.ctx.viewport = (0, 0, bw, bh)
|
|
362
|
-
tmp.color_attachments[0].use(0)
|
|
363
|
-
p["dir"] = (0.0, 1.0); p.vao.render(moderngl.TRIANGLES)
|
|
364
|
-
|
|
365
|
-
sigma_scale = 4.0 if self.native else 1.0
|
|
366
|
-
blur(small, base_raw.color_attachments[0], 1.45 * sigma_scale)
|
|
367
|
-
# 内框中心再多加一点模糊;只影响 strongw / 内框,不影响底图。
|
|
368
|
-
strong_boost = 1.45 if self.native else 1.0
|
|
369
|
-
blur(strong, small.color_attachments[0], 2.6 * sigma_scale * strong_boost)
|
|
370
|
-
|
|
371
|
-
for dst, src in ((warped, small), (strongw, strong)):
|
|
372
|
-
dst.use(); self.ctx.viewport = (0, 0, hw, hh)
|
|
373
|
-
src.color_attachments[0].use(0)
|
|
374
|
-
self.texs["warpcoord"].use(1)
|
|
375
|
-
p = self.progs["warp"]; self._set(p, src=0, coord=1, offset=(float(ox), float(oy)),
|
|
376
|
-
src_size=(float(bw), float(bh)))
|
|
377
|
-
p.vao.render(moderngl.TRIANGLES)
|
|
378
|
-
|
|
379
|
-
# 单独拷一份 blur(1.45) 给边缘采样;直接把 small 的 render target
|
|
380
|
-
# 当 sampler 会触发驱动崩溃。
|
|
381
|
-
edgeblur = self._fbo("edgeblur", (bw, bh))
|
|
382
|
-
edgeblur.use(); self.ctx.viewport = (0, 0, bw, bh)
|
|
383
|
-
small.color_attachments[0].use(0)
|
|
384
|
-
p = self.progs["final"]; p["tex"] = 0; p.vao.render(moderngl.TRIANGLES)
|
|
385
|
-
|
|
386
|
-
lens.use(); self.ctx.viewport = (0, 0, hw, hh)
|
|
387
|
-
warped.color_attachments[0].use(0)
|
|
388
|
-
strongw.color_attachments[0].use(1)
|
|
389
|
-
edgeblur.color_attachments[0].use(2)
|
|
390
|
-
self.texs["alpha"].use(3)
|
|
391
|
-
self.texs["edge"].use(4)
|
|
392
|
-
self.texs["coord"].use(5)
|
|
393
|
-
p = self.progs["box"]
|
|
394
|
-
self._set(p, warped=0, strongw=1, raw=2, alpha=3, edge=4, coord=5,
|
|
395
|
-
offset=(float(box_ox), float(box_oy)),
|
|
396
|
-
src_size=(float(box_bw), float(box_bh)))
|
|
397
|
-
p.vao.render(moderngl.TRIANGLES)
|
|
398
|
-
|
|
399
|
-
panel2x.use(); self.ctx.viewport = (0, 0, HW, HH)
|
|
400
|
-
warped.color_attachments[0].use(0)
|
|
401
|
-
lens.color_attachments[0].use(1)
|
|
402
|
-
self.texs["union"].use(2)
|
|
403
|
-
self.texs["overlay"].use(3)
|
|
404
|
-
p = self.progs["panel"]
|
|
405
|
-
self._set(p, warped=0, lens=1, union_mask=2, overlay=3)
|
|
406
|
-
p.vao.render(moderngl.TRIANGLES)
|
|
407
|
-
|
|
408
|
-
if self.native:
|
|
409
|
-
base_out = panel2x
|
|
410
|
-
else:
|
|
411
|
-
final = self._fbo("final", (W, H))
|
|
412
|
-
final.use(); self.ctx.viewport = (0, 0, W, H)
|
|
413
|
-
panel2x.color_attachments[0].use(0)
|
|
414
|
-
p = self.progs["final"]; p["tex"] = 0; p.vao.render(moderngl.TRIANGLES)
|
|
415
|
-
base_out = final
|
|
416
|
-
|
|
417
|
-
prev = self._fbo("prev", (W, H))
|
|
418
|
-
out.use(); self.ctx.viewport = (0, 0, W, H)
|
|
419
|
-
base_out.color_attachments[0].use(0)
|
|
420
|
-
prev.color_attachments[0].use(1)
|
|
421
|
-
p = self.progs["post"]
|
|
422
|
-
p["texel"] = (1.0 / W, 1.0 / H)
|
|
423
|
-
# 和 CPU 一致:只在“本帧检测到移动”时混上一帧,停下立刻干净;
|
|
424
|
-
# 移动帧跳过色散,先把拖动/滚动响应保住。
|
|
425
|
-
p["blend"] = float(0.35 * max(0.0, min(1.0, motion))
|
|
426
|
-
if moved and getattr(self, "_prev_valid", False) else 0.0)
|
|
427
|
-
p["chroma"] = 0.0 if moved else 3.6 * (W / 900.0)
|
|
428
|
-
self._set(p, curr=0, prev=1)
|
|
429
|
-
p.vao.render(moderngl.TRIANGLES)
|
|
430
|
-
self.ctx.copy_framebuffer(prev, out)
|
|
431
|
-
self._prev_valid = True
|
|
432
|
-
|
|
433
|
-
data = out.read(components=3, alignment=1)
|
|
434
|
-
return Image.frombytes("RGB", (W, H), data)
|
|
435
|
-
|
|
436
|
-
_gpu = None
|
|
437
|
-
_orig_capture = G.LiveGlass._capture
|
|
438
|
-
|
|
439
|
-
def capture_gpu(self, sct):
|
|
440
|
-
global _gpu
|
|
441
|
-
try:
|
|
442
|
-
if _gpu is None:
|
|
443
|
-
_gpu = GPULens()
|
|
444
|
-
l, t, r, b = self._bbox()
|
|
445
|
-
if r <= l or b <= t:
|
|
446
|
-
return None
|
|
447
|
-
gl = max(0, l - G.PAD)
|
|
448
|
-
gt = max(0, t - G.PAD)
|
|
449
|
-
gr = min(self._sw, r + G.PAD)
|
|
450
|
-
gb = min(self._sh, b + G.PAD)
|
|
451
|
-
# 和 window.py 的 CPU 路径保持一致:多显示器 / DPI 变化 / 被别的程序
|
|
452
|
-
# 挪出屏幕时,钳位后的宽高可能 <= 0;mss 会抛
|
|
453
|
-
# "Region has zero or negative size",而 _work() 会每帧吞掉它 ——
|
|
454
|
-
# 表现就是日志每帧刷一条、背景永久停在旧帧。没有可抓的区域就别抓。
|
|
455
|
-
if gr - gl <= 0 or gb - gt <= 0:
|
|
456
|
-
return None
|
|
457
|
-
shot = sct.grab({"left": gl, "top": gt, "width": gr - gl, "height": gb - gt})
|
|
458
|
-
factor = 1.0 if _gpu.native else 4.0
|
|
459
|
-
px = int(round((l - gl) / factor))
|
|
460
|
-
py = int(round((t - gt) / factor))
|
|
461
|
-
|
|
462
|
-
# 静态判断放在渲染之前:用抓到的原始 BGRA 直接算签名,静止时
|
|
463
|
-
# 连 GPU 合成都不做;运动时把强度交给 post pass 做残影。
|
|
464
|
-
sw, sh = shot.size
|
|
465
|
-
raw = np.frombuffer(shot.bgra, dtype=np.uint8).reshape(sh, sw, 4)
|
|
466
|
-
sig = raw[::4, ::4, :3].astype(np.int16).mean(axis=2)
|
|
467
|
-
box = (gl, gt, gr, gb)
|
|
468
|
-
now = time.perf_counter()
|
|
469
|
-
moved = False
|
|
470
|
-
if self._sig is not None and box == self._sig_bbox and sig.shape == self._sig.shape:
|
|
471
|
-
d = np.abs(sig - self._sig)
|
|
472
|
-
moved = bool(float(d.mean()) > 0.01 or float(d.max()) > 0.5)
|
|
473
|
-
if moved:
|
|
474
|
-
self._last_motion = now
|
|
475
|
-
_gpu._motion = min(1.0, _gpu._motion * 0.50 + 0.50)
|
|
476
|
-
else:
|
|
477
|
-
_gpu._motion *= 0.55
|
|
478
|
-
if (not G.FORCE and not moved and box == self._sig_bbox
|
|
479
|
-
and now - getattr(self, "_last_motion", 0.0) > 0.30):
|
|
480
|
-
self._sig = sig
|
|
481
|
-
_gpu._motion = 0.0
|
|
482
|
-
_gpu._prev_valid = False
|
|
483
|
-
return None
|
|
484
|
-
self._sig = sig
|
|
485
|
-
self._sig_bbox = box
|
|
486
|
-
|
|
487
|
-
panel = _gpu.render(shot.bgra, shot.size, px, py,
|
|
488
|
-
motion=_gpu._motion, moved=moved)
|
|
489
|
-
out = panel.copy()
|
|
490
|
-
mic_box = out.crop((G.MIC_POS[0], G.MIC_POS[1],
|
|
491
|
-
G.MIC_POS[0] + 28, G.MIC_POS[1] + 28)).convert("L")
|
|
492
|
-
mic_bg = float(np.median(np.asarray(mic_box))) / 255.0
|
|
493
|
-
out = out.convert("RGBA")
|
|
494
|
-
icon = G.MIC_LIGHT if mic_bg < 0.48 else G.MIC_DARK
|
|
495
|
-
out.paste(icon, G.MIC_POS, icon)
|
|
496
|
-
return G.finish(out.convert("RGB"))
|
|
497
|
-
except Exception as exc:
|
|
498
|
-
print("GPU capture failed, falling back to CPU:", repr(exc), flush=True)
|
|
499
|
-
return _orig_capture(self, sct)
|
|
500
|
-
|
|
501
|
-
G.LiveGlass._capture = capture_gpu
|
|
502
|
-
|
|
503
|
-
# PR15's SNAPSHOT path is re-armed by _tick; make the doc screenshot path keep
|
|
504
|
-
# capture paused and WDA disabled, without touching the normal path.
|
|
505
|
-
def _snap(self, _e=None):
|
|
506
|
-
self._snapshot_done = True
|
|
507
|
-
self._pause.set()
|
|
508
|
-
if self._excluded:
|
|
509
|
-
self._set_exclude(False)
|
|
510
|
-
|
|
511
|
-
def _tick(self):
|
|
512
|
-
if getattr(self, "_snapshot_done", False):
|
|
513
|
-
if self._excluded:
|
|
514
|
-
self._set_exclude(False)
|
|
515
|
-
self.root.after(100, self._tick)
|
|
516
|
-
return
|
|
517
|
-
return _orig_tick(self)
|
|
19
|
+
os.environ.setdefault("DSH_GLASS_GPU", "1")
|
|
518
20
|
|
|
519
|
-
|
|
520
|
-
_orig_tick = G.LiveGlass._tick
|
|
521
|
-
G.LiveGlass._snapshot_mode = _snap
|
|
522
|
-
G.LiveGlass._tick = _tick
|
|
21
|
+
import window # noqa: E402 (必须先设环境变量:后端在 import 时确定)
|
|
523
22
|
|
|
524
23
|
if __name__ == "__main__":
|
|
525
|
-
|
|
526
|
-
app.run()
|
|
24
|
+
window.LiveGlass().run()
|
|
@@ -14,3 +14,8 @@
|
|
|
14
14
|
Pillow>=10 # 截图、模糊、色散、合成
|
|
15
15
|
mss>=9 # 抓屏(比 ImageGrab 快约一倍:38ms vs 71ms)
|
|
16
16
|
numpy>=1.24 # 模糊 / SDF 边缘折射 / 透镜采样都在数组上做
|
|
17
|
+
|
|
18
|
+
# 可选:只有切 GPU 合成(DSH_GLASS_GPU=1 / DSH_GLASS_NATIVE=1)才需要。
|
|
19
|
+
# 不装就走 CPU,窗口照常开;装了但显卡不支持 OpenGL 3.3 会自动回落 CPU。
|
|
20
|
+
# pip install moderngl
|
|
21
|
+
# moderngl>=5.8 # OpenGL 3.3+ / Windows WGL、Linux EGL、macOS CGL
|
|
@@ -11,12 +11,16 @@ rem DSH_GLASS_ACRYLIC (不读,没有 ac
|
|
|
11
11
|
rem
|
|
12
12
|
rem 现在真正有效的开关:
|
|
13
13
|
rem
|
|
14
|
-
rem
|
|
14
|
+
rem DSH_GLASS_GPU=1 GPU 合成(需要 moderngl + OpenGL 3.3;起不来会回落 CPU)
|
|
15
|
+
rem DSH_GLASS_NATIVE=1 同上,且采样走全分辨率(边缘更干净,更吃显存)
|
|
15
16
|
rem GLASS_FORCE=1 每帧强制重合成(调试用,很费 CPU)
|
|
16
17
|
rem DSH_GLASS_PREVIEW=1 预览模式:不抓屏,只贴最后一帧
|
|
17
18
|
rem DSH_GLASS_SNAPSHOT=1 文档截图模式:跑 2.5 秒后冻结画面
|
|
18
19
|
rem DSH_GLASS_WDA=0 允许截图/录屏拍到窗口(等价于配置里的 capturable)
|
|
19
20
|
rem
|
|
21
|
+
rem 想开 GPU 的话把下面这行前面的 rem 去掉(或者用 glass_window_gpu.py 起窗):
|
|
22
|
+
rem set DSH_GLASS_GPU=1
|
|
23
|
+
rem
|
|
20
24
|
rem 尺寸、名字、capturable 这类持久设置写 ~/.dsh/watch-window.json。
|
|
21
25
|
rem ---------------------------------------------------------------------------
|
|
22
26
|
|