/
filter_draw.c
197 lines (156 loc) · 5.46 KB
/
filter_draw.c
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
/*****************************************************************************
* filter_draw.c
*****************************************************************************
* Copyright (C) 2020 VLC authors and VideoLAN
* Copyright (C) 2020 Videolabs
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_modules.h>
#include <vlc_opengl.h>
#include "filter_draw.h"
#include "filter.h"
#include "gl_api.h"
#include "gl_common.h"
#include "gl_util.h"
static const char *const filter_options[] = { "vflip", NULL };
struct sys {
GLuint program_id;
GLuint vbo;
struct {
GLint vertex_pos;
} loc;
};
static int
Draw(struct vlc_gl_filter *filter, const struct vlc_gl_input_meta *meta)
{
(void) meta;
struct sys *sys = filter->sys;
const opengl_vtable_t *vt = &filter->api->vt;
vt->UseProgram(sys->program_id);
struct vlc_gl_sampler *sampler = vlc_gl_filter_GetSampler(filter);
vlc_gl_sampler_Load(sampler);
vt->BindBuffer(GL_ARRAY_BUFFER, sys->vbo);
vt->EnableVertexAttribArray(sys->loc.vertex_pos);
vt->VertexAttribPointer(sys->loc.vertex_pos, 2, GL_FLOAT, GL_FALSE, 0,
(const void *) 0);
vt->Clear(GL_COLOR_BUFFER_BIT);
vt->DrawArrays(GL_TRIANGLE_STRIP, 0, 4);
return VLC_SUCCESS;
}
static void
Close(struct vlc_gl_filter *filter)
{
struct sys *sys = filter->sys;
const opengl_vtable_t *vt = &filter->api->vt;
vt->DeleteProgram(sys->program_id);
vt->DeleteBuffers(1, &sys->vbo);
free(sys);
}
int
vlc_gl_filter_draw_Open(struct vlc_gl_filter *filter,
const config_chain_t *config,
struct vlc_gl_tex_size *size_out)
{
(void) size_out;
struct sys *sys = filter->sys = malloc(sizeof(*sys));
if (!sys)
return VLC_EGENERIC;
struct vlc_gl_sampler *sampler = vlc_gl_filter_GetSampler(filter);
static const char *const VERTEX_SHADER_BODY =
"attribute vec2 vertex_pos;\n"
"varying vec2 tex_coords;\n"
"void main() {\n"
" gl_Position = vec4(vertex_pos, 0.0, 1.0);\n"
" tex_coords = vec2((vertex_pos.x + 1.0) / 2.0,\n"
" (vertex_pos.y + 1.0) / 2.0);\n"
"}\n";
static const char *const VERTEX_SHADER_BODY_VFLIP =
"attribute vec2 vertex_pos;\n"
"varying vec2 tex_coords;\n"
"void main() {\n"
" gl_Position = vec4(vertex_pos, 0.0, 1.0);\n"
" tex_coords = vec2((vertex_pos.x + 1.0) / 2.0,\n"
" (-vertex_pos.y + 1.0) / 2.0);\n"
"}\n";
static const char *const FRAGMENT_SHADER_BODY =
"varying vec2 tex_coords;\n"
"void main() {\n"
" gl_FragColor = vlc_texture(tex_coords);\n"
"}\n";
const char *extensions = sampler->shader.extensions
? sampler->shader.extensions : "";
const opengl_vtable_t *vt = &filter->api->vt;
const char *shader_version;
const char *shader_precision;
if (filter->api->is_gles)
{
shader_version = "#version 100\n";
shader_precision = "precision highp float;\n";
}
else
{
shader_version = "#version 120\n";
shader_precision = "";
}
config_ChainParse(filter, DRAW_CFG_PREFIX, filter_options, config);
bool vflip = var_InheritBool(filter, DRAW_CFG_PREFIX "vflip");
const char *vertex_shader[] = {
shader_version,
vflip ? VERTEX_SHADER_BODY_VFLIP : VERTEX_SHADER_BODY,
};
const char *fragment_shader[] = {
shader_version,
extensions,
shader_precision,
sampler->shader.body,
FRAGMENT_SHADER_BODY,
};
GLuint program_id =
vlc_gl_BuildProgram(VLC_OBJECT(filter), vt,
ARRAY_SIZE(vertex_shader), vertex_shader,
ARRAY_SIZE(fragment_shader), fragment_shader);
if (!program_id)
goto error;
vlc_gl_sampler_FetchLocations(sampler, program_id);
sys->program_id = program_id;
sys->loc.vertex_pos = vt->GetAttribLocation(program_id, "vertex_pos");
assert(sys->loc.vertex_pos != -1);
vt->GenBuffers(1, &sys->vbo);
static const GLfloat vertex_pos[] = {
-1, 1,
-1, -1,
1, 1,
1, -1,
};
vt->BindBuffer(GL_ARRAY_BUFFER, sys->vbo);
vt->BufferData(GL_ARRAY_BUFFER, sizeof(vertex_pos), vertex_pos,
GL_STATIC_DRAW);
vt->BindBuffer(GL_ARRAY_BUFFER, 0);
static const struct vlc_gl_filter_ops ops = {
.draw = Draw,
.close = Close,
};
filter->ops = &ops;
return VLC_SUCCESS;
error:
free(sys);
return VLC_EGENERIC;
}