Skip to content

Commit

Permalink
[dxvk] Allow getting device creation info without creating device
Browse files Browse the repository at this point in the history
  • Loading branch information
AlpyneDreams committed Dec 4, 2024
1 parent 0a79f0e commit 1514d3d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 26 deletions.
67 changes: 42 additions & 25 deletions src/dxvk/dxvk_adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,16 +272,21 @@ namespace dxvk {
}


Rc<DxvkDevice> DxvkAdapter::createDevice(
const Rc<DxvkInstance>& instance,
DxvkDeviceFeatures enabledFeatures) {
DxvkDeviceExtensions devExtensions;
bool DxvkAdapter::getDeviceCreateInfo(
const Rc<DxvkInstance>& instance,
DxvkDeviceFeatures baseEnabledFeatures,
bool logDeviceInfo,
DxvkDeviceCreateInfo& createInfo) const {
auto& [info, queueFamilies, extensionsEnabled, extensionNameList, devExtensions, enableCudaInterop, queueInfos, enabledFeatures] = createInfo;

enabledFeatures = baseEnabledFeatures;

auto devExtensionList = getExtensionList(devExtensions);

// Only enable Cuda interop extensions in 64-bit builds in
// order to avoid potential driver or address space issues.
// VK_KHR_buffer_device_address is expensive on some drivers.
bool enableCudaInterop = !env::is32BitHostPlatform() &&
enableCudaInterop = !env::is32BitHostPlatform() &&
m_deviceExtensions.supports(devExtensions.nvxBinaryImport.name()) &&
m_deviceExtensions.supports(devExtensions.nvxImageViewHandle.name()) &&
m_deviceFeatures.vk12.bufferDeviceAddress;
Expand All @@ -298,17 +303,15 @@ namespace dxvk {
if (!m_deviceExtensions.supports(devExtensions.extPageableDeviceLocalMemory.name()))
devExtensions.amdMemoryOverallocationBehaviour.setMode(DxvkExtMode::Optional);

DxvkNameSet extensionsEnabled;

if (!m_deviceExtensions.enableExtensions(
devExtensionList.size(),
devExtensionList.data(),
&extensionsEnabled))
throw DxvkError("DxvkAdapter: Failed to create device");
return false;

// Enable additional extensions if necessary
extensionsEnabled.merge(m_extraExtensions);
DxvkNameList extensionNameList = extensionsEnabled.toNameList();
extensionNameList = extensionsEnabled.toNameList();

// Always enable robust buffer access
enabledFeatures.core.features.robustBufferAccess = VK_TRUE;
Expand Down Expand Up @@ -443,33 +446,34 @@ namespace dxvk {
// Create pNext chain for additional device features
initFeatureChain(enabledFeatures, devExtensions, instance->extensions());

// Log feature support info an extension list
Logger::info(str::format("Device properties:"
"\n Device : ", m_deviceInfo.core.properties.deviceName,
"\n Driver : ", m_deviceInfo.vk12.driverName, " ", m_deviceInfo.driverVersion.toString()));
if (logDeviceInfo) {
// Log feature support info an extension list
Logger::info(str::format("Device properties:"
"\n Device : ", m_deviceInfo.core.properties.deviceName,
"\n Driver : ", m_deviceInfo.vk12.driverName, " ", m_deviceInfo.driverVersion.toString()));

Logger::info("Enabled device extensions:");
this->logNameList(extensionNameList);
this->logFeatures(enabledFeatures);
Logger::info("Enabled device extensions:");
this->logNameList(extensionNameList);
this->logFeatures(enabledFeatures);
}

// Report the desired overallocation behaviour to the driver
VkDeviceMemoryOverallocationCreateInfoAMD overallocInfo = { VK_STRUCTURE_TYPE_DEVICE_MEMORY_OVERALLOCATION_CREATE_INFO_AMD };
overallocInfo.overallocationBehavior = VK_MEMORY_OVERALLOCATION_BEHAVIOR_ALLOWED_AMD;

// Create the requested queues
float queuePriority = 1.0f;
std::vector<VkDeviceQueueCreateInfo> queueInfos;

std::unordered_set<uint32_t> queueFamiliySet;

DxvkAdapterQueueIndices queueFamilies = findQueueFamilies();
queueFamilies = findQueueFamilies();
queueFamiliySet.insert(queueFamilies.graphics);
queueFamiliySet.insert(queueFamilies.transfer);

if (queueFamilies.sparse != VK_QUEUE_FAMILY_IGNORED)
queueFamiliySet.insert(queueFamilies.sparse);

this->logQueueFamilies(queueFamilies);
if (logDeviceInfo)
this->logQueueFamilies(queueFamilies);

for (uint32_t family : queueFamiliySet) {
VkDeviceQueueCreateInfo graphicsQueue = { VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO };
Expand All @@ -479,7 +483,8 @@ namespace dxvk {
queueInfos.push_back(graphicsQueue);
}

VkDeviceCreateInfo info = { VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, enabledFeatures.core.pNext };
info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
info.pNext = enabledFeatures.core.pNext;
info.queueCreateInfoCount = queueInfos.size();
info.pQueueCreateInfos = queueInfos.data();
info.enabledExtensionCount = extensionNameList.count();
Expand All @@ -488,7 +493,19 @@ namespace dxvk {

if (devExtensions.amdMemoryOverallocationBehaviour)
overallocInfo.pNext = std::exchange(info.pNext, &overallocInfo);


return true;
}

Rc<DxvkDevice> DxvkAdapter::createDevice(
const Rc<DxvkInstance> &instance,
DxvkDeviceFeatures enabledFeatures) {
DxvkDeviceCreateInfo createInfo;
if (!getDeviceCreateInfo(instance, enabledFeatures, true, createInfo))
throw DxvkError("DxvkAdapter: Failed to create device");

auto& [info, queueFamilies, extensionsEnabled, extensionNameList, devExtensions, enableCudaInterop, queueInfos, features] = createInfo;

VkDevice device = VK_NULL_HANDLE;
VkResult vr = m_vki->vkCreateDevice(m_handle, &info, nullptr, &device);

Expand All @@ -501,7 +518,7 @@ namespace dxvk {
extensionsEnabled.disableExtension(devExtensions.nvxBinaryImport);
extensionsEnabled.disableExtension(devExtensions.nvxImageViewHandle);

enabledFeatures.vk12.bufferDeviceAddress = VK_FALSE;
features.vk12.bufferDeviceAddress = VK_FALSE;

extensionNameList = extensionsEnabled.toNameList();
info.enabledExtensionCount = extensionNameList.count();
Expand All @@ -520,7 +537,7 @@ namespace dxvk {
queues.transfer = getDeviceQueue(vkd, queueFamilies.transfer, 0);
queues.sparse = getDeviceQueue(vkd, queueFamilies.sparse, 0);

return new DxvkDevice(instance, this, vkd, enabledFeatures, queues, DxvkQueueCallback());
return new DxvkDevice(instance, this, vkd, features, queues, DxvkQueueCallback());
}


Expand Down Expand Up @@ -998,7 +1015,7 @@ namespace dxvk {


std::vector<DxvkExt*> DxvkAdapter::getExtensionList(
DxvkDeviceExtensions& devExtensions) {
DxvkDeviceExtensions& devExtensions) const {
return {{
&devExtensions.amdMemoryOverallocationBehaviour,
&devExtensions.amdShaderFragmentMask,
Expand Down
30 changes: 29 additions & 1 deletion src/dxvk/dxvk_adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ namespace dxvk {
std::atomic<uint64_t> used = { 0u };
};

/**
* \brief Device create info
*/
struct DxvkDeviceCreateInfo {
VkDeviceCreateInfo info;
DxvkAdapterQueueIndices queueFamilies;
DxvkNameSet extensionsEnabled;
DxvkNameList extensionNameList;
DxvkDeviceExtensions devExtensions;
bool enableCudaInterop;
std::vector<VkDeviceQueueCreateInfo> queueInfos;
DxvkDeviceFeatures features;
};

/**
* \brief Device import info
Expand Down Expand Up @@ -218,6 +231,21 @@ namespace dxvk {
void enableExtensions(
const DxvkNameSet& extensions);

/**
* \brief Gets device create info
*
* \param [in] instance Parent instance
* \param [in] enabledFeatures Device features
* \param [in] logDeviceInfo If true, prints device info
* \param [out] info Device create info
* \returns true if succeeded
*/
bool getDeviceCreateInfo(
const Rc<DxvkInstance>& instance,
DxvkDeviceFeatures enabledFeatures,
bool logDeviceInfo,
DxvkDeviceCreateInfo& createInfo) const;

/**
* \brief Creates a DXVK device
*
Expand Down Expand Up @@ -349,7 +377,7 @@ namespace dxvk {
VkQueueFlags flags) const;

std::vector<DxvkExt*> getExtensionList(
DxvkDeviceExtensions& devExtensions);
DxvkDeviceExtensions& devExtensions) const;

static void initFeatureChain(
DxvkDeviceFeatures& enabledFeatures,
Expand Down

0 comments on commit 1514d3d

Please sign in to comment.