mirror of
https://github.com/citra-emu/citra.git
synced 2024-11-27 07:50:05 +00:00
Rework OpenGL renderer.
The OpenGL renderer has been revised, with the following changes: - Initialization and rendering have been refactored to reduce the number of redundant objects used. - Framebuffer rotation is now done directly, using texture mapping. - Attempt to support the LCD framebuffer size registers, under the assumption that the 3DS hardware does hardware scaling in case they don't match the physical LCD resolution. - Vertex coordinates are now given in pixels, and the projection matrix isn't hardcoded anymore.
This commit is contained in:
parent
d90252f92d
commit
f1f381aec6
@ -6,34 +6,32 @@
|
|||||||
|
|
||||||
namespace GLShaders {
|
namespace GLShaders {
|
||||||
|
|
||||||
static const char g_vertex_shader[] = R"(
|
const char g_vertex_shader[] = R"(
|
||||||
#version 150 core
|
#version 150 core
|
||||||
in vec3 position;
|
|
||||||
in vec2 texCoord;
|
|
||||||
|
|
||||||
out vec2 UV;
|
in vec2 vert_position;
|
||||||
|
in vec2 vert_tex_coord;
|
||||||
|
out vec2 frag_tex_coord;
|
||||||
|
|
||||||
mat3 window_scale = mat3(
|
uniform mat3x2 proj_matrix;
|
||||||
vec3(1.0, 0.0, 0.0),
|
|
||||||
vec3(0.0, 5.0/6.0, 0.0), // TODO(princesspeachum): replace hard-coded aspect with uniform
|
|
||||||
vec3(0.0, 0.0, 1.0)
|
|
||||||
);
|
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
gl_Position.xyz = window_scale * position;
|
gl_Position = vec4(mat2(proj_matrix) * vert_position + proj_matrix[2], 0.0, 1.0);
|
||||||
gl_Position.w = 1.0;
|
frag_tex_coord = vert_tex_coord;
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
|
||||||
UV = texCoord;
|
const char g_fragment_shader[] = R"(
|
||||||
})";
|
|
||||||
|
|
||||||
static const char g_fragment_shader[] = R"(
|
|
||||||
#version 150 core
|
#version 150 core
|
||||||
in vec2 UV;
|
|
||||||
out vec3 color;
|
in vec2 frag_tex_coord;
|
||||||
uniform sampler2D sampler;
|
out vec4 color;
|
||||||
|
|
||||||
|
uniform sampler2D color_texture;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
color = texture(sampler, UV).rgb;
|
color = texture(color_texture, frag_tex_coord);
|
||||||
})";
|
}
|
||||||
|
)";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,65 +2,37 @@
|
|||||||
// Licensed under GPLv2
|
// Licensed under GPLv2
|
||||||
// Refer to the license.txt file included.
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
#include "core/hw/gpu.h"
|
#include <GL/glew.h>
|
||||||
|
|
||||||
|
#include "core/hw/gpu.h"
|
||||||
|
#include "core/mem_map.h"
|
||||||
|
#include "common/emu_window.h"
|
||||||
#include "video_core/video_core.h"
|
#include "video_core/video_core.h"
|
||||||
#include "video_core/renderer_opengl/renderer_opengl.h"
|
#include "video_core/renderer_opengl/renderer_opengl.h"
|
||||||
#include "video_core/renderer_opengl/gl_shader_util.h"
|
#include "video_core/renderer_opengl/gl_shader_util.h"
|
||||||
#include "video_core/renderer_opengl/gl_shaders.h"
|
#include "video_core/renderer_opengl/gl_shaders.h"
|
||||||
|
|
||||||
#include "core/mem_map.h"
|
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
static const GLfloat kViewportAspectRatio =
|
struct GLVertex {
|
||||||
(static_cast<float>(VideoCore::kScreenTopHeight) + VideoCore::kScreenBottomHeight) / VideoCore::kScreenTopWidth;
|
GLfloat position[2];
|
||||||
|
GLfloat tex_coord[2];
|
||||||
// Fullscreen quad dimensions
|
|
||||||
static const GLfloat kTopScreenWidthNormalized = 2;
|
|
||||||
static const GLfloat kTopScreenHeightNormalized = kTopScreenWidthNormalized * (static_cast<float>(VideoCore::kScreenTopHeight) / VideoCore::kScreenTopWidth);
|
|
||||||
static const GLfloat kBottomScreenWidthNormalized = kTopScreenWidthNormalized * (static_cast<float>(VideoCore::kScreenBottomWidth) / VideoCore::kScreenTopWidth);
|
|
||||||
static const GLfloat kBottomScreenHeightNormalized = kBottomScreenWidthNormalized * (static_cast<float>(VideoCore::kScreenBottomHeight) / VideoCore::kScreenBottomWidth);
|
|
||||||
|
|
||||||
static const GLfloat g_vbuffer_top[] = {
|
|
||||||
// x, y, z u, v
|
|
||||||
-1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
|
|
||||||
1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
|
|
||||||
1.0f, kTopScreenHeightNormalized, 0.0f, 1.0f, 0.0f,
|
|
||||||
1.0f, kTopScreenHeightNormalized, 0.0f, 1.0f, 0.0f,
|
|
||||||
-1.0f, kTopScreenHeightNormalized, 0.0f, 0.0f, 0.0f,
|
|
||||||
-1.0f, 0.0f, 0.0f, 0.0f, 1.0f
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static const GLfloat g_vbuffer_bottom[] = {
|
/**
|
||||||
// x, y, z u, v
|
* Defines a 1:1 pixel ortographic projection matrix with (0,0) on the top-left
|
||||||
-(kBottomScreenWidthNormalized / 2), -kBottomScreenHeightNormalized, 0.0f, 0.0f, 1.0f,
|
* corner and (width, height) on the lower-bottom.
|
||||||
(kBottomScreenWidthNormalized / 2), -kBottomScreenHeightNormalized, 0.0f, 1.0f, 1.0f,
|
*/
|
||||||
(kBottomScreenWidthNormalized / 2), 0.0f, 0.0f, 1.0f, 0.0f,
|
static void write_orthographic_matrix(std::array<GLfloat, 3*2>& m, float width, float height) {
|
||||||
(kBottomScreenWidthNormalized / 2), 0.0f, 0.0f, 1.0f, 0.0f,
|
m[0] = 2.f / width; m[2] = 0.f; m[4] = -1.f;
|
||||||
-(kBottomScreenWidthNormalized / 2), 0.0f, 0.0f, 0.0f, 0.0f,
|
m[1] = 0.f; m[3] = -2.f / height; m[5] = 1.f;
|
||||||
-(kBottomScreenWidthNormalized / 2), -kBottomScreenHeightNormalized, 0.0f, 0.0f, 1.0f
|
}
|
||||||
};
|
|
||||||
|
|
||||||
/// RendererOpenGL constructor
|
/// RendererOpenGL constructor
|
||||||
RendererOpenGL::RendererOpenGL() {
|
RendererOpenGL::RendererOpenGL() {
|
||||||
|
|
||||||
resolution_width = std::max(VideoCore::kScreenTopWidth, VideoCore::kScreenBottomWidth);
|
resolution_width = std::max(VideoCore::kScreenTopWidth, VideoCore::kScreenBottomWidth);
|
||||||
resolution_height = VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight;
|
resolution_height = VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight;
|
||||||
|
|
||||||
// Initialize screen info
|
|
||||||
const auto& framebuffer_top = GPU::g_regs.framebuffer_config[0];
|
|
||||||
const auto& framebuffer_sub = GPU::g_regs.framebuffer_config[1];
|
|
||||||
|
|
||||||
screen_info.Top().width = VideoCore::kScreenTopWidth;
|
|
||||||
screen_info.Top().height = VideoCore::kScreenTopHeight;
|
|
||||||
screen_info.Top().stride = framebuffer_top.stride;
|
|
||||||
screen_info.Top().flipped_xfb_data = xfb_top_flipped;
|
|
||||||
|
|
||||||
screen_info.Bottom().width = VideoCore::kScreenBottomWidth;
|
|
||||||
screen_info.Bottom().height = VideoCore::kScreenBottomHeight;
|
|
||||||
screen_info.Bottom().stride = framebuffer_sub.stride;
|
|
||||||
screen_info.Bottom().flipped_xfb_data = xfb_bottom_flipped;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// RendererOpenGL destructor
|
/// RendererOpenGL destructor
|
||||||
@ -74,13 +46,8 @@ void RendererOpenGL::SwapBuffers() {
|
|||||||
// EFB->XFB copy
|
// EFB->XFB copy
|
||||||
// TODO(bunnei): This is a hack and does not belong here. The copy should be triggered by some
|
// TODO(bunnei): This is a hack and does not belong here. The copy should be triggered by some
|
||||||
// register write.
|
// register write.
|
||||||
//
|
RenderXFB(textures[0], GPU::g_regs.framebuffer_config[0]);
|
||||||
// TODO(princesspeachum): (related to above^) this should only be called when there's new data, not every frame.
|
RenderXFB(textures[1], GPU::g_regs.framebuffer_config[1]);
|
||||||
// Currently this uploads data that shouldn't have changed.
|
|
||||||
common::Rect framebuffer_size(0, 0, resolution_width, resolution_height);
|
|
||||||
RenderXFB(framebuffer_size, framebuffer_size);
|
|
||||||
|
|
||||||
// XFB->Window copy
|
|
||||||
DrawScreens();
|
DrawScreens();
|
||||||
|
|
||||||
// Swap buffers
|
// Swap buffers
|
||||||
@ -89,115 +56,117 @@ void RendererOpenGL::SwapBuffers() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function to flip framebuffer from left-to-right to top-to-bottom
|
* Resolves a framebuffer from emulated video memory to a OpenGL texture.
|
||||||
* @param raw_data Pointer to input raw framebuffer in V/RAM
|
|
||||||
* @param screen_info ScreenInfo structure with screen size and output buffer pointer
|
|
||||||
* @todo Early on hack... I'd like to find a more efficient way of doing this /bunnei
|
|
||||||
*/
|
*/
|
||||||
void RendererOpenGL::FlipFramebuffer(const u8* raw_data, ScreenInfo& screen_info) {
|
void RendererOpenGL::RenderXFB(TextureInfo& texture, const GPU::Regs::FramebufferConfig& framebuffer) {
|
||||||
for (int x = 0; x < screen_info.width; x++) {
|
const VAddr framebuffer_vaddr = Memory::PhysicalToVirtualAddress(
|
||||||
int in_coord = x * screen_info.stride;
|
framebuffer.active_fb == 1 ? framebuffer.address_left2 : framebuffer.address_left1);
|
||||||
for (int y = screen_info.height-1; y >= 0; y--) {
|
|
||||||
// TODO: Properly support other framebuffer formats
|
|
||||||
int out_coord = (x + y * screen_info.width) * 3;
|
|
||||||
screen_info.flipped_xfb_data[out_coord] = raw_data[in_coord + 2]; // Red
|
|
||||||
screen_info.flipped_xfb_data[out_coord + 1] = raw_data[in_coord + 1]; // Green
|
|
||||||
screen_info.flipped_xfb_data[out_coord + 2] = raw_data[in_coord]; // Blue
|
|
||||||
in_coord += 3;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Renders external framebuffer (XFB)
|
|
||||||
* @param src_rect Source rectangle in XFB to copy
|
|
||||||
* @param dst_rect Destination rectangle in output framebuffer to copy to
|
|
||||||
*/
|
|
||||||
void RendererOpenGL::RenderXFB(const common::Rect& src_rect, const common::Rect& dst_rect) {
|
|
||||||
const auto& framebuffer_top = GPU::g_regs.framebuffer_config[0];
|
|
||||||
const auto& framebuffer_sub = GPU::g_regs.framebuffer_config[1];
|
|
||||||
const u32 active_fb_top = (framebuffer_top.active_fb == 1)
|
|
||||||
? Memory::PhysicalToVirtualAddress(framebuffer_top.address_left2)
|
|
||||||
: Memory::PhysicalToVirtualAddress(framebuffer_top.address_left1);
|
|
||||||
const u32 active_fb_sub = (framebuffer_sub.active_fb == 1)
|
|
||||||
? Memory::PhysicalToVirtualAddress(framebuffer_sub.address_left2)
|
|
||||||
: Memory::PhysicalToVirtualAddress(framebuffer_sub.address_left1);
|
|
||||||
|
|
||||||
DEBUG_LOG(GPU, "RenderXFB: 0x%08x bytes from 0x%08x(%dx%d), fmt %x",
|
DEBUG_LOG(GPU, "RenderXFB: 0x%08x bytes from 0x%08x(%dx%d), fmt %x",
|
||||||
framebuffer_top.stride * framebuffer_top.height,
|
framebuffer.stride * framebuffer.height,
|
||||||
active_fb_top, (int)framebuffer_top.width,
|
framebuffer_vaddr, (int)framebuffer.width,
|
||||||
(int)framebuffer_top.height, (int)framebuffer_top.format);
|
(int)framebuffer.height, (int)framebuffer.format);
|
||||||
|
|
||||||
FlipFramebuffer(Memory::GetPointer(active_fb_top), screen_info.Top());
|
const u8* framebuffer_data = Memory::GetPointer(framebuffer_vaddr);
|
||||||
FlipFramebuffer(Memory::GetPointer(active_fb_sub), screen_info.Bottom());
|
|
||||||
|
|
||||||
for (int i = 0; i < 2; i++) {
|
// TODO: Handle other pixel formats
|
||||||
ScreenInfo* current_screen = &screen_info[i];
|
assert(framebuffer.color_format == GPU::Regs::PixelFormat::RGB8);
|
||||||
|
|
||||||
glBindTexture(GL_TEXTURE_2D, current_screen->texture_id);
|
size_t pixel_stride = framebuffer.stride / 3;
|
||||||
|
// OpenGL only supports specifying a stride in units of pixels, unfortunately
|
||||||
|
assert(pixel_stride * 3 == framebuffer.stride);
|
||||||
|
// Ensure no bad interactions with GL_UNPACK_ALIGNMENT
|
||||||
|
assert(pixel_stride % 4 == 0);
|
||||||
|
|
||||||
// TODO: This should consider the GPU registers for framebuffer width, height and stride.
|
glBindTexture(GL_TEXTURE_2D, texture.handle);
|
||||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, current_screen->width, current_screen->height,
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, (GLint)pixel_stride);
|
||||||
GL_RGB, GL_UNSIGNED_BYTE, current_screen->flipped_xfb_data);
|
|
||||||
|
// TODO: Testing needs to be done on hardware to see if scaling is really
|
||||||
|
// what happens if you change the framebuffer dimensions so that they
|
||||||
|
// differ from the LCD resolution.
|
||||||
|
// TODO: Prevent memory overread
|
||||||
|
if (texture.width != framebuffer.width || texture.height != framebuffer.height) {
|
||||||
|
// If the framebuffer size was changed, re-allocated the texture. The
|
||||||
|
// expectation is that this won't happen very often.
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, framebuffer.width, framebuffer.height, 0,
|
||||||
|
GL_BGR, GL_UNSIGNED_BYTE, framebuffer_data);
|
||||||
|
texture.width = framebuffer.width;
|
||||||
|
texture.height = framebuffer.height;
|
||||||
|
} else {
|
||||||
|
// Else just update the existing texture
|
||||||
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, framebuffer.width, framebuffer.height,
|
||||||
|
GL_BGR, GL_UNSIGNED_BYTE, framebuffer_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
|
||||||
|
|
||||||
// TODO(princesspeachum):
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
// Only the subset src_rect of the GPU buffer
|
|
||||||
// should be copied into the texture of the relevant screen.
|
|
||||||
//
|
|
||||||
// The method's parameters also only include src_rect and dest_rec for one screen,
|
|
||||||
// so this may need to be changed (pair for each screen).
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the OpenGL state and creates persistent objects.
|
* Initializes the OpenGL state and creates persistent objects.
|
||||||
*/
|
*/
|
||||||
void RendererOpenGL::InitOpenGLObjects() {
|
void RendererOpenGL::InitOpenGLObjects() {
|
||||||
glGenVertexArrays(1, &vertex_array_id);
|
|
||||||
glBindVertexArray(vertex_array_id);
|
|
||||||
|
|
||||||
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
|
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
|
||||||
glDisable(GL_DEPTH_TEST);
|
glDisable(GL_DEPTH_TEST);
|
||||||
|
//glDisable(GL_CULL_FACE);
|
||||||
|
|
||||||
|
// Links shaders and get variable locations
|
||||||
program_id = ShaderUtil::LoadShaders(GLShaders::g_vertex_shader, GLShaders::g_fragment_shader);
|
program_id = ShaderUtil::LoadShaders(GLShaders::g_vertex_shader, GLShaders::g_fragment_shader);
|
||||||
sampler_id = glGetUniformLocation(program_id, "sampler");
|
uniform_proj_matrix = glGetUniformLocation(program_id, "proj_matrix");
|
||||||
attrib_position = glGetAttribLocation(program_id, "position");
|
uniform_color_texture = glGetUniformLocation(program_id, "color_texture");
|
||||||
attrib_texcoord = glGetAttribLocation(program_id, "texCoord");
|
attrib_position = glGetAttribLocation(program_id, "vert_position");
|
||||||
|
attrib_tex_coord = glGetAttribLocation(program_id, "vert_tex_coord");
|
||||||
|
|
||||||
// Generate vertex buffers for both screens
|
// Generate VBO handle for drawing
|
||||||
glGenBuffers(1, &screen_info.Top().vertex_buffer_id);
|
glGenBuffers(1, &vertex_buffer_handle);
|
||||||
glGenBuffers(1, &screen_info.Bottom().vertex_buffer_id);
|
|
||||||
|
|
||||||
// Attach vertex data for top screen
|
// Generate VAO
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, screen_info.Top().vertex_buffer_id);
|
glGenVertexArrays(1, &vertex_array_handle);
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vbuffer_top), g_vbuffer_top, GL_STATIC_DRAW);
|
glBindVertexArray(vertex_array_handle);
|
||||||
|
|
||||||
// Attach vertex data for bottom screen
|
// Attach vertex data to VAO
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, screen_info.Bottom().vertex_buffer_id);
|
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_handle);
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vbuffer_bottom), g_vbuffer_bottom, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, sizeof(GLVertex) * 4, nullptr, GL_STREAM_DRAW);
|
||||||
|
glVertexAttribPointer(attrib_position, 2, GL_FLOAT, GL_FALSE, sizeof(GLVertex), (GLvoid*)offsetof(GLVertex, position));
|
||||||
|
glVertexAttribPointer(attrib_tex_coord, 2, GL_FLOAT, GL_FALSE, sizeof(GLVertex), (GLvoid*)offsetof(GLVertex, tex_coord));
|
||||||
|
glEnableVertexAttribArray(attrib_position);
|
||||||
|
glEnableVertexAttribArray(attrib_tex_coord);
|
||||||
|
|
||||||
// Create color buffers for both screens
|
// Allocate textures for each screen
|
||||||
glGenTextures(1, &screen_info.Top().texture_id);
|
for (auto& texture : textures) {
|
||||||
glGenTextures(1, &screen_info.Bottom().texture_id);
|
glGenTextures(1, &texture.handle);
|
||||||
|
|
||||||
for (int i = 0; i < 2; i++) {
|
// Allocation of storage is deferred until the first frame, when we
|
||||||
|
// know the framebuffer size.
|
||||||
ScreenInfo* current_screen = &screen_info[i];
|
|
||||||
|
|
||||||
// Allocate texture
|
|
||||||
glBindTexture(GL_TEXTURE_2D, current_screen->vertex_buffer_id);
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, current_screen->width, current_screen->height,
|
|
||||||
0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
|
|
||||||
|
|
||||||
|
glBindTexture(GL_TEXTURE_2D, texture.handle);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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<GLVertex, 4> vertices = {{
|
||||||
|
{{x, y}, {1.f, 0.f}},
|
||||||
|
{{x+w, y}, {1.f, 1.f}},
|
||||||
|
{{x, y+h}, {0.f, 0.f}},
|
||||||
|
{{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);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Draws the emulated screens to the emulator window.
|
* Draws the emulated screens to the emulator window.
|
||||||
*/
|
*/
|
||||||
@ -207,37 +176,23 @@ void RendererOpenGL::DrawScreens() {
|
|||||||
|
|
||||||
glUseProgram(program_id);
|
glUseProgram(program_id);
|
||||||
|
|
||||||
|
// Set projection matrix
|
||||||
|
std::array<GLfloat, 3 * 2> ortho_matrix;
|
||||||
|
write_orthographic_matrix(ortho_matrix, (float)resolution_width, (float)resolution_height);
|
||||||
|
glUniformMatrix3x2fv(uniform_proj_matrix, 1, GL_FALSE, ortho_matrix.data());
|
||||||
|
|
||||||
// Bind texture in Texture Unit 0
|
// Bind texture in Texture Unit 0
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
|
glUniform1i(uniform_color_texture, 0);
|
||||||
|
|
||||||
glEnableVertexAttribArray(attrib_position);
|
const float max_width = std::max((float)VideoCore::kScreenTopWidth, (float)VideoCore::kScreenBottomWidth);
|
||||||
glEnableVertexAttribArray(attrib_texcoord);
|
const float top_x = 0.5f * (max_width - VideoCore::kScreenTopWidth);
|
||||||
|
const float bottom_x = 0.5f * (max_width - VideoCore::kScreenBottomWidth);
|
||||||
|
|
||||||
for (int i = 0; i < 2; i++) {
|
DrawSingleScreenRotated(textures[0], top_x, 0,
|
||||||
|
(float)VideoCore::kScreenTopWidth, (float)VideoCore::kScreenTopHeight);
|
||||||
ScreenInfo* current_screen = &screen_info[i];
|
DrawSingleScreenRotated(textures[1], bottom_x, (float)VideoCore::kScreenTopHeight,
|
||||||
|
(float)VideoCore::kScreenBottomWidth, (float)VideoCore::kScreenBottomHeight);
|
||||||
glBindTexture(GL_TEXTURE_2D, current_screen->texture_id);
|
|
||||||
|
|
||||||
// Set sampler on Texture Unit 0
|
|
||||||
glUniform1i(sampler_id, 0);
|
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, current_screen->vertex_buffer_id);
|
|
||||||
|
|
||||||
// Vertex buffer layout
|
|
||||||
const GLsizei stride = 5 * sizeof(GLfloat);
|
|
||||||
const GLvoid* uv_offset = (const GLvoid*)(3 * sizeof(GLfloat));
|
|
||||||
|
|
||||||
// Configure vertex buffer
|
|
||||||
glVertexAttribPointer(attrib_position, 3, GL_FLOAT, GL_FALSE, stride, NULL);
|
|
||||||
glVertexAttribPointer(attrib_texcoord, 2, GL_FLOAT, GL_FALSE, stride, uv_offset);
|
|
||||||
|
|
||||||
// Draw screen
|
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
|
||||||
}
|
|
||||||
|
|
||||||
glDisableVertexAttribArray(attrib_position);
|
|
||||||
glDisableVertexAttribArray(attrib_texcoord);
|
|
||||||
|
|
||||||
m_current_frame++;
|
m_current_frame++;
|
||||||
}
|
}
|
||||||
|
@ -5,14 +5,14 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
|
|
||||||
#include "common/common.h"
|
#include "common/common.h"
|
||||||
#include "common/emu_window.h"
|
#include "core/hw/gpu.h"
|
||||||
|
|
||||||
#include "video_core/renderer_base.h"
|
#include "video_core/renderer_base.h"
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
|
||||||
|
class EmuWindow;
|
||||||
|
|
||||||
class RendererOpenGL : public RendererBase {
|
class RendererOpenGL : public RendererBase {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@ -22,13 +22,6 @@ public:
|
|||||||
/// Swap buffers (render frame)
|
/// Swap buffers (render frame)
|
||||||
void SwapBuffers();
|
void SwapBuffers();
|
||||||
|
|
||||||
/**
|
|
||||||
* Renders external framebuffer (XFB)
|
|
||||||
* @param src_rect Source rectangle in XFB to copy
|
|
||||||
* @param dst_rect Destination rectangle in output framebuffer to copy to
|
|
||||||
*/
|
|
||||||
void RenderXFB(const common::Rect& src_rect, const common::Rect& dst_rect);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the emulator window to use for renderer
|
* Set the emulator window to use for renderer
|
||||||
* @param window EmuWindow handle to emulator window to use for rendering
|
* @param window EmuWindow handle to emulator window to use for rendering
|
||||||
@ -42,32 +35,18 @@ public:
|
|||||||
void ShutDown();
|
void ShutDown();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void InitOpenGLObjects();
|
/// Structure used for storing information about the textures for each 3DS screen
|
||||||
void DrawScreens();
|
struct TextureInfo {
|
||||||
void UpdateFramerate();
|
GLuint handle;
|
||||||
|
GLsizei width;
|
||||||
/// Structure used for storing information for rendering each 3DS screen
|
GLsizei height;
|
||||||
struct ScreenInfo {
|
|
||||||
// Properties
|
|
||||||
int width;
|
|
||||||
int height;
|
|
||||||
int stride; ///< Number of bytes between the coordinates (0,0) and (1,0)
|
|
||||||
|
|
||||||
// OpenGL object IDs
|
|
||||||
GLuint texture_id;
|
|
||||||
GLuint vertex_buffer_id;
|
|
||||||
|
|
||||||
// Temporary
|
|
||||||
u8* flipped_xfb_data;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
void InitOpenGLObjects();
|
||||||
* Helper function to flip framebuffer from left-to-right to top-to-bottom
|
void DrawScreens();
|
||||||
* @param raw_data Pointer to input raw framebuffer in V/RAM
|
void DrawSingleScreenRotated(const TextureInfo& texture, float x, float y, float w, float h);
|
||||||
* @param screen_info ScreenInfo structure with screen size and output buffer pointer
|
void UpdateFramerate();
|
||||||
* @todo Early on hack... I'd like to find a more efficient way of doing this /bunnei
|
static void RenderXFB(TextureInfo& texture, const GPU::Regs::FramebufferConfig& framebuffer);
|
||||||
*/
|
|
||||||
void FlipFramebuffer(const u8* raw_data, ScreenInfo& screen_info);
|
|
||||||
|
|
||||||
EmuWindow* render_window; ///< Handle to render window
|
EmuWindow* render_window; ///< Handle to render window
|
||||||
u32 last_mode; ///< Last render mode
|
u32 last_mode; ///< Last render mode
|
||||||
@ -75,22 +54,15 @@ private:
|
|||||||
int resolution_width; ///< Current resolution width
|
int resolution_width; ///< Current resolution width
|
||||||
int resolution_height; ///< Current resolution height
|
int resolution_height; ///< Current resolution height
|
||||||
|
|
||||||
// OpenGL global object IDs
|
// OpenGL object IDs
|
||||||
GLuint vertex_array_id;
|
GLuint vertex_array_handle;
|
||||||
|
GLuint vertex_buffer_handle;
|
||||||
GLuint program_id;
|
GLuint program_id;
|
||||||
GLuint sampler_id;
|
std::array<TextureInfo, 2> textures;
|
||||||
|
// Shader uniform location indices
|
||||||
|
GLuint uniform_proj_matrix;
|
||||||
|
GLuint uniform_color_texture;
|
||||||
// Shader attribute input indices
|
// Shader attribute input indices
|
||||||
GLuint attrib_position;
|
GLuint attrib_position;
|
||||||
GLuint attrib_texcoord;
|
GLuint attrib_tex_coord;
|
||||||
|
|
||||||
struct : std::array<ScreenInfo, 2> {
|
|
||||||
ScreenInfo& Top() { return (*this)[0]; }
|
|
||||||
ScreenInfo& Bottom() { return (*this)[1]; }
|
|
||||||
} screen_info;
|
|
||||||
|
|
||||||
// "Flipped" framebuffers translate scanlines from native 3DS left-to-right to top-to-bottom
|
|
||||||
// as OpenGL expects them in a texture. There probably is a more efficient way of doing this:
|
|
||||||
u8 xfb_top_flipped[VideoCore::kScreenTopWidth * VideoCore::kScreenTopHeight * 4];
|
|
||||||
u8 xfb_bottom_flipped[VideoCore::kScreenBottomWidth * VideoCore::kScreenBottomHeight * 4];
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -17,6 +17,9 @@ namespace VideoCore {
|
|||||||
// 3DS Video Constants
|
// 3DS Video Constants
|
||||||
// -------------------
|
// -------------------
|
||||||
|
|
||||||
|
// NOTE: The LCDs actually rotate the image 90 degrees when displaying. Because
|
||||||
|
// of that the framebuffers in video memory are stored in column-major order
|
||||||
|
// and rendered sideways, causing the widths and heights here to be switched.
|
||||||
static const int kScreenTopWidth = 400; ///< 3DS top screen width
|
static const int kScreenTopWidth = 400; ///< 3DS top screen width
|
||||||
static const int kScreenTopHeight = 240; ///< 3DS top screen height
|
static const int kScreenTopHeight = 240; ///< 3DS top screen height
|
||||||
static const int kScreenBottomWidth = 320; ///< 3DS bottom screen width
|
static const int kScreenBottomWidth = 320; ///< 3DS bottom screen width
|
||||||
|
Loading…
Reference in New Issue
Block a user