update parallel rdp

This commit is contained in:
Logan McNaughton committed 2022-12-07 18:11:41 -07:00
1 parent 1f7414444d
commit dc27483cc9
35 files changed
+4001 -1071

No files matched your search

+146 -143
View File
@@ -23,6 +23,10 @@
#include "wsi.hpp"
#include "quirks.hpp"
#if defined(ANDROID) && defined(HAVE_SWAPPY)
#include "swappy/swappyVk.h"
#endif
namespace Vulkan
{
WSI::WSI()
@@ -40,6 +44,32 @@ WSI::WSI()
#else
present_frame_latency = 1;
#endif
const char *env = getenv("GRANITE_VULKAN_PRESENT_WAIT_LATENCY");
if (env)
{
present_frame_latency = uint32_t(strtoul(env, nullptr, 0));
LOGI("Overriding VK_KHR_present_wait latency to %u frames.\n", present_frame_latency);
}
// Primaries are ST.2020 with D65 whitepoint as specified.
hdr_metadata.displayPrimaryRed = { 0.708f, 0.292f };
hdr_metadata.displayPrimaryGreen = { 0.170f, 0.797f };
hdr_metadata.displayPrimaryBlue = { 0.131f, 0.046f };
hdr_metadata.whitePoint = { 0.3127f, 0.3290f };
// HDR10 range? Just arbitrary values, user can override later.
hdr_metadata.minLuminance = 0.01f;
hdr_metadata.maxLuminance = 1000.0f;
hdr_metadata.maxContentLightLevel = 1000.0f;
hdr_metadata.maxFrameAverageLightLevel = 200.0f;
}
void WSI::set_hdr_metadata(const VkHdrMetadataEXT &hdr)
{
hdr_metadata = hdr;
if (swapchain && swapchain_surface_format.colorSpace == VK_COLOR_SPACE_HDR10_ST2084_EXT)
table->vkSetHdrMetadataEXT(device->get_device(), 1, &swapchain, &hdr_metadata);
}
void WSIPlatform::set_window_title(const std::string &)
@@ -77,38 +107,6 @@ double WSI::get_smooth_frame_time() const
return smooth_frame_time;
}
float WSIPlatform::get_estimated_frame_presentation_duration()
{
// Just assume 60 FPS for now.
// TODO: Be more intelligent.
return 1.0f / 60.0f;
}
float WSI::get_estimated_video_latency()
{
if (using_display_timing)
{
// Very accurate estimate.
double latency = timing.get_current_latency();
return float(latency);
}
else
{
// Very rough estimate.
unsigned latency_frames = device->get_num_swapchain_images();
if (latency_frames > 0)
latency_frames--;
if (platform)
{
float frame_duration = platform->get_estimated_frame_presentation_duration();
return frame_duration * float(latency_frames);
}
else
return -1.0f;
}
}
bool WSI::init_from_existing_context(ContextHandle existing_context)
{
VK_ASSERT(platform);
@@ -129,16 +127,17 @@ bool WSI::init_external_swapchain(std::vector<ImageHandle> swapchain_images_)
swapchain_width = external_swapchain_images.front()->get_width();
swapchain_height = external_swapchain_images.front()->get_height();
swapchain_format = external_swapchain_images.front()->get_format();
swapchain_surface_format = { external_swapchain_images.front()->get_format(), VK_COLOR_SPACE_SRGB_NONLINEAR_KHR };
LOGI("Created swapchain %u x %u (fmt: %u).\n",
swapchain_width, swapchain_height, static_cast<unsigned>(swapchain_format));
swapchain_width, swapchain_height, static_cast<unsigned>(swapchain_surface_format.format));
platform->event_swapchain_destroyed();
platform->event_swapchain_created(device.get(), swapchain_width, swapchain_height,
platform->event_swapchain_created(device.get(), VK_NULL_HANDLE, swapchain_width, swapchain_height,
swapchain_aspect_ratio,
external_swapchain_images.size(),
swapchain_format, swapchain_current_prerotate);
swapchain_surface_format.format, swapchain_surface_format.colorSpace,
swapchain_current_prerotate);
device->init_external_swapchain(this->external_swapchain_images);
platform->get_frame_timer().reset();
@@ -215,7 +214,7 @@ bool WSI::init_surface_swapchain()
return false;
}
device->init_swapchain(swapchain_images, swapchain_width, swapchain_height, swapchain_format,
device->init_swapchain(swapchain_images, swapchain_width, swapchain_height, swapchain_surface_format.format,
swapchain_current_prerotate,
current_extra_usage | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
platform->get_frame_timer().reset();
@@ -279,6 +278,7 @@ void WSI::drain_swapchain()
void WSI::tear_down_swapchain()
{
drain_swapchain();
platform->event_swapchain_destroyed();
if (swapchain != VK_NULL_HANDLE)
{
@@ -303,8 +303,6 @@ void WSI::deinit_surface_and_swapchain()
platform->destroy_surface(context->get_instance(), surface);
surface = VK_NULL_HANDLE;
}
platform->event_swapchain_destroyed();
}
void WSI::set_external_frame(unsigned index, Semaphore acquire_semaphore, double frame_time)
@@ -351,6 +349,33 @@ Semaphore WSI::consume_external_release_semaphore()
//#define VULKAN_WSI_TIMING_DEBUG
void WSI::wait_swapchain_latency()
{
if (device->get_device_features().present_wait_features.presentWait &&
present_last_id > present_frame_latency &&
current_present_mode == PresentMode::SyncToVBlank)
{
// The effective latency is more like present_frame_latency + 1.
// If 0, we wait for vblank, and we must do CPU work and GPU work in one frame
// to hit next vblank.
uint64_t target = present_last_id - present_frame_latency;
#ifdef VULKAN_WSI_TIMING_DEBUG
auto begin_wait = Util::get_current_time_nsecs();
#endif
auto wait_ts = device->write_calibrated_timestamp();
VkResult wait_result = table->vkWaitForPresentKHR(context->get_device(), swapchain, target, UINT64_MAX);
device->register_time_interval("WSI", std::move(wait_ts),
device->write_calibrated_timestamp(), "wait_frame_latency");
if (wait_result != VK_SUCCESS)
LOGE("vkWaitForPresentKHR failed, vr %d.\n", wait_result);
#ifdef VULKAN_WSI_TIMING_DEBUG
auto end_wait = Util::get_current_time_nsecs();
LOGI("WaitForPresentKHR took %.3f ms.\n", 1e-6 * double(end_wait - begin_wait));
#endif
}
}
bool WSI::begin_frame()
{
if (frame_is_external)
@@ -386,21 +411,13 @@ bool WSI::begin_frame()
{
auto acquire = device->request_legacy_semaphore();
// For adaptive low latency we don't want to observe the time it takes to wait for
// WSI semaphore as part of our latency,
// which means we will never get sub-frame latency on some implementations,
// so block on that first.
Fence fence;
if (timing.get_options().latency_limiter == LatencyLimiter::AdaptiveLowLatency)
fence = device->request_legacy_fence();
#ifdef VULKAN_WSI_TIMING_DEBUG
auto acquire_start = Util::get_current_time_nsecs();
#endif
auto acquire_ts = device->write_calibrated_timestamp();
result = table->vkAcquireNextImageKHR(context->get_device(), swapchain, UINT64_MAX, acquire->get_semaphore(),
fence ? fence->get_fence() : VK_NULL_HANDLE, &swapchain_index);
VK_NULL_HANDLE, &swapchain_index);
device->register_time_interval("WSI", std::move(acquire_ts), device->write_calibrated_timestamp(), "acquire");
#if defined(ANDROID)
@@ -410,9 +427,6 @@ bool WSI::begin_frame()
result = VK_SUCCESS;
#endif
if ((result >= 0) && fence)
fence->wait();
if (result == VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT)
{
LOGE("Lost exclusive full-screen ...\n");
@@ -436,12 +450,11 @@ bool WSI::begin_frame()
has_acquired_swapchain_index = true;
acquire->signal_external();
wait_swapchain_latency();
auto frame_time = platform->get_frame_timer().frame();
auto elapsed_time = platform->get_frame_timer().get_elapsed();
if (using_display_timing)
timing.begin_frame(frame_time, elapsed_time);
smooth_frame_time = frame_time;
smooth_elapsed_time = elapsed_time;
@@ -463,7 +476,7 @@ bool WSI::begin_frame()
if (!blocking_init_swapchain(swapchain_width, swapchain_height))
return false;
device->init_swapchain(swapchain_images, swapchain_width, swapchain_height,
swapchain_format, swapchain_current_prerotate,
swapchain_surface_format.format, swapchain_current_prerotate,
current_extra_usage | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
}
else
@@ -508,17 +521,6 @@ bool WSI::end_frame()
info.pImageIndices = &swapchain_index;
info.pResults = &result;
VkPresentTimeGOOGLE present_time;
VkPresentTimesInfoGOOGLE present_timing = { VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE };
if (using_display_timing && timing.fill_present_info_timing(present_time))
{
present_timing.swapchainCount = 1;
present_timing.pTimes = &present_time;
present_timing.pNext = info.pNext;
info.pNext = &present_timing;
}
VkPresentIdKHR present_id_info = { VK_STRUCTURE_TYPE_PRESENT_ID_KHR };
if (device->get_device_features().present_id_features.presentId)
{
@@ -534,7 +536,13 @@ bool WSI::end_frame()
#endif
auto present_ts = device->write_calibrated_timestamp();
#if defined(ANDROID) && defined(HAVE_SWAPPY)
VkResult overall = SwappyVk_queuePresent(device->get_current_present_queue(), &info);
#else
VkResult overall = table->vkQueuePresentKHR(device->get_current_present_queue(), &info);
#endif
device->register_time_interval("WSI", std::move(present_ts), device->write_calibrated_timestamp(), "present");
#if defined(ANDROID)
@@ -561,29 +569,8 @@ bool WSI::end_frame()
// This makes sense I guess. Record the latest present ID which was successfully presented
// so we don't risk deadlock.
if ((result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR) &&
device->get_device_features().present_wait_features.presentWait)
device->get_device_features().present_id_features.presentId)
{
if (present_id > present_frame_latency)
{
uint64_t target = present_id - present_frame_latency;
// In case there are weird gaps which present IDs got a successful present.
if (target > present_last_id)
target = present_last_id;
#ifdef VULKAN_WSI_TIMING_DEBUG
auto begin_wait = Util::get_current_time_nsecs();
#endif
auto wait_ts = device->write_calibrated_timestamp();
VkResult wait_result = table->vkWaitForPresentKHR(context->get_device(), swapchain,
target, UINT64_MAX);
device->register_time_interval("WSI", std::move(wait_ts),
device->write_calibrated_timestamp(), "wait_frame_latency");
if (wait_result != VK_SUCCESS)
LOGE("vkWaitForPresentKHR failed, vr %d.\n", wait_result);
#ifdef VULKAN_WSI_TIMING_DEBUG
auto end_wait = Util::get_current_time_nsecs();
LOGI("WaitForPresentKHR took %.3f ms.\n", 1e-6 * double(end_wait - begin_wait));
#endif
}
present_last_id = present_id;
}
@@ -610,11 +597,11 @@ bool WSI::end_frame()
}
// Re-init swapchain.
if (present_mode != current_present_mode || srgb_backbuffer_enable != current_srgb_backbuffer_enable ||
if (present_mode != current_present_mode || backbuffer_format != current_backbuffer_format ||
extra_usage != current_extra_usage)
{
current_present_mode = present_mode;
current_srgb_backbuffer_enable = srgb_backbuffer_enable;
current_backbuffer_format = backbuffer_format;
current_extra_usage = extra_usage;
update_framebuffer(swapchain_width, swapchain_height);
}
@@ -630,7 +617,7 @@ void WSI::update_framebuffer(unsigned width, unsigned height)
drain_swapchain();
if (blocking_init_swapchain(width, height))
{
device->init_swapchain(swapchain_images, swapchain_width, swapchain_height, swapchain_format,
device->init_swapchain(swapchain_images, swapchain_width, swapchain_height, swapchain_surface_format.format,
swapchain_current_prerotate,
current_extra_usage | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
}
@@ -660,26 +647,28 @@ void WSI::set_extra_usage_flags(VkImageUsageFlags usage)
}
}
void WSI::set_backbuffer_srgb(bool enable)
void WSI::set_backbuffer_format(BackbufferFormat format)
{
srgb_backbuffer_enable = enable;
if (!has_acquired_swapchain_index && srgb_backbuffer_enable != current_srgb_backbuffer_enable)
backbuffer_format = format;
if (!has_acquired_swapchain_index && backbuffer_format != current_backbuffer_format)
{
current_srgb_backbuffer_enable = srgb_backbuffer_enable;
current_backbuffer_format = backbuffer_format;
update_framebuffer(swapchain_width, swapchain_height);
}
}
void WSI::set_backbuffer_srgb(bool enable)
{
set_backbuffer_format(enable ? BackbufferFormat::sRGB : BackbufferFormat::UNORM);
}
void WSI::teardown()
{
if (platform)
platform->release_resources();
if (context)
{
tear_down_swapchain();
platform->event_swapchain_destroyed();
}
if (surface != VK_NULL_HANDLE)
{
@@ -694,8 +683,6 @@ void WSI::teardown()
external_swapchain_images.clear();
device.reset();
context.reset();
using_display_timing = false;
}
bool WSI::blocking_init_swapchain(unsigned width, unsigned height)
@@ -729,11 +716,17 @@ bool WSI::blocking_init_swapchain(unsigned width, unsigned height)
return swapchain != VK_NULL_HANDLE;
}
VkSurfaceFormatKHR WSI::find_suitable_present_format(const std::vector<VkSurfaceFormatKHR> &formats) const
VkSurfaceFormatKHR WSI::find_suitable_present_format(const std::vector<VkSurfaceFormatKHR> &formats, BackbufferFormat desired_format) const
{
size_t format_count = formats.size();
VkSurfaceFormatKHR format = { VK_FORMAT_UNDEFINED };
if (desired_format == BackbufferFormat::HDR10 && !device->get_device_features().supports_hdr_metadata)
{
LOGW("VK_EXT_hdr_metadata is not supported, ignoring HDR10.\n");
return format;
}
VkFormatFeatureFlags features = VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT;
if ((current_extra_usage & VK_IMAGE_USAGE_STORAGE_BIT) != 0)
@@ -750,11 +743,22 @@ VkSurfaceFormatKHR WSI::find_suitable_present_format(const std::vector<VkSurface
if (!device->image_format_is_supported(formats[i].format, features))
continue;
if (current_srgb_backbuffer_enable)
if (desired_format == BackbufferFormat::HDR10)
{
if (formats[i].format == VK_FORMAT_R8G8B8A8_SRGB ||
formats[i].format == VK_FORMAT_B8G8R8A8_SRGB ||
formats[i].format == VK_FORMAT_A8B8G8R8_SRGB_PACK32)
if (formats[i].colorSpace == VK_COLOR_SPACE_HDR10_ST2084_EXT &&
(formats[i].format == VK_FORMAT_A2B10G10R10_UNORM_PACK32 ||
formats[i].format == VK_FORMAT_A2R10G10B10_UNORM_PACK32))
{
format = formats[i];
break;
}
}
else if (desired_format == BackbufferFormat::sRGB)
{
if (formats[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR &&
(formats[i].format == VK_FORMAT_R8G8B8A8_SRGB ||
formats[i].format == VK_FORMAT_B8G8R8A8_SRGB ||
formats[i].format == VK_FORMAT_A8B8G8R8_SRGB_PACK32))
{
format = formats[i];
break;
@@ -762,9 +766,10 @@ VkSurfaceFormatKHR WSI::find_suitable_present_format(const std::vector<VkSurface
}
else
{
if (formats[i].format == VK_FORMAT_R8G8B8A8_UNORM ||
formats[i].format == VK_FORMAT_B8G8R8A8_UNORM ||
formats[i].format == VK_FORMAT_A8B8G8R8_UNORM_PACK32)
if (formats[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR &&
(formats[i].format == VK_FORMAT_R8G8B8A8_UNORM ||
formats[i].format == VK_FORMAT_B8G8R8A8_UNORM ||
formats[i].format == VK_FORMAT_A8B8G8R8_UNORM_PACK32))
{
format = formats[i];
break;
@@ -809,6 +814,14 @@ WSI::SwapchainError WSI::init_swapchain(unsigned width, unsigned height)
const char *exclusive = getenv("GRANITE_EXCLUSIVE_FULL_SCREEN");
bool prefer_exclusive = exclusive && strtoul(exclusive, nullptr, 0) != 0;
if (device->get_device_features().driver_properties.driverID == VK_DRIVER_ID_AMD_PROPRIETARY_KHR &&
current_backbuffer_format == BackbufferFormat::HDR10)
{
LOGI("HDR requested on AMD Windows. Forcing exclusive fullscreen, or HDR will not work properly.\n");
prefer_exclusive = true;
}
if (prefer_exclusive)
{
LOGI("Win32: Opting in to exclusive full-screen!\n");
@@ -918,13 +931,22 @@ WSI::SwapchainError WSI::init_swapchain(unsigned width, unsigned height)
extra_usage = current_extra_usage;
}
auto surface_format = find_suitable_present_format(formats);
auto attempt_backbuffer_format = current_backbuffer_format;
auto surface_format = find_suitable_present_format(formats, attempt_backbuffer_format);
if (surface_format.format == VK_FORMAT_UNDEFINED && attempt_backbuffer_format == BackbufferFormat::HDR10)
{
LOGW("Could not find suitable present format for HDR10. Attempting fallback to sRGB.\n");
attempt_backbuffer_format = BackbufferFormat::sRGB;
surface_format = find_suitable_present_format(formats, attempt_backbuffer_format);
}
if (surface_format.format == VK_FORMAT_UNDEFINED)
{
LOGW("Could not find supported format for swapchain usage flags 0x%x.\n", current_extra_usage);
current_extra_usage = 0;
extra_usage = 0;
surface_format = find_suitable_present_format(formats);
surface_format = find_suitable_present_format(formats, attempt_backbuffer_format);
}
static const char *transform_names[] = {
@@ -1127,6 +1149,7 @@ WSI::SwapchainError WSI::init_swapchain(unsigned width, unsigned height)
info.pNext = &exclusive_info;
#endif
platform->event_swapchain_destroyed();
auto res = table->vkCreateSwapchainKHR(context->get_device(), &info, nullptr, &swapchain);
if (old_swapchain != VK_NULL_HANDLE)
table->vkDestroySwapchainKHR(context->get_device(), old_swapchain, nullptr);
@@ -1145,20 +1168,6 @@ WSI::SwapchainError WSI::init_swapchain(unsigned width, unsigned height)
}
#endif
#if 0
if (use_vsync && context->get_enabled_device_features().supports_google_display_timing)
{
WSITimingOptions timing_options;
timing_options.swap_interval = 1;
//timing_options.adaptive_swap_interval = true;
//timing_options.latency_limiter = LatencyLimiter::IdealPipeline;
timing.init(platform, device.get(), swapchain, timing_options);
using_display_timing = true;
}
else
#endif
using_display_timing = false;
if (res != VK_SUCCESS)
{
LOGE("Failed to create swapchain (code: %d)\n", int(res));
@@ -1168,11 +1177,11 @@ WSI::SwapchainError WSI::init_swapchain(unsigned width, unsigned height)
swapchain_width = swapchain_size.width;
swapchain_height = swapchain_size.height;
swapchain_format = surface_format.format;
swapchain_surface_format = surface_format;
swapchain_is_suboptimal = false;
LOGI("Created swapchain %u x %u (fmt: %u, transform: %u).\n", swapchain_width, swapchain_height,
unsigned(swapchain_format), unsigned(swapchain_current_prerotate));
unsigned(swapchain_surface_format.format), unsigned(swapchain_current_prerotate));
uint32_t image_count;
if (table->vkGetSwapchainImagesKHR(context->get_device(), swapchain, &image_count, nullptr) != VK_SUCCESS)
@@ -1184,24 +1193,18 @@ WSI::SwapchainError WSI::init_swapchain(unsigned width, unsigned height)
LOGI("Got %u swapchain images.\n", image_count);
platform->event_swapchain_destroyed();
platform->event_swapchain_created(device.get(), swapchain_width, swapchain_height,
swapchain_aspect_ratio, image_count, info.imageFormat, swapchain_current_prerotate);
platform->event_swapchain_created(device.get(), swapchain, swapchain_width, swapchain_height,
swapchain_aspect_ratio, image_count,
swapchain_surface_format.format,
swapchain_surface_format.colorSpace,
swapchain_current_prerotate);
if (swapchain_surface_format.colorSpace == VK_COLOR_SPACE_HDR10_ST2084_EXT)
table->vkSetHdrMetadataEXT(device->get_device(), 1, &swapchain, &hdr_metadata);
return SwapchainError::None;
}
double WSI::get_estimated_refresh_interval() const
{
uint64_t interval = timing.get_refresh_interval();
if (interval)
return interval * 1e-9;
else if (platform)
return platform->get_estimated_frame_presentation_duration();
else
return 0.0;
}
void WSI::set_support_prerotate(bool enable)
{
support_prerotate = enable;
@@ -1219,10 +1222,10 @@ WSI::~WSI()
void WSIPlatform::event_device_created(Device *) {}
void WSIPlatform::event_device_destroyed() {}
void WSIPlatform::event_swapchain_created(Device *, unsigned, unsigned, float, size_t, VkFormat, VkSurfaceTransformFlagBitsKHR) {}
void WSIPlatform::event_swapchain_created(Device *, VkSwapchainKHR, unsigned, unsigned, float, size_t,
VkFormat, VkColorSpaceKHR,
VkSurfaceTransformFlagBitsKHR) {}
void WSIPlatform::event_swapchain_destroyed() {}
void WSIPlatform::event_frame_tick(double, double) {}
void WSIPlatform::event_swapchain_index(Device *, unsigned) {}
void WSIPlatform::event_display_timing_stutter(uint32_t, uint32_t, uint32_t) {}
}