mirror of
https://github.com/melonDS-emu/melonDS.git
synced 2025-07-24 14:49:53 -06:00
No more context mess (#1531)
* WIP: use Duckstation's context code to directly render into QT Widget from separate thread without two OpenGL contexts currently only works on Windows * reenable gay OSD * add back vsync * make it atleast a little more thread safe * linux support * don't segfault on closing * reorganise and cleanup build system it's still not good, but better than before * macos? * try to get it working on Ubuntu CI also update instructions * let's try this * ok how about this * try creating an OGL 4.3 context first (https://i.kym-cdn.com/photos/images/original/001/264/842/220.png) * fix Ubuntu * hm * try again for Windows * let's try this * make the OpenGL renderer work again that was stupid * do OGL surface resizing from the mainthread * Fix small mistake in GL context creation on macOS causing version 3.2 to be considered invalid * C stupidness * cleanup * don't let the emuthread deinit OGL if there's no OGL * reset lastScreenWidth/Height when deiniting OpenGL * disable stencil test while drawing framebuffers * macOS: Link Cocoa framework explicitly when not building with Qt6 Seems to be needed for the classes used by DuckStation's GL context code. * Set ScreenPanelGL's minimum size immediately Fixes GL context creation for OpenGL display on macOS using the wrong size as the underlying window was not resized to the correct size by Qt yet. * don't emit window updates when OGL display is used * stuff Arisotura said Co-authored-by: Nadia Holmquist Pedersen <nadia@nhp.sh>
This commit is contained in:
@ -23,6 +23,7 @@
|
||||
#include "../types.h"
|
||||
|
||||
#include "main.h"
|
||||
#include "OpenGLSupport.h"
|
||||
#include <QPainter>
|
||||
|
||||
#include "OSD.h"
|
||||
@ -52,42 +53,37 @@ struct Item
|
||||
|
||||
bool GLTextureLoaded;
|
||||
GLuint GLTexture;
|
||||
|
||||
};
|
||||
|
||||
std::deque<Item> ItemQueue;
|
||||
|
||||
QOpenGLShaderProgram* Shader;
|
||||
GLuint Shader[3];
|
||||
GLint uScreenSize, uOSDPos, uOSDSize;
|
||||
GLfloat uScaleFactor;
|
||||
GLuint OSDVertexArray;
|
||||
GLuint OSDVertexBuffer;
|
||||
|
||||
volatile bool Rendering;
|
||||
QMutex Rendering;
|
||||
|
||||
|
||||
bool Init(QOpenGLFunctions_3_2_Core* f)
|
||||
bool Init(bool openGL)
|
||||
{
|
||||
if (f)
|
||||
if (openGL)
|
||||
{
|
||||
Shader = new QOpenGLShaderProgram();
|
||||
Shader->addShaderFromSourceCode(QOpenGLShader::Vertex, kScreenVS_OSD);
|
||||
Shader->addShaderFromSourceCode(QOpenGLShader::Fragment, kScreenFS_OSD);
|
||||
OpenGL::BuildShaderProgram(kScreenVS_OSD, kScreenFS_OSD, Shader, "OSDShader");
|
||||
|
||||
GLuint pid = Shader->programId();
|
||||
f->glBindAttribLocation(pid, 0, "vPosition");
|
||||
f->glBindFragDataLocation(pid, 0, "oColor");
|
||||
GLuint pid = Shader[2];
|
||||
glBindAttribLocation(pid, 0, "vPosition");
|
||||
glBindFragDataLocation(pid, 0, "oColor");
|
||||
|
||||
Shader->link();
|
||||
OpenGL::LinkShaderProgram(Shader);
|
||||
glUseProgram(pid);
|
||||
glUniform1i(glGetUniformLocation(pid, "OSDTex"), 0);
|
||||
|
||||
Shader->bind();
|
||||
Shader->setUniformValue("OSDTex", (GLint)0);
|
||||
Shader->release();
|
||||
|
||||
uScreenSize = Shader->uniformLocation("uScreenSize");
|
||||
uOSDPos = Shader->uniformLocation("uOSDPos");
|
||||
uOSDSize = Shader->uniformLocation("uOSDSize");
|
||||
uScaleFactor = Shader->uniformLocation("uScaleFactor");
|
||||
uScreenSize = glGetUniformLocation(pid, "uScreenSize");
|
||||
uOSDPos = glGetUniformLocation(pid, "uOSDPos");
|
||||
uOSDSize = glGetUniformLocation(pid, "uOSDSize");
|
||||
uScaleFactor = glGetUniformLocation(pid, "uScaleFactor");
|
||||
|
||||
float vertices[6*2] =
|
||||
{
|
||||
@ -99,32 +95,30 @@ bool Init(QOpenGLFunctions_3_2_Core* f)
|
||||
1, 1
|
||||
};
|
||||
|
||||
f->glGenBuffers(1, &OSDVertexBuffer);
|
||||
f->glBindBuffer(GL_ARRAY_BUFFER, OSDVertexBuffer);
|
||||
f->glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
glGenBuffers(1, &OSDVertexBuffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, OSDVertexBuffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
|
||||
f->glGenVertexArrays(1, &OSDVertexArray);
|
||||
f->glBindVertexArray(OSDVertexArray);
|
||||
f->glEnableVertexAttribArray(0); // position
|
||||
f->glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void*)(0));
|
||||
glGenVertexArrays(1, &OSDVertexArray);
|
||||
glBindVertexArray(OSDVertexArray);
|
||||
glEnableVertexAttribArray(0); // position
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void*)(0));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void DeInit(QOpenGLFunctions_3_2_Core* f)
|
||||
void DeInit()
|
||||
{
|
||||
for (auto it = ItemQueue.begin(); it != ItemQueue.end(); )
|
||||
{
|
||||
Item& item = *it;
|
||||
|
||||
if (item.GLTextureLoaded && f) f->glDeleteTextures(1, &item.GLTexture);
|
||||
if (item.GLTextureLoaded) glDeleteTextures(1, &item.GLTexture);
|
||||
if (item.Bitmap) delete[] item.Bitmap;
|
||||
|
||||
it = ItemQueue.erase(it);
|
||||
}
|
||||
|
||||
if (f) delete Shader;
|
||||
}
|
||||
|
||||
|
||||
@ -329,7 +323,7 @@ void AddMessage(u32 color, const char* text)
|
||||
{
|
||||
if (!Config::ShowOSD) return;
|
||||
|
||||
while (Rendering);
|
||||
Rendering.lock();
|
||||
|
||||
Item item;
|
||||
|
||||
@ -342,27 +336,29 @@ void AddMessage(u32 color, const char* text)
|
||||
item.GLTextureLoaded = false;
|
||||
|
||||
ItemQueue.push_back(item);
|
||||
|
||||
Rendering.unlock();
|
||||
}
|
||||
|
||||
void Update(QOpenGLFunctions_3_2_Core* f)
|
||||
void Update()
|
||||
{
|
||||
if (!Config::ShowOSD)
|
||||
{
|
||||
Rendering = true;
|
||||
Rendering.lock();
|
||||
for (auto it = ItemQueue.begin(); it != ItemQueue.end(); )
|
||||
{
|
||||
Item& item = *it;
|
||||
|
||||
if (item.GLTextureLoaded && f) f->glDeleteTextures(1, &item.GLTexture);
|
||||
if (item.GLTextureLoaded) glDeleteTextures(1, &item.GLTexture);
|
||||
if (item.Bitmap) delete[] item.Bitmap;
|
||||
|
||||
it = ItemQueue.erase(it);
|
||||
}
|
||||
Rendering = false;
|
||||
Rendering.unlock();
|
||||
return;
|
||||
}
|
||||
|
||||
Rendering = true;
|
||||
Rendering.lock();
|
||||
|
||||
Uint32 tick_now = SDL_GetTicks();
|
||||
Uint32 tick_min = tick_now - 2500;
|
||||
@ -373,7 +369,7 @@ void Update(QOpenGLFunctions_3_2_Core* f)
|
||||
|
||||
if (item.Timestamp < tick_min)
|
||||
{
|
||||
if (item.GLTextureLoaded) f->glDeleteTextures(1, &item.GLTexture);
|
||||
if (item.GLTextureLoaded) glDeleteTextures(1, &item.GLTexture);
|
||||
if (item.Bitmap) delete[] item.Bitmap;
|
||||
|
||||
it = ItemQueue.erase(it);
|
||||
@ -388,14 +384,14 @@ void Update(QOpenGLFunctions_3_2_Core* f)
|
||||
it++;
|
||||
}
|
||||
|
||||
Rendering = false;
|
||||
Rendering.unlock();
|
||||
}
|
||||
|
||||
void DrawNative(QPainter& painter)
|
||||
{
|
||||
if (!Config::ShowOSD) return;
|
||||
|
||||
Rendering = true;
|
||||
Rendering.lock();
|
||||
|
||||
u32 y = kOSDMargin;
|
||||
|
||||
@ -417,30 +413,30 @@ void DrawNative(QPainter& painter)
|
||||
it++;
|
||||
}
|
||||
|
||||
Rendering = false;
|
||||
Rendering.unlock();
|
||||
}
|
||||
|
||||
void DrawGL(QOpenGLFunctions_3_2_Core* f, float w, float h)
|
||||
void DrawGL(float w, float h)
|
||||
{
|
||||
if (!Config::ShowOSD) return;
|
||||
if (!mainWindow || !mainWindow->panel) return;
|
||||
|
||||
Rendering = true;
|
||||
Rendering.lock();
|
||||
|
||||
u32 y = kOSDMargin;
|
||||
|
||||
Shader->bind();
|
||||
glUseProgram(Shader[2]);
|
||||
|
||||
f->glUniform2f(uScreenSize, w, h);
|
||||
f->glUniform1f(uScaleFactor, mainWindow->devicePixelRatioF());
|
||||
glUniform2f(uScreenSize, w, h);
|
||||
glUniform1f(uScaleFactor, mainWindow->devicePixelRatioF());
|
||||
|
||||
f->glBindBuffer(GL_ARRAY_BUFFER, OSDVertexBuffer);
|
||||
f->glBindVertexArray(OSDVertexArray);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, OSDVertexBuffer);
|
||||
glBindVertexArray(OSDVertexArray);
|
||||
|
||||
f->glActiveTexture(GL_TEXTURE0);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
|
||||
f->glEnable(GL_BLEND);
|
||||
f->glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
for (auto it = ItemQueue.begin(); it != ItemQueue.end(); )
|
||||
{
|
||||
@ -448,30 +444,30 @@ void DrawGL(QOpenGLFunctions_3_2_Core* f, float w, float h)
|
||||
|
||||
if (!item.GLTextureLoaded)
|
||||
{
|
||||
f->glGenTextures(1, &item.GLTexture);
|
||||
f->glBindTexture(GL_TEXTURE_2D, item.GLTexture);
|
||||
f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
f->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, item.Width, item.Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, item.Bitmap);
|
||||
glGenTextures(1, &item.GLTexture);
|
||||
glBindTexture(GL_TEXTURE_2D, item.GLTexture);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, item.Width, item.Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, item.Bitmap);
|
||||
|
||||
item.GLTextureLoaded = true;
|
||||
}
|
||||
|
||||
f->glBindTexture(GL_TEXTURE_2D, item.GLTexture);
|
||||
f->glUniform2i(uOSDPos, kOSDMargin, y);
|
||||
f->glUniform2i(uOSDSize, item.Width, item.Height);
|
||||
f->glDrawArrays(GL_TRIANGLES, 0, 2*3);
|
||||
glBindTexture(GL_TEXTURE_2D, item.GLTexture);
|
||||
glUniform2i(uOSDPos, kOSDMargin, y);
|
||||
glUniform2i(uOSDSize, item.Width, item.Height);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 2*3);
|
||||
|
||||
y += item.Height;
|
||||
it++;
|
||||
}
|
||||
|
||||
f->glDisable(GL_BLEND);
|
||||
Shader->release();
|
||||
glDisable(GL_BLEND);
|
||||
glUseProgram(0);
|
||||
|
||||
Rendering = false;
|
||||
Rendering.unlock();
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user