Skip to content

Commit

Permalink
GPU: Propagate initialization error to caller
Browse files Browse the repository at this point in the history
Avoids double error popup.
  • Loading branch information
stenzek committed Oct 25, 2024
1 parent 6af71be commit 1ae2dcf
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 48 deletions.
45 changes: 22 additions & 23 deletions src/core/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ GPU::~GPU()
g_gpu_device->RecycleTexture(std::move(m_chroma_smoothing_texture));
}

bool GPU::Initialize()
bool GPU::Initialize(Error* error)
{
if (!System::IsReplayingGPUDump())
s_crtc_tick_event.Activate();
Expand All @@ -107,11 +107,8 @@ bool GPU::Initialize()
m_console_is_pal = System::IsPALRegion();
UpdateCRTCConfig();

if (!CompileDisplayPipelines(true, true, g_settings.display_24bit_chroma_smoothing))
{
Host::ReportErrorAsync("Error", "Failed to compile base GPU pipelines.");
if (!CompileDisplayPipelines(true, true, g_settings.display_24bit_chroma_smoothing, error))
return false;
}

#ifdef PSX_GPU_STATS
s_active_gpu_cycles = 0;
Expand Down Expand Up @@ -152,7 +149,7 @@ void GPU::UpdateSettings(const Settings& old_settings)
if (!CompileDisplayPipelines(g_settings.display_scaling != old_settings.display_scaling,
g_settings.display_deinterlacing_mode != old_settings.display_deinterlacing_mode,
g_settings.display_24bit_chroma_smoothing !=
old_settings.display_24bit_chroma_smoothing))
old_settings.display_24bit_chroma_smoothing, nullptr))
{
Panic("Failed to compile display pipeline on settings change.");
}
Expand Down Expand Up @@ -1650,7 +1647,7 @@ void GPU::ReadCLUT(u16* dest, GPUTexturePaletteReg reg, bool clut_is_8bit)
}
}

bool GPU::CompileDisplayPipelines(bool display, bool deinterlace, bool chroma_smoothing)
bool GPU::CompileDisplayPipelines(bool display, bool deinterlace, bool chroma_smoothing, Error* error)
{
GPUShaderGen shadergen(g_gpu_device->GetRenderAPI(), g_gpu_device->GetFeatures().dual_source_blend,
g_gpu_device->GetFeatures().framebuffer_fetch);
Expand Down Expand Up @@ -1693,16 +1690,18 @@ bool GPU::CompileDisplayPipelines(bool display, bool deinterlace, bool chroma_sm
break;
}

std::unique_ptr<GPUShader> vso = g_gpu_device->CreateShader(GPUShaderStage::Vertex, shadergen.GetLanguage(), vs);
std::unique_ptr<GPUShader> fso = g_gpu_device->CreateShader(GPUShaderStage::Fragment, shadergen.GetLanguage(), fs);
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_settings.display_scaling));
plconfig.vertex_shader = vso.get();
plconfig.fragment_shader = fso.get();
if (!(m_display_pipeline = g_gpu_device->CreatePipeline(plconfig)))
if (!(m_display_pipeline = g_gpu_device->CreatePipeline(plconfig, error)))
return false;
GL_OBJECT_NAME_FMT(m_display_pipeline, "Display Pipeline [{}]",
Settings::GetDisplayScalingName(g_settings.display_scaling));
Expand All @@ -1713,14 +1712,14 @@ bool GPU::CompileDisplayPipelines(bool display, bool deinterlace, bool chroma_sm
plconfig.SetTargetFormats(GPUTexture::Format::RGBA8);

std::unique_ptr<GPUShader> vso = g_gpu_device->CreateShader(GPUShaderStage::Vertex, shadergen.GetLanguage(),
shadergen.GenerateScreenQuadVertexShader());
shadergen.GenerateScreenQuadVertexShader(), error);
if (!vso)
return false;
GL_OBJECT_NAME(vso, "Deinterlace Vertex Shader");

std::unique_ptr<GPUShader> fso;
if (!(fso = g_gpu_device->CreateShader(GPUShaderStage::Fragment, shadergen.GetLanguage(),
shadergen.GenerateInterleavedFieldExtractFragmentShader())))
shadergen.GenerateInterleavedFieldExtractFragmentShader(), error)))
{
return false;
}
Expand All @@ -1730,7 +1729,7 @@ bool GPU::CompileDisplayPipelines(bool display, bool deinterlace, bool chroma_sm
plconfig.layout = GPUPipeline::Layout::SingleTextureAndPushConstants;
plconfig.vertex_shader = vso.get();
plconfig.fragment_shader = fso.get();
if (!(m_deinterlace_extract_pipeline = g_gpu_device->CreatePipeline(plconfig)))
if (!(m_deinterlace_extract_pipeline = g_gpu_device->CreatePipeline(plconfig, error)))
return false;

GL_OBJECT_NAME(m_deinterlace_extract_pipeline, "Deinterlace Field Extract Pipeline");
Expand All @@ -1744,7 +1743,7 @@ bool GPU::CompileDisplayPipelines(bool display, bool deinterlace, bool chroma_sm
case DisplayDeinterlacingMode::Weave:
{
if (!(fso = g_gpu_device->CreateShader(GPUShaderStage::Fragment, shadergen.GetLanguage(),
shadergen.GenerateDeinterlaceWeaveFragmentShader())))
shadergen.GenerateDeinterlaceWeaveFragmentShader(), error)))
{
return false;
}
Expand All @@ -1754,7 +1753,7 @@ bool GPU::CompileDisplayPipelines(bool display, bool deinterlace, bool chroma_sm
plconfig.layout = GPUPipeline::Layout::SingleTextureAndPushConstants;
plconfig.vertex_shader = vso.get();
plconfig.fragment_shader = fso.get();
if (!(m_deinterlace_pipeline = g_gpu_device->CreatePipeline(plconfig)))
if (!(m_deinterlace_pipeline = g_gpu_device->CreatePipeline(plconfig, error)))
return false;

GL_OBJECT_NAME(m_deinterlace_pipeline, "Weave Deinterlace Pipeline");
Expand All @@ -1764,7 +1763,7 @@ bool GPU::CompileDisplayPipelines(bool display, bool deinterlace, bool chroma_sm
case DisplayDeinterlacingMode::Blend:
{
if (!(fso = g_gpu_device->CreateShader(GPUShaderStage::Fragment, shadergen.GetLanguage(),
shadergen.GenerateDeinterlaceBlendFragmentShader())))
shadergen.GenerateDeinterlaceBlendFragmentShader(), error)))
{
return false;
}
Expand All @@ -1774,7 +1773,7 @@ bool GPU::CompileDisplayPipelines(bool display, bool deinterlace, bool chroma_sm
plconfig.layout = GPUPipeline::Layout::MultiTextureAndPushConstants;
plconfig.vertex_shader = vso.get();
plconfig.fragment_shader = fso.get();
if (!(m_deinterlace_pipeline = g_gpu_device->CreatePipeline(plconfig)))
if (!(m_deinterlace_pipeline = g_gpu_device->CreatePipeline(plconfig, error)))
return false;

GL_OBJECT_NAME(m_deinterlace_pipeline, "Blend Deinterlace Pipeline");
Expand All @@ -1784,15 +1783,15 @@ bool GPU::CompileDisplayPipelines(bool display, bool deinterlace, bool chroma_sm
case DisplayDeinterlacingMode::Adaptive:
{
fso = g_gpu_device->CreateShader(GPUShaderStage::Fragment, shadergen.GetLanguage(),
shadergen.GenerateFastMADReconstructFragmentShader());
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)))
if (!(m_deinterlace_pipeline = g_gpu_device->CreatePipeline(plconfig, error)))
return false;

GL_OBJECT_NAME(m_deinterlace_pipeline, "FastMAD Reconstruct Pipeline");
Expand All @@ -1815,17 +1814,17 @@ bool GPU::CompileDisplayPipelines(bool display, bool deinterlace, bool chroma_sm
plconfig.SetTargetFormats(GPUTexture::Format::RGBA8);

std::unique_ptr<GPUShader> vso = g_gpu_device->CreateShader(GPUShaderStage::Vertex, shadergen.GetLanguage(),
shadergen.GenerateScreenQuadVertexShader());
std::unique_ptr<GPUShader> fso = g_gpu_device->CreateShader(GPUShaderStage::Fragment, shadergen.GetLanguage(),
shadergen.GenerateChromaSmoothingFragmentShader());
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)))
if (!(m_chroma_smoothing_pipeline = g_gpu_device->CreatePipeline(plconfig, error)))
return false;
GL_OBJECT_NAME(m_chroma_smoothing_pipeline, "Chroma Smoothing Pipeline");
}
Expand Down
8 changes: 4 additions & 4 deletions src/core/gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class GPU
virtual const Threading::Thread* GetSWThread() const = 0;
virtual bool IsHardwareRenderer() const = 0;

virtual bool Initialize();
virtual bool Initialize(Error* error);
virtual void Reset(bool clear_vram);
virtual bool DoState(StateWrapper& sw, GPUTexture** save_to_texture, bool update_display);

Expand Down Expand Up @@ -184,8 +184,8 @@ class GPU
float ComputeVerticalFrequency() const;
float ComputeDisplayAspectRatio() const;

static std::unique_ptr<GPU> CreateHardwareRenderer();
static std::unique_ptr<GPU> CreateSoftwareRenderer();
static std::unique_ptr<GPU> CreateHardwareRenderer(Error* error);
static std::unique_ptr<GPU> CreateSoftwareRenderer(Error* error);

// Converts window coordinates into horizontal ticks and scanlines. Returns false if out of range. Used for lightguns.
void ConvertScreenCoordinatesToDisplayCoordinates(float window_x, float window_y, float* display_x,
Expand Down Expand Up @@ -631,7 +631,7 @@ class GPU
Stats m_stats = {};

private:
bool CompileDisplayPipelines(bool display, bool deinterlace, bool chroma_smoothing);
bool CompileDisplayPipelines(bool display, bool deinterlace, bool chroma_smoothing, Error* error);

using GP0CommandHandler = bool (GPU::*)();
using GP0CommandHandlerTable = std::array<GP0CommandHandler, 256>;
Expand Down
18 changes: 7 additions & 11 deletions src/core/gpu_hw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ bool GPU_HW::IsHardwareRenderer() const
return true;
}

bool GPU_HW::Initialize()
bool GPU_HW::Initialize(Error* error)
{
if (!GPU::Initialize())
if (!GPU::Initialize(error))
return false;

const GPUDevice::Features features = g_gpu_device->GetFeatures();
Expand Down Expand Up @@ -275,16 +275,12 @@ bool GPU_HW::Initialize()

PrintSettingsToLog();

Error error;
if (!CompilePipelines(&error))
{
ERROR_LOG("Failed to compile pipelines: {}", error.GetDescription());
if (!CompilePipelines(error))
return false;
}

if (!CreateBuffers())
{
ERROR_LOG("Failed to create framebuffer");
Error::SetStringView(error, "Failed to create framebuffer");
return false;
}

Expand Down Expand Up @@ -4214,11 +4210,11 @@ void GPU_HW::DrawRendererStats()
}
}

std::unique_ptr<GPU> GPU::CreateHardwareRenderer()
std::unique_ptr<GPU> GPU::CreateHardwareRenderer(Error* error)
{
std::unique_ptr<GPU_HW> gpu(std::make_unique<GPU_HW>());
if (!gpu->Initialize())
return nullptr;
if (!gpu->Initialize(error))
gpu.reset();

return gpu;
}
2 changes: 1 addition & 1 deletion src/core/gpu_hw.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class GPU_HW final : public GPU
const Threading::Thread* GetSWThread() const override;
bool IsHardwareRenderer() const override;

bool Initialize() override;
bool Initialize(Error* error) override;
void Reset(bool clear_vram) override;
bool DoState(StateWrapper& sw, GPUTexture** host_texture, bool update_display) override;

Expand Down
10 changes: 5 additions & 5 deletions src/core/gpu_sw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ bool GPU_SW::IsHardwareRenderer() const
return false;
}

bool GPU_SW::Initialize()
bool GPU_SW::Initialize(Error* error)
{
if (!GPU::Initialize() || !m_backend.Initialize(g_settings.gpu_use_thread))
if (!GPU::Initialize(error) || !m_backend.Initialize(g_settings.gpu_use_thread))
return false;

static constexpr const std::array formats_for_16bit = {GPUTexture::Format::RGB565, GPUTexture::Format::RGBA5551,
Expand Down Expand Up @@ -810,11 +810,11 @@ void GPU_SW::UpdateCLUT(GPUTexturePaletteReg reg, bool clut_is_8bit)
m_backend.PushCommand(cmd);
}

std::unique_ptr<GPU> GPU::CreateSoftwareRenderer()
std::unique_ptr<GPU> GPU::CreateSoftwareRenderer(Error* error)
{
std::unique_ptr<GPU_SW> gpu(std::make_unique<GPU_SW>());
if (!gpu->Initialize())
return nullptr;
if (!gpu->Initialize(error))
gpu.reset();

return gpu;
}
2 changes: 1 addition & 1 deletion src/core/gpu_sw.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class GPU_SW final : public GPU
const Threading::Thread* GetSWThread() const override;
bool IsHardwareRenderer() const override;

bool Initialize() override;
bool Initialize(Error* error) override;
bool DoState(StateWrapper& sw, GPUTexture** host_texture, bool update_display) override;
void Reset(bool clear_vram) override;
void UpdateSettings(const Settings& old_settings) override;
Expand Down
6 changes: 3 additions & 3 deletions src/core/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2466,9 +2466,9 @@ bool System::CreateGPU(GPURenderer renderer, bool is_switching, bool fullscreen,
}

if (renderer == GPURenderer::Software)
g_gpu = GPU::CreateSoftwareRenderer();
g_gpu = GPU::CreateSoftwareRenderer(error);
else
g_gpu = GPU::CreateHardwareRenderer();
g_gpu = GPU::CreateHardwareRenderer(error);

if (!g_gpu)
{
Expand All @@ -2479,7 +2479,7 @@ bool System::CreateGPU(GPURenderer renderer, bool is_switching, bool fullscreen,
Settings::GetRendererName(renderer)),
Host::OSD_CRITICAL_ERROR_DURATION);
g_gpu.reset();
g_gpu = GPU::CreateSoftwareRenderer();
g_gpu = GPU::CreateSoftwareRenderer(error);
if (!g_gpu)
{
ERROR_LOG("Failed to create fallback software renderer.");
Expand Down

0 comments on commit 1ae2dcf

Please sign in to comment.