Skip to content

Commit

Permalink
Fixed usage of VK_KHR_buffer_device_address extension
Browse files Browse the repository at this point in the history
It was confused with VK_EXT_buffer_device_address which is not officially supported by VMA and has different VK_STRUCTURE_TYPE...
Also a fix in PrintMemoryConclusions.
  • Loading branch information
adam-sawicki-a committed Mar 2, 2021
1 parent 41b4111 commit 0a3c6b5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 71 deletions.
17 changes: 7 additions & 10 deletions src/Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ static const char* CODE_DESCRIPTION = "Foo";

extern VkCommandBuffer g_hTemporaryCommandBuffer;
extern const VkAllocationCallbacks* g_Allocs;
extern bool g_BufferDeviceAddressEnabled;
extern bool VK_KHR_buffer_device_address_enabled;
extern bool VK_EXT_memory_priority_enabled;
extern PFN_vkGetBufferDeviceAddressEXT g_vkGetBufferDeviceAddressEXT;
extern PFN_vkGetBufferDeviceAddressKHR g_vkGetBufferDeviceAddressKHR;
void BeginSingleTimeCommands();
void EndSingleTimeCommands();
void SetDebugUtilsObjectName(VkObjectType type, uint64_t handle, const char* name);
Expand Down Expand Up @@ -3802,7 +3802,7 @@ static void TestBufferDeviceAddress()
{
wprintf(L"Test buffer device address\n");

assert(g_BufferDeviceAddressEnabled);
assert(VK_KHR_buffer_device_address_enabled);

VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
bufCreateInfo.size = 0x10000;
Expand All @@ -3825,12 +3825,9 @@ static void TestBufferDeviceAddress()

VkBufferDeviceAddressInfoEXT bufferDeviceAddressInfo = { VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO_EXT };
bufferDeviceAddressInfo.buffer = bufInfo.Buffer;
//assert(g_vkGetBufferDeviceAddressEXT != nullptr);
if(g_vkGetBufferDeviceAddressEXT != nullptr)
{
VkDeviceAddress addr = g_vkGetBufferDeviceAddressEXT(g_hDevice, &bufferDeviceAddressInfo);
TEST(addr != 0);
}
TEST(g_vkGetBufferDeviceAddressKHR != nullptr);
VkDeviceAddress addr = g_vkGetBufferDeviceAddressKHR(g_hDevice, &bufferDeviceAddressInfo);
TEST(addr != 0);

vmaDestroyBuffer(g_hAllocator, bufInfo.Buffer, bufInfo.Allocation);
}
Expand Down Expand Up @@ -6554,7 +6551,7 @@ void Test()
BasicTestBuddyAllocator();
BasicTestAllocatePages();

if(g_BufferDeviceAddressEnabled)
if(VK_KHR_buffer_device_address_enabled)
TestBufferDeviceAddress();
if(VK_EXT_memory_priority_enabled)
TestMemoryPriority();
Expand Down
85 changes: 24 additions & 61 deletions src/VulkanSample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,13 @@ bool VK_KHR_dedicated_allocation_enabled = false;
bool VK_KHR_bind_memory2_enabled = false;
bool VK_EXT_memory_budget_enabled = false;
bool VK_AMD_device_coherent_memory_enabled = false;
bool VK_EXT_buffer_device_address_enabled = false;
bool VK_KHR_buffer_device_address_enabled = false;
bool VK_EXT_memory_priority_enabled = false;
bool VK_EXT_debug_utils_enabled = false;
bool g_SparseBindingEnabled = false;
bool g_BufferDeviceAddressEnabled = false;

// # Pointers to functions from extensions
PFN_vkGetBufferDeviceAddressEXT g_vkGetBufferDeviceAddressEXT;
PFN_vkGetBufferDeviceAddressKHR g_vkGetBufferDeviceAddressKHR;

static HINSTANCE g_hAppInstance;
static HWND g_hWnd;
Expand Down Expand Up @@ -1131,7 +1129,6 @@ static void PrintEnabledFeatures()
wprintf(L"Enabled extensions and features:\n");
wprintf(L"Validation layer: %d\n", g_EnableValidationLayer ? 1 : 0);
wprintf(L"Sparse binding: %d\n", g_SparseBindingEnabled ? 1 : 0);
wprintf(L"Buffer device address: %d\n", g_BufferDeviceAddressEnabled ? 1 : 0);
if(GetVulkanApiVersion() == VK_API_VERSION_1_0)
{
wprintf(L"VK_KHR_get_memory_requirements2: %d\n", VK_KHR_get_memory_requirements2_enabled ? 1 : 0);
Expand All @@ -1141,8 +1138,14 @@ static void PrintEnabledFeatures()
}
wprintf(L"VK_EXT_memory_budget: %d\n", VK_EXT_memory_budget_enabled ? 1 : 0);
wprintf(L"VK_AMD_device_coherent_memory: %d\n", VK_AMD_device_coherent_memory_enabled ? 1 : 0);
wprintf(L"VK_KHR_buffer_device_address: %d\n", VK_KHR_buffer_device_address_enabled ? 1 : 0);
wprintf(L"VK_EXT_buffer_device_address: %d\n", VK_EXT_buffer_device_address_enabled ? 1 : 0);
if(GetVulkanApiVersion() < VK_API_VERSION_1_2)
{
wprintf(L"VK_KHR_buffer_device_address: %d\n", VK_KHR_buffer_device_address_enabled ? 1 : 0);
}
else
{
wprintf(L"bufferDeviceAddress: %d\n", VK_KHR_buffer_device_address_enabled ? 1 : 0);
}
wprintf(L"VK_EXT_memory_priority: %d\n", VK_EXT_memory_priority ? 1 : 0);
}

Expand Down Expand Up @@ -1174,7 +1177,7 @@ void SetAllocatorCreateInfo(VmaAllocatorCreateInfo& outInfo)
{
outInfo.flags |= VMA_ALLOCATOR_CREATE_AMD_DEVICE_COHERENT_MEMORY_BIT;
}
if(g_BufferDeviceAddressEnabled)
if(VK_KHR_buffer_device_address_enabled)
{
outInfo.flags |= VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT;
}
Expand Down Expand Up @@ -1469,7 +1472,7 @@ static void PrintMemoryConclusions()
if(deviceLocalAndHostVisibleHeapCount == heapCount)
wprintf(L"- All heaps are DEVICE_LOCAL and HOST_VISIBLE.\n");
else
wprintf(L"- %u heaps are DEVICE_LOCAL and HOST_VISIBLE, total %s.\n", deviceLocalHeapCount, SizeToStr(deviceLocalAndHostVisibleHeapSumSize).c_str());
wprintf(L"- %u heaps are DEVICE_LOCAL and HOST_VISIBLE, total %s.\n", deviceLocalAndHostVisibleHeapCount, SizeToStr(deviceLocalAndHostVisibleHeapSumSize).c_str());
}

if(hostVisibleNotHostCoherentTypeCount == 0)
Expand Down Expand Up @@ -1672,19 +1675,12 @@ static void InitializeApplication()
VK_KHR_buffer_device_address_enabled = true;
}
}
else if(strcmp(physicalDeviceExtensionProperties[i].extensionName, VK_EXT_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME) == 0)
{
if(GetVulkanApiVersion() < VK_API_VERSION_1_2)
{
VK_EXT_buffer_device_address_enabled = true;
}
}
else if(strcmp(physicalDeviceExtensionProperties[i].extensionName, VK_EXT_MEMORY_PRIORITY_EXTENSION_NAME) == 0)
VK_EXT_memory_priority_enabled = true;
}

if(VK_EXT_buffer_device_address_enabled && VK_KHR_buffer_device_address_enabled)
VK_EXT_buffer_device_address_enabled = false;
if(GetVulkanApiVersion() >= VK_API_VERSION_1_2)
VK_KHR_buffer_device_address_enabled = true; // Promoted to core Vulkan 1.2.

// Query for features

Expand Down Expand Up @@ -1724,8 +1720,8 @@ static void InitializeApplication()
PnextChainPushFront(&physicalDeviceFeatures, &physicalDeviceCoherentMemoryFeatures);
}

VkPhysicalDeviceBufferDeviceAddressFeaturesEXT physicalDeviceBufferDeviceAddressFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES_EXT };
if(VK_KHR_buffer_device_address_enabled || VK_EXT_buffer_device_address_enabled || GetVulkanApiVersion() >= VK_API_VERSION_1_2)
VkPhysicalDeviceBufferDeviceAddressFeaturesKHR physicalDeviceBufferDeviceAddressFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES_KHR };
if(VK_KHR_buffer_device_address_enabled)
{
PnextChainPushFront(&physicalDeviceFeatures, &physicalDeviceBufferDeviceAddressFeatures);
}
Expand All @@ -1743,8 +1739,8 @@ static void InitializeApplication()
// The extension is supported as fake with no real support for this feature? Don't use it.
if(VK_AMD_device_coherent_memory_enabled && !physicalDeviceCoherentMemoryFeatures.deviceCoherentMemory)
VK_AMD_device_coherent_memory_enabled = false;
if(VK_KHR_buffer_device_address_enabled || VK_EXT_buffer_device_address_enabled || GetVulkanApiVersion() >= VK_API_VERSION_1_2)
g_BufferDeviceAddressEnabled = physicalDeviceBufferDeviceAddressFeatures.bufferDeviceAddress != VK_FALSE;
if(VK_KHR_buffer_device_address_enabled && !physicalDeviceBufferDeviceAddressFeatures.bufferDeviceAddress)
VK_KHR_buffer_device_address_enabled = false;
if(VK_EXT_memory_priority_enabled && !physicalDeviceMemoryPriorityFeatures.memoryPriority)
VK_EXT_memory_priority_enabled = false;

Expand Down Expand Up @@ -1835,10 +1831,8 @@ static void InitializeApplication()
enabledDeviceExtensions.push_back(VK_EXT_MEMORY_BUDGET_EXTENSION_NAME);
if(VK_AMD_device_coherent_memory_enabled)
enabledDeviceExtensions.push_back(VK_AMD_DEVICE_COHERENT_MEMORY_EXTENSION_NAME);
if(VK_KHR_buffer_device_address_enabled)
if(VK_KHR_buffer_device_address_enabled && GetVulkanApiVersion() < VK_API_VERSION_1_2)
enabledDeviceExtensions.push_back(VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME);
if(VK_EXT_buffer_device_address_enabled)
enabledDeviceExtensions.push_back(VK_EXT_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME);
if(VK_EXT_memory_priority_enabled)
enabledDeviceExtensions.push_back(VK_EXT_MEMORY_PRIORITY_EXTENSION_NAME);

Expand All @@ -1851,9 +1845,9 @@ static void InitializeApplication()
physicalDeviceCoherentMemoryFeatures.deviceCoherentMemory = VK_TRUE;
PnextChainPushBack(&deviceFeatures, &physicalDeviceCoherentMemoryFeatures);
}
if(g_BufferDeviceAddressEnabled)
if(VK_KHR_buffer_device_address_enabled)
{
physicalDeviceBufferDeviceAddressFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES_EXT };
physicalDeviceBufferDeviceAddressFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES_KHR };
physicalDeviceBufferDeviceAddressFeatures.bufferDeviceAddress = VK_TRUE;
PnextChainPushBack(&deviceFeatures, &physicalDeviceBufferDeviceAddressFeatures);
}
Expand All @@ -1874,48 +1868,17 @@ static void InitializeApplication()
ERR_GUARD_VULKAN( vkCreateDevice(g_hPhysicalDevice, &deviceCreateInfo, g_Allocs, &g_hDevice) );

// Fetch pointers to extension functions
if(g_BufferDeviceAddressEnabled)
if(VK_KHR_buffer_device_address_enabled)
{
if(GetVulkanApiVersion() >= VK_API_VERSION_1_2)
{
g_vkGetBufferDeviceAddressEXT = (PFN_vkGetBufferDeviceAddressEXT)vkGetDeviceProcAddr(g_hDevice, "vkGetBufferDeviceAddress");
//assert(g_vkGetBufferDeviceAddressEXT != nullptr);
/*
For some reason this doesn't work, the pointer is NULL :( None of the below methods help.
Validation layers also report following error:
[ VUID-VkMemoryAllocateInfo-flags-03331 ] Object: VK_NULL_HANDLE (Type = 0) | If VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT_KHR is set, bufferDeviceAddress must be enabled. The Vulkan spec states: If VkMemoryAllocateFlagsInfo::flags includes VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT, the bufferDeviceAddress feature must be enabled (https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#VUID-VkMemoryAllocateInfo-flags-03331)
Despite I'm posting VkPhysicalDeviceBufferDeviceAddressFeaturesEXT::bufferDeviceAddress = VK_TRUE in VkDeviceCreateInfo::pNext chain.
if(g_vkGetBufferDeviceAddressEXT == nullptr)
{
g_vkGetBufferDeviceAddressEXT = &vkGetBufferDeviceAddress; // Doesn't run, cannot find entry point...
}
if(g_vkGetBufferDeviceAddressEXT == nullptr)
{
g_vkGetBufferDeviceAddressEXT = (PFN_vkGetBufferDeviceAddressEXT)vkGetInstanceProcAddr(g_hVulkanInstance, "vkGetBufferDeviceAddress");
}
if(g_vkGetBufferDeviceAddressEXT == nullptr)
{
g_vkGetBufferDeviceAddressEXT = (PFN_vkGetBufferDeviceAddressEXT)vkGetDeviceProcAddr(g_hDevice, "vkGetBufferDeviceAddressKHR");
}
if(g_vkGetBufferDeviceAddressEXT == nullptr)
{
g_vkGetBufferDeviceAddressEXT = (PFN_vkGetBufferDeviceAddressEXT)vkGetDeviceProcAddr(g_hDevice, "vkGetBufferDeviceAddressEXT");
}
*/
g_vkGetBufferDeviceAddressKHR = (PFN_vkGetBufferDeviceAddressEXT)vkGetDeviceProcAddr(g_hDevice, "vkGetBufferDeviceAddress");
}
else if(VK_KHR_buffer_device_address_enabled)
{
g_vkGetBufferDeviceAddressEXT = (PFN_vkGetBufferDeviceAddressEXT)vkGetDeviceProcAddr(g_hDevice, "vkGetBufferDeviceAddressKHR");
assert(g_vkGetBufferDeviceAddressEXT != nullptr);
}
else if(VK_EXT_buffer_device_address_enabled)
{
g_vkGetBufferDeviceAddressEXT = (PFN_vkGetBufferDeviceAddressEXT)vkGetDeviceProcAddr(g_hDevice, "vkGetBufferDeviceAddressEXT");
assert(g_vkGetBufferDeviceAddressEXT != nullptr);
g_vkGetBufferDeviceAddressKHR = (PFN_vkGetBufferDeviceAddressEXT)vkGetDeviceProcAddr(g_hDevice, "vkGetBufferDeviceAddressKHR");
}
assert(g_vkGetBufferDeviceAddressKHR != nullptr);
}

// Create memory allocator
Expand Down

0 comments on commit 0a3c6b5

Please sign in to comment.