revolver

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit c9ba0adbd7064240488f173bde6b565b1b59367b
parent 55e6f13db4e4079d556841a517b180a48da19b57
Author: Samdal <samdal@protonmail.com>
Date:   Tue, 18 Mar 2025 10:24:31 +0100

move egl into platform layer

Diffstat:
Mexamples/test.c | 4++--
Rsrc/render/KHR/khrplatform.h -> src/platform/gfx/KHR/khrplatform.h | 0
Rsrc/render/impl/glad.c -> src/platform/gfx/glad.c | 0
Rsrc/render/impl/glad.h -> src/platform/gfx/glad.h | 0
Msrc/platform/gfx/platform_gfx.h | 7++++++-
Msrc/platform/gfx/xcb_impl.c | 164++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
Msrc/platform/platform_inc.c | 4++++
Msrc/platform/platform_inc.h | 4++++
Msrc/render/impl/opengl.c | 140+------------------------------------------------------------------------------
Msrc/render/render.h | 12------------
Msrc/render/render_inc.c | 1-
Msrc/render/render_inc.h | 4----
12 files changed, 171 insertions(+), 169 deletions(-)

diff --git a/examples/test.c b/examples/test.c @@ -15,11 +15,11 @@ int main(void) { rv_assert(false); } break; case RV_EVENT_BUTTON_PRESS: { - rv_abort(0); + rv_abort_msg(0, "button press"); } break; case RV_EVENT_WINDOW_CLOSE: { if (e->window_close == window) { - rv_abort(0); + rv_abort_msg(0, "close request"); } } break; case RV_EVENT_WINDOW_CHANGE: { diff --git a/src/render/KHR/khrplatform.h b/src/platform/gfx/KHR/khrplatform.h diff --git a/src/render/impl/glad.c b/src/platform/gfx/glad.c diff --git a/src/render/impl/glad.h b/src/platform/gfx/glad.h diff --git a/src/platform/gfx/platform_gfx.h b/src/platform/gfx/platform_gfx.h @@ -51,4 +51,9 @@ RV_GLOBAL rv_window_event_t* rv_get_events(rv_arena* arena); // non-blocking // Querying RV_GLOBAL rv_vec2 rv_window_size(rv_window_handle_t* window); -RV_GLOBAL void* rv_window_render(rv_window_handle_t* window); + +////////////////////////////////////////////////////////////////// +// Rendering + +RV_GLOBAL void rv_window_render_begin(rv_window_handle_t* window); +RV_GLOBAL void rv_window_render_end(rv_window_handle_t* window); diff --git a/src/platform/gfx/xcb_impl.c b/src/platform/gfx/xcb_impl.c @@ -3,6 +3,157 @@ #include <xcb/xcb.h> +struct rv_window_handle_t { + rv_window_handle_t* next; + xcb_window_t window_id; + xcb_intern_atom_reply_t* close_reply; + void* render; +}; + + +////////////////////////////////////////////////////////////////// +// EGL + +#if RV_RENDER_OPENGL + +#include <EGL/egl.h> +#include <EGL/eglext.h> + +RV_INTERNAL struct { + bool32 is_init; + EGLContext* egl_context; + EGLDisplay egl_display; + EGLConfig config; +} egl_context; + + +RV_GLOBAL void* egl_attach(xcb_window_t window, xcb_connection_t* connection) +{ + { // make egl context + if (!egl_context.is_init) { + egl_context.is_init = true; + + { // egl_display + PFNEGLGETPLATFORMDISPLAYEXTPROC GetPlatformDisplayEXT = (PFNEGLGETPLATFORMDISPLAYEXTPROC)eglGetProcAddress("eglGetPlatformDisplayEXT"); + // initialize EGL + { + egl_context.egl_display = GetPlatformDisplayEXT(EGL_PLATFORM_XCB_EXT, connection, + (const EGLint[]){ + EGL_PLATFORM_XCB_SCREEN_EXT, + 0, // This is a screenp that you can get from 2nd argument of xcb_connect + EGL_NONE, + }); + rv_assert(egl_context.egl_display != EGL_NO_DISPLAY); + + EGLint major, minor; + if (!eglInitialize(egl_context.egl_display, &major, &minor)) + { + rv_abort_msg(1, "Cannot initialize EGL display"); + } + if (major < 1 || (major == 1 && minor < 5)) + { + rv_abort_msg(1, "EGL version 1.5 or higher required"); + } + } + } + + // choose OpenGL API for EGL, by default it uses OpenGL ES + EGLBoolean ok = eglBindAPI(EGL_OPENGL_API); + rv_assert(ok); + + { // egl_context + // choose EGL configuration + { + EGLint attr[] = + { + EGL_SURFACE_TYPE, EGL_WINDOW_BIT, + EGL_CONFORMANT, EGL_OPENGL_BIT, + EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, + EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER, + + EGL_RED_SIZE, 8, + EGL_GREEN_SIZE, 8, + EGL_BLUE_SIZE, 8, + EGL_DEPTH_SIZE, 24, + EGL_STENCIL_SIZE, 8, + + // uncomment for multisampled framebuffer + //EGL_SAMPLE_BUFFERS, 1, + //EGL_SAMPLES, 4, // 4x MSAA + + EGL_NONE, + }; + + EGLint count; + if (!eglChooseConfig(egl_context.egl_display, attr, &egl_context.config, 1, &count) || count != 1) + { + rv_abort_msg(1, "Cannot choose EGL config"); + } + } + + { + EGLint attr[] = + { + EGL_CONTEXT_MAJOR_VERSION, 3, + EGL_CONTEXT_MINOR_VERSION, 3, + EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT, + // ask for debug context for non "Release" builds + // this is so we can enable debug callback + EGL_CONTEXT_OPENGL_DEBUG, EGL_TRUE, + EGL_NONE, + }; + + egl_context.egl_context = eglCreateContext(egl_context.egl_display, egl_context.config, EGL_NO_CONTEXT, attr); + if (egl_context.egl_context == EGL_NO_CONTEXT) + { + rv_abort_msg(1, "Cannot create EGL context, OpenGL 4.5 not supported?"); + } + } + } + } + } + + eglSwapInterval(egl_context.egl_display, 1); + + EGLint attr[] = { + EGL_GL_COLORSPACE, EGL_GL_COLORSPACE_LINEAR, // or use EGL_GL_COLORSPACE_SRGB for sRGB framebuffer + EGL_RENDER_BUFFER, EGL_BACK_BUFFER, + EGL_NONE, + }; + + EGLSurface* surface = eglCreateWindowSurface(egl_context.egl_display, egl_context.config, window, attr); + if (surface == EGL_NO_SURFACE) { + rv_abort_msg(1, "Cannot create EGL surface"); + } + + { // load opengl functions + RV_LOCAL_PERSIST bool32 glad_loaded = false; + if (!glad_loaded) { + glad_loaded = true; + eglMakeCurrent(egl_context.egl_display, surface, surface, egl_context.egl_context); + gladLoadGLLoader((GLADloadproc)eglGetProcAddress); + } + } + + return surface; +} + +RV_GLOBAL void rv_window_render_begin(rv_window_handle_t* window) +{ + EGLSurface* surface = window->render; + eglMakeCurrent(egl_context.egl_display, surface, surface, egl_context.egl_context); +} + +RV_GLOBAL void rv_window_render_end(rv_window_handle_t* window) +{ + EGLSurface* surface = window->render; + eglSwapBuffers(egl_context.egl_display, surface); +} + + +#endif // RV_RENDER_OPENGL + + ////////////////////////////////////////////////////////////////// // Context @@ -25,13 +176,6 @@ RV_INTERNAL void make_xcb_context(void) { ////////////////////////////////////////////////////////////////// // window creation/destruction -struct rv_window_handle_t { - rv_window_handle_t* next; - xcb_window_t window_id; - xcb_intern_atom_reply_t* close_reply; - void* render; -}; - RV_GLOBAL rv_window_handle_t* rv_create_window(rv_window_desc_t desc) { make_xcb_context(); @@ -111,11 +255,11 @@ RV_GLOBAL rv_window_handle_t* rv_create_window(rv_window_desc_t desc) free(protocolReply); } - #if RV_RENDER_ENABLED if (desc.attach_render) { - res->render = rv_window_render_attach((void*)(s64)res->window_id, (void*)xcb_context.connection); +#if RV_RENDER_OPENGL + res->render = egl_attach(res->window_id, xcb_context.connection); +#endif } - #endif xcb_flush(xcb_context.connection); diff --git a/src/platform/platform_inc.c b/src/platform/platform_inc.c @@ -11,3 +11,7 @@ #elif RV_WIN_WINDOWS #error Windows GFX not implemented #endif + +#if RV_RENDER_OPENGL + #include "gfx/glad.c" +#endif diff --git a/src/platform/platform_inc.h b/src/platform/platform_inc.h @@ -5,4 +5,8 @@ #include "platform_types.h" #include "platform_core.h" #include "platform_arena.h" + #include "gfx/platform_gfx.h" +#if RV_RENDER_OPENGL + #include "gfx/glad.h" +#endif diff --git a/src/render/impl/opengl.c b/src/render/impl/opengl.c @@ -1,130 +1,8 @@ ////////////////////////////////////////////////////////////////// // opengl.c -#include <EGL/egl.h> -#include <EGL/eglext.h> #include <GL/glcorearb.h> -#include<GL/gl.h> - -RV_INTERNAL struct { - bool32 is_init; - EGLContext* egl_context; - EGLDisplay egl_display; - EGLConfig config; -} egl_context; - -RV_INTERNAL void make_egl_context(void* external_display) { - if (egl_context.is_init) return; - egl_context.is_init = true; - - { // egl_display - PFNEGLGETPLATFORMDISPLAYEXTPROC GetPlatformDisplayEXT = (PFNEGLGETPLATFORMDISPLAYEXTPROC)eglGetProcAddress("eglGetPlatformDisplayEXT"); - // initialize EGL - { - egl_context.egl_display = GetPlatformDisplayEXT(EGL_PLATFORM_XCB_EXT, external_display, - (const EGLint[]){ - EGL_PLATFORM_XCB_SCREEN_EXT, - 0, // This is a screenp that you can get from 2nd argument of xcb_connect - EGL_NONE, - }); - rv_assert(egl_context.egl_display != EGL_NO_DISPLAY); - - EGLint major, minor; - if (!eglInitialize(egl_context.egl_display, &major, &minor)) - { - rv_abort_msg(1, "Cannot initialize EGL display"); - } - if (major < 1 || (major == 1 && minor < 5)) - { - rv_abort_msg(1, "EGL version 1.5 or higher required"); - } - } - } - - // choose OpenGL API for EGL, by default it uses OpenGL ES - EGLBoolean ok = eglBindAPI(EGL_OPENGL_API); - rv_assert(ok); - - { // egl_context - // choose EGL configuration - { - EGLint attr[] = - { - EGL_SURFACE_TYPE, EGL_WINDOW_BIT, - EGL_CONFORMANT, EGL_OPENGL_BIT, - EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, - EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER, - - EGL_RED_SIZE, 8, - EGL_GREEN_SIZE, 8, - EGL_BLUE_SIZE, 8, - EGL_DEPTH_SIZE, 24, - EGL_STENCIL_SIZE, 8, - - // uncomment for multisampled framebuffer - //EGL_SAMPLE_BUFFERS, 1, - //EGL_SAMPLES, 4, // 4x MSAA - - EGL_NONE, - }; - - EGLint count; - if (!eglChooseConfig(egl_context.egl_display, attr, &egl_context.config, 1, &count) || count != 1) - { - rv_abort_msg(1, "Cannot choose EGL config"); - } - } - - { - EGLint attr[] = - { - EGL_CONTEXT_MAJOR_VERSION, 3, - EGL_CONTEXT_MINOR_VERSION, 3, - EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT, - // ask for debug context for non "Release" builds - // this is so we can enable debug callback - EGL_CONTEXT_OPENGL_DEBUG, EGL_TRUE, - EGL_NONE, - }; - - egl_context.egl_context = eglCreateContext(egl_context.egl_display, egl_context.config, EGL_NO_CONTEXT, attr); - if (egl_context.egl_context == EGL_NO_CONTEXT) - { - rv_abort_msg(1, "Cannot create EGL context, OpenGL 4.5 not supported?"); - } - } - } -} - -RV_GLOBAL void* rv_window_render_attach(void* window, void* external_display) -{ - make_egl_context(external_display); - - eglSwapInterval(egl_context.egl_display, 1); - - EGLint attr[] = { - EGL_GL_COLORSPACE, EGL_GL_COLORSPACE_LINEAR, // or use EGL_GL_COLORSPACE_SRGB for sRGB framebuffer - EGL_RENDER_BUFFER, EGL_BACK_BUFFER, - EGL_NONE, - }; - - EGLSurface* surface = eglCreateWindowSurface(egl_context.egl_display, egl_context.config, (EGLNativeWindowType)window, attr); - if (surface == EGL_NO_SURFACE) { - rv_abort_msg(1, "Cannot create EGL surface"); - } - - { // load opengl functions - RV_LOCAL_PERSIST bool32 glad_loaded = false; - if (!glad_loaded) { - glad_loaded = true; - eglMakeCurrent(egl_context.egl_display, surface, surface, egl_context.egl_context); - gladLoadGLLoader((GLADloadproc)eglGetProcAddress); - } - } - - - return surface; -} +#include <GL/gl.h> ////////////////////////////////////////////////////////////////// @@ -166,19 +44,3 @@ RV_GLOBAL void rv_window_render_commit(rv_window_handle_t* window, rv_render_pas rv_window_render_end(window); } - - -////////////////////////////////////////////////////////////////// -// non command buffer API - -RV_GLOBAL void rv_window_render_begin(rv_window_handle_t* window) -{ - EGLSurface* surface = rv_window_render(window); - eglMakeCurrent(egl_context.egl_display, surface, surface, egl_context.egl_context); -} - -RV_GLOBAL void rv_window_render_end(rv_window_handle_t* window) -{ - EGLSurface* surface = rv_window_render(window); - eglSwapBuffers(egl_context.egl_display, surface); -} diff --git a/src/render/render.h b/src/render/render.h @@ -1,13 +1,6 @@ ////////////////////////////////////////////////////////////////// // render.h -#define RV_RENDER_ENABLED 1 - -////////////////////////////////////////////////////////////////// -// Render attach - -RV_GLOBAL void* rv_window_render_attach(void* window, void* external_display); - ////////////////////////////////////////////////////////////////// // Render types @@ -88,8 +81,3 @@ RV_GLOBAL rv_render_clear_desc_t* rv_render_push_clear_desc(rv_arena* arena, rv_ // Submit RV_GLOBAL void rv_window_render_commit(rv_window_handle_t* window, rv_render_pass_list_t* passes); -////////////////////////////////////////////////////////////////// -// non command buffer API - -RV_GLOBAL void rv_window_render_begin(rv_window_handle_t* window); -RV_GLOBAL void rv_window_render_end(rv_window_handle_t* window); diff --git a/src/render/render_inc.c b/src/render/render_inc.c @@ -4,6 +4,5 @@ #include "render_helpers.c" #if RV_RENDER_OPENGL - #include "impl/glad.c" #include "impl/opengl.c" #endif diff --git a/src/render/render_inc.h b/src/render/render_inc.h @@ -2,7 +2,3 @@ // render_inc.h #include "render.h" - -#if RV_RENDER_OPENGL - #include "impl/glad.h" -#endif