summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/citra/emu_window/emu_window_glfw.cpp3
-rw-r--r--src/citra_qt/bootmanager.cpp6
-rw-r--r--src/core/hw/gpu.cpp10
-rw-r--r--src/core/hw/gpu.h14
-rw-r--r--src/video_core/renderer_base.h2
-rw-r--r--src/video_core/renderer_opengl/gl_shaders.h46
-rw-r--r--src/video_core/renderer_opengl/renderer_opengl.cpp329
-rw-r--r--src/video_core/renderer_opengl/renderer_opengl.h80
-rw-r--r--src/video_core/video_core.h4
9 files changed, 220 insertions, 274 deletions
diff --git a/src/citra/emu_window/emu_window_glfw.cpp b/src/citra/emu_window/emu_window_glfw.cpp
index 661521eb7..6cdba2b87 100644
--- a/src/citra/emu_window/emu_window_glfw.cpp
+++ b/src/citra/emu_window/emu_window_glfw.cpp
@@ -42,12 +42,9 @@ EmuWindow_GLFW::EmuWindow_GLFW() {
42 } 42 }
43 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 43 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
44 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); 44 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
45
46#if EMU_PLATFORM == PLATFORM_MACOSX
47 // GLFW on OSX requires these window hints to be set to create a 3.2+ GL context. 45 // GLFW on OSX requires these window hints to be set to create a 3.2+ GL context.
48 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 46 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
49 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 47 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
50#endif
51 48
52 m_render_window = glfwCreateWindow(VideoCore::kScreenTopWidth, 49 m_render_window = glfwCreateWindow(VideoCore::kScreenTopWidth,
53 (VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight), 50 (VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight),
diff --git a/src/citra_qt/bootmanager.cpp b/src/citra_qt/bootmanager.cpp
index 5dce9e570..0430aa1ed 100644
--- a/src/citra_qt/bootmanager.cpp
+++ b/src/citra_qt/bootmanager.cpp
@@ -113,10 +113,10 @@ GRenderWindow::GRenderWindow(QWidget* parent) : QWidget(parent), emu_thread(this
113 113
114 // TODO: One of these flags might be interesting: WA_OpaquePaintEvent, WA_NoBackground, WA_DontShowOnScreen, WA_DeleteOnClose 114 // TODO: One of these flags might be interesting: WA_OpaquePaintEvent, WA_NoBackground, WA_DontShowOnScreen, WA_DeleteOnClose
115 QGLFormat fmt; 115 QGLFormat fmt;
116 fmt.setProfile(QGLFormat::CoreProfile);
117 fmt.setVersion(3,2); 116 fmt.setVersion(3,2);
118 fmt.setSampleBuffers(true); 117 fmt.setProfile(QGLFormat::CoreProfile);
119 fmt.setSamples(4); 118 // Requests a forward-compatible context, which is required to get a 3.2+ context on OS X
119 fmt.setOption(QGL::NoDeprecatedFunctions);
120 120
121 child = new GGLWidgetInternal(fmt, this); 121 child = new GGLWidgetInternal(fmt, this);
122 QBoxLayout* layout = new QHBoxLayout(this); 122 QBoxLayout* layout = new QHBoxLayout(this);
diff --git a/src/core/hw/gpu.cpp b/src/core/hw/gpu.cpp
index 9c7192313..33a0e0fe7 100644
--- a/src/core/hw/gpu.cpp
+++ b/src/core/hw/gpu.cpp
@@ -89,7 +89,7 @@ inline void Write(u32 addr, const T data) {
89 } source_color = { 0, 0, 0, 0 }; 89 } source_color = { 0, 0, 0, 0 };
90 90
91 switch (config.input_format) { 91 switch (config.input_format) {
92 case Regs::FramebufferFormat::RGBA8: 92 case Regs::PixelFormat::RGBA8:
93 { 93 {
94 // TODO: Most likely got the component order messed up. 94 // TODO: Most likely got the component order messed up.
95 u8* srcptr = source_pointer + x * 4 + y * config.input_width * 4; 95 u8* srcptr = source_pointer + x * 4 + y * config.input_width * 4;
@@ -106,7 +106,7 @@ inline void Write(u32 addr, const T data) {
106 } 106 }
107 107
108 switch (config.output_format) { 108 switch (config.output_format) {
109 /*case Regs::FramebufferFormat::RGBA8: 109 /*case Regs::PixelFormat::RGBA8:
110 { 110 {
111 // TODO: Untested 111 // TODO: Untested
112 u8* dstptr = (u32*)(dest_pointer + x * 4 + y * config.output_width * 4); 112 u8* dstptr = (u32*)(dest_pointer + x * 4 + y * config.output_width * 4);
@@ -117,7 +117,7 @@ inline void Write(u32 addr, const T data) {
117 break; 117 break;
118 }*/ 118 }*/
119 119
120 case Regs::FramebufferFormat::RGB8: 120 case Regs::PixelFormat::RGB8:
121 { 121 {
122 // TODO: Most likely got the component order messed up. 122 // TODO: Most likely got the component order messed up.
123 u8* dstptr = dest_pointer + x * 3 + y * config.output_width * 3; 123 u8* dstptr = dest_pointer + x * 3 + y * config.output_width * 3;
@@ -236,13 +236,13 @@ void Init() {
236 framebuffer_top.width = 240; 236 framebuffer_top.width = 240;
237 framebuffer_top.height = 400; 237 framebuffer_top.height = 400;
238 framebuffer_top.stride = 3 * 240; 238 framebuffer_top.stride = 3 * 240;
239 framebuffer_top.color_format = Regs::FramebufferFormat::RGB8; 239 framebuffer_top.color_format = Regs::PixelFormat::RGB8;
240 framebuffer_top.active_fb = 0; 240 framebuffer_top.active_fb = 0;
241 241
242 framebuffer_sub.width = 240; 242 framebuffer_sub.width = 240;
243 framebuffer_sub.height = 320; 243 framebuffer_sub.height = 320;
244 framebuffer_sub.stride = 3 * 240; 244 framebuffer_sub.stride = 3 * 240;
245 framebuffer_sub.color_format = Regs::FramebufferFormat::RGB8; 245 framebuffer_sub.color_format = Regs::PixelFormat::RGB8;
246 framebuffer_sub.active_fb = 0; 246 framebuffer_sub.active_fb = 0;
247 247
248 NOTICE_LOG(GPU, "initialized OK"); 248 NOTICE_LOG(GPU, "initialized OK");
diff --git a/src/core/hw/gpu.h b/src/core/hw/gpu.h
index c853429a0..92097d182 100644
--- a/src/core/hw/gpu.h
+++ b/src/core/hw/gpu.h
@@ -56,7 +56,7 @@ struct Regs {
56 "Structure size and register block length don't match") 56 "Structure size and register block length don't match")
57#endif 57#endif
58 58
59 enum class FramebufferFormat : u32 { 59 enum class PixelFormat : u32 {
60 RGBA8 = 0, 60 RGBA8 = 0,
61 RGB8 = 1, 61 RGB8 = 1,
62 RGB565 = 2, 62 RGB565 = 2,
@@ -84,9 +84,7 @@ struct Regs {
84 84
85 INSERT_PADDING_WORDS(0x10b); 85 INSERT_PADDING_WORDS(0x10b);
86 86
87 struct { 87 struct FramebufferConfig {
88 using Format = Regs::FramebufferFormat;
89
90 union { 88 union {
91 u32 size; 89 u32 size;
92 90
@@ -102,7 +100,7 @@ struct Regs {
102 union { 100 union {
103 u32 format; 101 u32 format;
104 102
105 BitField< 0, 3, Format> color_format; 103 BitField< 0, 3, PixelFormat> color_format;
106 }; 104 };
107 105
108 INSERT_PADDING_WORDS(0x1); 106 INSERT_PADDING_WORDS(0x1);
@@ -130,8 +128,6 @@ struct Regs {
130 INSERT_PADDING_WORDS(0x169); 128 INSERT_PADDING_WORDS(0x169);
131 129
132 struct { 130 struct {
133 using Format = Regs::FramebufferFormat;
134
135 u32 input_address; 131 u32 input_address;
136 u32 output_address; 132 u32 output_address;
137 133
@@ -161,8 +157,8 @@ struct Regs {
161 u32 flags; 157 u32 flags;
162 158
163 BitField< 0, 1, u32> flip_data; // flips input data horizontally (TODO) if true 159 BitField< 0, 1, u32> flip_data; // flips input data horizontally (TODO) if true
164 BitField< 8, 3, Format> input_format; 160 BitField< 8, 3, PixelFormat> input_format;
165 BitField<12, 3, Format> output_format; 161 BitField<12, 3, PixelFormat> output_format;
166 BitField<16, 1, u32> output_tiled; // stores output in a tiled format 162 BitField<16, 1, u32> output_tiled; // stores output in a tiled format
167 }; 163 };
168 164
diff --git a/src/video_core/renderer_base.h b/src/video_core/renderer_base.h
index 2650620b4..f1dbc9d17 100644
--- a/src/video_core/renderer_base.h
+++ b/src/video_core/renderer_base.h
@@ -19,7 +19,7 @@ public:
19 RendererBase() : m_current_fps(0), m_current_frame(0) { 19 RendererBase() : m_current_fps(0), m_current_frame(0) {
20 } 20 }
21 21
22 ~RendererBase() { 22 virtual ~RendererBase() {
23 } 23 }
24 24
25 /// Swap buffers (render frame) 25 /// Swap buffers (render frame)
diff --git a/src/video_core/renderer_opengl/gl_shaders.h b/src/video_core/renderer_opengl/gl_shaders.h
index 380648f45..0f88ab802 100644
--- a/src/video_core/renderer_opengl/gl_shaders.h
+++ b/src/video_core/renderer_opengl/gl_shaders.h
@@ -6,34 +6,40 @@
6 6
7namespace GLShaders { 7namespace GLShaders {
8 8
9static const char g_vertex_shader[] = R"( 9const char g_vertex_shader[] = R"(
10#version 150 core 10#version 150 core
11in vec3 position;
12in vec2 texCoord;
13 11
14out vec2 UV; 12in vec2 vert_position;
13in vec2 vert_tex_coord;
14out vec2 frag_tex_coord;
15 15
16mat3 window_scale = mat3( 16// This is a truncated 3x3 matrix for 2D transformations:
17 vec3(1.0, 0.0, 0.0), 17// The upper-left 2x2 submatrix performs scaling/rotation/mirroring.
18 vec3(0.0, 5.0/6.0, 0.0), // TODO(princesspeachum): replace hard-coded aspect with uniform 18// The third column performs translation.
19 vec3(0.0, 0.0, 1.0) 19// The third row could be used for projection, which we don't need in 2D. It hence is assumed to
20 ); 20// implicitly be [0, 0, 1]
21uniform mat3x2 modelview_matrix;
21 22
22void main() { 23void main() {
23 gl_Position.xyz = window_scale * position; 24 // Multiply input position by the rotscale part of the matrix and then manually translate by
24 gl_Position.w = 1.0; 25 // the last column. This is equivalent to using a full 3x3 matrix and expanding the vector
25 26 // to `vec3(vert_position.xy, 1.0)`
26 UV = texCoord; 27 gl_Position = vec4(mat2(modelview_matrix) * vert_position + modelview_matrix[2], 0.0, 1.0);
27})"; 28 frag_tex_coord = vert_tex_coord;
29}
30)";
28 31
29static const char g_fragment_shader[] = R"( 32const char g_fragment_shader[] = R"(
30#version 150 core 33#version 150 core
31in vec2 UV; 34
32out vec3 color; 35in vec2 frag_tex_coord;
33uniform sampler2D sampler; 36out vec4 color;
37
38uniform sampler2D color_texture;
34 39
35void main() { 40void main() {
36 color = texture(sampler, UV).rgb; 41 color = texture(color_texture, frag_tex_coord);
37})"; 42}
43)";
38 44
39} 45}
diff --git a/src/video_core/renderer_opengl/renderer_opengl.cpp b/src/video_core/renderer_opengl/renderer_opengl.cpp
index bc1683cb5..8483f79be 100644
--- a/src/video_core/renderer_opengl/renderer_opengl.cpp
+++ b/src/video_core/renderer_opengl/renderer_opengl.cpp
@@ -3,64 +3,51 @@
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include "core/hw/gpu.h" 5#include "core/hw/gpu.h"
6 6#include "core/mem_map.h"
7#include "common/emu_window.h"
7#include "video_core/video_core.h" 8#include "video_core/video_core.h"
8#include "video_core/renderer_opengl/renderer_opengl.h" 9#include "video_core/renderer_opengl/renderer_opengl.h"
9#include "video_core/renderer_opengl/gl_shader_util.h" 10#include "video_core/renderer_opengl/gl_shader_util.h"
10#include "video_core/renderer_opengl/gl_shaders.h" 11#include "video_core/renderer_opengl/gl_shaders.h"
11 12
12#include "core/mem_map.h"
13
14#include <algorithm> 13#include <algorithm>
15 14
16static const GLfloat kViewportAspectRatio = 15/**
17 (static_cast<float>(VideoCore::kScreenTopHeight) + VideoCore::kScreenBottomHeight) / VideoCore::kScreenTopWidth; 16 * Vertex structure that the drawn screen rectangles are composed of.
18 17 */
19// Fullscreen quad dimensions 18struct ScreenRectVertex {
20static const GLfloat kTopScreenWidthNormalized = 2; 19 ScreenRectVertex(GLfloat x, GLfloat y, GLfloat u, GLfloat v) {
21static const GLfloat kTopScreenHeightNormalized = kTopScreenWidthNormalized * (static_cast<float>(VideoCore::kScreenTopHeight) / VideoCore::kScreenTopWidth); 20 position[0] = x;
22static const GLfloat kBottomScreenWidthNormalized = kTopScreenWidthNormalized * (static_cast<float>(VideoCore::kScreenBottomWidth) / VideoCore::kScreenTopWidth); 21 position[1] = y;
23static const GLfloat kBottomScreenHeightNormalized = kBottomScreenWidthNormalized * (static_cast<float>(VideoCore::kScreenBottomHeight) / VideoCore::kScreenBottomWidth); 22 tex_coord[0] = u;
24 23 tex_coord[1] = v;
25static const GLfloat g_vbuffer_top[] = { 24 }
26 // x, y z u v
27 -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
28 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
29 1.0f, kTopScreenHeightNormalized, 0.0f, 1.0f, 0.0f,
30 1.0f, kTopScreenHeightNormalized, 0.0f, 1.0f, 0.0f,
31 -1.0f, kTopScreenHeightNormalized, 0.0f, 0.0f, 0.0f,
32 -1.0f, 0.0f, 0.0f, 0.0f, 1.0f
33};
34 25
35static const GLfloat g_vbuffer_bottom[] = { 26 GLfloat position[2];
36 // x y z u v 27 GLfloat tex_coord[2];
37 -(kBottomScreenWidthNormalized / 2), -kBottomScreenHeightNormalized, 0.0f, 0.0f, 1.0f,
38 (kBottomScreenWidthNormalized / 2), -kBottomScreenHeightNormalized, 0.0f, 1.0f, 1.0f,
39 (kBottomScreenWidthNormalized / 2), 0.0f, 0.0f, 1.0f, 0.0f,
40 (kBottomScreenWidthNormalized / 2), 0.0f, 0.0f, 1.0f, 0.0f,
41 -(kBottomScreenWidthNormalized / 2), 0.0f, 0.0f, 0.0f, 0.0f,
42 -(kBottomScreenWidthNormalized / 2), -kBottomScreenHeightNormalized, 0.0f, 0.0f, 1.0f
43}; 28};
44 29
30/**
31 * Defines a 1:1 pixel ortographic projection matrix with (0,0) on the top-left
32 * corner and (width, height) on the lower-bottom.
33 *
34 * The projection part of the matrix is trivial, hence these operations are represented
35 * by a 3x2 matrix.
36 */
37static std::array<GLfloat, 3*2> MakeOrthographicMatrix(const float width, const float height) {
38 std::array<GLfloat, 3*2> matrix;
39
40 matrix[0] = 2.f / width; matrix[2] = 0.f; matrix[4] = -1.f;
41 matrix[1] = 0.f; matrix[3] = -2.f / height; matrix[5] = 1.f;
42 // Last matrix row is implicitly assumed to be [0, 0, 1].
43
44 return matrix;
45}
46
45/// RendererOpenGL constructor 47/// RendererOpenGL constructor
46RendererOpenGL::RendererOpenGL() { 48RendererOpenGL::RendererOpenGL() {
47
48 resolution_width = std::max(VideoCore::kScreenTopWidth, VideoCore::kScreenBottomWidth); 49 resolution_width = std::max(VideoCore::kScreenTopWidth, VideoCore::kScreenBottomWidth);
49 resolution_height = VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight; 50 resolution_height = VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight;
50
51 // Initialize screen info
52 const auto& framebuffer_top = GPU::g_regs.framebuffer_config[0];
53 const auto& framebuffer_sub = GPU::g_regs.framebuffer_config[1];
54
55 screen_info.Top().width = VideoCore::kScreenTopWidth;
56 screen_info.Top().height = VideoCore::kScreenTopHeight;
57 screen_info.Top().stride = framebuffer_top.stride;
58 screen_info.Top().flipped_xfb_data = xfb_top_flipped;
59
60 screen_info.Bottom().width = VideoCore::kScreenBottomWidth;
61 screen_info.Bottom().height = VideoCore::kScreenBottomHeight;
62 screen_info.Bottom().stride = framebuffer_sub.stride;
63 screen_info.Bottom().flipped_xfb_data = xfb_bottom_flipped;
64} 51}
65 52
66/// RendererOpenGL destructor 53/// RendererOpenGL destructor
@@ -71,17 +58,24 @@ RendererOpenGL::~RendererOpenGL() {
71void RendererOpenGL::SwapBuffers() { 58void RendererOpenGL::SwapBuffers() {
72 render_window->MakeCurrent(); 59 render_window->MakeCurrent();
73 60
74 // EFB->XFB copy 61 for(int i : {0, 1}) {
75 // TODO(bunnei): This is a hack and does not belong here. The copy should be triggered by some 62 const auto& framebuffer = GPU::g_regs.framebuffer_config[i];
76 // register write. 63
77 // 64 if (textures[i].width != framebuffer.width || textures[i].height != framebuffer.height) {
78 // TODO(princesspeachum): (related to above^) this should only be called when there's new data, not every frame. 65 // Reallocate texture if the framebuffer size has changed.
79 // Currently this uploads data that shouldn't have changed. 66 // This is expected to not happen very often and hence should not be a
80 Common::Rect framebuffer_size(0, 0, resolution_width, resolution_height); 67 // performance problem.
81 RenderXFB(framebuffer_size, framebuffer_size); 68 glBindTexture(GL_TEXTURE_2D, textures[i].handle);
69 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, framebuffer.width, framebuffer.height, 0,
70 GL_BGR, GL_UNSIGNED_BYTE, nullptr);
71 textures[i].width = framebuffer.width;
72 textures[i].height = framebuffer.height;
73 }
74
75 LoadFBToActiveGLTexture(GPU::g_regs.framebuffer_config[i], textures[i]);
76 }
82 77
83 // XFB->Window copy 78 DrawScreens();
84 RenderFramebuffer();
85 79
86 // Swap buffers 80 // Swap buffers
87 render_window->PollEvents(); 81 render_window->PollEvents();
@@ -89,144 +83,135 @@ void RendererOpenGL::SwapBuffers() {
89} 83}
90 84
91/** 85/**
92 * Helper function to flip framebuffer from left-to-right to top-to-bottom 86 * Loads framebuffer from emulated memory into the active OpenGL texture.
93 * @param raw_data Pointer to input raw framebuffer in V/RAM
94 * @param screen_info ScreenInfo structure with screen size and output buffer pointer
95 * @todo Early on hack... I'd like to find a more efficient way of doing this /bunnei
96 */ 87 */
97void RendererOpenGL::FlipFramebuffer(const u8* raw_data, ScreenInfo& screen_info) { 88void RendererOpenGL::LoadFBToActiveGLTexture(const GPU::Regs::FramebufferConfig& framebuffer,
98 for (int x = 0; x < screen_info.width; x++) { 89 const TextureInfo& texture) {
99 int in_coord = x * screen_info.stride; 90 const VAddr framebuffer_vaddr = Memory::PhysicalToVirtualAddress(
100 for (int y = screen_info.height-1; y >= 0; y--) { 91 framebuffer.active_fb == 1 ? framebuffer.address_left2 : framebuffer.address_left1);
101 // TODO: Properly support other framebuffer formats 92
102 int out_coord = (x + y * screen_info.width) * 3; 93 DEBUG_LOG(GPU, "0x%08x bytes from 0x%08x(%dx%d), fmt %x",
103 screen_info.flipped_xfb_data[out_coord] = raw_data[in_coord + 2]; // Red 94 framebuffer.stride * framebuffer.height,
104 screen_info.flipped_xfb_data[out_coord + 1] = raw_data[in_coord + 1]; // Green 95 framebuffer_vaddr, (int)framebuffer.width,
105 screen_info.flipped_xfb_data[out_coord + 2] = raw_data[in_coord]; // Blue 96 (int)framebuffer.height, (int)framebuffer.format);
106 in_coord += 3; 97
107 } 98 const u8* framebuffer_data = Memory::GetPointer(framebuffer_vaddr);
108 } 99
100 // TODO: Handle other pixel formats
101 _dbg_assert_msg_(RENDER, framebuffer.color_format == GPU::Regs::PixelFormat::RGB8,
102 "Unsupported 3DS pixel format.");
103
104 size_t pixel_stride = framebuffer.stride / 3;
105 // OpenGL only supports specifying a stride in units of pixels, not bytes, unfortunately
106 _dbg_assert_(RENDER, pixel_stride * 3 == framebuffer.stride);
107 // Ensure no bad interactions with GL_UNPACK_ALIGNMENT, which by default
108 // only allows rows to have a memory alignement of 4.
109 _dbg_assert_(RENDER, pixel_stride % 4 == 0);
110
111 glBindTexture(GL_TEXTURE_2D, texture.handle);
112 glPixelStorei(GL_UNPACK_ROW_LENGTH, (GLint)pixel_stride);
113
114 // Update existing texture
115 // TODO: Test what happens on hardware when you change the framebuffer dimensions so that they
116 // differ from the LCD resolution.
117 // TODO: Applications could theoretically crash Citra here by specifying too large
118 // framebuffer sizes. We should make sure that this cannot happen.
119 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, framebuffer.width, framebuffer.height,
120 GL_BGR, GL_UNSIGNED_BYTE, framebuffer_data);
121
122 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
123
124 glBindTexture(GL_TEXTURE_2D, 0);
109} 125}
110 126
111/** 127/**
112 * Renders external framebuffer (XFB) 128 * Initializes the OpenGL state and creates persistent objects.
113 * @param src_rect Source rectangle in XFB to copy
114 * @param dst_rect Destination rectangle in output framebuffer to copy to
115 */ 129 */
116void RendererOpenGL::RenderXFB(const Common::Rect& src_rect, const Common::Rect& dst_rect) { 130void RendererOpenGL::InitOpenGLObjects() {
117 const auto& framebuffer_top = GPU::g_regs.framebuffer_config[0]; 131 glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
118 const auto& framebuffer_sub = GPU::g_regs.framebuffer_config[1]; 132 glDisable(GL_DEPTH_TEST);
119 const u32 active_fb_top = (framebuffer_top.active_fb == 1)
120 ? Memory::PhysicalToVirtualAddress(framebuffer_top.address_left2)
121 : Memory::PhysicalToVirtualAddress(framebuffer_top.address_left1);
122 const u32 active_fb_sub = (framebuffer_sub.active_fb == 1)
123 ? Memory::PhysicalToVirtualAddress(framebuffer_sub.address_left2)
124 : Memory::PhysicalToVirtualAddress(framebuffer_sub.address_left1);
125
126 DEBUG_LOG(GPU, "RenderXFB: 0x%08x bytes from 0x%08x(%dx%d), fmt %x",
127 framebuffer_top.stride * framebuffer_top.height,
128 active_fb_top, (int)framebuffer_top.width,
129 (int)framebuffer_top.height, (int)framebuffer_top.format);
130
131 FlipFramebuffer(Memory::GetPointer(active_fb_top), screen_info.Top());
132 FlipFramebuffer(Memory::GetPointer(active_fb_sub), screen_info.Bottom());
133
134 for (int i = 0; i < 2; i++) {
135 ScreenInfo* current_screen = &screen_info[i];
136
137 glBindTexture(GL_TEXTURE_2D, current_screen->texture_id);
138
139 // TODO: This should consider the GPU registers for framebuffer width, height and stride.
140 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, current_screen->width, current_screen->height,
141 GL_RGB, GL_UNSIGNED_BYTE, current_screen->flipped_xfb_data);
142 }
143
144 glBindTexture(GL_TEXTURE_2D, 0);
145
146 // TODO(princesspeachum):
147 // Only the subset src_rect of the GPU buffer
148 // should be copied into the texture of the relevant screen.
149 //
150 // The method's parameters also only include src_rect and dest_rec for one screen,
151 // so this may need to be changed (pair for each screen).
152}
153 133
154/// Initialize the FBO 134 // Link shaders and get variable locations
155void RendererOpenGL::InitFramebuffer() {
156 program_id = ShaderUtil::LoadShaders(GLShaders::g_vertex_shader, GLShaders::g_fragment_shader); 135 program_id = ShaderUtil::LoadShaders(GLShaders::g_vertex_shader, GLShaders::g_fragment_shader);
157 sampler_id = glGetUniformLocation(program_id, "sampler"); 136 uniform_modelview_matrix = glGetUniformLocation(program_id, "modelview_matrix");
158 attrib_position = glGetAttribLocation(program_id, "position"); 137 uniform_color_texture = glGetUniformLocation(program_id, "color_texture");
159 attrib_texcoord = glGetAttribLocation(program_id, "texCoord"); 138 attrib_position = glGetAttribLocation(program_id, "vert_position");
160 139 attrib_tex_coord = glGetAttribLocation(program_id, "vert_tex_coord");
161 // Generate vertex buffers for both screens
162 glGenBuffers(1, &screen_info.Top().vertex_buffer_id);
163 glGenBuffers(1, &screen_info.Bottom().vertex_buffer_id);
164
165 // Attach vertex data for top screen
166 glBindBuffer(GL_ARRAY_BUFFER, screen_info.Top().vertex_buffer_id);
167 glBufferData(GL_ARRAY_BUFFER, sizeof(g_vbuffer_top), g_vbuffer_top, GL_STATIC_DRAW);
168
169 // Attach vertex data for bottom screen
170 glBindBuffer(GL_ARRAY_BUFFER, screen_info.Bottom().vertex_buffer_id);
171 glBufferData(GL_ARRAY_BUFFER, sizeof(g_vbuffer_bottom), g_vbuffer_bottom, GL_STATIC_DRAW);
172 140
173 // Create color buffers for both screens 141 // Generate VBO handle for drawing
174 glGenTextures(1, &screen_info.Top().texture_id); 142 glGenBuffers(1, &vertex_buffer_handle);
175 glGenTextures(1, &screen_info.Bottom().texture_id);
176 143
177 for (int i = 0; i < 2; i++) { 144 // Generate VAO
145 glGenVertexArrays(1, &vertex_array_handle);
146 glBindVertexArray(vertex_array_handle);
147
148 // Attach vertex data to VAO
149 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_handle);
150 glBufferData(GL_ARRAY_BUFFER, sizeof(ScreenRectVertex) * 4, nullptr, GL_STREAM_DRAW);
151 glVertexAttribPointer(attrib_position, 2, GL_FLOAT, GL_FALSE, sizeof(ScreenRectVertex), (GLvoid*)offsetof(ScreenRectVertex, position));
152 glVertexAttribPointer(attrib_tex_coord, 2, GL_FLOAT, GL_FALSE, sizeof(ScreenRectVertex), (GLvoid*)offsetof(ScreenRectVertex, tex_coord));
153 glEnableVertexAttribArray(attrib_position);
154 glEnableVertexAttribArray(attrib_tex_coord);
178 155
179 ScreenInfo* current_screen = &screen_info[i]; 156 // Allocate textures for each screen
157 for (auto& texture : textures) {
158 glGenTextures(1, &texture.handle);
180 159
181 // Allocate texture 160 // Allocation of storage is deferred until the first frame, when we
182 glBindTexture(GL_TEXTURE_2D, current_screen->vertex_buffer_id); 161 // know the framebuffer size.
183 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, current_screen->width, current_screen->height,
184 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
185 162
163 glBindTexture(GL_TEXTURE_2D, texture.handle);
164 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
186 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 165 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
187 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 166 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
167 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
168 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
188 } 169 }
189
190 glBindTexture(GL_TEXTURE_2D, 0); 170 glBindTexture(GL_TEXTURE_2D, 0);
191} 171}
192 172
193void RendererOpenGL::RenderFramebuffer() { 173/**
174 * Draws a single texture to the emulator window, rotating the texture to correct for the 3DS's LCD rotation.
175 */
176void RendererOpenGL::DrawSingleScreenRotated(const TextureInfo& texture, float x, float y, float w, float h) {
177 std::array<ScreenRectVertex, 4> vertices = {
178 ScreenRectVertex(x, y, 1.f, 0.f),
179 ScreenRectVertex(x+w, y, 1.f, 1.f),
180 ScreenRectVertex(x, y+h, 0.f, 0.f),
181 ScreenRectVertex(x+w, y+h, 0.f, 1.f),
182 };
183
184 glBindTexture(GL_TEXTURE_2D, texture.handle);
185 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_handle);
186 glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices.data());
187 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
188}
189
190/**
191 * Draws the emulated screens to the emulator window.
192 */
193void RendererOpenGL::DrawScreens() {
194 glViewport(0, 0, resolution_width, resolution_height); 194 glViewport(0, 0, resolution_width, resolution_height);
195 glClear(GL_COLOR_BUFFER_BIT); 195 glClear(GL_COLOR_BUFFER_BIT);
196 196
197 glUseProgram(program_id); 197 glUseProgram(program_id);
198 198
199 // Set projection matrix
200 std::array<GLfloat, 3*2> ortho_matrix = MakeOrthographicMatrix((float)resolution_width, (float)resolution_height);
201 glUniformMatrix3x2fv(uniform_modelview_matrix, 1, GL_FALSE, ortho_matrix.data());
202
199 // Bind texture in Texture Unit 0 203 // Bind texture in Texture Unit 0
200 glActiveTexture(GL_TEXTURE0); 204 glActiveTexture(GL_TEXTURE0);
205 glUniform1i(uniform_color_texture, 0);
201 206
202 glEnableVertexAttribArray(attrib_position); 207 const float max_width = std::max((float)VideoCore::kScreenTopWidth, (float)VideoCore::kScreenBottomWidth);
203 glEnableVertexAttribArray(attrib_texcoord); 208 const float top_x = 0.5f * (max_width - VideoCore::kScreenTopWidth);
204 209 const float bottom_x = 0.5f * (max_width - VideoCore::kScreenBottomWidth);
205 for (int i = 0; i < 2; i++) {
206 210
207 ScreenInfo* current_screen = &screen_info[i]; 211 DrawSingleScreenRotated(textures[0], top_x, 0,
208 212 (float)VideoCore::kScreenTopWidth, (float)VideoCore::kScreenTopHeight);
209 glBindTexture(GL_TEXTURE_2D, current_screen->texture_id); 213 DrawSingleScreenRotated(textures[1], bottom_x, (float)VideoCore::kScreenTopHeight,
210 214 (float)VideoCore::kScreenBottomWidth, (float)VideoCore::kScreenBottomHeight);
211 // Set sampler on Texture Unit 0
212 glUniform1i(sampler_id, 0);
213
214 glBindBuffer(GL_ARRAY_BUFFER, current_screen->vertex_buffer_id);
215
216 // Vertex buffer layout
217 const GLsizei stride = 5 * sizeof(GLfloat);
218 const GLvoid* uv_offset = (const GLvoid*)(3 * sizeof(GLfloat));
219
220 // Configure vertex buffer
221 glVertexAttribPointer(attrib_position, 3, GL_FLOAT, GL_FALSE, stride, NULL);
222 glVertexAttribPointer(attrib_texcoord, 2, GL_FLOAT, GL_FALSE, stride, uv_offset);
223
224 // Draw screen
225 glDrawArrays(GL_TRIANGLES, 0, 6);
226 }
227
228 glDisableVertexAttribArray(attrib_position);
229 glDisableVertexAttribArray(attrib_texcoord);
230 215
231 m_current_frame++; 216 m_current_frame++;
232} 217}
@@ -253,20 +238,8 @@ void RendererOpenGL::Init() {
253 exit(-1); 238 exit(-1);
254 } 239 }
255 240
256 // Generate VAO
257 glGenVertexArrays(1, &vertex_array_id);
258 glBindVertexArray(vertex_array_id);
259
260 glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
261 glDisable(GL_DEPTH_TEST);
262
263 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
264
265 // Initialize everything else
266 // --------------------------
267 InitFramebuffer();
268
269 NOTICE_LOG(RENDER, "GL_VERSION: %s\n", glGetString(GL_VERSION)); 241 NOTICE_LOG(RENDER, "GL_VERSION: %s\n", glGetString(GL_VERSION));
242 InitOpenGLObjects();
270} 243}
271 244
272/// Shutdown the renderer 245/// Shutdown the renderer
diff --git a/src/video_core/renderer_opengl/renderer_opengl.h b/src/video_core/renderer_opengl/renderer_opengl.h
index 0d25b2a52..82ef4b14b 100644
--- a/src/video_core/renderer_opengl/renderer_opengl.h
+++ b/src/video_core/renderer_opengl/renderer_opengl.h
@@ -7,29 +7,23 @@
7#include "generated/gl_3_2_core.h" 7#include "generated/gl_3_2_core.h"
8 8
9#include "common/common.h" 9#include "common/common.h"
10#include "common/emu_window.h" 10#include "core/hw/gpu.h"
11
12#include "video_core/renderer_base.h" 11#include "video_core/renderer_base.h"
13 12
14#include <array> 13#include <array>
15 14
16class RendererOpenGL : virtual public RendererBase { 15class EmuWindow;
16
17class RendererOpenGL : public RendererBase {
17public: 18public:
18 19
19 RendererOpenGL(); 20 RendererOpenGL();
20 ~RendererOpenGL(); 21 ~RendererOpenGL() override;
21 22
22 /// Swap buffers (render frame) 23 /// Swap buffers (render frame)
23 void SwapBuffers(); 24 void SwapBuffers();
24 25
25 /** 26 /**
26 * Renders external framebuffer (XFB)
27 * @param src_rect Source rectangle in XFB to copy
28 * @param dst_rect Destination rectangle in output framebuffer to copy to
29 */
30 void RenderXFB(const Common::Rect& src_rect, const Common::Rect& dst_rect);
31
32 /**
33 * Set the emulator window to use for renderer 27 * Set the emulator window to use for renderer
34 * @param window EmuWindow handle to emulator window to use for rendering 28 * @param window EmuWindow handle to emulator window to use for rendering
35 */ 29 */
@@ -42,38 +36,21 @@ public:
42 void ShutDown(); 36 void ShutDown();
43 37
44private: 38private:
39 /// Structure used for storing information about the textures for each 3DS screen
40 struct TextureInfo {
41 GLuint handle;
42 GLsizei width;
43 GLsizei height;
44 };
45 45
46 /// Initialize the FBO 46 void InitOpenGLObjects();
47 void InitFramebuffer(); 47 void DrawScreens();
48 48 void DrawSingleScreenRotated(const TextureInfo& texture, float x, float y, float w, float h);
49 // Blit the FBO to the OpenGL default framebuffer
50 void RenderFramebuffer();
51
52 /// Updates the framerate
53 void UpdateFramerate(); 49 void UpdateFramerate();
54 50
55 /// Structure used for storing information for rendering each 3DS screen 51 // Loads framebuffer from emulated memory into the active OpenGL texture.
56 struct ScreenInfo { 52 static void LoadFBToActiveGLTexture(const GPU::Regs::FramebufferConfig& framebuffer,
57 // Properties 53 const TextureInfo& texture);
58 int width;
59 int height;
60 int stride; ///< Number of bytes between the coordinates (0,0) and (1,0)
61
62 // OpenGL object IDs
63 GLuint texture_id;
64 GLuint vertex_buffer_id;
65
66 // Temporary
67 u8* flipped_xfb_data;
68 };
69
70 /**
71 * Helper function to flip framebuffer from left-to-right to top-to-bottom
72 * @param raw_data Pointer to input raw framebuffer in V/RAM
73 * @param screen_info ScreenInfo structure with screen size and output buffer pointer
74 * @todo Early on hack... I'd like to find a more efficient way of doing this /bunnei
75 */
76 void FlipFramebuffer(const u8* raw_data, ScreenInfo& screen_info);
77 54
78 EmuWindow* render_window; ///< Handle to render window 55 EmuWindow* render_window; ///< Handle to render window
79 u32 last_mode; ///< Last render mode 56 u32 last_mode; ///< Last render mode
@@ -81,22 +58,15 @@ private:
81 int resolution_width; ///< Current resolution width 58 int resolution_width; ///< Current resolution width
82 int resolution_height; ///< Current resolution height 59 int resolution_height; ///< Current resolution height
83 60
84 // OpenGL global object IDs 61 // OpenGL object IDs
85 GLuint vertex_array_id; 62 GLuint vertex_array_handle;
63 GLuint vertex_buffer_handle;
86 GLuint program_id; 64 GLuint program_id;
87 GLuint sampler_id; 65 std::array<TextureInfo, 2> textures;
66 // Shader uniform location indices
67 GLuint uniform_modelview_matrix;
68 GLuint uniform_color_texture;
88 // Shader attribute input indices 69 // Shader attribute input indices
89 GLuint attrib_position; 70 GLuint attrib_position;
90 GLuint attrib_texcoord; 71 GLuint attrib_tex_coord;
91
92 struct : std::array<ScreenInfo, 2> {
93 ScreenInfo& Top() { return (*this)[0]; }
94 ScreenInfo& Bottom() { return (*this)[1]; }
95 } screen_info;
96
97 // "Flipped" framebuffers translate scanlines from native 3DS left-to-right to top-to-bottom
98 // as OpenGL expects them in a texture. There probably is a more efficient way of doing this:
99 u8 xfb_top_flipped[VideoCore::kScreenTopWidth * VideoCore::kScreenTopHeight * 4];
100 u8 xfb_bottom_flipped[VideoCore::kScreenBottomWidth * VideoCore::kScreenBottomHeight * 4];
101
102}; 72};
diff --git a/src/video_core/video_core.h b/src/video_core/video_core.h
index 5e8129b5a..609aac513 100644
--- a/src/video_core/video_core.h
+++ b/src/video_core/video_core.h
@@ -17,6 +17,10 @@ namespace VideoCore {
17// 3DS Video Constants 17// 3DS Video Constants
18// ------------------- 18// -------------------
19 19
20// NOTE: The LCDs actually rotate the image 90 degrees when displaying. Because of that the
21// framebuffers in video memory are stored in column-major order and rendered sideways, causing
22// the widths and heights of the framebuffers read by the LCD to be switched compared to the
23// heights and widths of the screens listed here.
20static const int kScreenTopWidth = 400; ///< 3DS top screen width 24static const int kScreenTopWidth = 400; ///< 3DS top screen width
21static const int kScreenTopHeight = 240; ///< 3DS top screen height 25static const int kScreenTopHeight = 240; ///< 3DS top screen height
22static const int kScreenBottomWidth = 320; ///< 3DS bottom screen width 26static const int kScreenBottomWidth = 320; ///< 3DS bottom screen width