add hdr display support

This commit is contained in:
nmzik
2026-10-11 20:29:32 +02:00
parent 984cfcebd1
commit e51de54ee4
18 changed files with 215 additions and 64 deletions
+3 -1
View File
@@ -26,11 +26,13 @@ public:
~Presenter();
KYTY_CLASS_NO_COPY(Presenter);
[[nodiscard]] Frame& PrepareFrame(CommandBuffer& command, const ImageInfo& info);
[[nodiscard]] Frame& PrepareFrame(CommandBuffer& command, const ImageInfo& info,
bool hdr = false);
[[nodiscard]] Frame& PrepareBlankFrame(uint32_t width, uint32_t height, bool opaque,
CommandBuffer* producer = nullptr);
[[nodiscard]] bool PresentLastFrame();
[[nodiscard]] bool IsGuestPaused() const noexcept;
[[nodiscard]] bool IsHdrSupported() const noexcept;
[[nodiscard]] bool NeedsSystemOverlayRefresh() const noexcept;
[[nodiscard]] RenderContext& Renderer() const noexcept;
void Present(Frame& frame);
@@ -7,6 +7,7 @@
#include "common/file.h"
#include "common/logging/log.h"
#include "common/stringUtils.h"
#include "gpu_blit_shaders/gpu_imgui_hdr_spv.h"
#include "graphics/host_gpu/graphicContext.h"
#include "graphics/presentation/ddsImage.h"
#include "imgui.h"
@@ -890,6 +891,11 @@ struct SystemOverlay::Impl {
info.PipelineInfoMain.PipelineRenderingCreateInfo.colorAttachmentCount = 1;
info.PipelineInfoMain.PipelineRenderingCreateInfo.pColorAttachmentFormats = &color_format;
info.CheckVkResultFn = CheckVulkanResult;
if (IsHdrSwapchainFormat(format)) {
info.CustomShaderFragCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
info.CustomShaderFragCreateInfo.codeSize = sizeof(GPU_IMGUI_HDR_SPV);
info.CustomShaderFragCreateInfo.pCode = GPU_IMGUI_HDR_SPV;
}
EXIT_IF(!ImGui_ImplVulkan_Init(&info));
vulkan_initialized = true;
}
+33 -31
View File
@@ -272,6 +272,7 @@ public:
void Init(uint32_t width, uint32_t height);
FlipQueue& GetFlipQueue() { return m_flip_queue; }
Graphics::RenderContext& Renderer() const noexcept { return m_renderer; }
bool IsHdrSupported() const noexcept { return m_presenter.IsHdrSupported(); }
void VblankBegin();
void VblankEnd();
@@ -558,14 +559,6 @@ Graphics::ImageInfo BufferAttributeGroup::ImageInfo(const VideoOutBuffer& buffer
if (!Graphics::DecodeVideoOutPixelFormat(attribute.pixel_format, pixel_format)) {
EXIT("unsupported video-out pixel format: 0x%016" PRIx64 "\n", attribute.pixel_format);
}
if (attribute.pixel_format == Graphics::VIDEO_OUT_PIXEL_FORMAT_R10_G10_B10_A2_BT2100_PQ) {
static std::atomic_flag warned = ATOMIC_FLAG_INIT;
if (!warned.test_and_set(std::memory_order_relaxed)) {
Log::WriteToConsoleAndLog(fmt::format(
"Warning: HDR-to-SDR conversion is not implemented; displaying PQ video-out "
"pixels unchanged (format=0x{:016x}).\n", attribute.pixel_format));
}
}
const auto tile_mode = Graphics::Prospero::TileMode::kRenderTarget;
Graphics::ImageInfo info {};
info.data = {buffer.data_address, 0};
@@ -1071,31 +1064,31 @@ void FlipQueue::Prepare(uint64_t request_id, Graphics::CommandBuffer& buffer) {
uint32_t height = 0;
bool current = false;
bool premultiplied_alpha = false;
bool hdr = false;
{
Common::LockGuard lock(cfg->mutex);
current = cfg->opened && !cfg->closing && cfg->generation == generation;
if (current) {
if (special) {
width = cfg->width;
height = cfg->height;
} else {
const auto& surface = cfg->buffers[index];
if (!surface.Occupied()) {
EXIT("cannot prepare flip from an unregistered surface, id=%" PRIu64
" index=%d\n",
request_id, index);
}
if (surface.group_index >= VIDEO_OUT_BUFFER_ATTRIBUTE_NUM_MAX ||
!cfg->groups[surface.group_index].occupied) {
EXIT("video-out surface references an unavailable attribute group, id=%" PRIu64
" index=%d group=%d\n",
request_id, index, surface.group_index);
}
source_info = cfg->groups[surface.group_index].ImageInfo(surface);
premultiplied_alpha =
(cfg->groups[surface.group_index].attribute.option &
VIDEO_OUT_BUFFER_ATTRIBUTE_OPTION_ALPHA_PREMULTIPLIED) != 0;
if (current && special) {
width = cfg->width;
height = cfg->height;
} else if (current) {
const auto& surface = cfg->buffers[index];
if (!surface.Occupied()) {
EXIT("cannot prepare flip from an unregistered surface, id=%" PRIu64
" index=%d\n",
request_id, index);
}
if (surface.group_index >= VIDEO_OUT_BUFFER_ATTRIBUTE_NUM_MAX ||
!cfg->groups[surface.group_index].occupied) {
EXIT("video-out surface references an unavailable attribute group, id=%" PRIu64
" index=%d group=%d\n",
request_id, index, surface.group_index);
}
const auto& group = cfg->groups[surface.group_index];
source_info = group.ImageInfo(surface);
hdr = Graphics::IsHdrVideoOutPixelFormat(group.attribute.pixel_format);
premultiplied_alpha = (group.attribute.option &
VIDEO_OUT_BUFFER_ATTRIBUTE_OPTION_ALPHA_PREMULTIPLIED) != 0;
}
}
if (!current) {
@@ -1108,12 +1101,18 @@ void FlipQueue::Prepare(uint64_t request_id, Graphics::CommandBuffer& buffer) {
}
return;
}
static std::atomic_flag warned = ATOMIC_FLAG_INIT;
if (hdr && !m_presenter.IsHdrSupported() && !warned.test_and_set(std::memory_order_relaxed)) {
Log::WriteToConsoleAndLog(
"Warning: HDR output is unavailable; HDR-to-SDR conversion is not implemented, "
"displaying PQ video-out pixels unchanged.\n");
}
Graphics::Presenter::Frame* frame = nullptr;
if (special) {
frame = &m_presenter.PrepareBlankFrame(width, height, index == VIDEO_OUT_BUFFER_INDEX_BLACK,
&buffer);
} else {
frame = &m_presenter.PrepareFrame(buffer, source_info);
frame = &m_presenter.PrepareFrame(buffer, source_info, hdr);
}
Common::LockGuard lock(m_mutex);
@@ -1790,11 +1789,14 @@ KYTY_SYSV_ABI int VideoOutGetOutputStatus(int handle, VideoOutOutputStatus* stat
int32_t attribute3 = 0;
Loader::SystemContentParamSfoGetInt("ATTRIBUTE3", &attribute3);
int32_t attribute = 0;
const bool hdr_allowed = !Loader::SystemContentParamSfoGetInt("ATTRIBUTE", &attribute) ||
(attribute & 0x20000000) != 0;
ctx->mutex.Lock();
// Primary output reports 4K unless param.json Video-out Info enables resolution detection.
status->resolution =
((attribute3 & 4) != 0 && ctx->width < 3840 && ctx->height < 2160 ? 1u : 2u);
status->dynamicRange = 1;
status->dynamicRange = hdr_allowed && DriverState().IsHdrSupported() ? 2u : 1u;
status->refreshRate =
(ctx->output_mode == VIDEO_OUT_OUTPUT_MODE_119_88HZ || Config::GetVblankFrequency() >= 119
? VIDEO_OUT_REFRESH_RATE_119_88HZ
+67 -16
View File
@@ -17,6 +17,7 @@
#include <algorithm>
#include <array>
#include <atomic>
#include <limits>
#include <memory>
#include <vector>
@@ -31,6 +32,8 @@ struct Presenter::Frame {
vk::ImageView view = nullptr;
uint64_t present_tick = 0;
bool busy = false;
bool hdr = false;
bool blank = false;
void Configure(GraphicContext& graphics, vk::Extent2D extent, vk::Format format);
void Transit(vk::CommandBuffer command, vk::ImageLayout layout, vk::AccessFlags2 access);
@@ -274,14 +277,24 @@ public:
return static_cast<uint32_t>(m_images.size());
}
[[nodiscard]] vk::Format Format() const noexcept { return m_format; }
[[nodiscard]] bool IsHdrSupported() const noexcept { return m_hdr_supported.load(); }
bool SetHdr(bool hdr) {
const bool changed = m_hdr_active != (hdr && IsHdrSupported());
m_hdr_requested = hdr;
return changed;
}
private:
vk::SurfaceFormatKHR SelectFormat();
void Destroy();
void DrawOverlay(vk::CommandBuffer command, const Presenter::Layer& layer);
WindowContext& m_window;
vk::SwapchainKHR m_handle = nullptr;
vk::Format m_format = vk::Format::eUndefined;
std::atomic_bool m_hdr_supported {false};
bool m_hdr_requested = false;
bool m_hdr_active = false;
vk::Extent2D m_extent {};
// Drawable pixel size observed when this swapchain was created.
vk::Extent2D m_window_extent {};
@@ -346,6 +359,40 @@ struct Presenter::Impl {
std::atomic<uint64_t> presented_overlay_revision {0};
};
vk::SurfaceFormatKHR Swapchain::SelectFormat() {
const auto& formats = m_window.surface_capabilities.formats;
auto& graphics = m_window.graphic_ctx;
const auto hdr_format = std::ranges::find_if(formats, [&](const auto& candidate) {
return candidate.colorSpace == vk::ColorSpaceKHR::eHdr10St2084EXT &&
IsHdrSwapchainFormat(candidate.format) &&
static_cast<bool>(
graphics.GetFormatProperties(candidate.format).optimalTilingFeatures &
vk::FormatFeatureFlagBits::eBlitDst);
});
m_hdr_supported = Config::HdrEnabled() && hdr_format != formats.end();
m_hdr_active = m_hdr_requested && IsHdrSupported();
if (m_hdr_active) {
return *hdr_format;
}
if (Config::HdrEnabled() && !IsHdrSupported()) {
LOGF("HDR10 surface unavailable; using SDR output\n");
}
if (formats.size() == 1 && formats.front().format == vk::Format::eUndefined) {
return {vk::Format::eR8G8B8A8Unorm, vk::ColorSpaceKHR::eSrgbNonlinear};
}
for (const auto& candidate: formats) {
if (candidate.colorSpace != vk::ColorSpaceKHR::eSrgbNonlinear) {
continue;
}
switch (candidate.format) {
case vk::Format::eB8G8R8A8Unorm:
case vk::Format::eR8G8B8A8Unorm: return candidate;
default: break;
}
}
EXIT("no supported UNORM swapchain format\n");
}
void Swapchain::Create() {
auto& graphics = m_window.graphic_ctx;
EXIT_IF(graphics.device == nullptr);
@@ -385,18 +432,7 @@ void Swapchain::Create() {
? vk::CompositeAlphaFlagBitsKHR::eOpaque
: vk::CompositeAlphaFlagBitsKHR::eInherit;
vk::SurfaceFormatKHR format {vk::Format::eR8G8B8A8Unorm, vk::ColorSpaceKHR::eSrgbNonlinear};
if (surface.formats.size() != 1 || surface.formats.front().format != vk::Format::eUndefined) {
const auto it = std::find_if(surface.formats.begin(), surface.formats.end(),
[](const vk::SurfaceFormatKHR& candidate) {
return candidate.format == vk::Format::eB8G8R8A8Unorm ||
candidate.format == vk::Format::eR8G8B8A8Unorm;
});
if (it == surface.formats.end()) {
EXIT("no supported UNORM swapchain format\n");
}
format = *it;
}
const auto format = SelectFormat();
m_format = format.format;
const auto swapchain_features = graphics.GetFormatProperties(m_format).optimalTilingFeatures;
if (!static_cast<bool>(swapchain_features & vk::FormatFeatureFlagBits::eBlitDst)) {
@@ -436,6 +472,7 @@ void Swapchain::Create() {
RequireVulkanSuccess(graphics.device.createSwapchainKHR(&create_info, nullptr, &m_handle),
"vkCreateSwapchainKHR");
EXIT_IF(m_handle == nullptr);
LOGF("Vulkan presentation: %s\n", m_hdr_active ? "HDR10 (PQ)" : "SDR");
m_images = EnumerateVulkan<vk::Image>(
"vkGetSwapchainImagesKHR", [&](uint32_t* count, vk::Image* images) {
@@ -692,9 +729,11 @@ void Swapchain::DrawOverlay(vk::CommandBuffer command, const Presenter::Layer& l
write.descriptorType = vk::DescriptorType::eCombinedImageSampler;
write.pImageInfo = &image;
command.pushDescriptorSetKHR(vk::PipelineBindPoint::eGraphics, m_overlay_layout, 0, 1, &write);
const uint32_t premultiplied = layer.premultiplied_alpha;
const uint32_t flags = (layer.premultiplied_alpha ? 1u : 0u) |
(m_hdr_active ? 2u : 0u) | (frame.hdr ? 4u : 0u) |
(frame.image.format == vk::Format::eR16G16B16A16Sfloat ? 8u : 0u);
command.pushConstants(m_overlay_layout, vk::ShaderStageFlagBits::eFragment, 0,
sizeof(premultiplied), &premultiplied);
sizeof(flags), &flags);
command.bindPipeline(vk::PipelineBindPoint::eGraphics, m_overlay_pipeline);
vk::RenderingAttachmentInfo attachment {};
attachment.imageView = m_image_views[m_image_index];
@@ -868,7 +907,7 @@ Presenter::Presenter(WindowContext& window): m_impl(std::make_unique<Impl>(windo
Presenter::~Presenter() = default;
Presenter::Frame& Presenter::PrepareFrame(CommandBuffer& buffer, const ImageInfo& info) {
Presenter::Frame& Presenter::PrepareFrame(CommandBuffer& buffer, const ImageInfo& info, bool hdr) {
KYTY_PROFILER_FUNCTION();
EXIT_IF(buffer.IsInvalid());
auto frame_format = info.pixel_format;
@@ -878,6 +917,8 @@ Presenter::Frame& Presenter::PrepareFrame(CommandBuffer& buffer, const ImageInfo
default: break;
}
auto* frame = m_impl->frames.Acquire({info.extent.width, info.extent.height}, frame_format);
frame->hdr = hdr;
frame->blank = false;
Common::LockGuard render_lock(m_impl->renderer.GetMutex());
auto& image = m_impl->ResolveSurface(info);
if (image.backing.format == vk::Format::eUndefined) {
@@ -894,6 +935,8 @@ Presenter::Frame& Presenter::PrepareBlankFrame(uint32_t width, uint32_t height,
KYTY_PROFILER_FUNCTION();
auto format = m_impl->frames.GetFormat();
auto* frame = m_impl->frames.Acquire({width, height}, format);
frame->hdr = false;
frame->blank = true;
Common::LockGuard render_lock(m_impl->renderer.GetMutex());
frame->Configure(m_impl->window.graphic_ctx, {width, height}, format);
vk::ClearColorValue clear {};
@@ -922,6 +965,10 @@ bool Presenter::IsGuestPaused() const noexcept {
return m_impl->window.loop.paused.load(std::memory_order_acquire);
}
bool Presenter::IsHdrSupported() const noexcept {
return m_impl->swapchain.IsHdrSupported();
}
bool Presenter::NeedsSystemOverlayRefresh() const noexcept {
const auto visual = GetSystemOverlayVisualState();
return visual.active ||
@@ -967,8 +1014,12 @@ void Presenter::Impl::Present() {
KYTY_PROFILER_FUNCTION();
const auto overlay_visual = GetSystemOverlayVisualState();
// Keep black/blank flips in the current color space; only real main-bus frames switch it.
const auto* main_frame = layers[0].frame;
const bool hdr_changed = main_frame != nullptr && !main_frame->blank &&
swapchain.SetHdr(main_frame->hdr);
// Some window systems keep presenting an old swapchain after a resize.
if (swapchain.NeedsResize()) {
if (hdr_changed || swapchain.NeedsResize()) {
RecoverSwapchain(Swapchain::Status::Recreate);
}
for (uint32_t attempt = 0; attempt < 2; attempt++) {
@@ -685,6 +685,11 @@ static void VulkanGetExtensions(VulkanExtensions& r) {
});
r.enable_validation_layers = Config::VulkanValidationEnabled();
if (Config::HdrEnabled() &&
HasExtension(r.available_extensions, VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME) &&
!HasExtension(r.required_extensions, VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME)) {
r.required_extensions.push_back(VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME);
}
if (HasExtension(r.available_extensions, VK_EXT_DEBUG_UTILS_EXTENSION_NAME)) {
r.required_extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);