summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/video_core/renderer_opengl/gl_shaders.h46
-rw-r--r--src/video_core/renderer_opengl/renderer_opengl.cpp308
-rw-r--r--src/video_core/renderer_opengl/renderer_opengl.h68
-rw-r--r--src/video_core/video_core.h4
4 files changed, 193 insertions, 233 deletions
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 cad278381..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,16 +58,23 @@ 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
84 DrawScreens(); 78 DrawScreens();
85 79
86 // Swap buffers 80 // Swap buffers
@@ -89,116 +83,111 @@ 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 */
97void RendererOpenGL::FlipFramebuffer(const u8* raw_data, ScreenInfo& screen_info) {
98 for (int x = 0; x < screen_info.width; x++) {
99 int in_coord = x * screen_info.stride;
100 for (int y = screen_info.height-1; y >= 0; y--) {
101 // TODO: Properly support other framebuffer formats
102 int out_coord = (x + y * screen_info.width) * 3;
103 screen_info.flipped_xfb_data[out_coord] = raw_data[in_coord + 2]; // Red
104 screen_info.flipped_xfb_data[out_coord + 1] = raw_data[in_coord + 1]; // Green
105 screen_info.flipped_xfb_data[out_coord + 2] = raw_data[in_coord]; // Blue
106 in_coord += 3;
107 }
108 }
109}
110
111/**
112 * Renders external framebuffer (XFB)
113 * @param src_rect Source rectangle in XFB to copy
114 * @param dst_rect Destination rectangle in output framebuffer to copy to
115 */ 87 */
116void RendererOpenGL::RenderXFB(const Common::Rect& src_rect, const Common::Rect& dst_rect) { 88void RendererOpenGL::LoadFBToActiveGLTexture(const GPU::Regs::FramebufferConfig& framebuffer,
117 const auto& framebuffer_top = GPU::g_regs.framebuffer_config[0]; 89 const TextureInfo& texture) {
118 const auto& framebuffer_sub = GPU::g_regs.framebuffer_config[1]; 90 const VAddr framebuffer_vaddr = Memory::PhysicalToVirtualAddress(
119 const u32 active_fb_top = (framebuffer_top.active_fb == 1) 91 framebuffer.active_fb == 1 ? framebuffer.address_left2 : framebuffer.address_left1);
120 ? Memory::PhysicalToVirtualAddress(framebuffer_top.address_left2) 92
121 : Memory::PhysicalToVirtualAddress(framebuffer_top.address_left1); 93 DEBUG_LOG(GPU, "0x%08x bytes from 0x%08x(%dx%d), fmt %x",
122 const u32 active_fb_sub = (framebuffer_sub.active_fb == 1) 94 framebuffer.stride * framebuffer.height,
123 ? Memory::PhysicalToVirtualAddress(framebuffer_sub.address_left2) 95 framebuffer_vaddr, (int)framebuffer.width,
124 : Memory::PhysicalToVirtualAddress(framebuffer_sub.address_left1); 96 (int)framebuffer.height, (int)framebuffer.format);
125 97
126 DEBUG_LOG(GPU, "RenderXFB: 0x%08x bytes from 0x%08x(%dx%d), fmt %x", 98 const u8* framebuffer_data = Memory::GetPointer(framebuffer_vaddr);
127 framebuffer_top.stride * framebuffer_top.height, 99
128 active_fb_top, (int)framebuffer_top.width, 100 // TODO: Handle other pixel formats
129 (int)framebuffer_top.height, (int)framebuffer_top.format); 101 _dbg_assert_msg_(RENDER, framebuffer.color_format == GPU::Regs::PixelFormat::RGB8,
130 102 "Unsupported 3DS pixel format.");
131 FlipFramebuffer(Memory::GetPointer(active_fb_top), screen_info.Top()); 103
132 FlipFramebuffer(Memory::GetPointer(active_fb_sub), screen_info.Bottom()); 104 size_t pixel_stride = framebuffer.stride / 3;
133 105 // OpenGL only supports specifying a stride in units of pixels, not bytes, unfortunately
134 for (int i = 0; i < 2; i++) { 106 _dbg_assert_(RENDER, pixel_stride * 3 == framebuffer.stride);
135 ScreenInfo* current_screen = &screen_info[i]; 107 // Ensure no bad interactions with GL_UNPACK_ALIGNMENT, which by default
136 108 // only allows rows to have a memory alignement of 4.
137 glBindTexture(GL_TEXTURE_2D, current_screen->texture_id); 109 _dbg_assert_(RENDER, pixel_stride % 4 == 0);
138 110
139 // TODO: This should consider the GPU registers for framebuffer width, height and stride. 111 glBindTexture(GL_TEXTURE_2D, texture.handle);
140 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, current_screen->width, current_screen->height, 112 glPixelStorei(GL_UNPACK_ROW_LENGTH, (GLint)pixel_stride);
141 GL_RGB, GL_UNSIGNED_BYTE, current_screen->flipped_xfb_data); 113
142 } 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);
143 123
144 glBindTexture(GL_TEXTURE_2D, 0); 124 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} 125}
153 126
154/** 127/**
155 * Initializes the OpenGL state and creates persistent objects. 128 * Initializes the OpenGL state and creates persistent objects.
156 */ 129 */
157void RendererOpenGL::InitOpenGLObjects() { 130void RendererOpenGL::InitOpenGLObjects() {
158 glGenVertexArrays(1, &vertex_array_id);
159 glBindVertexArray(vertex_array_id);
160
161 glClearColor(1.0f, 1.0f, 1.0f, 0.0f); 131 glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
162 glDisable(GL_DEPTH_TEST); 132 glDisable(GL_DEPTH_TEST);
163 133
134 // Link shaders and get variable locations
164 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);
165 sampler_id = glGetUniformLocation(program_id, "sampler"); 136 uniform_modelview_matrix = glGetUniformLocation(program_id, "modelview_matrix");
166 attrib_position = glGetAttribLocation(program_id, "position"); 137 uniform_color_texture = glGetUniformLocation(program_id, "color_texture");
167 attrib_texcoord = glGetAttribLocation(program_id, "texCoord"); 138 attrib_position = glGetAttribLocation(program_id, "vert_position");
168 139 attrib_tex_coord = glGetAttribLocation(program_id, "vert_tex_coord");
169 // Generate vertex buffers for both screens 140
170 glGenBuffers(1, &screen_info.Top().vertex_buffer_id); 141 // Generate VBO handle for drawing
171 glGenBuffers(1, &screen_info.Bottom().vertex_buffer_id); 142 glGenBuffers(1, &vertex_buffer_handle);
172 143
173 // Attach vertex data for top screen 144 // Generate VAO
174 glBindBuffer(GL_ARRAY_BUFFER, screen_info.Top().vertex_buffer_id); 145 glGenVertexArrays(1, &vertex_array_handle);
175 glBufferData(GL_ARRAY_BUFFER, sizeof(g_vbuffer_top), g_vbuffer_top, GL_STATIC_DRAW); 146 glBindVertexArray(vertex_array_handle);
176 147
177 // Attach vertex data for bottom screen 148 // Attach vertex data to VAO
178 glBindBuffer(GL_ARRAY_BUFFER, screen_info.Bottom().vertex_buffer_id); 149 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_handle);
179 glBufferData(GL_ARRAY_BUFFER, sizeof(g_vbuffer_bottom), g_vbuffer_bottom, GL_STATIC_DRAW); 150 glBufferData(GL_ARRAY_BUFFER, sizeof(ScreenRectVertex) * 4, nullptr, GL_STREAM_DRAW);
180 151 glVertexAttribPointer(attrib_position, 2, GL_FLOAT, GL_FALSE, sizeof(ScreenRectVertex), (GLvoid*)offsetof(ScreenRectVertex, position));
181 // Create color buffers for both screens 152 glVertexAttribPointer(attrib_tex_coord, 2, GL_FLOAT, GL_FALSE, sizeof(ScreenRectVertex), (GLvoid*)offsetof(ScreenRectVertex, tex_coord));
182 glGenTextures(1, &screen_info.Top().texture_id); 153 glEnableVertexAttribArray(attrib_position);
183 glGenTextures(1, &screen_info.Bottom().texture_id); 154 glEnableVertexAttribArray(attrib_tex_coord);
184
185 for (int i = 0; i < 2; i++) {
186 155
187 ScreenInfo* current_screen = &screen_info[i]; 156 // Allocate textures for each screen
157 for (auto& texture : textures) {
158 glGenTextures(1, &texture.handle);
188 159
189 // Allocate texture 160 // Allocation of storage is deferred until the first frame, when we
190 glBindTexture(GL_TEXTURE_2D, current_screen->vertex_buffer_id); 161 // know the framebuffer size.
191 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, current_screen->width, current_screen->height,
192 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
193 162
163 glBindTexture(GL_TEXTURE_2D, texture.handle);
164 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
194 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 165 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
195 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);
196 } 169 }
197
198 glBindTexture(GL_TEXTURE_2D, 0); 170 glBindTexture(GL_TEXTURE_2D, 0);
199} 171}
200 172
201/** 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/**
202 * Draws the emulated screens to the emulator window. 191 * Draws the emulated screens to the emulator window.
203 */ 192 */
204void RendererOpenGL::DrawScreens() { 193void RendererOpenGL::DrawScreens() {
@@ -207,37 +196,22 @@ void RendererOpenGL::DrawScreens() {
207 196
208 glUseProgram(program_id); 197 glUseProgram(program_id);
209 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
210 // Bind texture in Texture Unit 0 203 // Bind texture in Texture Unit 0
211 glActiveTexture(GL_TEXTURE0); 204 glActiveTexture(GL_TEXTURE0);
205 glUniform1i(uniform_color_texture, 0);
212 206
213 glEnableVertexAttribArray(attrib_position); 207 const float max_width = std::max((float)VideoCore::kScreenTopWidth, (float)VideoCore::kScreenBottomWidth);
214 glEnableVertexAttribArray(attrib_texcoord); 208 const float top_x = 0.5f * (max_width - VideoCore::kScreenTopWidth);
215 209 const float bottom_x = 0.5f * (max_width - VideoCore::kScreenBottomWidth);
216 for (int i = 0; i < 2; i++) {
217
218 ScreenInfo* current_screen = &screen_info[i];
219
220 glBindTexture(GL_TEXTURE_2D, current_screen->texture_id);
221
222 // Set sampler on Texture Unit 0
223 glUniform1i(sampler_id, 0);
224
225 glBindBuffer(GL_ARRAY_BUFFER, current_screen->vertex_buffer_id);
226
227 // Vertex buffer layout
228 const GLsizei stride = 5 * sizeof(GLfloat);
229 const GLvoid* uv_offset = (const GLvoid*)(3 * sizeof(GLfloat));
230
231 // Configure vertex buffer
232 glVertexAttribPointer(attrib_position, 3, GL_FLOAT, GL_FALSE, stride, NULL);
233 glVertexAttribPointer(attrib_texcoord, 2, GL_FLOAT, GL_FALSE, stride, uv_offset);
234
235 // Draw screen
236 glDrawArrays(GL_TRIANGLES, 0, 6);
237 }
238 210
239 glDisableVertexAttribArray(attrib_position); 211 DrawSingleScreenRotated(textures[0], top_x, 0,
240 glDisableVertexAttribArray(attrib_texcoord); 212 (float)VideoCore::kScreenTopWidth, (float)VideoCore::kScreenTopHeight);
213 DrawSingleScreenRotated(textures[1], bottom_x, (float)VideoCore::kScreenTopHeight,
214 (float)VideoCore::kScreenBottomWidth, (float)VideoCore::kScreenBottomHeight);
241 215
242 m_current_frame++; 216 m_current_frame++;
243} 217}
diff --git a/src/video_core/renderer_opengl/renderer_opengl.h b/src/video_core/renderer_opengl/renderer_opengl.h
index 3dcb331be..82ef4b14b 100644
--- a/src/video_core/renderer_opengl/renderer_opengl.h
+++ b/src/video_core/renderer_opengl/renderer_opengl.h
@@ -7,12 +7,13 @@
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
15class EmuWindow;
16
16class RendererOpenGL : public RendererBase { 17class RendererOpenGL : public RendererBase {
17public: 18public:
18 19
@@ -23,13 +24,6 @@ public:
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,32 +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 void InitOpenGLObjects(); 46 void InitOpenGLObjects();
46 void DrawScreens(); 47 void DrawScreens();
48 void DrawSingleScreenRotated(const TextureInfo& texture, float x, float y, float w, float h);
47 void UpdateFramerate(); 49 void UpdateFramerate();
48 50
49 /// Structure used for storing information for rendering each 3DS screen 51 // Loads framebuffer from emulated memory into the active OpenGL texture.
50 struct ScreenInfo { 52 static void LoadFBToActiveGLTexture(const GPU::Regs::FramebufferConfig& framebuffer,
51 // Properties 53 const TextureInfo& texture);
52 int width;
53 int height;
54 int stride; ///< Number of bytes between the coordinates (0,0) and (1,0)
55
56 // OpenGL object IDs
57 GLuint texture_id;
58 GLuint vertex_buffer_id;
59
60 // Temporary
61 u8* flipped_xfb_data;
62 };
63
64 /**
65 * Helper function to flip framebuffer from left-to-right to top-to-bottom
66 * @param raw_data Pointer to input raw framebuffer in V/RAM
67 * @param screen_info ScreenInfo structure with screen size and output buffer pointer
68 * @todo Early on hack... I'd like to find a more efficient way of doing this /bunnei
69 */
70 void FlipFramebuffer(const u8* raw_data, ScreenInfo& screen_info);
71 54
72 EmuWindow* render_window; ///< Handle to render window 55 EmuWindow* render_window; ///< Handle to render window
73 u32 last_mode; ///< Last render mode 56 u32 last_mode; ///< Last render mode
@@ -75,22 +58,15 @@ private:
75 int resolution_width; ///< Current resolution width 58 int resolution_width; ///< Current resolution width
76 int resolution_height; ///< Current resolution height 59 int resolution_height; ///< Current resolution height
77 60
78 // OpenGL global object IDs 61 // OpenGL object IDs
79 GLuint vertex_array_id; 62 GLuint vertex_array_handle;
63 GLuint vertex_buffer_handle;
80 GLuint program_id; 64 GLuint program_id;
81 GLuint sampler_id; 65 std::array<TextureInfo, 2> textures;
66 // Shader uniform location indices
67 GLuint uniform_modelview_matrix;
68 GLuint uniform_color_texture;
82 // Shader attribute input indices 69 // Shader attribute input indices
83 GLuint attrib_position; 70 GLuint attrib_position;
84 GLuint attrib_texcoord; 71 GLuint attrib_tex_coord;
85
86 struct : std::array<ScreenInfo, 2> {
87 ScreenInfo& Top() { return (*this)[0]; }
88 ScreenInfo& Bottom() { return (*this)[1]; }
89 } screen_info;
90
91 // "Flipped" framebuffers translate scanlines from native 3DS left-to-right to top-to-bottom
92 // as OpenGL expects them in a texture. There probably is a more efficient way of doing this:
93 u8 xfb_top_flipped[VideoCore::kScreenTopWidth * VideoCore::kScreenTopHeight * 4];
94 u8 xfb_bottom_flipped[VideoCore::kScreenBottomWidth * VideoCore::kScreenBottomHeight * 4];
95
96}; 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