1 /*
2  * This file is part of mpv.
3  *
4  * mpv is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * mpv is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef MP_GL_USER_SHADERS_H
19 #define MP_GL_USER_SHADERS_H
20 
21 #include "utils.h"
22 #include "ra.h"
23 
24 #define SHADER_MAX_HOOKS 16
25 #define SHADER_MAX_BINDS 16
26 #define MAX_SZEXP_SIZE 32
27 
28 enum szexp_op {
29     SZEXP_OP_ADD,
30     SZEXP_OP_SUB,
31     SZEXP_OP_MUL,
32     SZEXP_OP_DIV,
33     SZEXP_OP_MOD,
34     SZEXP_OP_NOT,
35     SZEXP_OP_GT,
36     SZEXP_OP_LT,
37     SZEXP_OP_EQ,
38 };
39 
40 enum szexp_tag {
41     SZEXP_END = 0, // End of an RPN expression
42     SZEXP_CONST, // Push a constant value onto the stack
43     SZEXP_VAR_W, // Get the width/height of a named texture (variable)
44     SZEXP_VAR_H,
45     SZEXP_OP2, // Pop two elements and push the result of a dyadic operation
46     SZEXP_OP1, // Pop one element and push the result of a monadic operation
47 };
48 
49 struct szexp {
50     enum szexp_tag tag;
51     union {
52         float cval;
53         struct bstr varname;
54         enum szexp_op op;
55     } val;
56 };
57 
58 struct compute_info {
59     bool active;
60     int block_w, block_h;     // Block size (each block corresponds to one WG)
61     int threads_w, threads_h; // How many threads form a working group
62     bool directly_writes;     // If true, shader is assumed to imageStore(out_image)
63 };
64 
65 struct gl_user_shader_hook {
66     struct bstr pass_desc;
67     struct bstr hook_tex[SHADER_MAX_HOOKS];
68     struct bstr bind_tex[SHADER_MAX_BINDS];
69     struct bstr save_tex;
70     struct bstr pass_body;
71     struct gl_transform offset;
72     bool align_offset;
73     struct szexp width[MAX_SZEXP_SIZE];
74     struct szexp height[MAX_SZEXP_SIZE];
75     struct szexp cond[MAX_SZEXP_SIZE];
76     int components;
77     struct compute_info compute;
78 };
79 
80 struct gl_user_shader_tex {
81     struct bstr name;
82     struct ra_tex_params params;
83     // for video.c
84     struct ra_tex *tex;
85 };
86 
87 // Parse the next shader block from `body`. The callbacks are invoked on every
88 // valid shader block parsed.
89 void parse_user_shader(struct mp_log *log, struct ra *ra, struct bstr shader,
90                        void *priv,
91                        bool (*dohook)(void *p, struct gl_user_shader_hook hook),
92                        bool (*dotex)(void *p, struct gl_user_shader_tex tex));
93 
94 // Evaluate a szexp, given a lookup function for named textures
95 bool eval_szexpr(struct mp_log *log, void *priv,
96                  bool (*lookup)(void *priv, struct bstr var, float size[2]),
97                  struct szexp expr[MAX_SZEXP_SIZE], float *result);
98 
99 #endif
100