Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Refactor Vulkan stream buffer memory type selection #238

Merged
merged 4 commits into from
Aug 25, 2024
Merged
Changes from 1 commit
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
39 changes: 30 additions & 9 deletions src/video_core/renderer_vulkan/vk_stream_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ vk::MemoryPropertyFlags MakePropertyFlags(BufferType type) {
case BufferType::Upload:
return vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent;
case BufferType::Download:
return vk::MemoryPropertyFlagBits::eHostVisible |
vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached;
return vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached;
case BufferType::Stream:
return vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible |
vk::MemoryPropertyFlagBits::eHostCoherent;
return vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent;
default:
UNREACHABLE_MSG("Unknown buffer type {}", type);
return vk::MemoryPropertyFlagBits::eHostVisible;
Expand All @@ -47,19 +45,42 @@ vk::MemoryPropertyFlags MakePropertyFlags(BufferType type) {
/// Get the preferred host visible memory type.
u32 GetMemoryType(const vk::PhysicalDeviceMemoryProperties& properties, BufferType type) {
vk::MemoryPropertyFlags flags = MakePropertyFlags(type);
std::optional preferred_type = FindMemoryType(properties, flags);
std::optional<u32> preferred_type;

// Try to find a memory type with all the requested flags
preferred_type = FindMemoryType(properties, flags);
if (preferred_type) {
return *preferred_type;
}

// If not found, try removing flags one by one
constexpr std::array remove_flags = {
vk::MemoryPropertyFlagBits::eDeviceLocal,
vk::MemoryPropertyFlagBits::eHostCached,
vk::MemoryPropertyFlagBits::eHostCoherent,
};

for (u32 i = 0; i < remove_flags.size() && !preferred_type; i++) {
flags &= ~remove_flags[i];
for (auto remove_flag : remove_flags) {
if ((flags & remove_flag) == vk::MemoryPropertyFlags{}) {
continue;
}
flags &= ~remove_flag;
preferred_type = FindMemoryType(properties, flags);
if (preferred_type) {
return *preferred_type;
}
}
ASSERT_MSG(preferred_type, "No suitable memory type found");
return preferred_type.value();

// If still not found, try with only eHostVisible flag
preferred_type = FindMemoryType(properties, vk::MemoryPropertyFlagBits::eHostVisible);
if (preferred_type) {
return *preferred_type;
}

// If we reach here, we couldn't find any suitable memory type
LOG_CRITICAL(Render_Vulkan, "Failed to find a suitable memory type for buffer type {}",
BufferTypeName(type));
return 0; // Return 0 as a fallback, though this will likely cause issues
i0x404 marked this conversation as resolved.
Show resolved Hide resolved
}

constexpr u64 WATCHES_INITIAL_RESERVE = 0x4000;
Expand Down
Loading