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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
/*
Copyright (C) 2025 Carl Hetherington <cth@carlh.net>
This file is part of DCP-o-matic.
DCP-o-matic is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
DCP-o-matic 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gl_util.h"
#include "lib/compose.hpp"
#include "lib/dcpomatic_assert.h"
#include "lib/image.h"
#include <wx/wx.h>
using std::shared_ptr;
using std::string;
using namespace dcpomatic::gl;
/* This will only build on an new-enough wxWidgets: see the comment in gl_video_view.h */
#if wxCHECK_VERSION(3,1,0)
GLuint
dcpomatic::gl::compile_shader(GLenum type, char const* source)
{
auto shader = glCreateShader(type);
DCPOMATIC_ASSERT(shader);
GLchar const * src[] = { static_cast<GLchar const *>(source) };
glShaderSource(shader, 1, src, nullptr);
check_gl_error("glShaderSource");
glCompileShader(shader);
check_gl_error("glCompileShader");
GLint ok;
glGetShaderiv(shader, GL_COMPILE_STATUS, &ok);
if (!ok) {
GLint log_length;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_length);
string log;
if (log_length > 0) {
std::vector<char> log_char(log_length);
glGetShaderInfoLog(shader, log_length, nullptr, log_char.data());
log = string(log_char.data());
}
glDeleteShader(shader);
boost::throw_exception(GLError(String::compose("Could not compile shader (%1)", log).c_str(), -1));
}
return shader;
}
void
dcpomatic::gl::check_gl_error(char const * last)
{
GLenum const e = glGetError();
if (e != GL_NO_ERROR) {
boost::throw_exception(GLError(last, e));
}
}
Texture::Texture(GLint unpack_alignment, int unit)
: _unpack_alignment(unpack_alignment)
, _unit(unit)
{
glGenTextures(1, &_name);
check_gl_error("glGenTextures");
}
Texture::Texture(Texture&& other)
: _name(other._name)
, _unpack_alignment(other._unpack_alignment)
, _unit(other._unit)
, _size(std::move(other._size))
{
other._name = 0;
}
Texture&
Texture::operator=(Texture&& other)
{
if (this == &other) {
return *this;
}
_name = other._name;
_unpack_alignment = other._unpack_alignment;
_unit = other._unit;
_size = std::move(other._size);
other._name = 0;
return *this;
}
Texture::~Texture()
{
glDeleteTextures(1, &_name);
}
void
Texture::bind()
{
glActiveTexture(GL_TEXTURE0 + _unit);
check_gl_error("glActiveTexture");
glBindTexture(GL_TEXTURE_2D, _name);
check_gl_error("glBindTexture");
}
void
Texture::set(shared_ptr<const Image> image, int component)
{
auto const create = !_size || image->size() != _size;
_size = image->size();
glPixelStorei(GL_UNPACK_ALIGNMENT, _unpack_alignment);
check_gl_error("glPixelStorei");
DCPOMATIC_ASSERT(image->alignment() == Image::Alignment::COMPACT);
GLint internal_format;
GLenum format;
GLenum type;
int subsample = 1;
switch (image->pixel_format()) {
case AV_PIX_FMT_YUV420P:
internal_format = GL_R8;
format = GL_RED;
type = GL_UNSIGNED_BYTE;
subsample = component > 0 ? 2 : 1;
break;
case AV_PIX_FMT_BGRA:
internal_format = GL_RGBA8;
format = GL_BGRA;
type = GL_UNSIGNED_BYTE;
break;
case AV_PIX_FMT_RGBA:
internal_format = GL_RGBA8;
format = GL_RGBA;
type = GL_UNSIGNED_BYTE;
break;
case AV_PIX_FMT_RGB24:
internal_format = GL_RGBA8;
format = GL_RGB;
type = GL_UNSIGNED_BYTE;
break;
case AV_PIX_FMT_XYZ12:
internal_format = GL_RGBA12;
format = GL_RGB;
type = GL_UNSIGNED_SHORT;
break;
default:
boost::throw_exception(PixelFormatError("Texture::set", image->pixel_format()));
}
bind();
if (create) {
glTexImage2D(GL_TEXTURE_2D, 0, internal_format, _size->width / subsample, _size->height / subsample, 0, format, type, image->data()[component]);
check_gl_error("glTexImage2D");
} else {
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, _size->width / subsample, _size->height / subsample, format, type, image->data()[component]);
check_gl_error("glTexSubImage2D");
}
}
/** @param x x position in pixels where 0 is left and canvas_width is right on screen */
static
float
x_pixels_to_gl(wxSize const& canvas_size, int x) {
return (x * 2.0f / canvas_size.GetWidth()) - 1.0f;
}
/** @param y y position in pixels where 0 is top and canvas_height is bottom on screen */
static
float
y_pixels_to_gl(wxSize const& canvas_size, int y) {
return 1.0f - (y * 2.0f / canvas_size.GetHeight());
}
Coords::Rectangle::Rectangle(Coords& coords, int node, int index)
: _coords(coords)
, _node(node)
, _index(index)
{
auto& c = _coords._coords;
auto offset = c.size();
c.resize(c.size() + 5 * 4);
c[offset + 2] = 0.0f;
c[offset + 3] = 1.0f;
c[offset + 4] = 1.0f;
c[offset + 7] = 0.0f;
c[offset + 8] = 1.0f;
c[offset + 9] = 0.0f;
c[offset + 12] = 0.0f;
c[offset + 13] = 0.0f;
c[offset + 14] = 0.0f;
c[offset + 17] = 0.0f;
c[offset + 18] = 0.0f;
c[offset + 19] = 1.0f;
}
void
Coords::Rectangle::set(wxSize canvas_size, int x, int y, dcp::Size size)
{
set(canvas_size, x, y, wxSize(size.width, size.height));
}
void
Coords::Rectangle::set(wxSize canvas_size, int x, int y, wxSize size)
{
auto const x1 = x_pixels_to_gl(canvas_size, x);
auto const y1 = y_pixels_to_gl(canvas_size, y);
auto const x2 = x_pixels_to_gl(canvas_size, x + size.GetWidth());
auto const y2 = y_pixels_to_gl(canvas_size, y + size.GetHeight());
/* The texture coordinates here have to account for the fact that when we put images into the texture OpenGL
* expected us to start at the lower left but we actually started at the top left. So although the
* top of the texture is at 1.0 we pretend it's the other way round.
*/
auto& coords = _coords._coords;
auto const o = _node * 5;
DCPOMATIC_ASSERT((o + 16) < static_cast<int>(coords.size()));
// bottom right
coords[o] = x2;
coords[o + 1] = y2;
// top right
coords[o + 5] = x2;
coords[o + 6] = y1;
// top left
coords[o + 10] = x1;
coords[o + 11] = y1;
// bottom left
coords[o + 15] = x1;
coords[o + 16] = y2;
}
Coords::FilledRectangle::FilledRectangle(Coords& coords)
: Rectangle(coords, coords._nodes, coords._indices.size())
{
auto& indices = coords._indices;
auto& nodes = coords._nodes;
indices.push_back(nodes + 0);
indices.push_back(nodes + 1);
indices.push_back(nodes + 3);
indices.push_back(nodes + 1);
indices.push_back(nodes + 2);
indices.push_back(nodes + 3);
nodes += 4;
}
void
Coords::FilledRectangle::draw()
{
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, reinterpret_cast<void*>(_index * sizeof(int)));
check_gl_error("glDrawElements");
}
Coords::FilledRectangle
Coords::add_filled_rectangle()
{
DCPOMATIC_ASSERT(!_setup);
return FilledRectangle(*this);
}
Coords::OutlineRectangle::OutlineRectangle(Coords& coords)
: Rectangle(coords, coords._nodes, coords._indices.size())
{
auto& indices = _coords._indices;
auto& nodes = _coords._nodes;
indices.push_back(nodes + 0);
indices.push_back(nodes + 1);
indices.push_back(nodes + 1);
indices.push_back(nodes + 2);
indices.push_back(nodes + 2);
indices.push_back(nodes + 3);
indices.push_back(nodes + 3);
indices.push_back(nodes + 0);
nodes += 4;
}
void
Coords::OutlineRectangle::draw()
{
glDrawElements(GL_LINES, 8, GL_UNSIGNED_INT, reinterpret_cast<void*>(_index * sizeof(int)));
check_gl_error("glDrawElements");
}
Coords::OutlineRectangle
Coords::add_outline_rectangle()
{
DCPOMATIC_ASSERT(!_setup);
return OutlineRectangle(*this);
}
void
Coords::FilledRectangleGroup::add(Coords::FilledRectangle const& rectangle)
{
if (_index == -1) {
_index = rectangle.index();
} else {
DCPOMATIC_ASSERT(rectangle.index() == _index + _number);
}
_number += 6;
}
void
Coords::FilledRectangleGroup::draw()
{
glDrawElements(GL_TRIANGLES, _number, GL_UNSIGNED_INT, reinterpret_cast<void*>(_index * sizeof(int)));
check_gl_error("glDrawElements");
}
void
Coords::setup()
{
DCPOMATIC_ASSERT(!_setup);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, _indices.size() * sizeof(unsigned int), _indices.data(), GL_STATIC_DRAW);
check_gl_error("glBufferData");
glBufferData(GL_ARRAY_BUFFER, _nodes * 5 * sizeof(float), nullptr, GL_STATIC_DRAW);
check_gl_error("glBufferData");
_setup = true;
}
void
Coords::set_array_buffer()
{
DCPOMATIC_ASSERT(_setup);
glBufferSubData(GL_ARRAY_BUFFER, 0, _coords.size() * sizeof(float), _coords.data());
check_gl_error("glBufferSubData");
}
void
Uniform::setup(int program, char const* name)
{
_location = glGetUniformLocation(program, name);
check_gl_error("glGetUniformLocation");
}
void
UniformVec4f::set(float a, float b, float c, float d)
{
DCPOMATIC_ASSERT(_location != -1);
glUniform4f(_location, a, b, c, d);
check_gl_error("glUniform4f");
}
void
Uniform1i::set(int v)
{
DCPOMATIC_ASSERT(_location != -1);
glUniform1i(_location, v);
check_gl_error("glUniform1i");
}
#endif
|