Vulkan: Use VK_KHR_DEDICATED_ALLOCATION for frame buffers for minor speedup on some GPUs. Check for the debug report extension before enabling it.

This commit is contained in:
Henrik Rydgård 2017-12-30 21:31:43 +01:00
parent 25a9573f8b
commit b9726245ac
3 changed files with 28 additions and 6 deletions

View File

@ -125,11 +125,16 @@ VkResult VulkanContext::CreateInstance(const CreateInfo &info) {
#endif
if (flags_ & VULKAN_FLAG_VALIDATE) {
for (size_t i = 0; i < ARRAY_SIZE(validationLayers); i++) {
instance_layer_names_.push_back(validationLayers[i]);
device_layer_names_.push_back(validationLayers[i]);
if (IsInstanceExtensionAvailable(VK_EXT_DEBUG_REPORT_EXTENSION_NAME)) {
for (size_t i = 0; i < ARRAY_SIZE(validationLayers); i++) {
instance_layer_names_.push_back(validationLayers[i]);
device_layer_names_.push_back(validationLayers[i]);
}
instance_extensions_enabled_.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
} else {
ELOG("Validation layer extension not available - not enabling Vulkan validation.");
flags_ &= ~VULKAN_FLAG_VALIDATE;
}
instance_extensions_enabled_.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
}
// Validate that all the instance extensions we ask for are actually available.
@ -550,6 +555,8 @@ VkResult VulkanContext::CreateDevice() {
}
assert(found);
deviceExtensionsLookup_.DEDICATED_ALLOCATION = EnableDeviceExtension(VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME);
VkDeviceCreateInfo device_info { VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO };
device_info.queueCreateInfoCount = 1;
device_info.pQueueCreateInfos = &queue_info;

View File

@ -104,6 +104,11 @@ private:
std::vector<Callback> callbacks_;
};
// For fast extension-enabled checks.
struct VulkanDeviceExtensions {
bool DEDICATED_ALLOCATION;
};
// VulkanContext manages the device and swapchain, and deferred deletion of objects.
class VulkanContext {
public:
@ -233,6 +238,8 @@ public:
MAX_INFLIGHT_FRAMES = 3,
};
const VulkanDeviceExtensions &DeviceExtensions() { return deviceExtensionsLookup_; }
private:
// A layer can expose extensions, keep track of those extensions here.
struct LayerProperties {
@ -265,6 +272,7 @@ private:
std::vector<const char *> device_extensions_enabled_;
std::vector<VkExtensionProperties> device_extension_properties_;
VulkanDeviceExtensions deviceExtensionsLookup_{};
std::vector<VkPhysicalDevice> physical_devices_;

View File

@ -38,13 +38,20 @@ void CreateImage(VulkanContext *vulkan, VkCommandBuffer cmd, VKRImage &img, int
vkCreateImage(vulkan->GetDevice(), &ici, nullptr, &img.image);
// TODO: If available, use nVidia's VK_NV_dedicated_allocation for framebuffers
VkMemoryRequirements memreq;
vkGetImageMemoryRequirements(vulkan->GetDevice(), img.image, &memreq);
VkMemoryAllocateInfo alloc{ VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO };
alloc.allocationSize = memreq.size;
// Hint to the driver that this allocation is image-specific. Some drivers benefit.
// We only bother supporting the KHR extension, not the old NV one.
VkMemoryDedicatedAllocateInfoKHR dedicated{ VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR };
if (vulkan->DeviceExtensions().DEDICATED_ALLOCATION) {
alloc.pNext = &dedicated;
dedicated.image = img.image;
}
vulkan->MemoryTypeFromProperties(memreq.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &alloc.memoryTypeIndex);
VkResult res = vkAllocateMemory(vulkan->GetDevice(), &alloc, nullptr, &img.memory);
assert(res == VK_SUCCESS);