okay maybe not

This commit is contained in:
itsmattkc
2020-11-08 00:47:59 +11:00
parent 83cefbd7c7
commit 6ecdb02fd0
24 changed files with 393 additions and 388 deletions
+113 -24
View File
@@ -24,6 +24,36 @@
OLIVE_NAMESPACE_ENTER
const QVector<GLfloat> 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
};
const QVector<GLfloat> blit_texcoords = {
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f
};
const QVector<GLfloat> flipped_blit_texcoords = {
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f
};
OpenGLRenderer::OpenGLRenderer(QObject* parent) :
Renderer(parent),
context_(nullptr)
@@ -75,18 +105,45 @@ void OpenGLRenderer::PostInit()
// Store OpenGL functions instance
functions_ = context_->functions();
functions_->glBlendFunc(GL_ONE, GL_ZERO);
// Set up framebuffer used for various things
functions_->glGenFramebuffers(1, &framebuffer_);
// Set up vertex array object
vao_.create();
// Set up vertex buffer
vert_vbo_.create();
vert_vbo_.bind();
vert_vbo_.allocate(blit_vertices.constData(), blit_vertices.size() * sizeof(GLfloat));
vert_vbo_.release();
// Set up fragment buffer
frag_vbo_.create();
frag_vbo_.bind();
frag_vbo_.allocate(blit_texcoords.constData(), blit_texcoords.size() * sizeof(GLfloat));
frag_vbo_.release();
}
void OpenGLRenderer::Destroy()
{
// Delete vertex array object
vao_.destroy();
// Delete framebuffer
functions_->glDeleteFramebuffers(1, &framebuffer_);
// Delete all shaders
qDeleteAll(shader_cache_);
shader_cache_.clear();
// Delete context if it belongs to us
if (context_->parent() == this) {
delete context_;
}
context_ = nullptr;
qDeleteAll(shader_cache_);
shader_cache_.clear();
// Destroy surface if we created it
if (surface_.isValid()) {
surface_.destroy();
}
@@ -183,11 +240,11 @@ Renderer::TexturePtr OpenGLRenderer::ProcessShader(const Node *node, const TimeR
}
if (frag_code.isEmpty()) {
frag_code = OpenGLShader::CodeDefaultFragment();
frag_code = Node::ReadFileAsString(QStringLiteral(":/shaders/default.frag"));
}
if (vert_code.isEmpty()) {
vert_code = OpenGLShader::CodeDefaultVertex();
vert_code = Node::ReadFileAsString(QStringLiteral(":/shaders/default.vert"));
}
shader = new QOpenGLShaderProgram(this);
@@ -354,8 +411,6 @@ Renderer::TexturePtr OpenGLRenderer::ProcessShader(const Node *node, const TimeR
static_cast<GLfloat>(params.width()),
static_cast<GLfloat>(params.height()));
shader->release();
// Create the output textures
PixelFormat::Format output_format = (input_textures_have_alpha || job.GetAlphaChannelRequired())
? PixelFormat::GetFormatWithAlphaChannel(params.format())
@@ -393,14 +448,28 @@ Renderer::TexturePtr OpenGLRenderer::ProcessShader(const Node *node, const TimeR
for (int i=0; i<textures_to_bind.size(); i++) {
functions_->glActiveTexture(GL_TEXTURE0 + i);
functions_->glBindTexture(GL_TEXTURE_2D, textures_to_bind.at(i));
OpenGLRenderFunctions::PrepareToDraw(functions_);
PrepareInputTexture(job.GetBilinearFiltering());
}
// Bind vertex array object
vao_.bind();
// Set buffers
int vertex_location = shader->attributeLocation("a_position");
vert_vbo_.bind();
functions_->glEnableVertexAttribArray(vertex_location);
functions_->glVertexAttribPointer(vertex_location, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
vert_vbo_.release();
int tex_location = shader->attributeLocation("a_texcoord");
frag_vbo_.bind();
functions_->glEnableVertexAttribArray(tex_location);
functions_->glVertexAttribPointer(tex_location, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
frag_vbo_.release();
for (int iteration=0; iteration<real_iteration_count; iteration++) {
// Set iteration number
shader->bind();
shader->setUniformValue("ove_iteration", iteration);
shader->release();
// Replace iterative input
if (iteration == 0) {
@@ -410,18 +479,22 @@ Renderer::TexturePtr OpenGLRenderer::ProcessShader(const Node *node, const TimeR
output_tex = dst_refs[iteration%2];
functions_->glActiveTexture(GL_TEXTURE0 + iterative_input);
functions_->glBindTexture(GL_TEXTURE_2D, input_tex->texture()->texture());
OpenGLRenderFunctions::PrepareToDraw(functions_);
functions_->glBindTexture(GL_TEXTURE_2D, input_tex->id().value<GLuint>());
PrepareInputTexture(job.GetBilinearFiltering());
}
buffer_.Attach(output_tex, true);
buffer_.Bind();
functions_->glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_);
functions_->glFramebufferTexture2D(GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D,
output_tex->id().value<GLuint>(),
0);
// Blit this texture through this shader
OpenGLRenderFunctions::Blit(shader);
functions_->glDrawArrays(GL_TRIANGLES, 0, blit_vertices.size() / 3);
buffer_.Release();
buffer_.Detach();
// Reset framebuffer to default
functions_->glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
// Release any textures we bound before
@@ -430,16 +503,15 @@ Renderer::TexturePtr OpenGLRenderer::ProcessShader(const Node *node, const TimeR
functions_->glBindTexture(GL_TEXTURE_2D, 0);
}
// Release vertex array object
vao_.release();
// Release shader
shader->release();
return output_tex;
}
/*VideoParams OpenGLRenderer::GetParamsFromTexture(QVariant texture)
{
GLuint t = texture.value<GLuint>();
return texture_params_.value(t);
}*/
GLint OpenGLRenderer::GetInternalFormat(PixelFormat::Format format)
{
switch (format) {
@@ -501,4 +573,21 @@ GLenum OpenGLRenderer::GetPixelType(PixelFormat::Format format)
return GL_INVALID_VALUE;
}
void OpenGLRenderer::PrepareInputTexture(bool bilinear)
{
if (bilinear) {
// Use mipmapped bilinear
functions_->glGenerateMipmap(GL_TEXTURE_2D);
functions_->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
functions_->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
} else {
// Use nearest
functions_->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
functions_->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
functions_->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
functions_->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
OLIVE_NAMESPACE_EXIT
+13 -1
View File
@@ -22,8 +22,10 @@
#define OPENGLCONTEXT_H
#include <QOffscreenSurface>
#include <QOpenGLBuffer>
#include <QOpenGLFunctions>
#include <QOpenGLShader>
#include <QOpenGLVertexArrayObject>
#include <QThread>
#include "render/backend/renderer.h"
@@ -60,7 +62,7 @@ public slots:
const OLIVE_NAMESPACE::ShaderJob &job,
const OLIVE_NAMESPACE::VideoParams &params) override;
virtual QVariant TransformColor(QVariant texture, ColorProcessorPtr processor) override;
virtual TexturePtr TransformColor(Texture* texture, OLIVE_NAMESPACE::ColorProcessorPtr processor) override;
private:
static GLint GetInternalFormat(PixelFormat::Format format);
@@ -69,12 +71,22 @@ private:
static GLenum GetPixelType(PixelFormat::Format format);
void PrepareInputTexture(bool bilinear);
QOpenGLContext* context_;
QOpenGLFunctions* functions_;
QOffscreenSurface surface_;
QOpenGLVertexArrayObject vao_;
QOpenGLBuffer vert_vbo_;
QOpenGLBuffer frag_vbo_;
GLuint framebuffer_;
QHash<QString, QOpenGLShaderProgram*> shader_cache_;
};
-254
View File
@@ -1,254 +0,0 @@
/***
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 "openglshader.h"
#include <QOpenGLExtraFunctions>
OLIVE_NAMESPACE_ENTER
OpenGLShaderPtr OpenGLShader::Create()
{
return std::make_shared<OpenGLShader>();
}
OpenGLShaderPtr OpenGLShader::CreateDefault(const QString &function_name, const QString &shader_code)
{
OpenGLShaderPtr program = Create();
// Add shaders to program
program->addShaderFromSourceCode(QOpenGLShader::Vertex, CodeDefaultVertex());
program->addShaderFromSourceCode(QOpenGLShader::Fragment, CodeDefaultFragment(function_name, shader_code));
program->link();
return program;
}
// copied from source code to OCIODisplay
const int OCIO_LUT3D_EDGE_SIZE = 64;
// copied from source code to OCIODisplay, expanded from 3*LUT3D_EDGE_SIZE*LUT3D_EDGE_SIZE*LUT3D_EDGE_SIZE
const int OCIO_NUM_3D_ENTRIES = 3*OCIO_LUT3D_EDGE_SIZE*OCIO_LUT3D_EDGE_SIZE*OCIO_LUT3D_EDGE_SIZE;
OpenGLShaderPtr OpenGLShader::CreateOCIO(QOpenGLContext* ctx,
GLuint& lut_texture,
OCIO::ConstProcessorRcPtr processor,
bool alpha_is_associated)
{
QOpenGLExtraFunctions* xf = ctx->extraFunctions();
// Set up shader description
OCIO::GpuShaderDesc shaderDesc;
const char* ocio_func_name = "OCIODisplay";
shaderDesc.setLanguage(OCIO::GPU_LANGUAGE_GLSL_1_3);
shaderDesc.setFunctionName(ocio_func_name);
shaderDesc.setLut3DEdgeLen(OCIO_LUT3D_EDGE_SIZE);
// Compute LUT
std::vector<float> ocio_lut_data(OCIO_NUM_3D_ENTRIES);
processor->getGpuLut3D(&ocio_lut_data[0], shaderDesc);
// Create LUT texture
xf->glGenTextures(1, &lut_texture);
// Bind LUT
xf->glActiveTexture(GL_TEXTURE1);
xf->glBindTexture(GL_TEXTURE_3D, lut_texture);
// Set texture parameters
xf->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
xf->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
xf->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
xf->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
xf->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
// Allocate storage for texture
xf->glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB16F,
OCIO_LUT3D_EDGE_SIZE, OCIO_LUT3D_EDGE_SIZE, OCIO_LUT3D_EDGE_SIZE,
0, GL_RGB, GL_FLOAT, &ocio_lut_data[0]);
// Create OCIO shader code
QString shader_text;
// Workaround since OCIO doesn't support the GLSL version we use
shader_text.append(QStringLiteral("#define texture2D texture\n"
"#define texture3D texture\n"));
// Append OCIO shader code
shader_text.append(processor->getGpuShaderText(shaderDesc));
QString shader_call;
// Enforce alpha association
if (alpha_is_associated) {
// If alpha is already associated, we'll need to disassociate and reassociate
shader_text.append("\n");
QString disassociate_func_name = "disassoc";
shader_text.append(CodeAlphaDisassociate(disassociate_func_name));
QString reassociate_func_name = "reassoc";
shader_text.append(CodeAlphaReassociate(reassociate_func_name));
// Make OCIO call pass through disassociate and reassociate function
shader_call = QStringLiteral("%3(%1(%2(col), ove_ociolut));").arg(ocio_func_name,
disassociate_func_name,
reassociate_func_name);
} else {
// If alpha is not already associated, we can just associate after OCIO
// Add associate function
QString associate_func_name = "assoc";
shader_text.append(CodeAlphaAssociate(associate_func_name));
// Make OCIO call pass through associate function
shader_call = QStringLiteral("%2(%1(col, ove_ociolut));").arg(ocio_func_name, associate_func_name);
}
// Add process() function, which GetPipeline() will call if specified
QString process_function_name = "process";
shader_text.append(QStringLiteral("\n"
"uniform sampler3D ove_ociolut;\n"
"\n"
"vec4 %2(vec4 col) {\n"
" return %1\n"
"}\n").arg(shader_call, process_function_name));
// Get pipeline-based shader to inject OCIO shader into
OpenGLShaderPtr shader = OpenGLShader::CreateDefault(process_function_name, shader_text);
// Release LUT
xf->glBindTexture(GL_TEXTURE_3D, 0);
xf->glActiveTexture(GL_TEXTURE0);
return shader;
}
QString OpenGLShader::CodeDefaultFragment(QString function_name, const QString &shader_code)
{
// Create shader header
QString frag_code = QStringLiteral("#version 150\n"
"\n"
"#ifdef GL_ES\n"
"precision highp int;\n"
"precision highp float;\n"
"#endif\n"
"\n"
"uniform sampler2D ove_maintex;\n"
"uniform vec2 ove_resolution;\n"
"uniform bool ove_deinterlace;\n"
"\n"
"in vec2 ove_texcoord;\n"
"\n"
"out vec4 fragColor;\n"
"\n");
// Check if additional code was passed to this function, add it here
if (!function_name.isEmpty() && !shader_code.isEmpty()) {
// If additional code was passed, add it and reference it in main().
//
// The function in the additional code is expected to be `vec4 function_name(vec4 color)`.
// The texture coordinate can be acquired through `ove_texcoord`.
frag_code.append(shader_code);
} else {
// No function to call
function_name = QString();
}
// Our function_name arg will either resolve to the function added to this or to nothing, in
// which case they'll just be benign brackets.
frag_code.append(QStringLiteral("\n"
"void main() {\n"
" vec2 using_texcoord = ove_texcoord;\n"
" if (ove_deinterlace) {\n"
" // A very basic deinterlace that halves the vertical\n"
" // resolution and linearly interpolates the two fields\n"
" // by reading the texture coord between them.\n"
" float half_vert = round(ove_resolution.y / 2.0);\n"
" using_texcoord.y = (round(using_texcoord.y * half_vert) + 0.25) / half_vert;\n"
" }\n"
" vec4 color = %1(texture(ove_maintex, using_texcoord));\n"
" fragColor = color;\n"
"}\n").arg(function_name));
return frag_code;
}
QString OpenGLShader::CodeDefaultVertex()
{
// Generate vertex shader
return QStringLiteral("#version 150\n"
"\n"
"#ifdef GL_ES\n"
"precision highp int;\n"
"precision highp float;\n"
"#endif\n"
"\n"
"uniform mat4 ove_mvpmat;\n"
"\n"
"in vec4 a_position;\n"
"in vec2 a_texcoord;\n"
"\n"
"out vec2 ove_texcoord;\n"
"\n"
"void main() {\n"
" gl_Position = ove_mvpmat * a_position;\n"
" ove_texcoord = a_texcoord;\n"
"}\n");
}
QString OpenGLShader::CodeAlphaDisassociate(const QString &function_name)
{
return QStringLiteral("vec4 %1(vec4 col) {\n"
" if (col.a > 0.0) {\n"
" return vec4(col.rgb / col.a, col.a);"
" }\n"
" return col;\n"
"}\n").arg(function_name);
}
QString OpenGLShader::CodeAlphaReassociate(const QString &function_name)
{
return QStringLiteral("vec4 %1(vec4 col) {\n"
" if (col.a > 0.0) {\n"
" return vec4(col.rgb * col.a, col.a);"
" }\n"
" return col;\n"
"}\n").arg(function_name);
}
QString OpenGLShader::CodeAlphaAssociate(const QString &function_name)
{
return QStringLiteral("vec4 %1(vec4 col) {\n"
" return vec4(col.rgb * col.a, col.a);\n"
"}\n").arg(function_name);
}
OLIVE_NAMESPACE_EXIT
-65
View File
@@ -1,65 +0,0 @@
/***
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/>.
***/
#ifndef OPENGLSHADER_H
#define OPENGLSHADER_H
#include <memory>
#include <QOpenGLShaderProgram>
#include <OpenColorIO/OpenColorIO.h>
namespace OCIO = OCIO_NAMESPACE::v1;
#include "common/define.h"
OLIVE_NAMESPACE_ENTER
class OpenGLShader;
using OpenGLShaderPtr = std::shared_ptr<OpenGLShader>;
/**
* @brief A simple QOpenGLShaderProgram derivative with static functions for creating
*/
class OpenGLShader : public QOpenGLShaderProgram {
public:
OpenGLShader() = default;
static OpenGLShaderPtr Create();
static OpenGLShaderPtr CreateDefault(const QString &function_name = QString(),
const QString &shader_code = QString());
static OpenGLShaderPtr CreateOCIO(QOpenGLContext* ctx,
GLuint& lut_texture,
OCIO::ConstProcessorRcPtr processor,
bool alpha_is_associated);
static QString CodeDefaultFragment(QString function_name = QString(),
const QString &shader_code = QString());
static QString CodeDefaultVertex();
static QString CodeAlphaDisassociate(const QString& function_name);
static QString CodeAlphaReassociate(const QString& function_name);
static QString CodeAlphaAssociate(const QString& function_name);
};
OLIVE_NAMESPACE_EXIT
#endif // OPENGLSHADER_H
+1 -1
View File
@@ -132,7 +132,7 @@ public slots:
const OLIVE_NAMESPACE::ShaderJob &job,
const OLIVE_NAMESPACE::VideoParams &params) = 0;
virtual QVariant TransformColor(QVariant texture, OLIVE_NAMESPACE::ColorProcessorPtr processor) = 0;
virtual TexturePtr TransformColor(Texture* texture, OLIVE_NAMESPACE::ColorProcessorPtr processor) = 0;
virtual void Render() = 0;