This commit is contained in:
Paris Oplopoios 2024-08-10 23:37:29 +00:00 committed by GitHub
commit fce94fbb71
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 684 additions and 69 deletions

View file

@ -19,6 +19,14 @@ public:
/// Prevents the function from being invoked when we go out of scope.
ALWAYS_INLINE void Cancel() { m_func.reset(); }
/// Runs the destructor function now instead of when we go out of scope.
ALWAYS_INLINE void Run() {
if (!m_func.has_value()) return;
m_func.value()();
m_func.reset();
}
/// Explicitly fires the function.
ALWAYS_INLINE void Invoke()
{

View file

@ -74,14 +74,7 @@ std::unique_ptr<GL::Context> Context::Create(const WindowInfo& wi, const Version
context = ContextAGL::Create(wi, versions_to_try, num_versions_to_try);
#else
if (wi.type == WindowInfo::Type::X11)
{
const char* use_egl_x11 = std::getenv("USE_EGL_X11");
if (use_egl_x11 && std::strcmp(use_egl_x11, "1") == 0)
context = ContextEGLX11::Create(wi, versions_to_try, num_versions_to_try);
else
context = ContextGLX::Create(wi, versions_to_try, num_versions_to_try);
}
context = ContextEGLX11::Create(wi, versions_to_try, num_versions_to_try);
#ifdef WAYLAND_ENABLED
if (wi.type == WindowInfo::Type::Wayland)
context = ContextEGLWayland::Create(wi, versions_to_try, num_versions_to_try);

View file

@ -20,6 +20,7 @@ std::unique_ptr<Context> ContextEGLX11::CreateSharedContext(const WindowInfo& wi
{
std::unique_ptr<ContextEGLX11> context = std::make_unique<ContextEGLX11>(wi);
context->m_display = m_display;
context->m_supports_surfaceless = m_supports_surfaceless;
if (!context->CreateContextAndSurface(m_version, m_context, false))
return nullptr;

View file

@ -19,6 +19,17 @@ static void* GetProcAddressCallback(const char* name)
}
namespace GL {
static bool ReloadWGL(HDC dc)
{
if (!gladLoadWGL(dc))
{
Log_ErrorPrint("Loading GLAD WGL functions failed");
return false;
}
return true;
}
ContextWGL::ContextWGL(const WindowInfo& wi) : Context(wi) {}
ContextWGL::~ContextWGL()
@ -149,8 +160,8 @@ std::unique_ptr<Context> ContextWGL::CreateSharedContext(const WindowInfo& wi)
}
else
{
Log_ErrorPrint("PBuffer not implemented");
return nullptr;
if (!context->CreatePBuffer())
return nullptr;
}
if (m_version.profile == Profile::NoProfile)
@ -305,6 +316,32 @@ bool ContextWGL::CreatePBuffer()
static constexpr const int pb_attribs[] = {0, 0};
HGLRC temp_rc = nullptr;
ScopedGuard temp_rc_guard([&temp_rc, hdc]() {
if (temp_rc)
{
wglMakeCurrent(hdc, nullptr);
wglDeleteContext(temp_rc);
}
});
if (!GLAD_WGL_ARB_pbuffer)
{
// we're probably running completely surfaceless... need a temporary context.
temp_rc = wglCreateContext(hdc);
if (!temp_rc || !wglMakeCurrent(hdc, temp_rc))
{
Log_ErrorPrint("Failed to create temporary context to load WGL for pbuffer.");
return false;
}
if (!ReloadWGL(hdc) || !GLAD_WGL_ARB_pbuffer)
{
Log_ErrorPrint("Missing WGL_ARB_pbuffer");
return false;
}
}
AssertMsg(m_pixel_format.has_value(), "Has pixel format for pbuffer");
HPBUFFERARB pbuffer = wglCreatePbufferARB(hdc, m_pixel_format.value(), 1, 1, pb_attribs);
if (!pbuffer)
@ -326,6 +363,7 @@ bool ContextWGL::CreatePBuffer()
m_dummy_dc = hdc;
m_pbuffer = pbuffer;
temp_rc_guard.Run();
pbuffer_guard.Cancel();
hdc_guard.Cancel();
hwnd_guard.Cancel();

21
third_party/lockfree/LICENSE vendored Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2023 Djordje Nedic
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

110
third_party/lockfree/lockfree/spsc/queue.hpp vendored Executable file
View file

@ -0,0 +1,110 @@
/**************************************************************
* @file queue.hpp
* @brief A queue implementation written in standard c++11
* suitable for both low-end microcontrollers all the way
* to HPC machines. Lock-free for single consumer single
* producer scenarios.
**************************************************************/
/**************************************************************
* Copyright (c) 2023 Djordje Nedic
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall
* be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
* KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* This file is part of lockfree
*
* Author: Djordje Nedic <nedic.djordje2@gmail.com>
* Version: v2.0.9
**************************************************************/
/************************** INCLUDE ***************************/
#ifndef LOCKFREE_QUEUE_HPP
#define LOCKFREE_QUEUE_HPP
#include <atomic>
#include <cstddef>
#include <type_traits>
#if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
#include <optional>
#endif
namespace lockfree {
namespace spsc {
/*************************** TYPES ****************************/
template <typename T, size_t size> class Queue {
static_assert(std::is_trivial<T>::value, "The type T must be trivial");
static_assert(size > 2, "Buffer size must be bigger than 2");
/********************** PUBLIC METHODS ************************/
public:
Queue();
/**
* @brief Adds an element into the queue.
* Should only be called from the producer thread.
* @param[in] element
* @retval Operation success
*/
bool Push(const T &element);
/**
* @brief Removes an element from the queue.
* Should only be called from the consumer thread.
* @param[out] element
* @retval Operation success
*/
bool Pop(T &element);
#if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
/**
* @brief Removes an element from the queue.
* Should only be called from the consumer thread.
* @retval Either the element or nothing
*/
std::optional<T> PopOptional();
#endif
/********************** PRIVATE MEMBERS ***********************/
private:
T _data[size]; /**< Data array */
#if LOCKFREE_CACHE_COHERENT
alignas(LOCKFREE_CACHELINE_LENGTH) std::atomic_size_t _r; /**< Read index */
alignas(
LOCKFREE_CACHELINE_LENGTH) std::atomic_size_t _w; /**< Write index */
#else
std::atomic_size_t _r; /**< Read index */
std::atomic_size_t _w; /**< Write index */
#endif
};
} /* namespace spsc */
} /* namespace lockfree */
/************************** INCLUDE ***************************/
/* Include the implementation */
#include "queue_impl.hpp"
#endif /* LOCKFREE_QUEUE_HPP */

View file

@ -0,0 +1,111 @@
/**************************************************************
* @file queue_impl.hpp
* @brief A queue implementation written in standard c++11
* suitable for both low-end microcontrollers all the way
* to HPC machines. Lock-free for single consumer single
* producer scenarios.
**************************************************************/
/**************************************************************
* Copyright (c) 2023 Djordje Nedic
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall
* be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
* KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* This file is part of lockfree
*
* Author: Djordje Nedic <nedic.djordje2@gmail.com>
* Version: v2.0.9
**************************************************************/
namespace lockfree {
namespace spsc {
/********************** PUBLIC METHODS ************************/
template <typename T, size_t size> Queue<T, size>::Queue() : _r(0U), _w(0U) {}
template <typename T, size_t size> bool Queue<T, size>::Push(const T &element) {
/*
The full check needs to be performed using the next write index not to
miss the case when the read index wrapped and write index is at the end
*/
const size_t w = _w.load(std::memory_order_relaxed);
size_t w_next = w + 1;
if (w_next == size) {
w_next = 0U;
}
/* Full check */
const size_t r = _r.load(std::memory_order_acquire);
if (w_next == r) {
return false;
}
/* Place the element */
_data[w] = element;
/* Store the next write index */
_w.store(w_next, std::memory_order_release);
return true;
}
template <typename T, size_t size> bool Queue<T, size>::Pop(T &element) {
/* Preload indexes with adequate memory ordering */
size_t r = _r.load(std::memory_order_relaxed);
const size_t w = _w.load(std::memory_order_acquire);
/* Empty check */
if (r == w) {
return false;
}
/* Remove the element */
element = _data[r];
/* Increment the read index */
r++;
if (r == size) {
r = 0U;
}
/* Store the read index */
_r.store(r, std::memory_order_release);
return true;
}
/********************* std::optional API **********************/
#if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
template <typename T, size_t size>
std::optional<T> Queue<T, size>::PopOptional() {
T element;
bool result = Pop(element);
if (result) {
return element;
} else {
return {};
}
}
#endif
} /* namespace spsc */
} /* namespace lockfree */