Common: Make GL::Context::Create use gsl::span

This commit is contained in:
TellowKrinkle 2022-01-10 00:13:27 -06:00 committed by tellowkrinkle
parent 074e90d046
commit 1ac081ef4c
12 changed files with 42 additions and 59 deletions

View File

@ -79,42 +79,41 @@ namespace GL
return {};
}
std::unique_ptr<GL::Context> Context::Create(const WindowInfo& wi, const Version* versions_to_try,
size_t num_versions_to_try)
std::unique_ptr<GL::Context> Context::Create(const WindowInfo& wi, gsl::span<const Version> versions_to_try)
{
if (ShouldPreferESContext())
{
// move ES versions to the front
Version* new_versions_to_try = static_cast<Version*>(alloca(sizeof(Version) * num_versions_to_try));
Version* new_versions_to_try = static_cast<Version*>(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<const Version>(new_versions_to_try, versions_to_try.size());
}
std::unique_ptr<Context> 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::Version, 16>& Context::GetAllVersionsList()
gsl::span<const Context::Version> Context::GetAllVersionsList()
{
static constexpr std::array<Version, 16> 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

View File

@ -18,6 +18,7 @@
#include "common/Pcsx2Defs.h"
#include "common/WindowInfo.h"
#include <gsl/span>
#include <array>
#include <memory>
#include <vector>
@ -66,18 +67,11 @@ namespace GL {
virtual std::vector<FullscreenModeInfo> EnumerateFullscreenModes();
static std::unique_ptr<Context> Create(const WindowInfo& wi, const Version* versions_to_try,
size_t num_versions_to_try);
template<size_t N>
static std::unique_ptr<Context> Create(const WindowInfo& wi, const std::array<Version, N>& versions_to_try)
{
return Create(wi, versions_to_try.data(), versions_to_try.size());
}
static std::unique_ptr<Context> Create(const WindowInfo& wi, gsl::span<const Version> versions_to_try);
static std::unique_ptr<Context> Create(const WindowInfo& wi) { return Create(wi, GetAllVersionsList()); }
static const std::array<Version, 16>& GetAllVersionsList();
static gsl::span<const Version> GetAllVersionsList();
protected:
WindowInfo m_wi;

View File

@ -33,8 +33,7 @@ namespace GL
ContextAGL(const WindowInfo& wi);
~ContextAGL() override;
static std::unique_ptr<Context> Create(const WindowInfo& wi, const Version* versions_to_try,
size_t num_versions_to_try);
static std::unique_ptr<Context> Create(const WindowInfo& wi, gsl::span<const Version> 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<Context> CreateSharedContext(const WindowInfo& wi) override;
private:
bool Initialize(const Version* versions_to_try, size_t num_versions_to_try);
bool Initialize(gsl::span<const Version> versions_to_try);
bool CreateContext(NSOpenGLContext* share_context, int profile, bool make_current);
void BindContextToView();
void CleanupView();

View File

@ -44,21 +44,19 @@ namespace GL
dlclose(m_opengl_module_handle);
}
std::unique_ptr<Context> ContextAGL::Create(const WindowInfo& wi, const Version* versions_to_try,
size_t num_versions_to_try)
std::unique_ptr<Context> ContextAGL::Create(const WindowInfo& wi, gsl::span<const Version> versions_to_try)
{
std::unique_ptr<ContextAGL> context = std::make_unique<ContextAGL>(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<const Version> 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

View File

@ -35,17 +35,16 @@ namespace GL
DestroyContext();
}
std::unique_ptr<Context> ContextEGL::Create(const WindowInfo& wi, const Version* versions_to_try,
size_t num_versions_to_try)
std::unique_ptr<Context> ContextEGL::Create(const WindowInfo& wi, gsl::span<const Version> versions_to_try)
{
std::unique_ptr<ContextEGL> context = std::make_unique<ContextEGL>(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<const Version> 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;
}

View File

@ -26,8 +26,7 @@ namespace GL
ContextEGL(const WindowInfo& wi);
~ContextEGL() override;
static std::unique_ptr<Context> Create(const WindowInfo& wi, const Version* versions_to_try,
size_t num_versions_to_try);
static std::unique_ptr<Context> Create(const WindowInfo& wi, gsl::span<const Version> 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<const Version> versions_to_try);
bool CreateDisplay();
bool CreateContext(const Version& version, EGLContext share_context);
bool CreateContextAndSurface(const Version& version, EGLContext share_context, bool make_current);

View File

@ -37,11 +37,10 @@ namespace GL
dlclose(m_wl_module);
}
std::unique_ptr<Context> ContextEGLWayland::Create(const WindowInfo& wi, const Version* versions_to_try,
size_t num_versions_to_try)
std::unique_ptr<Context> ContextEGLWayland::Create(const WindowInfo& wi, gsl::span<const Version> versions_to_try)
{
std::unique_ptr<ContextEGLWayland> context = std::make_unique<ContextEGLWayland>(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;

View File

@ -28,8 +28,7 @@ namespace GL
ContextEGLWayland(const WindowInfo& wi);
~ContextEGLWayland() override;
static std::unique_ptr<Context> Create(const WindowInfo& wi, const Version* versions_to_try,
size_t num_versions_to_try);
static std::unique_ptr<Context> Create(const WindowInfo& wi, gsl::span<const Version> versions_to_try);
std::unique_ptr<Context> CreateSharedContext(const WindowInfo& wi) override;
void ResizeSurface(u32 new_surface_width = 0, u32 new_surface_height = 0) override;

View File

@ -27,11 +27,10 @@ namespace GL
}
ContextEGLX11::~ContextEGLX11() = default;
std::unique_ptr<Context> ContextEGLX11::Create(const WindowInfo& wi, const Version* versions_to_try,
size_t num_versions_to_try)
std::unique_ptr<Context> ContextEGLX11::Create(const WindowInfo& wi, gsl::span<const Version> versions_to_try)
{
std::unique_ptr<ContextEGLX11> context = std::make_unique<ContextEGLX11>(wi);
if (!context->Initialize(versions_to_try, num_versions_to_try))
if (!context->Initialize(versions_to_try))
return nullptr;
return context;

View File

@ -25,8 +25,7 @@ namespace GL
ContextEGLX11(const WindowInfo& wi);
~ContextEGLX11() override;
static std::unique_ptr<Context> Create(const WindowInfo& wi, const Version* versions_to_try,
size_t num_versions_to_try);
static std::unique_ptr<Context> Create(const WindowInfo& wi, gsl::span<const Version> versions_to_try);
std::unique_ptr<Context> CreateSharedContext(const WindowInfo& wi) override;
void ResizeSurface(u32 new_surface_width = 0, u32 new_surface_height = 0) override;

View File

@ -46,17 +46,16 @@ namespace GL
ReleaseDC();
}
std::unique_ptr<Context> ContextWGL::Create(const WindowInfo& wi, const Version* versions_to_try,
size_t num_versions_to_try)
std::unique_ptr<Context> ContextWGL::Create(const WindowInfo& wi, gsl::span<const Version> versions_to_try)
{
std::unique_ptr<ContextWGL> context = std::make_unique<ContextWGL>(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<const Version> 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

View File

@ -34,8 +34,7 @@ namespace GL
ContextWGL(const WindowInfo& wi);
~ContextWGL() override;
static std::unique_ptr<Context> Create(const WindowInfo& wi, const Version* versions_to_try,
size_t num_versions_to_try);
static std::unique_ptr<Context> Create(const WindowInfo& wi, gsl::span<const Version> 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<const Version> versions_to_try);
bool InitializeDC();
void ReleaseDC();
bool CreatePBuffer();