2014-04-09 01:04:25 +02:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 06:38:14 +01:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-04-09 01:04:25 +02:00
|
|
|
// Refer to the license.txt file included.
|
2014-04-06 22:55:39 +02:00
|
|
|
|
2015-06-21 18:47:21 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdlib>
|
2018-01-15 05:51:54 +01:00
|
|
|
#include <cstring>
|
2016-04-30 17:34:51 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <glad/glad.h>
|
2015-06-21 18:47:21 +02:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/logging/log.h"
|
2018-08-31 18:21:34 +02:00
|
|
|
#include "common/telemetry.h"
|
2017-02-19 23:34:47 +01:00
|
|
|
#include "core/core.h"
|
2017-02-21 01:31:59 +01:00
|
|
|
#include "core/core_timing.h"
|
2016-12-23 14:37:40 +01:00
|
|
|
#include "core/frontend/emu_window.h"
|
2019-01-08 05:16:01 +01:00
|
|
|
#include "core/frontend/scope_acquire_window_context.h"
|
2016-04-30 17:34:51 +02:00
|
|
|
#include "core/memory.h"
|
2018-08-30 16:50:54 +02:00
|
|
|
#include "core/perf_stats.h"
|
2016-04-30 17:34:51 +02:00
|
|
|
#include "core/settings.h"
|
2018-08-31 18:21:34 +02:00
|
|
|
#include "core/telemetry_session.h"
|
2016-04-30 17:34:51 +02:00
|
|
|
#include "core/tracer/recorder.h"
|
2018-11-25 03:53:17 +01:00
|
|
|
#include "video_core/morton.h"
|
2018-08-21 01:22:43 +02:00
|
|
|
#include "video_core/renderer_opengl/gl_rasterizer.h"
|
2016-09-21 08:52:38 +02:00
|
|
|
#include "video_core/renderer_opengl/renderer_opengl.h"
|
2015-04-04 12:57:31 +02:00
|
|
|
|
2018-08-21 10:18:27 +02:00
|
|
|
namespace OpenGL {
|
|
|
|
|
2015-10-06 04:33:47 +02:00
|
|
|
static const char vertex_shader[] = R"(
|
|
|
|
#version 150 core
|
|
|
|
|
|
|
|
in vec2 vert_position;
|
|
|
|
in vec2 vert_tex_coord;
|
|
|
|
out vec2 frag_tex_coord;
|
|
|
|
|
|
|
|
// This is a truncated 3x3 matrix for 2D transformations:
|
|
|
|
// The upper-left 2x2 submatrix performs scaling/rotation/mirroring.
|
|
|
|
// The third column performs translation.
|
|
|
|
// The third row could be used for projection, which we don't need in 2D. It hence is assumed to
|
|
|
|
// implicitly be [0, 0, 1]
|
|
|
|
uniform mat3x2 modelview_matrix;
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
// Multiply input position by the rotscale part of the matrix and then manually translate by
|
|
|
|
// the last column. This is equivalent to using a full 3x3 matrix and expanding the vector
|
|
|
|
// to `vec3(vert_position.xy, 1.0)`
|
|
|
|
gl_Position = vec4(mat2(modelview_matrix) * vert_position + modelview_matrix[2], 0.0, 1.0);
|
|
|
|
frag_tex_coord = vert_tex_coord;
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
|
|
|
|
static const char fragment_shader[] = R"(
|
|
|
|
#version 150 core
|
|
|
|
|
|
|
|
in vec2 frag_tex_coord;
|
|
|
|
out vec4 color;
|
|
|
|
|
|
|
|
uniform sampler2D color_texture;
|
|
|
|
|
|
|
|
void main() {
|
2018-01-11 04:43:17 +01:00
|
|
|
// Swap RGBA -> ABGR so we don't have to do this on the CPU. This needs to change if we have to
|
|
|
|
// support more framebuffer pixel formats.
|
2018-04-10 04:02:59 +02:00
|
|
|
color = texture(color_texture, frag_tex_coord);
|
2015-10-06 04:33:47 +02:00
|
|
|
}
|
|
|
|
)";
|
|
|
|
|
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.
|
|
|
|
*/
|
2016-02-04 06:03:20 +01:00
|
|
|
static std::array<GLfloat, 3 * 2> MakeOrthographicMatrix(const float width, const float height) {
|
2016-09-19 03:01:46 +02:00
|
|
|
std::array<GLfloat, 3 * 2> matrix; // Laid out in column-major order
|
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
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;
|
2014-08-28 20:21:54 +02:00
|
|
|
// Last matrix row is implicitly assumed to be [0, 0, 1].
|
2016-09-19 03:01:46 +02:00
|
|
|
// clang-format on
|
2014-08-28 20:21:54 +02:00
|
|
|
|
|
|
|
return matrix;
|
|
|
|
}
|
|
|
|
|
2019-01-15 20:28:42 +01:00
|
|
|
RendererOpenGL::RendererOpenGL(Core::Frontend::EmuWindow& window, Core::System& system)
|
|
|
|
: VideoCore::RendererBase{window}, system{system} {}
|
2018-08-12 02:20:19 +02:00
|
|
|
|
2017-05-28 03:02:46 +02:00
|
|
|
RendererOpenGL::~RendererOpenGL() = default;
|
2014-04-06 22:55:39 +02:00
|
|
|
|
|
|
|
/// Swap buffers (render frame)
|
2018-10-30 05:03:25 +01:00
|
|
|
void RendererOpenGL::SwapBuffers(
|
|
|
|
std::optional<std::reference_wrapper<const Tegra::FramebufferConfig>> framebuffer) {
|
2018-06-29 20:10:16 +02:00
|
|
|
|
2019-02-13 01:33:20 +01:00
|
|
|
system.GetPerfStats().EndSystemFrame();
|
2018-03-27 02:04:35 +02:00
|
|
|
|
2015-05-19 06:21:33 +02:00
|
|
|
// Maintain the rasterizer's state as a priority
|
|
|
|
OpenGLState prev_state = OpenGLState::GetCurState();
|
|
|
|
state.Apply();
|
2014-04-22 04:00:32 +02:00
|
|
|
|
2018-10-30 05:03:25 +01:00
|
|
|
if (framebuffer) {
|
2018-03-23 02:04:30 +01:00
|
|
|
// If framebuffer is provided, reload it from memory to a texture
|
2018-10-30 05:03:25 +01:00
|
|
|
if (screen_info.texture.width != (GLsizei)framebuffer->get().width ||
|
|
|
|
screen_info.texture.height != (GLsizei)framebuffer->get().height ||
|
|
|
|
screen_info.texture.pixel_format != framebuffer->get().pixel_format) {
|
2018-01-15 05:51:54 +01: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.
|
2018-03-23 02:04:30 +01:00
|
|
|
ConfigureFramebufferTexture(screen_info.texture, *framebuffer);
|
2018-01-15 05:51:54 +01:00
|
|
|
}
|
2018-03-27 02:04:35 +02:00
|
|
|
|
|
|
|
// Load the framebuffer from memory, draw it to the screen, and swap buffers
|
2018-08-21 01:34:02 +02:00
|
|
|
LoadFBToScreenInfo(*framebuffer);
|
2018-08-31 08:16:16 +02:00
|
|
|
|
|
|
|
if (renderer_settings.screenshot_requested)
|
|
|
|
CaptureScreenshot();
|
|
|
|
|
|
|
|
DrawScreen(render_window.GetFramebufferLayout());
|
|
|
|
|
2018-08-02 02:59:42 +02:00
|
|
|
render_window.SwapBuffers();
|
2014-08-28 20:21:54 +02:00
|
|
|
}
|
2014-04-06 22:55:39 +02:00
|
|
|
|
2018-08-02 02:59:42 +02:00
|
|
|
render_window.PollEvents();
|
2015-02-05 17:53:25 +01:00
|
|
|
|
2019-02-14 18:42:58 +01:00
|
|
|
system.FrameLimiter().DoFrameLimiting(system.CoreTiming().GetGlobalTimeUs());
|
2019-02-13 01:33:20 +01:00
|
|
|
system.GetPerfStats().BeginSystemFrame();
|
2015-05-19 06:21:33 +02:00
|
|
|
|
2018-03-27 02:04:35 +02:00
|
|
|
// Restore the rasterizer state
|
2017-02-21 01:31:59 +01:00
|
|
|
prev_state.Apply();
|
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
|
|
|
*/
|
2018-08-21 01:34:02 +02:00
|
|
|
void RendererOpenGL::LoadFBToScreenInfo(const Tegra::FramebufferConfig& framebuffer) {
|
2018-03-23 20:49:04 +01:00
|
|
|
const u32 bytes_per_pixel{Tegra::FramebufferConfig::BytesPerPixel(framebuffer.pixel_format)};
|
|
|
|
const u64 size_in_bytes{framebuffer.stride * framebuffer.height * bytes_per_pixel};
|
|
|
|
const VAddr framebuffer_addr{framebuffer.address + framebuffer.offset};
|
|
|
|
|
2018-03-23 19:58:27 +01:00
|
|
|
// Framebuffer orientation handling
|
|
|
|
framebuffer_transform_flags = framebuffer.transform_flags;
|
2018-07-18 02:11:41 +02:00
|
|
|
framebuffer_crop_rect = framebuffer.crop_rect;
|
2018-03-23 19:58:27 +01:00
|
|
|
|
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.
|
2018-03-23 02:04:30 +01:00
|
|
|
ASSERT(framebuffer.stride % 4 == 0);
|
2015-05-25 20:34:09 +02:00
|
|
|
|
2018-08-21 01:34:02 +02:00
|
|
|
if (!rasterizer->AccelerateDisplay(framebuffer, framebuffer_addr, framebuffer.stride)) {
|
2018-03-23 04:28:37 +01:00
|
|
|
// Reset the screen info's display texture to its own permanent texture
|
|
|
|
screen_info.display_texture = screen_info.texture.resource.handle;
|
2018-02-12 01:04:27 +01:00
|
|
|
|
2018-04-24 06:19:36 +02:00
|
|
|
Memory::RasterizerFlushVirtualRegion(framebuffer_addr, size_in_bytes,
|
|
|
|
Memory::FlushMode::Flush);
|
2014-08-28 20:21:54 +02:00
|
|
|
|
2019-03-13 20:32:33 +01:00
|
|
|
constexpr u32 linear_bpp = 4;
|
|
|
|
VideoCore::MortonCopyPixels128(VideoCore::MortonSwizzleMode::MortonToLinear,
|
|
|
|
framebuffer.width, framebuffer.height, bytes_per_pixel,
|
|
|
|
linear_bpp, Memory::GetPointer(framebuffer_addr),
|
|
|
|
gl_framebuffer_data.data());
|
2014-08-28 20:21:54 +02:00
|
|
|
|
2018-03-23 04:28:37 +01:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, static_cast<GLint>(framebuffer.stride));
|
2016-04-17 00:57:57 +02:00
|
|
|
|
2018-03-23 04:28:37 +01:00
|
|
|
// 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 yuzu here by specifying too large
|
|
|
|
// framebuffer sizes. We should make sure that this cannot happen.
|
2019-01-06 22:49:23 +01:00
|
|
|
glTextureSubImage2D(screen_info.texture.resource.handle, 0, 0, 0, framebuffer.width,
|
|
|
|
framebuffer.height, screen_info.texture.gl_format,
|
|
|
|
screen_info.texture.gl_type, gl_framebuffer_data.data());
|
2016-04-17 00:57:57 +02:00
|
|
|
|
2018-03-23 04:28:37 +01:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
|
|
|
|
}
|
2014-10-13 07:40:26 +02:00
|
|
|
}
|
2014-04-07 06:52:34 +02:00
|
|
|
|
2014-10-13 07:40:26 +02:00
|
|
|
/**
|
2016-09-19 03:01:46 +02:00
|
|
|
* Fills active OpenGL texture with the given RGB color. Since the color is solid, the texture can
|
|
|
|
* be 1x1 but will stretch across whatever it's rendered on.
|
2014-10-13 07:40:26 +02:00
|
|
|
*/
|
2018-01-15 06:20:19 +01:00
|
|
|
void RendererOpenGL::LoadColorToActiveGLTexture(u8 color_r, u8 color_g, u8 color_b, u8 color_a,
|
2014-10-13 07:40:26 +02:00
|
|
|
const TextureInfo& texture) {
|
2019-01-06 22:49:23 +01:00
|
|
|
const u8 framebuffer_data[4] = {color_a, color_b, color_g, color_r};
|
|
|
|
glClearTexImage(texture.resource.handle, 0, GL_RGBA, GL_UNSIGNED_BYTE, framebuffer_data);
|
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() {
|
2016-09-18 02:38:01 +02:00
|
|
|
glClearColor(Settings::values.bg_red, Settings::values.bg_green, Settings::values.bg_blue,
|
|
|
|
0.0f);
|
2014-08-28 11:34:47 +02:00
|
|
|
|
2014-08-28 20:21:54 +02:00
|
|
|
// Link shaders and get variable locations
|
2018-04-07 05:56:42 +02:00
|
|
|
shader.CreateFromSource(vertex_shader, nullptr, fragment_shader);
|
2016-04-17 00:57:57 +02:00
|
|
|
state.draw.shader_program = shader.handle;
|
|
|
|
state.Apply();
|
|
|
|
uniform_modelview_matrix = glGetUniformLocation(shader.handle, "modelview_matrix");
|
|
|
|
uniform_color_texture = glGetUniformLocation(shader.handle, "color_texture");
|
|
|
|
attrib_position = glGetAttribLocation(shader.handle, "vert_position");
|
|
|
|
attrib_tex_coord = glGetAttribLocation(shader.handle, "vert_tex_coord");
|
2014-08-28 20:21:54 +02:00
|
|
|
|
|
|
|
// Generate VBO handle for drawing
|
2016-04-17 00:57:57 +02:00
|
|
|
vertex_buffer.Create();
|
2014-08-28 20:21:54 +02:00
|
|
|
|
|
|
|
// Generate VAO
|
2016-04-17 00:57:57 +02:00
|
|
|
vertex_array.Create();
|
|
|
|
state.draw.vertex_array = vertex_array.handle;
|
2014-08-28 20:21:54 +02:00
|
|
|
|
|
|
|
// Attach vertex data to VAO
|
2019-01-06 05:53:27 +01:00
|
|
|
glNamedBufferData(vertex_buffer.handle, sizeof(ScreenRectVertex) * 4, nullptr, GL_STREAM_DRAW);
|
|
|
|
glVertexArrayAttribFormat(vertex_array.handle, attrib_position, 2, GL_FLOAT, GL_FALSE,
|
|
|
|
offsetof(ScreenRectVertex, position));
|
|
|
|
glVertexArrayAttribFormat(vertex_array.handle, attrib_tex_coord, 2, GL_FLOAT, GL_FALSE,
|
|
|
|
offsetof(ScreenRectVertex, tex_coord));
|
|
|
|
glVertexArrayAttribBinding(vertex_array.handle, attrib_position, 0);
|
|
|
|
glVertexArrayAttribBinding(vertex_array.handle, attrib_tex_coord, 0);
|
|
|
|
glEnableVertexArrayAttrib(vertex_array.handle, attrib_position);
|
|
|
|
glEnableVertexArrayAttrib(vertex_array.handle, attrib_tex_coord);
|
|
|
|
glVertexArrayVertexBuffer(vertex_array.handle, 0, vertex_buffer.handle, 0,
|
|
|
|
sizeof(ScreenRectVertex));
|
2014-04-07 06:52:34 +02:00
|
|
|
|
2018-01-11 04:43:17 +01:00
|
|
|
// Allocate textures for the screen
|
2019-01-06 22:49:23 +01:00
|
|
|
screen_info.texture.resource.Create(GL_TEXTURE_2D);
|
2014-04-07 06:52:34 +02:00
|
|
|
|
2019-01-06 22:49:23 +01:00
|
|
|
const GLuint texture = screen_info.texture.resource.handle;
|
|
|
|
glTextureStorage2D(texture, 1, GL_RGBA8, 1, 1);
|
2016-04-17 00:57:57 +02:00
|
|
|
|
2018-01-11 04:43:17 +01:00
|
|
|
screen_info.display_texture = screen_info.texture.resource.handle;
|
2015-05-19 06:21:33 +02:00
|
|
|
|
2018-01-15 06:20:19 +01:00
|
|
|
// Clear screen to black
|
|
|
|
LoadColorToActiveGLTexture(0, 0, 0, 0, screen_info.texture);
|
2014-04-06 22:55:39 +02:00
|
|
|
}
|
|
|
|
|
2019-03-02 21:08:11 +01:00
|
|
|
void RendererOpenGL::AddTelemetryFields() {
|
|
|
|
const char* const gl_version{reinterpret_cast<char const*>(glGetString(GL_VERSION))};
|
|
|
|
const char* const gpu_vendor{reinterpret_cast<char const*>(glGetString(GL_VENDOR))};
|
|
|
|
const char* const gpu_model{reinterpret_cast<char const*>(glGetString(GL_RENDERER))};
|
|
|
|
|
|
|
|
LOG_INFO(Render_OpenGL, "GL_VERSION: {}", gl_version);
|
|
|
|
LOG_INFO(Render_OpenGL, "GL_VENDOR: {}", gpu_vendor);
|
|
|
|
LOG_INFO(Render_OpenGL, "GL_RENDERER: {}", gpu_model);
|
|
|
|
|
|
|
|
auto& telemetry_session = system.TelemetrySession();
|
|
|
|
telemetry_session.AddField(Telemetry::FieldType::UserSystem, "GPU_Vendor", gpu_vendor);
|
|
|
|
telemetry_session.AddField(Telemetry::FieldType::UserSystem, "GPU_Model", gpu_model);
|
|
|
|
telemetry_session.AddField(Telemetry::FieldType::UserSystem, "GPU_OpenGL_Version", gl_version);
|
|
|
|
}
|
|
|
|
|
2018-08-21 01:22:43 +02:00
|
|
|
void RendererOpenGL::CreateRasterizer() {
|
|
|
|
if (rasterizer) {
|
|
|
|
return;
|
|
|
|
}
|
2018-10-24 22:09:40 +02:00
|
|
|
// Initialize sRGB Usage
|
|
|
|
OpenGLState::ClearsRGBUsed();
|
2019-01-15 20:28:42 +01:00
|
|
|
rasterizer = std::make_unique<RasterizerOpenGL>(render_window, system, screen_info);
|
2018-08-21 01:22:43 +02:00
|
|
|
}
|
|
|
|
|
2015-02-05 15:15:42 +01:00
|
|
|
void RendererOpenGL::ConfigureFramebufferTexture(TextureInfo& texture,
|
2018-03-23 02:04:30 +01:00
|
|
|
const Tegra::FramebufferConfig& framebuffer) {
|
|
|
|
texture.width = framebuffer.width;
|
|
|
|
texture.height = framebuffer.height;
|
2019-02-26 07:47:16 +01:00
|
|
|
texture.pixel_format = framebuffer.pixel_format;
|
2015-02-05 15:15:42 +01:00
|
|
|
|
2018-01-11 04:43:17 +01:00
|
|
|
GLint internal_format;
|
2018-03-23 02:04:30 +01:00
|
|
|
switch (framebuffer.pixel_format) {
|
|
|
|
case Tegra::FramebufferConfig::PixelFormat::ABGR8:
|
2019-01-06 22:49:23 +01:00
|
|
|
internal_format = GL_RGBA8;
|
2015-02-05 15:15:42 +01:00
|
|
|
texture.gl_format = GL_RGBA;
|
2018-04-25 01:44:09 +02:00
|
|
|
texture.gl_type = GL_UNSIGNED_INT_8_8_8_8_REV;
|
2018-01-11 04:43:17 +01:00
|
|
|
gl_framebuffer_data.resize(texture.width * texture.height * 4);
|
2015-02-05 15:15:42 +01:00
|
|
|
break;
|
|
|
|
default:
|
2019-01-06 22:49:23 +01:00
|
|
|
internal_format = GL_RGBA8;
|
2018-11-21 02:27:34 +01:00
|
|
|
texture.gl_format = GL_RGBA;
|
|
|
|
texture.gl_type = GL_UNSIGNED_INT_8_8_8_8_REV;
|
|
|
|
gl_framebuffer_data.resize(texture.width * texture.height * 4);
|
|
|
|
LOG_CRITICAL(Render_OpenGL, "Unknown framebuffer pixel format: {}",
|
|
|
|
static_cast<u32>(framebuffer.pixel_format));
|
2018-03-25 04:38:08 +02:00
|
|
|
UNREACHABLE();
|
2015-02-05 15:15:42 +01:00
|
|
|
}
|
|
|
|
|
2019-01-06 22:49:23 +01:00
|
|
|
texture.resource.Release();
|
|
|
|
texture.resource.Create(GL_TEXTURE_2D);
|
|
|
|
glTextureStorage2D(texture.resource.handle, 1, internal_format, texture.width, texture.height);
|
2015-02-05 15:15:42 +01:00
|
|
|
}
|
|
|
|
|
2018-03-27 03:06:37 +02:00
|
|
|
void RendererOpenGL::DrawScreenTriangles(const ScreenInfo& screen_info, float x, float y, float w,
|
|
|
|
float h) {
|
2018-02-12 01:04:27 +01:00
|
|
|
const auto& texcoords = screen_info.display_texcoords;
|
2018-03-23 19:58:27 +01:00
|
|
|
auto left = texcoords.left;
|
|
|
|
auto right = texcoords.right;
|
2018-04-24 17:12:11 +02:00
|
|
|
if (framebuffer_transform_flags != Tegra::FramebufferConfig::TransformFlags::Unset) {
|
2018-03-23 19:58:27 +01:00
|
|
|
if (framebuffer_transform_flags == Tegra::FramebufferConfig::TransformFlags::FlipV) {
|
|
|
|
// Flip the framebuffer vertically
|
|
|
|
left = texcoords.right;
|
|
|
|
right = texcoords.left;
|
|
|
|
} else {
|
|
|
|
// Other transformations are unsupported
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_CRITICAL(Render_OpenGL, "Unsupported framebuffer_transform_flags={}",
|
2018-07-02 18:20:50 +02:00
|
|
|
static_cast<u32>(framebuffer_transform_flags));
|
2018-03-23 19:58:27 +01:00
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
2018-04-24 17:12:11 +02:00
|
|
|
}
|
2016-04-17 00:57:57 +02:00
|
|
|
|
2018-07-18 02:11:41 +02:00
|
|
|
ASSERT_MSG(framebuffer_crop_rect.top == 0, "Unimplemented");
|
|
|
|
ASSERT_MSG(framebuffer_crop_rect.left == 0, "Unimplemented");
|
|
|
|
|
|
|
|
// Scale the output by the crop width/height. This is commonly used with 1280x720 rendering
|
|
|
|
// (e.g. handheld mode) on a 1920x1080 framebuffer.
|
|
|
|
f32 scale_u = 1.f, scale_v = 1.f;
|
|
|
|
if (framebuffer_crop_rect.GetWidth() > 0) {
|
|
|
|
scale_u = static_cast<f32>(framebuffer_crop_rect.GetWidth()) / screen_info.texture.width;
|
|
|
|
}
|
|
|
|
if (framebuffer_crop_rect.GetHeight() > 0) {
|
|
|
|
scale_v = static_cast<f32>(framebuffer_crop_rect.GetHeight()) / screen_info.texture.height;
|
|
|
|
}
|
|
|
|
|
2015-09-16 14:38:12 +02:00
|
|
|
std::array<ScreenRectVertex, 4> vertices = {{
|
2018-07-18 02:11:41 +02:00
|
|
|
ScreenRectVertex(x, y, texcoords.top * scale_u, left * scale_v),
|
|
|
|
ScreenRectVertex(x + w, y, texcoords.bottom * scale_u, left * scale_v),
|
|
|
|
ScreenRectVertex(x, y + h, texcoords.top * scale_u, right * scale_v),
|
|
|
|
ScreenRectVertex(x + w, y + h, texcoords.bottom * scale_u, right * scale_v),
|
2015-09-16 14:38:12 +02:00
|
|
|
}};
|
2014-08-28 20:21:54 +02:00
|
|
|
|
2018-09-01 05:54:31 +02:00
|
|
|
state.texture_units[0].texture = screen_info.display_texture;
|
2018-10-24 22:09:40 +02:00
|
|
|
// Workaround brigthness problems in SMO by enabling sRGB in the final output
|
2019-01-06 05:53:27 +01:00
|
|
|
// if it has been used in the frame. Needed because of this bug in QT: QTBUG-50987
|
2018-10-24 22:09:40 +02:00
|
|
|
state.framebuffer_srgb.enabled = OpenGLState::GetsRGBUsed();
|
2015-05-19 06:21:33 +02:00
|
|
|
state.Apply();
|
2019-01-06 05:53:27 +01:00
|
|
|
glNamedBufferSubData(vertex_buffer.handle, 0, sizeof(vertices), vertices.data());
|
2014-08-28 20:21:54 +02:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
2019-01-06 05:53:27 +01:00
|
|
|
// Restore default state
|
2018-10-24 22:09:40 +02:00
|
|
|
state.framebuffer_srgb.enabled = false;
|
2018-09-01 05:54:31 +02:00
|
|
|
state.texture_units[0].texture = 0;
|
2016-04-17 00:57:57 +02:00
|
|
|
state.Apply();
|
2018-10-24 22:09:40 +02:00
|
|
|
// Clear sRGB state for the next frame
|
|
|
|
OpenGLState::ClearsRGBUsed();
|
2014-08-28 20:21:54 +02:00
|
|
|
}
|
|
|
|
|
2014-08-28 11:34:47 +02:00
|
|
|
/**
|
|
|
|
* Draws the emulated screens to the emulator window.
|
|
|
|
*/
|
2018-08-31 08:16:16 +02:00
|
|
|
void RendererOpenGL::DrawScreen(const Layout::FramebufferLayout& layout) {
|
2018-09-09 01:57:19 +02:00
|
|
|
if (renderer_settings.set_background_color) {
|
|
|
|
// Update background color before drawing
|
|
|
|
glClearColor(Settings::values.bg_red, Settings::values.bg_green, Settings::values.bg_blue,
|
|
|
|
0.0f);
|
|
|
|
}
|
|
|
|
|
2018-01-10 04:47:49 +01:00
|
|
|
const auto& screen = layout.screen;
|
2015-03-07 23:21:19 +01:00
|
|
|
|
|
|
|
glViewport(0, 0, layout.width, layout.height);
|
2014-08-21 09:27:53 +02:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2014-04-07 06:52:34 +02:00
|
|
|
|
2014-08-28 20:21:54 +02:00
|
|
|
// Set projection matrix
|
2016-09-18 02:38:01 +02:00
|
|
|
std::array<GLfloat, 3 * 2> ortho_matrix =
|
|
|
|
MakeOrthographicMatrix((float)layout.width, (float)layout.height);
|
2014-08-28 20:21:54 +02:00
|
|
|
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
|
|
|
|
2018-03-27 03:06:37 +02:00
|
|
|
DrawScreenTriangles(screen_info, (float)screen.left, (float)screen.top,
|
|
|
|
(float)screen.GetWidth(), (float)screen.GetHeight());
|
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
|
2016-09-19 03:01:46 +02:00
|
|
|
void RendererOpenGL::UpdateFramerate() {}
|
2014-04-06 22:55:39 +02:00
|
|
|
|
2018-08-31 08:16:16 +02:00
|
|
|
void RendererOpenGL::CaptureScreenshot() {
|
|
|
|
// Draw the current frame to the screenshot framebuffer
|
|
|
|
screenshot_framebuffer.Create();
|
|
|
|
GLuint old_read_fb = state.draw.read_framebuffer;
|
|
|
|
GLuint old_draw_fb = state.draw.draw_framebuffer;
|
|
|
|
state.draw.read_framebuffer = state.draw.draw_framebuffer = screenshot_framebuffer.handle;
|
|
|
|
state.Apply();
|
|
|
|
|
|
|
|
Layout::FramebufferLayout layout{renderer_settings.screenshot_framebuffer_layout};
|
|
|
|
|
|
|
|
GLuint renderbuffer;
|
|
|
|
glGenRenderbuffers(1, &renderbuffer);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
|
2019-02-15 21:27:29 +01:00
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, state.GetsRGBUsed() ? GL_SRGB8 : GL_RGB8, layout.width,
|
|
|
|
layout.height);
|
2018-08-31 08:16:16 +02:00
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffer);
|
|
|
|
|
|
|
|
DrawScreen(layout);
|
|
|
|
|
|
|
|
glReadPixels(0, 0, layout.width, layout.height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
|
|
|
|
renderer_settings.screenshot_bits);
|
|
|
|
|
|
|
|
screenshot_framebuffer.Release();
|
|
|
|
state.draw.read_framebuffer = old_read_fb;
|
|
|
|
state.draw.draw_framebuffer = old_draw_fb;
|
|
|
|
state.Apply();
|
|
|
|
glDeleteRenderbuffers(1, &renderbuffer);
|
|
|
|
|
|
|
|
renderer_settings.screenshot_complete_callback();
|
|
|
|
renderer_settings.screenshot_requested = false;
|
|
|
|
}
|
|
|
|
|
2015-10-10 01:32:40 +02:00
|
|
|
static const char* GetSource(GLenum source) {
|
2016-09-18 02:38:01 +02:00
|
|
|
#define RET(s) \
|
|
|
|
case GL_DEBUG_SOURCE_##s: \
|
|
|
|
return #s
|
2015-10-10 01:32:40 +02:00
|
|
|
switch (source) {
|
2016-09-18 02:38:01 +02:00
|
|
|
RET(API);
|
|
|
|
RET(WINDOW_SYSTEM);
|
|
|
|
RET(SHADER_COMPILER);
|
|
|
|
RET(THIRD_PARTY);
|
|
|
|
RET(APPLICATION);
|
|
|
|
RET(OTHER);
|
2015-10-10 01:32:40 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2018-12-19 02:52:32 +01:00
|
|
|
return "Unknown source";
|
2015-10-10 01:32:40 +02:00
|
|
|
}
|
|
|
|
#undef RET
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* GetType(GLenum type) {
|
2016-09-18 02:38:01 +02:00
|
|
|
#define RET(t) \
|
|
|
|
case GL_DEBUG_TYPE_##t: \
|
|
|
|
return #t
|
2015-10-10 01:32:40 +02:00
|
|
|
switch (type) {
|
2016-09-18 02:38:01 +02:00
|
|
|
RET(ERROR);
|
|
|
|
RET(DEPRECATED_BEHAVIOR);
|
|
|
|
RET(UNDEFINED_BEHAVIOR);
|
|
|
|
RET(PORTABILITY);
|
|
|
|
RET(PERFORMANCE);
|
|
|
|
RET(OTHER);
|
|
|
|
RET(MARKER);
|
2015-10-10 01:32:40 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2018-12-19 02:52:32 +01:00
|
|
|
return "Unknown type";
|
2015-10-10 01:32:40 +02:00
|
|
|
}
|
|
|
|
#undef RET
|
|
|
|
}
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
static void APIENTRY DebugHandler(GLenum source, GLenum type, GLuint id, GLenum severity,
|
|
|
|
GLsizei length, const GLchar* message, const void* user_param) {
|
2018-04-27 17:52:18 +02:00
|
|
|
const char format[] = "{} {} {}: {}";
|
|
|
|
const char* const str_source = GetSource(source);
|
|
|
|
const char* const str_type = GetType(type);
|
|
|
|
|
2015-10-10 01:32:40 +02:00
|
|
|
switch (severity) {
|
|
|
|
case GL_DEBUG_SEVERITY_HIGH:
|
2018-08-17 04:09:20 +02:00
|
|
|
LOG_CRITICAL(Render_OpenGL, format, str_source, str_type, id, message);
|
2015-10-10 01:32:40 +02:00
|
|
|
break;
|
|
|
|
case GL_DEBUG_SEVERITY_MEDIUM:
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_WARNING(Render_OpenGL, format, str_source, str_type, id, message);
|
2015-10-10 01:32:40 +02:00
|
|
|
break;
|
|
|
|
case GL_DEBUG_SEVERITY_NOTIFICATION:
|
|
|
|
case GL_DEBUG_SEVERITY_LOW:
|
2018-08-21 06:23:05 +02:00
|
|
|
LOG_DEBUG(Render_OpenGL, format, str_source, str_type, id, message);
|
2015-10-10 01:32:40 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-06 22:55:39 +02:00
|
|
|
/// Initialize the renderer
|
2016-01-07 20:33:54 +01:00
|
|
|
bool RendererOpenGL::Init() {
|
2019-01-08 05:16:01 +01:00
|
|
|
Core::Frontend::ScopeAcquireWindowContext acquire_context{render_window};
|
2014-04-06 22:55:39 +02:00
|
|
|
|
2015-10-10 01:32:40 +02:00
|
|
|
if (GLAD_GL_KHR_debug) {
|
|
|
|
glEnable(GL_DEBUG_OUTPUT);
|
|
|
|
glDebugMessageCallback(DebugHandler, nullptr);
|
|
|
|
}
|
|
|
|
|
2019-03-02 21:08:11 +01:00
|
|
|
AddTelemetryFields();
|
2017-07-13 04:19:34 +02:00
|
|
|
|
2018-11-18 01:28:14 +01:00
|
|
|
if (!GLAD_GL_VERSION_4_3) {
|
2016-01-07 20:33:54 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-28 11:34:47 +02:00
|
|
|
InitOpenGLObjects();
|
2018-08-21 01:22:43 +02:00
|
|
|
CreateRasterizer();
|
2016-01-07 20:33:54 +01:00
|
|
|
|
|
|
|
return true;
|
2014-04-06 22:55:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Shutdown the renderer
|
2016-09-19 03:01:46 +02:00
|
|
|
void RendererOpenGL::ShutDown() {}
|
2018-08-21 10:18:27 +02:00
|
|
|
|
|
|
|
} // namespace OpenGL
|