From 1093c4de2cfad715f0da1460aafbf88f47bd7bb1 Mon Sep 17 00:00:00 2001 From: Yizhou Jiang Date: Tue, 15 Oct 2019 07:43:05 +0000 Subject: [PATCH] Add a toggle to turn off vsync in Dawn This commit add a toggle to turn off vsync in Dawn. When turn off vsync, choose vulkan present mode VK_PRESENT_MODE_IMMEDIATE_KHR if the mode is available on the system, but if the mode isn't supported, choose default mode VK_PRESENT_MODE_FIFO_KHR. BUG=dawn:237 Change-Id: If400262b67cc8051422745e3bed737431183c0b6 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/12100 Reviewed-by: Corentin Wallez Reviewed-by: Austin Eng Commit-Queue: Yizhou Jiang --- src/dawn_native/Toggles.cpp | 5 ++++ src/dawn_native/Toggles.h | 1 + .../vulkan/NativeSwapChainImplVk.cpp | 30 ++++++++++++++++--- .../validation/ToggleValidationTests.cpp | 16 ++++++++++ 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/dawn_native/Toggles.cpp b/src/dawn_native/Toggles.cpp index bbb69a328a..7cc6f355c1 100644 --- a/src/dawn_native/Toggles.cpp +++ b/src/dawn_native/Toggles.cpp @@ -57,6 +57,11 @@ namespace dawn_native { "Clears resource to zero on first usage. This initializes the resource " "so that no dirty bits from recycled memory is present in the new resource.", "https://bugs.chromium.org/p/dawn/issues/detail?id=145"}}, + {Toggle::TurnOffVsync, + {"turn_off_vsync", + "Turn off vsync when rendering. In order to do performance test or run perf tests, " + "turn off vsync so that the fps can exeed 60.", + "https://bugs.chromium.org/p/dawn/issues/detail?id=237"}}, {Toggle::UseTemporaryBufferInCompressedTextureToTextureCopy, {"use_temporary_buffer_in_texture_to_texture_copy", "Split texture-to-texture copy into two copies: copy from source texture into a " diff --git a/src/dawn_native/Toggles.h b/src/dawn_native/Toggles.h index 73da01eacd..d5265946dc 100644 --- a/src/dawn_native/Toggles.h +++ b/src/dawn_native/Toggles.h @@ -28,6 +28,7 @@ namespace dawn_native { NonzeroClearResourcesOnCreationForTesting, AlwaysResolveIntoZeroLevelAndLayer, LazyClearResourceOnFirstUse, + TurnOffVsync, UseTemporaryBufferInCompressedTextureToTextureCopy, EnumCount, diff --git a/src/dawn_native/vulkan/NativeSwapChainImplVk.cpp b/src/dawn_native/vulkan/NativeSwapChainImplVk.cpp index 7ddd4a44c8..bd7e499da6 100644 --- a/src/dawn_native/vulkan/NativeSwapChainImplVk.cpp +++ b/src/dawn_native/vulkan/NativeSwapChainImplVk.cpp @@ -24,8 +24,30 @@ namespace dawn_native { namespace vulkan { namespace { + bool chooseSwapPresentMode(const std::vector& availablePresentModes, + bool turnOffVsync, + VkPresentModeKHR* presentMode) { + if (turnOffVsync) { + for (const auto& availablePresentMode : availablePresentModes) { + if (availablePresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) { + *presentMode = availablePresentMode; + return true; + } + } + return false; + } + + *presentMode = VK_PRESENT_MODE_FIFO_KHR; + return true; + } + bool ChooseSurfaceConfig(const VulkanSurfaceInfo& info, - NativeSwapChainImpl::ChosenConfig* config) { + NativeSwapChainImpl::ChosenConfig* config, + bool turnOffVsync) { + VkPresentModeKHR presentMode; + if (!chooseSwapPresentMode(info.presentModes, turnOffVsync, &presentMode)) { + return false; + } // TODO(cwallez@chromium.org): For now this is hardcoded to what works with one NVIDIA // driver. Need to generalize config->nativeFormat = VK_FORMAT_B8G8R8A8_UNORM; @@ -35,11 +57,11 @@ namespace dawn_native { namespace vulkan { // TODO(cwallez@chromium.org): This is upside down compared to what we want, at least // on Linux config->preTransform = info.capabilities.currentTransform; - config->presentMode = VK_PRESENT_MODE_FIFO_KHR; + config->presentMode = presentMode; config->compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; + return true; } - } // anonymous namespace NativeSwapChainImpl::NativeSwapChainImpl(Device* device, VkSurfaceKHR surface) @@ -63,7 +85,7 @@ namespace dawn_native { namespace vulkan { ASSERT(false); } - if (!ChooseSurfaceConfig(mInfo, &mConfig)) { + if (!ChooseSurfaceConfig(mInfo, &mConfig, mDevice->IsToggleEnabled(Toggle::TurnOffVsync))) { ASSERT(false); } } diff --git a/src/tests/unittests/validation/ToggleValidationTests.cpp b/src/tests/unittests/validation/ToggleValidationTests.cpp index 9ebedfb72f..82b973c71a 100644 --- a/src/tests/unittests/validation/ToggleValidationTests.cpp +++ b/src/tests/unittests/validation/ToggleValidationTests.cpp @@ -75,4 +75,20 @@ TEST_F(ToggleValidationTest, OverrideToggleUsage) { ASSERT_EQ(InvalidToggleExists, false); } } + +TEST_F(ToggleValidationTest, TurnOffVsyncWithToggle) { + const char* kValidToggleName = "turn_off_vsync"; + dawn_native::DeviceDescriptor descriptor; + descriptor.forceEnabledToggles.push_back(kValidToggleName); + + DawnDevice deviceWithToggle = adapter.CreateDevice(&descriptor); + std::vector toggleNames = dawn_native::GetTogglesUsed(deviceWithToggle); + bool validToggleExists = false; + for (const char* toggle : toggleNames) { + if (strcmp(toggle, kValidToggleName) == 0) { + validToggleExists = true; + } + } + ASSERT_EQ(validToggleExists, true); +} } // anonymous namespace