From 84bda112ac9048528ff85fa3588215fc540bb44b Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Thu, 13 Aug 2026 20:44:31 +1000 Subject: [PATCH] [idd] postprocess: centralize color transform state --- idd/LGIdd/Device.cpp | 5 +- idd/LGIdd/postprocess/CPostProcessor.cpp | 40 --------------- idd/LGIdd/postprocess/D12FrameFormat.cpp | 49 +++++++++++++++++++ idd/LGIdd/postprocess/D12FrameFormat.h | 4 +- .../effect/CColorTransformEffect.cpp | 16 +++--- idd/LGIdd/postprocess/effect/CRGB24Effect.cpp | 7 +-- 6 files changed, 63 insertions(+), 58 deletions(-) diff --git a/idd/LGIdd/Device.cpp b/idd/LGIdd/Device.cpp index e5d6ee4a..af9fdaae 100644 --- a/idd/LGIdd/Device.cpp +++ b/idd/LGIdd/Device.cpp @@ -241,13 +241,14 @@ NTSTATUS LGIddMonitorSetGammaRamp(IDDCX_MONITOR monitor, const IDARG_IN_SET_GAMM transform->lut[i][3] = 1.0f; } - if (IsIdentityColorTransform(*transform)) + auto active = D12::Transform(transform); + if (!active) { control.SetColorTransform(nullptr); return STATUS_SUCCESS; } - control.SetColorTransform(std::move(transform)); + control.SetColorTransform(std::move(active)); DEBUG_INFO("Display color transform updated (matrix:%d lut:%d)", input->MatrixEnabled, input->LutEnabled); return STATUS_SUCCESS; diff --git a/idd/LGIdd/postprocess/CPostProcessor.cpp b/idd/LGIdd/postprocess/CPostProcessor.cpp index 135b9258..4208c596 100644 --- a/idd/LGIdd/postprocess/CPostProcessor.cpp +++ b/idd/LGIdd/postprocess/CPostProcessor.cpp @@ -30,46 +30,6 @@ #include #include -namespace -{ - bool NearlyEqual(float a, float b, float tolerance) - { - const float delta = a - b; - return delta >= -tolerance && delta <= tolerance; - } -} - -bool IsIdentityColorTransform(const D12ColorTransform& transform) -{ - static const float matrixTolerance = 1.0f / 1048576.0f; - static const float lutTolerance = 1.0f / 65535.0f; - - if (transform.matrixEnabled) - { - for (unsigned row = 0; row < 3; ++row) - for (unsigned column = 0; column < 4; ++column) - { - const float expected = row == column ? 1.0f : 0.0f; - const float effective = - transform.matrix[row][column] * transform.scalar; - if (!NearlyEqual(effective, expected, matrixTolerance)) - return false; - } - } - - if (transform.lutEnabled) - for (unsigned i = 0; i < 4096; ++i) - { - const float expected = (float)i / 4095.0f; - if (!NearlyEqual(transform.lut[i][0], expected, lutTolerance) || - !NearlyEqual(transform.lut[i][1], expected, lutTolerance) || - !NearlyEqual(transform.lut[i][2], expected, lutTolerance)) - return false; - } - - return true; -} - bool CPostProcessor::Init(std::shared_ptr dx12Device, bool enableEffects) { diff --git a/idd/LGIdd/postprocess/D12FrameFormat.cpp b/idd/LGIdd/postprocess/D12FrameFormat.cpp index 7172e031..ca3616df 100644 --- a/idd/LGIdd/postprocess/D12FrameFormat.cpp +++ b/idd/LGIdd/postprocess/D12FrameFormat.cpp @@ -22,6 +22,55 @@ #include +namespace +{ + bool NearlyEqual(float a, float b, float tolerance) + { + const float delta = a - b; + return delta >= -tolerance && delta <= tolerance; + } + + bool Identity(const D12ColorTransform& transform) + { + static const float matrixTolerance = 1.0f / 1048576.0f; + static const float lutTolerance = 1.0f / 65535.0f; + + if (transform.matrixEnabled) + { + for (unsigned row = 0; row < 3; ++row) + for (unsigned column = 0; column < 4; ++column) + { + const float expected = row == column ? 1.0f : 0.0f; + const float effective = + transform.matrix[row][column] * transform.scalar; + if (!NearlyEqual(effective, expected, matrixTolerance)) + return false; + } + } + + if (transform.lutEnabled) + for (unsigned i = 0; i < 4096; ++i) + { + const float expected = static_cast(i) / 4095.0f; + if (!NearlyEqual(transform.lut[i][0], expected, lutTolerance) || + !NearlyEqual(transform.lut[i][1], expected, lutTolerance) || + !NearlyEqual(transform.lut[i][2], expected, lutTolerance)) + return false; + } + + return true; + } +} + +std::shared_ptr D12::Transform( + const std::shared_ptr& transform) +{ + if (!transform || (!transform->matrixEnabled && !transform->lutEnabled) || + Identity(*transform)) + return nullptr; + return transform; +} + FrameType D12::Type(DXGI_FORMAT format) { switch (format) diff --git a/idd/LGIdd/postprocess/D12FrameFormat.h b/idd/LGIdd/postprocess/D12FrameFormat.h index 942d1551..0b8694e2 100644 --- a/idd/LGIdd/postprocess/D12FrameFormat.h +++ b/idd/LGIdd/postprocess/D12FrameFormat.h @@ -38,8 +38,6 @@ struct D12ColorTransform float lut[4096][4] = {}; }; -bool IsIdentityColorTransform(const D12ColorTransform& transform); - struct D12FrameFormat { D3D12_RESOURCE_DESC desc = {}; @@ -89,6 +87,8 @@ namespace D12 FrameType Type(DXGI_FORMAT format); void CopyHdr(D12FrameFormat& dst, const D12FrameFormat& src); + std::shared_ptr Transform( + const std::shared_ptr& transform); bool Same(const D3D12_RESOURCE_DESC& left, const D3D12_RESOURCE_DESC& right, DescCmp cmp = DescCmp::EXACT); diff --git a/idd/LGIdd/postprocess/effect/CColorTransformEffect.cpp b/idd/LGIdd/postprocess/effect/CColorTransformEffect.cpp index 6bdd7059..f3c8d280 100644 --- a/idd/LGIdd/postprocess/effect/CColorTransformEffect.cpp +++ b/idd/LGIdd/postprocess/effect/CColorTransformEffect.cpp @@ -208,8 +208,8 @@ PostProcessStatus CColorTransformEffect::SetFormat( const ComPtr& device, const D12FrameFormat& src, D12FrameFormat& dst) { - if (!src.colorTransform || IsIdentityColorTransform(*src.colorTransform) || - (!src.colorTransform->matrixEnabled && !src.colorTransform->lutEnabled)) + const auto transform = D12::Transform(src.colorTransform); + if (!transform) return PostProcessStatus::BYPASS_EFFECT; DXGI_FORMAT dstFormat; @@ -247,16 +247,16 @@ PostProcessStatus CColorTransformEffect::SetFormat( return PostProcessStatus::FAILED; } - std::memcpy(m_consts.matrix, src.colorTransform->matrix, + std::memcpy(m_consts.matrix, transform->matrix, sizeof(m_consts.matrix)); - m_consts.scalar = src.colorTransform->scalar; - m_consts.matrixEnabled = src.colorTransform->matrixEnabled; - m_consts.lutEnabled = src.colorTransform->lutEnabled; - m_consts.inputTransfer = src.hdrPQ ? TRANSFER_PQ : + m_consts.scalar = transform->scalar; + m_consts.matrixEnabled = transform->matrixEnabled; + m_consts.lutEnabled = transform->lutEnabled; + m_consts.inputTransfer = src.hdrPQ ? TRANSFER_PQ : (src.hdr ? TRANSFER_LINEAR : TRANSFER_SRGB); m_consts.outputTransfer = src.hdr ? TRANSFER_PQ : TRANSFER_SRGB; - std::memcpy(m_lut, src.colorTransform->lut, sizeof(m_lut)); + std::memcpy(m_lut, transform->lut, sizeof(m_lut)); m_uploadPending = true; m_srcFormat = src.desc.Format; diff --git a/idd/LGIdd/postprocess/effect/CRGB24Effect.cpp b/idd/LGIdd/postprocess/effect/CRGB24Effect.cpp index 38e0a73c..55daea45 100644 --- a/idd/LGIdd/postprocess/effect/CRGB24Effect.cpp +++ b/idd/LGIdd/postprocess/effect/CRGB24Effect.cpp @@ -83,12 +83,7 @@ struct CRGB24Effect::State format.desc.Format != DXGI_FORMAT_B8G8R8A8_UNORM) return false; - if (!format.colorTransform || - (!format.colorTransform->matrixEnabled && - !format.colorTransform->lutEnabled)) - return true; - - return IsIdentityColorTransform(*format.colorTransform); + return !D12::Transform(format.colorTransform); } bool WantsPackedLocked() const