From 66ea2e2c9ed4e9257521e6706fbd21feb7e3cc5b Mon Sep 17 00:00:00 2001 From: UjinT34 <41110182+UjinT34@users.noreply.github.com> Date: Thu, 16 Apr 2026 21:19:25 +0300 Subject: renderer: Various CM fixes, part 8 of refactors (#13860) Part 8 of Ujin's refactors. --- src/helpers/cm/ColorManagement.cpp | 30 ++++++++++++++++++- src/helpers/cm/ColorManagement.hpp | 60 +++++++++++++++++++++++++++++++++++++- 2 files changed, 88 insertions(+), 2 deletions(-) (limited to 'src/helpers/cm') diff --git a/src/helpers/cm/ColorManagement.cpp b/src/helpers/cm/ColorManagement.cpp index 829adbe7..5e4725d4 100644 --- a/src/helpers/cm/ColorManagement.cpp +++ b/src/helpers/cm/ColorManagement.cpp @@ -1,10 +1,12 @@ #include "ColorManagement.hpp" #include "../../macros.hpp" +#include "helpers/TransferFunction.hpp" #include #include #include using namespace NColorManagement; +using namespace NTransferFunction; namespace NColorManagement { // expected to be small @@ -110,6 +112,20 @@ WP CImageDescription::getPrimaries() const { return CPrimaries::from(m_primariesId); } +bool CImageDescription::needsCM(WP target) const { + if (m_id == target->m_id) + return false; + + return m_imageDescription.icc.present || target->m_imageDescription.icc.present // TODO compare ICC + || m_imageDescription.transferFunction != target->m_imageDescription.transferFunction // + //|| m_imageDescription.transferFunctionPower != target->m_imageDescription.transferFunctionPower // TODO unsupported + || m_imageDescription.getPrimaries() != target->m_imageDescription.getPrimaries() // + // || m_imageDescription.masteringPrimaries != target->m_imageDescription.masteringPrimaries // TODO unused + || m_imageDescription.luminances != target->m_imageDescription.luminances // + // || m_imageDescription.masteringLuminances != target->m_imageDescription.masteringLuminances // TODO unused + ; +} + static Mat3x3 diag3(const std::array& s) { return Mat3x3{std::array{s[0], 0, 0, 0, s[1], 0, 0, 0, s[2]}}; } @@ -190,4 +206,16 @@ Mat3x3 NColorManagement::adaptBradford(Hyprgraphics::CColor::xy srcW, Hyprgraphi result.multiply(diag3(scale)).multiply(Bradford); return result; -} \ No newline at end of file +} + +PImageDescription NColorManagement::getDefaultImageDescription() { + const auto TF = fromConfig(); + switch (TF) { + case TF_AUTO: + case TF_GAMMA22: + case TF_FORCED_GAMMA22: return DEFAULT_GAMMA22_IMAGE_DESCRIPTION; + case TF_DEFAULT: + case TF_SRGB: return DEFAULT_SRGB_IMAGE_DESCRIPTION; + default: UNREACHABLE(); + } +} diff --git a/src/helpers/cm/ColorManagement.hpp b/src/helpers/cm/ColorManagement.hpp index dccbb972..183a8544 100644 --- a/src/helpers/cm/ColorManagement.hpp +++ b/src/helpers/cm/ColorManagement.hpp @@ -1,11 +1,13 @@ #pragma once #include "color-management-v1.hpp" +#include #include #include "../../helpers/memory/Memory.hpp" #include "../../helpers/math/Math.hpp" #include +#include #include #include @@ -78,6 +80,24 @@ namespace NColorManagement { default: return sc(tf); } } + inline std::string tfToString(eTransferFunction tf) { + switch (tf) { + case CM_TRANSFER_FUNCTION_BT1886: return "TF:BT1886"; + case CM_TRANSFER_FUNCTION_GAMMA22: return "TF:GAMMA22"; + case CM_TRANSFER_FUNCTION_GAMMA28: return "TF:GAMMA28"; + case CM_TRANSFER_FUNCTION_ST240: return "TF:ST240"; + case CM_TRANSFER_FUNCTION_EXT_LINEAR: return "TF:EXT_LINEAR"; + case CM_TRANSFER_FUNCTION_LOG_100: return "TF:LOG_100"; + case CM_TRANSFER_FUNCTION_LOG_316: return "TF:LOG_316"; + case CM_TRANSFER_FUNCTION_XVYCC: return "TF:XVYCC"; + case CM_TRANSFER_FUNCTION_SRGB: return "TF:SRGB"; + case CM_TRANSFER_FUNCTION_EXT_SRGB: return "TF:EXT_SRGB"; + case CM_TRANSFER_FUNCTION_ST2084_PQ: return "TF:ST2084_PQ"; + case CM_TRANSFER_FUNCTION_ST428: return "TF:ST428"; + case CM_TRANSFER_FUNCTION_HLG: return "TF:HLG"; + default: return "TF:ERROR"; + } + } using SPCPRimaries = Hyprgraphics::SPCPRimaries; @@ -320,6 +340,7 @@ namespace NColorManagement { uint64_t id() const; WP getPrimaries() const; + bool needsCM(WP target) const; private: CImageDescription(const SImageDescription& imageDescription, const uint64_t imageDescriptionId); @@ -330,7 +351,9 @@ namespace NColorManagement { using PImageDescription = WP; - static const auto DEFAULT_IMAGE_DESCRIPTION = CImageDescription::from(SImageDescription{ + PImageDescription getDefaultImageDescription(); + + static const auto DEFAULT_GAMMA22_IMAGE_DESCRIPTION = CImageDescription::from(SImageDescription{ .transferFunction = NColorManagement::CM_TRANSFER_FUNCTION_GAMMA22, .primariesNameSet = true, .primariesNamed = NColorManagement::CM_PRIMARIES_SRGB, @@ -338,6 +361,14 @@ namespace NColorManagement { .luminances = {.min = SDR_MIN_LUMINANCE, .max = 80, .reference = 80}, }); + static const auto DEFAULT_SRGB_IMAGE_DESCRIPTION = CImageDescription::from(SImageDescription{ + .transferFunction = NColorManagement::CM_TRANSFER_FUNCTION_SRGB, + .primariesNameSet = true, + .primariesNamed = NColorManagement::CM_PRIMARIES_SRGB, + .primaries = NColorManagement::getPrimaries(NColorManagement::CM_PRIMARIES_SRGB), + .luminances = {.min = SDR_MIN_LUMINANCE, .max = 80, .reference = 80}, + }); + static const auto DEFAULT_HDR_IMAGE_DESCRIPTION = CImageDescription::from(SImageDescription{ .transferFunction = NColorManagement::CM_TRANSFER_FUNCTION_ST2084_PQ, .primariesNameSet = true, @@ -363,3 +394,30 @@ namespace NColorManagement { .luminances = {.min = 0, .max = 10000, .reference = 80}, }); } + +template +struct std::formatter : std::formatter { + template + auto format(const Hyprgraphics::SPCPRimaries& primaries, FormatContext& ctx) const { + return std::format_to(ctx.out(), "[r={},{} g={},{} b={},{} w={},{}]", primaries.red.x, primaries.red.y, primaries.green.x, primaries.green.y, primaries.blue.x, + primaries.blue.y, primaries.white.x, primaries.white.y); + } +}; + +template +struct std::formatter : std::formatter { + template + auto format(const NColorManagement::SImageDescription::SPCLuminances& luminances, FormatContext& ctx) const { + return std::format_to(ctx.out(), "[{}-{}({})]", luminances.min, luminances.max, luminances.reference); + } +}; + +template +struct std::formatter : std::formatter { + template + auto format(const NColorManagement::SImageDescription& imageDescription, FormatContext& ctx) const { + return std::format_to(ctx.out(), "[{}{}, primaries={}, luminances={}]", NColorManagement::tfToString(imageDescription.transferFunction), + imageDescription.transferFunctionPower != 1.0f ? std::format("^{}", imageDescription.transferFunctionPower) : "", imageDescription.getPrimaries(), + imageDescription.luminances); + } +}; -- cgit v1.2.3