Debian Codegraph @

Source/Core/VideoBackends/Software/SWOGLWindow.cpp

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