fix: Linux build

- Prepare for supporting Xlib and Wayland (in the future), by having a
  option for 'use_xlib'
- Fixed a few compile errors
This commit is contained in:
Kevin Trogant 2023-10-17 15:22:02 +02:00
parent 81798df3e9
commit 4b97f8ff33
8 changed files with 59 additions and 32 deletions

View File

@ -23,7 +23,7 @@ endif
# Debug specific flags # Debug specific flags
if buildtype == 'debug' or buildtype == 'debugoptimized' if buildtype == 'debug' or buildtype == 'debugoptimized'
add_project_arguments([ '-DDEBUG'], language : 'c') add_project_arguments([ '-DVY_DEBUG'], language : 'c')
endif endif
# Gather dependencies # Gather dependencies
@ -31,6 +31,12 @@ thread_dep = dependency('threads')
m_dep = compiler.find_library('m', required : false) m_dep = compiler.find_library('m', required : false)
vk_dep = dependency('vulkan', required : false) vk_dep = dependency('vulkan', required : false)
windowing_dep = []
if get_option('use_xlib')
windowing_dep = dependency('x11', required : true)
add_project_arguments(['-DVY_USE_XLIB'], language : 'c')
endif
# glfw_proj = subproject('glfw', default_options : 'default_library=static') # glfw_proj = subproject('glfw', default_options : 'default_library=static')
# glfw_dep = glfw_proj.get_variable('glfw_dep') # glfw_dep = glfw_proj.get_variable('glfw_dep')
@ -59,7 +65,7 @@ runtime_lib = library('vyrt',
# Contrib Sources # Contrib Sources
'contrib/xxhash/xxhash.c', 'contrib/xxhash/xxhash.c',
dependencies : [thread_dep, m_dep], dependencies : [thread_dep, m_dep, windowing_dep],
include_directories : incdir, include_directories : incdir,
c_pch : 'pch/rt_pch.h') c_pch : 'pch/rt_pch.h')

1
meson.options Normal file
View File

@ -0,0 +1 @@
option('use_xlib', type : 'boolean', value : false, description : 'Use Xlib for window creation under linux')

1
meson_options.txt Symbolic link
View File

@ -0,0 +1 @@
meson.options

View File

@ -1 +1,3 @@
#include <Windows.h> #ifdef _WIN32
#include <Windows.h>
#endif

View File

@ -2,8 +2,8 @@
#ifdef _WIN32 #ifdef _WIN32
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <Windows.h> #include <Windows.h>
int WINAPI wWinMain(HINSTANCE hInstance, int WINAPI wWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance, HINSTANCE hPrevInstance,
@ -12,4 +12,10 @@ int WINAPI wWinMain(HINSTANCE hInstance,
return vyWin32Entry(hInstance, hPrevInstance, pCmdLine, nCmdShow); return vyWin32Entry(hInstance, hPrevInstance, pCmdLine, nCmdShow);
} }
#elif defined(__linux__)
int main(int argc, char **argv) {
return 0;
}
#endif #endif

View File

@ -1,18 +1,18 @@
#include <stdlib.h>
#include <malloc.h> #include <malloc.h>
#include <stdlib.h>
#define VY_VK_DONT_DEFINE_GPU_GLOBAL #define VY_VK_DONT_DEFINE_GPU_GLOBAL
#include "gpu.h" #include "gpu.h"
#include "runtime/runtime.h"
#include "runtime/renderer_api.h"
#include "runtime/config.h" #include "runtime/config.h"
#include "runtime/renderer_api.h"
#include "runtime/runtime.h"
VY_CVAR_I(r_EnableAPIAllocTracking, VY_CVAR_I(
r_EnableAPIAllocTracking,
"Enable tracking of allocations done by the vulkan api. [0/1] Default: 0", "Enable tracking of allocations done by the vulkan api. [0/1] Default: 0",
0); 0);
typedef struct { typedef struct {
size_t allocated; size_t allocated;
size_t reallocated; size_t reallocated;
@ -25,8 +25,7 @@ vy_vk_gpu g_gpu;
static VkAllocationCallbacks _tracking_alloc_cbs; static VkAllocationCallbacks _tracking_alloc_cbs;
static vy_vk_alloc_stats _alloc_stats; static vy_vk_alloc_stats _alloc_stats;
static const char *AllocationScopeToString(VkSystemAllocationScope scope) static const char *AllocationScopeToString(VkSystemAllocationScope scope) {
{
switch (scope) { switch (scope) {
case VK_SYSTEM_ALLOCATION_SCOPE_COMMAND: case VK_SYSTEM_ALLOCATION_SCOPE_COMMAND:
return "COMMAND"; return "COMMAND";
@ -52,7 +51,10 @@ static void LogAllocationStats(const vy_vk_alloc_stats *stats) {
stats->max_alignment); stats->max_alignment);
} }
static void *TrackAllocation(void *userData, size_t size, size_t alignment, VkSystemAllocationScope scope) { static void *TrackAllocation(void *userData,
size_t size,
size_t alignment,
VkSystemAllocationScope scope) {
vy_vk_alloc_stats *stats = userData; vy_vk_alloc_stats *stats = userData;
if (alignment > stats->max_alignment) if (alignment > stats->max_alignment)
stats->max_alignment = alignment; stats->max_alignment = alignment;
@ -63,7 +65,11 @@ static void *TrackAllocation(void *userData, size_t size, size_t alignment, VkSy
alignment, alignment,
AllocationScopeToString(scope)); AllocationScopeToString(scope));
LogAllocationStats(stats); LogAllocationStats(stats);
#ifdef _WIN32
return _aligned_malloc(size, alignment); return _aligned_malloc(size, alignment);
#else
return aligned_alloc(alignment, size);
#endif
} }
static void *TrackReallocation(void *userData, static void *TrackReallocation(void *userData,
@ -81,7 +87,7 @@ static void *TrackReallocation(void *userData,
alignment, alignment,
AllocationScopeToString(scope)); AllocationScopeToString(scope));
LogAllocationStats(stats); LogAllocationStats(stats);
return _aligned_realloc(original, size, alignment); return realloc(original, size);
} }
static void TrackFree(void *userData, void *memory) { static void TrackFree(void *userData, void *memory) {

View File

@ -10,12 +10,17 @@
#ifdef _WIN32 #ifdef _WIN32
struct HINSTANCE__; struct HINSTANCE__;
struct HWND__; struct HWND__;
#elif defined(VY_USE_XLIB)
struct _XDisplay;
#endif #endif
struct vy_renderer_init_info_s { struct vy_renderer_init_info_s {
#ifdef _WIN32 #ifdef _WIN32
struct HINSTANCE__ *hInstance; struct HINSTANCE__ *hInstance;
struct HWND__ *hWnd; struct HWND__ *hWnd;
#elif defined(VY_USE_XLIB)
struct _XDisplay *display;
unsigned long window;
#endif #endif
}; };

View File

@ -2,15 +2,15 @@
#include "runtime.h" #include "runtime.h"
#ifdef _WIN32 #ifdef _WIN32
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <Windows.h> #include <Windows.h>
struct vy_mutex_s { struct vy_mutex_s {
HANDLE handle; HANDLE handle;
ptrdiff_t next_reusable; ptrdiff_t next_reusable;
}; };
#define MAX_MUTEX 1024 #define MAX_MUTEX 1024
static vy_mutex _mutex[MAX_MUTEX]; static vy_mutex _mutex[MAX_MUTEX];
static ptrdiff_t _first_reusable = MAX_MUTEX; static ptrdiff_t _first_reusable = MAX_MUTEX;
static ptrdiff_t _next = 0; static ptrdiff_t _next = 0;
@ -51,7 +51,7 @@ VY_DLLEXPORT bool vyUnlockMutex(vy_mutex *mutex) {
#elif defined(__linux__) #elif defined(__linux__)
#include <pthread.h> #include <pthread.h>
struct vy_mutex_s { struct vy_mutex_s {
pthread_mutex_t handle; pthread_mutex_t handle;
ptrdiff_t next_reusable; ptrdiff_t next_reusable;
@ -70,7 +70,7 @@ VY_DLLEXPORT vy_mutex *vyCreateMutex(void) {
} else if (_next < MAX_MUTEX) { } else if (_next < MAX_MUTEX) {
vy_mutex *mtx = &_mutex[_next]; vy_mutex *mtx = &_mutex[_next];
if (pthread_mutex_init(&mtx->handle, NULL) != 0) { if (pthread_mutex_init(&mtx->handle, NULL) != 0) {
vyLog("core", "Mutex creation failed: %u", GetLastError()); vyLog("core", "Mutex creation failed");
return NULL; return NULL;
} }
mtx->next_reusable = MAX_MUTEX; mtx->next_reusable = MAX_MUTEX;