mirror of https://github.com/stenzek/duckstation
GPU: Split backend into Backend+Presenter
parent
8f19ac2dee
commit
d52bf795e4
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,943 @@
|
||||
// SPDX-FileCopyrightText: 2019-2025 Connor McLaughlin <stenzek@gmail.com>
|
||||
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
|
||||
|
||||
#include "gpu_presenter.h"
|
||||
#include "fullscreen_ui.h"
|
||||
#include "gpu.h"
|
||||
#include "gpu_backend.h"
|
||||
#include "gpu_shadergen.h"
|
||||
#include "gpu_thread.h"
|
||||
#include "gpu_thread_commands.h"
|
||||
#include "host.h"
|
||||
#include "imgui_overlays.h"
|
||||
#include "performance_counters.h"
|
||||
#include "save_state_version.h"
|
||||
#include "settings.h"
|
||||
#include "system.h"
|
||||
|
||||
#include "util/gpu_device.h"
|
||||
#include "util/image.h"
|
||||
#include "util/imgui_fullscreen.h"
|
||||
#include "util/imgui_manager.h"
|
||||
#include "util/media_capture.h"
|
||||
#include "util/postprocessing.h"
|
||||
#include "util/state_wrapper.h"
|
||||
|
||||
#include "common/align.h"
|
||||
#include "common/error.h"
|
||||
#include "common/file_system.h"
|
||||
#include "common/gsvector_formatter.h"
|
||||
#include "common/log.h"
|
||||
#include "common/path.h"
|
||||
#include "common/small_string.h"
|
||||
#include "common/string_util.h"
|
||||
#include "common/threading.h"
|
||||
#include "common/timer.h"
|
||||
|
||||
#include <numbers>
|
||||
|
||||
LOG_CHANNEL(GPU);
|
||||
|
||||
static constexpr GPUTexture::Format DISPLAY_INTERNAL_POSTFX_FORMAT = GPUTexture::Format::RGBA8;
|
||||
|
||||
GPUPresenter::GPUPresenter() = default;
|
||||
|
||||
GPUPresenter::~GPUPresenter()
|
||||
{
|
||||
DestroyDeinterlaceTextures();
|
||||
g_gpu_device->RecycleTexture(std::move(m_chroma_smoothing_texture));
|
||||
}
|
||||
|
||||
bool GPUPresenter::Initialize(Error* error)
|
||||
{
|
||||
if (!CompileDisplayPipelines(true, true, g_gpu_settings.display_24bit_chroma_smoothing, error))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void GPUPresenter::UpdateSettings(const GPUSettings& old_settings)
|
||||
{
|
||||
if (g_gpu_settings.display_scaling != old_settings.display_scaling ||
|
||||
g_gpu_settings.display_deinterlacing_mode != old_settings.display_deinterlacing_mode ||
|
||||
g_gpu_settings.display_24bit_chroma_smoothing != old_settings.display_24bit_chroma_smoothing)
|
||||
{
|
||||
// Toss buffers on mode change.
|
||||
if (g_gpu_settings.display_deinterlacing_mode != old_settings.display_deinterlacing_mode)
|
||||
DestroyDeinterlaceTextures();
|
||||
|
||||
if (!CompileDisplayPipelines(
|
||||
g_gpu_settings.display_scaling != old_settings.display_scaling,
|
||||
g_gpu_settings.display_deinterlacing_mode != old_settings.display_deinterlacing_mode,
|
||||
g_gpu_settings.display_24bit_chroma_smoothing != old_settings.display_24bit_chroma_smoothing, nullptr))
|
||||
{
|
||||
Panic("Failed to compile display pipeline on settings change.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool GPUPresenter::CompileDisplayPipelines(bool display, bool deinterlace, bool chroma_smoothing, Error* error)
|
||||
{
|
||||
const GPUShaderGen shadergen(g_gpu_device->GetRenderAPI(), g_gpu_device->GetFeatures().dual_source_blend,
|
||||
g_gpu_device->GetFeatures().framebuffer_fetch);
|
||||
|
||||
GPUPipeline::GraphicsConfig plconfig;
|
||||
plconfig.primitive = GPUPipeline::Primitive::Triangles;
|
||||
plconfig.rasterization = GPUPipeline::RasterizationState::GetNoCullState();
|
||||
plconfig.depth = GPUPipeline::DepthState::GetNoTestsState();
|
||||
plconfig.blend = GPUPipeline::BlendState::GetNoBlendingState();
|
||||
plconfig.geometry_shader = nullptr;
|
||||
plconfig.depth_format = GPUTexture::Format::Unknown;
|
||||
plconfig.samples = 1;
|
||||
plconfig.per_sample_shading = false;
|
||||
plconfig.render_pass_flags = GPUPipeline::NoRenderPassFlags;
|
||||
|
||||
if (display)
|
||||
{
|
||||
GPUBackend::SetScreenQuadInputLayout(plconfig);
|
||||
|
||||
plconfig.layout = GPUPipeline::Layout::SingleTextureAndPushConstants;
|
||||
plconfig.SetTargetFormats(g_gpu_device->HasMainSwapChain() ? g_gpu_device->GetMainSwapChain()->GetFormat() :
|
||||
GPUTexture::Format::RGBA8);
|
||||
|
||||
std::string vs = shadergen.GenerateDisplayVertexShader();
|
||||
std::string fs;
|
||||
switch (g_gpu_settings.display_scaling)
|
||||
{
|
||||
case DisplayScalingMode::BilinearSharp:
|
||||
fs = shadergen.GenerateDisplaySharpBilinearFragmentShader();
|
||||
break;
|
||||
|
||||
case DisplayScalingMode::BilinearSmooth:
|
||||
case DisplayScalingMode::BilinearInteger:
|
||||
fs = shadergen.GenerateDisplayFragmentShader(true, false);
|
||||
break;
|
||||
|
||||
case DisplayScalingMode::Nearest:
|
||||
case DisplayScalingMode::NearestInteger:
|
||||
default:
|
||||
fs = shadergen.GenerateDisplayFragmentShader(false, true);
|
||||
break;
|
||||
}
|
||||
|
||||
std::unique_ptr<GPUShader> vso =
|
||||
g_gpu_device->CreateShader(GPUShaderStage::Vertex, shadergen.GetLanguage(), vs, error);
|
||||
std::unique_ptr<GPUShader> fso =
|
||||
g_gpu_device->CreateShader(GPUShaderStage::Fragment, shadergen.GetLanguage(), fs, error);
|
||||
if (!vso || !fso)
|
||||
return false;
|
||||
GL_OBJECT_NAME(vso, "Display Vertex Shader");
|
||||
GL_OBJECT_NAME_FMT(fso, "Display Fragment Shader [{}]",
|
||||
Settings::GetDisplayScalingName(g_gpu_settings.display_scaling));
|
||||
plconfig.vertex_shader = vso.get();
|
||||
plconfig.fragment_shader = fso.get();
|
||||
if (!(m_display_pipeline = g_gpu_device->CreatePipeline(plconfig, error)))
|
||||
return false;
|
||||
GL_OBJECT_NAME_FMT(m_display_pipeline, "Display Pipeline [{}]",
|
||||
Settings::GetDisplayScalingName(g_gpu_settings.display_scaling));
|
||||
}
|
||||
|
||||
plconfig.input_layout = {};
|
||||
plconfig.primitive = GPUPipeline::Primitive::Triangles;
|
||||
|
||||
if (deinterlace)
|
||||
{
|
||||
std::unique_ptr<GPUShader> vso = g_gpu_device->CreateShader(GPUShaderStage::Vertex, shadergen.GetLanguage(),
|
||||
shadergen.GenerateScreenQuadVertexShader(), error);
|
||||
if (!vso)
|
||||
return false;
|
||||
GL_OBJECT_NAME(vso, "Deinterlace Vertex Shader");
|
||||
|
||||
plconfig.layout = GPUPipeline::Layout::SingleTextureAndPushConstants;
|
||||
plconfig.vertex_shader = vso.get();
|
||||
plconfig.SetTargetFormats(GPUTexture::Format::RGBA8);
|
||||
|
||||
switch (g_gpu_settings.display_deinterlacing_mode)
|
||||
{
|
||||
case DisplayDeinterlacingMode::Disabled:
|
||||
case DisplayDeinterlacingMode::Progressive:
|
||||
break;
|
||||
|
||||
case DisplayDeinterlacingMode::Weave:
|
||||
{
|
||||
std::unique_ptr<GPUShader> fso = g_gpu_device->CreateShader(
|
||||
GPUShaderStage::Fragment, shadergen.GetLanguage(), shadergen.GenerateDeinterlaceWeaveFragmentShader(), error);
|
||||
if (!fso)
|
||||
return false;
|
||||
|
||||
GL_OBJECT_NAME(fso, "Weave Deinterlace Fragment Shader");
|
||||
|
||||
plconfig.layout = GPUPipeline::Layout::SingleTextureAndPushConstants;
|
||||
plconfig.vertex_shader = vso.get();
|
||||
plconfig.fragment_shader = fso.get();
|
||||
if (!(m_deinterlace_pipeline = g_gpu_device->CreatePipeline(plconfig, error)))
|
||||
return false;
|
||||
|
||||
GL_OBJECT_NAME(m_deinterlace_pipeline, "Weave Deinterlace Pipeline");
|
||||
}
|
||||
break;
|
||||
|
||||
case DisplayDeinterlacingMode::Blend:
|
||||
{
|
||||
std::unique_ptr<GPUShader> fso = g_gpu_device->CreateShader(
|
||||
GPUShaderStage::Fragment, shadergen.GetLanguage(), shadergen.GenerateDeinterlaceBlendFragmentShader(), error);
|
||||
if (!fso)
|
||||
return false;
|
||||
|
||||
GL_OBJECT_NAME(fso, "Blend Deinterlace Fragment Shader");
|
||||
|
||||
plconfig.layout = GPUPipeline::Layout::MultiTextureAndPushConstants;
|
||||
plconfig.vertex_shader = vso.get();
|
||||
plconfig.fragment_shader = fso.get();
|
||||
if (!(m_deinterlace_pipeline = g_gpu_device->CreatePipeline(plconfig, error)))
|
||||
return false;
|
||||
|
||||
GL_OBJECT_NAME(m_deinterlace_pipeline, "Blend Deinterlace Pipeline");
|
||||
}
|
||||
break;
|
||||
|
||||
case DisplayDeinterlacingMode::Adaptive:
|
||||
{
|
||||
std::unique_ptr<GPUShader> fso =
|
||||
g_gpu_device->CreateShader(GPUShaderStage::Fragment, shadergen.GetLanguage(),
|
||||
shadergen.GenerateFastMADReconstructFragmentShader(), error);
|
||||
if (!fso)
|
||||
return false;
|
||||
|
||||
GL_OBJECT_NAME(fso, "FastMAD Reconstruct Fragment Shader");
|
||||
|
||||
plconfig.layout = GPUPipeline::Layout::MultiTextureAndPushConstants;
|
||||
plconfig.fragment_shader = fso.get();
|
||||
if (!(m_deinterlace_pipeline = g_gpu_device->CreatePipeline(plconfig, error)))
|
||||
return false;
|
||||
|
||||
GL_OBJECT_NAME(m_deinterlace_pipeline, "FastMAD Reconstruct Pipeline");
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
UnreachableCode();
|
||||
}
|
||||
}
|
||||
|
||||
if (chroma_smoothing)
|
||||
{
|
||||
m_chroma_smoothing_pipeline.reset();
|
||||
g_gpu_device->RecycleTexture(std::move(m_chroma_smoothing_texture));
|
||||
|
||||
if (g_gpu_settings.display_24bit_chroma_smoothing)
|
||||
{
|
||||
plconfig.layout = GPUPipeline::Layout::SingleTextureAndPushConstants;
|
||||
plconfig.SetTargetFormats(GPUTexture::Format::RGBA8);
|
||||
|
||||
std::unique_ptr<GPUShader> vso = g_gpu_device->CreateShader(GPUShaderStage::Vertex, shadergen.GetLanguage(),
|
||||
shadergen.GenerateScreenQuadVertexShader(), error);
|
||||
std::unique_ptr<GPUShader> fso = g_gpu_device->CreateShader(
|
||||
GPUShaderStage::Fragment, shadergen.GetLanguage(), shadergen.GenerateChromaSmoothingFragmentShader(), error);
|
||||
if (!vso || !fso)
|
||||
return false;
|
||||
GL_OBJECT_NAME(vso, "Chroma Smoothing Vertex Shader");
|
||||
GL_OBJECT_NAME(fso, "Chroma Smoothing Fragment Shader");
|
||||
|
||||
plconfig.vertex_shader = vso.get();
|
||||
plconfig.fragment_shader = fso.get();
|
||||
if (!(m_chroma_smoothing_pipeline = g_gpu_device->CreatePipeline(plconfig, error)))
|
||||
return false;
|
||||
GL_OBJECT_NAME(m_chroma_smoothing_pipeline, "Chroma Smoothing Pipeline");
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void GPUPresenter::ClearDisplay()
|
||||
{
|
||||
ClearDisplayTexture();
|
||||
|
||||
// Just recycle the textures, it'll get re-fetched.
|
||||
DestroyDeinterlaceTextures();
|
||||
}
|
||||
|
||||
void GPUPresenter::ClearDisplayTexture()
|
||||
{
|
||||
m_display_texture = nullptr;
|
||||
m_display_texture_view_x = 0;
|
||||
m_display_texture_view_y = 0;
|
||||
m_display_texture_view_width = 0;
|
||||
m_display_texture_view_height = 0;
|
||||
}
|
||||
|
||||
void GPUPresenter::SetDisplayParameters(u16 display_width, u16 display_height, u16 display_origin_left,
|
||||
u16 display_origin_top, u16 display_vram_width, u16 display_vram_height,
|
||||
float display_pixel_aspect_ratio)
|
||||
{
|
||||
m_display_width = display_width;
|
||||
m_display_height = display_height;
|
||||
m_display_origin_left = display_origin_left;
|
||||
m_display_origin_top = display_origin_top;
|
||||
m_display_vram_width = display_vram_width;
|
||||
m_display_vram_height = display_vram_height;
|
||||
m_display_pixel_aspect_ratio = display_pixel_aspect_ratio;
|
||||
}
|
||||
|
||||
void GPUPresenter::SetDisplayTexture(GPUTexture* texture, GPUTexture* depth_buffer, s32 view_x, s32 view_y,
|
||||
s32 view_width, s32 view_height)
|
||||
{
|
||||
DebugAssert(texture);
|
||||
|
||||
if (g_gpu_settings.display_auto_resize_window &&
|
||||
(view_width != m_display_texture_view_width || view_height != m_display_texture_view_height))
|
||||
{
|
||||
Host::RunOnCPUThread([]() { System::RequestDisplaySize(); });
|
||||
}
|
||||
|
||||
m_display_texture = texture;
|
||||
m_display_depth_buffer = depth_buffer;
|
||||
m_display_texture_view_x = view_x;
|
||||
m_display_texture_view_y = view_y;
|
||||
m_display_texture_view_width = view_width;
|
||||
m_display_texture_view_height = view_height;
|
||||
}
|
||||
|
||||
GPUDevice::PresentResult GPUPresenter::PresentDisplay()
|
||||
{
|
||||
if (!g_gpu_device->HasMainSwapChain())
|
||||
return GPUDevice::PresentResult::SkipPresent;
|
||||
|
||||
GSVector4i display_rect;
|
||||
GSVector4i draw_rect;
|
||||
CalculateDrawRect(g_gpu_device->GetMainSwapChain()->GetWidth(), g_gpu_device->GetMainSwapChain()->GetHeight(),
|
||||
!g_gpu_settings.gpu_show_vram, true, &display_rect, &draw_rect);
|
||||
return RenderDisplay(nullptr, display_rect, draw_rect, !g_gpu_settings.gpu_show_vram);
|
||||
}
|
||||
|
||||
GPUDevice::PresentResult GPUPresenter::RenderDisplay(GPUTexture* target, const GSVector4i display_rect,
|
||||
const GSVector4i draw_rect, bool postfx)
|
||||
{
|
||||
GL_SCOPE_FMT("RenderDisplay: {}", draw_rect);
|
||||
|
||||
if (m_display_texture)
|
||||
m_display_texture->MakeReadyForSampling();
|
||||
|
||||
// Internal post-processing.
|
||||
GPUTexture* display_texture = m_display_texture;
|
||||
s32 display_texture_view_x = m_display_texture_view_x;
|
||||
s32 display_texture_view_y = m_display_texture_view_y;
|
||||
s32 display_texture_view_width = m_display_texture_view_width;
|
||||
s32 display_texture_view_height = m_display_texture_view_height;
|
||||
if (postfx && display_texture && PostProcessing::InternalChain.IsActive() &&
|
||||
PostProcessing::InternalChain.CheckTargets(DISPLAY_INTERNAL_POSTFX_FORMAT, display_texture_view_width,
|
||||
display_texture_view_height))
|
||||
{
|
||||
DebugAssert(display_texture_view_x == 0 && display_texture_view_y == 0 &&
|
||||
static_cast<s32>(display_texture->GetWidth()) == display_texture_view_width &&
|
||||
static_cast<s32>(display_texture->GetHeight()) == display_texture_view_height);
|
||||
|
||||
// Now we can apply the post chain.
|
||||
GPUTexture* post_output_texture = PostProcessing::InternalChain.GetOutputTexture();
|
||||
if (PostProcessing::InternalChain.Apply(display_texture, m_display_depth_buffer, post_output_texture,
|
||||
GSVector4i(0, 0, display_texture_view_width, display_texture_view_height),
|
||||
display_texture_view_width, display_texture_view_height, m_display_width,
|
||||
m_display_height) == GPUDevice::PresentResult::OK)
|
||||
{
|
||||
display_texture_view_x = 0;
|
||||
display_texture_view_y = 0;
|
||||
display_texture = post_output_texture;
|
||||
display_texture->MakeReadyForSampling();
|
||||
}
|
||||
}
|
||||
|
||||
const GPUTexture::Format hdformat = target ? target->GetFormat() : g_gpu_device->GetMainSwapChain()->GetFormat();
|
||||
const u32 target_width = target ? target->GetWidth() : g_gpu_device->GetMainSwapChain()->GetWidth();
|
||||
const u32 target_height = target ? target->GetHeight() : g_gpu_device->GetMainSwapChain()->GetHeight();
|
||||
const bool really_postfx = (postfx && PostProcessing::DisplayChain.IsActive() && g_gpu_device->HasMainSwapChain() &&
|
||||
hdformat != GPUTexture::Format::Unknown && target_width > 0 && target_height > 0 &&
|
||||
PostProcessing::DisplayChain.CheckTargets(hdformat, target_width, target_height));
|
||||
const u32 real_target_width =
|
||||
(target || really_postfx) ? target_width : g_gpu_device->GetMainSwapChain()->GetPostRotatedWidth();
|
||||
const u32 real_target_height =
|
||||
(target || really_postfx) ? target_height : g_gpu_device->GetMainSwapChain()->GetPostRotatedHeight();
|
||||
GSVector4i real_draw_rect =
|
||||
(target || really_postfx) ? draw_rect : g_gpu_device->GetMainSwapChain()->PreRotateClipRect(draw_rect);
|
||||
if (really_postfx)
|
||||
{
|
||||
g_gpu_device->ClearRenderTarget(PostProcessing::DisplayChain.GetInputTexture(), GPUDevice::DEFAULT_CLEAR_COLOR);
|
||||
g_gpu_device->SetRenderTarget(PostProcessing::DisplayChain.GetInputTexture());
|
||||
}
|
||||
else
|
||||
{
|
||||
if (target)
|
||||
{
|
||||
g_gpu_device->SetRenderTarget(target);
|
||||
}
|
||||
else
|
||||
{
|
||||
const GPUDevice::PresentResult pres = g_gpu_device->BeginPresent(g_gpu_device->GetMainSwapChain());
|
||||
if (pres != GPUDevice::PresentResult::OK)
|
||||
return pres;
|
||||
}
|
||||
}
|
||||
|
||||
if (display_texture)
|
||||
{
|
||||
bool texture_filter_linear = false;
|
||||
|
||||
struct alignas(16) Uniforms
|
||||
{
|
||||
float src_size[4];
|
||||
float clamp_rect[4];
|
||||
float params[4];
|
||||
} uniforms;
|
||||
std::memset(uniforms.params, 0, sizeof(uniforms.params));
|
||||
|
||||
switch (g_gpu_settings.display_scaling)
|
||||
{
|
||||
case DisplayScalingMode::Nearest:
|
||||
case DisplayScalingMode::NearestInteger:
|
||||
break;
|
||||
|
||||
case DisplayScalingMode::BilinearSmooth:
|
||||
case DisplayScalingMode::BilinearInteger:
|
||||
texture_filter_linear = true;
|
||||
break;
|
||||
|
||||
case DisplayScalingMode::BilinearSharp:
|
||||
{
|
||||
texture_filter_linear = true;
|
||||
uniforms.params[0] = std::max(
|
||||
std::floor(static_cast<float>(draw_rect.width()) / static_cast<float>(m_display_texture_view_width)), 1.0f);
|
||||
uniforms.params[1] = std::max(
|
||||
std::floor(static_cast<float>(draw_rect.height()) / static_cast<float>(m_display_texture_view_height)), 1.0f);
|
||||
uniforms.params[2] = 0.5f - 0.5f / uniforms.params[0];
|
||||
uniforms.params[3] = 0.5f - 0.5f / uniforms.params[1];
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
UnreachableCode();
|
||||
break;
|
||||
}
|
||||
|
||||
g_gpu_device->SetPipeline(m_display_pipeline.get());
|
||||
g_gpu_device->SetTextureSampler(
|
||||
0, display_texture, texture_filter_linear ? g_gpu_device->GetLinearSampler() : g_gpu_device->GetNearestSampler());
|
||||
|
||||
// For bilinear, clamp to 0.5/SIZE-0.5 to avoid bleeding from the adjacent texels in VRAM. This is because
|
||||
// 1.0 in UV space is not the bottom-right texel, but a mix of the bottom-right and wrapped/next texel.
|
||||
const GSVector2 display_texture_size = GSVector2(display_texture->GetSizeVec());
|
||||
const GSVector4 display_texture_size4 = GSVector4::xyxy(display_texture_size);
|
||||
const GSVector4 uv_rect = GSVector4(GSVector4i(display_texture_view_x, display_texture_view_y,
|
||||
display_texture_view_x + display_texture_view_width,
|
||||
display_texture_view_y + display_texture_view_height)) /
|
||||
display_texture_size4;
|
||||
GSVector4::store<true>(uniforms.clamp_rect,
|
||||
GSVector4(static_cast<float>(display_texture_view_x) + 0.5f,
|
||||
static_cast<float>(display_texture_view_y) + 0.5f,
|
||||
static_cast<float>(display_texture_view_x + display_texture_view_width) - 0.5f,
|
||||
static_cast<float>(display_texture_view_y + display_texture_view_height) - 0.5f) /
|
||||
display_texture_size4);
|
||||
GSVector4::store<true>(uniforms.src_size,
|
||||
GSVector4::xyxy(display_texture_size, GSVector2::cxpr(1.0f) / display_texture_size));
|
||||
|
||||
g_gpu_device->PushUniformBuffer(&uniforms, sizeof(uniforms));
|
||||
|
||||
g_gpu_device->SetViewport(0, 0, real_target_width, real_target_height);
|
||||
g_gpu_device->SetScissor(g_gpu_device->UsesLowerLeftOrigin() ?
|
||||
GPUDevice::FlipToLowerLeft(real_draw_rect, real_target_height) :
|
||||
real_draw_rect);
|
||||
|
||||
GPUBackend::ScreenVertex* vertices;
|
||||
u32 space;
|
||||
u32 base_vertex;
|
||||
g_gpu_device->MapVertexBuffer(sizeof(GPUBackend::ScreenVertex), 4, reinterpret_cast<void**>(&vertices), &space,
|
||||
&base_vertex);
|
||||
|
||||
const WindowInfo::PreRotation surface_prerotation = (target || really_postfx) ?
|
||||
WindowInfo::PreRotation::Identity :
|
||||
g_gpu_device->GetMainSwapChain()->GetPreRotation();
|
||||
|
||||
const DisplayRotation uv_rotation = static_cast<DisplayRotation>(
|
||||
(static_cast<u32>(g_gpu_settings.display_rotation) + static_cast<u32>(surface_prerotation)) %
|
||||
static_cast<u32>(DisplayRotation::Count));
|
||||
|
||||
const GSVector4 xy =
|
||||
GPUBackend::GetScreenQuadClipSpaceCoordinates(real_draw_rect, GSVector2i(real_target_width, real_target_height));
|
||||
switch (uv_rotation)
|
||||
{
|
||||
case DisplayRotation::Normal:
|
||||
vertices[0].Set(xy.xy(), uv_rect.xy());
|
||||
vertices[1].Set(xy.zyzw().xy(), uv_rect.zyzw().xy());
|
||||
vertices[2].Set(xy.xwzw().xy(), uv_rect.xwzw().xy());
|
||||
vertices[3].Set(xy.zw(), uv_rect.zw());
|
||||
break;
|
||||
case DisplayRotation::Rotate90:
|
||||
vertices[0].Set(xy.xy(), uv_rect.xwzw().xy());
|
||||
vertices[1].Set(xy.zyzw().xy(), uv_rect.xy());
|
||||
vertices[2].Set(xy.xwzw().xy(), uv_rect.zw());
|
||||
vertices[3].Set(xy.zw(), uv_rect.zyzw().xy());
|
||||
break;
|
||||
case DisplayRotation::Rotate180:
|
||||
vertices[0].Set(xy.xy(), uv_rect.xwzw().xy());
|
||||
vertices[1].Set(xy.zyzw().xy(), uv_rect.zw());
|
||||
vertices[2].Set(xy.xwzw().xy(), uv_rect.xy());
|
||||
vertices[3].Set(xy.zw(), uv_rect.zyzw().xy());
|
||||
break;
|
||||
case DisplayRotation::Rotate270:
|
||||
vertices[0].Set(xy.xy(), uv_rect.zyzw().xy());
|
||||
vertices[1].Set(xy.zyzw().xy(), uv_rect.zw());
|
||||
vertices[2].Set(xy.xwzw().xy(), uv_rect.xy());
|
||||
vertices[3].Set(xy.zw(), uv_rect.xwzw().xy());
|
||||
break;
|
||||
|
||||
DefaultCaseIsUnreachable();
|
||||
}
|
||||
|
||||
g_gpu_device->UnmapVertexBuffer(sizeof(GPUBackend::ScreenVertex), 4);
|
||||
g_gpu_device->Draw(4, base_vertex);
|
||||
}
|
||||
|
||||
if (really_postfx)
|
||||
{
|
||||
DebugAssert(!g_gpu_settings.gpu_show_vram);
|
||||
|
||||
// "original size" in postfx includes padding.
|
||||
const float upscale_x =
|
||||
m_display_texture ? static_cast<float>(m_display_texture_view_width) / static_cast<float>(m_display_vram_width) :
|
||||
1.0f;
|
||||
const float upscale_y = m_display_texture ? static_cast<float>(m_display_texture_view_height) /
|
||||
static_cast<float>(m_display_vram_height) :
|
||||
1.0f;
|
||||
const s32 orig_width = static_cast<s32>(std::ceil(static_cast<float>(m_display_width) * upscale_x));
|
||||
const s32 orig_height = static_cast<s32>(std::ceil(static_cast<float>(m_display_height) * upscale_y));
|
||||
|
||||
return PostProcessing::DisplayChain.Apply(PostProcessing::DisplayChain.GetInputTexture(), nullptr, target,
|
||||
display_rect, orig_width, orig_height, m_display_width, m_display_height);
|
||||
}
|
||||
else
|
||||
{
|
||||
return GPUDevice::PresentResult::OK;
|
||||
}
|
||||
}
|
||||
|
||||
void GPUPresenter::SendDisplayToMediaCapture(MediaCapture* cap)
|
||||
{
|
||||
GPUTexture* target = cap->GetRenderTexture();
|
||||
if (!target) [[unlikely]]
|
||||
{
|
||||
WARNING_LOG("Failed to get video capture render texture.");
|
||||
Host::RunOnCPUThread(&System::StopMediaCapture);
|
||||
return;
|
||||
}
|
||||
|
||||
const bool apply_aspect_ratio =
|
||||
(g_gpu_settings.display_screenshot_mode != DisplayScreenshotMode::UncorrectedInternalResolution);
|
||||
const bool postfx = (g_gpu_settings.display_screenshot_mode != DisplayScreenshotMode::InternalResolution);
|
||||
GSVector4i display_rect, draw_rect;
|
||||
CalculateDrawRect(target->GetWidth(), target->GetHeight(), !g_gpu_settings.gpu_show_vram, apply_aspect_ratio,
|
||||
&display_rect, &draw_rect);
|
||||
|
||||
// Not cleared by RenderDisplay().
|
||||
g_gpu_device->ClearRenderTarget(target, GPUDevice::DEFAULT_CLEAR_COLOR);
|
||||
|
||||
if (RenderDisplay(target, display_rect, draw_rect, postfx) != GPUDevice::PresentResult::OK ||
|
||||
!cap->DeliverVideoFrame(target)) [[unlikely]]
|
||||
{
|
||||
WARNING_LOG("Failed to render/deliver video capture frame.");
|
||||
Host::RunOnCPUThread(&System::StopMediaCapture);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void GPUPresenter::DestroyDeinterlaceTextures()
|
||||
{
|
||||
for (std::unique_ptr<GPUTexture>& tex : m_deinterlace_buffers)
|
||||
g_gpu_device->RecycleTexture(std::move(tex));
|
||||
g_gpu_device->RecycleTexture(std::move(m_deinterlace_texture));
|
||||
m_current_deinterlace_buffer = 0;
|
||||
}
|
||||
|
||||
bool GPUPresenter::Deinterlace(u32 field)
|
||||
{
|
||||
GPUTexture* src = m_display_texture;
|
||||
const u32 x = m_display_texture_view_x;
|
||||
const u32 y = m_display_texture_view_y;
|
||||
const u32 width = m_display_texture_view_width;
|
||||
const u32 height = m_display_texture_view_height;
|
||||
|
||||
const auto copy_to_field_buffer = [&](u32 buffer) {
|
||||
if (!g_gpu_device->ResizeTexture(&m_deinterlace_buffers[buffer], width, height, GPUTexture::Type::Texture,
|
||||
src->GetFormat(), GPUTexture::Flags::None, false)) [[unlikely]]
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
GL_OBJECT_NAME_FMT(m_deinterlace_buffers[buffer], "Blend Deinterlace Buffer {}", buffer);
|
||||
|
||||
GL_INS_FMT("Copy {}x{} from {},{} to field buffer {}", width, height, x, y, buffer);
|
||||
g_gpu_device->CopyTextureRegion(m_deinterlace_buffers[buffer].get(), 0, 0, 0, 0, m_display_texture, x, y, 0, 0,
|
||||
width, height);
|
||||
return true;
|
||||
};
|
||||
|
||||
src->MakeReadyForSampling();
|
||||
|
||||
switch (g_gpu_settings.display_deinterlacing_mode)
|
||||
{
|
||||
case DisplayDeinterlacingMode::Disabled:
|
||||
{
|
||||
GL_INS("Deinterlacing disabled, displaying field texture");
|
||||
return true;
|
||||
}
|
||||
|
||||
case DisplayDeinterlacingMode::Weave:
|
||||
{
|
||||
GL_SCOPE_FMT("DeinterlaceWeave({{{},{}}}, {}x{}, field={})", x, y, width, height, field);
|
||||
|
||||
const u32 full_height = height * 2;
|
||||
if (!DeinterlaceSetTargetSize(width, full_height, true)) [[unlikely]]
|
||||
{
|
||||
ClearDisplayTexture();
|
||||
return false;
|
||||
}
|
||||
|
||||
src->MakeReadyForSampling();
|
||||
|
||||
g_gpu_device->SetRenderTarget(m_deinterlace_texture.get());
|
||||
g_gpu_device->SetPipeline(m_deinterlace_pipeline.get());
|
||||
g_gpu_device->SetTextureSampler(0, src, g_gpu_device->GetNearestSampler());
|
||||
const u32 uniforms[4] = {x, y, field, 0};
|
||||
g_gpu_device->PushUniformBuffer(uniforms, sizeof(uniforms));
|
||||
g_gpu_device->SetViewportAndScissor(0, 0, width, full_height);
|
||||
g_gpu_device->Draw(3, 0);
|
||||
|
||||
m_deinterlace_texture->MakeReadyForSampling();
|
||||
SetDisplayTexture(m_deinterlace_texture.get(), m_display_depth_buffer, 0, 0, width, full_height);
|
||||
return true;
|
||||
}
|
||||
|
||||
case DisplayDeinterlacingMode::Blend:
|
||||
{
|
||||
constexpr u32 NUM_BLEND_BUFFERS = 2;
|
||||
|
||||
GL_SCOPE_FMT("DeinterlaceBlend({{{},{}}}, {}x{}, field={})", x, y, width, height, field);
|
||||
|
||||
const u32 this_buffer = m_current_deinterlace_buffer;
|
||||
m_current_deinterlace_buffer = (m_current_deinterlace_buffer + 1u) % NUM_BLEND_BUFFERS;
|
||||
GL_INS_FMT("Current buffer: {}", this_buffer);
|
||||
if (!DeinterlaceSetTargetSize(width, height, false) || !copy_to_field_buffer(this_buffer)) [[unlikely]]
|
||||
{
|
||||
ClearDisplayTexture();
|
||||
return false;
|
||||
}
|
||||
|
||||
copy_to_field_buffer(this_buffer);
|
||||
|
||||
// TODO: could be implemented with alpha blending instead..
|
||||
g_gpu_device->InvalidateRenderTarget(m_deinterlace_texture.get());
|
||||
g_gpu_device->SetRenderTarget(m_deinterlace_texture.get());
|
||||
g_gpu_device->SetPipeline(m_deinterlace_pipeline.get());
|
||||
g_gpu_device->SetTextureSampler(0, m_deinterlace_buffers[this_buffer].get(), g_gpu_device->GetNearestSampler());
|
||||
g_gpu_device->SetTextureSampler(1, m_deinterlace_buffers[(this_buffer - 1) % NUM_BLEND_BUFFERS].get(),
|
||||
g_gpu_device->GetNearestSampler());
|
||||
g_gpu_device->SetViewportAndScissor(0, 0, width, height);
|
||||
g_gpu_device->Draw(3, 0);
|
||||
|
||||
m_deinterlace_texture->MakeReadyForSampling();
|
||||
SetDisplayTexture(m_deinterlace_texture.get(), m_display_depth_buffer, 0, 0, width, height);
|
||||
return true;
|
||||
}
|
||||
|
||||
case DisplayDeinterlacingMode::Adaptive:
|
||||
{
|
||||
GL_SCOPE_FMT("DeinterlaceAdaptive({{{},{}}}, {}x{}, field={})", x, y, width, height, field);
|
||||
|
||||
const u32 this_buffer = m_current_deinterlace_buffer;
|
||||
const u32 full_height = height * 2;
|
||||
m_current_deinterlace_buffer = (m_current_deinterlace_buffer + 1u) % DEINTERLACE_BUFFER_COUNT;
|
||||
GL_INS_FMT("Current buffer: {}", this_buffer);
|
||||
if (!DeinterlaceSetTargetSize(width, full_height, false) || !copy_to_field_buffer(this_buffer)) [[unlikely]]
|
||||
{
|
||||
ClearDisplayTexture();
|
||||
return false;
|
||||
}
|
||||
|
||||
g_gpu_device->SetRenderTarget(m_deinterlace_texture.get());
|
||||
g_gpu_device->SetPipeline(m_deinterlace_pipeline.get());
|
||||
g_gpu_device->SetTextureSampler(0, m_deinterlace_buffers[this_buffer].get(), g_gpu_device->GetNearestSampler());
|
||||
g_gpu_device->SetTextureSampler(1, m_deinterlace_buffers[(this_buffer - 1) % DEINTERLACE_BUFFER_COUNT].get(),
|
||||
g_gpu_device->GetNearestSampler());
|
||||
g_gpu_device->SetTextureSampler(2, m_deinterlace_buffers[(this_buffer - 2) % DEINTERLACE_BUFFER_COUNT].get(),
|
||||
g_gpu_device->GetNearestSampler());
|
||||
g_gpu_device->SetTextureSampler(3, m_deinterlace_buffers[(this_buffer - 3) % DEINTERLACE_BUFFER_COUNT].get(),
|
||||
g_gpu_device->GetNearestSampler());
|
||||
const u32 uniforms[] = {field, full_height};
|
||||
g_gpu_device->PushUniformBuffer(uniforms, sizeof(uniforms));
|
||||
g_gpu_device->SetViewportAndScissor(0, 0, width, full_height);
|
||||
g_gpu_device->Draw(3, 0);
|
||||
|
||||
m_deinterlace_texture->MakeReadyForSampling();
|
||||
SetDisplayTexture(m_deinterlace_texture.get(), m_display_depth_buffer, 0, 0, width, full_height);
|
||||
return true;
|
||||
}
|
||||
|
||||
default:
|
||||
UnreachableCode();
|
||||
}
|
||||
}
|
||||
|
||||
bool GPUPresenter::DeinterlaceSetTargetSize(u32 width, u32 height, bool preserve)
|
||||
{
|
||||
if (!g_gpu_device->ResizeTexture(&m_deinterlace_texture, width, height, GPUTexture::Type::RenderTarget,
|
||||
GPUTexture::Format::RGBA8, GPUTexture::Flags::None, preserve)) [[unlikely]]
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
GL_OBJECT_NAME(m_deinterlace_texture, "Deinterlace target texture");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GPUPresenter::ApplyChromaSmoothing()
|
||||
{
|
||||
const u32 x = m_display_texture_view_x;
|
||||
const u32 y = m_display_texture_view_y;
|
||||
const u32 width = m_display_texture_view_width;
|
||||
const u32 height = m_display_texture_view_height;
|
||||
if (!g_gpu_device->ResizeTexture(&m_chroma_smoothing_texture, width, height, GPUTexture::Type::RenderTarget,
|
||||
GPUTexture::Format::RGBA8, GPUTexture::Flags::None, false))
|
||||
{
|
||||
ClearDisplayTexture();
|
||||
return false;
|
||||
}
|
||||
|
||||
GL_OBJECT_NAME(m_chroma_smoothing_texture, "Chroma smoothing texture");
|
||||
|
||||
GL_SCOPE_FMT("ApplyChromaSmoothing({{{},{}}}, {}x{})", x, y, width, height);
|
||||
|
||||
m_display_texture->MakeReadyForSampling();
|
||||
g_gpu_device->InvalidateRenderTarget(m_chroma_smoothing_texture.get());
|
||||
g_gpu_device->SetRenderTarget(m_chroma_smoothing_texture.get());
|
||||
g_gpu_device->SetPipeline(m_chroma_smoothing_pipeline.get());
|
||||
g_gpu_device->SetTextureSampler(0, m_display_texture, g_gpu_device->GetNearestSampler());
|
||||
const u32 uniforms[] = {x, y, width - 1, height - 1};
|
||||
g_gpu_device->PushUniformBuffer(uniforms, sizeof(uniforms));
|
||||
g_gpu_device->SetViewportAndScissor(0, 0, width, height);
|
||||
g_gpu_device->Draw(3, 0);
|
||||
|
||||
m_chroma_smoothing_texture->MakeReadyForSampling();
|
||||
SetDisplayTexture(m_chroma_smoothing_texture.get(), m_display_depth_buffer, 0, 0, width, height);
|
||||
return true;
|
||||
}
|
||||
|
||||
void GPUPresenter::CalculateDrawRect(s32 window_width, s32 window_height, bool apply_rotation, bool apply_aspect_ratio,
|
||||
GSVector4i* display_rect, GSVector4i* draw_rect) const
|
||||
{
|
||||
const bool integer_scale = (g_gpu_settings.display_scaling == DisplayScalingMode::NearestInteger ||
|
||||
g_gpu_settings.display_scaling == DisplayScalingMode::BilinearInteger);
|
||||
const bool show_vram = g_gpu_settings.gpu_show_vram;
|
||||
const u32 display_width = show_vram ? VRAM_WIDTH : m_display_width;
|
||||
const u32 display_height = show_vram ? VRAM_HEIGHT : m_display_height;
|
||||
const s32 display_origin_left = show_vram ? 0 : m_display_origin_left;
|
||||
const s32 display_origin_top = show_vram ? 0 : m_display_origin_top;
|
||||
const u32 display_vram_width = show_vram ? VRAM_WIDTH : m_display_vram_width;
|
||||
const u32 display_vram_height = show_vram ? VRAM_HEIGHT : m_display_vram_height;
|
||||
const float display_pixel_aspect_ratio = show_vram ? 1.0f : m_display_pixel_aspect_ratio;
|
||||
GPU::CalculateDrawRect(window_width, window_height, display_width, display_height, display_origin_left,
|
||||
display_origin_top, display_vram_width, display_vram_height, g_gpu_settings.display_rotation,
|
||||
g_gpu_settings.display_alignment, display_pixel_aspect_ratio,
|
||||
g_gpu_settings.display_stretch_vertically, integer_scale, display_rect, draw_rect);
|
||||
}
|
||||
|
||||
bool GPUPresenter::PresentFrame(GPUPresenter* presenter, GPUBackend* backend, bool allow_skip_present, u64 present_time)
|
||||
{
|
||||
const bool skip_present = (!g_gpu_device->HasMainSwapChain() ||
|
||||
(allow_skip_present && g_gpu_device->GetMainSwapChain()->ShouldSkipPresentingFrame() &&
|
||||
presenter && presenter->m_skipped_present_count < MAX_SKIPPED_PRESENT_COUNT));
|
||||
|
||||
if (!skip_present)
|
||||
{
|
||||
// acquire for IO.MousePos and system state.
|
||||
std::atomic_thread_fence(std::memory_order_acquire);
|
||||
|
||||
FullscreenUI::Render();
|
||||
|
||||
if (backend && System::IsValid())
|
||||
ImGuiManager::RenderTextOverlays(backend);
|
||||
|
||||
ImGuiManager::RenderOverlayWindows();
|
||||
|
||||
ImGuiManager::RenderOSDMessages();
|
||||
|
||||
ImGuiFullscreen::RenderOverlays();
|
||||
|
||||
if (backend && System::GetState() == System::State::Running)
|
||||
ImGuiManager::RenderSoftwareCursors();
|
||||
|
||||
ImGuiManager::RenderDebugWindows();
|
||||
}
|
||||
|
||||
const GPUDevice::PresentResult pres =
|
||||
skip_present ?
|
||||
GPUDevice::PresentResult::SkipPresent :
|
||||
(presenter ? presenter->PresentDisplay() : g_gpu_device->BeginPresent(g_gpu_device->GetMainSwapChain()));
|
||||
if (pres == GPUDevice::PresentResult::OK)
|
||||
{
|
||||
if (presenter)
|
||||
presenter->m_skipped_present_count = 0;
|
||||
|
||||
g_gpu_device->RenderImGui(g_gpu_device->GetMainSwapChain());
|
||||
|
||||
const GPUDevice::Features features = g_gpu_device->GetFeatures();
|
||||
const bool scheduled_present = (present_time != 0);
|
||||
const bool explicit_present = (scheduled_present && (features.explicit_present && !features.timed_present));
|
||||
const bool timed_present = (scheduled_present && features.timed_present);
|
||||
|
||||
if (scheduled_present && !explicit_present)
|
||||
{
|
||||
// No explicit present support, simulate it with Flush.
|
||||
g_gpu_device->FlushCommands();
|
||||
SleepUntilPresentTime(present_time);
|
||||
}
|
||||
|
||||
g_gpu_device->EndPresent(g_gpu_device->GetMainSwapChain(), explicit_present, timed_present ? present_time : 0);
|
||||
|
||||
if (g_gpu_device->IsGPUTimingEnabled())
|
||||
PerformanceCounters::AccumulateGPUTime();
|
||||
|
||||
if (explicit_present)
|
||||
{
|
||||
SleepUntilPresentTime(present_time);
|
||||
g_gpu_device->SubmitPresent(g_gpu_device->GetMainSwapChain());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (presenter)
|
||||
presenter->m_skipped_present_count++;
|
||||
|
||||
if (pres == GPUDevice::PresentResult::DeviceLost) [[unlikely]]
|
||||
{
|
||||
ERROR_LOG("GPU device lost during present.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pres == GPUDevice::PresentResult::ExclusiveFullscreenLost) [[unlikely]]
|
||||
{
|
||||
WARNING_LOG("Lost exclusive fullscreen.");
|
||||
Host::SetFullscreen(false);
|
||||
}
|
||||
|
||||
if (!skip_present)
|
||||
g_gpu_device->FlushCommands();
|
||||
|
||||
// Still need to kick ImGui or it gets cranky.
|
||||
ImGui::EndFrame();
|
||||
}
|
||||
|
||||
ImGuiManager::NewFrame();
|
||||
return true;
|
||||
}
|
||||
|
||||
void GPUPresenter::SleepUntilPresentTime(u64 present_time)
|
||||
{
|
||||
// Use a spinwait if we undersleep for all platforms except android.. don't want to burn battery.
|
||||
// Linux also seems to do a much better job of waking up at the requested time.
|
||||
|
||||
#if !defined(__linux__) && !defined(__ANDROID__)
|
||||
Timer::SleepUntil(present_time, true);
|
||||
#else
|
||||
Timer::SleepUntil(present_time, false);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool GPUPresenter::RenderScreenshotToBuffer(u32 width, u32 height, const GSVector4i display_rect,
|
||||
const GSVector4i draw_rect, bool postfx, Image* out_image)
|
||||
{
|
||||
const GPUTexture::Format hdformat =
|
||||
g_gpu_device->HasMainSwapChain() ? g_gpu_device->GetMainSwapChain()->GetFormat() : GPUTexture::Format::RGBA8;
|
||||
const ImageFormat image_format = GPUTexture::GetImageFormatForTextureFormat(hdformat);
|
||||
if (image_format == ImageFormat::None)
|
||||
return false;
|
||||
|
||||
auto render_texture = g_gpu_device->FetchAutoRecycleTexture(width, height, 1, 1, 1, GPUTexture::Type::RenderTarget,
|
||||
hdformat, GPUTexture::Flags::None);
|
||||
if (!render_texture)
|
||||
return false;
|
||||
|
||||
g_gpu_device->ClearRenderTarget(render_texture.get(), GPUDevice::DEFAULT_CLEAR_COLOR);
|
||||
|
||||
// TODO: this should use copy shader instead.
|
||||
RenderDisplay(render_texture.get(), display_rect, draw_rect, postfx);
|
||||
|
||||
Image image(width, height, image_format);
|
||||
|
||||
Error error;
|
||||
std::unique_ptr<GPUDownloadTexture> dltex;
|
||||
if (g_gpu_device->GetFeatures().memory_import)
|
||||
{
|
||||
dltex = g_gpu_device->CreateDownloadTexture(width, height, hdformat, image.GetPixels(), image.GetStorageSize(),
|
||||
image.GetPitch(), &error);
|
||||
}
|
||||
if (!dltex)
|
||||
{
|
||||
if (!(dltex = g_gpu_device->CreateDownloadTexture(width, height, hdformat, &error)))
|
||||
{
|
||||
ERROR_LOG("Failed to create {}x{} download texture: {}", width, height, error.GetDescription());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
dltex->CopyFromTexture(0, 0, render_texture.get(), 0, 0, width, height, 0, 0, false);
|
||||
if (!dltex->ReadTexels(0, 0, width, height, image.GetPixels(), image.GetPitch()))
|
||||
return false;
|
||||
|
||||
*out_image = std::move(image);
|
||||
return true;
|
||||
}
|
||||
|
||||
void GPUPresenter::CalculateScreenshotSize(DisplayScreenshotMode mode, u32* width, u32* height,
|
||||
GSVector4i* display_rect, GSVector4i* draw_rect) const
|
||||
{
|
||||
const bool internal_resolution = (mode != DisplayScreenshotMode::ScreenResolution || g_gpu_settings.gpu_show_vram);
|
||||
if (internal_resolution && m_display_texture_view_width != 0 && m_display_texture_view_height != 0)
|
||||
{
|
||||
if (mode == DisplayScreenshotMode::InternalResolution)
|
||||
{
|
||||
float f_width = static_cast<float>(m_display_texture_view_width);
|
||||
float f_height = static_cast<float>(m_display_texture_view_height);
|
||||
if (!g_gpu_settings.gpu_show_vram)
|
||||
GPU::ApplyPixelAspectRatioToSize(m_display_pixel_aspect_ratio, &f_width, &f_height);
|
||||
|
||||
// DX11 won't go past 16K texture size.
|
||||
const float max_texture_size = static_cast<float>(g_gpu_device->GetMaxTextureSize());
|
||||
if (f_width > max_texture_size)
|
||||
{
|
||||
f_height = f_height / (f_width / max_texture_size);
|
||||
f_width = max_texture_size;
|
||||
}
|
||||
if (f_height > max_texture_size)
|
||||
{
|
||||
f_height = max_texture_size;
|
||||
f_width = f_width / (f_height / max_texture_size);
|
||||
}
|
||||
|
||||
*width = static_cast<u32>(std::ceil(f_width));
|
||||
*height = static_cast<u32>(std::ceil(f_height));
|
||||
}
|
||||
else // if (mode == DisplayScreenshotMode::UncorrectedInternalResolution)
|
||||
{
|
||||
*width = m_display_texture_view_width;
|
||||
*height = m_display_texture_view_height;
|
||||
}
|
||||
|
||||
// Remove padding, it's not part of the framebuffer.
|
||||
*draw_rect = GSVector4i(0, 0, static_cast<s32>(*width), static_cast<s32>(*height));
|
||||
*display_rect = *draw_rect;
|
||||
}
|
||||
else
|
||||
{
|
||||
*width = g_gpu_device->HasMainSwapChain() ? g_gpu_device->GetMainSwapChain()->GetWidth() : 1;
|
||||
*height = g_gpu_device->HasMainSwapChain() ? g_gpu_device->GetMainSwapChain()->GetHeight() : 1;
|
||||
CalculateDrawRect(*width, *height, true, !g_settings.gpu_show_vram, display_rect, draw_rect);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,124 @@
|
||||
// SPDX-FileCopyrightText: 2019-2025 Connor McLaughlin <stenzek@gmail.com>
|
||||
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "util/gpu_device.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
class Error;
|
||||
class Image;
|
||||
class MediaCapture;
|
||||
|
||||
enum class DisplayScreenshotMode : u8;
|
||||
|
||||
class GPUBackend;
|
||||
|
||||
struct GPUSettings;
|
||||
struct GPUBackendUpdateDisplayCommand;
|
||||
struct GPUBackendFramePresentationParameters;
|
||||
|
||||
class GPUPresenter final
|
||||
{
|
||||
public:
|
||||
GPUPresenter();
|
||||
virtual ~GPUPresenter();
|
||||
|
||||
/// Main frame presenter - used both when a game is and is not running.
|
||||
static bool PresentFrame(GPUPresenter* presenter, GPUBackend* backend, bool allow_skip_present, u64 present_time);
|
||||
|
||||
ALWAYS_INLINE s32 GetDisplayWidth() const { return m_display_width; }
|
||||
ALWAYS_INLINE s32 GetDisplayHeight() const { return m_display_height; }
|
||||
ALWAYS_INLINE s32 GetDisplayVRAMWidth() const { return m_display_vram_width; }
|
||||
ALWAYS_INLINE s32 GetDisplayVRAMHeight() const { return m_display_vram_height; }
|
||||
ALWAYS_INLINE s32 GetDisplayTextureViewX() const { return m_display_texture_view_x; }
|
||||
ALWAYS_INLINE s32 GetDisplayTextureViewY() const { return m_display_texture_view_y; }
|
||||
ALWAYS_INLINE s32 GetDisplayTextureViewWidth() const { return m_display_texture_view_width; }
|
||||
ALWAYS_INLINE s32 GetDisplayTextureViewHeight() const { return m_display_texture_view_height; }
|
||||
ALWAYS_INLINE GPUTexture* GetDisplayTexture() const { return m_display_texture; }
|
||||
ALWAYS_INLINE GPUTexture* GetDisplayDepthBuffer() const { return m_display_depth_buffer; }
|
||||
ALWAYS_INLINE bool HasDisplayTexture() const { return m_display_texture; }
|
||||
|
||||
bool Initialize(Error* error);
|
||||
|
||||
void UpdateSettings(const GPUSettings& old_settings);
|
||||
|
||||
void ClearDisplay();
|
||||
void ClearDisplayTexture();
|
||||
void SetDisplayParameters(u16 display_width, u16 display_height, u16 display_origin_left, u16 display_origin_top,
|
||||
u16 display_vram_width, u16 display_vram_height, float display_pixel_aspect_ratio);
|
||||
void SetDisplayTexture(GPUTexture* texture, GPUTexture* depth_buffer, s32 view_x, s32 view_y, s32 view_width,
|
||||
s32 view_height);
|
||||
bool Deinterlace(u32 field);
|
||||
bool ApplyChromaSmoothing();
|
||||
|
||||
/// Helper function for computing the draw rectangle in a larger window.
|
||||
void CalculateDrawRect(s32 window_width, s32 window_height, bool apply_rotation, bool apply_aspect_ratio,
|
||||
GSVector4i* display_rect, GSVector4i* draw_rect) const;
|
||||
|
||||
/// Helper function for computing screenshot bounds.
|
||||
void CalculateScreenshotSize(DisplayScreenshotMode mode, u32* width, u32* height, GSVector4i* display_rect,
|
||||
GSVector4i* draw_rect) const;
|
||||
|
||||
/// Renders the display, optionally with postprocessing to the specified image.
|
||||
bool RenderScreenshotToBuffer(u32 width, u32 height, const GSVector4i display_rect, const GSVector4i draw_rect,
|
||||
bool postfx, Image* out_image);
|
||||
|
||||
/// Sends the current frame to media capture.
|
||||
void SendDisplayToMediaCapture(MediaCapture* cap);
|
||||
|
||||
private:
|
||||
enum : u32
|
||||
{
|
||||
DEINTERLACE_BUFFER_COUNT = 4,
|
||||
MAX_SKIPPED_PRESENT_COUNT = 50,
|
||||
};
|
||||
|
||||
static void SleepUntilPresentTime(u64 present_time);
|
||||
|
||||
/// Draws the current display texture, with any post-processing.
|
||||
GPUDevice::PresentResult PresentDisplay();
|
||||
|
||||
bool CompileDisplayPipelines(bool display, bool deinterlace, bool chroma_smoothing, Error* error);
|
||||
|
||||
GPUDevice::PresentResult RenderDisplay(GPUTexture* target, const GSVector4i display_rect, const GSVector4i draw_rect,
|
||||
bool postfx);
|
||||
|
||||
bool DeinterlaceSetTargetSize(u32 width, u32 height, bool preserve);
|
||||
void DestroyDeinterlaceTextures();
|
||||
|
||||
s32 m_display_width = 0;
|
||||
s32 m_display_height = 0;
|
||||
|
||||
s32 m_display_origin_left = 0;
|
||||
s32 m_display_origin_top = 0;
|
||||
s32 m_display_vram_width = 0;
|
||||
s32 m_display_vram_height = 0;
|
||||
float m_display_pixel_aspect_ratio = 1.0f;
|
||||
|
||||
u32 m_current_deinterlace_buffer = 0;
|
||||
std::unique_ptr<GPUPipeline> m_deinterlace_pipeline;
|
||||
std::array<std::unique_ptr<GPUTexture>, DEINTERLACE_BUFFER_COUNT> m_deinterlace_buffers;
|
||||
std::unique_ptr<GPUTexture> m_deinterlace_texture;
|
||||
|
||||
std::unique_ptr<GPUPipeline> m_chroma_smoothing_pipeline;
|
||||
std::unique_ptr<GPUTexture> m_chroma_smoothing_texture;
|
||||
|
||||
std::unique_ptr<GPUPipeline> m_display_pipeline;
|
||||
GPUTexture* m_display_texture = nullptr;
|
||||
GPUTexture* m_display_depth_buffer = nullptr;
|
||||
s32 m_display_texture_view_x = 0;
|
||||
s32 m_display_texture_view_y = 0;
|
||||
s32 m_display_texture_view_width = 0;
|
||||
s32 m_display_texture_view_height = 0;
|
||||
|
||||
u32 m_skipped_present_count = 0;
|
||||
};
|
||||
|
||||
namespace Host {
|
||||
|
||||
/// Called at the end of the frame, before presentation.
|
||||
void FrameDoneOnGPUThread(GPUPresenter* gpu_presenter, u32 frame_number);
|
||||
|
||||
} // namespace Host
|
||||
Loading…
Reference in New Issue