2021-12-04 21:33:04 +01:00
|
|
|
#include "qt_hardwarerenderer.hpp"
|
2021-12-08 16:36:55 +06:00
|
|
|
#include <QApplication>
|
2021-12-25 21:49:25 +06:00
|
|
|
#include <QVector2D>
|
2021-12-15 00:37:48 +02:00
|
|
|
#include <atomic>
|
2021-12-04 21:33:04 +01:00
|
|
|
|
2021-12-05 20:56:07 +01:00
|
|
|
extern "C" {
|
|
|
|
|
#include <86box/86box.h>
|
2021-12-17 18:37:57 +02:00
|
|
|
#include <86box/plat.h>
|
2021-12-26 01:34:34 +06:00
|
|
|
#include <86box/video.h>
|
2021-12-05 20:56:07 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-04 21:33:04 +01:00
|
|
|
void HardwareRenderer::resizeGL(int w, int h)
|
|
|
|
|
{
|
|
|
|
|
glViewport(0, 0, w, h);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-25 21:49:25 +06:00
|
|
|
#define PROGRAM_VERTEX_ATTRIBUTE 0
|
|
|
|
|
#define PROGRAM_TEXCOORD_ATTRIBUTE 1
|
|
|
|
|
|
2021-12-04 21:33:04 +01:00
|
|
|
void HardwareRenderer::initializeGL()
|
|
|
|
|
{
|
2021-12-23 20:31:51 +02:00
|
|
|
m_context->makeCurrent(this);
|
2021-12-04 21:33:04 +01:00
|
|
|
initializeOpenGLFunctions();
|
2021-12-25 15:34:00 +06:00
|
|
|
m_texture = new QOpenGLTexture(image);
|
|
|
|
|
m_blt = new QOpenGLTextureBlitter;
|
|
|
|
|
m_blt->setRedBlueSwizzle(true);
|
|
|
|
|
m_blt->create();
|
2021-12-25 21:49:25 +06:00
|
|
|
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);
|
2021-12-04 21:33:04 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-07 13:47:42 +01:00
|
|
|
void HardwareRenderer::paintGL() {
|
2021-12-25 15:34:00 +06:00
|
|
|
m_context->makeCurrent(this);
|
2021-12-25 21:49:25 +06:00
|
|
|
QVector<QVector2D> 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();
|
2021-12-26 01:34:34 +06:00
|
|
|
m_texture->setMinMagFilters(video_filter_method ? QOpenGLTexture::Linear : QOpenGLTexture::Nearest, video_filter_method ? QOpenGLTexture::Linear : QOpenGLTexture::Nearest);
|
2021-12-25 21:49:25 +06:00
|
|
|
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
2021-12-04 21:33:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HardwareRenderer::setRenderType(RenderType type) {
|
|
|
|
|
QSurfaceFormat format;
|
|
|
|
|
switch (type) {
|
|
|
|
|
case RenderType::OpenGL:
|
|
|
|
|
format.setRenderableType(QSurfaceFormat::OpenGL);
|
|
|
|
|
break;
|
|
|
|
|
case RenderType::OpenGLES:
|
|
|
|
|
format.setRenderableType(QSurfaceFormat::OpenGLES);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-12-16 07:52:33 +02:00
|
|
|
format.setSwapInterval(0);
|
2021-12-04 21:33:04 +01:00
|
|
|
setFormat(format);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 16:38:13 +06:00
|
|
|
void HardwareRenderer::onBlit(const std::unique_ptr<uint8_t>* img, int x, int y, int w, int h, std::atomic_flag* in_use) {
|
2021-12-25 15:34:00 +06:00
|
|
|
auto tval = this;
|
|
|
|
|
void* nuldata = 0;
|
|
|
|
|
if (memcmp(&tval, &nuldata, sizeof(void*)) == 0) return;
|
|
|
|
|
m_context->makeCurrent(this);
|
|
|
|
|
m_texture->setData(QOpenGLTexture::PixelFormat::RGBA, QOpenGLTexture::PixelType::UInt8, (const void*)img->get());
|
2021-12-21 16:38:13 +06:00
|
|
|
in_use->clear();
|
2021-12-07 13:47:42 +01:00
|
|
|
source.setRect(x, y, w, h);
|
2021-12-04 21:33:04 +01:00
|
|
|
update();
|
|
|
|
|
}
|
2021-12-07 13:47:42 +01:00
|
|
|
|
|
|
|
|
void HardwareRenderer::resizeEvent(QResizeEvent *event) {
|
|
|
|
|
onResize(width(), height());
|
2021-12-23 20:31:51 +02:00
|
|
|
|
2021-12-17 12:17:54 +06:00
|
|
|
QOpenGLWindow::resizeEvent(event);
|
2021-12-07 13:47:42 +01:00
|
|
|
}
|
2021-12-17 18:37:57 +02:00
|
|
|
|
2021-12-18 00:37:30 +06:00
|
|
|
bool HardwareRenderer::event(QEvent *event)
|
2021-12-17 18:37:57 +02:00
|
|
|
{
|
2021-12-18 00:37:30 +06:00
|
|
|
switch (event->type())
|
2021-12-17 18:37:57 +02:00
|
|
|
{
|
2021-12-18 00:37:30 +06:00
|
|
|
default:
|
|
|
|
|
return QOpenGLWindow::event(event);
|
|
|
|
|
case QEvent::MouseButtonPress:
|
|
|
|
|
case QEvent::MouseMove:
|
|
|
|
|
case QEvent::MouseButtonRelease:
|
|
|
|
|
case QEvent::KeyPress:
|
|
|
|
|
case QEvent::KeyRelease:
|
|
|
|
|
case QEvent::Wheel:
|
|
|
|
|
case QEvent::Enter:
|
|
|
|
|
case QEvent::Leave:
|
|
|
|
|
return QApplication::sendEvent(parentWidget, event);
|
2021-12-17 18:37:57 +02:00
|
|
|
}
|
2021-12-18 00:37:30 +06:00
|
|
|
return false;
|
|
|
|
|
}
|