| 1 | // Copyright 2023 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/AbstractGfx.h" |
| 5 | |
| 6 | #include "Common/Assert.h" |
| 7 | |
| 8 | #include "VideoCommon/AbstractFramebuffer.h" |
| 9 | #include "VideoCommon/AbstractTexture.h" |
| 10 | #include "VideoCommon/BPFunctions.h" |
| 11 | #include "VideoCommon/FramebufferManager.h" |
| 12 | #include "VideoCommon/ShaderCache.h" |
| 13 | #include "VideoCommon/VertexManagerBase.h" |
| 14 | #include "VideoCommon/VideoConfig.h" |
| 15 | |
| 16 | std::unique_ptr<AbstractGfx> g_gfx; |
| 17 | |
| 18 | AbstractGfx::AbstractGfx() |
| 19 | { |
| 20 | m_config_changed = |
| 21 | GetVideoEvents().config_changed_event.Register([this](u32 bits) { OnConfigChanged(bits); }); |
| 22 | } |
| 23 | |
| 24 | bool AbstractGfx::IsHeadless() const |
| 25 | { |
| 26 | return true; |
| 27 | } |
| 28 | |
| 29 | void AbstractGfx::BeginUtilityDrawing() |
| 30 | { |
| 31 | g_vertex_manager->Flush(); |
| 32 | } |
| 33 | |
| 34 | void AbstractGfx::EndUtilityDrawing() |
| 35 | { |
| 36 | // Reset framebuffer. Pipeline will be reset at next draw. |
| 37 | g_framebuffer_manager->BindEFBFramebuffer(); |
| 38 | |
| 39 | // Reset our viewport and scissor to the last stored value |
| 40 | SetViewport(m_viewport_and_scissor.viewport_x, m_viewport_and_scissor.viewport_y, |
| 41 | m_viewport_and_scissor.viewport_width, m_viewport_and_scissor.viewport_height, |
| 42 | m_viewport_and_scissor.viewport_near_depth, |
| 43 | m_viewport_and_scissor.viewport_far_depth); |
| 44 | SetScissorRect(m_viewport_and_scissor.scissor_rect); |
| 45 | } |
| 46 | |
| 47 | void AbstractGfx::SetFramebuffer(AbstractFramebuffer* framebuffer) |
| 48 | { |
| 49 | m_current_framebuffer = framebuffer; |
| 50 | } |
| 51 | |
| 52 | void AbstractGfx::SetAndDiscardFramebuffer(AbstractFramebuffer* framebuffer) |
| 53 | { |
| 54 | m_current_framebuffer = framebuffer; |
| 55 | } |
| 56 | |
| 57 | void AbstractGfx::SetAndClearFramebuffer(AbstractFramebuffer* framebuffer, |
| 58 | const ClearColor& color_value, float depth_value) |
| 59 | { |
| 60 | m_current_framebuffer = framebuffer; |
| 61 | } |
| 62 | |
| 63 | void AbstractGfx::ClearRegion(const MathUtil::Rectangle<int>& target_rc, bool colorEnable, |
| 64 | bool alphaEnable, bool zEnable, u32 color, u32 z) |
| 65 | { |
| 66 | // This is a generic fallback for any ClearRegion operations that backends don't support. |
| 67 | // It simply draws a Quad. |
| 68 | |
| 69 | BeginUtilityDrawing(); |
| 70 | |
| 71 | // Set up uniforms. |
| 72 | struct Uniforms |
| 73 | { |
| 74 | float clear_color[4]; |
| 75 | float clear_depth; |
| 76 | float padding1, padding2, padding3; |
| 77 | }; |
| 78 | static_assert(std::is_standard_layout<Uniforms>::value); |
| 79 | Uniforms uniforms = {{static_cast<float>((color >> 16) & 0xFF) / 255.0f, |
| 80 | static_cast<float>((color >> 8) & 0xFF) / 255.0f, |
| 81 | static_cast<float>((color >> 0) & 0xFF) / 255.0f, |
| 82 | static_cast<float>((color >> 24) & 0xFF) / 255.0f}, |
| 83 | static_cast<float>(z & 0xFFFFFF) / 16777216.0f}; |
| 84 | if (!g_backend_info.bSupportsReversedDepthRange) |
| 85 | uniforms.clear_depth = 1.0f - uniforms.clear_depth; |
| 86 | g_vertex_manager->UploadUtilityUniforms(&uniforms, sizeof(uniforms)); |
| 87 | |
| 88 | g_gfx->SetPipeline(g_framebuffer_manager->GetClearPipeline(colorEnable, alphaEnable, zEnable)); |
| 89 | g_gfx->SetViewportAndScissor(target_rc); |
| 90 | g_gfx->Draw(0, 3); |
| 91 | EndUtilityDrawing(); |
| 92 | } |
| 93 | |
| 94 | void AbstractGfx::StoreViewportAndScissor(const ViewportAndScissor& viewport_and_scissor) |
| 95 | { |
| 96 | m_viewport_and_scissor = viewport_and_scissor; |
| 97 | } |
| 98 | |
| 99 | void AbstractGfx::SetViewportAndScissor(const MathUtil::Rectangle<int>& rect, float min_depth, |
| 100 | float max_depth) |
| 101 | { |
| 102 | SetViewport(static_cast<float>(rect.left), static_cast<float>(rect.top), |
| 103 | static_cast<float>(rect.GetWidth()), static_cast<float>(rect.GetHeight()), min_depth, |
| 104 | max_depth); |
| 105 | SetScissorRect(rect); |
| 106 | } |
| 107 | |
| 108 | void AbstractGfx::ScaleTexture(AbstractFramebuffer* dst_framebuffer, |
| 109 | const MathUtil::Rectangle<int>& dst_rect, |
| 110 | const AbstractTexture* src_texture, |
| 111 | const MathUtil::Rectangle<int>& src_rect) |
| 112 | { |
| 113 | ASSERT(dst_framebuffer->GetColorFormat() == AbstractTextureFormat::RGBA8); |
| 114 | |
| 115 | BeginUtilityDrawing(); |
| 116 | |
| 117 | // The shader needs to know the source rectangle. |
| 118 | const auto converted_src_rect = |
| 119 | ConvertFramebufferRectangle(src_rect, src_texture->GetWidth(), src_texture->GetHeight()); |
| 120 | const float rcp_src_width = 1.0f / src_texture->GetWidth(); |
| 121 | const float rcp_src_height = 1.0f / src_texture->GetHeight(); |
| 122 | const std::array<float, 4> uniforms = {{converted_src_rect.left * rcp_src_width, |
| 123 | converted_src_rect.top * rcp_src_height, |
| 124 | converted_src_rect.GetWidth() * rcp_src_width, |
| 125 | converted_src_rect.GetHeight() * rcp_src_height}}; |
| 126 | g_vertex_manager->UploadUtilityUniforms(&uniforms, sizeof(uniforms)); |
| 127 | |
| 128 | // Discard if we're overwriting the whole thing. |
| 129 | if (static_cast<u32>(dst_rect.GetWidth()) == dst_framebuffer->GetWidth() && |
| 130 | static_cast<u32>(dst_rect.GetHeight()) == dst_framebuffer->GetHeight()) |
| 131 | { |
| 132 | SetAndDiscardFramebuffer(dst_framebuffer); |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | SetFramebuffer(dst_framebuffer); |
| 137 | } |
| 138 | |
| 139 | SetViewportAndScissor(ConvertFramebufferRectangle(dst_rect, dst_framebuffer)); |
| 140 | SetPipeline(dst_framebuffer->GetLayers() > 1 ? g_shader_cache->GetRGBA8StereoCopyPipeline() : |
| 141 | g_shader_cache->GetRGBA8CopyPipeline()); |
| 142 | SetTexture(0, src_texture); |
| 143 | SetSamplerState(0, RenderState::GetLinearSamplerState()); |
| 144 | Draw(0, 3); |
| 145 | EndUtilityDrawing(); |
| 146 | if (dst_framebuffer->GetColorAttachment()) |
| 147 | dst_framebuffer->GetColorAttachment()->FinishedRendering(); |
| 148 | } |
| 149 | |
| 150 | MathUtil::Rectangle<int> |
| 151 | AbstractGfx::ConvertFramebufferRectangle(const MathUtil::Rectangle<int>& rect, |
| 152 | const AbstractFramebuffer* framebuffer) const |
| 153 | { |
| 154 | return ConvertFramebufferRectangle(rect, framebuffer->GetWidth(), framebuffer->GetHeight()); |
| 155 | } |
| 156 | |
| 157 | MathUtil::Rectangle<int> |
| 158 | AbstractGfx::ConvertFramebufferRectangle(const MathUtil::Rectangle<int>& rect, u32 fb_width, |
| 159 | u32 fb_height) const |
| 160 | { |
| 161 | MathUtil::Rectangle<int> ret = rect; |
| 162 | if (g_backend_info.bUsesLowerLeftOrigin) |
| 163 | { |
| 164 | ret.top = fb_height - rect.bottom; |
| 165 | ret.bottom = fb_height - rect.top; |
| 166 | } |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | std::unique_ptr<VideoCommon::AsyncShaderCompiler> AbstractGfx::CreateAsyncShaderCompiler() |
| 171 | { |
| 172 | return std::make_unique<VideoCommon::AsyncShaderCompiler>(); |
| 173 | } |
| 174 | |
| 175 | void AbstractGfx::OnConfigChanged(u32 changed_bits) |
| 176 | { |
| 177 | // If there's any shader changes, wait for the GPU to finish before destroying anything. |
| 178 | if (changed_bits & (CONFIG_CHANGE_BIT_HOST_CONFIG | CONFIG_CHANGE_BIT_MULTISAMPLES)) |
| 179 | { |
| 180 | WaitForGPUIdle(); |
| 181 | SetPipeline(nullptr); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | bool AbstractGfx::UseGeometryShaderForUI() const |
| 186 | { |
| 187 | // OpenGL doesn't render to a 2-layer backbuffer like D3D/Vulkan for quad-buffered stereo, |
| 188 | // instead drawing twice and the eye selected by glDrawBuffer() (see Presenter::RenderXFBToScreen) |
| 189 | return g_ActiveConfig.stereo_mode == StereoMode::QuadBuffer && |
| 190 | !g_backend_info.bUsesExplictQuadBuffering; |
| 191 | } |