[Bf-blender-cvs] [3efdb3f232c] tmp-vulkan: GHOST: Vulkan: Add MoltenVK Backend on osx

Clément Foucault noreply at git.blender.org
Sun Sep 13 00:44:35 CEST 2020


Commit: 3efdb3f232ca3d0ca9a0b1c00f77800a3c1f979f
Author: Clément Foucault
Date:   Sat Sep 12 23:44:28 2020 +0200
Branches: tmp-vulkan
https://developer.blender.org/rB3efdb3f232ca3d0ca9a0b1c00f77800a3c1f979f

GHOST: Vulkan: Add MoltenVK Backend on osx

This is still highly experimental.

===================================================================

M	CMakeLists.txt
M	intern/ghost/CMakeLists.txt
M	intern/ghost/intern/GHOST_ContextVK.cpp
M	intern/ghost/intern/GHOST_ContextVK.h
M	intern/ghost/intern/GHOST_WindowCocoa.mm

===================================================================

diff --git a/CMakeLists.txt b/CMakeLists.txt
index d855c372605..e2d07fda9de 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1005,7 +1005,7 @@ endif()
 # Configure Vulkan.
 
 if(WITH_VULKAN)
-  find_package(Vulkan)
+  find_package(Vulkan REQUIRED)
 
   list(APPEND BLENDER_GL_LIBRARIES ${Vulkan_LIBRARY})
 
diff --git a/intern/ghost/CMakeLists.txt b/intern/ghost/CMakeLists.txt
index 2573460530e..b08bb51de2b 100644
--- a/intern/ghost/CMakeLists.txt
+++ b/intern/ghost/CMakeLists.txt
@@ -169,6 +169,18 @@ elseif(APPLE AND NOT WITH_GHOST_X11)
     )
   endif()
 
+  if(WITH_VULKAN)
+    list(APPEND SRC
+      intern/GHOST_ContextVK.cpp
+
+      intern/GHOST_ContextVK.h
+    )
+
+    list(APPEND INC_SYS
+      ${Vulkan_INCLUDE_DIRS}
+    )
+  endif()
+
   if(WITH_INPUT_NDOF)
     list(APPEND SRC
       intern/GHOST_NDOFManagerCocoa.mm
diff --git a/intern/ghost/intern/GHOST_ContextVK.cpp b/intern/ghost/intern/GHOST_ContextVK.cpp
index 5dd89a4ad2d..556a8e39370 100644
--- a/intern/ghost/intern/GHOST_ContextVK.cpp
+++ b/intern/ghost/intern/GHOST_ContextVK.cpp
@@ -24,13 +24,12 @@
  */
 
 #include "GHOST_ContextVK.h"
-#include "GHOST_SystemX11.h"
-
-#include <vulkan/vulkan.h>
 
 #ifdef _WIN32
 #  include <vulkan/vulkan_win32.h>
-#else
+#elif defined(__APPLE__)
+#  include <MoltenVK/vk_mvk_moltenvk.h>
+#else /* X11 */
 #  include <vulkan/vulkan_xlib.h>
 #endif
 
@@ -120,7 +119,9 @@ const int MAX_FRAMES_IN_FLIGHT = 2;
 GHOST_ContextVK::GHOST_ContextVK(bool stereoVisual,
 #ifdef _WIN32
                                  HWND hwnd,
-#else
+#elif defined(__APPLE__)
+                                 CAMetalLayer *metal_layer,
+#else /* X11 */
                                  Window window,
                                  Display *display,
 #endif
@@ -131,7 +132,9 @@ GHOST_ContextVK::GHOST_ContextVK(bool stereoVisual,
 #ifdef _WIN32
       m_hinstance(hinstance),
       m_hwnd(hwnd),
-#else
+#elif defined(__APPLE__)
+      m_metal_layer(metal_layer),
+#else /* X11 */
       m_display(display),
       m_window(window),
 #endif
@@ -607,7 +610,7 @@ GHOST_TSuccess GHOST_ContextVK::recordCommandBuffers(void)
           .offset = {0, 0},
           .extent = m_render_extent,
       };
-      VkClearValue clearColor = {0.0f, 0.5f, 0.3f, 1.0f};
+      VkClearValue clearColor = {{{0.0f, 0.5f, 0.3f, 1.0f}}};
       VkRenderPassBeginInfo render_pass_info = {
           .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
           .renderPass = m_render_pass,
@@ -805,7 +808,9 @@ GHOST_TSuccess GHOST_ContextVK::initializeDrawingContext()
 {
 #ifdef _WIN32
   const bool use_window_surface = (m_hwnd != NULL);
-#else
+#elif defined(__APPLE__)
+  const bool use_window_surface = true;
+#else /* X11 */
   const bool use_window_surface = (m_display != NULL);
 #endif
 
@@ -823,7 +828,9 @@ GHOST_TSuccess GHOST_ContextVK::initializeDrawingContext()
   if (use_window_surface) {
 #ifdef _WIN32
     const char *native_surface_extension_name = VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
-#else
+#elif defined(__APPLE__)
+    const char *native_surface_extension_name = VK_MVK_MACOS_SURFACE_EXTENSION_NAME;
+#else /* X11 */
     const char *native_surface_extension_name = VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
 #endif
     requireExtension(extensions_available, extensions_enabled, "VK_KHR_surface");
@@ -860,6 +867,13 @@ GHOST_TSuccess GHOST_ContextVK::initializeDrawingContext()
         hwnd = m_hwnd,
     };
     VK_CHECK(vkCreateWin32SurfaceKHR(m_instance, &surface_create_info, NULL, &m_surface));
+#elif defined(__APPLE__)
+    VkMacOSSurfaceCreateInfoMVK info = {};
+    info.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
+    info.pNext = NULL;
+    info.flags = 0;
+    info.pView = m_metal_layer;
+    VK_CHECK(vkCreateMacOSSurfaceMVK(m_instance, &info, nullptr, &m_surface));
 #else
     VkXlibSurfaceCreateInfoKHR surface_create_info = {
         .sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR,
diff --git a/intern/ghost/intern/GHOST_ContextVK.h b/intern/ghost/intern/GHOST_ContextVK.h
index 3cae1e560fd..506525133e2 100644
--- a/intern/ghost/intern/GHOST_ContextVK.h
+++ b/intern/ghost/intern/GHOST_ContextVK.h
@@ -28,12 +28,19 @@
 
 #ifdef _WIN32
 #  include "GHOST_SystemWin32.h"
+#elif defined(__APPLE__)
+#  include "GHOST_SystemCocoa.h"
 #else
 #  include "GHOST_SystemX11.h"
 #endif
 
 #include <vector>
-#include <vulkan/vulkan.h>
+
+#ifdef __APPLE__
+#  include <MoltenVK/vk_mvk_moltenvk.h>
+#else
+#  include <vulkan/vulkan.h>
+#endif
 
 #ifndef GHOST_OPENGL_VK_CONTEXT_FLAGS
 /* leave as convenience define for the future */
@@ -55,7 +62,9 @@ class GHOST_ContextVK : public GHOST_Context {
   GHOST_ContextVK(bool stereoVisual,
 #ifdef _WIN32
                   HWND hwnd,
-#else
+#elif defined(__APPLE__)
+                  CAMetalLayer *metal_layer,
+#else /* X11 */
                   Window window,
                   Display *display,
 #endif
@@ -122,7 +131,9 @@ class GHOST_ContextVK : public GHOST_Context {
  private:
 #ifdef _WIN32
   HWND hwnd;
-#else
+#elif defined(__APPLE__)
+  CAMetalLayer *m_metal_layer;
+#else /* X11 */
   Display *m_display;
   Window m_window;
 #endif
diff --git a/intern/ghost/intern/GHOST_WindowCocoa.mm b/intern/ghost/intern/GHOST_WindowCocoa.mm
index f8e2f96d111..74d4bb9425a 100644
--- a/intern/ghost/intern/GHOST_WindowCocoa.mm
+++ b/intern/ghost/intern/GHOST_WindowCocoa.mm
@@ -28,6 +28,10 @@
 #  include "GHOST_ContextCGL.h"
 #endif
 
+#if defined(WITH_VULKAN)
+#  include "GHOST_ContextVK.h"
+#endif
+
 #include <Cocoa/Cocoa.h>
 #include <Metal/Metal.h>
 #include <QuartzCore/QuartzCore.h>
@@ -833,8 +837,21 @@ GHOST_TSuccess GHOST_WindowCocoa::setOrder(GHOST_TWindowOrder order)
 
 GHOST_Context *GHOST_WindowCocoa::newDrawingContext(GHOST_TDrawingContextType type)
 {
-  if (type == GHOST_kDrawingContextTypeOpenGL) {
+#ifdef WITH_VULKAN
+  if (type == GHOST_kDrawingContextTypeVulkan) {
+    GHOST_Context *context = new GHOST_ContextVK(m_wantStereoVisual, m_metalLayer, 1, 0, false);
+
+    if (context->initializeDrawingContext()) {
+      return context;
+    }
 
+    delete context;
+    /* Fallback to opengl. */
+    type = GHOST_kDrawingContextTypeOpenGL;
+  }
+#endif
+
+  if (type == GHOST_kDrawingContextTypeOpenGL) {
     GHOST_Context *context = new GHOST_ContextCGL(
         m_wantStereoVisual, m_metalView, m_metalLayer, m_openGLView);



More information about the Bf-blender-cvs mailing list