diff options
| author | skallweitNV <64953474+skallweitNV@users.noreply.github.com> | 2023-12-19 00:16:14 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-18 15:16:14 -0800 |
| commit | 93b8f68b2e9ddc450ce63f1b6e1806960312d803 (patch) | |
| tree | d5c9c38efe1e7c86637c4be6157595b44a5c4856 /tools/gfx | |
| parent | b6da04424aff71ddba9629c94401a9a897b152a0 (diff) | |
macos/vulkan support (#3418)
Diffstat (limited to 'tools/gfx')
| -rw-r--r-- | tools/gfx/apple/cocoa-util.h | 12 | ||||
| -rw-r--r-- | tools/gfx/apple/cocoa-util.mm | 15 | ||||
| -rw-r--r-- | tools/gfx/render.cpp | 2 | ||||
| -rw-r--r-- | tools/gfx/vulkan/vk-api.h | 4 | ||||
| -rw-r--r-- | tools/gfx/vulkan/vk-device.cpp | 13 | ||||
| -rw-r--r-- | tools/gfx/vulkan/vk-module.cpp | 3 | ||||
| -rw-r--r-- | tools/gfx/vulkan/vk-module.h | 2 | ||||
| -rw-r--r-- | tools/gfx/vulkan/vk-swap-chain.cpp | 16 | ||||
| -rw-r--r-- | tools/gfx/vulkan/vk-swap-chain.h | 17 |
9 files changed, 64 insertions, 20 deletions
diff --git a/tools/gfx/apple/cocoa-util.h b/tools/gfx/apple/cocoa-util.h new file mode 100644 index 000000000..80467d8a4 --- /dev/null +++ b/tools/gfx/apple/cocoa-util.h @@ -0,0 +1,12 @@ +#pragma once + +namespace gfx { + +// Utility functions for Cocoa +struct CocoaUtil { + + static void getNSViewRectSize(void* nsview, int* widthOut, int* heightOut); + +}; + +} diff --git a/tools/gfx/apple/cocoa-util.mm b/tools/gfx/apple/cocoa-util.mm new file mode 100644 index 000000000..45b1c3df0 --- /dev/null +++ b/tools/gfx/apple/cocoa-util.mm @@ -0,0 +1,15 @@ +#include "cocoa-util.h" + +#import <Cocoa/Cocoa.h> + +namespace gfx { + +void CocoaUtil::getNSViewRectSize(void* nsview, int* widthOut, int* heightOut) +{ + NSView* view = (NSView*)nsview; + NSRect rect = [view frame]; + *widthOut = rect.size.width; + *heightOut = rect.size.height; +} + +}
\ No newline at end of file diff --git a/tools/gfx/render.cpp b/tools/gfx/render.cpp index aad544eb4..6dd0c90dd 100644 --- a/tools/gfx/render.cpp +++ b/tools/gfx/render.cpp @@ -324,7 +324,7 @@ extern "C" return SLANG_FAIL; } break; -#elif SLANG_LINUX_FAMILY && !defined(__CYGWIN__) +#elif (SLANG_LINUX_FAMILY || SLANG_APPLE_FAMILY) && !defined(__CYGWIN__) case DeviceType::Default: case DeviceType::Vulkan: { diff --git a/tools/gfx/vulkan/vk-api.h b/tools/gfx/vulkan/vk-api.h index b7cbf13de..213921406 100644 --- a/tools/gfx/vulkan/vk-api.h +++ b/tools/gfx/vulkan/vk-api.h @@ -141,6 +141,10 @@ namespace gfx { # define VK_API_INSTANCE_PLATFORM_KHR_PROCS(x) \ x(vkCreateWin32SurfaceKHR) \ /* */ +#elif SLANG_APPLE_FAMILY +# define VK_API_INSTANCE_PLATFORM_KHR_PROCS(x) \ + x(vkCreateMacOSSurfaceMVK) \ + /* */ #elif SLANG_ENABLE_XLIB # define VK_API_INSTANCE_PLATFORM_KHR_PROCS(x) \ x(vkCreateXlibSurfaceKHR) \ diff --git a/tools/gfx/vulkan/vk-device.cpp b/tools/gfx/vulkan/vk-device.cpp index fe3680eda..66c2c811b 100644 --- a/tools/gfx/vulkan/vk-device.cpp +++ b/tools/gfx/vulkan/vk-device.cpp @@ -171,8 +171,11 @@ Result DeviceImpl::initVulkanInstanceAndDevice( applicationInfo.engineVersion = 1; applicationInfo.applicationVersion = 1; - Array<const char*, 6> instanceExtensions; + Array<const char*, 7> instanceExtensions; +#if SLANG_APPLE_FAMILY + instanceExtensions.add(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME); +#endif instanceExtensions.add(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME); instanceExtensions.add(VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME); @@ -185,6 +188,8 @@ Result DeviceImpl::initVulkanInstanceAndDevice( // instanceExtensions.add("VK_GOOGLE_surfaceless_query"); #if SLANG_WINDOWS_FAMILY instanceExtensions.add(VK_KHR_WIN32_SURFACE_EXTENSION_NAME); +#elif SLANG_APPLE_FAMILY + instanceExtensions.add(VK_MVK_MACOS_SURFACE_EXTENSION_NAME); #elif defined(SLANG_ENABLE_XLIB) instanceExtensions.add(VK_KHR_XLIB_SURFACE_EXTENSION_NAME); @@ -195,6 +200,9 @@ Result DeviceImpl::initVulkanInstanceAndDevice( instanceExtensions.add(VK_EXT_DEBUG_REPORT_EXTENSION_NAME); VkInstanceCreateInfo instanceCreateInfo = { VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO }; +#if SLANG_APPLE_FAMILY + instanceCreateInfo.flags = VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR; +#endif instanceCreateInfo.pApplicationInfo = &applicationInfo; instanceCreateInfo.enabledExtensionCount = (uint32_t)instanceExtensions.getCount(); instanceCreateInfo.ppEnabledExtensionNames = &instanceExtensions[0]; @@ -352,6 +360,9 @@ Result DeviceImpl::initVulkanInstanceAndDevice( List<const char*> deviceExtensions; deviceExtensions.add(VK_KHR_SWAPCHAIN_EXTENSION_NAME); +#if SLANG_APPLE_FAMILY + deviceExtensions.add("VK_KHR_portability_subset"); +#endif VkDeviceCreateInfo deviceCreateInfo = { VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO }; deviceCreateInfo.queueCreateInfoCount = 1; diff --git a/tools/gfx/vulkan/vk-module.cpp b/tools/gfx/vulkan/vk-module.cpp index cff75569e..0e4df8e7f 100644 --- a/tools/gfx/vulkan/vk-module.cpp +++ b/tools/gfx/vulkan/vk-module.cpp @@ -32,6 +32,9 @@ Slang::Result VulkanModule::init(bool useSoftwareImpl) dynamicLibraryName = useSoftwareImpl ? "vk_swiftshader.dll" : "vulkan-1.dll"; HMODULE module = ::LoadLibraryA(dynamicLibraryName); m_module = (void*)module; +#elif SLANG_APPLE_FAMILY + dynamicLibraryName = useSoftwareImpl ? "libvk_swiftshader.dylib" : "libvulkan.1.dylib"; + m_module = dlopen(dynamicLibraryName, RTLD_NOW | RTLD_GLOBAL); #else dynamicLibraryName = useSoftwareImpl ? "libvk_swiftshader.so" : "libvulkan.so.1"; if (useSoftwareImpl) diff --git a/tools/gfx/vulkan/vk-module.h b/tools/gfx/vulkan/vk-module.h index 97a0c8aba..9b8fe0ed3 100644 --- a/tools/gfx/vulkan/vk-module.h +++ b/tools/gfx/vulkan/vk-module.h @@ -7,6 +7,8 @@ #if SLANG_WINDOWS_FAMILY # define VK_USE_PLATFORM_WIN32_KHR 1 +#elif SLANG_APPLE_FAMILY +# define VK_USE_PLATFORM_MACOS_MVK 1 #else # define VK_USE_PLATFORM_XLIB_KHR 1 #endif diff --git a/tools/gfx/vulkan/vk-swap-chain.cpp b/tools/gfx/vulkan/vk-swap-chain.cpp index 384ca86ed..856fa2489 100644 --- a/tools/gfx/vulkan/vk-swap-chain.cpp +++ b/tools/gfx/vulkan/vk-swap-chain.cpp @@ -2,6 +2,7 @@ #include "vk-swap-chain.h" #include "vk-util.h" +#include "../apple/cocoa-util.h" namespace gfx { @@ -38,6 +39,8 @@ void SwapchainImpl::getWindowSize(int* widthOut, int* heightOut) const ::GetClientRect((HWND)m_windowHandle.handleValues[0], &rc); *widthOut = rc.right - rc.left; *heightOut = rc.bottom - rc.top; +#elif SLANG_APPLE_FAMILY + CocoaUtil::getNSViewRectSize((void*)m_windowHandle.handleValues[0], widthOut, heightOut); #elif defined(SLANG_ENABLE_XLIB) XWindowAttributes winAttr = {}; XGetWindowAttributes( @@ -221,6 +224,12 @@ Result SwapchainImpl::init(DeviceImpl* renderer, const ISwapchain::Desc& desc, W surfaceCreateInfo.hwnd = (HWND)window.handleValues[0]; SLANG_VK_RETURN_ON_FAIL( m_api->vkCreateWin32SurfaceKHR(m_api->m_instance, &surfaceCreateInfo, nullptr, &m_surface)); +#elif SLANG_APPLE_FAMILY + VkMacOSSurfaceCreateInfoMVK surfaceCreateInfo = {}; + surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK; + surfaceCreateInfo.pView = (void*)window.handleValues[0]; + SLANG_VK_RETURN_ON_FAIL( + m_api->vkCreateMacOSSurfaceMVK(m_api->m_instance, &surfaceCreateInfo, nullptr, &m_surface)); #elif SLANG_ENABLE_XLIB VkXlibSurfaceCreateInfoKHR surfaceCreateInfo = {}; surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR; @@ -347,7 +356,12 @@ int SwapchainImpl::acquireNextImage() VK_NULL_HANDLE, (uint32_t*)&m_currentImageIndex); - if (result != VK_SUCCESS) + if ( + result != VK_SUCCESS +#if SLANG_APPLE_FAMILY + && result != VK_SUBOPTIMAL_KHR +#endif + ) { m_currentImageIndex = -1; destroySwapchainAndImages(); diff --git a/tools/gfx/vulkan/vk-swap-chain.h b/tools/gfx/vulkan/vk-swap-chain.h index 6a39e2afa..2c0fe62ed 100644 --- a/tools/gfx/vulkan/vk-swap-chain.h +++ b/tools/gfx/vulkan/vk-swap-chain.h @@ -23,23 +23,6 @@ public: ISwapchain* getInterface(const Guid& guid); public: - struct PlatformDesc - {}; - -#if SLANG_WINDOWS_FAMILY - struct WinPlatformDesc : public PlatformDesc - { - HINSTANCE m_hinstance; - HWND m_hwnd; - }; -#else - struct XPlatformDesc : public PlatformDesc - { - Display* m_display; - Window m_window; - }; -#endif -public: VkSwapchainKHR m_swapChain; VkSurfaceKHR m_surface; VkSemaphore m_nextImageSemaphore; // Semaphore to signal after `acquireNextImage`. |
