Remove gnCreateWindowSurface, add platform specific window creation

functions
This commit is contained in:
Greg Wells
2025-05-13 17:35:17 -04:00
parent 556fa7b71c
commit f8a4ba22dd
22 changed files with 272 additions and 106 deletions

View File

@@ -11,7 +11,6 @@ target_include_directories(GryphnMetalImpl PUBLIC
${CMAKE_SOURCE_DIR}/gryphn/include/
${CMAKE_SOURCE_DIR}/gryphn/src/
${CMAKE_SOURCE_DIR}/gryphn/src/
${CMAKE_SOURCE_DIR}/depends/glfw/glfw-3.4/include/
depends/metal-cpp/
src/
depends/SPIRV-Cross/
@@ -21,10 +20,7 @@ add_compile_definitions(GN_REVEAL_IMPL)
add_subdirectory(depends/metal-cpp)
add_subdirectory(depends/SPIRV-Cross)
add_library(glfw SHARED IMPORTED)
set_target_properties(glfw PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/build/depends/glfw/glfw-3.4/src/libglfw.3.4.dylib)
target_link_libraries(GryphnMetalImpl METAL_CPP glfw spirv-cross-core spirv-cross-msl spirv-cross-cpp)
target_link_libraries(GryphnMetalImpl METAL_CPP spirv-cross-core spirv-cross-msl spirv-cross-cpp)
target_link_libraries(GryphnMetalImpl
"-framework IOKit"

View File

@@ -3,16 +3,12 @@
#import <QuartzCore/CAMetalLayer.h>
#import <QuartzCore/QuartzCore.h>
#import <Metal/Metal.h>
#define GLFW_EXPOSE_NATIVE_COCOA
#include <GLFW/glfw3.h>
#include <GLFW/glfw3native.h>
void mtlObjectCSetContentViewsLayer(void* window, void* layer) {
void mtlObjectCSetContentViewsLayer(void* view, void* layer) {
[CATransaction begin];
[CATransaction setDisableActions:YES];
NSWindow* nsWindow = (NSWindow*)glfwGetCocoaWindow((GLFWwindow*)window);
NSView* contentView = [nsWindow contentView];
NSView* contentView = (__bridge NSView*)view;
CAMetalLayer* metalLayer = (__bridge CAMetalLayer*)layer;
@@ -22,17 +18,17 @@ void mtlObjectCSetContentViewsLayer(void* window, void* layer) {
[CATransaction commit];
}
void* mtlCreateContentView(void* targetWindow) {
NSWindow* window = (__bridge NSWindow*)targetWindow;
NSRect frame = [[window contentView] frame]; // fallback size
NSView* contentView = [[NSView alloc] initWithFrame:frame];
[window setContentView:contentView];
// void* mtlCreateContentView(void* targetWindow) {
// NSWindow* window = (__bridge NSWindow*)targetWindow;
// NSRect frame = [[window contentView] frame]; // fallback size
// NSView* contentView = [[NSView alloc] initWithFrame:frame];
// [window setContentView:contentView];
if (contentView == nullptr)
frame = NSMakeRect(0, 0, window.frame.size.width, window.frame.size.height);
// if (contentView == nullptr)
// frame = NSMakeRect(0, 0, window.frame.size.width, window.frame.size.height);
return contentView;
}
// return contentView;
// }
// void* mtlInitContentView(void* targetView) {
// NSView* view = (__bridge NSView*)targetView;

View File

@@ -1,5 +1,5 @@
#pragma once
void mtlObjectCSetContentViewsLayer(void* window, void* layer);
void* mtlCreateContentView(void* targetWindow);
void mtlObjectCSetContentViewsLayer(void* view, void* layer);
// void* mtlCreateContentView(void* targetView);
void mtlInitializeMetalLayer(void* layer, bool vsync);

View File

@@ -1,8 +1,6 @@
#include <core/output_device/gryphn_physical_output_device.h>
#include <Metal/Metal.hpp>
// #include
#include <core/metal_instance.h>
#include <GLFW/glfw3.h>
#include "metal_output_devices.h"
struct mtlFramebufferVertex {

View File

@@ -1,10 +1,5 @@
#include <gryphn/gryphn.h>
#include <gryphn/gryphn_utils.h>
#import <GLFW/glfw3.h>
#define GLFW_EXPOSE_NATIVE_COCOA
#import <GLFW/glfw3native.h>
#include "bridge/metal_bridge.h"
#include "metal_instance.h"
@@ -18,14 +13,22 @@ GN_EXPORT void gnDestroyInstanceFn(gnInstance& instance) {
}
GN_EXPORT gnReturnCode gnInstanceSetWindowFn(gnInstance& instance, GLFWwindow* window) {
GN_EXPORT gnReturnCode gnCreateMacOSWindowSurfaceFn(gnInstance& instance, NS::Window* window, NS::View* view) {
if (instance.instance == nullptr) instance.instance = new gnPlatformInstanceData();
instance.instance->window = window;
int width, height;
glfwGetFramebufferSize(instance.instance->window, &width, &height);
instance.instance->metalWindow = reinterpret_cast<NS::Window*>(glfwGetCocoaWindow(window));
instance.instance->metalContentView = reinterpret_cast<NS::View*>(glfwGetCocoaView(window));
instance.instance->metalContentView = view;
return GN_SUCCESS;
}
// GN_EXPORT gnReturnCode gnInstanceSetWindowFn(gnInstance& instance, GLFWwindow* window) {
// if (instance.instance == nullptr) instance.instance = new gnPlatformInstanceData();
// instance.instance->window = window;
// int width, height;
// glfwGetFramebufferSize(instance.instance->window, &width, &height);
// instance.instance->metalWindow = reinterpret_cast<NS::Window*>(glfwGetCocoaWindow(window));
// instance.instance->metalContentView = reinterpret_cast<NS::View*>(glfwGetCocoaView(window));
// return GN_SUCCESS;
// }

View File

@@ -13,9 +13,6 @@
struct GLFWwindow;
struct gnPlatformInstanceData {
NS::Window* metalWindow;
NS::View* metalContentView;
GLFWwindow* window;
MTL::RenderPipelineState* framebufferRenderer, *testSquareRenderer;
};

View File

@@ -21,7 +21,7 @@ GN_EXPORT gnDevicePresentationDetails gnGetDevicePresentationDetailsFn(const gnP
GN_EXPORT gnReturnCode gnPresentationQueueGetNextImageAsyncFn(gnPresentationQueue& presentationQueue, const gnSyncSemaphore& semaphore, gnUInt* imageIndex) {
mtlObjectCSetContentViewsLayer(
presentationQueue.presentationQueue->outputDevice->outputDevice->instance->instance->window,
presentationQueue.presentationQueue->outputDevice->outputDevice->instance->instance->metalContentView,
presentationQueue.presentationQueue->layer);
presentationQueue.presentationQueue->currentDrawableIndex++;
@@ -43,7 +43,7 @@ GN_EXPORT gnReturnCode gnCreatePresentationQueueFn(gnPresentationQueue* presenta
presentationQueue->presentationQueue->outputDevice = const_cast<gnOutputDevice*>(&device);
presentationQueue->presentationQueue->layer = CA::MetalLayer::layer();
presentationQueue->presentationQueue->layer->setPixelFormat(MTL::PixelFormat::PixelFormatBGRA8Unorm_sRGB);
presentationQueue->presentationQueue->layer->setPixelFormat(MTL::PixelFormat::PixelFormatBGRA8Unorm);
presentationQueue->presentationQueue->layer->setFramebufferOnly(true);
presentationQueue->presentationQueue->layer->setDrawableSize({ (double)details.ImageSize.x, (double)details.ImageSize.y });
presentationQueue->presentationQueue->layer->setDevice(device.outputDevice->device);
@@ -51,7 +51,7 @@ GN_EXPORT gnReturnCode gnCreatePresentationQueueFn(gnPresentationQueue* presenta
mtlInitializeMetalLayer(presentationQueue->presentationQueue->layer, true);
mtlObjectCSetContentViewsLayer(
device.outputDevice->instance->instance->window,
device.outputDevice->instance->instance->metalContentView,
presentationQueue->presentationQueue->layer);
for (int i = 0; i < details.ImageCount; i++) {