summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoromniscient <17525998+omnisci3nce@users.noreply.github.com>2024-10-17 16:49:11 +1100
committeromniscient <17525998+omnisci3nce@users.noreply.github.com>2024-10-17 16:49:11 +1100
commit3e1aea0243f54e0b68baa3b19ac19f3d965484e0 (patch)
tree9f6e6d691be59ed328ffd716a0f56a2e33dbdf3d /src
parent16afbddeada7161e931dc261d3404bb5bbc1743d (diff)
start on metal backend
Diffstat (limited to 'src')
-rw-r--r--src/backend_mtl.m52
-rw-r--r--src/core.c29
2 files changed, 75 insertions, 6 deletions
diff --git a/src/backend_mtl.m b/src/backend_mtl.m
index 9a99e14..b3cd224 100644
--- a/src/backend_mtl.m
+++ b/src/backend_mtl.m
@@ -3,20 +3,66 @@
#ifdef GPU_METAL
#include <celeritas.h>
+#define MTL_DEBUG_LAYER 1
+
// Obj-C imports
#import <Foundation/Foundation.h>
#import <Metal/Metal.h>
#import <MetalKit/MetalKit.h>
#import <QuartzCore/CAMetalLayer.h>
+#define GLFW_INCLUDE_NONE
+#import <GLFW/glfw3.h>
+#define GLFW_EXPOSE_NATIVE_COCOA
+#import <GLFW/glfw3native.h>
+
// --- RAL types
-struct gpu_device {
- id<MTLDevice> id;
-};
struct gpu_swapchain {
int width, height;
CAMetalLayer* swapchain;
};
+typedef struct metal_context {
+ GLFWwindow* window;
+ NSWindow* metal_window;
+
+ id<MTLDevice> device;
+ id<CAMetalDrawable> surface;
+ gpu_swapchain default_swapchain;
+
+ id<MTLCommandQueue> command_queue;
+} metal_context;
+
+static metal_context ctx;
+
+void ral_backend_init(const char* window_name, struct GLFWwindow* window) {
+ printf("loading Metal backend\n");
+
+ printf("gpu device creation\n");
+ const id<MTLDevice> gpu = MTLCreateSystemDefaultDevice();
+ ctx.device = gpu;
+
+ printf("window init\n");
+ glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
+ glfwMakeContextCurrent(window);
+ NSWindow* nswindow = glfwGetCocoaWindow(window);
+ ctx.metal_window = nswindow;
+
+ // effectively the "framebuffer"
+ CAMetalLayer* metal_layer = [CAMetalLayer layer];
+ metal_layer.device = gpu;
+ metal_layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
+ ctx.metal_window.contentView.layer = metal_layer;
+ ctx.metal_window.contentView.wantsLayer = true;
+
+ printf("command queue creation\n");
+ const id<MTLCommandQueue> queue = [ctx.device newCommandQueue];
+ ctx.command_queue = queue;
+}
+
+void ral_backend_shutdown() {
+ // no-op
+}
+
#endif \ No newline at end of file
diff --git a/src/core.c b/src/core.c
index 0c3c5ea..080e806 100644
--- a/src/core.c
+++ b/src/core.c
@@ -4,12 +4,35 @@
NAMESPACED_LOGGER(core);
+core g_core = {0};
+
+// forward declares
+void key_callback(GLFWwindow* win, int key, int scancode, int action, int mods);
+
void core_bringup(const char* window_name, struct GLFWwindow* optional_window) {
- // INFO("Initiate Core bringup");
INFO("Initiate Core bringup");
INFO("Create GLFW window");
+ glfwInit();
+ GLFWwindow* glfw_window = glfwCreateWindow(800, 600, window_name, NULL, NULL);
+ g_core.window = glfw_window;
+
+ // This may move into a renderer struct
+ ral_backend_init(window_name, glfw_window);
+
+ glfwSetKeyCallback(glfw_window, key_callback);
+}
+void core_shutdown() {
+ ral_backend_shutdown();
+ glfwTerminate();
+}
+
+bool app_should_exit() {
+ return glfwWindowShouldClose(g_core.window) || g_core.should_exit;
}
-void core_shutdown() {}
-bool app_should_exit() { return false; }
+void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
+ if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) {
+ g_core.should_exit = true;
+ }
+} \ No newline at end of file