From 033abed3fecf8a35066935d0a8196f36ac3045f4 Mon Sep 17 00:00:00 2001 From: Cacodemon345 Date: Sat, 25 Dec 2021 21:49:25 +0600 Subject: [PATCH] Use shader-based hardware rendering --- src/qt/qt_hardwarerenderer.cpp | 60 +++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/src/qt/qt_hardwarerenderer.cpp b/src/qt/qt_hardwarerenderer.cpp index 9c3df9c48..2e029f066 100644 --- a/src/qt/qt_hardwarerenderer.cpp +++ b/src/qt/qt_hardwarerenderer.cpp @@ -1,5 +1,6 @@ #include "qt_hardwarerenderer.hpp" #include +#include #include extern "C" { @@ -12,6 +13,9 @@ void HardwareRenderer::resizeGL(int w, int h) glViewport(0, 0, w, h); } +#define PROGRAM_VERTEX_ATTRIBUTE 0 +#define PROGRAM_TEXCOORD_ATTRIBUTE 1 + void HardwareRenderer::initializeGL() { m_context->makeCurrent(this); @@ -20,14 +24,62 @@ void HardwareRenderer::initializeGL() m_blt = new QOpenGLTextureBlitter; m_blt->setRedBlueSwizzle(true); m_blt->create(); + QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this); + const char *vsrc = + "attribute highp vec4 vertex;\n" + "attribute mediump vec4 texCoord;\n" + "varying mediump vec4 texc;\n" + "uniform mediump mat4 matrix;\n" + "void main(void)\n" + "{\n" + " gl_Position = matrix * vertex;\n" + " texc = texCoord;\n" + "}\n"; + vshader->compileSourceCode(vsrc); + + QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this); + const char *fsrc = + "uniform sampler2D texture;\n" + "varying mediump vec4 texc;\n" + "void main(void)\n" + "{\n" + " gl_FragColor = texture2D(texture, texc.st).bgra;\n" + "}\n"; + fshader->compileSourceCode(fsrc); + + m_prog = new QOpenGLShaderProgram; + m_prog->addShader(vshader); + m_prog->addShader(fshader); + m_prog->bindAttributeLocation("vertex", PROGRAM_VERTEX_ATTRIBUTE); + m_prog->bindAttributeLocation("texCoord", PROGRAM_TEXCOORD_ATTRIBUTE); + m_prog->link(); + + m_prog->bind(); + m_prog->setUniformValue("texture", 0); } void HardwareRenderer::paintGL() { m_context->makeCurrent(this); - m_blt->bind(); - QMatrix4x4 target = QOpenGLTextureBlitter::targetTransform(QRect(0, 0, 2048, 2048), source); - m_blt->blit(m_texture->textureId(), target, QOpenGLTextureBlitter::Origin::OriginTopLeft); - m_blt->release(); + QVector verts, texcoords; + QMatrix4x4 mat; + mat.setToIdentity(); + mat.ortho(QRect(0, 0, width(), height())); + verts.push_back(QVector2D((float)destination.x(), (float)destination.y())); + verts.push_back(QVector2D((float)destination.x(), (float)destination.y() + destination.height())); + verts.push_back(QVector2D((float)destination.x() + destination.width(), (float)destination.y() + destination.height())); + verts.push_back(QVector2D((float)destination.x() + destination.width(), (float)destination.y())); + texcoords.push_back(QVector2D((float)source.x() / 2048.f, (float)(source.y()) / 2048.f)); + texcoords.push_back(QVector2D((float)source.x() / 2048.f, (float)(source.y() + source.height()) / 2048.f)); + texcoords.push_back(QVector2D((float)(source.x() + source.width()) / 2048.f, (float)(source.y() + source.height()) / 2048.f)); + texcoords.push_back(QVector2D((float)(source.x() + source.width()) / 2048.f, (float)(source.y()) / 2048.f)); + + m_prog->setUniformValue("matrix", mat); + m_prog->enableAttributeArray(PROGRAM_VERTEX_ATTRIBUTE); + m_prog->enableAttributeArray(PROGRAM_TEXCOORD_ATTRIBUTE); + m_prog->setAttributeArray(PROGRAM_VERTEX_ATTRIBUTE, verts.data()); + m_prog->setAttributeArray(PROGRAM_TEXCOORD_ATTRIBUTE, texcoords.data()); + m_texture->bind(); + glDrawArrays(GL_TRIANGLE_FAN, 0, 4); } void HardwareRenderer::setRenderType(RenderType type) {