move gl rendering to another thread
This commit is contained in:
@@ -0,0 +1,496 @@
|
||||
#include "renderfunctions.h"
|
||||
|
||||
#include <QOpenGLFramebufferObject>
|
||||
#include <QApplication>
|
||||
#include <QDesktopWidget>
|
||||
#include <QDebug>
|
||||
|
||||
#include "project/clip.h"
|
||||
#include "project/sequence.h"
|
||||
#include "project/media.h"
|
||||
#include "project/effect.h"
|
||||
#include "project/footage.h"
|
||||
#include "project/transition.h"
|
||||
|
||||
#include "ui/collapsiblewidget.h"
|
||||
|
||||
#include "playback/audio.h"
|
||||
#include "playback/playback.h"
|
||||
|
||||
#include "io/math.h"
|
||||
#include "io/config.h"
|
||||
#include "io/avtogl.h"
|
||||
|
||||
#include "panels/timeline.h"
|
||||
#include "panels/viewer.h"
|
||||
|
||||
extern "C" {
|
||||
#include <libavformat/avformat.h>
|
||||
}
|
||||
|
||||
#define GL_DEFAULT_BLEND glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE)
|
||||
|
||||
GLuint draw_clip(QOpenGLContext* ctx, QOpenGLFramebufferObject* fbo, GLuint texture, bool clear) {
|
||||
glPushMatrix();
|
||||
glLoadIdentity();
|
||||
glOrtho(0, 1, 0, 1, -1, 1);
|
||||
|
||||
GLint current_fbo = 0;
|
||||
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, ¤t_fbo);
|
||||
|
||||
fbo->bind();
|
||||
|
||||
if (clear) glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
// get current blend mode
|
||||
GLint src_rgb, src_alpha, dst_rgb, dst_alpha;
|
||||
glGetIntegerv(GL_BLEND_SRC_RGB, &src_rgb);
|
||||
glGetIntegerv(GL_BLEND_SRC_ALPHA, &src_alpha);
|
||||
glGetIntegerv(GL_BLEND_DST_RGB, &dst_rgb);
|
||||
glGetIntegerv(GL_BLEND_DST_ALPHA, &dst_alpha);
|
||||
|
||||
ctx->functions()->GL_DEFAULT_BLEND;
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glBegin(GL_QUADS);
|
||||
glTexCoord2f(0, 0); // top left
|
||||
glVertex2f(0, 0); // top left
|
||||
glTexCoord2f(1, 0); // top right
|
||||
glVertex2f(1, 0); // top right
|
||||
glTexCoord2f(1, 1); // bottom right
|
||||
glVertex2f(1, 1); // bottom right
|
||||
glTexCoord2f(0, 1); // bottom left
|
||||
glVertex2f(0, 1); // bottom left
|
||||
glEnd();
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
// fbo->release();
|
||||
ctx->functions()->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, current_fbo);
|
||||
|
||||
// restore previous blendFunc
|
||||
ctx->functions()->glBlendFuncSeparate(src_rgb, dst_rgb, src_alpha, dst_alpha);
|
||||
|
||||
//if (default_fbo != nullptr) default_fbo->bind();
|
||||
|
||||
glPopMatrix();
|
||||
return fbo->texture();
|
||||
}
|
||||
|
||||
void process_effect(QOpenGLContext* ctx,
|
||||
Clip* c,
|
||||
Effect* e,
|
||||
double timecode,
|
||||
GLTextureCoords& coords,
|
||||
GLuint& composite_texture,
|
||||
bool& fbo_switcher,
|
||||
int data) {
|
||||
if (e->is_enabled()) {
|
||||
if (e->enable_coords) {
|
||||
e->process_coords(timecode, coords, data);
|
||||
}
|
||||
if ((e->enable_shader && shaders_are_enabled) || e->enable_superimpose) {
|
||||
e->startEffect();
|
||||
if ((e->enable_shader && shaders_are_enabled) && e->is_glsl_linked()) {
|
||||
e->process_shader(timecode, coords);
|
||||
composite_texture = draw_clip(ctx, c->fbo[fbo_switcher], composite_texture, true);
|
||||
fbo_switcher = !fbo_switcher;
|
||||
}
|
||||
if (e->enable_superimpose) {
|
||||
GLuint superimpose_texture = e->process_superimpose(timecode);
|
||||
if (superimpose_texture == 0) {
|
||||
qWarning() << "Superimpose texture was nullptr, retrying...";
|
||||
texture_failed = true;
|
||||
} else {
|
||||
composite_texture = draw_clip(ctx, c->fbo[!fbo_switcher], superimpose_texture, false);
|
||||
}
|
||||
}
|
||||
e->endEffect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GLuint compose_sequence(Viewer* viewer,
|
||||
QOpenGLContext* ctx,
|
||||
Sequence* seq,
|
||||
QVector<Clip*>& nests,
|
||||
bool video,
|
||||
bool render_audio,
|
||||
Effect** gizmos) {
|
||||
GLint current_fbo = 0;
|
||||
if (video) {
|
||||
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, ¤t_fbo);
|
||||
}
|
||||
|
||||
bool drawn_gizmos = false;
|
||||
if (video && nests.isEmpty()) texture_failed = false;
|
||||
|
||||
Sequence* s = seq;
|
||||
long playhead = s->playhead;
|
||||
|
||||
if (!nests.isEmpty()) {
|
||||
for (int i=0;i<nests.size();i++) {
|
||||
s = nests.at(i)->media->to_sequence();
|
||||
playhead += nests.at(i)->clip_in - nests.at(i)->get_timeline_in_with_transition();
|
||||
playhead = refactor_frame_number(playhead, nests.at(i)->sequence->frame_rate, s->frame_rate);
|
||||
}
|
||||
|
||||
if (video && nests.last()->fbo != nullptr) {
|
||||
nests.last()->fbo[0]->bind();
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
// nests.last()->fbo[0]->release();
|
||||
ctx->functions()->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, current_fbo);
|
||||
}
|
||||
}
|
||||
|
||||
int audio_track_count = 0;
|
||||
|
||||
QVector<Clip*> current_clips;
|
||||
|
||||
for (int i=0;i<s->clips.size();i++) {
|
||||
Clip* c = s->clips.at(i);
|
||||
|
||||
// if clip starts within one second and/or hasn't finished yet
|
||||
if (c != nullptr) {
|
||||
// if (!(!nests.isEmpty() && !same_sign(c->track, nests.last()->track))) {
|
||||
if ((c->track < 0) == video) {
|
||||
bool clip_is_active = false;
|
||||
|
||||
if (c->media != nullptr && c->media->get_type() == MEDIA_TYPE_FOOTAGE) {
|
||||
Footage* m = c->media->to_footage();
|
||||
if (!m->invalid && !(c->track >= 0 && !is_audio_device_set())) {
|
||||
if (m->ready) {
|
||||
const FootageStream* ms = m->get_stream_from_file_index(c->track < 0, c->media_stream);
|
||||
if (ms != nullptr && is_clip_active(c, playhead)) {
|
||||
// if thread is already working, we don't want to touch this,
|
||||
// but we also don't want to hang the UI thread
|
||||
if (!c->open) {
|
||||
open_clip(c, !rendering);
|
||||
}
|
||||
clip_is_active = true;
|
||||
if (c->track >= 0) audio_track_count++;
|
||||
} else if (c->open) {
|
||||
close_clip(c, false);
|
||||
}
|
||||
} else {
|
||||
//qWarning() << "Media '" + m->name + "' was not ready, retrying...";
|
||||
texture_failed = true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (is_clip_active(c, playhead)) {
|
||||
if (!c->open) open_clip(c, !rendering);
|
||||
clip_is_active = true;
|
||||
} else if (c->open) {
|
||||
close_clip(c, false);
|
||||
}
|
||||
}
|
||||
if (clip_is_active) {
|
||||
bool added = false;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int half_width = s->width/2;
|
||||
int half_height = s->height/2;
|
||||
if (rendering || !nests.isEmpty()) half_height = -half_height; // invert vertical
|
||||
|
||||
glPushMatrix();
|
||||
glLoadIdentity();
|
||||
glOrtho(-half_width, half_width, half_height, -half_height, -1, 10);
|
||||
|
||||
for (int i=0;i<current_clips.size();i++) {
|
||||
ctx->functions()->GL_DEFAULT_BLEND;
|
||||
glColor4f(1.0, 1.0, 1.0, 1.0);
|
||||
|
||||
Clip* c = current_clips.at(i);
|
||||
|
||||
if (c->media != nullptr && c->media->get_type() == MEDIA_TYPE_FOOTAGE && !c->finished_opening) {
|
||||
qWarning() << "Tried to display clip" << i << "but it's closed";
|
||||
texture_failed = true;
|
||||
} else {
|
||||
if (c->track < 0) {
|
||||
GLuint textureID = 0;
|
||||
int video_width = c->getWidth();
|
||||
int video_height = c->getHeight();
|
||||
|
||||
if (c->media != nullptr) {
|
||||
switch (c->media->get_type()) {
|
||||
case MEDIA_TYPE_FOOTAGE:
|
||||
// set up opengl texture
|
||||
if (c->texture == nullptr) {
|
||||
c->texture = new QOpenGLTexture(QOpenGLTexture::Target2D);
|
||||
c->texture->setSize(c->stream->codecpar->width, c->stream->codecpar->height);
|
||||
c->texture->setFormat(get_gl_tex_fmt_from_av(c->pix_fmt));
|
||||
c->texture->setMipLevels(c->texture->maximumMipLevels());
|
||||
c->texture->setMinMagFilters(QOpenGLTexture::Linear, QOpenGLTexture::Linear);
|
||||
c->texture->allocateStorage(get_gl_pix_fmt_from_av(c->pix_fmt), QOpenGLTexture::UInt8);
|
||||
}
|
||||
get_clip_frame(c, playhead);
|
||||
textureID = c->texture->textureId();
|
||||
break;
|
||||
case MEDIA_TYPE_SEQUENCE:
|
||||
textureID = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (textureID == 0 && c->media != nullptr) {
|
||||
qWarning() << "Texture hasn't been created yet";
|
||||
texture_failed = true;
|
||||
} else if (playhead >= c->get_timeline_in_with_transition()) {
|
||||
glPushMatrix();
|
||||
|
||||
// start preparing cache
|
||||
if (c->fbo == nullptr) {
|
||||
c->fbo = new QOpenGLFramebufferObject* [2];
|
||||
c->fbo[0] = new QOpenGLFramebufferObject(video_width, video_height);
|
||||
c->fbo[1] = new QOpenGLFramebufferObject(video_width, video_height);
|
||||
}
|
||||
|
||||
// clear fbos
|
||||
/*c->fbo[0]->bind();
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
c->fbo[0]->release();
|
||||
c->fbo[1]->bind();
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
c->fbo[1]->release();*/
|
||||
|
||||
bool fbo_switcher = false;
|
||||
|
||||
glViewport(0, 0, video_width, video_height);
|
||||
|
||||
GLuint composite_texture;
|
||||
|
||||
if (c->media == nullptr) {
|
||||
c->fbo[fbo_switcher]->bind();
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
// c->fbo[fbo_switcher]->release();
|
||||
ctx->functions()->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, current_fbo);
|
||||
composite_texture = c->fbo[fbo_switcher]->texture();
|
||||
} else {
|
||||
// for nested sequences
|
||||
if (c->media->get_type()== MEDIA_TYPE_SEQUENCE) {
|
||||
nests.append(c);
|
||||
textureID = compose_sequence(viewer, ctx, seq, nests, video, render_audio, gizmos);
|
||||
nests.removeLast();
|
||||
fbo_switcher = true;
|
||||
}
|
||||
|
||||
composite_texture = draw_clip(ctx, c->fbo[fbo_switcher], textureID, true);
|
||||
}
|
||||
|
||||
fbo_switcher = !fbo_switcher;
|
||||
|
||||
// set up default coords
|
||||
GLTextureCoords coords;
|
||||
coords.grid_size = 1;
|
||||
coords.vertexTopLeftX = coords.vertexBottomLeftX = -video_width/2;
|
||||
coords.vertexTopLeftY = coords.vertexTopRightY = -video_height/2;
|
||||
coords.vertexTopRightX = coords.vertexBottomRightX = video_width/2;
|
||||
coords.vertexBottomLeftY = coords.vertexBottomRightY = video_height/2;
|
||||
coords.vertexBottomLeftZ = coords.vertexBottomRightZ = coords.vertexTopLeftZ = coords.vertexTopRightZ = 1;
|
||||
coords.textureTopLeftY = coords.textureTopRightY = coords.textureTopLeftX = coords.textureBottomLeftX = 0.0;
|
||||
coords.textureBottomLeftY = coords.textureBottomRightY = coords.textureTopRightX = coords.textureBottomRightX = 1.0;
|
||||
coords.textureTopLeftQ = coords.textureTopRightQ = coords.textureTopLeftQ = coords.textureBottomLeftQ = 1;
|
||||
|
||||
// set up autoscale
|
||||
if (c->autoscale && (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);
|
||||
glScalef(scale_multiplier, scale_multiplier, 1);
|
||||
}
|
||||
|
||||
// EFFECT CODE START
|
||||
double timecode = get_timecode(c, playhead);
|
||||
|
||||
Effect* first_gizmo_effect = nullptr;
|
||||
Effect* selected_effect = nullptr;
|
||||
|
||||
for (int j=0;j<c->effects.size();j++) {
|
||||
Effect* e = c->effects.at(j);
|
||||
process_effect(ctx, c, e, timecode, coords, composite_texture, fbo_switcher, TA_NO_TRANSITION);
|
||||
|
||||
if (e->are_gizmos_enabled()) {
|
||||
if (first_gizmo_effect == nullptr) first_gizmo_effect = e;
|
||||
if (e->container->selected) selected_effect = e;
|
||||
}
|
||||
}
|
||||
|
||||
if (!rendering) {
|
||||
if (selected_effect != nullptr) {
|
||||
gizmos = &selected_effect;
|
||||
} else if (is_clip_selected(c, true)) {
|
||||
gizmos = &first_gizmo_effect;
|
||||
}
|
||||
}
|
||||
|
||||
if (c->get_opening_transition() != nullptr) {
|
||||
int transition_progress = playhead - c->get_timeline_in_with_transition();
|
||||
if (transition_progress < c->get_opening_transition()->get_length()) {
|
||||
process_effect(ctx, c, c->get_opening_transition(), (double)transition_progress/(double)c->get_opening_transition()->get_length(), coords, composite_texture, fbo_switcher, TA_OPENING_TRANSITION);
|
||||
}
|
||||
}
|
||||
|
||||
if (c->get_closing_transition() != nullptr) {
|
||||
int transition_progress = playhead - (c->get_timeline_out_with_transition() - c->get_closing_transition()->get_length());
|
||||
if (transition_progress >= 0 && transition_progress < c->get_closing_transition()->get_length()) {
|
||||
process_effect(ctx, c, c->get_closing_transition(), (double)transition_progress/(double)c->get_closing_transition()->get_length(), coords, composite_texture, fbo_switcher, TA_CLOSING_TRANSITION);
|
||||
}
|
||||
}
|
||||
// EFFECT CODE END
|
||||
|
||||
/*if (!nests.isEmpty()) {
|
||||
nests.last()->fbo[0]->bind();
|
||||
glViewport(0, 0, s->width, s->height);
|
||||
} else if (rendering) {
|
||||
glViewport(0, 0, s->width, s->height);
|
||||
} else {
|
||||
int widget_width = width();
|
||||
int widget_height = height();
|
||||
|
||||
widget_width *= QApplication::desktop()->devicePixelRatio();
|
||||
widget_height *= QApplication::desktop()->devicePixelRatio();
|
||||
|
||||
glViewport(0, 0, widget_width, widget_height);
|
||||
}*/
|
||||
if (!nests.isEmpty()) {
|
||||
nests.last()->fbo[0]->bind();
|
||||
}
|
||||
glViewport(0, 0, s->width, s->height);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, composite_texture);
|
||||
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
glBegin(GL_QUADS);
|
||||
|
||||
if (coords.grid_size <= 1) {
|
||||
float z = 0.0f;
|
||||
|
||||
glTexCoord2f(coords.textureTopLeftX, coords.textureTopLeftY); // top left
|
||||
glVertex3f(coords.vertexTopLeftX, coords.vertexTopLeftY, z); // top left
|
||||
glTexCoord2f(coords.textureTopRightX, coords.textureTopRightY); // top right
|
||||
glVertex3f(coords.vertexTopRightX, coords.vertexTopRightY, z); // top right
|
||||
glTexCoord2f(coords.textureBottomRightX, coords.textureBottomRightY); // bottom right
|
||||
glVertex3f(coords.vertexBottomRightX, coords.vertexBottomRightY, z); // bottom right
|
||||
glTexCoord2f(coords.textureBottomLeftX, coords.textureBottomLeftY); // bottom left
|
||||
glVertex3f(coords.vertexBottomLeftX, coords.vertexBottomLeftY, z); // bottom left
|
||||
} else {
|
||||
float rows = coords.grid_size;
|
||||
float cols = coords.grid_size;
|
||||
|
||||
for (float k=0;k<rows;k++) {
|
||||
float row_prog = k/rows;
|
||||
float next_row_prog = (k+1)/rows;
|
||||
for (float j=0;j<cols;j++) {
|
||||
float col_prog = j/cols;
|
||||
float next_col_prog = (j+1)/cols;
|
||||
|
||||
float vertexTLX = float_lerp(coords.vertexTopLeftX, coords.vertexBottomLeftX, row_prog);
|
||||
float vertexTRX = float_lerp(coords.vertexTopRightX, coords.vertexBottomRightX, row_prog);
|
||||
float vertexBLX = float_lerp(coords.vertexTopLeftX, coords.vertexBottomLeftX, next_row_prog);
|
||||
float vertexBRX = float_lerp(coords.vertexTopRightX, coords.vertexBottomRightX, next_row_prog);
|
||||
|
||||
float vertexTLY = float_lerp(coords.vertexTopLeftY, coords.vertexTopRightY, col_prog);
|
||||
float vertexTRY = float_lerp(coords.vertexTopLeftY, coords.vertexTopRightY, next_col_prog);
|
||||
float vertexBLY = float_lerp(coords.vertexBottomLeftY, coords.vertexBottomRightY, col_prog);
|
||||
float vertexBRY = float_lerp(coords.vertexBottomLeftY, coords.vertexBottomRightY, next_col_prog);
|
||||
|
||||
glTexCoord2f(float_lerp(coords.textureTopLeftX, coords.textureTopRightX, col_prog), float_lerp(coords.textureTopLeftY, coords.textureBottomLeftY, row_prog)); // top left
|
||||
glVertex2f(float_lerp(vertexTLX, vertexTRX, col_prog), float_lerp(vertexTLY, vertexBLY, row_prog)); // top left
|
||||
glTexCoord2f(float_lerp(coords.textureTopLeftX, coords.textureTopRightX, next_col_prog), float_lerp(coords.textureTopRightY, coords.textureBottomRightY, row_prog)); // top right
|
||||
glVertex2f(float_lerp(vertexTLX, vertexTRX, next_col_prog), float_lerp(vertexTRY, vertexBRY, row_prog)); // top right
|
||||
glTexCoord2f(float_lerp(coords.textureBottomLeftX, coords.textureBottomRightX, next_col_prog), float_lerp(coords.textureTopRightY, coords.textureBottomRightY, next_row_prog)); // bottom right
|
||||
glVertex2f(float_lerp(vertexBLX, vertexBRX, next_col_prog), float_lerp(vertexTRY, vertexBRY, next_row_prog)); // bottom right
|
||||
glTexCoord2f(float_lerp(coords.textureBottomLeftX, coords.textureBottomRightX, col_prog), float_lerp(coords.textureTopLeftY, coords.textureBottomLeftY, next_row_prog)); // bottom left
|
||||
glVertex2f(float_lerp(vertexBLX, vertexBRX, col_prog), float_lerp(vertexTLY, vertexBLY, next_row_prog)); // bottom left
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
glEnd();
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0); // unbind texture
|
||||
|
||||
// TODO restore this function
|
||||
/*if (gizmos != nullptr && !drawn_gizmos && nests.isEmpty()) {
|
||||
(*gizmos)->gizmo_draw(timecode, coords); // set correct gizmo coords
|
||||
(*gizmos)->gizmo_world_to_screen();
|
||||
|
||||
drawn_gizmos = true;
|
||||
}*/
|
||||
|
||||
if (!nests.isEmpty()) {
|
||||
// nests.last()->fbo[0]->release();
|
||||
// if (default_fbo != nullptr) default_fbo->bind();
|
||||
ctx->functions()->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, current_fbo);
|
||||
}
|
||||
|
||||
glPopMatrix();
|
||||
|
||||
/*GLfloat motion_blur_frac = (GLfloat) motion_blur_prog / (GLfloat) motion_blur_lim;
|
||||
if (motion_blur_prog == 0) {
|
||||
glAccum(GL_LOAD, motion_blur_frac);
|
||||
} else {
|
||||
glAccum(GL_ACCUM, motion_blur_frac);
|
||||
}
|
||||
motion_blur_prog++;*/
|
||||
}
|
||||
} else {
|
||||
if (render_audio || (config.enable_audio_scrubbing && audio_scrub)) {
|
||||
if (c->media != nullptr && c->media->get_type() == MEDIA_TYPE_SEQUENCE) {
|
||||
nests.append(c);
|
||||
compose_sequence(viewer, ctx, seq, nests, video, render_audio, gizmos);
|
||||
nests.removeLast();
|
||||
} else {
|
||||
if (c->lock.tryLock()) {
|
||||
// clip is not caching, start caching audio
|
||||
cache_clip(c, playhead, c->audio_reset, !render_audio, nests);
|
||||
c->lock.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// visually update all the keyframe values
|
||||
if (c->sequence == seq) { // only if you can currently see them
|
||||
double ts = (playhead - c->get_timeline_in_with_transition() + c->get_clip_in_with_transition())/s->frame_rate;
|
||||
for (int i=0;i<c->effects.size();i++) {
|
||||
Effect* e = c->effects.at(i);
|
||||
for (int j=0;j<e->row_count();j++) {
|
||||
EffectRow* r = e->row(j);
|
||||
for (int k=0;k<r->fieldCount();k++) {
|
||||
r->field(k)->validate_keyframe_data(ts);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (audio_track_count == 0 && viewer != nullptr) {
|
||||
viewer->play_wake();
|
||||
}
|
||||
|
||||
glPopMatrix();
|
||||
|
||||
if (!nests.isEmpty() && nests.last()->fbo != nullptr) {
|
||||
// returns nested clip's texture
|
||||
return nests.last()->fbo[0]->texture();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef RENDERFUNCTIONS_H
|
||||
#define RENDERFUNCTIONS_H
|
||||
|
||||
#include <QOpenGLContext>
|
||||
#include <QVector>
|
||||
|
||||
class Effect;
|
||||
class Viewer;
|
||||
struct Sequence;
|
||||
struct Clip;
|
||||
|
||||
GLuint compose_sequence(Viewer* viewer,
|
||||
QOpenGLContext* ctx,
|
||||
Sequence* seq,
|
||||
QVector<Clip*>& nests,
|
||||
bool video,
|
||||
bool render_audio,
|
||||
Effect **gizmos);
|
||||
|
||||
#endif // RENDERFUNCTIONS_H
|
||||
@@ -0,0 +1,111 @@
|
||||
#include "renderthread.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QImage>
|
||||
#include <QOpenGLFunctions>
|
||||
#include <QDebug>
|
||||
|
||||
#include "ui/renderfunctions.h"
|
||||
|
||||
RenderThread::RenderThread() :
|
||||
share_ctx(nullptr),
|
||||
ctx(nullptr),
|
||||
frameBuffer(0),
|
||||
texColorBuffer(0)
|
||||
{
|
||||
surface.create();
|
||||
}
|
||||
|
||||
RenderThread::~RenderThread() {
|
||||
surface.destroy();
|
||||
}
|
||||
|
||||
void RenderThread::run() {
|
||||
mutex.lock();
|
||||
|
||||
bool running = true;
|
||||
|
||||
while (running) {
|
||||
waitCond.wait(&mutex);
|
||||
|
||||
if (share_ctx != nullptr) {
|
||||
if (ctx == nullptr) {
|
||||
ctx = new QOpenGLContext();
|
||||
ctx->setFormat(share_ctx->format());
|
||||
ctx->setShareContext(share_ctx);
|
||||
ctx->create();
|
||||
ctx->makeCurrent(&surface);
|
||||
}
|
||||
|
||||
if (ctx != nullptr) {
|
||||
// gen fbo
|
||||
if (frameBuffer == 0) {
|
||||
ctx->functions()->glGenFramebuffers(1, &frameBuffer);
|
||||
}
|
||||
|
||||
// bind
|
||||
ctx->functions()->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, frameBuffer);
|
||||
|
||||
// gen texture
|
||||
if (texColorBuffer == 0) {
|
||||
glGenTextures(1, &texColorBuffer);
|
||||
glBindTexture(GL_TEXTURE_2D, texColorBuffer);
|
||||
glTexImage2D(
|
||||
GL_TEXTURE_2D, 0, GL_RGB, 1920, 1080, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr
|
||||
);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
ctx->functions()->glFramebufferTexture2D(
|
||||
GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texColorBuffer, 0
|
||||
);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
// draw
|
||||
paint();
|
||||
|
||||
// flush changes
|
||||
glFlush();
|
||||
|
||||
// release
|
||||
ctx->functions()->glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ctx != nullptr) {
|
||||
if (frameBuffer != 0) ctx->functions()->glDeleteFramebuffers(1, &frameBuffer);
|
||||
ctx->doneCurrent();
|
||||
delete ctx;
|
||||
}
|
||||
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
void RenderThread::paint() {
|
||||
glClearColor(0, 0, 0, 1);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glLoadIdentity();
|
||||
|
||||
glClearColor(0, 0, 0, 0);
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glEnable(GL_BLEND);
|
||||
glEnable(GL_DEPTH);
|
||||
|
||||
Effect* gizmos; // does nothing yet
|
||||
QVector<Clip*> nests;
|
||||
|
||||
compose_sequence(nullptr, ctx, seq, nests, true, false, &gizmos);
|
||||
|
||||
glDisable(GL_DEPTH);
|
||||
glDisable(GL_BLEND);
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
}
|
||||
|
||||
void RenderThread::start_render(QOpenGLContext *share, Sequence *s) {
|
||||
share_ctx = share;
|
||||
seq = s;
|
||||
waitCond.wakeAll();
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef RENDERTHREAD_H
|
||||
#define RENDERTHREAD_H
|
||||
|
||||
#include <QThread>
|
||||
#include <QMutex>
|
||||
#include <QWaitCondition>
|
||||
#include <QOffscreenSurface>
|
||||
#include <QOpenGLContext>
|
||||
#include <QOpenGLFramebufferObject>
|
||||
|
||||
struct Sequence;
|
||||
|
||||
class RenderThread : public QThread {
|
||||
public:
|
||||
RenderThread();
|
||||
~RenderThread();
|
||||
void run();
|
||||
QMutex mutex;
|
||||
GLuint frameBuffer;
|
||||
GLuint texColorBuffer;
|
||||
void paint();
|
||||
void start_render(QOpenGLContext* share, Sequence* s);
|
||||
private:
|
||||
QWaitCondition waitCond;
|
||||
QOffscreenSurface surface;
|
||||
QOpenGLContext* share_ctx;
|
||||
QOpenGLContext* ctx;
|
||||
Sequence* seq;
|
||||
};
|
||||
|
||||
#endif // RENDERTHREAD_H
|
||||
@@ -98,7 +98,7 @@ void TimelineWidget::show_context_menu(const QPoint& pos) {
|
||||
QVector<Clip*> selected_clips;
|
||||
for (int i=0;i<sequence->clips.size();i++) {
|
||||
Clip* c = sequence->clips.at(i);
|
||||
if (c != nullptr && panel_timeline->is_clip_selected(c, true)) {
|
||||
if (c != nullptr && is_clip_selected(c, true)) {
|
||||
selected_clips.append(c);
|
||||
}
|
||||
}
|
||||
@@ -204,7 +204,7 @@ void TimelineWidget::toggle_autoscale() {
|
||||
SetAutoscaleAction* action = new SetAutoscaleAction();
|
||||
for (int i=0;i<sequence->clips.size();i++) {
|
||||
Clip* c = sequence->clips.at(i);
|
||||
if (c != nullptr && panel_timeline->is_clip_selected(c, true)) {
|
||||
if (c != nullptr && is_clip_selected(c, true)) {
|
||||
action->clips.append(c);
|
||||
}
|
||||
}
|
||||
@@ -237,7 +237,7 @@ void TimelineWidget::rename_clip() {
|
||||
QVector<Clip*> selected_clips;
|
||||
for (int i=0;i<sequence->clips.size();i++) {
|
||||
Clip* c = sequence->clips.at(i);
|
||||
if (c != nullptr && panel_timeline->is_clip_selected(c, true)) {
|
||||
if (c != nullptr && is_clip_selected(c, true)) {
|
||||
selected_clips.append(c);
|
||||
}
|
||||
}
|
||||
@@ -629,7 +629,7 @@ void TimelineWidget::mousePressEvent(QMouseEvent *event) {
|
||||
if (clip_index >= 0) {
|
||||
Clip* clip = sequence->clips.at(clip_index);
|
||||
if (clip != nullptr) {
|
||||
if (panel_timeline->is_clip_selected(clip, true)) {
|
||||
if (is_clip_selected(clip, true)) {
|
||||
if (shift) {
|
||||
panel_timeline->deselect_area(clip->timeline_in, clip->timeline_out, clip->track);
|
||||
|
||||
@@ -695,7 +695,7 @@ void TimelineWidget::mousePressEvent(QMouseEvent *event) {
|
||||
if (!alt && panel_timeline->transition_select == TA_NO_TRANSITION) {
|
||||
for (int i=0;i<clip->linked.size();i++) {
|
||||
Clip* link = sequence->clips.at(clip->linked.at(i));
|
||||
if (!panel_timeline->is_clip_selected(link, true)) {
|
||||
if (!is_clip_selected(link, true)) {
|
||||
Selection ss;
|
||||
ss.in = link->timeline_in;
|
||||
ss.out = link->timeline_out;
|
||||
@@ -1696,7 +1696,7 @@ void TimelineWidget::mouseMoveEvent(QMouseEvent *event) {
|
||||
Ghost g;
|
||||
g.transition = nullptr;
|
||||
|
||||
bool add = panel_timeline->is_clip_selected(c, true);
|
||||
bool add = is_clip_selected(c, true);
|
||||
|
||||
// if a whole clip is not selected, maybe just a transition is
|
||||
if (panel_timeline->tool == TIMELINE_TOOL_POINTER && (c->get_opening_transition() != nullptr || c->get_closing_transition() != nullptr)) {
|
||||
@@ -1838,7 +1838,7 @@ void TimelineWidget::mouseMoveEvent(QMouseEvent *event) {
|
||||
|
||||
for (int i=0;i<sequence->clips.size();i++) {
|
||||
Clip* c = sequence->clips.at(i);
|
||||
if (c != nullptr && !panel_timeline->is_clip_selected(c, true)) {
|
||||
if (c != nullptr && !is_clip_selected(c, true)) {
|
||||
bool clip_is_post = (c->timeline_in >= axis);
|
||||
|
||||
// see if this a clip on this track is already in the list, and if it's closer
|
||||
|
||||
+61
-438
@@ -21,6 +21,8 @@
|
||||
#include "ui/viewercontainer.h"
|
||||
#include "io/avtogl.h"
|
||||
#include "ui/timelinewidget.h"
|
||||
#include "ui/renderfunctions.h"
|
||||
#include "ui/renderthread.h"
|
||||
|
||||
#include <QPainter>
|
||||
#include <QAudioOutput>
|
||||
@@ -42,8 +44,6 @@ extern "C" {
|
||||
#include <libavformat/avformat.h>
|
||||
}
|
||||
|
||||
#define GL_DEFAULT_BLEND glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
|
||||
|
||||
ViewerWidget::ViewerWidget(QWidget *parent) :
|
||||
QOpenGLWidget(parent),
|
||||
default_fbo(nullptr),
|
||||
@@ -51,6 +51,7 @@ ViewerWidget::ViewerWidget(QWidget *parent) :
|
||||
waveform_zoom(1.0),
|
||||
waveform_scroll(0),
|
||||
dragging(false),
|
||||
gizmos(nullptr),
|
||||
selected_gizmo(nullptr)
|
||||
{
|
||||
setMouseTracking(true);
|
||||
@@ -66,6 +67,9 @@ ViewerWidget::ViewerWidget(QWidget *parent) :
|
||||
|
||||
setContextMenuPolicy(Qt::CustomContextMenu);
|
||||
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(show_context_menu()));
|
||||
|
||||
renderer = new RenderThread();
|
||||
renderer->start(QThread::HighPriority);
|
||||
}
|
||||
|
||||
void ViewerWidget::delete_function() {
|
||||
@@ -387,442 +391,61 @@ void ViewerWidget::drawTitleSafeArea() {
|
||||
glEnd();
|
||||
}
|
||||
|
||||
GLuint ViewerWidget::draw_clip(QOpenGLFramebufferObject* fbo, GLuint texture, bool clear) {
|
||||
glPushMatrix();
|
||||
glLoadIdentity();
|
||||
glOrtho(0, 1, 0, 1, -1, 1);
|
||||
|
||||
fbo->bind();
|
||||
|
||||
if (clear) glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
// get current blend mode
|
||||
GLint src_rgb, src_alpha, dst_rgb, dst_alpha;
|
||||
glGetIntegerv(GL_BLEND_SRC_RGB, &src_rgb);
|
||||
glGetIntegerv(GL_BLEND_SRC_ALPHA, &src_alpha);
|
||||
glGetIntegerv(GL_BLEND_DST_RGB, &dst_rgb);
|
||||
glGetIntegerv(GL_BLEND_DST_ALPHA, &dst_alpha);
|
||||
|
||||
GL_DEFAULT_BLEND
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glBegin(GL_QUADS);
|
||||
glTexCoord2f(0, 0); // top left
|
||||
glVertex2f(0, 0); // top left
|
||||
glTexCoord2f(1, 0); // top right
|
||||
glVertex2f(1, 0); // top right
|
||||
glTexCoord2f(1, 1); // bottom right
|
||||
glVertex2f(1, 1); // bottom right
|
||||
glTexCoord2f(0, 1); // bottom left
|
||||
glVertex2f(0, 1); // bottom left
|
||||
glEnd();
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
fbo->release();
|
||||
|
||||
// restore previous blendFunc
|
||||
glBlendFuncSeparate(src_rgb, dst_rgb, src_alpha, dst_alpha);
|
||||
|
||||
if (default_fbo != nullptr) default_fbo->bind();
|
||||
|
||||
glPopMatrix();
|
||||
return fbo->texture();
|
||||
}
|
||||
|
||||
void ViewerWidget::process_effect(Clip* c, Effect* e, double timecode, GLTextureCoords& coords, GLuint& composite_texture, bool& fbo_switcher, int data) {
|
||||
if (e->is_enabled()) {
|
||||
if (e->enable_coords) {
|
||||
e->process_coords(timecode, coords, data);
|
||||
}
|
||||
if ((e->enable_shader && shaders_are_enabled) || e->enable_superimpose) {
|
||||
e->startEffect();
|
||||
if ((e->enable_shader && shaders_are_enabled) && e->is_glsl_linked()) {
|
||||
e->process_shader(timecode, coords);
|
||||
composite_texture = draw_clip(c->fbo[fbo_switcher], composite_texture, true);
|
||||
fbo_switcher = !fbo_switcher;
|
||||
}
|
||||
if (e->enable_superimpose) {
|
||||
GLuint superimpose_texture = e->process_superimpose(timecode);
|
||||
if (superimpose_texture == 0) {
|
||||
qWarning() << "Superimpose texture was nullptr, retrying...";
|
||||
texture_failed = true;
|
||||
} else {
|
||||
composite_texture = draw_clip(c->fbo[!fbo_switcher], superimpose_texture, false);
|
||||
}
|
||||
}
|
||||
e->endEffect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int motion_blur_prog = 0;
|
||||
int motion_blur_lim = 4;
|
||||
|
||||
GLuint ViewerWidget::compose_sequence(QVector<Clip*>& nests, bool render_audio) {
|
||||
Sequence* s = viewer->seq;
|
||||
long playhead = s->playhead;
|
||||
|
||||
if (!nests.isEmpty()) {
|
||||
for (int i=0;i<nests.size();i++) {
|
||||
s = nests.at(i)->media->to_sequence();
|
||||
playhead += nests.at(i)->clip_in - nests.at(i)->get_timeline_in_with_transition();
|
||||
playhead = refactor_frame_number(playhead, nests.at(i)->sequence->frame_rate, s->frame_rate);
|
||||
}
|
||||
|
||||
if (nests.last()->fbo != nullptr) {
|
||||
nests.last()->fbo[0]->bind();
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
nests.last()->fbo[0]->release();
|
||||
}
|
||||
}
|
||||
|
||||
int audio_track_count = 0;
|
||||
|
||||
QVector<Clip*> current_clips;
|
||||
|
||||
for (int i=0;i<s->clips.size();i++) {
|
||||
Clip* c = s->clips.at(i);
|
||||
|
||||
// if clip starts within one second and/or hasn't finished yet
|
||||
if (c != nullptr) {
|
||||
if (!(!nests.isEmpty() && !same_sign(c->track, nests.last()->track))) {
|
||||
bool clip_is_active = false;
|
||||
|
||||
if (c->media != nullptr && c->media->get_type() == MEDIA_TYPE_FOOTAGE) {
|
||||
Footage* m = c->media->to_footage();
|
||||
if (!m->invalid && !(c->track >= 0 && !is_audio_device_set())) {
|
||||
if (m->ready) {
|
||||
const FootageStream* ms = m->get_stream_from_file_index(c->track < 0, c->media_stream);
|
||||
if (ms != nullptr && is_clip_active(c, playhead)) {
|
||||
// if thread is already working, we don't want to touch this,
|
||||
// but we also don't want to hang the UI thread
|
||||
if (!c->open) {
|
||||
open_clip(c, !rendering);
|
||||
}
|
||||
clip_is_active = true;
|
||||
if (c->track >= 0) audio_track_count++;
|
||||
} else if (c->open) {
|
||||
close_clip(c, false);
|
||||
}
|
||||
} else {
|
||||
//qWarning() << "Media '" + m->name + "' was not ready, retrying...";
|
||||
texture_failed = true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (is_clip_active(c, playhead)) {
|
||||
if (!c->open) open_clip(c, !rendering);
|
||||
clip_is_active = true;
|
||||
} else if (c->open) {
|
||||
close_clip(c, false);
|
||||
}
|
||||
}
|
||||
if (clip_is_active) {
|
||||
bool added = false;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int half_width = s->width/2;
|
||||
int half_height = s->height/2;
|
||||
if (rendering || !nests.isEmpty()) half_height = -half_height; // invert vertical
|
||||
|
||||
glPushMatrix();
|
||||
glLoadIdentity();
|
||||
glOrtho(-half_width, half_width, half_height, -half_height, -1, 10);
|
||||
|
||||
for (int i=0;i<current_clips.size();i++) {
|
||||
GL_DEFAULT_BLEND
|
||||
glColor4f(1.0, 1.0, 1.0, 1.0);
|
||||
|
||||
Clip* c = current_clips.at(i);
|
||||
|
||||
if (c->media != nullptr && c->media->get_type() == MEDIA_TYPE_FOOTAGE && !c->finished_opening) {
|
||||
qWarning() << "Tried to display clip" << i << "but it's closed";
|
||||
texture_failed = true;
|
||||
} else {
|
||||
if (c->track < 0) {
|
||||
GLuint textureID = 0;
|
||||
int video_width = c->getWidth();
|
||||
int video_height = c->getHeight();
|
||||
|
||||
if (c->media != nullptr) {
|
||||
switch (c->media->get_type()) {
|
||||
case MEDIA_TYPE_FOOTAGE:
|
||||
// set up opengl texture
|
||||
if (c->texture == nullptr) {
|
||||
c->texture = new QOpenGLTexture(QOpenGLTexture::Target2D);
|
||||
c->texture->setSize(c->stream->codecpar->width, c->stream->codecpar->height);
|
||||
c->texture->setFormat(get_gl_tex_fmt_from_av(c->pix_fmt));
|
||||
c->texture->setMipLevels(c->texture->maximumMipLevels());
|
||||
c->texture->setMinMagFilters(QOpenGLTexture::Linear, QOpenGLTexture::Linear);
|
||||
c->texture->allocateStorage(get_gl_pix_fmt_from_av(c->pix_fmt), QOpenGLTexture::UInt8);
|
||||
}
|
||||
get_clip_frame(c, playhead);
|
||||
textureID = c->texture->textureId();
|
||||
break;
|
||||
case MEDIA_TYPE_SEQUENCE:
|
||||
textureID = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (textureID == 0 && c->media != nullptr) {
|
||||
qWarning() << "Texture hasn't been created yet";
|
||||
texture_failed = true;
|
||||
} else if (playhead >= c->get_timeline_in_with_transition()) {
|
||||
glPushMatrix();
|
||||
|
||||
// start preparing cache
|
||||
if (c->fbo == nullptr) {
|
||||
c->fbo = new QOpenGLFramebufferObject* [2];
|
||||
c->fbo[0] = new QOpenGLFramebufferObject(video_width, video_height);
|
||||
c->fbo[1] = new QOpenGLFramebufferObject(video_width, video_height);
|
||||
}
|
||||
|
||||
// clear fbos
|
||||
/*c->fbo[0]->bind();
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
c->fbo[0]->release();
|
||||
c->fbo[1]->bind();
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
c->fbo[1]->release();*/
|
||||
|
||||
bool fbo_switcher = false;
|
||||
|
||||
glViewport(0, 0, video_width, video_height);
|
||||
|
||||
GLuint composite_texture;
|
||||
|
||||
if (c->media == nullptr) {
|
||||
c->fbo[fbo_switcher]->bind();
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
c->fbo[fbo_switcher]->release();
|
||||
composite_texture = c->fbo[fbo_switcher]->texture();
|
||||
} else {
|
||||
// for nested sequences
|
||||
if (c->media->get_type()== MEDIA_TYPE_SEQUENCE) {
|
||||
nests.append(c);
|
||||
textureID = compose_sequence(nests, render_audio);
|
||||
nests.removeLast();
|
||||
fbo_switcher = true;
|
||||
}
|
||||
|
||||
composite_texture = draw_clip(c->fbo[fbo_switcher], textureID, true);
|
||||
}
|
||||
|
||||
fbo_switcher = !fbo_switcher;
|
||||
|
||||
// set up default coords
|
||||
GLTextureCoords coords;
|
||||
coords.grid_size = 1;
|
||||
coords.vertexTopLeftX = coords.vertexBottomLeftX = -video_width/2;
|
||||
coords.vertexTopLeftY = coords.vertexTopRightY = -video_height/2;
|
||||
coords.vertexTopRightX = coords.vertexBottomRightX = video_width/2;
|
||||
coords.vertexBottomLeftY = coords.vertexBottomRightY = video_height/2;
|
||||
coords.vertexBottomLeftZ = coords.vertexBottomRightZ = coords.vertexTopLeftZ = coords.vertexTopRightZ = 1;
|
||||
coords.textureTopLeftY = coords.textureTopRightY = coords.textureTopLeftX = coords.textureBottomLeftX = 0.0;
|
||||
coords.textureBottomLeftY = coords.textureBottomRightY = coords.textureTopRightX = coords.textureBottomRightX = 1.0;
|
||||
coords.textureTopLeftQ = coords.textureTopRightQ = coords.textureTopLeftQ = coords.textureBottomLeftQ = 1;
|
||||
|
||||
// set up autoscale
|
||||
if (c->autoscale && (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);
|
||||
glScalef(scale_multiplier, scale_multiplier, 1);
|
||||
}
|
||||
|
||||
// EFFECT CODE START
|
||||
double timecode = get_timecode(c, playhead);
|
||||
|
||||
Effect* first_gizmo_effect = nullptr;
|
||||
Effect* selected_effect = nullptr;
|
||||
|
||||
for (int j=0;j<c->effects.size();j++) {
|
||||
Effect* e = c->effects.at(j);
|
||||
process_effect(c, e, timecode, coords, composite_texture, fbo_switcher, TA_NO_TRANSITION);
|
||||
|
||||
if (e->are_gizmos_enabled()) {
|
||||
if (first_gizmo_effect == nullptr) first_gizmo_effect = e;
|
||||
if (e->container->selected) selected_effect = e;
|
||||
}
|
||||
}
|
||||
|
||||
if (!rendering) {
|
||||
if (selected_effect != nullptr) {
|
||||
gizmos = selected_effect;
|
||||
} else if (panel_timeline->is_clip_selected(c, true)) {
|
||||
gizmos = first_gizmo_effect;
|
||||
}
|
||||
}
|
||||
|
||||
if (c->get_opening_transition() != nullptr) {
|
||||
int transition_progress = playhead - c->get_timeline_in_with_transition();
|
||||
if (transition_progress < c->get_opening_transition()->get_length()) {
|
||||
process_effect(c, c->get_opening_transition(), (double)transition_progress/(double)c->get_opening_transition()->get_length(), coords, composite_texture, fbo_switcher, TA_OPENING_TRANSITION);
|
||||
}
|
||||
}
|
||||
|
||||
if (c->get_closing_transition() != nullptr) {
|
||||
int transition_progress = playhead - (c->get_timeline_out_with_transition() - c->get_closing_transition()->get_length());
|
||||
if (transition_progress >= 0 && transition_progress < c->get_closing_transition()->get_length()) {
|
||||
process_effect(c, c->get_closing_transition(), (double)transition_progress/(double)c->get_closing_transition()->get_length(), coords, composite_texture, fbo_switcher, TA_CLOSING_TRANSITION);
|
||||
}
|
||||
}
|
||||
// EFFECT CODE END
|
||||
|
||||
if (!nests.isEmpty()) {
|
||||
nests.last()->fbo[0]->bind();
|
||||
glViewport(0, 0, s->width, s->height);
|
||||
} else if (rendering) {
|
||||
glViewport(0, 0, s->width, s->height);
|
||||
} else {
|
||||
int widget_width = width();
|
||||
int widget_height = height();
|
||||
|
||||
widget_width *= QApplication::desktop()->devicePixelRatio();
|
||||
widget_height *= QApplication::desktop()->devicePixelRatio();
|
||||
|
||||
glViewport(0, 0, widget_width, widget_height);
|
||||
}
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, composite_texture);
|
||||
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
glBegin(GL_QUADS);
|
||||
|
||||
if (coords.grid_size <= 1) {
|
||||
float z = 0.0f;
|
||||
|
||||
glTexCoord2f(coords.textureTopLeftX, coords.textureTopLeftY); // top left
|
||||
glVertex3f(coords.vertexTopLeftX, coords.vertexTopLeftY, z); // top left
|
||||
glTexCoord2f(coords.textureTopRightX, coords.textureTopRightY); // top right
|
||||
glVertex3f(coords.vertexTopRightX, coords.vertexTopRightY, z); // top right
|
||||
glTexCoord2f(coords.textureBottomRightX, coords.textureBottomRightY); // bottom right
|
||||
glVertex3f(coords.vertexBottomRightX, coords.vertexBottomRightY, z); // bottom right
|
||||
glTexCoord2f(coords.textureBottomLeftX, coords.textureBottomLeftY); // bottom left
|
||||
glVertex3f(coords.vertexBottomLeftX, coords.vertexBottomLeftY, z); // bottom left
|
||||
} else {
|
||||
float rows = coords.grid_size;
|
||||
float cols = coords.grid_size;
|
||||
|
||||
for (float k=0;k<rows;k++) {
|
||||
float row_prog = k/rows;
|
||||
float next_row_prog = (k+1)/rows;
|
||||
for (float j=0;j<cols;j++) {
|
||||
float col_prog = j/cols;
|
||||
float next_col_prog = (j+1)/cols;
|
||||
|
||||
float vertexTLX = float_lerp(coords.vertexTopLeftX, coords.vertexBottomLeftX, row_prog);
|
||||
float vertexTRX = float_lerp(coords.vertexTopRightX, coords.vertexBottomRightX, row_prog);
|
||||
float vertexBLX = float_lerp(coords.vertexTopLeftX, coords.vertexBottomLeftX, next_row_prog);
|
||||
float vertexBRX = float_lerp(coords.vertexTopRightX, coords.vertexBottomRightX, next_row_prog);
|
||||
|
||||
float vertexTLY = float_lerp(coords.vertexTopLeftY, coords.vertexTopRightY, col_prog);
|
||||
float vertexTRY = float_lerp(coords.vertexTopLeftY, coords.vertexTopRightY, next_col_prog);
|
||||
float vertexBLY = float_lerp(coords.vertexBottomLeftY, coords.vertexBottomRightY, col_prog);
|
||||
float vertexBRY = float_lerp(coords.vertexBottomLeftY, coords.vertexBottomRightY, next_col_prog);
|
||||
|
||||
glTexCoord2f(float_lerp(coords.textureTopLeftX, coords.textureTopRightX, col_prog), float_lerp(coords.textureTopLeftY, coords.textureBottomLeftY, row_prog)); // top left
|
||||
glVertex2f(float_lerp(vertexTLX, vertexTRX, col_prog), float_lerp(vertexTLY, vertexBLY, row_prog)); // top left
|
||||
glTexCoord2f(float_lerp(coords.textureTopLeftX, coords.textureTopRightX, next_col_prog), float_lerp(coords.textureTopRightY, coords.textureBottomRightY, row_prog)); // top right
|
||||
glVertex2f(float_lerp(vertexTLX, vertexTRX, next_col_prog), float_lerp(vertexTRY, vertexBRY, row_prog)); // top right
|
||||
glTexCoord2f(float_lerp(coords.textureBottomLeftX, coords.textureBottomRightX, next_col_prog), float_lerp(coords.textureTopRightY, coords.textureBottomRightY, next_row_prog)); // bottom right
|
||||
glVertex2f(float_lerp(vertexBLX, vertexBRX, next_col_prog), float_lerp(vertexTRY, vertexBRY, next_row_prog)); // bottom right
|
||||
glTexCoord2f(float_lerp(coords.textureBottomLeftX, coords.textureBottomRightX, col_prog), float_lerp(coords.textureTopLeftY, coords.textureBottomLeftY, next_row_prog)); // bottom left
|
||||
glVertex2f(float_lerp(vertexBLX, vertexBRX, col_prog), float_lerp(vertexTLY, vertexBLY, next_row_prog)); // bottom left
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
glEnd();
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0); // unbind texture
|
||||
|
||||
if (gizmos != nullptr && !drawn_gizmos) {
|
||||
gizmos->gizmo_draw(timecode, coords); // set correct gizmo coords
|
||||
gizmos->gizmo_world_to_screen();
|
||||
|
||||
drawn_gizmos = true;
|
||||
}
|
||||
|
||||
if (!nests.isEmpty()) {
|
||||
nests.last()->fbo[0]->release();
|
||||
if (default_fbo != nullptr) default_fbo->bind();
|
||||
}
|
||||
|
||||
glPopMatrix();
|
||||
|
||||
/*GLfloat motion_blur_frac = (GLfloat) motion_blur_prog / (GLfloat) motion_blur_lim;
|
||||
if (motion_blur_prog == 0) {
|
||||
glAccum(GL_LOAD, motion_blur_frac);
|
||||
} else {
|
||||
glAccum(GL_ACCUM, motion_blur_frac);
|
||||
}
|
||||
motion_blur_prog++;*/
|
||||
}
|
||||
} else {
|
||||
if (render_audio || (config.enable_audio_scrubbing && audio_scrub)) {
|
||||
if (c->media != nullptr && c->media->get_type() == MEDIA_TYPE_SEQUENCE) {
|
||||
nests.append(c);
|
||||
compose_sequence(nests, render_audio);
|
||||
nests.removeLast();
|
||||
} else {
|
||||
if (c->lock.tryLock()) {
|
||||
// clip is not caching, start caching audio
|
||||
cache_clip(c, playhead, c->audio_reset, !render_audio, nests);
|
||||
c->lock.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// visually update all the keyframe values
|
||||
if (c->sequence == viewer->seq) { // only if you can currently see them
|
||||
double ts = (playhead - c->get_timeline_in_with_transition() + c->get_clip_in_with_transition())/s->frame_rate;
|
||||
for (int i=0;i<c->effects.size();i++) {
|
||||
Effect* e = c->effects.at(i);
|
||||
for (int j=0;j<e->row_count();j++) {
|
||||
EffectRow* r = e->row(j);
|
||||
for (int k=0;k<r->fieldCount();k++) {
|
||||
r->field(k)->validate_keyframe_data(ts);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (audio_track_count == 0) {
|
||||
viewer->play_wake();
|
||||
}
|
||||
|
||||
glPopMatrix();
|
||||
|
||||
if (!nests.isEmpty() && nests.last()->fbo != nullptr) {
|
||||
// returns nested clip's texture
|
||||
return nests.last()->fbo[0]->texture();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ViewerWidget::paintGL() {
|
||||
drawn_gizmos = false;
|
||||
if (viewer->seq != nullptr) {
|
||||
gizmos = nullptr;
|
||||
drawn_gizmos = false;
|
||||
force_quit = false;
|
||||
|
||||
bool render_audio = (viewer->playing || rendering);
|
||||
|
||||
retry_timer.stop();
|
||||
|
||||
// send context to other thread for drawing
|
||||
doneCurrent();
|
||||
renderer->start_render(context(), viewer->seq);
|
||||
|
||||
// render the audio
|
||||
QVector<Clip*> nests;
|
||||
compose_sequence(viewer, context(), viewer->seq, nests, false, render_audio, &gizmos);
|
||||
|
||||
// try to draw the texture from the other thread if we got it
|
||||
if (renderer->mutex.tryLock()) {
|
||||
makeCurrent();
|
||||
|
||||
glClearColor(0.0, 0.0, 0.0, 1.0);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, renderer->texColorBuffer);
|
||||
|
||||
glLoadIdentity();
|
||||
glOrtho(0, 1, 1, 0, -1, 1);
|
||||
|
||||
glBegin(GL_QUADS);
|
||||
|
||||
glVertex2f(0, 0);
|
||||
glTexCoord2f(0, 0);
|
||||
glVertex2f(0, 1);
|
||||
glTexCoord2f(1, 0);
|
||||
glVertex2f(1, 1);
|
||||
glTexCoord2f(1, 1);
|
||||
glVertex2f(1, 0);
|
||||
glTexCoord2f(0, 1);
|
||||
|
||||
glEnd();
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
|
||||
renderer->mutex.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*drawn_gizmos = false;
|
||||
force_quit = false;
|
||||
if (viewer->seq != nullptr) {
|
||||
gizmos = nullptr;
|
||||
@@ -953,5 +576,5 @@ void ViewerWidget::paintGL() {
|
||||
glDisable(GL_BLEND);
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
} while (loop);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
+31
-29
@@ -18,62 +18,64 @@ class Effect;
|
||||
class EffectGizmo;
|
||||
class ViewerContainer;
|
||||
struct GLTextureCoords;
|
||||
class RenderThread;
|
||||
|
||||
class ViewerWidget : public QOpenGLWidget, QOpenGLFunctions
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
ViewerWidget(QWidget *parent = 0);
|
||||
ViewerWidget(QWidget *parent = 0);
|
||||
|
||||
void paintGL();
|
||||
void initializeGL();
|
||||
void paintGL();
|
||||
void initializeGL();
|
||||
Viewer* viewer;
|
||||
ViewerContainer* container;
|
||||
ViewerContainer* container;
|
||||
|
||||
QOpenGLFramebufferObject* default_fbo;
|
||||
QOpenGLFramebufferObject* default_fbo;
|
||||
|
||||
bool waveform;
|
||||
Clip* waveform_clip;
|
||||
const FootageStream* waveform_ms;
|
||||
double waveform_zoom;
|
||||
int waveform_scroll;
|
||||
const FootageStream* waveform_ms;
|
||||
double waveform_zoom;
|
||||
int waveform_scroll;
|
||||
|
||||
bool force_quit;
|
||||
bool force_quit;
|
||||
public slots:
|
||||
void delete_function();
|
||||
void set_waveform_scroll(int s);
|
||||
void delete_function();
|
||||
void set_waveform_scroll(int s);
|
||||
protected:
|
||||
void paintEvent(QPaintEvent *e);
|
||||
void paintEvent(QPaintEvent *e);
|
||||
// void resizeGL(int w, int h);
|
||||
void mousePressEvent(QMouseEvent *event);
|
||||
void mouseMoveEvent(QMouseEvent *event);
|
||||
void mouseReleaseEvent(QMouseEvent *event);
|
||||
private:
|
||||
QTimer retry_timer;
|
||||
void drawTitleSafeArea();
|
||||
void drawTitleSafeArea();
|
||||
bool dragging;
|
||||
void seek_from_click(int x);
|
||||
GLuint compose_sequence(QVector<Clip *> &nests, bool render_audio);
|
||||
GLuint draw_clip(QOpenGLFramebufferObject *clip, GLuint texture, bool clear);
|
||||
void process_effect(Clip* c, Effect* e, double timecode, GLTextureCoords& coords, GLuint& composite_texture, bool& fbo_switcher, int data);
|
||||
Effect* gizmos;
|
||||
int drag_start_x;
|
||||
int drag_start_y;
|
||||
int gizmo_x_mvmt;
|
||||
int gizmo_y_mvmt;
|
||||
EffectGizmo* selected_gizmo;
|
||||
EffectGizmo* get_gizmo_from_mouse(int x, int y);
|
||||
bool drawn_gizmos;
|
||||
void move_gizmos(QMouseEvent *event, bool done);
|
||||
// GLuint compose_sequence(QVector<Clip *> &nests, bool video, bool render_audio);
|
||||
// GLuint draw_clip(QOpenGLFramebufferObject *clip, GLuint texture, bool clear);
|
||||
// void process_effect(Clip* c, Effect* e, double timecode, GLTextureCoords& coords, GLuint& composite_texture, bool& fbo_switcher, int data);
|
||||
Effect* gizmos;
|
||||
int drag_start_x;
|
||||
int drag_start_y;
|
||||
int gizmo_x_mvmt;
|
||||
int gizmo_y_mvmt;
|
||||
EffectGizmo* selected_gizmo;
|
||||
EffectGizmo* get_gizmo_from_mouse(int x, int y);
|
||||
bool drawn_gizmos;
|
||||
void move_gizmos(QMouseEvent *event, bool done);
|
||||
RenderThread* renderer;
|
||||
private slots:
|
||||
void retry();
|
||||
void show_context_menu();
|
||||
void save_frame();
|
||||
void show_fullscreen();
|
||||
void show_fullscreen();
|
||||
|
||||
void set_fit_zoom();
|
||||
void set_custom_zoom();
|
||||
void set_menu_zoom(QAction *action);
|
||||
void set_fit_zoom();
|
||||
void set_custom_zoom();
|
||||
void set_menu_zoom(QAction *action);
|
||||
};
|
||||
|
||||
#endif // VIEWERWIDGET_H
|
||||
|
||||
Reference in New Issue
Block a user