From 05a8c841da9067bed66f09a7ad1bfc8cf27e8b9f Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Tue, 28 Oct 2025 23:19:51 +0100 Subject: [PATCH] Fix color grading lut to be refreshed when shader gets reloaded --- Content/Shaders/ColorGrading.flax | 4 ++-- Source/Engine/Renderer/ColorGradingPass.cpp | 20 ++++++++++++++++++++ Source/Engine/Renderer/ColorGradingPass.h | 7 ++----- Source/Shaders/ColorGrading.shader | 7 +++---- Source/Shaders/GammaCorrectionCommon.hlsl | 7 ++----- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/Content/Shaders/ColorGrading.flax b/Content/Shaders/ColorGrading.flax index 15f723620..716e18593 100644 --- a/Content/Shaders/ColorGrading.flax +++ b/Content/Shaders/ColorGrading.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:ad1d9597dd930f0b63358dbe8326d131661f9cc2251181eeec0cb44ffc530d7a -size 12311 +oid sha256:bce57f0ccf6d808985f4d79cc4e15d85cae999bee598d8e93ff5b1b126bc42b8 +size 12310 diff --git a/Source/Engine/Renderer/ColorGradingPass.cpp b/Source/Engine/Renderer/ColorGradingPass.cpp index bbb45ef4c..d6e164622 100644 --- a/Source/Engine/Renderer/ColorGradingPass.cpp +++ b/Source/Engine/Renderer/ColorGradingPass.cpp @@ -47,6 +47,9 @@ public: Data CachedData; ToneMappingMode Mode = ToneMappingMode::None; Texture* LutTexture = nullptr; +#if COMPILE_WITH_DEV_ENV + uint64 FrameRendered = 0; +#endif ~ColorGradingCustomBuffer() { @@ -54,6 +57,17 @@ public: } }; +#if COMPILE_WITH_DEV_ENV + +void ColorGradingPass::OnShaderReloading(Asset* obj) +{ + _psLut.Release(); + invalidateResources(); + _reloadedFrame = Engine::FrameCount; +} + +#endif + String ColorGradingPass::ToString() const { return TEXT("ColorGradingPass"); @@ -194,6 +208,9 @@ GPUTexture* ColorGradingPass::RenderLUT(RenderContext& renderContext) // Check if LUT parameter hasn't been changed since the last time if (Platform::MemoryCompare(&colorGradingBuffer.CachedData , &data, sizeof(Data)) == 0 && colorGradingBuffer.Mode == toneMapping.Mode && +#if COMPILE_WITH_DEV_ENV + colorGradingBuffer.FrameRendered > _reloadedFrame && +#endif Engine::FrameCount > 30 && // Skip caching when engine is starting TODO: find why this hack is needed colorGradingBuffer.LutTexture == lutTexture) { @@ -203,6 +220,9 @@ GPUTexture* ColorGradingPass::RenderLUT(RenderContext& renderContext) colorGradingBuffer.CachedData = data; colorGradingBuffer.Mode = toneMapping.Mode; colorGradingBuffer.LutTexture = lutTexture; +#if COMPILE_WITH_DEV_ENV + colorGradingBuffer.FrameRendered = Engine::FrameCount; +#endif // Render LUT PROFILE_GPU("Color Grading LUT"); diff --git a/Source/Engine/Renderer/ColorGradingPass.h b/Source/Engine/Renderer/ColorGradingPass.h index 612940e76..dd17e7786 100644 --- a/Source/Engine/Renderer/ColorGradingPass.h +++ b/Source/Engine/Renderer/ColorGradingPass.h @@ -25,11 +25,8 @@ public: private: #if COMPILE_WITH_DEV_ENV - void OnShaderReloading(Asset* obj) - { - _psLut.Release(); - invalidateResources(); - } + uint64 _reloadedFrame = 0; + void OnShaderReloading(Asset* obj); #endif public: diff --git a/Source/Shaders/ColorGrading.shader b/Source/Shaders/ColorGrading.shader index 9d1d71697..fe2ca4c91 100644 --- a/Source/Shaders/ColorGrading.shader +++ b/Source/Shaders/ColorGrading.shader @@ -164,16 +164,14 @@ float3 TonemapACES(float3 linearColor) // The code was originally written by Stephen Hill (@self_shadow). // sRGB => XYZ => D65_2_D60 => AP1 => RRT_SAT - static const float3x3 ACESInputMat = - { + static const float3x3 ACESInputMat = { {0.59719, 0.35458, 0.04823}, {0.07600, 0.90834, 0.01566}, {0.02840, 0.13383, 0.83777} }; // ODT_SAT => XYZ => D60_2_D65 => sRGB - static const float3x3 ACESOutputMat = - { + static const float3x3 ACESOutputMat = { { 1.60475, -0.53108, -0.07367}, {-0.10208, 1.10813, -0.00605}, {-0.00327, -0.07276, 1.07602} @@ -188,6 +186,7 @@ float3 TonemapACES(float3 linearColor) color = a / b; color = mul(ACESOutputMat, color); + return saturate(color); } diff --git a/Source/Shaders/GammaCorrectionCommon.hlsl b/Source/Shaders/GammaCorrectionCommon.hlsl index b687f9166..e410400ad 100644 --- a/Source/Shaders/GammaCorrectionCommon.hlsl +++ b/Source/Shaders/GammaCorrectionCommon.hlsl @@ -37,7 +37,7 @@ float4 FastTonemapInvert(float4 c) return float4(FastTonemapInvert(c.rgb), c.a); } -float LinearToSrgbChannel(float linearColor) +float LinearToSrgb(float linearColor) { if (linearColor < 0.00313067) return linearColor * 12.92; @@ -46,10 +46,7 @@ float LinearToSrgbChannel(float linearColor) float3 LinearToSrgb(float3 linearColor) { - return float3( - LinearToSrgbChannel(linearColor.r), - LinearToSrgbChannel(linearColor.g), - LinearToSrgbChannel(linearColor.b)); + return float3(LinearToSrgb(linearColor.r), LinearToSrgb(linearColor.g), LinearToSrgb(linearColor.b)); } float3 sRGBToLinear(float3 color)