Skip to content

Commit

Permalink
Refactor the linux backend, and add support for Wayland.
Browse files Browse the repository at this point in the history
  • Loading branch information
Apprentice-Alchemist committed Feb 3, 2022
1 parent d764a21 commit 51006cd
Show file tree
Hide file tree
Showing 37 changed files with 12,322 additions and 1,501 deletions.
2 changes: 2 additions & 0 deletions .clangd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CompileFlags:
Add: [-D,KINC_EDITOR_SUPPORT]
13 changes: 12 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
/build
/Tests/Empty/build
.DS_Store

# clangd cache
.cache
# ignore all build directories
**/build/**
# Shaders
*.vert
*.frag
*.comp
**/Deployment/Empty
**/Deployment/MultiWindow
compile_commands.json
.vscode/**
126 changes: 111 additions & 15 deletions Backends/Graphics4/OpenGL/Sources/kinc/backend/graphics4/OpenGL.c.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
#include "OpenGL.h"
#include "ogl.h"
#include <GL/gl.h>
#include <GL/glext.h>
#ifdef KINC_EGL
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <EGL/eglplatform.h>
#endif
#ifdef KINC_GLX
#include <GL/glx.h>
#endif
#include <kinc/backend/graphics4/vertexbuffer.h>

#include <kinc/graphics4/indexbuffer.h>
Expand Down Expand Up @@ -102,7 +112,9 @@ static void *glesDrawElementsInstanced;
static int texModesU[256];
static int texModesV[256];

void kinc_internal_resize(int window, int width, int height) {}
void kinc_internal_resize(int window, int width, int height) {
glViewport(0, 0, width, height);
}

void kinc_internal_change_framebuffer(int window, kinc_framebuffer_options_t *frame) {
#ifdef KORE_WINDOWS
Expand All @@ -115,7 +127,38 @@ void kinc_internal_change_framebuffer(int window, kinc_framebuffer_options_t *fr
#endif
}

#ifdef KINC_EGL
static EGLDisplay egl_display;
static EGLContext egl_context;
static EGLConfig egl_config;
#endif

struct {
#ifdef KINC_EGL
EGLSurface surface;
#endif
} kinc_opengl_windows[16] = {0};

#ifdef KINC_EGL
#define EGL_CHECK_ERROR() \
do { \
EGLint error = eglGetError(); \
if (error != EGL_SUCCESS) { \
kinc_log(KINC_LOG_LEVEL_ERROR, "EGL Error : %i", error); \
__builtin_trap(); \
exit(1); \
} \
} while (0);
#endif

void kinc_g4_destroy(int window) {
#ifdef KINC_EGL
eglMakeCurrent(egl_display, kinc_opengl_windows[window].surface, kinc_opengl_windows[window].surface, egl_context);
EGL_CHECK_ERROR()
eglDestroySurface(egl_display, kinc_opengl_windows[window].surface);
EGL_CHECK_ERROR()
kinc_opengl_windows[window].surface = NULL;
#endif
#ifdef KORE_WINDOWS
if (Kinc_Internal_windows[window].glContext) {
assert(wglMakeCurrent(NULL, NULL));
Expand Down Expand Up @@ -145,6 +188,11 @@ static void initGLState(int window) {
#endif
}

#ifdef KINC_EGL
EGLDisplay kinc_egl_get_display(void);
EGLNativeWindowType kinc_egl_get_native_window(int);
#endif

void kinc_g4_init(int windowId, int depthBufferBits, int stencilBufferBits, bool vsync) {
for (int i = 0; i < 256; ++i) {
texModesU[i] = GL_CLAMP_TO_EDGE;
Expand All @@ -153,7 +201,29 @@ void kinc_g4_init(int windowId, int depthBufferBits, int stencilBufferBits, bool
#ifdef KORE_WINDOWS
Kinc_Internal_initWindowsGLContext(windowId, depthBufferBits, stencilBufferBits);
#endif
#ifdef KINC_EGL
eglBindAPI(EGL_OPENGL_API);
if (!egl_display) {
egl_display = kinc_egl_get_display();
eglInitialize(egl_display, NULL, NULL);
EGL_CHECK_ERROR()
const EGLint attribs[] = {
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8,
EGL_DEPTH_SIZE, depthBufferBits, EGL_STENCIL_SIZE, stencilBufferBits, EGL_NONE};
EGLint num_configs = 0;
eglChooseConfig(egl_display, NULL, &egl_config, 1, &num_configs);
EGL_CHECK_ERROR()
egl_context = eglCreateContext(egl_display, egl_config, EGL_NO_CONTEXT, NULL);
EGL_CHECK_ERROR()
}

EGLSurface egl_surface = eglCreateWindowSurface(egl_display, egl_config, kinc_egl_get_native_window(windowId), NULL);
EGL_CHECK_ERROR()
eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context);
EGL_CHECK_ERROR()
kinc_opengl_windows[windowId].surface = egl_surface;

#endif
initGLState(windowId);

#ifdef KORE_WINDOWS
Expand Down Expand Up @@ -183,12 +253,14 @@ void kinc_g4_init(int windowId, int depthBufferBits, int stencilBufferBits, bool
#endif

#ifndef KORE_OPENGL_ES
int extensions;
int extensions = 0;
glGetIntegerv(GL_NUM_EXTENSIONS, &extensions);
for (int i = 0; i < extensions; ++i) {
const char *extension = (const char *)glGetStringi(GL_EXTENSIONS, i);
if (extension != NULL && strcmp(extension, "GL_NV_conservative_raster") == 0) {
Kinc_Internal_SupportsConservativeRaster = true;
if (glGetError() != GL_NO_ERROR) {
for (int i = 0; i < extensions; ++i) {
const char *extension = (const char *)glGetStringi(GL_EXTENSIONS, i);
if (extension != NULL && strcmp(extension, "GL_NV_conservative_raster") == 0) {
Kinc_Internal_SupportsConservativeRaster = true;
}
}
}
maxColorAttachments = 8;
Expand Down Expand Up @@ -413,9 +485,12 @@ void kinc_g4_draw_indexed_vertices_instanced_from_to(int instanceCount, int star
void androidSwapBuffers();
#endif

#ifdef KORE_LINUX
void swapLinuxBuffers(int window);
#endif
// #ifdef KORE_LINUX
// #include <kinc/backend/Linux.h>
// #include <kinc/backend/windowdata.h>
// // #define MAXIMUM_WINDOWS 16
// // extern struct KincWindowData kinc_internal_windows[MAXIMUM_WINDOWS];
// #endif

#ifdef KORE_MACOS
void swapBuffersMac(int window);
Expand All @@ -438,8 +513,22 @@ bool kinc_g4_swap_buffers() {
}
#elif defined(KORE_ANDROID)
androidSwapBuffers();
#elif defined(KORE_LINUX)
swapLinuxBuffers(0);
#elif defined(KINC_GLX)
for (int window = 9; window >= 0; --window) {
if (kinc_internal_windows[window].handle) {
glXMakeCurrent(kinc_linux_display, kinc_internal_windows[window].handle, kinc_internal_windows[window].context);
glXSwapBuffers(kinc_linux_display, kinc_internal_windows[window].handle);
}
}
#elif defined(KINC_EGL)
for (int window = 15; window >= 0; --window) {
if (kinc_opengl_windows[window].surface) {
eglMakeCurrent(egl_display, kinc_opengl_windows[window].surface, kinc_opengl_windows[window].surface, egl_context);
EGL_CHECK_ERROR()
eglSwapBuffers(egl_display, kinc_opengl_windows[window].surface);
EGL_CHECK_ERROR()
}
}
#elif defined(KORE_MACOS)
swapBuffersMac(0);
#elif defined(KORE_IOS)
Expand All @@ -454,14 +543,20 @@ void beginGL();

void kinc_g4_begin(int window) {
currentWindow = window;
#ifdef KINC_EGL
eglMakeCurrent(egl_display, kinc_opengl_windows[window].surface, kinc_opengl_windows[window].surface, egl_context);
EGL_CHECK_ERROR()
#elif defined(KINC_GLX)
glXMakeCurrent(kinc_linux_display, kinc_internal_windows[window].handle, kinc_internal_windows[window].context);
#else
Kinc_Internal_setWindowRenderTarget(window);

#endif
#ifdef KORE_IOS
beginGL();
#endif

glBindFramebuffer(GL_FRAMEBUFFER, GL_NONE);
glViewport(0, 0, kinc_window_width(window), kinc_window_height(window));

glCheckErrors();
#ifdef KORE_ANDROID
// if rendered to a texture, strange things happen if the backbuffer is not cleared
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Expand Down Expand Up @@ -835,7 +930,8 @@ void kinc_g4_set_render_target_face(kinc_g4_render_target_t *texture, int face)
}

void kinc_g4_restore_render_target() {
Kinc_Internal_setWindowRenderTarget(currentWindow);
// Kinc_Internal_setWindowRenderTarget(currentWindow);
glBindFramebuffer(GL_FRAMEBUFFER, GL_NONE);
glCheckErrors();
int w = kinc_window_width(currentWindow);
int h = kinc_window_height(currentWindow);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ static void compileShader(unsigned *id, const char *source, size_t length, kinc_
int result;
glGetShaderiv(*id, GL_COMPILE_STATUS, &result);
if (result != GL_TRUE) {
int length;
int length = 0;
glGetShaderiv(*id, GL_INFO_LOG_LENGTH, &length);
char *errormessage = (char *)malloc(length);
glGetShaderInfoLog(*id, length, NULL, errormessage);
Expand Down Expand Up @@ -200,7 +200,7 @@ void kinc_g4_pipeline_compile(kinc_g4_pipeline_t *state) {
int result;
glGetProgramiv(state->impl.programId, GL_LINK_STATUS, &result);
if (result != GL_TRUE) {
int length;
int length = 0;
glGetProgramiv(state->impl.programId, GL_INFO_LOG_LENGTH, &length);
char *errormessage = (char *)malloc(length);
glGetProgramInfoLog(state->impl.programId, length, NULL, errormessage);
Expand Down
70 changes: 32 additions & 38 deletions Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/Vulkan.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include <kinc/math/core.h>
#include <kinc/system.h>
#include <kinc/window.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef KORE_WINDOWS
#define ERR_EXIT(err_msg, err_class) \
Expand All @@ -22,15 +24,8 @@
} while (0)
#endif

#ifdef VK_USE_PLATFORM_WIN32_KHR
#define KINC_SURFACE_EXT_NAME VK_KHR_WIN32_SURFACE_EXTENSION_NAME
#endif
#ifdef VK_USE_PLATFORM_XLIB_KHR
#define KINC_SURFACE_EXT_NAME VK_KHR_XLIB_SURFACE_EXTENSION_NAME
#endif
#ifdef VK_USE_PLATFORM_ANDROID_KHR
#define KINC_SURFACE_EXT_NAME VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
#endif
void kinc_vulkan_get_instance_extensions(const char **extensions, int *index, int max);
VkBool32 kinc_wayland_vulkan_get_physical_device_presentation_support(VkPhysicalDevice physicalDevice, uint32_t queueFamilyIndex);
VkResult kinc_vulkan_create_surface(VkInstance instance, int window_index, VkSurfaceKHR *surface);

#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
Expand Down Expand Up @@ -645,46 +640,45 @@ void kinc_g5_init(int window, int depthBufferBits, int stencilBufferBits, bool v
err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
assert(!err);

static const char *wanted_extensions[64];
static bool found_extensions[64];
int num_found_extensions;
int wanted_extension_count = 0;
int max = 64;
#ifdef VALIDATE
wanted_extensions[wanted_extension_count++] = VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
#endif
wanted_extensions[wanted_extension_count++] = VK_KHR_SURFACE_EXTENSION_NAME;
kinc_vulkan_get_instance_extensions(wanted_extensions, &wanted_extension_count, max);

if (instance_extension_count > 0) {
VkExtensionProperties *instance_extensions = (VkExtensionProperties *)malloc(sizeof(VkExtensionProperties) * instance_extension_count);
err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
assert(!err);
for (uint32_t i = 0; i < instance_extension_count; i++) {
if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
surfaceExtFound = 1;
extension_names[enabled_extension_count++] = VK_KHR_SURFACE_EXTENSION_NAME;
}
if (!strcmp(KINC_SURFACE_EXT_NAME, instance_extensions[i].extensionName)) {
platformSurfaceExtFound = 1;
extension_names[enabled_extension_count++] = KINC_SURFACE_EXT_NAME;
}
if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extensionName)) {
#ifdef VALIDATE
extension_names[enabled_extension_count++] = VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
#endif
for (int i2 = 0; i2 < wanted_extension_count; i2++) {
if (strcmp(wanted_extensions[i2], instance_extensions[i].extensionName) == 0) {
found_extensions[i2] = true;
}
}
assert(enabled_extension_count < 64);
}

free(instance_extensions);
}

if (!surfaceExtFound) {
ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
"the " VK_KHR_SURFACE_EXTENSION_NAME " extension.\n\nDo you have a compatible "
"Vulkan installable client driver (ICD) installed?\nPlease "
"look at the Getting Started guide for additional "
"information.\n",
"vkCreateInstance Failure");
bool missing_extensions = false;

for (int i = 0; i < wanted_extension_count; i++) {
if (!found_extensions[i]) {
kinc_log(KINC_LOG_LEVEL_ERROR, "Failed to find required extension %s", wanted_extensions[i]);
missing_extensions = true;
}
}
if (!platformSurfaceExtFound) {
ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
"the " KINC_SURFACE_EXT_NAME " extension.\n\nDo you have a compatible "
"Vulkan installable client driver (ICD) installed?\nPlease "
"look at the Getting Started guide for additional "
"information.\n",
"vkCreateInstance Failure");

if (missing_extensions) {
exit(1);
}

VkApplicationInfo app = {0};
app.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
app.pNext = NULL;
Expand All @@ -709,8 +703,8 @@ void kinc_g5_init(int window, int depthBufferBits, int stencilBufferBits, bool v
info.enabledLayerCount = 0;
info.ppEnabledLayerNames = NULL;
#endif
info.enabledExtensionCount = enabled_extension_count;
info.ppEnabledExtensionNames = (const char *const *)extension_names;
info.enabledExtensionCount = wanted_extension_count;
info.ppEnabledExtensionNames = (const char *const *)wanted_extensions;

uint32_t gpu_count;

Expand Down
19 changes: 0 additions & 19 deletions Backends/System/Linux/Sources/kinc/backend/Display_XRandr.c.h

This file was deleted.

Loading

0 comments on commit 51006cd

Please sign in to comment.