Skip to content

Commit 91fd20f

Browse files
committed
Update based on changes to main
This refactores handling of extensions and layers to support variable arrays of extensions and layers based on querying for availability.
1 parent 2551a64 commit 91fd20f

File tree

1 file changed

+49
-10
lines changed

1 file changed

+49
-10
lines changed

lib/API/VK/Device.cpp

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ class VKDevice : public offloadtest::Device {
163163
Capabilities Caps;
164164
using LayerVector = std::vector<VkLayerProperties>;
165165
LayerVector Layers;
166+
using ExtensionVector = std::vector<VkExtensionProperties>;
167+
ExtensionVector Extensions;
166168

167169
struct BufferRef {
168170
VkBuffer Buffer;
@@ -226,6 +228,20 @@ class VKDevice : public offloadtest::Device {
226228
return false;
227229
}
228230

231+
const ExtensionVector &getExtensions() {
232+
if (Extensions.empty())
233+
queryExtensions();
234+
return Extensions;
235+
}
236+
237+
bool isExtensionSupported(llvm::StringRef QueryName) {
238+
for (const auto &Ext : getExtensions()) {
239+
if (Ext.extensionName == QueryName)
240+
return true;
241+
}
242+
return false;
243+
}
244+
229245
void printExtra(llvm::raw_ostream &OS) override {
230246
OS << " Layers:\n";
231247
for (auto Layer : getLayers()) {
@@ -236,6 +252,12 @@ class VKDevice : public offloadtest::Device {
236252
Sz = strnlen(Layer.description, VK_MAX_DESCRIPTION_SIZE);
237253
OS << " LayerDesc: " << llvm::StringRef(Layer.description, Sz) << "\n";
238254
}
255+
256+
OS << " Extensions:\n";
257+
for (const auto &Ext : getExtensions()) {
258+
OS << " - ExtensionName: " << llvm::StringRef(Ext.extensionName) << "\n";
259+
OS << " SpecVersion: " << Ext.specVersion << "\n";
260+
}
239261
}
240262

241263
const VkPhysicalDeviceProperties &getProps() const { return Props; }
@@ -301,6 +323,19 @@ class VKDevice : public offloadtest::Device {
301323
vkEnumerateInstanceLayerProperties(&LayerCount, Layers.data());
302324
}
303325

326+
void queryExtensions() {
327+
assert(Extensions.empty() && "Should not be called twice!");
328+
uint32_t ExtCount;
329+
vkEnumerateDeviceExtensionProperties(Device, nullptr, &ExtCount, nullptr);
330+
331+
if (ExtCount == 0)
332+
return;
333+
334+
Extensions.insert(Extensions.begin(), ExtCount, VkExtensionProperties());
335+
vkEnumerateDeviceExtensionProperties(Device, nullptr, &ExtCount,
336+
Extensions.data());
337+
}
338+
304339
public:
305340
llvm::Error createDevice(InvocationState &IS) {
306341

@@ -907,6 +942,7 @@ class VKContext {
907942
CreateInfo.pApplicationInfo = &AppInfo;
908943

909944
llvm::SmallVector<const char *> Extensions;
945+
llvm::SmallVector<const char *> Layers;
910946
#if __APPLE__
911947
Extensions.push_back(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME);
912948
CreateInfo.flags |= VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR;
@@ -936,20 +972,23 @@ class VKContext {
936972
{
937973
auto TmpDev = std::make_shared<VKDevice>(PhysicalDevicesTmp[0]);
938974
AppInfo.apiVersion = TmpDev->getProps().apiVersion;
939-
}
940-
vkDestroyInstance(Instance, NULL);
941-
Instance = VK_NULL_HANDLE;
942975

943-
// TODO: This is a bit hacky but matches what I did in DX.
944976
#ifndef NDEBUG
945-
const char *ValidationLayer = "VK_LAYER_KHRONOS_validation";
946-
CreateInfo.ppEnabledLayerNames = &ValidationLayer;
947-
CreateInfo.enabledLayerCount = 1;
977+
const llvm::StringRef ValidationLayer = "VK_LAYER_KHRONOS_validation";
978+
if (TmpDev->isLayerSupported(ValidationLayer))
979+
Layers.push_back(ValidationLayer.data());
948980

949-
const char *DebugUtilsExtensionName = "VK_EXT_debug_utils";
950-
CreateInfo.ppEnabledExtensionNames = &DebugUtilsExtensionName;
951-
CreateInfo.enabledExtensionCount = 1;
981+
const llvm::StringRef DebugUtilsExtensionName = "VK_EXT_debug_utils";
982+
if (TmpDev->isExtensionSupported(DebugUtilsExtensionName))
983+
Extensions.push_back(DebugUtilsExtensionName.data());
952984
#endif
985+
CreateInfo.ppEnabledLayerNames = Layers.data();
986+
CreateInfo.enabledLayerCount = Layers.size();
987+
CreateInfo.ppEnabledExtensionNames = Extensions.data();
988+
CreateInfo.enabledExtensionCount = Extensions.size();
989+
}
990+
vkDestroyInstance(Instance, NULL);
991+
Instance = VK_NULL_HANDLE;
953992

954993
// This second creation shouldn't ever fail, but it tries to create the
955994
// highest supported device version.

0 commit comments

Comments
 (0)