SWOGLWindow.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2015 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "VideoBackends/Software/SWOGLWindow.h"
  4. #include <memory>
  5. #include "Common/GL/GLContext.h"
  6. #include "Common/GL/GLUtil.h"
  7. #include "Common/Logging/Log.h"
  8. #include "Common/MsgHandler.h"
  9. #include "VideoBackends/Software/SWTexture.h"
  10. SWOGLWindow::SWOGLWindow() = default;
  11. SWOGLWindow::~SWOGLWindow() = default;
  12. std::unique_ptr<SWOGLWindow> SWOGLWindow::Create(const WindowSystemInfo& wsi)
  13. {
  14. std::unique_ptr<SWOGLWindow> window = std::unique_ptr<SWOGLWindow>(new SWOGLWindow());
  15. if (!window->Initialize(wsi))
  16. {
  17. PanicAlertFmt("Failed to create OpenGL window");
  18. return nullptr;
  19. }
  20. return window;
  21. }
  22. bool SWOGLWindow::IsHeadless() const
  23. {
  24. return m_gl_context->IsHeadless();
  25. }
  26. bool SWOGLWindow::Initialize(const WindowSystemInfo& wsi)
  27. {
  28. m_gl_context = GLContext::Create(wsi);
  29. if (!m_gl_context)
  30. return false;
  31. // Init extension support.
  32. if (!GLExtensions::Init(m_gl_context.get()))
  33. {
  34. ERROR_LOG_FMT(VIDEO, "GLExtensions::Init failed!Does your video card support OpenGL 2.0?");
  35. return false;
  36. }
  37. else if (GLExtensions::Version() < 310)
  38. {
  39. ERROR_LOG_FMT(VIDEO, "OpenGL Version {} detected, but at least 3.1 is required.",
  40. GLExtensions::Version());
  41. return false;
  42. }
  43. std::string frag_shader = "in vec2 TexCoord;\n"
  44. "out vec4 ColorOut;\n"
  45. "uniform sampler2D samp;\n"
  46. "void main() {\n"
  47. " ColorOut = texture(samp, TexCoord);\n"
  48. "}\n";
  49. std::string vertex_shader = "out vec2 TexCoord;\n"
  50. "void main() {\n"
  51. " vec2 rawpos = vec2(gl_VertexID & 1, (gl_VertexID & 2) >> 1);\n"
  52. " gl_Position = vec4(rawpos * 2.0 - 1.0, 0.0, 1.0);\n"
  53. " TexCoord = vec2(rawpos.x, -rawpos.y);\n"
  54. "}\n";
  55. std::string header = m_gl_context->IsGLES() ? "#version 300 es\n"
  56. "precision highp float;\n" :
  57. "#version 140\n";
  58. m_image_program = GLUtil::CompileProgram(header + vertex_shader, header + frag_shader);
  59. glUseProgram(m_image_program);
  60. glUniform1i(glGetUniformLocation(m_image_program, "samp"), 0);
  61. glGenTextures(1, &m_image_texture);
  62. glBindTexture(GL_TEXTURE_2D, m_image_texture);
  63. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  64. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  65. glGenVertexArrays(1, &m_image_vao);
  66. return true;
  67. }
  68. void SWOGLWindow::ShowImage(const AbstractTexture* image,
  69. const MathUtil::Rectangle<int>& xfb_region)
  70. {
  71. const SW::SWTexture* sw_image = static_cast<const SW::SWTexture*>(image);
  72. m_gl_context->Update(); // just updates the render window position and the backbuffer size
  73. GLsizei glWidth = (GLsizei)m_gl_context->GetBackBufferWidth();
  74. GLsizei glHeight = (GLsizei)m_gl_context->GetBackBufferHeight();
  75. glViewport(0, 0, glWidth, glHeight);
  76. glActiveTexture(GL_TEXTURE9);
  77. glBindTexture(GL_TEXTURE_2D, m_image_texture);
  78. // TODO: Apply xfb_region
  79. glPixelStorei(GL_UNPACK_ALIGNMENT, 4); // 4-byte pixel alignment
  80. glPixelStorei(GL_UNPACK_ROW_LENGTH, sw_image->GetConfig().width);
  81. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, static_cast<GLsizei>(sw_image->GetConfig().width),
  82. static_cast<GLsizei>(sw_image->GetConfig().height), 0, GL_RGBA, GL_UNSIGNED_BYTE,
  83. sw_image->GetData(0, 0));
  84. glUseProgram(m_image_program);
  85. glBindVertexArray(m_image_vao);
  86. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  87. m_gl_context->Swap();
  88. }