fix CAMetalLayers

This commit is contained in:
Greg Wells
2025-06-01 20:39:49 -04:00
parent 964ac7127d
commit 5b9f0d8ebe
22 changed files with 161 additions and 117 deletions

View File

@@ -60,7 +60,7 @@ typedef struct gnFunctions_t {
#include "core/presentation_queue/gryphn_presentation_queue.h"
typedef struct gnDeviceFunctions_t {
gnReturnCode (*_gnCreatePresentationQueue)(gnPresentationQueue* presentationQueue, const gnOutputDevice* device, struct gnPresentationQueueInfo_t presentationInfo);
void (*_gnPresentationQueueGetImage)(gnPresentationQueue* presentationQueue, uint64_t timeout, struct gnSemaphore_t* semaphore, uint32_t* imageIndex);
gnReturnCode (*_gnPresentationQueueGetImage)(gnPresentationQueue* presentationQueue, uint64_t timeout, struct gnSemaphore_t* semaphore, uint32_t* imageIndex);
void (*_gnDestroyPresentationQueue)(gnPresentationQueue *presentationQueue);
gnReturnCode (*_gnCreateShaderModule)(struct gnShaderModule_t* module, struct gnOutputDevice_t* device, struct gnShaderModuleInfo_t shaderModuleInfo);

View File

@@ -7,8 +7,8 @@ gnReturnCode gnCreatePresentationQueue(gnPresentationQueue* presentationQueue, s
return device->deviceFunctions->_gnCreatePresentationQueue(presentationQueue, device, presentationInfo);
}
void gnPresentationQueueGetImage(gnPresentationQueue* presentationQueue, uint64_t timeout, struct gnSemaphore_t* semaphore, uint32_t* imageIndex) {
presentationQueue->outputDevice->deviceFunctions->_gnPresentationQueueGetImage(presentationQueue, timeout, semaphore, imageIndex);
gnReturnCode gnPresentationQueueGetImage(gnPresentationQueue* presentationQueue, uint64_t timeout, struct gnSemaphore_t* semaphore, uint32_t* imageIndex) {
return presentationQueue->outputDevice->deviceFunctions->_gnPresentationQueueGetImage(presentationQueue, timeout, semaphore, imageIndex);
}
void gnDestroyPresentationQueue(gnPresentationQueue *presentationQueue) {

View File

@@ -27,5 +27,5 @@ typedef struct gnPresentationQueue_t {
} gnPresentationQueue;
gnReturnCode gnCreatePresentationQueue(gnPresentationQueue* presentationQueue, struct gnOutputDevice_t* device, struct gnPresentationQueueInfo_t presentationInfo);
void gnPresentationQueueGetImage(gnPresentationQueue* presentationQueue, uint64_t timeout, struct gnSemaphore_t* semaphore, uint32_t* imageIndex);
gnReturnCode gnPresentationQueueGetImage(gnPresentationQueue* presentationQueue, uint64_t timeout, struct gnSemaphore_t* semaphore, uint32_t* imageIndex);
void gnDestroyPresentationQueue(gnPresentationQueue* presentationQueue);

View File

@@ -32,7 +32,7 @@
#ifdef GN_PLATFORM_MACOS
typedef struct gnMacOSWindowSurfaceInfo_t {
NSWindow* window;
CAMetalLayer* layer;
} gnMacOSWindowSurfaceInfo;
gnReturnCode gnCreateMacOSWindowSurface(struct gnWindowSurface_t* windowSurface, gnInstance* instance, struct gnMacOSWindowSurfaceInfo_t createInfo);

View File

@@ -1,33 +0,0 @@
#ifdef GN_PLATFORM_MACOS
#undef GN_UTILS_CPP
#include "core/gryphn_rendering_api.h"
#include "core/instance/init/gryphn_dynamic_library.h"
#include "dlfcn.h"
gnRenderingAPI renderingAPIs[3] = {
GN_RENDERINGAPI_METAL,
GN_RENDERINGAPI_VULKAN,
GN_RENDERINGAPI_SOFTWARE
};
gnRenderingAPI* gnGetSupportedRenderingAPIs(int* count) {
*count = 3;
return renderingAPIs;
}
struct gnDynamicLibrary_t* gnLoadDynamicLibrary(const gnString path) {
struct gnDynamicLibrary_t* dll = malloc(sizeof(struct gnDynamicLibrary_t));
dll->dllPtr = dlopen(gnToCString(gnCombineStrings(path, ".dylib")), RTLD_LAZY),
dll->isValid = true;
if (dll->dllPtr == NULL) dll->isValid = false;
return dll;
}
void* gnLoadFunctionPtr(struct gnDynamicLibrary_t* dll, const char* name) {
if (dll->isValid == false) return NULL;
return dlsym(dll->dllPtr, name);
}
void gnUnloadDynamicLibrary(struct gnDynamicLibrary_t* dll) {
if (dll->isValid) dlclose(dll->dllPtr);
}
#endif

View File

@@ -4,6 +4,19 @@
#ifdef __OBJC__
@class NSWindow;
@class CAMetalLayer;
@class MTKView;
#else
typedef void NSWindow;
typedef void CAMetalLayer;
typedef void MTKView;
#endif
MTKView* gnCreateMTKView(NSWindow* window);
void gnWindowSetMTKView(NSWindow* window, MTKView* view);
CAMetalLayer* gnGetCAMetalLayer(NSWindow* window);
// CAMetalLayer* gnCreateMetalLayer(NSWindow* window);
// void gnAttachMetalLayer(NSWindow* window, CAMetalLayer* layer);
// void gnMetalLayerSetBounds(CAMetalLayer* layer, NSWindow* window);
// CAMetalLayer* gnGetMetalLayer(NSWindow* window);

View File

@@ -0,0 +1,91 @@
#ifdef GN_PLATFORM_MACOS
#undef GN_UTILS_CPP
#include "core/gryphn_rendering_api.h"
#include "core/instance/init/gryphn_dynamic_library.h"
#include "dlfcn.h"
#include "gryphn_platform_macos.h"
#import <QuartzCore/QuartzCore.h>
#import <Cocoa/Cocoa.h>
#import <MetalKit/MetalKit.h>
gnRenderingAPI renderingAPIs[3] = {
GN_RENDERINGAPI_METAL,
GN_RENDERINGAPI_VULKAN,
GN_RENDERINGAPI_SOFTWARE
};
gnRenderingAPI* gnGetSupportedRenderingAPIs(int* count) {
*count = 3;
return renderingAPIs;
}
struct gnDynamicLibrary_t* gnLoadDynamicLibrary(const gnString path) {
struct gnDynamicLibrary_t* dll = malloc(sizeof(struct gnDynamicLibrary_t));
dll->dllPtr = dlopen(gnToCString(gnCombineStrings(path, ".dylib")), RTLD_LAZY),
dll->isValid = true;
if (dll->dllPtr == NULL) dll->isValid = false;
return dll;
}
void* gnLoadFunctionPtr(struct gnDynamicLibrary_t* dll, const char* name) {
if (dll->isValid == false) return NULL;
return dlsym(dll->dllPtr, name);
}
void gnUnloadDynamicLibrary(struct gnDynamicLibrary_t* dll) {
if (dll->isValid) dlclose(dll->dllPtr);
}
MTKView* gnCreateMTKView(NSWindow* window) {
MTKView* view = [[MTKView alloc] initWithFrame:window.frame];
[view setColorPixelFormat:MTLPixelFormatBGRA8Unorm_sRGB];
[view setPreferredFramesPerSecond:120];
CGSize rect = { window.frame.size.width * window.backingScaleFactor, window.frame.size.height * window.backingScaleFactor };
[view setClearColor:MTLClearColorMake(1.0f, 0.0f, 0.0f, 1.0f)];
[view setDrawableSize:rect];
return view;
}
void gnWindowSetMTKView(NSWindow* window, MTKView* view) {
[window setContentView:view];
}
CAMetalLayer* gnGetCAMetalLayer(NSWindow* window) {
MTKView* view = window.contentView;
return (CAMetalLayer*)view.layer;
}
/*
CAMetalLayer* gnCreateMetalLayer(NSWindow* window) {
// CAMetalLayer* layer = [CAMetalLayer layer];
// [window.contentView setWantsLayer:YES];
// [layer setContentsScale:[window backingScaleFactor]];
// [layer setFrame:window.contentView.bounds];
// [window.contentView setLayer:layer];
// return layer;
CAMetalLayer* layer = [CAMetalLayer layer];
// if (window.scaleFramebuffer)
// [window->ns.layer setContentsScale:[window->ns.object backingScaleFactor]];
[window.contentView layer];
[window.contentView setWantsLayer:YES];
return layer;
}
void gnAttachMetalLayer(NSWindow* window, CAMetalLayer* layer) {
// [window.contentView setWantsLayer:YES];
// // [window.contentView setLayer:layer];
// [layer setFrame:window.contentView.bounds];
// [layer setContentsScale:[window backingScaleFactor]];
}
void gnMetalLayerSetBounds(CAMetalLayer* layer, NSWindow* window) {
// CGSize size = window.contentView.bounds.size;
// CGFloat scale = window.backingScaleFactor;
// [layer setFrame:window.contentView.bounds];
// [layer setDrawableSize:CGSizeMake(size.width * scale, size.height * scale)];
// [layer setContentsScale:[window backingScaleFactor]];
}
*/
#endif

View File

@@ -33,7 +33,9 @@ typedef enum gnReturnCode_t {
GN_FAILED_TO_ALLOCATE_COMMAND_BUFFERS,
GN_FAILED_TO_CREATE_FENCE,
GN_FAILED_TO_CREATE_SEMAPHORE,
GN_FAILED_TO_SUBMIT_COMMAND_BUFFER
GN_FAILED_TO_SUBMIT_COMMAND_BUFFER,
GN_OUT_OF_DATE_PRESENTATION_QUEUE,
GN_SUBOPTIMAL_PRESENTATION_QUEUE
} gnReturnCode;
typedef gnReturnCode gnErrorCode;
@@ -72,5 +74,7 @@ static const char* gnErrorCodeToCString(enum gnReturnCode_t returnCode) {
case GN_FAILED_TO_CREATE_FENCE: return "GN_FAILED_TO_CREATE_FENCE";
case GN_FAILED_TO_CREATE_SEMAPHORE: return "GN_FAILED_TO_CREATE_SEMAPHORE";
case GN_FAILED_TO_SUBMIT_COMMAND_BUFFER: return "GN_FAILED_TO_SUBMIT_COMMAND_BUFFER";
case GN_OUT_OF_DATE_PRESENTATION_QUEUE: return "GN_OUT_OF_DATE_PRESENTATION_QUEUE";
case GN_SUBOPTIMAL_PRESENTATION_QUEUE: return "GN_SUBOPTIMAL_PRESENTATION_QUEUE";
}
}