2014-04-09 01:04:25 +02:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2014-04-06 22:55:39 +02:00
|
|
|
|
2014-05-17 22:50:33 +02:00
|
|
|
#include "core/hw/gpu.h"
|
2014-08-28 20:21:54 +02:00
|
|
|
#include "core/mem_map.h"
|
|
|
|
#include "common/emu_window.h"
|
2014-04-09 02:15:08 +02:00
|
|
|
#include "video_core/video_core.h"
|
|
|
|
#include "video_core/renderer_opengl/renderer_opengl.h"
|
2014-08-21 09:27:53 +02:00
|
|
|
#include "video_core/renderer_opengl/gl_shader_util.h"
|
|
|
|
#include "video_core/renderer_opengl/gl_shaders.h"
|
2014-04-09 02:15:08 +02:00
|
|
|
|
2014-08-21 09:27:53 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2014-08-28 20:21:54 +02:00
|
|
|
/**
|
|
|
|
* Vertex structure that the drawn screen rectangles are composed of.
|
|
|
|
*/
|
|
|
|
struct ScreenRectVertex {
|
|
|
|
ScreenRectVertex(GLfloat x, GLfloat y, GLfloat u, GLfloat v) {
|
|
|
|
position[0] = x;
|
|
|
|
position[1] = y;
|
|
|
|
tex_coord[0] = u;
|
|
|
|
tex_coord[1] = v;
|
|
|
|
}
|
2014-08-21 09:27:53 +02:00
|
|
|
|
2014-08-28 20:21:54 +02:00
|
|
|
GLfloat position[2];
|
|
|
|
GLfloat tex_coord[2];
|
2014-08-21 09:27:53 +02:00
|
|
|
};
|
2014-04-07 06:52:34 +02:00
|
|
|
|
2014-08-28 20:21:54 +02:00
|
|
|
/**
|
|
|
|
* Defines a 1:1 pixel ortographic projection matrix with (0,0) on the top-left
|
|
|
|
* corner and (width, height) on the lower-bottom.
|
|
|
|
*
|
|
|
|
* The projection part of the matrix is trivial, hence these operations are represented
|
|
|
|
* by a 3x2 matrix.
|
|
|
|
*/
|
|
|
|
static std::array<GLfloat, 3*2> MakeOrthographicMatrix(const float width, const float height) {
|
|
|
|
std::array<GLfloat, 3*2> matrix;
|
|
|
|
|
|
|
|
matrix[0] = 2.f / width; matrix[2] = 0.f; matrix[4] = -1.f;
|
|
|
|
matrix[1] = 0.f; matrix[3] = -2.f / height; matrix[5] = 1.f;
|
|
|
|
// Last matrix row is implicitly assumed to be [0, 0, 1].
|
|
|
|
|
|
|
|
return matrix;
|
|
|
|
}
|
|
|
|
|
2014-04-06 22:55:39 +02:00
|
|
|
/// RendererOpenGL constructor
|
|
|
|
RendererOpenGL::RendererOpenGL() {
|
2014-08-21 09:27:53 +02:00
|
|
|
resolution_width = std::max(VideoCore::kScreenTopWidth, VideoCore::kScreenBottomWidth);
|
|
|
|
resolution_height = VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight;
|
2014-04-06 22:55:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// RendererOpenGL destructor
|
|
|
|
RendererOpenGL::~RendererOpenGL() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Swap buffers (render frame)
|
|
|
|
void RendererOpenGL::SwapBuffers() {
|
2014-08-21 09:27:53 +02:00
|
|
|
render_window->MakeCurrent();
|
2014-04-22 04:00:32 +02:00
|
|
|
|
2014-08-28 20:21:54 +02:00
|
|
|
for(int i : {0, 1}) {
|
|
|
|
const auto& framebuffer = GPU::g_regs.framebuffer_config[i];
|
|
|
|
|
2014-11-30 08:44:30 +01:00
|
|
|
if (textures[i].width != (GLsizei)framebuffer.width || textures[i].height != (GLsizei)framebuffer.height) {
|
2014-08-28 20:21:54 +02:00
|
|
|
// Reallocate texture if the framebuffer size has changed.
|
|
|
|
// This is expected to not happen very often and hence should not be a
|
|
|
|
// performance problem.
|
|
|
|
glBindTexture(GL_TEXTURE_2D, textures[i].handle);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, framebuffer.width, framebuffer.height, 0,
|
|
|
|
GL_BGR, GL_UNSIGNED_BYTE, nullptr);
|
|
|
|
textures[i].width = framebuffer.width;
|
|
|
|
textures[i].height = framebuffer.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadFBToActiveGLTexture(GPU::g_regs.framebuffer_config[i], textures[i]);
|
|
|
|
}
|
2014-04-06 22:55:39 +02:00
|
|
|
|
2014-08-28 11:34:47 +02:00
|
|
|
DrawScreens();
|
2014-04-06 22:55:39 +02:00
|
|
|
|
|
|
|
// Swap buffers
|
2014-08-21 09:27:53 +02:00
|
|
|
render_window->PollEvents();
|
|
|
|
render_window->SwapBuffers();
|
2014-04-09 00:59:02 +02:00
|
|
|
}
|
2014-04-06 22:55:39 +02:00
|
|
|
|
2014-04-09 00:59:02 +02:00
|
|
|
/**
|
2014-08-28 20:21:54 +02:00
|
|
|
* Loads framebuffer from emulated memory into the active OpenGL texture.
|
2014-04-06 22:55:39 +02:00
|
|
|
*/
|
2014-08-28 20:21:54 +02:00
|
|
|
void RendererOpenGL::LoadFBToActiveGLTexture(const GPU::Regs::FramebufferConfig& framebuffer,
|
|
|
|
const TextureInfo& texture) {
|
|
|
|
const VAddr framebuffer_vaddr = Memory::PhysicalToVirtualAddress(
|
|
|
|
framebuffer.active_fb == 1 ? framebuffer.address_left2 : framebuffer.address_left1);
|
|
|
|
|
2014-12-06 02:53:49 +01:00
|
|
|
LOG_TRACE(Render_OpenGL, "0x%08x bytes from 0x%08x(%dx%d), fmt %x",
|
2014-08-28 20:21:54 +02:00
|
|
|
framebuffer.stride * framebuffer.height,
|
|
|
|
framebuffer_vaddr, (int)framebuffer.width,
|
|
|
|
(int)framebuffer.height, (int)framebuffer.format);
|
|
|
|
|
|
|
|
const u8* framebuffer_data = Memory::GetPointer(framebuffer_vaddr);
|
|
|
|
|
|
|
|
// TODO: Handle other pixel formats
|
2014-12-06 02:53:49 +01:00
|
|
|
_dbg_assert_msg_(Render_OpenGL, framebuffer.color_format == GPU::Regs::PixelFormat::RGB8,
|
2014-08-28 20:21:54 +02:00
|
|
|
"Unsupported 3DS pixel format.");
|
|
|
|
|
|
|
|
size_t pixel_stride = framebuffer.stride / 3;
|
|
|
|
// OpenGL only supports specifying a stride in units of pixels, not bytes, unfortunately
|
2014-12-06 02:53:49 +01:00
|
|
|
_dbg_assert_(Render_OpenGL, pixel_stride * 3 == framebuffer.stride);
|
2014-08-28 20:21:54 +02:00
|
|
|
// Ensure no bad interactions with GL_UNPACK_ALIGNMENT, which by default
|
|
|
|
// only allows rows to have a memory alignement of 4.
|
2014-12-06 02:53:49 +01:00
|
|
|
_dbg_assert_(Render_OpenGL, pixel_stride % 4 == 0);
|
2014-08-28 20:21:54 +02:00
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture.handle);
|
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, (GLint)pixel_stride);
|
|
|
|
|
|
|
|
// Update existing texture
|
|
|
|
// TODO: Test what happens on hardware when you change the framebuffer dimensions so that they
|
|
|
|
// differ from the LCD resolution.
|
|
|
|
// TODO: Applications could theoretically crash Citra here by specifying too large
|
|
|
|
// framebuffer sizes. We should make sure that this cannot happen.
|
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, framebuffer.width, framebuffer.height,
|
|
|
|
GL_BGR, GL_UNSIGNED_BYTE, framebuffer_data);
|
|
|
|
|
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
|
2014-04-07 06:52:34 +02:00
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
2014-08-21 09:27:53 +02:00
|
|
|
}
|
2014-04-06 22:55:39 +02:00
|
|
|
|
2014-08-28 11:34:47 +02:00
|
|
|
/**
|
|
|
|
* Initializes the OpenGL state and creates persistent objects.
|
|
|
|
*/
|
|
|
|
void RendererOpenGL::InitOpenGLObjects() {
|
|
|
|
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
|
|
2014-08-28 20:21:54 +02:00
|
|
|
// Link shaders and get variable locations
|
2014-08-21 09:27:53 +02:00
|
|
|
program_id = ShaderUtil::LoadShaders(GLShaders::g_vertex_shader, GLShaders::g_fragment_shader);
|
2014-08-28 20:21:54 +02:00
|
|
|
uniform_modelview_matrix = glGetUniformLocation(program_id, "modelview_matrix");
|
|
|
|
uniform_color_texture = glGetUniformLocation(program_id, "color_texture");
|
|
|
|
attrib_position = glGetAttribLocation(program_id, "vert_position");
|
|
|
|
attrib_tex_coord = glGetAttribLocation(program_id, "vert_tex_coord");
|
|
|
|
|
|
|
|
// Generate VBO handle for drawing
|
|
|
|
glGenBuffers(1, &vertex_buffer_handle);
|
|
|
|
|
|
|
|
// Generate VAO
|
|
|
|
glGenVertexArrays(1, &vertex_array_handle);
|
|
|
|
glBindVertexArray(vertex_array_handle);
|
|
|
|
|
|
|
|
// Attach vertex data to VAO
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_handle);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(ScreenRectVertex) * 4, nullptr, GL_STREAM_DRAW);
|
|
|
|
glVertexAttribPointer(attrib_position, 2, GL_FLOAT, GL_FALSE, sizeof(ScreenRectVertex), (GLvoid*)offsetof(ScreenRectVertex, position));
|
|
|
|
glVertexAttribPointer(attrib_tex_coord, 2, GL_FLOAT, GL_FALSE, sizeof(ScreenRectVertex), (GLvoid*)offsetof(ScreenRectVertex, tex_coord));
|
|
|
|
glEnableVertexAttribArray(attrib_position);
|
|
|
|
glEnableVertexAttribArray(attrib_tex_coord);
|
2014-04-07 06:52:34 +02:00
|
|
|
|
2014-08-28 20:21:54 +02:00
|
|
|
// Allocate textures for each screen
|
|
|
|
for (auto& texture : textures) {
|
|
|
|
glGenTextures(1, &texture.handle);
|
2014-04-07 06:52:34 +02:00
|
|
|
|
2014-08-28 20:21:54 +02:00
|
|
|
// Allocation of storage is deferred until the first frame, when we
|
|
|
|
// know the framebuffer size.
|
2014-04-07 06:52:34 +02:00
|
|
|
|
2014-08-28 20:21:54 +02:00
|
|
|
glBindTexture(GL_TEXTURE_2D, texture.handle);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
|
2014-08-21 09:27:53 +02:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
2014-08-28 20:21:54 +02:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
2014-08-21 09:27:53 +02:00
|
|
|
}
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
2014-04-06 22:55:39 +02:00
|
|
|
}
|
|
|
|
|
2014-08-28 20:21:54 +02:00
|
|
|
/**
|
|
|
|
* Draws a single texture to the emulator window, rotating the texture to correct for the 3DS's LCD rotation.
|
|
|
|
*/
|
|
|
|
void RendererOpenGL::DrawSingleScreenRotated(const TextureInfo& texture, float x, float y, float w, float h) {
|
|
|
|
std::array<ScreenRectVertex, 4> vertices = {
|
|
|
|
ScreenRectVertex(x, y, 1.f, 0.f),
|
|
|
|
ScreenRectVertex(x+w, y, 1.f, 1.f),
|
|
|
|
ScreenRectVertex(x, y+h, 0.f, 0.f),
|
|
|
|
ScreenRectVertex(x+w, y+h, 0.f, 1.f),
|
|
|
|
};
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture.handle);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_handle);
|
|
|
|
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices.data());
|
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
}
|
|
|
|
|
2014-08-28 11:34:47 +02:00
|
|
|
/**
|
|
|
|
* Draws the emulated screens to the emulator window.
|
|
|
|
*/
|
|
|
|
void RendererOpenGL::DrawScreens() {
|
2014-11-13 19:33:29 +01:00
|
|
|
auto viewport_extent = GetViewportExtent();
|
|
|
|
glViewport(viewport_extent.left, viewport_extent.top, viewport_extent.GetWidth(), viewport_extent.GetHeight()); // TODO: Or bottom?
|
2014-08-21 09:27:53 +02:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2014-04-07 06:52:34 +02:00
|
|
|
|
2014-08-21 09:27:53 +02:00
|
|
|
glUseProgram(program_id);
|
2014-04-07 06:52:34 +02:00
|
|
|
|
2014-08-28 20:21:54 +02:00
|
|
|
// Set projection matrix
|
|
|
|
std::array<GLfloat, 3*2> ortho_matrix = MakeOrthographicMatrix((float)resolution_width, (float)resolution_height);
|
|
|
|
glUniformMatrix3x2fv(uniform_modelview_matrix, 1, GL_FALSE, ortho_matrix.data());
|
|
|
|
|
2014-08-21 09:27:53 +02:00
|
|
|
// Bind texture in Texture Unit 0
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
2014-08-28 20:21:54 +02:00
|
|
|
glUniform1i(uniform_color_texture, 0);
|
2014-04-07 06:52:34 +02:00
|
|
|
|
2014-08-28 20:21:54 +02:00
|
|
|
const float max_width = std::max((float)VideoCore::kScreenTopWidth, (float)VideoCore::kScreenBottomWidth);
|
|
|
|
const float top_x = 0.5f * (max_width - VideoCore::kScreenTopWidth);
|
|
|
|
const float bottom_x = 0.5f * (max_width - VideoCore::kScreenBottomWidth);
|
2014-08-21 09:27:53 +02:00
|
|
|
|
2014-08-28 20:21:54 +02:00
|
|
|
DrawSingleScreenRotated(textures[0], top_x, 0,
|
|
|
|
(float)VideoCore::kScreenTopWidth, (float)VideoCore::kScreenTopHeight);
|
|
|
|
DrawSingleScreenRotated(textures[1], bottom_x, (float)VideoCore::kScreenTopHeight,
|
|
|
|
(float)VideoCore::kScreenBottomWidth, (float)VideoCore::kScreenBottomHeight);
|
2014-04-06 22:55:39 +02:00
|
|
|
|
2014-04-09 00:59:02 +02:00
|
|
|
m_current_frame++;
|
2014-04-06 22:55:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Updates the framerate
|
|
|
|
void RendererOpenGL::UpdateFramerate() {
|
|
|
|
}
|
|
|
|
|
2014-07-16 11:24:09 +02:00
|
|
|
/**
|
2014-04-06 22:55:39 +02:00
|
|
|
* Set the emulator window to use for renderer
|
|
|
|
* @param window EmuWindow handle to emulator window to use for rendering
|
|
|
|
*/
|
|
|
|
void RendererOpenGL::SetWindow(EmuWindow* window) {
|
2014-08-21 09:27:53 +02:00
|
|
|
render_window = window;
|
2014-04-06 22:55:39 +02:00
|
|
|
}
|
|
|
|
|
2014-11-13 19:33:29 +01:00
|
|
|
MathUtil::Rectangle<unsigned> RendererOpenGL::GetViewportExtent() {
|
|
|
|
unsigned framebuffer_width;
|
|
|
|
unsigned framebuffer_height;
|
|
|
|
std::tie(framebuffer_width, framebuffer_height) = render_window->GetFramebufferSize();
|
2014-08-30 07:23:12 +02:00
|
|
|
|
2014-11-13 19:33:29 +01:00
|
|
|
float window_aspect_ratio = static_cast<float>(framebuffer_height) / framebuffer_width;
|
2014-08-30 07:23:12 +02:00
|
|
|
float emulation_aspect_ratio = static_cast<float>(resolution_height) / resolution_width;
|
|
|
|
|
2014-11-13 19:33:29 +01:00
|
|
|
MathUtil::Rectangle<unsigned> viewport_extent;
|
2014-08-30 07:23:12 +02:00
|
|
|
if (window_aspect_ratio > emulation_aspect_ratio) {
|
2014-11-19 10:02:05 +01:00
|
|
|
// Window is narrower than the emulation content => apply borders to the top and bottom
|
2014-11-30 09:48:10 +01:00
|
|
|
unsigned viewport_height = std::round(emulation_aspect_ratio * framebuffer_width);
|
2014-11-13 19:33:29 +01:00
|
|
|
viewport_extent.left = 0;
|
|
|
|
viewport_extent.top = (framebuffer_height - viewport_height) / 2;
|
|
|
|
viewport_extent.right = viewport_extent.left + framebuffer_width;
|
|
|
|
viewport_extent.bottom = viewport_extent.top + viewport_height;
|
2014-08-30 07:23:12 +02:00
|
|
|
} else {
|
2014-11-13 19:33:29 +01:00
|
|
|
// Otherwise, apply borders to the left and right sides of the window.
|
2014-11-30 09:48:10 +01:00
|
|
|
unsigned viewport_width = std::round(framebuffer_height / emulation_aspect_ratio);
|
2014-11-13 19:33:29 +01:00
|
|
|
viewport_extent.left = (framebuffer_width - viewport_width) / 2;
|
|
|
|
viewport_extent.top = 0;
|
|
|
|
viewport_extent.right = viewport_extent.left + viewport_width;
|
|
|
|
viewport_extent.bottom = viewport_extent.top + framebuffer_height;
|
2014-08-30 07:23:12 +02:00
|
|
|
}
|
2014-11-13 19:33:29 +01:00
|
|
|
|
|
|
|
return viewport_extent;
|
2014-08-30 07:23:12 +02:00
|
|
|
}
|
|
|
|
|
2014-04-06 22:55:39 +02:00
|
|
|
/// Initialize the renderer
|
|
|
|
void RendererOpenGL::Init() {
|
2014-08-21 09:27:53 +02:00
|
|
|
render_window->MakeCurrent();
|
2014-04-06 22:55:39 +02:00
|
|
|
|
2014-08-24 02:00:08 +02:00
|
|
|
int err = ogl_LoadFunctions();
|
|
|
|
if (ogl_LOAD_SUCCEEDED != err) {
|
2014-12-06 02:53:49 +01:00
|
|
|
LOG_CRITICAL(Render_OpenGL, "Failed to initialize GL functions! Exiting...");
|
2014-04-06 22:55:39 +02:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2014-12-06 02:53:49 +01:00
|
|
|
LOG_INFO(Render_OpenGL, "GL_VERSION: %s", glGetString(GL_VERSION));
|
2014-08-28 11:34:47 +02:00
|
|
|
InitOpenGLObjects();
|
2014-04-06 22:55:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Shutdown the renderer
|
|
|
|
void RendererOpenGL::ShutDown() {
|
|
|
|
}
|