From 4589582eaf9aa5a1d9bc4e74ed71b0bf7903a1d2 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Thu, 28 Nov 2019 20:11:03 -0300 Subject: [PATCH 1/6] renderer_opengl: Move commentaries to header file --- .../renderer_opengl/renderer_opengl.cpp | 19 +------------------ .../renderer_opengl/renderer_opengl.h | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/video_core/renderer_opengl/renderer_opengl.cpp b/src/video_core/renderer_opengl/renderer_opengl.cpp index a57a564f77..69fd735bd1 100644 --- a/src/video_core/renderer_opengl/renderer_opengl.cpp +++ b/src/video_core/renderer_opengl/renderer_opengl.cpp @@ -62,9 +62,7 @@ void main() { } )"; -/** - * Vertex structure that the drawn screen rectangles are composed of. - */ +/// Vertex structure that the drawn screen rectangles are composed of. struct ScreenRectVertex { ScreenRectVertex(GLfloat x, GLfloat y, GLfloat u, GLfloat v) { position[0] = x; @@ -138,9 +136,6 @@ void RendererOpenGL::SwapBuffers(const Tegra::FramebufferConfig* framebuffer) { prev_state.Apply(); } -/** - * Loads framebuffer from emulated memory into the active OpenGL texture. - */ void RendererOpenGL::LoadFBToScreenInfo(const Tegra::FramebufferConfig& framebuffer) { // Framebuffer orientation handling framebuffer_transform_flags = framebuffer.transform_flags; @@ -181,19 +176,12 @@ void RendererOpenGL::LoadFBToScreenInfo(const Tegra::FramebufferConfig& framebuf glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); } -/** - * 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. - */ void RendererOpenGL::LoadColorToActiveGLTexture(u8 color_r, u8 color_g, u8 color_b, u8 color_a, const TextureInfo& texture) { 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); } -/** - * Initializes the OpenGL state and creates persistent objects. - */ void RendererOpenGL::InitOpenGLObjects() { glClearColor(Settings::values.bg_red, Settings::values.bg_green, Settings::values.bg_blue, 0.0f); @@ -351,9 +339,6 @@ void RendererOpenGL::DrawScreenTriangles(const ScreenInfo& screen_info, float x, state.Apply(); } -/** - * Draws the emulated screens to the emulator window. - */ void RendererOpenGL::DrawScreen(const Layout::FramebufferLayout& layout) { if (renderer_settings.set_background_color) { // Update background color before drawing @@ -381,7 +366,6 @@ void RendererOpenGL::DrawScreen(const Layout::FramebufferLayout& layout) { m_current_frame++; } -/// Updates the framerate void RendererOpenGL::UpdateFramerate() {} void RendererOpenGL::CaptureScreenshot() { @@ -495,7 +479,6 @@ bool RendererOpenGL::Init() { return true; } -/// Shutdown the renderer void RendererOpenGL::ShutDown() {} } // namespace OpenGL diff --git a/src/video_core/renderer_opengl/renderer_opengl.h b/src/video_core/renderer_opengl/renderer_opengl.h index cf26628ca4..6bb620a317 100644 --- a/src/video_core/renderer_opengl/renderer_opengl.h +++ b/src/video_core/renderer_opengl/renderer_opengl.h @@ -59,21 +59,31 @@ public: void ShutDown() override; private: + /// Initializes the OpenGL state and creates persistent objects. void InitOpenGLObjects(); + void AddTelemetryFields(); + void CreateRasterizer(); void ConfigureFramebufferTexture(TextureInfo& texture, const Tegra::FramebufferConfig& framebuffer); + + /// Draws the emulated screens to the emulator window. void DrawScreen(const Layout::FramebufferLayout& layout); + void DrawScreenTriangles(const ScreenInfo& screen_info, float x, float y, float w, float h); + + /// Updates the framerate. void UpdateFramerate(); void CaptureScreenshot(); - // Loads framebuffer from emulated memory into the display information structure + /// Loads framebuffer from emulated memory into the active OpenGL texture. void LoadFBToScreenInfo(const Tegra::FramebufferConfig& framebuffer); - // Fills active OpenGL texture with the given RGBA color. + + /// 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. void LoadColorToActiveGLTexture(u8 color_r, u8 color_g, u8 color_b, u8 color_a, const TextureInfo& texture); From 056f049b269f7f6397c9aa1e736b0d03d31f186d Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Thu, 28 Nov 2019 20:14:40 -0300 Subject: [PATCH 2/6] renderer_opengl: Move static definitions to anonymous namespace --- .../renderer_opengl/renderer_opengl.cpp | 128 +++++++++--------- 1 file changed, 66 insertions(+), 62 deletions(-) diff --git a/src/video_core/renderer_opengl/renderer_opengl.cpp b/src/video_core/renderer_opengl/renderer_opengl.cpp index 69fd735bd1..82b4262233 100644 --- a/src/video_core/renderer_opengl/renderer_opengl.cpp +++ b/src/video_core/renderer_opengl/renderer_opengl.cpp @@ -24,7 +24,9 @@ namespace OpenGL { -static const char vertex_shader[] = R"( +namespace { + +constexpr char vertex_shader[] = R"( #version 150 core in vec2 vert_position; @@ -47,7 +49,7 @@ void main() { } )"; -static const char fragment_shader[] = R"( +constexpr char fragment_shader[] = R"( #version 150 core in vec2 frag_tex_coord; @@ -82,18 +84,77 @@ struct ScreenRectVertex { * The projection part of the matrix is trivial, hence these operations are represented * by a 3x2 matrix. */ -static std::array MakeOrthographicMatrix(const float width, const float height) { +std::array MakeOrthographicMatrix(float width, float height) { std::array 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; + 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]. // clang-format on return matrix; } +const char* GetSource(GLenum source) { +#define RET(s) \ + case GL_DEBUG_SOURCE_##s: \ + return #s + switch (source) { + RET(API); + RET(WINDOW_SYSTEM); + RET(SHADER_COMPILER); + RET(THIRD_PARTY); + RET(APPLICATION); + RET(OTHER); + default: + UNREACHABLE(); + return "Unknown source"; + } +#undef RET +} + +const char* GetType(GLenum type) { +#define RET(t) \ + case GL_DEBUG_TYPE_##t: \ + return #t + switch (type) { + RET(ERROR); + RET(DEPRECATED_BEHAVIOR); + RET(UNDEFINED_BEHAVIOR); + RET(PORTABILITY); + RET(PERFORMANCE); + RET(OTHER); + RET(MARKER); + default: + UNREACHABLE(); + return "Unknown type"; + } +#undef RET +} + +void APIENTRY DebugHandler(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, + const GLchar* message, const void* user_param) { + const char format[] = "{} {} {}: {}"; + const char* const str_source = GetSource(source); + const char* const str_type = GetType(type); + + switch (severity) { + case GL_DEBUG_SEVERITY_HIGH: + LOG_CRITICAL(Render_OpenGL, format, str_source, str_type, id, message); + break; + case GL_DEBUG_SEVERITY_MEDIUM: + LOG_WARNING(Render_OpenGL, format, str_source, str_type, id, message); + break; + case GL_DEBUG_SEVERITY_NOTIFICATION: + case GL_DEBUG_SEVERITY_LOW: + LOG_DEBUG(Render_OpenGL, format, str_source, str_type, id, message); + break; + } +} + +} // Anonymous namespace + RendererOpenGL::RendererOpenGL(Core::Frontend::EmuWindow& emu_window, Core::System& system) : VideoCore::RendererBase{emu_window}, emu_window{emu_window}, system{system} {} @@ -402,63 +463,6 @@ void RendererOpenGL::CaptureScreenshot() { renderer_settings.screenshot_requested = false; } -static const char* GetSource(GLenum source) { -#define RET(s) \ - case GL_DEBUG_SOURCE_##s: \ - return #s - switch (source) { - RET(API); - RET(WINDOW_SYSTEM); - RET(SHADER_COMPILER); - RET(THIRD_PARTY); - RET(APPLICATION); - RET(OTHER); - default: - UNREACHABLE(); - return "Unknown source"; - } -#undef RET -} - -static const char* GetType(GLenum type) { -#define RET(t) \ - case GL_DEBUG_TYPE_##t: \ - return #t - switch (type) { - RET(ERROR); - RET(DEPRECATED_BEHAVIOR); - RET(UNDEFINED_BEHAVIOR); - RET(PORTABILITY); - RET(PERFORMANCE); - RET(OTHER); - RET(MARKER); - default: - UNREACHABLE(); - return "Unknown type"; - } -#undef RET -} - -static void APIENTRY DebugHandler(GLenum source, GLenum type, GLuint id, GLenum severity, - GLsizei length, const GLchar* message, const void* user_param) { - const char format[] = "{} {} {}: {}"; - const char* const str_source = GetSource(source); - const char* const str_type = GetType(type); - - switch (severity) { - case GL_DEBUG_SEVERITY_HIGH: - LOG_CRITICAL(Render_OpenGL, format, str_source, str_type, id, message); - break; - case GL_DEBUG_SEVERITY_MEDIUM: - LOG_WARNING(Render_OpenGL, format, str_source, str_type, id, message); - break; - case GL_DEBUG_SEVERITY_NOTIFICATION: - case GL_DEBUG_SEVERITY_LOW: - LOG_DEBUG(Render_OpenGL, format, str_source, str_type, id, message); - break; - } -} - bool RendererOpenGL::Init() { Core::Frontend::ScopeAcquireWindowContext acquire_context{render_window}; From 75cc501d521842cd4e6d2dd4dcef8acb02bd250f Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Thu, 28 Nov 2019 20:15:25 -0300 Subject: [PATCH 3/6] renderer_opengl: Drop macros for message decorations --- .../renderer_opengl/renderer_opengl.cpp | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/video_core/renderer_opengl/renderer_opengl.cpp b/src/video_core/renderer_opengl/renderer_opengl.cpp index 82b4262233..23b60dbf4f 100644 --- a/src/video_core/renderer_opengl/renderer_opengl.cpp +++ b/src/video_core/renderer_opengl/renderer_opengl.cpp @@ -97,40 +97,45 @@ std::array MakeOrthographicMatrix(float width, float height) { } const char* GetSource(GLenum source) { -#define RET(s) \ - case GL_DEBUG_SOURCE_##s: \ - return #s switch (source) { - RET(API); - RET(WINDOW_SYSTEM); - RET(SHADER_COMPILER); - RET(THIRD_PARTY); - RET(APPLICATION); - RET(OTHER); + case GL_DEBUG_SOURCE_API: + return "API"; + case GL_DEBUG_SOURCE_WINDOW_SYSTEM: + return "WINDOW_SYSTEM"; + case GL_DEBUG_SOURCE_SHADER_COMPILER: + return "SHADER_COMPILER"; + case GL_DEBUG_SOURCE_THIRD_PARTY: + return "THIRD_PARTY"; + case GL_DEBUG_SOURCE_APPLICATION: + return "APPLICATION"; + case GL_DEBUG_SOURCE_OTHER: + return "OTHER"; default: UNREACHABLE(); return "Unknown source"; } -#undef RET } const char* GetType(GLenum type) { -#define RET(t) \ - case GL_DEBUG_TYPE_##t: \ - return #t switch (type) { - RET(ERROR); - RET(DEPRECATED_BEHAVIOR); - RET(UNDEFINED_BEHAVIOR); - RET(PORTABILITY); - RET(PERFORMANCE); - RET(OTHER); - RET(MARKER); + case GL_DEBUG_TYPE_ERROR: + return "ERROR"; + case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: + return "DEPRECATED_BEHAVIOR"; + case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: + return "UNDEFINED_BEHAVIOR"; + case GL_DEBUG_TYPE_PORTABILITY: + return "PORTABILITY"; + case GL_DEBUG_TYPE_PERFORMANCE: + return "PERFORMANCE"; + case GL_DEBUG_TYPE_OTHER: + return "OTHER"; + case GL_DEBUG_TYPE_MARKER: + return "MARKER"; default: UNREACHABLE(); return "Unknown type"; } -#undef RET } void APIENTRY DebugHandler(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, From 3a44faff11892861cb7782750dd7e78079ec40bd Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Thu, 28 Nov 2019 20:25:56 -0300 Subject: [PATCH 4/6] renderer_opengl: Use explicit binding for presentation shaders --- .../renderer_opengl/renderer_opengl.cpp | 46 ++++++++----------- .../renderer_opengl/renderer_opengl.h | 8 ---- 2 files changed, 20 insertions(+), 34 deletions(-) diff --git a/src/video_core/renderer_opengl/renderer_opengl.cpp b/src/video_core/renderer_opengl/renderer_opengl.cpp index 23b60dbf4f..9e66dea600 100644 --- a/src/video_core/renderer_opengl/renderer_opengl.cpp +++ b/src/video_core/renderer_opengl/renderer_opengl.cpp @@ -27,18 +27,18 @@ namespace OpenGL { namespace { constexpr char vertex_shader[] = R"( -#version 150 core +#version 430 core -in vec2 vert_position; -in vec2 vert_tex_coord; -out vec2 frag_tex_coord; +layout (location = 0) in vec2 vert_position; +layout (location = 1) in vec2 vert_tex_coord; +layout (location = 0) 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; +layout (location = 0) uniform mat3x2 modelview_matrix; void main() { // Multiply input position by the rotscale part of the matrix and then manually translate by @@ -50,20 +50,22 @@ void main() { )"; constexpr char fragment_shader[] = R"( -#version 150 core +#version 430 core -in vec2 frag_tex_coord; -out vec4 color; +layout (location = 0) in vec2 frag_tex_coord; +layout (location = 0) out vec4 color; -uniform sampler2D color_texture; +layout (binding = 0) uniform sampler2D color_texture; void main() { - // 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. color = texture(color_texture, frag_tex_coord); } )"; +constexpr GLint PositionLocation = 0; +constexpr GLint TexCoordLocation = 1; +constexpr GLint ModelViewMatrixLocation = 0; + /// Vertex structure that the drawn screen rectangles are composed of. struct ScreenRectVertex { ScreenRectVertex(GLfloat x, GLfloat y, GLfloat u, GLfloat v) { @@ -257,10 +259,6 @@ void RendererOpenGL::InitOpenGLObjects() { state.draw.shader_program = shader.handle; state.AllDirty(); 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"); // Generate VBO handle for drawing vertex_buffer.Create(); @@ -271,14 +269,14 @@ void RendererOpenGL::InitOpenGLObjects() { // Attach vertex data to VAO glNamedBufferData(vertex_buffer.handle, sizeof(ScreenRectVertex) * 4, nullptr, GL_STREAM_DRAW); - glVertexArrayAttribFormat(vertex_array.handle, attrib_position, 2, GL_FLOAT, GL_FALSE, + glVertexArrayAttribFormat(vertex_array.handle, PositionLocation, 2, GL_FLOAT, GL_FALSE, offsetof(ScreenRectVertex, position)); - glVertexArrayAttribFormat(vertex_array.handle, attrib_tex_coord, 2, GL_FLOAT, GL_FALSE, + glVertexArrayAttribFormat(vertex_array.handle, TexCoordLocation, 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); + glVertexArrayAttribBinding(vertex_array.handle, PositionLocation, 0); + glVertexArrayAttribBinding(vertex_array.handle, TexCoordLocation, 0); + glEnableVertexArrayAttrib(vertex_array.handle, PositionLocation); + glEnableVertexArrayAttrib(vertex_array.handle, TexCoordLocation); glVertexArrayVertexBuffer(vertex_array.handle, 0, vertex_buffer.handle, 0, sizeof(ScreenRectVertex)); @@ -420,11 +418,7 @@ void RendererOpenGL::DrawScreen(const Layout::FramebufferLayout& layout) { // Set projection matrix std::array ortho_matrix = MakeOrthographicMatrix((float)layout.width, (float)layout.height); - glUniformMatrix3x2fv(uniform_modelview_matrix, 1, GL_FALSE, ortho_matrix.data()); - - // Bind texture in Texture Unit 0 - glActiveTexture(GL_TEXTURE0); - glUniform1i(uniform_color_texture, 0); + glUniformMatrix3x2fv(ModelViewMatrixLocation, 1, GL_FALSE, ortho_matrix.data()); DrawScreenTriangles(screen_info, (float)screen.left, (float)screen.top, (float)screen.GetWidth(), (float)screen.GetHeight()); diff --git a/src/video_core/renderer_opengl/renderer_opengl.h b/src/video_core/renderer_opengl/renderer_opengl.h index 6bb620a317..b56328a7f0 100644 --- a/src/video_core/renderer_opengl/renderer_opengl.h +++ b/src/video_core/renderer_opengl/renderer_opengl.h @@ -104,14 +104,6 @@ private: /// OpenGL framebuffer data std::vector gl_framebuffer_data; - // Shader uniform location indices - GLuint uniform_modelview_matrix; - GLuint uniform_color_texture; - - // Shader attribute input indices - GLuint attrib_position; - GLuint attrib_tex_coord; - /// Used for transforming the framebuffer orientation Tegra::FramebufferConfig::TransformFlags framebuffer_transform_flags; Common::Rectangle framebuffer_crop_rect; From dee784444359236c5adc123c90ea03b573cd6782 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Thu, 28 Nov 2019 20:28:27 -0300 Subject: [PATCH 5/6] renderer_opengl: Remove C casts --- src/video_core/renderer_opengl/renderer_opengl.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/video_core/renderer_opengl/renderer_opengl.cpp b/src/video_core/renderer_opengl/renderer_opengl.cpp index 9e66dea600..aa837eb955 100644 --- a/src/video_core/renderer_opengl/renderer_opengl.cpp +++ b/src/video_core/renderer_opengl/renderer_opengl.cpp @@ -416,12 +416,13 @@ void RendererOpenGL::DrawScreen(const Layout::FramebufferLayout& layout) { glClear(GL_COLOR_BUFFER_BIT); // Set projection matrix - std::array ortho_matrix = - MakeOrthographicMatrix((float)layout.width, (float)layout.height); + const std::array ortho_matrix = + MakeOrthographicMatrix(static_cast(layout.width), static_cast(layout.height)); glUniformMatrix3x2fv(ModelViewMatrixLocation, 1, GL_FALSE, ortho_matrix.data()); - DrawScreenTriangles(screen_info, (float)screen.left, (float)screen.top, - (float)screen.GetWidth(), (float)screen.GetHeight()); + DrawScreenTriangles(screen_info, static_cast(screen.left), + static_cast(screen.top), static_cast(screen.GetWidth()), + static_cast(screen.GetHeight())); m_current_frame++; } From e6a0a30334c2b9d869ba3a1d2f189e2d6308c956 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Thu, 28 Nov 2019 20:36:02 -0300 Subject: [PATCH 6/6] renderer_opengl: Make ScreenRectVertex's constructor constexpr --- .../renderer_opengl/renderer_opengl.cpp | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/video_core/renderer_opengl/renderer_opengl.cpp b/src/video_core/renderer_opengl/renderer_opengl.cpp index aa837eb955..bba16afaf4 100644 --- a/src/video_core/renderer_opengl/renderer_opengl.cpp +++ b/src/video_core/renderer_opengl/renderer_opengl.cpp @@ -66,17 +66,12 @@ constexpr GLint PositionLocation = 0; constexpr GLint TexCoordLocation = 1; constexpr GLint ModelViewMatrixLocation = 0; -/// 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; - } + constexpr ScreenRectVertex(GLfloat x, GLfloat y, GLfloat u, GLfloat v) + : position{{x, y}}, tex_coord{{u, v}} {} - GLfloat position[2]; - GLfloat tex_coord[2]; + std::array position; + std::array tex_coord; }; /** @@ -383,18 +378,18 @@ void RendererOpenGL::DrawScreenTriangles(const ScreenInfo& screen_info, float x, static_cast(screen_info.texture.height); } - std::array vertices = {{ + const std::array vertices = { 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), - }}; + }; state.textures[0] = screen_info.display_texture; state.framebuffer_srgb.enabled = screen_info.display_srgb; state.AllDirty(); state.Apply(); - glNamedBufferSubData(vertex_buffer.handle, 0, sizeof(vertices), vertices.data()); + glNamedBufferSubData(vertex_buffer.handle, 0, sizeof(vertices), std::data(vertices)); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); // Restore default state state.framebuffer_srgb.enabled = false;