Skip to content

Commit baa6ac6

Browse files
committed
Vulkan present mode override for dxvk/vkd3d-proton swapchains (D3D games under Proton), live from the menu
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016GbUfLURPGYGYjaYFdWw5X
1 parent 4bd7a77 commit baa6ac6

2 files changed

Lines changed: 104 additions & 8 deletions

File tree

‎OptiScaler/hooks/Vulkan_Hooks.cpp‎

Lines changed: 98 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ static VkDevice _fifoLatestReadyDevice = VK_NULL_HANDLE;
100100
// The game's swapchain, its presents ask the game to recreate it when the present mode override changes
101101
static VkSwapchainKHR _gameSwapchain = VK_NULL_HANDLE;
102102

103+
// Present mode override for the swapchains dxvk/vkd3d-proton create for D3D games under Proton
104+
static VkPhysicalDevice _dxgiPD = VK_NULL_HANDLE;
105+
static VkSwapchainKHR _dxgiSwapchain = VK_NULL_HANDLE;
106+
static VkPresentModeKHR _dxgiForcedMode = VK_PRESENT_MODE_MAX_ENUM_KHR;
107+
static VkPresentModeKHR _dxgiForcedModes[8] {};
108+
103109
// Set while the FG output handles a swapchain call
104110
static thread_local bool _inFGCall = false;
105111

@@ -810,6 +816,12 @@ static VkResult hkvkCreateDevice(VkPhysicalDevice physicalDevice, const VkDevice
810816
LOG_DEBUG("_device captured: {0:X}", (UINT64) _device);
811817
MenuOverlayVk::DeviceCreated(_device, &localCreateInfo);
812818
}
819+
else
820+
{
821+
// The D3D device's physical device, for the present mode override of its swapchain
822+
_dxgiPD = physicalDevice;
823+
LOG_DEBUG("_dxgiPD captured: {0:X}", (UINT64) _dxgiPD);
824+
}
813825
}
814826

815827
ScopedSkipSpoofingGlobal skipSpoofingGlobal {};
@@ -973,6 +985,31 @@ static VkResult hkvkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPres
973985
fg->Present();
974986
}
975987

988+
// dxvk/vkd3d-proton switch present modes per present (VK_EXT_swapchain_maintenance1), keep the forced one
989+
VkSwapchainPresentModeInfoEXT* dxgiModeInfo = nullptr;
990+
const VkPresentModeKHR* dxgiModesOld = nullptr;
991+
if (dxgiBacked && _dxgiForcedMode != VK_PRESENT_MODE_MAX_ENUM_KHR)
992+
{
993+
for (auto next = (VkBaseOutStructure*) localPresentInfo.pNext; next != nullptr; next = next->pNext)
994+
{
995+
if (next->sType != VK_STRUCTURE_TYPE_SWAPCHAIN_PRESENT_MODE_INFO_EXT)
996+
continue;
997+
998+
auto info = (VkSwapchainPresentModeInfoEXT*) next;
999+
if (info->swapchainCount > std::size(_dxgiForcedModes) || info->pPresentModes == nullptr)
1000+
break;
1001+
1002+
for (uint32_t i = 0; i < info->swapchainCount; i++)
1003+
_dxgiForcedModes[i] =
1004+
localPresentInfo.pSwapchains[i] == _dxgiSwapchain ? _dxgiForcedMode : info->pPresentModes[i];
1005+
1006+
dxgiModeInfo = info;
1007+
dxgiModesOld = info->pPresentModes;
1008+
info->pPresentModes = _dxgiForcedModes;
1009+
break;
1010+
}
1011+
}
1012+
9761013
// original call
9771014
ScopedVulkanCreatingSC scopedVulkanCreatingSC {};
9781015
VkResult result;
@@ -987,6 +1024,9 @@ static VkResult hkvkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPres
9871024
result = o_QueuePresentKHR(queue, &localPresentInfo);
9881025
}
9891026

1027+
if (dxgiModeInfo != nullptr)
1028+
dxgiModeInfo->pPresentModes = dxgiModesOld;
1029+
9901030
// dxvk/vkd3d-proton's are counted by the DXGI swapchain
9911031
if (!dxgiBacked)
9921032
DeferredRelease::Process();
@@ -1005,6 +1045,16 @@ static VkResult hkvkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPres
10051045
result = VK_SUBOPTIMAL_KHR;
10061046
}
10071047

1048+
// Same for dxvk/vkd3d-proton, they recreate their swapchain on suboptimal
1049+
if (State::Instance().vkPresentModeChanged && result == VK_SUCCESS && dxgiBacked &&
1050+
_dxgiSwapchain != VK_NULL_HANDLE &&
1051+
std::find(localPresentInfo.pSwapchains, localPresentInfo.pSwapchains + localPresentInfo.swapchainCount,
1052+
_dxgiSwapchain) != localPresentInfo.pSwapchains + localPresentInfo.swapchainCount)
1053+
{
1054+
LOG_TRACE("Present mode override changed, asking dxvk/vkd3d-proton to recreate their swapchain");
1055+
result = VK_SUBOPTIMAL_KHR;
1056+
}
1057+
10081058
LOG_FUNC_RESULT(result);
10091059
return result;
10101060
}
@@ -1060,19 +1110,28 @@ static VkResult hkvkCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateI
10601110
}
10611111
}
10621112

1063-
// Present mode override, the FG outputs create their swapchains from this create info too
1064-
if (pCreateInfo != nullptr && !State::Instance().vulkanSkipHooks && !dxgiBacked && _PD != VK_NULL_HANDLE)
1113+
// Present mode override, the FG outputs create their swapchains from this create info too. dxvk/vkd3d-proton's
1114+
// (D3D games under Proton) use the D3D device's physical device
1115+
const VkPhysicalDevice modePD = dxgiBacked ? _dxgiPD : _PD;
1116+
VkSwapchainPresentModesCreateInfoEXT* dxgiModeList = nullptr;
1117+
uint32_t dxgiModeListCount = 0;
1118+
const VkPresentModeKHR* dxgiModeListModes = nullptr;
1119+
static VkPresentModeKHR dxgiListMode = VK_PRESENT_MODE_FIFO_KHR;
1120+
if (dxgiBacked)
1121+
_dxgiForcedMode = VK_PRESENT_MODE_MAX_ENUM_KHR;
1122+
1123+
if (pCreateInfo != nullptr && !State::Instance().vulkanSkipHooks && modePD != VK_NULL_HANDLE)
10651124
{
10661125
auto& state = State::Instance();
10671126
state.vkGamePresentMode = pCreateInfo->presentMode;
10681127

10691128
uint32_t count = 0;
10701129
std::vector<VkPresentModeKHR> supported;
10711130

1072-
if (vkGetPhysicalDeviceSurfacePresentModesKHR(_PD, pCreateInfo->surface, &count, nullptr) == VK_SUCCESS)
1131+
if (vkGetPhysicalDeviceSurfacePresentModesKHR(modePD, pCreateInfo->surface, &count, nullptr) == VK_SUCCESS)
10731132
{
10741133
supported.resize(count);
1075-
vkGetPhysicalDeviceSurfacePresentModesKHR(_PD, pCreateInfo->surface, &count, supported.data());
1134+
vkGetPhysicalDeviceSurfacePresentModesKHR(modePD, pCreateInfo->surface, &count, supported.data());
10761135
supported.resize(count);
10771136
}
10781137

@@ -1096,7 +1155,7 @@ static VkResult hkvkCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateI
10961155
for (auto next = (const VkBaseInStructure*) pCreateInfo->pNext; next != nullptr; next = next->pNext)
10971156
gameModeList |= next->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_PRESENT_MODES_CREATE_INFO_EXT;
10981157

1099-
if (const auto& mode = Config::Instance()->VulkanPresentMode; mode.has_value() && !gameModeList)
1158+
if (const auto& mode = Config::Instance()->VulkanPresentMode; mode.has_value() && (!gameModeList || dxgiBacked))
11001159
{
11011160
const auto entry = std::ranges::find(VulkanHooks::PresentModes, (VkPresentModeKHR) mode.value(),
11021161
&VulkanHooks::PresentModeEntry::mode);
@@ -1113,6 +1172,26 @@ static VkResult hkvkCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateI
11131172
}
11141173

11151174
localCreateInfo.presentMode = entry->mode;
1175+
1176+
if (dxgiBacked)
1177+
{
1178+
_dxgiForcedMode = entry->mode;
1179+
1180+
// dxvk/vkd3d-proton's present mode list becomes the forced mode only, restored after the call
1181+
for (auto next = (VkBaseOutStructure*) pCreateInfo->pNext; next != nullptr; next = next->pNext)
1182+
{
1183+
if (next->sType != VK_STRUCTURE_TYPE_SWAPCHAIN_PRESENT_MODES_CREATE_INFO_EXT)
1184+
continue;
1185+
1186+
dxgiModeList = (VkSwapchainPresentModesCreateInfoEXT*) next;
1187+
dxgiModeListCount = dxgiModeList->presentModeCount;
1188+
dxgiModeListModes = dxgiModeList->pPresentModes;
1189+
dxgiListMode = entry->mode;
1190+
dxgiModeList->presentModeCount = 1;
1191+
dxgiModeList->pPresentModes = &dxgiListMode;
1192+
break;
1193+
}
1194+
}
11161195
}
11171196
else
11181197
{
@@ -1189,6 +1268,20 @@ static VkResult hkvkCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateI
11891268
if (result == VK_SUCCESS && lowLatencySwapchain)
11901269
VulkanHooks::SetLowLatencySwapchain(*pSwapchain);
11911270

1271+
if (dxgiModeList != nullptr)
1272+
{
1273+
dxgiModeList->presentModeCount = dxgiModeListCount;
1274+
dxgiModeList->pPresentModes = dxgiModeListModes;
1275+
}
1276+
1277+
if (result == VK_SUCCESS && dxgiBacked && pSwapchain != nullptr && *pSwapchain != VK_NULL_HANDLE &&
1278+
!State::Instance().vulkanSkipHooks)
1279+
{
1280+
_dxgiSwapchain = *pSwapchain;
1281+
State::Instance().vkPresentModeChanged = false;
1282+
LOG_DEBUG("dxvk/vkd3d-proton swapchain: {0:X}", (UINT64) _dxgiSwapchain);
1283+
}
1284+
11921285
if (result == VK_SUCCESS && device != VK_NULL_HANDLE && pCreateInfo != nullptr && *pSwapchain != VK_NULL_HANDLE &&
11931286
!State::Instance().vulkanSkipHooks && !dxgiBacked)
11941287
{

‎OptiScaler/menu/menu_common.cpp‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9370,11 +9370,14 @@ void MenuCommon::RenderVsyncSettings(RenderMenuContext& ctx)
93709370
}
93719371
}
93729372
}
9373-
else
9373+
9374+
// Vulkan games, and D3D games under Proton once dxvk/vkd3d-proton's swapchain is seen (its present mode list)
9375+
if (state.swapchainApi == Vulkan || state.vkPresentModeMask != 0)
93749376
{
9375-
SectionTitle("V-Sync Settings");
9377+
const bool underProton = state.swapchainApi != Vulkan;
9378+
SectionTitle(underProton ? "Present Mode (Proton)" : "V-Sync Settings");
93769379
{
9377-
ScopedID sectionId { "V-Sync Settings" };
9380+
ScopedID sectionId { "Vulkan Present Mode" };
93789381

93799382
const auto& forced = config->VulkanPresentMode;
93809383
const char* preview = forced.has_value() ? VulkanHooks::PresentModeName(forced.value()) : "Game's";

0 commit comments

Comments
 (0)