- Implemented EGL Context
This commit is contained in:
42
source/platform/interface/display.cpp
Normal file
42
source/platform/interface/display.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
// =====================================================================================================================
|
||||
// fennec, a free and open source game engine
|
||||
// Copyright © 2025 Medusa Slockbower
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
// =====================================================================================================================
|
||||
|
||||
#include <fennec/platform/interface/display.h>
|
||||
#include <fennec/platform/interface/gfxcontext.h>
|
||||
#include <fennec/platform/interface/platform.h>
|
||||
|
||||
namespace fennec
|
||||
{
|
||||
|
||||
display::~display() {
|
||||
delete _context;
|
||||
}
|
||||
|
||||
void display::select_context() {
|
||||
if (_context != nullptr) return;
|
||||
|
||||
const platform::global_context& globals = platform::get_globals();
|
||||
for (const auto& ctx : globals.graphics) {
|
||||
_context = ctx.constructor(this);
|
||||
if (_context) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,21 +23,22 @@ namespace fennec
|
||||
{
|
||||
|
||||
template<typename CtorT>
|
||||
static constexpr void insert_driver(list<platform::driver<CtorT>>& list, CtorT ctor, int priority) {
|
||||
using list_t = fennec::list<platform::driver<CtorT>>;
|
||||
static constexpr void insert_driver(list<platform::driver<CtorT>>& drvrs, CtorT ctor, int priority) {
|
||||
using list_t = list<platform::driver<CtorT>>;
|
||||
using iter_t = typename list_t::iterator;
|
||||
|
||||
iter_t it = list.begin();
|
||||
while (it != list.end()) {
|
||||
iter_t it = drvrs.begin();
|
||||
while (it != drvrs.end()) {
|
||||
if (priority > it->priority) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
list.insert(it, { priority, ctor });
|
||||
drvrs.insert(it, { priority, ctor });
|
||||
}
|
||||
|
||||
void platform::initialize() {
|
||||
load_display();
|
||||
_display->select_context();
|
||||
}
|
||||
|
||||
void platform::shutdown() {
|
||||
|
||||
@@ -25,8 +25,7 @@ namespace fennec
|
||||
{
|
||||
|
||||
STATIC_CONSTRUCTOR(_init_linux) {
|
||||
static linux_platform* platform = new linux_platform();
|
||||
file::cout().write(platform, sizeof(platform), 1);
|
||||
static linux_platform platform;
|
||||
}
|
||||
|
||||
void linux_platform::initialize() {
|
||||
@@ -34,7 +33,7 @@ void linux_platform::initialize() {
|
||||
}
|
||||
|
||||
void linux_platform::shutdown() {
|
||||
|
||||
platform::shutdown();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ STATIC_CONSTRUCTOR(_wayland_init) {
|
||||
}
|
||||
|
||||
wayland_display::wayland_display(platform* platform)
|
||||
: display(platform)
|
||||
: display(platform, "wayland", this)
|
||||
, _handle(nullptr)
|
||||
, _registry(nullptr)
|
||||
, _compositor(nullptr)
|
||||
@@ -56,22 +56,23 @@ wayland_display::wayland_display(platform* platform)
|
||||
|
||||
// Get handles
|
||||
_handle = wl_display_connect(nullptr);
|
||||
|
||||
if (not _handle) {
|
||||
cleanup();
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the wayland registry
|
||||
_registry = wl_display_get_registry(_handle);
|
||||
|
||||
if (not _registry) {
|
||||
cleanup();
|
||||
return;
|
||||
}
|
||||
|
||||
// Add listener for interfaces
|
||||
wl_registry_add_listener(_registry, &listener, this);
|
||||
wl_display_roundtrip(_handle);
|
||||
|
||||
// Check for fifo v1, wayland's builtin fifo only allows one frame to be in flight at a time
|
||||
if (not _fifo) {
|
||||
assert(_fifo, "Compositor does not support fifo-v1 protocol, falling back to other display protocols.");
|
||||
cleanup();
|
||||
@@ -111,7 +112,7 @@ void wayland_display::cleanup() {
|
||||
libwayland::unload_symbols(_platform);
|
||||
}
|
||||
|
||||
void wayland_display::listen_global(void* data, wl_registry* registry, uint32_t name, const char* itfc, uint32_t version) {
|
||||
void wayland_display::listen_global(void* data, wl_registry* registry, uint32_t name, const char* itfc, uint32_t version) {
|
||||
static constexpr wl_seat_listener seat_listener = {
|
||||
listen_seat, nullptr
|
||||
};
|
||||
@@ -132,10 +133,10 @@ void wayland_display::cleanup() {
|
||||
|
||||
}
|
||||
|
||||
void wayland_display::listen_global_remove(void*, wl_registry*, uint32_t) {
|
||||
void wayland_display::listen_global_remove(void*, wl_registry*, uint32_t) {
|
||||
}
|
||||
|
||||
void wayland_display::listen_seat(void*, wl_seat*, uint32_t) {
|
||||
void wayland_display::listen_seat(void*, wl_seat*, uint32_t) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
136
source/platform/opengl/egl/context.cpp
Normal file
136
source/platform/opengl/egl/context.cpp
Normal file
@@ -0,0 +1,136 @@
|
||||
// =====================================================================================================================
|
||||
// fennec, a free and open source game engine
|
||||
// Copyright © 2025 Medusa Slockbower
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
// =====================================================================================================================
|
||||
|
||||
#include <fennec/lang/startup.h>
|
||||
#include <fennec/platform/interface/display.h>
|
||||
#include <fennec/platform/interface/platform.h>
|
||||
#include <fennec/platform/opengl/egl/context.h>
|
||||
|
||||
namespace fennec
|
||||
{
|
||||
|
||||
static gfxcontext* _create_egl_context(display* display) {
|
||||
eglcontext* ctx = new eglcontext(display);
|
||||
if (not ctx->connected()) {
|
||||
delete ctx, ctx = nullptr;
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
|
||||
STATIC_CONSTRUCTOR(_egl_init) {
|
||||
platform::add_driver(_create_egl_context, 1);
|
||||
}
|
||||
|
||||
eglcontext::eglcontext(display* display)
|
||||
: gfxcontext(display, "EGL", this) {
|
||||
|
||||
const display::pixel_format& fmt = _display->get_color_format();
|
||||
|
||||
EGLint context_attrs[] = {
|
||||
#if FENNEC_GRAPHICS_OPENGL
|
||||
EGL_CONTEXT_MAJOR_VERSION, 4,
|
||||
EGL_CONTEXT_MINOR_VERSION, 3,
|
||||
#elif FENNEC_GRAPHICS_GLES3
|
||||
EGL_CONTEXT_MAJOR_VERSION, 3,
|
||||
EGL_CONTEXT_MINOR_VERSION, 2,
|
||||
#elif FENNEC_GRAPHICS_GLES2
|
||||
EGL_CONTEXT_MAJOR_VERSION, 2,
|
||||
EGL_CONTEXT_MINOR_VERSION, 0,
|
||||
#endif
|
||||
EGL_NONE
|
||||
};
|
||||
|
||||
EGLint config_attrs[] = {
|
||||
EGL_SURFACE_TYPE,
|
||||
EGL_WINDOW_BIT,
|
||||
EGL_DEPTH_SIZE, fmt.depth,
|
||||
EGL_RED_SIZE, fmt.r,
|
||||
EGL_RED_SIZE, fmt.g,
|
||||
EGL_RED_SIZE, fmt.b,
|
||||
EGL_RENDERABLE_TYPE,
|
||||
|
||||
#if FENNEC_GRAPHICS_OPENGL
|
||||
EGL_OPENGL_BIT,
|
||||
#elif FENNEC_GRAPHICS_GLES3
|
||||
EGL_OPENGL_ES3_BIT,
|
||||
#elif FENNEC_GRAPHICS_GLES2
|
||||
EGL_OPENGL_ES2_BIT,
|
||||
#endif
|
||||
EGL_NONE
|
||||
};
|
||||
|
||||
|
||||
_egldisplay = eglGetDisplay(_display->get_native_handle());
|
||||
if (_egldisplay == nullptr) {
|
||||
cleanup();
|
||||
return;
|
||||
}
|
||||
|
||||
if (not eglInitialize(_egldisplay, &_eglvmajor, &_eglvminor)) {
|
||||
cleanup();
|
||||
return;
|
||||
}
|
||||
|
||||
#if FENNEC_GRAPHICS_OPENGL
|
||||
if (not eglBindAPI(EGL_OPENGL_API)) {
|
||||
#elif FENNEC_GRAPHICS_GLES3 or FENNEC_GRAPHICS_GLES2
|
||||
if (not eglBindAPI(EGL_OPENGL_ES_API)) {
|
||||
#endif
|
||||
cleanup();
|
||||
return;
|
||||
}
|
||||
|
||||
EGLint n;
|
||||
if (not eglChooseConfig(_egldisplay, config_attrs, &_eglconfig, 1, &n)) {
|
||||
cleanup();
|
||||
return;
|
||||
}
|
||||
|
||||
_eglcontext = eglCreateContext(_egldisplay, _eglconfig, EGL_NO_CONTEXT, context_attrs);
|
||||
if (_eglcontext == nullptr) {
|
||||
cleanup();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
eglcontext::~eglcontext() {
|
||||
cleanup();
|
||||
}
|
||||
|
||||
bool eglcontext::connected() {
|
||||
return _eglcontext != nullptr;
|
||||
}
|
||||
|
||||
void eglcontext::make_current(gfxsurface*) {
|
||||
|
||||
}
|
||||
|
||||
void eglcontext::cleanup() {
|
||||
eglMakeCurrent(_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
||||
eglDestroyContext(_display, _eglcontext);
|
||||
eglTerminate(_display);
|
||||
eglReleaseThread();
|
||||
|
||||
_egldisplay = nullptr;
|
||||
_eglconfig = nullptr;
|
||||
_eglcontext = nullptr;
|
||||
_eglvmajor = 0;
|
||||
_eglvminor = 0;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user