summaryrefslogtreecommitdiff
path: root/drivers
diff options
context:
space:
mode:
Diffstat (limited to 'drivers')
-rw-r--r--drivers/vulkan/SCsub140
-rw-r--r--drivers/vulkan/rendering_device_vulkan.cpp107
-rw-r--r--drivers/vulkan/rendering_device_vulkan.h4
-rw-r--r--drivers/vulkan/vulkan_context.cpp9
-rw-r--r--drivers/vulkan/vulkan_context.h4
-rw-r--r--drivers/windows/file_access_windows.cpp22
6 files changed, 154 insertions, 132 deletions
diff --git a/drivers/vulkan/SCsub b/drivers/vulkan/SCsub
index 6a2a57d18c..3e0f5788c3 100644
--- a/drivers/vulkan/SCsub
+++ b/drivers/vulkan/SCsub
@@ -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
diff --git a/drivers/vulkan/rendering_device_vulkan.cpp b/drivers/vulkan/rendering_device_vulkan.cpp
index b5bd6cef38..f6677e2da4 100644
--- a/drivers/vulkan/rendering_device_vulkan.cpp
+++ b/drivers/vulkan/rendering_device_vulkan.cpp
@@ -3299,13 +3299,25 @@ VkRenderPass RenderingDeviceVulkan::_render_pass_create(const Vector<AttachmentF
// Also, each UNDEFINED will do an immediate layout transition (write), s.t. we must ensure execution synchronization vs.
// the read. If this is a performance issue, one could track the actual last accessor of each resource, adding only that
// stage
+
switch (is_depth ? p_initial_depth_action : p_initial_action) {
case INITIAL_ACTION_CLEAR_REGION:
case INITIAL_ACTION_CLEAR: {
- description.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
- description.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
- description.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; //don't care what is there
- dependency_from_external.srcStageMask |= reading_stages;
+ if (p_attachments[i].usage_flags & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT) {
+ description.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
+ description.initialLayout = is_sampled ? VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL : (is_storage ? VK_IMAGE_LAYOUT_GENERAL : VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
+ description.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
+ } else if (p_attachments[i].usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
+ description.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
+ description.initialLayout = is_sampled ? VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL : (is_storage ? VK_IMAGE_LAYOUT_GENERAL : VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
+ description.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
+ dependency_from_external.srcStageMask |= reading_stages;
+ } else {
+ description.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
+ description.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
+ description.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; //don't care what is there
+ dependency_from_external.srcStageMask |= reading_stages;
+ }
} break;
case INITIAL_ACTION_KEEP: {
if (p_attachments[i].usage_flags & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT) {
@@ -3363,7 +3375,58 @@ VkRenderPass RenderingDeviceVulkan::_render_pass_create(const Vector<AttachmentF
}
}
- switch (is_depth ? p_final_depth_action : p_final_action) {
+ bool used_last = false;
+
+ {
+ int last_pass = p_passes.size() - 1;
+
+ if (is_depth) {
+ //likely missing depth resolve?
+ if (p_passes[last_pass].depth_attachment == i) {
+ used_last = true;
+ }
+ } else {
+ if (p_passes[last_pass].resolve_attachments.size()) {
+ //if using resolve attachments, check resolve attachments
+ for (int j = 0; j < p_passes[last_pass].resolve_attachments.size(); j++) {
+ if (p_passes[last_pass].resolve_attachments[j] == i) {
+ used_last = true;
+ break;
+ }
+ }
+ } else {
+ for (int j = 0; j < p_passes[last_pass].color_attachments.size(); j++) {
+ if (p_passes[last_pass].color_attachments[j] == i) {
+ used_last = true;
+ break;
+ }
+ }
+ }
+ }
+
+ if (!used_last) {
+ for (int j = 0; j < p_passes[last_pass].preserve_attachments.size(); j++) {
+ if (p_passes[last_pass].preserve_attachments[j] == i) {
+ used_last = true;
+ break;
+ }
+ }
+ }
+ }
+
+ FinalAction final_action = p_final_action;
+ FinalAction final_depth_action = p_final_depth_action;
+
+ if (!used_last) {
+ if (is_depth) {
+ final_depth_action = FINAL_ACTION_DISCARD;
+
+ } else {
+ final_action = FINAL_ACTION_DISCARD;
+ }
+ }
+
+ switch (is_depth ? final_depth_action : final_action) {
case FINAL_ACTION_READ: {
if (p_attachments[i].usage_flags & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT) {
description.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
@@ -3516,21 +3579,6 @@ VkRenderPass RenderingDeviceVulkan::_render_pass_create(const Vector<AttachmentF
resolve_references.push_back(reference);
}
- LocalVector<uint32_t> &preserve_references = preserve_reference_array[i];
-
- for (int j = 0; j < pass->preserve_attachments.size(); j++) {
- int32_t attachment = pass->preserve_attachments[j];
-
- ERR_FAIL_COND_V_MSG(attachment == FramebufferPass::ATTACHMENT_UNUSED, VK_NULL_HANDLE, "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), preserve attachment (" + itos(j) + "). Preserve attachments can't be unused.");
-
- ERR_FAIL_INDEX_V_MSG(attachment, p_attachments.size(), VK_NULL_HANDLE, "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), preserve attachment (" + itos(j) + ").");
- ERR_FAIL_COND_V_MSG(attachment_last_pass[attachment] == i, VK_NULL_HANDLE, "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), it already was used for something else before in this pass.");
-
- attachment_last_pass[attachment] = i;
-
- preserve_references.push_back(attachment);
- }
-
VkAttachmentReference &depth_stencil_reference = depth_reference_array[i];
if (pass->depth_attachment != FramebufferPass::ATTACHMENT_UNUSED) {
@@ -3554,6 +3602,22 @@ VkRenderPass RenderingDeviceVulkan::_render_pass_create(const Vector<AttachmentF
depth_stencil_reference.layout = VK_IMAGE_LAYOUT_UNDEFINED;
}
+ LocalVector<uint32_t> &preserve_references = preserve_reference_array[i];
+
+ for (int j = 0; j < pass->preserve_attachments.size(); j++) {
+ int32_t attachment = pass->preserve_attachments[j];
+
+ ERR_FAIL_COND_V_MSG(attachment == FramebufferPass::ATTACHMENT_UNUSED, VK_NULL_HANDLE, "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), preserve attachment (" + itos(j) + "). Preserve attachments can't be unused.");
+
+ ERR_FAIL_INDEX_V_MSG(attachment, p_attachments.size(), VK_NULL_HANDLE, "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), preserve attachment (" + itos(j) + ").");
+
+ if (attachment_last_pass[attachment] != i) {
+ //preserve can still be used to keep depth or color from being discarded after use
+ attachment_last_pass[attachment] = i;
+ preserve_references.push_back(attachment);
+ }
+ }
+
VkSubpassDescription &subpass = subpasses[i];
subpass.flags = 0;
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
@@ -3761,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;
@@ -8864,6 +8928,7 @@ void RenderingDeviceVulkan::_free_rids(T &p_owner, const char *p_type) {
}
void RenderingDeviceVulkan::capture_timestamp(const String &p_name) {
+ ERR_FAIL_COND_MSG(draw_list != nullptr, "Capturing timestamps during draw list creation is not allowed. Offending timestap was: " + p_name);
ERR_FAIL_COND(frames[frame].timestamp_count >= max_timestamp_query_elements);
//this should be optional for profiling, else it will slow things down
diff --git a/drivers/vulkan/rendering_device_vulkan.h b/drivers/vulkan/rendering_device_vulkan.h
index dc4583d837..6175369285 100644
--- a/drivers/vulkan/rendering_device_vulkan.h
+++ b/drivers/vulkan/rendering_device_vulkan.h
@@ -44,7 +44,11 @@
#endif
#include "vk_mem_alloc.h"
+#ifdef USE_VOLK
+#include <volk.h>
+#else
#include <vulkan/vulkan.h>
+#endif
class VulkanContext;
diff --git a/drivers/vulkan/vulkan_context.cpp b/drivers/vulkan/vulkan_context.cpp
index a9d0fb6fed..87749450c4 100644
--- a/drivers/vulkan/vulkan_context.cpp
+++ b/drivers/vulkan/vulkan_context.cpp
@@ -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);
@@ -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;
diff --git a/drivers/vulkan/vulkan_context.h b/drivers/vulkan/vulkan_context.h
index 83e9524046..1690b853e3 100644
--- a/drivers/vulkan/vulkan_context.h
+++ b/drivers/vulkan/vulkan_context.h
@@ -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:
diff --git a/drivers/windows/file_access_windows.cpp b/drivers/windows/file_access_windows.cpp
index f504bad60d..775c999b15 100644
--- a/drivers/windows/file_access_windows.cpp
+++ b/drivers/windows/file_access_windows.cpp
@@ -35,6 +35,7 @@
#include "core/os/os.h"
#include "core/string/print_string.h"
+#include <share.h> // _SH_DENYNO
#include <shlwapi.h>
#include <windows.h>
@@ -77,8 +78,8 @@ Error FileAccessWindows::_open(const String &p_path, int p_mode_flags) {
return ERR_INVALID_PARAMETER;
}
- /* pretty much every implementation that uses fopen as primary
- backend supports utf8 encoding */
+ /* Pretty much every implementation that uses fopen as primary
+ backend supports utf8 encoding. */
struct _stat st;
if (_wstat((LPCWSTR)(path.utf16().get_data()), &st) == 0) {
@@ -113,10 +114,10 @@ Error FileAccessWindows::_open(const String &p_path, int p_mode_flags) {
path = path + ".tmp";
}
- errno_t errcode = _wfopen_s(&f, (LPCWSTR)(path.utf16().get_data()), mode_string);
+ f = _wfsopen((LPCWSTR)(path.utf16().get_data()), mode_string, _SH_DENYNO);
if (f == nullptr) {
- switch (errcode) {
+ switch (errno) {
case ENOENT: {
last_error = ERR_FILE_NOT_FOUND;
} break;
@@ -157,10 +158,10 @@ void FileAccessWindows::close() {
#else
if (!PathFileExistsW((LPCWSTR)(save_path.utf16().get_data()))) {
#endif
- //creating new file
+ // Creating new file
rename_error = _wrename((LPCWSTR)((save_path + ".tmp").utf16().get_data()), (LPCWSTR)(save_path.utf16().get_data())) != 0;
} else {
- //atomic replace for existing file
+ // Atomic replace for existing file
rename_error = !ReplaceFileW((LPCWSTR)(save_path.utf16().get_data()), (LPCWSTR)((save_path + ".tmp").utf16().get_data()), nullptr, 2 | 4, nullptr, nullptr);
}
if (rename_error) {
@@ -205,6 +206,7 @@ void FileAccessWindows::seek(uint64_t p_position) {
void FileAccessWindows::seek_end(int64_t p_position) {
ERR_FAIL_COND(!f);
+
if (_fseeki64(f, p_position, SEEK_END)) {
check_errors();
}
@@ -237,6 +239,7 @@ bool FileAccessWindows::eof_reached() const {
uint8_t FileAccessWindows::get_8() const {
ERR_FAIL_COND_V(!f, 0);
+
if (flags == READ_WRITE || flags == WRITE_READ) {
if (prev_op == WRITE) {
fflush(f);
@@ -273,6 +276,7 @@ Error FileAccessWindows::get_error() const {
void FileAccessWindows::flush() {
ERR_FAIL_COND(!f);
+
fflush(f);
if (prev_op == WRITE) {
prev_op = 0;
@@ -281,6 +285,7 @@ void FileAccessWindows::flush() {
void FileAccessWindows::store_8(uint8_t p_dest) {
ERR_FAIL_COND(!f);
+
if (flags == READ_WRITE || flags == WRITE_READ) {
if (prev_op == READ) {
if (last_error != ERR_FILE_EOF) {
@@ -295,6 +300,7 @@ void FileAccessWindows::store_8(uint8_t p_dest) {
void FileAccessWindows::store_buffer(const uint8_t *p_src, uint64_t p_length) {
ERR_FAIL_COND(!f);
ERR_FAIL_COND(!p_src && p_length > 0);
+
if (flags == READ_WRITE || flags == WRITE_READ) {
if (prev_op == READ) {
if (last_error != ERR_FILE_EOF) {
@@ -307,10 +313,8 @@ void FileAccessWindows::store_buffer(const uint8_t *p_src, uint64_t p_length) {
}
bool FileAccessWindows::file_exists(const String &p_name) {
- FILE *g;
- //printf("opening file %s\n", p_fname.utf8().get_data());
String filename = fix_path(p_name);
- _wfopen_s(&g, (LPCWSTR)(filename.utf16().get_data()), L"rb");
+ FILE *g = _wfsopen((LPCWSTR)(filename.utf16().get_data()), L"rb", _SH_DENYNO);
if (g == nullptr) {
return false;
} else {