ScriptManager.cpp 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  1. /*
  2. * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
  3. * its licensors.
  4. *
  5. * For complete copyright and license terms please see the LICENSE at the root of this
  6. * distribution (the "License"). All use of this software is governed by the License,
  7. * or, if provided, by the license below or the license accompanying this file. Do not
  8. * remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. *
  11. */
  12. #include <Automation/ScriptManager.h>
  13. #include <Automation/ScriptableImGui.h>
  14. #include <SampleComponentManagerBus.h>
  15. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  16. #include <Atom/RPI.Public/RPISystemInterface.h>
  17. #include <Atom/Component/DebugCamera/CameraComponent.h>
  18. #include <Atom/Component/DebugCamera/NoClipControllerComponent.h>
  19. #include <Atom/Component/DebugCamera/ArcBallControllerComponent.h>
  20. #include <Atom/Feature/ImGui/SystemBus.h>
  21. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  22. #include <AzCore/Component/Entity.h>
  23. #include <AzCore/Script/ScriptContext.h>
  24. #include <AzCore/Script/ScriptSystemBus.h>
  25. #include <AzCore/Script/ScriptAsset.h>
  26. #include <AzCore/Math/MathReflection.h>
  27. #include <AzCore/Console/IConsole.h>
  28. #include <AzFramework/API/ApplicationAPI.h>
  29. #include <AzFramework/Components/ConsoleBus.h>
  30. #include <AzFramework/IO/LocalFileIO.h>
  31. #include <AzFramework/Windowing/WindowBus.h>
  32. #include <AtomCore/Serialization/Json/JsonUtils.h>
  33. #include <AtomSampleViewerRequestBus.h>
  34. #include <Utils/Utils.h>
  35. namespace AtomSampleViewer
  36. {
  37. ScriptManager* ScriptManager::s_instance = nullptr;
  38. ScriptManager::ScriptManager()
  39. : m_scriptBrowser("@user@/lua_script_browser.xml")
  40. {
  41. }
  42. void ScriptManager::Activate()
  43. {
  44. AZ_Assert(s_instance == nullptr, "ScriptManager is already activated");
  45. s_instance = this;
  46. ScriptableImGui::Create();
  47. m_scriptContext = AZStd::make_unique<AZ::ScriptContext>();
  48. m_sriptBehaviorContext = AZStd::make_unique<AZ::BehaviorContext>();
  49. ReflectScriptContext(m_sriptBehaviorContext.get());
  50. m_scriptContext->BindTo(m_sriptBehaviorContext.get());
  51. m_scriptBrowser.SetFilter([this](const AZ::Data::AssetInfo& assetInfo)
  52. {
  53. return AzFramework::StringFunc::EndsWith(assetInfo.m_relativePath, ".bv.luac");
  54. });
  55. m_scriptBrowser.Activate();
  56. ScriptRepeaterRequestBus::Handler::BusConnect();
  57. ScriptRunnerRequestBus::Handler::BusConnect();
  58. m_imageComparisonOptions.Activate();
  59. }
  60. void ScriptManager::Deactivate()
  61. {
  62. s_instance = nullptr;
  63. m_scriptContext = nullptr;
  64. m_sriptBehaviorContext = nullptr;
  65. m_scriptBrowser.Deactivate();
  66. ScriptableImGui::Destory();
  67. m_imageComparisonOptions.Deactivate();
  68. ScriptRunnerRequestBus::Handler::BusDisconnect();
  69. ScriptRepeaterRequestBus::Handler::BusDisconnect();
  70. AZ::Debug::CameraControllerNotificationBus::Handler::BusDisconnect();
  71. }
  72. void ScriptManager::SetCameraEntity(AZ::Entity* cameraEntity)
  73. {
  74. AZ::Debug::CameraControllerNotificationBus::Handler::BusDisconnect();
  75. m_cameraEntity = cameraEntity;
  76. AZ::Debug::CameraControllerNotificationBus::Handler::BusConnect(m_cameraEntity->GetId());
  77. }
  78. void ScriptManager::PauseScript()
  79. {
  80. PauseScriptWithTimeout(DefaultPauseTimeout);
  81. }
  82. void ScriptManager::PauseScriptWithTimeout(float timeout)
  83. {
  84. m_scriptPaused = true;
  85. m_scriptPauseTimeout = AZ::GetMax(timeout, m_scriptPauseTimeout);
  86. }
  87. void ScriptManager::ResumeScript()
  88. {
  89. AZ_Warning("Automation", m_scriptPaused, "Script is not paused");
  90. m_scriptPaused = false;
  91. }
  92. void ScriptManager::ReportScriptError([[maybe_unused]] const AZStd::string& message)
  93. {
  94. AZ_Error("Automation", false, "Script: %s", message.c_str());
  95. }
  96. void ScriptManager::ReportScriptWarning([[maybe_unused]] const AZStd::string& message)
  97. {
  98. AZ_Warning("Automation", false, "Script: %s", message.c_str());
  99. }
  100. void ScriptManager::ReportScriptableAction([[maybe_unused]] AZStd::string_view scriptCommand)
  101. {
  102. AZ_TracePrintf("Automation", "Scriptable Action: %.*s\n", AZ_STRING_ARG(scriptCommand));
  103. }
  104. void ScriptManager::ImageComparisonOptions::Activate()
  105. {
  106. m_configAsset = AZ::RPI::AssetUtils::LoadAssetByProductPath<AZ::RPI::AnyAsset>("config/ImageComparisonConfig.azasset", AZ::RPI::AssetUtils::TraceLevel::Assert);
  107. if (m_configAsset)
  108. {
  109. AZ::Data::AssetBus::Handler::BusConnect(m_configAsset.GetId());
  110. OnAssetReloaded(m_configAsset);
  111. }
  112. }
  113. void ScriptManager::ImageComparisonOptions::Deactivate()
  114. {
  115. m_configAsset.Release();
  116. ResetImGuiSettings();
  117. AZ::Data::AssetBus::Handler::BusDisconnect();
  118. }
  119. ImageComparisonToleranceLevel* ScriptManager::ImageComparisonOptions::FindToleranceLevel(const AZStd::string& name, bool allowLevelAdjustment)
  120. {
  121. size_t foundIndex = m_config.m_toleranceLevels.size();
  122. for (size_t i = 0; i < m_config.m_toleranceLevels.size(); ++i)
  123. {
  124. if (m_config.m_toleranceLevels[i].m_name == name)
  125. {
  126. foundIndex = i;
  127. break;
  128. }
  129. }
  130. if (foundIndex == m_config.m_toleranceLevels.size())
  131. {
  132. return nullptr;
  133. }
  134. if (allowLevelAdjustment)
  135. {
  136. int adjustedIndex = aznumeric_cast<int>(foundIndex);
  137. adjustedIndex += m_toleranceAdjustment;
  138. adjustedIndex = AZ::GetClamp(adjustedIndex, 0, aznumeric_cast<int>(m_config.m_toleranceLevels.size()) - 1);
  139. foundIndex = aznumeric_cast<size_t>(adjustedIndex);
  140. }
  141. return &m_config.m_toleranceLevels[foundIndex];
  142. }
  143. const AZStd::vector<ImageComparisonToleranceLevel>& ScriptManager::ImageComparisonOptions::GetAvailableToleranceLevels() const
  144. {
  145. return m_config.m_toleranceLevels;
  146. }
  147. void ScriptManager::ImageComparisonOptions::SelectToleranceLevel(const AZStd::string& name, bool allowLevelAdjustment)
  148. {
  149. if (m_selectedOverrideSetting == OverrideSetting_ScriptControlled)
  150. {
  151. ImageComparisonToleranceLevel* level = FindToleranceLevel(name, allowLevelAdjustment);
  152. if (level)
  153. {
  154. m_currentToleranceLevel = level;
  155. }
  156. else
  157. {
  158. ReportScriptError(AZStd::string::format("ImageComparisonToleranceLevel '%s' not found.", name.c_str()));
  159. }
  160. }
  161. }
  162. void ScriptManager::ImageComparisonOptions::SelectToleranceLevel(ImageComparisonToleranceLevel* level)
  163. {
  164. if (nullptr == level)
  165. {
  166. m_currentToleranceLevel = level;
  167. return;
  168. }
  169. else
  170. {
  171. SelectToleranceLevel(level->m_name);
  172. AZ_Assert(GetCurrentToleranceLevel() == level, "Wrong ImageComparisonToleranceLevel pointer used");
  173. }
  174. }
  175. ImageComparisonToleranceLevel* ScriptManager::ImageComparisonOptions::GetCurrentToleranceLevel()
  176. {
  177. return m_currentToleranceLevel;
  178. }
  179. bool ScriptManager::ImageComparisonOptions::IsScriptControlled() const
  180. {
  181. return m_selectedOverrideSetting == OverrideSetting_ScriptControlled;
  182. }
  183. bool ScriptManager::ImageComparisonOptions::IsLevelAdjusted() const
  184. {
  185. return m_toleranceAdjustment != 0;
  186. }
  187. void ScriptManager::ImageComparisonOptions::DrawImGuiSettings()
  188. {
  189. ImGui::Text("Tolerance");
  190. ImGui::Indent();
  191. if (ImGui::Combo("Level",
  192. &m_selectedOverrideSetting,
  193. m_overrideSettings.data(),
  194. aznumeric_cast<int>(m_overrideSettings.size())))
  195. {
  196. if (m_selectedOverrideSetting == OverrideSetting_ScriptControlled)
  197. {
  198. m_currentToleranceLevel = nullptr;
  199. }
  200. else
  201. {
  202. m_currentToleranceLevel = &m_config.m_toleranceLevels[m_selectedOverrideSetting - 1];
  203. }
  204. }
  205. if (IsScriptControlled())
  206. {
  207. ImGui::InputInt("Level Adjustment", &m_toleranceAdjustment);
  208. }
  209. ImGui::Unindent();
  210. }
  211. void ScriptManager::ImageComparisonOptions::ResetImGuiSettings()
  212. {
  213. m_currentToleranceLevel = nullptr;
  214. m_selectedOverrideSetting = OverrideSetting_ScriptControlled;
  215. m_toleranceAdjustment = 0;
  216. }
  217. void ScriptManager::ImageComparisonOptions::OnAssetReloaded(AZ::Data::Asset<AZ::Data::AssetData> asset)
  218. {
  219. m_configAsset = asset;
  220. m_config = *m_configAsset->GetDataAs<ImageComparisonConfig>();
  221. m_overrideSettings.clear();
  222. m_overrideSettings.push_back("[Script-controlled]");
  223. for (size_t i = 0; i < m_config.m_toleranceLevels.size(); ++i)
  224. {
  225. AZ_Assert(i == 0 || m_config.m_toleranceLevels[i].m_threshold > m_config.m_toleranceLevels[i - 1].m_threshold, "Threshold values are not sequential");
  226. m_overrideSettings.push_back(m_config.m_toleranceLevels[i].m_name.c_str());
  227. }
  228. }
  229. void ScriptManager::TickImGui()
  230. {
  231. if (m_showScriptRunnerDialog)
  232. {
  233. ShowScriptRunnerDialog();
  234. }
  235. m_scriptReporter.TickImGui();
  236. if (m_testSuiteRunConfig.m_automatedRunEnabled)
  237. {
  238. if (m_testSuiteRunConfig.m_isStarted == false)
  239. {
  240. m_testSuiteRunConfig.m_isStarted = true;
  241. PrepareAndExecuteScript(m_testSuiteRunConfig.m_testSuitePath);
  242. }
  243. }
  244. }
  245. void ScriptManager::TickScript(float deltaTime)
  246. {
  247. // All actions must be consumed each frame. Otherwise, this indicates that a script is
  248. // scheduling ScriptableImGui actions for fields that don't exist.
  249. ScriptableImGui::CheckAllActionsConsumed();
  250. ScriptableImGui::ClearActions();
  251. // We delayed PopScript() until after the above CheckAllActionsConsumed(), so that any errors
  252. // reported by that function will be associated with the proper script.
  253. if (m_shouldPopScript)
  254. {
  255. m_scriptReporter.PopScript();
  256. m_shouldPopScript = false;
  257. }
  258. while (!m_scriptOperations.empty())
  259. {
  260. if (m_shouldPopScript)
  261. {
  262. // If we just finished executing a script, the remaining m_scriptOperations are for some other script.
  263. // We need to proceed to the next frame and allow that PopScript() to happen, otherwise any errors related
  264. // to subsequent operations would be reported against the prior script.
  265. break;
  266. }
  267. if (m_scriptPaused)
  268. {
  269. m_scriptPauseTimeout -= deltaTime;
  270. if (m_scriptPauseTimeout < 0)
  271. {
  272. AZ_Error("Automation", false, "Script pause timed out. Continuing...");
  273. m_scriptPaused = false;
  274. }
  275. else
  276. {
  277. break;
  278. }
  279. }
  280. if (m_waitForAssetTracker)
  281. {
  282. m_assetTrackingTimeout -= deltaTime;
  283. if (m_assetTrackingTimeout < 0)
  284. {
  285. AZ_Error("Automation", false, "Script asset tracking timed out. Continuing...");
  286. m_waitForAssetTracker = false;
  287. }
  288. else if (m_assetStatusTracker.DidExpectedAssetsFinish())
  289. {
  290. m_waitForAssetTracker = false;
  291. }
  292. else
  293. {
  294. break;
  295. }
  296. }
  297. if (m_scriptIdleFrames > 0)
  298. {
  299. m_scriptIdleFrames--;
  300. break;
  301. }
  302. if (m_scriptIdleSeconds > 0)
  303. {
  304. m_scriptIdleSeconds -= deltaTime;
  305. break;
  306. }
  307. // Execute the next operation
  308. m_scriptOperations.front()();
  309. m_scriptOperations.pop();
  310. if (m_scriptOperations.empty())
  311. {
  312. m_doFinalScriptCleanup = true;
  313. }
  314. }
  315. if (m_shouldPopScript)
  316. {
  317. // We need to proceed for one more frame to do the last PopScript() before final cleanup
  318. return;
  319. }
  320. if (m_doFinalScriptCleanup)
  321. {
  322. bool frameCapturePending = false;
  323. SampleComponentManagerRequestBus::BroadcastResult(frameCapturePending, &SampleComponentManagerRequests::IsFrameCapturePending);
  324. if (!frameCapturePending && !m_isCapturePending)
  325. {
  326. AZ_Assert(m_scriptPaused == false, "Script manager is in an unexpected state.");
  327. AZ_Assert(m_scriptIdleFrames == 0, "Script manager is in an unexpected state.");
  328. AZ_Assert(m_scriptIdleSeconds <= 0.0f, "Script manager is in an unexpected state.");
  329. AZ_Assert(m_waitForAssetTracker == false, "Script manager is in an unexpected state.");
  330. AZ_Assert(!m_scriptReporter.HasActiveScript(), "Script manager is in an unexpected state.");
  331. AZ_Assert(m_executingScripts.size() == 0, "Script manager is in an unexpected state");
  332. m_assetStatusTracker.StopTracking();
  333. if (m_frameTimeIsLocked)
  334. {
  335. AZ::Interface<AZ::IConsole>::Get()->PerformCommand("t_frameTimeOverride 0");
  336. m_frameTimeIsLocked = false;
  337. }
  338. if (m_shouldRestoreViewportSize)
  339. {
  340. Utils::ResizeClientArea(m_savedViewportWidth, m_savedViewportHeight);
  341. m_shouldRestoreViewportSize = false;
  342. }
  343. // In case scripts were aborted while ImGui was temporarily hidden, show it again.
  344. SetShowImGui(true);
  345. m_scriptReporter.OpenReportDialog();
  346. m_shouldPopScript = false;
  347. m_doFinalScriptCleanup = false;
  348. if (m_testSuiteRunConfig.m_automatedRunEnabled && m_testSuiteRunConfig.m_closeOnTestScriptFinish)
  349. {
  350. m_testSuiteRunConfig.m_automatedRunEnabled = false;
  351. if (m_scriptReporter.HasErrorsAssertsInReport())
  352. {
  353. AtomSampleViewerRequestsBus::Broadcast(&AtomSampleViewerRequestsBus::Events::SetExitCode, 1);
  354. // Useful console logging for Hydra tests
  355. int failedTests = 0;
  356. for (const ScriptReporter::ScriptReport& testReport : m_scriptReporter.GetScriptReport())
  357. {
  358. if (testReport.m_assertCount > 0 || testReport.m_generalErrorCount > 0 || testReport.m_screenshotErrorCount > 0)
  359. {
  360. ++failedTests;
  361. AZ_Printf("AtomSampleViewer", "Test failure %s: asserts %u, general errors %u, screenshot failures %u\n", testReport.m_scriptAssetPath.c_str(),
  362. testReport.m_assertCount, testReport.m_generalErrorCount, testReport.m_screenshotErrorCount);
  363. }
  364. }
  365. AZ_Printf("AtomSampleViewer", "%d tests failed\n", failedTests);
  366. }
  367. AzFramework::ApplicationRequests::Bus::Broadcast(&AzFramework::ApplicationRequests::ExitMainLoop);
  368. }
  369. }
  370. }
  371. }
  372. void ScriptManager::OpenScriptRunnerDialog()
  373. {
  374. m_showScriptRunnerDialog = true;
  375. }
  376. void ScriptManager::RunMainTestSuite(const AZStd::string& suiteFilePath, bool exitOnTestEnd, int randomSeed)
  377. {
  378. m_testSuiteRunConfig.m_automatedRunEnabled = true;
  379. m_testSuiteRunConfig.m_testSuitePath = suiteFilePath;
  380. m_testSuiteRunConfig.m_closeOnTestScriptFinish = exitOnTestEnd;
  381. m_testSuiteRunConfig.m_randomSeed = randomSeed;
  382. }
  383. void ScriptManager::AbortScripts(const AZStd::string& reason)
  384. {
  385. m_scriptReporter.SetInvalidationMessage(reason);
  386. m_scriptOperations = {};
  387. m_executingScripts.clear();
  388. m_scriptPaused = false;
  389. m_scriptIdleFrames = 0;
  390. m_scriptIdleSeconds = 0.0f;
  391. m_waitForAssetTracker = false;
  392. while (m_scriptReporter.HasActiveScript())
  393. {
  394. m_scriptReporter.PopScript();
  395. }
  396. m_doFinalScriptCleanup = true;
  397. }
  398. void ScriptManager::ShowScriptRunnerDialog()
  399. {
  400. if (ImGui::Begin("Script Runner", &m_showScriptRunnerDialog))
  401. {
  402. auto drawAbortButton = [this](const char* uniqueId)
  403. {
  404. ImGui::PushID(uniqueId);
  405. if (ImGui::Button("Abort"))
  406. {
  407. AbortScripts("Script(s) manually aborted.");
  408. }
  409. ImGui::PopID();
  410. };
  411. // The main buttons are at the bottom, but show the Abort button at the top too, in case the window size is small.
  412. if (!m_scriptOperations.empty())
  413. {
  414. drawAbortButton("Button1");
  415. }
  416. ImGuiAssetBrowser::WidgetSettings assetBrowserSettings;
  417. assetBrowserSettings.m_labels.m_root = "Lua Scripts";
  418. m_scriptBrowser.Tick(assetBrowserSettings);
  419. AZStd::string selectedFileName = "<none>";
  420. AzFramework::StringFunc::Path::GetFullFileName(m_scriptBrowser.GetSelectedAssetPath().c_str(), selectedFileName);
  421. ImGui::LabelText("##SelectedScript", "Selected: %s", selectedFileName.c_str());
  422. ImGui::Separator();
  423. ImGui::Text("Settings");
  424. ImGui::Indent();
  425. ImGui::InputInt("Random Seed for Test Order Execution", &m_testSuiteRunConfig.m_randomSeed);
  426. m_imageComparisonOptions.DrawImGuiSettings();
  427. if (ImGui::Button("Reset"))
  428. {
  429. m_imageComparisonOptions.ResetImGuiSettings();
  430. }
  431. ImGui::Unindent();
  432. ImGui::Separator();
  433. if (ImGui::Button("Run"))
  434. {
  435. auto scriptAsset = m_scriptBrowser.GetSelectedAsset<AZ::ScriptAsset>();
  436. if (scriptAsset.GetId().IsValid())
  437. {
  438. PrepareAndExecuteScript(m_scriptBrowser.GetSelectedAssetPath());
  439. }
  440. }
  441. if (ImGui::Button("View Latest Results"))
  442. {
  443. m_scriptReporter.OpenReportDialog();
  444. }
  445. if (m_scriptOperations.size() > 0)
  446. {
  447. ImGui::LabelText("##RunningScript", "Running %zu operations...", m_scriptOperations.size());
  448. drawAbortButton("Button2");
  449. }
  450. }
  451. m_messageBox.TickPopup();
  452. ImGui::End();
  453. }
  454. void ScriptManager::PrepareAndExecuteScript(const AZStd::string& scriptFilePath)
  455. {
  456. ReportScriptableAction(AZStd::string::format("RunScript('%s')", scriptFilePath.c_str()));
  457. // Save the window size so we can restore it after running the script, in case the script calls ResizeViewport
  458. AzFramework::NativeWindowHandle defaultWindowHandle;
  459. AzFramework::WindowSize windowSize;
  460. AzFramework::WindowSystemRequestBus::BroadcastResult(defaultWindowHandle, &AzFramework::WindowSystemRequestBus::Events::GetDefaultWindowHandle);
  461. AzFramework::WindowRequestBus::EventResult(windowSize, defaultWindowHandle, &AzFramework::WindowRequests::GetClientAreaSize);
  462. m_savedViewportWidth = windowSize.m_width;
  463. m_savedViewportHeight = windowSize.m_height;
  464. if (m_savedViewportWidth == 0 || m_savedViewportHeight == 0)
  465. {
  466. AZ_Assert(false, "Could not get current window size");
  467. }
  468. else
  469. {
  470. m_shouldRestoreViewportSize = true;
  471. }
  472. // Setup the ScriptReporter to track and report the results
  473. m_scriptReporter.Reset();
  474. m_scriptReporter.SetAvailableToleranceLevels(m_imageComparisonOptions.GetAvailableToleranceLevels());
  475. if (m_imageComparisonOptions.IsLevelAdjusted())
  476. {
  477. m_scriptReporter.SetInvalidationMessage("Results are invalid because the tolerance level has been adjusted.");
  478. }
  479. else if (!m_imageComparisonOptions.IsScriptControlled())
  480. {
  481. m_scriptReporter.SetInvalidationMessage("Results are invalid because the tolerance level has been overridden.");
  482. }
  483. else
  484. {
  485. m_scriptReporter.SetInvalidationMessage("");
  486. }
  487. AZ_Assert(m_executingScripts.empty(), "There should be no active scripts at this point");
  488. ExecuteScript(scriptFilePath);
  489. }
  490. void ScriptManager::ExecuteScript(const AZStd::string& scriptFilePath)
  491. {
  492. AZ::Data::Asset<AZ::ScriptAsset> scriptAsset = AZ::RPI::AssetUtils::LoadAssetByProductPath<AZ::ScriptAsset>(scriptFilePath.c_str());
  493. if (!scriptAsset)
  494. {
  495. // Push an error operation on the back of the queue instead of reporting it immediately so it doesn't get lost
  496. // in front of a bunch of queued m_scriptOperations.
  497. Script_Error(AZStd::string::format("Could not find or load script asset '%s'.", scriptFilePath.c_str()));
  498. return;
  499. }
  500. if (s_instance->m_executingScripts.find(scriptAsset.GetId()) != s_instance->m_executingScripts.end())
  501. {
  502. Script_Error(AZStd::string::format("Calling script '%s' would likely cause an infinite loop and crash. Skipping.", scriptFilePath.c_str()));
  503. return;
  504. }
  505. if (s_instance->m_imageComparisonOptions.IsScriptControlled())
  506. {
  507. s_instance->m_imageComparisonOptions.SelectToleranceLevel(nullptr); // Clear the preset before each script to make sure the script is selecting it.
  508. }
  509. // Execute(script) will add commands to the m_scriptOperations. These should be considered part of their own test script, for reporting purposes.
  510. s_instance->m_scriptOperations.push([scriptFilePath]()
  511. {
  512. s_instance->m_scriptReporter.PushScript(scriptFilePath);
  513. }
  514. );
  515. s_instance->m_scriptOperations.push([scriptFilePath]()
  516. {
  517. AZ_Printf("Automation", "Running script '%s'...\n", scriptFilePath.c_str());
  518. }
  519. );
  520. s_instance->m_executingScripts.insert(scriptAsset.GetId());
  521. if (!s_instance->m_scriptContext->Execute(scriptAsset->GetScriptBuffer().data(), scriptFilePath.c_str(), scriptAsset->GetScriptBuffer().size()))
  522. {
  523. // Push an error operation on the back of the queue instead of reporting it immediately so it doesn't get lost
  524. // in front of a bunch of queued m_scriptOperations.
  525. Script_Error(AZStd::string::format("Error running script '%s'.", scriptAsset.ToString<AZStd::string>().c_str()));
  526. }
  527. s_instance->m_executingScripts.erase(scriptAsset.GetId());
  528. // Execute(script) will have added commands to the m_scriptOperations. When they finish, consider this test as completed, for reporting purposes.
  529. s_instance->m_scriptOperations.push([]()
  530. {
  531. // We don't call m_scriptReporter.PopScript() yet because some cleanup needs to happen in TickScript() on the next frame.
  532. AZ_Assert(!s_instance->m_shouldPopScript, "m_shouldPopScript is already true");
  533. s_instance->m_shouldPopScript = true;
  534. }
  535. );
  536. }
  537. void ScriptManager::OnCameraMoveEnded(AZ::TypeId controllerTypeId, uint32_t channels)
  538. {
  539. if (controllerTypeId == azrtti_typeid<AZ::Debug::ArcBallControllerComponent>())
  540. {
  541. if (channels & AZ::Debug::ArcBallControllerChannel_Center)
  542. {
  543. AZ::Vector3 center = AZ::Vector3::CreateZero();
  544. AZ::Debug::ArcBallControllerRequestBus::EventResult(center, m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequests::GetCenter);
  545. ReportScriptableAction(AZStd::string::format("ArcBallCameraController_SetCenter(Vector3(%f, %f, %f))", (float)center.GetX(), (float)center.GetY(), (float)center.GetZ()));
  546. }
  547. if (channels & AZ::Debug::ArcBallControllerChannel_Pan)
  548. {
  549. AZ::Vector3 pan = AZ::Vector3::CreateZero();
  550. AZ::Debug::ArcBallControllerRequestBus::EventResult(pan, m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequests::GetPan);
  551. ReportScriptableAction(AZStd::string::format("ArcBallCameraController_SetPan(Vector3(%f, %f, %f))", (float)pan.GetX(), (float)pan.GetY(), (float)pan.GetZ()));
  552. }
  553. if (channels & AZ::Debug::ArcBallControllerChannel_Heading)
  554. {
  555. float heading = 0.0;
  556. AZ::Debug::ArcBallControllerRequestBus::EventResult(heading, m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequests::GetHeading);
  557. ReportScriptableAction(AZStd::string::format("ArcBallCameraController_SetHeading(DegToRad(%f))", AZ::RadToDeg(heading)));
  558. }
  559. if (channels & AZ::Debug::ArcBallControllerChannel_Pitch)
  560. {
  561. float pitch = 0.0;
  562. AZ::Debug::ArcBallControllerRequestBus::EventResult(pitch, m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequests::GetPitch);
  563. ReportScriptableAction(AZStd::string::format("ArcBallCameraController_SetPitch(DegToRad(%f))", AZ::RadToDeg(pitch)));
  564. }
  565. if (channels & AZ::Debug::ArcBallControllerChannel_Distance)
  566. {
  567. float distance = 0.0;
  568. AZ::Debug::ArcBallControllerRequestBus::EventResult(distance, m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequests::GetDistance);
  569. ReportScriptableAction(AZStd::string::format("ArcBallCameraController_SetDistance(%f)", distance));
  570. }
  571. }
  572. if (controllerTypeId == azrtti_typeid<AZ::Debug::NoClipControllerComponent>())
  573. {
  574. if (channels & AZ::Debug::NoClipControllerChannel_Position)
  575. {
  576. AZ::Vector3 position = AZ::Vector3::CreateZero();
  577. AZ::Debug::NoClipControllerRequestBus::EventResult(position, m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequests::GetPosition);
  578. ReportScriptableAction(AZStd::string::format("NoClipCameraController_SetPosition(Vector3(%f, %f, %f))", (float)position.GetX(), (float)position.GetY(), (float)position.GetZ()));
  579. }
  580. if (channels & AZ::Debug::NoClipControllerChannel_Orientation)
  581. {
  582. float heading = 0.0;
  583. AZ::Debug::NoClipControllerRequestBus::EventResult(heading, m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequests::GetHeading);
  584. ReportScriptableAction(AZStd::string::format("NoClipCameraController_SetHeading(DegToRad(%f))", AZ::RadToDeg(heading)));
  585. float pitch = 0.0;
  586. AZ::Debug::NoClipControllerRequestBus::EventResult(pitch, m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequests::GetPitch);
  587. ReportScriptableAction(AZStd::string::format("NoClipCameraController_SetPitch(DegToRad(%f))", AZ::RadToDeg(pitch)));
  588. }
  589. if (channels & AZ::Debug::NoClipControllerChannel_Fov)
  590. {
  591. float fov = 0.0;
  592. AZ::Debug::NoClipControllerRequestBus::EventResult(fov, m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequests::GetFov);
  593. ReportScriptableAction(AZStd::string::format("NoClipCameraController_SetFov(DegToRad(%f))", AZ::RadToDeg(fov)));
  594. }
  595. }
  596. }
  597. void ScriptManager::ReflectScriptContext(AZ::BehaviorContext* behaviorContext)
  598. {
  599. AZ::MathReflect(behaviorContext);
  600. // Utilities...
  601. behaviorContext->Method("RunScript", &Script_RunScript);
  602. behaviorContext->Method("Error", &Script_Error);
  603. behaviorContext->Method("Warning", &Script_Warning);
  604. behaviorContext->Method("Print", &Script_Print);
  605. behaviorContext->Method("IdleFrames", &Script_IdleFrames);
  606. behaviorContext->Method("IdleSeconds", &Script_IdleSeconds);
  607. behaviorContext->Method("LockFrameTime", &Script_LockFrameTime);
  608. behaviorContext->Method("UnlockFrameTime", &Script_UnlockFrameTime);
  609. behaviorContext->Method("ResizeViewport", &Script_ResizeViewport);
  610. behaviorContext->Method("SetShowImGui", &Script_SetShowImGui);
  611. behaviorContext->Method("ExecuteConsoleCommand", &Script_ExecuteConsoleCommand);
  612. // Utilities returning data (these special functions do return data because they don't read dynamic state)...
  613. behaviorContext->Method("ResolvePath", &Script_ResolvePath);
  614. behaviorContext->Method("NormalizePath", &Script_NormalizePath);
  615. behaviorContext->Method("DegToRad", &Script_DegToRad);
  616. behaviorContext->Method("GetRenderApiName", &Script_GetRenderApiName);
  617. behaviorContext->Method("GetRandomTestSeed", &Script_GetRandomTestSeed);
  618. // Samples...
  619. behaviorContext->Method("OpenSample", &Script_OpenSample);
  620. behaviorContext->Method("SetImguiValue", &Script_SetImguiValue);
  621. // Debug profilers...
  622. behaviorContext->Method("ShowTool", &Script_ShowTool);
  623. // Screenshots...
  624. behaviorContext->Method("SelectImageComparisonToleranceLevel", &Script_SelectImageComparisonToleranceLevel);
  625. behaviorContext->Method("CaptureScreenshot", &Script_CaptureScreenshot);
  626. behaviorContext->Method("CaptureScreenshotWithImGui", &Script_CaptureScreenshotWithImGui);
  627. behaviorContext->Method("CaptureScreenshotWithPreview", &Script_CaptureScreenshotWithPreview);
  628. behaviorContext->Method("CapturePassAttachment", &Script_CapturePassAttachment);
  629. // Profiling data...
  630. behaviorContext->Method("CapturePassTimestamp", &Script_CapturePassTimestamp);
  631. behaviorContext->Method("CapturePassPipelineStatistics", &Script_CapturePassPipelineStatistics);
  632. behaviorContext->Method("CaptureCpuProfilingStatistics", &Script_CaptureCpuProfilingStatistics);
  633. // Camera...
  634. behaviorContext->Method("ArcBallCameraController_SetCenter", &Script_ArcBallCameraController_SetCenter);
  635. behaviorContext->Method("ArcBallCameraController_SetPan", &Script_ArcBallCameraController_SetPan);
  636. behaviorContext->Method("ArcBallCameraController_SetDistance", &Script_ArcBallCameraController_SetDistance);
  637. behaviorContext->Method("ArcBallCameraController_SetHeading", &Script_ArcBallCameraController_SetHeading);
  638. behaviorContext->Method("ArcBallCameraController_SetPitch", &Script_ArcBallCameraController_SetPitch);
  639. behaviorContext->Method("NoClipCameraController_SetPosition", &Script_NoClipCameraController_SetPosition);
  640. behaviorContext->Method("NoClipCameraController_SetHeading", &Script_NoClipCameraController_SetHeading);
  641. behaviorContext->Method("NoClipCameraController_SetPitch", &Script_NoClipCameraController_SetPitch);
  642. behaviorContext->Method("NoClipCameraController_SetFov", &Script_NoClipCameraController_SetFov);
  643. // Asset System...
  644. AZ::BehaviorParameterOverrides expectedCountDetails = {"expectedCount", "Expected number of asset jobs; default=1", aznew AZ::BehaviorDefaultValue(1u)};
  645. const AZStd::array<AZ::BehaviorParameterOverrides, 2> assetTrackingExpectAssetArgs = {{ AZ::BehaviorParameterOverrides{}, expectedCountDetails }};
  646. behaviorContext->Method("AssetTracking_Start", &Script_AssetTracking_Start);
  647. behaviorContext->Method("AssetTracking_ExpectAsset", &Script_AssetTracking_ExpectAsset, assetTrackingExpectAssetArgs);
  648. behaviorContext->Method("AssetTracking_IdleUntilExpectedAssetsFinish", &Script_AssetTracking_IdleUntilExpectedAssetsFinish);
  649. behaviorContext->Method("AssetTracking_Stop", &Script_AssetTracking_Stop);
  650. }
  651. void ScriptManager::Script_Error(const AZStd::string& message)
  652. {
  653. auto func = [message]()
  654. {
  655. ReportScriptError(message.c_str());
  656. };
  657. s_instance->m_scriptOperations.push(AZStd::move(func));
  658. }
  659. void ScriptManager::Script_Warning(const AZStd::string& message)
  660. {
  661. auto func = [message]()
  662. {
  663. ReportScriptWarning(message.c_str());
  664. };
  665. s_instance->m_scriptOperations.push(AZStd::move(func));
  666. }
  667. void ScriptManager::Script_Print(const AZStd::string& message)
  668. {
  669. auto func = [message]()
  670. {
  671. AZ_TracePrintf("Automation", "Script: %s\n", message.c_str());
  672. };
  673. s_instance->m_scriptOperations.push(AZStd::move(func));
  674. }
  675. AZStd::string ScriptManager::Script_ResolvePath(const AZStd::string& path)
  676. {
  677. return Utils::ResolvePath(path);
  678. }
  679. AZStd::string ScriptManager::Script_NormalizePath(const AZStd::string& path)
  680. {
  681. AZStd::string normalizedPath = path;
  682. AzFramework::StringFunc::Path::Normalize(normalizedPath);
  683. return normalizedPath;
  684. }
  685. void ScriptManager::Script_OpenSample(const AZStd::string& sampleName)
  686. {
  687. auto operation = [sampleName]()
  688. {
  689. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  690. if (sampleName.empty())
  691. {
  692. SampleComponentManagerRequestBus::Broadcast(&SampleComponentManagerRequests::Reset);
  693. }
  694. else
  695. {
  696. bool foundSample = false;
  697. SampleComponentManagerRequestBus::BroadcastResult(foundSample, &SampleComponentManagerRequests::OpenSample, sampleName);
  698. if (foundSample)
  699. {
  700. // Samples need a few frames to initialize before consuming actions from ScriptableImGui,
  701. // so we need to wait before letting the script schedule ScriptableImGui actions.
  702. // They need 1 frame to activate, 1 frame to start ticking, and 1 frame to guarantee
  703. // that a sample OnTick occurs before a ScriptManager::OnTick. We schedule
  704. // a few extra just in case.
  705. AZ_Assert(s_instance->m_scriptIdleFrames == 0, "m_scriptIdleFrames is being stomped");
  706. s_instance->m_scriptIdleFrames = 6;
  707. }
  708. }
  709. };
  710. s_instance->m_scriptOperations.push(AZStd::move(operation));
  711. }
  712. void ScriptManager::Script_ShowTool(const AZStd::string& toolName, bool enable)
  713. {
  714. auto operation = [toolName, enable]()
  715. {
  716. bool foundTool = false;
  717. SampleComponentManagerRequestBus::BroadcastResult(foundTool, &SampleComponentManagerRequests::ShowTool, toolName, enable);
  718. AZ_Warning("ScriptManager", foundTool, "Can't find [%s] tool", toolName.c_str());
  719. };
  720. s_instance->m_scriptOperations.push(AZStd::move(operation));
  721. }
  722. void ScriptManager::Script_RunScript(const AZStd::string& scriptFilePath)
  723. {
  724. // Unlike other Script_ callback functions, we process immediately instead of pushing onto the m_scriptOperations queue.
  725. // This function is special because running the script is what adds more commands onto the m_scriptOperations queue.
  726. s_instance->ExecuteScript(scriptFilePath);
  727. }
  728. void ScriptManager::Script_IdleFrames(int numFrames)
  729. {
  730. auto operation = [numFrames]()
  731. {
  732. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  733. AZ_Assert(s_instance->m_scriptIdleFrames == 0, "m_scriptIdleFrames is being stomped");
  734. s_instance->m_scriptIdleFrames = numFrames;
  735. };
  736. s_instance->m_scriptOperations.push(AZStd::move(operation));
  737. }
  738. void ScriptManager::Script_IdleSeconds(float numSeconds)
  739. {
  740. auto operation = [numSeconds]()
  741. {
  742. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  743. s_instance->m_scriptIdleSeconds = numSeconds;
  744. };
  745. s_instance->m_scriptOperations.push(AZStd::move(operation));
  746. }
  747. void ScriptManager::Script_LockFrameTime(float seconds)
  748. {
  749. auto operation = [seconds]()
  750. {
  751. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  752. AZ::Interface<AZ::IConsole>::Get()->PerformCommand(AZStd::string::format("t_frameTimeOverride %f", seconds).c_str());
  753. s_instance->m_frameTimeIsLocked = true;
  754. };
  755. s_instance->m_scriptOperations.push(AZStd::move(operation));
  756. }
  757. void ScriptManager::Script_UnlockFrameTime()
  758. {
  759. auto operation = []()
  760. {
  761. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  762. AZ::Interface<AZ::IConsole>::Get()->PerformCommand("t_frameTimeOverride 0");
  763. s_instance->m_frameTimeIsLocked = false;
  764. };
  765. s_instance->m_scriptOperations.push(AZStd::move(operation));
  766. }
  767. void ScriptManager::Script_SetImguiValue(AZ::ScriptDataContext& dc)
  768. {
  769. if (dc.GetNumArguments() != 2)
  770. {
  771. ReportScriptError("Wrong number of arguments for SetImguiValue");
  772. return;
  773. }
  774. if (!dc.IsString(0))
  775. {
  776. ReportScriptError("SetImguiValue first argument must be a string");
  777. return;
  778. }
  779. const char* fieldName = nullptr;
  780. dc.ReadArg(0, fieldName);
  781. AZStd::string fieldNameString = fieldName; // Because the lambda will need to capture a copy of something, not a pointer
  782. if (dc.IsBoolean(1))
  783. {
  784. bool value = false;
  785. dc.ReadArg(1, value);
  786. auto func = [fieldNameString, value]()
  787. {
  788. ScriptableImGui::SetBool(fieldNameString, value);
  789. };
  790. s_instance->m_scriptOperations.push(AZStd::move(func));
  791. }
  792. else if (dc.IsNumber(1))
  793. {
  794. float value = 0.0f;
  795. dc.ReadArg(1, value);
  796. auto func = [fieldNameString, value]()
  797. {
  798. ScriptableImGui::SetNumber(fieldNameString, value);
  799. };
  800. s_instance->m_scriptOperations.push(AZStd::move(func));
  801. }
  802. else if (dc.IsString(1))
  803. {
  804. const char* value = nullptr;
  805. dc.ReadArg(1, value);
  806. AZStd::string valueString = value; // Because the lambda will need to capture a copy of something, not a pointer
  807. auto func = [fieldNameString, valueString]()
  808. {
  809. ScriptableImGui::SetString(fieldNameString, valueString);
  810. };
  811. s_instance->m_scriptOperations.push(AZStd::move(func));
  812. }
  813. else if (dc.IsClass<AZ::Vector3>(1))
  814. {
  815. AZ::Vector3 value = AZ::Vector3::CreateZero();
  816. dc.ReadArg(1, value);
  817. auto func = [fieldNameString, value]()
  818. {
  819. ScriptableImGui::SetVector(fieldNameString, value);
  820. };
  821. s_instance->m_scriptOperations.push(AZStd::move(func));
  822. }
  823. else if (dc.IsClass<AZ::Vector2>(1))
  824. {
  825. AZ::Vector2 value = AZ::Vector2::CreateZero();
  826. dc.ReadArg(1, value);
  827. auto func = [fieldNameString, value]()
  828. {
  829. ScriptableImGui::SetVector(fieldNameString, value);
  830. };
  831. s_instance->m_scriptOperations.push(AZStd::move(func));
  832. }
  833. }
  834. void ScriptManager::Script_ResizeViewport(int width, int height)
  835. {
  836. auto operation = [width,height]()
  837. {
  838. if (Utils::SupportsResizeClientArea())
  839. {
  840. Utils::ResizeClientArea(width, height);
  841. }
  842. else
  843. {
  844. s_instance->ReportScriptError("ResizeViewport() is not supported on this platform");
  845. }
  846. };
  847. s_instance->m_scriptOperations.push(AZStd::move(operation));
  848. }
  849. void ScriptManager::Script_ExecuteConsoleCommand(const AZStd::string& command)
  850. {
  851. auto operation = [command]()
  852. {
  853. AzFramework::ConsoleRequestBus::Broadcast(&AzFramework::ConsoleRequests::ExecuteConsoleCommand, command.c_str());
  854. };
  855. s_instance->m_scriptOperations.push(AZStd::move(operation));
  856. }
  857. void ScriptManager::SetShowImGui(bool show)
  858. {
  859. m_prevShowImGui = m_showImGui;
  860. if (show)
  861. {
  862. AZ::Render::ImGuiSystemRequestBus::Broadcast(&AZ::Render::ImGuiSystemRequestBus::Events::ShowAllImGuiPasses);
  863. }
  864. else
  865. {
  866. AZ::Render::ImGuiSystemRequestBus::Broadcast(&AZ::Render::ImGuiSystemRequestBus::Events::HideAllImGuiPasses);
  867. }
  868. m_showImGui = show;
  869. }
  870. void ScriptManager::Script_SetShowImGui(bool show)
  871. {
  872. auto operation = [show]()
  873. {
  874. s_instance->SetShowImGui(show);
  875. };
  876. s_instance->m_scriptOperations.push(AZStd::move(operation));
  877. }
  878. bool ScriptManager::PrepareForScreenCapture(const AZStd::string& path)
  879. {
  880. if (!Utils::IsFileUnderFolder(Utils::ResolvePath(path), ScreenshotPaths::GetScreenshotsFolder(true)))
  881. {
  882. // The main reason we require screenshots to be in a specific folder is to ensure we don't delete or replace some other important file.
  883. ReportScriptError(AZStd::string::format(
  884. "Screenshots must be captured under the '%s' folder. Attempted to save screenshot to '%s'.",
  885. ScreenshotPaths::GetScreenshotsFolder(false).c_str(), path.c_str()));
  886. return false;
  887. }
  888. // Delete the file if it already exists because if the screen capture fails, we don't want to do a screenshot comparison test using an old screenshot.
  889. if (AZ::IO::LocalFileIO::GetInstance()->Exists(path.c_str()) && !AZ::IO::LocalFileIO::GetInstance()->Remove(path.c_str()))
  890. {
  891. ReportScriptError(AZStd::string::format("Failed to delete existing screenshot file '%s'.", path.c_str()));
  892. return false;
  893. }
  894. s_instance->m_scriptReporter.AddScreenshotTest(path);
  895. s_instance->m_isCapturePending = true;
  896. s_instance->AZ::Render::FrameCaptureNotificationBus::Handler::BusConnect();
  897. s_instance->PauseScript();
  898. return true;
  899. }
  900. void ScriptManager::Script_SelectImageComparisonToleranceLevel(const AZStd::string& presetName)
  901. {
  902. auto operation = [presetName]()
  903. {
  904. s_instance->m_imageComparisonOptions.SelectToleranceLevel(presetName);
  905. };
  906. s_instance->m_scriptOperations.push(AZStd::move(operation));
  907. }
  908. void ScriptManager::Script_CaptureScreenshot(const AZStd::string& filePath)
  909. {
  910. Script_SetShowImGui(false);
  911. auto operation = [filePath]()
  912. {
  913. // Note this will pause the script until the capture is complete
  914. if (PrepareForScreenCapture(filePath))
  915. {
  916. AZ::Render::FrameCaptureRequestBus::Broadcast(&AZ::Render::FrameCaptureRequestBus::Events::CaptureScreenshot, filePath);
  917. }
  918. };
  919. s_instance->m_scriptOperations.push(AZStd::move(operation));
  920. s_instance->m_scriptOperations.push([]()
  921. {
  922. s_instance->m_scriptReporter.CheckLatestScreenshot(s_instance->m_imageComparisonOptions.GetCurrentToleranceLevel());
  923. });
  924. // restore imgui show/hide
  925. s_instance->m_scriptOperations.push([]()
  926. {
  927. s_instance->SetShowImGui(s_instance->m_prevShowImGui);
  928. });
  929. }
  930. void ScriptManager::Script_CaptureScreenshotWithImGui(const AZStd::string& filePath)
  931. {
  932. Script_SetShowImGui(true);
  933. auto operation = [filePath]()
  934. {
  935. // Note this will pause the script until the capture is complete
  936. if (PrepareForScreenCapture(filePath))
  937. {
  938. AZ::Render::FrameCaptureRequestBus::Broadcast(&AZ::Render::FrameCaptureRequestBus::Events::CaptureScreenshot, filePath);
  939. }
  940. };
  941. s_instance->m_scriptOperations.push(AZStd::move(operation));
  942. s_instance->m_scriptOperations.push([]()
  943. {
  944. s_instance->m_scriptReporter.CheckLatestScreenshot(s_instance->m_imageComparisonOptions.GetCurrentToleranceLevel());
  945. });
  946. // restore imgui show/hide
  947. s_instance->m_scriptOperations.push([]()
  948. {
  949. s_instance->SetShowImGui(s_instance->m_prevShowImGui);
  950. });
  951. }
  952. void ScriptManager::Script_CaptureScreenshotWithPreview(const AZStd::string& filePath)
  953. {
  954. auto operation = [filePath]()
  955. {
  956. // Note this will pause the script until the capture is complete
  957. if (PrepareForScreenCapture(filePath))
  958. {
  959. AZ::Render::FrameCaptureRequestBus::Broadcast(&AZ::Render::FrameCaptureRequestBus::Events::CaptureScreenshotWithPreview, filePath);
  960. }
  961. };
  962. s_instance->m_scriptOperations.push(AZStd::move(operation));
  963. s_instance->m_scriptOperations.push([]()
  964. {
  965. s_instance->m_scriptReporter.CheckLatestScreenshot(s_instance->m_imageComparisonOptions.GetCurrentToleranceLevel());
  966. });
  967. }
  968. void ScriptManager::Script_CapturePassAttachment(AZ::ScriptDataContext& dc)
  969. {
  970. if (dc.GetNumArguments() != 3)
  971. {
  972. ReportScriptError("CapturePassAttachment needs three arguments");
  973. return;
  974. }
  975. if (!dc.IsTable(0))
  976. {
  977. ReportScriptError("CapturePassAttachment's first argument must be a table of strings");
  978. return;
  979. }
  980. if (!dc.IsString(1) || !dc.IsString(2))
  981. {
  982. ReportScriptError("CapturePassAttachment's second and third argument must be strings");
  983. return;
  984. }
  985. const char* stringValue = nullptr;
  986. AZStd::vector<AZStd::string> passHierarchy;
  987. AZStd::string slot;
  988. AZStd::string outputFilePath;
  989. // read slot name and output file path
  990. dc.ReadArg(1, stringValue);
  991. slot = AZStd::string(stringValue);
  992. dc.ReadArg(2, stringValue);
  993. outputFilePath = AZStd::string(stringValue);
  994. // read pass hierarchy
  995. AZ::ScriptDataContext stringtable;
  996. dc.InspectTable(0, stringtable);
  997. const char* fieldName;
  998. int fieldIndex;
  999. int elementIndex;
  1000. while (stringtable.InspectNextElement(elementIndex, fieldName, fieldIndex))
  1001. {
  1002. if (fieldIndex != -1)
  1003. {
  1004. if (!stringtable.IsString(elementIndex))
  1005. {
  1006. ReportScriptError("CapturePassAttachment's first argument must contain only strings");
  1007. return;
  1008. }
  1009. const char* stringTableValue = nullptr;
  1010. if (stringtable.ReadValue(elementIndex, stringTableValue))
  1011. {
  1012. passHierarchy.push_back(stringTableValue);
  1013. }
  1014. }
  1015. }
  1016. auto operation = [passHierarchy, slot, outputFilePath]()
  1017. {
  1018. // Note this will pause the script until the capture is complete
  1019. if (PrepareForScreenCapture(outputFilePath))
  1020. {
  1021. AZ::Render::FrameCaptureRequestBus::Broadcast(&AZ::Render::FrameCaptureRequestBus::Events::CapturePassAttachment, passHierarchy, slot, outputFilePath);
  1022. }
  1023. };
  1024. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1025. s_instance->m_scriptOperations.push([]()
  1026. {
  1027. s_instance->m_scriptReporter.CheckLatestScreenshot(s_instance->m_imageComparisonOptions.GetCurrentToleranceLevel());
  1028. });
  1029. }
  1030. void ScriptManager::OnCaptureFinished(AZ::Render::FrameCaptureResult result, const AZStd::string &info)
  1031. {
  1032. m_isCapturePending = false;
  1033. AZ::Render::FrameCaptureNotificationBus::Handler::BusDisconnect();
  1034. ResumeScript();
  1035. // This is checking for the exact scenario that results from an HDR setup. The goal is to add a very specific and prominent message that will
  1036. // alert users to a common issue and what action to take. Any other Format issues will be reported by FrameCaptureSystemComponent with a
  1037. // "Can't save image with format %s to a ppm file" message.
  1038. if (result == AZ::Render::FrameCaptureResult::UnsupportedFormat && info.find(AZ::RHI::ToString(AZ::RHI::Format::R10G10B10A2_UNORM)) != AZStd::string::npos)
  1039. {
  1040. m_messageBox.OpenPopupMessage("HDR Not Supported", "Screen capture testing is not supported in HDR. Please change the system configuration to disable the HDR display feature.");
  1041. AbortScripts("Script(s) aborted due to HDR configuration.");
  1042. }
  1043. }
  1044. void ScriptManager::OnCaptureQueryTimestampFinished([[maybe_unused]] bool result, [[maybe_unused]] const AZStd::string& info)
  1045. {
  1046. m_isCapturePending = false;
  1047. AZ::Render::ProfilingCaptureNotificationBus::Handler::BusDisconnect();
  1048. ResumeScript();
  1049. }
  1050. void ScriptManager::OnCaptureQueryPipelineStatisticsFinished([[maybe_unused]] bool result, [[maybe_unused]] const AZStd::string& info)
  1051. {
  1052. m_isCapturePending = false;
  1053. AZ::Render::ProfilingCaptureNotificationBus::Handler::BusDisconnect();
  1054. ResumeScript();
  1055. }
  1056. void ScriptManager::OnCaptureCpuProfilingStatisticsFinished([[maybe_unused]] bool result, [[maybe_unused]] const AZStd::string& info)
  1057. {
  1058. m_isCapturePending = false;
  1059. AZ::Render::ProfilingCaptureNotificationBus::Handler::BusDisconnect();
  1060. ResumeScript();
  1061. }
  1062. void ScriptManager::Script_CapturePassTimestamp(AZ::ScriptDataContext& dc)
  1063. {
  1064. AZStd::string outputFilePath;
  1065. const bool readScriptDataContext = ValidateProfilingCaptureScripContexts(dc, outputFilePath);
  1066. if (!readScriptDataContext)
  1067. {
  1068. return;
  1069. }
  1070. auto operation = [outputFilePath]()
  1071. {
  1072. s_instance->m_isCapturePending = true;
  1073. s_instance->AZ::Render::ProfilingCaptureNotificationBus::Handler::BusConnect();
  1074. s_instance->PauseScript();
  1075. AZ::Render::ProfilingCaptureRequestBus::Broadcast(&AZ::Render::ProfilingCaptureRequestBus::Events::CapturePassTimestamp, outputFilePath);
  1076. };
  1077. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1078. }
  1079. void ScriptManager::Script_CapturePassPipelineStatistics(AZ::ScriptDataContext& dc)
  1080. {
  1081. AZStd::string outputFilePath;
  1082. const bool readScriptDataContext = ValidateProfilingCaptureScripContexts(dc, outputFilePath);
  1083. if (!readScriptDataContext)
  1084. {
  1085. return;
  1086. }
  1087. auto operation = [outputFilePath]()
  1088. {
  1089. s_instance->m_isCapturePending = true;
  1090. s_instance->AZ::Render::ProfilingCaptureNotificationBus::Handler::BusConnect();
  1091. s_instance->PauseScript();
  1092. AZ::Render::ProfilingCaptureRequestBus::Broadcast(&AZ::Render::ProfilingCaptureRequestBus::Events::CapturePassPipelineStatistics, outputFilePath);
  1093. };
  1094. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1095. }
  1096. void ScriptManager::Script_CaptureCpuProfilingStatistics(AZ::ScriptDataContext& dc)
  1097. {
  1098. AZStd::string outputFilePath;
  1099. const bool readScriptDataContext = ValidateProfilingCaptureScripContexts(dc, outputFilePath);
  1100. if (!readScriptDataContext)
  1101. {
  1102. return;
  1103. }
  1104. auto operation = [outputFilePath]()
  1105. {
  1106. s_instance->m_isCapturePending = true;
  1107. s_instance->AZ::Render::ProfilingCaptureNotificationBus::Handler::BusConnect();
  1108. s_instance->PauseScript();
  1109. AZ::Render::ProfilingCaptureRequestBus::Broadcast(&AZ::Render::ProfilingCaptureRequestBus::Events::CaptureCpuProfilingStatistics, outputFilePath);
  1110. };
  1111. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1112. }
  1113. bool ScriptManager::ValidateProfilingCaptureScripContexts(AZ::ScriptDataContext& dc, AZStd::string& outputFilePath)
  1114. {
  1115. if (dc.GetNumArguments() != 1)
  1116. {
  1117. ReportScriptError("ProfilingCaptureScriptDataContext needs one argument");
  1118. return false;
  1119. }
  1120. if (!dc.IsString(0))
  1121. {
  1122. ReportScriptError("ProfilingCaptureScriptDataContext's first (and only) argument must be of type string");
  1123. return false;
  1124. }
  1125. // Read slot name and output file path
  1126. const char* stringValue = nullptr;
  1127. dc.ReadArg(0, stringValue);
  1128. if (stringValue == nullptr)
  1129. {
  1130. ReportScriptError("ProfilingCaptureScriptDataContext failed to read the string value");
  1131. return false;
  1132. }
  1133. outputFilePath = AZStd::string(stringValue);
  1134. return true;
  1135. }
  1136. float ScriptManager::Script_DegToRad(float degrees)
  1137. {
  1138. return AZ::DegToRad(degrees);
  1139. }
  1140. AZStd::string ScriptManager::Script_GetRenderApiName()
  1141. {
  1142. AZ::RPI::RPISystemInterface* rpiSystem = AZ::RPI::RPISystemInterface::Get();
  1143. return rpiSystem->GetRenderApiName().GetCStr();
  1144. }
  1145. int ScriptManager::Script_GetRandomTestSeed()
  1146. {
  1147. return s_instance->m_testSuiteRunConfig.m_randomSeed;
  1148. }
  1149. void ScriptManager::CheckArcBallControllerHandler()
  1150. {
  1151. if (0 == AZ::Debug::ArcBallControllerRequestBus::GetNumOfEventHandlers(s_instance->m_cameraEntity->GetId()))
  1152. {
  1153. ReportScriptError("There is no handler for ArcBallControllerRequestBus for the camera entity.");
  1154. }
  1155. }
  1156. void ScriptManager::CheckNoClipControllerHandler()
  1157. {
  1158. if (0 == AZ::Debug::NoClipControllerRequestBus::GetNumOfEventHandlers(s_instance->m_cameraEntity->GetId()))
  1159. {
  1160. ReportScriptError("There is no handler for NoClipControllerRequestBus for the camera entity.");
  1161. }
  1162. }
  1163. void ScriptManager::Script_ArcBallCameraController_SetCenter(AZ::Vector3 center)
  1164. {
  1165. auto operation = [center]()
  1166. {
  1167. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1168. CheckArcBallControllerHandler();
  1169. AZ::Debug::ArcBallControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetCenter, center);
  1170. };
  1171. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1172. }
  1173. void ScriptManager::Script_ArcBallCameraController_SetPan(AZ::Vector3 pan)
  1174. {
  1175. auto operation = [pan]()
  1176. {
  1177. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1178. CheckArcBallControllerHandler();
  1179. AZ::Debug::ArcBallControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetPan, pan);
  1180. };
  1181. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1182. }
  1183. void ScriptManager::Script_ArcBallCameraController_SetDistance(float distance)
  1184. {
  1185. auto operation = [distance]()
  1186. {
  1187. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1188. CheckArcBallControllerHandler();
  1189. AZ::Debug::ArcBallControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetDistance, distance);
  1190. };
  1191. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1192. }
  1193. void ScriptManager::Script_ArcBallCameraController_SetHeading(float heading)
  1194. {
  1195. auto operation = [heading]()
  1196. {
  1197. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1198. CheckArcBallControllerHandler();
  1199. AZ::Debug::ArcBallControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetHeading, heading);
  1200. };
  1201. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1202. }
  1203. void ScriptManager::Script_ArcBallCameraController_SetPitch(float pitch)
  1204. {
  1205. auto operation = [pitch]()
  1206. {
  1207. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1208. CheckArcBallControllerHandler();
  1209. AZ::Debug::ArcBallControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetPitch, pitch);
  1210. };
  1211. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1212. }
  1213. void ScriptManager::Script_NoClipCameraController_SetPosition(AZ::Vector3 position)
  1214. {
  1215. auto operation = [position]()
  1216. {
  1217. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1218. CheckNoClipControllerHandler();
  1219. AZ::Debug::NoClipControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequestBus::Events::SetPosition, position);
  1220. };
  1221. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1222. }
  1223. void ScriptManager::Script_NoClipCameraController_SetHeading(float heading)
  1224. {
  1225. auto operation = [heading]()
  1226. {
  1227. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1228. CheckNoClipControllerHandler();
  1229. AZ::Debug::NoClipControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequestBus::Events::SetHeading, heading);
  1230. };
  1231. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1232. }
  1233. void ScriptManager::Script_NoClipCameraController_SetPitch(float pitch)
  1234. {
  1235. auto operation = [pitch]()
  1236. {
  1237. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1238. CheckNoClipControllerHandler();
  1239. AZ::Debug::NoClipControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequestBus::Events::SetPitch, pitch);
  1240. };
  1241. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1242. }
  1243. void ScriptManager::Script_NoClipCameraController_SetFov(float fov)
  1244. {
  1245. auto operation = [fov]()
  1246. {
  1247. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1248. CheckNoClipControllerHandler();
  1249. AZ::Debug::NoClipControllerRequestBus::Event(s_instance->m_cameraEntity->GetId(), &AZ::Debug::NoClipControllerRequestBus::Events::SetFov, fov);
  1250. };
  1251. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1252. }
  1253. void ScriptManager::Script_AssetTracking_Start()
  1254. {
  1255. auto operation = []()
  1256. {
  1257. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1258. s_instance->m_assetStatusTracker.StartTracking();
  1259. };
  1260. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1261. }
  1262. void ScriptManager::Script_AssetTracking_ExpectAsset(const AZStd::string& sourceAssetPath, uint32_t expectedCount)
  1263. {
  1264. auto operation = [sourceAssetPath, expectedCount]()
  1265. {
  1266. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1267. s_instance->m_assetStatusTracker.ExpectAsset(sourceAssetPath, expectedCount);
  1268. };
  1269. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1270. }
  1271. void ScriptManager::Script_AssetTracking_IdleUntilExpectedAssetsFinish(float timeout)
  1272. {
  1273. auto operation = [timeout]()
  1274. {
  1275. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1276. AZ_Assert(!s_instance->m_waitForAssetTracker, "It shouldn't be possible to run the next command until m_waitForAssetTracker is false");
  1277. s_instance->m_waitForAssetTracker = true;
  1278. s_instance->m_assetTrackingTimeout = timeout;
  1279. };
  1280. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1281. }
  1282. void ScriptManager::Script_AssetTracking_Stop()
  1283. {
  1284. auto operation = []()
  1285. {
  1286. AZ_DEBUG_STATIC_MEMEBER(instance, s_instance);
  1287. s_instance->m_assetStatusTracker.StopTracking();
  1288. };
  1289. s_instance->m_scriptOperations.push(AZStd::move(operation));
  1290. }
  1291. } // namespace AtomSampleViewer