Fix color grading lut to be refreshed when shader gets reloaded

This commit is contained in:
Wojtek Figat
2025-10-28 23:19:51 +01:00
parent c4fcaa999c
commit 05a8c841da
5 changed files with 29 additions and 16 deletions

BIN
Content/Shaders/ColorGrading.flax (Stored with Git LFS)

Binary file not shown.

View File

@@ -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");

View File

@@ -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:

View File

@@ -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);
}

View File

@@ -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)