Skip to content

Commit aeca5b9

Browse files
committed
Present mode override for D3D games (Proton and Windows)
The Present Mode option now also works for D3D games, live from the menu: - Proton: the swapchain dxvk/vkd3d-proton create gets the forced mode, also in their VkSwapchainPresentModesCreateInfoEXT list and in the per present VkSwapchainPresentModeInfoEXT (VK_EXT_swapchain_maintenance1). When the option changes, the present returns VK_SUBOPTIMAL_KHR so they recreate it. - Windows (native DXGI): FIFO is SyncInterval 1, Mailbox is SyncInterval 0 without tearing, Immediate is SyncInterval 0 with ALLOW_TEARING when the swapchain supports it. The menu shows only these three modes there. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016GbUfLURPGYGYjaYFdWw5X
1 parent 4bd7a77 commit aeca5b9

3 files changed

Lines changed: 139 additions & 10 deletions

File tree

‎OptiScaler/hooks/FG_Hooks.cpp‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,35 @@ HRESULT FGHooks::FGPresent(IDXGISwapChain* This, UINT SyncInterval, UINT Flags,
15391539
LOG_DEBUG("Final SyncInterval: {}", SyncInterval);
15401540
}
15411541

1542+
// Present mode on native D3D (no dxvk/vkd3d-proton swapchain underneath, Windows): FIFO is V-Sync, Mailbox is
1543+
// no V-Sync without tearing (the compositor shows the newest frame), Immediate is no V-Sync with tearing.
1544+
// Takes priority over ForceVsync, applies at the next present.
1545+
if (willPresent && config->VulkanPresentMode.has_value() && state.vkPresentModeMask == 0)
1546+
{
1547+
const int mode = config->VulkanPresentMode.value();
1548+
const int IMMEDIATE = 0, MAILBOX = 1, FIFO = 2;
1549+
1550+
if (mode == FIFO)
1551+
{
1552+
SyncInterval = 1;
1553+
Flags &= ~DXGI_PRESENT_ALLOW_TEARING;
1554+
}
1555+
else if (mode == MAILBOX)
1556+
{
1557+
SyncInterval = 0;
1558+
Flags &= ~DXGI_PRESENT_ALLOW_TEARING;
1559+
}
1560+
else if (mode == IMMEDIATE)
1561+
{
1562+
SyncInterval = 0;
1563+
1564+
DXGI_SWAP_CHAIN_DESC scDesc {};
1565+
if (This->GetDesc(&scDesc) == S_OK && (scDesc.Flags & DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING) != 0 &&
1566+
scDesc.Windowed)
1567+
Flags |= DXGI_PRESENT_ALLOW_TEARING;
1568+
}
1569+
}
1570+
15421571
// FSR-FG and XeFG with the Reflex output: the game's present as DLSSG reports it
15431572
ID3D12CommandQueue* gameQueue = nullptr;
15441573
uint32_t frameMultiplier = 1;

‎OptiScaler/hooks/Vulkan_Hooks.cpp‎

Lines changed: 95 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,11 @@ 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+
}
813824
}
814825

815826
ScopedSkipSpoofingGlobal skipSpoofingGlobal {};
@@ -973,6 +984,31 @@ static VkResult hkvkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPres
973984
fg->Present();
974985
}
975986

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

1026+
if (dxgiModeInfo != nullptr)
1027+
dxgiModeInfo->pPresentModes = dxgiModesOld;
1028+
9901029
// dxvk/vkd3d-proton's are counted by the DXGI swapchain
9911030
if (!dxgiBacked)
9921031
DeferredRelease::Process();
@@ -1005,6 +1044,15 @@ static VkResult hkvkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPres
10051044
result = VK_SUBOPTIMAL_KHR;
10061045
}
10071046

1047+
// Same for dxvk/vkd3d-proton, they recreate their swapchain on suboptimal
1048+
if (State::Instance().vkPresentModeChanged && result == VK_SUCCESS && dxgiBacked &&
1049+
_dxgiSwapchain != VK_NULL_HANDLE &&
1050+
std::find(localPresentInfo.pSwapchains, localPresentInfo.pSwapchains + localPresentInfo.swapchainCount,
1051+
_dxgiSwapchain) != localPresentInfo.pSwapchains + localPresentInfo.swapchainCount)
1052+
{
1053+
result = VK_SUBOPTIMAL_KHR;
1054+
}
1055+
10081056
LOG_FUNC_RESULT(result);
10091057
return result;
10101058
}
@@ -1060,19 +1108,28 @@ static VkResult hkvkCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateI
10601108
}
10611109
}
10621110

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)
1111+
// Present mode override, the FG outputs create their swapchains from this create info too. dxvk/vkd3d-proton's
1112+
// (D3D games under Proton) use the D3D device's physical device
1113+
const VkPhysicalDevice modePD = dxgiBacked ? _dxgiPD : _PD;
1114+
VkSwapchainPresentModesCreateInfoEXT* dxgiModeList = nullptr;
1115+
uint32_t dxgiModeListCount = 0;
1116+
const VkPresentModeKHR* dxgiModeListModes = nullptr;
1117+
static VkPresentModeKHR dxgiListMode = VK_PRESENT_MODE_FIFO_KHR;
1118+
if (dxgiBacked)
1119+
_dxgiForcedMode = VK_PRESENT_MODE_MAX_ENUM_KHR;
1120+
1121+
if (pCreateInfo != nullptr && !State::Instance().vulkanSkipHooks && modePD != VK_NULL_HANDLE)
10651122
{
10661123
auto& state = State::Instance();
10671124
state.vkGamePresentMode = pCreateInfo->presentMode;
10681125

10691126
uint32_t count = 0;
10701127
std::vector<VkPresentModeKHR> supported;
10711128

1072-
if (vkGetPhysicalDeviceSurfacePresentModesKHR(_PD, pCreateInfo->surface, &count, nullptr) == VK_SUCCESS)
1129+
if (vkGetPhysicalDeviceSurfacePresentModesKHR(modePD, pCreateInfo->surface, &count, nullptr) == VK_SUCCESS)
10731130
{
10741131
supported.resize(count);
1075-
vkGetPhysicalDeviceSurfacePresentModesKHR(_PD, pCreateInfo->surface, &count, supported.data());
1132+
vkGetPhysicalDeviceSurfacePresentModesKHR(modePD, pCreateInfo->surface, &count, supported.data());
10761133
supported.resize(count);
10771134
}
10781135

@@ -1096,7 +1153,7 @@ static VkResult hkvkCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateI
10961153
for (auto next = (const VkBaseInStructure*) pCreateInfo->pNext; next != nullptr; next = next->pNext)
10971154
gameModeList |= next->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_PRESENT_MODES_CREATE_INFO_EXT;
10981155

1099-
if (const auto& mode = Config::Instance()->VulkanPresentMode; mode.has_value() && !gameModeList)
1156+
if (const auto& mode = Config::Instance()->VulkanPresentMode; mode.has_value() && (!gameModeList || dxgiBacked))
11001157
{
11011158
const auto entry = std::ranges::find(VulkanHooks::PresentModes, (VkPresentModeKHR) mode.value(),
11021159
&VulkanHooks::PresentModeEntry::mode);
@@ -1113,6 +1170,26 @@ static VkResult hkvkCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateI
11131170
}
11141171

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

1269+
if (dxgiModeList != nullptr)
1270+
{
1271+
dxgiModeList->presentModeCount = dxgiModeListCount;
1272+
dxgiModeList->pPresentModes = dxgiModeListModes;
1273+
}
1274+
1275+
if (result == VK_SUCCESS && dxgiBacked && pSwapchain != nullptr && *pSwapchain != VK_NULL_HANDLE &&
1276+
!State::Instance().vulkanSkipHooks)
1277+
{
1278+
_dxgiSwapchain = *pSwapchain;
1279+
State::Instance().vkPresentModeChanged = false;
1280+
}
1281+
11921282
if (result == VK_SUCCESS && device != VK_NULL_HANDLE && pCreateInfo != nullptr && *pSwapchain != VK_NULL_HANDLE &&
11931283
!State::Instance().vulkanSkipHooks && !dxgiBacked)
11941284
{

‎OptiScaler/menu/menu_common.cpp‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9370,11 +9370,15 @@ void MenuCommon::RenderVsyncSettings(RenderMenuContext& ctx)
93709370
}
93719371
}
93729372
}
9373-
else
9373+
9374+
// Vulkan games, D3D games under Proton once dxvk/vkd3d-proton's swapchain is seen (its present mode list), and
9375+
// native D3D games (Windows), where FIFO, Mailbox and Immediate map to the DXGI present's sync interval and tearing
93749376
{
9375-
SectionTitle("V-Sync Settings");
9377+
const bool underProton = state.swapchainApi != Vulkan && state.vkPresentModeMask != 0;
9378+
const bool nativeDxgi = state.swapchainApi != Vulkan && state.vkPresentModeMask == 0;
9379+
SectionTitle(underProton ? "Present Mode (Proton)" : (nativeDxgi ? "Present Mode" : "V-Sync Settings"));
93769380
{
9377-
ScopedID sectionId { "V-Sync Settings" };
9381+
ScopedID sectionId { "Vulkan Present Mode" };
93789382

93799383
const auto& forced = config->VulkanPresentMode;
93809384
const char* preview = forced.has_value() ? VulkanHooks::PresentModeName(forced.value()) : "Game's";
@@ -9392,7 +9396,10 @@ void MenuCommon::RenderVsyncSettings(RenderMenuContext& ctx)
93929396
{
93939397
const auto& entry = VulkanHooks::PresentModes[i];
93949398

9395-
if ((state.vkPresentModeMask & (1u << i)) == 0)
9399+
if (nativeDxgi
9400+
? (entry.mode != VK_PRESENT_MODE_IMMEDIATE_KHR &&
9401+
entry.mode != VK_PRESENT_MODE_MAILBOX_KHR && entry.mode != VK_PRESENT_MODE_FIFO_KHR)
9402+
: (state.vkPresentModeMask & (1u << i)) == 0)
93969403
continue;
93979404

93989405
if (ImGui::Selectable(entry.name, forced.has_value() && forced.value() == entry.mode))
@@ -9412,7 +9419,10 @@ void MenuCommon::RenderVsyncSettings(RenderMenuContext& ctx)
94129419
"FIFO Relaxed: V-Sync, tears when below the refresh rate.\n"
94139420
"FIFO Latest Ready: V-Sync, shows the newest ready frame at each refresh.\n\n"
94149421
"Mailbox and FIFO Latest Ready can drop generated frames above the refresh rate.\n"
9415-
"Only modes the display supports are listed.");
9422+
"Only modes the display supports are listed.\n\n"
9423+
"D3D games on Windows: FIFO is V-Sync, Mailbox is no V-Sync without tearing (windowed or\n"
9424+
"borderless), Immediate is no V-Sync with tearing. Applies at the next frame and takes\n"
9425+
"priority over Override Vsync.");
94169426

94179427
if (state.vkGamePresentMode >= 0)
94189428
ImGui::Text("Game's present mode: %s", VulkanHooks::PresentModeName(state.vkGamePresentMode));

0 commit comments

Comments
 (0)