Compare commits
10 Commits
e11a705dbd
...
204383372d
| Author | SHA1 | Date | |
|---|---|---|---|
| 204383372d | |||
| d9c1e8a345 | |||
| 3ad8f10f50 | |||
| 562b235f6a | |||
| d5c6aa10ae | |||
| dca85ff7e8 | |||
| 4087972149 | |||
| cf212dd9ad | |||
| b93fd9cfec | |||
| b46497d3e1 |
@@ -1 +1,2 @@
|
|||||||
build/
|
build/
|
||||||
|
glfw
|
||||||
|
|||||||
+13
-2
@@ -1,10 +1,21 @@
|
|||||||
cmake_minimum_required(VERSION 3.12)
|
cmake_minimum_required(VERSION 3.12)
|
||||||
project(ChemistryRenderingApp LANGUAGES C CXX OBJCXX) # OBJCXX is required for Metal .mm files
|
project(ChemistryRenderingApp LANGUAGES C CXX OBJCXX) # OBJCXX is required for Metal .mm files
|
||||||
add_executable(ChemistryRenderingApp main.cpp)
|
add_executable(ChemistryRenderingApp main.mm)
|
||||||
|
target_link_libraries(ChemistryRenderingApp PRIVATE
|
||||||
|
"-framework Metal"
|
||||||
|
"-framework Foundation"
|
||||||
|
"-framework QuartzCore"
|
||||||
|
"-framework Cocoa"
|
||||||
|
)
|
||||||
target_include_directories(ChemistryRenderingApp PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
|
target_include_directories(ChemistryRenderingApp PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
|
||||||
target_link_libraries(ChemistryRenderingApp PRIVATE GryphnLoader)
|
target_include_directories(ChemistryRenderingApp PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/glfw/include/")
|
||||||
|
target_link_libraries(ChemistryRenderingApp PRIVATE GryphnLoader glfw)
|
||||||
|
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
add_subdirectory(Gryphn)
|
add_subdirectory(Gryphn)
|
||||||
|
set(BUILD_SHARED_LIBS ON)
|
||||||
|
add_subdirectory(glfw)
|
||||||
|
set(BUILD_SHARED_LIBS OFF)
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
typedef enum gnFormat {
|
||||||
|
GN_FORMAT_UNDEFINED = 0,
|
||||||
|
GN_FORMAT_RGBA8_UNORM = 1,
|
||||||
|
GN_FORMAT_RGBA8_SNORM = 2,
|
||||||
|
GN_FORMAT_RGBA8_USCALED = 3,
|
||||||
|
GN_FORMAT_RGBA8_SSCALED = 4,
|
||||||
|
GN_FORMAT_RGBA8_UINT = 5,
|
||||||
|
GN_FORMAT_RGBA8_SINT = 6,
|
||||||
|
GN_FORMAT_RGBA8_SRGB = 7,
|
||||||
|
|
||||||
|
GN_FORMAT_BGRA8_UNORM = 8,
|
||||||
|
GN_FORMAT_BGRA8_SNORM = 9,
|
||||||
|
GN_FORMAT_BGRA8_USCALED = 10,
|
||||||
|
GN_FORMAT_BGRA8_SSCALED = 11,
|
||||||
|
GN_FORMAT_BGRA8_UINT = 12,
|
||||||
|
GN_FORMAT_BGRA8_SINT = 13,
|
||||||
|
GN_FORMAT_BGRA8_SRGB = 14,
|
||||||
|
} gnFormat;
|
||||||
|
|
||||||
|
typedef enum gnColorSpace {
|
||||||
|
GN_COLOR_SPACE_SRGB_NONLINEAR = 0,
|
||||||
|
} gnColorSpace;
|
||||||
|
|
||||||
|
typedef struct gnSurfaceFormat {
|
||||||
|
gnFormat format;
|
||||||
|
gnColorSpace colorSpace;
|
||||||
|
} gnSurfaceFormat;
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
typedef enum gnPresentMode {
|
||||||
|
GN_PRESENT_MODE_IMMEDIATE = 0,
|
||||||
|
GN_PRESENT_MODE_MAILBOX = 1,
|
||||||
|
GN_PRESENT_MODE_FIFO = 2,
|
||||||
|
GN_PRESENT_MODE_FIFO_RELAXED = 3,
|
||||||
|
} gnPresentMode;
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "stdint.h"
|
||||||
|
|
||||||
|
typedef struct gnExtent2D {
|
||||||
|
uint32_t width;
|
||||||
|
uint32_t height;
|
||||||
|
} gnExtent2D;
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#include "gryphn_device.h"
|
||||||
|
#include "instance/gryphn_instance.h"
|
||||||
|
#include "stdio.h"
|
||||||
|
#include "stdlib.h"
|
||||||
|
|
||||||
|
gnReturnCode gnCreateDevice(gnInstance instance, gnDeviceCreateInfo* createInfo, gnDevice* device) {
|
||||||
|
gnDevice newDevice = malloc(sizeof(gnDevice_t));
|
||||||
|
newDevice->instance = instance;
|
||||||
|
*device = newDevice;
|
||||||
|
return instance->dispatchTable.createDevice(instance, createInfo, newDevice);
|
||||||
|
}
|
||||||
|
|
||||||
|
gnReturnCode gnDestroyDevice(gnDevice* device) {
|
||||||
|
gnReturnCode returnCode = (*device)->dispatchTable.destroyDevice(*device);
|
||||||
|
*device = NULL;
|
||||||
|
return returnCode;
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "gryphn_physical_device.h"
|
||||||
|
#include "instance/dispatch/gryphn_device_dispatch_table.h"
|
||||||
|
|
||||||
|
typedef struct gnDeviceCreateInfo {
|
||||||
|
gnPhysicalDevice physicalDevice;
|
||||||
|
uint32_t enabledExtensionCount;
|
||||||
|
const char** enabledExtensions;
|
||||||
|
} gnDeviceCreateInfo;
|
||||||
|
|
||||||
|
typedef struct gnDevice_t {
|
||||||
|
gnDeviceDispatchTable dispatchTable;
|
||||||
|
gnInstance instance;
|
||||||
|
void* internalData;
|
||||||
|
} gnDevice_t;
|
||||||
|
|
||||||
|
gnReturnCode gnCreateDevice(gnInstance instance, gnDeviceCreateInfo* createInfo, gnDevice* device);
|
||||||
|
gnReturnCode gnDestroyDevice(gnDevice* device);
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
#include "gryphn_physical_device.h"
|
||||||
|
#include "instance/gryphn_instance.h"
|
||||||
|
#include "stdint.h"
|
||||||
|
|
||||||
|
gnReturnCode gnEnumeratePhysicalDevices(gnInstance instance, uint32_t* deviceCount, gnPhysicalDevice* devices) {
|
||||||
|
return instance->dispatchTable.enumeratePhysicalDevices(instance, deviceCount, devices);
|
||||||
|
}
|
||||||
|
|
||||||
|
gnReturnCode gnGetPhysicalDeviceProperties(gnPhysicalDevice device, gnPhysicalDeviceProperties* properties) {
|
||||||
|
return device->instance->dispatchTable.getPhysicalDeviceProperties(device, properties);
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "gryphn_handle.h"
|
||||||
|
#include "gryphn_return_code.h"
|
||||||
|
#include "stdint.h"
|
||||||
|
#include "instance/gryphn_application_info.h"
|
||||||
|
|
||||||
|
typedef enum gnPhysicalDeviceType {
|
||||||
|
GN_PHYSICAL_DEVICE_TYPE_CPU = 1,
|
||||||
|
GN_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU = 2,
|
||||||
|
GN_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU = 3,
|
||||||
|
GN_PHYSICAL_DEVICE_TYPE_EXTERNAL_GPU = 4,
|
||||||
|
GN_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU = 5,
|
||||||
|
GN_PHYSICAL_DEVICE_TYPE_OTHER = 6,
|
||||||
|
} gnPhysicalDeviceType;
|
||||||
|
|
||||||
|
// typedef struct gnPhysicalDeviceLimits {
|
||||||
|
|
||||||
|
// } gnPhysicalDeviceLimits;
|
||||||
|
|
||||||
|
typedef struct gnPhysicalDeviceProperties {
|
||||||
|
gnVersion apiVersion;
|
||||||
|
uint32_t vendorID;
|
||||||
|
uint32_t deviceID;
|
||||||
|
gnPhysicalDeviceType deviceType;
|
||||||
|
char deviceName[256];
|
||||||
|
// gnPhysicalDeviceLimits limits;
|
||||||
|
} gnPhysicalDeviceProperties;
|
||||||
|
|
||||||
|
typedef struct gnPhysicalDevice_t {
|
||||||
|
gnInstance instance;
|
||||||
|
void* internalData;
|
||||||
|
} gnPhysicalDevice_t;
|
||||||
|
|
||||||
|
gnReturnCode gnEnumeratePhysicalDevices(gnInstance instance, uint32_t* deviceCount, gnPhysicalDevice* devices);
|
||||||
|
gnReturnCode gnGetPhysicalDeviceProperties(gnPhysicalDevice device, gnPhysicalDeviceProperties* properties);
|
||||||
|
gnReturnCode gnDoesDeviceSupportSurface(gnPhysicalDevice device, gnSurface surface);
|
||||||
@@ -1,4 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define GN_HANDLE(type) typedef struct type##_t* type
|
#define GN_HANDLE(type) typedef struct type##_t* type
|
||||||
|
#define GN_IMPLEMENTATION_HANDLE(type) typedef void* type
|
||||||
|
|
||||||
GN_HANDLE(gnInstance);
|
GN_HANDLE(gnInstance);
|
||||||
|
GN_HANDLE(gnPhysicalDevice);
|
||||||
|
GN_HANDLE(gnDevice);
|
||||||
|
GN_IMPLEMENTATION_HANDLE(gnSurface);
|
||||||
|
GN_IMPLEMENTATION_HANDLE(gnSwapchain);
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "gryphn_handle.h"
|
||||||
|
|
||||||
|
typedef gnReturnCode (*PFN_gnDestroyDevice)(gnDevice);
|
||||||
|
|
||||||
|
typedef struct gnDeviceDispatchTable {
|
||||||
|
PFN_gnDestroyDevice destroyDevice;
|
||||||
|
} gnDeviceDispatchTable;
|
||||||
@@ -1,8 +1,30 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "gryphn_handle.h"
|
#include "gryphn_handle.h"
|
||||||
|
#include "core/gryphn_format.h"
|
||||||
|
#include "core/gryphn_present_mode.h"
|
||||||
|
typedef struct gnMetalSurfaceCreateInfo gnMetalSurfaceCreateInfo;
|
||||||
|
typedef struct gnSurfaceCapabilities gnSurfaceCapabilities;
|
||||||
|
typedef struct gnPhysicalDeviceProperties gnPhysicalDeviceProperties;
|
||||||
|
typedef struct gnDeviceCreateInfo gnDeviceCreateInfo;
|
||||||
|
|
||||||
|
typedef gnReturnCode (*PFN_gnCreateMetalSurface)(gnInstance, gnMetalSurfaceCreateInfo*, gnSurface*);
|
||||||
|
typedef gnReturnCode (*PFN_enumeratePhysicalDevices)(gnInstance, uint32_t*, gnPhysicalDevice*);
|
||||||
|
typedef gnReturnCode (*PFN_gnGetPhysicalDeviceProperties)(gnPhysicalDevice, gnPhysicalDeviceProperties*);
|
||||||
|
typedef gnReturnCode (*PFN_gnCreateDevice)(gnInstance, gnDeviceCreateInfo*, gnDevice);
|
||||||
|
typedef gnReturnCode (*PFN_gnGetSurfaceFormats)(gnPhysicalDevice, gnSurface, uint32_t*, gnSurfaceFormat*);
|
||||||
|
typedef gnReturnCode (*PFN_gnGetSurfacePresentModes)(gnPhysicalDevice, gnSurface, uint32_t*, gnPresentMode*);
|
||||||
|
typedef gnReturnCode (*PFN_gnGetSurfaceCapabilities)(gnPhysicalDevice, gnSurface, gnSurfaceCapabilities*);
|
||||||
|
typedef gnReturnCode (*PFN_gnDestroySurface)(gnInstance, gnSurface*);
|
||||||
typedef gnReturnCode (*PFN_gnDestroyInstance)(gnInstance);
|
typedef gnReturnCode (*PFN_gnDestroyInstance)(gnInstance);
|
||||||
|
|
||||||
typedef struct gnInstanceDispatchTable {
|
typedef struct gnInstanceDispatchTable {
|
||||||
|
PFN_gnCreateMetalSurface createMetalSurface;
|
||||||
|
PFN_gnGetSurfaceCapabilities getSurfaceCapabilities;
|
||||||
|
PFN_gnGetSurfaceFormats getSurfaceFormats;
|
||||||
|
PFN_gnGetSurfacePresentModes getSurfacePresentModes;
|
||||||
|
PFN_gnDestroySurface destroySurface;
|
||||||
|
PFN_enumeratePhysicalDevices enumeratePhysicalDevices;
|
||||||
|
PFN_gnGetPhysicalDeviceProperties getPhysicalDeviceProperties;
|
||||||
|
PFN_gnCreateDevice createDevice;
|
||||||
PFN_gnDestroyInstance destroyInstance;
|
PFN_gnDestroyInstance destroyInstance;
|
||||||
} gnInstanceDispatchTable;
|
} gnInstanceDispatchTable;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
|
#include "gryphn_return_code.h"
|
||||||
#include "gryphn_instance.h"
|
#include "gryphn_instance.h"
|
||||||
#include "stdlib.h"
|
#include "stdlib.h"
|
||||||
#include "stdio.h"
|
|
||||||
|
|
||||||
typedef gnReturnCode (*PFN_initBackend)(gnInstance, gnInstanceCreateInfo*);
|
typedef gnReturnCode (*PFN_initBackend)(gnInstance, gnInstanceCreateInfo*);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#include "gryphn_metal_surface.h"
|
||||||
|
#include "instance/gryphn_instance.h"
|
||||||
|
|
||||||
|
gnReturnCode gnCreateMetalSurface(gnInstance instance, gnMetalSurfaceCreateInfo* createInfo, gnSurface* surface) {
|
||||||
|
return instance->dispatchTable.createMetalSurface(instance, createInfo, surface);
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "gryphn_handle.h"
|
||||||
|
#include "gryphn_return_code.h"
|
||||||
|
|
||||||
|
#ifndef GN_CA_METAL_LAYER
|
||||||
|
typedef struct CAMetalLayer CAMetalLayer;
|
||||||
|
#endif
|
||||||
|
typedef struct gnMetalSurfaceCreateInfo {
|
||||||
|
CAMetalLayer* metalLayer;
|
||||||
|
} gnMetalSurfaceCreateInfo;
|
||||||
|
gnReturnCode gnCreateMetalSurface(gnInstance instance, gnMetalSurfaceCreateInfo* createInfo, gnSurface* surface);
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#include "gryphn_surface.h"
|
||||||
|
#include "instance/gryphn_instance.h"
|
||||||
|
#include "device/gryphn_physical_device.h"
|
||||||
|
|
||||||
|
gnReturnCode gnGetSurfaceCapabilities(gnPhysicalDevice device, gnSurface surface, gnSurfaceCapabilities* capabilities) {
|
||||||
|
return device->instance->dispatchTable.getSurfaceCapabilities(device, surface, capabilities);
|
||||||
|
}
|
||||||
|
|
||||||
|
gnReturnCode gnGetSurfaceFormats(gnPhysicalDevice device, gnSurface surface, uint32_t* formatCount, gnSurfaceFormat* formats) {
|
||||||
|
return device->instance->dispatchTable.getSurfaceFormats(device, surface, formatCount, formats);
|
||||||
|
}
|
||||||
|
gnReturnCode gnGetSurfacePresentModes(gnPhysicalDevice device, gnSurface surface, uint32_t* presentModeCount, gnPresentMode* presentModes) {
|
||||||
|
return device->instance->dispatchTable.getSurfacePresentModes(device, surface, presentModeCount, presentModes);
|
||||||
|
}
|
||||||
|
|
||||||
|
gnReturnCode gnDestroySurface(gnInstance instance, gnSurface* surface) {
|
||||||
|
return instance->dispatchTable.destroySurface(instance, surface);
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "gryphn_metal_surface.h"
|
||||||
|
#include "core/gryphn_vec2d.h"
|
||||||
|
#include "core/gryphn_format.h"
|
||||||
|
#include "core/gryphn_present_mode.h"
|
||||||
|
|
||||||
|
typedef struct gnSurfaceCapabilities {
|
||||||
|
uint32_t minImageCount;
|
||||||
|
uint32_t maxImageCount;
|
||||||
|
gnExtent2D currentExtent;
|
||||||
|
gnExtent2D minImageExtent;
|
||||||
|
gnExtent2D maxImageExtent;
|
||||||
|
uint32_t maxImageArrayLayers;
|
||||||
|
} gnSurfaceCapabilities;
|
||||||
|
|
||||||
|
gnReturnCode gnGetSurfaceCapabilities(gnPhysicalDevice device, gnSurface surface, gnSurfaceCapabilities* capabilities);
|
||||||
|
gnReturnCode gnGetSurfaceFormats(gnPhysicalDevice device, gnSurface surface, uint32_t* formatCount, gnSurfaceFormat* formats);
|
||||||
|
gnReturnCode gnGetSurfacePresentModes(gnPhysicalDevice device, gnSurface surface, uint32_t* presentModeCount, gnPresentMode* presentModes);
|
||||||
|
gnReturnCode gnDestroySurface(gnInstance instance, gnSurface* surface);
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
file(GLOB_RECURSE METAL_SOURCES CONFIGURE_DEPENDS "src/*.m")
|
file(GLOB_RECURSE METAL_SOURCES CONFIGURE_DEPENDS "src/*.m")
|
||||||
add_library(gryphn_metal SHARED ${METAL_SOURCES})
|
add_library(gryphn_metal SHARED ${METAL_SOURCES})
|
||||||
|
project(gryphn_metal LANGUAGES C CXX OBJC OBJCXX)
|
||||||
set_target_properties(gryphn_metal PROPERTIES PREFIX "")
|
set_target_properties(gryphn_metal PROPERTIES PREFIX "")
|
||||||
target_link_libraries(gryphn_metal PRIVATE GryphnLoader)
|
target_link_libraries(gryphn_metal PRIVATE GryphnLoader)
|
||||||
if(APPLE)
|
if(APPLE)
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
#import <Metal/Metal.h>
|
||||||
|
#include "../metal_functions.h"
|
||||||
|
#include "core/gryphn_format.h"
|
||||||
|
|
||||||
|
gnReturnCode metalCreateSurface(gnInstance instance, gnMetalSurfaceCreateInfo* createInfo, gnSurface* surface) {
|
||||||
|
*surface = createInfo->metalLayer;
|
||||||
|
return GN_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
gnReturnCode metalGetSurfaceCapabilities(gnPhysicalDevice device, gnSurface surface, gnSurfaceCapabilities* capabilities) {
|
||||||
|
CAMetalLayer* layer = surface;
|
||||||
|
capabilities->minImageCount = 2;
|
||||||
|
capabilities->maxImageCount = 3;
|
||||||
|
|
||||||
|
capabilities->currentExtent.width = (uint32_t)layer.drawableSize.width;
|
||||||
|
capabilities->currentExtent.height = (uint32_t)layer.drawableSize.height;
|
||||||
|
|
||||||
|
id<MTLDevice> mtlDevice = (id<MTLDevice>)device->internalData;
|
||||||
|
uint32_t maxTextureSize = 8192;
|
||||||
|
if ([mtlDevice respondsToSelector:@selector(supportsFamily:)]) {
|
||||||
|
if ([mtlDevice supportsFamily:MTLGPUFamilyApple1] ||
|
||||||
|
[mtlDevice supportsFamily:MTLGPUFamilyMac2]) {
|
||||||
|
maxTextureSize = 16384;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
capabilities->minImageExtent = (gnExtent2D){ 1, 1 };
|
||||||
|
capabilities->maxImageExtent = (gnExtent2D){ maxTextureSize, maxTextureSize };
|
||||||
|
|
||||||
|
capabilities->maxImageArrayLayers = 1;
|
||||||
|
|
||||||
|
return GN_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
gnReturnCode metalGetSurfaceFormats(gnPhysicalDevice device, gnSurface surface, uint32_t* formatCount, gnSurfaceFormat* formats) {
|
||||||
|
gnSurfaceFormat supported[] = {
|
||||||
|
{ GN_FORMAT_BGRA8_UNORM, GN_COLOR_SPACE_SRGB_NONLINEAR },
|
||||||
|
{ GN_FORMAT_BGRA8_SRGB, GN_COLOR_SPACE_SRGB_NONLINEAR },
|
||||||
|
};
|
||||||
|
if (formats == NULL) {
|
||||||
|
*formatCount = 2;
|
||||||
|
return GN_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < 2; i++) {
|
||||||
|
formats[i] = supported[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return GN_SUCCESS;
|
||||||
|
}
|
||||||
|
gnReturnCode metalGetSurfacePresentModes(gnPhysicalDevice device, gnSurface surface, uint32_t* presentModeCount, gnPresentMode* presentModes) {
|
||||||
|
return GN_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
gnReturnCode metalDestroySurface(gnInstance instance, gnSurface* surface) {
|
||||||
|
*surface = NULL;
|
||||||
|
return GN_SUCCESS;
|
||||||
|
}
|
||||||
@@ -2,23 +2,38 @@
|
|||||||
#include "stdio.h"
|
#include "stdio.h"
|
||||||
#include "instance/gryphn_instance.h"
|
#include "instance/gryphn_instance.h"
|
||||||
#include "gryphn_handle.h"
|
#include "gryphn_handle.h"
|
||||||
|
#include "metal_functions.h"
|
||||||
|
|
||||||
extern "C" uint32_t gnInternalIsApiSupported(gnVersion version) {
|
uint32_t gnInternalIsApiSupported(gnVersion version) {
|
||||||
if (version != gnCreateVersion(1, 0, 0)) return 0;
|
if (version != gnCreateVersion(1, 0, 0)) return 0;
|
||||||
id<MTLDevice> testDevice = MTLCreateSystemDefaultDevice();
|
id<MTLDevice> testDevice = MTLCreateSystemDefaultDevice();
|
||||||
if (testDevice == nil)
|
if (testDevice == nil)
|
||||||
return 0;
|
return 0;
|
||||||
[testDevice release];
|
[testDevice release];
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
gnReturnCode destroyBackend(gnInstance instance) {
|
gnReturnCode destroyBackend(gnInstance instance) {
|
||||||
printf("Calling destroy instance");
|
|
||||||
return GN_SUCCESS;
|
return GN_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" gnReturnCode initBackend(gnInstance instance, gnInstanceCreateInfo* info) {
|
gnReturnCode initBackend(gnInstance instance, gnInstanceCreateInfo* info) {
|
||||||
printf("calling init instance\n");
|
|
||||||
instance->dispatchTable.destroyInstance = destroyBackend;
|
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;
|
return GN_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,73 @@
|
|||||||
|
#import "Metal/Metal.h"
|
||||||
|
#include "metal_functions.h"
|
||||||
|
#include "stdio.h"
|
||||||
|
#include "stdint.h"
|
||||||
|
#include "device/gryphn_physical_device.h"
|
||||||
|
#include "device/gryphn_device.h"
|
||||||
|
|
||||||
|
gnReturnCode metalEnumeratePhysicalDevices(gnInstance instance, uint32_t* deviceCount, gnPhysicalDevice* devices) {
|
||||||
|
NSArray<id<MTLDevice>>* mtlDevices = MTLCopyAllDevices();
|
||||||
|
uint32_t systemCount = (uint32_t)[mtlDevices count];
|
||||||
|
*deviceCount = systemCount;
|
||||||
|
|
||||||
|
if (devices == NULL)
|
||||||
|
return GN_SUCCESS;
|
||||||
|
for (uint32_t i = 0; i < *deviceCount; i++) {
|
||||||
|
devices[i] = (gnPhysicalDevice)malloc(sizeof(gnPhysicalDevice_t));
|
||||||
|
devices[i]->instance = instance;
|
||||||
|
devices[i]->internalData = mtlDevices[i];
|
||||||
|
}
|
||||||
|
return GN_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
gnReturnCode metalGetPhysicalDeviceProperties(gnPhysicalDevice device, gnPhysicalDeviceProperties* properties) {
|
||||||
|
id<MTLDevice> mtlDevice = (id<MTLDevice>)device->internalData;
|
||||||
|
|
||||||
|
properties->apiVersion = gnCreateVersion(1, 0, 0);
|
||||||
|
|
||||||
|
if ([[mtlDevice name] containsString:@"Apple"]) {
|
||||||
|
properties->vendorID = 0x106B;
|
||||||
|
} else if ([[mtlDevice name] containsString:@"Intel"]) {
|
||||||
|
properties->vendorID = 0x8086;
|
||||||
|
} else if ([[mtlDevice name] containsString:@"AMD"] || [[mtlDevice name] containsString:@"Radeon"]) {
|
||||||
|
properties->vendorID = 0x1002;
|
||||||
|
} else {
|
||||||
|
properties->vendorID = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (@available(macOS 10.13, *)) {
|
||||||
|
properties->deviceID = (uint32_t)[mtlDevice registryID];
|
||||||
|
} else {
|
||||||
|
properties->deviceID = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ([mtlDevice isRemovable])
|
||||||
|
properties->deviceType = GN_PHYSICAL_DEVICE_TYPE_EXTERNAL_GPU;
|
||||||
|
else if ([mtlDevice isLowPower])
|
||||||
|
properties->deviceType = GN_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU;
|
||||||
|
else if (![mtlDevice isLowPower] && ![mtlDevice hasUnifiedMemory])
|
||||||
|
properties->deviceType = GN_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU;
|
||||||
|
else
|
||||||
|
properties->deviceType = GN_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU;
|
||||||
|
|
||||||
|
const char* rawName = [[mtlDevice name] UTF8String];
|
||||||
|
strncpy(properties->deviceName, rawName, 255);
|
||||||
|
properties->deviceName[255] = '\0';
|
||||||
|
return GN_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
gnReturnCode metalCreateDevice(gnInstance instance, gnDeviceCreateInfo* createInfo, gnDevice device) {
|
||||||
|
device->dispatchTable.destroyDevice = metalDestroyDevice;
|
||||||
|
device->internalData = createInfo->physicalDevice->internalData;
|
||||||
|
for (int i = 0; i < createInfo->enabledExtensionCount; i++) {
|
||||||
|
if (strcmp(createInfo->enabledExtensions[i], "GN_EXT_swapchain") == 0) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return GN_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
gnReturnCode metalDestroyDevice(gnDevice device) {
|
||||||
|
device->internalData = NULL;
|
||||||
|
return GN_SUCCESS;
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
#pragma once
|
||||||
|
#define GN_CA_METAL_LAYER
|
||||||
|
#import <QuartzCore/CAMetalLayer.h>
|
||||||
|
#include "gryphn_handle.h"
|
||||||
|
#include "gryphn_return_code.h"
|
||||||
|
#include "stdint.h"
|
||||||
|
#include "device/gryphn_physical_device.h"
|
||||||
|
#include "device/gryphn_device.h"
|
||||||
|
#include "surface/gryphn_surface.h"
|
||||||
|
|
||||||
|
gnReturnCode metalEnumeratePhysicalDevices(gnInstance instance, uint32_t* deviceCount, gnPhysicalDevice* devices);
|
||||||
|
gnReturnCode metalGetPhysicalDeviceProperties(gnPhysicalDevice device, gnPhysicalDeviceProperties* properties);
|
||||||
|
gnReturnCode metalCreateDevice(gnInstance instance, gnDeviceCreateInfo* createInfo, gnDevice device);
|
||||||
|
gnReturnCode metalDestroyDevice(gnDevice device);
|
||||||
|
gnReturnCode metalCreateSurface(gnInstance instance, gnMetalSurfaceCreateInfo* createInfo, gnSurface* surface);
|
||||||
|
gnReturnCode metalGetSurfaceCapabilities(gnPhysicalDevice device, gnSurface surface, gnSurfaceCapabilities* capabilities);
|
||||||
|
gnReturnCode metalGetSurfaceFormats(gnPhysicalDevice device, gnSurface surface, uint32_t* formatCount, gnSurfaceFormat* formats);
|
||||||
|
gnReturnCode metalGetSurfacePresentModes(gnPhysicalDevice device, gnSurface surface, uint32_t* presentModeCount, gnPresentMode* presentModes);
|
||||||
|
gnReturnCode metalDestroySurface(gnInstance instance, gnSurface* surface);
|
||||||
@@ -4,6 +4,9 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
#include "GryphnLoader/src/instance/gryphn_instance.h"
|
#include "GryphnLoader/src/instance/gryphn_instance.h"
|
||||||
|
#include "GryphnLoader/src/device/gryphn_physical_device.h"
|
||||||
|
#include "GryphnLoader/src/device/gryphn_device.h"
|
||||||
|
#include "GryphnLoader/src/surface/gryphn_surface.h"
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,51 +0,0 @@
|
|||||||
#include "Gryphn/gryphn.h"
|
|
||||||
#include <iostream>
|
|
||||||
#include "stdlib.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"; \
|
|
||||||
}
|
|
||||||
|
|
||||||
gnVersion version = gnCreateVersion(1, 0, 0);
|
|
||||||
gnInstance instance;
|
|
||||||
|
|
||||||
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));
|
|
||||||
|
|
||||||
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 = 0,
|
|
||||||
.enabledExtensions = nullptr
|
|
||||||
};
|
|
||||||
CHECK(gnCreateInstance(&instance, &createInfo));
|
|
||||||
free(backends);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
try {
|
|
||||||
createInstance();
|
|
||||||
} catch (const std::exception& e) {
|
|
||||||
std::cerr << "Caught Exception: " << e.what() << "\n";
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
gnDestroyInstance(&instance);
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,136 @@
|
|||||||
|
#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",
|
||||||
|
.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