Skip to content

Commit

Permalink
- Fix vk_hdr looking for the wrong colorspace
Browse files Browse the repository at this point in the history
  • Loading branch information
dpjudas committed Feb 15, 2020
1 parent 6aed119 commit dd2d9f4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/rendering/vulkan/renderer/vk_postprocess.cpp
Expand Up @@ -220,7 +220,7 @@ void VkPostprocess::DrawPresentTexture(const IntRect &box, bool applyGamma, bool
uniforms.Offset = { 0.0f, 1.0f };
}

if (applyGamma && fb->swapChain->swapChainFormat.colorSpace == VK_COLOR_SPACE_HDR10_ST2084_EXT && !screenshot)
if (applyGamma && fb->swapChain->IsHdrModeActive() && !screenshot)
{
uniforms.HdrMode = 1;
}
Expand Down
42 changes: 15 additions & 27 deletions src/rendering/vulkan/system/vk_swapchain.cpp
Expand Up @@ -249,6 +249,11 @@ void VulkanSwapChain::CreateViews()
}
}

bool VulkanSwapChain::IsHdrModeActive() const
{
return swapChainFormat.colorSpace == VK_COLOR_SPACE_HDR10_ST2084_EXT || swapChainFormat.colorSpace == VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT;
}

void VulkanSwapChain::SelectFormat()
{
std::vector<VkSurfaceFormatKHR> surfaceFormats = GetSurfaceFormats();
Expand All @@ -264,6 +269,16 @@ void VulkanSwapChain::SelectFormat()

if (vk_hdr)
{
for (const auto& format : surfaceFormats)
{
if (format.format == VK_FORMAT_R16G16B16A16_SFLOAT && format.colorSpace == VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT)
{
swapChainFormat = format;
return;
}
}

// For older drivers that reported the wrong colorspace
for (const auto &format : surfaceFormats)
{
if (format.format == VK_FORMAT_R16G16B16A16_SFLOAT && format.colorSpace == VK_COLOR_SPACE_HDR10_ST2084_EXT)
Expand Down Expand Up @@ -312,33 +327,6 @@ void VulkanSwapChain::SelectPresentMode()
}
}

void VulkanSwapChain::SetHdrMetadata()
{
if (swapChainFormat.colorSpace != VK_COLOR_SPACE_HDR10_ST2084_EXT)
return;

// Mastering display with HDR10_ST2084 color primaries and D65 white point,
// maximum luminance of 1000 nits and minimum luminance of 0.001 nits;
// content has maximum luminance of 2000 nits and maximum frame average light level (MaxFALL) of 500 nits.

VkHdrMetadataEXT metadata = {};
metadata.sType = VK_STRUCTURE_TYPE_HDR_METADATA_EXT;
metadata.displayPrimaryRed.x = 0.708f;
metadata.displayPrimaryRed.y = 0.292f;
metadata.displayPrimaryGreen.x = 0.170f;
metadata.displayPrimaryGreen.y = 0.797f;
metadata.displayPrimaryBlue.x = 0.131f;
metadata.displayPrimaryBlue.y = 0.046f;
metadata.whitePoint.x = 0.3127f;
metadata.whitePoint.y = 0.3290f;
metadata.maxLuminance = 1000.0f;
metadata.minLuminance = 0.001f;
metadata.maxContentLightLevel = 2000.0f;
metadata.maxFrameAverageLightLevel = 500.0f;

vkSetHdrMetadataEXT(device->device, 1, &swapChain, &metadata);
}

void VulkanSwapChain::GetImages()
{
uint32_t imageCount;
Expand Down
3 changes: 2 additions & 1 deletion src/rendering/vulkan/system/vk_swapchain.h
Expand Up @@ -17,6 +17,8 @@ class VulkanSwapChain

void Recreate();

bool IsHdrModeActive() const;

VkSwapchainKHR swapChain = VK_NULL_HANDLE;
VkSurfaceFormatKHR swapChainFormat;
VkPresentModeKHR swapChainPresentMode;
Expand All @@ -32,7 +34,6 @@ class VulkanSwapChain
void SelectPresentMode();
bool CreateSwapChain(VkSwapchainKHR oldSwapChain = VK_NULL_HANDLE);
void CreateViews();
void SetHdrMetadata();
void GetImages();
void ReleaseResources();
void ReleaseViews();
Expand Down

0 comments on commit dd2d9f4

Please sign in to comment.