summaryrefslogtreecommitdiff
path: root/raveos-gnome-theme/theme-data/extensions/installed/blur-my-shell@aunetx/conveniences/shader_uniforms.js
blob: a82b261863cd99396f9c6a5bcf09898b57e884f0 (plain)
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
import GObject from 'gi://GObject';

const INTEGRAL_UNIFORMS = new Set([
    'corners_bottom',
    'corners_top',
    'divider',
    'dir',
    'downsampling_mode',
    'factor',
    'iterations',
    'mode',
    'operation',
    'prefer_closer_pixels',
    'straight_corners',
    'use_base_pixel',
]);

export function set_uniform(effect, name, value) {
    if (!effect._bms_uniforms)
        effect._bms_uniforms = new Map();

    effect._bms_uniforms.set(name, value);
    effect._bms_uniforms_dirty = true;
    effect.queue_repaint();
}

export function mark_dirty(effect) {
    if (effect._bms_uniforms)
        effect._bms_uniforms_dirty = true;
}

export function upload_uniforms(effect) {
    if (!effect._bms_uniforms_dirty || !effect._bms_uniforms || !effect.get_actor())
        return;

    for (const [name, value] of effect._bms_uniforms) {
        try {
            effect.set_uniform_value(name, get_shader_value(name, value));
        } catch (e) {
            if (!effect._bms_uniform_warning_shown) {
                effect._bms_uniform_warning_shown = true;
                console.warn(`[Blur my Shell] shader uniform upload failed for ${effect.constructor.name}`, e);
            }
        }
    }

    effect._bms_uniforms_dirty = false;
}

function get_shader_value(name, value) {
    if (typeof value !== 'number')
        return value;

    if (INTEGRAL_UNIFORMS.has(name))
        return Math.trunc(value);

    const float_value = new GObject.Value();
    float_value.init(GObject.TYPE_FLOAT);
    float_value.set_float(value);
    return float_value;
}