137 lines
4.1 KiB
Plaintext
137 lines
4.1 KiB
Plaintext
#import <QuartzCore/CAMetalLayer.h>
|
|
#import <Foundation/Foundation.h>
|
|
#import <AppKit/AppKit.h>
|
|
#define GN_CA_METAL_LAYER
|
|
#include "Gryphn/gryphn.h"
|
|
#include <iostream>
|
|
#include "stdlib.h"
|
|
#define GLFW_EXPOSE_NATIVE_COCOA
|
|
#include "glfw/include/GLFW/glfw3.h"
|
|
#include "glfw/include/GLFW/glfw3native.h"
|
|
|
|
#define CONCAT_IMPL(a, b) a##b
|
|
#define CONCAT(a, b) CONCAT_IMPL(a, b)
|
|
#define CHECK(func) \
|
|
gnReturnCode CONCAT(res, __LINE__) = (func); \
|
|
if (CONCAT(res, __LINE__) != GN_SUCCESS) { \
|
|
std::cout << "Gryphn Error at line " << __LINE__ \
|
|
<< ": " << CONCAT(res, __LINE__) << "\n"; \
|
|
}
|
|
|
|
GLFWwindow* window;
|
|
CAMetalLayer* layer;
|
|
gnVersion version = gnCreateVersion(1, 0, 0);
|
|
gnInstance instance;
|
|
gnPhysicalDevice pysicalDevice;
|
|
gnDevice device;
|
|
gnSurface surface;
|
|
|
|
void createInstance() {
|
|
uint32_t backendCount = 0;
|
|
CHECK(gnGetAvaliableBackends(version, &backendCount, nullptr));
|
|
if (backendCount == 0) {
|
|
throw std::runtime_error("Gryphn returned 0 avaliable backends");
|
|
}
|
|
gnBackend* backends = (gnBackend*)malloc(sizeof(gnBackend) * backendCount);
|
|
CHECK(gnGetAvaliableBackends(version, &backendCount, backends));
|
|
const char* extensions[2] = {
|
|
"GN_EXT_surface",
|
|
"GN_EXT_surface_cocoa"
|
|
};
|
|
|
|
gnInstanceCreateInfo createInfo = {
|
|
.backend = backends[0],
|
|
.info = {
|
|
.applicationName = "Chemistry Rendering App",
|
|
.applicationVersion = gnCreateVersion(0, 0, 1),
|
|
.engineName = "Chemistry Rendering Engine",
|
|
.engineVersion = gnCreateVersion(0, 0, 1)
|
|
},
|
|
.enabledValidationLayerCount = 0,
|
|
.enabledValidationLayers = nullptr,
|
|
.enabledExtensionCount = 2,
|
|
.enabledExtensions = extensions
|
|
};
|
|
CHECK(gnCreateInstance(&instance, &createInfo));
|
|
free(backends);
|
|
}
|
|
|
|
void createDevice() {
|
|
uint32_t physicalDeviceCount;
|
|
CHECK(gnEnumeratePhysicalDevices(instance, &physicalDeviceCount, nullptr));
|
|
gnPhysicalDevice* devices = (gnPhysicalDevice*)malloc(sizeof(gnPhysicalDevice) * physicalDeviceCount);
|
|
CHECK(gnEnumeratePhysicalDevices(instance, &physicalDeviceCount, devices));
|
|
|
|
std::cout << "Found " << physicalDeviceCount << " physical devices:\n";
|
|
for (int i = 0; i < physicalDeviceCount; i++) {
|
|
gnPhysicalDeviceProperties properties;
|
|
gnGetPhysicalDeviceProperties(devices[i], &properties);
|
|
std::cout << "Name: " << properties.deviceName << "\n";
|
|
}
|
|
|
|
pysicalDevice = devices[0];
|
|
|
|
const char* extensions[1] = {
|
|
"GN_EXT_swapchain"
|
|
};
|
|
|
|
gnDeviceCreateInfo createInfo = {
|
|
.physicalDevice = pysicalDevice,
|
|
.enabledExtensionCount = 1,
|
|
.enabledExtensions = extensions
|
|
};
|
|
CHECK(gnCreateDevice(instance, &createInfo, &device));
|
|
}
|
|
|
|
void createSurface() {
|
|
NSWindow* nswin = glfwGetCocoaWindow((GLFWwindow*)window);
|
|
NSView* view = [nswin contentView];
|
|
layer = [CAMetalLayer layer];
|
|
[view setWantsLayer:YES];
|
|
[view setLayer:layer];
|
|
layer.contentsScale = [nswin backingScaleFactor];
|
|
|
|
gnMetalSurfaceCreateInfo createInfo = {
|
|
.metalLayer = layer
|
|
};
|
|
CHECK(gnCreateMetalSurface(instance, &createInfo, &surface));
|
|
|
|
gnSurfaceCapabilities capabilites;
|
|
gnGetSurfaceCapabilities(pysicalDevice, surface, &capabilites);
|
|
}
|
|
|
|
void createSwapchain() {
|
|
|
|
}
|
|
|
|
int main() {
|
|
if (!glfwInit()) {
|
|
std::cout << "Failed to init GLFW\n";
|
|
return -1;
|
|
}
|
|
|
|
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
|
window = glfwCreateWindow(640, 360, "Chemistry Rendering App", nullptr, nullptr);
|
|
if (!window) {
|
|
std::cerr << "Failed to create window\n";
|
|
return -1;
|
|
}
|
|
|
|
try {
|
|
createInstance();
|
|
createDevice();
|
|
createSurface();
|
|
createSwapchain();
|
|
while (!glfwWindowShouldClose(window)) {
|
|
glfwPollEvents();
|
|
}
|
|
} catch (const std::exception& e) {
|
|
std::cerr << "Caught Exception: " << e.what() << "\n";
|
|
return 0;
|
|
}
|
|
glfwDestroyWindow(window);
|
|
gnDestroySurface(instance, &surface);
|
|
gnDestroyDevice(&device);
|
|
gnDestroyInstance(&instance);
|
|
}
|