BUGZ-964: fix for incorrect color curve when rendering 3D into QML

This commit is contained in:
Brad Davis 2019-07-25 18:24:53 -07:00
parent 7e91fe9e8b
commit 11d7890f48
2 changed files with 70 additions and 11 deletions

View file

@ -11,11 +11,52 @@
#include "ResourceImageItem.h"
#include <gl/Config.h>
#include <gl/GLHelpers.h>
#include <QOpenGLFramebufferObjectFormat>
#include <QOpenGLShaderProgram>
#include <plugins/DisplayPlugin.h>
static const char* VERTEX_SHADER = R"SHADER(
#version 450 core
out vec2 vTexCoord;
void main(void) {
const float depth = 0.0;
const vec4 UNIT_QUAD[4] = vec4[4](
vec4(-1.0, -1.0, depth, 1.0),
vec4(1.0, -1.0, depth, 1.0),
vec4(-1.0, 1.0, depth, 1.0),
vec4(1.0, 1.0, depth, 1.0)
);
vec4 pos = UNIT_QUAD[gl_VertexID];
gl_Position = pos;
vTexCoord = (pos.xy + 1.0) * 0.5;
}
)SHADER";
static const char* FRAGMENT_SHADER = R"SHADER(
#version 450 core
uniform sampler2D sampler;
in vec2 vTexCoord;
out vec4 FragColor;
vec3 color_LinearTosRGB(vec3 lrgb) {
return mix(vec3(1.055) * pow(vec3(lrgb), vec3(0.41666)) - vec3(0.055), vec3(lrgb) * vec3(12.92), vec3(lessThan(lrgb, vec3(0.0031308))));
}
void main() {
FragColor = vec4(color_LinearTosRGB(texture(sampler, vTexCoord).rgb), 1.0);
}
)SHADER";
ResourceImageItem::ResourceImageItem() : QQuickFramebufferObject() {
auto textureCache = DependencyManager::get<TextureCache>();
connect(textureCache.data(), SIGNAL(spectatorCameraFramebufferReset()), this, SLOT(update()));
@ -95,16 +136,29 @@ void ResourceImageItemRenderer::render() {
}
if (_ready) {
_fboMutex.lock();
_copyFbo->bind();
QOpenGLFramebufferObject::blitFramebuffer(framebufferObject(), _copyFbo, GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT, GL_NEAREST);
// this clears the copyFbo texture
// so next frame starts fresh - helps
// when aspect ratio changes
_copyFbo->takeTexture();
if (!_shader) {
_shader = new QOpenGLShaderProgram();
_shader->addCacheableShaderFromSourceCode(QOpenGLShader::Vertex, VERTEX_SHADER);
_shader->addCacheableShaderFromSourceCode(QOpenGLShader::Fragment, FRAGMENT_SHADER);
_shader->link();
glGenVertexArrays(1, &_vao);
}
framebufferObject()->bind();
_shader->bind();
auto sourceTextureId = _copyFbo->takeTexture();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, sourceTextureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindVertexArray(_vao);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDeleteTextures(1, &sourceTextureId);
_copyFbo->bind();
_copyFbo->release();
_fboMutex.unlock();
}
glFlush();

View file

@ -22,6 +22,9 @@
#include <TextureCache.h>
class QOpenGLFramebufferObject;
class QOpenGLShaderProgram;
class ResourceImageItemRenderer : public QObject, public QQuickFramebufferObject::Renderer {
Q_OBJECT
public:
@ -30,14 +33,16 @@ public:
void synchronize(QQuickFramebufferObject* item) override;
void render() override;
private:
bool _ready;
bool _ready{ false };
QString _url;
bool _visible;
bool _visible{ false };
NetworkTexturePointer _networkTexture;
QQuickWindow* _window;
QQuickWindow* _window{ nullptr };
QMutex _fboMutex;
uint32_t _vao{ 0 };
QOpenGLFramebufferObject* _copyFbo { nullptr };
QOpenGLShaderProgram* _shader{ nullptr };
GLsync _fenceSync { 0 };
QTimer _updateTimer;
public slots: