If you run into new OpenGL issues, this is likely the commit that caused it. I've tested this on as many systems as I have access to (including AMD, Apple, Intel, and NVIDIA GPUs with a mixture of Windows, Linux, and macOS) and it appears to work fine, however we all know how unpredictable GPUs can be. This should help increase GPU performance and compatibility. Squashed commit of the following: commit fa3c0ac7c469aebe2941a803736bdc06cdd5a2d8 Author: itsmattkc <itsmattkc@gmail.com> Date: Thu May 27 13:17:44 2021 +1000 viewer: updated pixel sampling for shared contexts commit 9334261da235f09cb67b193c5bb6169abd7a95f7 Author: itsmattkc <itsmattkc@gmail.com> Date: Thu May 27 13:12:39 2021 +1000 pixelsampler: show color preview box commit dacd9be6971c5c0312ae45c8db349d47910a80dd Author: itsmattkc <itsmattkc@gmail.com> Date: Thu May 27 12:43:32 2021 +1000 renderer: interlace on GPU commit 074cea06cb87e9fdc4e8ea6b09587be6df296057 Author: itsmattkc <itsmattkc@gmail.com> Date: Thu May 27 12:43:14 2021 +1000 renderer: added texture clear option commit 5d4f86b4cf1b1dcd3fb8e8c854ffa7f6fb8ca346 Author: itsmattkc <itsmattkc@gmail.com> Date: Thu May 27 12:42:12 2021 +1000 renderer: updated scopes for shared textures commit 6fad96358d189874cf45f7887b7e13cc2b1603c4 Author: itsmattkc <itsmattkc@gmail.com> Date: Thu May 27 11:42:05 2021 +1000 opengl: fixed edge case commit 1a19ffb53703ac6f30ed210f6d8eb5fc4b7b0f2d Author: itsmattkc <itsmattkc@gmail.com> Date: Thu May 27 10:56:13 2021 +1000 opengl: fix deinterlace shader commit 085961aa39cca90ebf51b919a78ef07a4882f0e1 Author: itsmattkc <itsmattkc@gmail.com> Date: Thu May 27 10:08:40 2021 +1000 opengl: downgrade to ES 2.0 With half-float/float/3D extensions, this should be doable commit ea1ab45cbcf5e017dfd0d8f28b231ff3255dfe92 Author: itsmattkc <itsmattkc@gmail.com> Date: Thu May 27 10:06:59 2021 +1000 renderer: fixed mutex deadlock commit ecc2f08b16f03421870c5b61625ecd26f2f96582 Author: itsmattkc <itsmattkc@gmail.com> Date: Wed May 26 21:28:13 2021 +1000 opengl: fixed shared contexts commit 261c689ea8089463155cf11b9ab15aa3f879f439 Merge: bfd4338a91c88b1cAuthor: itsmattkc <itsmattkc@gmail.com> Date: Wed May 26 19:52:00 2021 +1000 Merge branch 'master' into shared-contexts commit bfd4338aaa3b09b6fbb7717c637face8d5490721 Author: itsmattkc <itsmattkc@gmail.com> Date: Wed May 26 18:52:38 2021 +1000 viewer: remove debug line commit 45868d8a396181bf81c24b92a1ac5ce9ffed3426 Merge: 642f8e9dd0bd5e21Author: itsmattkc <itsmattkc@gmail.com> Date: Wed May 26 17:21:48 2021 +1000 Merge branch 'master' into shared-contexts commit 642f8e9df7d7d1a9ed4202d428f4d968ff668552 Merge: 861e5b894de0a727Author: itsmattkc <itsmattkc@gmail.com> Date: Wed May 26 17:01:38 2021 +1000 Merge branch 'master' of https://github.com/olive-editor/olive into shared-contexts commit 861e5b89a38c281897dc44b20e3587d39c4f808c Merge: 472598a7ef31c3fbAuthor: itsmattkc <itsmattkc@gmail.com> Date: Wed May 26 14:28:21 2021 +1000 Merge branch 'master' into shared-contexts commit 472598a740a095aa43a4aad5680dd5abcf453a4c Author: itsmattkc <itsmattkc@gmail.com> Date: Fri May 14 16:29:35 2021 +1000 attempt at sharing contexts
307 lines
13 KiB
C++
307 lines
13 KiB
C++
/***
|
|
|
|
Olive - Non-Linear Video Editor
|
|
Copyright (C) 2021 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 "renderer.h"
|
|
|
|
#include <QVector2D>
|
|
|
|
#include "common/ocioutils.h"
|
|
|
|
namespace olive {
|
|
|
|
Renderer::Renderer(QObject *parent) :
|
|
QObject(parent)
|
|
{
|
|
|
|
}
|
|
|
|
TexturePtr Renderer::CreateTexture(const VideoParams ¶ms, Texture::Type type, const void *data, int linesize)
|
|
{
|
|
QVariant v;
|
|
|
|
if (type == Texture::k3D) {
|
|
v = CreateNativeTexture3D(params.effective_width(), params.effective_height(),
|
|
params.effective_depth(), params.format(), params.channel_count(), data, linesize);
|
|
} else {
|
|
v = CreateNativeTexture2D(params.effective_width(), params.effective_height(), params.format(),
|
|
params.channel_count(), data, linesize);
|
|
}
|
|
|
|
return CreateTextureFromNativeHandle(v, params, type);
|
|
}
|
|
|
|
TexturePtr Renderer::CreateTexture(const VideoParams ¶ms, const void *data, int linesize)
|
|
{
|
|
return CreateTexture(params, Texture::k2D, data, linesize);
|
|
}
|
|
|
|
void Renderer::BlitColorManaged(ColorProcessorPtr color_processor, TexturePtr source, bool source_is_premultiplied, Texture *destination, bool clear_destination, const QMatrix4x4 &matrix, const QMatrix4x4 &crop_matrix)
|
|
{
|
|
BlitColorManagedInternal(color_processor, source, source_is_premultiplied, destination, destination->params(), clear_destination, matrix, crop_matrix);
|
|
}
|
|
|
|
void Renderer::BlitColorManaged(ColorProcessorPtr color_processor, TexturePtr source, bool source_is_premultiplied, VideoParams params, bool clear_destination, const QMatrix4x4& matrix, const QMatrix4x4 &crop_matrix)
|
|
{
|
|
BlitColorManagedInternal(color_processor, source, source_is_premultiplied, nullptr, params, clear_destination, matrix, crop_matrix);
|
|
}
|
|
|
|
TexturePtr Renderer::InterlaceTexture(TexturePtr top, TexturePtr bottom, const VideoParams ¶ms)
|
|
{
|
|
color_cache_mutex_.lock();
|
|
if (interlace_texture_.isNull()) {
|
|
interlace_texture_ = CreateNativeShader(ShaderCode(FileFunctions::ReadFileAsString(QStringLiteral(":/shaders/interlace.frag"))));
|
|
}
|
|
color_cache_mutex_.unlock();
|
|
|
|
ShaderJob job;
|
|
job.InsertValue(QStringLiteral("top_tex_in"), NodeValue(NodeValue::kTexture, QVariant::fromValue(top)));
|
|
job.InsertValue(QStringLiteral("bottom_tex_in"), NodeValue(NodeValue::kTexture, QVariant::fromValue(bottom)));
|
|
job.InsertValue(QStringLiteral("resolution_in"), NodeValue(NodeValue::kVec2, QVector2D(params.effective_width(), params.effective_height())));
|
|
|
|
TexturePtr output = CreateTexture(params);
|
|
|
|
BlitToTexture(interlace_texture_, job, output.get());
|
|
|
|
return output;
|
|
}
|
|
|
|
void Renderer::Destroy()
|
|
{
|
|
color_cache_.clear();
|
|
|
|
if (!interlace_texture_.isNull()) {
|
|
DestroyNativeShader(interlace_texture_);
|
|
interlace_texture_.clear();
|
|
}
|
|
|
|
DestroyInternal();
|
|
}
|
|
|
|
TexturePtr Renderer::CreateTextureFromNativeHandle(const QVariant &v, const VideoParams ¶ms, Texture::Type type)
|
|
{
|
|
if (v.isNull()) {
|
|
return nullptr;
|
|
}
|
|
|
|
return std::make_shared<Texture>(this, v, params, type);
|
|
}
|
|
|
|
bool Renderer::GetColorContext(ColorProcessorPtr color_processor, Renderer::ColorContext *ctx)
|
|
{
|
|
QMutexLocker locker(&color_cache_mutex_);
|
|
|
|
ColorContext& color_ctx = *ctx;
|
|
|
|
if (color_cache_.contains(color_processor->id())) {
|
|
color_ctx = color_cache_.value(color_processor->id());
|
|
return true;
|
|
} else {
|
|
// Create shader description
|
|
const char* ocio_func_name = "OCIODisplay";
|
|
auto shader_desc = OCIO::GpuShaderDesc::CreateShaderDesc();
|
|
shader_desc->setLanguage(OCIO::GPU_LANGUAGE_GLSL_1_3);
|
|
shader_desc->setFunctionName(ocio_func_name);
|
|
shader_desc->setResourcePrefix("ocio_");
|
|
|
|
// Generate shader
|
|
color_processor->GetProcessor()->getDefaultGPUProcessor()->extractGpuShaderInfo(shader_desc);
|
|
|
|
QString shader_frag;
|
|
shader_frag.append(QStringLiteral("// Main texture input\n"
|
|
"uniform sampler2D ove_maintex;\n"
|
|
"uniform int ove_maintex_alpha;\n"
|
|
"uniform mat4 ove_cropmatrix;\n"
|
|
"\n"
|
|
"// Macros defining `ove_maintex_alpha` state\n"
|
|
"// Matches `AlphaAssociated` C++ enum\n"
|
|
"#define ALPHA_NONE 0\n"
|
|
"#define ALPHA_UNASSOC 1\n"
|
|
"#define ALPHA_ASSOC 2\n"
|
|
"\n"
|
|
"// Main texture coordinate\n"
|
|
"varying vec2 ove_texcoord;\n"
|
|
"\n"));
|
|
shader_frag.append(shader_desc->getShaderText());
|
|
shader_frag.append(QStringLiteral("\n"
|
|
"// Alpha association functions\n"
|
|
"vec4 assoc(vec4 c) {\n"
|
|
" return vec4(c.rgb * c.a, c.a);\n"
|
|
"}\n"
|
|
"\n"
|
|
"vec4 reassoc(vec4 c) {\n"
|
|
" return (c.a == 0.0) ? c : assoc(c);\n"
|
|
"}\n"
|
|
"\n"
|
|
"vec4 deassoc(vec4 c) {\n"
|
|
" return (c.a == 0.0) ? c : vec4(c.rgb / c.a, c.a);\n"
|
|
"}\n"
|
|
"\n"
|
|
"void main() {\n"
|
|
" vec2 cropped_coord = (vec4(ove_texcoord-vec2(0.5, 0.5), 0.0, 1.0)*ove_cropmatrix).xy + vec2(0.5, 0.5);\n"
|
|
" if (cropped_coord.x < 0.0 || cropped_coord.x >= 1.0 || cropped_coord.y < 0.0 || cropped_coord.y >= 1.0) {\n"
|
|
" gl_FragColor = vec4(0.0);\n"
|
|
" return;\n"
|
|
" }\n"
|
|
" \n"
|
|
" vec4 col = texture2D(ove_maintex, cropped_coord);\n"
|
|
"\n"
|
|
" // If alpha is associated, de-associate now\n"
|
|
" if (ove_maintex_alpha == ALPHA_ASSOC) {\n"
|
|
" col = deassoc(col);\n"
|
|
" }\n"
|
|
"\n"
|
|
" // Perform color conversion\n"
|
|
" col = %1(col);\n"
|
|
"\n"
|
|
" // Associate or re-associate here\n"
|
|
" if (ove_maintex_alpha == ALPHA_ASSOC) {\n"
|
|
" col = reassoc(col);\n"
|
|
" } else if (ove_maintex_alpha == ALPHA_UNASSOC) {\n"
|
|
" col = assoc(col);\n"
|
|
" }\n"
|
|
"\n"
|
|
" gl_FragColor = col;\n"
|
|
"}\n").arg(ocio_func_name));
|
|
|
|
// Try to compile shader
|
|
color_ctx.compiled_shader = CreateNativeShader(ShaderCode(shader_frag,
|
|
FileFunctions::ReadFileAsString(QStringLiteral(":/shaders/default.vert"))));
|
|
|
|
if (color_ctx.compiled_shader.isNull()) {
|
|
return false;
|
|
}
|
|
|
|
color_ctx.lut3d_textures.resize(shader_desc->getNum3DTextures());
|
|
for (unsigned int i=0; i<shader_desc->getNum3DTextures(); i++) {
|
|
const char* tex_name = nullptr;
|
|
const char* sampler_name = nullptr;
|
|
unsigned int edge_len = 0;
|
|
OCIO::Interpolation interpolation = OCIO::INTERP_LINEAR;
|
|
|
|
shader_desc->get3DTexture(i, tex_name, sampler_name, edge_len, interpolation);
|
|
|
|
if (!tex_name || !*tex_name
|
|
|| !sampler_name || !*sampler_name
|
|
|| !edge_len) {
|
|
qCritical() << "3D LUT texture data is corrupted";
|
|
return false;
|
|
}
|
|
|
|
const float* values = nullptr;
|
|
shader_desc->get3DTextureValues(i, values);
|
|
if (!values) {
|
|
qCritical() << "3D LUT texture values are missing";
|
|
return false;
|
|
}
|
|
|
|
// Allocate 3D LUT
|
|
color_ctx.lut3d_textures[i].texture = CreateTexture(VideoParams(edge_len, edge_len, edge_len, VideoParams::kFormatFloat32, VideoParams::kRGBChannelCount),
|
|
Texture::k3D, values);
|
|
color_ctx.lut3d_textures[i].name = sampler_name;
|
|
color_ctx.lut3d_textures[i].interpolation = (interpolation == OCIO::INTERP_NEAREST) ? Texture::kNearest : Texture::kLinear;
|
|
}
|
|
|
|
color_ctx.lut1d_textures.resize(shader_desc->getNumTextures());
|
|
for (unsigned int i=0; i<shader_desc->getNumTextures(); i++) {
|
|
const char* tex_name = nullptr;
|
|
const char* sampler_name = nullptr;
|
|
unsigned int width = 0, height = 0;
|
|
OCIO::GpuShaderDesc::TextureType channel = OCIO::GpuShaderDesc::TEXTURE_RGB_CHANNEL;
|
|
OCIO::Interpolation interpolation = OCIO::INTERP_LINEAR;
|
|
|
|
shader_desc->getTexture(i, tex_name, sampler_name, width, height, channel, interpolation);
|
|
|
|
if (!tex_name || !*tex_name
|
|
|| !sampler_name || !*sampler_name
|
|
|| !width) {
|
|
qCritical() << "1D LUT texture data is corrupted";
|
|
return false;
|
|
}
|
|
|
|
const float* values = nullptr;
|
|
shader_desc->getTextureValues(i, values);
|
|
if (!values) {
|
|
qCritical() << "1D LUT texture values are missing";
|
|
return false;
|
|
}
|
|
|
|
// Allocate 1D LUT
|
|
color_ctx.lut1d_textures[i].texture = CreateTexture(VideoParams(width, height, VideoParams::kFormatFloat32, (channel == OCIO::GpuShaderDesc::TEXTURE_RED_CHANNEL) ? 1 : VideoParams::kRGBChannelCount),
|
|
Texture::k2D,
|
|
values);
|
|
color_ctx.lut1d_textures[i].name = sampler_name;
|
|
color_ctx.lut1d_textures[i].interpolation = (interpolation == OCIO::INTERP_NEAREST) ? Texture::kNearest : Texture::kLinear;
|
|
}
|
|
|
|
color_cache_.insert(color_processor->id(), color_ctx);
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
void Renderer::BlitColorManagedInternal(ColorProcessorPtr color_processor, TexturePtr source,
|
|
bool source_is_premultiplied, Texture *destination,
|
|
VideoParams params, bool clear_destination, const QMatrix4x4& matrix,
|
|
const QMatrix4x4& crop_matrix)
|
|
{
|
|
ColorContext color_ctx;
|
|
if (!GetColorContext(color_processor, &color_ctx)) {
|
|
return;
|
|
}
|
|
|
|
ShaderJob job;
|
|
|
|
job.InsertValue(QStringLiteral("ove_maintex"), NodeValue(NodeValue::kTexture, QVariant::fromValue(source)));
|
|
job.InsertValue(QStringLiteral("ove_mvpmat"), NodeValue(NodeValue::kMatrix, matrix));
|
|
job.InsertValue(QStringLiteral("ove_cropmatrix"), NodeValue(NodeValue::kMatrix, crop_matrix.inverted()));
|
|
|
|
AlphaAssociated associated;
|
|
if (source->channel_count() == VideoParams::kRGBAChannelCount) {
|
|
if (source_is_premultiplied) {
|
|
// De-assoc/re-assoc required for color management
|
|
associated = kAlphaAssociated;
|
|
} else {
|
|
// Just assoc at the end
|
|
associated = kAlphaUnassociated;
|
|
}
|
|
} else {
|
|
// No assoc/deassoc required
|
|
associated = kAlphaNone;
|
|
}
|
|
job.InsertValue(QStringLiteral("ove_maintex_alpha"), NodeValue(NodeValue::kInt, associated));
|
|
|
|
foreach (const ColorContext::LUT& l, color_ctx.lut3d_textures) {
|
|
job.InsertValue(l.name, NodeValue(NodeValue::kTexture, QVariant::fromValue(l.texture)));
|
|
job.SetInterpolation(l.name, l.interpolation);
|
|
}
|
|
foreach (const ColorContext::LUT& l, color_ctx.lut1d_textures) {
|
|
job.InsertValue(l.name, NodeValue(NodeValue::kTexture, QVariant::fromValue(l.texture)));
|
|
job.SetInterpolation(l.name, l.interpolation);
|
|
}
|
|
|
|
if (destination) {
|
|
BlitToTexture(color_ctx.compiled_shader, job, destination, clear_destination);
|
|
} else {
|
|
Blit(color_ctx.compiled_shader, job, params, clear_destination);
|
|
}
|
|
}
|
|
|
|
}
|