From c8496ac2a8fb2a12431a4018778bf3d0552bd063 Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Sun, 17 Mar 2019 15:53:54 +1100 Subject: [PATCH] began transition to gles --- effects/internal/internalshaders.qrc | 2 + effects/internal/pipeline.frag | 11 +++++ effects/internal/pipeline.vert | 16 ++++++ main.cpp | 2 + rendering/framebufferobject.cpp | 12 +++-- rendering/renderfunctions.cpp | 18 ++++++- rendering/renderthread.cpp | 6 +-- rendering/renderthread.h | 3 -- ui/viewerwidget.cpp | 73 +++++++++++++++++++++++++++- ui/viewerwidget.h | 6 ++- 10 files changed, 134 insertions(+), 15 deletions(-) create mode 100644 effects/internal/pipeline.frag create mode 100644 effects/internal/pipeline.vert diff --git a/effects/internal/internalshaders.qrc b/effects/internal/internalshaders.qrc index ae51e0b27..52ec17302 100644 --- a/effects/internal/internalshaders.qrc +++ b/effects/internal/internalshaders.qrc @@ -5,5 +5,7 @@ cornerpin.vert premultiply.frag dropshadow.frag + pipeline.frag + pipeline.vert diff --git a/effects/internal/pipeline.frag b/effects/internal/pipeline.frag new file mode 100644 index 000000000..71719c428 --- /dev/null +++ b/effects/internal/pipeline.frag @@ -0,0 +1,11 @@ +#ifdef GL_ES +precision mediump int; +precision mediump float; +#endif + +uniform sampler2D texture; +varying vec2 v_texcoord; + +void main() { + gl_FragColor = texture2D(texture, v_texcoord); +} \ No newline at end of file diff --git a/effects/internal/pipeline.vert b/effects/internal/pipeline.vert new file mode 100644 index 000000000..8dc969907 --- /dev/null +++ b/effects/internal/pipeline.vert @@ -0,0 +1,16 @@ +#ifdef GL_ES +precision mediump int; +precision mediump float; +#endif + +uniform mat4 mvp_matrix; + +attribute vec4 a_position; +attribute vec2 a_texcoord; + +varying vec2 v_texcoord; + +void main() { + gl_Position = mvp_matrix * a_position; + v_texcoord = a_texcoord; +}; \ No newline at end of file diff --git a/main.cpp b/main.cpp index 3a0957357..24ff13bb9 100644 --- a/main.cpp +++ b/main.cpp @@ -109,7 +109,9 @@ int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts); QSurfaceFormat format; + format.setVersion(3, 2); format.setDepthBufferSize(24); + format.setProfile(QSurfaceFormat::CompatibilityProfile); QSurfaceFormat::setDefaultFormat(format); QApplication a(argc, argv); diff --git a/rendering/framebufferobject.cpp b/rendering/framebufferobject.cpp index 6ca510443..bb31ce231 100644 --- a/rendering/framebufferobject.cpp +++ b/rendering/framebufferobject.cpp @@ -1,6 +1,8 @@ #include "framebufferobject.h" #include +#include +#include FramebufferObject::FramebufferObject() : buffer_(0), @@ -39,16 +41,16 @@ void FramebufferObject::Create(QOpenGLContext *ctx, int width, int height) ctx->functions()->glBindTexture(GL_TEXTURE_2D, texture_); // allocate storage for texture - ctx->functions()->glTexImage2D( - GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr + glTexImage2D( + GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA8, GL_UNSIGNED_BYTE, nullptr ); // set texture filtering to bilinear - ctx->functions()->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - ctx->functions()->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // attach texture to framebuffer - ctx->functions()->glFramebufferTexture2D( + ctx->extraFunctions()->glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_, 0 ); diff --git a/rendering/renderfunctions.cpp b/rendering/renderfunctions.cpp index dcac906b5..32477ee9b 100644 --- a/rendering/renderfunctions.cpp +++ b/rendering/renderfunctions.cpp @@ -316,6 +316,7 @@ GLuint compose_sequence(ComposeSequenceParams ¶ms) { } else { // retrieve ID from c->texture textureID = c->texture->textureId(); + qDebug() << "tex 1" << textureID; } if (textureID == 0) { @@ -359,18 +360,22 @@ GLuint compose_sequence(ComposeSequenceParams ¶ms) { params.nests.removeLast(); // compose_sequence() would have written to this clip's fbo[0], so we switch to fbo[1] - fbo_switcher = true; + fbo_switcher = !fbo_switcher; } else if (c->media()->get_type() == MEDIA_TYPE_FOOTAGE) { if (!c->media()->to_footage()->alpha_is_premultiplied) { + // alpha is not premultiplied, we'll need to multiply it for the rest of the pipeline params.ctx->functions()->glBlendFuncSeparate(GL_SRC_ALPHA, GL_ZERO, GL_ONE, GL_ZERO); + qDebug() << "about to draw on" << fbo_switcher; textureID = draw_clip(c->fbo[fbo_switcher], textureID, true); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); fbo_switcher = !fbo_switcher; + qDebug() << "tex 3" << textureID << fbo_switcher; + } #ifndef NO_OCIO @@ -382,11 +387,14 @@ GLuint compose_sequence(ComposeSequenceParams ¶ms) { params.ocio_shader->setUniformValue("tex1", 0); params.ocio_shader->setUniformValue("tex2", 2); + qDebug() << "about to draw on" << fbo_switcher; textureID = draw_clip(c->fbo[fbo_switcher], textureID, true); params.ocio_shader->release(); fbo_switcher = !fbo_switcher; + + qDebug() << "tex 4" << textureID << fbo_switcher; } #endif @@ -414,8 +422,11 @@ GLuint compose_sequence(ComposeSequenceParams ¶ms) { // run through all of the clip's effects for (int j=0;jeffects.size();j++) { + Effect* e = c->effects.at(j).get(); + qDebug() << "about to draw on" << fbo_switcher; process_effect(c, e, timecode, coords, textureID, fbo_switcher, params.texture_failed, kTransitionNone); + qDebug() << "tex 5" << textureID << fbo_switcher; } @@ -457,6 +468,7 @@ GLuint compose_sequence(ComposeSequenceParams ¶ms) { + qDebug() << "final texture ID" << textureID; if (textureID > 0) { // set viewport to sequence size params.ctx->functions()->glViewport(0, 0, s->width, s->height); @@ -549,6 +561,8 @@ GLuint compose_sequence(ComposeSequenceParams ¶ms) { // Check if we're using a blend mode (< 0 means no blend mode) if (coords.blendmode < 0) { + qDebug() << "normal blending"; + params.ctx->functions()->glBindTexture(GL_TEXTURE_2D, backend_tex_1); glColor4f(coords.opacity, coords.opacity, coords.opacity, coords.opacity); @@ -559,6 +573,8 @@ GLuint compose_sequence(ComposeSequenceParams ¶ms) { } else { + qDebug() << "blending mode?"; + // 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); diff --git a/rendering/renderthread.cpp b/rendering/renderthread.cpp index 586b85d6f..19ebeed04 100644 --- a/rendering/renderthread.cpp +++ b/rendering/renderthread.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #ifndef NO_OCIO @@ -160,7 +161,6 @@ const char * g_fragShaderText = "" #ifndef NO_OCIO void RenderThread::set_up_ocio() { - functions.initializeOpenGLFunctions(); // // SETUP LUT TEXTURE @@ -181,7 +181,7 @@ void RenderThread::set_up_ocio() ctx->functions()->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); // Allocate storage for texture - functions.glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB16F_ARB, + ctx->extraFunctions()->glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB16F_ARB, OCIO_LUT3D_EDGE_SIZE, OCIO_LUT3D_EDGE_SIZE, OCIO_LUT3D_EDGE_SIZE, 0, GL_RGB,GL_FLOAT, ocio_lut_data); @@ -241,7 +241,7 @@ void RenderThread::set_up_ocio() processor->getGpuLut3D(ocio_lut_data, shaderDesc); glBindTexture(GL_TEXTURE_3D, ocio_lut_texture); - functions.glTexSubImage3D(GL_TEXTURE_3D, 0, + ctx->extraFunctions()->glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 0, OCIO_LUT3D_EDGE_SIZE, OCIO_LUT3D_EDGE_SIZE, OCIO_LUT3D_EDGE_SIZE, GL_RGB,GL_FLOAT, ocio_lut_data); diff --git a/rendering/renderthread.h b/rendering/renderthread.h index 7c8cbf8f9..62e4ec2bc 100644 --- a/rendering/renderthread.h +++ b/rendering/renderthread.h @@ -28,7 +28,6 @@ #include #include #include -#include #include "timeline/sequence.h" #include "effects/effect.h" @@ -119,8 +118,6 @@ private: QString save_fn; GLvoid *pixel_buffer; int pixel_buffer_linesize; - - QOpenGLFunctions_2_0 functions; }; #endif // RENDERTHREAD_H diff --git a/ui/viewerwidget.cpp b/ui/viewerwidget.cpp index 6c0c7238d..b96d0fc6b 100644 --- a/ui/viewerwidget.cpp +++ b/ui/viewerwidget.cpp @@ -27,6 +27,7 @@ extern "C" { #include #include #include +#include #include #include #include @@ -39,6 +40,8 @@ extern "C" { #include #include #include +#include +#include #include "panels/panels.h" #include "project/projectelements.h" @@ -68,7 +71,8 @@ ViewerWidget::ViewerWidget(QWidget *parent) : gizmos(nullptr), selected_gizmo(nullptr), x_scroll(0), - y_scroll(0) + y_scroll(0), + pipeline_(nullptr) { setMouseTracking(true); setFocusPolicy(Qt::ClickFocus); @@ -82,6 +86,8 @@ ViewerWidget::ViewerWidget(QWidget *parent) : connect(renderer, SIGNAL(finished()), renderer, SLOT(deleteLater())); window = new ViewerWindow(this); + + projection_.setToIdentity(); } ViewerWidget::~ViewerWidget() { @@ -216,9 +222,14 @@ void ViewerWidget::retry() { } void ViewerWidget::initializeGL() { - initializeOpenGLFunctions(); + context()->functions()->initializeOpenGLFunctions(); connect(context(), SIGNAL(aboutToBeDestroyed()), this, SLOT(context_destroy()), Qt::DirectConnection); + + pipeline_ = new QOpenGLShaderProgram(); + pipeline_->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/internalshaders/pipeline.vert"); + pipeline_->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/internalshaders/pipeline.frag"); + pipeline_->link(); } void ViewerWidget::frame_update() { @@ -252,10 +263,16 @@ void ViewerWidget::seek_from_click(int x) { void ViewerWidget::context_destroy() { makeCurrent(); + if (viewer->seq != nullptr) { close_active_clips(viewer->seq.get()); } + renderer->delete_ctx(); + + delete pipeline_; + pipeline_ = nullptr; + doneCurrent(); } @@ -539,6 +556,57 @@ void ViewerWidget::draw_gizmos() { } void ViewerWidget::paintGL() { + QOpenGLFunctions* f = context()->functions(); + QOpenGLExtraFunctions* xf = context()->extraFunctions(); + + f->glClearColor(0.0, 0.0, 0.0, 0.0); + + GLfloat vertices[] = { + -1.0f, -1.0f, -1.0f, + 1.0f, -1.0f, -1.0f, + 1.0f, 1.0f, -1.0f, + + -1.0f, -1.0f, -1.0f, + -1.0f, 1.0f, -1.0f, + 1.0f, 1.0f, -1.0f, + }; + + GLfloat tex_coords[] = { + 0.0, 0.0, + 1.0, 0.0, + 1.0, 1.0, + + 0.0, 0.0, + 0.0, 1.0, + 1.0, 1.0 + }; + + f->glViewport(0, 0, width(), height()); + + f->glClear(GL_COLOR_BUFFER_BIT); + + QOpenGLTexture texture(QImage("C:/Users/Matt/Desktop/bliss.png")); + + pipeline_->bind(); + texture.bind(); + + pipeline_->setUniformValue("mvp_matrix", projection_); + pipeline_->setUniformValue("texture", 0); + + GLuint vertex_location = pipeline_->attributeLocation("a_position"); + f->glEnableVertexAttribArray(vertex_location); + f->glVertexAttribPointer(vertex_location, 3, GL_FLOAT, GL_FALSE, 0, vertices); + + GLuint tex_location = pipeline_->attributeLocation("a_texcoord"); + f->glEnableVertexAttribArray(tex_location); + f->glVertexAttribPointer(tex_location, 2, GL_FLOAT, GL_FALSE, 0, tex_coords); + + f->glDrawArrays(GL_TRIANGLES, 0, 6); + + texture.release(); + pipeline_->release(); + + /* if (waveform) { draw_waveform_func(); } else { @@ -615,4 +683,5 @@ void ViewerWidget::paintGL() { renderer->start_render(context(), viewer->seq.get()); } } + */ } diff --git a/ui/viewerwidget.h b/ui/viewerwidget.h index f01ae7063..edd5a3938 100644 --- a/ui/viewerwidget.h +++ b/ui/viewerwidget.h @@ -41,7 +41,7 @@ class Viewer; class QOpenGLFramebufferObject; struct GLTextureCoords; -class ViewerWidget : public QOpenGLWidget, QOpenGLFunctions +class ViewerWidget : public QOpenGLWidget { Q_OBJECT public: @@ -91,6 +91,10 @@ private: ViewerWindow* window; double x_scroll; double y_scroll; + + QMatrix4x4 projection_; + QOpenGLShaderProgram* pipeline_; + private slots: void context_destroy(); void retry();