From 61c3a0d9e45a587e8fb5e5770eb4db05386bed5f Mon Sep 17 00:00:00 2001 From: degasus Date: Sat, 26 Sep 2015 10:07:48 +0200 Subject: VideoSW: Split up OGL window handling This removes OSD support for video software, but it was already broken before. This commit does not try to fix coding style issues, the rewrite of this presentation API is splitted up. --- Source/Core/VideoBackends/Software/SWOGLWindow.cpp | 141 +++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 Source/Core/VideoBackends/Software/SWOGLWindow.cpp (limited to 'Source/Core/VideoBackends/Software/SWOGLWindow.cpp') diff --git a/Source/Core/VideoBackends/Software/SWOGLWindow.cpp b/Source/Core/VideoBackends/Software/SWOGLWindow.cpp new file mode 100644 index 0000000000..c3a5d55941 --- /dev/null +++ b/Source/Core/VideoBackends/Software/SWOGLWindow.cpp @@ -0,0 +1,141 @@ +// Copyright 2015 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include "Common/GL/GLInterfaceBase.h" +#include "Common/GL/GLUtil.h" +#include "Common/Logging/Log.h" + +#include "VideoBackends/Software/SWOGLWindow.h" + +std::unique_ptr SWOGLWindow::s_instance; + +void SWOGLWindow::Init(void *window_handle) +{ + InitInterface(); + GLInterface->SetMode(GLInterfaceMode::MODE_DETECT); + if (!GLInterface->Create(window_handle, false)) + { + INFO_LOG(VIDEO, "GLInterface::Create failed."); + } + + s_instance.reset(new SWOGLWindow()); +} + +void SWOGLWindow::Shutdown() +{ + GLInterface->Shutdown(); + delete GLInterface; + GLInterface = nullptr; + + SWOGLWindow::s_instance.release(); +} + +void SWOGLWindow::Prepare() +{ + if (m_init) return; + m_init = true; + + // Init extension support. + if (!GLExtensions::Init()) + { + ERROR_LOG(VIDEO, "GLExtensions::Init failed!Does your video card support OpenGL 2.0?"); + return; + } + + static const char *fragShaderText = + "#ifdef GL_ES\n" + "precision highp float;\n" + "#endif\n" + "varying vec2 TexCoordOut;\n" + "uniform sampler2D Texture;\n" + "void main() {\n" + " gl_FragColor = texture2D(Texture, TexCoordOut);\n" + "}\n"; + static const char *vertShaderText = + "#ifdef GL_ES\n" + "precision highp float;\n" + "#endif\n" + "attribute vec4 pos;\n" + "attribute vec2 TexCoordIn;\n " + "varying vec2 TexCoordOut;\n " + "void main() {\n" + " gl_Position = pos;\n" + " TexCoordOut = TexCoordIn;\n" + "}\n"; + + m_image_program = OpenGL_CompileProgram(vertShaderText, fragShaderText); + + glUseProgram(m_image_program); + + m_uni_tex = glGetUniformLocation(m_image_program, "Texture"); + m_attr_pos = glGetAttribLocation(m_image_program, "pos"); + m_attr_tex = glGetAttribLocation(m_image_program, "TexCoordIn"); + + glUniform1i(m_uni_tex, 0); + + glGenTextures(1, &m_image_texture); +} + +void SWOGLWindow::PrintText(const std::string& text, int x, int y, u32 color) +{ + TextData data{text, x, y, color}; + m_text.emplace_back(data); +} + +void SWOGLWindow::ShowImage(u8* data, int stride, int width, int height, float aspect) +{ + GLInterface->MakeCurrent(); + GLInterface->Update(); + Prepare(); + + GLsizei glWidth = (GLsizei)GLInterface->GetBackBufferWidth(); + GLsizei glHeight = (GLsizei)GLInterface->GetBackBufferHeight(); + + glViewport(0, 0, glWidth, glHeight); + + glBindTexture(GL_TEXTURE_2D, m_image_texture); + + glPixelStorei(GL_UNPACK_ALIGNMENT, 4); // 4-byte pixel alignment + //glPixelStorei(GL_UNPACK_ROW_LENGTH, stride); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + + glUseProgram(m_image_program); + static const GLfloat verts[4][2] = { + { -1, -1}, // Left top + { -1, 1}, // left bottom + { 1, 1}, // right bottom + { 1, -1} // right top + }; + static const GLfloat texverts[4][2] = { + {0, 1}, + {0, 0}, + {1, 0}, + {1, 1} + }; + + glVertexAttribPointer(m_attr_pos, 2, GL_FLOAT, GL_FALSE, 0, verts); + glVertexAttribPointer(m_attr_tex, 2, GL_FLOAT, GL_FALSE, 0, texverts); + glEnableVertexAttribArray(m_attr_pos); + glEnableVertexAttribArray(m_attr_tex); + glDrawArrays(GL_TRIANGLE_FAN, 0, 4); + glDisableVertexAttribArray(m_attr_pos); + glDisableVertexAttribArray(m_attr_tex); + +// TODO: implement OSD +// for (TextData& text : m_text) +// { +// } + m_text.clear(); + + GLInterface->Swap(); + GLInterface->ClearCurrent(); +} + +int SWOGLWindow::PeekMessages() +{ + return GLInterface->PeekMessages(); +} + -- cgit v1.2.3 From b69bff069055b32fc759d7722a773dc355c6b459 Mon Sep 17 00:00:00 2001 From: degasus Date: Fri, 9 Oct 2015 09:25:09 +0200 Subject: VideoSW: Use OpenGL Core context --- Source/Core/VideoBackends/Software/SWOGLWindow.cpp | 79 ++++++++++------------ 1 file changed, 34 insertions(+), 45 deletions(-) (limited to 'Source/Core/VideoBackends/Software/SWOGLWindow.cpp') diff --git a/Source/Core/VideoBackends/Software/SWOGLWindow.cpp b/Source/Core/VideoBackends/Software/SWOGLWindow.cpp index c3a5d55941..be4ee68e32 100644 --- a/Source/Core/VideoBackends/Software/SWOGLWindow.cpp +++ b/Source/Core/VideoBackends/Software/SWOGLWindow.cpp @@ -14,7 +14,7 @@ void SWOGLWindow::Init(void *window_handle) { InitInterface(); GLInterface->SetMode(GLInterfaceMode::MODE_DETECT); - if (!GLInterface->Create(window_handle, false)) + if (!GLInterface->Create(window_handle)) { INFO_LOG(VIDEO, "GLInterface::Create failed."); } @@ -42,39 +42,47 @@ void SWOGLWindow::Prepare() ERROR_LOG(VIDEO, "GLExtensions::Init failed!Does your video card support OpenGL 2.0?"); return; } + else if (GLExtensions::Version() < 310) + { + ERROR_LOG(VIDEO, "OpenGL Version %d detected, but at least 3.1 is required.", GLExtensions::Version()); + return; + } - static const char *fragShaderText = - "#ifdef GL_ES\n" - "precision highp float;\n" - "#endif\n" - "varying vec2 TexCoordOut;\n" + std::string frag_shader = + "in vec2 TexCoord;\n" + "out vec4 ColorOut;\n" "uniform sampler2D Texture;\n" "void main() {\n" - " gl_FragColor = texture2D(Texture, TexCoordOut);\n" + " ColorOut = texture2D(Texture, TexCoord);\n" "}\n"; - static const char *vertShaderText = - "#ifdef GL_ES\n" - "precision highp float;\n" - "#endif\n" - "attribute vec4 pos;\n" - "attribute vec2 TexCoordIn;\n " - "varying vec2 TexCoordOut;\n " + + std::string vertex_shader = + "out vec2 TexCoord;\n" "void main() {\n" - " gl_Position = pos;\n" - " TexCoordOut = TexCoordIn;\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"; - m_image_program = OpenGL_CompileProgram(vertShaderText, fragShaderText); + std::string header = + GLInterface->GetMode() == GLInterfaceMode::MODE_OPENGL ? + "#version 140\n" + : + "#version 300 es\n" + "precision highp float;\n"; - glUseProgram(m_image_program); + m_image_program = OpenGL_CompileProgram(header + vertex_shader, header + frag_shader); - m_uni_tex = glGetUniformLocation(m_image_program, "Texture"); - m_attr_pos = glGetAttribLocation(m_image_program, "pos"); - m_attr_tex = glGetAttribLocation(m_image_program, "TexCoordIn"); + glUseProgram(m_image_program); - glUniform1i(m_uni_tex, 0); + glUniform1i(glGetUniformLocation(m_image_program, "Texture"), 0); glGenTextures(1, &m_image_texture); + glBindTexture(GL_TEXTURE_2D, m_image_texture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + + glGenVertexArrays(1, &m_image_vao); } void SWOGLWindow::PrintText(const std::string& text, int x, int y, u32 color) @@ -97,32 +105,13 @@ void SWOGLWindow::ShowImage(u8* data, int stride, int width, int height, float a glBindTexture(GL_TEXTURE_2D, m_image_texture); glPixelStorei(GL_UNPACK_ALIGNMENT, 4); // 4-byte pixel alignment - //glPixelStorei(GL_UNPACK_ROW_LENGTH, stride); + glPixelStorei(GL_UNPACK_ROW_LENGTH, stride / 4); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glUseProgram(m_image_program); - static const GLfloat verts[4][2] = { - { -1, -1}, // Left top - { -1, 1}, // left bottom - { 1, 1}, // right bottom - { 1, -1} // right top - }; - static const GLfloat texverts[4][2] = { - {0, 1}, - {0, 0}, - {1, 0}, - {1, 1} - }; - - glVertexAttribPointer(m_attr_pos, 2, GL_FLOAT, GL_FALSE, 0, verts); - glVertexAttribPointer(m_attr_tex, 2, GL_FLOAT, GL_FALSE, 0, texverts); - glEnableVertexAttribArray(m_attr_pos); - glEnableVertexAttribArray(m_attr_tex); - glDrawArrays(GL_TRIANGLE_FAN, 0, 4); - glDisableVertexAttribArray(m_attr_pos); - glDisableVertexAttribArray(m_attr_tex); + + glBindVertexArray(m_image_vao); + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); // TODO: implement OSD // for (TextData& text : m_text) -- cgit v1.2.3