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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
import GObject from 'gi://GObject';
import * as utils from '../conveniences/utils.js';
import * as uniforms from '../conveniences/shader_uniforms.js';
const St = await utils.import_in_shell_only('gi://St');
const Shell = await utils.import_in_shell_only('gi://Shell');
const Clutter = await utils.import_in_shell_only('gi://Clutter');
const SHADER_FILENAME = 'monte_carlo_blur.glsl';
const SHADER_SOURCE = utils.get_shader_source(Shell, SHADER_FILENAME, import.meta.url);
const DEFAULT_PARAMS = {
radius: 2., iterations: 5, brightness: .6,
width: 0, height: 0, use_base_pixel: true,
prefer_closer_pixels: true,
};
const MONTE_CARLO_BLUR_EFFECT_META = {
GTypeName: "MonteCarloBlurEffect",
Properties: {
'radius': GObject.ParamSpec.double(
`radius`,
`Radius`,
`Blur radius`,
GObject.ParamFlags.READWRITE,
0.0, 2000.0,
2.0,
),
'iterations': GObject.ParamSpec.int(
`iterations`,
`Iterations`,
`Blur iterations`,
GObject.ParamFlags.READWRITE,
0, 64,
5,
),
'brightness': GObject.ParamSpec.double(
`brightness`,
`Brightness`,
`Blur brightness`,
GObject.ParamFlags.READWRITE,
0.0, 1.0,
0.6,
),
'width': GObject.ParamSpec.double(
`width`,
`Width`,
`Width`,
GObject.ParamFlags.READWRITE,
0.0, Number.MAX_SAFE_INTEGER,
0.0,
),
'height': GObject.ParamSpec.double(
`height`,
`Height`,
`Height`,
GObject.ParamFlags.READWRITE,
0.0, Number.MAX_SAFE_INTEGER,
0.0,
),
'use_base_pixel': GObject.ParamSpec.boolean(
`use_base_pixel`,
`Use base pixel`,
`Use base pixel`,
GObject.ParamFlags.READWRITE,
true,
),
'prefer_closer_pixels': GObject.ParamSpec.boolean(
`prefer_closer_pixels`,
`Prefer closer pixels`,
`Prefer closer pixels`,
GObject.ParamFlags.READWRITE,
true,
),
}
};
const MonteCarloBlurEffectClass = utils.IS_IN_PREFERENCES ? null : class MonteCarloBlurEffect extends Clutter.ShaderEffect {
constructor(params) {
super();
utils.initialize_shader_effect(this, SHADER_SOURCE);
utils.setup_params(this, params);
const theme_context = St.ThemeContext.get_for_stage(global.stage);
theme_context.connectObject(
'notify::scale-factor',
_ => uniforms.set_uniform(this, 'radius',
parseFloat(this._radius * theme_context.scale_factor - 1e-6)
),
this
);
}
static get default_params() {
return DEFAULT_PARAMS;
}
get radius() {
return this._radius;
}
set radius(value) {
if (this._radius !== value) {
this._radius = value;
const scale_factor = St.ThemeContext.get_for_stage(global.stage).scale_factor;
uniforms.set_uniform(this, 'radius', parseFloat(this._radius * scale_factor - 1e-6));
this.set_enabled(this.radius > 0. && this.iterations > 0);
}
}
get iterations() {
return this._iterations;
}
set iterations(value) {
if (this._iterations !== value) {
this._iterations = value;
uniforms.set_uniform(this, 'iterations', this._iterations);
this.set_enabled(this.radius > 0. && this.iterations > 0);
}
}
get brightness() {
return this._brightness;
}
set brightness(value) {
if (this._brightness !== value) {
this._brightness = value;
uniforms.set_uniform(this, 'brightness', parseFloat(this._brightness - 1e-6));
}
}
get width() {
return this._width;
}
set width(value) {
const v = Math.max(1, value || 1);
if (this._width !== v) {
this._width = v;
uniforms.set_uniform(this, 'width', parseFloat(this._width + 3.0 - 1e-6));
}
}
get height() {
return this._height;
}
set height(value) {
const v = Math.max(1, value || 1);
if (this._height !== v) {
this._height = v;
uniforms.set_uniform(this, 'height', parseFloat(this._height + 3.0 - 1e-6));
}
}
get use_base_pixel() {
return this._use_base_pixel;
}
set use_base_pixel(value) {
if (this._use_base_pixel !== value) {
this._use_base_pixel = value;
uniforms.set_uniform(this, 'use_base_pixel', this._use_base_pixel ? 1 : 0);
}
}
get prefer_closer_pixels() {
return this._prefer_closer_pixels;
}
set prefer_closer_pixels(value) {
if (this._prefer_closer_pixels !== value) {
this._prefer_closer_pixels = value;
uniforms.set_uniform(this, 'prefer_closer_pixels', this._prefer_closer_pixels ? 1 : 0);
}
}
vfunc_set_actor(actor) {
if (this._actor_connection_size_id) {
let old_actor = this.get_actor();
old_actor?.disconnect(this._actor_connection_size_id);
}
if (actor) {
this.width = actor.width;
this.height = actor.height;
this._actor_connection_size_id = actor.connect('notify::size', _ => {
this.width = actor.width;
this.height = actor.height;
});
}
else
this._actor_connection_size_id = null;
super.vfunc_set_actor(actor);
}
vfunc_paint_target(paint_node, paint_context) {
uniforms.upload_uniforms(this);
super.vfunc_paint_target(paint_node, paint_context);
}
};
export const MonteCarloBlurEffect = utils.IS_IN_PREFERENCES
? { default_params: DEFAULT_PARAMS }
: utils.register_shader_effect(MONTE_CARLO_BLUR_EFFECT_META, MonteCarloBlurEffectClass, SHADER_SOURCE);
|