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-04-17 03:30:10 +02:00
|
|
|
|
2014-04-09 02:15:08 +02:00
|
|
|
#include "video_core/video_core.h"
|
|
|
|
#include "video_core/renderer_opengl/renderer_opengl.h"
|
|
|
|
|
|
|
|
#include "core/mem_map.h"
|
2014-04-06 22:55:39 +02:00
|
|
|
|
2014-04-07 06:52:34 +02:00
|
|
|
|
2014-04-06 22:55:39 +02:00
|
|
|
/// RendererOpenGL constructor
|
|
|
|
RendererOpenGL::RendererOpenGL() {
|
2014-04-09 00:59:02 +02:00
|
|
|
memset(m_fbo, 0, sizeof(m_fbo));
|
|
|
|
memset(m_fbo_rbo, 0, sizeof(m_fbo_rbo));
|
|
|
|
memset(m_fbo_depth_buffers, 0, sizeof(m_fbo_depth_buffers));
|
2014-04-06 22:55:39 +02:00
|
|
|
|
2014-04-09 00:59:02 +02:00
|
|
|
m_resolution_width = max(VideoCore::kScreenTopWidth, VideoCore::kScreenBottomWidth);
|
|
|
|
m_resolution_height = VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight;
|
2014-04-07 06:52:34 +02:00
|
|
|
|
2014-04-09 00:59:02 +02:00
|
|
|
m_xfb_texture_top = 0;
|
|
|
|
m_xfb_texture_bottom = 0;
|
2014-04-07 06:52:34 +02:00
|
|
|
|
2014-04-09 00:59:02 +02:00
|
|
|
m_xfb_top = 0;
|
|
|
|
m_xfb_bottom = 0;
|
2014-04-06 22:55:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// RendererOpenGL destructor
|
|
|
|
RendererOpenGL::~RendererOpenGL() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Swap buffers (render frame)
|
|
|
|
void RendererOpenGL::SwapBuffers() {
|
2014-04-22 04:00:32 +02:00
|
|
|
m_render_window->MakeCurrent();
|
|
|
|
|
2014-04-06 22:55:39 +02:00
|
|
|
// EFB->XFB copy
|
|
|
|
// TODO(bunnei): This is a hack and does not belong here. The copy should be triggered by some
|
|
|
|
// register write We're also treating both framebuffers as a single one in OpenGL.
|
2014-05-20 02:57:35 +02:00
|
|
|
common::Rect framebuffer_size(0, 0, m_resolution_width, m_resolution_height);
|
2014-04-07 06:52:34 +02:00
|
|
|
RenderXFB(framebuffer_size, framebuffer_size);
|
2014-04-06 22:55:39 +02:00
|
|
|
|
|
|
|
// XFB->Window copy
|
|
|
|
RenderFramebuffer();
|
|
|
|
|
|
|
|
// Swap buffers
|
2014-04-09 00:59:02 +02:00
|
|
|
m_render_window->PollEvents();
|
|
|
|
m_render_window->SwapBuffers();
|
2014-04-06 22:55:39 +02:00
|
|
|
|
|
|
|
// Switch back to EFB and clear
|
2014-04-09 00:59:02 +02:00
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo[kFramebuffer_EFB]);
|
|
|
|
}
|
2014-04-06 22:55:39 +02:00
|
|
|
|
2014-04-09 00:59:02 +02:00
|
|
|
/**
|
|
|
|
* Helper function to flip framebuffer from left-to-right to top-to-bottom
|
2014-04-27 18:42:01 +02:00
|
|
|
* @param in Pointer to input raw framebuffer in V/RAM
|
2014-04-09 00:59:02 +02:00
|
|
|
* @param out Pointer to output buffer with flipped framebuffer
|
|
|
|
* @todo Early on hack... I'd like to find a more efficient way of doing this /bunnei
|
|
|
|
*/
|
2014-04-27 18:42:01 +02:00
|
|
|
void RendererOpenGL::FlipFramebuffer(const u8* in, u8* out) {
|
2014-05-08 18:05:30 +02:00
|
|
|
int in_coord = 0;
|
|
|
|
for (int x = 0; x < VideoCore::kScreenTopWidth; x++) {
|
|
|
|
for (int y = VideoCore::kScreenTopHeight-1; y >= 0; y--) {
|
2014-07-16 09:19:42 +02:00
|
|
|
// TODO: Properly support other framebuffer formats
|
2014-05-08 18:05:30 +02:00
|
|
|
int out_coord = (x + y * VideoCore::kScreenTopWidth) * 3;
|
2014-07-16 09:19:42 +02:00
|
|
|
out[out_coord] = in[in_coord]; // blue?
|
|
|
|
out[out_coord + 1] = in[in_coord + 1]; // green?
|
|
|
|
out[out_coord + 2] = in[in_coord + 2]; // red?
|
2014-05-08 18:05:30 +02:00
|
|
|
in_coord+=3;
|
2014-04-09 00:59:02 +02:00
|
|
|
}
|
|
|
|
}
|
2014-04-06 22:55:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-07 06:52:34 +02:00
|
|
|
* Renders external framebuffer (XFB)
|
|
|
|
* @param src_rect Source rectangle in XFB to copy
|
|
|
|
* @param dst_rect Destination rectangle in output framebuffer to copy to
|
2014-04-06 22:55:39 +02:00
|
|
|
*/
|
2014-05-20 02:57:35 +02:00
|
|
|
void RendererOpenGL::RenderXFB(const common::Rect& src_rect, const common::Rect& dst_rect) {
|
2014-04-07 06:52:34 +02:00
|
|
|
|
2014-07-16 09:19:42 +02:00
|
|
|
DEBUG_LOG(GPU, "RenderXFB: %x bytes from %x(%xx%x), fmt %x",
|
|
|
|
GPU::g_regs.top_framebuffer.stride * GPU::g_regs.top_framebuffer.height,
|
|
|
|
GPU::GetFramebufferAddr(GPU::g_regs.framebuffer_top_left_1), (int)GPU::g_regs.top_framebuffer.width,
|
|
|
|
(int)GPU::g_regs.top_framebuffer.height, (int)GPU::g_regs.top_framebuffer.format);
|
|
|
|
|
|
|
|
// TODO: This should consider the GPU registers for framebuffer width, height and stride.
|
2014-05-17 22:50:33 +02:00
|
|
|
FlipFramebuffer(GPU::GetFramebufferPointer(GPU::g_regs.framebuffer_top_left_1), m_xfb_top_flipped);
|
|
|
|
FlipFramebuffer(GPU::GetFramebufferPointer(GPU::g_regs.framebuffer_sub_left_1), m_xfb_bottom_flipped);
|
2014-04-06 22:55:39 +02:00
|
|
|
|
2014-04-07 06:52:34 +02:00
|
|
|
// Blit the top framebuffer
|
|
|
|
// ------------------------
|
|
|
|
|
|
|
|
// Update textures with contents of XFB in RAM - top
|
2014-04-09 00:59:02 +02:00
|
|
|
glBindTexture(GL_TEXTURE_2D, m_xfb_texture_top);
|
2014-04-07 06:52:34 +02:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, VideoCore::kScreenTopWidth, VideoCore::kScreenTopHeight,
|
2014-04-17 23:47:34 +02:00
|
|
|
GL_BGR, GL_UNSIGNED_BYTE, m_xfb_top_flipped);
|
2014-04-07 06:52:34 +02:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
2014-04-06 22:55:39 +02:00
|
|
|
// Render target is destination framebuffer
|
2014-04-09 00:59:02 +02:00
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo[kFramebuffer_VirtualXFB]);
|
2014-04-07 06:52:34 +02:00
|
|
|
glViewport(0, 0, VideoCore::kScreenTopWidth, VideoCore::kScreenTopHeight);
|
2014-04-06 22:55:39 +02:00
|
|
|
|
|
|
|
// Render source is our EFB
|
2014-04-09 00:59:02 +02:00
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, m_xfb_top);
|
2014-04-06 22:55:39 +02:00
|
|
|
glReadBuffer(GL_COLOR_ATTACHMENT0);
|
|
|
|
|
|
|
|
// Blit
|
|
|
|
glBlitFramebuffer(src_rect.x0_, src_rect.y0_, src_rect.x1_, src_rect.y1_,
|
|
|
|
dst_rect.x0_, dst_rect.y1_, dst_rect.x1_, dst_rect.y0_,
|
|
|
|
GL_COLOR_BUFFER_BIT, GL_LINEAR);
|
|
|
|
|
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
|
|
|
|
|
2014-04-07 06:52:34 +02:00
|
|
|
// Blit the bottom framebuffer
|
|
|
|
// ---------------------------
|
|
|
|
|
|
|
|
// Update textures with contents of XFB in RAM - bottom
|
2014-04-09 00:59:02 +02:00
|
|
|
glBindTexture(GL_TEXTURE_2D, m_xfb_texture_bottom);
|
2014-04-07 06:52:34 +02:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, VideoCore::kScreenTopWidth, VideoCore::kScreenTopHeight,
|
2014-04-09 00:59:02 +02:00
|
|
|
GL_RGB, GL_UNSIGNED_BYTE, m_xfb_bottom_flipped);
|
2014-04-07 06:52:34 +02:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
|
|
|
// Render target is destination framebuffer
|
2014-04-09 00:59:02 +02:00
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo[kFramebuffer_VirtualXFB]);
|
2014-04-07 06:52:34 +02:00
|
|
|
glViewport(0, 0,
|
|
|
|
VideoCore::kScreenBottomWidth, VideoCore::kScreenBottomHeight);
|
|
|
|
|
|
|
|
// Render source is our EFB
|
2014-04-09 00:59:02 +02:00
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, m_xfb_bottom);
|
2014-04-07 06:52:34 +02:00
|
|
|
glReadBuffer(GL_COLOR_ATTACHMENT0);
|
|
|
|
|
|
|
|
// Blit
|
|
|
|
int offset = (VideoCore::kScreenTopWidth - VideoCore::kScreenBottomWidth) / 2;
|
|
|
|
glBlitFramebuffer(0,0, VideoCore::kScreenBottomWidth, VideoCore::kScreenBottomHeight,
|
|
|
|
offset, VideoCore::kScreenBottomHeight, VideoCore::kScreenBottomWidth + offset, 0,
|
|
|
|
GL_COLOR_BUFFER_BIT, GL_LINEAR);
|
|
|
|
|
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
|
2014-04-06 22:55:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Initialize the FBO
|
|
|
|
void RendererOpenGL::InitFramebuffer() {
|
2014-04-09 00:59:02 +02:00
|
|
|
// TODO(bunnei): This should probably be implemented with the top screen and bottom screen as
|
2014-04-06 22:55:39 +02:00
|
|
|
// separate framebuffers
|
|
|
|
|
|
|
|
// Init the FBOs
|
|
|
|
// -------------
|
|
|
|
|
2014-04-09 00:59:02 +02:00
|
|
|
glGenFramebuffers(kMaxFramebuffers, m_fbo); // Generate primary framebuffer
|
|
|
|
glGenRenderbuffers(kMaxFramebuffers, m_fbo_rbo); // Generate primary RBOs
|
|
|
|
glGenRenderbuffers(kMaxFramebuffers, m_fbo_depth_buffers); // Generate primary depth buffer
|
2014-04-06 22:55:39 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < kMaxFramebuffers; i++) {
|
|
|
|
// Generate color buffer storage
|
2014-04-09 00:59:02 +02:00
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, m_fbo_rbo[i]);
|
2014-04-07 06:52:34 +02:00
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, VideoCore::kScreenTopWidth,
|
|
|
|
VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight);
|
2014-04-06 22:55:39 +02:00
|
|
|
|
|
|
|
// Generate depth buffer storage
|
2014-04-09 00:59:02 +02:00
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, m_fbo_depth_buffers[i]);
|
2014-04-07 06:52:34 +02:00
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT32, VideoCore::kScreenTopWidth,
|
|
|
|
VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight);
|
2014-04-06 22:55:39 +02:00
|
|
|
|
|
|
|
// Attach the buffers
|
2014-04-09 00:59:02 +02:00
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo[i]);
|
2014-04-06 22:55:39 +02:00
|
|
|
glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
|
2014-04-09 00:59:02 +02:00
|
|
|
GL_RENDERBUFFER, m_fbo_depth_buffers[i]);
|
2014-04-06 22:55:39 +02:00
|
|
|
glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
|
2014-04-09 00:59:02 +02:00
|
|
|
GL_RENDERBUFFER, m_fbo_rbo[i]);
|
2014-04-06 22:55:39 +02:00
|
|
|
|
|
|
|
// Check for completeness
|
|
|
|
if (GL_FRAMEBUFFER_COMPLETE == glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER)) {
|
|
|
|
NOTICE_LOG(RENDER, "framebuffer(%d) initialized ok", i);
|
|
|
|
} else {
|
|
|
|
ERROR_LOG(RENDER, "couldn't create OpenGL frame buffer");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0); // Unbind our frame buffer(s)
|
2014-04-07 06:52:34 +02:00
|
|
|
|
|
|
|
// Initialize framebuffer textures
|
|
|
|
// -------------------------------
|
|
|
|
|
|
|
|
// Create XFB textures
|
2014-04-09 00:59:02 +02:00
|
|
|
glGenTextures(1, &m_xfb_texture_top);
|
|
|
|
glGenTextures(1, &m_xfb_texture_bottom);
|
2014-04-07 06:52:34 +02:00
|
|
|
|
|
|
|
// Alocate video memorry for XFB textures
|
2014-04-09 00:59:02 +02:00
|
|
|
glBindTexture(GL_TEXTURE_2D, m_xfb_texture_top);
|
2014-04-07 06:52:34 +02:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, VideoCore::kScreenTopWidth, VideoCore::kScreenTopHeight,
|
|
|
|
0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
2014-04-09 00:59:02 +02:00
|
|
|
glBindTexture(GL_TEXTURE_2D, m_xfb_texture_bottom);
|
2014-04-07 06:52:34 +02:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, VideoCore::kScreenTopWidth, VideoCore::kScreenTopHeight,
|
|
|
|
0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
|
|
|
// Create the FBO and attach color/depth textures
|
2014-04-09 00:59:02 +02:00
|
|
|
glGenFramebuffers(1, &m_xfb_top); // Generate framebuffer
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_xfb_top);
|
2014-04-07 06:52:34 +02:00
|
|
|
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
|
2014-04-09 00:59:02 +02:00
|
|
|
m_xfb_texture_top, 0);
|
2014-04-07 06:52:34 +02:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
|
2014-04-09 00:59:02 +02:00
|
|
|
glGenFramebuffers(1, &m_xfb_bottom); // Generate framebuffer
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_xfb_bottom);
|
2014-04-07 06:52:34 +02:00
|
|
|
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
|
2014-04-09 00:59:02 +02:00
|
|
|
m_xfb_texture_bottom, 0);
|
2014-04-07 06:52:34 +02:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
2014-04-06 22:55:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Blit the FBO to the OpenGL default framebuffer
|
|
|
|
void RendererOpenGL::RenderFramebuffer() {
|
|
|
|
// Render target is default framebuffer
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
2014-04-09 00:59:02 +02:00
|
|
|
glViewport(0, 0, m_resolution_width, m_resolution_height);
|
2014-04-06 22:55:39 +02:00
|
|
|
|
|
|
|
// Render source is our XFB
|
2014-04-09 00:59:02 +02:00
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, m_fbo[kFramebuffer_VirtualXFB]);
|
2014-04-06 22:55:39 +02:00
|
|
|
glReadBuffer(GL_COLOR_ATTACHMENT0);
|
|
|
|
|
|
|
|
// Blit
|
2014-04-09 00:59:02 +02:00
|
|
|
glBlitFramebuffer(0, 0, m_resolution_width, m_resolution_height, 0, 0, m_resolution_width,
|
|
|
|
m_resolution_height, GL_COLOR_BUFFER_BIT, GL_LINEAR);
|
2014-04-06 22:55:39 +02:00
|
|
|
|
|
|
|
// Update the FPS count
|
|
|
|
UpdateFramerate();
|
|
|
|
|
|
|
|
// Rebind EFB
|
2014-04-09 00:59:02 +02:00
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo[kFramebuffer_EFB]);
|
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() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-04-09 00:59:02 +02:00
|
|
|
m_render_window = window;
|
2014-04-06 22:55:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Initialize the renderer
|
|
|
|
void RendererOpenGL::Init() {
|
2014-04-09 00:59:02 +02:00
|
|
|
m_render_window->MakeCurrent();
|
2014-04-06 22:55:39 +02:00
|
|
|
glShadeModel(GL_SMOOTH);
|
|
|
|
|
|
|
|
|
|
|
|
glStencilFunc(GL_ALWAYS, 0, 0);
|
|
|
|
glBlendFunc(GL_ONE, GL_ONE);
|
|
|
|
|
2014-04-09 00:59:02 +02:00
|
|
|
glViewport(0, 0, m_resolution_width, m_resolution_height);
|
2014-04-06 22:55:39 +02:00
|
|
|
|
|
|
|
glClearDepth(1.0f);
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glDisable(GL_LIGHTING);
|
|
|
|
glDepthFunc(GL_LEQUAL);
|
|
|
|
|
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
|
|
|
|
|
|
|
glDisable(GL_STENCIL_TEST);
|
|
|
|
glEnable(GL_SCISSOR_TEST);
|
|
|
|
|
2014-04-09 00:59:02 +02:00
|
|
|
glScissor(0, 0, m_resolution_width, m_resolution_height);
|
2014-04-06 22:55:39 +02:00
|
|
|
glClearDepth(1.0f);
|
|
|
|
|
|
|
|
GLenum err = glewInit();
|
|
|
|
if (GLEW_OK != err) {
|
2014-04-09 00:59:02 +02:00
|
|
|
ERROR_LOG(RENDER, "Failed to initialize GLEW! Error message: \"%s\". Exiting...",
|
2014-04-06 22:55:39 +02:00
|
|
|
glewGetErrorString(err));
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize everything else
|
|
|
|
// --------------------------
|
|
|
|
|
|
|
|
InitFramebuffer();
|
|
|
|
|
|
|
|
NOTICE_LOG(RENDER, "GL_VERSION: %s\n", glGetString(GL_VERSION));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Shutdown the renderer
|
|
|
|
void RendererOpenGL::ShutDown() {
|
|
|
|
}
|