forked from olive-editor/olive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderfunctions.cpp
850 lines (626 loc) · 29 KB
/
renderfunctions.cpp
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
/***
Olive - Non-Linear Video Editor
Copyright (C) 2019 Olive Team
This program 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 3 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "renderfunctions.h"
extern "C" {
#include <libavformat/avformat.h>
}
#include <QApplication>
#include <QDesktopWidget>
#include <QOpenGLExtraFunctions>
#include <QOpenGLBuffer>
#include <QOpenGLVertexArrayObject>
#include <QDebug>
#include "timeline/clip.h"
#include "timeline/sequence.h"
#include "project/media.h"
#include "nodes/oldeffectnode.h"
#include "project/footage.h"
#include "effects/transition.h"
#include "ui/collapsiblewidget.h"
#include "rendering/audio.h"
#include "global/math.h"
#include "global/timing.h"
#include "global/config.h"
#include "panels/timeline.h"
#include "qopenglshaderprogramptr.h"
#include "shadergenerators.h"
GLfloat olive::rendering::blit_vertices[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 0.0f,
-1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f
};
GLfloat olive::rendering::blit_texcoords[] = {
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 0.0,
0.0, 1.0,
1.0, 1.0
};
GLfloat olive::rendering::flipped_blit_texcoords[] = {
0.0, 1.0,
1.0, 1.0,
1.0, 0.0,
0.0, 1.0,
0.0, 0.0,
1.0, 0.0
};
void PrepareToDraw(QOpenGLFunctions* f) {
f->glGenerateMipmap(GL_TEXTURE_2D);
f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
}
void olive::rendering::Blit(QOpenGLShaderProgram* pipeline, bool flipped, QMatrix4x4 matrix) {
QOpenGLFunctions* func = QOpenGLContext::currentContext()->functions();
PrepareToDraw(func);
QOpenGLVertexArrayObject m_vao;
m_vao.create();
m_vao.bind();
QOpenGLBuffer m_vbo;
m_vbo.create();
m_vbo.bind();
m_vbo.allocate(blit_vertices, 18 * sizeof(GLfloat));
m_vbo.release();
QOpenGLBuffer m_vbo2;
m_vbo2.create();
m_vbo2.bind();
m_vbo2.allocate(flipped ? flipped_blit_texcoords : blit_texcoords, 12 * sizeof(GLfloat));
m_vbo2.release();
pipeline->bind();
pipeline->setUniformValue("mvp_matrix", matrix);
pipeline->setUniformValue("texture", 0);
GLuint vertex_location = pipeline->attributeLocation("a_position");
m_vbo.bind();
func->glEnableVertexAttribArray(vertex_location);
func->glVertexAttribPointer(vertex_location, 3, GL_FLOAT, GL_FALSE, 0, 0);
m_vbo.release();
GLuint tex_location = pipeline->attributeLocation("a_texcoord");
m_vbo2.bind();
func->glEnableVertexAttribArray(tex_location);
func->glVertexAttribPointer(tex_location, 2, GL_FLOAT, GL_FALSE, 0, 0);
m_vbo2.release();
func->glDrawArrays(GL_TRIANGLES, 0, 6);
pipeline->release();
}
void draw_clip(QOpenGLContext* ctx,
QOpenGLShaderProgram* pipeline,
GLuint fbo,
GLuint texture,
bool clear) {
ctx->functions()->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
if (clear) {
ctx->functions()->glClear(GL_COLOR_BUFFER_BIT);
}
ctx->functions()->glBindTexture(GL_TEXTURE_2D, texture);
olive::rendering::Blit(pipeline);
ctx->functions()->glBindTexture(GL_TEXTURE_2D, 0);
ctx->functions()->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
}
GLuint draw_clip(QOpenGLContext* ctx,
QOpenGLShaderProgram* pipeline,
const FramebufferObject& fbo,
GLuint texture,
bool clear) {
fbo.BindBuffer();
if (clear) {
ctx->functions()->glClear(GL_COLOR_BUFFER_BIT);
}
ctx->functions()->glBindTexture(GL_TEXTURE_2D, texture);
olive::rendering::Blit(pipeline);
ctx->functions()->glBindTexture(GL_TEXTURE_2D, 0);
fbo.ReleaseBuffer();
return fbo.texture();
}
void process_effect(QOpenGLContext* ctx,
QOpenGLShaderProgram* pipeline,
Clip* c,
OldEffectNode* e,
double timecode,
GLTextureCoords& coords,
GLuint& composite_texture,
bool& fbo_switcher,
bool& texture_failed,
int data) {
if (e->IsEnabled()) {
if (e->Flags() & OldEffectNode::CoordsFlag) {
e->process_coords(timecode, coords, data);
}
bool can_process_shaders = ((e->Flags() & OldEffectNode::ShaderFlag) && olive::runtime_config.shaders_are_enabled);
if (can_process_shaders || (e->Flags() & OldEffectNode::SuperimposeFlag)) {
if (!e->is_open()) {
e->open();
}
if (can_process_shaders && e->is_shader_linked()) {
for (int i=0;i<e->getIterations();i++) {
e->process_shader(timecode, coords, i);
composite_texture = draw_clip(ctx, e->GetShaderPipeline(), c->fbo.at(fbo_switcher), composite_texture, true);
fbo_switcher = !fbo_switcher;
}
}
if (e->Flags() & OldEffectNode::SuperimposeFlag) {
GLuint superimpose_texture = e->process_superimpose(ctx, timecode);
if (superimpose_texture == 0) {
qWarning() << "Superimpose texture was nullptr, retrying...";
texture_failed = true;
} else if (composite_texture == 0) {
// if there is no previous texture, just return the superimposes texture
// UNLESS this is a shader-extended superimpose effect in which case,
// we'll need to draw it below
composite_texture = superimpose_texture;
} else {
// if the source texture is not already a framebuffer texture,
// we'll need to make it one before drawing a superimpose effect on it
if (composite_texture != c->fbo.at(0).texture() && composite_texture != c->fbo.at(1).texture()) {
draw_clip(ctx, pipeline, c->fbo.at(!fbo_switcher), composite_texture, true);
}
composite_texture = draw_clip(ctx, pipeline, c->fbo.at(!fbo_switcher), superimpose_texture, false);
}
}
}
}
}
GLuint olive::rendering::compose_sequence(ComposeSequenceParams ¶ms) {
GLuint final_fbo = params.type == olive::kTypeVideo ? params.main_buffer->buffer() : 0;
Sequence* s = params.seq;
long playhead = s->playhead;
if (!params.nests.isEmpty()) {
for (int i=0;i<params.nests.size();i++) {
s = params.nests.at(i)->media()->to_sequence().get();
playhead += params.nests.at(i)->clip_in(true) - params.nests.at(i)->timeline_in(true);
playhead = rescale_frame_number(playhead, params.nests.at(i)->track()->sequence()->frame_rate(), s->frame_rate());
}
if (params.type == olive::kTypeVideo && !params.nests.last()->fbo.isEmpty()) {
params.nests.last()->fbo.at(0).BindBuffer();
params.ctx->functions()->glClear(GL_COLOR_BUFFER_BIT);
final_fbo = params.nests.last()->fbo.at(0).buffer();
}
}
int audio_track_count = 0;
QVector<Clip*> current_clips;
// loop through clips, find currently active, and sort by track
QVector<Clip*> sequence_clips = s->GetAllClips();
for (int i=0;i<sequence_clips.size();i++) {
Clip* c = sequence_clips.at(i);
if (c != nullptr) {
// if clip is video and we're processing video
if (c->type() == params.type) {
bool clip_is_active = false;
// is the clip a "footage" clip?
if (c->media() != nullptr && c->media()->get_type() == MEDIA_TYPE_FOOTAGE) {
Footage* m = c->media()->to_footage();
// does the clip have a valid media source?
if (!m->invalid && !(c->type() == olive::kTypeAudio && !is_audio_device_set())) {
// is the media process and ready?
if (m->ready) {
const FootageStream* ms = c->media_stream();
// does the media have a valid media stream source and is it active?
if (ms != nullptr && c->IsActiveAt(playhead)) {
// open if not open
if (!c->IsOpen()) {
c->Open();
}
clip_is_active = true;
// increment audio track count
if (c->type() == olive::kTypeAudio) audio_track_count++;
} else if (c->IsOpen()) {
// close the clip if it isn't active anymore
c->Close(false);
}
} else {
// media wasn't ready, schedule a redraw
params.texture_failed = true;
}
}
} else {
// if the clip is a nested sequence or null clip, just open it
if (c->IsActiveAt(playhead)) {
if (!c->IsOpen()) {
c->Open();
}
clip_is_active = true;
} else if (c->IsOpen()) {
c->Close(false);
}
}
// if the clip is active, added it to "current_clips", sorted by track
if (clip_is_active) {
bool added = false;
// track sorting is only necessary for video clips
// audio clips are mixed equally, so we skip sorting for those
if (params.type == olive::kTypeVideo) {
// insertion sort by track
for (int j=0;j<current_clips.size();j++) {
if (current_clips.at(j)->track() < c->track()) {
current_clips.insert(j, c);
added = true;
break;
}
}
}
if (!added) {
current_clips.append(c);
}
}
}
}
}
QMatrix4x4 projection;
if (params.type == olive::kTypeVideo) {
// set default coordinates based on the sequence, with 0 in the direct center
params.ctx->functions()->glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
int half_width = s->width()/2;
int half_height = s->height()/2;
projection.ortho(-half_width, half_width, -half_height, half_height, -1, 1);
}
// loop through current clips
for (int i=0;i<current_clips.size();i++) {
Clip* c = current_clips.at(i);
bool got_mutex = true;
if (params.wait_for_mutexes) {
// wait for clip to finish opening
c->state_change_lock.lock();
} else {
got_mutex = c->state_change_lock.tryLock();
}
if (got_mutex && c->IsOpen()) {
// if clip is a video clip
if (c->type() == olive::kTypeVideo) {
// textureID variable contains texture to be drawn on screen at the end
GLuint textureID = 0;
// store video source dimensions
int video_width = c->media_width();
int video_height = c->media_height();
// prepare framebuffers for backend drawing operations
if (c->fbo.isEmpty()) {
// create 3 fbos for nested sequences, 2 for most clips
int fbo_count = (c->media() != nullptr && c->media()->get_type() == MEDIA_TYPE_SEQUENCE) ? 3 : 2;
c->fbo.resize(fbo_count);
for (int j=0;j<fbo_count;j++) {
c->fbo[j].Create(params.ctx, video_width, video_height);
}
}
bool convert_frame_to_internal = false;
// if media is footage
if (c->media() != nullptr && c->media()->get_type() == MEDIA_TYPE_FOOTAGE) {
// retrieve video frame from cache and store it in c->texture
c->Cache(qMax(playhead, c->timeline_in(true)), false, params.nests, params.playback_speed);
if (!c->Retrieve()) {
params.texture_failed = true;
} else {
// retrieve ID from c->texture
textureID = c->texture;
}
if (textureID == 0) {
qWarning() << "Failed to create texture";
} else {
convert_frame_to_internal = true;
}
}
// if clip should actually be shown on screen in this frame
if (playhead >= c->timeline_in(true)
&& playhead < c->timeline_out(true)) {
// simple bool for switching between the two framebuffers
bool fbo_switcher = false;
params.ctx->functions()->glViewport(0, 0, video_width, video_height);
if (c->media() != nullptr) {
if (c->media()->get_type() == MEDIA_TYPE_SEQUENCE) {
// for a nested sequence, run this function again on that sequence and retrieve the texture
// add nested sequence to nest list
params.nests.append(c);
// compose sequence
textureID = compose_sequence(params);
// remove sequence from nest list
params.nests.removeLast();
// compose_sequence() would have written to this clip's fbo[0], so we switch to fbo[1]
fbo_switcher = !fbo_switcher;
} else if (c->media()->get_type() == MEDIA_TYPE_FOOTAGE) {
// Convert frame from source to linear colorspace
if (olive::config.enable_color_management)
{
// Convert texture to sequence's internal format
if (textureID != c->fbo.at(0).texture() && textureID != c->fbo.at(1).texture()) {
textureID = draw_clip(params.ctx, params.pipeline, c->fbo.at(fbo_switcher), textureID, true);
fbo_switcher = !fbo_switcher;
}
// Check if this clip has an OCIO shader set up or not
if (c->ocio_shader == nullptr) {
// Set default input colorspace
QString input_cs = OCIO::ROLE_SCENE_LINEAR;
if (c->media() != nullptr && c->media()->get_type() == MEDIA_TYPE_FOOTAGE) {
input_cs = c->media()->to_footage()->Colorspace();
}
// Try to get a shader based on the input color space to scene linear
try {
OCIO::ConstConfigRcPtr config = OCIO::GetCurrentConfig();
OCIO::ConstProcessorRcPtr processor = config->getProcessor(input_cs.toUtf8(),
OCIO::ROLE_SCENE_LINEAR);
c->ocio_shader = olive::shader::SetupOCIO(params.ctx,
c->ocio_lut_texture,
processor,
c->media()->to_footage()->alpha_is_associated);
} catch (OCIO::Exception& e) {
qWarning() << e.what();
}
}
// Ensure we got a shader, and if so, blit with it
if (c->ocio_shader != nullptr) {
textureID = olive::rendering::OCIOBlit(c->ocio_shader.get(),
c->ocio_lut_texture,
c->fbo.at(fbo_switcher),
textureID);
fbo_switcher = !fbo_switcher;
}
}
}
}
// set up default coordinates for drawing the clip
GLTextureCoords coords;
coords.vertex_top_left = QVector3D(-video_width/2, -video_height/2, 0.0f);
coords.vertex_top_right = QVector3D(video_width/2, -video_height/2, 0.0f);
coords.vertex_bottom_left = QVector3D(-video_width/2, video_height/2, 0.0f);
coords.vertex_bottom_right = QVector3D(video_width/2, video_height/2, 0.0f);
coords.texture_top_left = QVector2D(0.0f, 0.0f);
coords.texture_top_right = QVector2D(1.0f, 0.0f);
coords.texture_bottom_left = QVector2D(0.0f, 1.0f);
coords.texture_bottom_right = QVector2D(1.0f, 1.0f);
coords.opacity = 1.0;
// == EFFECT CODE START ==
// get current sequence time in seconds (used for effects)
double timecode = get_timecode(c, playhead);
// run through all of the clip's effects
for (int j=0;j<c->effects.size();j++) {
OldEffectNode* e = c->effects.at(j).get();
process_effect(params.ctx, params.pipeline, c, e, timecode, coords, textureID, fbo_switcher, params.texture_failed, kTransitionNone);
}
// if the clip has an opening transition, process that now
if (c->opening_transition != nullptr) {
int transition_progress = playhead - c->timeline_in(true);
if (transition_progress < c->opening_transition->get_length()) {
process_effect(params.ctx, params.pipeline, c, c->opening_transition.get(), double(transition_progress)/double(c->opening_transition->get_length()), coords, textureID, fbo_switcher, params.texture_failed, kTransitionOpening);
}
}
// if the clip has a closing transition, process that now
if (c->closing_transition != nullptr) {
int transition_progress = playhead - (c->timeline_out(true) - c->closing_transition->get_length());
if (transition_progress >= 0 && transition_progress < c->closing_transition->get_length()) {
process_effect(params.ctx, params.pipeline, c, c->closing_transition.get(), double(transition_progress)/double(c->closing_transition->get_length()), coords, textureID, fbo_switcher, params.texture_failed, kTransitionClosing);
}
}
// == EFFECT CODE END ==
// Check whether the parent clip is auto-scaled
if (c->autoscaled()
&& (video_width != s->width()
&& video_height != s->height())) {
float width_multiplier = float(s->width()) / float(video_width);
float height_multiplier = float(s->height()) / float(video_height);
float scale_multiplier = qMin(width_multiplier, height_multiplier);
coords.matrix.scale(scale_multiplier, scale_multiplier);
}
// Configure effect gizmos if they exist
if (params.gizmos != nullptr) {
// set correct gizmo coords at this matrix
params.gizmos->gizmo_draw(timecode, coords);
// convert gizmo coords to screen coords
params.gizmos->gizmo_world_to_screen(coords.matrix, projection);
}
if (textureID > 0) {
// set viewport to sequence size
params.ctx->functions()->glViewport(0, 0, s->width(), s->height());
// == START RENDER CLIP IN CONTEXT OF SEQUENCE ==
// use clip textures for nested sequences, otherwise use main frame buffers
GLuint back_buffer_1;
GLuint back_buffer_2;
GLuint backend_tex_1;
GLuint backend_tex_2;
GLuint comp_texture;
if (params.nests.size() > 0) {
back_buffer_1 = params.nests.last()->fbo[1].buffer();
back_buffer_2 = params.nests.last()->fbo[2].buffer();
backend_tex_1 = params.nests.last()->fbo[1].texture();
backend_tex_2 = params.nests.last()->fbo[2].texture();
comp_texture = params.nests.last()->fbo[0].texture();
} else {
back_buffer_1 = params.backend_buffer1->buffer();
back_buffer_2 = params.backend_buffer2->buffer();
backend_tex_1 = params.backend_buffer1->texture();
backend_tex_2 = params.backend_buffer2->texture();
comp_texture = params.main_buffer->texture();
}
// render a backbuffer
params.ctx->functions()->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, back_buffer_1);
params.ctx->functions()->glClearColor(0.0, 0.0, 0.0, 0.0);
params.ctx->functions()->glClear(GL_COLOR_BUFFER_BIT);
// bind final clip texture
params.ctx->functions()->glBindTexture(GL_TEXTURE_2D, textureID);
// set texture filter to bilinear
PrepareToDraw(params.ctx->functions());
// draw clip on screen according to gl coordinates
params.pipeline->bind();
params.pipeline->setUniformValue("mvp_matrix", projection * coords.matrix);
params.pipeline->setUniformValue("texture", 0);
params.pipeline->setUniformValue("opacity", coords.opacity);
GLfloat vertices[] = {
coords.vertex_top_left.x(), coords.vertex_top_left.y(), 0.0f,
coords.vertex_top_right.x(), coords.vertex_top_right.y(), 0.0f,
coords.vertex_bottom_right.x(), coords.vertex_bottom_right.y(), 0.0f,
coords.vertex_top_left.x(), coords.vertex_top_left.y(), 0.0f,
coords.vertex_bottom_left.x(), coords.vertex_bottom_left.y(), 0.0f,
coords.vertex_bottom_right.x(), coords.vertex_bottom_right.y(), 0.0f,
};
GLfloat texcoords[] = {
coords.texture_top_left.x(), coords.texture_top_left.y(),
coords.texture_top_right.x(), coords.texture_top_right.y(),
coords.texture_bottom_right.x(), coords.texture_bottom_right.y(),
coords.texture_top_left.x(), coords.texture_top_left.y(),
coords.texture_bottom_left.x(), coords.texture_bottom_left.y(),
coords.texture_bottom_right.x(), coords.texture_bottom_right.y(),
};
QOpenGLVertexArrayObject vao;
vao.create();
vao.bind();
QOpenGLBuffer vertex_buffer;
vertex_buffer.create();
vertex_buffer.bind();
vertex_buffer.allocate(vertices, 18 * sizeof(GLfloat));
vertex_buffer.release();
QOpenGLBuffer texcoord_buffer;
texcoord_buffer.create();
texcoord_buffer.bind();
texcoord_buffer.allocate(texcoords, 12 * sizeof(GLfloat));
texcoord_buffer.release();
GLuint vertex_location = params.pipeline->attributeLocation("a_position");
vertex_buffer.bind();
params.ctx->functions()->glEnableVertexAttribArray(vertex_location);
params.ctx->functions()->glVertexAttribPointer(vertex_location, 3, GL_FLOAT, GL_FALSE, 0, 0);
vertex_buffer.release();
GLuint tex_location = params.pipeline->attributeLocation("a_texcoord");
texcoord_buffer.bind();
params.ctx->functions()->glEnableVertexAttribArray(tex_location);
params.ctx->functions()->glVertexAttribPointer(tex_location, 2, GL_FLOAT, GL_FALSE, 0, 0);
texcoord_buffer.release();
params.ctx->functions()->glDrawArrays(GL_TRIANGLES, 0, 6);
params.pipeline->setUniformValue("opacity", 1.0f);
params.pipeline->release();
// release final clip texture
params.ctx->functions()->glBindTexture(GL_TEXTURE_2D, 0);
params.ctx->functions()->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
// == END RENDER CLIP IN CONTEXT OF SEQUENCE ==
//
//
// PROCESS POST-SHADERS
//
//
// copy front buffer to back buffer (only if we're using a blending mode)
/*
if (coords.blendmode >= 0) {
draw_clip(params.ctx, params.pipeline, back_buffer_2, comp_texture, true);
}
*/
// == START FINAL DRAW ON SEQUENCE BUFFER ==
// bind front buffer as draw buffer
params.ctx->functions()->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, final_fbo);
// Check if we're using a blend mode (< 0 means no blend mode)
//if (coords.blendmode < 0) {
params.ctx->functions()->glBindTexture(GL_TEXTURE_2D, backend_tex_1);
olive::rendering::Blit(params.pipeline);
params.ctx->functions()->glBindTexture(GL_TEXTURE_2D, 0);
//} else {
/*
// load background texture into texture unit 0
params.ctx->functions()->glActiveTexture(GL_TEXTURE0 + 0); // Texture unit 0
params.ctx->functions()->glBindTexture(GL_TEXTURE_2D, backend_tex_2);
// load foreground texture into texture unit 1
params.ctx->functions()->glActiveTexture(GL_TEXTURE0 + 1); // Texture unit 1
params.ctx->functions()->glBindTexture(GL_TEXTURE_2D, backend_tex_1);
// bind and configure blending mode shader
params.blend_mode_program->bind();
params.blend_mode_program->setUniformValue("blendmode", coords.blendmode);
params.blend_mode_program->setUniformValue("opacity", coords.opacity);
params.blend_mode_program->setUniformValue("background", 0);
params.blend_mode_program->setUniformValue("foreground", 1);
params.ctx->functions()->glClear(GL_COLOR_BUFFER_BIT);
olive::rendering::Blit(params.pipeline);
// release blend mode shader
params.blend_mode_program->release();
// unbind texture from texture unit 1
params.ctx->functions()->glBindTexture(GL_TEXTURE_2D, 0);
// unbind texture from texture unit 0
params.ctx->functions()->glActiveTexture(GL_TEXTURE0 + 0); // Texture unit 0
params.ctx->functions()->glBindTexture(GL_TEXTURE_2D, 0);
*/
//}
// unbind framebuffer
params.ctx->functions()->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
// == END FINAL DRAW ON SEQUENCE BUFFER ==
}
}
} else if (c->type() == olive::kTypeAudio) {
if (c->media() != nullptr && c->media()->get_type() == MEDIA_TYPE_SEQUENCE) {
params.nests.append(c);
compose_sequence(params);
params.nests.removeLast();
} else {
// Check whether cacher is currently active, if not activate it now
bool got_mutex2 = false;
if (params.wait_for_mutexes) {
c->cache_lock.lock();
got_mutex2 = true;
} else {
got_mutex2 = c->cache_lock.tryLock(got_mutex2);
}
if (got_mutex2) {
c->cache_lock.unlock();
c->Cache(playhead,
(params.viewer != nullptr && !params.viewer->playing),
params.nests,
params.playback_speed);
}
}
}
} else {
params.texture_failed = true;
}
if (got_mutex) {
c->state_change_lock.unlock();
}
}
if (audio_track_count == 0) {
WakeAudioWakeObject();
}
if (!params.nests.isEmpty() && !params.nests.last()->fbo.isEmpty()) {
// returns nested clip's texture
return params.nests.last()->fbo[0].texture();
}
return 0;
}
void olive::rendering::compose_audio(Viewer* viewer, Sequence* seq, int playback_speed, bool wait_for_mutexes) {
ComposeSequenceParams params;
params.viewer = viewer;
params.ctx = nullptr;
params.seq = seq;
params.type = olive::kTypeAudio;
params.gizmos = nullptr;
params.wait_for_mutexes = wait_for_mutexes;
params.playback_speed = playback_speed;
compose_sequence(params);
}
GLuint olive::rendering::OCIOBlit(QOpenGLShaderProgram *pipeline,
GLuint lut,
const FramebufferObject& fbo,
GLuint texture)
{
if (pipeline == nullptr) {
return 0;
}
QOpenGLContext* ctx = QOpenGLContext::currentContext();
QOpenGLExtraFunctions* xf = ctx->extraFunctions();
xf->glActiveTexture(GL_TEXTURE2);
xf->glBindTexture(GL_TEXTURE_3D, lut);
xf->glActiveTexture(GL_TEXTURE0);
pipeline->bind();
pipeline->setUniformValue("tex2", 2);
//textureID = draw_clip(params.ctx, pipeline, c->fbo.at(fbo_switcher), textureID, true);
GLuint textureID = draw_clip(ctx, pipeline, fbo, texture, true);
pipeline->release();
xf->glActiveTexture(GL_TEXTURE2);
xf->glBindTexture(GL_TEXTURE_3D, 0);
xf->glActiveTexture(GL_TEXTURE0);
return textureID;
}