[Bf-blender-cvs] [6dac345a64b] master: GHOST: Vulkan Backend.

Jeroen Bakker noreply at git.blender.org
Tue Nov 22 11:30:17 CET 2022


Commit: 6dac345a64b1ab6d58ce838e9acd7f09af43a77f
Author: Jeroen Bakker
Date:   Tue Nov 22 11:28:37 2022 +0100
Branches: master
https://developer.blender.org/rB6dac345a64b1ab6d58ce838e9acd7f09af43a77f

GHOST: Vulkan Backend.

This adds a vulkan backend to GHOST. The code was extracted from the
tmp-vulkan branch. The main difference with the original code is that
GHOST isn't responsible for fallback. For Metal backend there is already
an idea that the GPU module is responsible for the fallback, not the system.

For Blender we target Vulkan 1.2 at the time of this patch.
MoltenVK (needed to convert Vulkan calls to Metal) has been added as
a separate package.

This patch isn't useful for end-users, currently when starting blender with
`--gpu-backend vulkan` it would crash as the `VBBackend` doesn't initialize
the expected global structs in the GPU module.

Validated to be working on Windows and Apple. Linux still needs to be tested.

Reviewed By: fclem

Differential Revision: https://developer.blender.org/D13155

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

M	CMakeLists.txt
A	build_files/cmake/Modules/FindMoltenVK.cmake
M	build_files/cmake/platform/platform_apple.cmake
M	build_files/cmake/platform/platform_unix.cmake
M	build_files/cmake/platform/platform_win32.cmake
M	intern/ghost/CMakeLists.txt
M	intern/ghost/GHOST_C-api.h
M	intern/ghost/GHOST_IContext.h
M	intern/ghost/GHOST_IWindow.h
M	intern/ghost/GHOST_Types.h
M	intern/ghost/intern/GHOST_C-api.cpp
M	intern/ghost/intern/GHOST_Context.h
A	intern/ghost/intern/GHOST_ContextVK.cpp
A	intern/ghost/intern/GHOST_ContextVK.h
M	intern/ghost/intern/GHOST_SystemCocoa.mm
M	intern/ghost/intern/GHOST_SystemWayland.cpp
M	intern/ghost/intern/GHOST_SystemWin32.cpp
M	intern/ghost/intern/GHOST_SystemX11.cpp
M	intern/ghost/intern/GHOST_Window.cpp
M	intern/ghost/intern/GHOST_Window.h
M	intern/ghost/intern/GHOST_WindowCocoa.mm
M	intern/ghost/intern/GHOST_WindowWayland.cpp
M	intern/ghost/intern/GHOST_WindowWin32.cpp
M	intern/ghost/intern/GHOST_WindowX11.cpp
M	source/blender/gpu/CMakeLists.txt
M	source/blender/gpu/vulkan/vk_backend.cc
M	source/blender/gpu/vulkan/vk_backend.hh
M	source/blender/windowmanager/CMakeLists.txt
M	source/blender/windowmanager/intern/wm_window.c
M	source/creator/CMakeLists.txt

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

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7a621b859e7..855f6aba44f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1239,12 +1239,14 @@ if(WITH_OPENGL)
   add_definitions(-DWITH_OPENGL)
 endif()
 
-
-# -----------------------------------------------------------------------------
+#-----------------------------------------------------------------------------
 # Configure Vulkan.
 
 if(WITH_VULKAN_BACKEND)
-  add_definitions(-DWITH_VULKAN_BACKEND)
+  list(APPEND BLENDER_GL_LIBRARIES ${VULKAN_LIBRARIES})
+  if(APPLE)
+    list(APPEND BLENDER_GL_LIBRARIES ${MOLTENVK_LIBRARIES})
+  endif()
 endif()
 
 # -----------------------------------------------------------------------------
diff --git a/build_files/cmake/Modules/FindMoltenVK.cmake b/build_files/cmake/Modules/FindMoltenVK.cmake
new file mode 100644
index 00000000000..07584e51ae5
--- /dev/null
+++ b/build_files/cmake/Modules/FindMoltenVK.cmake
@@ -0,0 +1,59 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright 2022 Blender Foundation.
+
+# - Find MoltenVK libraries
+# Find the MoltenVK includes and libraries
+# This module defines
+#  MOLTENVK_INCLUDE_DIRS, where to find MoltenVK headers, Set when
+#                        MOLTENVK_INCLUDE_DIR is found.
+#  MOLTENVK_LIBRARIES, libraries to link against to use MoltenVK.
+#  MOLTENVK_ROOT_DIR, The base directory to search for MoltenVK.
+#                    This can also be an environment variable.
+#  MOLTENVK_FOUND, If false, do not try to use MoltenVK.
+#
+
+# If MOLTENVK_ROOT_DIR was defined in the environment, use it.
+IF(NOT MOLTENVK_ROOT_DIR AND NOT $ENV{MOLTENVK_ROOT_DIR} STREQUAL "")
+  SET(MOLTENVK_ROOT_DIR $ENV{MOLTENVK_ROOT_DIR})
+ENDIF()
+
+SET(_moltenvk_SEARCH_DIRS
+  ${MOLTENVK_ROOT_DIR}
+  ${LIBDIR}/vulkan/MoltenVK
+)
+
+
+FIND_PATH(MOLTENVK_INCLUDE_DIR
+  NAMES
+    MoltenVK/vk_mvk_moltenvk.h
+  HINTS
+    ${_moltenvk_SEARCH_DIRS}
+  PATH_SUFFIXES
+    include
+)
+
+FIND_LIBRARY(MOLTENVK_LIBRARY
+  NAMES
+    MoltenVK
+  HINTS
+    ${_moltenvk_SEARCH_DIRS}
+  PATH_SUFFIXES
+    dylib/macOS
+)
+
+# handle the QUIETLY and REQUIRED arguments and set MOLTENVK_FOUND to TRUE if
+# all listed variables are TRUE
+INCLUDE(FindPackageHandleStandardArgs)
+FIND_PACKAGE_HANDLE_STANDARD_ARGS(MoltenVK DEFAULT_MSG MOLTENVK_LIBRARY MOLTENVK_INCLUDE_DIR)
+
+IF(MOLTENVK_FOUND)
+  SET(MOLTENVK_LIBRARIES ${MOLTENVK_LIBRARY})
+  SET(MOLTENVK_INCLUDE_DIRS ${MOLTENVK_INCLUDE_DIR})
+ENDIF()
+
+MARK_AS_ADVANCED(
+  MOLTENVK_INCLUDE_DIR
+  MOLTENVK_LIBRARY
+)
+
+UNSET(_moltenvk_SEARCH_DIRS)
diff --git a/build_files/cmake/platform/platform_apple.cmake b/build_files/cmake/platform/platform_apple.cmake
index c5fe3c908de..b923a076792 100644
--- a/build_files/cmake/platform/platform_apple.cmake
+++ b/build_files/cmake/platform/platform_apple.cmake
@@ -100,6 +100,11 @@ if(WITH_USD)
   find_package(USD REQUIRED)
 endif()
 
+if(WITH_VULKAN_BACKEND)
+  find_package(Vulkan REQUIRED)
+  find_package(MoltenVK REQUIRED)
+endif()
+
 if(WITH_OPENSUBDIV)
   find_package(OpenSubdiv)
 endif()
diff --git a/build_files/cmake/platform/platform_unix.cmake b/build_files/cmake/platform/platform_unix.cmake
index f1ce3221440..10be375ee0f 100644
--- a/build_files/cmake/platform/platform_unix.cmake
+++ b/build_files/cmake/platform/platform_unix.cmake
@@ -108,6 +108,10 @@ find_package_wrapper(ZLIB REQUIRED)
 find_package_wrapper(Zstd REQUIRED)
 find_package_wrapper(Epoxy REQUIRED)
 
+if(WITH_VULKAN_BACKEND)
+  find_package_wrapper(Vulkan REQUIRED)
+endif()
+
 function(check_freetype_for_brotli)
   include(CheckSymbolExists)
   set(CMAKE_REQUIRED_INCLUDES ${FREETYPE_INCLUDE_DIRS})
diff --git a/build_files/cmake/platform/platform_win32.cmake b/build_files/cmake/platform/platform_win32.cmake
index 47673794652..0439b9b2fb6 100644
--- a/build_files/cmake/platform/platform_win32.cmake
+++ b/build_files/cmake/platform/platform_win32.cmake
@@ -926,6 +926,20 @@ if(WITH_HARU)
   set(HARU_LIBRARIES ${HARU_ROOT_DIR}/lib/libhpdfs.lib)
 endif()
 
+if(WITH_VULKAN_BACKEND)
+  if(EXISTS ${LIBDIR}/vulkan)
+    set(VULKAN_FOUND On)
+    set(VULKAN_ROOT_DIR ${LIBDIR}/vulkan)
+    set(VULKAN_INCLUDE_DIR ${VULKAN_ROOT_DIR}/include)
+    set(VULKAN_INCLUDE_DIRS ${VULKAN_INCLUDE_DIR})
+    set(VULKAN_LIBRARY ${VULKAN_ROOT_DIR}/lib/vulkan-1.lib)
+    set(VULKAN_LIBRARIES ${VULKAN_LIBRARY})
+  else()
+    message(WARNING "Vulkan was not found, disabling WITH_VULKAN_BACKEND")
+    set(WITH_VULKAN_BACKEND OFF)
+  endif()
+endif()
+
 if(WITH_CYCLES AND WITH_CYCLES_PATH_GUIDING)
   find_package(openpgl QUIET)
   if(openpgl_FOUND)
diff --git a/intern/ghost/CMakeLists.txt b/intern/ghost/CMakeLists.txt
index 599d5304cac..512ce5300c6 100644
--- a/intern/ghost/CMakeLists.txt
+++ b/intern/ghost/CMakeLists.txt
@@ -76,6 +76,26 @@ set(LIB
   ${Epoxy_LIBRARIES}
 )
 
+if(WITH_VULKAN_BACKEND)
+  list(APPEND SRC
+    intern/GHOST_ContextVK.cpp
+
+    intern/GHOST_ContextVK.h
+  )
+
+  list(APPEND INC_SYS
+    ${VULKAN_INCLUDE_DIRS}
+    ${MOLTENVK_INCLUDE_DIRS}
+  )
+
+  list(APPEND LIB
+    ${VULKAN_LIBRARIES}
+    ${MOLTENVK_LIBRARIES}
+  )
+
+  add_definitions(-DWITH_VULKAN_BACKEND)
+endif()
+
 if(WITH_GHOST_DEBUG)
   list(APPEND SRC
     intern/GHOST_EventPrinter.cpp
diff --git a/intern/ghost/GHOST_C-api.h b/intern/ghost/GHOST_C-api.h
index 62984c762c1..a8dae232d8b 100644
--- a/intern/ghost/GHOST_C-api.h
+++ b/intern/ghost/GHOST_C-api.h
@@ -1185,6 +1185,30 @@ int GHOST_XrGetControllerModelData(GHOST_XrContextHandle xr_context,
 
 #endif /* WITH_XR_OPENXR */
 
+#ifdef WITH_VULKAN_BACKEND
+
+/**
+ * Return vulkan handles for the given context.
+ */
+void GHOST_GetVulkanHandles(GHOST_ContextHandle context,
+                            void *r_instance,
+                            void *r_physical_device,
+                            void *r_device,
+                            uint32_t *r_graphic_queue_familly);
+
+/**
+ * Return vulkan backbuffer resources handles for the given window.
+ */
+void GHOST_GetVulkanBackbuffer(GHOST_WindowHandle windowhandle,
+                               void *image,
+                               void *framebuffer,
+                               void *command_buffer,
+                               void *render_pass,
+                               void *extent,
+                               uint32_t *fb_id);
+
+#endif
+
 #ifdef __cplusplus
 }
 
diff --git a/intern/ghost/GHOST_IContext.h b/intern/ghost/GHOST_IContext.h
index 3d4a034f7f3..52863e8c061 100644
--- a/intern/ghost/GHOST_IContext.h
+++ b/intern/ghost/GHOST_IContext.h
@@ -40,6 +40,20 @@ class GHOST_IContext {
 
   virtual unsigned int getDefaultFramebuffer() = 0;
 
+  virtual GHOST_TSuccess getVulkanHandles(void *, void *, void *, uint32_t *) = 0;
+
+  /**
+   * Gets the Vulkan framebuffer related resource handles associated with the Vulkan context.
+   * Needs to be called after each swap events as the framebuffer will change.
+   * \return  A boolean success indicator.
+   */
+  virtual GHOST_TSuccess getVulkanBackbuffer(void *image,
+                                             void *framebuffer,
+                                             void *command_buffer,
+                                             void *render_pass,
+                                             void *extent,
+                                             uint32_t *fb_id) = 0;
+
   virtual GHOST_TSuccess swapBuffers() = 0;
 
 #ifdef WITH_CXX_GUARDEDALLOC
diff --git a/intern/ghost/GHOST_IWindow.h b/intern/ghost/GHOST_IWindow.h
index f712d9bd9f0..33b9d160f0f 100644
--- a/intern/ghost/GHOST_IWindow.h
+++ b/intern/ghost/GHOST_IWindow.h
@@ -14,6 +14,8 @@
 #include <stdlib.h>
 #include <string>
 
+class GHOST_IContext;
+
 /**
  * Interface for GHOST windows.
  *
@@ -62,6 +64,12 @@ class GHOST_IWindow {
    */
   virtual GHOST_TSuccess setDrawingContextType(GHOST_TDrawingContextType type) = 0;
 
+  /**
+   * Returns the drawing context used in this window.
+   * \return The current drawing context.
+   */
+  virtual GHOST_IContext *getDrawingContext() = 0;
+
   /**
    * Sets the title displayed in the title bar.
    * \param title: The title to display in the title bar.
@@ -202,6 +210,18 @@ class GHOST_IWindow {
    */
   virtual unsigned int getDefaultFramebuffer() = 0;
 
+  /**
+   * Gets the Vulkan framebuffer related resource handles associated with the Vulkan context.
+   * Needs to be called after each swap events as the framebuffer will change.
+   * \return  A boolean success indicator.
+   */
+  virtual GHOST_TSuccess getVulkanBackbuffer(void *image,
+                                             void *framebuffer,
+                                             void *command_buffer,
+                                             void *render_pass,
+                                             void *extent,
+                                             uint32_t *fb_id) = 0;
+
   /**
    * Invalidates the contents of this window.
    * \return Indication of success.
diff --git a/intern/ghost/GHOST_Types.h b/intern/ghost/GHOST_Types.h
index db4eeff3122..3932bc76af0 100644
--- a/intern/ghost/GHOST_Types.h
+++ b/intern/ghost/GHOST_Types.h
@@ -156,6 +156,9 @@ typedef enum {
 #ifdef __APPLE__
   GHOST_kDrawingContextTypeMetal,
 #endif
+#ifdef WITH_VULKAN_BACKEND
+  GHOST_kDrawingContextTypeVulkan,
+#endif
 } GHOST_TDrawingContextType;
 
 typedef enum {
diff --git a/intern/ghost/intern/GHOST_C-api.cpp b/intern/ghost/intern/GHOST_C-api.cpp
index 0c595b27148..0430dc8602c 100644
--- a/intern/ghost/intern/GHOST_C-api.cpp
+++ b/intern/ghost/intern/GHOST_C-api.cpp
@@ -565,6 +565,12 @@ GHOST_TSuccess GHOST_SetDrawingContextType(GHOST_WindowHandle windowhandle,
   return window->setDrawingContextType(type);
 }
 
+GHOST_ContextHandle GHOST_GetDrawingContext(GHOST_WindowHandle windowhandle)
+{
+  GHOST_IWindow *window = (GHOST_IWindow *)windowhandle;
+  return (GHOST_ContextHandle)window->getDrawingContext();
+}
+
 void GHOST_SetTitle(GHOST_WindowHandle windowhandle, const char *title)
 {
   GHOST_IWindow *window = (GHOST_IWindow *)windowhandle;
@@ -1190,3 +1196,35 @@ int GHOST_XrGetControllerModelData(GHOST_XrContextHandle xr_contexthandle,
 }
 
 #endif /* WITH_XR_OPENXR */
+
+#ifdef WITH_VULKAN_BACKEND
+
+/**
+ * Return vulkan handles for the given co

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list