Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e8e54c0c0d | |||
| 01148e2dad | |||
| 2379e3ae6b | |||
| de67b88f18 | |||
| 6fd8917b88 |
+10
-1
@@ -1,5 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.12)
|
||||
project(ChemistryRenderingApp LANGUAGES C CXX OBJCXX) # OBJCXX is required for Metal .mm files
|
||||
project(ChemistryRenderingApp LANGUAGES C CXX)
|
||||
if (APPLE)
|
||||
add_executable(ChemistryRenderingApp main.mm)
|
||||
target_link_libraries(ChemistryRenderingApp PRIVATE
|
||||
"-framework Metal"
|
||||
@@ -7,6 +8,10 @@ target_link_libraries(ChemistryRenderingApp PRIVATE
|
||||
"-framework QuartzCore"
|
||||
"-framework Cocoa"
|
||||
)
|
||||
endif()
|
||||
if (UNIX AND NOT APPLE)
|
||||
add_executable(ChemistryRenderingApp main.cpp)
|
||||
endif()
|
||||
target_include_directories(ChemistryRenderingApp PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
|
||||
target_include_directories(ChemistryRenderingApp PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/glfw/include/")
|
||||
target_link_libraries(ChemistryRenderingApp PRIVATE GryphnLoader glfw)
|
||||
@@ -17,5 +22,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
add_subdirectory(Gryphn)
|
||||
set(BUILD_SHARED_LIBS ON)
|
||||
|
||||
set(GLFW_BUILD_WAYLAND OFF CACHE BOOL "" FORCE)
|
||||
set(GLFW_BUILD_X11 ON CACHE BOOL "" FORCE)
|
||||
|
||||
add_subdirectory(glfw)
|
||||
set(BUILD_SHARED_LIBS OFF)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
cmake_minimum_required(VERSION 3.12)
|
||||
project(Gryphn LANGUAGES C CXX OBJCXX) # OBJCXX is required for Metal .mm files
|
||||
project(Gryphn LANGUAGES C CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
@@ -10,4 +10,9 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
|
||||
# Add subdirectories
|
||||
add_subdirectory(GryphnLoader)
|
||||
if (APPLE)
|
||||
add_subdirectory(apis/GryphnMetal)
|
||||
endif()
|
||||
if (UNIX)
|
||||
add_subdirectory(apis/GryphnVulkan)
|
||||
endif()
|
||||
|
||||
@@ -16,6 +16,9 @@ target_include_directories(GryphnLoader PUBLIC
|
||||
if(APPLE)
|
||||
target_compile_definitions(GryphnLoader PUBLIC GN_PLATFORM_MACOS)
|
||||
endif()
|
||||
if (UNIX AND NOT APPLE)
|
||||
target_compile_definitions(GryphnLoader PUBLIC GN_PLATFORM_LINUX)
|
||||
endif()
|
||||
|
||||
# 4. Links
|
||||
if(UNIX AND NOT APPLE)
|
||||
|
||||
@@ -6,18 +6,31 @@
|
||||
|
||||
typedef uint32_t (*PFN_gnInternalIsApiSupported)(gnVersion);
|
||||
|
||||
gnReturnCode gnGetAvaliableBackends(gnVersion version, uint32_t* avaliable_backends, gnBackend* backends) {
|
||||
*avaliable_backends = 0;
|
||||
gnLib metalLib = gnLoadLib("bin/gryphn_metal.dylib");
|
||||
int gryphn_CheckLib(gnVersion version, const char* name) {
|
||||
gnLib metalLib = gnLoadLib(name);
|
||||
if (gnLibValid(&metalLib)) {
|
||||
PFN_gnInternalIsApiSupported isSupported = (PFN_gnInternalIsApiSupported)gnLoadLibFunction(&metalLib, "gnInternalIsApiSupported");
|
||||
if (isSupported(version) > 0) {
|
||||
if (backends != NULL) {
|
||||
backends[*avaliable_backends] = GN_BACKEND_METAL;
|
||||
}
|
||||
*avaliable_backends += 1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
gnCloseLib(&metalLib);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
gnReturnCode gnGetAvaliableBackends(gnVersion version, uint32_t* avaliable_backends, gnBackend* backends) {
|
||||
*avaliable_backends = 0;
|
||||
int metalSupported = gryphn_CheckLib(version, "bin/gryphn_metal.dylib");
|
||||
int vulkanSupported = gryphn_CheckLib(version, "bin/gryphn_vulkan.so");
|
||||
if (metalSupported) {
|
||||
if (backends != NULL)
|
||||
backends[*avaliable_backends] = GN_BACKEND_METAL;
|
||||
*avaliable_backends += 1;
|
||||
}
|
||||
if (vulkanSupported) {
|
||||
if (backends != NULL)
|
||||
backends[*avaliable_backends] = GN_BACKEND_VULKAN;
|
||||
*avaliable_backends += 1;
|
||||
}
|
||||
return GN_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
typedef struct gnLib {
|
||||
#ifdef GN_PLATFORM_MACOS
|
||||
#if defined(GN_PLATFORM_MACOS) || defined(GN_PLATFORM_LINUX)
|
||||
void* dylib;
|
||||
#endif
|
||||
} gnLib;
|
||||
|
||||
+3
-11
@@ -1,31 +1,23 @@
|
||||
#include "gryphn_lib.h"
|
||||
#ifdef GN_PLATFORM_MACOS
|
||||
#if defined(GN_PLATFORM_MACOS) || defined(GN_PLATFORM_LINUX)
|
||||
#include <dlfcn.h>
|
||||
#endif
|
||||
|
||||
|
||||
gnLib gnLoadLib(const char* path) {
|
||||
#ifdef GN_PLATFORM_MACOS
|
||||
gnLib lib;
|
||||
lib.dylib = dlopen(path, RTLD_NOW | RTLD_LOCAL);
|
||||
return lib;
|
||||
#endif
|
||||
}
|
||||
|
||||
int gnLibValid(const gnLib* lib) {
|
||||
#ifdef GN_PLATFORM_MACOS
|
||||
return lib->dylib != 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
void* gnLoadLibFunction(const gnLib* lib, const char* function) {
|
||||
#ifdef GN_PLATFORM_MACOS
|
||||
return dlsym(lib->dylib, function);
|
||||
#endif
|
||||
}
|
||||
|
||||
void gnCloseLib(const gnLib* lib) {
|
||||
#ifdef GN_PLATFORM_MACOS
|
||||
dlclose(lib->dylib);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,5 @@
|
||||
file(GLOB_RECURSE VULKAN_SOURCES CONFIGURE_DEPENDS "src/*.c")
|
||||
add_library(gryphn_vulkan SHARED ${VULKAN_SOURCES})
|
||||
project(gryphn_vulkan LANGUAGES C)
|
||||
set_target_properties(gryphn_vulkan PROPERTIES PREFIX "")
|
||||
target_link_libraries(gryphn_vulkan PRIVATE GryphnLoader vulkan)
|
||||
@@ -0,0 +1,54 @@
|
||||
#include "stdio.h"
|
||||
#include "instance/gryphn_instance.h"
|
||||
#include "gryphn_handle.h"
|
||||
#include "vulkan/vulkan.h"
|
||||
|
||||
uint32_t gnInternalIsApiSupported(gnVersion gryphnVersion) {
|
||||
if (gryphnVersion != gnCreateVersion(1, 0, 0)) return 0;
|
||||
|
||||
VkApplicationInfo appInfo = {
|
||||
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
|
||||
.pApplicationName = "Gryphn Vulkan Test",
|
||||
.applicationVersion = VK_MAKE_VERSION(1, 0, 0),
|
||||
.pEngineName = "Gryphn Vulkan Test",
|
||||
.engineVersion = VK_MAKE_VERSION(1, 0, 0),
|
||||
.apiVersion = VK_API_VERSION_1_0
|
||||
};
|
||||
|
||||
VkInstanceCreateInfo createInfo = {
|
||||
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
|
||||
.pApplicationInfo = &appInfo
|
||||
};
|
||||
|
||||
VkInstance instance;
|
||||
VkResult result = vkCreateInstance(&createInfo, NULL, &instance);
|
||||
if (result != VK_SUCCESS)
|
||||
return 0;
|
||||
vkDestroyInstance(instance, NULL);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// gnReturnCode destroyBackend(gnInstance instance) {
|
||||
// return GN_SUCCESS;
|
||||
// }
|
||||
|
||||
// gnReturnCode initBackend(gnInstance instance, gnInstanceCreateInfo* info) {
|
||||
// instance->dispatchTable.destroyInstance = destroyBackend;
|
||||
// instance->dispatchTable.enumeratePhysicalDevices = metalEnumeratePhysicalDevices;
|
||||
// instance->dispatchTable.getPhysicalDeviceProperties = metalGetPhysicalDeviceProperties;
|
||||
// instance->dispatchTable.createDevice = metalCreateDevice;
|
||||
|
||||
// for (int i = 0; i < info->enabledExtensionCount; i++) {
|
||||
// if (strcmp(info->enabledExtensions[i], "GN_EXT_surface") == 0) {
|
||||
// instance->dispatchTable.destroySurface = metalDestroySurface;
|
||||
// instance->dispatchTable.getSurfaceCapabilities = metalGetSurfaceCapabilities;
|
||||
// instance->dispatchTable.getSurfaceFormats = metalGetSurfaceFormats;
|
||||
// instance->dispatchTable.getSurfacePresentModes = metalGetSurfacePresentModes;
|
||||
// }
|
||||
// if (strcmp(info->enabledExtensions[i], "GN_EXT_surface_cocoa") == 0) {
|
||||
// instance->dispatchTable.createMetalSurface = metalCreateSurface;
|
||||
// }
|
||||
// }
|
||||
|
||||
// return GN_SUCCESS;
|
||||
// }
|
||||
@@ -0,0 +1,132 @@
|
||||
#include "Gryphn/gryphn.h"
|
||||
#include <iostream>
|
||||
#include "stdlib.h"
|
||||
#define GLFW_EXPOSE_NATIVE_X11
|
||||
#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",
|
||||
.enginnVersion = 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);
|
||||
}
|
||||
Reference in New Issue
Block a user