| 1 | // Copyright 2010 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/FramebufferManager.h" |
| 5 | |
| 6 | #include <fmt/format.h> |
| 7 | #include <memory> |
| 8 | |
| 9 | #include "Common/ChunkFile.h" |
| 10 | #include "Common/Logging/Log.h" |
| 11 | #include "Common/MsgHandler.h" |
| 12 | #include "Core/Config/GraphicsSettings.h" |
| 13 | #include "Core/System.h" |
| 14 | #include "VideoCommon/AbstractFramebuffer.h" |
| 15 | #include "VideoCommon/AbstractGfx.h" |
| 16 | #include "VideoCommon/AbstractPipeline.h" |
| 17 | #include "VideoCommon/AbstractShader.h" |
| 18 | #include "VideoCommon/AbstractStagingTexture.h" |
| 19 | #include "VideoCommon/AbstractTexture.h" |
| 20 | #include "VideoCommon/BPFunctions.h" |
| 21 | #include "VideoCommon/DriverDetails.h" |
| 22 | #include "VideoCommon/FramebufferShaderGen.h" |
| 23 | #include "VideoCommon/PixelShaderManager.h" |
| 24 | #include "VideoCommon/Present.h" |
| 25 | #include "VideoCommon/VertexManagerBase.h" |
| 26 | #include "VideoCommon/VideoCommon.h" |
| 27 | #include "VideoCommon/VideoConfig.h" |
| 28 | |
| 29 | // Maximum number of pixels poked in one batch * 6 |
| 30 | constexpr size_t MAX_POKE_VERTICES = 32768; |
| 31 | |
| 32 | std::unique_ptr<FramebufferManager> g_framebuffer_manager; |
| 33 | |
| 34 | FramebufferManager::FramebufferManager() : m_prev_efb_format(PixelFormat::INVALID_FMT) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | FramebufferManager::~FramebufferManager() |
| 39 | { |
| 40 | DestroyClearPipelines(); |
| 41 | DestroyPokePipelines(); |
| 42 | DestroyConversionPipelines(); |
| 43 | DestroyReadbackPipelines(); |
| 44 | DestroyReadbackFramebuffer(); |
| 45 | DestroyEFBFramebuffer(); |
| 46 | } |
| 47 | |
| 48 | bool FramebufferManager::Initialize(int efb_scale) |
| 49 | { |
| 50 | if (!CreateEFBFramebuffer(efb_scale)) |
| 51 | { |
| 52 | PanicAlertFmt("Failed to create EFB framebuffer"); |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | m_efb_cache_tile_size = static_cast<u32>(std::max(g_ActiveConfig.iEFBAccessTileSize, 0)); |
| 57 | if (!CreateReadbackFramebuffer()) |
| 58 | { |
| 59 | PanicAlertFmt("Failed to create EFB readback framebuffer"); |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | if (!CompileReadbackPipelines()) |
| 64 | { |
| 65 | PanicAlertFmt("Failed to compile EFB readback pipelines"); |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | if (!CompileConversionPipelines()) |
| 70 | { |
| 71 | PanicAlertFmt("Failed to compile EFB conversion pipelines"); |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | if (!CompileClearPipelines()) |
| 76 | { |
| 77 | PanicAlertFmt("Failed to compile EFB clear pipelines"); |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | if (!CompilePokePipelines()) |
| 82 | { |
| 83 | PanicAlertFmt("Failed to compile EFB poke pipelines"); |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | m_end_of_frame_event = |
| 88 | GetVideoEvents().after_frame_event.Register([this](Core::System&) { EndOfFrame(); }); |
| 89 | |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | void FramebufferManager::RecreateEFBFramebuffer(int efb_scale) |
| 94 | { |
| 95 | FlushEFBPokes(); |
| 96 | InvalidatePeekCache(true); |
| 97 | |
| 98 | DestroyReadbackFramebuffer(); |
| 99 | DestroyEFBFramebuffer(); |
| 100 | if (!CreateEFBFramebuffer(efb_scale) || !CreateReadbackFramebuffer()) |
| 101 | PanicAlertFmt("Failed to recreate EFB framebuffer"); |
| 102 | } |
| 103 | |
| 104 | void FramebufferManager::RecompileShaders() |
| 105 | { |
| 106 | DestroyPokePipelines(); |
| 107 | DestroyClearPipelines(); |
| 108 | DestroyConversionPipelines(); |
| 109 | DestroyReadbackPipelines(); |
| 110 | if (!CompileReadbackPipelines() || !CompileConversionPipelines() || !CompileClearPipelines() || |
| 111 | !CompilePokePipelines()) |
| 112 | { |
| 113 | PanicAlertFmt("Failed to recompile EFB pipelines"); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | AbstractTextureFormat FramebufferManager::GetEFBColorFormat() |
| 118 | { |
| 119 | // The EFB can be set to different pixel formats by the game through the |
| 120 | // BPMEM_ZCOMPARE register (which should probably have a different name). |
| 121 | // They are: |
| 122 | // - 24-bit RGB (8-bit components) with 24-bit Z |
| 123 | // - 24-bit RGBA (6-bit components) with 24-bit Z |
| 124 | // - Multisampled 16-bit RGB (5-6-5 format) with 16-bit Z |
| 125 | // We only use one EFB format here: 32-bit ARGB with 32-bit Z. |
| 126 | // Multisampling depends on user settings. |
| 127 | // The distinction becomes important for certain operations, i.e. the |
| 128 | // alpha channel should be ignored if the EFB does not have one. |
| 129 | return AbstractTextureFormat::RGBA8; |
| 130 | } |
| 131 | |
| 132 | AbstractTextureFormat FramebufferManager::GetEFBDepthFormat() |
| 133 | { |
| 134 | // 32-bit depth clears are broken in the Adreno Vulkan driver, and have no effect. |
| 135 | // To work around this, we use a D24_S8 buffer instead, which results in a loss of accuracy. |
| 136 | // We still resolve this to a R32F texture, as there is no 24-bit format. |
| 137 | if (DriverDetails::HasBug(DriverDetails::BUG_BROKEN_D32F_CLEAR)) |
| 138 | return AbstractTextureFormat::D24_S8; |
| 139 | else |
| 140 | return AbstractTextureFormat::D32F; |
| 141 | } |
| 142 | |
| 143 | AbstractTextureFormat FramebufferManager::GetEFBDepthCopyFormat() |
| 144 | { |
| 145 | return AbstractTextureFormat::R32F; |
| 146 | } |
| 147 | |
| 148 | static u32 CalculateEFBLayers() |
| 149 | { |
| 150 | return (g_ActiveConfig.stereo_mode != StereoMode::Off) ? 2 : 1; |
| 151 | } |
| 152 | |
| 153 | TextureConfig FramebufferManager::GetEFBColorTextureConfig(u32 width, u32 height) |
| 154 | { |
| 155 | return TextureConfig(width, height, 1, CalculateEFBLayers(), g_ActiveConfig.iMultisamples, |
| 156 | GetEFBColorFormat(), AbstractTextureFlag_RenderTarget, |
| 157 | AbstractTextureType::Texture_2DArray); |
| 158 | } |
| 159 | |
| 160 | TextureConfig FramebufferManager::GetEFBDepthTextureConfig(u32 width, u32 height) |
| 161 | { |
| 162 | return TextureConfig(width, height, 1, CalculateEFBLayers(), g_ActiveConfig.iMultisamples, |
| 163 | GetEFBDepthFormat(), AbstractTextureFlag_RenderTarget, |
| 164 | AbstractTextureType::Texture_2DArray); |
| 165 | } |
| 166 | |
| 167 | FramebufferState FramebufferManager::GetEFBFramebufferState() const |
| 168 | { |
| 169 | FramebufferState ret = {}; |
| 170 | ret.color_texture_format = m_efb_color_texture->GetFormat(); |
| 171 | ret.depth_texture_format = m_efb_depth_texture->GetFormat(); |
| 172 | ret.per_sample_shading = IsEFBMultisampled() && g_ActiveConfig.bSSAA; |
| 173 | ret.samples = m_efb_color_texture->GetSamples(); |
| 174 | return ret; |
| 175 | } |
| 176 | |
| 177 | MathUtil::Rectangle<int> |
| 178 | FramebufferManager::ConvertEFBRectangle(const MathUtil::Rectangle<int>& rc) const |
| 179 | { |
| 180 | MathUtil::Rectangle<int> result; |
| 181 | result.left = EFBToScaledX(rc.left); |
| 182 | result.top = EFBToScaledY(rc.top); |
| 183 | result.right = EFBToScaledX(rc.right); |
| 184 | result.bottom = EFBToScaledY(rc.bottom); |
| 185 | return result; |
| 186 | } |
| 187 | |
| 188 | unsigned int FramebufferManager::GetEFBScale() const |
| 189 | { |
| 190 | return m_efb_scale; |
| 191 | } |
| 192 | |
| 193 | int FramebufferManager::EFBToScaledX(int x) const |
| 194 | { |
| 195 | return x * static_cast<int>(m_efb_scale); |
| 196 | } |
| 197 | |
| 198 | int FramebufferManager::EFBToScaledY(int y) const |
| 199 | { |
| 200 | return y * static_cast<int>(m_efb_scale); |
| 201 | } |
| 202 | |
| 203 | float FramebufferManager::EFBToScaledXf(float x) const |
| 204 | { |
| 205 | return x * ((float)GetEFBWidth() / (float)EFB_WIDTH); |
| 206 | } |
| 207 | |
| 208 | float FramebufferManager::EFBToScaledYf(float y) const |
| 209 | { |
| 210 | return y * ((float)GetEFBHeight() / (float)EFB_HEIGHT); |
| 211 | } |
| 212 | |
| 213 | std::tuple<u32, u32> FramebufferManager::CalculateTargetSize(int efb_scale) |
| 214 | { |
| 215 | if (efb_scale == EFB_SCALE_AUTO_INTEGRAL) |
| 216 | m_efb_scale = g_presenter->AutoIntegralScale(); |
| 217 | else |
| 218 | m_efb_scale = efb_scale; |
| 219 | |
| 220 | const u32 max_size = g_backend_info.MaxTextureSize; |
| 221 | if (max_size < EFB_WIDTH * m_efb_scale) |
| 222 | m_efb_scale = max_size / EFB_WIDTH; |
| 223 | |
| 224 | u32 new_efb_width = std::max(EFB_WIDTH * static_cast<int>(m_efb_scale), 1u); |
| 225 | u32 new_efb_height = std::max(EFB_HEIGHT * static_cast<int>(m_efb_scale), 1u); |
| 226 | |
| 227 | return std::make_tuple(new_efb_width, new_efb_height); |
| 228 | } |
| 229 | |
| 230 | bool FramebufferManager::CreateEFBFramebuffer(int efb_scale) |
| 231 | { |
| 232 | auto [width, height] = CalculateTargetSize(efb_scale); |
| 233 | |
| 234 | const TextureConfig efb_color_texture_config = GetEFBColorTextureConfig(width, height); |
| 235 | const TextureConfig efb_depth_texture_config = GetEFBDepthTextureConfig(width, height); |
| 236 | |
| 237 | // We need a second texture to swap with for changing pixel formats |
| 238 | m_efb_color_texture = g_gfx->CreateTexture(efb_color_texture_config, "EFB color texture"); |
| 239 | m_efb_depth_texture = g_gfx->CreateTexture(efb_depth_texture_config, "EFB depth texture"); |
| 240 | m_efb_convert_color_texture = |
| 241 | g_gfx->CreateTexture(efb_color_texture_config, "EFB convert color texture"); |
| 242 | if (!m_efb_color_texture || !m_efb_depth_texture || !m_efb_convert_color_texture) |
| 243 | return false; |
| 244 | |
| 245 | m_efb_framebuffer = |
| 246 | g_gfx->CreateFramebuffer(m_efb_color_texture.get(), m_efb_depth_texture.get()); |
| 247 | m_efb_convert_framebuffer = |
| 248 | g_gfx->CreateFramebuffer(m_efb_convert_color_texture.get(), m_efb_depth_texture.get()); |
| 249 | if (!m_efb_framebuffer || !m_efb_convert_framebuffer) |
| 250 | return false; |
| 251 | |
| 252 | // Create resolved textures if MSAA is on |
| 253 | if (g_ActiveConfig.MultisamplingEnabled()) |
| 254 | { |
| 255 | u32 flags = 0; |
| 256 | if (!g_backend_info.bSupportsPartialMultisampleResolve) |
| 257 | flags |= AbstractTextureFlag_RenderTarget; |
| 258 | m_efb_resolve_color_texture = g_gfx->CreateTexture( |
| 259 | TextureConfig(efb_color_texture_config.width, efb_color_texture_config.height, 1, |
| 260 | efb_color_texture_config.layers, 1, efb_color_texture_config.format, flags, |
| 261 | AbstractTextureType::Texture_2DArray), |
| 262 | "EFB color resolve texture"); |
| 263 | if (!m_efb_resolve_color_texture) |
| 264 | return false; |
| 265 | |
| 266 | if (!g_backend_info.bSupportsPartialMultisampleResolve) |
| 267 | { |
| 268 | m_efb_color_resolve_framebuffer = |
| 269 | g_gfx->CreateFramebuffer(m_efb_resolve_color_texture.get(), nullptr); |
| 270 | if (!m_efb_color_resolve_framebuffer) |
| 271 | return false; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | // We also need one to convert the D24S8 to R32F if that is being used (Adreno). |
| 276 | if (g_ActiveConfig.MultisamplingEnabled() || GetEFBDepthFormat() != AbstractTextureFormat::R32F) |
| 277 | { |
| 278 | m_efb_depth_resolve_texture = g_gfx->CreateTexture( |
| 279 | TextureConfig(efb_depth_texture_config.width, efb_depth_texture_config.height, 1, |
| 280 | efb_depth_texture_config.layers, 1, GetEFBDepthCopyFormat(), |
| 281 | AbstractTextureFlag_RenderTarget, AbstractTextureType::Texture_2DArray), |
| 282 | "EFB depth resolve texture"); |
| 283 | if (!m_efb_depth_resolve_texture) |
| 284 | return false; |
| 285 | |
| 286 | m_efb_depth_resolve_framebuffer = |
| 287 | g_gfx->CreateFramebuffer(m_efb_depth_resolve_texture.get(), nullptr); |
| 288 | if (!m_efb_depth_resolve_framebuffer) |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | // Clear the renderable textures out. |
| 293 | g_gfx->SetAndClearFramebuffer(m_efb_framebuffer.get(), {{0.0f, 0.0f, 0.0f, 0.0f}}, |
| 294 | g_backend_info.bSupportsReversedDepthRange ? 1.0f : 0.0f); |
| 295 | |
| 296 | // Pixel Shader uses EFB scale as a constant, dirty that in case it changed |
| 297 | Core::System::GetInstance().GetPixelShaderManager().Dirty(); |
| 298 | |
| 299 | return true; |
| 300 | } |
| 301 | |
| 302 | void FramebufferManager::DestroyEFBFramebuffer() |
| 303 | { |
| 304 | m_efb_framebuffer.reset(); |
| 305 | m_efb_convert_framebuffer.reset(); |
| 306 | m_efb_color_texture.reset(); |
| 307 | m_efb_convert_color_texture.reset(); |
| 308 | m_efb_depth_texture.reset(); |
| 309 | m_efb_resolve_color_texture.reset(); |
| 310 | m_efb_depth_resolve_framebuffer.reset(); |
| 311 | m_efb_depth_resolve_texture.reset(); |
| 312 | } |
| 313 | |
| 314 | void FramebufferManager::BindEFBFramebuffer() |
| 315 | { |
| 316 | g_gfx->SetFramebuffer(m_efb_framebuffer.get()); |
| 317 | } |
| 318 | |
| 319 | AbstractTexture* FramebufferManager::ResolveEFBColorTexture(const MathUtil::Rectangle<int>& region) |
| 320 | { |
| 321 | // Return the normal EFB texture if multisampling is off. |
| 322 | if (!IsEFBMultisampled()) |
| 323 | return m_efb_color_texture.get(); |
| 324 | |
| 325 | // It's not valid to resolve an out-of-range rectangle. |
| 326 | MathUtil::Rectangle<int> clamped_region = region; |
| 327 | clamped_region.ClampUL(0, 0, GetEFBWidth(), GetEFBHeight()); |
| 328 | |
| 329 | // Resolve to our already-created texture. |
| 330 | if (g_backend_info.bSupportsPartialMultisampleResolve) |
| 331 | { |
| 332 | for (u32 layer = 0; layer < GetEFBLayers(); layer++) |
| 333 | { |
| 334 | m_efb_resolve_color_texture->ResolveFromTexture(m_efb_color_texture.get(), clamped_region, |
| 335 | layer, 0); |
| 336 | } |
| 337 | } |
| 338 | else |
| 339 | { |
| 340 | g_gfx->BeginUtilityDrawing(); |
| 341 | m_efb_color_texture->FinishedRendering(); |
| 342 | g_gfx->SetAndDiscardFramebuffer(m_efb_color_resolve_framebuffer.get()); |
| 343 | g_gfx->SetPipeline(m_efb_color_resolve_pipeline.get()); |
| 344 | g_gfx->SetTexture(0, m_efb_color_texture.get()); |
| 345 | g_gfx->SetSamplerState(0, RenderState::GetPointSamplerState()); |
| 346 | g_gfx->SetViewportAndScissor(clamped_region); |
| 347 | g_gfx->Draw(0, 3); |
| 348 | m_efb_resolve_color_texture->FinishedRendering(); |
| 349 | g_gfx->EndUtilityDrawing(); |
| 350 | } |
| 351 | m_efb_resolve_color_texture->FinishedRendering(); |
| 352 | return m_efb_resolve_color_texture.get(); |
| 353 | } |
| 354 | |
| 355 | AbstractTexture* FramebufferManager::ResolveEFBDepthTexture(const MathUtil::Rectangle<int>& region, |
| 356 | bool force_r32f) |
| 357 | { |
| 358 | if (!IsEFBMultisampled() && |
| 359 | (!force_r32f || m_efb_depth_texture->GetFormat() == AbstractTextureFormat::D32F)) |
| 360 | { |
| 361 | return m_efb_depth_texture.get(); |
| 362 | } |
| 363 | |
| 364 | // It's not valid to resolve an out-of-range rectangle. |
| 365 | MathUtil::Rectangle<int> clamped_region = region; |
| 366 | clamped_region.ClampUL(0, 0, GetEFBWidth(), GetEFBHeight()); |
| 367 | |
| 368 | g_gfx->BeginUtilityDrawing(); |
| 369 | m_efb_depth_texture->FinishedRendering(); |
| 370 | g_gfx->SetAndDiscardFramebuffer(m_efb_depth_resolve_framebuffer.get()); |
| 371 | g_gfx->SetPipeline(IsEFBMultisampled() ? m_efb_depth_resolve_pipeline.get() : |
| 372 | m_efb_depth_cache.copy_pipeline.get()); |
| 373 | g_gfx->SetTexture(0, m_efb_depth_texture.get()); |
| 374 | g_gfx->SetSamplerState(0, RenderState::GetPointSamplerState()); |
| 375 | g_gfx->SetViewportAndScissor(clamped_region); |
| 376 | g_gfx->Draw(0, 3); |
| 377 | m_efb_depth_resolve_texture->FinishedRendering(); |
| 378 | g_gfx->EndUtilityDrawing(); |
| 379 | |
| 380 | return m_efb_depth_resolve_texture.get(); |
| 381 | } |
| 382 | |
| 383 | bool FramebufferManager::ReinterpretPixelData(EFBReinterpretType convtype) |
| 384 | { |
| 385 | if (!m_format_conversion_pipelines[static_cast<u32>(convtype)]) |
| 386 | return false; |
| 387 | |
| 388 | // Draw to the secondary framebuffer. |
| 389 | // We don't discard here because discarding the framebuffer also throws away the depth |
| 390 | // buffer, which we want to preserve. If we find this to be hindering performance in the |
| 391 | // future (e.g. on mobile/tilers), it may be worth discarding only the color buffer. |
| 392 | g_gfx->BeginUtilityDrawing(); |
| 393 | m_efb_color_texture->FinishedRendering(); |
| 394 | g_gfx->SetFramebuffer(m_efb_convert_framebuffer.get()); |
| 395 | g_gfx->SetViewportAndScissor(m_efb_framebuffer->GetRect()); |
| 396 | g_gfx->SetPipeline(m_format_conversion_pipelines[static_cast<u32>(convtype)].get()); |
| 397 | g_gfx->SetTexture(0, m_efb_color_texture.get()); |
| 398 | g_gfx->Draw(0, 3); |
| 399 | |
| 400 | // And swap the framebuffers around, so we do new drawing to the converted framebuffer. |
| 401 | std::swap(m_efb_color_texture, m_efb_convert_color_texture); |
| 402 | std::swap(m_efb_framebuffer, m_efb_convert_framebuffer); |
| 403 | g_gfx->EndUtilityDrawing(); |
| 404 | InvalidatePeekCache(true); |
| 405 | return true; |
| 406 | } |
| 407 | |
| 408 | bool FramebufferManager::CompileConversionPipelines() |
| 409 | { |
| 410 | for (u32 i = 0; i < NUM_EFB_REINTERPRET_TYPES; i++) |
| 411 | { |
| 412 | EFBReinterpretType convtype = static_cast<EFBReinterpretType>(i); |
| 413 | std::unique_ptr<AbstractShader> pixel_shader = g_gfx->CreateShaderFromSource( |
| 414 | ShaderStage::Pixel, |
| 415 | FramebufferShaderGen::GenerateFormatConversionShader(convtype, GetEFBSamples()), nullptr, |
| 416 | fmt::format("Framebuffer conversion pixel shader {}", convtype)); |
| 417 | if (!pixel_shader) |
| 418 | return false; |
| 419 | |
| 420 | AbstractPipelineConfig config = {}; |
| 421 | config.vertex_shader = g_shader_cache->GetScreenQuadVertexShader(); |
| 422 | config.geometry_shader = IsEFBStereo() ? g_shader_cache->GetTexcoordGeometryShader() : nullptr; |
| 423 | config.pixel_shader = pixel_shader.get(); |
| 424 | config.rasterization_state = RenderState::GetNoCullRasterizationState(PrimitiveType::Triangles); |
| 425 | config.depth_state = RenderState::GetNoDepthTestingDepthState(); |
| 426 | config.blending_state = RenderState::GetNoBlendingBlendState(); |
| 427 | config.framebuffer_state = GetEFBFramebufferState(); |
| 428 | config.usage = AbstractPipelineUsage::Utility; |
| 429 | m_format_conversion_pipelines[i] = g_gfx->CreatePipeline(config); |
| 430 | if (!m_format_conversion_pipelines[i]) |
| 431 | return false; |
| 432 | } |
| 433 | |
| 434 | return true; |
| 435 | } |
| 436 | |
| 437 | void FramebufferManager::DestroyConversionPipelines() |
| 438 | { |
| 439 | for (auto& pipeline : m_format_conversion_pipelines) |
| 440 | pipeline.reset(); |
| 441 | } |
| 442 | |
| 443 | bool FramebufferManager::IsUsingTiledEFBCache() const |
| 444 | { |
| 445 | return m_efb_cache_tile_size > 0; |
| 446 | } |
| 447 | |
| 448 | bool FramebufferManager::IsEFBCacheTilePresent(bool depth, u32 x, u32 y, u32* tile_index) const |
| 449 | { |
| 450 | const EFBCacheData& data = depth ? m_efb_depth_cache : m_efb_color_cache; |
| 451 | if (!IsUsingTiledEFBCache()) |
| 452 | { |
| 453 | *tile_index = 0; |
| 454 | } |
| 455 | else |
| 456 | { |
| 457 | const u32 tile_x = x / m_efb_cache_tile_size; |
| 458 | const u32 tile_y = y / m_efb_cache_tile_size; |
| 459 | *tile_index = (tile_y * m_efb_cache_tile_row_stride) + tile_x; |
| 460 | } |
| 461 | return data.tiles[*tile_index].present; |
| 462 | } |
| 463 | |
| 464 | MathUtil::Rectangle<int> FramebufferManager::GetEFBCacheTileRect(u32 tile_index) const |
| 465 | { |
| 466 | if (!IsUsingTiledEFBCache()) |
| 467 | return MathUtil::Rectangle<int>(0, 0, EFB_WIDTH, EFB_HEIGHT); |
| 468 | |
| 469 | const u32 tile_y = tile_index / m_efb_cache_tile_row_stride; |
| 470 | const u32 tile_x = tile_index % m_efb_cache_tile_row_stride; |
| 471 | const u32 start_y = tile_y * m_efb_cache_tile_size; |
| 472 | const u32 start_x = tile_x * m_efb_cache_tile_size; |
| 473 | return MathUtil::Rectangle<int>( |
| 474 | start_x, start_y, std::min(start_x + m_efb_cache_tile_size, static_cast<u32>(EFB_WIDTH)), |
| 475 | std::min(start_y + m_efb_cache_tile_size, static_cast<u32>(EFB_HEIGHT))); |
| 476 | } |
| 477 | |
| 478 | u32 FramebufferManager::PeekEFBColor(u32 x, u32 y) |
| 479 | { |
| 480 | // The y coordinate here assumes upper-left origin, but the readback texture is lower-left in GL. |
| 481 | if (g_backend_info.bUsesLowerLeftOrigin) |
| 482 | y = EFB_HEIGHT - 1 - y; |
| 483 | |
| 484 | u32 tile_index; |
| 485 | if (!IsEFBCacheTilePresent(false, x, y, &tile_index)) |
| 486 | PopulateEFBCache(false, tile_index); |
| 487 | |
| 488 | m_efb_color_cache.tiles[tile_index].frame_access_mask |= 1; |
| 489 | |
| 490 | if (m_efb_color_cache.needs_flush) |
| 491 | { |
| 492 | m_efb_color_cache.readback_texture->Flush(); |
| 493 | m_efb_color_cache.needs_flush = false; |
| 494 | } |
| 495 | |
| 496 | u32 value; |
| 497 | m_efb_color_cache.readback_texture->ReadTexel(x, y, &value); |
| 498 | return value; |
| 499 | } |
| 500 | |
| 501 | float FramebufferManager::PeekEFBDepth(u32 x, u32 y) |
| 502 | { |
| 503 | // The y coordinate here assumes upper-left origin, but the readback texture is lower-left in GL. |
| 504 | if (g_backend_info.bUsesLowerLeftOrigin) |
| 505 | y = EFB_HEIGHT - 1 - y; |
| 506 | |
| 507 | u32 tile_index; |
| 508 | if (!IsEFBCacheTilePresent(true, x, y, &tile_index)) |
| 509 | PopulateEFBCache(true, tile_index); |
| 510 | |
| 511 | m_efb_depth_cache.tiles[tile_index].frame_access_mask |= 1; |
| 512 | |
| 513 | if (m_efb_depth_cache.needs_flush) |
| 514 | { |
| 515 | m_efb_depth_cache.readback_texture->Flush(); |
| 516 | m_efb_depth_cache.needs_flush = false; |
| 517 | } |
| 518 | |
| 519 | float value; |
| 520 | m_efb_depth_cache.readback_texture->ReadTexel(x, y, &value); |
| 521 | return value; |
| 522 | } |
| 523 | |
| 524 | void FramebufferManager::SetEFBCacheTileSize(u32 size) |
| 525 | { |
| 526 | if (m_efb_cache_tile_size == size) |
| 527 | return; |
| 528 | |
| 529 | InvalidatePeekCache(true); |
| 530 | m_efb_cache_tile_size = size; |
| 531 | DestroyReadbackFramebuffer(); |
| 532 | if (!CreateReadbackFramebuffer()) |
| 533 | PanicAlertFmt("Failed to create EFB readback framebuffers"); |
| 534 | } |
| 535 | |
| 536 | void FramebufferManager::RefreshPeekCache() |
| 537 | { |
| 538 | if (!m_efb_color_cache.needs_refresh && !m_efb_depth_cache.needs_refresh) |
| 539 | { |
| 540 | // The cache has already been refreshed. |
| 541 | return; |
| 542 | } |
| 543 | |
| 544 | bool flush_command_buffer = false; |
| 545 | for (u32 i = 0; i < m_efb_color_cache.tiles.size(); i++) |
| 546 | { |
| 547 | if (m_efb_color_cache.tiles[i].frame_access_mask != 0 && !m_efb_color_cache.tiles[i].present) |
| 548 | { |
| 549 | PopulateEFBCache(false, i, true); |
| 550 | flush_command_buffer = true; |
| 551 | } |
| 552 | if (m_efb_depth_cache.tiles[i].frame_access_mask != 0 && !m_efb_depth_cache.tiles[i].present) |
| 553 | { |
| 554 | PopulateEFBCache(true, i, true); |
| 555 | flush_command_buffer = true; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | m_efb_depth_cache.needs_refresh = false; |
| 560 | m_efb_color_cache.needs_refresh = false; |
| 561 | |
| 562 | if (flush_command_buffer) |
| 563 | { |
| 564 | g_gfx->Flush(); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | void FramebufferManager::InvalidatePeekCache(bool forced) |
| 569 | { |
| 570 | if (forced || m_efb_color_cache.out_of_date) |
| 571 | { |
| 572 | if (m_efb_color_cache.has_active_tiles) |
| 573 | { |
| 574 | for (u32 i = 0; i < m_efb_color_cache.tiles.size(); i++) |
| 575 | { |
| 576 | m_efb_color_cache.tiles[i].present = false; |
| 577 | } |
| 578 | |
| 579 | m_efb_color_cache.needs_refresh = true; |
| 580 | } |
| 581 | |
| 582 | m_efb_color_cache.has_active_tiles = false; |
| 583 | m_efb_color_cache.out_of_date = false; |
| 584 | } |
| 585 | if (forced || m_efb_depth_cache.out_of_date) |
| 586 | { |
| 587 | if (m_efb_depth_cache.has_active_tiles) |
| 588 | { |
| 589 | for (u32 i = 0; i < m_efb_depth_cache.tiles.size(); i++) |
| 590 | { |
| 591 | m_efb_depth_cache.tiles[i].present = false; |
| 592 | } |
| 593 | |
| 594 | m_efb_depth_cache.needs_refresh = true; |
| 595 | } |
| 596 | |
| 597 | m_efb_depth_cache.has_active_tiles = false; |
| 598 | m_efb_depth_cache.out_of_date = false; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | void FramebufferManager::FlagPeekCacheAsOutOfDate() |
| 603 | { |
| 604 | if (m_efb_color_cache.has_active_tiles) |
| 605 | m_efb_color_cache.out_of_date = true; |
| 606 | if (m_efb_depth_cache.has_active_tiles) |
| 607 | m_efb_depth_cache.out_of_date = true; |
| 608 | |
| 609 | if (!g_ActiveConfig.bEFBAccessDeferInvalidation) |
| 610 | InvalidatePeekCache(); |
| 611 | } |
| 612 | |
| 613 | void FramebufferManager::EndOfFrame() |
| 614 | { |
| 615 | for (u32 i = 0; i < m_efb_color_cache.tiles.size(); i++) |
| 616 | { |
| 617 | m_efb_color_cache.tiles[i].frame_access_mask <<= 1; |
| 618 | m_efb_depth_cache.tiles[i].frame_access_mask <<= 1; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | bool FramebufferManager::CompileReadbackPipelines() |
| 623 | { |
| 624 | AbstractPipelineConfig config = {}; |
| 625 | config.vertex_shader = g_shader_cache->GetTextureCopyVertexShader(); |
| 626 | config.geometry_shader = IsEFBStereo() ? g_shader_cache->GetTexcoordGeometryShader() : nullptr; |
| 627 | config.pixel_shader = g_shader_cache->GetTextureCopyPixelShader(); |
| 628 | config.rasterization_state = RenderState::GetNoCullRasterizationState(PrimitiveType::Triangles); |
| 629 | config.depth_state = RenderState::GetNoDepthTestingDepthState(); |
| 630 | config.blending_state = RenderState::GetNoBlendingBlendState(); |
| 631 | config.framebuffer_state = RenderState::GetColorFramebufferState(GetEFBColorFormat()); |
| 632 | config.usage = AbstractPipelineUsage::Utility; |
| 633 | m_efb_color_cache.copy_pipeline = g_gfx->CreatePipeline(config); |
| 634 | if (!m_efb_color_cache.copy_pipeline) |
| 635 | return false; |
| 636 | |
| 637 | // same for depth, except different format |
| 638 | config.framebuffer_state.color_texture_format = GetEFBDepthCopyFormat(); |
| 639 | m_efb_depth_cache.copy_pipeline = g_gfx->CreatePipeline(config); |
| 640 | if (!m_efb_depth_cache.copy_pipeline) |
| 641 | return false; |
| 642 | |
| 643 | if (IsEFBMultisampled()) |
| 644 | { |
| 645 | auto depth_resolve_shader = g_gfx->CreateShaderFromSource( |
| 646 | ShaderStage::Pixel, FramebufferShaderGen::GenerateResolveDepthPixelShader(GetEFBSamples()), |
| 647 | nullptr, "Depth resolve pixel shader"); |
| 648 | if (!depth_resolve_shader) |
| 649 | return false; |
| 650 | |
| 651 | config.pixel_shader = depth_resolve_shader.get(); |
| 652 | m_efb_depth_resolve_pipeline = g_gfx->CreatePipeline(config); |
| 653 | if (!m_efb_depth_resolve_pipeline) |
| 654 | return false; |
| 655 | |
| 656 | if (!g_backend_info.bSupportsPartialMultisampleResolve) |
| 657 | { |
| 658 | config.framebuffer_state.color_texture_format = GetEFBColorFormat(); |
| 659 | auto color_resolve_shader = g_gfx->CreateShaderFromSource( |
| 660 | ShaderStage::Pixel, |
| 661 | FramebufferShaderGen::GenerateResolveColorPixelShader(GetEFBSamples()), nullptr, |
| 662 | "Color resolve pixel shader"); |
| 663 | if (!color_resolve_shader) |
| 664 | return false; |
| 665 | |
| 666 | config.pixel_shader = color_resolve_shader.get(); |
| 667 | m_efb_color_resolve_pipeline = g_gfx->CreatePipeline(config); |
| 668 | if (!m_efb_color_resolve_pipeline) |
| 669 | return false; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | // EFB restore pipeline |
| 674 | auto restore_shader = g_gfx->CreateShaderFromSource( |
| 675 | ShaderStage::Pixel, FramebufferShaderGen::GenerateEFBRestorePixelShader(), nullptr, |
| 676 | "EFB restore pixel shader"); |
| 677 | if (!restore_shader) |
| 678 | return false; |
| 679 | |
| 680 | config.depth_state = RenderState::GetAlwaysWriteDepthState(); |
| 681 | config.framebuffer_state = GetEFBFramebufferState(); |
| 682 | config.framebuffer_state.per_sample_shading = false; |
| 683 | config.vertex_shader = g_shader_cache->GetScreenQuadVertexShader(); |
| 684 | config.pixel_shader = restore_shader.get(); |
| 685 | m_efb_restore_pipeline = g_gfx->CreatePipeline(config); |
| 686 | if (!m_efb_restore_pipeline) |
| 687 | return false; |
| 688 | |
| 689 | return true; |
| 690 | } |
| 691 | |
| 692 | void FramebufferManager::DestroyReadbackPipelines() |
| 693 | { |
| 694 | m_efb_depth_resolve_pipeline.reset(); |
| 695 | m_efb_depth_cache.copy_pipeline.reset(); |
| 696 | m_efb_color_cache.copy_pipeline.reset(); |
| 697 | } |
| 698 | |
| 699 | bool FramebufferManager::CreateReadbackFramebuffer() |
| 700 | { |
| 701 | if (GetEFBScale() != 1) |
| 702 | { |
| 703 | const TextureConfig color_config(IsUsingTiledEFBCache() ? m_efb_cache_tile_size : EFB_WIDTH, |
| 704 | IsUsingTiledEFBCache() ? m_efb_cache_tile_size : EFB_HEIGHT, 1, |
| 705 | 1, 1, GetEFBColorFormat(), AbstractTextureFlag_RenderTarget, |
| 706 | AbstractTextureType::Texture_2DArray); |
| 707 | m_efb_color_cache.texture = g_gfx->CreateTexture(color_config, "EFB color cache"); |
| 708 | if (!m_efb_color_cache.texture) |
| 709 | return false; |
| 710 | |
| 711 | m_efb_color_cache.framebuffer = |
| 712 | g_gfx->CreateFramebuffer(m_efb_color_cache.texture.get(), nullptr); |
| 713 | if (!m_efb_color_cache.framebuffer) |
| 714 | return false; |
| 715 | } |
| 716 | |
| 717 | // Since we can't partially copy from a depth buffer directly to the staging texture in D3D, we |
| 718 | // use an intermediate buffer to avoid copying the whole texture. |
| 719 | if (!g_backend_info.bSupportsDepthReadback || |
| 720 | (IsUsingTiledEFBCache() && !g_backend_info.bSupportsPartialDepthCopies) || |
| 721 | !AbstractTexture::IsCompatibleDepthAndColorFormats(m_efb_depth_texture->GetFormat(), |
| 722 | GetEFBDepthCopyFormat()) || |
| 723 | GetEFBScale() != 1) |
| 724 | { |
| 725 | const TextureConfig depth_config(IsUsingTiledEFBCache() ? m_efb_cache_tile_size : EFB_WIDTH, |
| 726 | IsUsingTiledEFBCache() ? m_efb_cache_tile_size : EFB_HEIGHT, 1, |
| 727 | 1, 1, GetEFBDepthCopyFormat(), |
| 728 | AbstractTextureFlag_RenderTarget, |
| 729 | AbstractTextureType::Texture_2DArray); |
| 730 | m_efb_depth_cache.texture = g_gfx->CreateTexture(depth_config, "EFB depth cache"); |
| 731 | if (!m_efb_depth_cache.texture) |
| 732 | return false; |
| 733 | |
| 734 | m_efb_depth_cache.framebuffer = |
| 735 | g_gfx->CreateFramebuffer(m_efb_depth_cache.texture.get(), nullptr); |
| 736 | if (!m_efb_depth_cache.framebuffer) |
| 737 | return false; |
| 738 | } |
| 739 | |
| 740 | // Staging texture use the full EFB dimensions, as this is the buffer for the whole cache. |
| 741 | m_efb_color_cache.readback_texture = |
| 742 | g_gfx->CreateStagingTexture(StagingTextureType::Mutable, |
| 743 | TextureConfig(EFB_WIDTH, EFB_HEIGHT, 1, 1, 1, GetEFBColorFormat(), |
| 744 | 0, AbstractTextureType::Texture_2DArray)); |
| 745 | m_efb_depth_cache.readback_texture = g_gfx->CreateStagingTexture( |
| 746 | StagingTextureType::Mutable, |
| 747 | TextureConfig(EFB_WIDTH, EFB_HEIGHT, 1, 1, 1, GetEFBDepthCopyFormat(), 0, |
| 748 | AbstractTextureType::Texture_2DArray)); |
| 749 | if (!m_efb_color_cache.readback_texture || !m_efb_depth_cache.readback_texture) |
| 750 | return false; |
| 751 | |
| 752 | u32 total_tiles = 1; |
| 753 | if (IsUsingTiledEFBCache()) |
| 754 | { |
| 755 | const u32 tiles_wide = ((EFB_WIDTH + (m_efb_cache_tile_size - 1)) / m_efb_cache_tile_size); |
| 756 | const u32 tiles_high = ((EFB_HEIGHT + (m_efb_cache_tile_size - 1)) / m_efb_cache_tile_size); |
| 757 | total_tiles = tiles_wide * tiles_high; |
| 758 | m_efb_cache_tile_row_stride = tiles_wide; |
| 759 | } |
| 760 | else |
| 761 | { |
| 762 | m_efb_cache_tile_row_stride = 1; |
| 763 | } |
| 764 | |
| 765 | m_efb_color_cache.tiles.resize(total_tiles); |
| 766 | std::ranges::fill(m_efb_color_cache.tiles, EFBCacheTile{false, 0}); |
| 767 | m_efb_depth_cache.tiles.resize(total_tiles); |
| 768 | std::ranges::fill(m_efb_depth_cache.tiles, EFBCacheTile{false, 0}); |
| 769 | |
| 770 | return true; |
| 771 | } |
| 772 | |
| 773 | void FramebufferManager::DestroyReadbackFramebuffer() |
| 774 | { |
| 775 | auto DestroyCache = [](EFBCacheData& data) { |
| 776 | data.readback_texture.reset(); |
| 777 | data.framebuffer.reset(); |
| 778 | data.texture.reset(); |
| 779 | data.needs_refresh = false; |
| 780 | data.has_active_tiles = false; |
| 781 | }; |
| 782 | DestroyCache(m_efb_color_cache); |
| 783 | DestroyCache(m_efb_depth_cache); |
| 784 | } |
| 785 | |
| 786 | void FramebufferManager::PopulateEFBCache(bool depth, u32 tile_index, bool async) |
| 787 | { |
| 788 | FlushEFBPokes(); |
| 789 | g_vertex_manager->OnCPUEFBAccess(); |
| 790 | |
| 791 | // Force the path through the intermediate texture, as we can't do an image copy from a depth |
| 792 | // buffer directly to a staging texture (must be the whole resource). |
| 793 | const bool force_intermediate_copy = |
| 794 | depth && (!g_backend_info.bSupportsDepthReadback || |
| 795 | (!g_backend_info.bSupportsPartialDepthCopies && IsUsingTiledEFBCache()) || |
| 796 | !AbstractTexture::IsCompatibleDepthAndColorFormats(m_efb_depth_texture->GetFormat(), |
| 797 | GetEFBDepthCopyFormat())); |
| 798 | |
| 799 | // Issue a copy from framebuffer -> copy texture if we have >1xIR or MSAA on. |
| 800 | EFBCacheData& data = depth ? m_efb_depth_cache : m_efb_color_cache; |
| 801 | const MathUtil::Rectangle<int> rect = GetEFBCacheTileRect(tile_index); |
| 802 | const MathUtil::Rectangle<int> native_rect = ConvertEFBRectangle(rect); |
| 803 | AbstractTexture* src_texture = |
| 804 | depth ? ResolveEFBDepthTexture(native_rect) : ResolveEFBColorTexture(native_rect); |
| 805 | if (GetEFBScale() != 1 || force_intermediate_copy) |
| 806 | { |
| 807 | // Downsample from internal resolution to 1x. |
| 808 | // TODO: This won't produce correct results at IRs above 2x. More samples are required. |
| 809 | // This is the same issue as with EFB copies. |
| 810 | g_gfx->BeginUtilityDrawing(); |
| 811 | src_texture->FinishedRendering(); |
| 812 | |
| 813 | const float rcp_src_width = 1.0f / m_efb_framebuffer->GetWidth(); |
| 814 | const float rcp_src_height = 1.0f / m_efb_framebuffer->GetHeight(); |
| 815 | const std::array<float, 4> uniforms = { |
| 816 | {native_rect.left * rcp_src_width, native_rect.top * rcp_src_height, |
| 817 | native_rect.GetWidth() * rcp_src_width, native_rect.GetHeight() * rcp_src_height}}; |
| 818 | g_vertex_manager->UploadUtilityUniforms(&uniforms, sizeof(uniforms)); |
| 819 | |
| 820 | // Viewport will not be TILE_SIZExTILE_SIZE for the last row of tiles, assuming a tile size of |
| 821 | // 64, because 528 is not evenly divisible by 64. |
| 822 | g_gfx->SetAndDiscardFramebuffer(data.framebuffer.get()); |
| 823 | g_gfx->SetViewportAndScissor(MathUtil::Rectangle<int>(0, 0, rect.GetWidth(), rect.GetHeight())); |
| 824 | g_gfx->SetPipeline(data.copy_pipeline.get()); |
| 825 | g_gfx->SetTexture(0, src_texture); |
| 826 | g_gfx->SetSamplerState(0, depth ? RenderState::GetPointSamplerState() : |
| 827 | RenderState::GetLinearSamplerState()); |
| 828 | g_gfx->Draw(0, 3); |
| 829 | |
| 830 | // Copy from EFB or copy texture to staging texture. |
| 831 | // No need to call FinishedRendering() here because CopyFromTexture() transitions. |
| 832 | data.readback_texture->CopyFromTexture( |
| 833 | data.texture.get(), MathUtil::Rectangle<int>(0, 0, rect.GetWidth(), rect.GetHeight()), 0, 0, |
| 834 | rect); |
| 835 | |
| 836 | g_gfx->EndUtilityDrawing(); |
| 837 | } |
| 838 | else |
| 839 | { |
| 840 | data.readback_texture->CopyFromTexture(src_texture, rect, 0, 0, rect); |
| 841 | } |
| 842 | |
| 843 | // Wait until the copy is complete. |
| 844 | if (!async) |
| 845 | { |
| 846 | data.readback_texture->Flush(); |
| 847 | data.needs_flush = false; |
| 848 | } |
| 849 | else |
| 850 | { |
| 851 | data.needs_flush = true; |
| 852 | } |
| 853 | data.has_active_tiles = true; |
| 854 | data.out_of_date = false; |
| 855 | data.tiles[tile_index].present = true; |
| 856 | } |
| 857 | |
| 858 | void FramebufferManager::ClearEFB(const MathUtil::Rectangle<int>& rc, bool color_enable, |
| 859 | bool alpha_enable, bool z_enable, u32 color, u32 z, |
| 860 | PixelFormat pixel_format) |
| 861 | { |
| 862 | FlushEFBPokes(); |
| 863 | FlagPeekCacheAsOutOfDate(); |
| 864 | |
| 865 | // Native -> EFB coordinates |
| 866 | MathUtil::Rectangle<int> target_rc = ConvertEFBRectangle(rc); |
| 867 | target_rc = g_gfx->ConvertFramebufferRectangle(target_rc, m_efb_framebuffer.get()); |
| 868 | target_rc.ClampUL(0, 0, m_efb_framebuffer->GetWidth(), m_efb_framebuffer->GetWidth()); |
| 869 | |
| 870 | // Determine whether the EFB has an alpha channel. If it doesn't, we can clear the alpha |
| 871 | // channel to 0xFF. |
| 872 | // On backends that don't allow masking Alpha clears, this allows us to use the fast path |
| 873 | // almost all the time |
| 874 | if (pixel_format == PixelFormat::RGB565_Z16 || pixel_format == PixelFormat::RGB8_Z24 || |
| 875 | pixel_format == PixelFormat::Z24) |
| 876 | { |
| 877 | // Force alpha writes, and clear the alpha channel. |
| 878 | alpha_enable = true; |
| 879 | color &= 0x00FFFFFF; |
| 880 | } |
| 881 | |
| 882 | g_gfx->ClearRegion(target_rc, color_enable, alpha_enable, z_enable, color, z); |
| 883 | } |
| 884 | |
| 885 | bool FramebufferManager::CompileClearPipelines() |
| 886 | { |
| 887 | auto vertex_shader = g_gfx->CreateShaderFromSource( |
| 888 | ShaderStage::Vertex, FramebufferShaderGen::GenerateClearVertexShader(), nullptr, |
| 889 | "Clear vertex shader"); |
| 890 | if (!vertex_shader) |
| 891 | return false; |
| 892 | |
| 893 | AbstractPipelineConfig config; |
| 894 | config.vertex_format = nullptr; |
| 895 | config.vertex_shader = vertex_shader.get(); |
| 896 | config.geometry_shader = IsEFBStereo() ? g_shader_cache->GetColorGeometryShader() : nullptr; |
| 897 | config.pixel_shader = g_shader_cache->GetColorPixelShader(); |
| 898 | config.rasterization_state = RenderState::GetNoCullRasterizationState(PrimitiveType::Triangles); |
| 899 | config.depth_state = RenderState::GetAlwaysWriteDepthState(); |
| 900 | config.blending_state = RenderState::GetNoBlendingBlendState(); |
| 901 | config.framebuffer_state = GetEFBFramebufferState(); |
| 902 | config.usage = AbstractPipelineUsage::Utility; |
| 903 | |
| 904 | for (u32 color_enable = 0; color_enable < 2; color_enable++) |
| 905 | { |
| 906 | config.blending_state.color_update = color_enable != 0; |
| 907 | for (u32 alpha_enable = 0; alpha_enable < 2; alpha_enable++) |
| 908 | { |
| 909 | config.blending_state.alpha_update = alpha_enable != 0; |
| 910 | for (u32 depth_enable = 0; depth_enable < 2; depth_enable++) |
| 911 | { |
| 912 | config.depth_state.test_enable = depth_enable != 0; |
| 913 | config.depth_state.update_enable = depth_enable != 0; |
| 914 | |
| 915 | m_clear_pipelines[color_enable][alpha_enable][depth_enable] = g_gfx->CreatePipeline(config); |
| 916 | if (!m_clear_pipelines[color_enable][alpha_enable][depth_enable]) |
| 917 | return false; |
| 918 | } |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | return true; |
| 923 | } |
| 924 | |
| 925 | void FramebufferManager::DestroyClearPipelines() |
| 926 | { |
| 927 | for (u32 color_enable = 0; color_enable < 2; color_enable++) |
| 928 | { |
| 929 | for (u32 alpha_enable = 0; alpha_enable < 2; alpha_enable++) |
| 930 | { |
| 931 | for (u32 depth_enable = 0; depth_enable < 2; depth_enable++) |
| 932 | { |
| 933 | m_clear_pipelines[color_enable][alpha_enable][depth_enable].reset(); |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | AbstractPipeline* FramebufferManager::GetClearPipeline(bool colorEnable, bool alphaEnable, |
| 940 | bool zEnable) const |
| 941 | { |
| 942 | return m_clear_pipelines[colorEnable][alphaEnable][zEnable].get(); |
| 943 | } |
| 944 | |
| 945 | void FramebufferManager::PokeEFBColor(u32 x, u32 y, u32 color) |
| 946 | { |
| 947 | // Flush if we exceeded the number of vertices per batch. |
| 948 | if ((m_color_poke_vertices.size() + 6) > MAX_POKE_VERTICES) |
| 949 | FlushEFBPokes(); |
| 950 | |
| 951 | CreatePokeVertices(&m_color_poke_vertices, x, y, 0.0f, color); |
| 952 | |
| 953 | // See comment above for reasoning for lower-left coordinates. |
| 954 | if (g_backend_info.bUsesLowerLeftOrigin) |
| 955 | y = EFB_HEIGHT - 1 - y; |
| 956 | |
| 957 | // Update the peek cache if it's valid, since we know the color of the pixel now. |
| 958 | u32 tile_index; |
| 959 | if (IsEFBCacheTilePresent(false, x, y, &tile_index)) |
| 960 | m_efb_color_cache.readback_texture->WriteTexel(x, y, &color); |
| 961 | } |
| 962 | |
| 963 | void FramebufferManager::PokeEFBDepth(u32 x, u32 y, float depth) |
| 964 | { |
| 965 | // Flush if we exceeded the number of vertices per batch. |
| 966 | if ((m_depth_poke_vertices.size() + 6) > MAX_POKE_VERTICES) |
| 967 | FlushEFBPokes(); |
| 968 | |
| 969 | CreatePokeVertices(&m_depth_poke_vertices, x, y, depth, 0); |
| 970 | |
| 971 | // See comment above for reasoning for lower-left coordinates. |
| 972 | if (g_backend_info.bUsesLowerLeftOrigin) |
| 973 | y = EFB_HEIGHT - 1 - y; |
| 974 | |
| 975 | // Update the peek cache if it's valid, since we know the color of the pixel now. |
| 976 | u32 tile_index; |
| 977 | if (IsEFBCacheTilePresent(true, x, y, &tile_index)) |
| 978 | m_efb_depth_cache.readback_texture->WriteTexel(x, y, &depth); |
| 979 | } |
| 980 | |
| 981 | void FramebufferManager::CreatePokeVertices(std::vector<EFBPokeVertex>* destination_list, u32 x, |
| 982 | u32 y, float z, u32 color) |
| 983 | { |
| 984 | const float cs_pixel_width = 1.0f / EFB_WIDTH * 2.0f; |
| 985 | const float cs_pixel_height = 1.0f / EFB_HEIGHT * 2.0f; |
| 986 | if (g_backend_info.bSupportsLargePoints) |
| 987 | { |
| 988 | // GPU will expand the point to a quad. |
| 989 | const float cs_x = (static_cast<float>(x) + 0.5f) * cs_pixel_width - 1.0f; |
| 990 | const float cs_y = 1.0f - (static_cast<float>(y) + 0.5f) * cs_pixel_height; |
| 991 | const float point_size = static_cast<float>(GetEFBScale()); |
| 992 | destination_list->push_back({{cs_x, cs_y, z, point_size}, color}); |
| 993 | return; |
| 994 | } |
| 995 | |
| 996 | // Generate quad from the single point (clip-space coordinates). |
| 997 | const float x1 = static_cast<float>(x) * cs_pixel_width - 1.0f; |
| 998 | const float y1 = 1.0f - static_cast<float>(y) * cs_pixel_height; |
| 999 | const float x2 = x1 + cs_pixel_width; |
| 1000 | const float y2 = y1 - cs_pixel_height; |
| 1001 | destination_list->push_back({{x1, y1, z, 1.0f}, color}); |
| 1002 | destination_list->push_back({{x2, y1, z, 1.0f}, color}); |
| 1003 | destination_list->push_back({{x1, y2, z, 1.0f}, color}); |
| 1004 | destination_list->push_back({{x1, y2, z, 1.0f}, color}); |
| 1005 | destination_list->push_back({{x2, y1, z, 1.0f}, color}); |
| 1006 | destination_list->push_back({{x2, y2, z, 1.0f}, color}); |
| 1007 | } |
| 1008 | |
| 1009 | void FramebufferManager::FlushEFBPokes() |
| 1010 | { |
| 1011 | if (!m_color_poke_vertices.empty()) |
| 1012 | { |
| 1013 | DrawPokeVertices(m_color_poke_vertices.data(), static_cast<u32>(m_color_poke_vertices.size()), |
| 1014 | m_color_poke_pipeline.get()); |
| 1015 | m_color_poke_vertices.clear(); |
| 1016 | } |
| 1017 | |
| 1018 | if (!m_depth_poke_vertices.empty()) |
| 1019 | { |
| 1020 | DrawPokeVertices(m_depth_poke_vertices.data(), static_cast<u32>(m_depth_poke_vertices.size()), |
| 1021 | m_depth_poke_pipeline.get()); |
| 1022 | m_depth_poke_vertices.clear(); |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | void FramebufferManager::DrawPokeVertices(const EFBPokeVertex* vertices, u32 vertex_count, |
| 1027 | const AbstractPipeline* pipeline) |
| 1028 | { |
| 1029 | // Copy to vertex buffer. |
| 1030 | g_gfx->BeginUtilityDrawing(); |
| 1031 | u32 base_vertex, base_index; |
| 1032 | g_vertex_manager->UploadUtilityVertices(vertices, sizeof(EFBPokeVertex), |
| 1033 | static_cast<u32>(vertex_count), nullptr, 0, &base_vertex, |
| 1034 | &base_index); |
| 1035 | |
| 1036 | // Now we can draw. |
| 1037 | g_gfx->SetViewportAndScissor(m_efb_framebuffer->GetRect()); |
| 1038 | g_gfx->SetPipeline(pipeline); |
| 1039 | g_gfx->Draw(base_vertex, vertex_count); |
| 1040 | g_gfx->EndUtilityDrawing(); |
| 1041 | } |
| 1042 | |
| 1043 | bool FramebufferManager::CompilePokePipelines() |
| 1044 | { |
| 1045 | PortableVertexDeclaration vtx_decl = {}; |
| 1046 | vtx_decl.position.enable = true; |
| 1047 | vtx_decl.position.type = ComponentFormat::Float; |
| 1048 | vtx_decl.position.components = 4; |
| 1049 | vtx_decl.position.integer = false; |
| 1050 | vtx_decl.position.offset = offsetof(EFBPokeVertex, position); |
| 1051 | vtx_decl.colors[0].enable = true; |
| 1052 | vtx_decl.colors[0].type = ComponentFormat::UByte; |
| 1053 | vtx_decl.colors[0].components = 4; |
| 1054 | vtx_decl.colors[0].integer = false; |
| 1055 | vtx_decl.colors[0].offset = offsetof(EFBPokeVertex, color); |
| 1056 | vtx_decl.stride = sizeof(EFBPokeVertex); |
| 1057 | |
| 1058 | m_poke_vertex_format = g_gfx->CreateNativeVertexFormat(vtx_decl); |
| 1059 | if (!m_poke_vertex_format) |
| 1060 | return false; |
| 1061 | |
| 1062 | auto poke_vertex_shader = g_gfx->CreateShaderFromSource( |
| 1063 | ShaderStage::Vertex, FramebufferShaderGen::GenerateEFBPokeVertexShader(), nullptr, |
| 1064 | "EFB poke vertex shader"); |
| 1065 | if (!poke_vertex_shader) |
| 1066 | return false; |
| 1067 | |
| 1068 | AbstractPipelineConfig config = {}; |
| 1069 | config.vertex_format = m_poke_vertex_format.get(); |
| 1070 | config.vertex_shader = poke_vertex_shader.get(); |
| 1071 | config.geometry_shader = IsEFBStereo() ? g_shader_cache->GetColorGeometryShader() : nullptr; |
| 1072 | config.pixel_shader = g_shader_cache->GetColorPixelShader(); |
| 1073 | config.rasterization_state = RenderState::GetNoCullRasterizationState( |
| 1074 | g_backend_info.bSupportsLargePoints ? PrimitiveType::Points : PrimitiveType::Triangles); |
| 1075 | config.depth_state = RenderState::GetNoDepthTestingDepthState(); |
| 1076 | config.blending_state = RenderState::GetNoBlendingBlendState(); |
| 1077 | config.framebuffer_state = GetEFBFramebufferState(); |
| 1078 | config.usage = AbstractPipelineUsage::Utility; |
| 1079 | m_color_poke_pipeline = g_gfx->CreatePipeline(config); |
| 1080 | if (!m_color_poke_pipeline) |
| 1081 | return false; |
| 1082 | |
| 1083 | // Turn off color writes, depth writes on for depth pokes. |
| 1084 | config.depth_state = RenderState::GetAlwaysWriteDepthState(); |
| 1085 | config.blending_state = RenderState::GetNoColorWriteBlendState(); |
| 1086 | m_depth_poke_pipeline = g_gfx->CreatePipeline(config); |
| 1087 | if (!m_depth_poke_pipeline) |
| 1088 | return false; |
| 1089 | |
| 1090 | return true; |
| 1091 | } |
| 1092 | |
| 1093 | void FramebufferManager::DestroyPokePipelines() |
| 1094 | { |
| 1095 | m_depth_poke_pipeline.reset(); |
| 1096 | m_color_poke_pipeline.reset(); |
| 1097 | m_poke_vertex_format.reset(); |
| 1098 | } |
| 1099 | |
| 1100 | void FramebufferManager::DoState(PointerWrap& p) |
| 1101 | { |
| 1102 | FlushEFBPokes(); |
| 1103 | p.Do(m_prev_efb_format); |
| 1104 | |
| 1105 | bool save_efb_state = Config::Get(Config::GFX_SAVE_TEXTURE_CACHE_TO_STATE); |
| 1106 | p.Do(save_efb_state); |
| 1107 | if (!save_efb_state) |
| 1108 | return; |
| 1109 | |
| 1110 | if (p.IsWriteMode() || p.IsMeasureMode()) |
| 1111 | DoSaveState(p); |
| 1112 | else |
| 1113 | DoLoadState(p); |
| 1114 | } |
| 1115 | |
| 1116 | void FramebufferManager::DoSaveState(PointerWrap& p) |
| 1117 | { |
| 1118 | // For multisampling, we need to resolve first before we can save. |
| 1119 | // This won't be bit-exact when loading, which could cause interesting rendering side-effects for |
| 1120 | // a frame. But whatever, MSAA doesn't exactly behave that well anyway. |
| 1121 | AbstractTexture* color_texture = ResolveEFBColorTexture(m_efb_color_texture->GetRect()); |
| 1122 | AbstractTexture* depth_texture = ResolveEFBDepthTexture(m_efb_depth_texture->GetRect(), true); |
| 1123 | |
| 1124 | // We don't want to save these as rendertarget textures, just the data itself when deserializing. |
| 1125 | const TextureConfig color_texture_config( |
| 1126 | color_texture->GetWidth(), color_texture->GetHeight(), color_texture->GetLevels(), |
| 1127 | color_texture->GetLayers(), 1, GetEFBColorFormat(), 0, AbstractTextureType::Texture_2DArray); |
| 1128 | g_texture_cache->SerializeTexture(color_texture, color_texture_config, p); |
| 1129 | |
| 1130 | const TextureConfig depth_texture_config(depth_texture->GetWidth(), depth_texture->GetHeight(), |
| 1131 | depth_texture->GetLevels(), depth_texture->GetLayers(), |
| 1132 | 1, GetEFBDepthCopyFormat(), 0, |
| 1133 | AbstractTextureType::Texture_2DArray); |
| 1134 | g_texture_cache->SerializeTexture(depth_texture, depth_texture_config, p); |
| 1135 | } |
| 1136 | |
| 1137 | void FramebufferManager::DoLoadState(PointerWrap& p) |
| 1138 | { |
| 1139 | // Invalidate any peek cache tiles. |
| 1140 | InvalidatePeekCache(true); |
| 1141 | |
| 1142 | // Deserialize the color and depth textures. This could fail. |
| 1143 | auto color_tex = g_texture_cache->DeserializeTexture(p); |
| 1144 | auto depth_tex = g_texture_cache->DeserializeTexture(p); |
| 1145 | |
| 1146 | // If the stereo mode is different in the save state, throw it away. |
| 1147 | if (!color_tex || !depth_tex || |
| 1148 | color_tex->texture->GetLayers() != m_efb_color_texture->GetLayers()) |
| 1149 | { |
| 1150 | WARN_LOG_FMT(VIDEO, "Failed to deserialize EFB contents. Clearing instead."); |
| 1151 | g_gfx->SetAndClearFramebuffer(m_efb_framebuffer.get(), {{0.0f, 0.0f, 0.0f, 0.0f}}, |
| 1152 | g_backend_info.bSupportsReversedDepthRange ? 1.0f : 0.0f); |
| 1153 | return; |
| 1154 | } |
| 1155 | |
| 1156 | // Size differences are okay here, since the linear filtering will downscale/upscale it. |
| 1157 | // Depth buffer is always point sampled, since we don't want to interpolate depth values. |
| 1158 | const bool rescale = color_tex->texture->GetWidth() != m_efb_color_texture->GetWidth() || |
| 1159 | color_tex->texture->GetHeight() != m_efb_color_texture->GetHeight(); |
| 1160 | |
| 1161 | // Draw the deserialized textures over the EFB. |
| 1162 | g_gfx->BeginUtilityDrawing(); |
| 1163 | g_gfx->SetAndDiscardFramebuffer(m_efb_framebuffer.get()); |
| 1164 | g_gfx->SetViewportAndScissor(m_efb_framebuffer->GetRect()); |
| 1165 | g_gfx->SetPipeline(m_efb_restore_pipeline.get()); |
| 1166 | g_gfx->SetTexture(0, color_tex->texture.get()); |
| 1167 | g_gfx->SetTexture(1, depth_tex->texture.get()); |
| 1168 | g_gfx->SetSamplerState(0, rescale ? RenderState::GetLinearSamplerState() : |
| 1169 | RenderState::GetPointSamplerState()); |
| 1170 | g_gfx->SetSamplerState(1, RenderState::GetPointSamplerState()); |
| 1171 | g_gfx->Draw(0, 3); |
| 1172 | g_gfx->EndUtilityDrawing(); |
| 1173 | } |