2020-04-22 11:13:51 +00:00
|
|
|
#include "openglhostdisplay.h"
|
2020-01-10 03:31:12 +00:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/log.h"
|
2020-01-02 07:45:25 +00:00
|
|
|
#include "imgui.h"
|
2020-04-22 11:13:51 +00:00
|
|
|
#include "qtdisplaywidget.h"
|
2020-01-02 06:13:03 +00:00
|
|
|
#include "qthostinterface.h"
|
2020-05-17 11:05:35 +00:00
|
|
|
#include <QtCore/QDebug>
|
2020-04-23 03:04:40 +00:00
|
|
|
#include <QtGui/QGuiApplication>
|
2020-01-02 06:13:03 +00:00
|
|
|
#include <QtGui/QKeyEvent>
|
2020-03-12 03:53:51 +00:00
|
|
|
#include <QtGui/QWindow>
|
2019-12-31 06:17:17 +00:00
|
|
|
#include <array>
|
2020-01-02 07:45:25 +00:00
|
|
|
#include <imgui_impl_opengl3.h>
|
2020-05-07 12:49:04 +00:00
|
|
|
#if !defined(WIN32) && !defined(APPLE)
|
|
|
|
#include <qpa/qplatformnativeinterface.h>
|
|
|
|
#endif
|
2019-12-31 06:17:17 +00:00
|
|
|
#include <tuple>
|
2020-04-22 11:13:51 +00:00
|
|
|
Log_SetChannel(OpenGLHostDisplay);
|
2019-12-31 06:17:17 +00:00
|
|
|
|
2020-03-12 03:53:51 +00:00
|
|
|
class OpenGLDisplayWidgetTexture : public HostDisplayTexture
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-03-12 03:53:51 +00:00
|
|
|
OpenGLDisplayWidgetTexture(GLuint id, u32 width, u32 height) : m_id(id), m_width(width), m_height(height) {}
|
|
|
|
~OpenGLDisplayWidgetTexture() override { glDeleteTextures(1, &m_id); }
|
2019-12-31 06:17:17 +00:00
|
|
|
|
|
|
|
void* GetHandle() const override { return reinterpret_cast<void*>(static_cast<uintptr_t>(m_id)); }
|
|
|
|
u32 GetWidth() const override { return m_width; }
|
|
|
|
u32 GetHeight() const override { return m_height; }
|
|
|
|
|
|
|
|
GLuint GetGLID() const { return m_id; }
|
|
|
|
|
2020-03-12 03:53:51 +00:00
|
|
|
static std::unique_ptr<OpenGLDisplayWidgetTexture> Create(u32 width, u32 height, const void* initial_data,
|
|
|
|
u32 initial_data_stride)
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
|
|
|
GLuint id;
|
|
|
|
glGenTextures(1, &id);
|
|
|
|
|
|
|
|
GLint old_texture_binding = 0;
|
|
|
|
glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_texture_binding);
|
|
|
|
|
|
|
|
// TODO: Set pack width
|
|
|
|
Assert(!initial_data || initial_data_stride == (width * sizeof(u32)));
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, id);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, initial_data);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, id);
|
2020-03-12 03:53:51 +00:00
|
|
|
return std::make_unique<OpenGLDisplayWidgetTexture>(id, width, height);
|
2019-12-31 06:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
GLuint m_id;
|
|
|
|
u32 m_width;
|
|
|
|
u32 m_height;
|
|
|
|
};
|
|
|
|
|
2020-04-22 11:13:51 +00:00
|
|
|
OpenGLHostDisplay::OpenGLHostDisplay(QtHostInterface* host_interface) : QtHostDisplay(host_interface) {}
|
|
|
|
|
|
|
|
OpenGLHostDisplay::~OpenGLHostDisplay() = default;
|
|
|
|
|
|
|
|
QtDisplayWidget* OpenGLHostDisplay::createWidget(QWidget* parent)
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
2020-04-22 11:13:51 +00:00
|
|
|
QtDisplayWidget* widget = QtHostDisplay::createWidget(parent);
|
|
|
|
|
|
|
|
QWindow* native_window = widget->windowHandle();
|
2020-03-12 03:53:51 +00:00
|
|
|
Assert(native_window);
|
|
|
|
native_window->setSurfaceType(QWindow::OpenGLSurface);
|
2019-12-31 06:17:17 +00:00
|
|
|
|
2020-04-22 11:13:51 +00:00
|
|
|
return widget;
|
2020-01-02 07:45:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 11:13:51 +00:00
|
|
|
HostDisplay::RenderAPI OpenGLHostDisplay::GetRenderAPI() const
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
2020-05-07 12:49:04 +00:00
|
|
|
return m_gl_context->IsGLES() ? HostDisplay::RenderAPI::OpenGLES : HostDisplay::RenderAPI::OpenGL;
|
2019-12-31 06:17:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 11:13:51 +00:00
|
|
|
void* OpenGLHostDisplay::GetRenderDevice() const
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-04-22 11:13:51 +00:00
|
|
|
void* OpenGLHostDisplay::GetRenderContext() const
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
2020-01-02 07:45:25 +00:00
|
|
|
return m_gl_context.get();
|
2019-12-31 06:17:17 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 12:49:04 +00:00
|
|
|
void OpenGLHostDisplay::WindowResized(s32 new_window_width, s32 new_window_height)
|
|
|
|
{
|
|
|
|
QtHostDisplay::WindowResized(new_window_width, new_window_height);
|
|
|
|
m_gl_context->ResizeSurface(static_cast<u32>(new_window_width), static_cast<u32>(new_window_height));
|
|
|
|
}
|
|
|
|
|
2020-04-22 11:13:51 +00:00
|
|
|
std::unique_ptr<HostDisplayTexture> OpenGLHostDisplay::CreateTexture(u32 width, u32 height, const void* initial_data,
|
|
|
|
u32 initial_data_stride, bool dynamic)
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
2020-03-12 03:53:51 +00:00
|
|
|
return OpenGLDisplayWidgetTexture::Create(width, height, initial_data, initial_data_stride);
|
2019-12-31 06:17:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 11:13:51 +00:00
|
|
|
void OpenGLHostDisplay::UpdateTexture(HostDisplayTexture* texture, u32 x, u32 y, u32 width, u32 height,
|
|
|
|
const void* texture_data, u32 texture_data_stride)
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
2020-03-12 03:53:51 +00:00
|
|
|
OpenGLDisplayWidgetTexture* tex = static_cast<OpenGLDisplayWidgetTexture*>(texture);
|
2020-04-11 08:54:09 +00:00
|
|
|
Assert((texture_data_stride % sizeof(u32)) == 0);
|
2019-12-31 06:17:17 +00:00
|
|
|
|
2020-04-11 08:54:09 +00:00
|
|
|
GLint old_texture_binding = 0, old_alignment = 0, old_row_length = 0;
|
2019-12-31 06:17:17 +00:00
|
|
|
glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_texture_binding);
|
2020-04-11 08:54:09 +00:00
|
|
|
glGetIntegerv(GL_UNPACK_ALIGNMENT, &old_alignment);
|
|
|
|
glGetIntegerv(GL_UNPACK_ROW_LENGTH, &old_row_length);
|
2019-12-31 06:17:17 +00:00
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, tex->GetGLID());
|
2020-04-11 08:54:09 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, texture_data_stride / sizeof(u32));
|
|
|
|
|
2020-03-12 03:53:51 +00:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, texture_data);
|
2019-12-31 06:17:17 +00:00
|
|
|
|
2020-04-11 08:54:09 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, old_alignment);
|
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, old_row_length);
|
2019-12-31 06:17:17 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, old_texture_binding);
|
|
|
|
}
|
|
|
|
|
2020-04-22 11:13:51 +00:00
|
|
|
bool OpenGLHostDisplay::DownloadTexture(const void* texture_handle, u32 x, u32 y, u32 width, u32 height, void* out_data,
|
|
|
|
u32 out_data_stride)
|
2020-03-15 14:03:49 +00:00
|
|
|
{
|
|
|
|
GLint old_alignment = 0, old_row_length = 0;
|
|
|
|
glGetIntegerv(GL_PACK_ALIGNMENT, &old_alignment);
|
|
|
|
glGetIntegerv(GL_PACK_ROW_LENGTH, &old_row_length);
|
|
|
|
glPixelStorei(GL_PACK_ALIGNMENT, sizeof(u32));
|
|
|
|
glPixelStorei(GL_PACK_ROW_LENGTH, out_data_stride / sizeof(u32));
|
|
|
|
|
|
|
|
const GLuint texture = static_cast<GLuint>(reinterpret_cast<uintptr_t>(texture_handle));
|
|
|
|
GL::Texture::GetTextureSubImage(texture, 0, x, y, 0, width, height, 1, GL_RGBA, GL_UNSIGNED_BYTE,
|
|
|
|
height * out_data_stride, out_data);
|
|
|
|
|
|
|
|
glPixelStorei(GL_PACK_ALIGNMENT, old_alignment);
|
|
|
|
glPixelStorei(GL_PACK_ROW_LENGTH, old_row_length);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-22 11:13:51 +00:00
|
|
|
void OpenGLHostDisplay::SetVSync(bool enabled)
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
|
|
|
// Window framebuffer has to be bound to call SetSwapInterval.
|
|
|
|
GLint current_fbo = 0;
|
|
|
|
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, ¤t_fbo);
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
2020-05-07 12:49:04 +00:00
|
|
|
m_gl_context->SetSwapInterval(enabled ? 1 : 0);
|
2019-12-31 06:17:17 +00:00
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, current_fbo);
|
|
|
|
}
|
|
|
|
|
2020-04-22 11:13:51 +00:00
|
|
|
const char* OpenGLHostDisplay::GetGLSLVersionString() const
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
2020-05-07 12:49:04 +00:00
|
|
|
if (m_gl_context->IsGLES())
|
2020-02-15 12:11:51 +00:00
|
|
|
{
|
|
|
|
if (GLAD_GL_ES_VERSION_3_0)
|
|
|
|
return "#version 300 es";
|
|
|
|
else
|
|
|
|
return "#version 100";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (GLAD_GL_VERSION_3_3)
|
|
|
|
return "#version 330";
|
|
|
|
else
|
|
|
|
return "#version 130";
|
|
|
|
}
|
2019-12-31 06:17:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 11:13:51 +00:00
|
|
|
std::string OpenGLHostDisplay::GetGLSLVersionHeader() const
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
|
|
|
std::string header = GetGLSLVersionString();
|
|
|
|
header += "\n\n";
|
2020-05-07 12:49:04 +00:00
|
|
|
if (m_gl_context->IsGLES())
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
|
|
|
header += "precision highp float;\n";
|
|
|
|
header += "precision highp int;\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void APIENTRY GLDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length,
|
|
|
|
const GLchar* message, const void* userParam)
|
|
|
|
{
|
|
|
|
switch (severity)
|
|
|
|
{
|
|
|
|
case GL_DEBUG_SEVERITY_HIGH_KHR:
|
|
|
|
Log_ErrorPrintf(message);
|
|
|
|
break;
|
|
|
|
case GL_DEBUG_SEVERITY_MEDIUM_KHR:
|
|
|
|
Log_WarningPrint(message);
|
|
|
|
break;
|
|
|
|
case GL_DEBUG_SEVERITY_LOW_KHR:
|
|
|
|
Log_InfoPrintf(message);
|
|
|
|
break;
|
|
|
|
case GL_DEBUG_SEVERITY_NOTIFICATION:
|
|
|
|
// Log_DebugPrint(message);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 11:13:51 +00:00
|
|
|
bool OpenGLHostDisplay::hasDeviceContext() const
|
2020-02-15 15:14:28 +00:00
|
|
|
{
|
|
|
|
return static_cast<bool>(m_gl_context);
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:49:04 +00:00
|
|
|
WindowInfo OpenGLHostDisplay::getWindowInfo() const
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
2020-05-07 12:49:04 +00:00
|
|
|
WindowInfo wi;
|
|
|
|
|
|
|
|
// Windows and Apple are easy here since there's no display connection.
|
|
|
|
#if defined(WIN32)
|
|
|
|
wi.type = WindowInfo::Type::Win32;
|
|
|
|
wi.window_handle = reinterpret_cast<void*>(m_widget->winId());
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
wi.type = WindowInfo::Type::MacOS;
|
|
|
|
wi.window_handle = reinterpret_cast<void*>(m_widget->winId());
|
|
|
|
#else
|
|
|
|
QPlatformNativeInterface* pni = QGuiApplication::platformNativeInterface();
|
|
|
|
const QString platform_name = QGuiApplication::platformName();
|
|
|
|
if (platform_name == QStringLiteral("xcb"))
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
2020-05-07 12:49:04 +00:00
|
|
|
wi.type = WindowInfo::Type::X11;
|
|
|
|
wi.display_connection = pni->nativeResourceForWindow("display", m_widget->windowHandle());
|
|
|
|
wi.window_handle = reinterpret_cast<void*>(m_widget->winId());
|
2019-12-31 06:17:17 +00:00
|
|
|
}
|
2020-05-07 12:49:04 +00:00
|
|
|
else if (platform_name == QStringLiteral("wayland"))
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
2020-05-07 12:49:04 +00:00
|
|
|
wi.type = WindowInfo::Type::Wayland;
|
|
|
|
wi.display_connection = pni->nativeResourceForWindow("display", m_widget->windowHandle());
|
|
|
|
wi.window_handle = pni->nativeResourceForWindow("surface", m_widget->windowHandle());
|
2019-12-31 06:17:17 +00:00
|
|
|
}
|
2020-05-07 12:49:04 +00:00
|
|
|
else
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
2020-05-07 12:49:04 +00:00
|
|
|
qCritical() << "Unknown PNI platform " << platform_name;
|
|
|
|
return wi;
|
2019-12-31 06:17:17 +00:00
|
|
|
}
|
2020-05-07 12:49:04 +00:00
|
|
|
#endif
|
2019-12-31 06:17:17 +00:00
|
|
|
|
2020-05-07 12:49:04 +00:00
|
|
|
wi.surface_width = m_widget->width();
|
|
|
|
wi.surface_height = m_widget->height();
|
|
|
|
wi.surface_format = WindowInfo::SurfaceFormat::RGB8;
|
2020-02-06 11:55:11 +00:00
|
|
|
|
2020-05-07 12:49:04 +00:00
|
|
|
return wi;
|
2019-12-31 06:17:17 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 12:49:04 +00:00
|
|
|
bool OpenGLHostDisplay::createDeviceContext(bool debug_device)
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
2020-05-07 12:49:04 +00:00
|
|
|
m_gl_context = GL::Context::Create(getWindowInfo());
|
|
|
|
if (!m_gl_context)
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
2020-05-07 12:49:04 +00:00
|
|
|
Log_ErrorPrintf("Failed to create any GL context");
|
2019-12-31 06:17:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:49:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OpenGLHostDisplay::initializeDeviceContext(bool debug_device)
|
|
|
|
{
|
2020-01-19 04:53:49 +00:00
|
|
|
if (debug_device && GLAD_GL_KHR_debug)
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
|
|
|
glad_glDebugMessageCallbackKHR(GLDebugCallback, nullptr);
|
|
|
|
glEnable(GL_DEBUG_OUTPUT);
|
|
|
|
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
|
|
|
|
}
|
|
|
|
|
2020-04-22 11:13:51 +00:00
|
|
|
if (!QtHostDisplay::initializeDeviceContext(debug_device))
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
2020-05-07 12:49:04 +00:00
|
|
|
m_gl_context->DoneCurrent();
|
2019-12-31 06:17:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:49:04 +00:00
|
|
|
bool OpenGLHostDisplay::activateDeviceContext()
|
2020-04-22 11:13:51 +00:00
|
|
|
{
|
2020-05-07 12:49:04 +00:00
|
|
|
if (!m_gl_context->MakeCurrent())
|
2020-04-22 11:13:51 +00:00
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Failed to make GL context current");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:49:04 +00:00
|
|
|
void OpenGLHostDisplay::deactivateDeviceContext()
|
2020-04-22 11:13:51 +00:00
|
|
|
{
|
2020-05-07 12:49:04 +00:00
|
|
|
m_gl_context->DoneCurrent();
|
2020-04-22 11:13:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLHostDisplay::destroyDeviceContext()
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
2020-04-22 11:13:51 +00:00
|
|
|
QtHostDisplay::destroyDeviceContext();
|
2020-05-07 12:49:04 +00:00
|
|
|
m_gl_context->DoneCurrent();
|
2020-01-02 07:45:25 +00:00
|
|
|
m_gl_context.reset();
|
2019-12-31 06:17:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 11:13:51 +00:00
|
|
|
bool OpenGLHostDisplay::createSurface()
|
|
|
|
{
|
|
|
|
m_window_width = m_widget->scaledWindowWidth();
|
|
|
|
m_window_height = m_widget->scaledWindowHeight();
|
|
|
|
emit m_widget->windowResizedEvent(m_window_width, m_window_height);
|
2020-05-07 12:49:04 +00:00
|
|
|
|
|
|
|
if (m_gl_context)
|
|
|
|
m_gl_context->ChangeSurface(getWindowInfo());
|
|
|
|
|
2020-04-22 11:13:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLHostDisplay::destroySurface() {}
|
|
|
|
|
|
|
|
bool OpenGLHostDisplay::createImGuiContext()
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
2020-04-22 11:13:51 +00:00
|
|
|
if (!QtHostDisplay::createImGuiContext())
|
2020-01-02 07:45:25 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!ImGui_ImplOpenGL3_Init(GetGLSLVersionString()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ImGui_ImplOpenGL3_NewFrame();
|
2020-01-02 09:14:16 +00:00
|
|
|
ImGui::NewFrame();
|
2019-12-31 06:17:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-22 11:13:51 +00:00
|
|
|
void OpenGLHostDisplay::destroyImGuiContext()
|
2020-01-02 07:45:25 +00:00
|
|
|
{
|
|
|
|
ImGui_ImplOpenGL3_Shutdown();
|
|
|
|
|
2020-04-22 11:13:51 +00:00
|
|
|
QtHostDisplay::destroyImGuiContext();
|
2020-01-02 07:45:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 11:13:51 +00:00
|
|
|
bool OpenGLHostDisplay::createDeviceResources()
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
|
|
|
static constexpr char fullscreen_quad_vertex_shader[] = R"(
|
|
|
|
uniform vec4 u_src_rect;
|
|
|
|
out vec2 v_tex0;
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
|
|
|
vec2 pos = vec2(float((gl_VertexID << 1) & 2), float(gl_VertexID & 2));
|
|
|
|
v_tex0 = u_src_rect.xy + pos * u_src_rect.zw;
|
|
|
|
gl_Position = vec4(pos * vec2(2.0f, -2.0f) + vec2(-1.0f, 1.0f), 0.0f, 1.0f);
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
|
|
|
|
static constexpr char display_fragment_shader[] = R"(
|
|
|
|
uniform sampler2D samp0;
|
|
|
|
|
|
|
|
in vec2 v_tex0;
|
|
|
|
out vec4 o_col0;
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
2020-05-02 16:59:00 +00:00
|
|
|
o_col0 = vec4(texture(samp0, v_tex0).rgb, 1.0);
|
2019-12-31 06:17:17 +00:00
|
|
|
}
|
|
|
|
)";
|
|
|
|
|
2020-04-16 10:41:27 +00:00
|
|
|
if (!m_display_program.Compile(GetGLSLVersionHeader() + fullscreen_quad_vertex_shader, {},
|
2019-12-31 06:17:17 +00:00
|
|
|
GetGLSLVersionHeader() + display_fragment_shader))
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Failed to compile display shaders");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:49:04 +00:00
|
|
|
if (!m_gl_context->IsGLES())
|
2019-12-31 06:17:17 +00:00
|
|
|
m_display_program.BindFragData(0, "o_col0");
|
|
|
|
|
|
|
|
if (!m_display_program.Link())
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Failed to link display program");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_display_program.Bind();
|
|
|
|
m_display_program.RegisterUniform("u_src_rect");
|
|
|
|
m_display_program.RegisterUniform("samp0");
|
|
|
|
m_display_program.Uniform1i(1, 0);
|
|
|
|
|
|
|
|
glGenVertexArrays(1, &m_display_vao);
|
|
|
|
|
|
|
|
// samplers
|
|
|
|
glGenSamplers(1, &m_display_nearest_sampler);
|
|
|
|
glSamplerParameteri(m_display_nearest_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
glSamplerParameteri(m_display_nearest_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
glGenSamplers(1, &m_display_linear_sampler);
|
|
|
|
glSamplerParameteri(m_display_linear_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glSamplerParameteri(m_display_linear_sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-22 11:13:51 +00:00
|
|
|
void OpenGLHostDisplay::destroyDeviceResources()
|
2020-01-02 07:45:25 +00:00
|
|
|
{
|
2020-04-22 11:13:51 +00:00
|
|
|
QtHostDisplay::destroyDeviceResources();
|
2020-01-02 07:45:25 +00:00
|
|
|
|
|
|
|
if (m_display_vao != 0)
|
|
|
|
glDeleteVertexArrays(1, &m_display_vao);
|
|
|
|
if (m_display_linear_sampler != 0)
|
|
|
|
glDeleteSamplers(1, &m_display_linear_sampler);
|
|
|
|
if (m_display_nearest_sampler != 0)
|
|
|
|
glDeleteSamplers(1, &m_display_nearest_sampler);
|
|
|
|
|
|
|
|
m_display_program.Destroy();
|
|
|
|
}
|
|
|
|
|
2020-04-22 11:13:51 +00:00
|
|
|
void OpenGLHostDisplay::Render()
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
2020-05-02 16:59:00 +00:00
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
2019-12-31 06:17:17 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
2020-01-02 07:45:25 +00:00
|
|
|
renderDisplay();
|
2019-12-31 06:17:17 +00:00
|
|
|
|
2020-01-02 09:14:16 +00:00
|
|
|
ImGui::Render();
|
|
|
|
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
2019-12-31 06:17:17 +00:00
|
|
|
|
2020-05-07 12:49:04 +00:00
|
|
|
m_gl_context->SwapBuffers();
|
2019-12-31 06:17:17 +00:00
|
|
|
|
2020-01-02 09:14:16 +00:00
|
|
|
ImGui::NewFrame();
|
2020-01-02 07:45:25 +00:00
|
|
|
ImGui_ImplOpenGL3_NewFrame();
|
2019-12-31 06:17:17 +00:00
|
|
|
|
|
|
|
GL::Program::ResetLastProgram();
|
|
|
|
}
|
|
|
|
|
2020-04-22 11:13:51 +00:00
|
|
|
void OpenGLHostDisplay::renderDisplay()
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
2020-01-24 04:51:36 +00:00
|
|
|
if (!m_display_texture_handle)
|
2019-12-31 06:17:17 +00:00
|
|
|
return;
|
|
|
|
|
2020-04-22 11:13:51 +00:00
|
|
|
const auto [vp_left, vp_top, vp_width, vp_height] =
|
|
|
|
CalculateDrawRect(m_window_width, m_window_height, m_display_top_margin);
|
2019-12-31 06:17:17 +00:00
|
|
|
|
2020-01-07 04:27:48 +00:00
|
|
|
glViewport(vp_left, m_window_height - (m_display_top_margin + vp_top) - vp_height, vp_width, vp_height);
|
2019-12-31 06:17:17 +00:00
|
|
|
glDisable(GL_BLEND);
|
|
|
|
glDisable(GL_CULL_FACE);
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
glDepthMask(GL_FALSE);
|
|
|
|
m_display_program.Bind();
|
2020-02-12 15:19:46 +00:00
|
|
|
m_display_program.Uniform4f(
|
2020-05-17 11:05:35 +00:00
|
|
|
0, static_cast<float>(m_display_texture_view_x) / static_cast<float>(m_display_texture_width),
|
|
|
|
static_cast<float>(m_display_texture_view_y) / static_cast<float>(m_display_texture_height),
|
2020-02-28 12:42:56 +00:00
|
|
|
(static_cast<float>(m_display_texture_view_width) - 0.5f) / static_cast<float>(m_display_texture_width),
|
2020-02-28 14:18:24 +00:00
|
|
|
(static_cast<float>(m_display_texture_view_height) + 0.5f) / static_cast<float>(m_display_texture_height));
|
2020-01-24 04:51:36 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, static_cast<GLuint>(reinterpret_cast<uintptr_t>(m_display_texture_handle)));
|
2019-12-31 06:17:17 +00:00
|
|
|
glBindSampler(0, m_display_linear_filtering ? m_display_linear_sampler : m_display_nearest_sampler);
|
|
|
|
glBindVertexArray(m_display_vao);
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
|
|
|
glBindSampler(0, 0);
|
|
|
|
}
|