2015-09-26 08:07:48 +00:00
|
|
|
// Copyright 2015 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2015-12-21 02:49:49 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2018-10-03 13:02:45 +00:00
|
|
|
#include "Common/GL/GLContext.h"
|
2015-09-26 08:07:48 +00:00
|
|
|
#include "Common/GL/GLUtil.h"
|
|
|
|
#include "Common/Logging/Log.h"
|
2018-10-03 13:03:26 +00:00
|
|
|
#include "Common/MsgHandler.h"
|
2015-09-26 08:07:48 +00:00
|
|
|
|
|
|
|
#include "VideoBackends/Software/SWOGLWindow.h"
|
2017-08-24 02:46:23 +00:00
|
|
|
#include "VideoBackends/Software/SWTexture.h"
|
2015-09-26 08:07:48 +00:00
|
|
|
|
2018-10-03 13:03:26 +00:00
|
|
|
SWOGLWindow::SWOGLWindow() = default;
|
2018-10-03 13:03:33 +00:00
|
|
|
SWOGLWindow::~SWOGLWindow() = default;
|
2015-09-26 08:07:48 +00:00
|
|
|
|
2018-10-03 13:03:26 +00:00
|
|
|
std::unique_ptr<SWOGLWindow> SWOGLWindow::Create(const WindowSystemInfo& wsi)
|
2015-09-26 08:07:48 +00:00
|
|
|
{
|
2018-10-03 13:03:26 +00:00
|
|
|
std::unique_ptr<SWOGLWindow> window = std::unique_ptr<SWOGLWindow>(new SWOGLWindow());
|
|
|
|
if (!window->Initialize(wsi))
|
|
|
|
{
|
|
|
|
PanicAlert("Failed to create OpenGL window");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-09-26 08:07:48 +00:00
|
|
|
|
2018-10-03 13:03:26 +00:00
|
|
|
return window;
|
2015-09-26 08:07:48 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 13:03:26 +00:00
|
|
|
bool SWOGLWindow::IsHeadless() const
|
2015-09-26 08:07:48 +00:00
|
|
|
{
|
2018-10-03 13:03:26 +00:00
|
|
|
return m_gl_context->IsHeadless();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SWOGLWindow::Initialize(const WindowSystemInfo& wsi)
|
|
|
|
{
|
|
|
|
m_gl_context = GLContext::Create(wsi);
|
|
|
|
if (!m_gl_context)
|
|
|
|
return false;
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Init extension support.
|
2018-10-03 13:03:26 +00:00
|
|
|
if (!GLExtensions::Init(m_gl_context.get()))
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "GLExtensions::Init failed!Does your video card support OpenGL 2.0?");
|
2018-10-03 13:03:26 +00:00
|
|
|
return false;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
else if (GLExtensions::Version() < 310)
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "OpenGL Version %d detected, but at least 3.1 is required.",
|
|
|
|
GLExtensions::Version());
|
2018-10-03 13:03:26 +00:00
|
|
|
return false;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string frag_shader = "in vec2 TexCoord;\n"
|
|
|
|
"out vec4 ColorOut;\n"
|
2017-08-24 02:46:23 +00:00
|
|
|
"uniform sampler2D samp;\n"
|
2016-06-24 08:43:46 +00:00
|
|
|
"void main() {\n"
|
2017-08-24 02:46:23 +00:00
|
|
|
" ColorOut = texture(samp, TexCoord);\n"
|
2016-06-24 08:43:46 +00:00
|
|
|
"}\n";
|
|
|
|
|
|
|
|
std::string vertex_shader = "out vec2 TexCoord;\n"
|
|
|
|
"void main() {\n"
|
|
|
|
" vec2 rawpos = vec2(gl_VertexID & 1, (gl_VertexID & 2) >> 1);\n"
|
|
|
|
" gl_Position = vec4(rawpos * 2.0 - 1.0, 0.0, 1.0);\n"
|
|
|
|
" TexCoord = vec2(rawpos.x, -rawpos.y);\n"
|
|
|
|
"}\n";
|
|
|
|
|
2018-10-03 13:03:26 +00:00
|
|
|
std::string header = m_gl_context->IsGLES() ? "#version 300 es\n"
|
|
|
|
"precision highp float;\n" :
|
|
|
|
"#version 140\n";
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2018-03-10 04:52:48 +00:00
|
|
|
m_image_program = GLUtil::CompileProgram(header + vertex_shader, header + frag_shader);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
glUseProgram(m_image_program);
|
|
|
|
|
2017-05-29 22:02:09 +00:00
|
|
|
glUniform1i(glGetUniformLocation(m_image_program, "samp"), 0);
|
2017-08-24 02:46:23 +00:00
|
|
|
glGenTextures(1, &m_image_texture);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_image_texture);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-08-24 02:46:23 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
glGenVertexArrays(1, &m_image_vao);
|
2018-10-03 13:03:26 +00:00
|
|
|
return true;
|
2015-09-26 08:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SWOGLWindow::PrintText(const std::string& text, int x, int y, u32 color)
|
|
|
|
{
|
2016-09-21 17:06:03 +00:00
|
|
|
m_text.push_back({text, x, y, color});
|
2015-09-26 08:07:48 +00:00
|
|
|
}
|
|
|
|
|
2017-09-29 05:31:08 +00:00
|
|
|
void SWOGLWindow::ShowImage(AbstractTexture* image, const EFBRectangle& xfb_region)
|
2015-09-26 08:07:48 +00:00
|
|
|
{
|
2017-09-03 02:30:34 +00:00
|
|
|
SW::SWTexture* sw_image = static_cast<SW::SWTexture*>(image);
|
2018-10-03 13:03:26 +00:00
|
|
|
m_gl_context->Update(); // just updates the render window position and the backbuffer size
|
2015-09-26 08:07:48 +00:00
|
|
|
|
2018-10-03 13:03:26 +00:00
|
|
|
GLsizei glWidth = (GLsizei)m_gl_context->GetBackBufferWidth();
|
|
|
|
GLsizei glHeight = (GLsizei)m_gl_context->GetBackBufferHeight();
|
2015-09-26 08:07:48 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
glViewport(0, 0, glWidth, glHeight);
|
2015-09-26 08:07:48 +00:00
|
|
|
|
2017-08-24 02:46:23 +00:00
|
|
|
glActiveTexture(GL_TEXTURE9);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_image_texture);
|
2015-09-26 08:07:48 +00:00
|
|
|
|
2017-09-29 05:31:08 +00:00
|
|
|
// TODO: Apply xfb_region
|
|
|
|
|
2017-08-24 02:46:23 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 4); // 4-byte pixel alignment
|
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, sw_image->GetConfig().width);
|
|
|
|
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, static_cast<GLsizei>(sw_image->GetConfig().width),
|
|
|
|
static_cast<GLsizei>(sw_image->GetConfig().height), 0, GL_RGBA, GL_UNSIGNED_BYTE,
|
|
|
|
sw_image->GetData());
|
2015-09-26 08:07:48 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
glUseProgram(m_image_program);
|
2015-10-09 07:25:09 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
glBindVertexArray(m_image_vao);
|
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
2015-09-26 08:07:48 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// TODO: implement OSD
|
|
|
|
// for (TextData& text : m_text)
|
|
|
|
// {
|
|
|
|
// }
|
|
|
|
m_text.clear();
|
2015-09-26 08:07:48 +00:00
|
|
|
|
2018-10-03 13:03:26 +00:00
|
|
|
m_gl_context->Swap();
|
2015-09-26 08:07:48 +00:00
|
|
|
}
|