Skip to content

Commit ec43ceb

Browse files
committed
vo_gpu: add support for ENUM parameters
1 parent e82667e commit ec43ceb

4 files changed

Lines changed: 85 additions & 6 deletions

File tree

DOCS/man/options.rst

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6434,11 +6434,19 @@ them.
64346434
Parameters are global across the entire shader file, all hooks in the
64356435
file can reference them, regardless of declaration order.
64366436

6437-
TYPE <DEFINE | <type>> (required)
6437+
TYPE [ENUM] <DEFINE | <type>> (required)
64386438
The parameter type. Supported types are ``float`` and ``int``.
6439+
64396440
The special type ``DEFINE`` emits a preprocessor define which can be
64406441
used inside ``#if`` directives.
64416442

6443+
If the ``ENUM`` qualifier is used then the type must be either
6444+
``DEFINE`` or ``int``. Instead of accepting a default value, ``ENUM``
6445+
qualified parameter body lists all the possible enumeration values
6446+
separated by newlines. These values are assigned integer values starting
6447+
from 0 incremented by 1. Each enumeration will also be emitted as a
6448+
preprocessor define. ``MINIMUM`` and ``MAXIMUM`` are ignored.
6449+
64426450
MINIMUM <value>
64436451
Minimum allowed value for this parameter.
64446452

@@ -6455,9 +6463,8 @@ them.
64556463
``vo=gpu`` supports only a subset of the parameter features available in
64566464
``vo=gpu-next``. See libplacebo documentation for more detailed
64576465
information about PARAM features supported in ``vo=gpu-next``. Notably
6458-
``uint``, ``ENUM``, ``DYNAMIC``, and ``CONSTANT`` types are
6459-
not available, and parameters cannot be referenced in ``WHEN``
6460-
expression.
6466+
``uint``, ``DYNAMIC``, and ``CONSTANT`` types are not available, and
6467+
parameters cannot be referenced in ``WHEN`` expression.
64616468

64626469
A ``HOOK`` block can set the following options:
64636470

video/out/gpu/user_shaders.c

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,35 @@
2525
#include "misc/ctype.h"
2626
#include "user_shaders.h"
2727

28+
static int resolve_enum_name(struct gl_user_shader_param *param, struct bstr val)
29+
{
30+
struct bstr enum_rest = param->enum_body;
31+
int idx = 0;
32+
while (enum_rest.len > 0) {
33+
struct bstr enum_line = bstr_strip(bstr_getline(enum_rest, &enum_rest));
34+
if (enum_line.len == 0)
35+
continue;
36+
if (bstr_equals(enum_line, val))
37+
return idx;
38+
idx++;
39+
}
40+
return -1;
41+
}
42+
2843
bool parse_shader_param_value(struct mp_log *log, struct gl_user_shader_param *param,
2944
struct bstr val, double *out)
3045
{
3146
double v, range[2];
32-
struct bstr rest;
47+
struct bstr rest = {0};
3348

3449
switch (param->type) {
3550
case GL_USER_SHADER_PARAM_UNKNOWN:
3651
mp_err(log, "Missing type for param '%.*s'\n", BSTR_P(param->name));
3752
return false;
3853
case GL_USER_SHADER_PARAM_INT:
3954
case GL_USER_SHADER_PARAM_DEFINE:
40-
v = bstrtoll(val, &rest, 10);
55+
v = resolve_enum_name(param, val);
56+
v = v >= 0 ? v : bstrtoll(val, &rest, 10);
4157
range[0] = INT_MIN;
4258
range[1] = INT_MAX;
4359
break;
@@ -99,6 +115,7 @@ static bool parse_param(struct mp_log *log, struct bstr *body,
99115

100116
struct gl_user_shader_param *param = &params[(*num_params)++];
101117
*param = (struct gl_user_shader_param){ .name = bstr_strip(line) };
118+
bool is_enum = false;
102119

103120
while (true) {
104121
*body = rest;
@@ -113,6 +130,47 @@ static bool parse_param(struct mp_log *log, struct bstr *body,
113130
}
114131

115132
if (!bstr_eatstart0(&line, "//!")) {
133+
if (is_enum) {
134+
int count = 0;
135+
struct bstr enum_rest = {
136+
.start = line.start,
137+
.len = rest.start + rest.len - line.start,
138+
};
139+
param->enum_body = enum_rest;
140+
141+
while (enum_rest.len > 0) {
142+
struct bstr next_line;
143+
struct bstr enum_line = bstr_strip(bstr_getline(enum_rest, &next_line));
144+
if (bstr_startswith0(enum_line, "//!"))
145+
break;
146+
if (next_line.len == 0)
147+
continue;
148+
enum_rest = next_line;
149+
count++;
150+
}
151+
152+
if (param->type != GL_USER_SHADER_PARAM_INT &&
153+
param->type != GL_USER_SHADER_PARAM_DEFINE)
154+
{
155+
mp_err(log, "ENUM parameter '%.*s' must be type int or DEFINE!\n",
156+
BSTR_P(param->name));
157+
return false;
158+
}
159+
if (count == 0) {
160+
mp_err(log, "ENUM parameter '%.*s' has no values!\n",
161+
BSTR_P(param->name));
162+
return false;
163+
}
164+
165+
param->enum_body.len -= enum_rest.len;
166+
param->has_min = true;
167+
param->has_max = true;
168+
param->max = count - 1;
169+
170+
*body = enum_rest;
171+
return true;
172+
}
173+
116174
if (!parse_shader_param_value(log, param, line, &param->initial))
117175
return false;
118176
param->value = param->initial;
@@ -132,6 +190,8 @@ static bool parse_param(struct mp_log *log, struct bstr *body,
132190
}
133191

134192
if (bstr_eatstart0(&line, "TYPE")) {
193+
line = bstr_strip(line);
194+
is_enum = bstr_eatstart0(&line, "ENUM");
135195
line = bstr_strip(line);
136196
if (bstr_equals0(line, "float")) {
137197
param->type = GL_USER_SHADER_PARAM_FLOAT;

video/out/gpu/user_shaders.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ struct gl_user_shader_param {
8080
double max;
8181
bool has_min;
8282
bool has_max;
83+
struct bstr enum_body;
8384
};
8485

8586
struct gl_user_shader_hook {

video/out/gpu/video.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,6 +2193,17 @@ static void user_hook(struct gl_video *p, struct image img,
21932193

21942194
for (int i = 0; i < shader->num_params; i++) {
21952195
const struct gl_user_shader_param *param = &shader->params[i];
2196+
2197+
struct bstr enum_rest = param->enum_body;
2198+
int idx = 0;
2199+
while (enum_rest.len > 0) {
2200+
struct bstr enum_line = bstr_strip(bstr_getline(enum_rest, &enum_rest));
2201+
if (enum_line.len == 0)
2202+
continue;
2203+
GLSLHF("#define %.*s %d\n", BSTR_P(enum_line), idx);
2204+
idx++;
2205+
}
2206+
21962207
double value = param->value;
21972208
get_param_dynamic(p, param->name, &value);
21982209

0 commit comments

Comments
 (0)