separation of previous renderer into base and derivation complete
The previous iteration was fairly OpenGL-heavy. It's now been separated into a base class that is OpenGL independent and a derived class that is OpenGL-based. Over time this should allow for portability away from OpenGL if necessary.
This commit is contained in:
@@ -2,8 +2,12 @@
|
||||
|
||||
#include <QThread>
|
||||
|
||||
OpenGLBackend::OpenGLBackend(QOpenGLContext *share_ctx) :
|
||||
share_ctx_(share_ctx)
|
||||
#include "functions.h"
|
||||
|
||||
OpenGLBackend::OpenGLBackend(QOpenGLContext *share_ctx, QObject *parent) :
|
||||
VideoRenderBackend(parent),
|
||||
share_ctx_(share_ctx),
|
||||
push_time_(-1)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -14,7 +18,9 @@ OpenGLBackend::~OpenGLBackend()
|
||||
|
||||
bool OpenGLBackend::Init()
|
||||
{
|
||||
threads_.resize(QThread::idealThreadCount());
|
||||
if (!OpenGLBackend::Init()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Some OpenGL implementations (notably wgl) require the context not to be current before sharing. We block the main
|
||||
// thread here to prevent QOpenGLWidget trying to reclaim the context before we're done
|
||||
@@ -22,16 +28,14 @@ bool OpenGLBackend::Init()
|
||||
share_ctx_->doneCurrent();
|
||||
|
||||
// Initiate one thread per CPU core
|
||||
for (int i=0;i<threads_.size();i++) {
|
||||
// Instantiate thread
|
||||
QThread* thread = new QThread(this);
|
||||
threads_.replace(i, thread);
|
||||
for (int i=0;i<threads().size();i++) {
|
||||
QThread* thread = threads().at(i);
|
||||
|
||||
// Create one processor object for each thread
|
||||
OpenGLProcessor* processor = new OpenGLProcessor(share_ctx_, thread);
|
||||
|
||||
// FIXME: Hardcoded values
|
||||
processor->SetParameters(VideoRenderingParams(viewer_node()->video_params(), olive::PIX_FMT_RGBA16F, olive::kOffline));
|
||||
processor->SetParameters(params());
|
||||
|
||||
// Finally, we can move it to its own thread
|
||||
processor->moveToThread(thread);
|
||||
@@ -40,23 +44,64 @@ bool OpenGLBackend::Init()
|
||||
// We've finished creating shared contexts, we can now restore the context to its previous current state
|
||||
share_ctx_->makeCurrent(old_surface);
|
||||
|
||||
// Create master texture (the one sent to the viewer)
|
||||
master_texture_ = std::make_shared<OpenGLTexture>();
|
||||
master_texture_->Create(share_ctx_, params().effective_width(), params().effective_height(), params().format());
|
||||
|
||||
// Create internal FBO for copying textures
|
||||
copy_buffer_.Create(share_ctx_);
|
||||
copy_buffer_.Attach(master_texture_);
|
||||
copy_pipeline_ = OpenGLShader::CreateDefault();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void OpenGLBackend::GenerateFrame(const rational &time)
|
||||
{
|
||||
Q_UNUSED(time)
|
||||
|
||||
/*threads().first()->Queue(NodeDependency(viewer_node()->texture_input()->get_connected_output(), time, time),
|
||||
true,
|
||||
false);*/
|
||||
}
|
||||
|
||||
void OpenGLBackend::Close()
|
||||
{
|
||||
if (!IsStarted()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Decompile();
|
||||
|
||||
// Clear all cores
|
||||
for (int i=0;i<threads_.size();i++) {
|
||||
delete threads_.at(i);
|
||||
copy_buffer_.Destroy();
|
||||
master_texture_ = nullptr;
|
||||
copy_pipeline_ = nullptr;
|
||||
|
||||
OpenGLBackend::Close();
|
||||
}
|
||||
|
||||
OpenGLTexturePtr OpenGLBackend::GetCachedFrameAsTexture(const rational &time)
|
||||
{
|
||||
last_time_requested_ = time;
|
||||
|
||||
if (push_time_ >= 0) {
|
||||
rational temp_push_time = push_time_;
|
||||
push_time_ = -1;
|
||||
|
||||
if (time == temp_push_time) {
|
||||
return master_texture_;
|
||||
}
|
||||
}
|
||||
threads_.clear();
|
||||
|
||||
const char* cached_frame = GetCachedFrame(time);
|
||||
|
||||
if (cached_frame != nullptr) {
|
||||
master_texture_->Upload(cached_frame);
|
||||
|
||||
return master_texture_;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool OpenGLBackend::Compile()
|
||||
@@ -160,6 +205,109 @@ QString OpenGLBackend::GenerateShaderID(NodeOutput *output)
|
||||
return QString("%1:%2").arg(output->parent()->id(), output->id());
|
||||
}
|
||||
|
||||
void OpenGLBackend::ThreadCallback(OpenGLTexturePtr texture, const rational& time, const QByteArray& hash)
|
||||
{
|
||||
// Threads are all done now, time to proceed
|
||||
caching_ = false;
|
||||
|
||||
DeferMap(time, hash);
|
||||
|
||||
if (texture != nullptr) {
|
||||
// We received a texture, time to start downloading it
|
||||
QString fn = CachePathName(hash);
|
||||
|
||||
/*
|
||||
download_threads_[last_download_thread_%download_threads_.size()]->Queue(texture,
|
||||
fn,
|
||||
hash);
|
||||
|
||||
last_download_thread_++;
|
||||
*/
|
||||
} else {
|
||||
// There was no texture here, we must update the viewer
|
||||
DownloadThreadComplete(hash);
|
||||
}
|
||||
|
||||
// If the connected output is using this time, signal it to update
|
||||
if (last_time_requested_ == time) {
|
||||
|
||||
copy_buffer_.Bind();
|
||||
|
||||
QOpenGLFunctions* f = QOpenGLContext::currentContext()->functions();
|
||||
|
||||
if (texture == nullptr) {
|
||||
|
||||
// No texture, clear the master and push it
|
||||
f->glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
f->glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
} else {
|
||||
texture->Bind();
|
||||
|
||||
f->glViewport(0, 0, master_texture_->width(), master_texture_->height());
|
||||
|
||||
olive::gl::Blit(copy_pipeline_);
|
||||
|
||||
texture->Release();
|
||||
}
|
||||
|
||||
copy_buffer_.Release();
|
||||
|
||||
push_time_ = time;
|
||||
|
||||
emit CachedFrameReady(time);
|
||||
}
|
||||
|
||||
CacheNext();
|
||||
}
|
||||
|
||||
void OpenGLBackend::ThreadRequestSibling(NodeDependency dep)
|
||||
{
|
||||
Q_UNUSED(dep)
|
||||
|
||||
// Try to queue another thread to run this dep in advance
|
||||
for (int i=1;i<threads().size();i++) {
|
||||
/*if (threads().at(i)->Queue(dep, false, true)) {
|
||||
return;
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGLBackend::ThreadSkippedFrame(const rational& time, const QByteArray& hash)
|
||||
{
|
||||
caching_ = false;
|
||||
|
||||
DeferMap(time, hash);
|
||||
|
||||
if (!IsCaching(hash)) {
|
||||
DownloadThreadComplete(hash);
|
||||
|
||||
// Signal output to update value
|
||||
emit CachedFrameReady(time);
|
||||
}
|
||||
|
||||
CacheNext();
|
||||
}
|
||||
|
||||
void OpenGLBackend::DownloadThreadComplete(const QByteArray &hash)
|
||||
{
|
||||
cache_hash_list_mutex_.lock();
|
||||
cache_hash_list_.removeAll(hash);
|
||||
cache_hash_list_mutex_.unlock();
|
||||
|
||||
for (int i=0;i<deferred_maps_.size();i++) {
|
||||
const HashTimeMapping& deferred = deferred_maps_.at(i);
|
||||
|
||||
if (deferred_maps_.at(i).hash == hash) {
|
||||
// Insert into hash map
|
||||
time_hash_map_.insert(deferred.time, deferred.hash);
|
||||
|
||||
deferred_maps_.removeAt(i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
OpenGLProcessor::OpenGLProcessor(QOpenGLContext *share_ctx, QObject *parent) :
|
||||
QObject(parent),
|
||||
share_ctx_(share_ctx),
|
||||
|
||||
@@ -5,8 +5,10 @@
|
||||
#include <QOffscreenSurface>
|
||||
#include <QOpenGLShaderProgram>
|
||||
|
||||
#include "../renderbackend.h"
|
||||
#include "../videorenderbackend.h"
|
||||
#include "openglframebuffer.h"
|
||||
#include "opengltexture.h"
|
||||
#include "openglshader.h"
|
||||
|
||||
class OpenGLProcessor : public QObject {
|
||||
public:
|
||||
@@ -45,24 +47,27 @@ private:
|
||||
VideoRenderingParams video_params_;
|
||||
};
|
||||
|
||||
class OpenGLBackend : public RenderBackend
|
||||
class OpenGLBackend : public VideoRenderBackend
|
||||
{
|
||||
public:
|
||||
OpenGLBackend(QOpenGLContext* share_ctx);
|
||||
OpenGLBackend(QOpenGLContext* share_ctx, QObject* parent = nullptr);
|
||||
|
||||
virtual ~OpenGLBackend() override;
|
||||
|
||||
virtual bool Init() override;
|
||||
|
||||
virtual void GenerateFrame(const rational& time) override;
|
||||
|
||||
virtual void Close() override;
|
||||
|
||||
OpenGLTexturePtr GetCachedFrameAsTexture(const rational& time);
|
||||
|
||||
public slots:
|
||||
virtual bool Compile() override;
|
||||
|
||||
virtual void Decompile() override;
|
||||
|
||||
protected:
|
||||
virtual void GenerateFrame(const rational& time) override;
|
||||
|
||||
private:
|
||||
QOpenGLContext* share_ctx_;
|
||||
|
||||
@@ -79,8 +84,22 @@ private:
|
||||
|
||||
QList<CompiledNode> compiled_nodes_;
|
||||
|
||||
QVector<QThread*> threads_;
|
||||
QVector<OpenGLProcessor*> processors_;
|
||||
|
||||
OpenGLTexturePtr master_texture_;
|
||||
rational push_time_;
|
||||
|
||||
OpenGLFramebuffer copy_buffer_;
|
||||
OpenGLShaderPtr copy_pipeline_;
|
||||
|
||||
private slots:
|
||||
void ThreadCallback(OpenGLTexturePtr texture, const rational& time, const QByteArray& hash);
|
||||
|
||||
void ThreadRequestSibling(NodeDependency dep);
|
||||
|
||||
void ThreadSkippedFrame(const rational &time, const QByteArray &hash);
|
||||
|
||||
void DownloadThreadComplete(const QByteArray &hash);
|
||||
};
|
||||
|
||||
#endif // OPENGLBACKEND_H
|
||||
|
||||
@@ -88,7 +88,7 @@ void OpenGLFramebuffer::Release()
|
||||
context_->functions()->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
void OpenGLFramebuffer::Attach(RenderTexturePtr texture)
|
||||
void OpenGLFramebuffer::Attach(OpenGLTexturePtr texture)
|
||||
{
|
||||
if (context_ == nullptr) {
|
||||
return;
|
||||
@@ -98,7 +98,7 @@ void OpenGLFramebuffer::Attach(RenderTexturePtr texture)
|
||||
AttachInternal(texture_->texture(), false);
|
||||
}
|
||||
|
||||
void OpenGLFramebuffer::AttachBackBuffer(RenderTexturePtr texture)
|
||||
void OpenGLFramebuffer::AttachBackBuffer(OpenGLTexturePtr texture)
|
||||
{
|
||||
if (context_ == nullptr) {
|
||||
return;
|
||||
|
||||
@@ -42,9 +42,9 @@ public:
|
||||
|
||||
void Release();
|
||||
|
||||
void Attach(RenderTexturePtr texture);
|
||||
void Attach(OpenGLTexturePtr texture);
|
||||
|
||||
void AttachBackBuffer(RenderTexturePtr texture);
|
||||
void AttachBackBuffer(OpenGLTexturePtr texture);
|
||||
|
||||
void Detach();
|
||||
|
||||
@@ -60,7 +60,7 @@ private:
|
||||
|
||||
GLuint buffer_;
|
||||
|
||||
RenderTexturePtr texture_;
|
||||
OpenGLTexturePtr texture_;
|
||||
};
|
||||
|
||||
#endif // OPENGLFRAMEBUFFER_H
|
||||
|
||||
@@ -84,7 +84,7 @@ private:
|
||||
olive::PixelFormat format_;
|
||||
};
|
||||
|
||||
using RenderTexturePtr = std::shared_ptr<OpenGLTexture>;
|
||||
Q_DECLARE_METATYPE(RenderTexturePtr)
|
||||
using OpenGLTexturePtr = std::shared_ptr<OpenGLTexture>;
|
||||
Q_DECLARE_METATYPE(OpenGLTexturePtr)
|
||||
|
||||
#endif // OPENGLTEXTURE_H
|
||||
|
||||
Reference in New Issue
Block a user