From 1ac081ef4cc19239fe9ae7b0e18380bd7a247bbe Mon Sep 17 00:00:00 2001 From: TellowKrinkle Date: Mon, 10 Jan 2022 00:13:27 -0600 Subject: [PATCH] Common: Make GL::Context::Create use gsl::span --- common/GL/Context.cpp | 27 ++++++++++++++------------- common/GL/Context.h | 12 +++--------- common/GL/ContextAGL.h | 5 ++--- common/GL/ContextAGL.mm | 10 ++++------ common/GL/ContextEGL.cpp | 11 +++++------ common/GL/ContextEGL.h | 5 ++--- common/GL/ContextEGLWayland.cpp | 5 ++--- common/GL/ContextEGLWayland.h | 3 +-- common/GL/ContextEGLX11.cpp | 5 ++--- common/GL/ContextEGLX11.h | 3 +-- common/GL/ContextWGL.cpp | 10 ++++------ common/GL/ContextWGL.h | 5 ++--- 12 files changed, 42 insertions(+), 59 deletions(-) diff --git a/common/GL/Context.cpp b/common/GL/Context.cpp index 9f4542aaf6..3ded7675f8 100644 --- a/common/GL/Context.cpp +++ b/common/GL/Context.cpp @@ -79,42 +79,41 @@ namespace GL return {}; } - std::unique_ptr Context::Create(const WindowInfo& wi, const Version* versions_to_try, - size_t num_versions_to_try) + std::unique_ptr Context::Create(const WindowInfo& wi, gsl::span versions_to_try) { if (ShouldPreferESContext()) { // move ES versions to the front - Version* new_versions_to_try = static_cast(alloca(sizeof(Version) * num_versions_to_try)); + Version* new_versions_to_try = static_cast(alloca(sizeof(Version) * versions_to_try.size())); size_t count = 0; - for (size_t i = 0; i < num_versions_to_try; i++) + for (size_t i = 0; i < versions_to_try.size(); i++) { if (versions_to_try[i].profile == Profile::ES) new_versions_to_try[count++] = versions_to_try[i]; } - for (size_t i = 0; i < num_versions_to_try; i++) + for (size_t i = 0; i < versions_to_try.size(); i++) { if (versions_to_try[i].profile != Profile::ES) new_versions_to_try[count++] = versions_to_try[i]; } - versions_to_try = new_versions_to_try; + versions_to_try = gsl::span(new_versions_to_try, versions_to_try.size()); } std::unique_ptr context; #if defined(_WIN32) && !defined(_M_ARM64) - context = ContextWGL::Create(wi, versions_to_try, num_versions_to_try); + context = ContextWGL::Create(wi, versions_to_try); #elif defined(__APPLE__) - context = ContextAGL::Create(wi, versions_to_try, num_versions_to_try); + context = ContextAGL::Create(wi, versions_to_try); #endif #if defined(X11_API) if (wi.type == WindowInfo::Type::X11) - context = ContextEGLX11::Create(wi, versions_to_try, num_versions_to_try); + context = ContextEGLX11::Create(wi, versions_to_try); #endif #if defined(WAYLAND_API) if (wi.type == WindowInfo::Type::Wayland) - context = ContextEGLWayland::Create(wi, versions_to_try, num_versions_to_try); + context = ContextEGLWayland::Create(wi, versions_to_try); #endif if (!context) @@ -160,9 +159,10 @@ namespace GL return context; } - const std::array& Context::GetAllVersionsList() + gsl::span Context::GetAllVersionsList() { - static constexpr std::array vlist = {{{Profile::Core, 4, 6}, + static constexpr Version vlist[] = { + {Profile::Core, 4, 6}, {Profile::Core, 4, 5}, {Profile::Core, 4, 4}, {Profile::Core, 4, 3}, @@ -177,7 +177,8 @@ namespace GL {Profile::ES, 3, 1}, {Profile::ES, 3, 0}, {Profile::ES, 2, 0}, - {Profile::NoProfile, 0, 0}}}; + {Profile::NoProfile, 0, 0} + }; return vlist; } } // namespace GL diff --git a/common/GL/Context.h b/common/GL/Context.h index cf0a6e4c8c..21ea1b2acb 100644 --- a/common/GL/Context.h +++ b/common/GL/Context.h @@ -18,6 +18,7 @@ #include "common/Pcsx2Defs.h" #include "common/WindowInfo.h" +#include #include #include #include @@ -66,18 +67,11 @@ namespace GL { virtual std::vector EnumerateFullscreenModes(); - static std::unique_ptr Create(const WindowInfo& wi, const Version* versions_to_try, - size_t num_versions_to_try); - - template - static std::unique_ptr Create(const WindowInfo& wi, const std::array& versions_to_try) - { - return Create(wi, versions_to_try.data(), versions_to_try.size()); - } + static std::unique_ptr Create(const WindowInfo& wi, gsl::span versions_to_try); static std::unique_ptr Create(const WindowInfo& wi) { return Create(wi, GetAllVersionsList()); } - static const std::array& GetAllVersionsList(); + static gsl::span GetAllVersionsList(); protected: WindowInfo m_wi; diff --git a/common/GL/ContextAGL.h b/common/GL/ContextAGL.h index 8694ce2cc7..3504a34263 100644 --- a/common/GL/ContextAGL.h +++ b/common/GL/ContextAGL.h @@ -33,8 +33,7 @@ namespace GL ContextAGL(const WindowInfo& wi); ~ContextAGL() override; - static std::unique_ptr Create(const WindowInfo& wi, const Version* versions_to_try, - size_t num_versions_to_try); + static std::unique_ptr Create(const WindowInfo& wi, gsl::span versions_to_try); void* GetProcAddress(const char* name) override; bool ChangeSurface(const WindowInfo& new_wi) override; @@ -46,7 +45,7 @@ namespace GL std::unique_ptr CreateSharedContext(const WindowInfo& wi) override; private: - bool Initialize(const Version* versions_to_try, size_t num_versions_to_try); + bool Initialize(gsl::span versions_to_try); bool CreateContext(NSOpenGLContext* share_context, int profile, bool make_current); void BindContextToView(); void CleanupView(); diff --git a/common/GL/ContextAGL.mm b/common/GL/ContextAGL.mm index 4705bc97f8..944d5002e9 100644 --- a/common/GL/ContextAGL.mm +++ b/common/GL/ContextAGL.mm @@ -44,21 +44,19 @@ namespace GL dlclose(m_opengl_module_handle); } - std::unique_ptr ContextAGL::Create(const WindowInfo& wi, const Version* versions_to_try, - size_t num_versions_to_try) + std::unique_ptr ContextAGL::Create(const WindowInfo& wi, gsl::span versions_to_try) { std::unique_ptr context = std::make_unique(wi); - if (!context->Initialize(versions_to_try, num_versions_to_try)) + if (!context->Initialize(versions_to_try)) return nullptr; return context; } - bool ContextAGL::Initialize(const Version* versions_to_try, size_t num_versions_to_try) + bool ContextAGL::Initialize(gsl::span versions_to_try) { - for (size_t i = 0; i < num_versions_to_try; i++) + for (const Version& cv : versions_to_try) { - const Version& cv = versions_to_try[i]; if (cv.profile == Profile::NoProfile && CreateContext(nullptr, NSOpenGLProfileVersionLegacy, true)) { // we already have the dummy context, so just use that diff --git a/common/GL/ContextEGL.cpp b/common/GL/ContextEGL.cpp index 70aa414daf..face6ac709 100644 --- a/common/GL/ContextEGL.cpp +++ b/common/GL/ContextEGL.cpp @@ -35,17 +35,16 @@ namespace GL DestroyContext(); } - std::unique_ptr ContextEGL::Create(const WindowInfo& wi, const Version* versions_to_try, - size_t num_versions_to_try) + std::unique_ptr ContextEGL::Create(const WindowInfo& wi, gsl::span versions_to_try) { std::unique_ptr context = std::make_unique(wi); - if (!context->Initialize(versions_to_try, num_versions_to_try)) + if (!context->Initialize(versions_to_try)) return nullptr; return context; } - bool ContextEGL::Initialize(const Version* versions_to_try, size_t num_versions_to_try) + bool ContextEGL::Initialize(gsl::span versions_to_try) { if (!gladLoadEGL()) { @@ -73,9 +72,9 @@ namespace GL if (!m_supports_surfaceless) Console.Warning("EGL implementation does not support surfaceless contexts, emulating with pbuffers"); - for (size_t i = 0; i < num_versions_to_try; i++) + for (const Version& version : versions_to_try) { - if (CreateContextAndSurface(versions_to_try[i], nullptr, true)) + if (CreateContextAndSurface(version, nullptr, true)) return true; } diff --git a/common/GL/ContextEGL.h b/common/GL/ContextEGL.h index 035bbdc534..5fa808efd1 100644 --- a/common/GL/ContextEGL.h +++ b/common/GL/ContextEGL.h @@ -26,8 +26,7 @@ namespace GL ContextEGL(const WindowInfo& wi); ~ContextEGL() override; - static std::unique_ptr Create(const WindowInfo& wi, const Version* versions_to_try, - size_t num_versions_to_try); + static std::unique_ptr Create(const WindowInfo& wi, gsl::span versions_to_try); void* GetProcAddress(const char* name) override; virtual bool ChangeSurface(const WindowInfo& new_wi) override; @@ -42,7 +41,7 @@ namespace GL virtual bool SetDisplay(); virtual EGLNativeWindowType GetNativeWindow(EGLConfig config); - bool Initialize(const Version* versions_to_try, size_t num_versions_to_try); + bool Initialize(gsl::span versions_to_try); bool CreateDisplay(); bool CreateContext(const Version& version, EGLContext share_context); bool CreateContextAndSurface(const Version& version, EGLContext share_context, bool make_current); diff --git a/common/GL/ContextEGLWayland.cpp b/common/GL/ContextEGLWayland.cpp index 8141fdf56b..a010db1164 100644 --- a/common/GL/ContextEGLWayland.cpp +++ b/common/GL/ContextEGLWayland.cpp @@ -37,11 +37,10 @@ namespace GL dlclose(m_wl_module); } - std::unique_ptr ContextEGLWayland::Create(const WindowInfo& wi, const Version* versions_to_try, - size_t num_versions_to_try) + std::unique_ptr ContextEGLWayland::Create(const WindowInfo& wi, gsl::span versions_to_try) { std::unique_ptr context = std::make_unique(wi); - if (!context->LoadModule() || !context->Initialize(versions_to_try, num_versions_to_try)) + if (!context->LoadModule() || !context->Initialize(versions_to_try)) return nullptr; return context; diff --git a/common/GL/ContextEGLWayland.h b/common/GL/ContextEGLWayland.h index e17662025a..156c79fec3 100644 --- a/common/GL/ContextEGLWayland.h +++ b/common/GL/ContextEGLWayland.h @@ -28,8 +28,7 @@ namespace GL ContextEGLWayland(const WindowInfo& wi); ~ContextEGLWayland() override; - static std::unique_ptr Create(const WindowInfo& wi, const Version* versions_to_try, - size_t num_versions_to_try); + static std::unique_ptr Create(const WindowInfo& wi, gsl::span versions_to_try); std::unique_ptr CreateSharedContext(const WindowInfo& wi) override; void ResizeSurface(u32 new_surface_width = 0, u32 new_surface_height = 0) override; diff --git a/common/GL/ContextEGLX11.cpp b/common/GL/ContextEGLX11.cpp index e2ee142895..c4c4322573 100644 --- a/common/GL/ContextEGLX11.cpp +++ b/common/GL/ContextEGLX11.cpp @@ -27,11 +27,10 @@ namespace GL } ContextEGLX11::~ContextEGLX11() = default; - std::unique_ptr ContextEGLX11::Create(const WindowInfo& wi, const Version* versions_to_try, - size_t num_versions_to_try) + std::unique_ptr ContextEGLX11::Create(const WindowInfo& wi, gsl::span versions_to_try) { std::unique_ptr context = std::make_unique(wi); - if (!context->Initialize(versions_to_try, num_versions_to_try)) + if (!context->Initialize(versions_to_try)) return nullptr; return context; diff --git a/common/GL/ContextEGLX11.h b/common/GL/ContextEGLX11.h index 65d0fe362f..e1ee5263a6 100644 --- a/common/GL/ContextEGLX11.h +++ b/common/GL/ContextEGLX11.h @@ -25,8 +25,7 @@ namespace GL ContextEGLX11(const WindowInfo& wi); ~ContextEGLX11() override; - static std::unique_ptr Create(const WindowInfo& wi, const Version* versions_to_try, - size_t num_versions_to_try); + static std::unique_ptr Create(const WindowInfo& wi, gsl::span versions_to_try); std::unique_ptr CreateSharedContext(const WindowInfo& wi) override; void ResizeSurface(u32 new_surface_width = 0, u32 new_surface_height = 0) override; diff --git a/common/GL/ContextWGL.cpp b/common/GL/ContextWGL.cpp index 8da0e766b0..33ca108c02 100644 --- a/common/GL/ContextWGL.cpp +++ b/common/GL/ContextWGL.cpp @@ -46,17 +46,16 @@ namespace GL ReleaseDC(); } - std::unique_ptr ContextWGL::Create(const WindowInfo& wi, const Version* versions_to_try, - size_t num_versions_to_try) + std::unique_ptr ContextWGL::Create(const WindowInfo& wi, gsl::span versions_to_try) { std::unique_ptr context = std::make_unique(wi); - if (!context->Initialize(versions_to_try, num_versions_to_try)) + if (!context->Initialize(versions_to_try)) return nullptr; return context; } - bool ContextWGL::Initialize(const Version* versions_to_try, size_t num_versions_to_try) + bool ContextWGL::Initialize(gsl::span versions_to_try) { if (m_wi.type == WindowInfo::Type::Win32) { @@ -73,9 +72,8 @@ namespace GL if (!CreateAnyContext(nullptr, true)) return false; - for (size_t i = 0; i < num_versions_to_try; i++) + for (const Version& cv : versions_to_try) { - const Version& cv = versions_to_try[i]; if (cv.profile == Profile::NoProfile) { // we already have the dummy context, so just use that diff --git a/common/GL/ContextWGL.h b/common/GL/ContextWGL.h index df0add38b2..789c8bc9e0 100644 --- a/common/GL/ContextWGL.h +++ b/common/GL/ContextWGL.h @@ -34,8 +34,7 @@ namespace GL ContextWGL(const WindowInfo& wi); ~ContextWGL() override; - static std::unique_ptr Create(const WindowInfo& wi, const Version* versions_to_try, - size_t num_versions_to_try); + static std::unique_ptr Create(const WindowInfo& wi, gsl::span versions_to_try); void* GetProcAddress(const char* name) override; bool ChangeSurface(const WindowInfo& new_wi) override; @@ -51,7 +50,7 @@ namespace GL HDC GetDCAndSetPixelFormat(HWND hwnd); - bool Initialize(const Version* versions_to_try, size_t num_versions_to_try); + bool Initialize(gsl::span versions_to_try); bool InitializeDC(); void ReleaseDC(); bool CreatePBuffer();