From 7e7b3dc18cb5c2c17a80e917787e29208a37e87f Mon Sep 17 00:00:00 2001 From: GPUCode Date: Tue, 4 Oct 2022 07:32:19 +0300 Subject: [PATCH] renderer_opengl: Address buffer overflow --- src/citra_qt/bootmanager.cpp | 2 -- .../renderer_opengl/gl_shader_gen.cpp | 4 ++-- .../renderer_opengl/gl_shader_manager.cpp | 1 + .../renderer_opengl/gl_texture_runtime.cpp | 22 ++++++++++++------- .../renderer_opengl/gl_texture_runtime.h | 2 +- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/citra_qt/bootmanager.cpp b/src/citra_qt/bootmanager.cpp index 79eba388d..df40dab6b 100644 --- a/src/citra_qt/bootmanager.cpp +++ b/src/citra_qt/bootmanager.cpp @@ -122,10 +122,8 @@ public: QSurfaceFormat format; if (Settings::values.graphics_api == Settings::GraphicsAPI::OpenGLES) { - QCoreApplication::setAttribute(Qt::AA_UseOpenGLES); format.setVersion(3, 2); } else { - QCoreApplication::setAttribute(Qt::AA_UseDesktopOpenGL); format.setVersion(4, 4); format.setProfile(QSurfaceFormat::CoreProfile); } diff --git a/src/video_core/renderer_opengl/gl_shader_gen.cpp b/src/video_core/renderer_opengl/gl_shader_gen.cpp index a5435e864..f1b2ef713 100644 --- a/src/video_core/renderer_opengl/gl_shader_gen.cpp +++ b/src/video_core/renderer_opengl/gl_shader_gen.cpp @@ -76,7 +76,7 @@ static std::string GetVertexInterfaceDeclaration(bool is_output, bool separable_ const auto append_variable = [&](std::string_view var, int location) { if (separable_shader) { - out += fmt::format("layout (location={}) ", location); + out += fmt::format("layout(location = {}) ", location); } out += fmt::format("{}{};\n", is_output ? "out " : "in ", var); }; @@ -1240,7 +1240,7 @@ ShaderDecompiler::ProgramResult GenerateFragmentShader(const PicaFSConfig& confi in vec4 gl_FragCoord; #endif // CITRA_GLES -out vec4 color; +layout (location = 0) out vec4 color; uniform sampler2D tex0; uniform sampler2D tex1; diff --git a/src/video_core/renderer_opengl/gl_shader_manager.cpp b/src/video_core/renderer_opengl/gl_shader_manager.cpp index c2dac6451..59654bffc 100644 --- a/src/video_core/renderer_opengl/gl_shader_manager.cpp +++ b/src/video_core/renderer_opengl/gl_shader_manager.cpp @@ -453,6 +453,7 @@ void ShaderProgramManager::ApplyTo(OpenGLState& state) { glUseProgramStages(impl->pipeline.handle, GL_VERTEX_SHADER_BIT, impl->current.vs); glUseProgramStages(impl->pipeline.handle, GL_GEOMETRY_SHADER_BIT, impl->current.gs); glUseProgramStages(impl->pipeline.handle, GL_FRAGMENT_SHADER_BIT, impl->current.fs); + state.draw.shader_program = 0; state.draw.program_pipeline = impl->pipeline.handle; } else { diff --git a/src/video_core/renderer_opengl/gl_texture_runtime.cpp b/src/video_core/renderer_opengl/gl_texture_runtime.cpp index abff64276..0f565913f 100644 --- a/src/video_core/renderer_opengl/gl_texture_runtime.cpp +++ b/src/video_core/renderer_opengl/gl_texture_runtime.cpp @@ -68,6 +68,7 @@ const StagingBuffer& TextureRuntime::FindStaging(u32 size, bool upload) { // Attempt to find a free buffer that fits the requested data for (auto it = search.lower_bound({.size = size}); it != search.end(); it++) { if (!upload || it->IsFree()) { + it->mapped = std::span{it->mapped.data(), size}; return *it; } } @@ -132,6 +133,7 @@ void TextureRuntime::FormatConvert(const Surface& surface, bool upload, } else if (format == VideoCore::PixelFormat::RGB8 && driver.IsOpenGLES()) { Pica::Texture::ConvertBGRToRGB(source, dest); } else { + ASSERT(dest.size() >= source.size()); std::memcpy(dest.data(), source.data(), source.size()); } } @@ -311,18 +313,22 @@ void TextureRuntime::BindFramebuffer(GLenum target, GLint level, GLenum textarge Surface::Surface(VideoCore::SurfaceParams& params, TextureRuntime& runtime) : VideoCore::SurfaceBase{params}, runtime{runtime}, driver{runtime.GetDriver()} { - texture = runtime.Allocate(GetScaledWidth(), GetScaledHeight(), params.pixel_format, texture_type); + if (pixel_format != VideoCore::PixelFormat::Invalid) { + texture = runtime.Allocate(GetScaledWidth(), GetScaledHeight(), params.pixel_format, texture_type); + } } Surface::~Surface() { - const VideoCore::HostTextureTag tag = { - .format = pixel_format, - .width = GetScaledWidth(), - .height = GetScaledHeight(), - .layers = texture_type == VideoCore::TextureType::CubeMap ? 6u : 1u - }; + if (pixel_format != VideoCore::PixelFormat::Invalid) { + const VideoCore::HostTextureTag tag = { + .format = pixel_format, + .width = GetScaledWidth(), + .height = GetScaledHeight(), + .layers = texture_type == VideoCore::TextureType::CubeMap ? 6u : 1u + }; - runtime.texture_recycler.emplace(tag, std::move(texture)); + runtime.texture_recycler.emplace(tag, std::move(texture)); + } } MICROPROFILE_DEFINE(OpenGL_Upload, "OpenGLSurface", "Texture Upload", MP_RGB(128, 192, 64)); diff --git a/src/video_core/renderer_opengl/gl_texture_runtime.h b/src/video_core/renderer_opengl/gl_texture_runtime.h index d57b2659c..332c0778f 100644 --- a/src/video_core/renderer_opengl/gl_texture_runtime.h +++ b/src/video_core/renderer_opengl/gl_texture_runtime.h @@ -22,7 +22,7 @@ struct FormatTuple { struct StagingBuffer { OGLBuffer buffer{}; mutable OGLSync buffer_lock{}; - std::span mapped{}; + mutable std::span mapped{}; u32 size{}; bool operator<(const StagingBuffer& other) const {