This commit is contained in:
Stenzek 2023-08-07 01:00:04 +10:00
parent a09ef8d4f6
commit df651e4c90
11 changed files with 162 additions and 116 deletions

View File

@ -478,6 +478,10 @@ bool D3D11Device::CreateDevice(const std::string_view& adapter)
if (m_window_info.type != WindowInfo::Type::Surfaceless && !CreateSwapChain())
return false;
// Render a frame as soon as possible to clear out whatever was previously being displayed.
m_context->ClearRenderTargetView(m_swap_chain_rtv.Get(), s_clear_color.data());
m_swap_chain->Present(0, m_using_allow_tearing ? DXGI_PRESENT_ALLOW_TEARING : 0);
if (!CreateBuffers())
return false;

View File

@ -471,7 +471,7 @@ void GPUDevice::RenderImGui()
const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
DebugAssert(!pcmd->UserCallback);
if (pcmd->ElemCount == 0 || pcmd->ClipRect.z <= pcmd->ClipRect.x || pcmd->ClipRect.w <= pcmd->ClipRect.x)
if (pcmd->ElemCount == 0 || pcmd->ClipRect.z <= pcmd->ClipRect.x || pcmd->ClipRect.w <= pcmd->ClipRect.y)
continue;
SetScissor(static_cast<s32>(pcmd->ClipRect.x), static_cast<s32>(pcmd->ClipRect.y),

View File

@ -407,6 +407,7 @@ class GPUDevice
{
public:
// TODO: drop virtuals
// TODO: make windowinfo use GPUTexture format
using DrawIndex = u16;
struct Features

View File

@ -143,6 +143,8 @@ bool MetalDevice::CreateDevice(const std::string_view& adapter, bool debug_devic
CreateCommandBuffer();
Panic("Render blank frame!");
if (!CreateBuffers())
{
Log_ErrorPrintf("Failed to create buffers.");

View File

@ -317,6 +317,10 @@ bool OpenGLDevice::CreateDevice(const std::string_view& adapter)
}
#endif
SetSwapInterval();
if (HasSurface())
RenderBlankFrame();
OpenGLTexture::s_use_pbo_for_uploads = true;
if (m_gl_context->IsGLES())
{
@ -453,7 +457,7 @@ bool OpenGLDevice::UpdateWindow()
{
// reset vsync rate, since it (usually) gets lost
SetSwapInterval();
// TODO RenderBlankFrame();
RenderBlankFrame();
}
return true;
@ -489,6 +493,17 @@ void OpenGLDevice::SetSwapInterval()
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, current_fbo);
}
void OpenGLDevice::RenderBlankFrame()
{
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glDisable(GL_SCISSOR_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
m_gl_context->SwapBuffers();
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_current_framebuffer ? m_current_framebuffer->GetGLId() : 0);
glEnable(GL_SCISSOR_TEST);
}
GPUDevice::AdapterAndModeList OpenGLDevice::GetAdapterAndModeList()
{
AdapterAndModeList aml;
@ -595,7 +610,13 @@ bool OpenGLDevice::BeginPresent(bool skip_present)
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_SCISSOR_TEST);
const Common::Rectangle<s32> window_rc =
Common::Rectangle<s32>::FromExtents(0, 0, m_window_info.surface_width, m_window_info.surface_height);
m_current_framebuffer = nullptr;
m_last_viewport = window_rc;
m_last_scissor = window_rc;
UpdateViewport();
UpdateScissor();
return true;
}
@ -612,48 +633,6 @@ void OpenGLDevice::EndPresent()
KickTimestampQuery();
}
#if 0
void OpenGLDevice::RenderDisplay(s32 left, s32 bottom, s32 width, s32 height, OpenGLTexture* texture,
s32 texture_view_x, s32 texture_view_y, s32 texture_view_width,
s32 texture_view_height, bool linear_filter)
{
glViewport(left, bottom, width, height);
glDisable(GL_BLEND);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
m_display_program.Bind();
texture->Bind();
const bool linear = IsUsingLinearFiltering();
if (!m_use_gles2_draw_path)
{
const float position_adjust = linear ? 0.5f : 0.0f;
const float size_adjust = linear ? 1.0f : 0.0f;
const float flip_adjust = (texture_view_height < 0) ? -1.0f : 1.0f;
m_display_program.Uniform4f(
0, (static_cast<float>(texture_view_x) + position_adjust) / static_cast<float>(texture->GetWidth()),
(static_cast<float>(texture_view_y) + (position_adjust * flip_adjust)) / static_cast<float>(texture->GetHeight()),
(static_cast<float>(texture_view_width) - size_adjust) / static_cast<float>(texture->GetWidth()),
(static_cast<float>(texture_view_height) - (size_adjust * flip_adjust)) /
static_cast<float>(texture->GetHeight()));
glBindSampler(0, linear_filter ? m_display_linear_sampler : m_display_nearest_sampler);
glBindVertexArray(m_display_vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindSampler(0, 0);
}
else
{
texture->SetLinearFilter(linear_filter);
DrawFullscreenQuadES2(m_display_texture_view_x, m_display_texture_view_y, m_display_texture_view_width,
m_display_texture_view_height, texture->GetWidth(), texture->GetHeight());
}
}
#endif
void OpenGLDevice::CreateTimestampQueries()
{
const bool gles = m_gl_context->IsGLES();
@ -834,23 +813,13 @@ void OpenGLDevice::UnbindPipeline(const OpenGLPipeline* pl)
}
}
void OpenGLDevice::PreDrawCheck()
{
DebugAssert(m_current_pipeline);
if (m_current_framebuffer)
CommitClear(m_current_framebuffer);
}
void OpenGLDevice::Draw(u32 vertex_count, u32 base_vertex)
{
PreDrawCheck();
glDrawArrays(m_current_pipeline->GetTopology(), base_vertex, vertex_count);
}
void OpenGLDevice::DrawIndexed(u32 index_count, u32 base_index, u32 base_vertex)
{
PreDrawCheck();
const void* indices = reinterpret_cast<const void*>(static_cast<uintptr_t>(base_index) * sizeof(u16));
glDrawElementsBaseVertex(m_current_pipeline->GetTopology(), index_count, GL_UNSIGNED_SHORT, indices, base_vertex);
}
@ -907,9 +876,17 @@ void OpenGLDevice::SetFramebuffer(GPUFramebuffer* fb)
if (m_current_framebuffer == fb)
return;
// TODO: maybe move clear check here? gets rid of the per-draw overhead
m_current_framebuffer = static_cast<OpenGLFramebuffer*>(fb);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_current_framebuffer ? m_current_framebuffer->GetGLId() : 0);
OpenGLFramebuffer* FB = static_cast<OpenGLFramebuffer*>(fb);
const bool prev_was_window = (m_current_framebuffer == nullptr);
const bool new_is_window = (FB == nullptr);
m_current_framebuffer = FB;
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, FB ? FB->GetGLId() : 0);
if (prev_was_window != new_is_window)
{
UpdateViewport();
UpdateScissor();
}
}
void OpenGLDevice::SetTextureSampler(u32 slot, GPUTexture* texture, GPUSampler* sampler)
@ -957,14 +934,56 @@ void OpenGLDevice::SetTextureBuffer(u32 slot, GPUTextureBuffer* buffer)
void OpenGLDevice::SetViewport(s32 x, s32 y, s32 width, s32 height)
{
// TODO: cache this
// TODO: lower-left origin flip for window fb?
glViewport(x, y, width, height);
const Common::Rectangle<s32> rc = Common::Rectangle<s32>::FromExtents(x, y, width, height);
if (m_last_viewport == rc)
return;
m_last_viewport = rc;
UpdateViewport();
}
void OpenGLDevice::SetScissor(s32 x, s32 y, s32 width, s32 height)
{
// TODO: cache this
// TODO: lower-left origin flip for window fb?
const Common::Rectangle<s32> rc = Common::Rectangle<s32>::FromExtents(x, y, width, height);
if (m_last_scissor == rc)
return;
m_last_scissor = rc;
UpdateScissor();
}
std::tuple<s32, s32, s32, s32> OpenGLDevice::GetFlippedViewportScissor(const Common::Rectangle<s32>& rc) const
{
// Only when rendering to window framebuffer.
// We draw everything else upside-down.
s32 x, y, width, height;
if (!m_current_framebuffer)
{
const s32 sh = static_cast<s32>(m_window_info.surface_height);
const s32 rh = rc.GetHeight();
x = rc.left;
y = sh - rc.top - rh;
width = rc.GetWidth();
height = rh;
}
else
{
x = rc.left;
y = rc.top;
width = rc.GetWidth();
height = rc.GetHeight();
}
return std::tie(x, y, width, height);
}
void OpenGLDevice::UpdateViewport()
{
const auto& [x, y, width, height] = GetFlippedViewportScissor(m_last_viewport);
glViewport(x, y, width, height);
}
void OpenGLDevice::UpdateScissor()
{
const auto& [x, y, width, height] = GetFlippedViewportScissor(m_last_scissor);
glScissor(x, y, width, height);
}

View File

@ -12,8 +12,9 @@
#include "postprocessing_chain.h"
#include <memory>
#include <tuple>
#include "common/timer.h"
#include "common/rectangle.h"
#include "common/window_info.h"
// TODO: build a cache for programs on top of the pipeline cache idea
@ -124,6 +125,10 @@ public:
void UnbindPipeline(const OpenGLPipeline* pl);
protected:
bool CreateDevice(const std::string_view& adapter) override;
void DestroyDevice() override;
private:
static constexpr u8 NUM_TIMESTAMP_QUERIES = 3;
static constexpr GLenum UPDATE_TEXTURE_UNIT = GL_TEXTURE8;
@ -133,45 +138,45 @@ protected:
static constexpr u32 UNIFORM_BUFFER_SIZE = 2 * 1024 * 1024;
static constexpr u32 TEXTURE_STREAM_BUFFER_SIZE = 16 * 1024 * 1024;
// TODO: pass in file instead of blob for pipeline cache
OpenGLPipeline::VertexArrayCache m_vao_cache;
OpenGLPipeline::ProgramCache m_program_cache;
bool CreateDevice(const std::string_view& adapter) override;
void DestroyDevice() override;
bool CheckFeatures();
bool CreateBuffers();
void DestroyBuffers();
void SetSwapInterval();
void RenderBlankFrame();
std::tuple<s32, s32, s32, s32> GetFlippedViewportScissor(const Common::Rectangle<s32>& rc) const;
void UpdateViewport();
void UpdateScissor();
void CreateTimestampQueries();
void DestroyTimestampQueries();
void PopTimestampQuery();
void KickTimestampQuery();
bool CheckFeatures();
void PreDrawCheck();
std::unique_ptr<GL::Context> m_gl_context;
std::unique_ptr<OpenGLFramebuffer> m_window_framebuffer;
GLuint m_uniform_buffer_alignment = 1;
std::unique_ptr<OpenGLStreamBuffer> m_vertex_buffer;
std::unique_ptr<OpenGLStreamBuffer> m_index_buffer;
std::unique_ptr<OpenGLStreamBuffer> m_uniform_buffer;
std::unique_ptr<OpenGLStreamBuffer> m_texture_stream_buffer;
// TODO: pass in file instead of blob for pipeline cache
OpenGLPipeline::VertexArrayCache m_vao_cache;
OpenGLPipeline::ProgramCache m_program_cache;
// VAO cache - fixed max as key
GPUPipeline::RasterizationState m_last_rasterization_state = {};
GPUPipeline::DepthState m_last_depth_state = {};
GPUPipeline::BlendState m_last_blend_state = {};
GLuint m_uniform_buffer_alignment = 1;
GLuint m_last_program = 0;
GLuint m_last_vao = 0;
u32 m_last_texture_unit = 0;
std::array<std::pair<GLuint, GLuint>, MAX_TEXTURE_SAMPLERS> m_last_samplers = {};
Common::Rectangle<s32> m_last_viewport{0, 0, 1, 1};
Common::Rectangle<s32> m_last_scissor{0, 0, 1, 1};
// Misc framebuffers
GLuint m_read_fbo = 0;

View File

@ -490,6 +490,7 @@ ALWAYS_INLINE static void ApplyBlendState(const GPUPipeline::BlendState& bs)
}};
// TODO: driver bugs
// TODO: rdoc and look for redundant calls
bs.enable ? glEnable(GL_BLEND) : glDisable(GL_BLEND);

View File

@ -46,6 +46,41 @@ ALWAYS_INLINE static bool ShouldDisableColorPerspective()
return g_settings.gpu_pgxp_enable && g_settings.gpu_pgxp_texture_correction && !g_settings.gpu_pgxp_color_correction;
}
namespace {
class ShaderCompileProgressTracker
{
public:
ShaderCompileProgressTracker(std::string title, u32 total)
: m_title(std::move(title)), m_min_time(Common::Timer::ConvertSecondsToValue(1.0)),
m_update_interval(Common::Timer::ConvertSecondsToValue(0.1)), m_start_time(Common::Timer::GetCurrentValue()),
m_last_update_time(0), m_progress(0), m_total(total)
{
}
~ShaderCompileProgressTracker() = default;
void Increment()
{
m_progress++;
const u64 tv = Common::Timer::GetCurrentValue();
if ((tv - m_start_time) >= m_min_time && (tv - m_last_update_time) >= m_update_interval)
{
Host::DisplayLoadingScreen(m_title.c_str(), 0, static_cast<int>(m_total), static_cast<int>(m_progress));
m_last_update_time = tv;
}
}
private:
std::string m_title;
u64 m_min_time;
u64 m_update_interval;
u64 m_start_time;
u64 m_last_update_time;
u32 m_progress;
u32 m_total;
};
} // namespace
GPU_HW::GPU_HW() : GPU()
{
m_vram_ptr = m_vram_shadow.data();
@ -2361,6 +2396,7 @@ void GPU_HW::UpdateDisplay()
}
else
{
// TODO: use a dynamically sized texture
g_gpu_device->SetDisplayParameters(m_crtc_state.display_width, m_crtc_state.display_height,
m_crtc_state.display_origin_left, m_crtc_state.display_origin_top,
m_crtc_state.display_vram_width, m_crtc_state.display_vram_height,
@ -2610,25 +2646,6 @@ void GPU_HW::DrawRendererStats(bool is_idle_frame)
}
}
GPU_HW::ShaderCompileProgressTracker::ShaderCompileProgressTracker(std::string title, u32 total)
: m_title(std::move(title)), m_min_time(Common::Timer::ConvertSecondsToValue(1.0)),
m_update_interval(Common::Timer::ConvertSecondsToValue(0.1)), m_start_time(Common::Timer::GetCurrentValue()),
m_last_update_time(0), m_progress(0), m_total(total)
{
}
void GPU_HW::ShaderCompileProgressTracker::Increment()
{
m_progress++;
const u64 tv = Common::Timer::GetCurrentValue();
if ((tv - m_start_time) >= m_min_time && (tv - m_last_update_time) >= m_update_interval)
{
Host::DisplayLoadingScreen(m_title.c_str(), 0, static_cast<int>(m_total), static_cast<int>(m_progress));
m_last_update_time = tv;
}
}
std::unique_ptr<GPU> GPU::CreateHardwareRenderer()
{
std::unique_ptr<GPU_HW> gpu(std::make_unique<GPU_HW>());

View File

@ -184,23 +184,6 @@ protected:
u32 num_uniform_buffer_updates;
};
class ShaderCompileProgressTracker
{
public:
ShaderCompileProgressTracker(std::string title, u32 total);
void Increment();
private:
std::string m_title;
u64 m_min_time;
u64 m_update_interval;
u64 m_start_time;
u64 m_last_update_time;
u32 m_progress;
u32 m_total;
};
static constexpr std::tuple<float, float, float, float> RGBA8ToFloat(u32 rgba)
{
return std::make_tuple(static_cast<float>(rgba & UINT32_C(0xFF)) * (1.0f / 255.0f),

View File

@ -477,7 +477,10 @@ void Host::DisplayLoadingScreen(const char* message, int progress_min /*= -1*/,
}
ImGui::End();
ImGui::SetNextWindowSize(ImVec2(width, (has_progress ? 50.0f : 30.0f) * scale), ImGuiCond_Always);
const float padding_and_rounding = 15.0f * scale;
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, padding_and_rounding);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(padding_and_rounding, padding_and_rounding));
ImGui::SetNextWindowSize(ImVec2(width, (has_progress ? 80.0f : 50.0f) * scale), ImGuiCond_Always);
ImGui::SetNextWindowPos(ImVec2(io.DisplaySize.x * 0.5f, (io.DisplaySize.y * 0.5f) + (100.0f * scale)),
ImGuiCond_Always, ImVec2(0.5f, 0.0f));
if (ImGui::Begin("LoadingScreen", nullptr,
@ -487,7 +490,17 @@ void Host::DisplayLoadingScreen(const char* message, int progress_min /*= -1*/,
{
if (has_progress)
{
ImGui::Text("%s: %d/%d", message, progress_value, progress_max);
ImGui::TextUnformatted(message);
TinyString buf;
buf.Fmt("{}/{}", progress_value, progress_max);
const ImVec2 prog_size = ImGui::CalcTextSize(buf.GetCharArray(), buf.GetCharArray() + buf.GetLength());
ImGui::SameLine();
ImGui::SetCursorPosX(width - padding_and_rounding - prog_size.x);
ImGui::TextUnformatted(buf.GetCharArray(), buf.GetCharArray() + buf.GetLength());
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + 5.0f);
ImGui::ProgressBar(static_cast<float>(progress_value) / static_cast<float>(progress_max - progress_min),
ImVec2(-1.0f, 0.0f), "");
Log_InfoPrintf("%s: %d/%d", message, progress_value, progress_max);
@ -501,6 +514,7 @@ void Host::DisplayLoadingScreen(const char* message, int progress_min /*= -1*/,
}
}
ImGui::End();
ImGui::PopStyleVar(2);
ImGui::EndFrame();
g_gpu_device->Render(false);

View File

@ -407,9 +407,9 @@ void ImGuiManager::DrawPerformanceOverlay()
void ImGuiManager::DrawEnhancementsOverlay()
{
// TODO: Fix device type name
LargeString text;
text.AppendFmtString("{} {}", Settings::GetConsoleRegionName(System::GetRegion()),
text.AppendFmtString("{} {}-{}", Settings::GetConsoleRegionName(System::GetRegion()),
GPUDevice::RenderAPIToString(g_gpu_device->GetRenderAPI()),
g_gpu->IsHardwareRenderer() ? "HW" : "SW");
if (g_settings.rewind_enable)