From 418b7eecf6bae42a666a7b37754455c3b29ea174 Mon Sep 17 00:00:00 2001
From: Jaswant Panchumarti <jaswant.panchumarti@kitware.com>
Date: Wed, 17 Apr 2024 13:08:00 -0400
Subject: [PATCH] Add timer observer only in interactive mode

- otherwise the shader could produce a different image from the baseline
  in case the timer fired before vtkWindowToImageFilter captured a screenshot
- make the test produce a deterministic image instead of using random
  noise function
---
 .../OpenGL2/Testing/Cxx/TestUserShader2D.cxx  | 29 ++++++++++---------
 .../Data/Baseline/TestUserShader2D.png.sha512 |  2 +-
 2 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/Rendering/OpenGL2/Testing/Cxx/TestUserShader2D.cxx b/Rendering/OpenGL2/Testing/Cxx/TestUserShader2D.cxx
index 1545ded3b63..1b77ecc2e3c 100644
--- a/Rendering/OpenGL2/Testing/Cxx/TestUserShader2D.cxx
+++ b/Rendering/OpenGL2/Testing/Cxx/TestUserShader2D.cxx
@@ -71,22 +71,20 @@ int TestUserShader2D(int argc, char* argv[])
   coord->SetReferenceCoordinate(pCoord);
   mapper->SetTransformCoordinate(coord);
 
-  // render white noise (like a TV that displays noise signal)
+  // render an animation that zooms out of a grid (only visible in interactive mode)
   vtkShaderProperty* sp = actor->GetShaderProperty();
   sp->AddFragmentShaderReplacement(
     "//VTK::CustomUniforms::Dec", // replace the custom uniforms block
     true,                         // before the standard replacements
     R"(
 uniform float time;
-float generateRandom (vec2 st) { return fract(sin(dot(st.xy, vec2(12.9898,78.233) + sin(time))) * 43758.5453123); }
 )",
     false // only do it once
   );
   sp->AddFragmentShaderReplacement("//VTK::Color::Impl", // replace the color block
     true,                                                // before the standard replacements
     R"(
-float noise = mix(-1.0f, 1.0f, generateRandom(tcoordVCVSOutput));
-gl_FragData[0] = vec4(noise, noise, noise, 1.0);
+gl_FragData[0] = vec4(sin(tcoordVCVSOutput.xy * time * 0.01), 0.0, 1.0);
 )",                                                      // but we add this
     false                                                // only do it once
   );
@@ -103,21 +101,26 @@ gl_FragData[0] = vec4(noise, noise, noise, 1.0);
     return EXIT_FAILURE;
   }
 
-  TestUserShader2D_TimerData timerData;
-  timerData.shaderProperty = sp;
-  timerData.time = 0;
-  timerData.id = iren->CreateRepeatingTimer(10);
-
-  vtkNew<vtkCallbackCommand> timerCmd;
-  timerCmd->SetClientData(&timerData);
-  timerCmd->SetCallback(::TestUserShader2D_OnTimerCallback);
-  iren->AddObserver(vtkCommand::TimerEvent, timerCmd);
+  sp->GetFragmentCustomUniforms()->SetUniformf("time", 150);
 
   renderWindow->Render();
 
   int retVal = vtkRegressionTestImage(renderWindow);
   if (retVal == vtkRegressionTester::DO_INTERACTOR)
   {
+    auto* timerData = new TestUserShader2D_TimerData();
+    timerData->shaderProperty = sp;
+    timerData->time = 0;
+    timerData->id = iren->CreateRepeatingTimer(10);
+
+    vtkNew<vtkCallbackCommand> timerCmd;
+    timerCmd->SetClientData(timerData);
+    timerCmd->SetCallback(::TestUserShader2D_OnTimerCallback);
+    timerCmd->SetClientDataDeleteCallback([](void* dPtr) {
+      auto tdPtr = static_cast<TestUserShader2D_TimerData*>(dPtr);
+      delete tdPtr;
+    });
+    iren->AddObserver(vtkCommand::TimerEvent, timerCmd);
     iren->Start();
   }
 
diff --git a/Rendering/OpenGL2/Testing/Data/Baseline/TestUserShader2D.png.sha512 b/Rendering/OpenGL2/Testing/Data/Baseline/TestUserShader2D.png.sha512
index f35943e6bbc..c306c531d8c 100644
--- a/Rendering/OpenGL2/Testing/Data/Baseline/TestUserShader2D.png.sha512
+++ b/Rendering/OpenGL2/Testing/Data/Baseline/TestUserShader2D.png.sha512
@@ -1 +1 @@
-73375daf4d559467d69e10f116f45014c82aae34d6c571fda5d86c75f4c3d59fc4a31d35eee09cbdf24853606d2f196811c605ff1c8bd05d24c749584d73df30
+44d9c7f5bdec8f987bbdf23d98b3733ecbbc222a4d61d94f8a5c140e318c11b4bd3d12a277eb7f47e7243605b756c565868382c3239929abfcb26311a6d19441
-- 
GitLab