From 797191c2b67aabd67883fedabd5a514b756f6728 Mon Sep 17 00:00:00 2001 From: Gregory Wells Date: Wed, 30 Jul 2025 21:32:04 -0400 Subject: [PATCH] start using the dispatcher --- .gitmodules | 3 +++ CMakeLists.txt | 3 ++- depends/Dispatcher | 1 + projects/apis/metal/CMakeLists.txt | 1 + projects/apis/vulkan/CMakeLists.txt | 1 + projects/core/CMakeLists.txt | 1 + projects/core/src/instance/gryphn_instance.c | 17 ++++++++++--- projects/core/src/instance/gryphn_instance.h | 9 ++++--- projects/loader/CMakeLists.txt | 1 + .../loader/src/gryphn_instance_functions.h | 2 +- projects/loader/src/gryphn_loader.c | 24 +++++++++---------- projects/loader/src/gryphn_loader.h | 10 ++++++-- 12 files changed, 51 insertions(+), 22 deletions(-) create mode 160000 depends/Dispatcher diff --git a/.gitmodules b/.gitmodules index 32bff6c..c64c247 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "projects/apis/metal/depends/SPIRV-Cross"] path = projects/apis/metal/depends/SPIRV-Cross url = https://github.com/KhronosGroup/SPIRV-Cross.git +[submodule "depends/Dispatcher"] + path = depends/Dispatcher + url = https://github.com/GregoryWells2007/Dispatcher.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 7b68180..abda137 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,7 +34,8 @@ add_subdirectory(projects/core) # build gryphn core add_subdirectory(projects/extensions) add_subdirectory(projects/platform) # build gryphn platform add_subdirectory(projects/validation_layers/function_loader/) -target_link_libraries(Gryphn INTERFACE GryphnCore GryphnLoader GryphnPlatform GryphnFunctionValidator GryphnExtensions) +add_subdirectory(depends/Dispatcher/) +target_link_libraries(Gryphn INTERFACE dispatcher GryphnCore GryphnLoader GryphnPlatform GryphnFunctionValidator GryphnExtensions) if (VULKAN_BUILT) target_link_libraries(Gryphn INTERFACE GryphnVulkanImpl) diff --git a/depends/Dispatcher b/depends/Dispatcher new file mode 160000 index 0000000..c08c8f9 --- /dev/null +++ b/depends/Dispatcher @@ -0,0 +1 @@ +Subproject commit c08c8f94efe4b69378945c83e3e2f903035f956d diff --git a/projects/apis/metal/CMakeLists.txt b/projects/apis/metal/CMakeLists.txt index a1b945d..3c6ffdf 100644 --- a/projects/apis/metal/CMakeLists.txt +++ b/projects/apis/metal/CMakeLists.txt @@ -17,6 +17,7 @@ target_include_directories(GryphnMetalImpl PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../ ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/ ${CMAKE_CURRENT_SOURCE_DIR}/../../platform/ + ${CMAKE_CURRENT_SOURCE_DIR}/../../../depends/ ${CMAKE_CURRENT_SOURCE_DIR}/src/ depends/SPIRV-Cross/ ) diff --git a/projects/apis/vulkan/CMakeLists.txt b/projects/apis/vulkan/CMakeLists.txt index 257e56d..dd93ab9 100644 --- a/projects/apis/vulkan/CMakeLists.txt +++ b/projects/apis/vulkan/CMakeLists.txt @@ -22,6 +22,7 @@ target_include_directories(GryphnVulkanImpl PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../platform/ ${CMAKE_CURRENT_SOURCE_DIR}/../../ ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/ + ${CMAKE_CURRENT_SOURCE_DIR}/../../../depends/ ) if(WIN32) diff --git a/projects/core/CMakeLists.txt b/projects/core/CMakeLists.txt index 8d66a84..50458f9 100644 --- a/projects/core/CMakeLists.txt +++ b/projects/core/CMakeLists.txt @@ -15,3 +15,4 @@ target_include_directories(GryphnCore PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/) target_include_directories(GryphnCore PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../include/) target_include_directories(GryphnCore PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../utils) target_include_directories(GryphnCore PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../platform/) +target_include_directories(GryphnCore PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../depends/) diff --git a/projects/core/src/instance/gryphn_instance.c b/projects/core/src/instance/gryphn_instance.c index 49f124d..e7ac817 100644 --- a/projects/core/src/instance/gryphn_instance.c +++ b/projects/core/src/instance/gryphn_instance.c @@ -10,6 +10,11 @@ gnReturnCode gnCreateInstance(gnInstanceHandle* instance, gnInstanceCreateInfo* *instance = malloc(sizeof(struct gnInstance_t)); (*instance)->hasDebugger = GN_FALSE; (*instance)->layers = loaderLayerArrayListCreate(); + dispatcher_init(&(*instance)->dispatch); + dispatcher_set_function_array_size(&(*instance)->dispatch, sizeof(gnInstanceFunctions)); + + dispatcher_create_layer(&(*instance)->dispatch, loadAPIInstanceFunctions, &info->coreAPI); + loaderLayerArrayListAdd(&(*instance)->layers, loadLayer((loaderInfo){ .api = info->coreAPI, .layerToLoad = api_layer @@ -41,11 +46,17 @@ gnReturnCode gnCreateInstance(gnInstanceHandle* instance, gnInstanceCreateInfo* (*instance)->currentLayer = ((*instance)->layers.count - 1); for (int i = 0; i < (*instance)->layers.count; i++) (*instance)->layers.data[i].layerIndex = i; (*instance)->callingLayer = &(*instance)->layers.data[(*instance)->layers.count - 1]; - gnReturnCode core_code = (*instance)->callingLayer->instanceFunctions._gnCreateInstance((*instance), info); + + gnInstanceFunctions* instance_funcs = (gnInstanceFunctions*)((*instance)->dispatch.first_layer->function_array); + gnReturnCode core_code = instance_funcs->_gnCreateInstance((*instance), info); if (unsupportedExtension) return GN_UNLOADED_EXTENSION; return core_code; } -void gnDestroyInstance(gnInstanceHandle instance) { - instance->callingLayer->instanceFunctions._gnDestroyInstance(instance); +void gnDestroyInstance(gnInstanceHandle* instance) { + if (instance == GN_NULL_HANDLE) return; + + gnInstanceFunctions* instance_funcs = (gnInstanceFunctions*)((*instance)->dispatch.first_layer->function_array); + instance_funcs->_gnDestroyInstance((*instance)); + *instance = GN_NULL_HANDLE; } diff --git a/projects/core/src/instance/gryphn_instance.h b/projects/core/src/instance/gryphn_instance.h index 111a4cd..80dd021 100644 --- a/projects/core/src/instance/gryphn_instance.h +++ b/projects/core/src/instance/gryphn_instance.h @@ -5,6 +5,7 @@ #include "core/gryphn_return_code.h" #include "core/src/instance/gryphn_debugger.h" #include +#include "Dispatcher/dispatcher.h" typedef struct gnApplicationInfo { gnString applicationName; @@ -26,14 +27,16 @@ typedef struct gnInstanceCreateInfo { #include struct gnInstance_t { struct gnPlatformInstance_t* instance; + gnDebuggerCreateInfo debugger; + gnBool enabledExtensions[GN_EXT_MAX]; + dispatcher dispatch; + loaderLayerArrayList layers; loaderLayer* callingLayer; uint32_t currentLayer; - gnBool enabledExtensions[GN_EXT_MAX]; gnBool hasDebugger; - gnDebuggerCreateInfo debugger; }; #endif gnReturnCode gnCreateInstance(gnInstanceHandle* instance, gnInstanceCreateInfo* info); -void gnDestroyInstance(gnInstanceHandle instance); +void gnDestroyInstance(gnInstanceHandle* instance); diff --git a/projects/loader/CMakeLists.txt b/projects/loader/CMakeLists.txt index e414be2..6a5c240 100644 --- a/projects/loader/CMakeLists.txt +++ b/projects/loader/CMakeLists.txt @@ -7,3 +7,4 @@ target_include_directories(GryphnLoader PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../ut target_include_directories(GryphnLoader PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../) target_include_directories(GryphnLoader PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../core/src/) target_include_directories(GryphnLoader PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../core/) +target_include_directories(GryphnLoader PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../depends/) diff --git a/projects/loader/src/gryphn_instance_functions.h b/projects/loader/src/gryphn_instance_functions.h index 2e0a56a..bc0a95f 100644 --- a/projects/loader/src/gryphn_instance_functions.h +++ b/projects/loader/src/gryphn_instance_functions.h @@ -25,7 +25,7 @@ typedef struct gnInstanceFunctions { gnPhysicalDevice* (*_gnGetPhysicalDevices)(gnInstanceHandle instance, uint32_t* count); gnBool (*_gnPhysicalDeviceCanPresentToSurface)(gnPhysicalDevice device, gnWindowSurfaceHandle windowSurface); - gnReturnCode (*_gnCreateOutputDevice)(gnOutputDeviceHandle device, gnInstanceHandle instance, gnOutputDeviceInfo deviceInfo); + gnReturnCode (*_gnCreateOutputDevice)(gnInstanceHandle instance, gnOutputDeviceHandle device, gnOutputDeviceInfo deviceInfo); void (*_gnDestroyOutputDevice)(gnOutputDeviceHandle device); diff --git a/projects/loader/src/gryphn_loader.c b/projects/loader/src/gryphn_loader.c index ad06b6d..5f049a3 100644 --- a/projects/loader/src/gryphn_loader.c +++ b/projects/loader/src/gryphn_loader.c @@ -14,24 +14,27 @@ #include "core/src/instance/gryphn_instance.h" // load the speedy API functions or something like that -gnInstanceFunctions loadAPIInstanceFunctions(gnRenderingAPI api) { +dispatcher_bool loadAPIInstanceFunctions(dispatcher_layer* layer) { + gnRenderingAPI api = *(gnRenderingAPI*)layer->userData; + gnInstanceFunctions* funcs = (gnInstanceFunctions*)layer->function_array; switch (api) { - case GN_RENDERINGAPI_NONE: return (gnInstanceFunctions){ NULL }; + case GN_RENDERINGAPI_NONE: *funcs = (gnInstanceFunctions){ NULL }; #ifdef GN_API_VULKAN - case GN_RENDERINGAPI_VULKAN: return loadVulkanInstanceFunctions(); + case GN_RENDERINGAPI_VULKAN: *funcs = loadVulkanInstanceFunctions(); #endif - case GN_RENDERINGAPI_SOFTWARE: return (gnInstanceFunctions){ NULL }; - case GN_RENDERINGAPI_DIRECTX11: return (gnInstanceFunctions){ NULL }; - case GN_RENDERINGAPI_DIRECTX12: return (gnInstanceFunctions){ NULL }; + case GN_RENDERINGAPI_SOFTWARE: *funcs = (gnInstanceFunctions){ NULL }; + case GN_RENDERINGAPI_DIRECTX11: *funcs = (gnInstanceFunctions){ NULL }; + case GN_RENDERINGAPI_DIRECTX12: *funcs = (gnInstanceFunctions){ NULL }; #ifdef GN_API_OPENGL - case GN_RENDERINGAPI_OPENGL: return loadOpenGLInstanceFunctions(); + case GN_RENDERINGAPI_OPENGL: *funcs = loadOpenGLInstanceFunctions(); #endif #ifdef GN_API_METAL - case GN_RENDERINGAPI_METAL: return loadMetalInstanceFunctions(); + case GN_RENDERINGAPI_METAL: *funcs = loadMetalInstanceFunctions(); #endif - default: return (gnInstanceFunctions){NULL}; + default: *funcs = (gnInstanceFunctions){NULL}; } + return dispatcher_true; } gnDeviceFunctions loadAPIDeviceFunctions(gnRenderingAPI api) { @@ -113,7 +116,6 @@ gnQueueExtFunctions loadAPIQueueFunctions(gnRenderingAPI api) { loaderLayer null_layer() { return (loaderLayer){ - .instanceFunctions = (gnInstanceFunctions){ NULL }, .deviceFunctions = (gnDeviceFunctions){ NULL }, .commandFunctions = (gnCommandFunctions){ NULL } }; @@ -121,7 +123,6 @@ loaderLayer null_layer() { loaderLayer api_loaded_layer(gnRenderingAPI api) { return (loaderLayer){ - .instanceFunctions = loadAPIInstanceFunctions(api), .deviceFunctions = loadAPIDeviceFunctions(api), .commandFunctions = loadAPICommandFunctions(api), }; @@ -129,7 +130,6 @@ loaderLayer api_loaded_layer(gnRenderingAPI api) { loaderLayer function_check_layer() { return (loaderLayer){ - .instanceFunctions = loadFunctionLoaderInstanceFunctions(), .deviceFunctions = loadFunctionLoaderDeviceFunctions(), .commandFunctions = loadFunctionLoaderCommandFunctions(), diff --git a/projects/loader/src/gryphn_loader.h b/projects/loader/src/gryphn_loader.h index e4d93c4..936cf14 100644 --- a/projects/loader/src/gryphn_loader.h +++ b/projects/loader/src/gryphn_loader.h @@ -1,9 +1,9 @@ #pragma once -#include "gryphn_instance_functions.h" #include "gryphn_device_functions.h" #include "gryphn_command_functions.h" #include "gryphn_loader_info.h" #include "utils/lists/gryphn_array_list.h" +#include #include "extensions/synchronization/loader/sync_functions.h" #include "extensions/queues/queues_functions.h" @@ -12,7 +12,9 @@ typedef struct loaderLayer { // idk why I sperate these info different classes, I should really shove them in one bit class // they used to be loaded seperatly but I guess there not anymore // initlization is hard - gnInstanceFunctions instanceFunctions; + + // gnInstanceFunctions instanceFunctions; + gnDeviceFunctions deviceFunctions; gnCommandFunctions commandFunctions; @@ -31,3 +33,7 @@ void resetLayer(gnInstance instance); gnSyncExtFunctions loadAPISyncFunctions(gnRenderingAPI api); gnQueueExtFunctions loadAPIQueueFunctions(gnRenderingAPI api); + + + +dispatcher_bool loadAPIInstanceFunctions(dispatcher_layer* layer);