working on OpenGL loading

This commit is contained in:
2025-03-23 00:43:40 +01:00
parent 3e6aa18fbe
commit 6c2119d806
28 changed files with 1088 additions and 63 deletions

View File

@@ -8,6 +8,10 @@
#error "You must define PULSE_EGL_FUNCTION before including this file"
#endif
#ifndef PULSE_EGL_FUNCTION_EXT
#error "You must define PULSE_EGL_FUNCTION_EXT before including this file"
#endif
PULSE_EGL_FUNCTION(eglBindAPI, PFNEGLBINDAPIPROC)
PULSE_EGL_FUNCTION(eglChooseConfig, PFNEGLCHOOSECONFIGPROC)
PULSE_EGL_FUNCTION(eglCreateContext, PFNEGLCREATECONTEXTPROC)
@@ -22,3 +26,7 @@ PULSE_EGL_FUNCTION(eglInitialize, PFNEGLINITIALIZEPROC)
PULSE_EGL_FUNCTION(eglMakeCurrent, PFNEGLMAKECURRENTPROC)
PULSE_EGL_FUNCTION(eglQueryString, PFNEGLQUERYSTRINGPROC)
PULSE_EGL_FUNCTION(eglTerminate, PFNEGLTERMINATEPROC)
PULSE_EGL_FUNCTION_EXT(eglQueryDevicesEXT, PFNEGLQUERYDEVICESEXTPROC)
PULSE_EGL_FUNCTION_EXT(eglGetPlatformDisplayEXT, PFNEGLGETPLATFORMDISPLAYEXTPROC)
PULSE_EGL_FUNCTION_EXT(eglQueryDeviceStringEXT, PFNEGLQUERYDEVICESTRINGEXTPROC)

View File

@@ -2,8 +2,12 @@
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#include <string.h>
#include "EGLInstance.h"
#include "../../../PulseInternal.h"
#include "EGL/eglext.h"
#include "../OpenGLDevice.h"
static PulseLibModule egl_lib_module = PULSE_NULL_LIB_MODULE;
static uint32_t loader_references_count = 0;
@@ -49,20 +53,76 @@ static bool EGLLoadFunctions(EGLInstance* instance)
instance->fn = (T)instance->eglGetProcAddress(#fn); \
if(!instance->fn) \
return false;
#define PULSE_EGL_FUNCTION_EXT(fn, T) instance->fn = (T)instance->eglGetProcAddress(#fn);
#include "EGLFunctions.h"
#undef PULSE_EGL_FUNCTION
#undef PULSE_EGL_FUNCTION_EXT
return true;
}
bool EGLLoadInstance(EGLInstance* instance, bool es_context)
static bool EGLIsDeviceForbidden(EGLInstance* instance, EGLDeviceEXT device, PulseDevice* forbiden_devices, uint32_t forbiden_devices_count)
{
if(device == EGL_NO_DEVICE_EXT)
return true;
const char* test_device_vendor = instance->eglQueryDeviceStringEXT(device, EGL_VENDOR);
for(uint32_t i = 0; i < forbiden_devices_count; i++)
{
OpenGLDevice* opengl_device = OPENGL_RETRIEVE_DRIVER_DATA_AS(forbiden_devices[i], OpenGLDevice*);
if(opengl_device->context_type != OPENGL_CONTEXT_EGL)
continue;
const char* device_vendor = instance->eglQueryDeviceStringEXT(opengl_device->egl_instance.device, EGL_VENDOR);
if(device_vendor && test_device_vendor && strcmp(test_device_vendor, device_vendor) == 0)
return true;
}
return false;
}
#include <stdio.h>
bool EGLLoadInstance(EGLInstance* instance, PulseDevice* forbiden_devices, uint32_t forbiden_devices_count, bool es_context)
{
PULSE_CHECK_PTR_RETVAL(instance, false);
if(!EGLLoadFunctions(instance))
return false;
instance->display = instance->eglGetDisplay(EGL_DEFAULT_DISPLAY);
instance->device = EGL_NO_DEVICE_EXT;
if(instance->eglGetPlatformDisplayEXT && instance->eglQueryDevicesEXT && instance->eglQueryDeviceStringEXT)
{
EGLDeviceEXT* devices = PULSE_NULLPTR;
EGLDeviceEXT chosen_one = EGL_NO_DEVICE_EXT;
int32_t device_count;
uint64_t best_device_score = 0;
instance->eglQueryDevicesEXT(0, PULSE_NULLPTR, &device_count);
devices = (EGLDeviceEXT*)calloc(device_count, sizeof(EGLDeviceEXT));
PULSE_CHECK_ALLOCATION_RETVAL(devices, false);
instance->eglQueryDevicesEXT(device_count, devices, &device_count);
for(int32_t i = 0; i < device_count; i++)
{
if(EGLIsDeviceForbidden(instance, devices[i], forbiden_devices, forbiden_devices_count))
continue;
const char* exts = instance->eglQueryDeviceStringEXT(devices[i], EGL_EXTENSIONS);
uint64_t current_device_score = 0;
if(strstr(exts, "EGL_EXT_device_drm")) // tricky way to check if it is a discrete GPU
current_device_score += 10000;
if(current_device_score > best_device_score)
{
best_device_score = current_device_score;
chosen_one = devices[i];
}
}
instance->display = instance->eglGetPlatformDisplayEXT(EGL_PLATFORM_DEVICE_EXT, chosen_one, PULSE_NULLPTR);
}
else
instance->display = instance->eglGetDisplay(EGL_DEFAULT_DISPLAY);
PULSE_CHECK_PTR_RETVAL(instance->display, false);
instance->eglInitialize(instance->display, PULSE_NULLPTR, PULSE_NULLPTR);

View File

@@ -9,6 +9,7 @@
#define EGL_EGL_PROTOTYPES 0
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <Pulse.h>
typedef struct EGLInstance
@@ -18,12 +19,16 @@ typedef struct EGLInstance
EGLContext context;
EGLConfig config;
EGLDeviceEXT device;
#define PULSE_EGL_FUNCTION(fn, T) T fn;
#define PULSE_EGL_FUNCTION_EXT(fn, T) T fn;
#include "EGLFunctions.h"
#undef PULSE_EGL_FUNCTION
#undef PULSE_EGL_FUNCTION_EXT
} EGLInstance;
bool EGLLoadInstance(EGLInstance* instance, bool es_context);
bool EGLLoadInstance(EGLInstance* instance, PulseDevice* forbiden_devices, uint32_t forbiden_devices_count, bool es_context);
void EGLUnloadInstance(EGLInstance* instance);
#endif // PULSE_EGL_CONTEXT_H_