Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use "volk" instead of statically linked Vulkan loader. #51516

Merged
merged 2 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions COPYRIGHT.txt
Original file line number Diff line number Diff line change
Expand Up @@ -438,12 +438,17 @@ Copyright: 2011, Khaled Mamou
2003-2009, Erwin Coumans
License: BSD-3-clause

Files: ./thirdparty/volk/
Comment: volk
Copyright: 2018-2019, Arseny Kapoulkine
License: Expat

Files: ./thirdparty/vulkan/
Comment: Vulkan Ecosystem Components (Vulkan ICD loader and headers)
Copyright: 2014-2020, The Khronos Group Inc.
2014-2020, Valve Corporation
2014-2020, LunarG, Inc.
2015-2020, Google Inc.
Comment: Vulkan Headers
Copyright: 2014-2021, The Khronos Group Inc.
2014-2021, Valve Corporation
2014-2021, LunarG, Inc.
2015-2021, Google Inc.
License: Apache-2.0

Files: ./thirdparty/vulkan/vk_mem_alloc.h
Expand Down
2 changes: 1 addition & 1 deletion SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ opts.Add(BoolVariable("xaudio2", "Enable the XAudio2 audio driver", False))
opts.Add(BoolVariable("vulkan", "Enable the vulkan video driver", True))
opts.Add("custom_modules", "A list of comma-separated directory paths containing custom modules to build.", "")
opts.Add(BoolVariable("custom_modules_recursive", "Detect custom modules recursively for each specified path.", True))
opts.Add(BoolVariable("use_volk", "Use the volk library to load the Vulkan loader dynamically", True))

# Advanced options
opts.Add(BoolVariable("dev", "If yes, alias for verbose=yes warnings=extra werror=yes", False))
Expand Down Expand Up @@ -174,7 +175,6 @@ opts.Add(BoolVariable("builtin_pcre2_with_jit", "Use JIT compiler for the built-
opts.Add(BoolVariable("builtin_recast", "Use the built-in Recast library", True))
opts.Add(BoolVariable("builtin_rvo2", "Use the built-in RVO2 library", True))
opts.Add(BoolVariable("builtin_squish", "Use the built-in squish library", True))
opts.Add(BoolVariable("builtin_vulkan", "Use the built-in Vulkan loader library and headers", True))
opts.Add(BoolVariable("builtin_xatlas", "Use the built-in xatlas library", True))
opts.Add(BoolVariable("builtin_zlib", "Use the built-in zlib library", True))
opts.Add(BoolVariable("builtin_zstd", "Use the built-in Zstd library", True))
Expand Down
140 changes: 38 additions & 102 deletions drivers/vulkan/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -3,116 +3,52 @@
Import("env")

thirdparty_obj = []
thirdparty_dir = "#thirdparty/vulkan"
thirdparty_volk_dir = "#thirdparty/volk"

# FIXME: Refactor all this to reduce code duplication.
if env["platform"] == "android":
if env["use_volk"]:
env.AppendUnique(CPPDEFINES=["USE_VOLK"])
env.Prepend(CPPPATH=[thirdparty_volk_dir])

if env["platform"] == "android" and not env["use_volk"]:
# Use NDK Vulkan headers
thirdparty_dir = env["ANDROID_NDK_ROOT"] + "/sources/third_party/vulkan/src"
ndk_vulkan_dir = env["ANDROID_NDK_ROOT"] + "/sources/third_party/vulkan/src"
thirdparty_includes = [
thirdparty_dir,
thirdparty_dir + "/include",
thirdparty_dir + "/layers",
thirdparty_dir + "/layers/generated",
ndk_vulkan_dir,
ndk_vulkan_dir + "/include",
ndk_vulkan_dir + "/layers",
ndk_vulkan_dir + "/layers/generated",
]
env.Prepend(CPPPATH=thirdparty_includes)

# Build Vulkan memory allocator
env_thirdparty = env.Clone()
env_thirdparty.disable_warnings()

thirdparty_dir = "#thirdparty/vulkan"
vma_sources = [thirdparty_dir + "/android/vk_mem_alloc.cpp"]
env_thirdparty.add_source_files(thirdparty_obj, vma_sources)

elif env["platform"] == "iphone":
# Use bundled Vulkan headers
thirdparty_dir = "#thirdparty/vulkan"
env.Prepend(CPPPATH=[thirdparty_dir, thirdparty_dir + "/include", thirdparty_dir + "/loader"])

# Build Vulkan memory allocator
env_thirdparty = env.Clone()
env_thirdparty.disable_warnings()

vma_sources = [thirdparty_dir + "/vk_mem_alloc.cpp"]
env_thirdparty.add_source_files(thirdparty_obj, vma_sources)

elif env["builtin_vulkan"]:
else:
# Use bundled Vulkan headers
thirdparty_dir = "#thirdparty/vulkan"
env.Prepend(CPPPATH=[thirdparty_dir, thirdparty_dir + "/include", thirdparty_dir + "/loader"])

# Build Vulkan loader library
env_thirdparty = env.Clone()
env_thirdparty.disable_warnings()

loader_sources = [
"cJSON.c",
"debug_utils.c",
"dev_ext_trampoline.c",
"loader.c",
"murmurhash.c",
"phys_dev_ext.c",
"trampoline.c",
"unknown_ext_chain.c",
"wsi.c",
"extension_manual.c",
]
vma_sources = [thirdparty_dir + "/vk_mem_alloc.cpp"]

if env["platform"] == "windows":
loader_sources.append("dirent_on_windows.c")
env_thirdparty.AppendUnique(
CPPDEFINES=[
"VK_USE_PLATFORM_WIN32_KHR",
"VULKAN_NON_CMAKE_BUILD",
"WIN32_LEAN_AND_MEAN",
'API_NAME=\\"%s\\"' % "Vulkan",
]
)
if not env.msvc: # Windows 7+, missing in mingw headers
env_thirdparty.AppendUnique(
CPPDEFINES=["CM_GETIDLIST_FILTER_CLASS=0x00000200", "CM_GETIDLIST_FILTER_PRESENT=0x00000100"]
)
elif env["platform"] == "osx":
env_thirdparty.AppendUnique(
CPPDEFINES=[
"VK_USE_PLATFORM_MACOS_MVK",
"VULKAN_NON_CMAKE_BUILD",
'SYSCONFDIR=\\"%s\\"' % "/etc",
'FALLBACK_DATA_DIRS=\\"%s\\"' % "/usr/local/share:/usr/share",
'FALLBACK_CONFIG_DIRS=\\"%s\\"' % "/etc/xdg",
]
)
elif env["platform"] == "linuxbsd":
env_thirdparty.AppendUnique(
CPPDEFINES=[
"VK_USE_PLATFORM_XLIB_KHR",
"VULKAN_NON_CMAKE_BUILD",
'SYSCONFDIR=\\"%s\\"' % "/etc",
'FALLBACK_DATA_DIRS=\\"%s\\"' % "/usr/local/share:/usr/share",
'FALLBACK_CONFIG_DIRS=\\"%s\\"' % "/etc/xdg",
]
)
import platform

if platform.system() == "Linux":
# In glibc since 2.17 and musl libc since 1.1.24. Used by loader.c.
env_thirdparty.AppendUnique(CPPDEFINES=["HAVE_SECURE_GETENV"])

loader_sources = [thirdparty_dir + "/loader/" + file for file in loader_sources]
env_thirdparty.add_source_files(thirdparty_obj, loader_sources)
env_thirdparty.add_source_files(thirdparty_obj, vma_sources)

else: # Always build VMA.
thirdparty_dir = "#thirdparty/vulkan"
env.Prepend(CPPPATH=[thirdparty_dir, thirdparty_dir + "/include"])

# Build Vulkan loader library
env_thirdparty = env.Clone()
env_thirdparty.disable_warnings()
vma_sources = [thirdparty_dir + "/vk_mem_alloc.cpp"]

env_thirdparty.add_source_files(thirdparty_obj, vma_sources)
if env["platform"] == "android":
env.AppendUnique(CPPDEFINES=["VK_USE_PLATFORM_ANDROID_KHR"])
elif env["platform"] == "iphone":
env.AppendUnique(CPPDEFINES=["VK_USE_PLATFORM_IOS_MVK"])
elif env["platform"] == "linuxbsd":
env.AppendUnique(CPPDEFINES=["VK_USE_PLATFORM_XLIB_KHR"])
elif env["platform"] == "osx":
env.AppendUnique(CPPDEFINES=["VK_USE_PLATFORM_MACOS_MVK"])
elif env["platform"] == "windows":
env.AppendUnique(CPPDEFINES=["VK_USE_PLATFORM_WIN32_KHR"])

# Build Vulkan memory allocator and volk
env_thirdparty_vma = env.Clone()
env_thirdparty_vma.disable_warnings()
thirdparty_sources_vma = [thirdparty_dir + "/vk_mem_alloc.cpp"]

if env["use_volk"]:
env_thirdparty_vma.AppendUnique(CPPDEFINES=["VMA_STATIC_VULKAN_FUNCTIONS=1"])
env_thirdparty_volk = env.Clone()
env_thirdparty_volk.disable_warnings()

thirdparty_sources_volk = [thirdparty_volk_dir + "/volk.c"]
env_thirdparty_volk.add_source_files(thirdparty_obj, thirdparty_sources_volk)

env_thirdparty_vma.add_source_files(thirdparty_obj, thirdparty_sources_vma)


env.drivers_sources += thirdparty_obj
Expand Down
2 changes: 1 addition & 1 deletion drivers/vulkan/rendering_device_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3825,7 +3825,7 @@ RenderingDevice::FramebufferFormatID RenderingDeviceVulkan::framebuffer_format_c
VkRenderPass render_pass;
VkResult res = vkCreateRenderPass(device, &render_pass_create_info, nullptr, &render_pass);

ERR_FAIL_COND_V_MSG(res, VK_NULL_HANDLE, "vkCreateRenderPass for empty fb failed with error " + itos(res) + ".");
ERR_FAIL_COND_V_MSG(res, 0, "vkCreateRenderPass for empty fb failed with error " + itos(res) + ".");

if (render_pass == VK_NULL_HANDLE) { //was likely invalid
return INVALID_ID;
Expand Down
4 changes: 4 additions & 0 deletions drivers/vulkan/rendering_device_vulkan.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@
#endif
#include "vk_mem_alloc.h"

#ifdef USE_VOLK
#include <volk.h>
#else
#include <vulkan/vulkan.h>
#endif

class VulkanContext;

Expand Down
9 changes: 9 additions & 0 deletions drivers/vulkan/vulkan_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,10 @@ Error VulkanContext::_create_physical_device() {

inst_initialized = true;

#ifdef USE_VOLK
volkLoadInstance(inst);
#endif

/* Make initial call to query gpu_count, then second call for gpu info*/
err = vkEnumeratePhysicalDevices(inst, &gpu_count, nullptr);
ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
Expand Down Expand Up @@ -1669,6 +1673,11 @@ Error VulkanContext::_update_swap_chain(Window *window) {
}

Error VulkanContext::initialize() {
#ifdef USE_VOLK
if (volkInitialize() != VK_SUCCESS) {
return FAILED;
}
#endif
Error err = _create_physical_device();
if (err) {
return err;
Expand Down
4 changes: 4 additions & 0 deletions drivers/vulkan/vulkan_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@
#include "core/templates/rid_owner.h"
#include "servers/display_server.h"

#ifdef USE_VOLK
#include <volk.h>
#else
#include <vulkan/vulkan.h>
#endif

class VulkanContext {
public:
Expand Down
10 changes: 8 additions & 2 deletions platform/android/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def get_android_ndk_root():
def get_flags():
return [
("tools", False),
("use_volk", False),
]


Expand Down Expand Up @@ -367,8 +368,13 @@ def mySpawn(sh, escape, cmd, args, env):
)

env.Prepend(CPPPATH=["#platform/android"])
env.Append(CPPDEFINES=["ANDROID_ENABLED", "UNIX_ENABLED", "VULKAN_ENABLED", "NO_FCNTL"])
env.Append(LIBS=["OpenSLES", "EGL", "GLESv2", "vulkan", "android", "log", "z", "dl"])
env.Append(CPPDEFINES=["ANDROID_ENABLED", "UNIX_ENABLED", "NO_FCNTL"])
env.Append(LIBS=["OpenSLES", "EGL", "GLESv2", "android", "log", "z", "dl"])

if env["vulkan"]:
env.Append(CPPDEFINES=["VULKAN_ENABLED"])
if not env["use_volk"]:
env.Append(LIBS=["vulkan"])


# Return the project NDK version.
Expand Down
6 changes: 5 additions & 1 deletion platform/android/vulkan/vulkan_context_android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@

#include "vulkan_context_android.h"

#include <vulkan/vulkan_android.h>
#ifdef USE_VOLK
#include <volk.h>
#else
#include <vulkan/vulkan.h>
#endif

const char *VulkanContextAndroid::_get_platform_surface_extension() const {
return VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
Expand Down
48 changes: 3 additions & 45 deletions platform/iphone/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ def get_opts():
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain",
),
("IPHONESDK", "Path to the iPhone SDK", ""),
BoolVariable(
"use_static_mvk",
"Link MoltenVK statically as Level-0 driver (better portability) or use Vulkan ICD loader (enables"
" validation layers)",
False,
),
BoolVariable("ios_simulator", "Build for iOS Simulator", False),
BoolVariable("ios_exceptions", "Enable exceptions", False),
("ios_triple", "Triple for ios toolchain", ""),
Expand All @@ -43,6 +37,7 @@ def get_opts():
def get_flags():
return [
("tools", False),
("use_volk", False),
]


Expand Down Expand Up @@ -111,12 +106,10 @@ def configure(env):
if env["ios_simulator"]:
detect_darwin_sdk_path("iphonesimulator", env)
env.Append(CCFLAGS=["-mios-simulator-version-min=13.0"])
env.Append(LINKFLAGS=["-mios-simulator-version-min=13.0"])
env.extra_suffix = ".simulator" + env.extra_suffix
else:
detect_darwin_sdk_path("iphone", env)
env.Append(CCFLAGS=["-miphoneos-version-min=11.0"])
env.Append(LINKFLAGS=["-miphoneos-version-min=11.0"])

if env["arch"] == "x86" or env["arch"] == "x86_64":
env["ENV"]["MACOSX_DEPLOYMENT_TARGET"] = "10.9"
Expand Down Expand Up @@ -162,50 +155,15 @@ def configure(env):
# Temp fix for ABS/MAX/MIN macros in iPhone SDK blocking compilation
env.Append(CCFLAGS=["-Wno-ambiguous-macro"])

## Link flags

if env["arch"] == "x86" or env["arch"] == "x86_64":
arch_flag = "i386" if env["arch"] == "x86" else env["arch"]
env.Append(
LINKFLAGS=[
"-arch",
arch_flag,
"-isysroot",
"$IPHONESDK",
"-Xlinker",
"-objc_abi_version",
"-Xlinker",
"2",
"-F$IPHONESDK",
]
)
elif env["arch"] == "arm":
env.Append(LINKFLAGS=["-arch", "armv7", "-Wl,-dead_strip"])
if env["arch"] == "arm64":
env.Append(LINKFLAGS=["-arch", "arm64", "-Wl,-dead_strip"])

env.Append(
LINKFLAGS=[
"-isysroot",
"$IPHONESDK",
]
)
bruvzg marked this conversation as resolved.
Show resolved Hide resolved

env.Prepend(
CPPPATH=[
"$IPHONESDK/usr/include",
"$IPHONESDK/System/Library/Frameworks/AudioUnit.framework/Headers",
]
)

env["ENV"]["CODESIGN_ALLOCATE"] = "/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate"

env.Prepend(CPPPATH=["#platform/iphone"])
env.Append(CPPDEFINES=["IPHONE_ENABLED", "UNIX_ENABLED", "COREAUDIO_ENABLED"])

env.Append(CPPDEFINES=["VULKAN_ENABLED"])
env.Append(LINKFLAGS=["-framework", "IOSurface"])

# Use Static Vulkan for iOS. Dynamic Framework works fine too.
env.Append(LINKFLAGS=["-framework", "MoltenVK"])
env["builtin_vulkan"] = False
if env["vulkan"]:
env.Append(CPPDEFINES=["VULKAN_ENABLED"])
6 changes: 5 additions & 1 deletion platform/iphone/display_server_iphone.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@
#include "vulkan_context_iphone.h"

#import <QuartzCore/CAMetalLayer.h>
#include <vulkan/vulkan_metal.h>
#ifdef USE_VOLK
#include <volk.h>
#else
#include <vulkan/vulkan.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intended that this is now vulkan/vulkan.h and no longer vulkan_metal.h? (same for vulkan_ios.h)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It's auto included in the vulkan.h if VK_USE_PLATFORM_IOS_MVK is defined, which was missing for iOS and Android and required for volk to build anyway.

#endif
#endif

class DisplayServerIPhone : public DisplayServer {
Expand Down
1 change: 0 additions & 1 deletion platform/iphone/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

#import <UIKit/UIKit.h>
#include <stdio.h>
#include <vulkan/vulkan.h>

int gargc;
char **gargv;
Expand Down
Loading